#t147. 优先购买

优先购买

Priority Purchase

Problem Description

Little A has a budget of MM yuan. The shop has NN products, each with three attributes: name SS, price PP, and priority VV, where VV is a positive integer and a smaller VV 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 MM and NN, the budget and the number of products. The following NN lines each describe one product: Si Pi ViS_i\ P_i\ V_i, the name, price, and priority of the ii-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 2020 yuan and 44 products. First, the highest-priority product is bus (priority 11), priced 1515 yuan; after buying it, 55 yuan remain. Next, among the remaining products the highest priority is 88, shared by apple (price 66) and water (price 44); by the rule the cheaper one is bought first, so water is bought, costing 44 yuan and leaving 11 yuan. Finally, among the remaining products the highest priority is cab (priority 1010), priced 11 yuan; after buying it 00 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, 1≤∣Si∣≤101 \le |S_i| \le 10, 1≤M,Pi≤1051 \le M, P_i \le 10^5, 1≤N≤1031 \le N \le 10^3, 1≤Vi≤101 \le V_i \le 10. Product names consist only of lowercase letters, and no two are the same.