#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; vector a(n + 1); for (int i = 1; i <= n; i++) cin >> a[i]; vector s(n + 1); for (int i = 1; i <= n; i++) s[a[i]] = i; ll cur = (ll)n * (n + 1) / 2, ans = 0; for (int i = n; i >= 1; i--) { ans = max(ans, cur - s[i] * 2); cur -= s[i]; } cout << ans << endl; return 0; }