#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MINV = 1;
const int MAXV = 10;
void a_m() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int step(vector<int> r) {
  int maxv = r[0];
  int count = 0;
  for (int i = 1; i < r.size(); i++) {
    if (maxv > r[i]) {
      count += (maxv - r[i]);
    } else {
      maxv = r[i];
    }
  }
  return count;
}
int32_t main() {
  a_m();
  int T;
  cin >> T;
  vector<int> r(T);
  for (int i = 0; i < T; i++) {
    cin >> r[i];
  }
  cout << step(r) << endl;
}
