#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 cost = 0, leftMax = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] > leftMax) leftMax = arr[i];
    cost += max(0, leftMax - arr[i]);
  }
  cout << cost;
  return 0;
}
