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<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#define int long long
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int>v(n);
for (int i = 0 ; i < n ; i++) cin >> v[i];
int left = 0, right = n-1, ans = 0;
while (v[left+1] > v[left]) left++;
while (v[right-1] > v[right]) right--;
int lg, rg;
lg = v[left] - v[left+1];
rg = v[right] - v[right-1];
while (left < right) {
if (lg == rg) {
ans += lg + 1;
left++, right--;
while (v[left+1] > v[left]) left++;
while (v[right-1] > v[right]) right--;
lg = v[left] - v[left+1];
rg = v[right] - v[right-1];
} else if (lg > rg) {
ans += rg + 1;
lg -= (rg+1);
right--;
while (v[right-1] > v[right]) right--;
rg = v[right] - v[right-1];
} else if (rg > lg) {
ans += lg + 1;
rg -= (lg+1);
left++;
while (v[left+1] > v[left]) left++;
lg = v[left] - v[left+1];
}
//cout << ans <<" " << left << " " << right<< "\n";
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |