A - First Contest of the Year

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

ある星では 1 年が D 日あり、7 日ごとにコンテストが開催されています。コンテストが日をまたいで開催されることはありません。

ある年の最初のコンテストは 1 年のうち F 日目に開催されました。その次の年の最初のコンテストは 1 年のうち何日目に開催されますか?

なお、この問題の制約のもとでは、次の年にコンテストが 1 回以上開催されることが保証されます。

制約

  • 10 \leq D \leq 366
  • 1 \leq F \leq 7
  • 入力される値はすべて整数

入力

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

D F

出力

答えを出力せよ。


入力例 1

365 4

出力例 1

3

ある年の最初のコンテストの開催が 1 年のうち 4 日目だったとき、次の年の最初のコンテストの開催は 1 年のうち 3 日目になります。


入力例 2

10 5

出力例 2

2

Score : 100 points

Problem Statement

On a certain planet, a year has D days, and contests are held every 7 days. Contests never span across days.

The first contest of a certain year was held on the F-th day of the year. On what day of the next year will the first contest of the next year be held?

The constraints of this problem guarantee that at least one contest will be held in the next year.

Constraints

  • 10 \leq D \leq 366
  • 1 \leq F \leq 7
  • All input values are integers.

Input

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

D F

Output

Print the integer N such that the next contest is held on the N-th day of the year.


Sample Input 1

365 4

Sample Output 1

3

If the first contest of a certain year was held on the 4th day of the year, the first contest of the next year will be held on the 3rd day of the year.


Sample Input 2

10 5

Sample Output 2

2
B - Substring 2

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

整数 N,M と長さ N の数字列 S 、長さ M の数字列 T が与えられます。ここで、数字列とは 0 から 9 までの数字のみから構成される文字列のことを表します。

あなたは以下の操作を 0 回以上好きな回数行うことができます:

  • T から 1 文字選び、選んだ数字を 1 増やす。ただし、選んだ数字が 9 である場合は 0 にする。

TS の部分文字列(連続する部分列)にするために必要な操作回数の最小値を求めてください。

制約

  • 1\le M\le N\le 100
  • N,M は整数
  • S は長さ N の数字列
  • T は長さ M の数字列

入力

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

N M
S
T

出力

TS の部分文字列にするために必要な操作回数の最小値を出力せよ。


入力例 1

4 2
2025
91

出力例 1

2

以下のように 2 回操作することで TS の部分文字列にすることができます。

  • T2 文字目に対して操作する。 T= 91 から T= 92 になる。
  • T1 文字目に対して操作する。 T= 92 から T= 02 になる。

02S2 文字目から 3 文字目までの部分文字列となっています。

2 回未満の操作で TS の部分文字列にすることはできないため、 2 を出力してください。


入力例 2

3 2
438
38

出力例 2

0

はじめから 38438 の部分文字列です。したがって、0 を出力してください。


入力例 3

5 5
00000
11111

出力例 3

45

入力例 4

8 3
20251227
438

出力例 4

13

Score : 200 points

Problem Statement

You are given integers N and M, a digit string S of length N, and a digit string T of length M. Here, a digit string is a string consisting of digits from 0 to 9.

You can perform the following operation zero or more times:

  • Choose one character from T and increase the chosen digit by 1. However, if the chosen digit is 9, change it to 0.

Find the minimum number of operations required to make T a substring (contiguous subsequence) of S.

Constraints

  • 1\le M\le N\le 100
  • N and M are integers.
  • S is a digit string of length N.
  • T is a digit string of length M.

Input

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

N M
S
T

Output

Output the minimum number of operations required to make T a substring of S.


Sample Input 1

4 2
2025
91

Sample Output 1

2

You can make T a substring of S with two operations as follows:

  • Perform the operation on the 2nd character of T. T= 91 becomes T= 92.
  • Perform the operation on the 1st character of T. T= 92 becomes T= 02.

02 is a substring from the 2nd character to the 3rd character of S.

It is impossible to make T a substring of S with less than two operations, so output 2.


Sample Input 2

3 2
438
38

Sample Output 2

0

38 is a substring of 438 from the beginning. Thus, output 0.


Sample Input 3

5 5
00000
11111

Sample Output 3

45

Sample Input 4

8 3
20251227
438

Sample Output 4

13
C - 1D puyopuyo

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

あなたは以下の操作を 0 回以上好きな順番で好きな回数行うことができます:

  • A_k=A_{k+1}=A_{k+2}=A_{k+3} を満たす 1 以上 |A|-3 以下の整数 k を選び、A から A_k,A_{k+1},A_{k+2},A_{k+3} を削除する。(より厳密には、 A(A_1,A_2,\ldots,A_{k-1},A_{k+4},A_{k+5},\ldots,A_N) に置き換える。)

ここで、 |A| は整数列 A の長さを表します。

操作を繰り返した後の最終的な |A| としてあり得る最小値を求めてください。

制約

  • 1\le N\le 2\times 10^5
  • 1\le A_i\le N
  • 入力される値は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

操作を繰り返した後の最終的な |A| としてあり得る最小値を出力せよ。


入力例 1

10
1 1 1 4 4 4 4 1 2 3

出力例 1

2

以下のように 2 回操作することで |A|=2 にすることができます。

  • k=4 を選ぶ。A_4=A_5=A_6=A_7=4 が成り立つためこの選択は正当である。 A=(1,1,1,1,2,3) となる。
  • k=1 を選ぶ。A_1=A_2=A_3=A_4=1 が成り立つためこの選択は正当である。 A=(2,3) となる。

|A|2 未満にすることはできないため、 2 を出力してください。


入力例 2

3
2 1 3

出力例 2

3

はじめから操作を行うことができません。


入力例 3

13
1 1 4 4 4 1 1 1 1 4 1 4 1

出力例 3

5

Score : 300 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N.

You can perform the following operation zero or more times in any order:

  • Choose an integer k between 1 and |A|-3, inclusive, such that A_k=A_{k+1}=A_{k+2}=A_{k+3}, and remove A_k,A_{k+1},A_{k+2},A_{k+3} from A. (More formally, replace A with (A_1,A_2,\ldots,A_{k-1},A_{k+4},A_{k+5},\ldots,A_N).)

Here, |A| represents the length of the integer sequence A.

Find the minimum possible value of the final |A| after repeating the operation.

Constraints

  • 1\le N\le 2\times 10^5
  • 1\le A_i\le N
  • All input values are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Output the minimum possible value of the final |A| after repeating the operation.


Sample Input 1

10
1 1 1 4 4 4 4 1 2 3

Sample Output 1

2

You can make |A|=2 with two operations as follows:

  • Choose k=4. This choice is valid since A_4=A_5=A_6=A_7=4 holds. A=(1,1,1,1,2,3) is obtained.
  • Choose k=1. This choice is valid since A_1=A_2=A_3=A_4=1 holds. A=(2,3) is obtained.

It is impossible to make |A| less than 2, so output 2.


Sample Input 2

3
2 1 3

Sample Output 2

3

You cannot perform any operation from the beginning.


Sample Input 3

13
1 1 4 4 4 1 1 1 1 4 1 4 1

Sample Output 3

5
D - Tail of Snake

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

すぬけ君は蛇を観察しており、どこからどこまでが頭・胴・尾なのかが気になっています。 すぬけ君は蛇を N 個のブロックに区切り、それぞれのブロックについて頭らしさ・胴らしさ・尾らしさを評価しました。 そして、らしさの合計が最大になる分け方を考えることにしました。

長さ N の整数列 A = (A_1, A_2, \ldots, A_N), B = (B_1, B_2, \ldots, B_N), C = (C_1, C_2, \ldots, C_N) が与えられます。

整数の組 (x, y)1 \leq x < y < N を満たすとき、\displaystyle\sum_{i = 1}^{x} A_i + \sum_{i = x + 1}^{y} B_i + \sum_{i = y + 1}^{N} C_i の値として取り得る最大値を求めてください。

制約

  • 3 \leq N \leq 3 \times 10^5
  • 1 \leq A_i, B_i, C_i \leq 10^6
  • 入力される値はすべて整数

入力

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

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N
C_1 C_2 \ldots C_N

出力

答えを出力せよ。


入力例 1

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

出力例 1

16

(x, y) = (2, 3) とすると \displaystyle\sum_{i = 1}^{x} A_i + \sum_{i = x + 1}^{y} B_i + \sum_{i = y + 1}^{N} C_i = 1 + 4 + 4 + 4 + 3 = 16 となります。


入力例 2

3
1 1 1
1 1 1
1 1 1

出力例 2

3

入力例 3

6
2 10 7 7 7 11
5 7 9 10 9 12
6 6 7 10 12 7

出力例 3

50

Score : 400 points

Problem Statement

Snuke is observing a snake and is curious about which parts are the head, body, and tail. He divided the snake into N blocks and evaluated the head-likeness, body-likeness, and tail-likeness of each block. Then, he decided to find the division that maximizes the sum of the likeness values.

You are given length-N integer sequences A = (A_1, A_2, \ldots, A_N), B = (B_1, B_2, \ldots, B_N), and C = (C_1, C_2, \ldots, C_N).

Find the maximum possible value of \displaystyle\sum_{i = 1}^{x} A_i + \sum_{i = x + 1}^{y} B_i + \sum_{i = y + 1}^{N} C_i for a pair of integers (x, y) satisfying 1 \leq x < y < N.

