#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int* h = new int[n];
  int p = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    if (i != 0 && p > h[i]) {
      ans += (p - h[i]);
    } else {
      p = h[i];
    }
  }
  cout << ans << endl;
}
