#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, maxv = -1, s = 0, x;
  cin >> n;
  cin >> x;
  maxv = x;
  for (i = 1; i <= n; i++) {
    cin >> x;
    if (x < maxv)
      s += maxv - x;
    else
      maxv = x;
  }
  cout << s << endl;
  return 0;
}
