E. Paint the Middle
time limit per test
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou are given elements numbered from to , the element has value and color , initially, for all .
The following operation can be applied:
- Select three elements , and (), such that , and are all equal to and , then set .
Find the maximum value of that can be obtained after applying the given operation any number of times.
Input
The first line contains an integer () — the number of elements.
The second line consists of integers (), where is the value of the -th element.
Output
Print a single integer in a line — the maximum value of that can be obtained after applying the given operation any number of times.
Note
In the first test, it is possible to apply the following operations in order:
Solution:
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- ll gcd(ll a,ll b)
- {
- if(a>b)
- return gcd(b,a);
- if(b%a==0)
- return a;
- return gcd(a,b%a);
- }
- void solve()
- {
- ll n;
- cin>>n;
- vector<ll>a(n);
- vector<ll>b(n);
- for(ll i=0;i<n;i++)
- {
- cin>>a[i];
- }
- for(ll i=0;i<n;i++)
- {
- cin>>b[i];
- }
- ll mxa=0,mxb=0;
- for(ll i=0;i<n;i++)
- {
- ll mn=min(a[i],b[i]);
- ll mx=max(a[i],b[i]);
- mxa=max(mxa,mx);
- mxb=max(mxb,mn);
- }
- cout<<mxa*mxb<<endl;
- }
- int main()
- {
- ll t; cin>>t;
- while(t--)
- {
- solve();
- }
- }
No comments:
Post a Comment