#include<cstdio>
#include<iostream>
using namespace std;
int B[100009];
struct ND
{
    int x,y,z;
}A[100009];
int main()
{
    //freopen("in.txt", "r", stdin);
    int m,n,i,l,c, type,k,p;
    scanf("%d", &m);
    for(i=1; i<=m; i++)
    {
        scanf("%d", &A[i].x);
        if(A[i].x==1) scanf("%d", &A[i].y),B[i]=B[i-1]+1;
        else
        {
            scanf("%d %d", &A[i].y,&A[i].z);
            B[i]=B[i-1]+A[i].y*A[i].z;
        }
    }
    scanf("%d", &n);//cout<<n<<"\n";
    for(i=0; i<n; i++)
    {
        scanf("%d", &p);//cout<<"p:"<<p<<" ";
        if(i) putchar(' ');

        while(true)
        {
            k=lower_bound(B,B+m+1,p)-B;//cout<<"k:"<<k<<"\n";
            if(A[k].x==1){printf("%d", A[k].y); break;}
            else
            {
                p-=B[k-1]+1;//cout<<"p"<<p;
                p=p%A[k].y;//cout<<"p"<<p;
                p++;
            }
        }
    }
    printf("\n");
    return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;
long long k=1,B[20000000];
int main()
{
    //freopen("in.txt", "r", stdin);
    int m,n,i,l,c,x,A[100009], type;
    scanf("%d", &m);
    for(i=0; i<m; i++)
    {
        scanf("%d", &type);
        if(type==1) cin>>B[k++];
        else
        {
            scanf("%d %d", &l,&c);
            if(k<20000000){
            for( ; c>0; c--)
            {
                for(x=1;x<=l;x++)
                {
                    B[k++]=B[x];
                }
            }
            }
        }
    }
    scanf("%d %d", &n, &x);cout<<B[x];
    for(i=1; i<n; i++)
    {
        scanf("%d", &x);
        cout<<" "<<B[x];
    }
    printf("\n");
    return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;
long long B[20000000];
int main()
{
   // freopen("in.txt", "r", stdin);
    int m,n,i,k=1,l,c,x,A[100009], type;
    scanf("%d", &m);
    for(i=0; i<m; i++)
    {
        scanf("%d", &type);
        if(type==1) cin>>B[k++];
        else
        {
            scanf("%d %d", &l,&c);
            for( ; c>0; c--)
            {
                for(x=1;x<=l;x++)
                {
                    B[k++]=B[x];
                }
            }
        }
    }
    scanf("%d %d", &n, &x);cout<<B[x];
    for(i=1; i<n; i++)
    {
        scanf("%d", &x);
        cout<<" "<<B[x];
    }
    printf("\n");
    return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;
long long B[10000009];
int main()
{
    //freopen("in.txt", "r", stdin);
    int m,n,i,k=1,l,c,x,A[100009], type;
    scanf("%d", &m);
    for(i=0; i<m; i++)
    {
        scanf("%d", &type);
        if(type==1) cin>>B[k++];
        else
        {
            scanf("%d %d", &l,&c);
            for( ; c>0; c--)
            {
                for(x=1;x<=l;x++)
                {
                    B[k++]=B[x];
                }
            }
        }
    }
    scanf("%d %d", &n, &x);cout<<B[x];
    for(i=1; i<n; i++)
    {
        scanf("%d", &x);
        cout<<" "<<B[x];
    }
    printf("\n");
    return 0;
}


#include <bits/stdc++.h>
#define pp pair<int, int>
#define mp make_pair
#define pb push_back
#define MAX 1000000001
#define MOD 1000000007

#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)

using namespace std;

int main(){
	ios::sync_with_stdio(false);
	int m,n;
	cin>>m;
	pair<int, long long> stages[m];
	REP(i,m){
		int p,q,r;
		cin>>p;
		if(p==1){
			cin>>q;
			stages[i]=mp(p,q);
		}
		else{
			cin>>q>>r;
			stages[i]=mp(p,10001*q+r);
		}
	}
	cin>>n;
	long long a[n];
	REP(i,n){
		cin>>a[i];
	}
	int nums[100000];
	int i=0,j=0;
	long long len=0,t;
	while(len<100000){
		if(stages[i].first==1){
			nums[len]=stages[i].second;
			len++;
		}
		else{
			int l=stages[i].second/10001,c=stages[i].second%10001;
			bool fl=false;
			if(len+l*c >= 100000){
				t=len;
			}
			if(len+l*c>100000)fl=true;
			// else{
			REP(q,c){
				REP(p,l){
					nums[len]=nums[p];
					len++;
					if(len==100000)break;
				}
				if(len==100000)break;
			}
			if(len==100000){
				len=t+l*c;
				if(fl)break;
			}
			// }
		}
		i++;
		if(i==m)break;
	}
	while(j!=n){
		if(a[j]<=100000){
			cout<<nums[a[j]-1]<<" ";
			j++;
		}
		else if(a[j]<=len){
			int l=stages[i].second/10001,c=stages[i].second%10001;
			long long t=len-l*c;
			long long index=(a[j]-t-1)%l;
			cout<<nums[index]<<" ";
			j++;
		}
		else{
			if(stages[i].first==1 && a[j]==len+1){
				cout<<stages[i].second<<" ";
				len++;
				j++;
				i++;
			}
			else if(stages[i].first==1){
				len++;
				i++;
			}
			else{
				int l=stages[i].second/10001,c=stages[i].second%10001;
				long long z=len+l*c;
				if(z < a[j]){
					len+=l*c;
					i++;
				}
				else{
					long long index=(a[j]-len-1)%l;
					cout<<nums[index]<<" ";
					j++;
					//i++;
				}
			}
		}
	}
}


#include <bits/stdc++.h>
#define pp pair<int, int>
#define mp make_pair
#define pb push_back
#define MAX 1000000001
#define MOD 1000000007

#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)

using namespace std;

int main(){
	ios::sync_with_stdio(false);
	int m,n;
	cin>>m;
	pair<int, long long> stages[m];
	REP(i,m){
		int p,q,r;
		cin>>p;
		if(p==1){
			cin>>q;
			stages[i]=mp(p,q);
		}
		else{
			cin>>q>>r;
			stages[i]=mp(p,10001*q+r);
		}
	}
	cin>>n;
	long long a[n];
	REP(i,n){
		cin>>a[i];
	}
	int nums[100000];
	int i=0,j=0;
	long long len=0,flag=1,t;
	while(len<100000){
		if(stages[i].first==1){
			nums[len]=stages[i].second;
			len++;
		}
		else{
			int l=stages[i].second/10001,c=stages[i].second%10001;
			bool fl=false;
			if(len+l*c >= 100000){
				t=len;
			}
			if(len+l*c>100000)fl=true;
			// else{
			REP(q,c){
				REP(p,l){
					nums[len]=nums[p];
					len++;
					if(len==100000)break;
				}
				if(len==100000)break;
			}
			if(len==100000){
				len=t+l*c;
				if(fl)break;
			}
			// }
		}
		i++;
	}
	while(j!=n){
		if(a[j]<=100000){
			cout<<nums[a[j]-1]<<" ";
			j++;
		}
		else if(a[j]<=len){
			int l=stages[i].second/10001,c=stages[i].second%10001;
			long long t=len-l*c;
			long long index=(a[j]-t-1)%l;
			cout<<nums[index]<<" ";
			j++;
		}
		else{
			if(stages[i].first==1 && a[j]==len+1){
				cout<<stages[i].second<<" ";
				len++;
				j++;
				i++;
			}
			else if(stages[i].first==1){
				len++;
				i++;
			}
			else{
				int l=stages[i].second/10001,c=stages[i].second%10001;
				if(len+l*c<a[j]){
					len+=l*c;
					i++;
				}
				else{
					long long index=(a[j]-len-1)%l;
					cout<<nums[index]<<" ";
					j++;
					//i++;
				}
			}
		}
	}
}


#include <bits/stdc++.h>
#define pp pair<int, int>
#define mp make_pair
#define pb push_back
#define MAX 1000000001
#define MOD 1000000007

#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)

using namespace std;

int main(){
	ios::sync_with_stdio(false);
	int m,n;
	cin>>m;
	pair<int, long long> stages[m];
	REP(i,m){
		int p,q,r;
		cin>>p;
		if(p==1){
			cin>>q;
			stages[i]=mp(p,q);
		}
		else{
			cin>>q>>r;
			stages[i]=mp(p,10001*q+r);
		}
	}
	cin>>n;
	long long a[n];
	REP(i,n){
		cin>>a[i];
	}
	int nums[100000];
	int i=0,j=0;
	long long len=0,flag=1,t;
	while(len<100000){
		if(stages[i].first==1){
			nums[len]=stages[i].second;
			len++;
		}
		else{
			int l=stages[i].second/10001,c=stages[i].second%10001;
			bool fl=false;
			if(len+l*c >= 100000){
				t=len;
			}
			if(len+l*c>100000)fl=true;
			// else{
			REP(q,c){
				REP(p,l){
					nums[len]=nums[p];
					len++;
					if(len==100000)break;
				}
				if(len==100000)break;
			}
			if(len==100000){
				len=t+l*c;
				if(fl)break;
			}
			// }
		}
		i++;
		if(i==m)break;
	}
	while(j!=n){
		if(a[j]<=100000){
			cout<<nums[a[j]-1]<<" ";
			j++;
		}
		else if(a[j]<=len){
			int l=stages[i].second/10001,c=stages[i].second%10001;
			long long t=len-l*c;
			long long index=(a[j]-t-1)%l;
			cout<<nums[index]<<" ";
			j++;
		}
		else{
			if(stages[i].first==1 && a[j]==len+1){
				cout<<stages[i].second<<" ";
				len++;
				j++;
				i++;
			}
			else if(stages[i].first==1){
				len++;
				i++;
			}
			else{
				int l=stages[i].second/10001,c=stages[i].second%10001;
				if(len+l*c<a[j]){
					len+=l*c;
					i++;
				}
				else{
					long long index=(a[j]-len-1)%l;
					cout<<nums[index]<<" ";
					j++;
					//i++;
				}
			}
		}
	}
}


#include <bits/stdc++.h>
#define pp pair<int, int>
#define mp make_pair
#define pb push_back
#define MAX 1000000001
#define MOD 1000000007

#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)

using namespace std;

int main(){
	ios::sync_with_stdio(false);
	int m,n;
	cin>>m;
	pair<int, long long> stages[m];
	REP(i,m){
		int p,q,r;
		cin>>p;
		if(p==1){
			cin>>q;
			stages[i]=mp(p,q);
		}
		else{
			cin>>q>>r;
			stages[i]=mp(p,10001*q+r);
		}
	}
	cin>>n;
	long long a[n];
	REP(i,n){
		cin>>a[i];
	}
	int nums[100000];
	int i=0,j=0;
	long long len=0,flag=1,t;
	while(len<100000){
		if(stages[i].first==1){
			nums[len]=stages[i].second;
			len++;
		}
		else{
			int l=stages[i].second/10001,c=stages[i].second%10001;
			if(len+l*c >= 100000){
				t=len;
			}
			// else{
				REP(q,c){
					REP(p,l){
						nums[len]=nums[p];
						len++;
						if(len==100000)break;
					}
					if(len==100000)break;
				}
				if(len==100000){
					len=t+l*c;
					break;
				}
			// }
		}
		i++;
	}
	while(j!=n){
		if(a[j]<=100000){
			cout<<nums[a[j]-1]<<" ";
			j++;
		}
		else if(a[j]<=len){
			int l=stages[i].second/10001,c=stages[i].second%10001;
			long long t=len-l*c;
			long long index=(a[j]-t-1)%l;
			cout<<nums[index]<<" ";
			j++;
		}
		else{
			if(stages[i].first==1 && a[j]==len+1){
				cout<<stages[i].second<<" ";
				len++;
				j++;
				i++;
			}
			else if(stages[i].first==1){
				len++;
				i++;
			}
			else{
				int l=stages[i].second/10001,c=stages[i].second%10001;
				if(len+l*c<a[j]){
					len+=l*c;
					i++;
				}
				else{
					long long index=(a[j]-len-1)%l;
					cout<<nums[index]<<" ";
					j++;
					//i++;
				}
			}
		}
	}
}


#include <bits/stdc++.h>
#define pp pair<int, int>
#define mp make_pair
#define pb push_back
#define MAX 1000000001
#define MOD 1000000007

#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)

using namespace std;

int main(){
	ios::sync_with_stdio(false);
	int m,n;
	cin>>m;
	pair<int, long long> stages[m];
	REP(i,m){
		int p,q,r;
		cin>>p;
		if(p==1){
			cin>>q;
			stages[i]=mp(p,q);
		}
		else{
			cin>>q>>r;
			stages[i]=mp(p,10001*q+r);
		}
	}
	cin>>n;
	long long a[n];
	REP(i,n){
		cin>>a[i];
	}
	int nums[100000];
	int i=0,j=0;
	long long len=0,t;
	while(len<100000){
		if(stages[i].first==1){
			nums[len]=stages[i].second;
			len++;
		}
		else{
			int l=stages[i].second/10001,c=stages[i].second%10001;
			bool fl=false;
			if(len+l*c >= 100000){
				t=len;
			}
			if(len+l*c>100000)fl=true;
			// else{
			REP(q,c){
				REP(p,l){
					nums[len]=nums[p];
					len++;
					if(len==100000)break;
				}
				if(len==100000)break;
			}
			if(len==100000){
				len=t;
				break;
			}
			// }
		}
		i++;
		if(i==m)break;
	}
	while(j!=n){
		if(a[j]<=100000){
			cout<<nums[a[j]-1]<<" ";
			j++;
		}
		// else if(a[j]<=len){
		// 	int l=stages[i].second/10001,c=stages[i].second%10001;
		// 	long long t=len-l*c;
		// 	long long index=(a[j]-t-1)%l;
		// 	cout<<nums[index]<<" ";
		// 	j++;
		// }
		else{
			if(stages[i].first==1 && a[j]==len+1){
				cout<<stages[i].second<<" ";
				len++;
				j++;
				i++;
			}
			else if(stages[i].first==1){
				len++;
				i++;
			}
			else{
				int l=stages[i].second/10001,c=stages[i].second%10001;
				long long z=len+l*c;
				if(z < a[j]){
					len+=l*c;
					i++;
				}
				else{
					long long index=(a[j]-len-1)%l;
					cout<<nums[index]<<" ";
					j++;
					//i++;
				}
			}
		}
	}
}


#include <bits/stdc++.h>
#define pp pair<int, int>
#define mp make_pair
#define pb push_back
#define MAX 1000000001
#define MOD 1000000007

#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)

using namespace std;

int main(){
	ios::sync_with_stdio(false);
	int m,n;
	cin>>m;
	pair<int, long long> stages[m];
	REP(i,m){
		int p,q,r;
		cin>>p;
		if(p==1){
			cin>>q;
			stages[i]=mp(p,q);
		}
		else{
			cin>>q>>r;
			stages[i]=mp(p,10001*q+r);
		}
	}
	cin>>n;
	long long a[n];
	REP(i,n){
		cin>>a[i];
	}
	int nums[100000];
	int i=0,j=0,len=0,flag=1,t;
	while(len<100000){
		if(stages[i].first==1){
			nums[len]=stages[i].second;
			len++;
		}
		else{
			int l=stages[i].second/10001,c=stages[i].second%10001;
			if(len+l*c >= 100000){
				t=len;
			}
			// else{
				REP(q,c){
					REP(p,l){
						nums[len]=nums[p];
						len++;
						if(len==100000)break;
					}
					if(len==100000)break;
				}
				if(len==100000){
					len=t+l*c;
					//break;
				}
			// }
		}
		i++;
	}
	while(j!=n){
		if(a[j]<=100000){
			cout<<nums[a[j]-1]<<" ";
			j++;
		}
		else if(a[j]<=len){
			int l=stages[i].second/10001,c=stages[i].second%10001;
			long long t=len-l*c;
			int index=(a[j]-t-1)%l;
			cout<<nums[index]<<" ";
			j++;
		}
		else{
			if(stages[i].first==1 && a[j]==len+1){
				cout<<stages[i].second<<" ";
				len++;
				j++;
				i++;
			}
			else if(stages[i].first==1){
				len++;
				i++;
			}
			else{
				int l=stages[i].second/10001,c=stages[i].second%10001;
				if(len+l*c<a[j]){
					len+=l*c;
					i++;
				}
				else{
					int index=(a[j]-len-1)%l;
					cout<<nums[index]<<" ";
					j++;
					//i++;
				}
			}
		}
	}
}


#include <bits/stdc++.h>
#define pp pair<int, int>
#define mp make_pair
#define pb push_back
#define MAX 1000000001
#define MOD 1000000007

#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)

using namespace std;

int main(){
	ios::sync_with_stdio(false);
	int m,n;
	cin>>m;
	pair<int, long long> stages[m];
	REP(i,m){
		int p,q,r;
		cin>>p;
		if(p==1){
			cin>>q;
			stages[i]=mp(p,q);
		}
		else{
			cin>>q>>r;
			stages[i]=mp(p,10001*q+r);
		}
	}
	cin>>n;
	long long a[n];
	REP(i,n){
		cin>>a[i];
	}
	int nums[100000];
	int i=0,j=0;
	long long len=0,flag=1,t;
	while(len<100000){
		if(stages[i].first==1){
			nums[len]=stages[i].second;
			len++;
		}
		else{
			int l=stages[i].second/10001,c=stages[i].second%10001;
			if(len+l*c >= 100000){
				t=len;
			}
			// else{
				REP(q,c){
					REP(p,l){
						nums[len]=nums[p];
						len++;
						if(len==100000)break;
					}
					if(len==100000)break;
				}
				if(len==100000){
					len=t+l*c;
					//break;
				}
			// }
		}
		i++;
	}
	while(j!=n){
		if(a[j]<=100000){
			cout<<nums[a[j]-1]<<" ";
			j++;
		}
		else if(a[j]<=len){
			int l=stages[i].second/10001,c=stages[i].second%10001;
			long long t=len-l*c;
			long long index=(a[j]-t-1)%l;
			cout<<nums[index]<<" ";
			j++;
		}
		else{
			if(stages[i].first==1 && a[j]==len+1){
				cout<<stages[i].second<<" ";
				len++;
				j++;
				i++;
			}
			else if(stages[i].first==1){
				len++;
				i++;
			}
			else{
				int l=stages[i].second/10001,c=stages[i].second%10001;
				if(len+l*c<a[j]){
					len+=l*c;
					i++;
				}
				else{
					long long index=(a[j]-len-1)%l;
					cout<<nums[index]<<" ";
					j++;
					//i++;
				}
			}
		}
	}
}


#include <bits/stdc++.h>
#define pp pair<int, int>
#define mp make_pair
#define pb push_back
#define MAX 1000000001
#define MOD 1000000007

#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)

using namespace std;

int main(){
	ios::sync_with_stdio(false);
	int m,n;
	cin>>m;
	pair<int, long long> stages[m];
	REP(i,m){
		int p,q,r;
		cin>>p;
		if(p==1){
			cin>>q;
			stages[i]=mp(p,q);
		}
		else{
			cin>>q>>r;
			stages[i]=mp(p,10001*q+r);
		}
	}
	cin>>n;
	long long a[n];
	REP(i,n){
		cin>>a[i];
	}
	int nums[100000];
	int i=0,j=0;
	long long len=0,t;
	while(len<100000){
		if(stages[i].first==1){
			nums[len]=stages[i].second;
			len++;
		}
		else{
			int l=stages[i].second/10001,c=stages[i].second%10001;
			bool fl=false;
			if(len+l*c >= 100000){
				t=len;
			}
			if(len+l*c>100000)fl=true;
			// else{
			REP(q,c){
				REP(p,l){
					nums[len]=nums[p];
					len++;
					if(len==100000)break;
				}
				if(len==100000)break;
			}
			if(len==100000){
				len=t;
				//break;
			}
			// }
		}
		i++;
		if(i==m)break;
	}
	while(j!=n){
		if(a[j]<=100000){
			cout<<nums[a[j]-1]<<" ";
			j++;
		}
		else if(a[j]<=len){
			int l=stages[i].second/10001,c=stages[i].second%10001;
			long long t=len-l*c;
			long long index=(a[j]-t-1)%l;
			cout<<nums[index]<<" ";
			j++;
		}
		else{
			if(stages[i].first==1 && a[j]==len+1){
				cout<<stages[i].second<<" ";
				len++;
				j++;
				i++;
			}
			else if(stages[i].first==1){
				len++;
				i++;
			}
			else{
				int l=stages[i].second/10001,c=stages[i].second%10001;
				long long z=len+l*c;
				if(z < a[j]){
					len+=l*c;
					i++;
				}
				else{
					long long index=(a[j]-len-1)%l;
					cout<<nums[index]<<" ";
					j++;
					//i++;
				}
			}
		}
	}
}


#include <bits/stdc++.h>
#define pp pair<int, int>
#define mp make_pair
#define pb push_back
#define MAX 1000000001
#define MOD 1000000007

#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)

using namespace std;

int main(){
	ios::sync_with_stdio(false);
	int m,n;
	cin>>m;
	pair<int, long long> stages[m];
	REP(i,m){
		int p,q,r;
		cin>>p;
		if(p==1){
			cin>>q;
			stages[i]=mp(p,q);
		}
		else{
			cin>>q>>r;
			stages[i]=mp(p,10001*q+r);
		}
	}
	cin>>n;
	long long a[n];
	REP(i,n){
		cin>>a[i];
	}
	int nums[100000];
	int i=0,j=0;
	long long len=0,t;
	while(len<100000){
		if(stages[i].first==1){
			nums[len]=stages[i].second;
			len++;
		}
		else{
			int l=stages[i].second/10001,c=stages[i].second%10001;
			bool fl=false;
			if(len+l*c >= 100000){
				t=len;
			}
			if(len+l*c>100000)fl=true;
			// else{
			REP(q,c){
				REP(p,l){
					nums[len]=nums[p];
					len++;
					if(len==100000)break;
				}
				if(len==100000)break;
			}
			if(len==100000){
				len=t;
				break;
			}
			// }
		}
		i++;
		if(i==m)break;
	}
	while(j!=n){
		if(a[j]<=100000){
			cout<<nums[a[j]-1]<<" ";
			j++;
		}
		else if(a[j]<=len){
			int l=stages[i].second/10001,c=stages[i].second%10001;
			long long t=len-l*c;
			long long index=(a[j]-t-1)%l;
			cout<<nums[index]<<" ";
			j++;
		}
		else{
			if(stages[i].first==1 && a[j]==len+1){
				cout<<stages[i].second<<" ";
				len++;
				j++;
				i++;
			}
			else if(stages[i].first==1){
				len++;
				i++;
			}
			else{
				int l=stages[i].second/10001,c=stages[i].second%10001;
				long long z=len+l*c;
				if(z < a[j]){
					len+=l*c;
					i++;
				}
				else{
					long long index=(a[j]-len-1)%l;
					cout<<nums[index]<<" ";
					j++;
					//i++;
				}
			}
		}
	}
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
#define maxn 100005
typedef __int64 LL;
int ki[maxn];
int l[maxn],c[maxn];
vector<int>index;
vector<int>ans;
map<LL,int>mp;
int last;
int solve(int now){
    if(mp[now]) return mp[now];
    int tmp=lower_bound(index.begin(),index.end(),now)-index.begin();
    if(ki[tmp]==1) return mp[now]=l[tmp];
    else{
        int pt=now-index[tmp-1];
        pt%=l[tmp];
        if(pt==0) pt+=l[tmp];
        return solve(pt);
    }
}
int main(){
    int m,n,kind;
    while(scanf("%d",&m)!=EOF){
        index.clear();
        ans.clear();
        mp.clear();
        LL now=0;
        for(int i=0;i<m;i++){
            scanf("%d",&ki[i]);
            if(ki[i]==1){
                scanf("%d",&l[i]);
                now++;
                index.push_back(now);
            }
            else{
                scanf("%d%d",&l[i],&c[i]);
                now+=l[i]*c[i];
                index.push_back(now);
            }
        }
        /*for(int i=0;i<index.size();i++)
            printf("%d ",index[i]);
        printf("\n");*/
        scanf("%d",&n);
        int pnow;
        for(int i=0;i<n;i++){
            scanf("%d",&pnow);
            ans.push_back(solve(pnow));
        }
        for(int i=0;i<ans.size();i++){
            if(i==0) printf("%d",ans[i]);
            else printf(" %d",ans[i]);
        }
        printf("\n");
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
#define maxn 100005
typedef __int64 LL;
int ki[maxn];
int l[maxn],c[maxn];
vector<long long>index;
//map<LL,int>mp;
int solve(long long now){
    //if(mp[now]) return mp[now];
    int tmp=lower_bound(index.begin(),index.end(),now)-index.begin();
    if(ki[tmp]==1) return l[tmp];//mp[now]=l[tmp];
    else{
        long long pt=now-index[tmp-1];
        pt%=l[tmp];
        if(pt==0) pt+=l[tmp];
        return solve(pt);
    }
}
int main(){
    int m,n,kind;
    while(scanf("%d",&m)!=EOF){
        index.clear();
        //mp.clear();
        LL now=0;
        for(int i=0;i<m;i++){
            scanf("%d",&ki[i]);
            if(ki[i]==1){
                scanf("%d",&l[i]);
                now++;
                index.push_back(now);
            }
            else{
                scanf("%d%d",&l[i],&c[i]);
                now+=l[i]*c[i];
                index.push_back(now);
            }
        }
        scanf("%d",&n);
        long long pnow;
        for(int i=0;i<n;i++){
            scanf("%I64d",&pnow);
            if(i+1==n) printf("%d\n",solve(pnow));
            else printf("%d ",solve(pnow));
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
#define maxn 100005
typedef __int64 LL;
int ki[maxn];
int l[maxn],c[maxn];
vector<LL>index;
map<LL,int>mp;
int solve(LL now){
    if(mp[now]) return mp[now];
    int tmp=lower_bound(index.begin(),index.end(),now)-index.begin();
    if(ki[tmp]==1) return mp[now]=l[tmp];
    else{
        LL pt=now-index[tmp-1];
        pt%=l[tmp];
        if(pt==0) pt+=l[tmp];
        return solve(pt);
    }
}
int main(){
    int m,n,kind;
    while(scanf("%d",&m)!=EOF){
        index.clear();
        mp.clear();
        LL now=0;
        for(int i=0;i<m;i++){
            scanf("%d",&ki[i]);
            if(ki[i]==1){
                scanf("%d",&l[i]);
                now++;
                index.push_back(now);
            }
            else{
                scanf("%d%d",&l[i],&c[i]);
                now+=l[i]*c[i];
                index.push_back(now);
            }
        }
        scanf("%d",&n);
        LL pnow;
        for(int i=0;i<n;i++){
            scanf("%I64d",&pnow);
            if(i+1==n) printf("%d\n",solve(pnow));
            else printf("%d ",solve(pnow));
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
#define maxn 100005
int ki[maxn];
int l[maxn],c[maxn];
vector<int>index;
//map<LL,int>mp;
int solve(int now){
    //if(mp[now]) return mp[now];
    int tmp=lower_bound(index.begin(),index.end(),now)-index.begin();
    if(ki[tmp]==1) return l[tmp];//mp[now]=l[tmp];
    else{
        int pt=now-index[tmp-1];
        pt%=l[tmp];
        if(pt==0) pt+=l[tmp];
        return solve(pt);
    }
}
int main(){
    int m,n,kind;
    //while(scanf("%d",&m)!=EOF){
    scanf("%d",&m);
    index.resize(m);
        //index.clear();
        //mp.clear();
        int now=0;
        for(int i=0;i<m;i++){
            scanf("%d",&ki[i]);
            if(ki[i]==1){
                scanf("%d",&l[i]);
                now++;
                index[i]=now;
            }
            else{
                scanf("%d%d",&l[i],&c[i]);
                now+=l[i]*c[i];
                index[i]=now;
            }
        }
        /*for(int i=0;i<index.size();i++)
            printf("%d ",index[i]);
        printf("\n");*/
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&now);
            if(i+1==n) printf("%d\n",solve(now));
            else printf("%d ",solve(now));
        }
    //}
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
#define maxn 100005
int ki[maxn];
int l[maxn],c[maxn];
vector<int>index;
//map<LL,int>mp;
int solve(int now){
    //if(mp[now]) return mp[now];
    int tmp=lower_bound(index.begin(),index.end(),now)-index.begin();
    if(ki[tmp]==1) return l[tmp];//mp[now]=l[tmp];
    else{
        int pt=now-index[tmp-1];
        pt%=l[tmp];
        if(pt==0) pt+=l[tmp];
        return solve(pt);
    }
}
int main(){
    int m,n,kind;
    while(scanf("%d",&m)!=EOF){
        index.clear();
        //mp.clear();
        int now=0;
        for(int i=0;i<m;i++){
            scanf("%d",&ki[i]);
            if(ki[i]==1){
                scanf("%d",&l[i]);
                now++;
                index.push_back(now);
            }
            else{
                scanf("%d%d",&l[i],&c[i]);
                now+=l[i]*c[i];
                index.push_back(now);
            }
        }
        /*for(int i=0;i<index.size();i++)
            printf("%d ",index[i]);
        printf("\n");*/
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&now);
            if(i+1==n) printf("%d\n",solve(now));
            else printf("%d ",solve(now));
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
#define maxn 100005
typedef __int64 LL;
int ki[maxn];
int l[maxn],c[maxn];
vector<int>index;
map<LL,int>mp;
int last;
int solve(int now){
    if(mp[now]) return mp[now];
    int tmp=lower_bound(index.begin(),index.end(),now)-index.begin();
    if(ki[tmp]==1) return mp[now]=l[tmp];
    else{
        int pt=now-index[tmp-1];
        pt%=l[tmp];
        if(pt==0) pt+=l[tmp];
        return solve(pt);
    }
}
int main(){
    int m,n,kind;
    while(scanf("%d",&m)!=EOF){
        index.clear();
        mp.clear();
        LL now=0;
        for(int i=0;i<m;i++){
            scanf("%d",&ki[i]);
            if(ki[i]==1){
                scanf("%d",&l[i]);
                now++;
                index.push_back(now);
            }
            else{
                scanf("%d%d",&l[i],&c[i]);
                now+=l[i]*c[i];
                index.push_back(now);
            }
        }
        /*for(int i=0;i<index.size();i++)
            printf("%d ",index[i]);
        printf("\n");*/
        scanf("%d",&n);
        int pnow;
        for(int i=0;i<n;i++){
            scanf("%d",&pnow);
            if(i+1==n) printf("%d\n",solve(pnow));
            else printf("%d ",solve(pnow));
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
#define maxn 100005
int ki[maxn];
int l[maxn],c[maxn];
vector<int>index;
//map<LL,int>mp;
int solve(int now){
    //if(mp[now]) return mp[now];
    int tmp=lower_bound(index.begin(),index.end(),now)-index.begin();
    if(ki[tmp]==1) return l[tmp];//mp[now]=l[tmp];
    else{
        int pt=now-index[tmp-1];
        pt%=l[tmp];
        if(pt==0) pt+=l[tmp];
        return solve(pt);
    }
}
int main(){
    int m,n,kind;
    //while(scanf("%d",&m)!=EOF){
    scanf("%d",&m);
        index.clear();
        //mp.clear();
        int now=0;
        for(int i=0;i<m;i++){
            scanf("%d",&ki[i]);
            if(ki[i]==1){
                scanf("%d",&l[i]);
                now++;
                index.push_back(now);
            }
            else{
                scanf("%d%d",&l[i],&c[i]);
                now+=l[i]*c[i];
                index.push_back(now);
            }
        }
        /*for(int i=0;i<index.size();i++)
            printf("%d ",index[i]);
        printf("\n");*/
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&now);
            if(i+1==n) printf("%d\n",solve(now));
            else printf("%d ",solve(now));
        }
    //}
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
#define maxn 100005
typedef __int64 LL;
int ki[maxn];
int l[maxn],c[maxn];
vector<int>index;
vector<int>ans;
map<LL,int>mp;
int last;
int solve(int now){
    if(mp[now]) return mp[now];
    int tmp=lower_bound(index.begin(),index.end(),now)-index.begin();
    if(ki[tmp]==1) return mp[now]=l[tmp];
    else{
        int pt=now-index[tmp-1];
        pt%=l[tmp];
        if(pt==0) pt+=l[tmp];
        return solve(pt);
    }
}
int main(){
    int m,n,kind;
    while(scanf("%d",&m)!=EOF){
        index.clear();
        ans.clear();
        mp.clear();
        LL now=0;
        for(int i=0;i<m;i++){
            scanf("%d",&ki[i]);
            if(ki[i]==1){
                scanf("%d",&l[i]);
                now++;
                index.push_back(now);
            }
            else{
                scanf("%d%d",&l[i],&c[i]);
                now+=l[i]*c[i];
                index.push_back(now);
            }
        }
        for(int i=0;i<index.size();i++)
            printf("%d ",index[i]);
        printf("\n");
        scanf("%d",&n);
        int pnow;
        for(int i=0;i<n;i++){
            scanf("%d",&pnow);
            ans.push_back(solve(pnow));
        }
        for(int i=0;i<ans.size();i++){
            if(i==0) printf("%d",ans[i]);
            else printf(" %d",ans[i]);
        }
        printf("\n");
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
#define maxn 100005
typedef __int64 LL;
int ki[maxn];
int l[maxn],c[maxn];
vector<int>index;
//map<LL,int>mp;
int solve(int now){
    //if(mp[now]) return mp[now];
    int tmp=lower_bound(index.begin(),index.end(),now)-index.begin();
    if(ki[tmp]==1) return l[tmp];//mp[now]=l[tmp];
    else{
        int pt=now-index[tmp-1];
        pt%=l[tmp];
        if(pt==0) pt+=l[tmp];
        return solve(pt);
    }
}
int main(){
    int m,n,kind;
    while(scanf("%d",&m)!=EOF){
        index.clear();
        //mp.clear();
        LL now=0;
        for(int i=0;i<m;i++){
            scanf("%d",&ki[i]);
            if(ki[i]==1){
                scanf("%d",&l[i]);
                now++;
                index.push_back(now);
            }
            else{
                scanf("%d%d",&l[i],&c[i]);
                now+=l[i]*c[i];
                index.push_back(now);
            }
        }
        /*for(int i=0;i<index.size();i++)
            printf("%d ",index[i]);
        printf("\n");*/
        scanf("%d",&n);
        int pnow;
        for(int i=0;i<n;i++){
            scanf("%d",&pnow);
            if(i+1==n) printf("%d\n",solve(pnow));
            else printf("%d ",solve(pnow));
        }
    }
    return 0;
}


#include <bits/stdc++.h>

#define o ((b+e)/2)

using namespace std;

typedef long long int Lint;
int N,K;
int as[110000],dn[110000],res[110000];
Lint ar[110000];

int bul( Lint x ){

	int b=1,e=N,q;

	while( b <= e ){

		if( ar[o] == x || ar[o-1]< x && ar[o]> x )	break;
		if( ar[o] > x ){ e=o-1; }
		else{ b=o+1;}

	}

	if( ar[o] == x && as[o] == 1 ) return res[o];
	else{
		b=o;
		x-=ar[b-1];
		//~ cout << x << endl;
		//~ cout << b << endl;
		x%=(Lint)res[b];
		if( !x ) x=res[b];
		return dn[x];
	}

}

int main(){

	cin >> N;

	int k,a,b,w=1;
	for( int i=1 ; i<=N ; i++ ){
		scanf(" %d",&k);
		as[i]=k;
		if( k == 1 ){
			scanf(" %d",&a);
			res[i]=a;
			ar[i]=ar[i-1]+1;
			if( w <= 100000 ) dn[w++]=a;
		}
		else{
			scanf(" %d %d",&a,&b);
			res[i]=a;
			ar[i]=ar[i-1]+(a*b);
			for( int j=1 ; j<=b && w <= 100000 ; j++ )
				for( int k=1 ; k<=a && w <= 100000; k++ , w++ ) dn[w]=dn[k];
		}
	}

	cin >> K;

	Lint z;
	for( int i=1 ; i<=K ; i++ ){

		scanf(" %I64d",&z);
		printf("%d ",bul( z ));

	}

	return 0;

}


#include <bits/stdc++.h>

#define o ((b+e)/2)

using namespace std;

typedef long long int Lint;
int N,K;
int as[110000],dn[110000],res[110000];
Lint ar[110000];

int bul( Lint x ){

	int b=1,e=N;

	while( b <= e ){

		if( ar[o] == x ) break;
		if( ar[o] > x ) e=o-1;
		else b=o+1;

	}

	if( ar[o] == x && as[o] == 1 ) return res[o];
	else{
		if( ar[o] == x )	b=o;
		x%=(Lint)res[b];
		if( !x ) x=(Lint)res[b];
		return bul( x );
	}

}

int main(){

	cin >> N;

	int k,a,b;
	for( int i=1 ; i<=N ; i++ ){
		scanf(" %d",&k);
		as[i]=k;
		if( k == 1 ){
			scanf(" %d",&a);
			res[i]=a;
			ar[i]=ar[i-1]+1;
		}
		else{
			scanf(" %d %d",&a,&b);
			res[i]=a;
			ar[i]=ar[i-1]+(a*b);
		}
	}

	cin >> K;

	Lint z;
	for( int i=1 ; i<=K ; i++ ){

		scanf(" %I64d",&z);
		printf("%d ",bul( z ));

	}

	return 0;

}


#include <bits/stdc++.h>

#define o ((b+e)/2)

using namespace std;

typedef long long int Lint;
int N,K;
int as[110000],dn[110000],res[110000];
Lint ar[110000];

int bul( Lint x ){

	int b=1,e=N,q;

	while( b <= e ){

		if( ar[o] == x ){ q=o; break;}
		if( ar[o] > x ){ e=o-1; q=e; }
		else{ b=o+1; q=b;}

	}

	if( ar[o] == x && as[o] == 1 ) return res[o];
	else{
		b=q;
		x%=(Lint)res[b];
		if( !x ) x=(Lint)res[b];
		return bul( x );
	}

}

int main(){

	cin >> N;

	int k,a,b;
	for( int i=1 ; i<=N ; i++ ){
		scanf(" %d",&k);
		as[i]=k;
		if( k == 1 ){
			scanf(" %d",&a);
			res[i]=a;
			ar[i]=ar[i-1]+1;
		}
		else{
			scanf(" %d %d",&a,&b);
			res[i]=a;
			ar[i]=ar[i-1]+(a*b);
		}
	}

	cin >> K;

	Lint z;
	for( int i=1 ; i<=K ; i++ ){

		scanf(" %I64d",&z);
		printf("%d ",bul( z ));

	}

	return 0;

}


#include <bits/stdc++.h>

#define o ((b+e)/2)

using namespace std;

typedef long long int Lint;
int N,K;
int as[110000],dn[110000],res[110000];
Lint ar[110000];

int bul( Lint x ){

	while( 1905 ){

		int b=1,e=N,q;

		while( b <= e ){

			if( ar[o] == x ){ q=o; break;}
			if( ar[o] > x ){ e=o-1; q=e; }
			else{ b=o+1; q=b;}

		}

		if( ar[o] == x && as[o] == 1 ) return res[o];
		else{
			b=q;
			x%=(Lint)res[b];
			if( !x ) x=(Lint)res[b];
		}

	}
}

int main(){

	cin >> N;

	int k,a,b;
	for( int i=1 ; i<=N ; i++ ){
		scanf(" %d",&k);
		as[i]=k;
		if( k == 1 ){
			scanf(" %d",&a);
			res[i]=a;
			ar[i]=ar[i-1]+1;
		}
		else{
			scanf(" %d %d",&a,&b);
			res[i]=a;
			ar[i]=ar[i-1]+(a*b);
		}
	}

	cin >> K;

	Lint z;
	for( int i=1 ; i<=K ; i++ ){

		scanf(" %I64d",&z);
		printf("%d ",bul( z ));

	}

	return 0;

}


#include <bits/stdc++.h>

#define o ((b+e)/2)

using namespace std;

typedef long long int Lint;
int N,K;
int as[110000],dn[110000],res[110000];
Lint ar[110000];

int bul( Lint x ){

	int b=1,e=N;

	while( b <= e ){

		if( ar[o] == x ) break;
		if( ar[o] > x ) e=o-1;
		else b=o+1;

	}

	if( ar[o] == x && as[o] == 1 ) return res[o];
	else{
		if( ar[o] == x )	b=o;
		x%=res[b];
		if( !x ) x=res[b];
		return bul( x );
	}

}

int main(){

	cin >> N;

	int k,a,b;
	for( int i=1 ; i<=N ; i++ ){
		scanf(" %d",&k);
		as[i]=k;
		if( k == 1 ){
			scanf(" %d",&a);
			res[i]=a;
			ar[i]=ar[i-1]+1;
		}
		else{
			scanf(" %d %d",&a,&b);
			res[i]=a;
			ar[i]=ar[i-1]+(a*b);
		}
	}

	cin >> K;

	Lint z;
	for( int i=1 ; i<=K ; i++ ){

		scanf(" %I64d",&z);
		printf("%d ",bul( z ));

	}

	return 0;

}


#include <bits/stdc++.h>

#define o ((b+e)/2)

using namespace std;

typedef long long int Lint;
int N,K;
int as[110000],dn[110000],res[110000];
Lint ar[110000];

int bul( Lint x ){

	while( 1905 ){

		int b=1,e=N,q;

		while( b <= e ){

			if( ar[o] == x ){ q=o; break;}
			if( ar[o] > x ){ e=o-1; q=e; }
			else{ b=o+1; q=b;}

		}

		if( ar[o] == x && as[o] == 1 ) return res[o];
		else{
			b=q;
			x%=(Lint)res[b];
			if( !x ) x=(Lint)res[b];
			return dn[x];
		}

	}
}

int main(){

	cin >> N;

	int k,a,b,w=1;
	for( int i=1 ; i<=N ; i++ ){
		scanf(" %d",&k);
		as[i]=k;
		if( k == 1 ){
			scanf(" %d",&a);
			res[i]=a;
			ar[i]=ar[i-1]+1;
			if( w <= 100000 ) dn[w++]=a;
		}
		else{
			scanf(" %d %d",&a,&b);
			res[i]=a;
			ar[i]=ar[i-1]+(a*b);
			for( int j=1 ; j<=b && w <= 100000 ; j++ )
				for( int k=1 ; k<=a && w <= 100000; k++ , w++ ) dn[w]=dn[k];
		}
	}

	cin >> K;

	Lint z;
	for( int i=1 ; i<=K ; i++ ){

		scanf(" %I64d",&z);
		printf("%d ",bul( z ));

	}

	return 0;

}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <map>
using namespace std;
#define MAXN 100005
map < int,vector<int> > col;
int type[MAXN],x[MAXN],l[MAXN],c[MAXN],n,snum[MAXN],getnum[MAXN];
void init(){
    col.clear();
}
void input(int m,int &n){
      for(int i=0;i<m;++i){
          scanf("%d",&type[i]);
          if(type[i]==1){

              scanf("%d",&x[i]);
          }
          else{
              scanf("%d%d",&l[i],&c[i]);
          }
      }
      scanf("%d",&n);
      for(int i=0;i<n;++i){
          scanf("%d",&snum[i]);
      }
}
int binsea(int upind,int goal){
     int Left,Right,sz,Mid,tmpnum,tmpnum2;
     for(int i=1;i<upind;++i){
         tmpnum = getnum[i];
         sz = col[tmpnum].size();
         Left = 0;Right = sz;
         while(Left<Right){
             Mid = (Left+Right)>>1;
             tmpnum2 = col[tmpnum][Mid];
             if(tmpnum2<goal) Left = Mid+1;
             else if(tmpnum2==goal) return tmpnum;
             else Right = Mid;

         }
         if(Left<sz){
         tmpnum2 = col[tmpnum][Left];
         if(tmpnum2==goal) return tmpnum;
         }
     }
}
void printans(int cnt){
    for(int i=1;i<cnt;++i ){
        printf("%d:",getnum[i]);
        for(int j=0;j<col[getnum[i]].size();++j){
            printf(" %d",col[getnum[i]][j]);
        }
        printf("\n");
    }
}
void solve(int m,int n){
    int ind,cnt;
    cnt = ind = 1;
    //init();
    for(int i=0;i<m;++i){
        if(type[i]==1){

        //  col[x[i]].push_back(ind);
          if(col.find(x[i])==col.end()){
              getnum[cnt] = x[i];
              cnt++;
          }
          col[x[i]].push_back(ind);
          //cnt++;
          ind++;
        }
        else {
            int sz,tmpind,tmpnum,tmpnum1;
             for(int h=0;h<c[i];++h) {
              for(int j=1;j<=l[i];++j){
                  //col[getnum[j]].push_back(ind+j-1+h*l[i]);
                  tmpnum = getnum[j];
                  sz = col[tmpnum].size();
                  for(int k=0;k<sz;++k){
                      tmpind = col[tmpnum][k];
                      if(tmpind>l[i])break;
                      //tmpnum1 =
                     // if()
                      col[tmpnum].push_back(tmpind+ind-1);
                  }

              }
               ind+=l[i];
            }
            //ind = ind +c[i]*l[i]; //ind + l[i]-1+(c[i]-1)*l[i];
        }
        //printans(cnt);
    }
    //init(cnt);
    //printans(cnt);
    int ans;
    for(int i=0;i<n;++i){
        ans = binsea(cnt,snum[i]);
        if(!i)printf("%d",ans);
        else printf(" %d",ans);
    }
    printf("\n");
}
int main(){
    int m,n;
    while(scanf("%d",&m)!=EOF){
        input(m,n);
        init();
        solve(m,n);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
const int num = 100005;
int ans[num],x[num],l[num],c[num],type[num],tmpb[num];
bool vis[num];

void solve(int m){
    //sort(b,b+m,cmp);
    //int type;
    int n;
    for(int i=0;i<m;++i){
        scanf("%d",&type[i]);
        if(type[i]==1){
            scanf("%d",&x[i]);
        }
        else{
            scanf("%d%d",&l[i],&c[i]);
        }
    }
    scanf("%d",&n);
    for(int i=0;i<n;++i)
        scanf("%d",&tmpb[i]);
    int cnt=0;
    for(int i=0;i<m;++i){
         if(cnt>=n)break;
         if(type[i]==1){
             if(cnt+1>n)break;
             ans[cnt++]=x[i];
         }
         else{
             if(cnt+l[i]*c[i]>n){
                 int t = (n-cnt)/l[i];
                 int tmpc = cnt;
                 int tmpt = t;
                 while(t>0){
                  for(int h=0;h<l[i];++h){
                      ans[cnt++] = ans[h];
                  }
                  t--;
                 }
                 t = tmpt;
                 int s=0;
                 for(int h=n-tmpc-t*c[i];h>0;h--){
                     ans[cnt++]=ans[s++];
                 }
                 }
             else{
                 for(int h=0;h<c[i];++h){
                     for(int s=0;s<l[i];++s)
                     ans[cnt++] = ans[s];
                 }
             }
             }
         }
    if(cnt>n)cnt=n;
   printf("%d",ans[tmpb[0]-1]);
   for(int i=1;i<cnt;++i)
       printf(" %d",ans[tmpb[i]-1]);
   printf("\n");

}
int main(){
    int m;
    while(scanf("%d",&m)!=EOF){

        solve(m);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 100005
typedef __int64 LL;
LL xvalue[MAXN],xind[MAXN],xlen[MAXN];
LL binarysearch(int m,LL g){
    int L = 1,R = m,Mid,tmpind;
    while(L<R){
        Mid = (R+L)>>1;
        if(xind[Mid]>=g)R = Mid;
        else L = Mid+1;
    }

    if(xvalue[L]==-1){
        tmpind = (g - xind[L])%xlen[L]+1;
        return binarysearch(m,tmpind);
    }
    if(xind[L]>g&&L>1){
        tmpind = (g - xind[L-1])%xlen[L-1]+1;
        return binarysearch(m,tmpind);
    }
    return xvalue[L];
}
void solve(int m){
    int n;
        LL gind;
    scanf("%d",&n);

    for(int i=0;i<n;++i){
        scanf("%I64d",&gind);
        LL ans = binarysearch(m,gind);
        if(i)printf(" ");
        printf("%I64d",ans);
    }
    printf("\n");
}
int main(){
        int m, type;
        LL ind,x,l,c;
        while(scanf("%d",&m)!=EOF){
            ind = 1;
            for(int i=1;i<=m;++i){
                scanf("%d",&type);
                if(type==1){
                    scanf("%I64d",&x);
                    xvalue[i] = x;
                    xind[i] = ind++;
                }
                else {
                    scanf("%I64d%I64d",&l,&c);

                    xvalue[i]=-1;
                    xind[i]=ind;
                    xlen[i] = l;
                    ind=ind+l*c;
                }
            }
            solve(m);
        }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
#define MAXN 100005
map < int,vector<int> > col;
int type[MAXN],x[MAXN],l[MAXN],c[MAXN],n,snum[MAXN],getnum[MAXN],maxind;
void init(){
    col.clear();
}
void input(int m,int &n){
      for(int i=0;i<m;++i){
          scanf("%d",&type[i]);
          if(type[i]==1){

              scanf("%d",&x[i]);
          }
          else{
              scanf("%d%d",&l[i],&c[i]);
          }
      }
      scanf("%d",&n);
      for(int i=0;i<n;++i){
          scanf("%d",&snum[i]);
          if(!i)maxind  =  snum[i];
          else if(maxind < snum[i]){
               maxind =  snum[i];
          }
      }
}
int binsea(int upind,int goal){
     int Left,Right,sz,Mid,tmpnum,tmpnum2;
     for(int i=1;i<upind;++i){
         tmpnum = getnum[i];
         sz = col[tmpnum].size();
         Left = 0;Right = sz;
         while(Left<Right){
             Mid = (Left+Right)>>1;
             tmpnum2 = col[tmpnum][Mid];
             if(tmpnum2<goal) Left = Mid+1;
             else if(tmpnum2==goal) return tmpnum;
             else Right = Mid;

         }
         if(Left<sz){
         tmpnum2 = col[tmpnum][Left];
         if(tmpnum2==goal) return tmpnum;
         }
     }
}
void printans(int cnt){
    for(int i=1;i<cnt;++i ){
        printf("%d:",getnum[i]);
        for(int j=0;j<col[getnum[i]].size();++j){
            printf(" %d",col[getnum[i]][j]);
        }
        printf("\n");
    }
}
void solve(int m,int n){
    int ind,cnt;
    cnt = ind = 1;
    //init();
    for(int i=0;i<m;++i){
        if(type[i]==1){

        //  col[x[i]].push_back(ind);
         // lower_bound(snum,snum+n,x[]  bb)
            if(ind>maxind) break;
          if(col.find(x[i])==col.end()){
              getnum[cnt] = x[i];
              cnt++;
          }
          col[x[i]].push_back(ind);
          //cnt++;
          ind++;
        }
        else {
            int sz,tmpind,tmpnum,tmpnum1;
             for(int h=0;h<c[i];++h) {
              for(int j=1;j<=l[i];++j){
                  //col[getnum[j]].push_back(ind+j-1+h*l[i]);
                  tmpnum = getnum[j];
                  sz = col[tmpnum].size();
                  for(int k=0;k<sz;++k){
                      tmpind = col[tmpnum][k];
                      if(tmpind>l[i])break;
                      //tmpnum1 =
                     // if()
                      col[tmpnum].push_back(tmpind+ind-1);
                  }

              }
               ind+=l[i];
               if(ind>maxind)break;
            }
            //ind = ind +c[i]*l[i]; //ind + l[i]-1+(c[i]-1)*l[i];
        }
        //printans(cnt);
    }
    //init(cnt);
    //printans(cnt);
    int ans;
    for(int i=0;i<n;++i){
        ans = binsea(cnt,snum[i]);
        if(!i)printf("%d",ans);
        else printf(" %d",ans);
    }
    printf("\n");
}
int main(){
    int m,n;
    while(scanf("%d",&m)!=EOF){
        input(m,n);
        init();
        solve(m,n);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
const int num = 100005;
int ans[num],x[num],l[num],c[num],type[num],tmpb[num];
bool vis[num];

void solve(int m){
	//sort(b,b+m,cmp);
	//int type;
	int n;
	for(int i=0;i<m;++i){
		scanf("%d",&type[i]);
		if(type[i]==1){
			scanf("%d",&x[i]);
		}
		else{
			scanf("%d%d",&l[i],&c[i]);
		}
	}
	scanf("%d",&n);
	for(int i=0;i<n;++i)
		scanf("%d",&tmpb[i]);
	int cnt=0;
	for(int i=0;i<m;++i){
		 if(cnt>=n)break;
		 if(type[i]==1){
			 if(cnt+1>n)break;
			 ans[cnt++]=x[i];
		 }
		 else{
			 if(cnt+l[i]*c[i]>n){
				 int t = (n-cnt)/l[i];
				 int tmpc = cnt;
				 int tmpt = t;
				 while(t>0){
				  for(int h=0;h<l[i];++h){
					  ans[cnt++] = ans[h];
				  }
				  t--;
				 }
				 t = tmpt;
				 int s=0;
				 for(int h=n-tmpc-t*c[i];h>0;h--){
					 ans[cnt++]=ans[s++];
				 }
				 }
			 else{
				 for(int h=0;h<c[i];++h){
					 for(int s=0;s<l[i];++s)
					 ans[cnt++] = ans[s];
				 }
			 }
			 }
		 }
	if(cnt>n)cnt=n;
   printf("%d",ans[0]);
   for(int i=1;i<cnt;++i)
	   printf(" %d",ans[i]);
   printf("\n");

}
int main(){
	int m;
	while(scanf("%d",&m)!=EOF){

		solve(m);
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
const int num = 100005;
int ans[num],x[num],l[num],c[num],type[num],tmpb[num];
bool vis[num];

void solve(int m){
    //sort(b,b+m,cmp);
    //int type;
    int n;
    for(int i=0;i<m;++i){
        scanf("%d",&type[i]);
        if(type[i]==1){
            scanf("%d",&x[i]);
        }
        else{
            scanf("%d%d",&l[i],&c[i]);
        }
    }
    scanf("%d",&n);
    for(int i=0;i<n;++i)
        scanf("%d",&tmpb[i]);
    int cnt=0;
    for(int i=0;i<m;++i){
         if(cnt>=n)break;
         if(type[i]==1){
             if(cnt+1>n)break;
             ans[cnt++]=x[i];
         }
         else{
             if(cnt+l[i]*c[i]>n){
                 int t = (n-cnt)/c[i];
                 int tmpc = cnt;
                 int tmpt = t;
                 while(t>0){
                  for(int h=0;h<l[i];++h){
                      ans[cnt++] = ans[h];
                  }
                  t--;
                 }
                 t = tmpt;
                 int s=0;
                 for(int h=n-tmpc-t*c[i];h>0;h--){
                     ans[cnt++]=ans[s++];
                 }
                 }
             else{
                 for(int h=0;h<c[i];++h){
                     for(int s=0;s<l[i];++s)
                     ans[cnt++] = ans[s];
                 }
             }
             }
         }

   printf("%d",ans[0]);
   for(int i=1;i<cnt;++i)
       printf(" %d",ans[i]);
   printf("\n");

}
int main(){
    int m;
    while(scanf("%d",&m)!=EOF){

        solve(m);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 100005
int xvalue[MAXN],xind[MAXN],xlen[MAXN];
int binarysearch(int m,int g){
    int L = 1,R = m,Mid,tmpind;
    while(L<R){
        Mid = (R+L)>>1;
        if(xind[Mid]>=g)R = Mid;
        else L = Mid+1;
    }

    if(xvalue[L]==-1){
        tmpind = (g - xind[L])%xlen[L]+1;
        return binarysearch(m,tmpind);
    }
    if(xind[L]>g&&L>1){
        tmpind = (g - xind[L-1])%xlen[L-1]+1;
        return binarysearch(m,tmpind);
    }
    return xvalue[L];
}
void solve(int m){
    int n,gind;
    scanf("%d",&n);

    for(int i=0;i<n;++i){
        scanf("%d",&gind);
        int ans = binarysearch(m,gind);
        if(i)printf(" ");
        printf("%d",ans);
    }
    printf("\n");
}
int main(){
        int m,x, type,l,c,ind;
        while(scanf("%d",&m)!=EOF){
            ind = 1;
            for(int i=1;i<=m;++i){
                scanf("%d",&type);
                if(type==1){
                    scanf("%d",&x);
                    xvalue[i] = x;
                    xind[i] = ind++;
                }
                else {
                    scanf("%d%d",&l,&c);

                    xvalue[i]=-1;
                    xind[i]=ind;
                    xlen[i] = l;
                    ind=ind+l*c;
                }
            }
            solve(m);
        }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <map>
using namespace std;
#define MAXN 100005
map < int,vector<int> > col;
//int type[MAXN],x[MAXN],l[MAXN],c[MAXN],n,snum[MAXN],getnum[MAXN];
int getnum[MAXN];
void clear(int cnt){
      for(int i=1;i<cnt;++i){
          if(col[getnum[i]].size()>0)
              col[getnum[i]].clear();
      }
      col.clear();

}
/*void input(int m,int &n){
      for(int i=0;i<m;++i){
          scanf("%d",&type[i]);
          if(type[i]==1){

              scanf("%d",&x[i]);
          }
          else{
              scanf("%d%d",&l[i],&c[i]);
          }
      }
      scanf("%d",&n);
      for(int i=0;i<n;++i){
          scanf("%d",&snum[i]);
      }
}*/
int binsea(int upind,int goal){
     int Left,Right,sz,Mid,tmpnum,tmpnum2;
     for(int i=1;i<upind;++i){
         tmpnum = getnum[i];
         sz = col[tmpnum].size();
         Left = 0;Right = sz;
         while(Left<Right){
             Mid = (Left+Right)>>1;
             tmpnum2 = col[tmpnum][Mid];
             if(tmpnum2<goal) Left = Mid+1;
             else if(tmpnum2==goal) return tmpnum;
             else Right = Mid;

         }
         if(Left<sz){
         tmpnum2 = col[tmpnum][Left];
         if(tmpnum2==goal) return tmpnum;
         }
     }
}
void printans(int cnt){
    for(int i=1;i<cnt;++i ){
        printf("%d:",getnum[i]);
        for(int j=0;j<col[getnum[i]].size();++j){
            printf(" %d",col[getnum[i]][j]);
        }
        printf("\n");
    }
}
void solve(int m){
    int ind,type,cnt,l,c,x;
    cnt = ind = 1;
    //init()
    for(int i=0;i<m;++i){
        scanf("%d",&type);
        if(type==1){

        //  col[x[i]].push_back(ind);
            scanf("%d",&x);
            if(!col.count(x)){
              getnum[cnt] = x;
              cnt++;
          }
          col[x].push_back(ind);
          //cnt++;
          ind++;
        }
        else {
            scanf("%d%d",&l,&c);
            int sz,tmpind,tmpnum,tmpnum1;
             for(int h=0;h<c;++h) {
              for(int j=1;j<=l;++j){
                  //col[getnum[j]].push_back(ind+j-1+h*l[i]);
                  tmpnum = getnum[j];
                  sz = col[tmpnum].size();
                  for(int k=0;k<sz;++k){
                      tmpind = col[tmpnum][k];
                      if(tmpind>l)break;
                      //tmpnum1 =
                     // if()
                      col[tmpnum].push_back(tmpind+ind-1);
                  }

              }
               ind+=l;
            }
            //ind = ind +c[i]*l[i]; //ind + l[i]-1+(c[i]-1)*l[i];
        }
        //printans(cnt);
    }
    //init(cnt);
    //printans(cnt);
    int ans,snum;
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        scanf("%d",&snum);
        ans = binsea(cnt,snum);
        if(!i)printf("%d",ans);
        else printf(" %d",ans);
    }
    printf("\n");
    clear(cnt);
}
int main(){
    int m;
    while(scanf("%d",&m)!=EOF){
        //input(m,n);
        //init();
        solve(m);
        //clear();
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
#define ALL(x) (x.begin(), x.end())
#define mset(a,i) memset(a,i,sizeof(a))
#define rep(i,n) for(int i = 0;i < n;i ++)

const int N = 1e5 + 5;
int m[N], add[N];
vector<int> pos;
int main() {
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
    int n;
    scanf("%d", &n);
    int maxValue = 0;
    rep(i, n) {
        scanf("%d", m + i);
        m[i] ++;
        if (m[i] > maxValue) {
            maxValue = m[i];
            pos.push_back(i);
        }
    }
    int cur = m[pos.back()];
    for (int i = int(pos.size()) - 1, j; i > 0; i --) {
        for (j = pos[i]; j >= pos[i-1]; j --) {
            add[j] = max(cur, m[j]);
            cur = max(cur - 1, m[pos[i-1]]);
        }
    }
    add[pos.front()] = 1;
    for (int i = pos.back() + 1; i < n; i++) {
        add[i] = maxValue;
    }
    LL answer = 0;
    for (int i = 0; i < n; i ++) {
        //printf("%d: %d\n", i, add[i]);
        answer += add[i] - m[i];
    }
    printf("%lld\n", answer);
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN=110000;
int a[MAXN], b[MAXN];
int main()
{
//	freopen("C.in", "r", stdin);
//	freopen("C.out", "w", stdout);
	int n;
	scanf("%d", &n);
	for (int i=1; i<=n; i++) scanf("%d", &a[i]);
	for (int i=n, mx=-n-233; i>=1; i--)
	{
		mx=max(mx, a[i]-i);
		b[i]=mx+i;
	}
	long long ans=0;
	for (int i=1; i<=n; i++)
	{
		b[i]=max(b[i], b[i-1]);
		ans+=b[i]-a[i];
	}
	printf("%lld\n", ans);
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <map>
#include <string>
#include <cstdlib>
#include <bitset>
#include <set>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long LL;
typedef pair<int, int> P;
typedef unsigned int uint;
#define SIGN puts("SIGN");
#define F first
#define S second
#define pb push_back
#define rep(i, j, k) for (int i = int(j); i <= int(k); ++ i)
#define dwn(i, j, k) for (int i = int(j); i >= int(k); -- i)
#define lson rt << 1, l, m
#define rson rt << 1 | 1, m + 1, r
const int MOD = 1e9 + 7;
const int N = 1e5 + 7;
int n;
int a[N];
int pre[N];

int main() {
	scanf("%d", &n);
	rep(i, 1, n) scanf("%d", a + i);
	rep(i, 1, n) pre[i] = max(pre[i - 1], a[i] + 1);
	int p;
	rep(i, 1, n) if (pre[i] == pre[n]) {
		p = i;
		break;
	}
	int ans = 0;
	rep(i, p, n) ans += pre[n] - 1 - a[i];
	int len = pre[n];
	dwn(i, p - 1, 1) {
		if (len > pre[i]) len --;
		ans += len - 1 - a[i];
	}
	cout << ans << endl;
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <map>
#include <string>
#include <cstdlib>
#include <bitset>
#include <set>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long LL;
typedef pair<int, int> P;
typedef unsigned int uint;
#define SIGN puts("SIGN");
#define F first
#define S second
#define pb push_back
#define rep(i, j, k) for (int i = int(j); i <= int(k); ++ i)
#define dwn(i, j, k) for (int i = int(j); i >= int(k); -- i)
#define lson rt << 1, l, m
#define rson rt << 1 | 1, m + 1, r
const int MOD = 1e9 + 7;
const int N = 1e5 + 7;
int n;
int a[N];
int pre[N];

int main() {
	scanf("%d", &n);
	rep(i, 1, n) scanf("%d", a + i);
	rep(i, 1, n) pre[i] = max(pre[i - 1], a[i] + 1);
	int p;
	rep(i, 1, n) if (pre[i] == pre[n]) {
		p = i;
		break;
	}
	LL ans = 0;
	rep(i, p, n) ans += (LL)pre[n] - 1 - a[i];
	int len = pre[n];
	dwn(i, p - 1, 1) {
		if (len > pre[i]) len --;
		ans += (LL)len - 1 - a[i];
	}
	cout << ans << endl;
	return 0;
}


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

typedef long long ll;

#define MAXN 100000

int main(){
    int n;scanf("%d", &n);
    int maxi=-1;
    ll tot=0;
    for(int i=0;i<n;i++){
        int m;scanf("%d", &m);
        tot+=max(0, maxi-m);
        if(m-maxi>1)
            tot+=m-maxi-1;
        maxi=max(maxi, m);
    }
    printf("%I64d", tot);
}


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

typedef long long ll;

int main(){
    int n;scanf("%d", &n);
    ll maxi=0;
    ll tot=0;
    for(int i=0;i<n;i++){
        ll m;scanf("%I64d", &m);
        tot+=max(0ll, maxi-m);
        if(m-maxi>1)
            tot+=m-maxi-1;
        maxi=max(maxi, m);
    }
    printf("%I64d", tot);
}


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

typedef long long ll;

#define MAXN 100000

ll nm[MAXN];
ll mi[MAXN];

int main(){
    int n;scanf("%d", &n);
    ll maxi=0;
    for(int i=0;i<n;i++){
        ll m;scanf("%I64d", &m);
        mi[i]=m;
        nm[i]=max(0ll, maxi-m);
        maxi=max(maxi,m);
    }

    ll cur=0, tot=0;
    for(int i=n-1;i>=0;i--){
        cur=max(0ll, cur-1);
       tot+=max(nm[i], cur-mi[i]);
        cur=max(cur, mi[i]);
    }

    printf("%I64d", tot);
}


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

typedef long long ll;

int main(){
    int n;scanf("%d", &n);
    ll maxi=0, loc=-1;
    ll tot=0;
    for(int i=0;i<n;i++){
        ll m;scanf("%I64d", &m);
        tot+=max(0ll, maxi-m);
        if(loc!=-1)
        if(m-maxi>1)
            tot+=(m-maxi-1)*(i-loc);
        if(m>=maxi)
        maxi=m,loc=i;
    }
    printf("%I64d", tot);
}


#include<cstdio>
#define int long long
const int N=1e5+5;
int n,ans;
int a[N],b[N];
signed main()
{
	int last=0;
	register int i;
	scanf("%lld",&n);
	for(i=1;i<=n;i++)
		scanf("%lld",&a[i]);
	for(i=n;i>=1;i--)
	{
		last--;
		if(a[i]+1>last)
			last=a[i]+1;
		b[i]=last;
	}
	for(i=2;i<=n;i++)
		if(b[i-1]>b[i])
			b[i]=b[i-1];
	for(i=1;i<=n;i++)
		ans+=b[i]-a[i]-1;
	printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second
#define ll long long
#define MP make_pair
#define PB push_back
#define INF ((1<<30)-1)
#define MOD 1000000007
#define MAXN 200100
#define EPS 1e-9
#define INFLL 1e18
using namespace std;
FILE *in,*out;

int N,M,K;
int ar[100100];
int t[100100];

int read()
{
    int i,j,k;
    int x,y;
    cin >> N;
    for(i=0;i<N;i++)
    {
        cin >> ar[i];
    }
    for(i=1;i<N;i++)
    {
        t[i] = max(t[i-1],ar[i]+1);
    }
    ll ans = 0;
    for(i=N-1;i>0;i--)
    {
        ans += t[i] - ar[i] - 1;
        if(t[i-1] < t[i] - 1)
            t[i-1] = t[i] - 1;
    }
    cout << ans << "\n";
    return 0;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    // in = fopen("A.in","r");
    // out  = fopen("A.out","w");
    in = stdin;
    out = stdout;
    read();
    return 0;
}
//////////// !!!!!!!!!! Check
// Interactive problems => Deactiave read optimizer!

//////////// Sublime Snippets
// forx
// fory
// vectorx
// pairx
// sizex
// compile
// mod_operations
// graph_edges

// lca
// rmq
// fft
// bigint
// bigint_fft
// bigint_normal
// hash_gnu
// simple_segment
// tree_decomposition
// number_theory
/*
g++ A.cpp -o a -std=c++11;./a
g++ B.cpp -o a -std=c++11;./a
g++ C.cpp -o a -std=c++11;./a
g++ D.cpp -o a -std=c++11;./a
g++ E.cpp -o a -std=c++11;./a
g++ F.cpp -o a -std=c++11;./a
g++ G.cpp -o a -std=c++11;./a
*/


#include <bits/stdc++.h>
#define f first
#define s second
#define ll long long
#define MP make_pair
#define PB push_back
#define INF ((1<<30)-1)
#define MOD 1000000007
#define MAXN 200100
#define EPS 1e-9
#define INFLL 1e18
using namespace std;
FILE *in,*out;

int N,M,K;
int ar[100100];
int res[100100];

int read()
{
    int i,j,k;
    int x,y;
    cin >> N;
    for(i=0;i<N;i++)
    {
        cin >> ar[i];
    }
    int minx=1,maxx=1;
    for(i=1,j=1,k=0;i<N;i++)
    {
        if(ar[i]==0)
            continue;
        while(k<ar[i]-1 && j<N)
        {
            if(ar[j]==0)
            {
                res[j] = +1;
                k++;
            }
            j++;
        }
        if(k==ar[i]-1)
        {
            k++;
            res[i] = +1;
        }
    }
    ll ans = 0;
    for(i=0,k=0;i<N;i++)
    {
        k += res[i];
        // printf("%d ",k-ar[i]);
        // printf("%d ",res[i]);
        ans += k-ar[i];
    }
    // printf("\n");
    cout << ans << "\n";
    return 0;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    // in = fopen("A.in","r");
    // out  = fopen("A.out","w");
    in = stdin;
    out = stdout;
    read();
    return 0;
}
//////////// !!!!!!!!!! Check
// Interactive problems => Deactiave read optimizer!

//////////// Sublime Snippets
// forx
// fory
// vectorx
// pairx
// sizex
// compile
// mod_operations
// graph_edges

// lca
// rmq
// fft
// bigint
// bigint_fft
// bigint_normal
// hash_gnu
// simple_segment
// tree_decomposition
// number_theory


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long long LL;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef pair<ll,int> li;
#ifdef DEBUG
    #define cek(x) cout<<x
#else
    #define cek(x) if(false){}
#endif // DEBUG

#define fi first
#define se second
#define INF 1000000000
#define INFLL 1000000000000000000LL
#define EPS 1e-9
#define PI acos(-1.0)
#define pb push_back
#define TC() while(tc--)
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORN(i,n) for(int i=0;i<=n;i++)
#define REP(i,a,b,c) for(int i=a;i<b;i+=c)
#define REPN(i,a,b,c) for(int i=a;i<=b;i+=c)
#define reset(a,b) memset(a,b,sizeof(a))
#define SZ(x) (int) x.size()

int r[100010], l[100010];
int mark[100010], decision[100010];

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

  int N;

  cin >> N;

  FOR (i, N) {
    cin >> mark[i];
  }

  l[0] = 0;
  for (int i = 1; i < N; i++) {
    l[i] = max(l[i - 1], mark[i]);
  }

  r[N - 1] = mark[N - 1];
  for (int i = N - 2; i >= 0; i--) {
    r[i] = max(mark[i], r[i + 1] - 1);
  }

  ll tot = 0;
  FOR (i, N) {
    tot += max(r[i], l[i]) - mark[i];
    // cout << max(r[i], l[i]) << " " ;
  }

  cout << tot << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long long LL;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef pair<ll,int> li;
#ifdef DEBUG
    #define cek(x) cout<<x
#else
    #define cek(x) if(false){}
#endif // DEBUG

#define fi first
#define se second
#define INF 1000000000
#define INFLL 1000000000000000000LL
#define EPS 1e-9
#define PI acos(-1.0)
#define pb push_back
#define TC() while(tc--)
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORN(i,n) for(int i=0;i<=n;i++)
#define REP(i,a,b,c) for(int i=a;i<b;i+=c)
#define REPN(i,a,b,c) for(int i=a;i<=b;i+=c)
#define reset(a,b) memset(a,b,sizeof(a))
#define SZ(x) (int) x.size()

int r[100010], l[100010];
int mark[100010], decision[100010];

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

  int N;

  cin >> N;

  FOR (i, N) {
    cin >> mark[i];
  }

  l[0] = 0;
  for (int i = 1; i < N; i++) {
    l[i] = max(l[i - 1], mark[i]);
  }

  r[N - 1] = mark[N - 1];
  for (int i = N - 2; i >= 0; i--) {
    r[i] = max(mark[i], r[i + 1] - 1);
  }

  int tot = 0;
  FOR (i, N) {
    tot += max(r[i], l[i]) - mark[i];
    // cout << max(r[i], l[i]) << " " ;
  }

  cout << tot << endl;

  return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <map>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cctype>
#include <vector>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;

inline char gc() {
	static const LL L = 233333;
	static char sxd[L], *sss = sxd, *ttt = sxd;
	if (sss == ttt) {
		ttt = (sss = sxd) + fread(sxd, 1, L, stdin);
		if (sss == ttt) {
			return EOF;
		}
	}
	return *sss++;
}

#define dd c = getchar()
#ifndef dd
#define dd c = gc()
#endif
inline char readalpha() {
	char dd;
	for (; !isalpha(c); dd);
	return c;
}

inline char readchar() {
	char dd;
	for (; c == ' '; dd);
	return c;
}

template <class T>
inline bool read(T& x) {
	bool flg = false;
	char dd;
	x = 0;
	for (; !isdigit(c); dd) {
		if (c == '-') {
			flg = true;
		} else if(c == EOF) {
			return false;
		}
	}
	for (; isdigit(c); dd) {
		x = (x << 1) + (x << 3) + (c ^ 48);
	}
	if (flg) {
		x = -x;
	}
	return true;
}
#undef dd

template <class T>
inline void write(T x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x < 10) {
		putchar(x | 48);
		return;
	}
	write(x / 10);
	putchar((x % 10) | 48);
}

typedef long long LL;

const int maxn = 100005;

int n;
int aa[maxn];
int maxx[maxn];

int main() {
	read(n);
	int mxpla = 1;
	for (int i = 1; i <= n; ++i) {
		read(aa[i]);
		maxx[i] = max(maxx[i - 1], aa[i]);
		if (aa[i] > aa[mxpla]) {
			mxpla = i;
		}
	}
	LL ans = 0;
	int nowx = aa[mxpla] + 1;
//	cout << mxpla << endl;
	for (int i = mxpla; i; --i) {
		ans += nowx - aa[i] - 1;
//		cout << i << ' ' << nowx << endl;
		if (maxx[i - 1] <= nowx - 1) {
			nowx--;
		}
	}
	for (int i = mxpla + 1; i <= n; ++i) {
		ans += aa[mxpla] - aa[i];
	}
	write(ans);
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <map>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cctype>
#include <vector>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;

inline char gc() {
	static const LL L = 233333;
	static char sxd[L], *sss = sxd, *ttt = sxd;
	if (sss == ttt) {
		ttt = (sss = sxd) + fread(sxd, 1, L, stdin);
		if (sss == ttt) {
			return EOF;
		}
	}
	return *sss++;
}

#define dd c = getchar()
#ifndef dd
#define dd c = gc()
#endif
inline char readalpha() {
	char dd;
	for (; !isalpha(c); dd);
	return c;
}

inline char readchar() {
	char dd;
	for (; c == ' '; dd);
	return c;
}

template <class T>
inline bool read(T& x) {
	bool flg = false;
	char dd;
	x = 0;
	for (; !isdigit(c); dd) {
		if (c == '-') {
			flg = true;
		} else if(c == EOF) {
			return false;
		}
	}
	for (; isdigit(c); dd) {
		x = (x << 1) + (x << 3) + (c ^ 48);
	}
	if (flg) {
		x = -x;
	}
	return true;
}
#undef dd

template <class T>
inline void write(T x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x < 10) {
		putchar(x | 48);
		return;
	}
	write(x / 10);
	putchar((x % 10) | 48);
}

typedef long long LL;

const int maxn = 100005;

int n;
int aa[maxn];
int maxx[maxn];

int main() {
	read(n);
	int mxpla = 1;
	for (int i = 1; i <= n; ++i) {
		read(aa[i]);
		maxx[i] = max(maxx[i - 1], aa[i]);
		if (aa[i] > aa[mxpla]) {
			mxpla = i;
		}
	}
	LL ans = 0;
	int nowx = aa[mxpla] + 1;
//	cout << mxpla << endl;
	for (int i = mxpla; i; --i) {
		ans += nowx - aa[i] - 1;
//		cout << i << ' ' << nowx << ' ' << aa[i] << endl;
		if (maxx[i - 1] < nowx - 1) {
			nowx--;
		}
	}
//	cout << ans << endl;
	for (int i = mxpla + 1; i <= n; ++i) {
		ans += aa[mxpla] - aa[i];
	}
	write(ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iterator>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
using namespace std;
const int SZ=4000010,mod=100;

int main()
{
	//std::ios::sync_with_stdio(0);
	//int casenum;
	//cin>>casenum;
//	int casenum;
//	cin>>casenum;
	//for(int time=1;time<=casenum;++time)
	{
		int n;
		cin>>n;
		vector<int> v;
		for(int i=0;i<n;++i)
		{
			int tmp;
			cin>>tmp;
			v.push_back(tmp);
		}
		long long ans=0;
		for(int i=1;i<n;++i)
		{
			if(v[i]<v[i-1])
			{
				ans+=v[i-1]-v[i];
				v[i]=v[i-1];
			}
		}
		for(int i=1;i<n;++i)
		{
			for(int j=i;v[j-1]+1<v[j]&&j>0;--j)
			{
				ans+=v[j]-1-v[j-1];
				v[j-1]=v[j]-1;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iterator>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
using namespace std;
const int SZ=4000010,mod=100;

int main()
{
	//std::ios::sync_with_stdio(0);
	//int casenum;
	//cin>>casenum;
//	int casenum;
//	cin>>casenum;
	//for(int time=1;time<=casenum;++time)
	{
		int n;
		cin>>n;
		int tmp;
		cin>>tmp;
		vector<int> v,src;
		v.push_back(1);
		src.push_back(tmp);
		for(int i=1;i<n;++i)
		{
			cin>>tmp;
			src.push_back(tmp);
			if(tmp>=v[i-1])
			{
				int delta=tmp-v[i-1];
				for(int j=1;j<=delta;++j)
				{
					v[i-j]=tmp+1-j;
				}
				v.push_back(tmp+1);
			}
			else v.push_back(v[i-1]);
		}

		for(int i=0;i<v.size();++i)--v[i];

		int ans=0;
		for(int i=0;i<v.size();++i)
		{
			v[i]-=src[i];
			ans+=v[i];
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iterator>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
using namespace std;
const int SZ=4000010,mod=100;

int main()
{
	//std::ios::sync_with_stdio(0);
	//int casenum;
	//cin>>casenum;
//	int casenum;
//	cin>>casenum;
	//for(int time=1;time<=casenum;++time)
	{
		int n;
		cin>>n;
		int tmp;
		cin>>tmp;
		vector<int> v,src;
		v.push_back(1);
		src.push_back(tmp);
		for(int i=1;i<n;++i)
		{
			cin>>tmp;
			src.push_back(tmp);
			if(tmp>=v[i-1])
			{
				int delta=tmp-v[i-1];
				//cout<<delta<<endl;
				for(int j=1;i-j>=0;++j)
				{
					if(v[i-j]==tmp+1-j)break;
					v[i-j]=tmp+1-j;
				}
				v.push_back(tmp+1);
			}
			else v.push_back(v[i-1]);
		}

//		for(int i=0;i<v.size();++i)
//		{
//			cout<<v[i]<<" ";
//		}cout<<endl;

		for(int i=0;i<v.size();++i)--v[i];

		long long ans=0;
		for(int i=0;i<v.size();++i)
		{
			v[i]-=src[i];
			ans+=v[i];
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iterator>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
using namespace std;
const int SZ=4000010,mod=100;

int main()
{
	//std::ios::sync_with_stdio(0);
	//int casenum;
	//cin>>casenum;
//	int casenum;
//	cin>>casenum;
	//for(int time=1;time<=casenum;++time)
	{
		int n;
		cin>>n;
		vector<int> v;
		for(int i=0;i<n;++i)
		{
			int tmp;
			cin>>tmp;
			v.push_back(tmp);
		}
		long long ans=0;
		for(int i=1;i<n;++i)
		{
			if(v[i]<v[i-1])
			{
				ans+=v[i-1]-v[i];
				v[i]=v[i-1];
			}
		}
		for(int i=n-1;i>=1;--i)
		{
			if(v[i-1]+1<v[i])
			{
				ans+=v[i]-1-v[i-1];
				v[i-1]=v[i]-1;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iterator>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
using namespace std;
const int SZ=4000010,mod=100;

int main()
{
	//std::ios::sync_with_stdio(0);
	//int casenum;
	//cin>>casenum;
//	int casenum;
//	cin>>casenum;
	//for(int time=1;time<=casenum;++time)
	{
		int n;
		cin>>n;
		int tmp;
		cin>>tmp;
		vector<int> v,src;
		v.push_back(1);
		src.push_back(tmp);
		for(int i=1;i<n;++i)
		{
			cin>>tmp;
			src.push_back(tmp);
			if(tmp>=v[i-1])
			{
				int delta=tmp-v[i-1];
				//cout<<delta<<endl;
				for(int j=1;i-j>=0;++j)
				{
					if(v[i-j]==tmp+1-j)break;
					v[i-j]=tmp+1-j;
				}
				v.push_back(tmp+1);
			}
			else v.push_back(v[i-1]);
		}

		for(int i=0;i<v.size();++i)
		{
			cout<<v[i]<<" ";
		}cout<<endl;

		for(int i=0;i<v.size();++i)--v[i];

		long long ans=0;
		for(int i=0;i<v.size();++i)
		{
			v[i]-=src[i];
			ans+=v[i];
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iterator>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
using namespace std;
const int SZ=4000010,mod=100;

int main()
{
	std::ios::sync_with_stdio(0);
	//int casenum;
	//cin>>casenum;
//	int casenum;
//	cin>>casenum;
	//for(int time=1;time<=casenum;++time)
	{
		int n;
		cin>>n;
		vector<int> v;
		for(int i=0;i<n;++i)
		{
			int tmp;
			cin>>tmp;
			v.push_back(tmp);
		}
		long long ans=0;
		for(int i=1;i<n;++i)
		{
			if(v[i]<v[i-1])
			{
				ans+=v[i-1]-v[i];
				v[i]=v[i-1];
			}
		}
		for(int i=1;i<n;++i)
		{
			for(int j=i;v[j-1]+1<v[j]&&j>0;--j)
			{
				ans+=v[j]-1-v[j-1];
				v[j-1]=v[j]-1;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iterator>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
using namespace std;
const int SZ=4000010,mod=100;

int main()
{
	//std::ios::sync_with_stdio(0);
	//int casenum;
	//cin>>casenum;
//	int casenum;
//	cin>>casenum;
	//for(int time=1;time<=casenum;++time)
	{
		int n;
		cin>>n;
		int tmp;
		cin>>tmp;
		vector<int> v,src;
		v.push_back(1);
		src.push_back(tmp);
		for(int i=1;i<n;++i)
		{
			cin>>tmp;
			src.push_back(tmp);
			if(tmp>=v[i-1])
			{
				int delta=tmp-v[i-1];
				for(int j=1;j<=delta;++j)
				{
					v[i-j]=tmp+1-j;
				}
				v.push_back(tmp+1);
			}
			else v.push_back(v[i-1]);
		}

		for(int i=0;i<v.size();++i)--v[i];

		long long ans=0;
		for(int i=0;i<v.size();++i)
		{
			v[i]-=src[i];
			ans+=v[i];
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iterator>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
using namespace std;
const int SZ=4000010,mod=100;

int main()
{
	//std::ios::sync_with_stdio(0);
	//int casenum;
	//cin>>casenum;
//	int casenum;
//	cin>>casenum;
	//for(int time=1;time<=casenum;++time)
	{
		int n;
		cin>>n;
		int tmp;
		cin>>tmp;
		vector<int> v,src;
		v.push_back(1);
		src.push_back(tmp);
		for(int i=1;i<n;++i)
		{
			cin>>tmp;
			src.push_back(tmp);
			if(tmp>=v[i-1])
			{
				int delta=tmp-v[i-1];
				for(int j=1;j<=delta;++j)
				{
					if(v[i-j]==tmp+1-j)break;
					v[i-j]=tmp+1-j;
				}
				v.push_back(tmp+1);
			}
			else v.push_back(v[i-1]);
		}

		for(int i=0;i<v.size();++i)--v[i];

		long long ans=0;
		for(int i=0;i<v.size();++i)
		{
			v[i]-=src[i];
			ans+=v[i];
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100005;
int n,t,cur=0,a[N],b[N];
ll ans;
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	b[n]=a[n]+1;
	for(int i=n-1;i;i--)b[i]=max(b[i+1]-1,a[i]+1);
	for(int i=2;i<=n;i++)b[i]=max(b[i],b[i-1]);
	for(int i=1;i<=n;i++)ans+=b[i]-a[i]-1;
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pii pair<int,int>
#define pll pair<ll,ll>
#define PB push_back
#define MP make_pair
#define N 100001

int a[N], b[N];

int main(){
  int n;

  scanf("%d", &n);

  for(int i = 1; i <= n; ++i){
    scanf("%d", &a[i]);
    b[i] = max(1 + a[i], b[i - 1]);
  }

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

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

  printf("%lld\n", ans);

  return 0;
}


#include <bits/stdc++.h>

using namespace std;
int n;
int t[10001];
vector<pair<int, int > > k;
long long p;
int m;
int l;
int h;
int main()
{
    cin >> n;
    for (int i=1; i<=n; i++) {
        int x;
        cin >> x;
        t[i]=x;
        k.push_back({i-x-1, i});
    }
    sort(k.begin(), k.end());
    for (int i=1; i<=n; i++) {
        while(l<k.size() && k[l].second<=i) {
            l++;
        }
        if (t[i]==m || k[l].first==h) {
            p+=m-t[i];
            m++;
        } else {
            h++;
            p+=m-t[i]-1;
        }
    }
    cout << p << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
int n;
int t[100001];
vector<pair<int, int > > k;
long long p;
int m;
int l;
int h;
int main()
{
    cin >> n;
    for (int i=1; i<=n; i++) {
        int x;
        cin >> x;
        t[i]=x;
        k.push_back({i-x-1, i});
    }
    sort(k.begin(), k.end());
    for (int i=1; i<=n; i++) {
        while(l<k.size() && k[l].second<=i) {
            l++;
        }
        if (t[i]==m || (k[l].first==h && k[l].second>i)) {
            p+=m-t[i];
            m++;
        } else {
            h++;
            p+=m-t[i]-1;
        }
    }
    cout << p << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
int n;
int t[100001];
vector<pair<int, int > > k;
long long p;
int m;
int l;
int h;
int main()
{
    cin >> n;
    for (int i=1; i<=n; i++) {
        int x;
        cin >> x;
        t[i]=x;
        k.push_back({i-x-1, i});
    }
    sort(k.begin(), k.end());
    for (int i=1; i<=n; i++) {
        while(l<k.size() && k[l].second<=i) {
            l++;
        }
        if (t[i]==m || k[l].first==h) {
            p+=m-t[i];
            m++;
        } else {
            h++;
            p+=m-t[i]-1;
        }
    }
    cout << p << endl;
    return 0;
}


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

const int N = 1e5 + 5;

int v[N], lvl[N];

long long ant;
int n;

int main() {
#ifdef HOME
	freopen("cf.in", "r", stdin);
	freopen("cf.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	cin >> n;
	for (int i = 1; i <= n; ++i)
		cin >> v[i];

	for (int i = n; i >= 1; --i)
		lvl[i] = max(lvl[i + 1] - 1, v[i] + 1);

	for (int i = 1; i <= n; ++i)
		lvl[i] = max(lvl[i], lvl[i - 1]);

	for (int i = 1; i <= n; ++i)
		ant+= lvl[i] - v[i] - 1;

	cout << ant << endl;

	return 0; }


//be name khoda :)

#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int)x.size())
#define rep(i, n) for(int i = 0; i < (n); i++)
#define res(i, s) for(int i = 0; i < (sz(s)); i++)
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define clear(a) (memset((a),0,sizeof(a)))
typedef long long ll;

const int N = 1e5 + 123;

int ne[N], n, a[N];
ll ans = 1;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	rep(i, n)
	{
		cin >> a[i];
		ans -= a[i];
	}
	ne[n - 1] = n;
	for(int i = n - 2; i >= 0; i--)
	{
		ne[i] = i + 1;
		while(ne[i] != n && a[ne[i]] <= a[i])
			ne[i] = ne[ne[i]];
	}
	int now = 1, i;
	for(i = 0; i < n - 1; i++)
	{
		if(ne[i] == n)
			break;
		if(a[ne[i]] + 1 - now == ne[i] - i)
			now++;
		ans += now;
		// cout << i << ' ' << now << ' ' << ans << endl;
	}
	ans += (ll)(n - i - 1) * now;
	cout << ans - n << endl;
	return 0;
}


//be name khoda :)

#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int)x.size())
#define rep(i, n) for(int i = 0; i < (n); i++)
#define res(i, s) for(int i = 0; i < (sz(s)); i++)
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define clear(a) (memset((a),0,sizeof(a)))
typedef long long ll;

const int N = 1e5 + 123;

int n, a[N];
ll ans;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	rep(i, n)
	{
		cin >> a[i];
		ans -= a[i];
	}
	vi v;
	for(int i = 0; i < n; i++)
		if(!sz(v) || a[i] > a[v.back()])
			v.pb(i);
	v.pb(n);

	int now = 0;
	for(int i = 0; i < n; i++)
	{
		int ne = *lower_bound(all(v), i);
		int nex = a[ne] + 1;
		if(nex - now > ne - i)
			now++;
		// cout << i << ' ' << now << ' ' << ne << endl;
		ans += now;
	}

	cout << ans - n << endl;
	return 0;
}


//be name khoda :)

#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int)x.size())
#define rep(i, n) for(int i = 0; i < (n); i++)
#define res(i, s) for(int i = 0; i < (sz(s)); i++)
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define clear(a) (memset((a),0,sizeof(a)))
typedef long long ll;

const int N = 1e5 + 123;

int a[N];

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;
	ll ans = 0;
	cin >> n;
	rep(i, n)
		cin >> a[i], ans -= a[i];
	vi maxs;
	for(int i = 0; i < n; i++)
		if(!sz(maxs) || a[i] > a[maxs.back()])
		{
			while(sz(maxs) and i - maxs.back() < a[i] - a[maxs.back()])
				maxs.pop_back();
			maxs.pb(i);
		}
	maxs.pb(n);

	int now = 0;
	rep(i, n)
	{
		int ne = *lower_bound(all(maxs), i);
		int nex = a[ne] + 1;
		if(nex - now > ne - i)
			now++;
		// cout << i << ' ' << now << ' ' << ne << endl;
		ans += now;
	}
	cout << ans - n << endl;
	return 0;
}


//be name khoda :)

#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int)x.size())
#define rep(i, n) for(int i = 0; i < (n); i++)
#define res(i, s) for(int i = 0; i < (sz(s)); i++)
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define clear(a) (memset((a),0,sizeof(a)))
typedef long long ll;

const int N = 1e5 + 123;

int n, a[N];
ll ans;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	rep(i, n)
	{
		cin >> a[i];
		ans -= a[i];
	}
	vi v;
	for(int i = 0; i < n; i++)
		if(!sz(v) || a[i] > a[v.back()])
		{
			while(sz(v) and i - v.back() < a[i] - a[v.back()])
				v.pop_back();
			v.pb(i);
		}
	v.pb(n);

	int now = 0;
	for(int i = 0; i < n; i++)
	{
		int ne = *lower_bound(all(v), i);
		int nex = a[ne] + 1;
		if(nex - now > ne - i)
			now++;
		// cout << i << ' ' << now << ' ' << ne << endl;
		ans += now;
	}

	cout << ans - n << endl;
	return 0;
}


//be name khoda :)

#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int)x.size())
#define rep(i, n) for(int i = 0; i < (n); i++)
#define res(i, s) for(int i = 0; i < (sz(s)); i++)
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define clear(a) (memset((a),0,sizeof(a)))
typedef long long ll;

const int N = 1e5 + 123;

int a[N];

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;
	ll ans;
	cin >> n;
	rep(i, n)
		cin >> a[i], ans -= a[i];
	vi maxs;
	for(int i = 0; i < n; i++)
		if(!sz(maxs) || a[i] > a[maxs.back()])
		{
			while(sz(maxs) and i - maxs.back() < a[i] - a[maxs.back()])
				maxs.pop_back();
			maxs.pb(i);
		}
	maxs.pb(n);

	int now = 0;
	rep(i, n)
	{
		int ne = *lower_bound(all(maxs), i);
		int nex = a[ne] + 1;
		if(nex - now > ne - i)
			now++;
		// cout << i << ' ' << now << ' ' << ne << endl;
		ans += now;
	}
	cout << ans - n << endl;
	return 0;
}


//be name khoda :)

#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int)x.size())
#define rep(i, n) for(int i = 0; i < (n); i++)
#define res(i, s) for(int i = 0; i < (sz(s)); i++)
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define clear(a) (memset((a),0,sizeof(a)))
typedef long long ll;

const int N = 1e5 + 123;

int ne[N], n, a[N];
ll ans = 1;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	rep(i, n)
	{
		cin >> a[i];
		ans -= a[i];
	}
	vi v;
	for(int i = 0; i < n; i++)
		if(!sz(v) || a[i] > a[v.back()])
			v.pb(i);
	v.pb(n);
	rep(i, n)
		ne[i] = *upper_bound(all(v), i);
	int now = 1, i;
	for(i = 0; i < n - 1; i++)
	{
		if(ne[i] == n)
			break;
		if(a[ne[i]] + 1 - now >= ne[i] - i)
			now++;
		ans += now;
		// cout << i << ' ' << now << ' ' << ans << endl;
	}
	ans += (ll)(n - i - 1) * now;
	cout << ans - n << endl;
	return 0;
}


//be name khoda :)

#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int)x.size())
#define rep(i, n) for(int i = 0; i < (n); i++)
#define res(i, s) for(int i = 0; i < (sz(s)); i++)
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define clear(a) (memset((a),0,sizeof(a)))
typedef long long ll;

const int N = 1e5 + 123;

int n, a[N];
ll ans;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	rep(i, n)
	{
		cin >> a[i];
		ans -= a[i];
	}
	vi v;
	for(int i = 0; i < n; i++)
		if(!sz(v) || a[i] > a[v.back()])
		{
			if(sz(v) and i - v.back() < a[i] - a[v.back()])
				v.pop_back();
			v.pb(i);
		}
	v.pb(n);

	int now = 0;
	for(int i = 0; i < n; i++)
	{
		int ne = *lower_bound(all(v), i);
		int nex = a[ne] + 1;
		if(nex - now > ne - i)
			now++;
		// cout << i << ' ' << now << ' ' << ne << endl;
		ans += now;
	}

	cout << ans - n << endl;
	return 0;
}


#include<bits/stdc++.h>
#define maxn 10000010
#define mod 1000000007
#define ll long long
using namespace std;
int a[100010];
int nd[100010];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++) scanf("%d",&a[i]);
	for(int i=n-2;i>=0;i--){
		nd[i]=max(nd[i+1]-1,a[i+1]);
	}
	ll ans=0,sum=0;
	for(int i=0;i<n;i++){
		if(sum==a[i]){
			sum++;
		}
		else if(sum==nd[i]-1){
			ans+=sum-a[i];
			sum++;
		}
		else{
			ans+=sum-a[i]-1;
		}
	}
	printf("%lld\n",ans);
	return 0;
}


#include <iostream>

using namespace std;

int main(){
	int n;
	cin >> n;
	int A[n], M[n];
	for (int i = 0; i < n; i++){
		cin >> A[i];
	}
	M[n-1]=A[n-1]+1;
	for (int i=n-2; i>-1; i--){
		M[i] = max(A[i]+1, M[i+1]-1);
	}
	for (int i=1; i<n; i++){
		M[i] = max(M[i-1], M[i]);
	}
	long long sum=0;
	for (int i=0; i<n; i++){
		sum += M[i] - A[i] - 1;
	}
	cout << sum << endl;
}


#include <iostream>
#include <vector>

using namespace std;

int n, m;
int main(){

    cin >> n;
    vector<int> aboveMarks;
    vector<int> marksNeeded;

    int temp;

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

        aboveMarks.push_back(temp);
        if(i ==0){
            marksNeeded.push_back(1);
        }else{
            marksNeeded.push_back(max(marksNeeded[i-1], temp+1));
        }
    }

    int answer = marksNeeded[marksNeeded.size()-1]-1 - aboveMarks[aboveMarks.size()-1];

    for(int i = marksNeeded.size()-2; i>=0; i--) {
        if (marksNeeded[i] + 1 < marksNeeded[i + 1]) {
            marksNeeded[i] = marksNeeded[i + 1] - 1;
        }
        answer += marksNeeded[i] - 1 - aboveMarks[i];
    }

    cout << answer << endl;

    return 0;
}


#include <algorithm>
#include <cstdio>

typedef long long LL;

const int N = 100050;

int m[N], a[N];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", &m[i]);
  LL ans = 0;
  a[0] = 1;
  for (int i = 1; i < n; ++i) a[i] = std::max(a[i - 1], m[i] + 1);
  for (int i = n - 1, j = -10000000; i >= 0; --i) {
    j = std::max(j, m[i] - i);
    ans += std::max(a[i], j + i + 1) - m[i] - 1;
  }
  printf("%I64d\n", ans);
  return 0;
}


#include <algorithm>
#include <cstdio>

typedef long long LL;

const int N = 100050;

int m[N], a[N];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", &m[i]);
  LL ans = 0;
  a[0] = 1;
  for (int i = 1; i < n; ++i) a[i] = std::max(a[i - 1], m[i] + 1);
  for (int i = n - 1, j = -1000; i >= 0; --i) {
    j = std::max(j, m[i] - i);
    ans += std::max(a[i], j + i + 1) - m[i] - 1;
  }
  printf("%I64d\n", ans);
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define err(a) cout << #a << " = " << a << endl

typedef long long int64;
typedef pair<int, int> ii;

int m[100005], t[100005];
int n;

int main() {
    // freopen("input.txt", "r", stdin);

    ios::sync_with_stdio(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> m[i];
        t[i] = max(t[i-1], m[i]+1);
    }

    int64 res = 0;

    for (int i = n; i >= 1; i--) {
        t[i] = max(t[i], t[i+1]-1);
        res += t[i] - m[i] - 1;
    }

    cout << res << endl;

    return 0;
}


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

const int N = 1e5 + 10;
int a[N];
int b[N];
int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0;i<n;i++)
        scanf("%d", a+i);
    for(int i=1;i<n;i++)
        b[i] = max(a[i], b[i-1]);
    for(int i=0;i+1<n;i++)
        b[i] = max(a[i+1]-1, b[i]);
    ll ans = 0;
    for(int i=0;i<n;i++)
        ans += b[i] - a[i];
    cout<<ans<<'\n';
}


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

const int N = 1e5 + 10;
int a[N];
int b[N];
int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0;i<n;i++)
        scanf("%d", a+i);
    for(int i=1;i<n;i++)
        b[i] = max(a[i], b[i-1]);
    for(int i=n-2;i>=0;i--)
    {
        b[i] = max({a[i+1]-1, b[i], b[i+1]-1});
    }

    ll ans = 0;
    for(int i=0;i<n;i++)
        ans += b[i] - a[i];
    cout<<ans<<'\n';
}


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

const int N = 1e5 + 10;
int a[N];
int b[N];
int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0;i<n;i++)
        scanf("%d", a+i);
    b[1] = 1;
    for(int i=2;i<n;i++)
        b[i] = max(a[i], b[i-1]);
    for(int i=0;i+1<n;i++)
        b[i] = max(a[i+1]-1, b[i]);
    ll ans = 0;
    for(int i=0;i<n;i++)
        ans += b[i] - a[i];
    cout<<ans<<'\n';
}


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

const int N = 1e5 + 10;
int a[N];
int b[N];
int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0;i<n;i++)
        scanf("%d", a+i);
    b[1] = 1;
    for(int i=2;i<n;i++)
    {
        b[i] = max(a[i], b[i-1]);
    }

    for(int i=0;i+1<n;i++)
        b[i] = max(a[i+1]-1, b[i]);
    ll ans = 0;
    for(int i=0;i<n;i++)
        ans += b[i] - a[i];
    cout<<ans<<'\n';
}


#include <cstdio>
#include <algorithm>

using namespace std;

int v[100010],d[100010];

int main()
{
    //freopen("file.in","r",stdin);
    //freopen("file.out","w",stdout);
    int n;
    long long sol=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&v[i]);
        d[i]=max(d[i-1],v[i]+1);
    }
    for(int i=n;i>=1;i--) d[i]=max(d[i],d[i+1]-1);
    for(int i=1;i<=n;i++) sol+=d[i]-v[i]-1;
    printf("%I64d",sol);
    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

long long v[100010];

int main()
{
    //freopen("file.in","r",stdin);
    //freopen("file.out","w",stdout);
    int n;
    long long sol=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%I64d",&v[i]);
    long long nr=0;
    for(int i=1;i<=n;i++)
    {
        long long c=v[i]-nr;
        if(v[i]+1>nr) sol+=((c+1)*c/2LL);
        nr=max(nr,v[i]+1);
        sol+=nr-v[i]-1;
    }
    printf("%I64d",sol);
    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

long long v[100010];

int main()
{
    //freopen("file.in","r",stdin);
    //freopen("file.out","w",stdout);
    int n;
    long long sol=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%I64d",&v[i]);
    long long nr=0;
    for(int i=1;i<=n;i++)
    {
        long long c=v[i]-nr+1;
        if(v[i]+1>=nr) sol+=1LL*c*(c-1)/2;
        nr=max(nr,v[i]+1);
        sol+=nr-v[i]-1;
    }
    printf("%I64d",sol);
    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

int v[100010];

int main()
{
    //freopen("file.in","r",stdin);
    //freopen("file.out","w",stdout);
    int n;
    long long sol=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&v[i]);
    int nr=0;
    for(int i=1;i<=n;i++)
    {
        int c=v[i]-nr;
        if(v[i]+1>nr) sol+=1LL*c*(c+1)/2;
        nr=max(nr,v[i]+1);
        sol+=nr-v[i]-1;
    }
    printf("%I64d",sol);
    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

int v[100010];

int main()
{
    //freopen("file.in","r",stdin);
    //freopen("file.out","w",stdout);
    int n;
    long long sol=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&v[i]);
    int nr=0;
    for(int i=1;i<=n;i++)
    {
        int c=v[i]-nr;
        if(v[i]+1>=nr) sol+=1LL*c*(c+1)/2;
        nr=max(nr,v[i]+1);
        sol+=nr-v[i]-1;
    }
    printf("%I64d",sol);
    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

int v[100010];

int main()
{
    //freopen("file.in","r",stdin);
    //freopen("file.out","w",stdout);
    int n;
    long long sol=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&v[i]);
    int nr=0;
    for(int i=1;i<=n;i++)
    {
        if(v[i]+1>nr) sol+=v[i]-nr;
        nr=max(nr,v[i]+1);
        sol+=nr-v[i]-1;
    }
    printf("%I64d",sol);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define SZ(a) (int)a.size()
#define PB push_back
#define x first
#define y second

typedef long long ll;
typedef vector<int> VI;
typedef vector<vector<int> > VVI;

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout << std::setprecision(9);

	int N;
	cin >> N;

	VI m(N+1);
	VI d(N+1);
	d[0] = 0;

	for(int i = 1; i <= N; i++) {
		cin >> m[i];
		d[i] = max(d[i-1],m[i]+1);
	}

	for(int i = N-1; i >= 1; i--) {
		d[i] = max(d[i], d[i+1]-1);
	}

	ll sum = 0;
	for(int i = 1; i <= N; i++) {
		sum += d[i]-m[i]-1;
	}

	cout << sum << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sqr(x) ((x)*(x))
#define sz(v) int(v.size())
#define par make_pair
#define pb push_back
#define fi first
#define se second
#define endl '\n'
template<class X, class Y> void amax(X& x, const Y& y){if(x < y) x = y;}
template<class X, class Y> void amin(X& x, const Y& y){if(x > y) x = y;}
typedef vector<int> vi; typedef pair<int, int> ii; typedef long long ll; typedef long double ld;
const int INF = 1e9 + 10; const ll INFL = 1e18 + 10;
const int MAX = 1e5 + 10;

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

void process(){
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];

    for(int i = n; i >= 1; i--){
        b[i] = max(a[i] + 1, b[i + 1] - 1);
    }

    int line = 1;
    ll res = 0;
    for(int i = 2; i <= n; i++){
        if(line >= b[i]) res += line - b[i];
        else{
            res += line;
            line++;
        }
    }
    cout << res;
}

int main(){
    ios_base::sync_with_stdio(false);
    //~ cin.tie(nullptr);
    //~ cout << fixed; cout.precision(10);
    //~ freopen(".in", "r", stdin);
    process();
}


#include <bits/stdc++.h>
using namespace std;
#define sqr(x) ((x)*(x))
#define sz(v) int(v.size())
#define par make_pair
#define pb push_back
#define fi first
#define se second
#define endl '\n'
template<class X, class Y> void amax(X& x, const Y& y){if(x < y) x = y;}
template<class X, class Y> void amin(X& x, const Y& y){if(x > y) x = y;}
typedef vector<int> vi; typedef pair<int, int> ii; typedef long long ll; typedef long double ld;
const int INF = 1e9 + 10; const ll INFL = 1e18 + 10;
const int MAX = 1e5 + 10;

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

void process(){
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];

    for(int i = n; i >= 1; i--){
        b[i] = max(a[i] + 1, b[i + 1] - 1);
    }

    int line = 1;
    ll res = 0;
    for(int i = 2; i <= n; i++){
        if(line < b[i]) line++;
        res += line - (a[i] + 1);
    }
    cout << res;
}

int main(){
    ios_base::sync_with_stdio(false);
    //~ cin.tie(nullptr);
    //~ cout << fixed; cout.precision(10);
    //~ freopen("c.in", "r", stdin);
    process();
}


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

int arr[100005];
int val[100005];

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

}


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

int arr[100005];
int val[100005];

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

	for(int i=1;i<=n;i++)
	{
		val[i]=max(val[i],val[i-1]);
	}
	for(int i=1;i<=n;i++)
	{
		ans+=val[i]-arr[i]-1;
	}
	printf("%lld\n",ans);

}


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

int arr[100005];
int val[100005];

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

}


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

vector <int> does_not;
int m[100010];
bool does[100010];

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

    for (int i(1); i <= n; i++) {
        cin >> m[i];
        while (m[i] > nract) {
            int x = does_not.back();
            does_not.pop_back();
            does[x] = 1;
            nract++;
        }
        if (m[i] == nract)
            does[i] = 1, nract++;
        else
            does_not.push_back(i);
    }

    long long ans(0);

    nract = 0;

    for (int i(1); i <= n; i++) {
        nract += does[i];
        ans += nract - m[i] - 1;
    }

    cout << ans << '\n';
    return 0;
}


#include<bits/stdc++.h>
#define oo 999999999
using namespace std;
typedef long long int lli;
lli gcd(lli a,lli b) {if(a==0||b==0) return 0;if(a==b) return a;if(a>b) return gcd(a-b,b);return gcd(a,b-a);}
lli max(lli a,lli b) {if(a>b) return a; return b;}
lli min(lli a,lli b) {if(a>b) return b; return a;}
int main()
{
	ios_base::sync_with_stdio;
	cin.tie(NULL);
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	//
	//	Saw the editorial
	//

	lli n;
	cin>>n;
	lli arr[n],t[n];
	for(lli i=0;i<n;i++)
	{
		cin>>arr[i];
		t[i]=i+1;
	}
	for(lli i=1;i<n;i++)
		t[i]=max(t[i-1],arr[i]+1);
	for(lli i=n-1;i>0;i--)
		t[i-1]=max(t[i]-1,t[i-1]);
	//for (int i = 0; i < n; ++i)
	//	cout<<t[i]<<" ";
	//cout<<"\n";
	lli answer=0;
	for(lli i=0;i<n;i++)
		answer+=t[i]-1-arr[i];
	cout<<answer;
	int dont_hack_please;
	return 0;
}


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

const int MAXN = 1e5 + 10;

int arr[MAXN], n;
int minim[MAXN];

int main() {
	ios_base::sync_with_stdio(false);
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> arr[i];
	int r_max = -1e8;
	for(int i = n; i > 0; i--) {
		minim[i] = max(0, r_max + i + 1);
		r_max = max(r_max, arr[i] - i);
	}
	int curr = 0, ans = 0;
	for(int i = 1; i <= n; i++) {
		if(minim[i] > curr) {
			assert(curr + 1 == minim[i]);
			ans += curr - arr[i];
			curr++;
		} else if(curr == arr[i]) {
			curr++;
		} else {
			ans += curr - arr[i] - 1;
		}
	}
	cout << ans << '\n';
	/* for(int i = 1; i <= n; i++) {
		cerr << minim[i] << " ";
	}
	cerr << endl; */
	return 0;
}


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

const int MAXN = 1e5 + 10;

int arr[MAXN], n;
int minim[MAXN];

int main() {
	ios_base::sync_with_stdio(false);
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> arr[i];
	int r_max = -1e8;
	for(int i = n; i > 0; i--) {
		minim[i] = max(0, r_max + i + 1);
		r_max = max(r_max, arr[i] - i);
	}
	minim[1] = max(1, minim[1]);
	long long curr = 0, ans = 0;
	for(int i = 1; i <= n; i++) {
		if(minim[i] > curr) {
			assert(curr + 1 == minim[i]);
			ans += curr - arr[i];
			curr++;
		} else if(curr == arr[i]) {
			curr++;
		} else {
			ans += curr - arr[i] - 1;
		}
	}
	cout << ans << '\n';
	/* for(int i = 1; i <= n; i++) {
		cerr << minim[i] << " ";
	}
	cerr << endl; */
	return 0;
}