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