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