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

#QHD0000086. Dịch vụ điện toán đám mây (Cloud Computing)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Dịch vụ điện toán đám mây (Cloud Computing)

Cloud Computing

Source: CEOI

Version: Phuoc Hung OJ Extended

Problem Statement

Johnny is founding Bytecomp, a cloud computing company. A shop has nn distinct computers. Computer ii has cic_i independent processor cores, each with clock rate fif_i, and costs viv_i to buy. Cores of a computer may serve different customers, but a computer must be bought as a whole, not core by core.

There are mm distinct customer orders. Order jj requires exactly CjC_j cores, each with clock rate at least FjF_j, and pays VjV_j if accepted. The cores assigned to an order may come from different computers, but each physical core can be assigned to at most one order. An order is either accepted in full or rejected: partial service earns no money.

Choose a subset of computers to buy and a subset of orders to accept so that every accepted order receives enough qualifying cores. Maximize the total revenue minus the total purchase cost. Buying no computers and accepting no orders is allowed and gives profit 00. A clock rate equal to the required threshold is sufficient; spare cores may remain unused. The answer may not fit in 32-bit integers.

Input

The first line contains nn. Each of the next nn lines contains ci,fi,vic_i,f_i,v_i. The next line contains mm. Each of the final mm lines contains Cj,Fj,VjC_j,F_j,V_j.

Output

Print one 64-bit integer: the maximum achievable profit.

Subtasks

  • Subtask 1 (20 points): 1≤n,m≤51\le n,m\le5, 1≤ci,Cj≤51\le c_i,C_j\le5, 1≤fi,Fj≤151\le f_i,F_j\le15, 1≤vi,Vj≤301\le v_i,V_j\le30.

  • Subtask 2 (30 points): 1≤n,m≤1001\le n,m\le100, 1≤ci,Cj≤101\le c_i,C_j\le10, 1≤fi,Fj≤10001\le f_i,F_j\le1000, 1≤vi,Vj≤1051\le v_i,V_j\le10^5.

  • Subtask 3 (50 points): 1≤n,m≤20001\le n,m\le2000, 1≤ci,Cj≤501\le c_i,C_j\le50, 1≤fi,Fj,vi,Vj≤1091\le f_i,F_j,v_i,V_j\le10^9.

Examples

Example 1

Input

4
4 2200 700
2 1800 10
20 2550 9999
4 2000 750
3
1 1500 300
6 1900 1500
3 2400 4550

Output

350

Explanation: Buy the two four-core computers priced 700 and 750, then fulfill the first two orders for revenue 1800, producing profit 350.

Example 2

Input

2
2 10 8
1 5 3
2
2 10 15
1 5 4

Output

8

Explanation: Buying both machines and fulfilling both orders yields revenue 19 and cost 11, so the optimal profit is 8.