#include using namespace std; using ll = long long; const ll MOD = 1000000007; using P = pair; #define rep(i, n) for(int i = 0; i < n; i++) #define all(x) (x).begin(),(x).end() int main(){ ll n; cin >> n; vector p(n), pinv(n); rep(i,n){ cin >> p[i]; p[i]--; pinv[p[i]] = i; } ll ans = 0; rep(i,n-1){ ll nans = -2 * (pinv[n-1-i] + 1); nans += (n-i) * (n-i+1) / 2; ans = max(ans,nans); } cout << ans << endl; return 0; }