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 all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define endl '\n'
#define pb push_back
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ii = pair<int,int>;
using iii = tuple<int,int,int>;
const int inf = 2e9+1;
const int mod = 1e9+7;
const int maxn = 3e5+100;
template<typename X, typename Y> bool ckmin(X& x, const Y& y) { return (y < x) ? (x=y,1):0; }
template<typename X, typename Y> bool ckmax(X& x, const Y& y) { return (x < y) ? (x=y,1):0; }
pair<ll,ll> suf[maxn], pref[maxn];
int main(){_
int n;cin>>n;
vector<ll>v(n);
for(auto&x:v)cin>>x;
ll cur = 0, usando = 0;
for(int i=1;i<=n;++i){
ll need = max(0ll, cur - v[i-1] + 1);
pref[i].first = pref[i-1].first + max(0ll, need - usando);
usando = need;
pref[i].second = usando;
ckmax(cur, max(v[i-1], cur+1));
}
cur = 0, usando = 0;
for(int i=n;i>=1;--i){
ll need = max(0ll, cur - v[i-1] + 1);
suf[i].first = suf[i+1].first + max(0ll, need - usando);
usando = need;
suf[i].second = usando;
ckmax(cur, max(v[i-1], cur+1));
}
ll resp = min(suf[1].first, pref[n].first);
for(int i=1;i<=n;++i){
ll need = max({max((i!=1?v[i-2]:0)+pref[i-1].second, (i!=n?v[i]:0)+ suf[i+1].second) - v[i-1] + 1, suf[i+1].second, pref[i-1].second});
ckmin(resp, need + pref[i-1].first + suf[i+1].first - min(need, pref[i-1].second) - min(need, suf[i+1].second));
}
cout<<resp<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |