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