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