#include <bits/stdc++.h>
int main(void) {
  int ans = 0, N, ai, i, max = 0;
  scanf("%d", &N);
  for (i = 0; i < N; i++) {
    scanf("%d", &ai);
    if (ai > max)
      max = ai;
    else if (ai < max)
      ans += (max - ai);
  }
  printf("%d\n", ans);
  return 0;
}
