#include <bits/stdc++.h>
using namespace std;
int arr[200001] = {0};
int main() {
  int a, total = 0;
  cin >> a;
  for (int i = 0; i < a; i++) {
    cin >> arr[i];
  }
  for (int i = 1; i < a; i++) {
    if (arr[i - 1] > arr[i]) {
      total += arr[i - 1] - arr[i];
      arr[i] = arr[i - 1];
    }
  }
  cout << total;
}
