A - Scary Fee

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 150 点

問題文

あなたはコワスギ銀行の通帳を持っています。コワスギ銀行の預金通帳には、引き出し額に応じて手数料が変わるという怖すぎる性質があります。

コワスギ銀行の通帳からお金を引き出すには、1\,000 円単位で引き出し額を指定したうえで、引き出し額 1\,000 円あたり C 円の手数料を残高から別途支払う必要があります。銀行の残高が 0 円未満になる引き出しを行うことはできません。

あなたが持っているコワスギ銀行の通帳の残高が X 円のとき、そこから最大で何円を引き出せますか?

制約

  • 1 \leq X \leq 10^7
  • 1 \leq C \leq 999
  • 入力される値はすべて整数

入力

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

X C

出力

答えを出力せよ。


入力例 1

650000 8

出力例 1

644000

644\,000 円を引き出すために必要な手数料は 644\,000 \times \displaystyle\frac{8}{1000} = 5\,152 円になるので、644\,000 + 5\,152 \leq 650\,000 より、644\,000 円は引き出すことができます。

一方で、645\,000 円引き出すために必要な手数料は 645\,000 \times \displaystyle\frac{8}{1000} = 5\,160 円になるので、645\,000 + 5\,160 > 650\,000 より、645\,000 円は引き出すことができません。


入力例 2

1003 4

出力例 2

0

まったくお金が引き出せないこともありえます。


入力例 3

10000000 24

出力例 3

9765000

Score : 150 points

Problem Statement

You have a bankbook from The Terrifying Bank. The deposit passbook of the bank has a terrifyingly scary property that the commission fee changes according to the withdrawal amount.

To withdraw money from the passbook, you need to specify the withdrawal amount in units of 1\,000 yen, and pay a commission fee of C yen per 1\,000 yen of withdrawal amount separately from the balance. Withdrawals are not allowed if they would leave the balance below 0 yen.

When the balance of your passbook from the bank is X yen, what is the maximum amount of money you can withdraw from it?

Constraints

  • 1 \leq X \leq 10^7
  • 1 \leq C \leq 999
  • All input values are integers.

Input

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

X C

Output

Output the answer.


Sample Input 1

650000 8

Sample Output 1

644000

The commission fee required to withdraw 644\,000 yen is 644\,000 \times \displaystyle\frac{8}{1000} = 5\,152 yen, so since 644\,000 + 5\,152 \leq 650\,000, you can withdraw 644\,000 yen.

On the other hand, the commission fee required to withdraw 645\,000 yen is 645\,000 \times \displaystyle\frac{8}{1000} = 5\,160 yen, so since 645\,000 + 5\,160 > 650\,000, you cannot withdraw 645\,000 yen.


Sample Input 2

1003 4

Sample Output 2

0

It is possible that no money can be withdrawn at all.


Sample Input 3

10000000 24

Sample Output 3

9765000
B - Scoreboard

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

チーム高橋とチーム青木が N 回の試合を行いました。 i 回め (1\leq i\leq N) の試合ではチーム高橋が X _ i 点、チーム青木が Y _ i 点を獲得しました。

N 回の試合で獲得した得点の合計がより多いチームの勝ちです。

どちらのチームが勝ったか出力してください。 ただし、獲得した得点の合計が等しい場合は引き分けとなります。

制約

  • 1\leq N\leq 100
  • 0\leq X _ i\leq 100\ (1\leq i\leq N)
  • 0\leq Y _ i\leq 100\ (1\leq i\leq N)
  • 入力はすべて整数

入力

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

N
X _ 1 Y _ 1
X _ 2 Y _ 2
\vdots
X _ N Y _ N

出力

チーム高橋が勝った場合 Takahashi を、チーム青木が勝った場合 Aoki を、引き分けの場合 Draw を出力せよ。


入力例 1

4
10 2
10 1
10 2
3 2

出力例 1

Takahashi

4 回の試合で、チーム高橋は 33 点、チーム青木は 7 点を獲得しました。 チーム高橋が勝ったため、Takahashi を出力してください。


入力例 2

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

出力例 2

Draw

いずれのチームも 22 点を獲得しました。 引き分けなので、Draw を出力してください。


入力例 3

4
0 0
10 10
50 50
0 100

出力例 3

Aoki

一方もしくは両方のチームが、一試合のうちに 1 点も取れない場合もあります。

Score: 100 points

Problem Statement

Team Takahashi and Team Aoki played N matches. In the i-th match (1\leq i\leq N), Team Takahashi scored X _ i points, and Team Aoki scored Y _ i points.

The team with the higher total score from the N matches wins.

Print the winner. If the two teams have the same total score, it is a draw.

Constraints

  • 1\leq N\leq 100
  • 0\leq X _ i\leq 100\ (1\leq i\leq N)
  • 0\leq Y _ i\leq 100\ (1\leq i\leq N)
  • All input values are integers.

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

If Team Takahashi wins, print Takahashi; if Team Aoki wins, print Aoki; if it is a draw, print Draw.


Sample Input 1

4
10 2
10 1
10 2
3 2

Sample Output 1

Takahashi

In four matches, Team Takahashi scored 33 points, and Team Aoki scored 7 points. Team Takahashi wins, so print Takahashi.


Sample Input 2

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

Sample Output 2

Draw

Both teams scored 22 points. It is a draw, so print Draw.


Sample Input 3

4
0 0
10 10
50 50
0 100

Sample Output 3

Aoki

One or both teams may score no points in a match.

C - Broken Rounding

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

非負整数 X に対し、 i=0,1,\dots,K-1 の順に次の操作を行ったとき、操作を全て終えた時点での X を求めてください。

  • X の 10^i の位以下を四捨五入する。
    • 厳密には、 X を「 |Y-X| が最小となる 10^{i+1} の倍数のうち最大のもの」である Y に置き換える。
    • 具体例を挙げる。
      • 273 の 10^1 の位以下を四捨五入すれば 300 となる。
      • 999 の 10^2 の位以下を四捨五入すれば 1000 となる。
      • 100 の 10^9 の位以下を四捨五入すれば 0 となる。
      • 1015 の 10^0 の位以下を四捨五入すれば 1020 となる。

制約

  • X,K は整数
  • 0 \le X < 10^{15}
  • 1 \le K \le 15

入力

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

X K

出力

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


入力例 1

2048 2

出力例 1

2100

操作の過程で、 X は 2048 \rightarrow 2050 \rightarrow 2100 と変化します。


入力例 2

1 15

出力例 2

0

入力例 3

999 3

出力例 3

1000

入力例 4

314159265358979 12

出力例 4

314000000000000

X は 32bit 整数型に収まらない可能性があります。

Score : 200 points

Problem Statement

Given a non-negative integer X, perform the following operation for i=1,2,\dots,K in this order and find the resulting X.

  • Round X off to the nearest 10^i.
    • Formally, replace X with Y that is "the largest multiple of 10^i that minimizes |Y-X|."
    • Here are some examples:
      • Rounding 273 off to the nearest 10^2 yields 300.
      • Rounding 999 off to the nearest 10^3 yields 1000.
      • Rounding 100 off to the nearest 10^{10} yields 0.
      • Rounding 1015 off to the nearest 10^1 yields 1020.

Constraints

  • X and K are integers.
  • 0 \le X < 10^{15}
  • 1 \le K \le 15

Input

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

X K

Output

Print the answer as an integer.


Sample Input 1

2048 2

Sample Output 1

2100

X changes as 2048 \rightarrow 2050 \rightarrow 2100 by the operations.


Sample Input 2

1 15

Sample Output 2

0

Sample Input 3

999 3

Sample Output 3

1000

Sample Input 4

314159265358979 12

Sample Output 4

314000000000000

X may not fit into a 32-bit integer type.

D - Longest Segment

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

二次元平面上に N 個の点があります。i 個目の点の座標は (x_i,y_i) です。

この中から 2 個の点を選ぶとき、それらを結ぶ線分の長さの最大値を求めてください。

制約

  • 2 \leq N \leq 100
  • -1000 \leq x_i,y_i \leq 1000
  • (x_i,y_i) \neq (x_j,y_j)\ (i \neq j)
  • 入力はすべて整数

入力

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

N
x_1 y_1
x_2 y_2
\hspace{0.4cm} \vdots
x_N y_N

出力

2 点を結ぶ線分の長さの最大値を出力せよ。

想定解との絶対誤差または相対誤差が 10^{-6} 以下であれば正解とみなされる。


入力例 1

3
0 0
0 1
1 1

出力例 1

1.4142135624

1 個目の点と 3 個目の点を選んだときそれらを結ぶ線分の長さは \sqrt 2 = 1.41421356237\dots となり、これが最大です。


入力例 2

5
315 271
-2 -621
-205 -511
-952 482
165 463

出力例 2

1455.7159750446

Score : 200 points

Problem Statement

There are N points in a two-dimensional plane. The coordinates of the i-th point are (x_i,y_i).

Find the maximum length of a segment connecting two of these points.

Constraints

  • 2 \leq N \leq 100
  • -1000 \leq x_i,y_i \leq 1000
  • (x_i,y_i) \neq (x_j,y_j)\ (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
\hspace{0.4cm} \vdots
x_N y_N

Output

Print the maximum length of a segment connecting two of the points.

Your answer will be considered correct when the absolute or relative error from the judge's answer is at most 10^{-6}.


Sample Input 1

3
0 0
0 1
1 1

Sample Output 1

1.4142135624

For the 1-st and 3-rd points, the length of the segment connecting them is \sqrt 2 = 1.41421356237\dots, which is the maximum length.


Sample Input 2

5
315 271
-2 -621
-205 -511
-952 482
165 463

Sample Output 2

1455.7159750446
E - Variety

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

N 個の宝石があります。i 番目の宝石の色(整数で表されます)は C_i で価値は V_i です。

この N 個の宝石の中から K 個を選びます。ただし、選んだ宝石の色が M 種類以上なければなりません。

このとき、選んだ宝石の価値の総和としてありうる最大値を求めてください。(与えられる入力では、このような選択が必ず可能です。)

制約

  • 1 \leq M \leq K \leq N \leq 2 \times 10^5
  • 1 \leq C_i \leq N
  • 1 \leq V_i \leq 10^9
  • M 種類以上の色の宝石が存在する
  • 入力される値はすべて整数

入力

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

N K M
C_1 V_1
C_2 V_2
\vdots
C_N V_N

出力

選んだ宝石の価値の総和としてありうる最大値を整数として出力せよ。


入力例 1

5 3 2
1 30
1 40
1 50
2 10
3 20

出力例 1

110

この例では 5 個の宝石から 3 個を選びます。選んだ宝石の色が 2 種類以上なければなりません。

宝石 2, 3, 5 を選ぶと、それらの色は 1, 1, 3 で 2 種類あります。それらの価値の総和は 40 + 50 + 20 = 110 で、これがありうる最大値です。


入力例 2

5 3 3
1 30
1 40
1 50
2 10
3 20

出力例 2

80

宝石や選ぶ個数は入力例 1 と同じですが、選んだ宝石の色が 3 種類以上なければなりません。

宝石 3, 4, 5 を選ぶと、それらの色は 1, 2, 3 で 3 種類あります。それらの価値の総和は 50 + 10 + 20 = 80 で、これがありうる最大値です。


入力例 3

5 5 1
4 1000000000
5 1000000000
4 1000000000
5 1000000000
4 1000000000

出力例 3

5000000000

オーバーフローに注意してください。

Score : 300 points

Problem Statement

There are N gems. The color (represented as an integer) of the i-th gem is C_i and its value is V_i.

Choose K gems from these N gems. Here, the chosen gems must have at least M distinct colors.

Find the maximum possible total value of the chosen gems. (Such a choice is always possible in the given input.)

Constraints

  • 1 \leq M \leq K \leq N \leq 2 \times 10^5
  • 1 \leq C_i \leq N
  • 1 \leq V_i \leq 10^9
  • There exist gems of at least M distinct colors.
  • All input values are integers.

Input

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

N K M
C_1 V_1
C_2 V_2
\vdots
C_N V_N

Output

Output the maximum possible total value of the chosen gems as an integer.


Sample Input 1

5 3 2
1 30
1 40
1 50
2 10
3 20

Sample Output 1

110

In this sample, choose three gems from five gems. The chosen gems must have at least two distinct colors.

Choosing gems 2, 3, 5 gives colors 1, 1, 3, which are two distinct colors. Their total value is 40 + 50 + 20 = 110, and this is the maximum possible value.


Sample Input 2

5 3 3
1 30
1 40
1 50
2 10
3 20

Sample Output 2

80

The gems and the number to choose are the same as in Sample Input 1, but the chosen gems must have at least three distinct colors.

Choosing gems 3, 4, 5 gives colors 1, 2, 3, which are three distinct colors. Their total value is 50 + 10 + 20 = 80, and this is the maximum possible value.


Sample Input 3

5 5 1
4 1000000000
5 1000000000
4 1000000000
5 1000000000
4 1000000000

Sample Output 3

5000000000

Beware of overflow.

F - Route Map

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

AtCoder 鉄道のとある路線には N 個の駅が存在し、始点から終点に向かって i \, (1 \leq i \leq N) 番目の駅の名前は S_i です。

普通列車は全ての駅に止まりますが、急行列車は全ての駅に止まるとは限りません。具体的には、急行列車は M \, (M \leq N) 個の駅にのみ止まり、j \, (1 \leq j \leq M) 番目に止まる駅の名前は T_j です。
ただし、T_1 = S_1 かつ T_M = S_N、すなわち急行列車は始点と終点の両方に止まることが保証されます。

N 個の駅それぞれについて、その駅に急行列車が止まるかどうか判定してください。

制約

  • 2 \leq M \leq N \leq 10^5
  • N, M は整数
  • S_i \, (1 \leq i \leq N) は英小文字のみからなる 1 文字以上 10 文字以下の文字列
  • S_i \neq S_j \, (i \neq j)
  • T_1 = S_1 かつ T_M = S_N
  • (T_1, \dots, T_M) は (S_1, \dots, S_N) から 0 個以上の文字列を選んで取り除き、残った文字列を元の順序で並べることで得られる

入力

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

N M
S_1 \ldots S_N
T_1 \ldots T_M

出力

N 行出力せよ。i \, (1 \leq i \leq N) 行目には、始点から終点に向かって i 番目の駅に急行列車が止まるなら Yes、そうでないなら No と出力せよ。


入力例 1

5 3
tokyo kanda akiba okachi ueno
tokyo akiba ueno

出力例 1

Yes
No
Yes
No
Yes

入力例 2

7 7
a t c o d e r
a t c o d e r

出力例 2

Yes
Yes
Yes
Yes
Yes
Yes
Yes

急行列車が全ての駅に止まることもあります。

Score : 300 points

Problem Statement

There are N stations on a certain line operated by AtCoder Railway. The i-th station (1 \leq i \leq N) from the starting station is named S_i.

Local trains stop at all stations, while express trains may not. Specifically, express trains stop at only M \, (M \leq N) stations, and the j-th stop (1 \leq j \leq M) is the station named T_j.
Here, it is guaranteed that T_1 = S_1 and T_M = S_N, that is, express trains stop at both starting and terminal stations.

For each of the N stations, determine whether express trains stop at that station.

Constrains

  • 2 \leq M \leq N \leq 10^5
  • N and M are integers.
  • S_i (1 \leq i \leq N) is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
  • S_i \neq S_j \, (i \neq j)
  • T_1 = S_1 and T_M = S_N.
  • (T_1, \dots, T_M) is obtained by removing zero or more strings from (S_1, \dots, S_N) and lining up the remaining strings without changing the order.

Input

Input is given from Standard Input in the following format:

N M
S_1 \ldots S_N
T_1 \ldots T_M

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain Yes if express trains stop at the i-th station from the starting station, and No otherwise.


Sample Input 1

5 3
tokyo kanda akiba okachi ueno
tokyo akiba ueno

Sample Output 1

Yes
No
Yes
No
Yes

Sample Input 2

7 7
a t c o d e r
a t c o d e r

Sample Output 2

Yes
Yes
Yes
Yes
Yes
Yes
Yes

Express trains may stop at all stations.

G - Escape Route

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

ある日、映画館を訪れた高橋君は、館内の全ての床に、最寄りの非常口の方向を示す矢印を描くことにしました。

縦 H マス、横 W マスのグリッドが与えられます。グリッドの上から i 行目、左から j 列目のマスを (i, j) と呼びます。
各マスの状態は、次のいずれかの文字 S_{i,j} で表されます。

  • . : 通路マス
  • # : 壁マス
  • E : 非常口

任意の通路マス (i, j) に対して、(i, j) と最寄りの非常口との距離 d(i, j) を次のように定義します。

  • (i, j) からスタートして、上下左右に隣接する 壁マスでないマス へ移動することを繰り返して非常口に到達する際の、必要な最小の移動回数。

ここで、入力として与えられるグリッドにおいて、全ての通路マスに対し d(i, j) が定義可能であることが保証されます。すなわち、すべての通路マス (i, j) において、通路マスのみを経由することで到達可能な非常口が少なくとも 1 つ存在します。

このとき、以下の条件を満たすように、すべての通路マスに対して「上下左右いずれかの方向の矢印」を書き込んでください。

  • 各通路マス (i, j) について、(i,j) にいる状態から開始して、以下の操作を d(i, j) 回行うことで非常口に到達することができる。
    • 現在いるマスに書かれた矢印の方向に従って 1 マス移動する。(ただし、壁マスやグリッドの外へは移動できない。)

制約

  • 2 \leq H \leq 1000
  • 2 \leq W \leq 1000
  • S_{i,j} は ., # または E
  • 全ての通路マス (i, j) について、そこからいずれかの非常口に到達することが可能である
  • H, W は整数である

入力

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

H W
S_{1,1}S_{1,2}\dots S_{1,W}
S_{2,1}S_{2,2}\dots S_{2,W}
\vdots
S_{H,1}S_{H,2}\dots S_{H,W}

出力

矢印を書き込んだ後のマス (i, j) の状態を T_{i,j} とする。T_{i,j} は次のいずれかである。

  • ^ : 上矢印が書かれた通路マス
  • v : 下矢印が書かれた通路マス
  • < : 左矢印が書かれた通路マス
  • > : 右矢印が書かれた通路マス
  • # : 壁マス
  • E : 非常口

これらを以下の形式で出力せよ。

T_{1,1}T_{1,2}\dots T_{1,W}
T_{2,1}T_{2,2}\dots T_{2,W}
\vdots
T_{H,1}T_{H,2}\dots T_{H,W}

答えが複数ある場合は、どれを出力しても正答とみなされる。


入力例 1

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

出力例 1

>>>E
^#>^
>>>^

出力例の (2, 3) について問題文の条件が成立することを確認します。(2, 3) と最寄りの非常口の距離は 2 です。そして、出力例のように書き込まれた矢印に従って移動することで 2 回の移動で非常口に到達することができます。
他の通路マスについても問題文の条件が成立することが確認できます。


入力例 2

3 2
##
##
##

出力例 2

##
##
##

通路マスおよび非常口が存在しない場合もあります。


入力例 3

7 20
....................
..#..#..####..#E##..
..#..#..#..#..#.....
..E###..#..#..####..
.....#..#..E.....#..
.....#..####..####..
....................

出力例 3

>v<<<<<>>>>>>>>v<<<<
>v#^<#^^####v^#E##vv
>v#^<#v^#>v#vv#^<<<<
>>E###vv#>v#vv####^<
>>^<<#vv#>>E<<<<<#^<
>>^<<#vv####^<####^<
>>^<<<<<>>>>^<<<<<^<

Score : 400 points

Problem Statement

One day, Takahashi visited a cinema and decided to draw arrows on every floor tile, each pointing toward the nearest emergency exit.

You are given a grid with H rows and W columns. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.
Each cell is represented by one of the following characters S_{i,j}:

  • . : corridor cell
  • # : wall cell
  • E : emergency exit

For any corridor cell (i, j), define d(i, j), the distance to the nearest emergency exit, as follows:

  • Starting from (i, j), repeatedly move to an adjacent non-wall cell in one of the four cardinal directions (up, down, left, right) until you reach an emergency exit. d(i, j) is the minimum number of moves required.

It is guaranteed that d(i, j) is definable for every corridor cell in the given grid; that is, every corridor cell (i, j) has at least one emergency exit reachable by passing through only corridor cells.

Write exactly one arrow (up, down, left, or right) in every corridor cell so that the following condition holds:

  • For every corridor cell (i, j), if you start at (i, j) and perform the following action d(i, j) times, you reach an emergency exit:
    • Move one cell in the direction of the arrow written in your current cell. (You cannot move into a wall cell or outside the grid.)

Constraints

  • 2 \le H \le 1000
  • 2 \le W \le 1000
  • Each S_{i,j} is ., #, or E.
  • Every corridor cell (i, j) has at least one reachable emergency exit.
  • H and W are integers.

Input

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

H W
S_{1,1}S_{1,2}\dots S_{1,W}
S_{2,1}S_{2,2}\dots S_{2,W}
\vdots
S_{H,1}S_{H,2}\dots S_{H,W}

Output

Let T_{i,j} be the state of cell (i, j) after writing the arrows. T_{i,j} is one of the following:

  • ^ : corridor cell with an upward arrow
  • v : corridor cell with a downward arrow
  • < : corridor cell with a leftward arrow
  • > : corridor cell with a rightward arrow
  • # : wall cell
  • E : emergency exit

Output the grid in the following format:

T_{1,1}T_{1,2}\dots T_{1,W}
T_{2,1}T_{2,2}\dots T_{2,W}
\vdots
T_{H,1}T_{H,2}\dots T_{H,W}

If multiple solutions exist, any of them will be accepted.


Sample Input 1

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

Sample Output 1

>>>E
^#>^
>>>^

Let us verify the condition for (2, 3) in the sample output. The distance from (2, 3) to the nearest emergency exit is 2, and following the arrows written in the sample output leads to an emergency exit in two moves.
The condition can be verified for every other corridor cell as well.


Sample Input 2

3 2
##
##
##

Sample Output 2

##
##
##

There may be cases with no corridor cells or emergency exits.


Sample Input 3

7 20
....................
..#..#..####..#E##..
..#..#..#..#..#.....
..E###..#..#..####..
.....#..#..E.....#..
.....#..####..####..
....................

Sample Output 3

>v<<<<<>>>>>>>>v<<<<
>v#^<#^^####v^#E##vv
>v#^<#v^#>v#vv#^<<<<
>>E###vv#>v#vv####^<
>>^<<#vv#>>E<<<<<#^<
>>^<<#vv####^<####^<
>>^<<<<<>>>>^<<<<<^<
H - Min of Restricted Sum

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 450 点

問題文

整数 N,M と長さ M の整数列 X=(X_1,X_2,\ldots,X_M),Y=(Y_1,Y_2,\ldots,Y_M),Z=(Z_1,Z_2,\ldots,Z_M) が与えられます。ここで、 X,Y の要素は全て 1 以上 N 以下であることが保証されます。

長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) であって、以下の条件を満たす整数列を 良い整数列 と呼びます。

  • 1\le i\le M を満たす全ての整数 i に対し、 A_{X_i} と A_{Y_i} の XOR が Z_i と一致する。

