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