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;
using ll = long long;
const int N = 1e6 + 66;
ll A[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int i = 1 ; i <= n ; ++ i) cin >> A[i];
int L = 1, R = n;
ll Ladd = 0, Radd = 0;
ll All = 0;
while (L < R) {
while (L < R && A[L] - Ladd < A[L + 1]) Ladd -= max(0ll, A[L] - A[L + 1] + 1), ++L;
while (L < R && A[R] - Radd < A[R - 1]) Radd -= max(0ll, A[R] - A[R - 1] + 1), --R;
if (L >= R) break;
if (L + 1 == R) {
All += A[L] == A[R] && Ladd == 0 && Radd == 0;
break;
}
ll nLadd = A[L] - Ladd - A[L + 1] + 1;
ll nRadd = A[R] - Radd - A[R - 1] + 1;
All += min(nLadd, nRadd);
if (nLadd <= nRadd) {
Radd += nLadd;
++L;
} else {
Ladd += nRadd;
--R;
}
}
cout << All;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |