#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, s = 0, lst = 0, a;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a;
    if (a > lst)
      lst = a;
    else
      s += lst - a;
  }
  return 0;
}
