#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, cnt = 0;
  cin >> n;
  int ara[n];
  for (int i = 0; i < n; i++) {
    cin >> ara[i];
  }
  int mx = ara[0];
  for (int i = 1; i < n; i++) {
    if (mx < ara[i]) mx = ara[i];
    if (ara[i] < mx) cnt += (mx - ara[i]);
  }
  cout << cnt << "\n";
  return 0;
}