Constraints

  • 3 \leq N \leq 3 \times 10^5
  • 1 \leq A_i, B_i, C_i \leq 10^6
  • All input values are integers.

Input

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

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N
C_1 C_2 \ldots C_N

Output

Output the answer.


Sample Input 1

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

Sample Output 1

16

With (x, y) = (2, 3), we have \displaystyle\sum_{i = 1}^{x} A_i + \sum_{i = x + 1}^{y} B_i + \sum_{i = y + 1}^{N} C_i = 1 + 4 + 4 + 4 + 3 = 16.


Sample Input 2

3
1 1 1
1 1 1
1 1 1

Sample Output 2

3

Sample Input 3

6
2 10 7 7 7 11
5 7 9 10 9 12
6 6 7 10 12 7

Sample Output 3

50
E - Heavy Buckets

実行時間制限: 3 sec / メモリ制限: 1024 MiB

配点 : 475

問題文

N 人の人がおり、N 個のバケツがあります。人とバケツにはそれぞれ 1, 2, \ldots, N の番号が付けられています。

i ははじめバケツ i のみを持っており、バケツ i には何も入っていません。

これから、以下の操作を 10^9 回行います。

  • i = 1, 2, \ldots, N について同時に、人 i が持っているバケツすべてに水を i ずつ入れ、それらのバケツを人 A_i に渡す。

ただし、バケツに入れることのできる水の量に制限はありません。

i = 1, 2, \ldots, Q について以下の形式のクエリに答えてください。

  • T_i 回目の操作の直後にバケツ B_i に入っている水の量を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq N
  • 1 \leq T_i \leq 10^9
  • 1 \leq B_i \leq N
  • 入力される値はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_N
T_1 B_1
T_2 B_2
\vdots
T_Q B_Q

出力

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


入力例 1

5 6
3 4 2 2 5
4 3
6 5
1 4
10 1
10 2
1000000000 1

出力例 1

11
30
4
28
30
2999999998

1 番目のクエリに対する説明を行います。

はじめ、バケツ 3 は人 3 が持っています。

1 回目の操作の直後、バケツ 3 は人 2 が持っており、水が 3 入っています。

2 回目の操作の直後、バケツ 3 は人 4 が持っており、水が 5 入っています。

3 回目の操作の直後、バケツ 3 は人 2 が持っており、水が 9 入っています。

4 回目の操作の直後、バケツ 3 は人 4 が持っており、水が 11 入っています。

したがって、1 番目のクエリに対する答えは 11 となります。

Score : 475 points

Problem Statement

There are N people and N buckets. Both the people and buckets are numbered 1, 2, \ldots, N.

Initially, person i holds only bucket i, and bucket i is empty.

From now on, the following operation will be performed 10^9 times:

  • For i = 1, 2, \ldots, N simultaneously, person i adds i units of water to each of the buckets they are holding and passes those buckets to person A_i.

Here, there is no limit on the amount of water that can be put in a bucket.

For i = 1, 2, \ldots, Q, answer the following queries:

  • Find the amount of water in bucket B_i immediately after the T_i-th operation.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq N
  • 1 \leq T_i \leq 10^9
  • 1 \leq B_i \leq N
  • 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
T_1 B_1
T_2 B_2
\vdots
T_Q B_Q

Output

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


Sample Input 1

5 6
3 4 2 2 5
4 3
6 5
1 4
10 1
10 2
1000000000 1

Sample Output 1

11
30
4
28
30
2999999998

We will explain the first query.

Initially, bucket 3 is held by person 3.

Immediately after the 1-st operation, bucket 3 is held by person 2 and contains 3 units of water.

Immediately after the 2-nd operation, bucket 3 is held by person 4 and contains 5 units of water.

Immediately after the 3-rd operation, bucket 3 is held by person 2 and contains 9 units of water.

Immediately after the 4-th operation, bucket 3 is held by person 4 and contains 11 units of water.

Thus, the answer to the first query is 11.

F - Sum of Mex

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 525

問題文

N 頂点の木 T が与えられます。各頂点には 0 から N-1 までの番号がついており、 i 番目 (1\le i\le N-1) の辺は頂点 u_i と頂点 v_i を双方向に繋いでいます。(頂点番号は 0-indexed で辺番号が 1-indexed であることに注意してください。)

0 以上 N 未満の整数の組 (i,j) に対し f(i,j) を以下のように定めます:

  • T の頂点 i から頂点 j までのパスに 含まれない 頂点のうち番号が最も小さい頂点の頂点番号。
    • ただし、頂点 i から頂点 j までのパスに頂点 0 から頂点 N-1 まで全ての頂点が含まれる場合 f(i,j)=N とする。

T の頂点 i から頂点 j までのパスに頂点 i,j も含まれることに注意してください。

