#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  long long max = -1;
  long long min = 10000000000;
  long long maxID = -1;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (max <= a[i]) {
      max = a[i];
      maxID = i;
    }
    if (min >= a[i]) min = a[i];
  }
  long long submax = -1;
  for (int i = 0; i < maxID; i++) {
    if (submax <= a[i]) submax = a[i];
  }
  for (int i = 0; i < maxID; i++) {
    ans += submax - a[i];
  }
  for (int i = maxID; i < n; i++) {
    ans += max - a[i];
  }
  cout << ans << endl;
}
