#include <bits/stdc++.h>
int main() {
  int n;
  scanf("%d", &n);
  int a[200000], max = -1, j;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (max < a[i]) {
      max = a[i];
      j = i;
    }
  }
  int count = 0;
  for (int i = 0; i < j; i++) {
    if (a[i] < a[0]) {
      count += a[0] - a[i];
    }
  }
  for (int i = j; i < n; i++) {
    if (a[i] < a[j]) {
      count += a[j] - a[i];
    }
  }
  printf("%d\n", count);
}
