A - Zero-Sum Ranges

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 200

問題文

長さ N の整数列 A があります。

A空でない 連続する 部分列であって、その総和が 0 になるものの個数を求めてください。 ただし、ここで数えるのは 部分列の取り出し方 であることに注意してください。 つまり、ある 2 つの部分列が列として同じでも、取り出した位置が異なるならば、それらは別々に数えるものとします。

制約

  • 1 \leq N \leq 2 \times 10^5
  • -10^9 \leq A_i \leq 10^9
  • 入力はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 A_2 ... A_N

出力

A の空でない連続する部分列であって、その総和が 0 になるものの個数を出力せよ。


入力例 1

6
1 3 -4 2 2 -2

出力例 1

3

空でない連続した部分列であって、その総和が 0 になるのは、(1,3,-4), (-4,2,2), (2,-2)3 つです。


入力例 2

7
1 -1 1 -1 1 -1 1

出力例 2

12

この例では、列として同じだが取り出す位置の異なる部分列が複数回数えられています。 例えば、(1,-1)3 回数えられています。


入力例 3

5
1 -2 3 -4 5

出力例 3

0

空でない連続した部分列であって、その総和が 0 になるものはありません。

Score : 200 points

Problem Statement

We have an integer sequence A, whose length is N.

Find the number of the non-empty contiguous subsequences of A whose sums are 0. Note that we are counting the ways to take out subsequences. That is, even if the contents of some two subsequences are the same, they are counted individually if they are taken from different positions.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • -10^9 \leq A_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Find the number of the non-empty contiguous subsequences of A whose sum is 0.


Sample Input 1

6
1 3 -4 2 2 -2

Sample Output 1

3

There are three contiguous subsequences whose sums are 0: (1,3,-4), (-4,2,2) and (2,-2).


Sample Input 2

7
1 -1 1 -1 1 -1 1

Sample Output 2

12

In this case, some subsequences that have the same contents but are taken from different positions are counted individually. For example, three occurrences of (1, -1) are counted.


Sample Input 3

5
1 -2 3 -4 5

Sample Output 3

0

There are no contiguous subsequences whose sums are 0.

B - Find Symmetries

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 500

問題文

すぬけ君は、NN 列からなるマス目状に区切られた盤面を 2 つ持っています。 どちらの盤面についても、上から i 行目、左から j 列目のマスをマス (i,j) と呼ぶことにします。

1 つめの盤面には、すべてのマスに 1 つの英小文字が書かれています。 マス (i,j) に書かれている文字は S_{i,j} です。 2 つめの盤面には、まだ何も書かれていません。

すぬけ君は、次のような手順で 2 つめの盤面に文字を書き込みます。

  • まず、2 つの整数 A, B ( 0 \leq A, B < N ) を選ぶ。

  • すべてのマスに文字を書き込む。 具体的には、2 つめの盤面のマス (i,j) に、1 つめの盤面のマス ( i+A, j+B ) の文字を書き込む。 ここで、N+k 行目と表される行は k 行目を意味する。 同様に、N+k 列目と表される列は k 列目を意味する。

操作後、2 つめの盤面がよい盤面であるとは、すべての i, j ( 1 \leq i, j \leq N ) に対して、 マス (i,j) とマス (j,i) にかかれている文字が等しいことを意味します。

2 つめの盤面がよい盤面になるような整数 A, B ( 0 \leq A, B < N ) の選び方が何通りあるかを求めてください。

制約

  • 1 \leq N \leq 300
  • S_{i,j} ( 1 \leq i, j \leq N ) は英小文字

入力

入力は以下の形式で標準入力から与えられる。

N
S_{1,1}S_{1,2}..S_{1,N}
S_{2,1}S_{2,2}..S_{2,N}
:
S_{N,1}S_{N,2}..S_{N,N}

出力

2 つめの盤面がよい盤面になるような整数 A, B ( 0 \leq A, B < N ) の選び方が何通りあるかを出力せよ。


入力例 1

2
ab
ca

出力例 1

2

A, B の組に対して、2 つめの盤面は図のようになります。 2 つめの盤面がよい盤面となっているのは (A,B) = (0,1)(A,B) = (1,0) のときです。 よって答えは 2 です。


入力例 2

4
aaaa
aaaa
aaaa
aaaa

出力例 2

16

どのように A, B を選んでも、2 つめの盤面はよい盤面になります。


入力例 3

5
abcde
fghij
klmno
pqrst
uvwxy

出力例 3

0

どのように A, B を選んでも、2 つめの盤面はよい盤面になりません。

Score : 500 points

Problem Statement

Snuke has two boards, each divided into a grid with N rows and N columns. For both of these boards, the square at the i-th row from the top and the j-th column from the left is called Square (i,j).

There is a lowercase English letter written in each square on the first board. The letter written in Square (i,j) is S_{i,j}. On the second board, nothing is written yet.

Snuke will write letters on the second board, as follows:

  • First, choose two integers A and B ( 0 \leq A, B < N ).
  • Write one letter in each square on the second board. Specifically, write the letter written in Square ( i+A, j+B ) on the first board into Square (i,j) on the second board. Here, the k-th row is also represented as the (N+k)-th row, and the k-th column is also represented as the (N+k)-th column.

After this operation, the second board is called a good board when, for every i and j ( 1 \leq i, j \leq N ), the letter in Square (i,j) and the letter in Square (j,i) are equal.

Find the number of the ways to choose integers A and B ( 0 \leq A, B < N ) such that the second board is a good board.

Constraints

  • 1 \leq N \leq 300
  • S_{i,j} ( 1 \leq i, j \leq N ) is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

N
S_{1,1}S_{1,2}..S_{1,N}
S_{2,1}S_{2,2}..S_{2,N}
:
S_{N,1}S_{N,2}..S_{N,N}

Output

Print the number of the ways to choose integers A and B ( 0 \leq A, B < N ) such that the second board is a good board.


Sample Input 1

2
ab
ca

Sample Output 1

2

For each pair of A and B, the second board will look as shown below:

The second board is a good board when (A,B) = (0,1) or (A,B) = (1,0), thus the answer is 2.


Sample Input 2

4
aaaa
aaaa
aaaa
aaaa

Sample Output 2

16

Every possible choice of A and B makes the second board good.


Sample Input 3

5
abcde
fghij
klmno
pqrst
uvwxy

Sample Output 3

0

No possible choice of A and B makes the second board good.

C - Painting Machines

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 800

問題文

N 個のマスが横一列に並んでおり、左から右へ 1 から N までの番号がついています。 最初、すべてのマス目は白いです。 また、N-1 台のペイントマシンがあり、1 から N-1 までの番号が付けられています。 ペイントマシン i は、稼働すると、マス i とマス i+1 を黒く塗ります。

すぬけ君は、これらのペイントマシンを、1 台ずつ順番に稼働させます。 すぬけくんがマシンを稼働させる順番は、(1, 2, ..., N-1) の順列 P によって表されます。 これは、i 番目に稼働させるマシンの番号が P_i であることを意味します。

ここで、ある順列 P のスコアは、その順列に従ってマシンを稼働させたとき、 すべてのマスが黒く塗られた状態に初めてなるまでに稼働させたマシンの台数と定義されます。 すぬけ君はまだ順列 P を決めていませんが、スコアに興味があります。 すぬけ君のために、すべての順列についてそのスコアを求め、その総和を求めてください。 ただし、答えは非常に大きくなることがあるので、10^9 +7 で割った余りを求めてください。

制約

  • 2 \leq N \leq 10^6

入力

入力は以下の形式で標準入力から与えられる。

N

出力

すべての順列 P のスコアの総和を 10^9 + 7 で割った余りを出力せよ。


入力例 1

4

出力例 1

16

