Under construction

Still being built. Things may change

Scales

Interview

Time limit1sMemory limit1024 MB

Summary
Given N weights where each one is at least the sum of the two before it, find the largest subset sum that does not exceed the limit C.
Level

Medium5 of 10

Topics
Greedy, Backtracking
Solved
No attempts yet

Problem

Farmer John has a balance for weighing cows. He also has a set of NN weights (1≤N≤10001 \le N \le 1000) with known masses. Every mass fits in 31 bits. To weigh a cow, he places the cow on one side of the balance and adds weights to the other side until the two sides balance. Weights cannot go on the same side as the cow, because cows kick weights in his face whenever they can.

The balance has a maximum mass rating. If the total mass on one side exceeds CC (1≤C<2301 \le C < 2^{30}), the balance breaks.

The weights have a special property. When sorted from smallest to largest, each weight from the third one onward has a mass at least as large as the sum of the previous two weights.

Find the largest mass that can be measured exactly with these weights. The total mass used must not exceed CC, so he may not be able to place every weight on the scale. Given the list of weights and the maximum mass CC, output the largest legal mass that can be weighed exactly.

Input

The first line contains two positive integers NN and CC, separated by a space.

Each of the next NN lines contains one positive integer, the mass of one weight. The masses are given in non-decreasing order.

Output

Print one integer on the first line: the largest mass that can be measured accurately and safely.

Examples1

  1. Example 1

    Input
    3 15
    1
    10
    20
    
    Expected output
    11