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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll n;
    cin >> n;
    vector<ll> numbers(n);
    for (int i = 0; i < n; i++)
    {
        cin >> numbers[i];
    }

    ll max_num = 0;
    ll second_max = 0;
    for (int i = 0; i < n; i++)
    {
        if (numbers[i] > max_num)
        {
            max_num = numbers[i];
        }
        else
        {
            second_max = max(second_max, numbers[i]);
        }
    }
    vector<ll> obscured;
    vector<ll> count_obscure(n);
    for (int i = n - 1; i >= 0; i--)
    {
        for (int j = obscured.size() - 1; j >= 0; j--)
        {
            if (obscured[j] <= numbers[i])
            {
                obscured.pop_back();
                count_obscure[i]++;
            }
            else
            {
                break;
            }
        }
        obscured.push_back(numbers[i]);
    }
    for (int i = n - 1; i >= 1; i--)
    {
        for (int j = obscured.size() - 1; j >= 0; j--)
        {
            if (obscured[j] == max_num && max_num != second_max)
            {
                break;
            }
            if (obscured[j] <= numbers[i])
            {
                obscured.pop_back();
                count_obscure[i]++;
            }
            else
            {
                break;
            }
        }
    }

    ll score = 1;
    ll best_score = 0;
    ll largest = numbers[0];
    for (int i = 1; i < n; i++)
    {
        if (numbers[i] > largest)
        {
            score++;
            largest = numbers[i];
        }
    }
    best_score = score;

    for (int i = 0; i < n - 1; i++)
    {
        score--;
        if (numbers[i] == max_num && max_num != second_max)
        {
            score++;
        }
        score += count_obscure[i];
        best_score = max(best_score, score);
    }
    cout << best_score << endl;
}