#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int main(void) {
  int n;
  cin >> n;
  int mx = -1;
  int c = 0;
  int pos = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
    if (x > mx) {
      mx = x;
      pos = i;
    }
  }
  for (int i = pos; i < n; i++) {
    c += abs(v[i] - mx);
  }
  int te = n - ++pos;
  te++;
  while (te--) {
    v.pop_back();
  }
  while (v.size()) {
    mx = 0;
    for (int i = 0; i < v.size(); i++) {
      if (v[i] > mx) {
        mx = v[i];
        pos = i;
      }
    }
    for (int i = pos; i < v.size(); i++) {
      c += abs(v[i] - mx);
    }
    n = v.size();
    te = n - ++pos;
    te++;
    if (te < 0) break;
    while (te--) {
      v.pop_back();
    }
  }
  cout << c << endl;
}