順列 P としてありうるものは 6 つあります。 この中で、P = (1, 3, 2) または P = (3, 1, 2) のときだけスコアは 2 になり、 それ以外のときはスコアは 3 になります。 よって、求める答えは 2 \times 2 + 3 \times 4 = 16 となります。


入力例 2

2

出力例 2

1

ありうる唯一つの順列は P = (1) で、スコアは 1 です。


入力例 3

5

出力例 3

84

入力例 4

100000

出力例 4

341429644

Score : 800 points

Problem Statement

There are N squares lining up in a row, numbered 1 through N from left to right. Initially, all squares are white. We also have N-1 painting machines, numbered 1 through N-1. When operated, Machine i paints Square i and i+1 black.

Snuke will operate these machines one by one. The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i.

Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P. Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations. Find the sum of the scores over all possible permutations for him. Since this can be extremely large, compute the sum modulo 10^9+7.

Constraints

  • 2 \leq N \leq 10^6

Input

Input is given from Standard Input in the following format:

N

Output

Print the sum of the scores over all possible permutations, modulo 10^9+7.


Sample Input 1

4

Sample Output 1

16

There are six possible permutations as P. Among them, P = (1, 3, 2) and P = (3, 1, 2) have a score of 2, and the others have a score of 3. Thus, the answer is 2 \times 2 + 3 \times 4 = 16.


Sample Input 2

2

Sample Output 2

1

There is only one possible permutation: P = (1), which has a score of 1.


Sample Input 3

5

Sample Output 3

84

Sample Input 4

100000

Sample Output 4

341429644
D - Go Home

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1200

問題文

N 棟のマンションが数直線上に並んでおり、1 から N までの番号がついています。 マンション i は座標 X_i の位置にあります。 また、AtCoder 社は座標 S の位置にあります。 AtCoder 社の社員は皆、N 棟のうちいずれかのマンションに住んでいます。 マンション i に住んでいる社員の人数は P_i です。

いま、AtCoder 社の社員全員が、一斉に帰宅しようとしています。 彼らは仕事で疲れているため、AtCoder 社の所有するバスでの帰宅を望んでいます。 AtCoder 社の所有するバスは 1 台しかありませんが、社員全員を乗せることができます。 このバスは、社員全員を乗せて座標 S から出発し、次のようなルールで動きます。

  • バスに乗っている全員(このバスは自動運転であり、運転手はいないものとする)で、正の方向へ進むか負の方向へ進むかの投票を行う。 各社員 1 票ずつ投票し、棄権は許されない。 その後、得票数の多い方向へ、距離 1 だけ移動する。 なお、得票数が同じ場合は、負の方向へ移動する。 もし移動した後の座標にマンションがあるなら、そこに住む社員全員が降りる。

  • バスに社員が乗っている限り、上の操作を繰り返す。

具体例については、入出力例 1 の説明を参照してください。

バスは、距離 1 進むのにちょうど 1 秒かかります。 また、投票や降車にかかる時間は無視できます。

AtCoder 社の社員は全員、自分がバスを降りるまでの時間を最小化するように投票します。 厳密にいうと、ある投票の際には、各社員は、バスがどちらに動いたとき自分の帰宅が早いか (社員全員が今後同じ戦略に従うとして) 考えます。 各社員は、この情報をもとに最適な選択を行いますが、バスがどちらの方向に動いても帰宅までの時間が変わらないときは、負の方向へ投票します。

この時、バスが AtCoder 社を出発してから、最後に社員がバスを降りるまでにかかる時間が何秒かを求めてください。 なお、マンションの位置、住人の数、バスの位置が与えられたとき、バスがどのように動くかは一意に定まることが証明できます。 また、有限時間にすべての社員が帰宅することも証明できます。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq S \leq 10^9
  • 1 \leq X_1 < X_2 < ... < X_N \leq 10^9
  • X_i \neq S ( 1 \leq i \leq N )
  • 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N )
  • 入力はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

N S
X_1 P_1
X_2 P_2
:
X_N P_N

出力

バスが出発してから最後に社員がバスを降りるまでにかかる時間が何秒かを出力せよ。


入力例 1

3 2
1 3
3 4
4 2

出力例 1

4

最初にバスが負の方向へ動いたとしましょう。 すると、バスの座標は 2 → 1 と変化し、マンション 1 の住人が降車します。 その後のバスの動きは明らかで、正の方向へ移動し続けます。 よって、最初にバスが負の方向へ動く場合は、出発してからバスの座標は 2 → 1 → 2 → 3 → 4 と変化することになり、 マンション 1, 2, 3 の住人の帰宅までの時間はそれぞれ、1 秒, 3 秒, 4 秒となります。

次に、最初にバスが正の方向へ動いたとしましょう。 すると、バスの座標は 2 → 3 と変化し、マンション 2 の住人が降車します。 その後バスは、マンション 1 へ向かって移動し始めます。 なぜなら、マンション 1 の住人がマンション 3 の住人より多いからです。 そして、バスはマンション 1 についた後、マンション 3 へ向かいます。 よって、最初にバスが正の方向へ動く場合は、出発してからバスの座標は 2 → 3 → 2 → 1 → 2 → 3 → 4 と変化することになり、 マンション 1, 2, 3 の住人の帰宅までの時間はそれぞれ、3 秒, 1 秒, 6 秒となります。

以上より、マンション 1, 3 の住人は、最初にバスを負の方向へ動かそうとします。 逆に、マンション 2 の住人は、最初にバスを正の方向へ動かそうとします。 マンション 1, 3 の住人の数を合わせると 3 + 2 = 5 人となり、マンション 2 の住人の数 4 人より多いです。 よって、最初にバスは負の方向へ動き、出発してからバスの座標は 2 → 1 → 2 → 3 → 4 と変化することになります。


入力例 2

6 4
1 10
2 1000
3 100000
5 1000000
6 10000
7 100

出力例 2

21

それぞれのマンションに住む人数が文字通り桁違いなので、バスは常に、バスに乗っている住人の数が最も多いマンションへと向かいます。


入力例 3

15 409904902
94198000 15017
117995501 7656764
275583856 313263626
284496300 356635175
324233841 607
360631781 148
472103717 5224
497641071 34695
522945827 816241
554305668 32
623788284 22832
667409501 124410641
876731548 12078
904557302 291749534
918215789 5

出力例 3

2397671583

Score : 1200 points

Problem Statement

There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i.

All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule:

  • Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off.

  • Repeat the operation above as long as there is one or more employees on the bus.

For a specific example, see Sample Input 1.

The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable.

Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction.

Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq S \leq 10^9
  • 1 \leq X_1 < X_2 < ... < X_N \leq 10^9
  • X_i \neq S ( 1 \leq i \leq N )
  • 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N )
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N S
X_1 P_1
X_2 P_2
:
X_N P_N

Output

Print the number of seconds the bus will take from departure to arrival at the last employees' apartment.


Sample Input 1

3 2
1 3
3 4
4 2

Sample Output 1

4

Assume that the bus moves in the negative direction first. Then, the coordinate of the bus changes from 2 to 1, and the employees living in Apartment 1 get off. The movement of the bus after that is obvious: it just continues moving in the positive direction. Thus, if the bus moves in the negative direction first, the coordinate of the bus changes as 2 → 1 → 2 → 3 → 4 from departure. The time it takes to get home for the employees living in Apartment 1, 2, 3 are 1, 3, 4 seconds, respectively.

Next, assume that the bus moves in the positive direction first. Then, the coordinate of the bus changes from 2 to 3, and the employees living in Apartment 2 get off. Afterwards, the bus starts heading to Apartment 1, because there are more employees living in Apartment 1 than in Apartment 3. Then, after arriving at Apartment 1, the bus heads to Apartment 3. Thus, if the bus moves in the positive direction first, the coordinate of the bus changes as 2 → 3 → 2 → 1 → 2 → 3 → 4 from departure. The time it takes to get home for the employees living in Apartment 1, 2, 3 are 3, 1, 6 seconds, respectively.

Therefore, in the beginning, the employees living in Apartment 1 or 3 will try to move the bus in the negative direction. On the other hand, the employees living in Apartment 2 will try to move the bus in the positive direction in the beginning. There are a total of 3 + 2 = 5 employees living in Apartment 1 and 3 combined, which is more than those living in Apartment 2, which is 4. Thus, the bus will move in the negative direction first, and the coordinate of the bus will change as 2 → 1 → 2 → 3 → 4 from departure.


Sample Input 2

6 4
1 10
2 1000
3 100000
5 1000000
6 10000
7 100

Sample Output 2

21

Since the numbers of employees living in different apartments are literally off by a digit, the bus consistently head to the apartment where the most number of employees on the bus lives.


Sample Input 3

15 409904902
94198000 15017
117995501 7656764
275583856 313263626
284496300 356635175
324233841 607
360631781 148
472103717 5224
497641071 34695
522945827 816241
554305668 32
623788284 22832
667409501 124410641
876731548 12078
904557302 291749534
918215789 5

Sample Output 3

2397671583
E - Inversions

Time Limit: 3 sec / Memory Limit: 256 MB

配点 : 1700

問題文

すぬけ君は、長さ N の整数列 A を持っています。 すぬけ君は、(1, 2, ..., N) の順列 P であって、次の条件を満たすものが好きです。

  • 全ての i ( 1 \leq i \leq N ) について、P_i \leq A_i

すぬけ君は、条件を満たすような順列の転倒数 ( ※ ) に興味を持ちました。 すぬけ君のために、条件を満たすような全ての順列について転倒数を求め、その総和を求めてください。 ただし、答えは非常に大きくなることがあるので、10^9 + 7 で割った余りを求めてください。

注釈

ある長さ N の数列 Z の転倒数とは、整数 i, j ( 1 \leq i < j \leq N ) の組であって、Z_i > Z_j を満たすものの個数を意味します。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq N ( 1 \leq i \leq N )
  • 入力はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 A_2 ... A_N

出力

条件を満たすすべての順列の転倒数の総和を 10^9 + 7 で割った余りを出力せよ。


入力例 1

3
2 3 3

出力例 1

4

条件を満たす順列は (1,2,3), (1,3,2), (2,1,3), (2,3,1)4 つです。 それぞれの転倒数は 0, 1, 1, 2 なので、その総和である 4 を出力します。


入力例 2

6
4 2 5 1 6 3

出力例 2

7

条件を満たす順列は (4,2,5,1,6,3) のみです。 この順列の転倒数は 7 なので、7 を出力します。


入力例 3

5
4 4 4 4 4

出力例 3

0

条件を満たす順列は 1 つもありません。


入力例 4

30
22 30 15 20 10 29 11 29 28 11 26 10 18 28 22 5 29 16 24 24 27 10 21 30 29 19 28 27 18 23

出力例 4

848414012

Score : 1700 points

Problem Statement

Snuke has an integer sequence A whose length is N. He likes permutations of (1, 2, ..., N), P, that satisfy the following condition:

  • P_i \leq A_i for all i ( 1 \leq i \leq N ).

Snuke is interested in the inversion numbers of such permutations. Find the sum of the inversion numbers over all permutations that satisfy the condition. Since this can be extremely large, compute the sum modulo 10^9+7.

Notes

The inversion number of a sequence Z whose length N is the number of pairs of integers i and j ( 1 \leq i < j \leq N ) such that Z_i > Z_j.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq N ( 1 \leq i \leq N )
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the sum of the inversion numbers over all permutations that satisfy the condition.


Sample Input 1

3
2 3 3

Sample Output 1

4

There are four permutations that satisfy the condition: (1,2,3), (1,3,2), (2,1,3) and (2,3,1). The inversion numbers of these permutations are 0, 1, 1 and 2, respectively, for a total of 4.


Sample Input 2

6
4 2 5 1 6 3

Sample Output 2

7

