This page is still under construction.

Parts of this page are still being built. What you see may change.

Darts

Interview

Time limit1sMemory limit256 MB

Summary
With up to four darts and N region scores, find the largest total not exceeding M, or 0 if every reachable total is over M.
Level

Medium6 of 10

Topics
Binary search, Sorting, Brute force, Two pointers
Solved
No attempts yet

Problem

You play a darts game with the following rules.

  • You may throw up to 4 arrows at the target. You do not have to throw all 4, and you may throw none at all.
  • The target is divided into NN regions, and region ii is labeled with a score PiP_i. Several arrows may stick in the same region, and each one adds that region's score again.
  • Let SS be the total score of the regions where your arrows stick. This is the basis of your points.
  • For a predetermined value MM: if S≤MS \le M, your score is exactly SS. However, if SS exceeds MM, your score becomes 00.

Given the scores written on the target and the value of MM, write a program that finds the maximum score you can obtain.

Input

Read the following data from standard input.

  • The first line contains two integers NN and MM separated by a space. The target is divided into NN regions, and the predetermined value is MM.
  • Each of the next NN lines contains one integer; the ii-th of them (1≤i≤N1 \le i \le N) is PiP_i, the score written on the ii-th region of the target.

Output

Print the maximum score you can obtain on a single line.

Constraints

  • 1≤N≤10001 \le N \le 1000
  • 1≤M≤2×1081 \le M \le 2 \times 10^8
  • 1≤Pi≤1081 \le P_i \le 10^8

Examples2

  1. Example 1

    Input
    4 50
    3
    14
    15
    9
    
    Expected output
    48
    
  2. Example 2

    Input
    3 21
    16
    11
    2
    
    Expected output
    20