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>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
long long ans = 1e18 + 7;
long long start = 0, finish = 0;
for (int i = 0; i < n - 1; ++i) {
if (a[i] <= a[i + 1]) {
finish += a[i + 1] - a[i] + 1;
}
}
for (int i = 0; i < n; ++i) {
ans = min(ans, max(start, finish));
if (i + 1 < n) {
if (a[i] >= a[i + 1]) {
start += a[i] - a[i + 1] + 1;
}
if (a[i] <= a[i + 1]) {
finish -= a[i + 1] - a[i] + 1;
}
}
}
cout << ans << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |