#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int inf = 0x3f3f3f3f;
constexpr ll linf = 0x3f3f3f3f3f3f3f3fLL;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<pair<ll, int>> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  ll ans = 0;
  for (int i = 0; i < n * 2; i++) {
    for (int j = 0; j < n - 1; j++) {
      ll tmp = 0;
      tmp += a[j].first * (a[j].second <= j ? 1 : -1);
      tmp += a[j + 1].first * (a[j + 1].second >= j + 1 ? 1 : -1);
      if (tmp > 0) {
        swap(a[j], a[j + 1]);
        ans += tmp;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
