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
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>

using namespace std;

class JubilerBajtazara {
    int n;
    vector<int> perly;

public:
    JubilerBajtazara(int n_input, const vector<int>& perly_input) {
        n = n_input;
        perly = perly_input;
    }

    int policz_maksymalne_ekstazy_bajtazara() {
        if (n == 0) return 0;

        int max_idx = 0;
        for (int i = 1; i < n; ++i) {
            if (perly[i] > perly[max_idx]) {
                max_idx = i;
            }
        }

        vector<int> b(n);
        for (int i = 0; i < n; ++i) {
            b[i] = perly[(max_idx + 1 + i) % n];
        }

        vector<int> dp(n, 1);
        stack<int> st;
        int max_zachwytow = 1;

        for (int i = n - 1; i >= 0; --i) {
            while (!st.empty() && b[st.top()] <= b[i]) {
                st.pop();
            }

            if (!st.empty()) {
                dp[i] = 1 + dp[st.top()];
            } else {
                dp[i] = 1;
            }

            max_zachwytow = max(max_zachwytow, dp[i]);
            st.push(i);
        }

        return max_zachwytow;
    }
};

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

    int n;
    if (cin >> n) {
        vector<int> perly(n);
        for (int i = 0; i < n; ++i) {
            cin >> perly[i];
        }

        JubilerBajtazara jubiler(n, perly);
        cout << jubiler.policz_maksymalne_ekstazy_bajtazara() << "\n";
    }
    return 0;
}