#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = int(0); i < int(n); i++) cin >> a.at(i);
  int cur = 0;
  int res = 0;
  for (int i = int(0); i < int(n); i++) {
    int tmp = a.at(i);
    if (i == 0) {
      cur = tmp;
      continue;
    }
    int add = 0;
    if (cur > tmp) {
      add = cur - tmp;
      res += add;
    }
    cur = tmp + add;
  }
  cout << res << endl;
  return 0;
}
