#include <bits/stdc++.h>
using namespace std;
const int n = 2e5;
int arr[n + 1];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int diff = 0, sum = 0;
  for (int i = 1; i < n; i++) {
    if (arr[i] < arr[i - 1]) {
      diff = arr[i - 1] - arr[i];
      sum += diff;
      arr[i] = arr[i] + diff;
    }
  }
  cout << sum << "\n";
}
