#include <bits/stdc++.h>
using namespace std;
using namespace std;
int f() {
  int n;
  int a, b;
  int ans = 0;
  int dif = 0;
  cin >> n;
  cin >> a;
  for (int i = 0; i < (int)(n - 1); ++i) {
    cin >> b;
    dif = a - b;
    if (dif > 0) {
      ans += dif;
      a = b + dif;
    } else
      a = b;
  }
  cout << ans << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  f();
  return 0;
}
