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;
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int n;
cin >> n;
long long ans = 0;
vector<long long> a(n + 1);
vector<bool> b(n + 1, false);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int l = 2, r = n - 1;
while (l <= n && a[l] > a[l - 1]) l++;
while (r >= 1 && a[r] > a[r + 1]) r--;
if (l > n || r < 1) {
cout << "0\n";
return 0;
}
while (l <= r) {
long long add = min(abs(a[l] - a[l - 1]), abs(a[r] - a[r + 1])) + 1;
a[l] += add;
a[r] += add;
ans += add;
while (l <= n && a[l] > a[l - 1]) {
l++;
if (!b[l]) {
b[l] = true;
a[l] += ans;
}
}
while (r >= 1 && a[r] > a[r + 1]) {
r--;
if (!b[r]) {
b[r] = true;
a[r] += ans;
}
}
}
if (l <= n && r >= 1 && a[l] == a[r]) ans++;
cout << ans << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |