#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, l, max, S;
  cin >> N;
  max = 0;
  S = 0;
  for (int i = 0; i < N; i++) {
    cin >> l;
    if (max > l) {
      S += (max - l);
    } else {
      max = l;
    }
  }
  cout << S << endl;
}
