#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j, a, size, temp, num, count = 0;
  cin >> size;
  vector<int> arr(size);
  for (i = 0; i < size; i++) cin >> arr[i];
  for (i = 0; i < size - 1; i++) {
    if (arr[i + 1] < arr[i]) {
      temp = (arr[i] - arr[i + 1]);
      arr[i + 1] += temp;
      count += temp;
    }
  }
  cout << count << endl;
  return 0;
}
