#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  vector<int> A;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    A.push_back(tmp);
  }
  int sum1 = 0;
  int min_height = 0;
  for (int i = 0; i < N; i++) {
    if (min_height < A[i]) {
      sum1 += A[i];
      min_height = A[i];
    } else {
      sum1 += min_height;
    }
  }
  int sum2 = 0;
  for (int i = 0; i < N; i++) {
    sum2 += A[i];
  }
  cout << sum1 - sum2;
  return 0;
}
