• References
• the meaning of problems
From the array of length n, extracted $ \ frac {n} {3} $ nonadjacent maximum value such that the sum (both can not be taken at the same time)
•answer
Select the current maximum greedy $ a_ {max} $, while deletion comprising $ a_ {max} $ including the left (A_L $ $), and the right thereof (A_R $ $) of these three digits;
However, it is also possible that $ a_l + a_r> a_ {max} $, then we need to choose $ a_l, a_r $ these two numbers, without selecting $ a_ {max} $ this number;
Therefore, we also need to $ a_l + a_r-a_ {max} $ into sequences to be considered;
Each time its left and its right to delete the number, may be two arrays $ L, R $ mark to record the current at its lower left and right positions thereof;
•Code
View Code1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define pll pair<ll ,ll > 5 #define F first 6 #define S second 7 const int maxn=1e5+50; 8 9 int n; 10 ll a[maxn]; 11 int L[maxn]; 12 int R[maxn]; 13 set<pll >_set; 14 15 void Del(int x) 16 { 17 _set.erase({a[x],x}); 18 L[R[x]]=L[x]; 19 R[L[x]]=R[x]; 20 } 21 ll Solve() 22 { 23 for(int i=1;i <= n;++i) 24 { 25 L[i]=(i-1+n-1)%n+1; 26 R[i]=i%n+1; 27 _set.insert({a[i],i}); 28 } 29 30 ll ans=0; 31 for(int i=1;i <= n/3;++i) 32 { 33 ans += _set.rbegin()->F; 34 35 int x=_set.rbegin()->S; 36 ll b=a[L[x]]; 37 ll c=a[x]; 38 ll d=a[R[x]]; 39 40 Del(L[x]); 41 Del(R[x]); 42 _set.erase({a[x],x}); 43 44 a[x]=b+d-c; 45 _set.insert({a[x],x}); 46 } 47 return ans; 48 } 49 int main() 50 { 51 scanf("%d",&n); 52 for(int i=1;i <= n;++i) 53 scanf("%lld",a+i); 54 printf("%lld\n",Solve()); 55 56 return 0; 57 }