#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  {
    long long n;
    cin >> n;
    long long *arr = new long long[n];
    for (long long i = 0; i < n; i++) cin >> arr[i];
    long long ans = 0;
    long long diff;
    for (long long i = 1; i < n; i++) {
      if (arr[i] > arr[i - 1]) {
        diff = arr[i] - arr[i - 1];
        ans += diff;
        arr[i] += diff;
      }
    }
    cout << ans << "\n";
  }
}
