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