#include <bits/stdc++.h>
using namespace std;
int n, i, j, a[200001], b[200001], ans;
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) cin >> a[i];
  b[1] = a[1];
  for (i = 1; i <= n - 1; i++) {
    j = i + 1;
    if (a[j] < b[i]) {
      b[j] = b[i];
      ans += b[i] - a[j];
    } else
      b[j] = a[j];
  }
  cout << ans << endl;
}
