This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
const int N = 200'010;
int a[N];
int d1[N], d2[N];
int n;
int main()
{
cin.tie(0) -> sync_with_stdio(false);
cin >> n;
Loop (i,0,n)
cin >> a[i];
Loop (i,0,n-1)
d1[i] = max(0, a[i] - a[i+1] + 1);
Loop (i,1,n)
d2[i] = max(0, a[i] - a[i-1] + 1);
ll s1=0, s2=0;
Loop (i,1,n)
s2 += d2[i];
ll ans = s2;
Loop (i,1,n) {
s1 += d1[i-1];
s2 -= d2[i];
ans = min(ans, max(s1, s2));
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |