Taro's Shopping
Time limit2sMemory limit512 MB
Given item prices and a budget, find the largest sum of two distinct items that does not exceed the budget.
- Level
Medium4 of 10
- Topics
- Two pointers, Sorting, Array
- Solved
- No attempts yet
Problem
Mammy decided to give Taro his first shopping experience. She tells him to pick any two items he likes from the shopping catalogue, but Taro cannot settle on two, because every item looks attractive. So he plans to buy the pair of items whose price sum is as large as possible without going over the amount Mammy allows. Buying two copies of the same item is boring, so he picks two different items.
Help Taro choose the two items. The price list of all the items is given. Among all pairs of two items in the list, find the pair whose price sum is the largest one that does not exceed the allowed amount, and report that sum. Taro buys exactly two items, not one, and not three or more. Two or more items in the list may be priced equally.
Input
The input consists of several datasets, each in the following format.
n m
a1 a2 ... an
One dataset takes two lines. The first line holds the number of items and the maximum payment allowed . is an integer with 2 ≤ ≤ 1,000, and is an integer with 2 ≤ ≤ 2,000,000. The second line holds the prices of the items. (1 ≤ ≤ ) is the price of the -th item, an integer between 1 and 1,000,000 inclusive.
The end of the input is marked by a line containing two zeros. The sum of over all datasets does not exceed 50,000.
Output
For each dataset, print on one line the largest price sum of a pair of items that does not exceed the allowed amount . If the price sum of every pair exceeds , print NONE instead of that value.