思路:dp。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a) const int N=1e5+;
ll dp[N]={};
ll dp1[N]={};
ll a[N];
ll t[N]={};
ll t1[N]={};
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int n;
cin>>n;
for(int i=;i<=n;i++)cin>>a[i]; t[]=a[];
for(int i=;i<=n;i++)
{
if(a[i]<=t[i-])
{
dp[i]=dp[i-]+t[i-]-a[i]+;
t[i]=t[i-]+;
}
else
{
t[i]=a[i];
dp[i]=dp[i-];
}
} t1[n]=a[n];
for(int i=n-;i>=;i--)
{
if(a[i]<=t1[i+])
{
dp1[i]=dp1[i+]+t1[i+]-a[i]+;
t1[i]=t1[i+]+;
}
else
{
t1[i]=a[i];
dp1[i]=dp1[i+];
}
} ll ans=min(dp1[],dp[n]);
for(int i=;i<=n-;i++)
ans=min(ans,dp1[i]+dp[i]-min(t[i],t1[i])+a[i]); cout<<ans<<endl;
return ;
}