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;
#define int long long
typedef pair<int, int> pii;
const int MXN = 200005;
int n, a[MXN], d[MXN];
int getANS() {
int l = 0, r = n - 2, ans = 0;
while (l < n - 1 && d[l] > 0) l++;
while (r >= 0 && d[r] < 0) r--;
if (l == n - 1 || r == -1) return 0;
while (l < r) {
int small = min(1 - d[l], d[r] + 1);
d[l] += small;
d[r] -= small;
ans += small;
while (l < n - 1 && d[l] > 0) l++;
while (r >= 0 && d[r] < 0) r--;
}
if (l == r && d[l] == 0) ans++;
return ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i < n; i++) d[i - 1] = a[i] - a[i - 1];
cout << getANS() << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |