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