A - AKIBA

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 300

問題文

文字列 S が与えられます。

高橋君はこの文字列の好きな位置に好きなだけ文字 A を挿入することができます。

SAKIHABARA に変えることはできるでしょうか?

制約

  • 1 \leq |S| \leq 50
  • S は大文字アルファベットのみからなる。

入力

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

S

出力

SAKIHABARA に変えることができる場合は YES、できない場合は NO を出力せよ。


入力例 1

KIHBR

出力例 1

YES

先頭、Hの直後、Bの直後、末尾の 4 箇所に 1 つずつ挿入すれば良いです。


入力例 2

AKIBAHARA

出力例 2

NO

AKIHABARA が正しい綴りです。


入力例 3

AAKIAHBAARA

出力例 3

NO

Score : 300 points

Problem Statement

You are given a string S.

Takahashi can insert the character A at any position in this string any number of times.

Can he change S into AKIHABARA?

Constraints

  • 1 \leq |S| \leq 50
  • S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If it is possible to change S into AKIHABARA, print YES; otherwise, print NO.


Sample Input 1

KIHBR

Sample Output 1

YES

Insert one A at each of the four positions: the beginning, immediately after H, immediately after B and the end.


Sample Input 2

AKIBAHARA

Sample Output 2

NO

The correct spell is AKIHABARA.


Sample Input 3

AAKIAHBAARA

Sample Output 3

NO
B - Palindrome-phobia

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 400

問題文

すぬけ君は abc3 種類の文字のみからなる文字列 S を持っています。

回文恐怖症のすぬけ君は S の文字を自由に並び替えて、2 文字以上の回文を部分文字列として含まないようにしようと思いました。 これが可能かどうかを判定して下さい。

制約

  • 1 \leq |S| \leq 10^5
  • Sabc 以外の文字を含まない。

入力

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

S

出力

可能な場合は YES、不可能な場合は NO を出力せよ。


入力例 1

abac

出力例 1

YES

このままだと aba という回文を含みますが、例えば acba のように並び替えると 2 文字以上の回文を含まなくなります。


入力例 2

aba

出力例 2

NO

入力例 3

babacccabab

出力例 3

YES

Score : 400 points

Problem Statement

Snuke has a string S consisting of three kinds of letters: a, b and c.

He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible.

Constraints

  • 1 \leq |S| \leq 10^5
  • S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

If the objective is achievable, print YES; if it is unachievable, print NO.


Sample Input 1

abac

Sample Output 1

YES

As it stands now, S contains a palindrome aba, but we can permute the characters to get acba, for example, that does not contain a palindrome of length 2 or more.


Sample Input 2

aba

Sample Output 2

NO

Sample Input 3

babacccabab

Sample Output 3

YES
C - Time Gap

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 500

問題文

XXXX年のCODE FESTIVALには、世界中から高橋君を含めて N+1 人の参加者が集まりました。

高橋君の都市と他の N 人の都市の時刻の差を調べてみたところ、i 番目の人の都市との時刻の差は D_i 時間でした。 ただし 2 つの都市について、片方の都市で 0 時の瞬間にもう一方の都市で d 時であるようなとき、これらの都市の時刻の差は min(d,24-d) であるものとします。 ここで、時刻の表記には 24 時間表記を用いるものとします。 つまり、例えば高橋君の都市で 0 時の瞬間には i 番目の人の都市は D_i 時または 24-D_i 時のいずれかとなります。

高橋君は次に、N+1 人のうちの全ての 2 人組についてその人の都市どうしの時刻の差を書き出し、それらの時刻の差のうちの最小値を s としました。

s として考えられる最大値を求めて下さい。

制約

  • 1 \leq N \leq 50
  • 0 \leq D_i \leq 12
  • 入力は全て整数である。

入力

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

N
D_1 D_2 ... D_N

出力

s として考えられる最大値を出力せよ。


入力例 1

3
7 12 8

出力例 1

4

例えば、高橋君の都市で 0 時の瞬間にそれぞれの人の都市での時刻が 7 時、12 時、16 時であるような状況のとき、2 番目の人と 3 番目の人の都市の時刻の差が 4 時間となります。


入力例 2

2
11 11

出力例 2

2

