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>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
vector<ll> v(n);
for(ll &x: v) cin >> x;
auto vv = v;
vector<pair<ll, ll>> prf(n), sff(n);
vector<ll> prf1(n), sff1(n);
prf[0] = {0, 0};
prf1[0] = v[0];
for(int i = 1; i < n; i++){
ll ls = prf[i-1].sc;
prf[i].f = prf[i-1].f, prf[i].sc = 0;
ll x = v[i-1] - v[i] + 1;
x = max(x, 0LL);
prf[i].f+=x;
prf[i].sc = ls+x;
prf1[i] = max(v[i], v[i]+ls+x);
}
v = vv;
sff[n-1] = {0, 0};
sff1[n-1] = v[n-1];
for(int i = n-2; i >= 0; i--){
ll ls = sff[i+1].sc;
sff[i].f = sff[i+1].f, sff[i].sc = 0;
ll x = v[i+1] - v[i] + 1;
x = max(x, 0LL);
sff[i].f+=x;
sff[i].sc = ls+x;
sff1[i] = max(v[i], v[i]+ls+x);
}
v = vv;
ll ans = 1e18;
for(int i = 0; i < n; i++){
ll cur = 0;
ll x1 = 0, y1 = 0, x2 = 0, y2 = 0;
tie(x1, x2) = prf[i];
if(i!=n-1){
tie(y1, y2) = sff[i+1];
}
cur+=x1+y1-min(x2, y2);
if(i!=n-1 && prf1[i] == sff1[i+1]) cur++;
ans = min(ans, cur);
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |