#include <bits/stdc++.h>
using namespace std;
signed main(void) {
  int n, now, ans = 0;
  scanf("%d", &n);
  int a[n];
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  now = a[0];
  for (int i = 1; i < n; i++) {
    if (a[i] >= now)
      now = a[i];
    else
      ans += now - a[i];
  }
  cout << ans << endl;
  return 0;
}
