A - Pawn on a Grid

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

上下左右に広がる HW 列のマス目があり、各マスにはコマが置かれているか、何も置かれていないかのどちらかです。

マス目の状態は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H によって表され、
S_ij 文字目が # のとき上から i 行目かつ左から j 列目のマスにはコマが置かれていることを、
S_ij 文字目が . のとき上から i 行目かつ左から j 列目のマスには何も置かれていないことを表しています。

マス目上のマスのうち、コマが置かれているようなものの個数を求めてください。

制約

  • 1\leq H,W \leq 10
  • H,W は整数
  • S_i#. のみからなる長さ W の文字列

入力

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

H W
S_1
S_2
\vdots
S_H

出力

コマが置かれているマスの個数を整数で出力せよ。


入力例 1

3 5
#....
.....
.##..

出力例 1

3
  • 上から 1 行目かつ左から 1 列目のマス
  • 上から 3 行目かつ左から 2 列目のマス
  • 上から 3 行目かつ左から 3 列目のマス

の計 3 つのマスにコマが置かれているため、3 を出力します。


入力例 2

1 10
..........

出力例 2

0

どのマスにもコマは置かれていないため、0 を出力します。


入力例 3

6 5
#.#.#
....#
..##.
####.
..#..
#####

出力例 3

16

Score : 100 points

Problem Statement

There is a grid with H rows from top to bottom and W columns from left to right. Each square has a piece placed on it or is empty.

The state of the grid is represented by H strings S_1, S_2, \ldots, S_H, each of length W.
If the j-th character of S_i is #, the square at the i-th row from the top and j-th column from the left has a piece on it;
if the j-th character of S_i is ., the square at the i-th row from the top and j-th column from the left is empty.

How many squares in the grid have pieces on them?

Constraints

  • 1\leq H,W \leq 10
  • H and W are integers.
  • S_i is a string of length W consisting of # and ..

Input

The input is given from Standard Input in the following format:

H W
S_1
S_2
\vdots
S_H

Output

Print the number of squares with pieces as an integer.


Sample Input 1

3 5
#....
.....
.##..

Sample Output 1

3

The following three squares have pieces on them:

  • the square at the 1-st row from the top and 1-st column from the left;
  • the square at the 3-rd row from the top and 2-nd column from the left;
  • the square at the 3-rd row from the top and 3-rd column from the left.

Thus, 3 should be printed.


Sample Input 2

1 10
..........

Sample Output 2

0

Since no square has a piece on it, 0 should be printed.


Sample Input 3

6 5
#.#.#
....#
..##.
####.
..#..
#####

Sample Output 3

16
B - Power

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

整数 A, B が与えられます。 A^B の値を出力してください。

制約

  • 1 \leq A, B \leq 9
  • 入力はすべて整数

入力

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

A B

出力

答えを出力せよ。


入力例 1

4 3

出力例 1

64

4^3 = 64 であるので、64 を出力します。


入力例 2

5 5

出力例 2

3125

入力例 3

8 1

出力例 3

8

Score : 100 points

Problem Statement

Given integers A and B, print the value A^B.

Constraints

  • 1 \leq A, B \leq 9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

A B

Output

Print the answer.


Sample Input 1

4 3

Sample Output 1

64

4^3 = 64, so 64 should be printed.


Sample Input 2

5 5

Sample Output 2

3125

Sample Input 3

8 1

Sample Output 3

8
C - AtCoder Amusement Park

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

AtCoder 遊園地には K 人乗りのアトラクションがあります。 現在、このアトラクションの待機列には N グループが並んでいます。

先頭から i 番目 (1\leq i\leq N) のグループは A _ i 人組です。 すべての i (1\leq i\leq N) について、A _ i\leq K です。

高橋君はこのアトラクションのスタッフとして、並んでいるグループを次の手順に従って誘導します。

