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