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