A - 中央値

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 9

注意

この問題に対する言及は、2020/11/8 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

相異なる整数 A, B, C が与えられます。この中で 2 番目に大きいものがどれか、A, B, C のいずれかで出力してください。

制約

  • 1 \le A, B, C \le 100
  • A, B, C は相異なる整数

入力

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

A B C

出力

A, B, C の中で 2 番目に大きいものはどれか、A, B, C のいずれかで出力せよ。


入力例 1

15 49 7

出力例 1

A

A = 152 番目に大きいです。


入力例 2

53 2 1

出力例 2

B

Score : 9 points

Warning

Do not make any mention of this problem until November 8, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Given are distinct integers A, B, and C. Which of them is the second greatest? Answer with A, B, or C.

Constraints

  • 1 \le A, B, C \le 100
  • A, B, and C are distinct integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print A, B, or C to answer the question: which of A, B, and C is the second greatest?


Sample Input 1

15 49 7

Sample Output 1

A

The second greatest is A = 15.


Sample Input 2

53 2 1

Sample Output 2

B
B - 電卓

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 8

注意

この問題に対する言及は、2020/11/8 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

あなたが持っている電卓は非負整数 X, Y を入力すると \frac{X}{Y} を先頭に余計な 0 をつけずに小数点第 3 位以下を切り捨てて小数点第 2 位までの値で表示します。 たとえば X=4,Y=2 のとき電卓には 2.00 が、X=2,Y=3 のときは 0.66 が表示されます。 ただし、Y=0 の場合は ERROR を表示します。

X,Y を入力したときの電卓の表示を求めるプログラムを作成してください。

制約

  • 与えられる入力は全て整数
  • 0 \leq X, Y \leq 100

入力

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

X Y

出力

電卓の表示を出力せよ。


入力例 1

100 3

出力例 1

33.33
  • \frac{X}{Y}= 33.33333 \cdots です。電卓に表示されるのは小数点第 2 位までである 33.33 となります。

入力例 2

42 0

出力例 2

ERROR
  • Y=0 の場合は ERROR を出力してください。

Score : 8 points

Warning

Do not make any mention of this problem until November 8, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

You have a calculator. When you enter non-negative integers X and Y, it shows the value \frac{X}{Y} without leading zeros after rounding down (towards negative infinity) to two decimal places. For example, if X=4,Y=2, the calculator shows 2.00; if X=2,Y=3, it shows 0.66. If Y=0, it shows ERROR.

Write a program to find what the calculator shows when X and Y are entered.

Constraints

  • All values in input are integers.
  • 0 \leq X, Y \leq 100

Input

Input is given from Standard Input in the following format:

X Y

Output

Print what the calculator shows.


Sample Input 1

100 3

Sample Output 1

33.33
  • We have \frac{X}{Y}= 33.33333 \cdots. The calculator rounds it down to two decimal places and shows 33.33.

Sample Input 2

42 0

Sample Output 2

ERROR
  • Print ERROR if Y=0.
C - 隣接カウント

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 8

注意

この問題に対する言及は、2020/11/8 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

NM 列のマス目が与えられます。それぞれのマスには #. が書かれており、上から i 行目、左から j 列目のマスには s_{i,j} が書かれています。 それぞれのマスについて、そのマス、およびそのマスと上下左右斜めに隣接したマス (最大で合計 9 マス) のうち # が書かれたマスがいくつあるかを数えてください。

制約

  • 1 \leq N,M \leq 30
  • s_i (1 \leq i \leq N) の長さは M
  • s_{i,j}#. のどちらか

入力

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

N M
s_{1,1}\cdots s_{1,M}
\vdots
s_{N,1}\cdots s_{N,M}

出力

以下のフォーマットで N 行に渡って、長さ M の文字列を出力せよ。 i 行目の先頭から j 文字目は上から i 行目、左から j 列目のマスについて、そのマス、およびそのマスと上下左右斜めに隣接したマスのうち # が書かれたマスの個数を 0 から 9 までの数字で出力せよ。

a_{1,1}\cdots a_{1,M}
\vdots
a_{N,1}\cdots a_{N,M}

入力例 1

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

出力例 1

1333
2433
1211
  • 例えば上から 1 行目、左から 3 列目のマスは、そのマス、およびそのマスと上下左右斜めに隣接したマスのうち、3 つに # が書かれています。
  • よって、1 行目の先頭から 3 文字目は 3 となります。

入力例 2

10 12
#.##..#...##
#..#..##...#
##.#....##.#
.#..###...#.
#..#..#...##
###...#..###
.###.#######
.#..#....###
.#.##..####.
.###....#..#

出力例 2

233322331133
455432343354
444344443343
444344332454
454335431465
466434554686
466434445796
346554457885
346542135664
235431134432

Score : 8 points

Warning

Do not make any mention of this problem until November 8, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Given is a grid with N rows and M columns. Each square has # or . written on it. More specifically, the square at the i-th row from the top and j-th column from the left has s_{i,j} written on it. For each square T, count the number of squares with # among the following squares (at most nine in total): T itself, and the squares that are horizontally, vertically, or diagonally adjacent to T.

Constraints

  • 1 \leq N,M \leq 30
  • The length of s_i (1 \leq i \leq N) is M.
  • s_{i,j} is # or ..

Input

Input is given from Standard Input in the following format:

N M
s_{1,1}\cdots s_{1,M}
\vdots
s_{N,1}\cdots s_{N,M}

Output

Print N lines, each containing a string of length M. In the i-th line, the j-th character from the beginning should be a digit between 0 and 9 (inclusive) representing the number of squares with # among the following squares: the square at the i-th row from the top and j-th column from the left, and the squares that are horizontally, vertically, or diagonally adjacent to that square.

a_{1,1}\cdots a_{1,M}
\vdots
a_{N,1}\cdots a_{N,M}

Sample Input 1

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

Sample Output 1

1333
2433
1211
  • For example, consider the square at the 1-st row from the top and 3-rd column from the left. Among that square and the ones that are horizontally, vertically, or diagonally adjacent to that square, there are 3 squares with #.
  • Thus, the 3-rd character from the beginning in the 1-st line should be 3.

Sample Input 2

10 12
#.##..#...##
#..#..##...#
##.#....##.#
.#..###...#.
#..#..#...##
###...#..###
.###.#######
.#..#....###
.#.##..####.
.###....#..#

Sample Output 2

233322331133
455432343354
444344443343
444344332454
454335431465
466434554686
466434445796
346554457885
346542135664
235431134432
D - 分身

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 7