はじめ、アトラクションには誰も誘導されておらず、空席は K 個あります。

  1. 待機列に並んでいるグループがない場合、アトラクションをスタートさせ、誘導を終了する。
  2. アトラクションの空席の数と待機列の先頭に並んでいるグループの人数を比較し、次のどちらかを行う。
    • 待機列の先頭に並んでいるグループの人数よりアトラクションの空席の数のほうが少ない場合、アトラクションをスタートさせる。 スタートしたのち、アトラクションの空席が K 個になる。
    • そうでない場合、待機列の先頭に並んでいるグループを全員アトラクションへ誘導する。 先頭のグループが待機列から取り出され、アトラクションの空席がグループの人数ぶんだけ減少する。
  3. 1 に戻る。

ただし、誘導を開始したあとに追加でグループが並ぶことはないとします。 以上の条件のもとで、この手順が有限回で終了することが示せます。

高橋君が誘導を開始してから誘導を終了するまで、何回アトラクションをスタートさせるか求めてください。

制約

  • 1\leq N\leq100
  • 1\leq K\leq100
  • 1\leq A _ i\leq K\ (1\leq i\leq N)
  • 入力はすべて整数

入力

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

N K
A _ 1 A _ 2 \ldots A _ N

出力

答えを出力せよ。


入力例 1

7 6
2 5 1 4 1 2 3

出力例 1

4

はじめ、7 つのグループは以下のように並んでいます。

高橋君の誘導の様子の一部を以下の図に示します。

  • はじめ、先頭に並んでいるグループは 2 人のグループで、空席は 6 個です。よって、高橋君は先頭のグループをアトラクションに誘導し、空席は 4 個になります。
  • 次に、先頭に並んでいるグループは 5 人のグループで、空席の個数 4 より多いため、アトラクションをスタートさせます。
  • 空席が 6 個になったため、先頭のグループをアトラクションに誘導し、空席は 1 個になります。
  • 次に先頭に並んでいるのは 1 人なので、アトラクションに誘導し、空席は 0 個になります。

すべての誘導が終了するまでに、高橋君は 4 回アトラクションをスタートさせることになります。 よって、4 を出力してください。


入力例 2

7 10
1 10 1 10 1 10 1

出力例 2

7

入力例 3

15 100
73 8 55 26 97 48 37 47 35 55 5 17 62 2 60

出力例 3

8

Score: 200 points

Problem Statement

The AtCoder amusement park has an attraction that can accommodate K people. Now, there are N groups lined up in the queue for this attraction.

The i-th group from the front (1\leq i\leq N) consists of A_i people. For all i (1\leq i\leq N), it holds that A_i \leq K.

Takahashi, as a staff member of this attraction, will guide the groups in the queue according to the following procedure.

Initially, no one has been guided to the attraction, and there are K empty seats.

  1. If there are no groups in the queue, start the attraction and end the guidance.
  2. Compare the number of empty seats in the attraction with the number of people in the group at the front of the queue, and do one of the following:
    • If the number of empty seats is less than the number of people in the group at the front, start the attraction. Then, the number of empty seats becomes K again.
    • Otherwise, guide the entire group at the front of the queue to the attraction. The front group is removed from the queue, and the number of empty seats decreases by the number of people in the group.
  3. Go back to step 1.

Here, no additional groups will line up after the guidance has started. Under these conditions, it can be shown that this procedure will end in a finite number of steps.

Determine how many times the attraction will be started throughout the guidance.

Constraints

  • 1\leq N\leq 100
  • 1\leq K\leq 100
  • 1\leq A_i\leq K\ (1\leq i\leq N)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N K
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

7 6
2 5 1 4 1 2 3

Sample Output 1

4

Initially, the seven groups are lined up as follows:

Part of Takahashi's guidance is shown in the following figure:

  • Initially, the group at the front has 2 people, and there are 6 empty seats. Thus, he guides the front group to the attraction, leaving 4 empty seats.
  • Next, the group at the front has 5 people, which is more than the 4 empty seats, so the attraction is started.
  • After the attraction is started, there are 6 empty seats again, so the front group is guided to the attraction, leaving 1 empty seat.
  • Next, the group at the front has 1 person, so they are guided to the attraction, leaving 0 empty seats.

In total, he starts the attraction four times before the guidance is completed. Therefore, print 4.


Sample Input 2

7 10
1 10 1 10 1 10 1

Sample Output 2

7

Sample Input 3

15 100
73 8 55 26 97 48 37 47 35 55 5 17 62 2 60

Sample Output 3

8
D - chess960

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

