Reseller's Eye

Time limit2sMemory limit128 MB

Summary
Given a budget and multiple sellers each offering a fixed bundle of items (buy all or none), choose a subset of sellers within budget to maximize resale profit, a bundled knapsack problem.
Level

Medium6 of 10

Topics
Dynamic programming, Greedy, Array
Solved
No attempts yet

Problem

Every morning, Minhyuk the reseller browses all sorts of fan-hobby sites to check the going prices of goods. While researching prices as usual, he felt the phone in his hand getting hot but paid it no mind, and before long the phone exploded!

When he came to, Minhyuk had gained a superpower: whenever he sees an item posted on the Peaceful Secondhand Land marketplace, he can tell the price at which he could resell that item tomorrow. Delighted, he named this power the Reseller's Eye.

On the marketplace's 'quitting the hobby' board, people who have given up their hobby sell their goods; they part with their beloved collectibles all at once to sever their ties. In other words, to buy from a person, you must buy all of that person's collectibles.

Now a true reseller, Minhyuk wants to buy collectibles from these people and resell them at a high price tomorrow. Buying without exceeding his capital, from which people should he buy to maximize the profit he makes tomorrow?

Input

The first line contains CC, the capital the reseller has today. (0<C≤2300 < C \le 2^{30})

The second line contains two integers NN and PP: NN is the number of kinds of collectibles, and PP is the number of people selling goods. (0<N≤5000 < N \le 500, 0<P≤500000 < P \le 50000)

Each of the next NN lines contains two integers aia_i and tit_i: aia_i is the current price of the ii-th collectible and tit_i is its tomorrow price revealed by the Reseller's Eye. (1≤i≤N1 \le i \le N; this ii is called the item number.)

Finally, PP lines describe the collectibles each person sells. Each line begins with RR, the number of kinds of collectibles that person has, followed by the item number sjs_j and the count qjq_j of each collectible that person sells. (1≤j≤R1 \le j \le R)

Output

Print the maximum profit the reseller can make tomorrow.

Examples2

  1. Example 1

    Input
    500
    4 6
    10 15
    8 6
    20 15
    12 12
    3 1 6 2 7 3 8
    3 3 8 1 10 2 4
    3 4 10 2 5 1 10
    2 1 4 2 4
    1 3 2
    2 4 3 2 1
    
    Expected output
    52
    
  2. Example 2

    Input
    200000000
    5 30
    2800 3500
    1400 4800
    2900 2800
    500 3800
    3300 4700
    2 2 13 4 15
    4 4 1 1 22 3 17 5 22
    1 3 2
    1 3 6
    4 1 11 2 5 3 7 5 15
    1 5 1
    4 2 26 1 21 3 8 5 26
    2 3 5 2 26
    4 2 30 4 12 3 7 5 14
    3 3 8 2 20 5 3
    1 5 30
    2 1 29 3 3
    5 3 3 1 20 5 26 4 9 2 25
    3 1 2 2 16 3 5
    2 5 5 4 26
    5 2 18 5 10 4 18 1 12 3 30
    3 2 5 3 27 5 4
    4 3 2 4 8 1 20 2 6
    3 2 14 1 1 4 22
    5 2 23 3 26 1 27 5 3 4 6
    1 2 16
    4 1 13 4 10 2 23 5 2
    1 1 14
    1 2 20
    1 3 14
    2 3 21 1 22
    1 2 27
    3 5 24 1 26 3 13
    5 4 15 3 3 2 21 1 5 5 16
    4 2 22 5 1 4 10 1 30
    
    Expected output
    2168800