#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int N, prev;
  cin >> N >> prev;
  int sum = 0;
  for (int i = 1; i < N; i++) {
    int a;
    cin >> a;
    if (prev <= a)
      prev = a;
    else
      sum += prev - a;
  }
  cout << sum << endl;
}
