//#pragma GCC optimize ("O2")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define task "task"
#define task "task"
#define prll pair<ll,ll>
#define pb push_back
#define ld long double
const ll MIN=-1e18,MAX=1e18,MOD=1e9+7,nmax=200005;
ll a[nmax],h1[nmax],h2[nmax],cost1[nmax],cost2[nmax];
int main(){
//freopen (task".inp", "r", stdin);
//freopen (task".out", "w", stdout);
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
h1[1]=a[1];
for(int i=2;i<=n;i++)
{
if(a[i]<=a[i-1])
{
cost1[i]=cost1[i-1]+a[i-1]-a[i]+1;
h1[i]=h1[i-1]+1;
}
else
{
cost1[i]=cost1[i-1];
h1[i]=max(h1[i-1]+1,a[i]);
}
}
h2[n]=a[n];
for(int i=n-1;i>=1;i--)
{
if(a[i]<=a[i+1])
{
cost2[i]=cost2[i+1]+a[i+1]-a[i]+1;
h2[i]=h2[i+1]+1;
}
else
{
cost2[i]=cost2[i+1];
h2[i]=max(h2[i+1]+1,a[i]);
}
}
// for(int i=1;i<=n;i++)
// {
// cout<<cost1[i]<<" ";
// }
// cout<<endl;
// for(int i=1;i<=n;i++)
// {
// cout<<h1[i]<<" ";
// }
// cout<<endl;
// for(int i=1;i<=n;i++)
// {
// cout<<cost2[i]<<" ";
// }
// cout<<endl;
// for(int i=1;i<=n;i++)
// {
// cout<<h2[i]<<" ";
// }
// cout<<endl;
// cout<<cost1[3]<<" "<<cost2[4];
ll ans=min(cost2[1],cost1[n]);
for(int i=2;i<n;i++)
{
ans=min(ans,max(cost1[i],cost2[i+1])+(h1[i]==h2[i+1]));
}
cout<<ans;
}
/*
8
12 2 34 85 4 91 29 85
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4488 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4584 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
21 |
Correct |
1 ms |
4440 KB |
Output is correct |
22 |
Correct |
1 ms |
4568 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4488 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4584 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
21 |
Correct |
1 ms |
4440 KB |
Output is correct |
22 |
Correct |
1 ms |
4568 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
18 ms |
10044 KB |
Output is correct |
27 |
Correct |
19 ms |
10116 KB |
Output is correct |
28 |
Correct |
19 ms |
10188 KB |
Output is correct |
29 |
Correct |
18 ms |
10072 KB |
Output is correct |
30 |
Correct |
15 ms |
9308 KB |
Output is correct |
31 |
Correct |
18 ms |
10204 KB |
Output is correct |
32 |
Correct |
18 ms |
10176 KB |
Output is correct |
33 |
Correct |
18 ms |
10076 KB |
Output is correct |
34 |
Correct |
18 ms |
10068 KB |
Output is correct |
35 |
Correct |
14 ms |
9296 KB |
Output is correct |
36 |
Correct |
15 ms |
9308 KB |
Output is correct |
37 |
Correct |
15 ms |
9308 KB |
Output is correct |
38 |
Correct |
18 ms |
10196 KB |
Output is correct |
39 |
Correct |
18 ms |
10020 KB |
Output is correct |
40 |
Correct |
18 ms |
10196 KB |
Output is correct |
41 |
Correct |
18 ms |
10068 KB |
Output is correct |
42 |
Correct |
18 ms |
9820 KB |
Output is correct |
43 |
Correct |
22 ms |
10132 KB |
Output is correct |
44 |
Correct |
17 ms |
9952 KB |
Output is correct |
45 |
Correct |
18 ms |
10064 KB |
Output is correct |
46 |
Correct |
18 ms |
10200 KB |
Output is correct |
47 |
Correct |
18 ms |
10076 KB |
Output is correct |
48 |
Correct |
18 ms |
10240 KB |
Output is correct |