#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int ma = INT_MIN, sum = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    ma = max(ma, x);
    sum += ma - x;
  }
  cout << sum;
  return 0;
}
