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