A - 気温予報の誤差分析

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 233

問題文

高橋君は気象予報士を目指しており、 N 地点の気温を毎日予報する練習をしています。

高橋君は D 日間にわたって各地点の気温を予報し、その結果を記録しました。高橋君はまだ練習を始めたばかりなので、地点 i に対する予報気温は毎日同じ値 T_i 度(摂氏)です。一方、 j 日目の地点 i の実際の気温は A_{i,j} 度でした。

地点 ij 日目における「予報誤差」は、予報気温と実際の気温の差の絶対値 |A_{i,j} - T_i| で定義されます。

高橋君は自分の予報の精度を振り返るため、各地点について D 日間の予報誤差の合計を求めることにしました。具体的には、各地点 i1 \leq i \leq N )について予報誤差の合計

S_i = \sum_{j=1}^{D} |A_{i,j} - T_i|

を計算します。

S_1, S_2, \ldots, S_N の最大値を求めてください。

制約

  • 1 \leq N \leq 10000
  • 1 \leq D \leq 10000
  • N \times D \leq 10^6
  • -50 \leq T_i \leq 501 \leq i \leq N
  • -50 \leq A_{i,j} \leq 501 \leq i \leq N, 1 \leq j \leq D
  • 入力はすべて整数である

入力

N D
T_1 T_2 \ldots T_N
A_{1,1} A_{1,2} \ldots A_{1,D}
A_{2,1} A_{2,2} \ldots A_{2,D}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,D}
  • 1 行目には、地点の数 N と日数 D が、スペース区切りで与えられる。
  • 2 行目には、各地点の予報気温 T_1, T_2, \ldots, T_N が、スペース区切りで与えられる。
  • 3 行目から N + 2 行目には、各地点の実際の気温が与えられる。
  • 2 + i 行目( 1 \leq i \leq N )には、地点 iD 日間の実際の気温 A_{i,1}, A_{i,2}, \ldots, A_{i,D} が、スペース区切りで与えられる。

出力

S_1, S_2, \ldots, S_N の最大値を整数で 1 行に出力してください。


入力例 1

3 4
10 0 -5
12 8 10 15
0 -3 4 1
-10 -5 0 -4

出力例 1

11

入力例 2

2 3
-2 7
-2 -1 -3
10 7 4

出力例 2

6

入力例 3

6 8
-10 0 15 20 -25 5
-10 -8 -12 0 10 -20 -30 -10
0 1 -1 2 -2 3 -3 4
15 15 16 14 30 0 -15 50
20 19 18 21 22 23 24 25
-50 -40 -30 -25 -20 -10 0 10
5 -5 15 -15 25 -25 35 -35

出力例 3

160

入力例 4

12 12
-50 -40 -30 -20 -10 0 10 20 30 40 50 5
-50 -49 -48 -47 -46 -45 -44 -43 -42 -41 -40 -39
-50 -45 -40 -35 -30 -25 -20 -15 -10 -5 0 5
-30 -30 -30 -30 -20 -10 0 10 20 30 40 50
-20 -25 -15 -30 -10 -35 -5 -40 0 -45 5 -50
-10 0 -20 10 -30 20 -40 30 -50 40 50 -10
0 0 0 0 0 0 0 0 0 0 0 0
10 9 8 7 6 5 4 3 2 1 0 -1
20 25 30 35 40 45 50 15 10 5 0 -5
30 20 10 0 -10 -20 -30 -40 -50 40 50 30
40 41 42 43 44 45 46 47 48 49 50 39
-50 -40 -30 -20 -10 0 10 20 30 40 50 50
5 -5 15 -15 25 -25 35 -35 45 -45 50 -50

出力例 4

550

入力例 5

1 1
-50
50

出力例 5

100

Score : 233 pts

Problem Statement

Takahashi aims to become a weather forecaster and is practicing by forecasting the daily temperature at N locations.

Takahashi forecasted the temperature at each location over D days and recorded the results. Since he has just started practicing, his forecasted temperature for location i is the same value T_i degrees (Celsius) every day. On the other hand, the actual temperature at location i on day j was A_{i,j} degrees.

The "forecast error" for location i on day j is defined as the absolute difference between the forecasted temperature and the actual temperature, |A_{i,j} - T_i|.

To review the accuracy of his forecasts, Takahashi decided to find the sum of the forecast errors over the D days for each location. Specifically, for each location i (1 \leq i \leq N), he calculates the total forecast error:

S_i = \sum_{j=1}^{D} |A_{i,j} - T_i|

Find the maximum value among S_1, S_2, \ldots, S_N.

Constraints

  • 1 \leq N \leq 10000
  • 1 \leq D \leq 10000
  • N \times D \leq 10^6
  • -50 \leq T_i \leq 50 (1 \leq i \leq N)
  • -50 \leq A_{i,j} \leq 50 (1 \leq i \leq N, 1 \leq j \leq D)
  • All input values are integers.

Input

N D
T_1 T_2 \ldots T_N
A_{1,1} A_{1,2} \ldots A_{1,D}
A_{2,1} A_{2,2} \ldots A_{2,D}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,D}
  • The first line contains the number of locations N and the number of days D, separated by a space.
  • The second line contains the forecasted temperatures for each location, T_1, T_2, \ldots, T_N, separated by spaces.
  • The third to (N + 2)-th lines contain the actual temperatures for each location.
  • The (2 + i)-th line (1 \leq i \leq N) contains the actual temperatures at location i over the D days, A_{i,1}, A_{i,2}, \ldots, A_{i,D}, separated by spaces.

Output

Print the maximum value among S_1, S_2, \ldots, S_N as an integer in a single line.


Sample Input 1

3 4
10 0 -5
12 8 10 15
0 -3 4 1
-10 -5 0 -4

Sample Output 1

11

Sample Input 2

2 3
-2 7
-2 -1 -3
10 7 4

Sample Output 2

6

Sample Input 3

6 8
-10 0 15 20 -25 5
-10 -8 -12 0 10 -20 -30 -10
0 1 -1 2 -2 3 -3 4
15 15 16 14 30 0 -15 50
20 19 18 21 22 23 24 25
-50 -40 -30 -25 -20 -10 0 10
5 -5 15 -15 25 -25 35 -35

Sample Output 3

160

Sample Input 4

12 12
-50 -40 -30 -20 -10 0 10 20 30 40 50 5
-50 -49 -48 -47 -46 -45 -44 -43 -42 -41 -40 -39
-50 -45 -40 -35 -30 -25 -20 -15 -10 -5 0 5
-30 -30 -30 -30 -20 -10 0 10 20 30 40 50
-20 -25 -15 -30 -10 -35 -5 -40 0 -45 5 -50
-10 0 -20 10 -30 20 -40 30 -50 40 50 -10
0 0 0 0 0 0 0 0 0 0 0 0
10 9 8 7 6 5 4 3 2 1 0 -1
20 25 30 35 40 45 50 15 10 5 0 -5
30 20 10 0 -10 -20 -30 -40 -50 40 50 30
40 41 42 43 44 45 46 47 48 49 50 39
-50 -40 -30 -20 -10 0 10 20 30 40 50 50
5 -5 15 -15 25 -25 35 -35 45 -45 50 -50

Sample Output 4

550

Sample Input 5

1 1
-50
50

Sample Output 5

100
B - 図書館の本の貸し出し

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 333

問題文

高橋君は大学の図書館でアルバイトをしています。この図書館では、学生の学習レベルに応じて借りられる本が制限されています。

図書館には N 人の学生が登録されており、それぞれ 1 から N までの番号が付けられています。学生 i の学習レベルは整数 S_i です。

図書館には M 冊の本があり、それぞれ 1 から M までの番号が付けられています。本 j を借りるために必要な最低学習レベルは整数 T_j です。

学生 i が本 j を借りることができるのは、S_i \geq T_j を満たすときに限ります。

なお、同じ本を複数の学生がそれぞれ借りることができます。すなわち、ある学生が借りられる本の冊数は、他の学生の貸出状況に影響されません。

