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