#include <bits/stdc++.h>
using namespace std;
int intinput() {
  int x;
  cin >> x;
  return x;
}
vector<int> forintinput(int n) {
  vector<int> x(100000);
  for (int i = 0; i < n; i++) cin >> x[i];
  return x;
}
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  a = forintinput(n);
  int Max = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > Max) {
      Max = a[i];
    }
    if (a[i] < Max) {
      cnt += (Max - a[i]);
    }
  }
  cout << cnt << endl;
}
