#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  long long cnt = 0;
  for (int i = 0; i < N; i++) {
    for (int k = i + 1; k < N; k++) {
      if (A.at(i) - A.at(k) > 0) {
        cnt += (A.at(i) - A.at(k));
        A.at(k) = A.at(i);
      }
    }
  }
  cout << cnt << endl;
}
