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