Choosing Orders and Renting Machines

Time limit2sMemory limit128 MB

Summary
Given orders with income and per-machine rent costs plus fixed machine purchase prices, choose orders and buy/rent decisions to maximize profit, solvable as a max-flow min-cut project selection problem.
Level

Hard8 of 10

Topics
Graph, Greedy, Dynamic programming
Solved
No attempts yet

Problem

Carpenter Sam receives NN orders. To finish them she needs MM machines that she does not yet own. Not every order needs every machine, but each order needs at least one of them.

To complete an order, Sam must, for each machine that order requires, either buy that machine or rent it. The rent of a machine depends on the order it is used for, because different orders need different amounts of work on it. The purchase price of a machine does not depend on any order, and a machine that has been bought once can be used for any number of orders at no additional cost.

If an order would cost more than it earns, Sam may reject it; a rejected order brings neither income nor cost.

Order ii has income viv_i. Completing it requires a given set of machines, and for each required machine jj the rent is rijr_{ij}. Machine jj has purchase price sjs_j.

Decide which orders to complete, which machines to buy, and which machines to rent so that Sam's profit (the total income of the completed orders minus every purchase and rent cost) is as large as possible. Because rejecting every order gives a profit of 00, the answer is never negative.

Input

The first line contains two integers NN and MM (1≤N≤12001 \le N \le 1200, 1≤M≤12001 \le M \le 1200).

Then follow NN order blocks. The block for order ii begins with a line holding two integers: the income viv_i (1≤vi≤50001 \le v_i \le 5000) and the number of required machines mim_i (1≤mi≤M1 \le m_i \le M). Each of the next mim_i lines contains two integers jj and rijr_{ij} (1≤j≤M1 \le j \le M, 1≤rij≤200001 \le r_{ij} \le 20000): a machine required by order ii and the rent to use that machine for this order.

After the last order block come MM lines; the jj-th of them contains one integer sjs_j (1≤sj≤200001 \le s_j \le 20000), the purchase price of machine jj.

Output

Print one integer: the maximum achievable profit.

Note

In the first sample a maximum profit of 5050 can be reached in two different ways:

  • Reject order 22, complete order 11, and rent both machine 11 and machine 22.
  • Complete both orders, buy machine 11, and rent machine 22 and machine 33.

Either choice yields a profit of 5050.

Examples6

  1. Example 1

    Input
    2 3
    100 2
    1 30
    2 20
    100 2
    1 40
    3 80
    50
    80
    110
    
    Expected output
    50
    
  2. Example 2

    Input
    1 1
    10 1
    1 3
    100
    
    Expected output
    7
    
  3. Example 3

    Input
    1 1
    5 1
    1 10
    20
    
    Expected output
    0
    
  4. Example 4

    Input
    2 1
    100 1
    1 60
    100 1
    1 60
    50
    
    Expected output
    150
    
  5. Example 5

    Input
    1 1
    100 1
    1 30
    200
    
    Expected output
    70
    
  6. Example 6

    Input
    2 2
    50 2
    1 10
    2 10
    50 1
    1 40
    30
    100
    
    Expected output
    60