#include <bits/stdc++.h>
using namespace std;
int main() {
  int q, p = 0, n, ans = 0;
  cin >> q;
  while (q--) {
    cin >> n;
    if (n < p)
      ans += p - n;
    else
      p = n;
  }
  cout << ans << "\n";
  return 0;
}