\displaystyle \sum_{0\le i \le j < N} f(i,j) の値を求めてください。

制約

  • 2\le N\le 2\times 10^5
  • 0\le u_i < v_i < N
  • 入力で与えられるグラフは木
  • 入力される値は全て整数

入力

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

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

出力

\displaystyle \sum_{0\le i \le j < N} f(i,j) の値を出力せよ。


入力例 1

2
0 1

出力例 1

3

f(0,0)=1,f(0,1)=2,f(1,1)=0 です。したがって、 1+2+0=3 を出力してください。


入力例 2

5
0 1
0 2
2 3
2 4

出力例 2

16

入力例 3

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

出力例 3

16

Score : 525 points

Problem Statement

You are given a tree T with N vertices. The vertices are numbered from 0 to N-1, and the i-th edge (1\le i\le N-1) bidirectionally connects vertices u_i and v_i. (Note that vertex numbers are 0-indexed and edge numbers are 1-indexed.)

For a pair of integers (i,j) where 0 \leq i,j < N, define f(i,j) as follows:

  • The vertex number of the vertex with the smallest number among the vertices not included in the path from vertex i to vertex j in tree T.
    • Here, if the path from vertex i to vertex j includes all vertices from vertex 0 to vertex N-1, let f(i,j)=N.

Note that the path from vertex i to vertex j in tree T includes vertices i and j.

Find the value of \displaystyle \sum_{0\le i \le j < N} f(i,j).

Constraints

  • 2\le N\le 2\times 10^5
  • 0\le u_i < v_i < N
  • The graph given in the input 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

Output the value of \displaystyle \sum_{0\le i \le j < N} f(i,j).


Sample Input 1

2
0 1

Sample Output 1

3

We have f(0,0)=1,f(0,1)=2,f(1,1)=0. Thus, output 1+2+0=3.


Sample Input 2

5
0 1
0 2
2 3
2 4

Sample Output 2

16

Sample Input 3

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

Sample Output 3

16
G - Sum of Min

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 575

問題文

整数 N,M,K と長さ N の整数列 A=(A_0,A_1,\ldots,A_{N-1}) 、長さ M の整数列 B=(B_0,B_1,\ldots,B_{M-1}) が与えられます。添字が 0 から始まることに注意してください。

\displaystyle\sum_{i=0}^{K-1} \min(A_{i\bmod N}, B_{i \bmod M}) 998244353 で割ったあまりを求めてください。

制約

  • 1\le N,M\le 2\times 10^5
  • 1\le K\le 10^{18}
  • 1\le A_i,B_i\le 10^9
  • 入力される値は全て整数

入力

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

N M K
A_0 A_1 \ldots A_{N-1}
B_0 B_1 \ldots B_{M-1}

出力

\displaystyle\sum_{i=0}^{K-1} \min(A_{i\bmod N}, B_{i \bmod M}) 998244353 で割ったあまりを出力せよ。


入力例 1

3 2 5
3 1 4
1 5

出力例 1

7

求める値は \min(3,1)+\min(1,5)+\min(4,1)+\min(3,5)+\min(1,1)=7 です。


入力例 2

4 4 27
6 1 10 42
87 6 21 33

出力例 2

317

入力例 3

5 7 583272014892537201
763832259 547096173 408327452 685495693 212251318
850800766 845647066 240229336 648345577 691868483 740301913 740485849

出力例 3

931208848

998244353 で割ったあまりを求めてください。

Score : 575 points

Problem Statement

You are given integers N,M,K, a length-N integer sequence A=(A_0,A_1,\ldots,A_{N-1}), and a length-M integer sequence B=(B_0,B_1,\ldots,B_{M-1}). Note that the indices start from 0.

Find \displaystyle\sum_{i=0}^{K-1} \min(A_{i\bmod N}, B_{i \bmod M}) , modulo 998244353.

Constraints

  • 1\le N,M\le 2\times 10^5
  • 1\le K\le 10^{18}
  • 1\le A_i,B_i\le 10^9
  • All input values are integers.

Input

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

N M K
A_0 A_1 \ldots A_{N-1}
B_0 B_1 \ldots B_{M-1}

Output

Output \displaystyle\sum_{i=0}^{K-1} \min(A_{i\bmod N}, B_{i \bmod M}) , modulo 998244353.


Sample Input 1

3 2 5
3 1 4
1 5

Sample Output 1

7

The desired value is \min(3,1)+\min(1,5)+\min(4,1)+\min(3,5)+\min(1,1)=7.


Sample Input 2

4 4 27
6 1 10 42
87 6 21 33

Sample Output 2

317

Sample Input 3

5 7 583272014892537201
763832259 547096173 408327452 685495693 212251318
850800766 845647066 240229336 648345577 691868483 740301913 740485849

Sample Output 3

931208848

Compute modulo 998244353.