#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec[i];
  }
  auto minmax = minmax_element(vec.begin(), vec.end());
  int max = (int)*minmax.second;
  int pos = 0;
  for (int i = 0; i < N; i++) {
    if (vec[i]) {
      pos = i;
    }
  }
  int tmp, sum = 0;
  for (int i = pos; i < N; i++) {
    tmp = max - vec[i];
    sum += tmp;
  }
  for (int i = 1; i < pos; i++) {
    if (vec[i] < vec[i - 1]) {
      tmp = vec[i - 1] - vec[i];
      sum += tmp;
    }
  }
  cout << sum << endl;
}
