#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int curh = -1;
  int res = 0;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    if ((curh < 0) || (curh < temp)) {
      curh = temp;
    } else if (curh > temp) {
      res += curh - temp;
    }
  }
  cout << res << endl;
}
