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