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