A - Buy a Pen

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

高橋君はペンを買うためにお店にやって来ました。お店では赤色のペンが一本 R 円、緑色のペンが一本 G 円、青色のペンが一本 B 円で売られています。

高橋君は色 C が嫌いです。CRed のときは赤色のペンを、Green のときは緑色のペンを、Blue のときは青色のペンを買うことができません。

高橋君がペンを一本買うために必要な金額の最小値を求めてください。

制約

  • 1\leq R,G,B\leq 100
  • R,G,B は整数
  • CRed,Green,Blue のいずれか

入力

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

R G B
C

出力

高橋君がペンを一本買うために必要な金額の最小値が X 円であるとき、X を出力せよ。


入力例 1

20 30 10
Blue

出力例 1

20

赤色のペンは 20 円で、緑色のペンは 30 円で、青色のペンは 10 円で売られています。高橋君は青色のペンを買うことができないので、20 円で赤色のペンを一本買うことができます。


入力例 2

100 100 100
Red

出力例 2

100

入力例 3

37 39 93
Blue

出力例 3

37

Score : 100 points

Problem Statement

Takahashi came to a store to buy a pen. Here, a red pen costs R yen, a green pen costs G yen, and a blue pen costs B yen.

Takahashi dislikes the color C. If C is Red, he cannot buy a red pen; if C is Green, he cannot buy a green pen; and if C is Blue, he cannot buy a blue pen.

Determine the minimum amount of money he needs to buy one pen.

Constraints

  • 1\leq R,G,B\leq 100
  • R, G, and B are integers.
  • C is Red, Green, or Blue.

Input

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

R G B
C

Output

If the minimum amount of money Takahashi needs to buy one pen is X yen, print X.


Sample Input 1

20 30 10
Blue

Sample Output 1

20

A red pen costs 20 yen, a green pen costs 30 yen, and a blue pen costs 10 yen. Takahashi cannot buy a blue pen, but he can buy a red pen for 20 yen.


Sample Input 2

100 100 100
Red

Sample Output 2

100

Sample Input 3

37 39 93
Blue

Sample Output 3

37
B - Right Triangle

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

xy 平面上に、同一直線上にない 3A(x_A,y_A), B(x_B,y_B),C(x_C,y_C) があります。三角形 ABC が直角三角形であるかどうか判定してください。

制約

  • -1000\leq x_A,y_A,x_B,y_B,x_C,y_C\leq 1000
  • 3A,B,C は同一直線上にない
  • 入力は全て整数

入力

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

x_A y_A
x_B y_B
x_C y_C

出力

三角形 ABC が直角三角形であるならば Yes を、そうでないならば No を出力せよ。


入力例 1

0 0
4 0
0 3

出力例 1

Yes

三角形 ABC は直角三角形です。


入力例 2

-4 3
2 1
3 4

出力例 2

Yes

三角形 ABC は直角三角形です。


入力例 3

2 4
-3 2
1 -2

出力例 3

No

三角形 ABC は直角三角形ではありません。

Score : 200 points

Problem Statement

In the xy-plane, there are three points A(x_A, y_A), B(x_B, y_B), and C(x_C, y_C) that are not collinear. Determine whether the triangle ABC is a right triangle.

Constraints

  • -1000 \leq x_A, y_A, x_B, y_B, x_C, y_C \leq 1000
  • The three points A, B, and C are not collinear.
  • All input values are integers.

Input

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

x_A y_A
x_B y_B
x_C y_C

Output

Print Yes if the triangle ABC is a right triangle, and No otherwise.


Sample Input 1

0 0
4 0
0 3

Sample Output 1

Yes

The triangle ABC is a right triangle.


Sample Input 2

-4 3
2 1
3 4

Sample Output 2

Yes

The triangle ABC is a right triangle.


Sample Input 3

2 4
-3 2
1 -2

Sample Output 3

No

The triangle ABC is not a right triangle.

C - Sum = 0

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

N 個の整数の組 (L_1,R_1),(L_2,R_2),\ldots,(L_N,R_N) が与えられます。

以下の条件を満たす長さ N の整数列 X=(X_1,X_2,\ldots,X_N) が存在するか判定し、存在するならば一つ出力してください。

  • i=1,2,\ldots,N に対して L_i\leq X_i\leq R_i
  • \displaystyle \sum_{i=1}^N X_i=0

