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