#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int a[200005];
  cin >> n;
  for (int i = 0; i < (n); i++) cin >> a[i];
  int prev = a[0];
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    int now = max(0, a[i] - prev);
    ans += now;
    prev = a[i] + now;
  }
  cout << ans << endl;
  return 0;
}
