#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
using namespace std;

#define ll long long int

int main()
{
    ll n, k;
    cin >> n;
    ll arr[n] = {0};
    for (ll x = 0; x < n; x++)
    {
        cin >> arr[x];
    }
    ll sum = 0;
    ll mx = 0;
    for (ll x = 0; x < n; x++)
    {
        if (arr[x] < mx)
        {
            sum = sum + mx - arr[x];
        }
        else
        {
            mx = arr[x];
        }
    }
    cout << sum << "\n";
}