#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, total = 0, cur, curmax, i;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> cur;
    if (i == 0) {
      curmax = cur;
    } else {
      if (cur < curmax) {
        total += (curmax - cur);
      } else {
        curmax = cur;
      }
    }
  }
  cout << total << "\n";
  return 0;
}
