#t147. 优先购买
优先购买
Priority Purchase
Problem Description
Little A has a budget of yuan. The shop has products, each with three attributes: name , price , and priority , where is a positive integer and a smaller means a higher priority.
Little A's shopping strategy is:
- Always buy the highest-priority item first;
- If several items share the highest priority, buy the cheapest one;
- If several items share the highest priority and the lowest price, buy the one whose name is lexicographically smallest.
Little A wants to know which products he can buy.
Input Format
The first line contains two positive integers and , the budget and the number of products. The following lines each describe one product: , the name, price, and priority of the -th product. It is guaranteed that no two products share the same name.
Output Format
Output the names of all purchased products in lexicographically ascending order.
Sample Input
20 4
apple 6 8
bus 15 1
cab 1 10
water 4 8
Sample Output
bus
cab
water
Sample Explanation
For the sample, Little A has a budget of yuan and products. First, the highest-priority product is bus (priority ), priced yuan; after buying it, yuan remain. Next, among the remaining products the highest priority is , shared by apple (price ) and water (price ); by the rule the cheaper one is bought first, so water is bought, costing yuan and leaving yuan. Finally, among the remaining products the highest priority is cab (priority ), priced yuan; after buying it yuan remain. The purchase order is bus, water, cab; sorted lexicographically ascending as required, the result is bus, cab, water.
Constraints
For all test points, , , , . Product names consist only of lowercase letters, and no two are the same.