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

#QHD0000048. Siêu giảm giá (SuperSale)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Siêu giảm giá (SuperSale)

SuperSale

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

A shop offers nn types of products, where product ii has value pip_i and weight wiw_i. There are gg family members, and member jj can carry at most cjc_j weight. Each member makes an independent selection and may take each product at most once within their own selection. Different members may select the same product. Maximize the sum of the optimal values across all family members.

Input

The first line contains nn, followed by nn lines with pi,wip_i,w_i. Next comes gg, followed by gg lines each containing a capacity cjc_j.

Output

Print the maximum combined total value.

Subtasks

The independent scoring groups have the following complete constraints:

  • Subtask 1 (20%): 1≤n≤121\le n\le12, 1≤pi≤1001\le p_i\le100, 1≤wi≤301\le w_i\le30, 1≤g≤41\le g\le4, 1≤cj≤301\le c_j\le30.
  • Subtask 2 (30%): 1≤n≤1001\le n\le100, 1≤pi≤5001\le p_i\le500, 1≤wi≤301\le w_i\le30, 1≤g≤301\le g\le30, 1≤cj≤301\le c_j\le30.
  • Subtask 3 (50%): 1≤n≤10001\le n\le1000, 1≤pi≤10001\le p_i\le1000, 1≤wi≤301\le w_i\le30, 1≤g≤1001\le g\le100, 1≤cj≤301\le c_j\le30.

Examples

Input

3
72 17
44 23
31 24
2
26
30

Output

144

Explanation

Both people can achieve maximum value 7272 by taking the first item of weight 1717. Every pair is too heavy, so the combined maximum is 144144.