良い整数列 A=(A_1,A_2,\ldots,A_N) が存在するか判定し、存在するならば要素の総和 \displaystyle \sum_{i=1}^N A_i が最小になるような良い整数列を一つ求めてください。

XOR とは 非負整数 A,B の XOR A \oplus B は、以下のように定義されます。
  • A \oplus B を二進表記した際の 2^k \, (k \geq 0) の位の数は、A,B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、 3 \oplus 5 = 6 となります (二進表記すると: 011 \oplus 101=110)。

制約

  • 1\le N\le 2\times 10^5
  • 0\le M\le 10^5
  • 1\le X_i,Y_i\le N
  • 0\le Z_i\le 10^9
  • 入力される値は全て整数

入力

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

N M
X_1 Y_1 Z_1
X_2 Y_2 Z_2
\vdots
X_M Y_M Z_M

出力

良い整数列が存在しないならば -1 を出力せよ。

良い整数列が存在するならば要素の総和が最小になる良い整数列を一つ空白区切りで出力せよ。

なお、要素の総和が最小となる良い整数列が複数存在する場合は、どれを出力しても正答となる。


入力例 1

3 2
1 3 4
1 2 3

出力例 1

0 3 4

A=(0,3,4) は A_1 と A_2 の XOR が 3 、 A_1 と A_3 の XOR が 4 なので良い整数列です。

この他にも A=(1,2,5),(7,4,3) などが良い整数列ですが、良い整数列の中で総和が最小となるのは A=(0,3,4) です。


入力例 2

3 3
1 3 4
1 2 3
2 3 5

出力例 2

-1

良い整数列は存在しないので、 -1 を出力してください。


入力例 3

5 8
4 2 4
2 3 11
3 4 15
4 5 6
3 2 11
3 3 0
3 1 9
3 4 15

出力例 3

0 2 9 6 0

Score : 450 points

Problem Statement

You are given integers N, M and three integer sequences of length M: X = (X_1, X_2, \ldots, X_M), Y = (Y_1, Y_2, \ldots, Y_M), and Z = (Z_1, Z_2, \ldots, Z_M). It is guaranteed that all elements of X and Y are between 1 and N, inclusive.

We call a length-N sequence of non-negative integers A = (A_1, A_2, \ldots, A_N) a good sequence if and only if it satisfies the following condition:

  • For every integer i with 1 \le i \le M, the XOR of A_{X_i} and A_{Y_i} is Z_i.

Determine whether a good sequence A=(A_1,A_2,\ldots,A_N) exists, and if it exists, find one good sequence that minimizes the sum of its elements \displaystyle \sum_{i=1}^N A_i.

Notes on XOR For non-negative integers A and B, their XOR A \oplus B is defined as follows:
  • In the binary representation of A \oplus B, the digit in the place corresponding to 2^k \,(k \ge 0) is 1 if and only if exactly one of the digits in the same place of A and B is 1; otherwise, it is 0.
For example, 3 \oplus 5 = 6 (in binary: 011 \oplus 101 = 110).

Constraints

  • 1 \le N \le 2\times 10^5
  • 0 \le M \le 10^5
  • 1 \le X_i, Y_i \le N
  • 0 \le Z_i \le 10^9
  • All input values are integers.

Input

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

N M
X_1 Y_1 Z_1
X_2 Y_2 Z_2
\vdots
X_M Y_M Z_M

Output

If no good sequence exists, print -1.

If a good sequence exists, print one good sequence that minimizes the sum of its elements, separated by spaces.

If there are multiple good sequences with the same minimum sum, printing any of them is accepted.


Sample Input 1

3 2
1 3 4
1 2 3

Sample Output 1

0 3 4

A=(0,3,4) is a good sequence because A_1 \oplus A_2 = 3 and A_1 \oplus A_3 = 4.

Other good sequences include A=(1,2,5) and A=(7,4,3), but A=(0,3,4) has the smallest sum among all good sequences.


Sample Input 2

3 3
1 3 4
1 2 3
2 3 5

Sample Output 2

-1

No good sequence exists, so print -1.


Sample Input 3

5 8
4 2 4
2 3 11
3 4 15
4 5 6
3 2 11
3 3 0
3 1 9
3 4 15

Sample Output 3

0 2 9 6 0
I - Email Scheduling Optimization

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 525 点

問題文

長さ N の正整数列 A=(A_1,A_2,\dots,A_N),\;B=(B_1,B_2,\dots,B_N) が与えられます。
Q 個のクエリが与えられます。
各クエリは以下の 2 種類のいずれかです。

  • 1 i x : A_i を x に変更する。
  • 2 i x : B_i を x に変更する。

各クエリを処理するたびに、以下の問題の答えを求めてください。

高橋君は N 個の会社にメールを送り、それぞれの会社から返信をもらう必要があります。
j 個目の会社に送るメールを書くには A_j 分の時間を要し、送ってから B_j 分後に返信が届きます。
高橋君は時刻 0 からメールを書き始めます。
高橋君は N 通のメールを好きな順番で書くことができますが、2 通以上のメールを同時に書くことはできません。
高橋君がすべての返信を受け取り終える時刻としてあり得る最小値を求めてください。
ただし、メールを送るのにかかる時間は無視できるものとします。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq A_j,B_j \leq 10^9
  • 各クエリにおいて、1 \leq i \leq N
  • 各クエリにおいて、1 \leq x \leq 10^9
  • 入力される値はすべて整数

入力

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

N Q  
A_1 A_2 \ldots A_N  
B_1 B_2 \ldots B_N  
\mathrm{query}_1  
\mathrm{query}_2  
\vdots  
\mathrm{query}_Q  

各クエリ \mathrm{query}_q では、クエリの種類(1 か 2)と i と x がこの順に空白区切りで与えられる。
すなわち、各クエリは以下の 2 つの形式のいずれかである。

1 i x  
2 i x  

出力

答えを合計 Q 行で出力せよ。 q 行目には、q 番目のクエリを処理した後の問題の答えを出力せよ。


入力例 1

3 3
4 6 7
4 6 7
1 2 1
2 3 7
2 3 1

出力例 1

16
16
13

最初のクエリを行った後、A=(4,1,7), \; B=(4,6,7) です。
時刻 0 に会社 3 に送るメールを書き始め、時刻 7 に書き終えて会社 3 にメールを送ると、時刻 14 に返信を受け取ります。
時刻 7 に会社 2 に送るメールを書き始め、時刻 8 に書き終えて会社 2 にメールを送ると、時刻 14 に返信を受け取ります。
時刻 8 に会社 1 に送るメールを書き始め、時刻 12 に書き終えて会社 1 にメールを送ると、時刻 16 に返信を受け取ります。

Score : 525 points

Problem Statement

You are given length-N sequences of positive integers A=(A_1,A_2,\dots,A_N) and B=(B_1,B_2,\dots,B_N).
You are given Q queries.
Each query is of one of the following two types.

  • 1 i x : Change A_i to x.
  • 2 i x : Change B_i to x.

After processing each query, solve the following problem.

Takahashi needs to send an email to each of N companies and receive a reply from each of them.
Writing the email to send to the j-th company takes A_j minutes, and the reply arrives B_j minutes after it is sent.
He starts writing emails at time 0.
He can write the N emails in any order he likes, but he cannot write two or more emails simultaneously.
Find the minimum possible time at which he finishes receiving all of the replies.
Assume that the time taken to send emails is negligible.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq A_j,B_j \leq 10^9
  • In each query, 1 \leq i \leq N.
  • In each query, 1 \leq x \leq 10^9.
  • All input values are integers.

Input

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

N Q  
A_1 A_2 \ldots A_N  
B_1 B_2 \ldots B_N  
\mathrm{query}_1  
\mathrm{query}_2  
\vdots  
\mathrm{query}_Q  

For each query \mathrm{query}_q, the type of the query (1 or 2), i, and x are given in this order, separated by spaces.
That is, each query is given in one of the following two formats:

1 i x  
2 i x  

Output

Output the answers in a total of Q lines. The q-th line should contain the answer to the problem after processing the q-th query.


Sample Input 1

3 3
4 6 7
4 6 7
1 2 1
2 3 7
2 3 1

Sample Output 1

16
16
13

After processing the first query, A=(4,1,7) and B=(4,6,7).
If Takahashi starts writing the email to send to company 3 at time 0, finishes writing it at time 7, and sends it to company 3, he receives the reply at time 14.
If he starts writing the email to send to company 2 at time 7, finishes writing it at time 8, and sends it to company 2, he receives the reply at time 14.
If he starts writing the email to send to company 1 at time 8, finishes writing it at time 12, and sends it to company 1, he receives the reply at time 16.