#P2637. Hay Auction Revenue Maximization

    ID: 15904 Type: Default 1000ms 256MiB

Hay Auction Revenue Maximization

Hay Auction Revenue Maximization

Farmer John (FJ) is holding an auction to sell his hay because his "dieting cows" have left him with an excess of hay batches. FJ has \( n \) batches of hay, and there are \( m \) farmer customers. The \( i\)-th farmer is willing to pay \( p_i \) for each batch of hay. Each farmer wants to buy exactly one batch.

To avoid envy among buyers, FJ will set a single fixed price \( x \) for each batch. Every farmer whose bid \( p_i \) satisfies \( p_i \ge x \) will buy one batch. However, since FJ only has \( n \) batches, he will sell \( \min(n, \text{number of farmers with } p_i \ge x) \) batches.

Your task is to help FJ choose the fixed price \( x \) that maximizes his total revenue, where revenue is defined as \[ R = x \times \min(n, \text{number of bids } \ge x)\] If there are multiple prices achieving the maximum revenue, output the lowest such price.

inputFormat

The input consists of two lines. The first line contains two integers \( n \) and \( m \) (the number of hay batches and the number of farmers, respectively). The second line contains \( m \) space-separated integers \( p_1, p_2, \ldots, p_m \), where \( p_i \) is the amount the \( i\)-th farmer is willing to pay for one batch.

outputFormat

Output a single integer, the fixed price \( x \) that maximizes FJ's revenue. If there are multiple optimal prices, output the lowest one.

sample

2 3
3 7 5
5