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

class MaxQueue {
  std::deque<std::pair<int, int>> q; // (t, x)

public:
  void add(int t, int x) {
    while (!q.empty() && q.back().second <= x)
      q.pop_back();
    q.push_back({t, x});

    /*std::cout << "After adding t=" << t << ", x= " << x << "\n";
    for (auto &[t, x] : q)
      std::cout << "(" << t << ", " << x << "), ";
    std::cout << "\n";*/
  }

  void pop(int t) {
    while (!q.empty() && q.front().first <= t)
      q.pop_front();
  }

  int size() { return q.size(); }
};

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

  int n;
  std::cin >> n;

  std::vector<int> v(2 * n, 0);
  for (int i = 0; i < n; i++)
    std::cin >> v[i];
  for (int i = n; i < 2 * n; i++)
    v[i] = v[i - n];

  std::reverse(v.begin(), v.end());

  MaxQueue q;
  for (int j = 0; j < n; j++)
    q.add(j, v[j]);

  int result = q.size();

  for (int i = n; i < 2 * n; i++) {
    q.pop(i - n);
    q.add(n, v[i]);
    result = std::max(result, q.size());
  }

  std::cout << result << "\n";
}