#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a = 0, ans = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    ans += max(0, a - x);
    a = x + max(0, a - x);
  }
  printf("%d\n", ans);
  return 0;
}
