This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
//#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define pb push_back
#define ss second
#define ff first
#define vt vector
#define uid(a, b) uniform_int_distribution<int>(a, b)(mt)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 2e9;
const int mod = 1e9+7;
const int maxn = 1e6;
mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
void solve() {
int n;
cin >> n;
ll a[n], p[n], s[n], x[n], y[n];
for(int i = 0; i < n; i++){
cin >> a[i];
p[i] = s[i] = 0;
}
for(int i = 0; i < n; i++){
if(i)p[i] = p[i - 1] + max(0ll, a[i - 1] + 1 - a[i]);
x[i] = a[i] + p[i];
}
ll t = 0;
for(int i = n - 1; i >= 0; i--){
t = max(t, a[i]);
if((i + 1) < n)s[i] = s[i + 1] + max(0ll, a[i + 1] + 1 - a[i]);
y[i] = t++;
}
ll ans = min(p[n - 1], s[0]);
for(int i = 0; i < n - 1; i++){
ans = min(max(p[i], s[i + 1]) + (p[i] == s[i + 1] && x[i] == y[i + 1]), ans);
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int times = 1; //cin >> times;
for(int i = 1; i <= times; i++) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |