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