#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long> v;
  long long a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    v.push_back(a);
  }
  reverse(v.begin(), v.end());
  long long z = 0;
  long long x = 0;
  long long count = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (v.at(j) > v.at(i)) {
        x = v.at(j) - v.at(i);
      }
      if (x > z) {
        z = x;
      }
    }
    count += z;
    z = 0;
    x = 0;
  }
  cout << count << endl;
  return 0;
}
