#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  int max1 = 0;
  for (int i = 0; i < n; i++) {
    if (max1 < A[i]) {
      max1 = A[i];
    }
  }
  int count1 = -1;
  for (int i = 0; i < n; i++) {
    count1++;
    if (A[i] == max1) {
      break;
    }
  }
  int max2 = 0;
  for (int i = 0; i < count1; i++) {
    if (max2 < A[i]) {
      max2 = A[i];
    }
  }
  int count2 = -1;
  for (int i = 0; i < count1; i++) {
    count2++;
    if (A[i] == max2) {
      break;
    }
  }
  int sum1 = 0;
  for (int i = count2; i < count1; i++) {
    sum1 += (max2 - A[i]);
  }
  int sum2 = 0;
  for (int i = count1; i < n; i++) {
    sum2 += (max1 - A[i]);
  }
  cout << sum1 + sum2 << endl;
}
