#include<bits/stdc++.h>
using namespace std;

//debug.....................................
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
//............................................

#define MOD 	1000000007
#define INF64 	(long long)1e18
#define INF 	(int)1e9
#define ll      long long
#define ld		long double
const int SIZE = 2e5+5;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

void fastio(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}

void solveTest(){
	int n,m,k;
	cin>>n>>m>>k;
	vector<vector<int>> a(n+1,vector<int> (2,0));
	vector<int>  b(n+1);
	int s = 0;
	int c = 0;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		a[x][0] = i;
		a[x][1] = s;
		b[i] = x;
		c++;
		if(c>=k){
			s++;
			c=0;
		}
	}
	//debug(a);
	//debug(b);
	int cnt=0;
	for(int i=0;i<m;i++){
		int x;
		cin>>x;
		cnt += a[x][1]+1;
		if(x>1){
			int t = a[x][0];
			swap(a[x],a[b[a[x][0]-1]]);
			swap(b[t-1],b[t]);
		}
		//debug(a);
		//debug(b);
	}
	cout<<cnt<<endl;
	//debug(mp);

	return;

}

int main(){
	fastio();

	int t=1;
	//cin>>t;
	while(t--){
		solveTest();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

//debug.....................................
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
//............................................

#define MOD 	1000000007
#define INF64 	(long long)1e18
#define INF 	(int)1e9
#define ll      long long
#define ld		long double
const int SIZE = 2e5+5;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

void fastio(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}

void solveTest(){
	int n,m,k;
	cin>>n>>m>>k;
	vector<int> a(n+1);
	vector<int> b(n+1);
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		a[x] = i;
		b[i] = x;
	}
	//debug(a);
	//debug(b);
	ll cnt=0;
	for(int i=0;i<m;i++){
		int x;
		cin>>x;
		cnt += (a[x]+k-1)/k;
		if(a[x]!=1){
			int t = a[x];
			swap(a[x],a[b[a[x]-1]]);
			swap(b[t-1],b[t]);
		}
		//debug(a);
		//debug(b);
	}
	cout<<cnt<<endl;
	//debug(mp);

	return;

}

int main(){
	fastio();

	int t=1;
	//cin>>t;
	while(t--){
		solveTest();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

//debug.....................................
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
//............................................

#define MOD 	1000000007
#define INF64 	(long long)1e18
#define INF 	(int)1e9
#define ll      long long
#define ld		long double
const int SIZE = 2e5+5;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

void fastio(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}

void solveTest(){
	int n,m,k;
	cin>>n>>m>>k;
	vector<int> a(n+1);
	vector<int> b(n+1);
	int s = 0;
	int c = 0;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		a[x] = i;
		b[i] = x;
	}
	//debug(a);
	//debug(b);
	ll cnt=0;
	for(int i=0;i<m;i++){
		int x;
		cin>>x;
		cnt += (a[x]+k-1)/k;
		if(x>1){
			int t = a[x];
			swap(a[x],a[b[a[x]-1]]);
			swap(b[t-1],b[t]);
		}
		//debug(a);
		//debug(b);
	}
	cout<<cnt<<endl;
	//debug(mp);

	return;

}

int main(){
	fastio();

	int t=1;
	//cin>>t;
	while(t--){
		solveTest();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,k,ans=0,key;
    string s1,s,ss1,ss;
    cin>>n>>m>>k;
    getchar();
    getline(cin,s1);
    getline(cin,ss1);
    for(int i=0;i<s1.size();i+=2) s+=s1[i];
    for(int i=0;i<ss1.size();i+=2) ss+=ss1[i];
    for(int i=0;i<ss.size();i++)
    {
        key=s.find(ss[i]);
        ans+=((key+k)/k);
        if(key)  swap(s[key],s[key-1]);
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,k,ans=0,a;
    cin>>n>>m>>k;
    vector<int>v,vv;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        v.push_back(a);
    }
    for(int i=0;i<m;i++)
    {
        cin>>a;
        vv.push_back(a);
    }
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(vv[i]==v[j])
            {
                ans+=(j/k+1);
                if(j)  swap(v[j],v[j-1]);
                break;
            }
        }
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int key[100002],val[100002],n,m,k,ans=0,temp;
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
    {
        cin>>val[i];
        key[val[i]]=i;
    }
    for(int i=0;i<m;i++)
    {
        cin>>temp;
        ans+=(key[temp]/k+1);
        if(key[temp])
        {
            key[val[key[temp]-1]]++;
            swap(val[key[temp]],val[key[temp]-1]);
            key[temp]--;
        }
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int key[100002],val[100002],n,m,k,ans=0,temp;
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
    {
        cin>>val[i];
        key[val[i]]=i;
    }
    for(int i=0;i<m;i++)
    {
        cin>>temp;
        ans+=(key[temp]/k+1);
        if(val[key[temp]]) swap(key[val[temp]],key[val[temp]-1]);
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,k,a,ans=0;
    cin>>n>>m>>k;
    map<int,int>mp;
    vector<int>v;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        mp[a]=i;
    }
    for(int i=0;i<m;i++)
    {
        cin>>a;
        v.push_back(a);
    }
    for(int i=0;i<m;i++)
    {
        ans+=(mp[v[i]]/k+1);
        mp[v[i]-1] ++;
        mp[v[i]] --;
    }
    cout<<ans<<endl;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int key[100002],val[100002],n,m,k,temp;
    long long int ans=0;
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
    {
        cin>>val[i];
        key[val[i]]=i;
    }
    for(int i=0;i<m;i++)
    {
        cin>>temp;
        ans+=(key[temp]/k+1);
        if(key[temp])
        {
            key[val[key[temp]-1]]++;
            swap(val[key[temp]],val[key[temp]-1]);
            key[temp]--;
        }
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> p;
const int mx=1e6+9;
int a[mx];
map<int,int>mp;
int n,m,k,q;
ll ans=0;
int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++){
        cin>>a[i];
        mp[a[i]]=i;
    }
    for(int i=0;i<m;i++){
        cin>>q;
        int f=mp[q];
        ans+=f/k+1;
        //cnt[f]++;
        if(f){
            //swap(cnt[f],cnt[f-1]);
            mp[q]=f-1;
            mp[a[f-1]]=f;
            swap(a[f],a[f-1]);
        }
    }
    cout<<ans;

    return 0;
}
/*
10 12 3
6 1 2 9 3 10 8 5 7 4
3 9 9 4 8 2 3 8 10 8 3 4
out : 29
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> p;
const int mx=1e5+9;
int a[mx];
map<int,int>mp;
int n,m,k,q,ans=0;
int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++){
        cin>>a[i];
        mp[a[i]]=i;
    }
    for(int i=0;i<m;i++){
        cin>>q;
        int f=mp[q];
        ans+=f/k+1;
        //cnt[f]++;
        if(f){
            //swap(cnt[f],cnt[f-1]);
            mp[q]=f-1;
            mp[a[f-1]]=f;
            swap(a[f],a[f-1]);
        }
    }
    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> p;
const int mx=1e5+9;
int a[mx];
map<int,int>mp;
int cnt[mx];
int n,m,k,q,ans=0;
int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++){
        cin>>a[i];
        mp[a[i]]=i;
    }
    for(int i=0;i<m;i++){
        cin>>q;
        int f=mp[q];
        ans+=f/k+1;
        if(f&&cnt[f]>=cnt[f-1]){
            cnt[f]++;
            swap(cnt[f],cnt[f-1]);
            mp[q]=f-1;
            mp[a[f-1]]=f;
        }
    }
    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> p;
const int mx=1e5+9;
int a[mx];
map<int,int>mp;
int cnt[mx];
int n,m,k,q,ans=0;
int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++){
        cin>>a[i];
        mp[a[i]]=i;
    }
    for(int i=0;i<m;i++){
        cin>>q;
        int f=mp[q];
        ans+=f/k+1;
        if(f&&cnt[f]>=cnt[f-1]){
            cnt[f]++;
            swap(cnt[f],cnt[f-1]);
            mp[q]=f-1;
            mp[a[f-1]]=f;
            swap(a[f],a[f-1]);
        }
        /*for(int i=0;i<n;i++)
            cout<<a[i]<<" ";
        cout<<endl;*/
    }
    cout<<ans;

    return 0;
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <iomanip>
#include <cmath>
#include <queue>
#include <bitset>
#include <numeric>
#include <array>
#include <cstring>
#include <random>
#include <chrono>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 1e5 + 42;
ll pos[N] = {0}, cur[N];
int main() {

  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

  ll n, m, k, ans = 0;

  cin >> n >> m >> k;

  for(ll i = 1; i <= n; i++) {
    cin >> cur[i];
    pos[cur[i]] = i;
  }

  while(m--) {
    ll b; cin >> b;
    if(pos[b] == 0) {
      ans++;
    } else {
      ans += 1 + (pos[b] - 1) / k;
      swap(cur[pos[b]], cur[pos[b] - 1]);
      swap(pos[cur[pos[b]]], pos[cur[pos[b] - 1]]);
    }
  }

  cout << ans;

  return 0;
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <iomanip>
#include <cmath>
#include <queue>
#include <bitset>
#include <numeric>
#include <array>
#include <cstring>
#include <random>
#include <chrono>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 1e5 + 42;
ll pos[N] = {0}, cur[N] = {0};
int main() {

  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

  ll n, m, k, ans = 0;

  cin >> n >> m >> k;

  for(ll i = 1; i <= n; i++) {
    cin >> cur[i];
    pos[cur[i]] = i;
  }

  while(m--) {
    ll b; cin >> b;
    if(pos[b] == 0) {
      ans++;
    } else {
      ans += 1 + (pos[b] - 1) / k;
      swap(cur[pos[b]], cur[pos[b] - 1]);
      swap(pos[cur[pos[b]]], pos[cur[pos[b] - 1]]);
    }
  }

  cout << ans;

  return 0;
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <iomanip>
#include <cmath>
#include <queue>
#include <bitset>
#include <numeric>
#include <array>
#include <cstring>
#include <random>
#include <chrono>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 1e5 + 42;
ll pos[N] = {0}, cur[N] = {0};
int main() {

  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

  ll n, m, k, ans = 0;

  cin >> n >> m >> k;

  for(ll i = 1; i <= n; i++) {
    cin >> cur[i];
    pos[cur[i]] = i;
  }

  while(m--) {
    ll b; cin >> b;
    if(pos[b] == 1) {
      ans++;
    } else {
      ans += 1 + (pos[b] - 1) / k;
      swap(cur[pos[b]], cur[pos[b] - 1]);
      swap(pos[cur[pos[b]]], pos[cur[pos[b] - 1]]);
    }
  }

  cout << ans;

  return 0;
}


#include<bits/stdc++.h>
typedef unsigned long long int ll;
using namespace std;

int main(){
ll test,i,j,count,flag=0,n,o1=0,o2=0,b1,x,y,m,l,z,max1,x1,y1,x2,y2,z1,k,sum,f,r,ans1,min1;

ll a[1000000];
cin>>n>>m>>k;
map<ll,ll>mk;
for(i=1;i<=n;i++){
	cin>>a[i];
	mk[a[i]]=i;
}
sum=0;
for(i=0;i<m;i++){
	cin>>x;
	sum+=((mk[x]-1)/k)+1;
	if(mk[x]>1){
	    y1=a[mk[x]-1];
	    swap(a[mk[x]-1],a[mk[x]]);
	    mk[y1]++;
	    mk[x]--;

	}
//cout<<sum<<"\n";
}
cout<<sum<<"\n";
return 0;
}


#include<bits/stdc++.h>
typedef unsigned long long int ll;
using namespace std;

int main(){
ll test,i,j,count,flag=0,n,o1=0,o2=0,b1,x,y,m,l,z,max1,x1,y1,x2,y2,z1,k,sum,f,r,ans1,min1;

ll a[1000000];
cin>>n>>m>>k;
map<ll,ll>mk;
for(i=1;i<=n;i++){
	cin>>a[i];
	mk[a[i]]=i;
}
sum=0;
for(i=0;i<m;i++){
	cin>>x;
	sum+=((mk[x]-1)/k)+1;
	if(mk[x]>1){
	    y1=a[mk[x]-1];
	    mk[y1]++;
	    mk[x]--;
	    swap(a[mk[x]-1],a[mk[x]]);
	}
//cout<<sum<<"\n";
}
cout<<sum<<"\n";
return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n,u,x[100005];
double sol = 0;

int main(){
	cin >> n >> u;
	for(int i = 0; i < n; i++){
		cin >> x[i];
	}
	bool ok = 0;
	int last = 2;
	for(int i = 1; i < n-1; i++){
		for(int j = last; j < n; j++){
			last = j;
			if(x[j+1] - x[i-1] > u) break;
		}
		if(x[last] - x[i-1] <= u) ok = 1;
		else if(x[last] - x[i-1] > u) continue;
		double high = x[last] - x[i];
		double low = x[last] - x[i-1];
		double temp = high/low;
		sol = max(sol, temp);
	}
	if(!ok) return cout << -1,0;
	printf("%.12f",sol);
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+100;
int n,U;
int e[maxn];
void input(){
	scanf("%d%d",&n,&U);
	for (int i=0;i<n;i++){
		scanf("%d",e+i);
	}
}
void solve(){
	double ans =0;
	for (int i=0;i<n-2;i++){
		int j = lower_bound(e,e+n,e[i]+U)-e;
		if (j==n)continue;
		if (e[j]!=e[i]+U)j--;
		if (j<=i+1)continue;
		ans = max(ans,1.0*(e[j]-e[i+1])/(e[j]-e[i]));
	}
	if (ans==0)printf("-1\n");
	else{
		printf("%.9f\n",ans);
	}
}
int main(){
	input();
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+100;
int n,U;
int e[maxn];
void input(){
	scanf("%d%d",&n,&U);
	for (int i=0;i<n;i++){
		scanf("%d",e+i);
	}
}
void solve(){
	double ans =0;
	for (int i=0;i<n;i++){
		int j = lower_bound(e,e+n,e[i]+U)-e;
		if (j==n)continue;
		if (e[j]!=e[i]+U)j--;
		if (j<=i+1)continue;
		ans = max(ans,1.0*(e[j]-e[i+1])/(e[j]-e[i]));
	}
	if (ans==0)printf("-1\n");
	else{
		printf("%.9f\n",ans);
	}
}
int main(){
	input();
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+100;
int n,U;
int e[maxn];
void input(){
	scanf("%d%d",&n,&U);
	for (int i=0;i<n;i++){
		scanf("%d",e+i);
	}
}
void solve(){
	double ans =0;
	for (int i=0;i<n-2;i++){
		int j = lower_bound(e,e+n,e[i]+U)-e;
		if (j==n)continue;
		if (e[j]!=e[i]+U)j--;
		if (j<=i+1)continue;
		ans = max(ans,1.0*(e[j]-e[i+1])/(e[j]-e[i]));
	}
	for (int i=n-3;i>=0;i--){
		if (e[n-1]-e[i]>U)break;
		ans = max(ans,1.0*(e[n-1]-e[i+1])/(e[n-1]-e[i]));
	}
	if (ans==0)printf("-1\n");
	else{
		printf("%.9f\n",ans);
	}
}
int main(){
	input();
	solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int a[maxn];
int n, u;
int main() {
    while (cin >> n >> u) {
        for (int i = 0; i < n; i++) cin >> a[i];
        int i = 0, j = 1, k = j;
        double ans = -1;
        for (; i < n - 2; i++, j++, k = j) {
            for (; k < n - 1 && a[k + 1] - a[i] <= u; k++) ;
            // printf("%d %d %d\n", i, j, k);
            if (k == j) continue;
            ans = max(ans, 1 + 1.0 * (a[i] - a[j]) / (a[k] - a[i]));
            // cout << ans << endl;
        }
        printf("%.9lf\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int a[maxn];
int n, u;
int main() {
    while (cin >> n >> u) {
        for (int i = 0; i < n; i++) cin >> a[i];
        int i = 0, k = i;
        double ans = -1;
        for (; i < n - 2; i++) {
            for (; k < n - 1 && a[k + 1] - a[i] <= u; k++) ;
            if (k - i >= 2) {
                ans = max(ans, 1 + 1.0 * (a[i] - a[i + 1]) / (a[k] - a[i]));
            }
        }
        printf("%.9lf\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int a[maxn];
int n, u;
int main() {
    while (cin >> n >> u) {
        for (int i = 0; i < n; i++) cin >> a[i];
        int i = 0, k = i;
        double ans = -1;
        for (; i < n - 2; i++, k++) {
            for (; k < n - 1 && a[k + 1] - a[i] <= u; k++) ;
            if (k - i >= 2) {
                ans = max(ans, 1 + 1.0 * (a[i] - a[i + 1]) / (a[k] - a[i]));
            }
        }
        printf("%.9lf\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int a[maxn];
int n, u;
int main() {
    while (cin >> n >> u) {
        for (int i = 0; i < n; i++) cin >> a[i];
        int i = 0, j = 1, k;
        double ans = -1;
        for (; i < n - 2; i++, j++) {
            for (k = j; k < n - 1 && a[k + 1] - a[i] <= u; k++) ;
            // cout << k << endl;
            if (k == j) { i++; j++; continue; }
            ans = max(ans, 1 + 1.0 * (a[i] - a[j]) / (a[k] - a[i]));
            // cout << ans << endl;
        }
        printf("%.9lf\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int a[maxn];
int n, u;
int main() {
    while (cin >> n >> u) {
        for (int i = 0; i < n; i++) cin >> a[i];
        int i = 0, j = 1, k;
        double ans = -1;
        for (; i < n - 2; i++, j++) {
            for (k = j; k < n - 1 && a[k + 1] - a[i] <= u; k++) ;
            // cout << k << endl;
            if (k == j) { i++; j++; continue; }
            ans = max(ans, 1 + 1.0 * (a[i] - a[j]) / (a[k] - a[i]));
            // cout << ans << endl;
        }
        printf("%lf\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int a[maxn];
int n, u;
int main() {
    while (cin >> n >> u) {
        for (int i = 0; i < n; i++) cin >> a[i];
        int i = 0, j = 1, k = j;
        double ans = -1;
        for (; i < n - 2; i++, j++) {
            for (; k < n - 1 && a[k + 1] - a[i] <= u; k++) ;
            if (k == j) { i++; j++; continue; }
            // cout << k << endl;
            ans = max(ans, 1 + 1.0 * (a[i] - a[j]) / (a[k] - a[i]));
            // cout << ans << endl;
        }
        printf("%.9lf\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int a[maxn];
int n, u;
int main() {
    while (cin >> n >> u) {
        for (int i = 0; i < n; i++) cin >> a[i];
        int i = 0, j = 1, k = j;
        double ans = -1;
        for (; i < n - 2; i++, j++) {
            for (; k < n - 1 && a[k + 1] - a[i] <= u; k++) ;
            // printf("%d %d %d\n", i, j, k);
            if (k == j) { k++; continue; }
            ans = max(ans, 1 + 1.0 * (a[i] - a[j]) / (a[k] - a[i]));
            // cout << ans << endl;
        }
        printf("%.9lf\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define FILE_IN freopen("kotlin.in", "r", stdin);
#define FILE_OUT freopen("kotlin.out", "w", stdout);

#define fr(i,n) for(int i=0;i<n;i++)
#define frr(i,a,b) for(int i =a;i<=b;i++)
// for(auto it : g[i].nb)

typedef long long ll;
typedef long double ld;

#define pb push_back

#define all(a) a.begin(),a.end()

#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef tuple<ll,ll,ll> t3ll;

#define mt make_tuple
#define get(a,id) get<id>(a)

const long double PI = acos(-1.0l);
const ll MOD = 1e9+7;

//LONG_LONG_MAX
//-DBL_MAX

bool debug = 1;
#define printa(a) cout << #a << " = " << (a) << endl
#define prin(a) if(debug) cout << #a << " = " << (a) << endl
#define soprin(a) if(debug) cout << (a)
#define ppal(a)  if(debug) cout << #a << endl
#define prinsep if(debug) cout << "------" << endl
#define cendl if(debug) cout << endl
#define prinpar(p) if(debug) cout << #p << ".fi=" << (p).fi << " " << #p << ".se=" << (p).se << endl
#define print(tup) if(debug) cout << #tup << " = {" << get(tup,0) << ", " << get(tup,1) << ", " << get(tup,2) << "}\n"
#define prinv(v) if(debug){ cout << #v << ":" << endl; for(auto it = (v).begin(); it!=(v).end();it++){ cout << *it << " ";} cout << endl;}

const int N = 1;

int main(){
	//FILE_IN FILE_OUT
	int n, u;
	cin >> n >> u;

	vector<int> v(n);

	fr(i,n){
		scanf("%d", &v[i]);
	}

	long double ans = 0;

	int k = -1;
	fr(i,n-2){
		k = max(k,i+2);
		while(k<n and v[k]-v[i]<=u){
			ans= max(ans,(long double)(v[k]-v[i+1])/(v[k]-v[i]));
			k++;
		}
		k--;
	}

	if(ans==0){
		cout << -1 << endl;
		return 0;
	}

	printf("%.10lf\n", (double)(ans));

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define FILE_IN freopen("kotlin.in", "r", stdin);
#define FILE_OUT freopen("kotlin.out", "w", stdout);

#define fr(i,n) for(int i=0;i<n;i++)
#define frr(i,a,b) for(int i =a;i<=b;i++)
// for(auto it : g[i].nb)

typedef long long ll;
typedef long double ld;

#define pb push_back

#define all(a) a.begin(),a.end()

#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef tuple<ll,ll,ll> t3ll;

#define mt make_tuple
#define get(a,id) get<id>(a)

const long double PI = acos(-1.0l);
const ll MOD = 1e9+7;

//LONG_LONG_MAX
//-DBL_MAX

bool debug = 1;
#define printa(a) cout << #a << " = " << (a) << endl
#define prin(a) if(debug) cout << #a << " = " << (a) << endl
#define soprin(a) if(debug) cout << (a)
#define ppal(a)  if(debug) cout << #a << endl
#define prinsep if(debug) cout << "------" << endl
#define cendl if(debug) cout << endl
#define prinpar(p) if(debug) cout << #p << ".fi=" << (p).fi << " " << #p << ".se=" << (p).se << endl
#define print(tup) if(debug) cout << #tup << " = {" << get(tup,0) << ", " << get(tup,1) << ", " << get(tup,2) << "}\n"
#define prinv(v) if(debug){ cout << #v << ":" << endl; for(auto it = (v).begin(); it!=(v).end();it++){ cout << *it << " ";} cout << endl;}

const int N = 1;

int main(){
	//FILE_IN FILE_OUT
	int n, u;
	cin >> n >> u;

	vector<int> v(n);

	fr(i,n){
		scanf("%d", &v[i]);
	}

	long double ans = -1;

	int k = 0;
	fr(i,n-2){
		k = max(k,i+2);
		while(k<n and v[k]-v[i]<=u){
			ans= max(ans,(long double)(v[k]-v[i+1])/(v[k]-v[i]));
			k++;
		}
		k--;
	}

	if(ans<0){
		cout << -1 << endl;
		return 0;
	}

	printf("%.10lf\n", (double)(ans));

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define FILE_IN freopen("kotlin.in", "r", stdin);
#define FILE_OUT freopen("kotlin.out", "w", stdout);

#define fr(i,n) for(int i=0;i<n;i++)
#define frr(i,a,b) for(int i =a;i<=b;i++)
// for(auto it : g[i].nb)

typedef long long ll;
typedef long double ld;

#define pb push_back

#define all(a) a.begin(),a.end()

#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef tuple<ll,ll,ll> t3ll;

#define mt make_tuple
#define get(a,id) get<id>(a)

const long double PI = acos(-1.0l);
const ll MOD = 1e9+7;

//LONG_LONG_MAX
//-DBL_MAX

bool debug = 1;
#define printa(a) cout << #a << " = " << (a) << endl
#define prin(a) if(debug) cout << #a << " = " << (a) << endl
#define soprin(a) if(debug) cout << (a)
#define ppal(a)  if(debug) cout << #a << endl
#define prinsep if(debug) cout << "------" << endl
#define cendl if(debug) cout << endl
#define prinpar(p) if(debug) cout << #p << ".fi=" << (p).fi << " " << #p << ".se=" << (p).se << endl
#define print(tup) if(debug) cout << #tup << " = {" << get(tup,0) << ", " << get(tup,1) << ", " << get(tup,2) << "}\n"
#define prinv(v) if(debug){ cout << #v << ":" << endl; for(auto it = (v).begin(); it!=(v).end();it++){ cout << *it << " ";} cout << endl;}

const int N = 1;

int main(){
	//FILE_IN FILE_OUT
	int n, u;
	cin >> n >> u;

	vector<int> v(n);

	fr(i,n){
		scanf("%d", &v[i]);
	}

	long double ans = 0;

	int k = 0;
	fr(i,n-2){
		k = max(k,i+2);
		while(k<n and v[k]-v[i]<=u){
			ans= max(ans,(long double)(v[k]-v[i+1])/(v[k]-v[i]));
			k++;
		}
	}

	if(ans==0){
		cout << -1 << endl;
		return 0;
	}

	printf("%.10lf\n", (double)(ans));

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define FILE_IN freopen("kotlin.in", "r", stdin);
#define FILE_OUT freopen("kotlin.out", "w", stdout);

#define fr(i,n) for(int i=0;i<n;i++)
#define frr(i,a,b) for(int i =a;i<=b;i++)
// for(auto it : g[i].nb)

typedef long long ll;
typedef long double ld;

#define pb push_back

#define all(a) a.begin(),a.end()

#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef tuple<ll,ll,ll> t3ll;

#define mt make_tuple
#define get(a,id) get<id>(a)

const long double PI = acos(-1.0l);
const ll MOD = 1e9+7;

//LONG_LONG_MAX
//-DBL_MAX

bool debug = 1;
#define printa(a) cout << #a << " = " << (a) << endl
#define prin(a) if(debug) cout << #a << " = " << (a) << endl
#define soprin(a) if(debug) cout << (a)
#define ppal(a)  if(debug) cout << #a << endl
#define prinsep if(debug) cout << "------" << endl
#define cendl if(debug) cout << endl
#define prinpar(p) if(debug) cout << #p << ".fi=" << (p).fi << " " << #p << ".se=" << (p).se << endl
#define print(tup) if(debug) cout << #tup << " = {" << get(tup,0) << ", " << get(tup,1) << ", " << get(tup,2) << "}\n"
#define prinv(v) if(debug){ cout << #v << ":" << endl; for(auto it = (v).begin(); it!=(v).end();it++){ cout << *it << " ";} cout << endl;}

const int N = 1;

int main(){
	//FILE_IN FILE_OUT
	int n, u;
	cin >> n >> u;

	vector<int> v(n);

	fr(i,n){
		scanf("%d", &v[i]);
	}

	long double ans = -1;

	int k = 0;
	fr(i,n-2){
		k = max(k,i+2);
		while(k<n and v[k]-v[i]<=u){
			ans= max(ans,(long double)(v[k]-v[i+1])/(v[k]-v[i]));
			k++;
		}
		//k--;
	}

	if(ans<0){
		cout << -1 << endl;
		return 0;
	}

	printf("%.10lf\n", (double)(ans));

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int n;
    cin >> n;
    int m[n], t[n];
    for (int i = 0; i < n; i++)
        cin >> m[i], t[i] = 0;
    int cur = 0;
    for (int i = n - 1; i >= 0; i--)
    {
        cur = max(0, cur - 1);
        cur = max(cur, m[i] + 1);
        t[i] = cur;
    }
    cur = 0;
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
        cur = max(t[i], cur);
        t[i] = cur;
        sum += t[i] - m[i] - 1;
    }
    cout << sum << "\n";
}


    #include <bits/stdc++.h>

    using namespace std;

    int main()
    {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);

        long long n;
        cin >> n;
        long long m[n], a[n];
        for (long long i = 0; i < n; i++)
            cin >> m[i], a[i] = 0;
        long long total = 1;
        long long j = 0;
        a[0] = 1;
        long long sum = 0;
        for (long long i = 1; i < n;)
        {
            j = i;
            while (j < n && m[j] < total)
                j++;
            if (j == n)
            {
                while (i < n)
                {
                    sum += total - m[i] - 1;
                    i++;
                }
                break;
            }
            long long k = j;
            long long limit = m[j] + 1 - total;
            while (k >= i && limit)
            {
                a[k] += 1;
                k--;
                limit--;
            }
            while (i <= j)
            {
                total += a[i];
                sum += total - m[i] - 1;
                i++;
            }
        }
        cout << sum << "\n";
    }


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int n;
    cin >> n;
    int m[n], a[n];
    for (int i = 0; i < n; i++)
        cin >> m[i], a[i] = 0;
    int total = 1;
    int j = 0;
    a[0] = 1;
    int sum = 0;
    for (int i = 1; i < n;)
    {
        j = i;
        while (j < n && m[j] < total)
            j++;
        if (j == n)
        {
            while (i < n)
            {
                sum += total - m[i] - 1;
                i++;
            }
            break;
        }
        int k = j;
        int limit = m[j] + 1 - total;
        while (k >= i && limit)
        {
            a[k] = 1;
            k--;
            limit--;
        }
        while (i <= j)
        {
            total += a[i];
            sum += total - m[i] - 1;
            i++;
        }
    }
    cout << sum << "\n";
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    long long n;
    cin >> n;
    long long m[n], t[n];
    for (long long i = 0; i < n; i++)
        cin >> m[i], t[i] = 0;
    long long cur = 0;
    for (long long i = n - 1; i >= 0; i--)
    {
        cur = max(0ll, cur - 1);
        cur = max(cur, m[i] + 1);
        t[i] = cur;
    }
    cur = 0;
    long long sum = 0;
    for (long long i = 0; i < n; i++)
    {
        cur = max(t[i], cur);
        t[i] = cur;
        sum += t[i] - m[i] - 1;
    }
    cout << sum << "\n";
}


#include "bits/stdc++.h"
#include<vector>
#include<iostream>
#include<queue>
#include<algorithm>
#include<map>
#include<set>
#include<iomanip>
#include<assert.h>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<stack>
#include<complex>
#include<memory>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse2")

#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;
using Graph=vector<vector<int>>;
using ll=long long;

#define WHATS(var)cout<<__LINE__<<' '<<#var<<"="<<var<<endl;

template<class S, class T> ostream& operator <<(ostream &os, const pair<S, T> v){
	os << "( " << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<T> &v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<vector<T>> &v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<set<T>> &v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const set<T> &v){
	int i=0;
	for(auto it:v){
		if(i > 0){os << ' ';}
		os << it;
		i++;
	}
	return os;
}
//1. maskの部分集合を列挙する
	// 	a. i==0を評価しない
	// 	for(int i=mask; i>0; i=(i-1)&mask) {
	// 	}

	// b. i==0を評価する
	// 	for(int i=mask; i>=0; i--) {
	// 		i&=mask;
	// 	}
// 2. maskを含む集合を列挙する

// 少し目的は変わりますが同じようなやり方でできる処理が、maskを含む集合の列挙です。

// 	for(int i=mask; i<(1<<n); i=(i+1)|mask) {
// 	}

struct UnionFind {
	vector<int> data;
	UnionFind(int size) : data(size, -1) { }
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool findSet(int x, int y) {
		return root(x) == root(y);
	}
	int root(int x) {
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
	int size(int x) {
		return -data[root(x)];
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie();

	// int H,W;cin>>H>>W;
	// vector<string>field(H);
	// for(int y=0;y<H;++y){
	// 	cin>>field[y];

	// }
	// UnionFind uf(H+W);
	// for(int y=0;y<H;++y){
	// 	for(int x=0;x<W;++x){
	// 		if(field[y][x]=='#')uf.unionSet(y,x+H);
	// 	}
	// }
	// bool ok=true;
	// for(int y=0;y<H;++y){
	// 	for(int x=0;x<W;++x){
	// 		if(field[y][x]=='.'){
	// 			if(uf.root(y)==uf.root(x+H))ok=false;
	// 		}
	// 	}
	// }
	// if(ok)cout<<"Yes"<<endl;
	// else cout<<"No"<<endl;

	int N;cin>>N;
	vector<ll>v(N);
	for(int i=0;i<N;++i)cin>>v[i];
	vector<ll>v2(N);
	ll answer=0;
	ll kk=0;
	for(int i=N-1;i>=0;--i){
		kk--;
		kk=max(kk,v[i]);
		v2[i]=kk;
	}
	kk=0;
	for(int i=0;i<N;++i){
		v2[i]=max(v2[i],kk);
		kk=max(kk,v[i]);
	}
	for(int i=0;i<N;++i)answer+=v2[i]-v[i];
	cout<<answer<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;

  vector<int> v(n);
  for (auto &x: v) {
    cin >> x;
  }

  vector<int> c(n);

  c[0] = 1;
  for (int i = 1; i < n; i++) {
    c[i] = max(c[i-1], v[i] + 1);
  }

  for (int i = n-2; i >= 0; i--) {
    c[i] = max(c[i], c[i+1]-1);
  }

  long long sum = 0;
  for (int i = 0; i < n; i++) {
    sum += c[i] - v[i] - 1;
  }

  cout << sum << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;

  vector<int> v(n);
  vector<int> c(n);
  c[0] = 1;
  for (int i = 0 i < n; i++) {
    cin >> v[i];
    if (i) c[i] = max(c[i-1], v[i] + 1);
  }

  long long sum = c[n-1] - v[n-1] - 1;;
  for (int i = n-2; i >= 0; i--) {
    c[i] = max(c[i], c[i+1]-1);
    sum += c[i] - v[i] - 1;
  }

  cout << sum << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;

  vector<int> v(n);
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    c[i] = i ? max(c[i-1], v[i] + 1) : 1;
  }

  long long sum = c[n-1] - v[n-1] - 1;;
  for (int i = n-2; i >= 0; i--) {
    c[i] = max(c[i], c[i+1]-1);
    sum += c[i] - v[i] - 1;
  }

  cout << sum << endl;

  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define SPEED ios_base::sync_with_stdio(false); cin.tie(NULL);
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define RFOR(i, b, a) for (ll i = b; i >= a; --i)
#define ALL(x) x.begin(), x.end()
#define DEBUG(args...) { string _s = #args; replace(ALL(_s), ' ', '\0');\
replace(ALL(_s), ',', ' ');\
stringstream _ss(_s); istream_iterator<string> _it(_ss); _debug(_it, args);}
#define endl "\n"
#define F first
#define S second
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void _debug(istream_iterator<string>) {}
template<typename T, typename... Args>
void _debug(istream_iterator<string> it, T first, Args... args) {
    cerr << ">> " << *it << " : " << first << endl; _debug(++it, args...);
}
template <typename T1, typename T2>
inline ostream& operator << (ostream& out, const pair<T1, T2>& p) {
    return out << "(" << p.F << ", " << p.S << ")";
}
template<typename T>
inline ostream& operator << (ostream& out, const vector<T>& v) {
    if (v.empty()) return out << "[]";
    else { out << '['; for (auto& e : v) { out << e << ", "; } return out << "\b\b]"; }
}
template<typename T>
inline ostream& operator << (ostream& out, const set<T>& s) {
    if (s.empty()) return out << "{}";
    else { out << '{'; for (auto& e : s) { out << e << ", "; } return out << "\b\b}"; }
}
template<typename T>
inline ostream& operator << (ostream& out, const unordered_set<T>& s) {
    return out << set<T>(ALL(s));
}
template<typename T1, typename T2>
inline ostream& operator << (ostream& out, const map<T1, T2>& m) {
    if (m.empty()) return out << "{}";
    out << '{'; for (auto& p : m) { out << p << ", "; } return out << "\b\b}";
}
template<typename T1, typename T2>
inline ostream& operator << (ostream& out, const unordered_map<T1, T2>& m) {
    return out << map<T1, T2>(ALL(m));
}
template<typename T>
inline ostream& operator << (ostream& out, const ordered_set<T>& s) {
    return out << set<T>(ALL(s));
}

typedef long long ll;
typedef long double ld;
typedef vector<long long> vll;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vpll;
typedef unordered_map<ll, ll> STll;
/************************************** MAIN PROGRAM ********************************************/

int main()
{
    //freopen("input.txt", "r", stdin);
    SPEED
    ll n;
    cin >> n;
    vector<ll> a(n);
    for (auto &e : a) {
        cin >> e;
    }
    ll prefMax = 0;
    vector<ll> total(n);
    for (int i = 0; i < n; ++i) {
        prefMax = max(prefMax, a[i] + 1);
        total[i] = max(total[i], prefMax);
    }
    //DEBUG(total)
    ll sumMax = total[n - 1] - 1;
 //   DEBUG(sumMax)
    for (ll i = n - 2; i >= 0; --i) {
        total[i] = max(total[i], sumMax);
        sumMax = max(sumMax - 1, total[i] - 1);
    }

   // DEBUG(total)
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        auto e = total[i];
        ans += e - 1 - a[i];
;    }
    cout << ans;
}
/************************************** END OF PROGRAM ******************************************/
/** Stuff you should look for:
 * int overflow, array bounds, over-counting, graph is not a tree/connected
 * special cases (n=1?), set/unordered_set TLE, multi-set/set error
 * do something instead of nothing and stay organized
 */


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define SPEED ios_base::sync_with_stdio(false); cin.tie(NULL);
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define RFOR(i, b, a) for (ll i = b; i >= a; --i)
#define ALL(x) x.begin(), x.end()
#define DEBUG(args...) { string _s = #args; replace(ALL(_s), ' ', '\0');\
replace(ALL(_s), ',', ' ');\
stringstream _ss(_s); istream_iterator<string> _it(_ss); _debug(_it, args);}
#define endl "\n"
#define F first
#define S second
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void _debug(istream_iterator<string>) {}
template<typename T, typename... Args>
void _debug(istream_iterator<string> it, T first, Args... args) {
    cerr << ">> " << *it << " : " << first << endl; _debug(++it, args...);
}
template <typename T1, typename T2>
inline ostream& operator << (ostream& out, const pair<T1, T2>& p) {
    return out << "(" << p.F << ", " << p.S << ")";
}
template<typename T>
inline ostream& operator << (ostream& out, const vector<T>& v) {
    if (v.empty()) return out << "[]";
    else { out << '['; for (auto& e : v) { out << e << ", "; } return out << "\b\b]"; }
}
template<typename T>
inline ostream& operator << (ostream& out, const set<T>& s) {
    if (s.empty()) return out << "{}";
    else { out << '{'; for (auto& e : s) { out << e << ", "; } return out << "\b\b}"; }
}
template<typename T>
inline ostream& operator << (ostream& out, const unordered_set<T>& s) {
    return out << set<T>(ALL(s));
}
template<typename T1, typename T2>
inline ostream& operator << (ostream& out, const map<T1, T2>& m) {
    if (m.empty()) return out << "{}";
    out << '{'; for (auto& p : m) { out << p << ", "; } return out << "\b\b}";
}
template<typename T1, typename T2>
inline ostream& operator << (ostream& out, const unordered_map<T1, T2>& m) {
    return out << map<T1, T2>(ALL(m));
}
template<typename T>
inline ostream& operator << (ostream& out, const ordered_set<T>& s) {
    return out << set<T>(ALL(s));
}

typedef long long ll;
typedef long double ld;
typedef vector<long long> vll;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vpll;
typedef unordered_map<ll, ll> STll;
/************************************** MAIN PROGRAM ********************************************/

int main()
{
    //freopen("input.txt", "r", stdin);
    SPEED
    ll n;
    cin >> n;
    vector<ll> a(n);
    for (auto &e : a) {
        cin >> e;
    }
    ll prefMax = 0;
    vector<ll> total(n);
    for (int i = 0; i < n; ++i) {
        prefMax = max(prefMax, a[i] + 1);
        total[i] = max(total[i], prefMax);
    }
 //   DEBUG(total)
    ll sumMax = a[n - 1] - 1;
    for (int i = n - 2; i >= 0; --i) {
        total[i] = max(total[i], sumMax);
        sumMax = max(sumMax - 1, total[i] - 1);
    }
   // DEBUG(total)
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        auto e = total[i];
        ans += e - 1 - a[i];
;    }
    cout << ans;
}
/************************************** END OF PROGRAM ******************************************/
/** Stuff you should look for:
 * int overflow, array bounds, over-counting, graph is not a tree/connected
 * special cases (n=1?), set/unordered_set TLE, multi-set/set error
 * do something instead of nothing and stay organized
 */


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize ("Ofast")
#pragma GCC optimization ("unroll-loops, no-stack-protector")
#pragma GCC target ("avx")
#pragma GCC target ("avx2")
#pragma GCC target ("fma")
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair <int, int>
#define pll pair <ll, ll>
#define pci pair <char, int>
#define pld pair <ld, ld>
#define ppld pair <pld, pld>
#define ppll pair <pll, pll>
#define pldl pair <ld, ll>
#define vll vector <ll>
#define vvll vector <vll>
#define vpll vector <pll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define mll map <ll, ll>
#define fastmap gp_hash_table
#define cd complex <double>
#define vcd vector <cd>
#define PI 3.14159265358979
#define ordered_set tree <ll, null_type, less <ll>, rb_tree_tag, tree_order_statistics_node_update>
#pragma 03
using namespace std;
using namespace __gnu_pbds;
// ...
ll m[200005], l[200005];
int main(){
	ll n; cin >> n;
	for (ll i = 0; i < n; i++) cin >> m[i];
	ll lb = 0;
	for (ll i = n - 1; i >= 0; i--){
		lb = max(0LL, lb - 1); lb = max(lb, m[i] + 1); l[i] = lb;
	}
	ll mf = 0, ans = 0;
	for (ll i = 0; i < n; i++){
		mf = max(mf, l[i]); ans += mf;
	}
	for (ll i = 0; i < n; i++) ans -= (m[i] + 1);
	cout << ans << endl;
}


#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define _ << ' ' <<
using namespace std;
using ll = long long;

int m[100005];
int req[100005];

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> m[i];

    int cr = 0;
    for (int i = n - 1; i >= 0; --i)
    {
        req[i] = cr;
        cr -= 1;
        cr = max(cr, m[i]);
    }

    int num = 1;
    ll sol = 0;
    for (int i = 1; i < n; ++i)
    {
        sol += num - m[i] - 1;
        if (num < req[i] || m[i] == num)
            num++, sol++;
    }
    cout << sol;
}


#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <iostream>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>

using namespace std;

#define int long long
#define fastInp ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long ll;
typedef long double ld;

const ll SIZE = 5 * 1e5, MOD = 1e9 + 7;
vector<ll> vec;
ll q, dp[SIZE];

signed main() {
	fastInp;

	ll n, u;
	cin >> n;

	vec.resize(n);
	for (int i = 0; i < n; i++) cin >> vec[i];

	vector<ll> st;
	ll lines = 0, ans = 0;
	for (int i = 0; i < n; i++) {
		st.push_back(i);
		while (vec[i] >= lines) {
			ans += (i - st.back());
			st.pop_back();
			lines++;
		}
		ans += lines - vec[i] - 1;
	}

	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

// template {{{
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#define f first
#define s second
#define resz resize

#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)

#define sort_by(x, y) sort(all(x), [&](const auto& a, const auto& b) { return y; })

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vd = vector<double>;
using vs = vector<string>;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;

using vpii = vector<pii>;
using vvpii = vector<vpii>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
using vpdd = vector<pdd>;
using vvpdd = vector<vpdd>;

template<typename T> void ckmin(T& a, const T& b) { a = min(a, b); }
template<typename T> void ckmax(T& a, const T& b) { a = max(a, b); }

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

namespace __input {
    template<class T1, class T2> void re(pair<T1,T2>& p);
    template<class T> void re(vector<T>& a);
    template<class T, size_t SZ> void re(array<T,SZ>& a);

    template<class T> void re(T& x) { cin >> x; }
    void re(double& x) { string t; re(t); x = stod(t); }
    template<class Arg, class... Args> void re(Arg& first, Args&... rest) {
        re(first); re(rest...);
    }

    template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
    template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
    template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
}
using namespace __input;

namespace __output {
    template<class T1, class T2> void pr(const pair<T1,T2>& x);
    template<class T, size_t SZ> void pr(const array<T,SZ>& x);
    template<class T> void pr(const vector<T>& x);
    template<class T> void pr(const deque<T>& x);
    template<class T> void pr(const set<T>& x);
    template<class T1, class T2> void pr(const map<T1,T2>& x);

    template<class T> void pr(const T& x) { cout << x; }
    template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) {
        pr(first); pr(rest...);
    }

    template<class T1, class T2> void pr(const pair<T1,T2>& x) {
        pr("{",x.f,", ",x.s,"}");
    }
    template<class T, bool pretty = true> void prContain(const T& x) {
        if (pretty) pr("{");
        bool fst = 1; for (const auto& a: x) pr(!fst?pretty?", ":" ":"",a), fst = 0;
        if (pretty) pr("}");
    }
    template<class T> void pc(const T& x) { prContain<T, false>(x); pr("\n"); }
    template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
    template<class T> void pr(const vector<T>& x) { prContain(x); }
    template<class T> void pr(const deque<T>& x) { prContain(x); }
    template<class T> void pr(const set<T>& x) { prContain(x); }
    template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }

    void ps() { pr("\n"); }
    template<class Arg> void ps(const Arg& first) {
        pr(first); ps();
    }
    template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) {
        pr(first," "); ps(rest...);
    }
}
using namespace __output;

#define TRACE(x) x
#define __pn(x) pr(#x, " = ")
#define pd(...) __pn((__VA_ARGS__)), ps(__VA_ARGS__), cout << flush

namespace __algorithm {
    template<typename T> void dedup(vector<T>& v) {
        sort(all(v)); v.erase(unique(all(v)), v.end());
    }
    template<typename T> typename vector<T>::iterator find(vector<T>& v, const T& x) {
        auto it = lower_bound(all(v), x); return it != v.end() && *it == x ? it : v.end();
    }
    template<typename T> size_t index(vector<T>& v, const T& x) {
        auto it = find(v, x); assert(it != v.end() && *it == x); return it - v.begin();
    }
    template<typename C, typename T, typename OP> vector<T> prefixes(const C& v, T id, OP op) {
        vector<T> r(sz(v)+1, id); F0R (i, sz(v)) r[i+1] = op(r[i], v[i]); return r;
    }
    template<typename C, typename T, typename OP> vector<T> suffixes(const C& v, T id, OP op) {
        vector<T> r(sz(v)+1, id); F0Rd (i, sz(v)) r[i] = op(v[i], r[i+1]); return r;
    }
}
using namespace __algorithm;

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
struct monostate {
    friend istream& operator>>(istream& is, const monostate& ms) { return is; }
    friend ostream& operator<<(ostream& os, const monostate& ms) { return os; }
    friend monostate operator+(const monostate& a, const monostate& b) { return a; }
} ms;
#pragma GCC diagnostic pop

namespace __io {
    void setIn(string s) { freopen(s.c_str(),"r",stdin); }
    void setOut(string s) { freopen(s.c_str(),"w",stdout); }
    void setIO(string s = "") {
        ios_base::sync_with_stdio(0); cin.tie(0);
        cout << fixed << setprecision(15);
        if (sz(s)) { setIn(s+".in"), setOut(s+".out"); }
    }
}
using namespace __io;
// }}}

int main() {
    setIO();

    int N; re(N);

    vi above(N); re(above);

    // number of marks which must exist before the ith day
    vi min_marks(N + 1);
    F0Rd (i, N) {
        min_marks[i] = max(above[i], min_marks[i+1] - 1);
    }

    ll ans = 0;
    int tot_marks = 0;

    F0R (i, N) {
        assert(tot_marks >= above[i]);

        if (above[i] == tot_marks || min_marks[i+1] > tot_marks) {
            ans += tot_marks - above[i]; // all that aren't above are below
            tot_marks++; // create a new one
        } else {
            ans += tot_marks - above[i] - 1; // one is exactly at water level
        }
    }

    ps(ans);

    // did you check N=1? did you mix up N,M?
    // check your "infinity" and "null" values against the bounds
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n, ans, sum[100005], curr, tmp;

int main() {

    cin >> n;

    sum[0] = 0;

    for(int i = 1; i < 100005; i++)
        sum[i] = sum[i - 1] + i;

    for(int i = 0; i < n; i++) {
        cin >> tmp;

        if(tmp < curr)
            ans += curr - (tmp + 1);
        else {
            ans += sum[tmp - curr];
            curr = tmp + 1;
        }
    }

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n, ans, sum[100005], curr, tmp[100005], val[100005], maxi;

int main() {

    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;

    for(int i = 0; i < n; i++) {
        cin >> tmp[i];

        maxi = max(maxi, tmp[i]);
        val[i] = maxi;
    }

    maxi = 0;

    for(int i = n - 1; i >= 0; i--) {
        val[i] = max(val[i], maxi);
        maxi = max(maxi, tmp[i]);
        maxi--;
    }

    curr = 1;
    for(int i = 1; i < n; i++) {
        if(val[i] > val[i - 1]) {
            curr++;
        }

        ans += curr - (tmp[i] + 1);
    }

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n, ans, sum[100005], curr, tmp;

int main() {

    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;

    sum[0] = 0;

    for(long long i = 1; i < 100005; i++)
        sum[i] = sum[i - 1] + i;

    for(int i = 0; i < n; i++) {
        cin >> tmp;

        if(tmp < curr)
            ans += curr - (tmp + 1ll);
        else {
            ans += sum[tmp - curr];
            curr = tmp + 1ll;
        }
    }

    cout << ans << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <climits>
#include <numeric>
using namespace std;
typedef long long int ll;

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n; cin >> n;
	vector<int> a(n),b(n+2,0);
	for(int i=0;i<n;i++){
		cin >> a[i];
	}
	for(int i=0;i<n;i++){
		b[i+1]=max(b[i],a[i]+1);
	}
	for(int i=n-1;i>=0;i--){
		b[i+1]=max(b[i+1],b[i+2]-1);
	}
	ll ans=0;
	for(int i=0;i<n;i++){
		ans+=(b[i+1]-a[i]-1);
	}
	cout << ans << endl;
}


#include <iostream> // includes cin to read from stdin and cout to write to stdout
#include <map>
#include <set>
#include <list>
#include <string>
#include <fstream>
#include <algorithm>
#include <vector>
#include <cassert>
#include <cassert>
#include <math.h>
///LLONG_MAX,INT_MA
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;i++)
#define REP(i,n) FOR(i,0,(n)-1)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;i--)
#define sz size()
#define FORA(i,c) REP(i,size(c))

#define itype(c) __typeof((c).begin())
#define FORE(e,c) for(itype(c) e=(c).begin();e!=(c).end();e++)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define SORT(x) sort(all(x))
#define REVERSE(x) reverse(all(x))
#define CLEAR(x,c) memset(x,c,sizeof(x))
#define amfor(Iterator, Container) 	for ( auto Iterator = begin(Container); (Iterator) != end(Container); ++(Iterator) )
#define ramfor(Iterator, Container) for ( auto Iterator = Container.rbegin(); (Iterator) != Container.rend(); ++(Iterator) )
template<class C, class E> inline bool contains(const C& container, const E& element) { return container.find(element) != container.end(); }
#define  NP(nn,ta,a,tb,b) struct nn : pair<ta,tb> { nn():pair<ta,tb>(){}; nn(ta pa,tb pb):pair<ta,tb>(pa,pb){} ta& a(){return first;} tb& b(){return second;} };
template<class T> inline void checkmin(T &a, T b) { if (b < a) a = b; }//asigna en a el minimo
template<class T> inline void checkmax(T &a, T b) { if (b > a) a = b; }//asigna en a el maximo

typedef long long ll;
using namespace std; // since cin and cout are both in namespace std, this saves some text
typedef vector<int> vi;
typedef vector<vector<int>> vvi;

int main()
{
	int n;

	cin >> n;
	vi m(n);
	vi d(n);
	int tot = 0;
	REP(i, n)
	{
		cin >> m[i];
		int newTot = m[i]+1;
		checkmax(tot, newTot);
		d[i] = tot;
	}

	FORD(i, n-2,1)
	{
		if (d[i] < d[i + 1])
			d[i] = d[i + 1] - 1;
	}

	int res = 0;
	REP(i, n)
		res += d[i] - m[i] - 1;
	cout << res;
	return 0;
}


#include <iostream> // includes cin to read from stdin and cout to write to stdout
#include <map>
#include <set>
#include <list>
#include <string>
#include <fstream>
#include <algorithm>
#include <vector>
#include <cassert>
#include <cassert>
#include <math.h>
///LLONG_MAX,INT_MA
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;i++)
#define REP(i,n) FOR(i,0,(n)-1)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;i--)
#define sz size()
#define FORA(i,c) REP(i,size(c))

#define itype(c) __typeof((c).begin())
#define FORE(e,c) for(itype(c) e=(c).begin();e!=(c).end();e++)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define SORT(x) sort(all(x))
#define REVERSE(x) reverse(all(x))
#define CLEAR(x,c) memset(x,c,sizeof(x))
#define amfor(Iterator, Container) 	for ( auto Iterator = begin(Container); (Iterator) != end(Container); ++(Iterator) )
#define ramfor(Iterator, Container) for ( auto Iterator = Container.rbegin(); (Iterator) != Container.rend(); ++(Iterator) )
template<class C, class E> inline bool contains(const C& container, const E& element) { return container.find(element) != container.end(); }
#define  NP(nn,ta,a,tb,b) struct nn : pair<ta,tb> { nn():pair<ta,tb>(){}; nn(ta pa,tb pb):pair<ta,tb>(pa,pb){} ta& a(){return first;} tb& b(){return second;} };
template<class T> inline void checkmin(T &a, T b) { if (b < a) a = b; }//asigna en a el minimo
template<class T> inline void checkmax(T &a, T b) { if (b > a) a = b; }//asigna en a el maximo

typedef long long ll;
using namespace std; // since cin and cout are both in namespace std, this saves some text
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

int main()
{
	int n;

	cin >> n;
	vll m(n);
	vll totals(n);
	ll tot = 0;
	REP(i, n)
	{
		cin >> m[i];
		ll newTot = m[i]+1LL;
		checkmax(tot, newTot);
		totals[i] = tot;
	}

	FORD(i, n-2,1)
	{
		if (totals[i] < totals[i + 1])
			totals[i] = totals[i + 1] - 1LL;
	}

	ll res = 0;
	REP(i, n)
		res += totals[i] - m[i] - 1;
	cout << res;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll mod=998244353;
ll mul(ll a, ll b)
{
	return ((a%mod)*(b%mod))%mod;
}
ll add(ll a, ll b)
{
	return ((a%mod)+(b%mod))%mod;
}
ll sub(ll a, ll b)
{
	return ((a%mod)-(b%mod)+mod)%mod;
}
bool c[100],r[100];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n;
	cin>>n;
	ll a[n];
	for(ll i=0;i<n;i++)
	{
		cin>>a[i];
	}
	ll out=0;
	ll b=0;
	for(ll i=0;i<n;i++)
	{
		if(a[i]>b)
		{
			ll x=a[i]-b-1;
			out+=((x)*(x+1))/2;
			b=a[i];
		}
		else
		{
			out+=b-a[i];
		}
	}
	cout<<out;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll mod=998244353;
ll mul(ll a, ll b)
{
	return ((a%mod)*(b%mod))%mod;
}
ll add(ll a, ll b)
{
	return ((a%mod)+(b%mod))%mod;
}
ll sub(ll a, ll b)
{
	return ((a%mod)-(b%mod)+mod)%mod;
}
bool c[100],r[100];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n;
	cin>>n;
	ll a[n];
	ll b[n];
	for(ll i=0;i<n;i++)
	{
		cin>>a[i];
		b[i]=a[i]+1;

	}
	for(ll i=1;i<n;i++)
	{
		b[i]=max(b[i],b[i-1]);
	}
	for(ll i=n-2;i>-1;i--)
	{
		b[i]=max(b[i],b[i+1]-1);
	}

	ll out=0;
	for(ll i=0;i<n;i++)
	{

		out+=b[i]-a[i]-1;
	}
	cout<<out;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll mod=998244353;
ll mul(ll a, ll b)
{
	return ((a%mod)*(b%mod))%mod;
}
ll add(ll a, ll b)
{
	return ((a%mod)+(b%mod))%mod;
}
ll sub(ll a, ll b)
{
	return ((a%mod)-(b%mod)+mod)%mod;
}
bool c[100],r[100];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n;
	cin>>n;
	ll a[n];
	for(ll i=0;i<n;i++)
	{
		cin>>a[i];
	}
	ll out=0;
	ll b=1;
	for(ll i=0;i<n;i++)
	{
		if(a[i]+1>b)
		{
			ll x=a[i]-b;
			out+=((x)*(x+1))/2;
			b=a[i]+1;
		}
		else
		{
			out+=b-a[i]-1;
		}
		//cout<<a[i]<<" "<<out<<"\n";
	}
	cout<<out;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;

int main() {
	cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
	int n; cin>>n;
	vector<int> m(n);
	for (int i=0; i<n; i++)
		cin>>m[i];
	vector<int> t(n,0);
	int cur=0;
	for (int i=n-1; i>=0; i--)
		cur=max({0,cur-1,m[i]+1}), t[i]=cur;
	ll ret=0;
	cur=0;
	for (int i=0; i<n; i++)
		cur=max(cur,t[i]), ret+=cur-(m[i]+1);
	cout<<ret<<"\n";
	return 0;
}


/*
 * Author       : YangDavid
 * Created Time : 2020年03月16日 星期一 16时49分37秒
 */

#include<bits/stdc++.h>
#define rep(i, n) for(int i = 1; i <= n; ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 102020;
int n, m[MAXN], ge[MAXN];
int main() {
    scanf("%d", &n);
    rep(i, n) {
        scanf("%d", &m[i]);
        ge[i] = max(ge[i - 1], m[i] + 1);
    }
    for(int i = n - 1; i > 0; --i)
        ge[i] = max(ge[i], ge[i + 1] - 1);
    ll ans = 0;
    for(int i = 1; i <= n; ++i)
        ans += ge[i] - m[i] - 1;
    printf("%lld\n", ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vec<ll> A(N);
    ll h = -1;
    int id = -1;
    vec<ll> ma(N+1,-1),mi(N+1,2e9);
    for(int i=0;i<N;i++){
        cin >> A[i];
        ma[i+1] = max(ma[i],A[i]);
        mi[i+1] = min(mi[i],A[i]);
        if(h<A[i]){
            h = A[i];
            id = i;
        }
    }
    ll ans = 0;
    for(int i=id+1;i<N;i++) ans += h-A[i];
    for(int i=id-1;i>=0;i--){
        if(ma[i+1]<h) h--;
        ans += h-A[i];
    }
    cout << ans << "\n";
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vec<ll> A(N);
    ll h = -1;
    int id = -1;
    vec<ll> ma(N+1,-1),mi(N+1,2e9);
    for(int i=0;i<N;i++){
        cin >> A[i];
        ma[i+1] = max(ma[i+1],A[i]);
        mi[i+1] = min(mi[i+1],A[i]);
        if(h<A[i]){
            h = A[i];
            id = i;
        }
    }
    ll ans = 0;
    for(int i=id+1;i<N;i++) ans += h-A[i];
    for(int i=id-1;i>=0;i--){
        if(ma[i+1]<h) h--;
        ans += h-A[i];
    }
    cout << ans << "\n";
}


#include <algorithm>
#include <cassert>
#include <iostream>

using namespace std;

int m[100001], at[100001];

int main() {
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    int maxi = 0;

    for (int i = 0; i < n; i++) {
        cin >> m[i];
        maxi = max(m[i]+1, maxi);
        at[i] = max(at[i-1], maxi);
    }

    for (int i = n-2; i >= 0; i--)
        at[i] = max(at[i], at[i+1]-1);

    for (int i = 1; i < n; i++)
        assert(at[i] >= at[i-1]);

    long long ans = 0;
    for (int i = 0; i < n; i++)
        ans += at[i] - m[i] - 1;

    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long

const int N=1e5+5;

int n;
int a[N], b[N];

int32_t main()
{
	IOS;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		b[i]=a[i];
	}
	b[n]=a[n]+1;
	for(int i=n-1;i>=1;i--)
	{
		b[i]=max(a[i]+1, b[i+1]-1);
	}
	for(int i=1;i<=n;i++)
	{
		b[i]=max(b[i], b[i-1]);
	}
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		ans+=max(0LL, (b[i] - 1 - a[i]));
	}
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long
#define double long double

#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl

template<typename T> T gcd(T a,T b) { if(a==0) return b; return gcd(b%a,a); }
template<typename T> T pow(T a,T b, long long m){T ans=1; while(b>0){ if(b%2==1) ans=(ans*a)%m; b/=2; a=(a*a)%m; } return ans%m; }

const int N=1e5+5;

int n;
int a[N], b[N];

int32_t main()
{
	IOS;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		b[i]=a[i];
	}
	b[n]=a[n]+1;
	for(int i=n-1;i>=1;i--)
	{
		b[i]=max(a[i]+1, b[i+1]-1);
	}
	for(int i=1;i<=n;i++)
	{
		b[i]=max(b[i], b[i-1]);
	}
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		ans+=max(0LL, (b[i] - 1 - a[i]));
	}
	cout<<ans;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <set>
#include <utility>
#include <iomanip>
#include <map>
#include <string>
#define INF 1000000007
using namespace std;
typedef long long s64;
typedef unsigned long long u64;
int n;
u64 tot;
u64 E[100100];
int hitpoint[100100];
int main() {
	cin >> n;
	for (int i=0; i<n; ++i) {
		cin >> E[i];
	}
	for (int i=0; i<n; ++i) {
		hitpoint[i] = n;
	}
	for (int i=0; i<n; ++i) {
		if (i < hitpoint[E[i]]) {
			hitpoint[E[i]] = i;
		}
	}
	for (int i=n-2; i>-1; --i) {
		if (hitpoint[i+1] != n && hitpoint[i] >= hitpoint[i+1]) {
			hitpoint[i] = hitpoint[i+1]-1;
		}
	}
	hitpoint[0] = 0;
	tot = 0;
	for (int i=0; i<n; ++i) {
		for (int j=hitpoint[i]; j<hitpoint[i+1]; ++j) {
			tot += i-E[j];
		}
	}
	cout << tot << endl;
	return 0;
}


#define  _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL

#define MOD 998244353LL
#define seg_size 262144
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

void init() {
    iostream::sync_with_stdio(false);
    cout << fixed << setprecision(20);
}

unsigned long xor128() {
    static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    unsigned long t = (x ^ (x << 11));
    x = y; y = z; z = w;
    return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

#define int long long
/*
int road[200][200];
int sea[200][200];

void solve(){
    while (true) {
        int n, m;
        cin >> n >> m;
        if (n == 0) return;
        REP(i, n) {
            REP(q, n) {
                road[i][q] = 1e9;
                sea[i][q] = 1e9;
            }
            road[i][i] = 0;
            sea[i][i] = 0;
        }
        REP(i, m) {
            int a, b, c;
            cin >> a >> b >> c;
            a--; b--;
            string d;
            cin >> d;
            if (d == "L") {
                road[a][b] = min(road[a][b], c);
                road[b][a] = min(road[b][a], c);
            }
            else {
                sea[a][b] = min(sea[a][b], c);
                sea[b][a] = min(sea[b][a], c);
            }
        }
        int query;
        cin >> query;
        vector<int> req;
        REP(i, query) {
            int a;
            cin >> a;
            a--;
            req.push_back(a);
        }
        vector<vector<int>> dist{ query,vector<int>(n,1e9) };
        dist[0][req[0]] = 0;
        for (int i = 0; i < query - 1; ++i) {
            REP(q, n) {
                dist[i + 1][q] = min(dist[i + 1][q], dist[i][q] + road[req[i]][req[i + 1]]);
            }
            vector<int> goi(n, 1e9);
            priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> next;
            REP(q, n) {
                goi[q] = dist[i][q];
                next.push(mp(dist[i][q], q));
            }
            while (next.empty() == false) {
                pair<int, int> now = next.top();
                next.pop();
                if (goi[now.second] != now.first) continue;
                REP(q, n) {
                    if (goi[q] > sea[now.second][q] + now.first) {
                        goi[q] = sea[now.second][q] + now.first;
                        next.push(mp(goi[q], q));
                    }
                }
            }
        }
    }
}
*/
void solve() {
    int n;
    cin >> n;
    vector<int> inputs;
    REP(i, n) {
        int a;
        cin >> a;
        inputs.push_back(a);
    }
    int ans = 0;
    int now_max = 0;
    vector<int> geko;
    REP(i, n) {
        now_max = max(now_max, inputs[i]);
        geko.push_back(now_max);
    }
    for (int q = (int)geko.size() - 2; q >= 0; --q) {
        geko[q] = max(geko[q + 1] - 1LL, geko[q]);
    }
    REP(i, geko.size()) {
        ans += geko[i] - inputs[i];
    }
    cout << ans << endl;
}
#undef int
int main() {
    init();
    solve();
}


#define  _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL

#define MOD 998244353LL
#define seg_size 262144
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

void init() {
    iostream::sync_with_stdio(false);
    cout << fixed << setprecision(20);
}

unsigned long xor128() {
    static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    unsigned long t = (x ^ (x << 11));
    x = y; y = z; z = w;
    return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

#define int long long
/*
int road[200][200];
int sea[200][200];

void solve(){
    while (true) {
        int n, m;
        cin >> n >> m;
        if (n == 0) return;
        REP(i, n) {
            REP(q, n) {
                road[i][q] = 1e9;
                sea[i][q] = 1e9;
            }
            road[i][i] = 0;
            sea[i][i] = 0;
        }
        REP(i, m) {
            int a, b, c;
            cin >> a >> b >> c;
            a--; b--;
            string d;
            cin >> d;
            if (d == "L") {
                road[a][b] = min(road[a][b], c);
                road[b][a] = min(road[b][a], c);
            }
            else {
                sea[a][b] = min(sea[a][b], c);
                sea[b][a] = min(sea[b][a], c);
            }
        }
        int query;
        cin >> query;
        vector<int> req;
        REP(i, query) {
            int a;
            cin >> a;
            a--;
            req.push_back(a);
        }
        vector<vector<int>> dist{ query,vector<int>(n,1e9) };
        dist[0][req[0]] = 0;
        for (int i = 0; i < query - 1; ++i) {
            REP(q, n) {
                dist[i + 1][q] = min(dist[i + 1][q], dist[i][q] + road[req[i]][req[i + 1]]);
            }
            vector<int> goi(n, 1e9);
            priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> next;
            REP(q, n) {
                goi[q] = dist[i][q];
                next.push(mp(dist[i][q], q));
            }
            while (next.empty() == false) {
                pair<int, int> now = next.top();
                next.pop();
                if (goi[now.second] != now.first) continue;
                REP(q, n) {
                    if (goi[q] > sea[now.second][q] + now.first) {
                        goi[q] = sea[now.second][q] + now.first;
                        next.push(mp(goi[q], q));
                    }
                }
            }
        }
    }
}
*/
void solve() {
    int n;
    cin >> n;
    vector<int> inputs;
    REP(i, n) {
        int a;
        cin >> a;
        inputs.push_back(a);
    }
    int ans = 0;
    int now_max = 0;
    REP(i, n) {
        if (inputs[i] < now_max) {
            ans += now_max - inputs[i];
        }
        else {
            int hoge = inputs[i] - now_max;
            ans += (hoge - 1LL) * hoge / 2LL;
            now_max = inputs[i];
        }
    }
    cout << ans << endl;
}
#undef int
int main() {
    init();
    solve();
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <unordered_map>
#include <iomanip>
#include <cmath>
#include <queue>
#include <bitset>
#include <numeric>
#include <array>
#include <cstring>
#include <random>
#include <chrono>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {

  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

  int n = 0;

  cin >> n;

  vector<ll> a(n), need(n);

  for(int i = 0; i < n; i++) {
    cin >> a[i];
    need[i] = a[i];
    if(i) need[i] = max(need[i], need[i - 1]);
  }

  for(int i = n - 2; i >= 0; i--) need[i] = max(need[i], need[i + 1] - 1);

  ll ans = 0;

  for(int i = 0; i < n; i++) ans += need[i] - a[i];

  cout << ans;

  return 0;
}
/*

*/


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define ul unsigned long
#define ld long double
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

using namespace std;
using namespace __gnu_pbds;

typedef tree
<
ll,
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update
> or_st;

ll N=998244353  ,M=2e5;
ll i,j;
ll x,y,z,n,m,k,l,r,t,ma=0,x2,y2,z2,x3,y3,x4,y4,f,a[510000],b[510000],dp[500100],d[500100],p[510000];
ll ans,ans2;
string s,s2,s3;
char c[510000];
vector<ll>v[510000],v2,v3;

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
        b[i]=a[i];
    }
    for(i=n;i>=1;i--)
    {
        b[i]=max(b[i],b[i+1]-1);
    }
    for(i=1;i<=n;i++)
    {
        b[i]=max(b[i],b[i-1]);
        ans+=b[i]-a[i];
    }
    cout<<ans;

}


#include <bits/stdc++.h>

using namespace std;

priority_queue<pair<int, int> > q;
int query[1000005];
int n, ans=0;
signed main()
{
	cin>>n;
	cin>>ans;
	for(int i=2; i<=n; i++)
	{
		int mi;
		cin>>mi;
		query[i]=mi;
		q.emplace(mi-i, -i);
	}
	int maxx=1, tk=1;
	int ans=0;
	while(q.size())
	{
		pair<int, int> temp=q.top();
		q.pop();
		if(maxx>-temp.second)
		{
			continue;
		}
		temp.first-=temp.second;
		temp.first++;
//		cout<<temp.first<<" "<<temp.second<<endl;
		for(int i=maxx+1; i<=-temp.second-max((temp.first-tk), 0); i++)
		{
			ans+=tk-query[i]-1;
		}
//		cout<<ans<<endl;
		for(int i=-temp.second-max((temp.first-tk), 0)+1; i<=-temp.second; i++)
		{
//			cout<<i<<" jfoienslkg"<<endl;
			tk++;
			ans+=tk-query[i]-1;
		}
		maxx=-temp.second;
//		cout<<ans<<endl;
	}
	cout<<ans;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;

priority_queue<pair<int, int> > q;
int query[1000005];
int n, ans=0;
signed main()
{
	cin>>n;
	cin>>ans;
	for(int i=2; i<=n; i++)
	{
		int mi;
		cin>>mi;
		query[i]=mi;
		q.emplace(mi-i, -i);
	}
	int maxx=1, tk=1;
	int ans=0;
	while(q.size())
	{
		pair<int, int> temp=q.top();
		q.pop();
		if(maxx>-temp.second)
		{
			continue;
		}
		temp.first-=temp.second;
		temp.first++;
//		cout<<temp.first<<" "<<temp.second<<endl;
		for(int i=maxx+1; i<=-temp.second-max((temp.first-tk), 0ll); i++)
		{
			ans+=tk-query[i]-1;
		}
//		cout<<ans<<endl;
		for(int i=-temp.second-max((temp.first-tk), 0ll)+1; i<=-temp.second; i++)
		{
//			cout<<i<<" jfoienslkg"<<endl;
			tk++;
			ans+=tk-query[i]-1;
		}
		maxx=-temp.second;
//		cout<<ans<<endl;
	}
	cout<<ans;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;

priority_queue<pair<int, int> > q;
int query[1000005];
int n, ans=0;
signed main()
{
	cin>>n;
	cin>>ans;
	for(int i=2; i<=n; i++)
	{
		int mi;
		cin>>mi;
		query[i]=mi;
		q.emplace(mi-i, -i);
	}
	int maxx=1, tk=1;
	int ans=0;
	while(q.size())
	{
		pair<int, int> temp=q.top();
		q.pop();
		if(maxx>-temp.second)
		{
			continue;
		}
		temp.first-=temp.second;
		temp.first++;
//		cout<<temp.first<<" "<<temp.second<<endl;
		for(int i=maxx+1; i<=-temp.second-max((temp.first-tk), 0); i++)
		{
			ans+=tk-query[i]-1;
		}
//		cout<<ans<<endl;
		for(int i=-temp.second-max((temp.first-tk), 0)+1; i<=-temp.second; i++)
		{
//			cout<<i<<" jfoienslkg"<<endl;
			tk++;
			ans+=tk-query[i]-1;
		}
		maxx=-temp.second;
//		cout<<ans<<endl;
	}
	cout<<ans;
}


#include <set>
#include <map>
#include <queue>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <bitset>
#include <vector>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
typedef double db;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pa;
typedef unsigned int uint;
typedef unsigned long long ull;
#define w1 first
#define ls (x<<1)
#define w2 second
#define ins insert
#define rs (x<<1|1)
#define mp make_pair
#define pb push_back
#define mid ((l+r)>>1)
#define sqr(x) ((x)*(x))
#define cle(x) ((x).clear())
#define lowbit(x) ((x)&(-x))
#define SZ(x) (int((x).size()))
#define All(x) (x).begin(),(x).end()
#define ms(x,y) memset(x,y,sizeof (x))
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define rep2(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define per(i,a,b) for(int (i)=(a);(i)>=(b);(i)--)
#define Rep(p,x) for(int (p)=head[(x)];(p);(p)=nxt[(p)])
#define Rep2(p,x) for(int (p)=cur[(x)];(p);(p)=nxt[(p)])
template<class T>inline void read(T&num){
	num=0;T f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();
	num*=f;
}
inline int getgcd(int x,int y){if(!x)return y;return getgcd(y%x,x);}
inline int power(int x,int k,int p){int res=1;for(;k;k>>=1,x=(ll)x*x%p)if(k&1)res=(ll)res*x%p;return res;}
const double pi=acos(-1);
inline void judge(){
	freopen("input.txt","r",stdin);
}
//********************************head*************************************
const int maxn=1e5+5;
int n,tot;
int m[maxn],hhy[maxn];
ll ans;
int main(){
	read(n);
	rep(i,1,n)read(m[i]);
	per(i,n,1){
		hhy[i]=m[i]+1-i;
		if(i<n)hhy[i]=max(hhy[i],hhy[i+1]);
	}
	rep(i,1,n){
		hhy[i]+=i;
		if(i>1)hhy[i]=max(hhy[i],hhy[i-1]);
		ans+=hhy[i]-m[i]-1;
	}
	cout<<ans<<endl;
	return 0;
}


#pragma GCC optimize("O2")
#pragma GCC target("avx,avx2,sse,fma,tune=native")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pi = pair<ll, ll>;
const int maxn = 1e5 + 666, mlg = 18, mod =1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, t, m = 0, a[maxn], d[maxn];
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for(int i = 0; i < n; i++) {
		cin >> a[i];
		d[i]=a[i]+1;
		if(i) d[i] = max(d[i], d[i-1]);
	}
	for(int i = n-1; i--;)
		d[i] = max(d[i], d[i+1]-1);
	ll ans = 0;
	for(int i = 0; i < n; i++) {
		ans += d[i]-a[i]-1;
	}
	cout << ans;
	return 0;
}


#include <iostream>
#include <iomanip>
#include <utility>
#include <cstdlib>
#include <cstdint>
#include <cstdio>
#include <vector>
#include <string>
#include <deque>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <cstring>
#include <assert.h>
#include <chrono>
#include <random>
#include <functional>
#include <bitset>
#include <iterator>
#include <unordered_map>
#include <unordered_set>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int INF = (int)1e9;
const int MAXN = 2010;

int main() {
  ios_base::sync_with_stdio(false);
  srand(time(0));
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = -(a[i] + 1);
  }
  int shit = -*min_element(b.begin(), b.end());
  vector<int> p(n);
  int have = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i; j >= 0; j--) {
      if (have >= -b[i]) break;
      if (!p[j]) {
	p[j] = 1;
	have++;
      }
    }
  }
  ll ans = 0;
  int lines = 0;
  for (int i = 0; i < n; i++) {
    lines += p[i];
    ans += lines - a[i] - 1;
  }
  cout << ans << '\n';
  return 0;
}


#include <iostream>
#include <iomanip>
#include <utility>
#include <cstdlib>
#include <cstdint>
#include <cstdio>
#include <vector>
#include <string>
#include <deque>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <cstring>
#include <assert.h>
#include <chrono>
#include <random>
#include <functional>
#include <bitset>
#include <iterator>
#include <unordered_map>
#include <unordered_set>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int INF = (int)1e9;
const int MAXN = 2010;

template<typename T>
struct FenwickTree {
  vector<T> bit;
  FenwickTree(int sz) : bit(sz + 1) {}
  void update(int x, T delta) {
    for (int i = ++x; i < bit.size(); i += i & -i)
      bit[i] += delta;
  }
  T query(int x) {
    T ans{};
    for (int i = ++x; i > 0; i -= i & -i)
      ans += bit[i];
    return ans;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  srand(time(0));
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = -(a[i] + 1);
  }
  int shit = -*min_element(b.begin(), b.end());
  FenwickTree<int> T(n);
  vector<int> p(n);
  int have = 0;
  for (int i = 0; i < n; i++) {
    int j = i;
    while (have < -b[i]) {
      int lo = 0;
      int hi = j;
      while (lo < hi) {
	int mid = (lo + hi) / 2;
	if (T.query(j) - T.query(j - mid - 1) != mid + 1) {
	  hi = mid;
	} else {
	  lo = mid + 1;
	}
      }
      p[j - lo] = 1;
      T.update(j - lo, +1);
      j -= (lo + 1);
      have++;
    }
  }
  ll ans = 0;
  int lines = 0;
  for (int i = 0; i < n; i++) {
    lines += p[i];
    ans += lines - a[i] - 1;
  }
  cout << ans << '\n';
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin >> n;
    vector<int> v(n);
    for (int i = 0; i < n; i++) {
        cin >> v[i];
    }

    int marks = 0;
    vector<bool> new_mark(n, false);
    vector<int> p(n);
    for (int i = 0; i < n; i++) {
        p[i] = i;
    }

    long long under = 0;

    for (int i = 0; i < n; i++) {
        int above = v[i];
        int atleast = above + 1;

        if (atleast <= marks) {
            // nothing
            // under += marks - atleast;
        } else {
            // add more marks
            long long additional = atleast - marks;

            int cur = i;
            for (int j = 0; j < additional; j++) {
                while (p[cur] != cur)
                    cur = p[cur];
                p[cur] = cur-1;
                new_mark[cur] = true;
            }
            p[i] = cur - 1;

            // under += additional * (additional - 1) / 2;
            marks = atleast;
        }
    }

    marks = 0;
    for (int i = 0; i < n; i++) {
         if (new_mark[i])
             marks++;
         int above = v[i];
         under += marks - above - 1;
    }
    cout << under << endl;

}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin >> n;
    vector<int> v(n);
    for (int i = 0; i < n; i++) {
        cin >> v[i];
    }

    int marks = 0;
    long long under = 0;

    for (int above : v) {
        int atleast = above + 1;

        if (atleast <= marks) {
            under += marks - atleast;
        } else {
            // add more marks
            long long additional = atleast - marks;
            under += additional * (additional - 1) / 2;
            marks = atleast;
        }
    }

    cout << under << endl;
}


#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 100005;

int n;
int a[N], mx[N];
LL ans;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    mx[i] = max(mx[i - 1], a[i]);
  }
  for (int i = 1; i <= n; ++i) {
    ans += max(mx[i], mx[i + 1] - 1) - a[i];
  }
  printf("%lld\n", ans);
  return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 100005;

int n;
int a[N], mx[N];
LL ans;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    mx[i] = max(mx[i - 1], a[i]);
  }
  for (int i = n; i >= 1; --i) {
    mx[i] = max(mx[i], mx[i + 1] - 1);
    ans += mx[i] - a[i];
  }
  printf("%lld\n", ans);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define long long long
#define MAXN 200002

int n, a[MAXN], t[MAXN];

void solve() {
    cin >> n;
    FOR (i, 1, n) {
        cin >> a[i];
    }

    // find some bound, some maybe wrong
    for (int i = n; i >= 1; --i) {
        t[i] = max(t[i + 1] - 1, a[i] + 1);
    }

    long sum = 0;

    // correct previous bounds
    FOR (i, 1, n) {
        t[i] = max(t[i - 1], t[i]);
        sum += t[i] - a[i] - 1;
    }

    // hopefully the answer is correct
    cout << sum << "\n";
}

int main() {
//    freopen("main.in", "r", stdin);
    ios::sync_with_stdio(0);
    solve();
}


#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()

#ifdef LOCAL
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define dump(x) true
#endif

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

template<class T, class U> void chmin(T& t, const U& u) { if (t > u) t = u; }
template<class T, class U> void chmax(T& t, const U& u) { if (t < u) t = u; }

template<class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os<<"("<<p.first<<","<<p.second<<")";
	return os;
}

template<class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
	os<<"{";
	rep(i, v.size()) {
		if (i) os<<",";
		os<<v[i];
	}
	os<<"}";
	return os;
}

const int INF = TEN(9);

int main() {
	int n; cin >> n;
	V<int> m(n); rep(i, n) cin >> m[i];

	V<int> mx(n+1);
	rep(i, n) mx[i] = m[i] - i;
	mx[n] = -INF;
	for (int i = n-1; i >= 0; --i) {
		chmax(mx[i], mx[i+1]);
	}

	ll s = 0;
	int cnt = 0;

	rep(i, n) {
		if (cnt == m[i]) {
			++cnt;
		} else if (mx[i+1] <= cnt - i - 1) {
			s += cnt - m[i] - 1;
		} else {
			s += cnt - m[i];
			++cnt;
		}
	}
	cout << s << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int Maxn = 100005;

int n;
int a[Maxn];
int mx[Maxn];
int cur[Maxn];
ll res;

int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		mx[i] = max(mx[i - 1], a[i]);
	}
	int got = mx[n] + 1;
	for (int i = n; i > 0; i--) {
		got = max(got - 1, mx[i] + 1);
		cur[i] = got;
		res += cur[i] - a[i] - 1;
	}
	cout << res << endl;
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
inline int in() { int x; scanf("%d",&x); return x;}
template<typename T>inline istream& operator>>(istream&i,v(T)&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const v(T)&v)
{stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>inline ostream& operator<<(ostream&o,const v(T)&v)
{if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<typename T1,typename T2>inline ostream& operator<<(ostream&o,const pair<T1,T2>&v)
{return o<<v.fi<<","<<v.se;}
template<typename T>inline ll suma(const v(T)& a) { ll res(0); for (auto&& x : a) res += x; return res;}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;

int main() {
  int n;
  scanf("%d",&n);
  vi a(n);
  cin>>a;
  vi b = a;
  drep(i,n-1) {
    maxs(b[i], b[i+1]-1);
  }
  rep(i,n-1) maxs(b[i+1], b[i]);
  ll ans = 0;
  rep(i,n) {
    ans += b[i]-a[i];
  }
  cout<<ans<<endl;
  return 0;
}


#include <bits/stdc++.h>
typedef long long LL;
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int,int>
#define VI vector<int>
#define VL vector<long long>
#define VPII vector<pair<int,int> >
#define PLL pair<long long,long long>
#define VPLL vector<pair<long long,long long> >
#define F first
#define S second

using namespace std;

int main(){
	int n;
	RI(n);
	VI v(n);
	VI m(n);
	REP(i,n) {
		RI(v[i]);
		m[i]=(i>0)?max(m[i-1],v[i]+1):1;
		}
	int mymax=0;
	for (int i=n-1;i>=0;i--){
		m[i]=max(mymax-1,m[i]);
		mymax=m[i];
	}
	LL res=0;
	REP (i,n) {
		//cout<<v[i]<<" "<<m[i]<<endl;
		res+=m[i]-v[i]-1;
		}
	printf("%I64d",res);
}


#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acos(-1.0);
//typedef vector<vector<ll>> mat;
typedef vector<int> vec;

ll mod_pow(ll a, ll n, ll m) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * a%m;
		a = a * a%m; n >>= 1;
	}
	return res;
}

struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n%mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint &a, modint b) { a.n = ((ll)a.n*b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, int n) {
	if (n == 0)return modint(1);
	modint res = (a*a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 1 << 18;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
using mP = pair<modint, modint>;

int dx[4] = { 0,1,0,-1 };
int dy[4] = { 1,0,-1,0 };

void solve() {
	ll ans = 0;
	ll cur = 0;
	int n; cin >> n;
	vector<int> m(n);
	rep(i, n)cin >> m[i];

	vector<int> inf(n);
	inf[n - 1] = m[n - 1];
	for (int i = n - 2; i >= 0; i--) {
		inf[i] = max(inf[i + 1] - 1, m[i]+1);
	}
	rep(i, n) {
		if (cur - 1 >= m[i]) {
			ans += cur - 1 - m[i];
		}
		else if (cur == m[i]) {
			cur++;
		}
		else {
			cur = m[i];
		}
		if (cur < inf[i]) {
			cur++; ans++;
		}
		assert(cur >= inf[i]);
		//cout << i << " " << cur << endl;
	}
	cout << ans << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	//init_f();
	//init();
	//int t; cin >> t; rep(i, t)solve();
	solve();
	stop
		return 0;
}


#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acos(-1.0);
//typedef vector<vector<ll>> mat;
typedef vector<int> vec;

ll mod_pow(ll a, ll n, ll m) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * a%m;
		a = a * a%m; n >>= 1;
	}
	return res;
}

struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n%mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint &a, modint b) { a.n = ((ll)a.n*b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, int n) {
	if (n == 0)return modint(1);
	modint res = (a*a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 1 << 18;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
using mP = pair<modint, modint>;

int dx[4] = { 0,1,0,-1 };
int dy[4] = { 1,0,-1,0 };

void solve() {
	ll ans = 0;
	ll cur = 0;
	int n; cin >> n;
	vector<int> m(n);
	rep(i, n)cin >> m[i];

	vector<int> inf(n);
	inf[n - 1] = m[n - 1]+1;
	for (int i = n - 2; i >= 0; i--) {
		inf[i] = max(inf[i + 1] - 1, m[i]+1);
	}
	rep(i, n) {
		if (cur - 1 >= m[i]) {
			ans += cur - 1 - m[i];
		}
		else if (cur == m[i]) {
			cur++;
		}
		else {
			cur = m[i];
		}
		if (cur < inf[i]) {
			cur++; ans++;
		}
		assert(cur >= inf[i]);
		//cout << i << " " << cur << endl;
	}
	cout << ans << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	//init_f();
	//init();
	//int t; cin >> t; rep(i, t)solve();
	solve();
	stop
		return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>

using namespace std;
long long all[100005]={0};
long long need[100005]={0};
int main()
{
    long long now=0,N,i,ans=0;
    scanf("%lld",&N);
    for(i=0;i<N;i++) scanf("%lld",&all[i]);
    need[N]=0;
    for(i=N-1;i>=0;i--) need[i]=max(all[i]+1,need[i+1]-1);
    for(i=0;i<N;i++)
    {
        if(now<need[i]) now++;

        ans+=now-all[i]-1;
    }
    printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin>>n;
	vi m(n);
	vi mx(n,0);
	ll ans=0;
	for(int i=0;i<n;i++){
		cin>>m[i];
		if(i)mx[i]=max(mx[i-1],m[i]);
		else mx[i]=m[i];
	}
	for(int i=n-2;i>=0;i--){
		mx[i]=max(mx[i],mx[i+1]-1);
	}
	for(int i=0;i<n;i++){
		ans+=mx[i]-m[i];
	}
	cout<<ans<<endl;
	return 0;
}