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