#include <bits/stdc++.h>
int n, x, lastans, ans;
int main() {
  scanf("%d", &n);
  scanf("%d", &lastans);
  for (int i = 2; i <= n; i++)
    scanf("%d", &x), ans += lastans > x ? lastans - x : 0,
        lastans = x < lastans ? lastans : x;
  printf("%d", ans);
  return 0;
}
