#include <bits/stdc++.h>
using namespace std;
int main() {
  int n = 0;
  long long moves = 0;
  cin >> n;
  vector<long long> arr(n, 0);
  for (int i = 0; i < n; i++) cin >> arr[i];
  long long last = arr[0];
  long long min = INT_MAX;
  bool found = false;
  for (int i = 1; i < n; i++) {
    if (arr[i] < arr[i - 1]) {
      if (arr[i] < min) {
        min = arr[i];
        found = true;
      }
    }
    if (arr[i] > arr[i - 1] || i + 1 == n) {
      if (found) {
        moves += last - min;
        found = false;
      }
      min = INT_MAX;
      last = arr[i];
    }
  }
  cout << moves << endl;
  return 0;
}
