#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int h;
  int ans = 0;
  int max = -1;
  while (cin >> h) {
    if (h >= max) {
      max = h;
      continue;
    } else {
      ans += max - h;
    }
  }
  cout << ans << endl;
  return 0;
}