入力例 3

1
0

出力例 3

0

高橋君も参加者に含まれる点に注意してください。

Score : 500 points

Problem Statement

In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi.

Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours. The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours. Here, we are using 24-hour notation. That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example.

Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours.

Find the maximum possible value of s.

Constraints

  • 1 \leq N \leq 50
  • 0 \leq D_i \leq 12
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N
D_1 D_2 ... D_N

Output

Print the maximum possible value of s.


Sample Input 1

3
7 12 8

Sample Output 1

4

For example, consider the situation where it is 7, 12 and 16 o'clock in each person's city at the moment when it is 0 o'clock in Takahashi's city. In this case, the time gap between the second and third persons' cities is 4 hours.


Sample Input 2

2
11 11

Sample Output 2

2

Sample Input 3

1
0

Sample Output 3

0

Note that Takahashi himself is also a participant.

D - Zabuton

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 700

問題文

ある年のCODE FESTIVALの本戦には N 人の参加者が集まりました。 参加者 i は身長が H_i で力が P_i です。

りんごさんは参加者に座布団を積むゲームをしてもらおうとしています。

参加者は好きな順番で並び、1 人ずつ座布団を 1 箇所に積んでいきます。 はじめ、座布団は 1 枚も積まれていません。 参加者 i は、自分の番が回ってきた時にすでに積まれた座布団が H_i 枚以下の場合はちょうど P_i 枚の座布団を積みます。そうでない場合は諦めて 1 枚も積みません。

りんごさんはできるだけ多くの参加者に座布団を積んで欲しいと思っています。 参加者の並び順を工夫することによって、最大で何人の参加者に座布団を積んでもらうことができるでしょうか?

制約

  • 1 \leq N \leq 5000
  • 0 \leq H_i \leq 10^9
  • 1 \leq P_i \leq 10^9

入力

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

N
H_1 P_1
H_2 P_2
:
H_N P_N

出力

座布団を積む参加者の人数の最大値を出力せよ。


入力例 1

3
0 2
1 3
3 4

出力例 1

2

入力の通りの順に参加者が並ぶと、1,3 番目の参加者が座布団を積むことができます。

また、3 人全員が座布団を積めるような並び順は存在しないため、答えは 2 人となります。


入力例 2

3
2 4
3 1
4 1

出力例 2

3

2 番、3 番、1 番の順で参加者が並ぶと、全員が座布団を積むことができます。


入力例 3

10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1

出力例 3

5

Score : 700 points

Problem Statement

In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.

Ringo is hosting a game of stacking zabuton (cushions).

The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.

Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?

Constraints

  • 1 \leq N \leq 5000
  • 0 \leq H_i \leq 10^9
  • 1 \leq P_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
H_1 P_1
H_2 P_2
:
H_N P_N

Output

Print the maximum number of participants who can add zabuton to the stack.


Sample Input 1

3
0 2
1 3
3 4

Sample Output 1

2

When the participants line up in the same order as the input, Participants 1 and 3 will be able to add zabuton.

On the other hand, there is no order such that all three participants can add zabuton. Thus, the answer is 2.


Sample Input 2

3
2 4
3 1
4 1

Sample Output 2

3

When the participants line up in the order 2, 3, 1, all of them will be able to add zabuton.


Sample Input 3

10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1

Sample Output 3

5
E - Combination Lock

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1000

問題文

りんごさんは文字列 S を持っています。

りんごさんは以下のような N 種類の操作を好きな順番で何回でも行うことができます。

  • 操作 iSL_i 文字目から R_i 文字目までをそれぞれ次のアルファベットにする。(ab に、bc に・・・)ただし、z の次のアルファベットは a であるとする。

回文が大好きなりんごさんは S を回文にしようとしています。 これが可能かどうかを判定してください。

制約

  • 1 \leq |S| \leq 10^5
  • S は小文字アルファベットのみからなる。
  • 1 \leq N \leq 10^5
  • 1 \leq L_i \leq R_i \leq |S|

入力

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

S
N
L_1 R_1
L_2 R_2
:
L_N R_N

出力

S を回文にできるなら YES を、できないなら NO を出力せよ。


入力例 1

bixzja
2
2 3
3 6

出力例 1

YES

例えば、操作 1、操作 2、操作 1 の順に行うと、bixzjabjyzjabjzakbbkaakb と変化し、回文になります。


入力例 2

abc
1
2 2

出力例 2

NO

入力例 3

cassert
4
1 2
3 4
1 1
2 2

出力例 3

YES

Score : 1000 points

Problem Statement

Ringo has a string S.

He can perform the following N kinds of operations any number of times in any order.

  • Operation i: For each of the characters from the L_i-th through the R_i-th characters in S, replace it with its succeeding letter in the English alphabet. (That is, replace a with b, replace b with c and so on.) For z, we assume that its succeeding letter is a.

Ringo loves palindromes and wants to turn S into a palindrome. Determine whether this is possible.

Constraints

  • 1 \leq |S| \leq 10^5
  • S consists of lowercase English letters.
  • 1 \leq N \leq 10^5
  • 1 \leq L_i \leq R_i \leq |S|

Input

Input is given from Standard Input in the following format:

S
N
L_1 R_1
L_2 R_2
:
L_N R_N

Output

Print YES if it is possible to turn S into a palindrome; print NO if it is impossible.


Sample Input 1

bixzja
2
2 3
3 6

Sample Output 1

YES

For example, if we perform Operation 1, 2 and 1 in this order, S changes as bixzjabjyzjabjzakbbkaakb and becomes a palindrome.


Sample Input 2

abc
1
2 2

Sample Output 2

NO

Sample Input 3

cassert
4
1 2
3 4
1 1
2 2

Sample Output 3

YES
F - Distribute Numbers

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1000

問題文

1000 以上 2000 以下の好きな整数 N1 以上の好きな整数 K を選び、以下の問題を解いてください。

問題

N 枚の紙があります。 これらの紙に以下の条件を満たすように K 個ずつ整数を書いてください。

  • 書く整数は 1 以上 N 以下でなければならない。
  • 同じ紙に書かれた K 個の整数は相異ならなければならない。
  • 1N の整数はいずれも K 枚ずつの紙に書かれていなければならない。
  • どの 2 枚の紙をとってきても、それらの紙に共通して書かれた整数がちょうど 1 つだけ存在する。

入力

この問題では入力は与えられない。

出力

1 行目に NK を空白区切りで出力せよ。

2 行目からの N 行には、各紙に書く整数の情報を出力せよ。 このうち i 行目には i 枚目の紙に書く K 個の整数を空白区切りで出力せよ。


出力例

3 2
1 2
2 3
3 1

N3K2 の例です。

ただし、N の制約を満たしていないためこの出力は不正解となります。

Score : 1000 points

Problem Statement

Select any integer N between 1000 and 2000 (inclusive), and any integer K not less than 1, then solve the problem below.

Problem

We have N sheets of paper. Write K integers on each of them to satisfy the following conditions:

  • Each integer written must be between 1 and N (inclusive).
  • The K integers written on the same sheet must be all different.
  • Each of the integers between 1 and N must be written on exactly K sheets.
  • For any two sheet, there is exactly one integer that appears on both.

Input

There is no input in this problem.

Output

In the first line, print N and K separated by a space.

In the subsequent N lines, print your solution. The i-th of these lines must contain the K integers written on the i-th sheet, with spaces in between.


Sample Output

3 2
1 2
2 3
3 1

This is an example of a solution for N = 3 and K = 2.

Note that this output will be judged as incorrect, since the constraint on N is not satisfied.

G - Mancala

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1000

問題文

以下のようなゲームを考えます。

  • 1 列に並んだ N 個のマスとたくさんの石を用意する。
  • 最初に、マス i\ (1 \leq i \leq N)a_i 個の石を入れる。
  • プレイヤーは「ちょうど i 個の石が入っているようなマス i1 つ選び、そこに入っている石を全て捨て、マス 1 からマス i-1 までの i-1 個のマスに石を 1 つずつ追加する」という操作を好きなだけ行う。
  • 最終的にマスに残った石の個数の合計がスコアとなる。

長さ N の数列 a に対してこのゲームを行ったときのスコアとして考えられる最小値を f(a) とします。

このとき、長さが N で各要素が 0 以上 K 以下であるような全ての数列 a についての f(a) の総和はいくつになるでしょうか? 答えは非常に大きくなる可能性があるため、1000000007 (= 10^9+7) で割った余りを求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq K \leq N

入力

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

N K

出力

f(a) の総和を 1000000007 (= 10^9+7) で割った余りを出力せよ。


入力例 1

2 2

出力例 1

10

長さが 2 で各要素が 0 以上 2 以下であるような数列 a9 通り考えられますが、それぞれに対する f(a) の値とそれを達成するための操作の例は以下の通りです。

  • f(\{0,0\})0(なにも操作できない)
  • f(\{0,1\})1(なにも操作できない)
  • f(\{0,2\})0(マス 2 、マス 1 の順に操作する)
  • f(\{1,0\})0(マス 1 を選ぶ)
  • f(\{1,1\})1(マス 1 を選ぶ)
  • f(\{1,2\})0(マス 1 、マス 2 、マス 1 の順に操作する)
  • f(\{2,0\})2(なにも操作できない)
  • f(\{2,1\})3(なにも操作できない)
  • f(\{2,2\})3(マス 2 を選ぶ)

入力例 2

20 17

出力例 2

983853488

Score : 1000 points

Problem Statement

Consider the following game:

  • The game is played using a row of N squares and many stones.
  • First, a_i stones are put in Square i\ (1 \leq i \leq N).
  • A player can perform the following operation as many time as desired: "Select an integer i such that Square i contains exactly i stones. Remove all the stones from Square i, and add one stone to each of the i-1 squares from Square 1 to Square i-1."
  • The final score of the player is the total number of the stones remaining in the squares.

For a sequence a of length N, let f(a) be the minimum score that can be obtained when the game is played on a.

Find the sum of f(a) over all sequences a of length N where each element is between 0 and K (inclusive). Since it can be extremely large, find the answer modulo 1000000007 (= 10^9+7).

Constraints

  • 1 \leq N \leq 100
  • 1 \leq K \leq N

Input

Input is given from Standard Input in the following format:

N K

Output

Print the sum of f(a) modulo 1000000007 (= 10^9+7).


Sample Input 1

2 2

Sample Output 1

10

There are nine sequences of length 2 where each element is between 0 and 2. For each of them, the value of f(a) and how to achieve it is as follows:

  • f(\{0,0\}): 0 (Nothing can be done)
  • f(\{0,1\}): 1 (Nothing can be done)
  • f(\{0,2\}): 0 (Select Square 2, then Square 1)
  • f(\{1,0\}): 0 (Select Square 1)
  • f(\{1,1\}): 1 (Select Square 1)
  • f(\{1,2\}): 0 (Select Square 1, Square 2, then Square 1)
  • f(\{2,0\}): 2 (Nothing can be done)
  • f(\{2,1\}): 3 (Nothing can be done)
  • f(\{2,2\}): 3 (Select Square 2)

Sample Input 2

20 17

Sample Output 2

983853488
H - Poor Penguin

Time Limit: 5 sec / Memory Limit: 256 MB

配点 : 1600

問題文

北極海のとある場所に HW 列のマス目状に氷が浮かんでいます。 i 行目 j 列目のマスをマス (i,j) と表すことにします。 浮かんでいる氷は薄氷または氷山であり、薄氷のマスのうちちょうど 1 マスにはペンギンが住んでいます。 また、HW 列のマス目の外には氷は浮かんでいません。

マス (i,j) に浮かんでいる氷は文字 S_{i,j} で表されます。 S_{i,j}+#P のいずれかであり、それぞれ以下のようなことを表しています。

  • +:薄氷が浮かんでいる。
  • #:氷山が浮かんでいる。
  • P:薄氷が浮かんでおり、ペンギンが住んでいる。

夏になると、氷に挟まれていない不安定な薄氷は次々に崩落していってしまいます。 つまり、マス (i,j) の薄氷が以下の条件をいずれも満たしていないとき、その薄氷は崩落してしまいます。

  • マス (i-1,j) とマス (i+1,j) の両方に氷山または崩落していない薄氷が存在する。
  • マス (i,j-1) とマス (i,j+1) の両方に氷山または崩落していない薄氷が存在する。

崩落は連鎖的に発生することもあります。 また、氷山は崩落しません。

さて、ここに悪い旅人がやってきてペンギンの住んでいる薄氷が夏になると崩落するように細工をしようと考えました。 旅人はハンマーで氷山を叩いて薄氷に変えることができます。 旅人は最小でいくつの氷山を薄氷に変えれば良いでしょうか?

制約

  • 1 \leq H,W \leq 40
  • S_{i,j}+#P のいずれかである。
  • S はちょうど 1 つだけ P を含む。

入力

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

H W
S_{1,1}S_{1,2}...S_{1,W}
S_{2,1}S_{2,2}...S_{2,W}
:
S_{H,1}S_{H,2}...S_{H,W}

出力

ペンギンの住んでいる薄氷が夏になると崩落するようにするために薄氷に変える必要のある氷山の個数の最小値を出力せよ。


入力例 1

3 3
+#+
#P#
+#+

出力例 1

2

例えば右と下の氷山を薄氷に変えると以下のように薄氷が崩落していきます。

+#+    .#.    .#.    .#.
#P+ -> #P+ -> #P. -> #..
+++    .+.    ...    ...

入力例 2

6 6
#+++++
+++#++
#+++++
+++P+#
+##+++
++++#+

出力例 2

1

入力例 3

40 40
#++#+++++#+#+#+##+++++++##+#+++#++##++##
+##++++++++++#+###+##++++#+++++++++#++##
+++#+++++#++#++####+++#+#+###+++##+++#++
+++#+######++##+#+##+#+++#+++++++++#++#+
+++##+#+#++#+++#++++##+++++++++#++#+#+#+
#++#+++#+#++++##+#+#+++##+#+##+#++++##++
++#+##+++#++####+#++##++#+++#+#+#++++#++
+#+###++++++##++++++#++##+#####++#++##++
##+##+#+++#+#+##++#+###+######++++#+###+
+++#+++##+#####+#+#++++#+#+++++#+##++##+
#+++#+##+++++++#++#++++++++++###+#++#+#+
##+++##++#+++++#++++#++#+##++#+#+#++##+#
##+++#+###+++++##++#+#+++####+#+++++#+++
+++#++#++#+++++++++#++###++++++++###+##+
++#+++#++++++#####++##++#+++#+++++#++++#
++#++#+##++++#####+###+++####+#+#+######
++++++##+++++##+++++#++###++#++##+++++++
+#++++##++++++#++++#+#++++#++++##+++##+#
+++++++#+#++##+##+#+++++++###+###++##+++
++++++#++###+#+#+++##+#++++++#++#+#++#+#
##+##++++++#+++++#++#+#++##+++#+#+++##+#
#+++#+#+##+#+##++#P#++#++++++##++#+#++##
#+++#++##+##+#++++#++#++##++++++#+#+#+++
++++####+#++#####+++#+###+#++###++++#++#
#+#++####++##++#+#+#+##+#+#+##++++##++#+
+###+###+#+##+++#++++++#+#++++###+#+++++
+++#+++++#+++#+++++##++++++++###++#+#+++
+#+#++#+#++++++###+#++##+#+##+##+#+#####
#++++++++#+#+###+######++#++#+++++++++++
##+++##+#+#++#++#++#++++++#++##+#+#++###
+#+#+#+++++++#+++++++######+##++#++##+##
++#+++#+###+#++###+++#+++#+#++++#+###+++
#+#+###++#+#####+++++#+####++#++#+###+++
+#+##+#++#++##+++++++######++#++++++++++
+####+#+#+++++##+#+#++#+#++#+++##++++#+#
#++##++#+#+++++##+#++++####+++++###+#+#+
##+#++#++#+##+#+#++##++###+###+#+++++##+
##++###+###+#+#++#++#########+++###+#+##
+++#+++#++++++++++#+#+++#++#++###+####+#
++##+###+++++++##+++++#++#++++++++++++++

出力例 3

151

入力例 4

1 1
P

出力例 4

0

Score : 1600 points

Problem Statement

In some place in the Arctic Ocean, there are H rows and W columns of ice pieces floating on the sea. We regard this area as a grid, and denote the square at the i-th row and j-th column as Square (i,j). The ice piece floating in each square is either thin ice or an iceberg, and a penguin lives in one of the squares that contain thin ice. There are no ice pieces floating outside the grid.

The ice piece at Square (i,j) is represented by the character S_{i,j}. S_{i,j} is +, # or P, each of which means the following:

  • +: Occupied by thin ice.
  • #: Occupied by an iceberg.
  • P: Occupied by thin ice. The penguin lives here.

When summer comes, unstable thin ice that is not held between some pieces of ice collapses one after another. Formally, thin ice at Square (i,j) will collapse when it does NOT satisfy either of the following conditions:

  • Both Square (i-1,j) and Square (i+1,j) are occupied by an iceberg or uncollapsed thin ice.
  • Both Square (i,j-1) and Square (i,j+1) are occupied by an iceberg or uncollapsed thin ice.

When a collapse happens, it may cause another. Note that icebergs do not collapse.

Now, a mischievous tourist comes here. He will do a little work so that, when summer comes, the thin ice inhabited by the penguin will collapse. He can smash an iceberg with a hammer to turn it to thin ice. At least how many icebergs does he need to smash?

Constraints

  • 1 \leq H,W \leq 40
  • S_{i,j} is +, # or P.
  • S contains exactly one P.

Input

Input is given from Standard Input in the following format:

H W
S_{1,1}S_{1,2}...S_{1,W}
S_{2,1}S_{2,2}...S_{2,W}
:
S_{H,1}S_{H,2}...S_{H,W}

Output

Print the minimum number of icebergs that needs to be changed to thin ice in order to cause the collapse of the thin ice inhabited by the penguin when summer comes.


Sample Input 1

3 3
+#+
#P#
+#+

Sample Output 1

2

For example, when the right and bottom icebergs are changed to thin ice, collapses happen as follows:

+#+    .#.    .#.    .#.
#P+ -> #P+ -> #P. -> #..
+++    .+.    ...    ...

Sample Input 2

6 6
#+++++
+++#++
#+++++
+++P+#
+##+++
++++#+

Sample Output 2

1

Sample Input 3

40 40
#++#+++++#+#+#+##+++++++##+#+++#++##++##
+##++++++++++#+###+##++++#+++++++++#++##
+++#+++++#++#++####+++#+#+###+++##+++#++
+++#+######++##+#+##+#+++#+++++++++#++#+
+++##+#+#++#+++#++++##+++++++++#++#+#+#+
#++#+++#+#++++##+#+#+++##+#+##+#++++##++
++#+##+++#++####+#++##++#+++#+#+#++++#++
+#+###++++++##++++++#++##+#####++#++##++
##+##+#+++#+#+##++#+###+######++++#+###+
+++#+++##+#####+#+#++++#+#+++++#+##++##+
#+++#+##+++++++#++#++++++++++###+#++#+#+
##+++##++#+++++#++++#++#+##++#+#+#++##+#
##+++#+###+++++##++#+#+++####+#+++++#+++
+++#++#++#+++++++++#++###++++++++###+##+
++#+++#++++++#####++##++#+++#+++++#++++#
++#++#+##++++#####+###+++####+#+#+######
++++++##+++++##+++++#++###++#++##+++++++
+#++++##++++++#++++#+#++++#++++##+++##+#
+++++++#+#++##+##+#+++++++###+###++##+++
++++++#++###+#+#+++##+#++++++#++#+#++#+#
##+##++++++#+++++#++#+#++##+++#+#+++##+#
#+++#+#+##+#+##++#P#++#++++++##++#+#++##
#+++#++##+##+#++++#++#++##++++++#+#+#+++
++++####+#++#####+++#+###+#++###++++#++#
#+#++####++##++#+#+#+##+#+#+##++++##++#+
+###+###+#+##+++#++++++#+#++++###+#+++++
+++#+++++#+++#+++++##++++++++###++#+#+++
+#+#++#+#++++++###+#++##+#+##+##+#+#####
#++++++++#+#+###+######++#++#+++++++++++
##+++##+#+#++#++#++#++++++#++##+#+#++###
+#+#+#+++++++#+++++++######+##++#++##+##
++#+++#+###+#++###+++#+++#+#++++#+###+++
#+#+###++#+#####+++++#+####++#++#+###+++
+#+##+#++#++##+++++++######++#++++++++++
+####+#+#+++++##+#+#++#+#++#+++##++++#+#
#++##++#+#+++++##+#++++####+++++###+#+#+
##+#++#++#+##+#+#++##++###+###+#+++++##+
##++###+###+#+#++#++#########+++###+#+##
+++#+++#++++++++++#+#+++#++#++###+####+#
++##+###+++++++##+++++#++#++++++++++++++

Sample Output 3

151

Sample Input 4

1 1
P

Sample Output 4

0
I - Full Tournament

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1600

問題文

2^N 人の選手でトーナメント戦を行いました。 各選手には 12^N の番号がついており、2 人が対戦したときは番号が小さい方の選手が必ず勝ちます。

行ったトーナメント戦は少し変わっていて、敗者どうしも対戦をすることで全員の順位を決めるものでした。

ここで、2^n 人で行うこのようなトーナメント戦を「レベル n のフルトーナメント」と呼ぶことにします。 レベル n のフルトーナメントの順位は以下のように決定されます。

  • レベル 0 のフルトーナメントでは参加者が 1 人であり、この人が 1 位となる。
  • レベル n\ (1 \leq n) のフルトーナメントは、2^n 人の選手が横 1 列に並んだ状態から始まり、以下のように順位を決定する。
  • まず、端から 2 人ずつに区切り、2^{n-1} 組の 2 人組を作る。
  • それぞれの 2 人組で対戦を行い、勝った方が勝ちグループに、負けた方が負けグループに入る。
  • 勝ちグループに入った選手を元の列での順序を保ったまま並べ、レベル n-1 のフルトーナメントを行い、各選手の順位を決定する。
  • 負けグループについても同様に順位をつけ、その後に負けグループの各選手の順位に 2^{n-1} を足す。

例えば、8 人の選手を 3,4,8,6,2,1,7,5 の順に並べてレベル 3 のフルトーナメントを行うと下図のようにトーナメントが行われ、結果の順位順に選手を並べると 1,3,5,6,2,4,7,8 となります。

e93269f0dfb68bcdff175a3b634ab0d8.png

高橋君は、選手の番号をトーナメントの結果の順位順に書いた紙を持っていましたが、いくつかの場所が汚れて読めなくなってしまいました。 この紙の情報は長さ N の数列 A として与えられ、A_i1 以上のときは i 位の選手の番号が A_i であったことを表し、0 のときは i 位の選手の番号が分からなくなってしまったことを表します。

最初の選手の並び順として考えられるものが存在するかどうかを判定し、存在するならば例を 1 つ答えてください。

制約

  • 1 \leq N \leq 18
  • 0 \leq A_i \leq 2^N
  • A_i には 0 以外の整数は重複して含まれていない。

入力

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

N
A_1 A_2 ... A_{2^N}

出力

最初の選手の並び順として考えられるものが存在する場合は YES と出力し、2 行目に選手の番号を順番に空白区切りで出力せよ。 存在しない場合は代わりに NO と出力せよ。


入力例 1

3
0 3 0 6 0 0 0 8

出力例 1

YES
3 4 8 6 2 1 7 5

問題文中の例と同じ並び順です。


入力例 2

1
2 1

出力例 2

NO

Score : 1600 points

Problem Statement

2^N players competed in a tournament. Each player has a unique ID number from 1 through 2^N. When two players play a match, the player with the smaller ID always wins.

This tournament was a little special, in which losers are not eliminated to fully rank all the players.

We will call such a tournament that involves 2^n players a full tournament of level n. In a full tournament of level n, the players are ranked as follows:

  • In a full tournament of level 0, the only player is ranked first.
  • In a full tournament of level n\ (1 \leq n), initially all 2^n players are lined up in a row. Then:
  • First, starting from the two leftmost players, the players are successively divided into 2^{n-1} pairs of two players.
  • Each of the pairs plays a match. The winner enters the "Won" group, and the loser enters the "Lost" group.
  • The players in the "Won" group are lined up in a row, maintaining the relative order in the previous row. Then, a full tournament of level n-1 is held to fully rank these players.
  • The players in the "Lost" group are also ranked in the same manner, then the rank of each of these players increases by 2^{n-1}.

For example, the figure below shows how a full tournament of level 3 progresses when eight players are lined up in the order 3,4,8,6,2,1,7,5. The list of the players sorted by the final ranking will be 1,3,5,6,2,4,7,8.

Takahashi has a sheet of paper with the list of the players sorted by the final ranking in the tournament, but some of it blurred and became unreadable. You are given the information on the sheet as a sequence A of length N. When A_i is 1 or greater, it means that the i-th ranked player had the ID A_i. If A_i is 0, it means that the ID of the i-th ranked player is lost.

Determine whether there exists a valid order in the first phase of the tournament which is consistent with the sheet. If it exists, provide one such order.

Constraints

  • 1 \leq N \leq 18
  • 0 \leq A_i \leq 2^N
  • No integer, except 0, occurs more than once in A_i.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{2^N}

Output

If there exists a valid order in the first phase of the tournament, print YES first, then in the subsequent line, print the IDs of the players sorted by the final ranking, with spaces in between. If there is no such order, print NO instead.


Sample Input 1

3
0 3 0 6 0 0 0 8

Sample Output 1

YES
3 4 8 6 2 1 7 5

This is the same order as the one in the statement.


Sample Input 2

1
2 1

Sample Output 2

NO
J - Tree MST

Time Limit: 5 sec / Memory Limit: 256 MB

配点 : 1900

問題文

りんごさんは N 頂点の木を持っています。 この木の N-1 本の辺のうち i 番目の辺は頂点 A_i と頂点 B_i を繋いでおり、重みは C_i です。 また、頂点 i には X_i の重みがついています。

ここで f(u,v) を、「頂点 u から頂点 v までの距離」と「X_u + X_v」の和と定めます。

N 頂点の完全グラフ G を考えます。 頂点 u と頂点 v を繋ぐ辺のコストは f(u,v) です。 グラフ G の最小全域木を求めて下さい。

制約

  • 2 \leq N \leq 200,000
  • 1 \leq X_i \leq 10^9
  • 1 \leq A_i,B_i \leq N
  • 1 \leq C_i \leq 10^9
  • 与えられるグラフは木である。
  • 入力は全て整数である。

入力

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

N
X_1 X_2 ... X_N
A_1 B_1 C_1
A_2 B_2 C_2
:
A_{N-1} B_{N-1} C_{N-1}

出力

グラフ G の最小全域木のコストを出力せよ。


入力例 1

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

出力例 1

22

頂点 12、頂点 14、頂点 34 を繋ぐとそれぞれのコストは 5,8,9 となり、合計は 22 となります。


入力例 2

6
44 23 31 29 32 15
1 2 10
1 3 12
1 4 16
4 5 8
4 6 15

出力例 2

359

入力例 3

2
1000000000 1000000000
2 1 1000000000

出力例 3

3000000000

Score : 1900 points

Problem Statement

Ringo has a tree with N vertices. The i-th of the N-1 edges in this tree connects Vertex A_i and Vertex B_i and has a weight of C_i. Additionally, Vertex i has a weight of X_i.

Here, we define f(u,v) as the distance between Vertex u and Vertex v, plus X_u + X_v.

We will consider a complete graph G with N vertices. The cost of its edge that connects Vertex u and Vertex v is f(u,v). Find the minimum spanning tree of G.

Constraints

  • 2 \leq N \leq 200,000
  • 1 \leq X_i \leq 10^9
  • 1 \leq A_i,B_i \leq N
  • 1 \leq C_i \leq 10^9
  • The given graph is a tree.
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N
A_1 B_1 C_1
A_2 B_2 C_2
:
A_{N-1} B_{N-1} C_{N-1}

Output

Print the cost of the minimum spanning tree of G.


Sample Input 1

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

Sample Output 1

22

We connect the following pairs: Vertex 1 and 2, Vertex 1 and 4, Vertex 3 and 4. The costs are 5, 8 and 9, respectively, for a total of 22.


Sample Input 2

6
44 23 31 29 32 15
1 2 10
1 3 12
1 4 16
4 5 8
4 6 15

Sample Output 2

359

Sample Input 3

2
1000000000 1000000000
2 1 1000000000

Sample Output 3

3000000000