#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
const ll MOD = 1000000007;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[4] = {1, 0, -1, 0};
const ll dx[4] = {0, 1, 0, -1};
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<pl> a(n);
  for (ll i = 0; i < n; i++) {
    ll p;
    cin >> p;
    a[i] = {p, i};
  }
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < n - 1; j++) {
      ll l, r;
      if (j < a[j].second)
        l = -a[j].first;
      else
        l = a[j].first;
      if (j + 1 > a[j + 1].second)
        r = -a[j + 1].first;
      else
        r = a[j + 1].first;
      if (l + r > 0) swap(a[j], a[j + 1]), ans += r + l;
    }
  }
  cout << ans << endl;
  return 0;
}
