#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int arr[n];
  int z = -1;
  int x;
  for (int i = 0; i < n; i++) {
    int y;
    cin >> y;
    arr[i] = y;
  }
  for (int i = 0; i < n; i++) {
    if (z < arr[i]) {
      z = arr[i];
      x = i;
    }
  }
  int sum = 0;
  for (int i = 1; i < x; i++) {
    if (arr[i] < arr[i - 1]) {
      sum += (arr[i - 1] - arr[i]);
      arr[i] = arr[i - 1];
    }
  }
  for (int i = x + 1; i < n; i++) {
    sum += (arr[x] - arr[i]);
  }
  cout << sum;
  return 0;
}
