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