#include <bits/stdc++.h>
const long double PI = 3.141592653589793236L;
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int max_ele = a[0];
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < max_ele) {
      ans += max_ele - a[i];
      a[i] = max_ele;
    } else {
      max_ele = a[i];
    }
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  while (T--) {
    solve();
  }
  return 0;
}