注意

この問題に対する言及は、2020/11/8 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

左右方向一列に並ぶ N 個のマスがあります。左から i 番目のマスをマス i と呼ぶことにします。
いくつかのマスには忍者がいて、その配置は文字列 S として与えられます。 Si 番目の文字が # のときマス i には忍者がいて、 . のときマス i には忍者がいません。
今からタイプ A とタイプ B の 2 種類の操作を任意の順序で何回か行うことができます。一回も操作を行わなくても構いません。

  • タイプ A : マス 1 以外に現在いる全ての忍者は、自分の一つ左のマスに分身を作る。分身は以後本物同様に扱われる。
  • タイプ B : マス N 以外に現在いる全ての忍者は、自分の一つ右のマスに分身を作る。分身は以後本物同様に扱われる。

タイプ A を行う回数を x 、タイプ B を行う回数を y とします。
全ての操作の終了後、全てのマスに少なくとも一人の忍者がいるような手順のうち、x + y が最小になるような手順における x, y を一つ求めてください。

制約

  • 1 \le N \le 50
  • N は整数
  • S.# からなる。
  • S には少なくとも 1#が含まれる。

入力

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

N
S

出力

タイプ A を行う回数を x 、タイプ B を行う回数を y としたとき、x + y が最小になるような手順における x, y を一つ、空白区切りで出力せよ。


入力例 1

5
.#..#

出力例 1

1 1

例えばタイプ A, タイプ B の順に行うと、各マスの忍者の有無は .#..# -> ##.## -> ##### と変化し、最終的に全てのマスに忍者が存在するようになります。
タイプ A とタイプ B の合計回数が 2 未満で目的を達成することはできないのでこれは答えの一つです。
他にも例えば 2 0 は正しい答えです。


入力例 2

6
..#...

出力例 2

2 3

これは唯一の正しい答えです。


入力例 3

3
###

出力例 3

0 0

全く分身しなくて良い場合もあります。

Score : 7 points

Warning

Do not make any mention of this problem until November 8, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

There are N squares arranged in a row from left to right. Let Square i be the i-th square from the left. There are ninjas in some squares. The positions of the ninjas are given as a string S. If the i-th character of S is #, there is a ninja in Square i; if that character is ., there is no ninja in Square i. We can now do two types of operations, A and B, any number of times, possibly zero, in any order.

  • Type A: Every ninja who is not in Square 1 creates his clone in the square to the immediate left of him. The clone will be treated as a real ninja from then on.
  • Type B: Every ninja who is not in Square N creates his clone in the square to the immediate right of him. The clone will be treated as a real ninja from then on.

Let x and y be the numbers of times the operations of Type A and B are performed, respectively. Consider the sequences of operations after which there is at least one ninja in every square. Find x and y in one of those sequences of operations that minimizes x + y.

Constraints

  • 1 \le N \le 50
  • N is an integer.
  • S consists of . and #.
  • S contains at least one #.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print x and y in a sequence of operations that minimizes x + y, with space in between, where x and y are the numbers of times the operations of Type A and B are performed, respectively.


Sample Input 1

5
.#..#

Sample Output 1

1 1

For example, if we first do Type A and then Type B, whether each square contains a ninja changes as follows: .#..# -> ##.## -> #####, and every square contains ninja eventually. It is impossible to achieve this objective with less than two operations in total, so this is a valid answer. There are also other valid answers; one of them is 2 0.


Sample Input 2

6
..#...

Sample Output 2

2 3

This is the only valid answer.


Sample Input 3

3
###

Sample Output 3

0 0

It is possible that no cloning is needed.

E - アナグラム

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 7

注意

この問題に対する言及は、2020/11/8 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

長さ N の文字列 S が与えられます。
文字列 T であって、次の条件をすべて満たすものが存在するか判定し、存在するなら一つ求めてください。

  • TS の文字を並び変えて作ることができる
  • TS は異なる
  • T を逆から読んだものと S は異なる

制約

  • 1 \le N \le 5
  • N は整数
  • S は英小文字からなる長さ N の文字列

入力

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

N
S

出力

条件を満たす T が存在する場合、そのような T のうち一つを、存在しない場合 None を出力せよ。


入力例 1

3
cba

出力例 1

acb

acbcba を並び変えて得られ、そのままでも逆から読んでも cba と異なるので条件を満たします。
他にも bac など条件を満たすものはありますが、xyzcbaabc などは条件を満たしません。


入力例 2

2
aa

出力例 2

None

条件を満たすようなものがありません。

Score : 7 points

Warning

Do not make any mention of this problem until November 8, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Given is a string S. Determine whether there is a string T that satisfies all of the following conditions, and find one such string if it exists.

  • T is a permutation of characters of S.
  • T is different from S.
  • The reversal of T is different from S.

Constraints

  • 1 \le N \le 5
  • N is an integer.
  • S is a string of length N consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

If there is a string T that satisfies the conditions, print one such string; otherwise, print None.


Sample Input 1

3
cba

Sample Output 1

acb

acb is a permutation of cba, and both itself and its reversal differ from cba, so it satisfies the conditions. Other strings such as bac also satisfy the conditions, but strings such as xyz, cba, and abc do not.


Sample Input 2

2
aa

Sample Output 2

None

No string satisfies the conditions.

F - 構文解析

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 7

注意

この問題に対する言及は、2020/11/8 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

ある文を構成する N 個の単語の列 S が与えられます。
この列には同じ単語が複数回出てくるかもしれません。
この列に一回以上出現する単語を、その出現回数の多い順に並べたとき K 番目の単語を出力してください。
但し、出現回数が同じ単語をどう並べるかによって K 番目の単語が一つに決まらないときは代わりに AMBIGUOUS を出力してください。

制約

  • 1 \le N \le 10^5
  • S_i は長さ 1 以上 10 以下の英小文字からなる文字列 (1 \le i \le N)
  • 1 \le K \le (S に含まれる異なる文字列の個数 )
  • N, K は整数

入力

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

N K
S_1
S_2
S_3
\hspace{3pt} \vdots
S_N

出力

S1 回以上出現する単語を出現回数の多い順に並べたときに K 番目の単語が一つに決まる場合その単語を、そうでない場合 AMBIGUOUS を出力せよ。


入力例 1

6 2
abcde
caac
abcde
caac
abc
caac

出力例 1

abcde

caac3 回、abcde2 回、abc1 回出現します。出現回数の多い順で 2 番目は abcde です。


入力例 2

9 3
a
a
bb
bb
a
ccc
bb
ccc
dddd

