#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, cal, val;
  cin >> n;
  int sum = 0;
  cin >> val;
  int mval = val;
  for (int i = 1; i < n; i++) {
    cin >> val;
    if (val >= mval)
      mval = val;
    else {
      sum += mval - val;
    }
  }
  cout << sum;
}
