#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int now;
  int bef;
  int ans = 0;
  cin >> bef;
  for (int i = 0; i < n - 1; i++) {
    cin >> now;
    if (bef > now) {
      ans += (bef - now);
    } else {
      bef = now;
    }
  }
  cout << ans << endl;
}
