PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000195. Giảm khối lượng công việc (Work Reduction)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Giảm khối lượng công việc (Work Reduction)

Work Reduction

Source: UVa

Version: Phuoc Hung OJ (single-case adaptation where applicable)

Problem Statement

You have NN units of work and must reduce them to exactly MM, 1≤M≤N1\le M\le N. Each of LL agencies has a record NAME:A,B: it charges AA to decrease the workload by one, or BB to reduce the current workload to its floor half. Going below MM is not allowed. Compute the minimum total cost for each agency, then list agencies by increasing cost, breaking ties by lexicographic name.

Input

Line 1 contains N,M,LN,M,L. Each of the next LL lines is NAME:A,B, where NAME is 1–16 distinct uppercase Latin letters, unique among agencies, and A,BA,B are nonnegative integers. This PHOJ version has one case.

Output

Print exactly LL lines, each containing the agency name, a space, and its optimal cost. Do not print the original multi-case Case i prefix.

Subtasks

  • Subtask 1 (20 points): 1≤M≤N≤1001\le M\le N\le 100; 1≤L≤101\le L\le 10; 0≤A,B≤1000\le A,B\le 100; all names are unique uppercase strings of length 1–16.
  • Subtask 2 (30 points): 1≤M≤N≤100001\le M\le N\le 10000; 1≤L≤301\le L\le 30; 0≤A,B≤100000\le A,B\le 10000; all names are unique uppercase strings of length 1–16.
  • Subtask 3 (50 points): 1≤M≤N≤1000001\le M\le N\le 100000; 1≤L≤1001\le L\le 100; 0≤A,B≤100000\le A,B\le 10000; all names are unique uppercase strings of length 1–16.

Examples

Example 1

Input

100 5 3
A:1,10
B:2,5
C:3,1

Output

C 7
B 22
A 37

Explanation

Agency C can use inexpensive halvings, costing 7 in total. B costs 22 and A costs 37. They are listed as C, B, A.

Example 2

Input

1123 1122 3
B:50,300
A:1,1000
E:0,0

Output

E 0
A 1
B 50

Explanation

Only one unit needs to be removed. E charges 0, A charges 1, and B charges 50, so this is the required order.