#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> arr;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    arr.push_back(t);
  }
  int cnt = 0;
  int j = 0;
  int i = 1;
  int max_so_far = -1;
  while (i < n) {
    max_so_far = max(max_so_far, arr[j]);
    if (arr[i] < max_so_far) {
      cnt += (max_so_far - arr[i]);
    }
    i += 1;
    j += 1;
  }
  cout << cnt << endl;
}
