#include <bits/stdc++.h>
using namespace std;
int main() {
  int ans = 0, max;
  int n, tmp;
  cin >> n;
  cin >> max;
  for (int i = 1; i < n; i++) {
    cin >> tmp;
    if (tmp < max)
      ans += max - tmp;
    else
      max = tmp;
  }
  cout << ans << '\n';
  return 0;
}
