#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &ai : a) cin >> ai;
  int sum = 0;
  int cur_max = 0;
  for (auto ai : a) {
    cur_max = max(cur_max, ai);
    sum += cur_max - ai;
  }
  cout << sum << '\n';
  return 0;
}
