#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int N;
  cin >> N;
  long long int arr[N];
  long long int max;
  long long int result = 0;
  for (long long int i = 0; i < N; i++) {
    cin >> arr[i];
  }
  max = arr[0];
  for (long long int i = 0; i < N; i++) {
    if (arr[i] > max) {
      max = arr[i];
    }
    if (i != 0 && arr[i - 1] - arr[i] > 0) {
      result += std::max(arr[i - 1] - arr[i], max - arr[i]);
    }
  }
  cout << result;
}
