#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  long long a[n], mx = 0;
  int pos = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > mx) {
      mx = a[i];
      pos = i;
    }
  }
  long long c = 0;
  for (int i = pos; i < n; i++) {
    c += abs(a[i] - mx);
  }
  while (pos != 0) {
    int pre = pos;
    mx = 0;
    for (int i = 0; i < pos; i++) {
      if (a[i] > mx) {
        mx = a[i];
        pos = i;
      }
    }
    for (int i = pos; i < pre; i++) {
      c += abs(a[i] - mx);
    }
  }
  cout << c << endl;
}
