#include <bits/stdc++.h>
using namespace std;
int a[222222];
int main() {
  int n, a, b = 0, ba = 0;
  cin >> n;
  while (n--) {
    cin >> a;
    if (a < b) {
      ba += b - a;
      a += b - a;
    }
    b = a;
  }
  cout << ba;
  return 0;
}
