#ABC262G. [ABC262G] LIS with Stack

[ABC262G] LIS with Stack

Score : 600600 points

Problem Statement

There is an empty sequence XX and an empty stack SS. Also, you are given an integer sequence A=(a1,,aN)A=(a_1,\ldots,a_N) of length NN. For each i=1,,Ni=1,\ldots,N in this order, Takahashi will do one of the following operations:

  • Move the integer aia_i onto the top of SS.
  • Discard the integer aia_i from AA.

Additionally, Takahashi may do the following operation whenever SS is not empty:

  • Move the integer at the top of SS to the tail of XX.

The score of the final XX is defined as follows.

  • If XX is non-decreasing, i.e. if xixi+1x_i \leq x_{i+1} holds for all integer i(1i<X)i(1 \leq i \lt |X|), where X=(x1,,xX)X=(x_1,\ldots,x_{|X|}), then the score is X|X| (where X|X| denotes the number of terms in XX).
  • If XX is not non-decreasing, then the score is 00.

Find the maximum possible score.

Constraints

  • 1N501 \leq N \leq 50
  • 1ai501 \leq a_i \leq 50
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

a1a_1 \ldots aNa_N

Output

Print the answer.

7
1 2 3 4 1 2 3
5

The following operations make the final XX equal (1,1,2,3,4)(1,1,2,3,4), for a score of 55.

  • Move a1=1a_1=1 onto the top of SS.
  • Move 11 at the top of SS to the tail of XX.
  • Move a2=2a_2=2 onto the top of SS.
  • Discard a3=3a_3=3.
  • Move a4=4a_4=4 onto the top of SS.
  • Move a5=1a_5=1 onto the top of SS.
  • Move 11 at the top of SS to the tail of XX.
  • Move a6=2a_6=2 onto the top of SS.
  • Move 22 at the top of SS to the tail of XX.
  • Move a7=3a_7=3 onto the top of SS.
  • Move 33 at the top of SS to the tail of XX.
  • Move 44 at the top of SS to the tail of XX.

We cannot make the score 66 or greater, so the maximum possible score is 55.

10
1 1 1 1 1 1 1 1 1 1
10