出力例 2

ccc

abb3 回、ccc2 回、dddd1 回出現します。
出現回数が同じ abb をどの順に並べたとしても、それらが 1 番目と 2 番目を占め 3 番目が ccc となるので、 ccc を出力してください。


入力例 3

7 2
caac
abcde
caac
abc
abcde
caac
abc

出力例 3

AMBIGUOUS

caac3 回、abcdeabc が共に 2 回出現します。2 番目に多く出現するのは abcdeabc か決まらないので AMBIGUOUS を出力してください。

Score : 7 points

Warning

Do not make any mention of this problem until November 8, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Given is a sequence S of N words that form a certain sentence. The same word may appear multiple times in this sequence. Consider sorting the words that appear at least once in S in descending order of frequency, and print the word that comes in K-th. However, if the K-th frequent word varies depending on the order of words with the same frequency, print AMBIGUOUS instead.

Constraints

  • 1 \le N \le 10^5
  • S_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters. (1 \le i \le N)
  • 1 \le K \le ( the number of different words contained in S)
  • N and K are integers.

Input

Input is given from Standard Input in the following format:

N K
S_1
S_2
S_3
\hspace{3pt} \vdots
S_N

Output

If, among the words appearing in S at least once, the K-th frequent word does not vary depending on the order of words with the same frequency, print that word; otherwise, print AMBIGUOUS.


Sample Input 1

6 2
abcde
caac
abcde
caac
abc
caac

Sample Output 1

abcde

caac appears three times, abcde appears twice, and abc appears once. The second most frequent word is abcde.


Sample Input 2

9 3
a
a
bb
bb
a
ccc
bb
ccc
dddd

Sample Output 2

ccc

a and bb appear three times each, c appears twice, and dddd appears once. Regardless of the order of a and bb, that have the same frequency, these two come first and second, and ccc comes third, so we should print ccc.


Sample Input 3

7 2
caac
abcde
caac
abc
abcde
caac
abc

Sample Output 3

AMBIGUOUS

caac appears three times, and abcde and abc appear twice each. The second frequent word is not uniquely determined - it can be abcde or abc, so we should print AMBIGUOUS.

G - 村整備

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

注意

この問題に対する言及は、2020/11/8 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

AtCoder 村は南北方向 N マス、東西方向 M マスの全部で N \times M 個のマスからなる長方形の形をしています。
いくつかのマスは壁であり、S_{i, j}# なら北から i 番目、西から j 番目のマスは壁であり、 . なら壁ではありません。
あなたは壁マスをちょうど 1 つ壁でないマスに変えます。以下の条件を満たすように変えるとき、変えるマスの候補はいくつあるかを求めてください。

  • 壁でない 2 マスは全て、上下左右に 1 マス動くことを繰り返し AtCoder 村内の壁でないマスのみを通って互いに行き来可能である

制約

  • 1 \le N \le 10
  • 1 \le M \le 10
  • N, M は整数
  • S_i#. からなる長さ M の文字列 (1 \le i \le N)
  • 壁マスと壁でないマスが少なくとも 1 つずつ存在する

入力

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

N M
S_1
S_2
S_3
\hspace{3pt} \vdots
S_N

出力

条件を満たす、壁でないマスに変えるマスの候補の数を出力せよ。


入力例 1

3 3
..#
#..
.##

出力例 1

2

例えば北から 2 個目、西から 1 個目のマスを壁でないマスにすると、全ての壁でないマスが壁を通らずに互いに行き来可能になります。
北から 3 個目、西から 2 個目のマスも条件を満たします。それ以外の壁マスは条件を満たさないので答えは 2 です。


入力例 2

3 3
##.
##.
...

出力例 2

3

一番北西のマスを除く全ての壁マスが条件を満たします。
新たにできた壁でないマスも含めて全ての壁でないマスが行き来可能でならなければならないことに注意してください。

Score : 6 points

Warning

Do not make any mention of this problem until November 8, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

AtCoder Village is divided into a rectangular grid with N rows going east-west and M columns going north-south, for a total of N \times M squares. Some of the squares are "wall squares". If S_{i, j} is #, the square at the i-th row and j-th column is a wall square; if S_{i, j} is ., that square is not a wall square. You will change exactly one of the wall squares to a non-wall square. Find the number of wall squares that you can choose to change if the following condition must be satisfied after the change:

  • For every pair of two non-wall squares, it is possible to travel between those squares by repeatedly moving one square up, down, left, or right, only passing through non-wall squares in the village.

Constraints

  • 1 \le N \le 10
  • 1 \le M \le 10
  • N and M are integers.
  • S_i is a string of length M consisting of # and .. (1 \le i \le N)
  • There is at least one wall square and at least one non-wall square.

Input

Input is given from Standard Input in the following format:

N M
S_1
S_2
S_3
\hspace{3pt} \vdots
S_N

Output

Print the number of wall squares that you can choose to change to satisfy the condition.


Sample Input 1

3 3
..#
#..
.##

Sample Output 1

2

For example, if you change the square at the 2-nd row from the north and 1-st column from the west to a non-wall square, it will be possible to travel between every pair of non-wall squares. You can also choose the square at the 3-rd row from the north and 2-nd column from the west to satisfy the condition. There is no other such wall square, so the answer is 2.


Sample Input 2

3 3
##.
##.
...

Sample Output 2

3

Every wall square except the one at the north-west corner can be chosen to satisfy the condition. Note that it must be possible to travel between every pair of two non-wall squares, including the new non-wall square you make.

H - マス目のカット

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

注意

この問題に対する言及は、2020/11/8 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

NM 列のマス目が与えられます。それぞれのマスには数字が書かれており、上から i 行目、左から j 列目のマスには s_{i,j} が書かれています。 あなたは整数 n を選んで、どのマスにも同じ数字が書かれているように元のマス目から nn 列の正方形状のマス目を切り出す仕事をしています。

K 個以下のマスを選んで書かれている数字を変えることができるとき、n の値は最大でいくつになりますか?

制約

  • 1 \leq N,M \leq 30
  • 0 \leq K \leq NM
  • s_i (1 \leq i \leq N) の長さは M
  • s_{i,j}0123456789 のいずれか

入力

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

N M K
s_{1,1}\cdots s_{1,M}
\vdots
s_{N,1}\cdots s_{N,M}

出力

K 個以下のマスを選んで書かれている数字を変えることができるときの n の値としてありうる値の最大値を出力せよ。


入力例 1

3 4 3
1123
1214
1810

出力例 1

3
  • 上から i 行目、左から j 列目のマスを (i,j) と書くことにします。
  • マス (1,3),(2,2),(3,2) に書かれた数字を 1 に書き換えると、33 列のマス目のどのマスにも 1 が書かれているようにすることができます。

入力例 2

8 6 40
846444
790187
264253
967004
578258
204367
681998
034243

出力例 2

6

Score : 6 points

Warning

Do not make any mention of this problem until November 8, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Given is a grid of squares with N rows and M columns. Each square has a digit written on it; the digit written on the square at the i-th row from the top and j-th column from the left is s_{i,j}. Your work is to choose an integer n and cut out a square-shaped grid of squares with n rows and n columns where all squares have the same digit written on them.

What is the maximum possible value of n when you can choose at most K squares and change the digits written on those squares?

Constraints

  • 1 \leq N,M \leq 30
  • 0 \leq K \leq NM
  • The length of s_i (1 \leq i \leq N) is M.
  • s_{i,j} is one of the digits 0123456789.

Input

Input is given from Standard Input in the following format:

N M K
s_{1,1}\cdots s_{1,M}
\vdots
s_{N,1}\cdots s_{N,M}

Output

Print the maximum possible value of n when you can choose at most K squares and change the digits written on those squares.


Sample Input 1

3 4 3
1123
1214
1810

Sample Output 1

3
  • Let (i,j) denotes the square at the i-th row from the top and j-th column from the left.
  • If you change the digits written on (1,3), (2,2), and (3,2), there will be a subgrid with 3 rows and 3 columns where all squares have 1 written on them.

Sample Input 2

8 6 40
846444
790187
264253
967004
578258
204367
681998
034243

Sample Output 2

6
I - ピザ

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

注意

この問題に対する言及は、2020/11/8 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

あなたの今日の夕食はピザです。 1 から N の番号がついた N 個のピースが番号順に時計回りで並んでいます。 ピース N1 が隣り合っていることに注意してください。

ピース i の美味しさは a_i です。

あなたは連続したいくつかのピースを選んで食べます。 例えば 10 ピースのピザがあるとき、ピース 8,9,10,1,2 を選んで食べることはできますが、ピース 1,3,5 を選んで食べることはできません。

食べるピースの美味しさの総和を X、残るピースの美味しさの総和を Y として、|X-Y| としてありうる値の最小値を求めてください。

制約

  • 与えられる入力は全て整数
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq a_i \leq 10^9

入力

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

N
a_1 \cdots a_N

出力

食べるピースの美味しさの総和を X、残るピースの美味しさの総和を Y として、|X-Y| としてありうる値の最小値を出力せよ。


入力例 1

4
10 20 40 30

出力例 1

20
  • ピース 4,1,2 を選んで食べるのが最適な選び方の 1 つです。
  • このとき、食べるピースの美味しさの総和 X30+10+20=60、残るピースの美味しさの総和 Y40|X-Y|20 となります。
  • ピース 1,3 やピース 2,4 は連続したいくつかのピースを選んでいないことに注意してください。

入力例 2

20
13 76 46 15 50 98 93 77 31 43 84 90 6 24 14 37 73 29 43 9

出力例 2

1

Score : 6 points

Warning

Do not make any mention of this problem until November 8, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Your dinner today is a pizza. There are N pieces of the pizza, numbered 1 through N, arranged clockwise in this order. Note that Piece N and Piece 1 are adjacent to each other.

The deliciousness of Piece i is a_i.

You will choose some consecutive pieces and eat them. For example, if the pizza has 10 pieces, you can choose the pieces 8,9,10,1,2 and eat them, but cannot choose 1,3,5.

Find the minimum possible value of |X-Y| where X and Y are the sum of the deliciousness of the pieces you eat and that of the remaining pieces, respectively.

Constraints

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

Input

Input is given from Standard Input in the following format:

N
a_1 \cdots a_N

Output

Print the minimum possible value of |X-Y| where X and Y are the sum of the deliciousness of the pieces you eat and that of the remaining pieces, respectively.


Sample Input 1

4
10 20 40 30

Sample Output 1

20
  • One optimal choice is to choose to eat the pieces 4,1,2.
  • Here, the sum of the deliciousness of the pieces you eat, X, is 30+10+20=60, and that of the remaining pieces, Y, is 40, resulting in |X-Y|=20.
  • Note that neither choosing the pieces 1,3 nor choosing 2,4 is allowed since those sets of pieces are not consecutive.

Sample Input 2

20
13 76 46 15 50 98 93 77 31 43 84 90 6 24 14 37 73 29 43 9

Sample Output 2

1
J - ワープ

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

注意

この問題に対する言及は、2020/11/8 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

1 から N までの番号がついた N 個の町があります。 M 本の道路があり、i 番目の道路は町 A_i と町 B_i を双方向に結んでいて、通ると C_i の疲労度がたまります。
どの 2 つの町も、その間を直接結んでいる道路は多くとも 1 つで、どの 2 つの町もいくつかの道路を辿って行き来できます。
また、それぞれの町にはタイプ A, タイプ B, タイプ C のいずれかのワープ台があります。ワープ台の配置は文字列 S によって与えられ、町 i のワープ台のタイプは S_i です。どの町にも存在しないワープ台のタイプがあるかもしれません。
違うタイプのワープ台を持つ 2 つの町の間はワープすることができ、ワープの際にたまる疲労度は以下の通りです。

  • タイプ A のある町とタイプ B のある町の間のワープ : X_{\mathrm{AB}}
  • タイプ A のある町とタイプ C のある町の間のワープ : X_{\mathrm{AC}}
  • タイプ B のある町とタイプ C のある町の間のワープ : X_{\mathrm{BC}}

同じタイプのワープ台を持つ町の間はワープできません。
1 から町 N へ道路とワープのみを使って行く際に合計でたまる疲労度の最小値を求めてください。

制約

  • 2 \le N \le 10^5
  • 1 \le M \le 10^5
  • 1 \le X_{\mathrm{AB}}, X_{\mathrm{AC}}, X_{\mathrm{BC}} \le 10^9
  • S は長さ N の、A, B, C からなる文字列
  • 1 \le A_i \le N
  • 1 \le B_i \le N
  • A_i \neq B_i
  • 1 \le C_i \le 10^9
  • どの 2 つの町も、その間を直接結んでいる道路は多くとも 1
  • どの 2 つの町も、いくつかの道路を辿って行き来できる
  • 入力は S 以外全て整数

入力

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

N M
X_{\mathrm{AB}} X_{\mathrm{AC}} X_{\mathrm{BC}}
S
A_1 B_1 C_1
A_2 B_2 C_2
A_3 B_3 C_3
\hspace{25pt} \vdots
A_M B_M C_M

出力

1 から町 N へ行く際に合計でたまる疲労度の最小値を出力せよ。


入力例 1

3 2
10 10 10
ABA
1 2 15
2 3 5

出力例 1

15

まず町 1 から町 2 に (タイプ A からタイプ B へ) ワープし、2 番目の道路を使って町 2 から町 3 へ行くと、10 + 5 = 15 の疲労度で到達でき、最適です。
1 と町 3 は両方タイプ A のワープ台があるので直接ワープすることはできないことに注意してください。


入力例 2

3 2
10 1000000000 10
ABC
1 2 1000000000
2 3 1000000000

出力例 2

20

1 から町 2 に (タイプ A からタイプ B へ) ワープし、更に町 2 から町 3 に (タイプ B からタイプ C へ) ワープすると 10 + 10 = 20 の疲労度で到達でき、最適です。
この場合町 1 から町 3 に直接ワープ可能ですが、タイプ A から タイプ C へのワープのため X_{\mathrm{AC}} = 1000000000 の疲労度がかかるので最適ではありません。


入力例 3

5 6
5 10 15
ABCBC
5 4 4
3 5 2
1 3 7
3 4 1
4 2 1
2 3 3

出力例 3

8

Score : 6 points

Warning

Do not make any mention of this problem until November 8, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

There are N towns numbered 1 through N, and M roads. The i-th road connects Town A_i and Town B_i bidirectionally, and you get C_i points of fatigue when you pass through that road. For every pair of towns, there is at most one road that directly connects those towns, and it is possible to travel between those towns by traversing some roads. Additionally, each town has a teleporter of type A, type B, or type C. The placement of teleporters is given to you by a string S; the teleporter in Town i is of type S_i. There may be some types of teleporters that do not exist in any towns. You can teleport between two towns with different types of teleporters. When you do so, you get the following points of fatigue:

  • X_{\mathrm{AB}} points when teleporting between towns with teleporters of type A and type B;
  • X_{\mathrm{AC}} points when teleporting between towns with teleporters of type A and type C;
  • X_{\mathrm{BC}} points when teleporting between towns with teleporters of type B and type C.

You cannot teleport between towns with the same type of teleporter. Find the minimum possible total points of fatigue you get when going from Town 1 to Town N only using roads and teleporters.

Constraints

  • 2 \le N \le 10^5
  • 1 \le M \le 10^5
  • 1 \le X_{\mathrm{AB}}, X_{\mathrm{AC}}, X_{\mathrm{BC}} \le 10^9
  • S is a string of length N consisting of A, B, and C.
  • 1 \le A_i \le N
  • 1 \le B_i \le N
  • A_i \neq B_i
  • 1 \le C_i \le 10^9
  • For every pair of towns, there is at most one road that directly connects those towns.
  • For every pair of towns, it is possible to travel between those towns by traversing some roads.
  • All values in input, except S, are integers.

Input

Input is given from Standard Input in the following format:

N M
X_{\mathrm{AB}} X_{\mathrm{AC}} X_{\mathrm{BC}}
S
A_1 B_1 C_1
A_2 B_2 C_2
A_3 B_3 C_3
\hspace{25pt} \vdots
A_M B_M C_M

Output

Find the minimum possible total points of fatigue you get when going from Town 1 to Town N.


Sample Input 1

3 2
10 10 10
ABA
1 2 15
2 3 5

Sample Output 1

15

If you first teleport from Town 1 (type A) to Town 2 (type B) and then use the 2-nd road to go from Town 2 to Town 3, you get 10 + 5 = 15 points of fatigue, which is optimal. Note that both Town 1 and Town 3 have a teleporter of type A, so you cannot directly teleport between them.


Sample Input 2

3 2
10 1000000000 10
ABC
1 2 1000000000
2 3 1000000000

Sample Output 2

20

If you first teleport from Town 1 (type A) to Town 2 (type B) and from Town 2 (type B) to Town C (type C), you will get 10 + 10 = 20 points of fatigue, which is optimal. Note that you can teleport from Town 1 to Town 3 directly this time, but it is not optimal since teleporting from a teleporter of type A to that of type C costs X_{\mathrm{AC}} = 1000000000 points of fatigue.


Sample Input 3

5 6
5 10 15
ABCBC
5 4 4
3 5 2
1 3 7
3 4 1
4 2 1
2 3 3

Sample Output 3

8
K - 転倒数

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 6

注意

この問題に対する言及は、2020/11/8 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

あなたは a_1 から a_KK 個の数列を持っています。 数列 a_i の長さは n_i で、先頭から j 番目の数は a_{i,j} です。

あなたはこれらの数列を使って、新しい数列 x を作ることにしました。 はじめ、x の長さは 0 です。

あなたは Q 回操作を行います。 i 回目の操作では、xx の末尾に数列 a_{b_i} を連結したものに置き換えます。

x の長さを |x| と書くことにします。 最終的な x において、1 \leq i < j \leq |x|x_i > x_j の両方を満たすような整数の組 (i,j) がいくつあるかを求めてください。 これは非常に大きくなりうるので、10^9 で割ったあまりを出力してください。

制約

  • 与えられる入力は全て整数
  • 1 \leq K,Q \leq 10^5
  • 1 \leq n_i \leq 10^5
  • 1 \leq a_{i,j} \leq 20
  • 1 \leq b_i \leq K
  • \sum_{i=1}^{K} n_i \leq 3 \times 10^5

入力

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

K
n_1
a_{1,1} a_{1,2} \cdots a_{1,{n_1}}
n_2
a_{2,1} a_{2,2} \cdots a_{2,{n_2}}
\vdots
n_K
a_{K,1} a_{K,2} \cdots a_{K,{n_K}}
Q
b_1 b_2 \cdots b_Q

出力

最終的な x における 1 \leq i < j \leq |x|x_i > x_j の両方を満たすような (i,j) の個数を 10^9 で割ったあまりを出力せよ。


入力例 1

2
3
1 3 2
2
5 4
2
1 2

出力例 1

2
  • x ははじめ空です。
  • 1 回目の操作により、xx(1,3,2) を連結した数列である (1,3,2) に置き換えられます。
  • 2 回目の操作により、xx(5,4) を連結した数列である (1,3,2,5,4) に置き換えられます。
  • 1 \leq i < j \leq |x|x_i > x_j の両方を満たす (i,j) の組は (2,3)(4,5)2 つです。

