#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  long long a, b = 0, c = 0, d = 0, e = 0;
  cin >> a;
  long long arr[a];
  for (long long i = 0; i < a; i++) {
    cin >> arr[i];
  }
  for (long long i = a - 1; i > 0; i--) {
    d = d + 1;
    if (arr[i - 1] > arr[i]) {
      c = c + d * (arr[i - 1] - arr[i]);
    } else {
      d = 0;
    }
  }
  cout << c;
}
