#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> vec;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    vec.push_back(x);
  }
  int m = vec[0];
  int sum = 0;
  for (int i = 1; i < n; i++) {
    if (m > vec[i]) {
      sum = sum + m - vec[i];
    } else
      m = vec[i];
  }
  cout << sum;
  return 0;
}
