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);
for (auto& n: v) cin >> n;
int l = 1, r = N - 2;
ll ans = 0;
while (r > l) {
while (v[l] > v[l - 1]) l++;
while (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';
}
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... |