#include <stdio.h>
​
int main(void){
    int n = 0;
    int max = 0;
    int temp = 0;
    long long sum = 0;
​
    scanf("%d", &n);
​
    for(int i = 0; i < n; i++){
        scanf("%d", &temp);
​
        if(temp < max){
            sum += (max - temp);
        } else {
            max = temp;
        }
    }
​
    printf("%ld",sum);
    
    return 0;
