#include using namespace std; typedef long long ll; typedef pair P; #define DUMP(x) cout << #x << " = " << (x) << endl; #define FOR(i, m, n) for (ll i = m; i < n; i++) #define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) #define ALL(v) (v).begin(), (v).end() #define SZ(x) ll(x.size()) int main() { ll n; cin >> n; vector a(n); set st; FOR(i, 1, n + 1) { st.insert(i); } REP(i, n) { cin >> a[i]; } if (n == 1) { cout << 0 << endl; return 0; } ll ans = 0; IREP(i, n) { auto it = st.lower_bound(a[i] + 1); auto it2 = st.lower_bound(a[i]); if (it == st.end()) { if (it2 == st.end()) { ans -= i + 1; st.erase(st.begin()); } else { st.erase(it2); } } else { ans += i + 1; st.erase(it); } } ll c = n * (n + 1) / 2; REP(i, n) { if (a[i] == n) { c -= 2 * (i + 1); } } ans = max(ans, c); ans = max(ans, 0LL); cout << ans << endl; }