#include <bits/stdc++.h>
using namespace std;
long long int check(long long int a[], long long int n) {
  long long int b = a[n - 1];
  long long int flag = 1;
  for (int i = n - 1, j = n - 2; i > 1, j > 0; i--, j--) {
    if (a[i] > a[j]) {
      flag = 1;
    } else
      flag = 0;
    break;
  }
  return flag;
}
int main() {
  long long int n;
  long long int a[200001];
  long long int max, min;
  scanf("%lld", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  max = min = a[0];
  long long int m = 0, t = 0;
  for (long long int i = 0; i < n; i++) {
    if (max <= a[i]) {
      max = a[i];
      t = i;
    }
  }
  long long int sum1 = 0;
  long long int max1 = a[0];
  for (long long int i = 0; i < t; i++) {
    if (max1 <= a[i]) {
      max1 = a[i];
    }
  }
  int q = 0;
  for (long long int i = 0; i < t; i++) {
    if (!check(a, i + 1)) {
      q = i - 1;
    }
    sum1 += (a[q] - a[i]);
  }
  long long int sum2 = 0;
  for (long long int i = t; i < n; i++) {
    sum2 += (max - a[i]);
  }
  if ((sum1 + sum2) == 0)
    printf("%lld", sum2 + sum1);
  else {
    printf("0");
  }
}