高橋君は chess960 と呼ばれるゲームで遊んでいます。 高橋君はランダムに決めた初期配置が chess960 の条件を満たすか確認するプログラムを書くことにしました。

長さ 8 の文字列 S が与えられます。S には K, Q がちょうど 1 文字ずつ、R, B, N がちょうど 2 文字ずつ含まれます。 S が以下の条件を全て満たしているか判定してください。

  • S において左から x,y\ (x < y) 文字目が B であるとする。このとき xy の偶奇が異なる。

  • K2 つの R の間にある。より形式的には、S において左から x,y\ (x < y) 文字目が R であり、 z 文字目が K であるとする。このとき、 x< z < y が成り立つ。

制約

  • S は 長さ 8 の文字列であり、K, Q がちょうど 1 文字ずつ、R, B, N がちょうど 2 文字ずつ含まれる。

入力

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

S

出力

S が条件を満たす場合 Yes を、そうでない場合 No を出力せよ。


入力例 1

RNBQKBNR

出力例 1

Yes

B は左から 3 番目、6 番目にあり、36 は偶奇が異なります。 また、K2 つの R の間にあります。よって条件を満たします。


入力例 2

KRRBBNNQ

出力例 2

No

K2 つの R の間にありません。


入力例 3

BRKRBQNN

出力例 3

No

Score : 200 points

Problem Statement

Takahashi is playing a game called Chess960. He has decided to write a code that determines if a random initial state satisfies the conditions of Chess960.

You are given a string S of length eight. S has exactly one K and Q, and exactly two R's, B's , and N's. Determine if S satisfies all of the following conditions.

  • Suppose that the x-th and y-th (x < y) characters from the left of S are B; then, x and y have different parities.

  • K is between two R's. More formally, suppose that the x-th and y-th (x < y) characters from the left of S are R and the z-th is K; then x< z < y.

Constraints

  • S is a string of length 8 that contains exactly one K and Q, and exactly two R's, B's , and N's.

Input

The input is given from Standard Input in the following format:

S

Output

Print Yes if S satisfies the conditions; print No otherwise.


Sample Input 1

RNBQKBNR

Sample Output 1

Yes

The 3-rd and 6-th characters are B, and 3 and 6 have different parities. Also, K is between the two R's. Thus, the conditions are fulfilled.


Sample Input 2

KRRBBNNQ

Sample Output 2

No

K is not between the two R's.


Sample Input 3

BRKRBQNN

Sample Output 3

No
E - RANDOM

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

#. からなる HW 列の図形 S,T が与えられます。
図形 SH 個の文字列として与えられ、 S_ij 文字目は Sij 列にある要素を表します。 T についても同様です。

S の列を並べ替えて T と等しくできるか判定してください。

但し、図形 X の列を並べ替えるとは、以下の操作を言います。

  • (1,2,\dots,W) の順列 P=(P_1,P_2,\dots,P_W) をひとつ選択する。
  • その後、全ての 1 \le i \le H を満たす整数 i について、以下の操作を同時に行う。
    • 1 \le j \le W を満たす全ての整数 j について同時に、 Xij 列にある要素を iP_j 列にある要素に置き換える。

制約

  • H,W は整数
  • 1 \le H,W
  • 1 \le H \times W \le 4 \times 10^5
  • S_i,T_i#. からなる長さ W の文字列

入力

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

H W
S_1
S_2
\vdots
S_H
T_1
T_2
\vdots
T_H

出力

ST と等しくできるなら Yes 、 そうでないなら No と出力せよ。


入力例 1

3 4
##.#
##..
#...
.###
..##
...#

出力例 1

Yes

例えば S3,4,2,1 列目をこの順に左から並べ替えた場合、 ST と等しくできます。


入力例 2

3 3
#.#
.#.
#.#
##.
##.
.#.

出力例 2

No

この入力では、 ST と等しくすることができません。


入力例 3

2 1
#
.
#
.

出力例 3

Yes

S=T である場合もあります。


入力例 4

8 7
#..#..#
.##.##.
#..#..#
.##.##.
#..#..#
.##.##.
#..#..#
.##.##.
....###
####...
....###
####...
....###
####...
....###
####...

出力例 4

Yes

Score : 300 points

Problem Statement

You are given patterns S and T consisting of # and ., each with H rows and W columns.
The pattern S is given as H strings, and the j-th character of S_i represents the element at the i-th row and j-th column. The same goes for T.

Determine whether S can be made equal to T by rearranging the columns of S.

Here, rearranging the columns of a pattern X is done as follows.

  • Choose a permutation P=(P_1,P_2,\dots,P_W) of (1,2,\dots,W).
  • Then, for every integer i such that 1 \le i \le H, simultaneously do the following.
    • For every integer j such that 1 \le j \le W, simultaneously replace the element at the i-th row and j-th column of X with the element at the i-th row and P_j-th column.

Constraints

  • H and W are integers.
  • 1 \le H,W
  • 1 \le H \times W \le 4 \times 10^5
  • S_i and T_i are strings of length W consisting of # and ..

Input

The input is given from Standard Input in the following format:

H W
S_1
S_2
\vdots
S_H
T_1
T_2
\vdots
T_H

Output

If S can be made equal to T, print Yes; otherwise, print No.


Sample Input 1

3 4
##.#
##..
#...
.###
..##
...#

Sample Output 1

Yes

If you, for instance, arrange the 3-rd, 4-th, 2-nd, and 1-st columns of S in this order from left to right, S will be equal to T.


Sample Input 2

3 3
#.#
.#.
#.#
##.
##.
.#.

Sample Output 2

No

In this input, S cannot be made equal to T.


Sample Input 3

2 1
#
.
#
.

Sample Output 3

Yes

It is possible that S=T.


Sample Input 4

8 7
#..#..#
.##.##.
#..#..#
.##.##.
#..#..#
.##.##.
#..#..#
.##.##.
....###
####...
....###
####...
....###
####...
....###
####...

Sample Output 4

Yes
F - Max - Min Query

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

整数の多重集合 S があります。はじめ S は空です。

Q 個のクエリが与えられるので順に処理してください。 クエリは次の 3 種類のいずれかです。

  • 1 x : Sx1 個追加する。

  • 2 x c : S から x\mathrm{min}(c, (S に含まれる x の個数 )) 個削除する。

  • 3 : (S の最大値 )- (S の最小値 ) を出力する。このクエリを処理するとき、 S が空でないことが保証される。

制約

  • 1 \leq Q \leq 2\times 10^5
  • 0 \leq x \leq 10^9
  • 1 \leq c \leq Q
  • 3 のクエリを処理するとき、S は空でない。
  • 入力は全て整数

入力

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

Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

i 番目のクエリを表す \mathrm{query}_i は以下の 3 種類のいずれかである。

1 x
2 x c
3 

出力

3 のクエリに対する答えを順に改行区切りで出力せよ。


入力例 1

8
1 3
1 2
3
1 2
1 7
3
2 2 3
3

出力例 1

1
5
4

多重集合 S は以下のように変化します。

  • 1 番目のクエリ : S3 を追加する。S\lbrace3 \rbrace となる。
  • 2 番目のクエリ : S2 を追加する。S\lbrace2, 3\rbrace となる。
  • 3 番目のクエリ : S = \lbrace 2, 3\rbrace の最大値は 3 、最小値は 2 なので、 3-2=1 を出力する。
  • 4 番目のクエリ : S2 を追加する。S\lbrace2,2,3 \rbrace となる。
  • 5 番目のクエリ : S7 を追加する。S\lbrace2, 2,3, 7\rbrace となる。
  • 6 番目のクエリ : S = \lbrace2,2,3, 7\rbrace の最大値は 7 、最小値は 2 なので、 7-2=5 を出力する。
  • 7 番目のクエリ : S に含まれる 2 の個数は 2 個なので、 \mathrm{min(2,3)} = 2 個の 2S から削除する。S\lbrace3, 7\rbrace となる。
  • 8 番目のクエリ : S = \lbrace3, 7\rbrace の最大値は 7 、最小値は 3 なので、 7-3=4 を出力する。

入力例 2

4
1 10000
1 1000
2 100 3
1 10

出力例 2


クエリ 3 が含まれない場合、何も出力してはいけません。

Score : 300 points