N 人の学生それぞれについて、その学生が借りることのできる本の冊数を求めてください。すなわち、各 i (1 \leq i \leq N) について、S_i \geq T_j を満たす本 j (1 \leq j \leq M) の個数 C_i を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq T_j \leq 10^9 (1 \leq j \leq M)
  • 入力はすべて整数

入力

N M
S_1 S_2 \ldots S_N
T_1 T_2 \ldots T_M
  • 1 行目には、学生の人数 N と本の冊数 M が、スペース区切りで与えられる。
  • 2 行目には、各学生の学習レベル S_1, S_2, \ldots, S_N が、スペース区切りで与えられる。
  • 3 行目には、各本を借りるために必要な最低学習レベル T_1, T_2, \ldots, T_M が、スペース区切りで与えられる。

出力

C_1
C_2
\vdots
C_N

N 行出力せよ。i 行目 (1 \leq i \leq N) には、学生 i が借りることができる本の冊数 C_i を出力せよ。


入力例 1

3 4
2 1 3
1 2 3 2

出力例 1

3
1
4

入力例 2

5 6
1 2 3 4 5
3 1 4 1 5 2

出力例 2

2
3
4
5
6

入力例 3

8 10
100 500 200 1000 300 50 750 400
150 300 500 100 250 600 800 450 200 350

出力例 3

1
8
3
10
5
0
9
6

Score : 333 pts

Problem Statement

Takahashi works part-time at a university library. At this library, the books a student can borrow are restricted based on their study level.

There are N students registered at the library, numbered from 1 to N. The study level of student i is an integer S_i.

The library has M books, numbered from 1 to M. The minimum study level required to borrow book j is an integer T_j.

Student i can borrow book j if and only if S_i \geq T_j.

Note that the same book can be borrowed by multiple students independently. That is, the number of books a student can borrow is not affected by the borrowing status of other students.

For each of the N students, determine the number of books that student can borrow. Specifically, for each i (1 \leq i \leq N), find the count C_i of books j (1 \leq j \leq M) satisfying S_i \geq T_j.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq T_j \leq 10^9 (1 \leq j \leq M)
  • All inputs are integers

Input

N M
S_1 S_2 \ldots S_N
T_1 T_2 \ldots T_M
  • The first line contains the number of students N and the number of books M, separated by a space.
  • The second line contains the study levels of each student S_1, S_2, \ldots, S_N, separated by spaces.
  • The third line contains the minimum study levels required to borrow each book T_1, T_2, \ldots, T_M, separated by spaces.

Output

C_1
C_2
\vdots
C_N

Output N lines. The i-th line (1 \leq i \leq N) should contain C_i, the number of books that student i can borrow.


Sample Input 1

3 4
2 1 3
1 2 3 2

Sample Output 1

3
1
4

Sample Input 2

5 6
1 2 3 4 5
3 1 4 1 5 2

Sample Output 2

2
3
4
5
6

Sample Input 3

8 10
100 500 200 1000 300 50 750 400
150 300 500 100 250 600 800 450 200 350

Sample Output 3

1
8
3
10
5
0
9
6
C - 高速道路の割引パス

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 366

問題文

高橋君は車で長い高速道路を走ろうとしています。この高速道路には N 個の料金所が 1 から N まで番号順に一列に並んでおり、料金所 i にはアルファベット小文字 1 文字からなる識別コードが割り当てられています。N 個の料金所の識別コードを番号順に並べると、長さ N の文字列 S が得られます。すなわち、Si 文字目が料金所 i の識別コードです。

高橋君は料金所 1 から料金所 N まで、すべての料金所を番号の昇順に通過しなければなりません。通常、料金所を 1 つ通過するごとに所要時間 1 がかかるため、何も工夫しなければ合計所要時間は N です。

しかし、高橋君は割引パスを持っています。割引パスは長さ M の文字列 T で表されます。この割引パスを使うと、連続するちょうど M 個の料金所をまとめて所要時間 1 で通過できる場合があります。割引パスは何度でも使うことができますが、適用する区間同士は重なってはいけません。

具体的には、割引パスの適用条件は次の通りです。連続する M 個の料金所からなる区間、すなわち料金所 l, l+1, \ldots, l+M-11 \leq l \leq N-M+1)について、その識別コードを順に並べた文字列(Sl 文字目から l+M-1 文字目までの部分文字列)が T と一致しているとき、その区間に割引パスを適用できます。適用すると、その M 個の料金所をまとめて所要時間 1 で通過できます(通常なら所要時間 M かかるところが所要時間 1 になります)。

高橋君は割引パスを適用する区間を 0 個以上いくつでも選べますが、選んだどの 2 つの区間も共通する料金所を持ってはいけません。ある区間の末尾の料金所と別の区間の先頭の料金所の番号が連続していること(隣り合うこと)は問題ありません。

割引パスを適用した区間に含まれない料金所は、1 つにつき所要時間 1 をかけて個別に通過します。割引パスを適用した区間の数を k とすると、合計所要時間は k + (N - kM) = N - k(M - 1) です。

高橋君がすべての料金所を通過するのにかかる最小の合計所要時間を求めてください。

制約

  • 1 \leq N \leq 10^6
  • 1 \leq M \leq N
  • N, M は整数
  • S は長さ N の英小文字からなる文字列
  • T は長さ M の英小文字からなる文字列

入力

N M
S
T
  • 1 行目には、料金所の数を表す整数 N と、割引パスの長さを表す整数 M が空白区切りで与えられる。
  • 2 行目には、料金所の識別コードを番号順に並べた長さ N の文字列 S が与えられる。
  • 3 行目には、割引パスを表す長さ M の文字列 T が与えられる。

出力

高橋君がすべての料金所を通過するのにかかる最小の合計所要時間を 1 行で出力せよ。


入力例 1

8 3
abcxxabc
abc

出力例 1

4

入力例 2

5 2
aaaaa
aa

出力例 2

3

入力例 3

30 4
abcaabcaabxabcayzabcaabcaqqqrr
abca

出力例 3

15

入力例 4

100 5
abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde
abcde

出力例 4

20

入力例 5

1 1
a
a

出力例 5

1

Score : 366 pts

Problem Statement

Takahashi is about to drive along a long highway. This highway has N toll gates lined up in a row, numbered from 1 to N, and each toll gate i is assigned an identification code consisting of a single lowercase alphabet letter. When the identification codes of the N toll gates are arranged in order of their numbers, a string S of length N is obtained. That is, the i-th character of S is the identification code of toll gate i.

Takahashi must pass through all toll gates from toll gate 1 to toll gate N in ascending order of their numbers. Normally, passing through each toll gate takes a travel time of 1, so without any optimization, the total travel time is N.

However, Takahashi has a discount pass. The discount pass is represented by a string T of length M. By using this discount pass, it may be possible to pass through exactly M consecutive toll gates together with a travel time of 1. The discount pass can be used any number of times, but the intervals to which it is applied must not overlap.

Specifically, the conditions for applying the discount pass are as follows. For an interval consisting of M consecutive toll gates, namely toll gates l, l+1, \ldots, l+M-1 (1 \leq l \leq N-M+1), if the string formed by arranging their identification codes in order (the substring of S from the l-th character to the (l+M-1)-th character) matches T, then the discount pass can be applied to that interval. When applied, those M toll gates can be passed together with a travel time of 1 (instead of the usual travel time of M).

Takahashi can choose 0 or more intervals to apply the discount pass, but no two chosen intervals may share a common toll gate. It is fine for the last toll gate of one interval and the first toll gate of another interval to have consecutive numbers (i.e., be adjacent).

Toll gates not included in any interval where the discount pass is applied are passed individually, each taking a travel time of 1. If the number of intervals where the discount pass is applied is k, the total travel time is k + (N - kM) = N - k(M - 1).

Find the minimum total travel time for Takahashi to pass through all toll gates.

Constraints

  • 1 \leq N \leq 10^6
  • 1 \leq M \leq N
  • N, M are integers
  • S is a string of length N consisting of lowercase English letters
  • T is a string of length M consisting of lowercase English letters

Input

N M
S
T
  • The first line contains an integer N representing the number of toll gates and an integer M representing the length of the discount pass, separated by a space.
  • The second line contains a string S of length N, which is the identification codes of the toll gates arranged in order of their numbers.
  • The third line contains a string T of length M, representing the discount pass.

Output

Output in one line the minimum total travel time for Takahashi to pass through all toll gates.


Sample Input 1

8 3
abcxxabc
abc

Sample Output 1

4

Sample Input 2

5 2
aaaaa
aa

Sample Output 2

3

Sample Input 3

30 4
abcaabcaabxabcayzabcaabcaqqqrr
abca

Sample Output 3

15

Sample Input 4

100 5
abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde
abcde

Sample Output 4

20

Sample Input 5

1 1
a
a

Sample Output 5

1
D - 都市巡回ラリー

実行時間制限: 3 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

高橋君は N 個の都市を巡るラリーに参加します。都市にはそれぞれ 1 から N の番号が付けられています。

このラリーは 1 日目から K 日目までの K 日間にわたって行われます。高橋君は毎日ちょうど 1 つの都市に滞在します。1 日目には N 個の都市のうち好きな都市を選んで滞在できます。2 日目以降は、前日に滞在していた都市から移動ルートを使って別の都市へ移動し、そこに滞在します。

都市間の移動に使えるルートは M 本あり、いずれも一方通行です。k 番目のルートは都市 a_k から都市 b_k への移動を表し、このルートを使うと都市 a_k に滞在している日の翌日に都市 b_k に滞在できます(逆方向に移動できるとは限りません)。各ルートについて a_k \neq b_k が保証されます。

2 日目以降は、前日の滞在都市から出発する移動ルートが存在する都市にのみ移動できます。同じ都市への自己ループは存在しないため、連続する 2 日間に同じ都市に滞在することはできません。ただし、間に別の都市を挟んで同じ都市に再び滞在することは許されます。

各都市 i にはスコア基礎値 P_i が設定されています。j 日目(1 \leq j \leq K)に都市 i に滞在した場合に得られるスコアは (P_i \times j) \bmod Q です。ここで Q は与えられる正の整数であり、\bmod は非負の剰余を表します。

K 日間の滞在計画とは、長さ K の都市番号の列 c_1, c_2, \ldots, c_K であって、以下の条件をすべて満たすものを指します。

  • c_j1 \leq j \leq K)は 1 以上 N 以下の整数である(同じ都市番号が列中に複数回現れてもよい)。
  • すべての 1 \leq j \leq K - 1 に対して、都市 c_j から都市 c_{j+1} への移動ルートが存在する。

K 日間のスコアの合計、すなわち

\sum_{j=1}^{K} \bigl((P_{c_j} \times j) \bmod Q\bigr)

を最大化してください。すべての有効な滞在計画に対するこの値の最大値を求めてください。

なお、有効な滞在計画が少なくとも 1 つ存在することが保証されます。

制約

  • 1 \leq N \leq 1000
  • 0 \leq M \leq \min(N \times (N - 1),\; 50000)
  • 1 \leq K \leq 1000
  • 1 \leq Q \leq 10^6
  • 0 \leq P_i \leq 10^61 \leq i \leq N
  • 1 \leq a_k, b_k \leq N1 \leq k \leq M
  • a_k \neq b_k1 \leq k \leq M
  • 移動ルートに重複はない(すなわち、(a_k, b_k) はすべて相異なる)
  • 有効な滞在計画が少なくとも 1 つ存在する
  • 入力はすべて整数である

入力

N M K Q
P_1 P_2 \ldots P_N
a_1 b_1
a_2 b_2
\vdots
a_M b_M
  • 1 行目には、都市の数 N、移動ルートの数 M、ラリーの日数 K、スコア計算に用いる正の整数 Q が、スペース区切りで与えられる。
  • 2 行目には、各都市のスコア基礎値 P_1, P_2, \ldots, P_N がスペース区切りで与えられる。
  • 続く M 行のうち k 行目(1 \leq k \leq M)には a_kb_k がスペース区切りで与えられ、都市 a_k から都市 b_k への一方通行の移動ルートが存在することを表す。M = 0 の場合、この部分は存在しない。

出力

K 日間のスコアの合計の最大値を 1 行で出力せよ。


入力例 1

3 3 4 10
2 5 7
1 2
2 3
3 1

出力例 1

24

入力例 2

4 5 3 13
4 9 1 7
1 2
1 3
2 4
3 4
4 1

出力例 2

22

入力例 3

8 14 10 97
3 14 159 26 535 897 932 38
1 2
1 3
2 4
3 4
4 5
5 6
6 4
5 7
7 8
8 5
2 6
3 7
6 8
8 1

出力例 3

606

入力例 4

15 35 25 1000000
0 999999 123456 789012 345678 901234 567890 234567 890123 456789 111111 222222 333333 444444 555555
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 1
1 5
1 8
2 6
2 9
3 1
3 7
4 8
4 12
5 9
5 13
6 10
6 14
7 11
7 15
8 12
8 1
9 13
9 2
10 14
10 3

出力例 4

20223100

入力例 5

1 0 1 1
0

出力例 5

0

Score : 400 pts

Problem Statement

Takahashi is participating in a rally that goes through N cities. The cities are numbered 1 through N.

This rally takes place over K days, from Day 1 to Day K. Takahashi stays in exactly one city each day. On Day 1, he can choose and stay in any of the N cities. From Day 2 onwards, he moves from the city he stayed in on the previous day to another city using a travel route, and stays there.

There are M travel routes available between cities, all of which are one-way. The k-th route represents travel from city a_k to city b_k. Using this route allows him to stay in city b_k on the day after he stayed in city a_k (travel in the reverse direction is not necessarily possible). It is guaranteed that a_k \neq b_k for each route.

From Day 2 onwards, he can only move to a city if there is a travel route starting from the city he stayed in on the previous day. Since there are no self-loops to the same city, he cannot stay in the same city for two consecutive days. However, he is allowed to stay in the same city again with other cities in between.

Each city i has a base score P_i. The score obtained by staying in city i on Day j (1 \leq j \leq K) is (P_i \times j) \bmod Q. Here, Q is a given positive integer, and \bmod denotes the non-negative remainder.

A staying plan for the K days is a sequence of city numbers of length K, c_1, c_2, \ldots, c_K, that satisfies all of the following conditions:

  • Each c_j (1 \leq j \leq K) is an integer between 1 and N, inclusive (the same city number may appear multiple times in the sequence).
  • For all 1 \leq j \leq K - 1, there exists a travel route from city c_j to city c_{j+1}.

Please maximize the total score over the K days, which is:

\sum_{j=1}^{K} \bigl((P_{c_j} \times j) \bmod Q\bigr)

Find the maximum value of this sum over all valid staying plans.

It is guaranteed that at least one valid staying plan exists.

Constraints

  • 1 \leq N \leq 1000
  • 0 \leq M \leq \min(N \times (N - 1),\; 50000)
  • 1 \leq K \leq 1000
  • 1 \leq Q \leq 10^6
  • 0 \leq P_i \leq 10^6 (1 \leq i \leq N)
  • 1 \leq a_k, b_k \leq N (1 \leq k \leq M)
  • a_k \neq b_k (1 \leq k \leq M)
  • There are no duplicate travel routes (i.e., all (a_k, b_k) are distinct).
  • There is at least one valid staying plan.
  • All input values are integers.

Input

N M K Q
P_1 P_2 \ldots P_N
a_1 b_1
a_2 b_2
\vdots
a_M b_M
  • The first line contains the number of cities N, the number of travel routes M, the number of days of the rally K, and the positive integer Q used for score calculation, separated by spaces.
  • The second line contains the base scores of the cities P_1, P_2, \ldots, P_N, separated by spaces.
  • The k-th of the following M lines (1 \leq k \leq M) contains a_k and b_k separated by a space, representing that there is a one-way travel route from city a_k to city b_k. If M = 0, this section is empty.

Output

Print the maximum total score over the K days in a single line.


Sample Input 1

3 3 4 10
2 5 7
1 2
2 3
3 1

Sample Output 1

24

Sample Input 2

4 5 3 13
4 9 1 7
1 2
1 3
2 4
3 4
4 1

Sample Output 2

22

Sample Input 3

8 14 10 97
3 14 159 26 535 897 932 38
1 2
1 3
2 4
3 4
4 5
5 6
6 4
5 7
7 8
8 5
2 6
3 7
6 8
8 1

Sample Output 3

606

Sample Input 4

15 35 25 1000000
0 999999 123456 789012 345678 901234 567890 234567 890123 456789 111111 222222 333333 444444 555555
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 1
1 5
1 8
2 6
2 9
3 1
3 7
4 8
4 12
5 9
5 13
6 10
6 14
7 11
7 15
8 12
8 1
9 13
9 2
10 14
10 3

Sample Output 4

20223100

Sample Input 5

1 0 1 1
0

Sample Output 5

0
E - 水路の整備

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 433

問題文

N 個の集落からなる地域があります。この地域の道路網は木構造になっており、集落 1 が根(中心地)です。各集落 i2 \leq i \leq N)には親集落 P_i があり、集落 i と集落 P_i は道路で直接結ばれています。道路は全部で N - 1 本あります。

それぞれの道路に沿って水路を 1 本ずつ整備することになりました。各水路の工事には基本工事コスト 1 がかかります。さらに、各水路の工事は、その水路に対応する道路が結ぶ 2 つの集落のうちちょうど一方を担当集落として選び、その集落が工事を請け負います。すなわち、集落 i と集落 P_i を結ぶ道路の水路工事について、集落 P_i が担当するか集落 i が担当するかのいずれかを選びます。すべての水路は必ずいずれかの集落に割り当てなければなりません。

ここで、各集落 i が担当しうる水路は、集落 i を端点とする道路に対応する水路のみです。具体的には、集落 i が根でなければ「集落 i と親 P_i を結ぶ辺」の水路、および「集落 i と各子集落を結ぶ辺」の水路が対象です。集落 1(根)には親がないため、子集落との辺の水路のみが対象です。

各集落 i1 \leq i \leq N)には工事許容量 C_i追加コスト係数 W_i が定められています。集落 i が担当することになった水路工事の本数を m_i とします。m_iC_i 以下であれば追加コストは発生しませんが、m_iC_i を超えた場合、超過 1 本ごとに W_i の追加コストが発生します。すなわち、集落 i で発生する追加コストは W_i \times \max(0,\, m_i - C_i) です。

総コストは、すべての水路の基本工事コストの合計(常に N - 1)と、すべての集落で発生する追加コストの和です:

\displaystyle (N - 1) + \sum_{i=1}^{N} W_i \times \max(0,\, m_i - C_i)

すべての水路について担当する集落を最適に決めたとき、総コストの最小値を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \leq i - 12 \leq i \leq N
  • 0 \leq C_i \leq N1 \leq i \leq N
  • 1 \leq W_i \leq 10^91 \leq i \leq N
  • 入力はすべて整数である。

入力

N
P_2 P_3 \ldots P_N
C_1 W_1
C_2 W_2
\vdots
C_N W_N
  • 1 行目には、集落の数 N が与えられる。
  • 2 行目には、集落 2, 3, \ldots, N の親集落を表す P_2, P_3, \ldots, P_N がスペース区切りで与えられる。
  • 続く N 行のうち i 行目(1 \leq i \leq N)には、集落 i の工事許容量 C_i と追加コスト係数 W_i がスペース区切りで与えられる。

出力

総コストの最小値を 1 行で出力せよ。


入力例 1

4
1 1 2
1 3
0 5
2 2
0 4

出力例 1

7

入力例 2

5
1 1 1 1
0 10
1 1
0 7
2 3
0 2

出力例 2

13

入力例 3

