#include using namespace std; long long n, p, m, j; long long u[200001]; int main(){ cin >> n; for(int i = 0; i < n; i++){ cin >> p; u[p] = i + 1; } j = n; m = n * (n + 1) / 2; while(j > 1 && u[j] > u[j - 1] * 2){ m -= u[j]; j--; } if(j > 1) m -= u[j] * 2; else m = 0; cout << m << endl; }