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