#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  vector<long long> vl(n);
  for (int i = 0; i < n; i++) cin >> vl[i];
  long long ma = -1;
  long long pos = -1;
  for (int i = 0; i < n; i++) {
    if (ma < vl[i]) {
      ma = vl[i];
      pos = i;
    }
  }
  long long mA = -1, Pos = -1;
  for (int i = 0; i < pos; i++) {
    if (mA < vl[i]) {
      mA = vl[i];
      Pos = i;
    }
  }
  long long ans = 0;
  for (int i = pos; i < n; i++) {
    ans += ma - vl[i];
  }
  for (int i = 0; i < pos; i++) {
    ans += mA - vl[i];
  }
  cout << ans << endl;
  return 0;
}
