#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
#define int long long
int n;
int a[MAXN];
int pre[MAXN], suf[MAXN];
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
pre[1] = 0;
int target = a[1];
for (int i = 2; i <= n; i++)
{
target = max(target + 1, a[i]);
pre[i] = pre[i-1] + (target - a[i]);
}
suf[n] = 0;
target = a[n];
for (int i = n-1; i >= 1; i--)
{
target = max(target + 1, a[i]);
suf[i] = suf[i+1] + (target - a[i]);
}
int ans = LLONG_MAX;
for (int i = 1; i <= n; i++)
{
int cost = pre[i] + suf[i] - (max(pre[i] - (i>1 ? pre[i-1] : 0), suf[i] - (i<n ? suf[i+1] : 0)));
ans = min(ans, cost);
}
ans = LLONG_MAX;
for (int i = 1; i <= n; i++)
{
ans = min(ans, max(pre[i], suf[i]));
}
cout << ans << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |