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