#include <bits/stdc++.h>
using namespace std;
int main() {
  int a;
  cin >> a;
  vector<int> array;
  for (int i = 0; i < a; i++) {
    int r;
    cin >> r;
    array.push_back(r);
  }
  int sm = 0;
  int p = max_element(array.begin(), array.end()) - array.begin();
  int r = *max_element(array.begin(), array.end());
  int q = *max_element(array.begin(), array.end() - (p + 1));
  for (int i = 0; i < p; i++) {
    if (array[i] < q) sm += (q - array[i]);
  }
  for (int i = p + 1; i < array.size(); i++) {
    if (array[i] < r) sm += (r - array[i]);
  }
  cout << sm;
}
