#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int a[100000];
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  int curmax{a[0]};
  int ans{0};
  for (int i = 1; i < n; i++) {
    if (curmax < a[i])
      curmax = a[i];
    else
      ans += curmax - a[i];
  }
  cout << ans << '\n';
  return 0;
}
