#include #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; using pii = pair; int main() { int n; cin >> n; set st; vector a(n + 1); rep(i, n) { cin >> a[i + 1]; st.insert(a[i + 1]); } ll ans = 0; for (int i = n; i > 0; i--) { auto itr = st.upper_bound(a[i]); if (itr == st.end()) itr = st.lower_bound(a[i]); if (itr == st.end()) itr = st.lower_bound(0); if (*itr > a[i]) ans += i; if (*itr < a[i]) ans -= i; st.erase(*itr); } cout << ans << endl; return 0; }