#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000;
const int mod = 1000000007;
const int nil = -1;
long long int i, n, m, k, ans = 0;
long long int a = 0, front = 0;
int main() {
  scanf(" %lld", &n);
  for (i = 0; i < n; i++) {
    front = a;
    scanf(" %lld", &a);
    if (front > a) {
      ans += front - a;
      a = front;
    }
  }
  printf("%d\n", ans);
}
