A high-altitude aerial tramway ferries hikers up a ridge one cabin trip at a time, and hikers must line up and board in strict ticket order. Each hiker carries gear of a known weight, and the cabin has a fixed maximum combined load. Boarding proceeds strictly in ticket order: a hiker steps aboard only if the running total weight of every hiker who has boarded so far (including this one) stays within the cabin's load limit. The instant a hiker's turn would push that running total over the limit, boarding stops for good, even if a later hiker further back in line is light enough to have fit on their own. Given the full ticket-order queue, determine the name of the last hiker who successfully boards.
Line 1: two integers n and limit — the number of hikers waiting in line and the cabin's maximum combined load.
Each of the next n lines contains one hiker's name (a token made only of letters and digits) followed by an integer weight, listed in ticket order.
Print the name of the last hiker who boards. If even the very first hiker in line already exceeds the limit alone, print NONE instead.
Example 1
Input
5 100 Ravi 30 Sana 40 Emil 10 Noor 25 Kofi 5
Expected
Emil
Explanation
Running totals in ticket order are Ravi=30, Sana=70, Emil=80, Noor=105, Kofi=110. Noor's turn pushes the total past the limit of 100, so boarding stops there; the last hiker who made it aboard is Emil (cumulative 80).
Example 2
Input
3 1000 Ravi 200 Sana 300 Emil 400
Expected
Emil
Explanation
Running totals are Ravi=200, Sana=500, Emil=900, all within the limit of 1000, so every hiker boards. The last hiker in the queue, Emil, is the last one to board.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →