実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 266 点
問題文
高橋君の家の庭には、 N 段の階段状に並んだ植木鉢があります。
それぞれの植木鉢には最初に水が入っており、 i 番目の植木鉢には A_i ミリリットルの水が入っています。階段状に配置されているため、 i 番目の植木鉢は i + 1 番目の植木鉢より高い位置にあります( N 番目の植木鉢が最も低い位置にあり、その先は排水溝につながっています)。
高橋君が植木鉢に日光を当てると、水の一部が蒸発し、残りが下の段の植木鉢へ流れ落ちる現象が起きます。具体的には、 i 番目の植木鉢に日光を当てると以下のことが起きます:
- その植木鉢に入っている水のうち B_i ミリリットルが蒸発して消える(水が B_i ミリリットル未満の場合は、すべての水が蒸発する)
- 蒸発後に残った水はすべて、下の段の植木鉢へ流れ落ちる( i < N の場合は i + 1 番目の植木鉢へ、 i = N の場合は排水溝へ流れ落ちて消える)
高橋君は Q 回の操作を順番に行います。 j 回目の操作では、 C_j 番目の植木鉢に日光を当てます。
すべての操作が終わった後、各植木鉢に残っている水の量をそれぞれ求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 0 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 0 \leq B_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq C_j \leq N (1 \leq j \leq Q)
- 入力はすべて整数
入力
N Q A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N C_1 C_2 \ldots C_Q
- 1 行目には、植木鉢の個数を表す N と、操作の回数を表す Q が、スペース区切りで与えられる。
- 2 行目には、各植木鉢の初期の水の量を表す A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
- 3 行目には、各植木鉢に日光を当てたときに蒸発する水の量を表す B_1, B_2, \ldots, B_N が、スペース区切りで与えられる。
- 4 行目には、各操作で日光を当てる植木鉢の番号を表す C_1, C_2, \ldots, C_Q が、スペース区切りで与えられる。
出力
すべての操作が終わった後の、各植木鉢に残っている水の量を、 1 番目から N 番目の順にスペース区切りで 1 行に出力せよ。
入力例 1
3 2 10 5 3 3 2 4 1 2
出力例 1
0 0 13
入力例 2
5 6 100 50 30 20 10 20 10 15 5 8 1 2 3 4 5 1
出力例 2
0 0 0 0 0
入力例 3
7 10 1000000000 500000000 300000000 200000000 100000000 50000000 25000000 100000000 200000000 150000000 80000000 60000000 30000000 20000000 1 1 2 2 3 3 4 5 6 7
出力例 3
0 0 0 0 0 0 0
Score : 266 pts
Problem Statement
In Takahashi's garden, there are flower pots arranged in N steps like a staircase.
Each flower pot initially contains water, and the i-th flower pot contains A_i milliliters of water. Due to the staircase arrangement, the i-th flower pot is positioned higher than the (i + 1)-th flower pot (the N-th flower pot is at the lowest position, and beyond it is a drain).
When Takahashi exposes a flower pot to sunlight, part of the water evaporates and the rest flows down to the flower pot on the step below. Specifically, when sunlight is applied to the i-th flower pot, the following occurs:
- B_i milliliters of the water in that flower pot evaporates and disappears (if the water is less than B_i milliliters, all the water evaporates)
- All the water remaining after evaporation flows down to the flower pot on the step below (if i < N, it flows to the (i + 1)-th flower pot; if i = N, it flows into the drain and disappears)
Takahashi performs Q operations in order. In the j-th operation, he exposes the C_j-th flower pot to sunlight.
After all operations are completed, determine the amount of water remaining in each flower pot.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 0 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 0 \leq B_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq C_j \leq N (1 \leq j \leq Q)
- All inputs are integers
Input
N Q A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N C_1 C_2 \ldots C_Q
- The first line contains N, the number of flower pots, and Q, the number of operations, separated by a space.
- The second line contains A_1, A_2, \ldots, A_N, the initial amounts of water in each flower pot, separated by spaces.
- The third line contains B_1, B_2, \ldots, B_N, the amounts of water that evaporate when each flower pot is exposed to sunlight, separated by spaces.
- The fourth line contains C_1, C_2, \ldots, C_Q, the numbers of the flower pots to be exposed to sunlight in each operation, separated by spaces.
Output
Output the amount of water remaining in each flower pot after all operations are completed, from the 1st to the N-th, separated by spaces on a single line.
Sample Input 1
3 2 10 5 3 3 2 4 1 2
Sample Output 1
0 0 13
Sample Input 2
5 6 100 50 30 20 10 20 10 15 5 8 1 2 3 4 5 1
Sample Output 2
0 0 0 0 0
Sample Input 3
7 10 1000000000 500000000 300000000 200000000 100000000 50000000 25000000 100000000 200000000 150000000 80000000 60000000 30000000 20000000 1 1 2 2 3 3 4 5 6 7
Sample Output 3
0 0 0 0 0 0 0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君は山道をハイキングしています。山道には N 個の地点が一列に並んでおり、i 番目(1 \leq i \leq N)の地点の標高は A_i です。
高橋君は、連続する 1 個以上の地点からなる区間のうち、標高が広義単調増加(非減少)であるものを「登り坂」と呼んでいます。すなわち、l 番目から r 番目(1 \leq l \leq r \leq N)までの地点からなる区間が「登り坂」であるとは、すべての l \leq i \leq r-1 について A_i \leq A_{i+1} が成り立つことを意味します。特に、地点が 1 個だけの区間(l = r)も「登り坂」です。
この山道の中で、含まれる地点の数が最大である「登り坂」を求め、その地点の数を出力してください。
制約
- 1 \leq N \leq 10^5
- 1 \leq A_i \leq 10^9
- 入力はすべて整数である。
入力
N A_1 A_2 : A_N
- 1 行目には、地点の数を表す整数 N が与えられる。
- 続く N 行の j 行目(1 \leq j \leq N)には、j 番目の地点の標高を表す整数 A_j が与えられる。
出力
含まれる地点の数が最大である「登り坂」の地点の数を 1 行で出力してください。
入力例 1
6 1 2 2 1 3 4
出力例 1
3
入力例 2
5 9 7 5 3 1
出力例 2
1
入力例 3
12 5 6 6 7 3 4 8 8 9 2 2 3
出力例 3
5
入力例 4
30 8 9 9 10 7 7 8 12 12 13 5 6 7 8 9 3 3 4 4 4 10 11 2 2 2 3 1 5 6 7
出力例 4
7
入力例 5
1 1000000000
出力例 5
1
Score : 300 pts
Problem Statement
Takahashi is hiking along a mountain trail. There are N points lined up in a row along the trail, and the elevation of the i-th point (1 \leq i \leq N) is A_i.
Takahashi calls a contiguous interval of one or more points an "uphill" if the elevations are non-decreasing. That is, an interval from the l-th to the r-th point (1 \leq l \leq r \leq N) is an "uphill" if A_i \leq A_{i+1} holds for all l \leq i \leq r-1. In particular, an interval consisting of only one point (l = r) is also an "uphill".
Find the "uphill" that contains the maximum number of points along this mountain trail, and output that number of points.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq A_i \leq 10^9
- All inputs are integers.
Input
N A_1 A_2 : A_N
- The first line contains an integer N representing the number of points.
- The j-th line (1 \leq j \leq N) of the following N lines contains an integer A_j representing the elevation of the j-th point.
Output
Output in one line the number of points in the "uphill" that contains the maximum number of points.
Sample Input 1
6 1 2 2 1 3 4
Sample Output 1
3
Sample Input 2
5 9 7 5 3 1
Sample Output 2
1
Sample Input 3
12 5 6 6 7 3 4 8 8 9 2 2 3
Sample Output 3
5
Sample Input 4
30 8 9 9 10 7 7 8 12 12 13 5 6 7 8 9 3 3 4 4 4 10 11 2 2 2 3 1 5 6 7
Sample Output 4
7
Sample Input 5
1 1000000000
Sample Output 5
1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 366 点
問題文
高橋君は、ある会社の人事部で働いています。この会社には N 人の社員がおり、組織は木構造になっています。
社員 1 が社長(組織のトップ)であり、各社員 i ( 2 \leq i \leq N )の直属の上司は社員 P_i です。
高橋君は、各社員に「給与ランク」と呼ばれる正の整数値を割り当てる必要があります。ただし、以下の条件を満たさなければなりません。
- 社長(社員 1 )の給与ランクは 1 である。
- 各社員の給与ランクは、その直属の上司の給与ランク以上である。
- 各社員 i には「上限ランク」 U_i が設定されており、社員 i の給与ランクは U_i 以下でなければならない。
高橋君は、すべての社員の給与ランクの合計を最大化したいと考えています。
条件を満たす給与ランクの割り当てが存在する場合は、給与ランクの合計の最大値を求めてください。条件を満たす割り当てが存在しない場合は -1 を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq U_i \leq 10^9 ( 1 \leq i \leq N )
- 1 \leq P_i < i ( 2 \leq i \leq N )
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N U_1 P_2 U_2 P_3 U_3 : P_N U_N
- 1 行目には、社員の数を表す N が与えられる。
- 2 行目には、社員 1 (社長)の上限ランク U_1 が与えられる。
- 3 行目から N + 1 行目には、社員 i ( 2 \leq i \leq N )の直属の上司 P_i と上限ランク U_i がスペース区切りで与えられる。
出力
条件を満たす給与ランクの割り当てが存在する場合は、すべての社員の給与ランクの合計の最大値を 1 行で出力してください。存在しない場合は -1 を出力してください。
入力例 1
5 3 1 4 1 2 2 5 2 4
出力例 1
16
入力例 2
4 5 1 3 1 0 2 4
出力例 2
-1
入力例 3
15 10 1 6 1 12 2 8 2 3 3 15 3 7 4 9 4 5 5 4 6 20 6 11 7 13 7 2 10 6
出力例 3
97
入力例 4
30 100 1 50 1 80 2 45 2 60 3 90 3 30 4 55 4 25 5 70 5 65 6 95 6 40 7 35 7 85 8 20 8 75 9 15 9 1000000000 10 10 10 68 11 72 11 58 12 99 12 33 13 44 13 88 14 22 14 77 15 66
出力例 4
1000001124
入力例 5
1 1
出力例 5
1
Score : 366 pts
Problem Statement
Takahashi works in the HR department of a company. The company has N employees, and the organization has a tree structure.
Employee 1 is the president (the top of the organization), and the direct supervisor of each employee i (2 \leq i \leq N) is employee P_i.
Takahashi needs to assign a positive integer value called a "salary rank" to each employee. However, the following conditions must be satisfied:
- The salary rank of the president (employee 1) is 1.
- The salary rank of each employee is greater than or equal to the salary rank of their direct supervisor.
- Each employee i has an "upper rank limit" U_i, and the salary rank of employee i must be at most U_i.
Takahashi wants to maximize the total salary rank of all employees.
If a valid assignment of salary ranks satisfying the conditions exists, find the maximum total of all salary ranks. If no valid assignment exists, output -1.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq U_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq P_i < i (2 \leq i \leq N)
- All inputs are integers.
Input
The input is given from standard input in the following format:
N U_1 P_2 U_2 P_3 U_3 : P_N U_N
- The first line contains N, representing the number of employees.
- The second line contains U_1, the upper rank limit of employee 1 (the president).
- From the third line to the (N+1)-th line, the direct supervisor P_i and the upper rank limit U_i of employee i (2 \leq i \leq N) are given, separated by a space.
Output
If a valid assignment of salary ranks satisfying the conditions exists, output the maximum total of all employees' salary ranks in one line. If no valid assignment exists, output -1.
Sample Input 1
5 3 1 4 1 2 2 5 2 4
Sample Output 1
16
Sample Input 2
4 5 1 3 1 0 2 4
Sample Output 2
-1
Sample Input 3
15 10 1 6 1 12 2 8 2 3 3 15 3 7 4 9 4 5 5 4 6 20 6 11 7 13 7 2 10 6
Sample Output 3
97
Sample Input 4
30 100 1 50 1 80 2 45 2 60 3 90 3 30 4 55 4 25 5 70 5 65 6 95 6 40 7 35 7 85 8 20 8 75 9 15 9 1000000000 10 10 10 68 11 72 11 58 12 99 12 33 13 44 13 88 14 22 14 77 15 66
Sample Output 4
1000001124
Sample Input 5
1 1
Sample Output 5
1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君は K 個のボールを N 個の箱に分配しようとしています。箱には左から順に箱 1, 箱 2, \ldots, 箱 N と番号が振られており、ボールにもボール 1, ボール 2, \ldots, ボール K と番号が振られています。箱どうし、ボールどうしはそれぞれ区別されます。
分配には以下のルールがあります:
- すべてのボールをいずれかの箱に入れなければならず、各ボールは ちょうど 1 つの箱 に入れられる。
- 各箱には 1 個以上 のボールを入れなければならない(空の箱があってはならない)。
- さらに、M 個の制約条件が与えられる。j 番目 (1 \leq j \leq M) の制約条件は「ボール U_j とボール V_j は 同じ箱に 入れなければならない」というものである。
ルール 3 の制約条件において、同じ箱に入れなければならない関係は推移的に適用されます。すなわち、ボール A とボール B が同じ箱に入る制約があり、ボール B とボール C が同じ箱に入る制約がある場合、ボール A, B, C はすべて同じ箱に入れなければなりません。一般に、制約条件を繰り返し適用することで同じ箱に入ると導かれるボールの組は、すべて同じ箱に入れる必要があります。
ルール 1, 2, 3 をすべて満たすような分配方法の総数を、998244353 で割った余りを求めてください。なお、条件を満たす分配方法が存在しない場合は 0 を出力してください。
ここで、2 つの分配方法が異なるとは、あるボールが存在して、そのボールが入っている箱の番号が一方の分配方法ともう一方の分配方法で異なることを言います。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq U_j < V_j \leq K (1 \leq j \leq M)
- (U_j, V_j) の組はすべて異なる。
- 入力はすべて整数である。
入力
N K M U_1 V_1 U_2 V_2 \vdots U_M V_M
- 1 行目には、箱の数を表す整数 N、ボールの数を表す整数 K、制約条件の数を表す整数 M が、スペース区切りで与えられる。
- 続く M 行のうち j 行目 (1 \leq j \leq M) には、j 番目の制約条件におけるボールの番号 U_j と V_j がスペース区切りで与えられる。M = 0 のとき、この部分は存在しない。
出力
分配方法の総数を 998244353 で割った余りを 1 行で出力せよ。
入力例 1
2 4 1 1 2
出力例 1
6
入力例 2
3 2 0
出力例 2
0
入力例 3
5 10 6 1 2 2 3 4 5 6 7 7 8 1 3
出力例 3
120
入力例 4
8 25 18 1 2 2 3 3 4 1 4 5 6 6 7 8 9 10 11 11 12 12 13 13 14 15 16 17 18 18 19 20 21 22 23 23 24 24 25
出力例 4
40320
入力例 5
1 1 0
出力例 5
1
Score : 400 pts
Problem Statement
Takahashi is trying to distribute K balls into N boxes. The boxes are numbered Box 1, Box 2, \ldots, Box N from left to right, and the balls are numbered Ball 1, Ball 2, \ldots, Ball K. The boxes are distinguishable from each other, and the balls are distinguishable from each other.
The distribution must follow these rules:
- Every ball must be placed in exactly one box, and each ball is placed in exactly one box.
- Each box must contain at least one ball (no box may be empty).
- Additionally, M constraints are given. The j-th constraint (1 \leq j \leq M) states that "Ball U_j and Ball V_j must be placed in the same box."
In the constraints of Rule 3, the "must be in the same box" relationship is applied transitively. That is, if there is a constraint that Ball A and Ball B must be in the same box, and a constraint that Ball B and Ball C must be in the same box, then Balls A, B, and C must all be placed in the same box. In general, any set of balls that can be derived to be in the same box by repeatedly applying the constraints must all be placed in the same box.
Find the total number of distribution methods that satisfy all of Rules 1, 2, and 3, modulo 998244353. If no valid distribution exists, output 0.
Here, two distribution methods are considered different if there exists a ball that is placed in a box with a different number in one distribution method compared to the other.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq U_j < V_j \leq K (1 \leq j \leq M)
- All pairs (U_j, V_j) are distinct.
- All input values are integers.
Input
N K M U_1 V_1 U_2 V_2 \vdots U_M V_M
- The first line contains three space-separated integers: N representing the number of boxes, K representing the number of balls, and M representing the number of constraints.
- The following M lines each contain the j-th constraint (1 \leq j \leq M): the ball numbers U_j and V_j separated by a space. When M = 0, this part does not exist.
Output
Output the total number of distribution methods modulo 998244353 in a single line.
Sample Input 1
2 4 1 1 2
Sample Output 1
6
Sample Input 2
3 2 0
Sample Output 2
0
Sample Input 3
5 10 6 1 2 2 3 4 5 6 7 7 8 1 3
Sample Output 3
120
Sample Input 4
8 25 18 1 2 2 3 3 4 1 4 5 6 6 7 8 9 10 11 11 12 12 13 13 14 15 16 17 18 18 19 20 21 22 23 23 24 24 25
Sample Output 4
40320
Sample Input 5
1 1 0
Sample Output 5
1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 466 点
問題文
高橋君と青木君は、一直線のランニングコースを走っています。このコースにはスタート地点と、その先に N 個の給水所が一列に並んでおり、スタート地点から近い順に給水所 1, 2, \ldots, N と番号が付けられています。給水所 N がこのコースのゴールです。
スタート地点から給水所 1 までの距離、および隣り合う給水所間の距離はすべて 1 です。高橋君は一定の速さ毎秒 V で走り、青木君は一定の速さ毎秒 W で走ります。
高橋君と青木君はそれぞれ、各給水所で立ち止まって給水することがあります。高橋君は給水所 i に到着すると R_i 秒間その場に立ち止まってから走行を再開し、青木君は給水所 i に到着すると S_i 秒間その場に立ち止まってから走行を再開します(0 秒の場合は立ち止まらずそのまま通過します)。ただし、ゴールである給水所 N では到着した時点で走行を終了し、R_N, S_N の値によらず給水のための停止は行いません。ゴールに到着した走者は、その後も観測終了時刻まで給水所 N にいるものとします。
二人は時刻 0 に同時にスタート地点を出発し、それぞれの速さでゴール方向(給水所の番号が増える方向)に向かって走ります。
時刻 t における高橋君のスタート地点からの距離を f(t)、青木君のスタート地点からの距離を g(t) とします。ある走者がもう一方の走者を 追い越す とは、f(t) - g(t) の符号が変わることを指します。厳密には、次のいずれかが発生することです:
- ある時刻 t_0 が存在して、十分小さい正の数 \varepsilon に対し、t_0 - \varepsilon < t < t_0 のとき f(t) < g(t) であり、t_0 < t < t_0 + \varepsilon のとき f(t) > g(t) である。これは高橋君が青木君を追い越したことに相当する。
- ある時刻 t_0 が存在して、十分小さい正の数 \varepsilon に対し、t_0 - \varepsilon < t < t_0 のとき f(t) > g(t) であり、t_0 < t < t_0 + \varepsilon のとき f(t) < g(t) である。これは青木君が高橋君を追い越したことに相当する。
f(t) = g(t) が一定時間続いた後に前後関係が入れ替わる場合も、追い越し 1 回と数えます。一方、f(t) = g(t) となった後に前後関係が入れ替わらず元に戻る場合は追い越しとは数えません。
以下の注意事項があります:
- 時刻 0 の時点では二人は同じ位置(スタート地点)にいます。出発前には前後関係が存在しないため、出発直後に一方が前に出ることは追い越しとは数えません。
- 観測区間の終了時刻は、二人のうち 遅い方 が給水所 N に到着する時刻 T とします。時刻 T ちょうどに前後関係が入れ替わる場合(すなわち上の定義における t_0 = T の場合)は、t_0 より後の状態を観測できないため、追い越しとは数えません。
- 給水所で立ち止まっている走者をもう一方が走行中に通過して前後関係が入れ替わる場合も、上の定義に従い追い越しとして数えます。
二人が時刻 0 にスタート地点を出発してから時刻 T までの間に発生する追い越しの合計回数を求めてください。高橋君が青木君を追い越す場合と、青木君が高橋君を追い越す場合の両方を数えます。
制約
- 1 \leq N \leq 5 \times 10^5
- 1 \leq V \leq 10^9
- 1 \leq W \leq 10^9
- V \neq W
- 0 \leq R_i \leq 10^9 (1 \leq i \leq N)
- 0 \leq S_i \leq 10^9 (1 \leq i \leq N)
- 入力はすべて整数
入力
N V W R_1 R_2 \ldots R_N S_1 S_2 \ldots S_N
- 1 行目には、給水所の数を表す整数 N、高橋君の速さを表す整数 V、青木君の速さを表す整数 W が、スペース区切りで与えられる。
- 2 行目には、高橋君が給水所 i で立ち止まる時間(秒)を表す整数 R_i が N 個、スペース区切りで与えられる。
- 3 行目には、青木君が給水所 i で立ち止まる時間(秒)を表す整数 S_i が N 個、スペース区切りで与えられる。
出力
二人がスタート地点を出発してから両者がともに給水所 N に到着するまでの間に発生する追い越しの合計回数を 1 行で出力せよ。
入力例 1
3 2 1 0 3 0 1 0 0
出力例 1
1
入力例 2
4 1 3 2 0 4 0 0 5 0 0
出力例 2
2
入力例 3
10 5 3 0 4 1 0 6 2 0 3 5 0 2 0 5 1 0 4 3 0 2 0
出力例 3
5
入力例 4
30 7 11 0 8 0 3 12 1 0 6 4 0 10 2 5 0 7 1 0 9 3 0 11 2 6 0 4 8 0 5 1 0 4 0 9 1 0 7 2 5 0 11 3 0 6 2 0 8 1 4 0 10 2 0 7 3 0 9 1 6 0 0
出力例 4
8
入力例 5
1 1000000000 1 1000000000 0
出力例 5
0
Score : 466 pts
Problem Statement
Takahashi and Aoki are running on a straight running course. This course has a start point, and N water stations are aligned in a straight line ahead of it, numbered 1, 2, \ldots, N in increasing order of distance from the start point. Water station N is the goal of this course.
The distance from the start point to water station 1, as well as the distance between any two adjacent water stations, is 1. Takahashi runs at a constant speed of V per second, and Aoki runs at a constant speed of W per second.
Takahashi and Aoki may stop at each water station to hydrate. Upon arriving at water station i, Takahashi stops for R_i seconds before resuming running, and Aoki stops for S_i seconds before resuming running (if the time is 0 seconds, they pass through without stopping). However, upon arriving at the goal, water station N, they finish running immediately and do not stop for hydration, regardless of the values of R_N and S_N. A runner who has reached the goal remains at water station N until the end of the observation period.
Both runners start from the start point simultaneously at time 0 and run towards the goal (the direction in which the water station numbers increase) at their respective speeds.
Let f(t) be Takahashi's distance from the start point at time t, and g(t) be Aoki's distance from the start point at time t. We say that one runner overtakes the other when the sign of f(t) - g(t) changes. Strictly speaking, it refers to the occurrence of either of the following:
- There exists a time t_0 such that, for a sufficiently small positive number \varepsilon, f(t) < g(t) holds for t_0 - \varepsilon < t < t_0, and f(t) > g(t) holds for t_0 < t < t_0 + \varepsilon. This corresponds to Takahashi overtaking Aoki.
- There exists a time t_0 such that, for a sufficiently small positive number \varepsilon, f(t) > g(t) holds for t_0 - \varepsilon < t < t_0, and f(t) < g(t) holds for t_0 < t < t_0 + \varepsilon. This corresponds to Aoki overtaking Takahashi.
If f(t) = g(t) persists for some duration before their relative order changes, this is also counted as 1 overtaking. On the other hand, if f(t) = g(t) occurs but their relative order does not change afterwards, it is not counted as an overtaking.
Please note the following:
- At time 0, both runners are at the same position (the start point). Since there is no relative order before departure, one runner pulling ahead immediately after starting is not counted as an overtaking.
- The end of the observation period is the time T when the slower of the two runners arrives at water station N. If their relative order changes exactly at time T (i.e., t_0 = T in the definition above), it is not counted as an overtaking because the state after t_0 cannot be observed.
- If one runner passes the other who is stopped at a water station, changing their relative order, this is also counted as an overtaking according to the definition above.
Find the total number of overtakings that occur from the time they start at time 0 until time T. Count both the cases where Takahashi overtakes Aoki and where Aoki overtakes Takahashi.
Constraints
- 1 \leq N \leq 5 \times 10^5
- 1 \leq V \leq 10^9
- 1 \leq W \leq 10^9
- V \neq W
- 0 \leq R_i \leq 10^9 (1 \leq i \leq N)
- 0 \leq S_i \leq 10^9 (1 \leq i \leq N)
- All input values are integers.
Input
N V W R_1 R_2 \ldots R_N S_1 S_2 \ldots S_N
- The first line contains three space-separated integers: N, the number of water stations; V, Takahashi's speed; and W, Aoki's speed.
- The second line contains N space-separated integers R_i, representing the time (in seconds) Takahashi stops at water station i.
- The third line contains N space-separated integers S_i, representing the time (in seconds) Aoki stops at water station i.
Output
Print the total number of overtakings that occur from the moment they start until both of them arrive at water station N in a single line.
Sample Input 1
3 2 1 0 3 0 1 0 0
Sample Output 1
1
Sample Input 2
4 1 3 2 0 4 0 0 5 0 0
Sample Output 2
2
Sample Input 3
10 5 3 0 4 1 0 6 2 0 3 5 0 2 0 5 1 0 4 3 0 2 0
Sample Output 3
5
Sample Input 4
30 7 11 0 8 0 3 12 1 0 6 4 0 10 2 5 0 7 1 0 9 3 0 11 2 6 0 4 8 0 5 1 0 4 0 9 1 0 7 2 5 0 11 3 0 6 2 0 8 1 4 0 10 2 0 7 3 0 9 1 6 0 0
Sample Output 4
8
Sample Input 5
1 1000000000 1 1000000000 0
Sample Output 5
0