#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, count = 0, i, j;
  cin >> n;
  int a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n - 1; i++) {
    for (j = i + 1; j < n; j++) {
      if (a[i] > a[j]) {
        count += (a[i] - a[j]);
        a[j] = a[i];
      }
    }
  }
  cout << count;
  return 0;
}
