From $N$ cards, choose exactly three whose values sum to as large a total as possible without exceeding $M$. A valid triple is guaranteed to exist. Print that maximum sum.
Input
Line 1: $N$ and $M$ ($3 \le N \le 100$, $10 \le M \le 300000$). Line 2: $N$ positive integers, each $\le 100000$.
Output
Print the largest achievable three-card sum not exceeding $M$.
Example 1
Input
6 31 4 7 9 11 13 15
Output
31
Example 2
Input
8 250 45 78 112 56 89 23 94 67
Output
250
Discussion
Threads about this problem. Spoilers are collapsed; wrap hints in
||double bars||.No discussion yet. Start a thread if something here needs explaining.