1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;
    vector<int> V(n + 1);
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        V[a] = i;
    }

    long long res = 0;
    int i = V[n], j = V[n] + 1, correct = 1;
    for (int size = 1; size <= n; size++) {
        int needed = (size / 2) + 1;
        while (needed > correct) {
            int next = V[n - correct];
            if (next >= j) j = next + 1;
            else if (next < i) i = next;
            size = max(size, j - i);
            needed = (size / 2) + 1;
            correct++;
        }
        int x = size - (j - i), tiles = min(x, i) + min(x, n - j);
        res += 1LL + tiles - x;
    }

    cout << 2 * n + 1 << ' ' << res;
}