#include <bits/stdc++.h>
using namespace std;
void Ok() {
  long int a[300013], b, c, d, e, f, g, h, t, i, j, k, l, m, n, Min, Max;
  long int ans_1, ans_2, ans_3, cnt_1, cnt_2, cnt_3, flag_1, flag_2, flag_3,
      temp, sum;
  double aa, bb, cc, dd, ee, ff, gg;
  char aaa[13], bbb, ccc, ddd;
  string spc, in, store;
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  cnt_1 = 0;
  for (i = 2; i <= n; i++) {
    if (a[i] < a[i - 1]) {
      cnt_1 += (a[i - 1] - a[i]);
    }
  }
  cout << cnt_1 << endl;
  return;
}
int main() {
  Ok();
  return 0;
}