Only one permutation (4,2,5,1,6,3) satisfies the condition. The inversion number of this permutation is 7, so the answer is 7.


Sample Input 3

5
4 4 4 4 4

Sample Output 3

0

No permutation satisfies the condition.


Sample Input 4

30
22 30 15 20 10 29 11 29 28 11 26 10 18 28 22 5 29 16 24 24 27 10 21 30 29 19 28 27 18 23

Sample Output 4

848414012
F - 01 on Tree

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1700

問題文

すぬけ君は、N 頂点からなる根付き木を持っています。 頂点には 1 から N までの番号が振られています。 頂点 1 はこの木の根です。 頂点 i ( 2\leq i \leq N ) の親は頂点 P_i ( P_i < i ) です。 各頂点には、0 または 1 が書かれていて、頂点 i に書かれている数は V_i です。

すぬけ君は、この木の頂点を横一列に並べたいと考えています。 その際、どの頂点についても、その頂点より右側にその頂点の先祖となる頂点がないようにします。

頂点を並べ終えたあと、頂点に書かれている数を頂点の並び順に沿って並べた数列を X とします。 すぬけ君は、X の転倒数 ( ※ ) を最小化したいです。 X の転倒数の最小値を求めてください。

注釈

ある長さ N の数列 Z の転倒数とは、整数 i, j ( 1 \leq i < j \leq N ) の組であって、Z_i > Z_j を満たすものの個数を意味します。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq P_i < i ( 2 \leq i \leq N )
  • 0 \leq V_i \leq 1 ( 1 \leq i \leq N )
  • 入力はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

N
P_2 P_3 ... P_N
V_1 V_2 ... V_N

出力

数列 X の転倒数の最小値を出力せよ。


入力例 1

6
1 1 2 3 3
0 1 1 0 0 0

出力例 1

4

頂点を 1, 3, 5, 6, 2, 4 の順に並べると、X = (0, 1, 0, 0, 1, 0) となり、 転倒数は 4 になります。 これ以上転倒数を小さくすることは出来ないので、4 を出力します。


入力例 2

1

0

出力例 2

0

X = (0) で、転倒数は 0 です。


入力例 3

15
1 2 3 2 5 6 2 2 9 10 1 12 13 12
1 1 1 0 1 1 0 0 1 0 0 1 1 0 0

出力例 3

31

Score : 1700 points

Problem Statement

Snuke has a rooted tree with N vertices, numbered 1 through N. Vertex 1 is the root of the tree, and the parent of Vertex i ( 2\leq i \leq N ) is Vertex P_i ( P_i < i ). There is a number, 0 or 1, written on each vertex. The number written on Vertex i is V_i.

Snuke would like to arrange the vertices of this tree in a horizontal row. Here, for every vertex, there should be no ancestor of that vertex to the right of that vertex.

After arranging the vertices, let X be the sequence obtained by reading the numbers written on the vertices from left to right in the arrangement. Snuke would like to minimize the inversion number of X. Find the minimum possible inversion number of X.

Notes

The inversion number of a sequence Z whose length N is the number of pairs of integers i and j ( 1 \leq i < j \leq N ) such that Z_i > Z_j.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq P_i < i ( 2 \leq i \leq N )
  • 0 \leq V_i \leq 1 ( 1 \leq i \leq N )
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_2 P_3 ... P_N
V_1 V_2 ... V_N

Output

Print the minimum possible inversion number of X.


Sample Input 1

6
1 1 2 3 3
0 1 1 0 0 0

Sample Output 1

4

When the vertices are arranged in the order 1, 3, 5, 6, 2, 4, X will be (0, 1, 0, 0, 1, 0), whose inversion number is 4. It is impossible to have fewer inversions, so the answer is 4.


Sample Input 2

1

0

Sample Output 2

0

X = (0), whose inversion number is 0.


Sample Input 3

15
1 2 3 2 5 6 2 2 9 10 1 12 13 12
1 1 1 0 1 1 0 0 1 0 0 1 1 0 0

Sample Output 3

31