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