#include <bits/stdc++.h>
using namespace std;
int n[200001];
int main() {
  int t;
  while (cin >> t) {
    double start = clock(), end;
    for (int i = 0; i < t; i++) cin >> n[i];
    int first = n[0];
    int ans = 0;
    for (int i = 1; i < t; i++) {
      if (n[i] < first)
        ans += first - n[i];
      else
        first = n[i];
    }
    end = clock();
    ;
    cout << ans << "\n";
  }
}
