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
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <numeric>
#include <assert.h>

using namespace std;
using ull = unsigned long long;
using ll = long long;


#ifdef GGDEBUG
#define dbg printf
#else 
#define dbg //dbg
#endif

ll possibilities(int from, int to, int n, int pos) {
    int actual_length = to-from + 1;
    if (pos < actual_length) {
        return 0;
    }

    ll min_starting_positions = to - pos + 1;
    ll max_starting_positions = n - pos;
    // cout << "raw:     (" << min_starting_positions << ", " << max_starting_positions << ")" << endl;

    min_starting_positions = max(min_starting_positions, 0ll);
    max_starting_positions = min(max_starting_positions, static_cast<ll>(from));

    // cout << "coerced: (" << min_starting_positions << ", " << max_starting_positions << ")" << endl;


    return max(0ll, max_starting_positions - min_starting_positions + 1);
}

int main() {
    assert(possibilities(1, 2, 5, 1) == 0);

    assert(possibilities(1, 2, 5, 2) == 1);
    assert(possibilities(1, 2, 5, 3) == 2);
    assert(possibilities(1, 2, 5, 4) == 2);
    assert(possibilities(1, 2, 5, 5) == 1);

    assert(possibilities(2, 3, 7, 2) == 1);
    assert(possibilities(2, 3, 7, 3) == 2);
    assert(possibilities(2, 3, 7, 4) == 3);
    assert(possibilities(2, 3, 7, 5) == 3);
    assert(possibilities(2, 3, 7, 6) == 2);
    assert(possibilities(2, 3, 7, 7) == 1);

    assert(possibilities(2, 2, 7, 1) == 1);
    assert(possibilities(2, 2, 7, 2) == 2);
    assert(possibilities(2, 2, 7, 3) == 3);
    assert(possibilities(2, 2, 7, 4) == 3);
    assert(possibilities(2, 2, 7, 5) == 3);
    assert(possibilities(2, 2, 7, 6) == 2);
    assert(possibilities(2, 2, 7, 7) == 1);

    assert(possibilities(3, 3, 7, 1) == 1);
    assert(possibilities(3, 3, 7, 2) == 2);
    assert(possibilities(3, 3, 7, 3) == 3);
    assert(possibilities(3, 3, 7, 4) == 4);
    assert(possibilities(3, 3, 7, 5) == 3);
    assert(possibilities(3, 3, 7, 6) == 2);
    assert(possibilities(3, 3, 7, 7) == 1);

    assert(possibilities(0, 0, 7, 1) == 1);
    assert(possibilities(0, 0, 7, 2) == 1);
    assert(possibilities(0, 0, 7, 3) == 1);
    assert(possibilities(0, 0, 7, 4) == 1);
    assert(possibilities(0, 0, 7, 5) == 1);
    assert(possibilities(0, 0, 7, 6) == 1);
    assert(possibilities(0, 0, 7, 7) == 1);

    dbg("OK!\n");
    
    int n;
    scanf("%d", &n);

    vector<int> in;
    vector<int> index_of;
    index_of.resize(n+1);
    for (int i = 0; i < n; i++) {
        int x;
        scanf("%d", &x);
        in.push_back(x);
        index_of[x] = i;
    }

    ull result = 0;

    int possible_length = 1;
    int actual_length;
    int from, to;

    // length 1
    from = index_of[n];
    to = index_of[n];
    actual_length = 1;
    possible_length = 1;
    result += possibilities(from, to, n, possible_length);

    dbg("new boundaries: (%d, %d)\n", from, to);

    for (actual_length = 2; actual_length <= n; ++actual_length) {
        dbg("\n");

        auto required = n - actual_length + 1;
        dbg("now length: %d, searching for %d\n", actual_length, required);
        auto index = index_of[required];

        // expand boundaries
        if (index < from) {
            from = index;
        }
        if (index > to) {
            to = index;
        }
        dbg("new boundaries: (%d, %d)\n", from, to);

        possible_length = 2*actual_length - 2;
        auto res_now = possibilities(from, to, n, possible_length);
        dbg("%ld possibilities for length %d\n", res_now, possible_length);
        result += res_now;

        possible_length = 2*actual_length - 1;
        res_now = possibilities(from, to, n, possible_length);
        dbg("%ld possibilities for length %d\n", res_now, possible_length);
        result += res_now;

        



        // from = index_of[];
        // to = index_of[n];
        // possible_length = actual_length;
        // result += possibilities(from, to, n, possible_length);
    }

    cout << 2*n+1 << " " << result << endl;
    

    return 0;
}