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