#include <bits/stdc++.h>
using namespace std;
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define INF 1023456789
#define LINF 1023456789123456789
#define lb(x,v) lower_bound(all(x),v)-x.begin()
#define disc(x) sort(all(x)),x.resize(unique(all(x))-x.begin())
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll,int> li;
#define maxn 500005
struct node{
int s,e,m,lz;ii v;
node *l,*r;
node(int _s,int _e){
s=_s,e=_e,m=(s+e)>>1,lz=0;v={1,s};
if(s!=e)l=new node(s,m),r=new node(m+1,e);
}
void ppo(){
v.se+=lz;
if(s!=e)l->lz+=lz,r->lz+=lz;
lz=0;
}
void add(int x,int y,int nv){
if(s==x&&e==y){lz+=nv;return;}
if(y<=m)l->add(x,y,nv);
else if(x>m)r->add(x,y,nv);
else l->add(x,m,nv),r->add(m+1,y,nv);
l->ppo(),r->ppo();
v=min(l->v,r->v);
}
inline void flip(int x){
if(s==x&&e==x){v.fi=1-v.fi;return;}
if(x<=m)l->flip(x);
else r->flip(x);
l->ppo(),r->ppo();
v=min(l->v,r->v);
}
ii qry(int x,int y){
ppo();
if(s==x&&e==y)return v;
if(y<=m)return l->qry(x,y);
else if(x>m)return r->qry(x,y);
else return min(l->qry(x,m),r->qry(m+1,y));
}
}*rt;
int n,a[maxn],l[maxn],r[maxn],pos[maxn];
vector<ii> cyc[maxn],stuff;
int dbg=0;
int md;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int main(){
if(rng()%2)md=-1;
else md=1;
sf("%d",&n);
for(int i=0;i<n;++i)sf("%d",&a[i]);
l[0]=(a[0]==-1)?0:-1;
for(int i=1;i<n;++i){
if(a[i]==-1)l[i]=i;
else l[i]=l[i-1];
}
r[n-1]=(a[n-1]==-1)?n-1:n;
for(int i=n-2;i>=0;--i){
if(a[i]==-1)r[i]=i;
else r[i]=r[i+1];
}
for(int i=0;i<n;++i){
if(a[i]>0){
int d=(r[i]==n?INF:r[i])-i;
if(l[i]!=-1)d=min(d,i-l[i]);
cyc[r[i]].pb({d,md*i});
}
}
rt=new node(0,n-1);
for(int i=0;i<=n;++i){
if(cyc[i].empty())continue;
sort(all(cyc[i]));
int x=md*cyc[i].back().se;
--a[x];
pos[i]=x;
//need to reach here at x time
rt->flip(x);
if(dbg)pf("walk: %d\n",x);
}
for(int i=0;i<n;++i){
if(a[i]>0){
int d=INF;
if(l[i]!=-1)d=min(d,i-l[i]);
d=min(d,(r[i]==n?INF:r[i])-i);
stuff.pb({d,i});
}
}
sort(all(stuff));
for(auto[d,i]:stuff){
int num=a[i];
ii mn=rt->qry(i,n-1);
if(mn.fi!=1)num=min(num,mn.se/(2*d));
if(dbg)pf("d: %d\n",d);
if(dbg)pf("mn: %d %d\n",mn.fi,mn.se);
int rem=rt->qry(i,i).se;
int cur=rem/(2*d);
if(rem>=0&&i<pos[r[i]])++cur;
if(dbg)pf("rem %d, cur %d, %d %d\n",rem,cur,i,pos[r[i]]);
num=min(num,cur);
if(dbg)pf("take %d of %d\n\n",num,i);
a[i]-=num;
if(num>0){
if(i!=pos[r[i]])rt->flip(i);
rt->add(i,n-1,-num*d*2);
}
}
ll ans=0;
for(int i=0;i<n;++i){
if(a[i]>0)ans+=a[i];
}
pf("%lld\n",ans);
}
/*
5
-1 1 1 1 1
2
8
-1 1 0 0 -1 0 0 3
1
8
2 -1 2 -1 2 -1 2 -1
1
5
-1 -1 3 3 3
*/
Compilation message
tortoise.cpp: In function 'int main()':
tortoise.cpp:68:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | sf("%d",&n);
| ^
tortoise.cpp:69:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | for(int i=0;i<n;++i)sf("%d",&a[i]);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11996 KB |
Output is correct |
2 |
Correct |
7 ms |
12080 KB |
Output is correct |
3 |
Correct |
7 ms |
11988 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
8 ms |
12056 KB |
Output is correct |
6 |
Correct |
7 ms |
12088 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
7 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
6 ms |
11988 KB |
Output is correct |
14 |
Correct |
7 ms |
11988 KB |
Output is correct |
15 |
Correct |
7 ms |
11988 KB |
Output is correct |
16 |
Correct |
6 ms |
11988 KB |
Output is correct |
17 |
Correct |
7 ms |
11988 KB |
Output is correct |
18 |
Correct |
6 ms |
11988 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
6 ms |
11988 KB |
Output is correct |
21 |
Correct |
8 ms |
11988 KB |
Output is correct |
22 |
Correct |
6 ms |
11988 KB |
Output is correct |
23 |
Correct |
7 ms |
11988 KB |
Output is correct |
24 |
Correct |
7 ms |
11988 KB |
Output is correct |
25 |
Correct |
7 ms |
11988 KB |
Output is correct |
26 |
Correct |
6 ms |
11988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11996 KB |
Output is correct |
2 |
Correct |
7 ms |
12080 KB |
Output is correct |
3 |
Correct |
7 ms |
11988 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
8 ms |
12056 KB |
Output is correct |
6 |
Correct |
7 ms |
12088 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
7 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
6 ms |
11988 KB |
Output is correct |
14 |
Correct |
7 ms |
11988 KB |
Output is correct |
15 |
Correct |
7 ms |
11988 KB |
Output is correct |
16 |
Correct |
6 ms |
11988 KB |
Output is correct |
17 |
Correct |
7 ms |
11988 KB |
Output is correct |
18 |
Correct |
6 ms |
11988 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
6 ms |
11988 KB |
Output is correct |
21 |
Correct |
8 ms |
11988 KB |
Output is correct |
22 |
Correct |
6 ms |
11988 KB |
Output is correct |
23 |
Correct |
7 ms |
11988 KB |
Output is correct |
24 |
Correct |
7 ms |
11988 KB |
Output is correct |
25 |
Correct |
7 ms |
11988 KB |
Output is correct |
26 |
Correct |
6 ms |
11988 KB |
Output is correct |
27 |
Correct |
7 ms |
11988 KB |
Output is correct |
28 |
Correct |
7 ms |
12116 KB |
Output is correct |
29 |
Correct |
7 ms |
12004 KB |
Output is correct |
30 |
Correct |
7 ms |
12116 KB |
Output is correct |
31 |
Correct |
7 ms |
11988 KB |
Output is correct |
32 |
Correct |
7 ms |
12012 KB |
Output is correct |
33 |
Correct |
7 ms |
12096 KB |
Output is correct |
34 |
Correct |
7 ms |
12028 KB |
Output is correct |
35 |
Correct |
7 ms |
12116 KB |
Output is correct |
36 |
Correct |
9 ms |
11988 KB |
Output is correct |
37 |
Correct |
7 ms |
12024 KB |
Output is correct |
38 |
Correct |
7 ms |
12112 KB |
Output is correct |
39 |
Correct |
7 ms |
12116 KB |
Output is correct |
40 |
Correct |
7 ms |
12116 KB |
Output is correct |
41 |
Correct |
8 ms |
12116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11996 KB |
Output is correct |
2 |
Correct |
7 ms |
12080 KB |
Output is correct |
3 |
Correct |
7 ms |
11988 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
8 ms |
12056 KB |
Output is correct |
6 |
Correct |
7 ms |
12088 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
7 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
6 ms |
11988 KB |
Output is correct |
14 |
Correct |
7 ms |
11988 KB |
Output is correct |
15 |
Correct |
7 ms |
11988 KB |
Output is correct |
16 |
Correct |
6 ms |
11988 KB |
Output is correct |
17 |
Correct |
7 ms |
11988 KB |
Output is correct |
18 |
Correct |
6 ms |
11988 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
6 ms |
11988 KB |
Output is correct |
21 |
Correct |
8 ms |
11988 KB |
Output is correct |
22 |
Correct |
6 ms |
11988 KB |
Output is correct |
23 |
Correct |
7 ms |
11988 KB |
Output is correct |
24 |
Correct |
7 ms |
11988 KB |
Output is correct |
25 |
Correct |
7 ms |
11988 KB |
Output is correct |
26 |
Correct |
6 ms |
11988 KB |
Output is correct |
27 |
Correct |
7 ms |
11988 KB |
Output is correct |
28 |
Correct |
7 ms |
12116 KB |
Output is correct |
29 |
Correct |
7 ms |
12004 KB |
Output is correct |
30 |
Correct |
7 ms |
12116 KB |
Output is correct |
31 |
Correct |
7 ms |
11988 KB |
Output is correct |
32 |
Correct |
7 ms |
12012 KB |
Output is correct |
33 |
Correct |
7 ms |
12096 KB |
Output is correct |
34 |
Correct |
7 ms |
12028 KB |
Output is correct |
35 |
Correct |
7 ms |
12116 KB |
Output is correct |
36 |
Correct |
9 ms |
11988 KB |
Output is correct |
37 |
Correct |
7 ms |
12024 KB |
Output is correct |
38 |
Correct |
7 ms |
12112 KB |
Output is correct |
39 |
Correct |
7 ms |
12116 KB |
Output is correct |
40 |
Correct |
7 ms |
12116 KB |
Output is correct |
41 |
Correct |
8 ms |
12116 KB |
Output is correct |
42 |
Correct |
7 ms |
12100 KB |
Output is correct |
43 |
Correct |
7 ms |
11988 KB |
Output is correct |
44 |
Correct |
6 ms |
12052 KB |
Output is correct |
45 |
Correct |
8 ms |
11988 KB |
Output is correct |
46 |
Correct |
7 ms |
12108 KB |
Output is correct |
47 |
Correct |
7 ms |
11988 KB |
Output is correct |
48 |
Correct |
7 ms |
11988 KB |
Output is correct |
49 |
Correct |
7 ms |
11988 KB |
Output is correct |
50 |
Correct |
7 ms |
12064 KB |
Output is correct |
51 |
Correct |
7 ms |
12120 KB |
Output is correct |
52 |
Correct |
6 ms |
11988 KB |
Output is correct |
53 |
Correct |
7 ms |
12116 KB |
Output is correct |
54 |
Correct |
7 ms |
12116 KB |
Output is correct |
55 |
Correct |
6 ms |
12116 KB |
Output is correct |
56 |
Correct |
6 ms |
12116 KB |
Output is correct |
57 |
Correct |
7 ms |
12092 KB |
Output is correct |
58 |
Correct |
7 ms |
11988 KB |
Output is correct |
59 |
Correct |
6 ms |
12116 KB |
Output is correct |
60 |
Correct |
6 ms |
12116 KB |
Output is correct |
61 |
Correct |
7 ms |
11988 KB |
Output is correct |
62 |
Incorrect |
7 ms |
12116 KB |
Output isn't correct |
63 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11996 KB |
Output is correct |
2 |
Correct |
7 ms |
12080 KB |
Output is correct |
3 |
Correct |
7 ms |
11988 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
8 ms |
12056 KB |
Output is correct |
6 |
Correct |
7 ms |
12088 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
7 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
6 ms |
11988 KB |
Output is correct |
14 |
Correct |
7 ms |
11988 KB |
Output is correct |
15 |
Correct |
7 ms |
11988 KB |
Output is correct |
16 |
Correct |
6 ms |
11988 KB |
Output is correct |
17 |
Correct |
7 ms |
11988 KB |
Output is correct |
18 |
Correct |
6 ms |
11988 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
6 ms |
11988 KB |
Output is correct |
21 |
Correct |
8 ms |
11988 KB |
Output is correct |
22 |
Correct |
6 ms |
11988 KB |
Output is correct |
23 |
Correct |
7 ms |
11988 KB |
Output is correct |
24 |
Correct |
7 ms |
11988 KB |
Output is correct |
25 |
Correct |
7 ms |
11988 KB |
Output is correct |
26 |
Correct |
6 ms |
11988 KB |
Output is correct |
27 |
Correct |
7 ms |
11988 KB |
Output is correct |
28 |
Correct |
7 ms |
12116 KB |
Output is correct |
29 |
Correct |
7 ms |
12004 KB |
Output is correct |
30 |
Correct |
7 ms |
12116 KB |
Output is correct |
31 |
Correct |
7 ms |
11988 KB |
Output is correct |
32 |
Correct |
7 ms |
12012 KB |
Output is correct |
33 |
Correct |
7 ms |
12096 KB |
Output is correct |
34 |
Correct |
7 ms |
12028 KB |
Output is correct |
35 |
Correct |
7 ms |
12116 KB |
Output is correct |
36 |
Correct |
9 ms |
11988 KB |
Output is correct |
37 |
Correct |
7 ms |
12024 KB |
Output is correct |
38 |
Correct |
7 ms |
12112 KB |
Output is correct |
39 |
Correct |
7 ms |
12116 KB |
Output is correct |
40 |
Correct |
7 ms |
12116 KB |
Output is correct |
41 |
Correct |
8 ms |
12116 KB |
Output is correct |
42 |
Correct |
7 ms |
12100 KB |
Output is correct |
43 |
Correct |
7 ms |
11988 KB |
Output is correct |
44 |
Correct |
6 ms |
12052 KB |
Output is correct |
45 |
Correct |
8 ms |
11988 KB |
Output is correct |
46 |
Correct |
7 ms |
12108 KB |
Output is correct |
47 |
Correct |
7 ms |
11988 KB |
Output is correct |
48 |
Correct |
7 ms |
11988 KB |
Output is correct |
49 |
Correct |
7 ms |
11988 KB |
Output is correct |
50 |
Correct |
7 ms |
12064 KB |
Output is correct |
51 |
Correct |
7 ms |
12120 KB |
Output is correct |
52 |
Correct |
6 ms |
11988 KB |
Output is correct |
53 |
Correct |
7 ms |
12116 KB |
Output is correct |
54 |
Correct |
7 ms |
12116 KB |
Output is correct |
55 |
Correct |
6 ms |
12116 KB |
Output is correct |
56 |
Correct |
6 ms |
12116 KB |
Output is correct |
57 |
Correct |
7 ms |
12092 KB |
Output is correct |
58 |
Correct |
7 ms |
11988 KB |
Output is correct |
59 |
Correct |
6 ms |
12116 KB |
Output is correct |
60 |
Correct |
6 ms |
12116 KB |
Output is correct |
61 |
Correct |
7 ms |
11988 KB |
Output is correct |
62 |
Incorrect |
7 ms |
12116 KB |
Output isn't correct |
63 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11996 KB |
Output is correct |
2 |
Correct |
7 ms |
12080 KB |
Output is correct |
3 |
Correct |
7 ms |
11988 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
8 ms |
12056 KB |
Output is correct |
6 |
Correct |
7 ms |
12088 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
7 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
6 ms |
11988 KB |
Output is correct |
14 |
Correct |
7 ms |
11988 KB |
Output is correct |
15 |
Correct |
7 ms |
11988 KB |
Output is correct |
16 |
Correct |
6 ms |
11988 KB |
Output is correct |
17 |
Correct |
7 ms |
11988 KB |
Output is correct |
18 |
Correct |
6 ms |
11988 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
6 ms |
11988 KB |
Output is correct |
21 |
Correct |
8 ms |
11988 KB |
Output is correct |
22 |
Correct |
6 ms |
11988 KB |
Output is correct |
23 |
Correct |
7 ms |
11988 KB |
Output is correct |
24 |
Correct |
7 ms |
11988 KB |
Output is correct |
25 |
Correct |
7 ms |
11988 KB |
Output is correct |
26 |
Correct |
6 ms |
11988 KB |
Output is correct |
27 |
Correct |
7 ms |
11988 KB |
Output is correct |
28 |
Correct |
7 ms |
12116 KB |
Output is correct |
29 |
Correct |
7 ms |
12004 KB |
Output is correct |
30 |
Correct |
7 ms |
12116 KB |
Output is correct |
31 |
Correct |
7 ms |
11988 KB |
Output is correct |
32 |
Correct |
7 ms |
12012 KB |
Output is correct |
33 |
Correct |
7 ms |
12096 KB |
Output is correct |
34 |
Correct |
7 ms |
12028 KB |
Output is correct |
35 |
Correct |
7 ms |
12116 KB |
Output is correct |
36 |
Correct |
9 ms |
11988 KB |
Output is correct |
37 |
Correct |
7 ms |
12024 KB |
Output is correct |
38 |
Correct |
7 ms |
12112 KB |
Output is correct |
39 |
Correct |
7 ms |
12116 KB |
Output is correct |
40 |
Correct |
7 ms |
12116 KB |
Output is correct |
41 |
Correct |
8 ms |
12116 KB |
Output is correct |
42 |
Correct |
7 ms |
12100 KB |
Output is correct |
43 |
Correct |
7 ms |
11988 KB |
Output is correct |
44 |
Correct |
6 ms |
12052 KB |
Output is correct |
45 |
Correct |
8 ms |
11988 KB |
Output is correct |
46 |
Correct |
7 ms |
12108 KB |
Output is correct |
47 |
Correct |
7 ms |
11988 KB |
Output is correct |
48 |
Correct |
7 ms |
11988 KB |
Output is correct |
49 |
Correct |
7 ms |
11988 KB |
Output is correct |
50 |
Correct |
7 ms |
12064 KB |
Output is correct |
51 |
Correct |
7 ms |
12120 KB |
Output is correct |
52 |
Correct |
6 ms |
11988 KB |
Output is correct |
53 |
Correct |
7 ms |
12116 KB |
Output is correct |
54 |
Correct |
7 ms |
12116 KB |
Output is correct |
55 |
Correct |
6 ms |
12116 KB |
Output is correct |
56 |
Correct |
6 ms |
12116 KB |
Output is correct |
57 |
Correct |
7 ms |
12092 KB |
Output is correct |
58 |
Correct |
7 ms |
11988 KB |
Output is correct |
59 |
Correct |
6 ms |
12116 KB |
Output is correct |
60 |
Correct |
6 ms |
12116 KB |
Output is correct |
61 |
Correct |
7 ms |
11988 KB |
Output is correct |
62 |
Incorrect |
7 ms |
12116 KB |
Output isn't correct |
63 |
Halted |
0 ms |
0 KB |
- |