Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 233 点
問題文
高橋君は初めての車を購入するため、中古車販売店を訪れました。販売店には N 台の中古車があり、それぞれについて「価格」と「走行距離」が分かっています。i 番目の車の価格は P_i 万円、走行距離は K_i km です。
高橋君は予算の都合上、価格が高すぎる車は購入できません。一方で、安すぎる車は品質に不安があるため避けたいとも考えています。さらに、走行距離が長すぎる車は故障のリスクが高いため避けたいと考えています。
具体的には、高橋君は以下の条件をすべて満たす車を「候補車」と呼ぶことにします:
- 価格が L 万円以上 R 万円以下である(すなわち L \leq P_i \leq R)
- 走行距離が M km 以下である(すなわち K_i \leq M)
N 台の中古車のうち「候補車」に該当するものの台数を求めてください。各車は 1 台ずつ区別して数えるものとします。
制約
- 1 \leq N \leq 10^5
- 1 \leq L \leq R \leq 10^4
- 1 \leq M \leq 10^6
- 1 \leq P_i \leq 10^4 (1 \leq i \leq N)
- 1 \leq K_i \leq 10^6 (1 \leq i \leq N)
- 入力はすべて整数である
入力
N L R M P_1 K_1 P_2 K_2 \vdots P_N K_N
- 1 行目には、中古車の台数 N、価格の下限 L、価格の上限 R、走行距離の上限 M がスペース区切りで与えられる。
- 続く N 行のうち i 行目 (1 \leq i \leq N) には、i 番目の車の価格 P_i と走行距離 K_i がスペース区切りで与えられる。
出力
「候補車」の台数を 1 行で出力せよ。
入力例 1
5 100 200 50000 150 30000 250 20000 80 10000 200 50000 180 60000
出力例 1
2
入力例 2
4 150 300 40000 100 30000 500 20000 200 80000 50 50000
出力例 2
0
入力例 3
10 50 150 100000 120 80000 50 100000 200 50000 30 90000 150 100001 75 60000 100 120000 90 99999 160 30000 45 100000
出力例 3
4
入力例 4
20 200 500 80000 300 50000 150 40000 450 80000 500 80001 200 79999 600 30000 250 90000 350 70000 199 60000 501 50000 400 80000 210 1 180 10000 320 75000 275 100000 480 60000 200 80000 100 20000 500 50000 300 80000
出力例 4
11
入力例 5
1 1 1 1 1 1
出力例 5
1
Score : 233 pts
Problem Statement
Takahashi is visiting a used car dealership to purchase his first car. The dealership has N used cars, and for each car, the "price" and "mileage" are known. The i-th car has a price of P_i ten-thousand yen and a mileage of K_i km.
Due to budget constraints, Takahashi cannot purchase a car that is too expensive. On the other hand, he also wants to avoid cars that are too cheap, as he is concerned about their quality. Furthermore, he wants to avoid cars with too high a mileage due to the increased risk of breakdowns.
Specifically, Takahashi defines a car as a "candidate car" if it satisfies all of the following conditions:
- The price is at least L ten-thousand yen and at most R ten-thousand yen (i.e., L \leq P_i \leq R)
- The mileage is at most M km (i.e., K_i \leq M)
Among the N used cars, find the number of cars that qualify as "candidate cars." Each car is counted individually.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq L \leq R \leq 10^4
- 1 \leq M \leq 10^6
- 1 \leq P_i \leq 10^4 (1 \leq i \leq N)
- 1 \leq K_i \leq 10^6 (1 \leq i \leq N)
- All inputs are integers
Input
N L R M P_1 K_1 P_2 K_2 \vdots P_N K_N
- The first line contains the number of used cars N, the lower bound of price L, the upper bound of price R, and the upper bound of mileage M, separated by spaces.
- Each of the following N lines, where the i-th line (1 \leq i \leq N), contains the price P_i and mileage K_i of the i-th car, separated by spaces.
Output
Output the number of "candidate cars" in a single line.
Sample Input 1
5 100 200 50000 150 30000 250 20000 80 10000 200 50000 180 60000
Sample Output 1
2
Sample Input 2
4 150 300 40000 100 30000 500 20000 200 80000 50 50000
Sample Output 2
0
Sample Input 3
10 50 150 100000 120 80000 50 100000 200 50000 30 90000 150 100001 75 60000 100 120000 90 99999 160 30000 45 100000
Sample Output 3
4
Sample Input 4
20 200 500 80000 300 50000 150 40000 450 80000 500 80001 200 79999 600 30000 250 90000 350 70000 199 60000 501 50000 400 80000 210 1 180 10000 320 75000 275 100000 480 60000 200 80000 100 20000 500 50000 300 80000
Sample Output 4
11
Sample Input 5
1 1 1 1 1 1
Sample Output 5
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 333 点
問題文
高橋君はお菓子屋さんで働いています。店には N 個のお菓子が左から右へ一列に並んでおり、左から i 番目(1 \leq i \leq N)のお菓子には「おいしさ」 A_i が定められています。
高橋君はこれからちょうど M 個の詰め合わせセットを作ります。各セットは、一列に並んだお菓子の中から連続する区間を 1 つ選んで構成します。具体的には、整数の組 (l, r)(1 \leq l \leq r \leq N)を 1 つ選び、番号 l, l+1, \ldots, r のお菓子をひとまとめにします。ただし、各区間の長さ r - l + 1 は K 以下でなければなりません。
M 個のセットを作る際、選ぶ M 個の区間はすべて異なっていなければなりません。すなわち、どの 2 つのセットについても、対応する組 (l, r) が一致してはいけません。一方で、異なるセットの区間同士が重なること、すなわち同じお菓子が複数のセットに含まれることは許されます。
各セットの「満足度」は、そのセットに含まれるお菓子のおいしさの総和として定義されます。すなわち、区間 (l, r) を選んだセットの満足度は A_l + A_{l+1} + \cdots + A_r です。
ちょうど M 個のセットを作るすべての方法のうち、M 個のセットの満足度の合計値の最大値を求め、出力してください。
なお、長さ K 以下の区間 (l, r) の総数は M 以上であることが保証されるため、条件を満たす M 個の異なる区間を選ぶことは常に可能です。
制約
- 1 \leq N \leq 3000
- 1 \leq K \leq N
- 1 \leq M \leq \displaystyle\sum_{k=1}^{K} (N - k + 1)(右辺は長さ K 以下の区間の総数に等しい)
- -10^6 \leq A_i \leq 10^6(1 \leq i \leq N)
- 入力はすべて整数である。
入力
N M K A_1 A_2 \ldots A_N
- 1 行目には、お菓子の個数を表す整数 N、作るセットの個数を表す整数 M、1 つのセットに含められるお菓子の個数の上限を表す整数 K が、スペース区切りで与えられる。
- 2 行目には、各お菓子のおいしさを表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
出力
M 個のセットの満足度の合計値の最大値を 1 行で出力せよ。
入力例 1
5 3 2 3 -1 4 1 5
出力例 1
16
入力例 2
7 5 3 2 -3 5 -1 8 -2 4
出力例 2
43
入力例 3
10 4 4 1 -2 3 4 -5 6 7 -8 9 1
出力例 3
49
Score : 333 pts
Problem Statement
Takahashi works at a candy shop. The shop has N candies lined up in a row from left to right, and the i-th candy from the left (1 \leq i \leq N) has a "deliciousness" value of A_i.
Takahashi will now make exactly M assortment sets. Each set is constructed by choosing one contiguous interval from the row of candies. Specifically, he chooses a pair of integers (l, r) (1 \leq l \leq r \leq N) and bundles together the candies numbered l, l+1, \ldots, r. However, the length r - l + 1 of each interval must be at most K.
When making the M sets, all M chosen intervals must be distinct. That is, for any two sets, their corresponding pairs (l, r) must not be identical. On the other hand, intervals of different sets are allowed to overlap, meaning the same candy may be included in multiple sets.
The "satisfaction" of each set is defined as the sum of deliciousness values of the candies included in that set. That is, the satisfaction of a set with interval (l, r) is A_l + A_{l+1} + \cdots + A_r.
Among all ways to make exactly M sets, find and output the maximum possible total satisfaction of the M sets.
It is guaranteed that the total number of intervals (l, r) with length at most K is at least M, so it is always possible to choose M distinct intervals satisfying the conditions.
Constraints
- 1 \leq N \leq 3000
- 1 \leq K \leq N
- 1 \leq M \leq \displaystyle\sum_{k=1}^{K} (N - k + 1) (the right-hand side equals the total number of intervals with length at most K)
- -10^6 \leq A_i \leq 10^6 (1 \leq i \leq N)
- All input values are integers.
Input
N M K A_1 A_2 \ldots A_N
- The first line contains the integer N representing the number of candies, the integer M representing the number of sets to make, and the integer K representing the upper limit on the number of candies that can be included in one set, separated by spaces.
- The second line contains the integers A_1, A_2, \ldots, A_N representing the deliciousness of each candy, separated by spaces.
Output
Output the maximum possible total satisfaction of the M sets in a single line.
Sample Input 1
5 3 2 3 -1 4 1 5
Sample Output 1
16
Sample Input 2
7 5 3 2 -3 5 -1 8 -2 4
Sample Output 2
43
Sample Input 3
10 4 4 1 -2 3 4 -5 6 7 -8 9 1
Sample Output 3
49
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君はスーパーマーケットに買い物に来ています。棚には N 個の商品が一列に並んでおり、左から i 番目の商品の価格は A_i 円です。それぞれの商品は 1 個ずつしかありません。
高橋君は、これらの商品の中からいくつかを選んでカゴに入れようとしています。各商品について「選ぶ」か「選ばない」かを決め、選んだ商品の価格の合計がちょうど S 円になるようにしたいと考えています。
ただし、高橋君はできるだけ少ない個数の商品で合計をちょうど S 円にしたいと思っています。
商品の価格の合計をちょうど S 円にすることが可能かどうかを判定し、可能な場合は選ぶ商品の最小個数を出力してください。不可能な場合は -1 を出力してください。
制約
- 1 \leq N \leq 100
- 1 \leq S \leq 10000
- 1 \leq A_i \leq 10000 (1 \leq i \leq N)
- 入力はすべて整数である
入力
N S A_1 A_2 \ldots A_N
- 1 行目には、商品の個数を表す N と、目標の合計金額を表す S が、スペース区切りで与えられる。
- 2 行目には、各商品の価格 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
出力
選んだ商品の価格の合計をちょうど S 円にできる場合は、選ぶ商品の最小個数を 1 行で出力せよ。不可能な場合は -1 を出力せよ。
入力例 1
5 10 3 5 7 2 8
出力例 1
2
入力例 2
4 11 3 6 9 12
出力例 2
-1
入力例 3
15 100 12 50 25 7 33 18 50 44 9 61 15 73 28 36 42
出力例 3
2
Score : 366 pts
Problem Statement
Takahashi is shopping at a supermarket. There are N items lined up in a row on the shelf, and the price of the i-th item from the left is A_i yen. There is only one of each item.
Takahashi wants to select some of these items and put them in his basket. For each item, he decides whether to "select" it or "not select" it, and he wants the total price of the selected items to be exactly S yen.
However, Takahashi wants to achieve a total of exactly S yen with as few items as possible.
Determine whether it is possible to make the total price of the selected items exactly S yen. If it is possible, output the minimum number of items to select. If it is impossible, output -1.
Constraints
- 1 \leq N \leq 100
- 1 \leq S \leq 10000
- 1 \leq A_i \leq 10000 (1 \leq i \leq N)
- All inputs are integers
Input
N S A_1 A_2 \ldots A_N
- The first line contains N, representing the number of items, and S, representing the target total price, separated by a space.
- The second line contains the prices of each item A_1, A_2, \ldots, A_N, separated by spaces.
Output
If it is possible to make the total price of the selected items exactly S yen, output the minimum number of items to select on a single line. If it is impossible, output -1.
Sample Input 1
5 10 3 5 7 2 8
Sample Output 1
2
Sample Input 2
4 11 3 6 9 12
Sample Output 2
-1
Sample Input 3
15 100 12 50 25 7 33 18 50 44 9 61 15 73 28 36 42
Sample Output 3
2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
空に N 個のもこもこした雲が浮かんでおり、 1 から N までの番号が付けられています。雲の間には M 本の風の通り道があり、それぞれ双方向に移動することができます。
各雲 i にはもこもこ度 F_i( 1 以上 K 以下の整数)が定められています。雲のもこもこ度は固定値であり、ゲームを通じて変化しません。
風の通り道 j は雲 U_j と雲 V_j を結び、基本移動時間 T_j を持ちます。
高橋君は雲 1 からスタートし、風の通り道をたどって雲 N に到達することを目指します。同じ雲や同じ風の通り道を複数回通ることもできます。
高橋君には「現在のもこもこ度」という状態があり、初期値は雲 1 のもこもこ度 F_1 です。高橋君が現在のもこもこ度 f の状態で風の通り道 j を通るとき、かかる時間は T_j \times f です。もこもこであるほど風の抵抗を受け、移動に時間がかかるのです。
高橋君が風の通り道を通ってある雲 i に到着するたびに、到着した雲のもこもこ度 F_i を「現在のもこもこ度」として採用するか、何もせず現在のもこもこ度を維持するかを選ぶことができます。この選択は、同じ雲に複数回到着した場合も毎回行えます。選択後の「現在のもこもこ度」が、次に風の通り道を通る際のコスト計算に使われます。
高橋君が雲 1 から雲 N に到達するまでにかかる合計時間の最小値を求めてください。雲 N に到達できない場合は -1 を出力してください。
制約
- 2 \leq N \leq 100000
- 0 \leq M \leq 100000
- 1 \leq K \leq 5
- 1 \leq F_i \leq K (1 \leq i \leq N)
- 1 \leq U_j, V_j \leq N (1 \leq j \leq M)
- U_j \neq V_j (1 \leq j \leq M)
- 同じ雲の組を結ぶ風の通り道が複数存在することがある
- 1 \leq T_j \leq 10^9 (1 \leq j \leq M)
- 入力はすべて整数である
- 答えは 10^{18} 以下になることが保証される(到達可能な場合)
入力
N M K F_1 F_2 \ldots F_N U_1 V_1 T_1 U_2 V_2 T_2 \vdots U_M V_M T_M
- 1 行目には、雲の数 N 、風の通り道の数 M 、もこもこ度の最大値 K が、スペース区切りで与えられる。
- 2 行目には、各雲のもこもこ度 F_1, F_2, \ldots, F_N が、スペース区切りで与えられる。
- 3 行目から M 行にわたり、風の通り道の情報が与えられる。 2 + j 行目には、 j 番目の風の通り道が結ぶ雲の番号 U_j , V_j と、基本移動時間 T_j が、スペース区切りで与えられる。
出力
高橋君が雲 1 から雲 N まで移動するのにかかる合計時間の最小値を 1 行で出力せよ。雲 N に到達できない場合は -1 を出力せよ。
入力例 1
4 4 3 2 1 3 1 1 2 5 2 4 4 1 3 2 3 4 1
出力例 1
6
入力例 2
5 3 3 1 2 3 1 2 1 2 10 2 3 5 4 5 7
出力例 2
-1
入力例 3
8 12 5 4 2 5 1 3 2 1 4 1 2 3 1 3 2 2 4 6 3 4 1 4 5 7 5 8 2 4 6 3 6 7 4 7 8 5 2 6 10 3 5 4 1 2 8
出力例 3
19
入力例 4
20 30 5 5 4 3 2 1 5 1 2 3 4 5 1 2 3 4 5 1 2 3 1 1 2 100 1 3 7 2 4 6 3 4 2 4 5 10 5 6 3 6 20 50 3 7 8 7 8 1 8 9 1 9 10 1 10 20 20 5 11 4 11 12 4 12 13 4 13 14 4 14 20 4 2 15 30 15 16 2 16 17 2 17 18 2 18 19 2 19 20 2 7 12 9 8 13 9 9 14 9 10 15 9 11 16 9 4 18 25 1 20 1000
出力例 4
74
入力例 5
2 1 1 1 1 1 2 1000000000
出力例 5
1000000000
Score : 400 pts
Problem Statement
There are N fluffy clouds floating in the sky, numbered from 1 to N. There are M wind paths between the clouds, each of which can be traversed in both directions.
Each cloud i has a fluffiness level F_i (an integer between 1 and K, inclusive). The fluffiness level of each cloud is a fixed value and does not change throughout the game.
Wind path j connects cloud U_j and cloud V_j, and has a base travel time T_j.
Takahashi starts at cloud 1 and aims to reach cloud N by following wind paths. He may pass through the same cloud or the same wind path multiple times.
Takahashi has a state called "current fluffiness level," whose initial value is F_1, the fluffiness level of cloud 1. When Takahashi traverses wind path j with a current fluffiness level of f, the time it takes is T_j \times f. The fluffier he is, the more wind resistance he encounters, and the longer the travel takes.
Each time Takahashi arrives at a cloud i via a wind path, he can choose either to adopt the fluffiness level F_i of the arrived cloud as his "current fluffiness level," or to do nothing and keep his current fluffiness level. This choice can be made each time he arrives at the same cloud. The "current fluffiness level" after this choice is used for the cost calculation when traversing the next wind path.
Find the minimum total time for Takahashi to travel from cloud 1 to cloud N. If cloud N cannot be reached, output -1.
Constraints
- 2 \leq N \leq 100000
- 0 \leq M \leq 100000
- 1 \leq K \leq 5
- 1 \leq F_i \leq K (1 \leq i \leq N)
- 1 \leq U_j, V_j \leq N (1 \leq j \leq M)
- U_j \neq V_j (1 \leq j \leq M)
- There may be multiple wind paths connecting the same pair of clouds
- 1 \leq T_j \leq 10^9 (1 \leq j \leq M)
- All input values are integers
- It is guaranteed that the answer is at most 10^{18} (when reachable)
Input
N M K F_1 F_2 \ldots F_N U_1 V_1 T_1 U_2 V_2 T_2 \vdots U_M V_M T_M
- The first line contains the number of clouds N, the number of wind paths M, and the maximum fluffiness level K, separated by spaces.
- The second line contains the fluffiness levels F_1, F_2, \ldots, F_N of each cloud, separated by spaces.
- The following M lines give the information of the wind paths. The (2 + j)-th line contains the cloud numbers U_j, V_j connected by the j-th wind path, and the base travel time T_j, separated by spaces.
Output
Output in one line the minimum total time for Takahashi to travel from cloud 1 to cloud N. If cloud N cannot be reached, output -1.
Sample Input 1
4 4 3 2 1 3 1 1 2 5 2 4 4 1 3 2 3 4 1
Sample Output 1
6
Sample Input 2
5 3 3 1 2 3 1 2 1 2 10 2 3 5 4 5 7
Sample Output 2
-1
Sample Input 3
8 12 5 4 2 5 1 3 2 1 4 1 2 3 1 3 2 2 4 6 3 4 1 4 5 7 5 8 2 4 6 3 6 7 4 7 8 5 2 6 10 3 5 4 1 2 8
Sample Output 3
19
Sample Input 4
20 30 5 5 4 3 2 1 5 1 2 3 4 5 1 2 3 4 5 1 2 3 1 1 2 100 1 3 7 2 4 6 3 4 2 4 5 10 5 6 3 6 20 50 3 7 8 7 8 1 8 9 1 9 10 1 10 20 20 5 11 4 11 12 4 12 13 4 13 14 4 14 20 4 2 15 30 15 16 2 16 17 2 17 18 2 18 19 2 19 20 2 7 12 9 8 13 9 9 14 9 10 15 9 11 16 9 4 18 25 1 20 1000
Sample Output 4
74
Sample Input 5
2 1 1 1 1 1 2 1000000000
Sample Output 5
1000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 466 点
問題文
高橋君はクリスマスパーティーの幹事をしています。パーティーには N 人の参加者がおり、高橋君は全員にプレゼントを配ることにしました。
高橋君は M 種類のプレゼントを用意しました。i 番目の種類のプレゼントは B_i 個あります。すべての参加者にちょうど 1 個ずつプレゼントを渡す必要がありますが、各種類のプレゼントは用意した個数を超えて配ることはできません。プレゼントの総数(B_1 + B_2 + \cdots + B_M)は N 以上であることが保証されているため、全員に配ることは必ず可能です。
参加者にはそれぞれ欲しいものの希望があります。j 番目の参加者は K_j 種類のプレゼントを希望しており、そのいずれかを受け取ると満足します。希望するプレゼントの種類の番号は C_{j,1}, C_{j,2}, \ldots, C_{j,K_j} として与えられます。K_j = 0 の場合、その参加者はどのプレゼントを受け取っても満足しません。希望していない種類のプレゼントを渡すことも許されますが、その参加者は満足しません。
高橋君は、満足する参加者の人数を最大化するようにプレゼントを配りたいと考えています。満足する参加者の人数の最大値を求めてください。
制約
- 1 \leq N \leq 500
- 1 \leq M \leq 500
- 1 \leq B_i \leq N(1 \leq i \leq M)
- B_1 + B_2 + \cdots + B_M \geq N
- 0 \leq K_j \leq M(1 \leq j \leq N)
- 1 \leq C_{j,k} \leq M(1 \leq j \leq N, 1 \leq k \leq K_j)
- j 番目の参加者について、C_{j,1}, C_{j,2}, \ldots, C_{j,K_j} はすべて異なる
- 入力はすべて整数である
入力
N M
B_1 B_2 \cdots B_M
K_1 C_{1,1} C_{1,2} \cdots C_{1,K_1}
K_2 C_{2,1} C_{2,2} \cdots C_{2,K_2}
:
K_N C_{N,1} C_{N,2} \cdots C_{N,K_N}
- 1 行目には、参加者の人数を表す N と、プレゼントの種類数を表す M が、スペース区切りで与えられる。
- 2 行目には、各種類のプレゼントの個数を表す B_1, B_2, \ldots, B_M が、スペース区切りで与えられる。
- 3 行目から N 行分にわたって、各参加者の希望が与えられる。
- 2 + j 行目では、j 番目の参加者が希望するプレゼントの種類数 K_j と、その種類の番号 C_{j,1}, C_{j,2}, \ldots, C_{j,K_j} が、スペース区切りで与えられる。K_j = 0 の場合、種類の番号は与えられず、K_j のみが記載される。
出力
満足する参加者の人数の最大値を 1 行で出力してください。
入力例 1
3 2 2 2 1 1 1 1 1 2
出力例 1
3
入力例 2
4 2 1 3 1 1 1 1 1 1 1 1
出力例 2
1
入力例 3
6 3 2 2 3 1 1 1 1 2 1 2 1 2 1 3 0
出力例 3
5
入力例 4
10 5 2 2 2 2 3 2 1 2 2 1 2 1 3 1 3 2 3 4 2 4 5 1 5 1 5 2 1 5 0
出力例 4
9
入力例 5
1 1 1 0
出力例 5
0
Score : 466 pts
Problem Statement
Takahashi is organizing a Christmas party. There are N participants at the party, and Takahashi has decided to give a present to everyone.
Takahashi has prepared M types of presents. There are B_i presents of the i-th type. He must give exactly 1 present to each participant, but he cannot distribute more presents of each type than the number he has prepared. It is guaranteed that the total number of presents (B_1 + B_2 + \cdots + B_M) is at least N, so it is always possible to give a present to everyone.
Each participant has their own preferences. The j-th participant desires K_j types of presents, and will be satisfied if they receive any one of them. The type numbers of the desired presents are given as C_{j,1}, C_{j,2}, \ldots, C_{j,K_j}. If K_j = 0, that participant will not be satisfied no matter which present they receive. It is allowed to give a participant a present of a type they do not desire, but that participant will not be satisfied.
Takahashi wants to distribute the presents so as to maximize the number of satisfied participants. Find the maximum number of satisfied participants.
Constraints
- 1 \leq N \leq 500
- 1 \leq M \leq 500
- 1 \leq B_i \leq N (1 \leq i \leq M)
- B_1 + B_2 + \cdots + B_M \geq N
- 0 \leq K_j \leq M (1 \leq j \leq N)
- 1 \leq C_{j,k} \leq M (1 \leq j \leq N, 1 \leq k \leq K_j)
- For the j-th participant, C_{j,1}, C_{j,2}, \ldots, C_{j,K_j} are all distinct
- All input values are integers
Input
N M
B_1 B_2 \cdots B_M
K_1 C_{1,1} C_{1,2} \cdots C_{1,K_1}
K_2 C_{2,1} C_{2,2} \cdots C_{2,K_2}
:
K_N C_{N,1} C_{N,2} \cdots C_{N,K_N}
- The first line contains N, the number of participants, and M, the number of types of presents, separated by a space.
- The second line contains B_1, B_2, \ldots, B_M, the number of presents of each type, separated by spaces.
- The following N lines describe each participant's preferences.
- The (2 + j)-th line contains the number of types K_j that the j-th participant desires, followed by the type numbers C_{j,1}, C_{j,2}, \ldots, C_{j,K_j}, separated by spaces. If K_j = 0, no type numbers are given, and only K_j is written.
Output
Output the maximum number of satisfied participants in a single line.
Sample Input 1
3 2 2 2 1 1 1 1 1 2
Sample Output 1
3
Sample Input 2
4 2 1 3 1 1 1 1 1 1 1 1
Sample Output 2
1
Sample Input 3
6 3 2 2 3 1 1 1 1 2 1 2 1 2 1 3 0
Sample Output 3
5
Sample Input 4
10 5 2 2 2 2 3 2 1 2 2 1 2 1 3 1 3 2 3 4 2 4 5 1 5 1 5 2 1 5 0
Sample Output 4
9
Sample Input 5
1 1 1 0
Sample Output 5
0