#include <bits/stdc++.h>
using namespace std;
int y[300000] = {};
int main() {
  int cow, gav = 0;
  cin >> cow;
  cin >> y[0];
  for (int i = 1; i < cow; i++) {
    cin >> y[i];
    if (y[i] < y[i - 1]) {
      gav += y[i - 1] - y[i];
      y[i] = y[i - 1];
    }
  }
  cout << gav;
}
