#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> arr(n);
  for (auto& e : arr) cin >> e;
  auto cpy = arr;
  int last = arr[0];
  for (int a = 1; a < n; a++) {
    if (arr[a] < last)
      arr[a] = last;
    else
      last = arr[a];
  }
  int pk = accumulate(arr.begin(), arr.end(), 0);
  int pk2 = accumulate(cpy.begin(), cpy.end(), 0);
  cout << pk - pk2;
  return 0;
}
