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 sp " "
#define sl "\n"
#define fi first
#define se second
#define mp make_pair
#define sz(a) ((int)a.size())
#define all(a) a.begin(),a.end()
#define rev(a) reverse(a.begin(),a.end())
#define PI 3.141592
#define fo(i,n) for (int i=0;i<n;i++)
#define fofo(i,n,j,m) for (int i=0;i<n;i++) for (int j=0;j<m;j++)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
const int N=2e5+5,mod=1e9+7;
ll n,a[N],b[N],c[N];
int main() {
cin.tie(0); cout.tie(0);
ios_base::sync_with_stdio();
cin >> n;
for (int i=0;i<n;i++)
cin >> a[i];
for (int i=1;i<n;i++)
if (a[i-1]>=a[i])
b[i] = a[i-1]-a[i]+1;
for (int i=n-2;i>=0;i--)
if (a[i+1]>=a[i])
c[i] = a[i+1]-a[i]+1;
long long k = 0;
for (int i=0;i<n;i++)
k += c[i];
long long res = 1e18;
long long q = 0;
for (int i=0;i<n;i++) {
q += b[i];
res = min(res,max(q,k));
k -= c[i];
}
cout << res;
return 0;
}
// Recuerda limites
// Recuerda casos especiales
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |