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