This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define debug(x) cerr << #x << ' ' << x << '\n'
#define endl '\n'
#define printv(v) cerr << #v << ": "; for (auto& n: v) cerr << setw(3) << n << " \n"[&n==&v.back()]
using namespace std;
const int M = 1e9 + 7;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int N;
cin >> N;
vector<ll> v(N + 2);
for (int i = 1; i <= N; i++) cin >> v[i];
int l = 1, r = N;
ll ans = 0;
while (r > l) {
while (v[l] > v[l - 1] || v[r] > v[r + 1]) {
if (v[l] > v[l - 1]) l++;
if (v[r] > v[r + 1]) r--;
}
if (r <= l) break;
int d = min(v[l - 1] + 1 - v[l], v[r + 1] + 1 - v[r]);
v[l - 1] -= d;
v[r + 1] -= d;
ans += d;
//cerr << l << ' ' << r << ' ' << d << '\n';
}
if (l == r || l == r + 1) {
//cerr << "last: " << l << ' ' << r << '\n';
ans += min(v[l - 1] - v[l] + 1, v[r + 1] - v[r] + 1);
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |