Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 233 点
問題文
高橋君は、会社の備品購入を担当しています。会社では N 種類の備品を取り扱っており、備品には 1 から N までの番号が付けられています。備品 i の実際の価格は P_i 円です。
ある日、高橋君が使用している古い価格表に誤りがあることが判明しました。この古い価格表では、備品 i の価格が Q_i 円と記載されています。
高橋君は、各部署からの購入申請に対して、実際の購入金額と古い価格表に基づく見積もり金額との差額を報告する必要があります。
今日、M 個の部署から購入申請がありました。j 番目の部署は、K_j 種類の備品を各 1 個ずつ購入することを希望しており、購入を希望する備品の番号は C_{j,1}, C_{j,2}, \ldots, C_{j,K_j} です。同じ部署の購入希望リストに同一の備品番号が複数回現れることはありません。
各部署について、実際の合計金額から古い価格表に基づく見積もり合計金額を引いた値、すなわち
\sum_{k=1}^{K_j} (P_{C_{j,k}} - Q_{C_{j,k}})
を求めてください。この値は負になることもあります。
制約
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 1 \leq P_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq Q_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq K_j \leq N (1 \leq j \leq M)
- 1 \leq C_{j,k} \leq N (1 \leq j \leq M, 1 \leq k \leq K_j)
- C_{j,1}, C_{j,2}, \ldots, C_{j,K_j} は互いに異なる (1 \leq j \leq M)
- \sum_{j=1}^{M} K_j \leq 2 \times 10^5
- 入力はすべて整数
入力
N M
P_1 P_2 \ldots P_N
Q_1 Q_2 \ldots Q_N
K_1 C_{1,1} C_{1,2} \ldots C_{1,K_1}
K_2 C_{2,1} C_{2,2} \ldots C_{2,K_2}
\vdots
K_M C_{M,1} C_{M,2} \ldots C_{M,K_M}
- 1 行目には、備品の種類数 N と部署の数 M が、スペース区切りで与えられる。
- 2 行目には、各備品の実際の価格 P_1, P_2, \ldots, P_N が、スペース区切りで与えられる。
- 3 行目には、古い価格表に記載されている各備品の価格 Q_1, Q_2, \ldots, Q_N が、スペース区切りで与えられる。
- 4 行目から 3+M 行目まで、各部署の購入申請情報が与えられる。第 3 + j 行目には、j 番目の部署が購入を希望する備品の種類数 K_j と、購入を希望する備品の番号 C_{j,1}, C_{j,2}, \ldots, C_{j,K_j} が、スペース区切りで与えられる。
出力
M 行にわたって出力せよ。j 行目 (1 \leq j \leq M) には、j 番目の部署について、実際の合計金額から古い価格表に基づく見積もり合計金額を引いた値を整数で出力せよ。
入力例 1
3 2 100 200 300 90 250 280 2 1 3 3 1 2 3
出力例 1
30 -20
入力例 2
4 3 50 30 100 200 100 80 50 300 1 1 2 3 4 4 1 2 3 4
出力例 2
-50 -50 -150
入力例 3
8 5 500 1200 800 350 960 1500 2200 430 480 1300 750 400 1000 1450 2100 500 3 1 4 7 2 2 6 5 1 2 3 4 5 1 8 4 5 6 7 8
出力例 3
70 -50 -120 -70 40
入力例 4
10 8 1000000000 999999999 500000000 123456789 777777777 100 200 300 999999 1 900000000 1000000000 600000000 123456790 777777776 200 100 400 999998 2 3 1 2 3 4 4 5 6 7 2 8 9 1 10 5 1 3 5 7 10 10 1 2 3 4 5 6 7 8 9 10 2 1 10 3 6 7 8
出力例 4
-1 0 -99 -1 100 -101 99999999 -100
入力例 5
1 1 1000000000 1 1 1
出力例 5
999999999
Score : 233 pts
Problem Statement
Takahashi is in charge of purchasing supplies for his company. The company handles N types of supplies, numbered from 1 to N. The actual price of supply i is P_i yen.
One day, it was discovered that the old price list Takahashi had been using contained errors. In this old price list, the price of supply i is listed as Q_i yen.
Takahashi needs to report the difference between the actual purchase amount and the estimated amount based on the old price list for each department's purchase request.
Today, purchase requests were received from M departments. The j-th department wishes to purchase K_j types of supplies, one of each, and the supply numbers they wish to purchase are C_{j,1}, C_{j,2}, \ldots, C_{j,K_j}. The same supply number does not appear more than once in the same department's purchase request list.
For each department, find the value obtained by subtracting the estimated total amount based on the old price list from the actual total amount, that is:
\sum_{k=1}^{K_j} (P_{C_{j,k}} - Q_{C_{j,k}})
This value may be negative.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 1 \leq P_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq Q_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq K_j \leq N (1 \leq j \leq M)
- 1 \leq C_{j,k} \leq N (1 \leq j \leq M, 1 \leq k \leq K_j)
- C_{j,1}, C_{j,2}, \ldots, C_{j,K_j} are pairwise distinct (1 \leq j \leq M)
- \sum_{j=1}^{M} K_j \leq 2 \times 10^5
- All inputs are integers
Input
N M
P_1 P_2 \ldots P_N
Q_1 Q_2 \ldots Q_N
K_1 C_{1,1} C_{1,2} \ldots C_{1,K_1}
K_2 C_{2,1} C_{2,2} \ldots C_{2,K_2}
\vdots
K_M C_{M,1} C_{M,2} \ldots C_{M,K_M}
- The first line contains the number of supply types N and the number of departments M, separated by a space.
- The second line contains the actual prices of each supply P_1, P_2, \ldots, P_N, separated by spaces.
- The third line contains the prices listed in the old price list for each supply Q_1, Q_2, \ldots, Q_N, separated by spaces.
- From the 4th line to the (3+M)-th line, the purchase request information for each department is given. The (3 + j)-th line contains the number of supply types K_j that the j-th department wishes to purchase, followed by the supply numbers C_{j,1}, C_{j,2}, \ldots, C_{j,K_j} they wish to purchase, separated by spaces.
Output
Output M lines. On the j-th line (1 \leq j \leq M), output as an integer the value obtained by subtracting the estimated total amount based on the old price list from the actual total amount for the j-th department.
Sample Input 1
3 2 100 200 300 90 250 280 2 1 3 3 1 2 3
Sample Output 1
30 -20
Sample Input 2
4 3 50 30 100 200 100 80 50 300 1 1 2 3 4 4 1 2 3 4
Sample Output 2
-50 -50 -150
Sample Input 3
8 5 500 1200 800 350 960 1500 2200 430 480 1300 750 400 1000 1450 2100 500 3 1 4 7 2 2 6 5 1 2 3 4 5 1 8 4 5 6 7 8
Sample Output 3
70 -50 -120 -70 40
Sample Input 4
10 8 1000000000 999999999 500000000 123456789 777777777 100 200 300 999999 1 900000000 1000000000 600000000 123456790 777777776 200 100 400 999998 2 3 1 2 3 4 4 5 6 7 2 8 9 1 10 5 1 3 5 7 10 10 1 2 3 4 5 6 7 8 9 10 2 1 10 3 6 7 8
Sample Output 4
-1 0 -99 -1 100 -101 99999999 -100
Sample Input 5
1 1 1000000000 1 1 1
Sample Output 5
999999999
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 333 点
問題文
高橋君は庭師として働いており、横一列に並んだ N 区画の芝生の管理を担当しています。各区画には左から順に 1 から N の番号が付いています。
最初、すべての区画の水分量は 0 です。j 番目(1 \leq j \leq N)の区画の目標水分量は T_j です。
高橋君はスプリンクラーを使って芝生に水をまきます。1回の操作では、開始位置として整数 i(1 \leq i \leq N-K+1)を選び、区画 i から区画 i+K-1 までの連続する K 区画それぞれの水分量を 1 増加させます。
高橋君はこの操作を好きな回数だけ(0 回でもよい)繰り返すことができます。操作ごとに開始位置は自由に選ぶことができ、同じ開始位置を複数回選ぶこともできます。
すべての区画の水分量をちょうど目標水分量に一致させることが可能かどうかを判定してください。
制約
- 1 \leq K \leq N \leq 2 \times 10^5
- 0 \leq T_j \leq 10^9(1 \leq j \leq N)
- 入力はすべて整数
入力
N K T_1 T_2 \ldots T_N
- 1 行目には、芝生の区画数 N と、1回の操作で水をまく連続区画数 K が、スペース区切りで与えられる。
- 2 行目には、各区画の目標水分量 T_1, T_2, \ldots, T_N が、スペース区切りで与えられる。
出力
すべての区画の水分量をちょうど目標水分量に一致させることが可能な場合は Yes を、不可能な場合は No を出力してください。
入力例 1
5 3 2 3 3 2 1
出力例 1
No
入力例 2
6 2 1 2 2 1 0 1
出力例 2
No
入力例 3
10 4 3 5 6 7 7 6 5 4 2 1
出力例 3
No
Score : 333 pts
Problem Statement
Takahashi works as a gardener and is responsible for managing N sections of lawn arranged in a horizontal row. Each section is numbered from 1 to N from left to right.
Initially, the moisture level of every section is 0. The target moisture level of the j-th section (1 \leq j \leq N) is T_j.
Takahashi waters the lawn using a sprinkler. In one operation, he chooses a starting position, an integer i (1 \leq i \leq N-K+1), and increases the moisture level of each of the K consecutive sections from section i to section i+K-1 by 1.
Takahashi can repeat this operation any number of times (including 0 times). The starting position can be freely chosen for each operation, and the same starting position can be chosen multiple times.
Determine whether it is possible to make the moisture level of every section exactly equal to its target moisture level.
Constraints
- 1 \leq K \leq N \leq 2 \times 10^5
- 0 \leq T_j \leq 10^9 (1 \leq j \leq N)
- All input values are integers.
Input
N K T_1 T_2 \ldots T_N
- The first line contains the number of lawn sections N and the number of consecutive sections watered in one operation K, separated by a space.
- The second line contains the target moisture levels T_1, T_2, \ldots, T_N for each section, separated by spaces.
Output
If it is possible to make the moisture level of every section exactly equal to the target moisture level, print Yes; otherwise, print No.
Sample Input 1
5 3 2 3 3 2 1
Sample Output 1
No
Sample Input 2
6 2 1 2 2 1 0 1
Sample Output 2
No
Sample Input 3
10 4 3 5 6 7 7 6 5 4 2 1
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は、ビル内の避難シミュレーションを行っています。
このビルには N 個の部屋と M 本の廊下があります。部屋には 1 から N までの番号が付けられており、各廊下は異なる2つの部屋を双方向に結んでいます。同じ2つの部屋を結ぶ廊下は高々1本です。
廊下や部屋に通行人数の上限はなく、同じ廊下を同じターンに複数の避難者が通ることも、同じ部屋に何人の避難者がいることも問題ありません。各避難者の行動は互いに影響しません。
ビル内には K 人の避難者がいます。避難者 i(1 \leq i \leq K)は最初、部屋 S_i にいます。異なる避難者が同じ部屋にいることもあります。
また、ビル内では Q 個の部屋で火災が発生しています。火災が発生している部屋は P_1, P_2, \ldots, P_Q であり、これらはすべて異なります。火災はシミュレーション中ずっと燃え続けますが、他の部屋に延焼することはありません。
部屋 T は非常出口であり、火災は発生していません。すべての避難者はグラフ全体の構造、火災の位置、他の避難者の位置を完全に把握しており、全員が部屋 T に到達できるよう最適な行動をとるものとします。各避難者は同じターン内でもそれぞれ独立に行動(異なる部屋への移動や、留まること)を選択できます。
シミュレーションの進行
シミュレーションはターン制で進行します。一度 脱落 または 避難完了 した避難者は、以降のターンには参加しません。
開始時の処理(第1ターンの前)
シミュレーション開始時に、以下の処理が行われます:
- 初期位置が部屋 T である避難者は、直ちに 避難完了 となります。
- 初期位置が火災の発生している部屋である避難者は、即座に 脱落 し、避難に失敗します。
制約より部屋 T では火災は発生しないため、両方に該当する避難者はいません。
各ターンの処理
その後、各ターンでは以下の処理がこの順に行われます:
- 移動フェーズ: まだ脱落も避難完了もしていないすべての避難者が同時に行動します。各避難者は、現在いる部屋に廊下で直接つながっている隣接部屋のうち1つへ移動するか、現在の部屋に留まるかを選択します。1回の移動フェーズで移動できるのは廊下1本分(隣接する部屋1つ)までです。
- 避難完了チェック: 移動後に部屋 T にいる避難者は 避難完了 となります。
- 脱落チェック: 移動後に火災の発生している部屋にいる避難者は 脱落 します。制約より部屋 T では火災は発生しないため、ステップ2で避難完了した避難者がこのステップで脱落することはありません。
注意: 火災の発生している部屋への移動は禁止されていませんが、移動した場合は脱落チェックにより脱落します。したがって、1回の移動フェーズで移動できるのは隣接部屋1つまでであるため、火災の発生している部屋を「通過」して向こう側に抜けることはできません。
求めるもの
すべての避難者が脱落することなく部屋 T に到達できるかどうかを判定してください。全員が到達可能な場合は、全員が避難完了するまでに必要な最小ターン数を求めてください。
すべての避難者がシミュレーション開始時点で既に避難完了している場合(全員の初期位置が部屋 T である場合)は、必要なターン数は 0 です。
1人でも脱落が避けられない避難者がいる場合は、全員の避難は不可能です。特に、初期位置が火災の発生している部屋の避難者は開始時に脱落するため、そのような避難者がいる場合は必ず不可能となります。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq K \leq 2 \times 10^5
- 0 \leq Q \leq N
- 1 \leq T \leq N
- 1 \leq U_i, V_i \leq N \ (1 \leq i \leq M)
- U_i \neq V_i \ (1 \leq i \leq M)
- 同じ2部屋を結ぶ廊下は高々1本である
- 1 \leq S_i \leq N \ (1 \leq i \leq K)
- S_i は重複することがある
- 1 \leq P_j \leq N \ (1 \leq j \leq Q)
- P_j はすべて異なる
- すべての j について P_j \neq T
- グラフは連結とは限らない
- 避難者の初期位置が火災の発生している部屋であることもありうる
- 入力はすべて整数である
入力
N M K Q T U_1 V_1 U_2 V_2 \vdots U_M V_M S_1 S_2 \ldots S_K P_1 P_2 \ldots P_Q
- 1行目には、部屋の数 N、廊下の数 M、避難者の数 K、火災箇所の数 Q、非常出口の部屋番号 T が、スペース区切りで与えられる。
- 続く M 行のそれぞれには、各廊下が結ぶ2つの部屋の番号 U_i, V_i がスペース区切りで与えられる。
- その次の行には、K 人の避難者の初期位置 S_1, S_2, \ldots, S_K がスペース区切りで与えられる。
- 最後の行には、Q 箇所の火災が発生している部屋の番号 P_1, P_2, \ldots, P_Q がスペース区切りで与えられる。Q = 0 の場合、この行は空行として与えられる。
出力
すべての避難者が脱落することなく部屋 T に到達できる場合は、そのために必要な最小ターン数を1行で出力してください。到達できない避難者が1人でもいる場合は -1 を出力してください。
入力例 1
5 4 3 1 5 1 2 2 3 3 5 2 4 1 3 5 4
出力例 1
3
入力例 2
6 5 3 1 6 1 2 2 3 3 4 4 5 5 6 1 3 5 3
出力例 2
-1
入力例 3
12 14 6 2 12 1 2 2 3 3 4 4 12 2 5 5 7 7 8 8 12 3 6 9 10 9 11 11 12 5 9 1 7 1 5 9 11 12 7 6 10
出力例 3
3
入力例 4
20 19 10 4 20 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 20 4 16 7 17 10 18 13 19 1 3 5 7 9 11 13 15 20 1 16 17 18 19
出力例 4
15
入力例 5
1 0 1 0 1 1
出力例 5
0
Score : 366 pts
Problem Statement
Takahashi is conducting an evacuation simulation inside a building.
The building has N rooms and M corridors. The rooms are numbered from 1 to N, and each corridor bidirectionally connects two distinct rooms. There is at most one corridor connecting any pair of rooms.
There is no limit on the number of people that can pass through corridors or be in rooms. Multiple evacuees can use the same corridor in the same turn, and any number of evacuees can be in the same room without issue. Each evacuee's actions do not affect the others.
There are K evacuees in the building. Evacuee i (1 \leq i \leq K) is initially in room S_i. Different evacuees may be in the same room.
Additionally, fires have broken out in Q rooms of the building. The rooms on fire are P_1, P_2, \ldots, P_Q, all of which are distinct. Fires continue to burn throughout the simulation but do not spread to other rooms.
Room T is the emergency exit, and it is not on fire. All evacuees have complete knowledge of the entire graph structure, fire locations, and positions of other evacuees, and they all act optimally so that everyone can reach room T. Each evacuee can independently choose their action (moving to a different room or staying) within the same turn.
Simulation Progression
The simulation proceeds in turns. Once an evacuee has been eliminated or has completed evacuation, they do not participate in subsequent turns.
Initial Processing (Before Turn 1)
At the start of the simulation, the following processing occurs:
- Evacuees whose initial position is room T immediately complete evacuation.
- Evacuees whose initial position is a room on fire are immediately eliminated and fail to evacuate.
Due to the constraints, room T is never on fire, so no evacuee can satisfy both conditions.
Processing Each Turn
After that, the following processing occurs in each turn in this order:
- Movement Phase: All evacuees who have neither been eliminated nor completed evacuation act simultaneously. Each evacuee chooses either to move to one adjacent room directly connected to their current room by a corridor, or to stay in their current room. In a single movement phase, an evacuee can move at most one corridor (to one adjacent room).
- Evacuation Completion Check: Evacuees who are in room T after moving complete evacuation.
- Elimination Check: Evacuees who are in a room on fire after moving are eliminated. Due to the constraints, room T is never on fire, so an evacuee who completed evacuation in step 2 will not be eliminated in this step.
Note: Moving to a room on fire is not prohibited, but doing so results in elimination during the elimination check. Therefore, since an evacuee can move at most one adjacent room per movement phase, it is impossible to "pass through" a room on fire to reach the other side.
Objective
Determine whether all evacuees can reach room T without being eliminated. If everyone can reach it, find the minimum number of turns required for all evacuees to complete evacuation.
If all evacuees have already completed evacuation at the start of the simulation (i.e., everyone's initial position is room T), the required number of turns is 0.
If even one evacuee cannot avoid elimination, then evacuation of everyone is impossible. In particular, if any evacuee's initial position is a room on fire, they are eliminated at the start, so evacuation is necessarily impossible in such cases.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq K \leq 2 \times 10^5
- 0 \leq Q \leq N
- 1 \leq T \leq N
- 1 \leq U_i, V_i \leq N \ (1 \leq i \leq M)
- U_i \neq V_i \ (1 \leq i \leq M)
- There is at most one corridor connecting any pair of rooms
- 1 \leq S_i \leq N \ (1 \leq i \leq K)
- S_i may contain duplicates
- 1 \leq P_j \leq N \ (1 \leq j \leq Q)
- All P_j are distinct
- P_j \neq T for all j
- The graph is not necessarily connected
- An evacuee's initial position may be a room on fire
- All input values are integers
Input
N M K Q T U_1 V_1 U_2 V_2 \vdots U_M V_M S_1 S_2 \ldots S_K P_1 P_2 \ldots P_Q
- The first line contains the number of rooms N, the number of corridors M, the number of evacuees K, the number of fire locations Q, and the emergency exit room number T, separated by spaces.
- Each of the following M lines contains the numbers of the two rooms U_i, V_i connected by each corridor, separated by a space.
- The next line contains the initial positions S_1, S_2, \ldots, S_K of the K evacuees, separated by spaces.
- The last line contains the room numbers P_1, P_2, \ldots, P_Q where fires have broken out, separated by spaces. If Q = 0, this line is given as an empty line.
Output
If all evacuees can reach room T without being eliminated, output the minimum number of turns required on a single line. If even one evacuee cannot reach it, output -1.
Sample Input 1
5 4 3 1 5 1 2 2 3 3 5 2 4 1 3 5 4
Sample Output 1
3
Sample Input 2
6 5 3 1 6 1 2 2 3 3 4 4 5 5 6 1 3 5 3
Sample Output 2
-1
Sample Input 3
12 14 6 2 12 1 2 2 3 3 4 4 12 2 5 5 7 7 8 8 12 3 6 9 10 9 11 11 12 5 9 1 7 1 5 9 11 12 7 6 10
Sample Output 3
3
Sample Input 4
20 19 10 4 20 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 20 4 16 7 17 10 18 13 19 1 3 5 7 9 11 13 15 20 1 16 17 18 19
Sample Output 4
15
Sample Input 5
1 0 1 0 1 1
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は、 N 個のブロックが一列に並んでいるパズルゲームをプレイしています。
左から i 番目のブロックには整数 A_i が書かれています。この値は正の場合も負の場合もあります。
高橋君は以下の操作を好きな回数( 0 回以上)行うことができます。
操作: 連続する K 個のブロックを選び、それらをすべて消去する。消去されたブロックは消滅し、残ったブロックは左に詰められて再び一列に並びます。
高橋君は、最終的に残るブロックに書かれた整数の合計を最大化したいと考えています。
ただし、ブロックが 1 個も残らない場合、合計は 0 とします。
操作を最適に行ったとき、残るブロックに書かれた整数の合計の最大値を求めてください。
制約
- 1 \leq K \leq N \leq 10^6
- -10^9 \leq A_i \leq 10^9
- 入力はすべて整数である
入力
N K A_1 A_2 \ldots A_N
- 1 行目には、ブロックの個数を表す N と、一度の操作で消去するブロックの個数を表す K が、スペース区切りで与えられる。
- 2 行目には、各ブロックに書かれた整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
出力
残るブロックに書かれた整数の合計の最大値を 1 行で出力せよ。
入力例 1
5 2 3 -5 4 -2 1
出力例 1
4
入力例 2
6 3 -1 2 -3 4 -5 6
出力例 2
7
入力例 3
15 4 8 -10 3 5 -2 -7 6 1 -4 9 -8 2 7 -6 4
出力例 3
21
入力例 4
30 5 12 -5 -20 7 3 -8 15 -2 6 -11 4 9 -30 18 -1 5 -6 14 -9 2 -3 10 -12 8 1 -7 16 -4 -15 11
出力例 4
74
入力例 5
1 1 -1000000000
出力例 5
0
Score : 400 pts
Problem Statement
Takahashi is playing a puzzle game where N blocks are arranged in a row.
The i-th block from the left has an integer A_i written on it. This value can be positive or negative.
Takahashi can perform the following operation any number of times (zero or more times).
Operation: Choose K consecutive blocks and remove all of them. The removed blocks disappear, and the remaining blocks are shifted to the left to form a single row again.
Takahashi wants to maximize the sum of the integers written on the blocks that remain in the end.
If no blocks remain, the sum is defined to be 0.
Find the maximum possible sum of the integers written on the remaining blocks when the operations are performed optimally.
Constraints
- 1 \leq K \leq N \leq 10^6
- -10^9 \leq A_i \leq 10^9
- All inputs are integers
Input
N K A_1 A_2 \ldots A_N
- The first line contains N, the number of blocks, and K, the number of blocks removed in a single operation, separated by a space.
- The second line contains the integers A_1, A_2, \ldots, A_N written on each block, separated by spaces.
Output
Print the maximum possible sum of the integers written on the remaining blocks in one line.
Sample Input 1
5 2 3 -5 4 -2 1
Sample Output 1
4
Sample Input 2
6 3 -1 2 -3 4 -5 6
Sample Output 2
7
Sample Input 3
15 4 8 -10 3 5 -2 -7 6 1 -4 9 -8 2 7 -6 4
Sample Output 3
21
Sample Input 4
30 5 12 -5 -20 7 3 -8 15 -2 6 -11 4 9 -30 18 -1 5 -6 14 -9 2 -3 10 -12 8 1 -7 16 -4 -15 11
Sample Output 4
74
Sample Input 5
1 1 -1000000000
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 466 点
問題文
高橋君は食品メーカーの品質管理担当者です。新商品の開発にあたり、N 種類の原材料を使用する予定ですが、これらの原材料の中にはアレルギーを引き起こす原因物質が含まれている可能性があります。
N 種類の原材料にはそれぞれ 1 から N の番号が付けられています。各原材料は「アレルギー原因物質である」か「安全である」のいずれかです。
高橋君は、M 人のモニターに協力を依頼し、アレルギー検査を行いました。
具体的には、各モニター j(1 \leq j \leq M)には、原材料の集合 S_j \subseteq \{1, 2, \ldots, N\} が割り当てられています。S_j の要素数を k_j とし、S_j の要素を s_{j,1}, s_{j,2}, \ldots, s_{j,k_j} と表します。同一の原材料が複数のモニターに割り当てられることもあります。また、どのモニターにも割り当てられていない原材料が存在することもあります。
モニター j は S_j に含まれるすべての原材料を同時に摂取します。摂取した原材料の中に 1 つでもアレルギー原因物質が含まれていれば、モニター j はアレルギー反応を示します。摂取した原材料がすべて安全であれば、モニター j にはアレルギー反応は起こりません。なお、ある原材料がアレルギー原因物質であるかどうかは原材料自体の性質であり、どのモニターが摂取しても結果は変わりません。
検査の結果、各モニターについて「アレルギー反応が出た」か「反応なし」かが判明しました。この結果を R_1, R_2, \ldots, R_M で表します。R_j = 1 のときモニター j にアレルギー反応が出たことを、R_j = 0 のときモニター j に反応がなかったことを意味します。
高橋君はこの結果から、アレルギー原因物質の組み合わせとして考えられるパターンが何通りあるかを知りたいと考えています。
すなわち、N 種類の原材料のそれぞれについてアレルギー原因物質か安全かを定めた 2^N 通りの組み合わせのうち、以下の条件をすべて満たすものの個数を求めてください。
- R_j = 1 であるすべてのモニター j について、S_j の中に少なくとも 1 つアレルギー原因物質が含まれる。
- R_j = 0 であるすべてのモニター j について、S_j の中にアレルギー原因物質が 1 つも含まれない。
答えは非常に大きくなる可能性があるため、998244353 で割った余りを出力してください。条件を満たす組み合わせが存在しない場合は 0 を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 20
- 1 \leq k_j \leq N(1 \leq j \leq M)。ここで k_j はモニター j が摂取する原材料の数である。
- 1 \leq s_{j,i} \leq N(1 \leq j \leq M, 1 \leq i \leq k_j)。ここで s_{j,i} はモニター j が摂取する原材料の番号である。
- 各 j について、s_{j,1}, s_{j,2}, \ldots, s_{j,k_j} はすべて相異なる
- \sum_{j=1}^{M} k_j \leq 2 \times 10^5
- R_j \in \{0, 1\}(1 \leq j \leq M)
- 入力はすべて整数である
入力
N M
k_1 s_{1,1} s_{1,2} \ldots s_{1,k_1}
k_2 s_{2,1} s_{2,2} \ldots s_{2,k_2}
\vdots
k_M s_{M,1} s_{M,2} \ldots s_{M,k_M}
R_1 R_2 \ldots R_M
- 1 行目には、原材料の種類数 N とモニターの人数 M が、スペース区切りで与えられる。
- 続く M 行の j 行目(1 \leq j \leq M)では、モニター j に割り当てられた原材料の集合が与えられる。先頭の整数 k_j はモニター j が摂取する原材料の数であり、続く k_j 個の整数 s_{j,1}, s_{j,2}, \ldots, s_{j,k_j} はそのモニターが摂取する原材料の番号である。
- 最後の行には、各モニターの検査結果 R_1, R_2, \ldots, R_M がスペース区切りで与えられる。R_j = 0 のときモニター j は反応なしだったことを、R_j = 1 のときモニター j にアレルギー反応が出たことを表す。
出力
すべてのモニターの検査結果と矛盾しないアレルギー原因物質の組み合わせの個数を 998244353 で割った余りを 1 行で出力せよ。
入力例 1
3 2 2 1 2 2 2 3 1 0
出力例 1
1
入力例 2
2 2 1 1 1 1 0 1
出力例 2
0
入力例 3
10 5 3 1 2 3 4 3 4 5 6 2 6 7 3 2 8 9 4 1 7 9 10 1 0 0 1 0
出力例 3
2
入力例 4
30 12 5 1 2 3 4 5 6 4 5 6 7 8 9 4 10 11 12 13 7 2 8 14 15 16 17 18 3 19 20 21 5 1 10 19 22 23 6 6 12 18 24 25 26 4 3 14 22 27 5 7 15 20 28 29 6 9 13 17 21 25 30 3 11 16 24 4 5 23 26 30 1 0 1 1 1 1 1 1 1 0 0 1
出力例 4
19587
入力例 5
1 1 1 1 0
出力例 5
1
Score : 466 pts
Problem Statement
Takahashi is a quality control manager at a food manufacturer. For the development of a new product, he plans to use N types of raw materials, but some of these raw materials may contain substances that cause allergies.
The N types of raw materials are numbered from 1 to N. Each raw material is either an "allergen" or "safe."
Takahashi asked M monitors to cooperate and conducted allergy tests.
Specifically, each monitor j (1 \leq j \leq M) is assigned a set of raw materials S_j \subseteq \{1, 2, \ldots, N\}. Let k_j denote the number of elements in S_j, and let the elements of S_j be represented as s_{j,1}, s_{j,2}, \ldots, s_{j,k_j}. The same raw material may be assigned to multiple monitors. There may also be raw materials that are not assigned to any monitor.
Monitor j consumes all raw materials in S_j simultaneously. If at least one allergen is included among the consumed raw materials, monitor j shows an allergic reaction. If all consumed raw materials are safe, no allergic reaction occurs for monitor j. Note that whether a raw material is an allergen is an inherent property of the raw material itself, and the result does not change regardless of which monitor consumes it.
As a result of the tests, it was determined for each monitor whether they "had an allergic reaction" or "had no reaction." This result is represented by R_1, R_2, \ldots, R_M. R_j = 1 means monitor j had an allergic reaction, and R_j = 0 means monitor j had no reaction.
From these results, Takahashi wants to know how many possible patterns of allergen combinations exist.
That is, among the 2^N combinations where each of the N types of raw materials is designated as either an allergen or safe, find the number of combinations that satisfy all of the following conditions:
- For every monitor j with R_j = 1, at least one allergen is contained in S_j.
- For every monitor j with R_j = 0, no allergens are contained in S_j.
Since the answer can be very large, output the remainder when divided by 998244353. If no valid combination exists, output 0.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 20
- 1 \leq k_j \leq N (1 \leq j \leq M), where k_j is the number of raw materials consumed by monitor j.
- 1 \leq s_{j,i} \leq N (1 \leq j \leq M, 1 \leq i \leq k_j), where s_{j,i} is the number of a raw material consumed by monitor j.
- For each j, s_{j,1}, s_{j,2}, \ldots, s_{j,k_j} are all distinct.
- \sum_{j=1}^{M} k_j \leq 2 \times 10^5
- R_j \in \{0, 1\} (1 \leq j \leq M)
- All input values are integers.
Input
N M
k_1 s_{1,1} s_{1,2} \ldots s_{1,k_1}
k_2 s_{2,1} s_{2,2} \ldots s_{2,k_2}
\vdots
k_M s_{M,1} s_{M,2} \ldots s_{M,k_M}
R_1 R_2 \ldots R_M
- The first line contains the number of raw material types N and the number of monitors M, separated by a space.
- In the following M lines, the j-th line (1 \leq j \leq M) gives the set of raw materials assigned to monitor j. The first integer k_j is the number of raw materials consumed by monitor j, followed by k_j integers s_{j,1}, s_{j,2}, \ldots, s_{j,k_j} representing the numbers of the raw materials consumed by that monitor.
- The last line contains the test results R_1, R_2, \ldots, R_M for each monitor, separated by spaces. R_j = 0 means monitor j had no reaction, and R_j = 1 means monitor j had an allergic reaction.
Output
Output in one line the number of allergen combinations consistent with all monitors' test results, modulo 998244353.
Sample Input 1
3 2 2 1 2 2 2 3 1 0
Sample Output 1
1
Sample Input 2
2 2 1 1 1 1 0 1
Sample Output 2
0
Sample Input 3
10 5 3 1 2 3 4 3 4 5 6 2 6 7 3 2 8 9 4 1 7 9 10 1 0 0 1 0
Sample Output 3
2
Sample Input 4
30 12 5 1 2 3 4 5 6 4 5 6 7 8 9 4 10 11 12 13 7 2 8 14 15 16 17 18 3 19 20 21 5 1 10 19 22 23 6 6 12 18 24 25 26 4 3 14 22 27 5 7 15 20 28 29 6 9 13 17 21 25 30 3 11 16 24 4 5 23 26 30 1 0 1 1 1 1 1 1 1 0 0 1
Sample Output 4
19587
Sample Input 5
1 1 1 1 0
Sample Output 5
1