#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
int main(int argc, char const *argv[]) {
  int N;
  cin >> N;
  int a[N];
  for (auto &x : a) {
    cin >> x;
  }
  int mx = a[0];
  int ans = 0;
  for (int i = 1; i < N; i++) {
    if (mx > a[i]) {
      ans += mx - a[i];
    } else if (mx < a[i]) {
      mx = a[i];
    }
  }
  cout << ans << endl;
}
