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