#include "bits/stdc++.h"
using namespace std;
#define int int64_t
#define pb push_back
using pii=pair<int,int>;
const int lim=2e5+100;
int mod=1e9+7;
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Local
freopen(".in","r",stdin);freopen(".out","w",stdout);
#endif
int n;
cin>>n;
int a[n],b[n],ll[n]{},rr[n]{};
for(int i=0;i<n;i++){cin>>a[i];b[i]=a[i];}
priority_queue<pii>pq;
for(int i=0;i<n;i++){
pq.push({a[i],i});
}
int cnt=0;
int pref[n];
pref[0]=a[0];
if(n)pref[1]=a[1];
for(int i=2;i<n;i++){
pref[i]=pref[i-2]+a[i];
}
auto f=[&](int l,int r){
return pref[r]-(1<l?pref[l-2]:int(0));
};
set<pii>st;
int ans=0;
for(int i=1;i<=(n+1)/2;i++){
while(pq.size()){
auto[val,ind]=pq.top();
pq.pop();
if(val!=b[ind])continue;
auto p=st.lower_bound({ind,ind}),q=(p==st.begin())?st.end():prev(p);
int l=ind,r=ind;
if(q!=st.end()){
if(q->first<=ind&&ind<=q->second)continue;
if(!q->first&&ind-1==q->second)continue;
}
if(p!=st.end()){
if(p->first<=ind&&ind<=p->second)continue;
}
//cerr<<val<<" "<<ind<<"\n";
if(p!=st.end()&&p->first<=ind+2){
if(p->first==ind+1&&p->second==n-1)continue;
r=p->second;
st.erase(p);
}
if(q!=st.end()&&ind-2<=q->second){
//cerr<<q->first<<" "<<q->second<<"\n";
l=q->first;
st.erase(q);
}
//cerr<<l<<" "<<r<<" lr\n";
if((r&1)!=(ind&1)){
r++;
if(n<=r)r-=2;
}
if((l&1)!=(ind&1)){
l--;
if(l<0)l-=2;
}
p=st.lower_bound({r,0});
while(p!=st.end()&&p->first==r+2){
r=p->second;
st.erase(p);
p=st.lower_bound({r,0});
}
p=st.lower_bound({r,0});
while(p!=st.begin()&&(--p)->second==l-2){
l=p->first;
st.erase(p);
p=st.lower_bound({r,0});
}
//cerr<<ind<<" "<<val<<" using \n";
ans+=val;
//cerr<<l<<" "<<r<<" lr\n";
st.insert({l,r});
if(l&&r+1<n){
int k=f(l,r)-((l+1<n)?f(l+1,r+1<n?r+1:r-1):0);
ll[l-1]=k;
b[l-1]=a[l-1]-ll[l-1]-rr[l-1];
pq.push({b[l-1],l-1});
k=f(l,r)-((0<=r-1)?f(0<=l-1?l-1:l+1,r-1):0);
rr[r+1]=k;
b[r+1]=a[r+1]-ll[r+1]-rr[r+1];
pq.push({b[r+1],r+1});
}
break;
}
cout<<ans<<"\n";
/*
for(int i:b){
cerr<<i<<" ";
}cerr<<"\n";
cerr<<"ranges:\n";
for(auto[f,s]:st){
cerr<<f<<" "<<s<<"\n";
}cerr<<"\n";
*/
}
}
Compilation message
candies.cpp: In function 'int main()':
candies.cpp:25:9: warning: unused variable 'cnt' [-Wunused-variable]
25 | int cnt=0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
135 ms |
17344 KB |
Output is correct |
22 |
Correct |
143 ms |
17412 KB |
Output is correct |
23 |
Correct |
158 ms |
17416 KB |
Output is correct |
24 |
Correct |
81 ms |
15676 KB |
Output is correct |
25 |
Correct |
80 ms |
15884 KB |
Output is correct |
26 |
Correct |
110 ms |
15856 KB |
Output is correct |
27 |
Correct |
95 ms |
17340 KB |
Output is correct |
28 |
Correct |
93 ms |
17412 KB |
Output is correct |
29 |
Correct |
90 ms |
17284 KB |
Output is correct |
30 |
Correct |
117 ms |
17340 KB |
Output is correct |
31 |
Correct |
90 ms |
17344 KB |
Output is correct |
32 |
Correct |
90 ms |
17344 KB |
Output is correct |
33 |
Correct |
112 ms |
17092 KB |
Output is correct |
34 |
Correct |
135 ms |
17340 KB |
Output is correct |
35 |
Correct |
145 ms |
17344 KB |
Output is correct |
36 |
Correct |
133 ms |
17420 KB |
Output is correct |
37 |
Correct |
136 ms |
17348 KB |
Output is correct |
38 |
Correct |
137 ms |
17524 KB |
Output is correct |
39 |
Correct |
85 ms |
15808 KB |
Output is correct |
40 |
Correct |
82 ms |
15804 KB |
Output is correct |
41 |
Correct |
80 ms |
15764 KB |
Output is correct |
42 |
Correct |
107 ms |
17448 KB |
Output is correct |
43 |
Correct |
112 ms |
17416 KB |
Output is correct |
44 |
Correct |
89 ms |
17344 KB |
Output is correct |
45 |
Correct |
92 ms |
17340 KB |
Output is correct |
46 |
Correct |
98 ms |
17292 KB |
Output is correct |
47 |
Correct |
96 ms |
17344 KB |
Output is correct |
48 |
Correct |
109 ms |
17256 KB |
Output is correct |
49 |
Correct |
117 ms |
17292 KB |
Output is correct |
50 |
Correct |
144 ms |
17364 KB |
Output is correct |