#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int val;
  vector<int> v, sol;
  cin >> n;
  int i;
  for (i = 0; i < n; i++) {
    cin >> val;
    v.push_back(val);
  }
  int max = INT_MIN;
  int ans = 0;
  for (i = 0; i < n; i++) {
    if (max < v[i]) {
      max = v[i];
    } else {
      ans = ans + max - v[i];
    }
  }
  cout << ans;
  return 0;
}
