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

#QHD0000051. Lặn tìm vàng (Diving for Gold)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Lặn tìm vàng (Diving for Gold)

Diving for Gold

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

A diver has discovered nn treasures. Treasure ii is at depth did_i and contains viv_i units of gold. The diver can stay underwater for a total of at most tt seconds.

Retrieving treasure ii takes wdiw d_i seconds to descend and 2wdi2w d_i seconds to ascend, for a total cost of 3wdi3wd_i seconds. Each dive brings back at most one treasure and each treasure can be retrieved at most once.

Select a subset of treasures that maximizes the total gold with total diving time at most tt. Output both the maximum gold and the selected treasures in their original input order. Any optimal selection is accepted.

Input

The first line contains integers t,wt,w. The second line contains nn. Each of the following nn lines contains di,vid_i,v_i.

Output

Print the maximum total gold, followed by the number kk of selected treasures. Then print kk depth-and-gold pairs in the same relative order as in the input. Any optimal solution is accepted.

Subtasks

Scoring groups are independent. All constraints listed for a group apply simultaneously.

  • Subtask 1 (20%): 1≤t≤1501\le t\le 150, 1≤w≤41\le w\le 4, 1≤n≤81\le n\le 8, 1≤di≤201\le d_i\le 20, 1≤vi≤1001\le v_i\le 100.
  • Subtask 2 (30%): 1≤t≤5001\le t\le 500, 1≤w≤81\le w\le 8, 1≤n≤201\le n\le 20, 1≤di≤901\le d_i\le 90, 1≤vi≤10001\le v_i\le 1000.
  • Subtask 3 (50%): 1≤t≤10001\le t\le 1000, 1≤w≤151\le w\le 15, 1≤n≤301\le n\le 30, 1≤di≤3001\le d_i\le 300, 1≤vi≤100001\le v_i\le 10000.

Examples

Input

210 4
3
10 5
10 1
7 2

Output

7
2
10 5
7 2

Explanation

Each depth-1010 treasure takes 120120 seconds and the depth-77 treasure takes 8484 seconds. Selecting treasures 1 and 3 takes 204≤210204\le210 seconds and yields 5+2=75+2=7 gold, which is optimal.