制約

  • 1\leq N\leq 2\times 10^5
  • -10^9\leq L_i\leq R_i\leq 10^9
  • 入力は全て整数

入力

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

出力

存在しない場合は No を出力せよ。存在する場合は条件を満たす整数列 X を以下の形式で出力せよ。

Yes
X_1 X_2 \ldots X_N

答えが複数存在する場合、どれを出力しても正解とみなされる。


入力例 1

3
3 5
-4 1
-2 3

出力例 1

Yes
4 -3 -1

数列 X=(4,-3,-1) は問題の条件をすべて満たします。ほかにも (3,-3,0)(5,-4,-1) などが条件を満たします。


入力例 2

3
1 2
1 2
1 2

出力例 2

No

条件を満たす整数列 X は存在しません。


入力例 3

6
-87 12
-60 -54
2 38
-76 6
87 96
-17 38

出力例 3

Yes
-66 -57 31 -6 89 9

Score : 350 points

Problem Statement

You are given N pairs of integers (L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N).

Determine whether there exists a sequence of N integers X = (X_1, X_2, \ldots, X_N) that satisfies the following conditions, and print one such sequence if it exists.

  • L_i \leq X_i \leq R_i for each i = 1, 2, \ldots, N.
  • \displaystyle \sum_{i=1}^N X_i = 0.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • -10^9 \leq L_i \leq R_i \leq 10^9
  • All input values are integers.

Input

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

Output

If no solution exists, print No. Otherwise, print an integer sequence X that satisfies the conditions in the following format:

Yes
X_1 X_2 \ldots X_N

If multiple solutions exist, any of them will be considered correct.


Sample Input 1

3
3 5
-4 1
-2 3

Sample Output 1

Yes
4 -3 -1

The sequence X = (4, -3, -1) satisfies all the conditions. Other valid sequences include (3, -3, 0) and (5, -4, -1).


Sample Input 2

3
1 2
1 2
1 2

Sample Output 2

No

No sequence X satisfies the conditions.


Sample Input 3

6
-87 12
-60 -54
2 38
-76 6
87 96
-17 38

Sample Output 3

Yes
-66 -57 31 -6 89 9
D - Shortest Path 3

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425

問題文

N 頂点 M 辺の単純連結無向グラフが与えられます。頂点 i\,(1\leq i \leq N) は重み A_i を持ちます。また、辺 j\,(1\leq j \leq M) は頂点 U_j,V_j を双方向に結び、重み B_j を持ちます。

このグラフ上のパスの重みを、パス上に現れる頂点の重みと辺の重みの総和と定義します。

i=2,3,\dots,N について、以下の問題を解いてください。

  • 頂点 1 から頂点 i までのパスであって、重みが最小となるものの重みを求めよ。

制約

  • 2 \leq N \leq 2 \times 10^5
  • N-1 \leq M \leq 2 \times 10^5
  • 1 \leq U_j < V_j \leq N
  • i\neq j なら (U_i,V_i) \neq (U_j,V_j)
  • グラフは連結である
  • 0 \leq A_i \leq 10^9
  • 0 \leq B_j \leq 10^9
  • 入力はすべて整数

入力

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

N M
A_1 A_2 \dots A_N
U_1 V_1 B_1
U_2 V_2 B_2
\vdots
U_M V_M B_M

出力

i=2,3,\dots,N に対する答えを、この順に空白区切りで一行で出力せよ。


入力例 1

3 3
1 2 3
1 2 1
1 3 6
2 3 2

出力例 1

4 9

頂点 1 から頂点 2 へのパスを考えます。 パス 1 \to 2 の重みは A_1+B_1+A_2=1+1+2=4、パス 1 \to 3 \to 2 の重みは A_1+B_2+A_3+B_3+A_2=1+6+3+2+2=14 であり、最小の重みは 4 です。

頂点 1 から頂点 3 へのパスを考えます。 パス 1 \to 3 の重みは A_1+B_2+A_3=1+6+3=10、パス 1 \to 2 \to 3 の重みは A_1+B_1+A_2+B_3+A_3=1+1+2+2+3=9 であり、最小の重みは 9 です。


入力例 2

