#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (--t >= 0) {
    long long int n, x, a, b, c;
    cin >> n;
    long long int arr[n];
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
    }
    int maxa = arr[0];
    long long int sum = 0;
    for (int i = 0; i < n; i++) {
      if (arr[i] > maxa)
        maxa = arr[i];
      else {
        sum = sum + (maxa - arr[i]);
      }
    }
    cout << sum << endl;
  }
  return 0;
}
