#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a, ma, b = 0, k, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    k = abs(a - b);
    if (a < b) {
      ma = b;
    } else {
      ma = a;
    }
    if (a >= b) {
      ans = ans + k * i;
    } else {
      ans = ans + k;
    }
    b = ma;
  }
  cout << ans;
}
