#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, sum = 0, a = 0, b = 0;
  cin >> N;
  cin >> a;
  for (int i = 1; i < N; i++) {
    cin >> b;
    if (a - b > 0) {
      sum = sum + a - b;
    } else {
      a = b;
    }
  }
  printf("%d\n", sum);
}
