#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int ar[n];
  for (int i = 0; i < n; ++i) cin >> ar[i];
  long long int end1 = n, end2 = n, index = 1, sum = 0;
  while (index != 0) {
    long long int mx = 0;
    for (int i = 0; i < end1; ++i) {
      if (mx < ar[i]) {
        mx = ar[i];
        index = i;
      }
    }
    for (int i = index; i < end2; ++i) {
      sum += (mx - ar[i]);
      ar[i] = mx;
    }
    end1 = index;
    end2 -= index + 1;
  }
  cout << sum;
  return 0;
}