2 1
0 1
1 2 3

出力例 2

4

入力例 3

5 8
928448202 994752369 906965437 942744902 907560126
2 5 975090662
1 2 908843627
1 5 969061140
3 4 964249326
2 3 957690728
2 4 942986477
4 5 948404113
1 3 988716403

出力例 3

2832044198 2824130042 4696218483 2805069468

答えが 32-bit 整数に収まらないことがあることに注意してください。

Score : 425 points

Problem Statement

You are given a simple connected undirected graph with N vertices and M edges. Each vertex i\,(1\leq i \leq N) has a weight A_i. Each edge j\,(1\leq j \leq M) connects vertices U_j and V_j bidirectionally and has a weight B_j.

The weight of a path in this graph is defined as the sum of the weights of the vertices and edges that appear on the path.

For each i=2,3,\dots,N, solve the following problem:

  • Find the minimum weight of a path from vertex 1 to vertex i.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • N-1 \leq M \leq 2 \times 10^5
  • 1 \leq U_j < V_j \leq N
  • (U_i, V_i) \neq (U_j, V_j) if i \neq j.
  • The graph is connected.
  • 0 \leq A_i \leq 10^9
  • 0 \leq B_j \leq 10^9
  • All input values are integers.

Input

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

N M
A_1 A_2 \dots A_N
U_1 V_1 B_1
U_2 V_2 B_2
\vdots
U_M V_M B_M

Output

Print the answers for i=2,3,\dots,N in a single line, separated by spaces.


Sample Input 1

3 3
1 2 3
1 2 1
1 3 6
2 3 2

Sample Output 1

4 9

Consider the paths from vertex 1 to vertex 2. The weight of the path 1 \to 2 is A_1 + B_1 + A_2 = 1 + 1 + 2 = 4, and the weight of the path 1 \to 3 \to 2 is A_1 + B_2 + A_3 + B_3 + A_2 = 1 + 6 + 3 + 2 + 2 = 14. The minimum weight is 4.

Consider the paths from vertex 1 to vertex 3. The weight of the path 1 \to 3 is A_1 + B_2 + A_3 = 1 + 6 + 3 = 10, and the weight of the path 1 \to 2 \to 3 is A_1 + B_1 + A_2 + B_3 + A_3 = 1 + 1 + 2 + 2 + 3 = 9. The minimum weight is 9.


Sample Input 2

2 1
0 1
1 2 3

Sample Output 2

4

Sample Input 3

5 8
928448202 994752369 906965437 942744902 907560126
2 5 975090662
1 2 908843627
1 5 969061140
3 4 964249326
2 3 957690728
2 4 942986477
4 5 948404113
1 3 988716403

Sample Output 3

2832044198 2824130042 4696218483 2805069468

Note that the answers may not fit in a 32-bit integer.

E - Count Arithmetic Subsequences

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

長さ N の数列 A=(A_1,A_2,\dots,A_N) が与えられます。各 k=1,2,\dots,N について、A の長さ k の(連続するとは限らない)部分列であって等差数列であるようなものの個数を 998244353 で割ったあまりを求めてください。ただし、2 つの部分列が列として同じでも、取り出す位置が異なるならば区別するものとします。

部分列とは 数列 A の部分列とは、A の要素を 0 個以上選んで削除し、残った要素を元の順序を保って並べた数列のことを指します。

制約

  • 1 \leq N \leq 80
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数

入力

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

N
A_1 A_2 \dots A_N

出力

k=1,2,\dots,N に対する答えを、この順に空白区切りで一行で出力せよ。


入力例 1

5
1 2 3 2 3

出力例 1

5 10 3 0 0
  • 長さ 1 の部分列は全部で 5 個あり、これらはすべて長さ 1 の等差数列です。
  • 長さ 2 の部分列は全部で 10 個あり、これらはすべて長さ 2 の等差数列です。
  • 長さ 3 の部分列であって等差数列であるものは、(A_1,A_2,A_3),(A_1,A_2,A_5),(A_1,A_4,A_5)3 つです。
  • 長さ 4 以上の部分列であって等差数列であるものは存在しません。

入力例 2

4
1 2 3 4

出力例 2

4 6 2 1

入力例 3

1
100

出力例 3

1

Score : 475 points

