#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, c = 0;
  int t, temp = 0;
  cin >> n;
  while (n--) {
    cin >> t;
    if (temp > t) {
      c = c + abs(temp - t);
      t = t + abs(temp - t);
    }
    temp = t;
  }
  cout << c << endl;
}
