#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  int n;
  cin >> n;
  int q, k = 0, mx = 0;
  for (int i = 1; i <= n; i++) {
    cin >> q;
    mx = max(mx, q);
    if (q < mx) k += mx - q;
  }
  cout << k << '\n';
  ;
  return 0;
}