Problem Statement

You are given a sequence A = (A_1, A_2, \dots, A_N) of length N. For each k = 1, 2, \dots, N, find the number, modulo 998244353, of (not necessarily contiguous) subsequences of A of length k that are arithmetic sequences. Two subsequences are distinguished if they are taken from different positions, even if they are equal as sequences.

What is a subsequence? A subsequence of a sequence A is a sequence obtained by deleting zero or more elements from A and arranging the remaining elements without changing the order.

Constraints

  • 1 \leq N \leq 80
  • 1 \leq A_i \leq 10^9
  • All input values are integers.

Input

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

N
A_1 A_2 \dots A_N

Output

Print the answers for k = 1, 2, \dots, N in this order, in a single line, separated by spaces.


Sample Input 1

5
1 2 3 2 3

Sample Output 1

5 10 3 0 0
  • There are 5 subsequences of length 1, all of which are arithmetic sequences.
  • There are 10 subsequences of length 2, all of which are arithmetic sequences.
  • There are 3 subsequences of length 3 that are arithmetic sequences: (A_1, A_2, A_3), (A_1, A_2, A_5), and (A_1, A_4, A_5).
  • There are no arithmetic subsequences of length 4 or more.

Sample Input 2

4
1 2 3 4

Sample Output 2

4 6 2 1

Sample Input 3

1
100

Sample Output 3

1
F - Perfect Matching on a Tree

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 550

問題文

N 頂点の木 T が与えられます。T の頂点には 1 から N の番号がついており、 i\,(1\leq i \leq N-1) 番目の辺は頂点 u_i と頂点 v_i を双方向に結んでいます。

T を用いて、N 頂点の完全グラフ G を次のように定めます。

  • G の頂点 x と頂点 y の間の辺の重み w(x,y) を、T における頂点 x と頂点 y の間の最短距離とする

G最大重み最大マッチングを一つ求めてください。すなわち、\lfloor N/2 \rfloor 個の頂点のペアの集合 M=\{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\} であって、各頂点 1,2,\dots, NM に現れる回数がたかだか 1 回であるようなもののうち、 \displaystyle \sum_{i=1}^{\lfloor N/2 \rfloor} w(x_i,y_i) が最大であるものを一つ求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq u_i < v_i \leq N
  • 入力されるグラフは木である
  • 入力はすべて整数

入力

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

N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

出力

答えを \{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\} として、以下の形式で出力せよ。答えが複数あり得る場合、そのうちどれを出力しても良い。

x_1 y_1
x_2 y_2
\vdots
x_{\lfloor N/2 \rfloor} y_{\lfloor N/2 \rfloor}

入力例 1

4
1 2
2 3
3 4

出力例 1

2 4
1 3

T において、頂点 2,4 間の距離は 2、頂点 1,3 間の距離は 2 なので、マッチング \{(2,4),(1,3)\} の重みは 4 です。重みが 4 より大きいマッチングは存在しないので、これが最大重み最大マッチングの一つです。他にも、

2 3
1 4

などを出力しても正解になります。


入力例 2

3
1 2
2 3

出力例 2

1 3

T において、頂点 1,3 間の距離は 2 なので、マッチング \{(1,3)\} の重みは 2 です。重みが 2 より大きいマッチングは存在しないので、これが最大重み最大マッチングの一つです。他にも、

3 1

を出力しても正解になります。

Score : 550 points

Problem Statement

You are given a tree T with N vertices. The vertices are numbered 1 to N, and the i-th edge (1 \leq i \leq N-1) connects vertices u_i and v_i bidirectionally.

Using T, define a complete graph G with N vertices as follows:

  • The weight w(x,y) of the edge between vertices x and y in G is the shortest distance between vertices x and y in T.

Find one maximum weight maximum matching in G. That is, find a set of \lfloor N/2 \rfloor pairs of vertices M=\{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\} such that each vertex 1,2,\dots, N appears in M at most once, and \displaystyle \sum_{i=1}^{\lfloor N/2 \rfloor} w(x_i,y_i) is maximized.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq u_i < v_i \leq N
  • The input graph is a tree.
  • All input values are integers.

Input

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

N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

Output

Print a solution as \{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\} in the following format. If multiple solutions exist, any of them is acceptable.

x_1 y_1
x_2 y_2
\vdots
x_{\lfloor N/2 \rfloor} y_{\lfloor N/2 \rfloor}

Sample Input 1

4
1 2
2 3
3 4

Sample Output 1

2 4
1 3

In T, the distance between vertices 2 and 4 is 2, and the distance between vertices 1 and 3 is 2, so the weight of the matching \{(2,4),(1,3)\} is 4. There is no matching with a weight greater than 4, so this is a maximum weight maximum matching. Other acceptable outputs include:

2 3
1 4

Sample Input 2

3
1 2
2 3

Sample Output 2

1 3

In T, the distance between vertices 1 and 3 is 2, so the weight of the matching \{(1,3)\} is 2. There is no matching with a weight greater than 2, so this is a maximum weight maximum matching. Another acceptable output is:

3 1
G - Count Substring Query

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 575

問題文

英小文字からなる文字列 S が与えられます。

Q 個のクエリが与えられるので順に処理してください。i 番目のクエリは以下で説明されます。

  • 英小文字からなる文字列 T_i が与えられる。S の部分文字列であって、T_i と一致するものは何通りあるか出力する。ただし、ある二つの部分文字列が文字列として同じでも、取り出された位置が異なるならばそれらは別々に数えるものとする。

制約

  • 1\leq |S|\leq 5\times 10^5
  • 1\leq Q\leq 5\times 10^5
  • 1\leq |T_i|\leq |S|
  • \displaystyle \sum_{i=1}^Q |T_i|\leq 5\times 10^5
  • S,T_i は英小文字列
  • Q は整数

入力

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

S
Q
T_1
T_2
\vdots
T_Q

出力

Q 行出力せよ。i 行目には i 番目のクエリに対する答えを出力せよ。


入力例 1

missisippi
5
i
s
a
is
missisippi

出力例 1

4
3
0
2
1

Sl 文字目から r 文字目までの部分文字列を S[l:r] とします。

  • 1 つ目のクエリについて、S の部分文字列であって i と一致するものは S[2:2],S[5:5],S[7:7],S[10:10]4 か所です。
  • 2 つ目のクエリについて、S の部分文字列であって s と一致するものは S[3:3],S[4:4],S[6:6]3 か所です。
  • 3 つ目のクエリについて、S の部分文字列であって a と一致するものは存在しません。
  • 4 つ目のクエリについて、S の部分文字列であって is と一致するものは S[2:3],S[5:6]2 か所です。
  • 5 つ目のクエリについて、S の部分文字列であって missisippi と一致するものは S[1:10]1 か所です。

入力例 2

aaaaaa
6
a
aa
aaa
aaaa
aaaaa
aaaaaa

出力例 2

6
5
4
3
2
1

Score : 575 points

Problem Statement

You are given a string S consisting of lowercase English letters.

You are also given Q queries to process sequentially. The i-th query is described as follows:

  • A string T_i consisting of lowercase English letters is given. Print the number of substrings of S that equal T_i. Two substrings are distinguished if they are taken from different positions, even if they are equal as strings.

Constraints

  • 1 \leq |S| \leq 5 \times 10^5
  • 1 \leq Q \leq 5 \times 10^5
  • 1 \leq |T_i| \leq |S|
  • \displaystyle \sum_{i=1}^Q |T_i| \leq 5 \times 10^5
  • S and T_i are strings consisting of lowercase English letters.
  • Q is an integer.

Input

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

S
Q
T_1
T_2
\vdots
T_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.


Sample Input 1

missisippi
5
i
s
a
is
missisippi

Sample Output 1

4
3
0
2
1

Let S[l:r] denote the substring of S from the l-th character through the r-th character.

  • For the 1st query, four substrings of S equal i: S[2:2], S[5:5], S[7:7], S[10:10].
  • For the 2nd query, three substrings of S equal s: S[3:3], S[4:4], S[6:6].
  • For the 3rd query, no substrings of S match a.
  • For the 4th query, two substrings of S equal is: S[2:3], S[5:6].
  • For the 5th query, one substring of S equals missisippi: S[1:10].

Sample Input 2

aaaaaa
6
a
aa
aaa
aaaa
aaaaa
aaaaaa

Sample Output 2

6
5
4
3
2
1