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