#include <bits/stdc++.h>
using namespace std;
int main() {
  int u = 0, n, an = 0;
  cin >> n;
  vector<int> ls(n);
  for (int &x : ls) cin >> x;
  for (int &x : ls) {
    if (u) {
      if (ls[u - 1] > x) {
        an += (ls[u - 1] - x);
        x = ls[u - 1];
      }
    }
    u++;
  }
  cout << an;
}
