#include <bits/stdc++.h>
using namespace std;
int main() {
  int X;
  cin >> X;
  int shin[X];
  for (int i; i < X; i++) {
    cin >> shin[i];
  }
  int num = 1;
  int dai = 0;
  for (int i = 1; i < X; i++) {
    if (shin[i - 1] > shin[i]) {
      dai = dai + shin[i - 1] - shin[i];
      shin[i] = shin[i - 1];
    }
  }
  cout << dai;
}
