#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  int *a_height;
  int *takasa;
  int cur_max, sum = 0;
  cin >> N;
  a_height = new int[N];
  takasa = new int[N];
  for (int i = 0; i < N; i++) {
    cin >> a_height[i];
  }
  takasa[0] = 0;
  cur_max = a_height[0];
  for (int i = 1; i < N; i++) {
    if (cur_max > a_height[i]) {
      takasa[i] = cur_max - a_height[i];
    } else {
      takasa[i] = 0;
      cur_max = a_height[i];
    }
    sum += takasa[i];
  }
  cout << sum << endl;
  delete[] a_height;
  delete[] takasa;
}
