#K64017. Balanced Tower

    ID: 31882 Type: Default 1000ms 256MiB

Balanced Tower

Balanced Tower

You are given N blocks, each with a certain height. Your task is to build a balanced tower using some or all of the blocks.

A balanced tower is defined as follows:

  • The tower consists of two sides of equal height (i.e. the number of blocks on the left side is equal to the number of blocks on the right side).
  • The left side of the tower must have the block heights in non-decreasing order from bottom to top.

You are to determine the maximum possible height of the tower, where the height is defined as the number of blocks on one side. In other words, if the maximum balanced tower you can build has h blocks on the left side and h blocks on the right side, then the output should be h.

Note that if N blocks are given, the trivial upper bound is \( \left\lfloor \frac{N}{2} \right\rfloor \).

inputFormat

The input is given via standard input (stdin) and consists of two lines:

  1. The first line contains a single non-negative integer N which represents the number of blocks.
  2. If N > 0, the second line contains N space-separated integers, where the ith integer represents the height of the ith block.

outputFormat

Print a single integer representing the maximum possible height of the balanced tower (i.e., the maximum number of blocks on one side of the tower) to standard output (stdout).

## sample
5
3 1 2 2 4
2

</p>