#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n, k, max, p;
  cin >> n;
  vector<int> s;
  for (int i = 0; i < (int)(n); i++) {
    cin >> k;
    s.push_back(k);
  }
  max = s[0];
  p = 0;
  for (int j = 0; j < (int)(n - 1); j++) {
    if (s[j + 1] < max) {
      p = p + (max - s[j + 1]);
    }
    if (s[j + 1] >= max) {
      max = s[j + 1];
    }
  }
  cout << p << endl;
  return 0;
}
