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;
#define ll long long
#define ar array
#define int long long
const int N = 5e5 + 20;
const int INF = 1e17;
const int MOD = 8;
const int X = 4000;
const int LOG = 21;
//#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
//#pragma GCC optimize("O3")
signed main(){ios_base::sync_with_stdio(false);cin.tie(0);
int n;
cin>>n;
int A[n];
for(int i = 0;i < n;i++)cin>>A[i];
int up[n], dwn[n];
up[0] = dwn[n-1] = 0;
for(int i = 1;i < n;i++)up[i] = up[i-1] + max(0ll, A[i-1] - A[i] + 1);
for(int i = n- 2;i >= 0;i--)dwn[i] = dwn[i + 1] + max(0ll, A[i+1] - A[i] + 1);
int ans = INF;
for(int i = 0;i < n;i++)ans = min(ans, up[i] + dwn[i]);
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |