#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
int main() {
  int n, a;
  cin >> n;
  int maxa = 0;
  if (n == 1) {
    cout << 0;
    return 0;
  }
  cin >> a;
  maxa = max(a, maxa);
  int s = 0;
  for (int(i) = 0; (i) < (n - 1); ++(i)) {
    cin >> a;
    maxa = max(a, maxa);
    s += maxa - a;
  }
  cout << s;
  return 0;
}
