#include<bits/stdc++.h>
#define ll long long
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int n;cin>>n;
ll a[n+1];
for(int i=1;i<=n;i++)cin>>a[i];
ll dpl[n+1];dpl[1]=0;
for(int i=2;i<=n;i++)dpl[i]=dpl[i-1]+(a[i-1]-a[i]>=0?a[i-1]-a[i]+1:0);
ll dpr[n+2]={0};dpr[n]=0;
for(int i=n-1;i>=1;i--)dpr[i]=dpr[i+1]+(a[i+1]-a[i]>=0?a[i+1]-a[i]+1:0);
ll ans=2e18;
for(int i=1;i<=n;i++)ans=min(ans,max(dpl[i],dpr[i]));
ll now[n+1];
for(int i=1;i<=n;i++)now[i]=a[i];
dpr[n]=0;ll cur=n-2,add=0;
queue<pii>q;
for(int i=n-1;i>=1;i--){
while(!q.empty()&&q.front().f>i)add-=q.front().s,q.pop();
now[i]+=add;
dpr[i]=dpr[i+1]+max(now[i+1]-now[i]+1,1ll*0);
int tt=max(now[i+1]-now[i]+1,1ll*0);
now[i]+=tt;
while(cur>0&&now[cur]<=now[i])q.push({cur--,tt}),add+=tt;
}
for(int i=1;i<=n-1;i++)ans=min(ans,dpl[i]+dpr[i+1]+max(now[i+1]-a[i]-dpl[i]+1,1ll*0));
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
848 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
848 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
23 ms |
11452 KB |
Output is correct |
27 |
Correct |
23 ms |
11604 KB |
Output is correct |
28 |
Correct |
23 ms |
11928 KB |
Output is correct |
29 |
Correct |
23 ms |
11856 KB |
Output is correct |
30 |
Correct |
19 ms |
11088 KB |
Output is correct |
31 |
Correct |
22 ms |
8532 KB |
Output is correct |
32 |
Correct |
23 ms |
11856 KB |
Output is correct |
33 |
Correct |
24 ms |
11864 KB |
Output is correct |
34 |
Correct |
21 ms |
8532 KB |
Output is correct |
35 |
Correct |
19 ms |
9300 KB |
Output is correct |
36 |
Correct |
20 ms |
11088 KB |
Output is correct |
37 |
Correct |
19 ms |
11100 KB |
Output is correct |
38 |
Correct |
24 ms |
11848 KB |
Output is correct |
39 |
Correct |
23 ms |
11860 KB |
Output is correct |
40 |
Correct |
23 ms |
11892 KB |
Output is correct |
41 |
Correct |
23 ms |
11860 KB |
Output is correct |
42 |
Correct |
23 ms |
11608 KB |
Output is correct |
43 |
Correct |
23 ms |
11856 KB |
Output is correct |
44 |
Correct |
22 ms |
11860 KB |
Output is correct |
45 |
Correct |
25 ms |
11868 KB |
Output is correct |
46 |
Correct |
23 ms |
11868 KB |
Output is correct |
47 |
Correct |
24 ms |
11900 KB |
Output is correct |
48 |
Correct |
23 ms |
11868 KB |
Output is correct |