Darts
InterviewTime limit1sMemory limit256 MB
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 regions, and region is labeled with a score . Several arrows may stick in the same region, and each one adds that region's score again.
- Let be the total score of the regions where your arrows stick. This is the basis of your points.
- For a predetermined value : if , your score is exactly . However, if exceeds , your score becomes .
Given the scores written on the target and the value of , 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 and separated by a space. The target is divided into regions, and the predetermined value is .
- Each of the next lines contains one integer; the -th of them () is , the score written on the -th region of the target.
Output
Print the maximum score you can obtain on a single line.