#include <bits/stdc++.h>
using namespace std;
static int n;
static vector<int> arr;
int solve(void) {
  int res = 0;
  for (int i = 1; i < n; i++) {
    if (arr[i] < arr[i - 1]) {
      res += arr[i - 1] - arr[i];
      arr[i] = arr[i - 1];
    }
  }
  return res;
}
int main() {
  while (cin >> n) {
    arr.resize(n);
    for (int i = 0; i < n; i++) cin >> arr[i];
    cout << solve() << "\n";
  }
  return 0;
}
