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>
#include<fstream>
using namespace std;
ifstream fin("FEEDING.INP");
ofstream fout("FEEDING.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
using u128 = __uint128_t;
//const int x[4] = {1, -1, 0, 0};
// int y[4] = {0, 0, 1, -1};
const ll mod = 1e15 + 7, inf = 1e16;
const int mxn = 2e5 + 5;
int n, q;
ll a[mxn + 1], pat[mxn + 1], sat[mxn + 1], pneed[mxn + 1], sneed[mxn + 1];
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)cin >> a[i];
pat[1] = a[1];
for(int i = 2; i <= n; i++){
pat[i] = max(pat[i - 1] + 1, a[i] + pneed[i - 1]);
pneed[i] = pneed[i - 1] + (pat[i] - a[i] - pneed[i - 1]);
}
sat[n] = a[n];
for(int i = n - 1; i >= 1; i--){
sat[i] = max(sat[i + 1] + 1, a[i] + sneed[i + 1]);
sneed[i] = sneed[i + 1] + (sat[i] - a[i] - sneed[i + 1]);
}
ll ans = inf;
for(int i = 1; i <= n; i++){
ll need = max(pneed[i - 1], sneed[i + 1]);
///cout << pneed[i - 1] << " " << sneed[i + 1] << " ";
a[i] += need;
ll v1 = pat[i - 1], v2 = sat[i + 1];
//cout << v1 << " " << v2 << " " << a[i] << " " << need << " ";
if(v1 >= a[i] && v2 >= a[i]){
need += max(v1 + 1, v2 + 1) - a[i];
}else if(v1 >= a[i]){
ll more = v1 + 1 - a[i];
need += more;
}else if(v2 >= a[i]){
ll more = v2 + 1 - a[i];
need += more;
}
//cout << need << "\n";
ans = min(ans, need);
}
cout << ans;
return(0);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |