#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (int)(n); i++) {
    cin >> a[i];
  }
  int step = 0;
  for (int i = 0; i < (int)(n - 1); i++) {
    if (a[i] > a[i + 1]) {
      step += a[i] - a[i + 1];
      a[i + 1] += a[i] - a[i + 1];
    }
  }
  cout << step << endl;
  return 0;
}
