cswork / OAOpen practice workspace

JPMorgan ChaseOA #28

Maximum Amount

Estimated MediumJudge-ready

Topics (AI-labelled)Binary SearchGreedyStacks & Heaps

Each customer buys exactly one item; the price of a product for that sale equals its remaining stock before the sale, and the stock drops by one afterward. Given m customers, find the maximum revenue, without taking any modulo. The original does not guarantee enough stock, so this site specifies that the output is −1 when the total stock is <m. The explanation of original example 1 misstates the stock; with input [10,10,8,9,1] and m=6, the answer should be 55.

Input

The first line contains n m, followed by n values of quantity; full original bounds 1≤n≤100000, 1≤m≤1000000000, 1≤quantity[i]≤1000000000. m≤total stock is not required.

Output

Output a single integer answer.

Example 1

Input:

5 6
10 10 8 9 1

Output:

55

Example 2

Input:

3 4
1 2 4

Output:

11

Example 3

Input:

2 3
1 1

Output:

-1

Example notes

The outputs of the three public examples are, in order: 55; 11; -1. These answers have been checked by independent enumeration or direct simulation.

Editorial

Loading solution…