#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2 * 1e5 + 5;
int n, ans, a[MAXN], maxx;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    ans += max(maxx - a[i], 0);
    maxx = max(maxx, a[i]);
  }
  printf("%lld", ans);
  return 0;
}
