#include <bits/stdc++.h>
using namespace std;
using L64 = int64_t;
int main(void) {
  int N, num, total = 0, max = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> num;
    if (max <= num) {
      max = num;
    } else {
      total += max - num;
    }
  }
  cout << total;
  return 0;
}
