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