#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#define int ll
const int N=1e6+99,S=2,inf=1e16;
int n,t,ans,a[N],res[N],ps[N][S],L[N],R[N];
set<pair<int,pair<int,int> > > s;
void Add(int l,int r){
if(r>n || l<1) return ;
int res=(ps[r][1]-ps[l-1][0])-(ps[r][0]-ps[l-1][1]);
s.insert({res,{l,r}});
}
void del(int l,int r){
if(r>n || l<1) return ;
int res=(ps[r][1]-ps[l-1][0])-(ps[r][0]-ps[l-1][1]);
s.erase({res,{l,r}});
}
main(){
cin>>n;
f(i,2,n+2){
cin>>a[i];
}
n+=2;
a[1]=-inf;
a[n]=-inf;
f(i,1,n+1){
L[i]=R[i]=i;
s.insert({a[i],{L[i],R[i]}});
ps[i][0]=ps[i-1][1];
ps[i][1]=ps[i-1][0]+a[i];
}
f(t,0,(n+1)/2-1){
pair<int,pair<int,int> > p=*s.rbegin();
int copy,l=p.S.F-1,r=p.S.S+1;
R[l+1]=0,L[r-1]=0;
s.erase(p);
ans+=p.F;
copy=l;
del(L[l],l);
l=L[l];
L[copy]=0,R[copy]=0;
R[l]=r;
copy=r;
del(r,R[r]);
r=R[r];
L[copy]=0,R[copy]=0;
L[r]=l;
Add(l,r);
R[l]=r;
L[r]=l;
cout<<ans<<" ";
}
}
Compilation message
candies.cpp:39:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
39 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
3 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
448 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
2 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
3 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
448 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
2 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
460 KB |
Output is correct |
21 |
Correct |
378 ms |
22328 KB |
Output is correct |
22 |
Correct |
322 ms |
22064 KB |
Output is correct |
23 |
Correct |
430 ms |
22208 KB |
Output is correct |
24 |
Correct |
188 ms |
22084 KB |
Output is correct |
25 |
Correct |
185 ms |
22100 KB |
Output is correct |
26 |
Correct |
178 ms |
22100 KB |
Output is correct |
27 |
Correct |
195 ms |
22084 KB |
Output is correct |
28 |
Correct |
227 ms |
22096 KB |
Output is correct |
29 |
Correct |
198 ms |
22072 KB |
Output is correct |
30 |
Correct |
221 ms |
22084 KB |
Output is correct |
31 |
Correct |
208 ms |
22136 KB |
Output is correct |
32 |
Correct |
211 ms |
22096 KB |
Output is correct |
33 |
Correct |
278 ms |
22040 KB |
Output is correct |
34 |
Correct |
258 ms |
22120 KB |
Output is correct |
35 |
Correct |
263 ms |
22072 KB |
Output is correct |
36 |
Correct |
347 ms |
22120 KB |
Output is correct |
37 |
Correct |
347 ms |
22076 KB |
Output is correct |
38 |
Correct |
329 ms |
22048 KB |
Output is correct |
39 |
Correct |
181 ms |
22156 KB |
Output is correct |
40 |
Correct |
175 ms |
22148 KB |
Output is correct |
41 |
Correct |
184 ms |
22036 KB |
Output is correct |
42 |
Correct |
211 ms |
22096 KB |
Output is correct |
43 |
Correct |
196 ms |
22140 KB |
Output is correct |
44 |
Correct |
196 ms |
22084 KB |
Output is correct |
45 |
Correct |
218 ms |
22072 KB |
Output is correct |
46 |
Correct |
244 ms |
22096 KB |
Output is correct |
47 |
Correct |
204 ms |
22108 KB |
Output is correct |
48 |
Correct |
273 ms |
22148 KB |
Output is correct |
49 |
Correct |
256 ms |
22056 KB |
Output is correct |
50 |
Correct |
263 ms |
22164 KB |
Output is correct |