#include <bits/stdc++.h>
const int MOD = 1000000007;
using namespace std;
int main() {
  int n;
  int cnt = 0;
  cin >> n;
  vector<int> a(n);
  for (int(i) = 0; (i) < (n); (i)++) {
    cin >> a[i];
  }
  for (int(i) = 0; (i) < (n - 1); (i)++) {
    int step;
    if (a[i + 1] < a[i]) {
      step = a[i] - a[i + 1];
      a[i + 1] += step;
      cnt += step;
    }
  }
  cout << cnt << endl;
  return 0;
}
