# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
882900 | hasan2006 | Growing Vegetables is Fun 4 (JOI21_ho_t1) | C++17 | 2 ms | 6492 KiB |
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 TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define rall(s) s.rbegin(),s.rend()
#define all(s) s.begin(),s.end()
#define pb push_back
#define se second
#define fi first
#define ll long long
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"
const int N = 5e5 + 9 , mod = 1e9 + 7;
ll a[N] = {}, b[N] , c[N] , d[N] , dp[N] , ans[N];
void solve()
{
ll q , i , j , m ,n, z ,s = 0 , f, l , r , k , x , y , mn = 1e18 , mx = 0;
cin>>n;
for(i = 1; i <= n; i++){
cin>>a[i];
}
b[1] = c[n] = 0;
for(i = 1;i <= n; i++){
mx = max(mx , a[i]);
b[i] = mx - a[i];
mx++;
}
mx = 0;
for(i = n;i >= 1; i--){
mx = max(mx , a[i]);
c[i] = mx - a[i];
mx++;
}
for(i = 1; i <= n; i++)
s += max(c[i] - c[i - 1] , 0ll);
for(i = 1; i <= n; i++){
x = max(c[i] , b[i]);
//if(i > 1){
s -= (max(c[i] - b[i - 1] , 0ll));
s += max(x - b[i - 1] , 0ll);
//}
if(i != n){
s -= max(c[i + 1] - c[i] , 0ll);
s += max(c[i + 1] - x , 0ll);
}
y = s;
k = 0;
if(i > 1)
k = b[i - 2];
//if(i == 6)
// cout<<y<<" "<<c[i + 1]<<" "<<x<<" "<<b[i - 1]<<" "<<k<<"\n";
if(b[i - 1] >= x && c[i] >= x && (b[i - 1] - x - k) >= 0)
y -= max(min(c[i + 1] - x , (b[i - 1] - x - k)) , 0ll);
mn = min(mn , y);
//cout<<y<<"\n";
//cout<<s<<" ";
//if(i > 1){
s -= max(x - b[i - 1] , 0ll);
s += max(b[i] - b[i - 1] , 0ll);
//}
if(i != n){
s -= max(c[i + 1] - x , 0ll);
s += max(c[i + 1] - b[i] , 0ll);
}
// cout<<s<<" ";
}
cout<<mn<<"\n";
}
int main(){
int t = 1;
//cin>>t;
while(t--)
{
solve();
}
}
// Author : حسن
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |