A - AtCoder Quiz 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

AtCoder 王国では、競技プログラミングの実力を測る検定試験が実施されています。

試験は 100 点満点であり、点数が高ければ高いほど、高いランクが認定されます。
ランクは以下のように定められています。

  • 0 点以上 40 点未満のとき、初級
  • 40 点以上 70 点未満のとき、中級
  • 70 点以上 90 点未満のとき、上級
  • 90 点以上のとき、エキスパート

高橋君は、この検定試験を受験し、X 点を取りました。

高橋君が認定されたランクより一つ高いランクとなるためには最低であと何点必要か求めてください。ただし、高橋君がエキスパートと認定された場合、それより高いランクは存在しないため expert と出力してください。

制約

  • 0 \leq X \leq 100
  • X は整数

入力

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

X

出力

答えを出力せよ。


入力例 1

56

出力例 1

14

高橋君は 56 点を取り、中級と認定されました。一つ高いランクである上級となるためには、最低であと 14 点必要です。


入力例 2

32

出力例 2

8

入力例 3

0

出力例 3

40

入力例 4

100

出力例 4

expert

高橋君は満点を取り、エキスパートと認定されました。これより高いランクは存在しないため、expert と出力します。

Score : 100 points

Problem Statement

In the Kingdom of AtCoder, there is a standardized test of competitive programming proficiency.

An examinee will get a score out of 100 and obtain a rank according to the score, as follows:

  • Novice, for a score not less than 0 but less than 40;
  • Intermediate, for a score not less than 40 but less than 70;
  • Advanced, for a score not less than 70 but less than 90;
  • Expert, for a score not less than 90.

Takahashi took this test and got X points.

Find the minimum number of extra points needed to go up one rank higher. If, however, his rank was Expert, print expert, as there is no higher rank than that.

Constraints

  • 0 \leq X \leq 100
  • X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the answer.


Sample Input 1

56

Sample Output 1

14

He got 56 points and was certified as Intermediate. In order to reach the next rank of Advanced, he needs at least 14 more points.


Sample Input 2

32

Sample Output 2

8

Sample Input 3

0

Sample Output 3

40

Sample Input 4

100

Sample Output 4

expert

He got full points and was certified as Expert. There is no rank higher than that, so print expert.

B - Maritozzo

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

英小文字からなる 3 つの文字列 S_1, S_2, S_3 と、123 のみからなる文字列 T が与えられます。

T の各文字に対応する文字列を連結してできる文字列を出力してください。より厳密には、以下の指示にしたがって文字列を出力してください。

  • 1 \leq i \leq |T| を満たす整数 i に対し、文字列 s_i を次のように定める。
    • Ti 文字目が 1 のとき、S_1
    • Ti 文字目が 2 のとき、S_2
    • Ti 文字目が 3 のとき、S_3
  • s_1, s_2, \dots, s_{|T|} をこの順に連結してできる文字列を出力する。

制約

  • 1 \leq |S_1|, |S_2|, |S_3| \leq 10
  • 1 \leq |T| \leq 1000
  • S_1, S_2, S_3 は英小文字からなる。
  • T123 のみからなる。

入力

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

S_1
S_2
S_3
T

出力

答えを出力せよ。


入力例 1

mari
to
zzo
1321

出力例 1

marizzotomari

s_1 = mari, s_2 = zzo, s_3 = to, s_4 = mari であるので、これらを連結してできる文字列である marizzotomari を出力します。


入力例 2

abra
cad
abra
123

出力例 2

abracadabra

入力例 3

a
b
c
1

出力例 3

a

Score : 200 points

Problem Statement

You are given three strings S_1, S_2, S_3 consisting of lowercase English letters, and a string T consisting of 1, 2, 3.

Concatenate the three strings according to the characters in T and print the resulting string. Formally, conform to the following instructions.

  • For each integer i such that 1 \leq i \leq |T|, let the string s_i be defined as follows:
    • S_1, if the i-th character of T is 1;
    • S_2, if the i-th character of T is 2;
    • S_3, if the i-th character of T is 3.
  • Concatenate the strings s_1, s_2, \dots, s_{|T|} in this order and print the resulting string.

Constraints

  • 1 \leq |S_1|, |S_2|, |S_3| \leq 10
  • 1 \leq |T| \leq 1000
  • S_1, S_2, and S_3 consist of lowercase English letters.
  • T consists of 1, 2, and 3.

Input

Input is given from Standard Input in the following format:

S_1
S_2
S_3
T

Output

Print the answer.


Sample Input 1

mari
to
zzo
1321

Sample Output 1

marizzotomari

We have s_1 = mari, s_2 = zzo, s_3 = to, s_4 = mari. Concatenate these and print the resulting string: marizzotomari.


Sample Input 2

abra
cad
abra
123

Sample Output 2

abracadabra

Sample Input 3

a
b
c
1

Sample Output 3

a
C - Neo-lexicographic Ordering

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

AtCoder 王国を治める高橋君は、英小文字のアルファベット順を変更することにしました。

新たなアルファベット順はa , b , \ldots, z を並べ替えて得られる文字列 X を用いて表されます。Xi \, (1 \leq i \leq 26) 文字目は、新たな順番において i 番目に小さい英小文字を表します。

AtCoder 王国には N 人の国民がおり、それぞれの国民の名前は S_1, S_2, \dots, S_N です。ここで、S_i \, (1 \leq i \leq N) は英小文字からなります。
これらの名前を、高橋君の定めたアルファベット順に基づく辞書順に従って並べ替えてください。

辞書順とは?

辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、英小文字からなる相異なる文字列 S, T の大小を判定するアルゴリズムを以下に説明します。

以下では「 Si 文字目の文字」を S_i のように表します。また、 ST より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。

  1. S, T のうち長さが大きくない方の文字列の長さを L とします。i=1,2,\dots,L に対して S_iT_i が一致するか調べます。
  2. S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_jT_j を比較して、S_jT_j よりアルファベット順で小さい場合は S \lt T 、そうでない場合は S \gt T と決定して、アルゴリズムを終了します。
  3. S_i \neq T_i である i が存在しない場合、ST の長さを比較して、ST より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。

制約

  • Xa , b , \ldots, z を並べ替えて得られる
  • 2 \leq N \leq 50000
  • N は整数
  • 1 \leq |S_i| \leq 10 \, (1 \leq i \leq N)
  • S_i は英小文字からなる (1 \leq i \leq N)
  • S_i \neq S_j (1 \leq i \lt j \leq N)

入力

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

X
N
S_1
S_2
\vdots
S_N

出力

N 行出力せよ。i \, (1 \leq i \leq N) 行目には、高橋君の定めたアルファベット順に基づく辞書順に従って国民の名前を並べ替えたとき、i 番目に小さいものを出力すること。


入力例 1

bacdefghijklmnopqrstuvwxzy
4
abx
bzz
bzy
caa

出力例 1

bzz
bzy
abx
caa

高橋君が新たに設定したアルファベット順において、ba より小さく、zy より小さいです。そのため、国民の名前を辞書順に並べ替えると、小さい順に bzz , bzy , abx , caa となります。


入力例 2

zyxwvutsrqponmlkjihgfedcba
5
a
ab
abc
ac
b

出力例 2

b
a
ac
ab
abc

Score : 300 points

Problem Statement

Takahashi, who governs the Kingdom of AtCoder, has decided to change the alphabetical order of English lowercase letters.

The new alphabetical order is represented by a string X, which is a permutation of a, b, \ldots, z. The i-th character of X (1 \leq i \leq 26) would be the i-th smallest English lowercase letter in the new order.

The kingdom has N citizens, whose names are S_1, S_2, \dots, S_N, where each S_i (1 \leq i \leq N) consists of lowercase English letters.
Sort these names lexicographically according to the alphabetical order decided by Takahashi.

What is the lexicographical order?

Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. As a more formal definition, here is the algorithm to determine the lexicographical order between different strings S and T.

Below, let S_i denote the i-th character of S. Also, if S is lexicographically smaller than T, we will denote that fact as S \lt T; if S is lexicographically larger than T, we will denote that fact as S \gt T.

  1. Let L be the smaller of the lengths of S and T. For each i=1,2,\dots,L, we check whether S_i and T_i are the same.
  2. If there is an i such that S_i \neq T_i, let j be the smallest such i. Then, we compare S_j and T_j. If S_j comes earlier than T_j in alphabetical order, we determine that S \lt T and quit; if S_j comes later than T_j, we determine that S \gt T and quit.
  3. If there is no i such that S_i \neq T_i, we compare the lengths of S and T. If S is shorter than T, we determine that S \lt T and quit; if S is longer than T, we determine that S \gt T and quit.

Constraints

  • X is a permutation of a, b, \ldots, z.
  • 2 \leq N \leq 50000
  • N is an integer.
  • 1 \leq |S_i| \leq 10 \, (1 \leq i \leq N)
  • S_i consists of lowercase English letters. (1 \leq i \leq N)
  • S_i \neq S_j (1 \leq i \lt j \leq N)

Input

Input is given from Standard Input in the following format:

X
N
S_1
S_2
\vdots
S_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the i-th smallest name when the citizens' names are sorted according to the alphabetical order decided by Takahashi.


Sample Input 1

bacdefghijklmnopqrstuvwxzy
4
abx
bzz
bzy
caa

Sample Output 1

bzz
bzy
abx
caa

In the new alphabetical order set by Takahashi, b is smaller than a and z is smaller than y. Thus, sorting the citizens' names lexicographically would result in bzz, bzy, abx, caa in ascending order.


Sample Input 2

zyxwvutsrqponmlkjihgfedcba
5
a
ab
abc
ac
b

Sample Output 2

b
a
ac
ab
abc
D - Strange Lunchbox

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

N 種類の弁当が、それぞれ 1 個ずつ売られています。
i = 1, 2, \ldots, N について、i 種類目の弁当には A_i 個のたこ焼きと B_i 個のたい焼きが入っています。

高橋君は、 X 個以上のたこ焼きと Y 個以上のたい焼きを食べたいです。
高橋君がいくつかの弁当を選んで買うことで、 X 個以上のたこ焼きと Y 個以上のたい焼きを手に入れることが可能かどうか判定して下さい。また、可能な場合はそのために高橋君が購入しなければならない弁当の個数の最小値を求めて下さい。

各種類の弁当は 1 個しか売られていないため、同じ種類の弁当を 2 個以上購入することは出来ないことに注意して下さい。

制約

  • 1 \leq N \leq 300
  • 1 \leq X, Y \leq 300
  • 1 \leq A_i, B_i \leq 300
  • 入力はすべて整数

入力

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

N
X Y
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

高橋君が X 個以上のたこ焼きと Y 個以上のたい焼きを手に入れることが不可能な場合は -1 を出力し、 可能な場合はそのために高橋君が購入しなければならない弁当の個数の最小値を出力せよ。


入力例 1

3
5 6
2 1
3 4
2 3

出力例 1

2

高橋君は、5 個以上のたこ焼きと 6 個以上のたい焼きを食べたいです。
高橋君は 2 種類目の弁当と 3 種類目の弁当を買うことで、 たこ焼きを 3 + 2 = 5 個、たい焼きを 4 + 3 = 7 個手に入れることができます。


入力例 2

3
8 8
3 4
2 3
2 1

出力例 2

-1

高橋君がたとえすべての弁当を買ったとしても、高橋君は 8 個以上のたこ焼きと 8 個以上のたい焼きを手に入れることが出来ません。
よって、-1 を出力します。

Score : 400 points

Problem Statement

A shop sells N kinds of lunchboxes, one for each kind.
For each i = 1, 2, \ldots, N, the i-th kind of lunchbox contains A_i takoyaki (octopus balls) and B_i taiyaki (fish-shaped cakes).

Takahashi wants to eat X or more takoyaki and Y or more taiyaki.
Determine whether it is possible to buy some number of lunchboxes to get at least X takoyaki and at least Y taiyaki. If it is possible, find the minimum number of lunchboxes that Takahashi must buy to get them.

Note that just one lunchbox is in stock for each kind; you cannot buy two or more lunchboxes of the same kind.

Constraints

  • 1 \leq N \leq 300
  • 1 \leq X, Y \leq 300
  • 1 \leq A_i, B_i \leq 300
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X Y
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

If Takahashi cannot get at least X takoyaki and at least Y taiyaki, print -1; otherwise, print the minimum number of lunchboxes that he must buy to get them.


Sample Input 1

3
5 6
2 1
3 4
2 3

Sample Output 1

2

He wants to eat 5 or more takoyaki and 6 or more taiyaki.
Buying the second and third lunchboxes will get him 3 + 2 = 5 taiyaki and 4 + 3 = 7 taiyaki.


Sample Input 2

3
8 8
3 4
2 3
2 1

Sample Output 2

-1

Even if he is to buy every lunchbox, it is impossible to get at least 8 takoyaki and at least 8 taiyaki.
Thus, print -1.

E - Moat

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

xy -平面上のいくつかの点に村があります。
高橋君はこれらの村を民兵や魔女などの外敵から守るため、これらのすべての村を囲むようなお堀を建設します。

01 からなる 4 \times 4 行列 A = (A_{i, j}) が与えられます。
A_{i, j} = 1 を満たす整数の組 (i, j) (1 \leq i, j \leq 4) ごとに、座標 (i-0.5, j-0.5) に村があります。

お堀は平面上の多角形です。 高橋君は以下の条件をすべて満たすお堀を建設します(入力例1・出力例1の説明も参考にして下さい)。

  1. 自己交差がない
  2. 内部にすべての村を含む
  3. すべての頂点の x 座標と y 座標は 0 以上 4 以下の整数
  4. すべての辺は x 軸と y 軸のどちらかに平行
  5. それぞれの内角の大きさは 90 度または 270

高橋君が建設するお堀として考えられるものが何通りあるかを出力して下さい。

制約

  • A_{i, j} \in \lbrace 0, 1\rbrace
  • A_{i, j} = 1 となる (i, j) が少なくとも 1 つ存在する

入力

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

A_{1, 1} A_{1, 2} A_{1, 3} A_{1, 4}
A_{2, 1} A_{2, 2} A_{2, 3} A_{2, 4}
A_{3, 1} A_{3, 2} A_{3, 3} A_{3, 4}
A_{4, 1} A_{4, 2} A_{4, 3} A_{4, 4}

出力

高橋君が建設するお堀として考えられるものが何通りあるかを出力せよ。


入力例 1

1 0 0 0
0 0 1 0
0 0 0 0
1 0 0 0

出力例 1

1272

下記の 2 つの画像の例は、高橋君が建設するお堀の条件を満たします

下記の 4 つの画像の例は、高橋君が建設するお堀の条件を満たしません

上記の 4 つの例が高橋君の建設するお堀の条件を満たさない理由は、以下の通りです。

  • 1 つ目の画像の例は、「自己交差がない」という条件を満たしません。
  • 2 つ目の画像の例は、「内部にすべての村を含む」という条件を満たしません。
  • 3 つ目の画像の例は、「すべての頂点の x 座標と y 座標は 0 以上 4 以下の整数」という条件を満たしません。(座標が整数でない頂点があります。)
  • 4 つ目の画像の例は、「すべての辺は x 軸と y 軸のどちらかに平行」という条件を満たしません。

入力例 2

1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

出力例 2

1

Score : 500 points

Problem Statement

There are villages at some number of points in the xy-plane.
Takahashi will construct a moat to protect these villages from enemies such as civil armies and witches.

You are given a 4 \times 4 matrix A = (A_{i, j}) consisting of 0 and 1.
For each pair of integers (i, j) (1 \leq i, j \leq 4) such that A_{i, j} = 1, there is a village at the coordinates (i-0.5, j-0.5).

The moat will be a polygon in the plane. Takahashi will construct it so that the following conditions will be satisfied. (See also the annotation at Sample Input/Output 1.)

  1. There is no self-intersection.
  2. All villages are contained in the interior of the polygon.
  3. The x- and y-coordinates of every vertex are integers between 0 and 4 (inclusive).
  4. Every edge is parallel to the x- or y-axis.
  5. Every inner angle is 90 or 270 degrees.

Print the number of ways in which Takahashi can construct the moat.

Constraints

  • A_{i, j} \in \lbrace 0, 1\rbrace
  • There is at least one pair (i, j) such that A_{i, j} = 1.

Input

Input is given from Standard Input in the following format:

A_{1, 1} A_{1, 2} A_{1, 3} A_{1, 4}
A_{2, 1} A_{2, 2} A_{2, 3} A_{2, 4}
A_{3, 1} A_{3, 2} A_{3, 3} A_{3, 4}
A_{4, 1} A_{4, 2} A_{4, 3} A_{4, 4}

Output

Print the number of ways in which Takahashi can construct the moat.


Sample Input 1

1 0 0 0
0 0 1 0
0 0 0 0
1 0 0 0

Sample Output 1

1272

The two ways to construct the moat shown in the images below are valid.

The four ways to construct the moat shown in the images below are invalid.

Here are the reasons the above ways are invalid.

  • The first way violates the condition: "There is no self-intersection."
  • The second way violates the condition: "All villages are contained in the interior of the polygon."
  • The third way violates the condition: "The x- and y-coordinates of every vertex are integers between 0 and 4." (Some vertices have non-integer coordinates.)
  • The fourth way violates the condition: "Every edge is parallel to the x- or y-axis."

Sample Input 2

1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

Sample Output 2

1
F - Cleaning Robot

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

無限に広がる二次元グリッドのマス (0, 0) に掃除ロボットが置かれています。

このロボットに、4 種類の文字 LRUD からなる文字列で表されたプログラムが与えられます。
ロボットは、与えられたプログラムの各文字を先頭の文字から順に読み、各文字について以下の行動を行います。

  1. ロボットの現在地をマス (x, y) とする
  2. 読んだ文字に応じて以下の通りに移動する:
    • L を読んだとき: マス (x-1, y) に移動する。
    • R を読んだとき: マス (x+1, y) に移動する。
    • U を読んだとき: マス (x, y-1) に移動する。
    • D を読んだとき: マス (x, y+1) に移動する。

LRUD からなる文字列 S が与えられます。 ロボットが実行するプログラムは、文字列 SK 個連接したものです。

ロボットが一度でも存在したことのあるマス(ロボットの初期位置であるマス (0, 0) を含む)は掃除されます。
ロボットがプログラムの実行を終えた後の時点で、掃除されているマスの個数を出力して下さい。

制約

  • SLRUD からなる長さ 1 以上 2 \times 10^5 以下の文字列
  • 1 \leq K \leq 10^{12}

入力

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

S
K

出力

ロボットがプログラムの実行を終えた後の時点で、掃除されているマスの個数を出力せよ。


入力例 1

RDRUL
2

出力例 1

7

ロボットが実行するプログラムは RDRULRDRUL です。ロボットは初期位置であるマス (0, 0) からはじめ、
(0, 0) \rightarrow (1, 0) \rightarrow (1, 1) \rightarrow (2, 1) \rightarrow (2, 0) \rightarrow (1, 0) \rightarrow (2, 0) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 0) \rightarrow (2, 0) と移動します。
その後掃除されているマスは、(0, 0), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1)7 個です。


入力例 2

LR
1000000000000

出力例 2

2

入力例 3

UUURRDDDRRRUUUURDLLUURRRDDDDDDLLLLLLU
31415926535

出力例 3

219911485785

Score : 500 points

Problem Statement

There is a cleaning robot on the square (0, 0) in an infinite two-dimensional grid.

The robot will be given a program represented as a string consisting of four kind of characters L, R, U, D.
It will read the characters in the program from left to right and perform the following action for each character read.

  1. Let (x, y) be the square where the robot is currently on.
  2. Make the following move according to the character read:
    • if L is read: go to (x-1, y).
    • if R is read: go to (x+1, y).
    • if U is read: go to (x, y-1).
    • if D is read: go to (x, y+1).

You are given a string S consisting of L, R, U, D. The program that will be executed by the robot is the concatenation of K copies of S.

Squares visited by the robot at least once, including the initial position (0, 0), will be cleaned.
Print the number of squares that will be cleaned at the end of the execution of the program.

Constraints

  • S is a string of length between 1 and 2 \times 10^5 (inclusive) consisting of L, R, U, D.
  • 1 \leq K \leq 10^{12}

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the number of squares that will be cleaned at the end of the execution of the program.


Sample Input 1

RDRUL
2

Sample Output 1

7

The robot will execute the program RDRULRDRUL. It will start on (0, 0) and travel as follows:
(0, 0) \rightarrow (1, 0) \rightarrow (1, 1) \rightarrow (2, 1) \rightarrow (2, 0) \rightarrow (1, 0) \rightarrow (2, 0) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 0) \rightarrow (2, 0).
In the end, seven squares will get cleaned: (0, 0), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1).


Sample Input 2

LR
1000000000000

Sample Output 2

2

Sample Input 3

UUURRDDDRRRUUUURDLLUURRRDDDDDDLLLLLLU
31415926535

Sample Output 3

219911485785
G - Propagation

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 頂点 M 辺の単純無向グラフが与えられます。N 個の頂点はそれぞれ頂点 1 、頂点 2\ldots 、頂点 N と呼ばれます。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結んでいます。
また、i = 1, 2, \ldots, N について、頂点 i には整数 i が書かれています。

Q 個のクエリが与えられます。
i = 1, 2, \ldots, Q について、i 番目のクエリは整数 x_i で表されます。 i 番目のクエリでは以下の操作をおこないます。

  1. 頂点 x_i に書かれている整数を X とおく。
  2. 頂点 x_i と隣接するすべての頂点について、それに書かれた整数を X に書き換える。

ただし、頂点 u と頂点 v が隣接するとは、頂点 u と頂点 v を結ぶ辺が存在することを言います。

入力で与えられる順にすべてのクエリを処理した後の時点における、各頂点に書かれた整数をそれぞれ出力して下さい。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq \min(2 \times 10^5, N(N-1)/2)
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N
  • 1 \leq x_i \leq N
  • 与えられるグラフは単純である。すなわち、自己ループや多重辺は存在しない。
  • 入力はすべて整数

入力

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

N M Q
u_1 v_1
u_2 v_2
\vdots
u_M v_M
x_1 x_2 \ldots x_Q

出力

すべてのクエリを処理した後の時点における、各頂点に書かれた整数を以下の形式で空白区切りで出力せよ。
ただし、i = 1, 2, \ldots, N について A_i は頂点 i に書かれた整数を表す。

A_1 A_2 \ldots A_N

入力例 1

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

出力例 1

1 3 3 3 3

それぞれのクエリでは以下のような操作が行われます。

  • 1 番目のクエリ (x_1 = 1) : 頂点 1 に書かれた整数は 1 であり、頂点 1 に隣接する頂点は頂点 2 と頂点 5 です。 よって、頂点 2 と頂点 5 に書かれた整数がそれぞれ 1 に書き換えられます。
  • 2 番目のクエリ (x_2 = 3) : 頂点 3 に書かれた整数は 3 であり、頂点 3 に隣接する頂点は頂点 2 と頂点 4 です。よって、頂点 2 と頂点 4 に書かれた整数がそれぞれ 3 に書き換えられます。
  • 3 番目のクエリ (x_3 = 4) : 頂点 4 に書かれた整数は 3 であり、頂点 4 に隣接する頂点は頂点 2 、頂点 3 、頂点 5 です。よって、頂点 2 、頂点 3 、頂点 5 に書かれた整数がそれぞれ 3 に書き換えられます。 (頂点 2 と頂点 3 にはすでに 3 が書かれているので、書かれた整数が実際に変更されるのは頂点 5 のみです。)

入力例 2

14 14 8
7 4
13 9
9 8
4 3
7 2
13 8
12 8
11 3
6 3
7 14
6 5
1 4
10 13
5 2
2 6 12 9 1 10 5 4

出力例 2

1 6 1 1 6 6 1 9 9 10 11 12 10 14

Score : 600 points

Problem Statement

You are given a simple undirected graph with N vertices and M edges. The N vertices are called Vertex 1, Vertex 2, \ldots, Vertex N.
For each i = 1, 2, \ldots, M, the i-th edge connects Vertex u_i and Vertex v_i.
Additionally, for each i = 1, 2, \ldots, N, Vertex i has an integer i written on it.

You are also given Q queries.
For each i = 1, 2, \ldots, Q, the i-th query is represented as an integer x_i. This query involves the following operation.

  1. Let X be the integer written on Vertex x_i.
  2. For every vertex adjacent to Vertex x_i, replace the integer written on it with X.

Here, Vertex u and Vertex v are said to be adjacent when there is an edge connecting them.

Print the integer written on each vertex after all queries are processed in the order they are given from input.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq \min(2 \times 10^5, N(N-1)/2)
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N
  • 1 \leq x_i \leq N
  • The given graph is simple. In other words, it has no self-loops and no multi-edges.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M Q
u_1 v_1
u_2 v_2
\vdots
u_M v_M
x_1 x_2 \ldots x_Q

Output

Print the integers written on the vertices after all queries are processed, in the format below, with spaces in between.
Here, for each i = 1, 2, \ldots, N, A_i denotes the integer written on Vertex i.

A_1 A_2 \ldots A_N

Sample Input 1

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

Sample Output 1

1 3 3 3 3

Each query involves the following operation.

  • The first query (x_1 = 1): Vertex 1 has the integer 1 written on it, and the vertices adjacent to Vertex 1 are Vertices 2 and 5. Thus, the integers on Vertices 2 and 5 get replaced with 1.
  • The second query (x_2 = 3): Vertex 3 has the integer 3 written on it, and the vertices adjacent to Vertex 3 are Vertices 2 and 4. Thus, the integers on Vertices 2 and 4 get replaced with 3.
  • The third query (x_3 = 4): Vertex 4 has the integer 3 written on it, and the vertices adjacent to Vertex 4 are Vertices 2, 3, and 5. Thus, the integers on Vertices 2, 3, and 5 get replaced with 3. (Vertices 2 and 3 already have 3 written on them, so the actual change takes place only on Vertex 5.)

Sample Input 2

14 14 8
7 4
13 9
9 8
4 3
7 2
13 8
12 8
11 3
6 3
7 14
6 5
1 4
10 13
5 2
2 6 12 9 1 10 5 4

Sample Output 2

1 6 1 1 6 6 1 9 9 10 11 12 10 14
H - Candles

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

無限に伸びる数直線の上に N 本のろうそくが置かれています。 i 番目のろうそくは座標 X_i にあり、時刻 0 には長さは A_i で、火がついています。 火のついたろうそくは 1 分あたり長さが 1 短くなり、長さが 0 になると燃え尽きてそれ以降長さは変わりません。また、火を消されたろうそくの長さは変わりません。

高橋君は時刻 0 に座標 0 にいて、毎分 1 以下の距離を移動することができます。高橋君は自分がいる座標と同じ座標にろうそくがある場合、そのろうそくの火を消すことができます。(同じ座標に複数ある場合はまとめて消すことができます。)ここで、ろうそくの火を消すのにかかる時間は無視できます。

高橋君が適切に行動したとき、時刻 0 から 10^{100} 分後に残っているろうそくの長さの総和としてあり得る最大の値を求めてください。

制約

  • 1 \leq N \leq 300
  • -10^9 \leq X_i \leq 10^9
  • 1 \leq A_i \leq 10^9
  • 入力は全て整数である。

入力

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

N
X_1 A_1
X_2 A_2
:
X_N A_N

出力

答えを出力せよ。


入力例 1

3
-2 10
3 10
12 10

出力例 1

11

3 番目のろうそくは座標 12 にあるため、高橋君の行動に関わらず火を消すより先に燃え尽きてしまいます。
残りの 2 本について、まず座標 -22 分かけて移動して 1 本目のろうそくの火を消し、その後 5 分かけて座標 3 へ移動して 2 本目のろうそくの火を消すと、これ以降ろうそくの長さが変化することはありません。 それぞれのろうそくの長さは 10-2=810-2-5=3 残り、このとき残った長さの総和は 8+3=11 となって、このときが最大です。 よって、 11 を出力します。


入力例 2

5
0 1000000000
0 1000000000
1 1000000000
2 1000000000
3 1000000000

出力例 2

4999999994

同じ座標に 2 つ以上のろうそくが存在する可能性があること、答えが 32 bit整数に収まらないことがあることに注意してください。

Score : 600 points

Problem Statement

There are N candles on an infinite number line. The i-th candle is at the coordinate X_i. At time 0, it has a length of A_i and is lit. Each minute, the length of a lit candle decreases by 1. When the length becomes 0, it burns out, and its length does not change after that. Additionally, the length of an unlit candle does not change.

Takahashi is at the coordinate 0 at time 0. Each minute, he can move a distance of at most 1. If there is a candle at his current coordinate, he can put out that candle. (If there are multiple candles there, he can put out all of them.) The time it takes to put out a candle is negligible.

Find the maximum possible total length of the candles remaining at 10^{100} minutes after time 0, achieved by Takahashi's optimal course of actions.

Constraints

  • 1 \leq N \leq 300
  • -10^9 \leq X_i \leq 10^9
  • 1 \leq A_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 A_1
X_2 A_2
:
X_N A_N

Output

Print the answer.


Sample Input 1

3
-2 10
3 10
12 10

Sample Output 1

11

The third candle, which is at coordinate 12, will burn out before Takahashi puts it out, regardless of Takahashi's behavior.
For the other two candles, if he goes to coordinate -2 in two minutes to put out the first and then goes to coordinate 3 in five minutes to put out the second, the lengths of those candles will not change after that. The lengths of those candles remaining are 10-2=8 and 10-2-5=3, for a total of 8+3=11, which is the maximum that can be achieved. Thus, print 11.


Sample Input 2

5
0 1000000000
0 1000000000
1 1000000000
2 1000000000
3 1000000000

Sample Output 2

4999999994

Note that two or more candles may occupy the same coordinate and that the answer may not fit into a 32-bit integer.