#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  vector<int> a(t);
  for (int i = 0; i < t; i++) {
    cin >> a[i];
  }
  int s = 0;
  int m = a[0];
  int ans = 0;
  for (int i = 0; i < t; ++i) {
    if (m < a[i]) {
      m = a[i];
    } else {
      ans += m - a[i];
    }
  }
  cout << ans << endl;
  return 0;
}
