#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, ar[200000], i, j, br[200001] = {0};
  int c = 0, mx = 0, mn = 1000000000;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> ar[i];
  }
  mn = ar[0];
  for (i = 1; i < n; i++) {
    if (mn > ar[i])
      c = c + mn - ar[i];
    else
      mn = ar[i];
  }
  cout << c << endl;
}
