#include<stdio.h>
#include<stdlib.h>

int main(){
    int N;
    scanf("%d%*c",&N);
    char *s = (char *)malloc(12 * N * sizeof(char));
    gets(s);
    long unsigned int max = 0;
    long unsigned int sum = 0;
    for(int i = 0;i < N; i++){
        int n;
        long unsigned int a;
        sscanf(s,"%ld %n",&a,&n);
        if(max < a)max = a;
        sum += max - a;
        s = &s[n];
    }
    printf("%d\n",sum);
    return 0;
}g