#include <bits/stdc++.h>
using namespace std;
long long int n, x[2000001], t, ans;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> x[i];
  for (int i = 1; i < n; i++) {
    for (int j = i - 1; j >= 0; j--)
      if (x[i] < x[j]) {
        ans += x[j] - x[i];
        x[i] += x[j] - x[i];
      }
  }
  cout << ans;
}
