https://www.acmicpc.net/problem/2847
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int arr[]=new int[n];
int count=0;
for(int i=0;i<n;i++){
arr[i]=s.nextInt();
}
for(int i=n-1;i>0;i--){
if(arr[i]<=arr[i-1]){
count+=arr[i-1]-arr[i]+1;
arr[i-1]=arr[i]-1;
}
}
System.out.print(count);
}
}