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 <iostream>
using namespace std;
#define ll long long
const ll inf = (ll) 1e18 + 69;
const int MAX = 2e5 + 7;
int n;
int a[MAX];
int main()
{
ios :: sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
int l = 1, r = n;
ll res = 0;
while (l < r)
{
if (a[l] < a[l+1]) l++;
else if (a[r] < a[r-1]) r--;
else if (a[l] - a[l+1] < a[r] - a[r-1])
{
int x = a[l] - a[l+1] + 1;
res += x;
l++, a[r] -= x;
}
else
{
int x = a[r] - a[r-1] + 1;
res += x;
r--, a[l] -= x;
}
}
cout << res << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |