#include <bits/stdc++.h>
using namespace std;
const int M = 2e5;
int A[M];
void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  int sum = 0;
  int max_h = A[0];
  for (int i = 1; i < n; i++) {
    if (A[i] > max_h) {
      max_h = A[i];
    }
    if (A[i] < max_h) {
      sum += abs(max_h - A[i]);
    }
  }
  cout << sum << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}