入力例 2

10
8
16 6 15 10 18 13 17 11
13
4 10 6 4 14 17 13 9 3 9 4 8 14
11
11 17 12 3 13 8 10 11 18 2 19
10
18 11 16 19 4 17 7 3 5 8
3
3 10 9
13
8 17 20 8 20 1 5 17 4 16 18 20 4
15
11 2 1 16 8 17 4 7 3 6 4 13 16 16 16
2
12 12
8
7 14 7 5 8 17 19 4
15
3 6 1 16 11 5 3 15 9 15 12 15 5 19 7
20
4 3 7 6 1 8 2 3 9 8 6 3 10 9 7 7 3 2 2 10

出力例 2

12430

Score : 6 points

Warning

Do not make any mention of this problem until November 8, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

You have K number sequences called a_1 through a_K. The length of the sequence a_i is n_i, and its j-th element is a_{i,j}.

Using those sequences, you have decided to make a new sequence x. Initially, the length of x is 0.

You will do Q operations. In the i-th of them, you will replace x with the result of concatenating the sequence a_{b_i} to the end of x.

Let |x| denotes the length of x. Find the number of pairs of integers (i,j) such that 1 \leq i < j \leq |x| and x_i > x_j after all the operations. Since it can be enormous, print it modulo 10^9.

Constraints

  • All values in input are integers.
  • 1 \leq K,Q \leq 10^5
  • 1 \leq n_i \leq 10^5
  • 1 \leq a_{i,j} \leq 20
  • 1 \leq b_i \leq K
  • \sum_{i=1}^{K} n_i \leq 3 \times 10^5

Input

Input is given from Standard Input in the following format:

K
n_1
a_{1,1} a_{1,2} \cdots a_{1,{n_1}}
n_2
a_{2,1} a_{2,2} \cdots a_{2,{n_2}}
\vdots
n_K
a_{K,1} a_{K,2} \cdots a_{K,{n_K}}
Q
b_1 b_2 \cdots b_Q

Output

Print the number of pairs of integers (i,j) such that 1 \leq i < j \leq |x| and x_i > x_j after all the operations, modulo 10^9.


Sample Input 1

2
3
1 3 2
2
5 4
2
1 2

Sample Output 1

2
  • Initially, x is empty.
  • In the 1-st operation, x is replaced with the concatenation of x and (1,3,2), that is, (1,3,2).
  • In the 2-st operation, x is replaced with the concatenation of x and (5,4), that is, (1,3,2,5,4).
  • Now, there are two pairs (i,j) such that 1 \leq i < j \leq |x| and x_i > x_j: (2,3) and (4,5).

Sample Input 2

10
8
16 6 15 10 18 13 17 11
13
4 10 6 4 14 17 13 9 3 9 4 8 14
11
11 17 12 3 13 8 10 11 18 2 19
10
18 11 16 19 4 17 7 3 5 8
3
3 10 9
13
8 17 20 8 20 1 5 17 4 16 18 20 4
15
11 2 1 16 8 17 4 7 3 6 4 13 16 16 16
2
12 12
8
7 14 7 5 8 17 19 4
15
3 6 1 16 11 5 3 15 9 15 12 15 5 19 7
20
4 3 7 6 1 8 2 3 9 8 6 3 10 9 7 7 3 2 2 10

Sample Output 2

12430
L - マンションの改築

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 6

注意

この問題に対する言及は、2020/11/8 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

1 から N の番号がついたマンションが番号の昇順で一列に並んでいます。 はじめ、マンション i の高さは h_i です。

Q 回の操作が順番に行われます。i 回目の操作の種類は t_{i} で表され、その内容は以下の通りです。

  • t_i=1 のとき: 整数 v_i が与えられる。番号が奇数であるような全てのマンションの高さを v_i だけ増やす。
  • t_i=2 のとき: 整数 v_i が与えられる。番号が偶数であるような全てのマンションの高さを v_i だけ増やす。
  • t_i=3 のとき: 整数 u_i,v_i が与えられる。マンション u_i の高さを v_i だけ増やす。

それぞれの操作が終わった時点で、1 \leq i < N を満たす整数 i のうち、マンション ii+1 の操作終了時点での高さが等しいようなものの個数を求めてください。

制約

  • 与えられる入力は全て整数
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq h_i, v_i \leq 10^9
  • t_i1,2,3 のいずれか
  • 1 \leq u_i \leq N

入力

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

N Q
h_1 h_2 \cdots h_N
\mathrm{Query}_{1}
\vdots
\mathrm{Query}_{Q}

\mathrm{Query}_{i} は以下の 3 つのいずれかである。

1 v_i
  • これは t_i=1 として操作を行うことを表す。
2 v_i
  • これは t_i=2 として操作を行うことを表す。
3 u_i v_i
  • これは t_i=3 として操作を行うことを表す。

出力

Q 行出力せよ。q 行目では、q 回の操作後、1 \leq i < N を満たす整数 i のうち、マンション ii+1 の操作終了時点での高さが等しいようなものの個数を出力せよ。


入力例 1

4 2
10 20 30 20
1 10
3 4 20

出力例 1

1
2
  • はじめ、マンション 1,2,3,4 の高さはそれぞれ 10,20,30,20 です。
  • 1 回目の操作により、番号が奇数であるようなマンションの高さが変化し、それぞれの高さは 20,20,40,20 へと変化します。
    • マンション ii+1 の高さが等しいような整数 i11 つです。
  • 2 回目の操作により、番号 4 のマンションの高さが変化し、それぞれの高さは 20,20,40,40 へと変化します。
    • マンション ii+1 の高さが等しいような整数 i1,32 つです。

入力例 2

10 20
108 112 112 110 110 109 108 110 111 112
3 4 2
1 1
3 9 1
3 4 2
2 1
1 1
3 7 2
2 1
3 8 2
1 1
2 1
1 1
3 10 2
3 6 1
2 1
3 7 1
1 1
2 1
3 3 2
3 3 2

出力例 2

2
2
1
1
2
1
0
3
3
0
3
0
0
0
4
3
1
3
3
2

Score : 6 points

Warning

Do not make any mention of this problem until November 8, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

There are N apartments numbered 1 through N, standing in a row in this order. Initially, the height of Apartment i is h_i.

Q operations will be done sequentially. The type of the i-th operation is represented by t_{i}, as follows:

  • If t_i=1: Given an integer v_i, increase the height of every odd-numbered apartment (Apartment 1, 3, ...) by v_i.
  • If t_i=2: Given an integer v_i, increase the height of every even-numbered apartment (Apartment 2, 4, ...) by v_i.
  • If t_i=2: Given integers u_i and v_i, increase the height of Apartment u_i apartment by v_i.

