#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v.at(i);
  }
  int step = 0;
  int cursor = n - 1;
  while (true) {
    if (cursor <= 0) break;
    int maxh = 0, key;
    for (int i = 0; i <= cursor; i++) {
      if (maxh < v.at(i)) {
        maxh = v.at(i);
        key = i;
      }
    }
    for (int i = key; i <= cursor; i++) {
      step += (maxh - v.at(i));
    }
    cursor = key - 1;
  }
  cout << step << endl;
}
