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
#include <bits/stdc++.h>

using namespace std;

int n;
vector<int> a;

void inic () {
    n = 7;
    a = {1, 7, 2, 3, 7, 2, 9};
}

void input () {
    cin >> n;
    a.resize(n);
    for (auto &t : a) {
        cin >> t;
    }
}

void wypisz (deque<int> dummy) {
    while (!dummy.empty()) {
        cout << dummy.front() << ' ';
        dummy.pop_front();
    }
    cout << endl;
}

long long solve () {
    for (int i = 0; i < n; i++) {
        a.push_back(a[i]);
    }

    deque<int> zachwyt;
    zachwyt.push_back(a[0]);
    for (auto t : a) {
        if (zachwyt.back() < t) {
            zachwyt.push_back(t);
        }
    }

    int M = (int)zachwyt.size();
    for (int i = 0; i < n; i++) {
        if (a[i] <= a[i + 1]) {
            zachwyt.pop_front();
        }
        else {
            int temp = zachwyt.front();
            zachwyt.pop_front();
        
            stack<int> dummy;
            dummy.push(a[i + 1]);
            for (int t = i + 2; t < i + n + 2; t++) {
                if (temp < a[t] || t == i + n + 1) {
                    while (!dummy.empty()) {
                        zachwyt.push_front(dummy.top());
                        dummy.pop();
                    }
                    break;
                }
                else if (dummy.top() < a[t]) {
                    dummy.push(a[t]);
                }
            }

        }

        if (!zachwyt.size()) {
            zachwyt.push_back(a[i + n]);       
        }
        else if (zachwyt.back() < a[i + n]) {
            zachwyt.push_back(a[i + n]);
        }

        M = max((int)zachwyt.size(), M);

        //wypisz(zachwyt);
    }

    return M;
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    //inic();
    input();

    cout << solve() << endl;
    return 0;
}