For each operation, find the number of integers i satisfying 1 \leq i < N such that the heights of Apartment i and Apartment i+1 are equal at the end of the operation.

Constraints

  • All values in input are integers.
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq h_i, v_i \leq 10^9
  • t_i is 1, 2, or 3.
  • 1 \leq u_i \leq N

Input

Input is given from Standard Input in the following format:

N Q
h_1 h_2 \cdots h_N
\mathrm{Query}_{1}
\vdots
\mathrm{Query}_{Q}

Here, \mathrm{Query}_{i} is one of the following:

1 v_i
  • This represents that the operation is performed with t_i=1.
2 v_i
  • This represents that the operation is performed with t_i=2.
3 u_i v_i
  • This represents that the operation is performed with t_i=3.

Output

Print Q lines. The q-th line should contain the number of integers i satisfying 1 \leq i < N such that the heights of Apartment i and Apartment i+1 are equal at the end of the q-th operation.


Sample Input 1

4 2
10 20 30 20
1 10
3 4 20

Sample Output 1

1
2
  • Initially, the heights of the apartments 1,2,3,4 are 10,20,30,20, respectively.
  • In the 1-st operation, the heights of the odd-numbered apartments change, resulting in the heights of the apartments being 20,20,40,20.
    • Now there is one integer i such that the heights of Apartment i and Apartment i+1 are equal: 1.
  • In the 2-nd operation, the height of Apartment 4 changes, resulting in the heights of the apartments being 20,20,40,40.
    • Now there are two integers i such that the heights of Apartment i and Apartment i+1 are equal: 1,3.

Sample Input 2

10 20
108 112 112 110 110 109 108 110 111 112
3 4 2
1 1
3 9 1
3 4 2
2 1
1 1
3 7 2
2 1
3 8 2
1 1
2 1
1 1
3 10 2
3 6 1
2 1
3 7 1
1 1
2 1
3 3 2
3 3 2

Sample Output 2

2
2
1
1
2
1
0
3
3
0
3
0
0
0
4
3
1
3
3
2
M - 筆塗り

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 6

注意

この問題に対する言及は、2020/11/8 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

1 から N の番号がついた N 個の頂点からなる木が与えられます。 1 から N-1 の番号がついた N-1 本の辺があり、辺 i は頂点 a_i と頂点 b_i を双方向につないでいます。 それぞれの辺には色を塗ることができます。色は 0 以上 10^5 以下の整数で表されます。 はじめ、全ての辺は色 0 で塗られています。

この木に対して、Q 回操作が行われます。i 回目の操作では、頂点 u_i と頂点 v_i の最短経路上にある辺の色が全て色 c_i で上書きされます。

Q 回の操作後、辺 1,2,\ldots,N-1 がどの色で塗られているかを調べてください。

制約

  • 与えられる入力は全て整数
  • 2 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq a_i, b_i,u_i,v_i \leq N
  • u_i \neq v_i
  • 1 \leq c_i \leq 10^5
  • 与えられるグラフは木

入力

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

N Q
a_1 b_1
\vdots
a_{N-1} b_{N-1}
u_1 v_1 c_1
\vdots
u_Q v_Q c_Q

出力

N-1 行出力せよ。i 行目では、辺 i に塗られている色を出力せよ。


入力例 1

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

出力例 1

5
10
0
  • はじめ、全ての辺は色 0 で塗られています。
  • 1 回目の操作では辺 12 の色が色 10 で上書きされます。
  • 2 回目の操作では辺 1 の色が色 5 で上書きされます。
  • 最終的に辺 1,2,3 はそれぞれ、5,10,0 で塗られています。

入力例 2

10 10
7 2
5 8
8 6
8 3
8 9
9 1
4 8
4 10
8 7
7 5 12773
2 6 74733
1 6 64470
7 2 41311
1 9 39776
4 8 71709
9 1 23551
4 6 29181
3 7 23742
8 4 54686

出力例 2

41311
12773
29181
23742
64470
23551
54686
0
23742

Score : 6 points

Warning

Do not make any mention of this problem until November 8, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Given is a tree with N vertices numbered 1 through N. The tree has N-1 edges numbered 1 through N-1, and Edge i connects Vertex a_i and b_i bidirectionally. Each edge can be painted in a color represented by an integer between 0 and 10^5 (inclusive). Initially, every edge is painted in Color 0.

Q operations will be done on this tree. In the i-th operation, the color of every edge on the shortest path from Vertex u_i to Vertex v_i becomes Color c_i.

Examine the color of the edges 1, 2, \ldots, N-1 after the Q operations.

Constraints

  • All values in input are integers.
  • 2 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq a_i, b_i,u_i,v_i \leq N
  • u_i \neq v_i
  • 1 \leq c_i \leq 10^5
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N Q
a_1 b_1
\vdots
a_{N-1} b_{N-1}
u_1 v_1 c_1
\vdots
u_Q v_Q c_Q

Output

Print N-1 lines. The i-th line should contain the color in which Edge i is painted.


Sample Input 1

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

Sample Output 1

5
10
0
  • Initially, every edge is painted in Color 0.
  • In the first operation, Edge 1 and Edge 2 are repainted in Color 10.
  • In the second operation, Edge 1 is repainted in Color 5.
  • In the end, the edges 1, 2, and 3 are painted in Color 5, 10, and 0, respectively.

Sample Input 2

10 10
7 2
5 8
8 6
8 3
8 9
9 1
4 8
4 10
8 7
7 5 12773
2 6 74733
1 6 64470
7 2 41311
1 9 39776
4 8 71709
9 1 23551
4 6 29181
3 7 23742
8 4 54686

Sample Output 2

41311
12773
29181
23742
64470
23551
54686
0
23742
N - マス目の穴埋め

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 6

注意

この問題に対する言及は、2020/11/8 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

186 列のマス目が与えられます。それぞれのマスには 0,1,? のいずれかが書かれており、上から i 行目、左から j 列目のマスには s_{i,j} が書かれています。

全ての ? が書かれたマスについて、書かれた文字を 01 のどちらかに書き換える方法のうち、書き換えを行った後のマス目が下記の条件を満たすようなものはいくつありますか?

条件:各マスに書かれた数は、そのマスと上下左右のマスに書かれた 5 つの数たちの中央値と等しい (マス目の外には 0 と書かれたマスが存在するものとして扱う)

制約

  • s_{i,j}01? のいずれか

入力

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

s_{1,1} \cdots s_{1,6}
\vdots
s_{18,1} \cdots s_{18,6}

出力

マス目が問題文中の条件を満たすような書き換え方の個数を出力せよ。


入力例 1

??0000
??0000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000

出力例 1

2
  • 全ての ? マスに 0 を書き込んだとき、あるいは全ての ? マスに 1 を書き込んだときのみ問題文中の条件が満たされます。

入力例 2

???000
???000
???000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000

出力例 2

16

入力例 3

?01000
1101?1
100111
1?11??
???00?
00011?
1?1??1
000101
100?11
1010??
?101??
?1??10
????10
?1??0?
1?1???
110?1?
0000?0
001?10

出力例 3

0
  • 条件を満たす書き込み方はありません。

入力例 4

??????
??????
??????
??????
??????
??????
??????
??????
??????
??????
??????
??????
??????
??????
??????
??????
??????
??????

出力例 4

243882696958399859

Score : 6 points

Warning

Do not make any mention of this problem until November 8, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Given is a grid with 18 rows and 6 columns. Each square has 0, 1, or ? written on it; written on the square at the i-th row from the top and j-th column from the left is s_{i,j}.

How many ways are there to replace each ? written on a square with 0 or 1 so that the grid will satisfy the following condition?

Condition: The number written on each square is equal to the median of the numbers written on the following five squares: the square itself and the squares horizontally or vertically adjacent to the square. (Assume that the area outside the grid is filled by squares with 0 written on them.)

Constraints

  • s_{i,j} is 0, 1, or ?.

Input

Input is given from Standard Input in the following format:

s_{1,1} \cdots s_{1,6}
\vdots
s_{18,1} \cdots s_{18,6}

Output

Print the number of ways to replace the characters so that the grid will satisfy the condition in the statement.


Sample Input 1

??0000
??0000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000

Sample Output 1

2
  • The condition is satisfied only if we write 0 in every ? square or write 1 in every ? square.

Sample Input 2

???000
???000
???000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000

Sample Output 2

16

Sample Input 3

?01000
1101?1
100111
1?11??
???00?
00011?
1?1??1
000101
100?11
1010??
?101??
?1??10
????10
?1??0?
1?1???
110?1?
0000?0
001?10

Sample Output 3

0
  • There is no way to write numbers that satisfies the condition.

Sample Input 4

??????
??????
??????
??????
??????
??????
??????
??????
??????
??????
??????
??????
??????
??????
??????
??????
??????
??????

Sample Output 4

243882696958399859
O - 宝箱

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 6

注意

この問題に対する言及は、2020/11/8 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

あなたは 1 から N の番号がついた N 個の宝箱を見つけました。 はじめ、全ての宝箱は施錠されており開けることはできません。 宝箱 i を解錠すると、あなたは a_i 円を得ることができます。

あなたには 1 から M の番号がついた M 人の鍵屋の友人がいます。 鍵屋 ic_i 円を払って解錠を依頼すると、宝箱 l_i, l_i + 1, \ldots, r_i のうち、まだ施錠されている宝箱全てが解錠されます。

あなたの目的は、鍵屋に支払った金額の総和を X、解錠された宝箱から得た金額の総和を Y として Y-X を最大化することです。Y-X としてありうる値の最大値を求めてください。

制約

  • 与えられる入力は全て整数
  • 1 \leq N,M \leq 2 \times 10^5
  • 1 \leq a_i,c_i \leq 10^9
  • 1 \leq l_i \leq r_i \leq N

入力

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

N M
a_1 a_2 \cdots a_N
l_1 r_1 c_i
\vdots
l_M r_M c_M

出力

鍵屋に支払った金額の総和を X、解錠された宝箱から得た金額の総和を Y として Y-X としてありうる値の最大値を出力せよ。


入力例 1

4 3
20 10 100 10
1 3 100
2 4 50
1 1 50

出力例 1

70
  • 鍵屋 250 円払い、宝箱 2,3,4 を解錠して 10+100+10=120 円を得るのが最適です。
  • 全ての宝箱を解錠する必要がないことに注意してください。

入力例 2

2 2
10 10
1 1 1000
1 2 100

出力例 2

0
  • どの鍵屋にも解錠を依頼しないのが最適な場合がありうることに注意してください。

入力例 3

20 10
40 28 12 29 34 89 37 64 48 53 81 95 46 42 77 76 49 59 14 15
2 11 221
14 20 14
2 11 126
1 8 273
5 11 94
2 8 48
12 15 83
2 7 13
5 16 269
3 12 115

出力例 3

760

Score : 6 points

Warning

Do not make any mention of this problem until November 8, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

You have found N treasure chests numbered 1 through N. Initially, all the chests are locked and cannot be opened. If you manage to unlock Chest i, you get a_i yen (the currency of Japan).

Among your friends, there are M locksmiths, numbered 1 through M. If you ask Locksmith i to unlock chests by paying c_i yen, the locksmith will unlock all chests that are still locked among the chests l_i, l_i + 1, \ldots, r_i.

Your objective is to maximize Y-X, where X is the total amount of money you pay to locksmiths, and Y is the total amount of money you get from chests. Find the maximum possible value of Y-X.

Constraints

  • All values in input are integers.
  • 1 \leq N,M \leq 2 \times 10^5
  • 1 \leq a_i,c_i \leq 10^9
  • 1 \leq l_i \leq r_i \leq N

Input

Input is given from Standard Input in the following format:

N M
a_1 a_2 \cdots a_N
l_1 r_1 c_i
\vdots
l_M r_M c_M

Output

Print the maximum possible value of Y-X, where X is the total amount of money you pay to locksmiths, and Y is the total amount of money you get from chests.


Sample Input 1

4 3
20 10 100 10
1 3 100
2 4 50
1 1 50

Sample Output 1

70
  • It is optimal to pay 50 yen to Locksmith 2 and get 10+100+10=120 yen from unlocking the chests 2,3,4.
  • Note that you do not have to unlock all the chests.

Sample Input 2

2 2
10 10
1 1 1000
1 2 100

Sample Output 2

0
  • Note that the optimal strategy might be not asking any locksmiths to unlock chests.

Sample Input 3

20 10
40 28 12 29 34 89 37 64 48 53 81 95 46 42 77 76 49 59 14 15
2 11 221
14 20 14
2 11 126
1 8 273
5 11 94
2 8 48
12 15 83
2 7 13
5 16 269
3 12 115

Sample Output 3

760