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