#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv) {
  int n, last, res = 0;
  scanf("%d%d", &n, &last);
  while (--n) {
    int x;
    scanf("%d", &x);
    if (x < last) {
      res += last - x;
      x = last;
    }
    last = x;
  }
  printf("%d\n", res);
  return 0;
}