Problem Statement

We have a multiset of integers S, which is initially empty.

Given Q queries, process them in order. Each query is of one of the following types.

  • 1 x: Insert an x into S.

  • 2 x c: Remove an x from S m times, where m = \mathrm{min}(c,( the number of x's contained in S)).

  • 3 : Print ( maximum value of S)-( minimum value of S). It is guaranteed that S is not empty when this query is given.

Constraints

  • 1 \leq Q \leq 2\times 10^5
  • 0 \leq x \leq 10^9
  • 1 \leq c \leq Q
  • When a query of type 3 is given, S is not empty.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

\mathrm{query}_i, which denotes the i-th query, is in one of the following formats:

1 x
2 x c
3 

Output

Print the answers for the queries of type 3 in the given order, separated by newlines.


Sample Input 1

8
1 3
1 2
3
1 2
1 7
3
2 2 3
3

Sample Output 1

1
5
4

The multiset S transitions as follows.

  • 1-st query: insert 3 into S. S is now \lbrace 3 \rbrace.
  • 2-nd query: insert 2 into S. S is now \lbrace 2, 3 \rbrace.
  • 3-rd query: the maximum value of S = \lbrace 2, 3\rbrace is 3 and its minimum value is 2, so print 3-2=1.
  • 4-th query: insert 2 into S. S is now \lbrace 2,2,3 \rbrace.
  • 5-th query: insert 7 into S. S is now \lbrace 2, 2,3, 7\rbrace.
  • 6-th query: the maximum value of S = \lbrace 2,2,3, 7\rbrace is 7 and its minimum value is 2, so print 7-2=5.
  • 7-th query: since there are two 2's in S and \mathrm{min(2,3)} = 2, remove 2 from S twice. S is now \lbrace 3, 7\rbrace.
  • 8-th query: the maximum value of S = \lbrace 3, 7\rbrace is 7 and its minimum value is 3, so print 7-3=4.

Sample Input 2

4
1 10000
1 1000
2 100 3
1 10

Sample Output 2


If the given queries do not contain that of type 3, nothing should be printed.

G - Do use hexagon grid

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

以下のような、無限に広い六角形のグリッドがあります。最初、全てのマスは白です。

ある六角形のマスは 2 つの整数 i,j を用いて (i,j) と表されます。
マス (i,j) は以下の 6 つのマスと隣接します。

  • (i-1,j-1)
  • (i-1,j)
  • (i,j-1)
  • (i,j+1)
  • (i+1,j)
  • (i+1,j+1)

高橋くんは、 N 個のマス (X_1,Y_1),(X_2,Y_2),\dots,(X_N,Y_N) を黒く塗りました。
黒いマスがいくつの連結成分をなすか求めてください。
ただし、ある 2 つの黒いマスが同じ連結成分に属するとは、この 2 つの黒いマスの間をいくつかの隣接する黒いマスを辿って移動できることを指します。

制約

  • 入力は全て整数
  • 1 \le N \le 1000
  • |X_i|,|Y_i| \le 1000
  • (X_i,Y_i) は相異なる

入力

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

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

答えを整数として出力せよ。


入力例 1

6
-1 -1
0 1
0 2
1 0
1 2
2 0

出力例 1

3

高橋くんがマスを黒く塗った後、グリッドは下図の状態になります。

黒いマスがなす連結成分は以下の 3 つです。

  • (-1,-1)
  • (1,0),(2,0)
  • (0,1),(0,2),(1,2)

入力例 2

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

出力例 2

4

入力例 3

5
2 1
2 -1
1 0
3 1
1 -1

出力例 3

1

Score : 400 points

Problem Statement

We have an infinite hexagonal grid shown below. Initially, all squares are white.

A hexagonal cell is represented as (i,j) with two integers i and j.
Cell (i,j) is adjacent to the following six cells:

  • (i-1,j-1)
  • (i-1,j)
  • (i,j-1)
  • (i,j+1)
  • (i+1,j)
  • (i+1,j+1)

Takahashi has painted N cells (X_1,Y_1),(X_2,Y_2),\dots,(X_N,Y_N) black.
Find the number of connected components formed by the black cells.
Two black cells belong to the same connected component when one can travel between those two black cells by repeatedly moving to an adjacent black cell.

Constraints

  • All values in the input are integers.
  • 1 \le N \le 1000
  • |X_i|,|Y_i| \le 1000
  • The pairs (X_i,Y_i) are distinct.

Input

The input is given from Standard Input in the following format:

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Print the answer as an integer.


Sample Input 1

6
-1 -1
0 1
0 2
1 0
1 2
2 0

Sample Output 1

3

After Takahashi paints cells black, the grid looks as shown below.

The black squares form the following three connected components:

  • (-1,-1)
  • (1,0),(2,0)
  • (0,1),(0,2),(1,2)

Sample Input 2

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

Sample Output 2

4

Sample Input 3

5
2 1
2 -1
1 0
3 1
1 -1

Sample Output 3

1
H - Minimize Sum of Distances

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

N 頂点からなる木が与えられます。頂点は 1 から N までの番号がついており、 i 番目の辺は頂点 A_i, B_i を結んでいます。

長さ N の正整数列 C = (C_1, C_2, \ldots ,C_N) が与えられます。d(a, b) を頂点 a, b の間にある辺の数とし、 x = 1, 2, \ldots ,N に対して \displaystyle f(x) = \sum_{i=1}^{N} (C_i \times d(x, i)) とします。\displaystyle \min_{1 \leq v \leq N} f(v) を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq A_i, B_i \leq N
  • 与えられるグラフは木である。
  • 1 \leq C_i \leq 10^9

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_{N - 1} B_{N - 1}
C_1 C_2 \cdots C_N

出力

答えを一行に出力せよ。


入力例 1

4
1 2
1 3
2 4
1 1 1 2

出力例 1

5

例として、 f(1) を求めることを考えます。d(1, 1) = 0, d(1, 2) = 1, d(1, 3) = 1, d(1, 4) = 2 です。
よって、 f(1) = 0 \times 1 + 1 \times 1 + 1 \times 1 + 2 \times 2 = 6 となります。

同様に、 f(2) = 5, f(3) = 9, f(4) = 6 です。f(2) が最小なので 5 を出力します。


入力例 2

2
2 1
1 1000000000

出力例 2

1

f(2) = 1 で、これが最小です。


入力例 3

7
7 3
2 5
2 4
3 1
3 6
2 1
2 7 6 9 3 4 6

出力例 3

56

Score: 475 points

Problem Statement

You are given a tree with N vertices. The vertices are numbered 1 to N, and the i-th edge connects vertices A_i and B_i.

You are also given a sequence of positive integers C = (C_1, C_2, \ldots ,C_N) of length N. Let d(a, b) be the number of edges between vertices a and b, and for x = 1, 2, \ldots, N, let \displaystyle f(x) = \sum_{i=1}^{N} (C_i \times d(x, i)). Find \displaystyle \min_{1 \leq v \leq N} f(v).

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq A_i, B_i \leq N
  • The given graph is a tree.
  • 1 \leq C_i \leq 10^9

Input

The input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
\vdots
A_{N - 1} B_{N - 1}
C_1 C_2 \cdots C_N

Output

Print the answer in one line.


Sample Input 1

4
1 2
1 3
2 4
1 1 1 2

Sample Output 1

5

For example, consider calculating f(1). We have d(1, 1) = 0, d(1, 2) = 1, d(1, 3) = 1, d(1, 4) = 2.
Thus, f(1) = 0 \times 1 + 1 \times 1 + 1 \times 1 + 2 \times 2 = 6.

Similarly, f(2) = 5, f(3) = 9, f(4) = 6. Since f(2) is the minimum, print 5.


Sample Input 2

2
2 1
1 1000000000

Sample Output 2

1

f(2) = 1, which is the minimum.


Sample Input 3

7
7 3
2 5
2 4
3 1
3 6
2 1
2 7 6 9 3 4 6

Sample Output 3

56
I - Push and Carry

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 525

問題文

座標平面上に高橋君と荷物があります。

高橋君は現在 (X_A,Y_A) におり、荷物は (X_B,Y_B) にあります。 高橋君は荷物を (X_C,Y_C) まで運びたいです。

高橋君は (x,y) にいるとき、1 回の行動で次のいずれかの動きをすることができます。

  • (x+1,y) に移動する。移動前の時点で荷物が (x+1,y) にあった時、荷物を (x+2,y) に移動させる。
  • (x-1,y) に移動する。移動前の時点で荷物が (x-1,y) にあった時、荷物を (x-2,y) に移動させる。
  • (x,y+1) に移動する。移動前の時点で荷物が (x,y+1) にあった時、荷物を (x,y+2) に移動させる。
  • (x,y-1) に移動する。移動前の時点で荷物が (x,y-1) にあった時、荷物を (x,y-2) に移動させる。

荷物を (X_C,Y_C) に移動させるまでに必要な最小の行動回数を求めてください。

制約

  • -10^{17}\leq X_A,Y_A,X_B,Y_B,X_C,Y_C\leq 10^{17}
  • (X_A,Y_A)\neq (X_B,Y_B)
  • (X_B,Y_B)\neq (X_C,Y_C)
  • 入力はすべて整数

入力

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

X_A Y_A X_B Y_B X_C Y_C

出力

荷物を (X_C,Y_C) に移動させるまでに必要な最小の行動回数を出力せよ。


入力例 1

1 2 3 3 0 5

出力例 1

9

高橋君は次のように行動することで 9 回で荷物を (0,5) に運ぶことができます。

  • (2,2) へ移動する。
  • (3,2) へ移動する。
  • (3,3) へ移動する。荷物は (3,4) に移動する。
  • (3,4) へ移動する。荷物は (3,5) に移動する。
  • (4,4) へ移動する。
  • (4,5) へ移動する。
  • (3,5) へ移動する。荷物は (2,5) に移動する。
  • (2,5) へ移動する。荷物は (1,5) に移動する。
  • (1,5) へ移動する。荷物は (0,5) に移動する。

8 回以下で荷物を (0,5) に運ぶことができないので、9 を出力します。


入力例 2

0 0 1 0 -1 0

出力例 2

6

入力例 3

-100000000000000000 -100000000000000000 100000000000000000 100000000000000000 -100000000000000000 -100000000000000000

出力例 3

800000000000000003

Score : 525 points

Problem Statement

Takahashi and a cargo are on a coordinate plane.

Takahashi is currently at (X_A,Y_A), and the cargo is at (X_B,Y_B). He wants to move the cargo to (X_C,Y_C).

When he is at (x,y), he can make one of the following moves in a single action.

  • Move to (x+1,y). If the cargo is at (x+1,y) before the move, move it to (x+2,y).
  • Move to (x-1,y). If the cargo is at (x-1,y) before the move, move it to (x-2,y).
  • Move to (x,y+1). If the cargo is at (x,y+1) before the move, move it to (x,y+2).
  • Move to (x,y-1). If the cargo is at (x,y-1) before the move, move it to (x,y-2).

Find the minimum number of actions required to move the cargo to (X_C,Y_C).

Constraints

  • -10^{17}\leq X_A,Y_A,X_B,Y_B,X_C,Y_C\leq 10^{17}
  • (X_A,Y_A)\neq (X_B,Y_B)
  • (X_B,Y_B)\neq (X_C,Y_C)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

X_A Y_A X_B Y_B X_C Y_C

Output

Print the minimum number of actions required to move the cargo to (X_C,Y_C).


Sample Input 1

1 2 3 3 0 5

Sample Output 1

9

Takahashi can move the cargo to (0,5) in nine actions as follows.

  • Move to (2,2).
  • Move to (3,2).
  • Move to (3,3). The cargo moves to (3,4).
  • Move to (3,4). The cargo moves to (3,5).
  • Move to (4,4).
  • Move to (4,5).
  • Move to (3,5). The cargo moves to (2,5).
  • Move to (2,5). The cargo moves to (1,5).
  • Move to (1,5). The cargo moves to (0,5).

It is impossible to move the cargo to (0,5) in eight or fewer actions, so you should print 9.


Sample Input 2

0 0 1 0 -1 0

Sample Output 2

6

Sample Input 3

-100000000000000000 -100000000000000000 100000000000000000 100000000000000000 -100000000000000000 -100000000000000000

Sample Output 3

800000000000000003