-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSUMAGCD.cpp
More file actions
58 lines (48 loc) · 1.15 KB
/
SUMAGCD.cpp
File metadata and controls
58 lines (48 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
long long maxi=-1,gcd,sz,a[100009],lf[100009],rt[100009],n,t,x;
int main()
{
cin>>t;
set<long long> s;
while(t--){
s.clear();
maxi=-1;
cin>>n;
int j=1;
for(int i=0;i<n;i++){
cin>>x;
sz=s.size();
s.insert(x);
if(sz!=s.size()){
a[j]=x;
j++;
}
}
j--;
lf[0]=-1;
rt[j+1]=-1;
lf[1]=a[1];
rt[j]=a[j];
for(int i=2;i<=j;i++){
lf[i]=__gcd(lf[i-1],a[i]);
}for(int i=j-1;i>=1;i--){
rt[i]=__gcd(rt[i+1],a[i]);
}
for(int i=2;i<j;i++){
gcd=__gcd(lf[i-1],rt[i+1]) + a[i];
maxi=max(maxi,gcd);
}
if(lf[0]==-1 && rt[2]==-1){
maxi=max(maxi,2*a[0]);
}else{
maxi=max(maxi,a[1]+rt[2]);
maxi=max(maxi,a[j]+lf[j-1]);
}
if(j==1){
maxi=max(maxi,2*a[1]);
}
cout<<maxi<<endl;
}
return 0;
}