#include #include using namespace std; using namespace atcoder; typedef int64_t lint; #define rep(i, n) for(int i=0; i; using vvi = vector>; template inline void vin(vector& v) { rep(i, v.size()) cin >> v.at(i); } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template inline void drop(T x) { cout << x << endl; exit(0); } template void vout(vector v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; } constexpr lint LINF = LLONG_MAX/2; int main() { lint N; cin >> N; vi v(N), w(N); vin(v); rep(i, N) v[i]--; if (N == 2) { if (!v[0]) drop(0); else drop(1); } lint a=0, b=0, c=0, x, y, z; set s; rep(i, N) s.insert(i); reverse(all(v)); rep(i, N) { auto u = s.upper_bound(v[i]); if (u == s.end()) { auto l = s.lower_bound(0); w[i] = *l; s.erase(*l); } else { w[i] = *u; s.erase(*u); } } reverse(all(v)); reverse(all(w)); rep(i, N) { if (v[i] < w[i]) a += i+1; if (v[i] > w[i]) a -= i+1; } std::cout << a << '\n'; }