#include<iostream>
using namespace std;


int A[1000000000];

int main() {

        int N, max_temp = 0, sum = 0;

        cin >> N;

        for ( int i = 0; i < N; i++){


                cin >> A[i];

        }


        for (int i = 0; i < N; i++){

        if ( max_temp < A[i] ){

                max_temp = A[i];

                }
        else if ( max_temp >= A[i]) {

                sum += max_temp - A[i];

        }
        }


        cout << sum;

        return 0;


}