#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x;
  cin >> n;
  int max = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (x < max) {
      ans += max - ans;
    } else
      max = x;
  }
  cout << ans;
  return 0;
}
