#include <bits/stdc++.h> #define rep(i, l, r) for (int i = (l); i < (r); i++) using namespace std; typedef long long ll; int main() { ll N; cin >> N; vector<int> P(N), Q(N); rep(i, 0, N) { cin >> P[i]; Q[P[i] - 1] = i + 1; } if (N == 1) { cout << 0 << endl; return 0; } if (N == 2) { if (P[0] == 1) cout << 0 << endl; else cout << 1 << endl; return 0; } ll t = N * (N + 1) / 2, ans = 0; //cout << ans << endl; rep(i, 0, N) { ans = max(ans, t - 2 * Q[N - 1 - i]); t -= Q[N - 1 - i]; //cout << i << " " << ans << endl; } cout << ans << endl; }