12
1 1 2 2 3 3 4 4 6 6 10
1 8
2 3
0 10
1 6
0 2
2 5
1 9
0 4
3 1
1 7
0 3
2 6

出力例 3

13

入力例 4

30
1 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 9 10 10 11 12 13 14 15 16 17 18 20 24
1 100
3 5
0 20
2 7
1 12
3 4
0 50
1 9
2 6
0 30
4 2
1 15
0 11
2 8
1 25
0 3
2 18
1 10
3 1
0 40
1 13
2 5
0 17
1 22
3 6
0 14
2 9
1 16
0 19
4 2

出力例 4

115

入力例 5

2
1
0 1000000000
2 1

出力例 5

1

Score : 433 pts

Problem Statement

There is a region consisting of N settlements. The road network of this region forms a tree structure, with settlement 1 as the root (central hub). Each settlement i (2 \leq i \leq N) has a parent settlement P_i, and settlement i and settlement P_i are directly connected by a road. There are N - 1 roads in total.

It has been decided to develop one waterway along each road. The construction of each waterway incurs a basic construction cost of 1. Furthermore, for each waterway construction, exactly one of the two settlements connected by the corresponding road must be chosen as the responsible settlement, and that settlement undertakes the construction. That is, for the waterway construction along the road connecting settlement i and settlement P_i, either settlement P_i or settlement i is chosen to be responsible. Every waterway must be assigned to one of the settlements.

Here, the waterways that each settlement i can be responsible for are only those corresponding to roads that have settlement i as an endpoint. Specifically, if settlement i is not the root, the eligible waterways are those for "the edge connecting settlement i and its parent P_i" and "the edges connecting settlement i and each of its child settlements." For settlement 1 (the root), which has no parent, only the waterways for edges to its child settlements are eligible.

For each settlement i (1 \leq i \leq N), a construction capacity C_i and an additional cost coefficient W_i are defined. Let m_i be the number of waterway constructions assigned to settlement i. If m_i is at most C_i, no additional cost is incurred. However, if m_i exceeds C_i, an additional cost of W_i is incurred for each excess waterway. That is, the additional cost incurred at settlement i is W_i \times \max(0,\, m_i - C_i).

The total cost is the sum of the basic construction costs of all waterways (always N - 1) plus the sum of additional costs incurred at all settlements:

\displaystyle (N - 1) + \sum_{i=1}^{N} W_i \times \max(0,\, m_i - C_i)

Determine the minimum total cost when the responsible settlement for each waterway is chosen optimally.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \leq i - 1 (2 \leq i \leq N)
  • 0 \leq C_i \leq N (1 \leq i \leq N)
  • 1 \leq W_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers.

Input

N
P_2 P_3 \ldots P_N
C_1 W_1
C_2 W_2
\vdots
C_N W_N
  • The first line gives the number of settlements N.
  • The second line gives P_2, P_3, \ldots, P_N, representing the parent settlements of settlements 2, 3, \ldots, N, separated by spaces.
  • In the following N lines, the i-th line (1 \leq i \leq N) gives the construction capacity C_i and additional cost coefficient W_i of settlement i, separated by a space.

Output

Output the minimum total cost in a single line.


Sample Input 1

4
1 1 2
1 3
0 5
2 2
0 4

Sample Output 1

7

Sample Input 2

5
1 1 1 1
0 10
1 1
0 7
2 3
0 2

Sample Output 2

13

Sample Input 3

12
1 1 2 2 3 3 4 4 6 6 10
1 8
2 3
0 10
1 6
0 2
2 5
1 9
0 4
3 1
1 7
0 3
2 6

Sample Output 3

13

Sample Input 4

30
1 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 9 10 10 11 12 13 14 15 16 17 18 20 24
1 100
3 5
0 20
2 7
1 12
3 4
0 50
1 9
2 6
0 30
4 2
1 15
0 11
2 8
1 25
0 3
2 18
1 10
3 1
0 40
1 13
2 5
0 17
1 22
3 6
0 14
2 9
1 16
0 19
4 2

Sample Output 4

115

Sample Input 5

2
1
0 1000000000
2 1

Sample Output 5

1