#include <iostream>

using namespace std;

int main(){

    int N = 0;
    cin >> N;
    int A[N] = {0};
    int sum = 0;

    for (int i = 0; i < N; i++){
        cin >> A[i];
    }

    for(int i = 0; i < N; i++){
        while(A[i] > A[i + 1]){
            sum++;
            A[i + 1]++;
        }
    }

    cout << sum << endl;
}

