#include <bits/stdc++.h>
using namespace std;
int n, a[3000003], ans, cmax;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cmax = max(a[i], cmax);
    if (a[i] < cmax) ans += abs(a[i] - cmax);
  }
  cout << ans << '\n';
}
