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 int long long
using namespace std;
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (auto& el : a)
cin >> el;
int left = 0, right = 0;
for (int i = 0; i + 1 < n; ++i)
if (a[i] <= a[i + 1])
right += a[i + 1] - a[i] + 1;
int best = right;
for (int i = 1; i < n; ++i) {
if (a[i - 1] <= a[i])
right -= a[i] - a[i - 1] + 1;
if (a[i] <= a[i - 1])
left += a[i - 1] - a[i] + 1;
best = min(best, max(left, right));
}
cout << best;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |