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