#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed;
  cout << setprecision(10);
  long long n;
  cin >> n;
  long long arr[n];
  for (long long i = 0; i < n; i++) {
    cin >> arr[i];
  }
  long long count = 0;
  for (long long i = n - 1; i >= 1; i--) {
    if (arr[i] < arr[i - 1]) {
      count += (arr[i - 1] - arr[i]) * (n - i - 1);
    }
  }
  cout << count << "\n";
  return 0;
}
