#include <bits/stdc++.h>
using namespace std;
int arr[200005];
int main() {
  int n, f;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  f = arr[0];
  int suma = 0, sumb = 0;
  for (int i = 1; i < n; i++) {
    if (arr[i] < f) {
      suma += f - arr[i];
    } else if (arr[i] > f) {
      f = arr[i];
    }
  }
  f = arr[n - 1];
  for (int i = n - 2; i >= 0; i--) {
    if (arr[i] < f) {
      sumb += f - arr[i];
    } else if (arr[i] > f) {
      f = arr[i];
    }
  }
  if (suma > sumb) {
    cout << sumb << endl;
  } else {
    cout << suma << endl;
  }
  return 0;
}
