A - Contest Result

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

配点 : 100

問題文

N 問の問題からなるコンテストが開催され、i\ (1\leq i\leq N) 問目の配点は A_i 点でした。

すぬけくんはこのコンテストに参加し、B_1,B_2,\ldots,B_M 問目の M 問を解きました。 すぬけくんの総得点を求めてください。

ただし、総得点とは解いた問題の配点の総和を意味するものとします。

制約

  • 1\leq M \leq N \leq 100
  • 1\leq A_i \leq 100
  • 1\leq B_1 < B_2 < \ldots < B_M \leq N
  • 入力は全て整数

入力

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

N M
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

出力

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


入力例 1

3 2
10 20 30
1 3

出力例 1

40

すぬけくんは 1 問目と 3 問目を解きました。 配点はそれぞれ 10 点と 30 点なので、総得点は 10+30=40 点です。


入力例 2

4 1
1 1 1 100
4

出力例 2

100

入力例 3

8 4
22 75 26 45 72 81 47 29
4 6 7 8

出力例 3

202

Score : 100 points

Problem Statement

There was a contest with N problems. The i-th (1\leq i\leq N) problem was worth A_i points.

Snuke took part in this contest and solved M problems: the B_1-th, B_2-th, \ldots, and B_M-th ones. Find his total score.

Here, the total score is defined as the sum of the points for the problems he solved.

Constraints

  • 1\leq M \leq N \leq 100
  • 1\leq A_i \leq 100
  • 1\leq B_1 < B_2 < \ldots < B_M \leq N
  • All values in the input are integers.

Input

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

N M
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

Output

Print the answer as an integer.


Sample Input 1

3 2
10 20 30
1 3

Sample Output 1

40

Snuke solved the 1-st and 3-rd problems, which are worth 10 and 30 points, respectively. Thus, the total score is 10+30=40 points.


Sample Input 2

4 1
1 1 1 100
4

Sample Output 2

100

Sample Input 3

8 4
22 75 26 45 72 81 47 29
4 6 7 8

Sample Output 3

202
B - Qual B

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

配点 : 200

問題文

あるプログラミングコンテストの予選に N 人が参加し、参加者全員が異なる順位を得ました。
長さ N の文字列 S が与えられ、この文字列は決勝への参加希望の有無を表現します。具体的には下記の通りです。

  • Si 文字目が o なら、予選 i 位の参加者が決勝への参加を希望した。
  • Si 文字目が x なら、予選 i 位の参加者が決勝への参加を希望しなかった。

決勝への参加を希望した参加者のうち順位の小さい方から K 人が予選を通過します。

以下の条件を満たす長さ N の文字列 T を出力してください。

  • 予選 i 位の参加者が予選を通過する場合、 Ti 文字目は o
  • 予選 i 位の参加者が予選を通過しない場合、 Ti 文字目は x

制約

  • N,K は整数
  • 1 \le K \le N \le 100
  • Sox からなる長さ N の文字列
  • S には少なくとも K 個の o が含まれる

入力

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

N K
S

出力

答えを出力せよ。


入力例 1

10 3
oxxoxooxox

出力例 1

oxxoxoxxxx

この入力の場合、予選の参加者は N=10 人であり、予選を通過する人数は K=3 人です。

  • 予選 1 位の参加者は決勝への参加を希望しているため、予選を通過します。この時点で、通過者は 1 人です。
  • 予選 2,3 位の参加者は決勝への参加を希望していないため、予選を通過しません。
  • 予選 4 位の参加者は決勝への参加を希望しているため、予選を通過します。この時点で、通過者は 2 人です。
  • 予選 5 位の参加者は決勝への参加を希望していないため、予選を通過しません。
  • 予選 6 位の参加者は決勝への参加を希望しているため、予選を通過します。この時点で、通過者は 3 人です。
  • ここで、予選を通過した人数が 3 人となりました。なので、予選 7 位以下の参加者は予選を通過しません。

Score : 200 points

Problem Statement

There were N contestants in the qualification round of a programming contest. All contestants got distinct ranks.
You are given a length-N string S, which represents whether the contestants want to participate in the final round or not. Specifically,

  • if the i-th character of S is o, the contestant ranked i-th in the qualification wants to participate in the final;
  • if the i-th character of S is x, the contestant ranked i-th in the qualification does not want to participate in the final.

Among those who want to participate in the final, K contestants with the smallest ranks advance to the final.

Print a string T of length N that satisfies the following conditions:

  • if the contestant ranked i-th in the qualification advances to the final, the i-th character of T is o;
  • if the contestant ranked i-th in the qualification does not advance to the final, the i-th character of T is x.

Constraints

  • N and K are integers.
  • 1 \le K \le N \le 100
  • S is a string of length N consisting of o and x.
  • S has at least K o's.

Input

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

N K
S

Output

Print the answer.


Sample Input 1

10 3
oxxoxooxox

Sample Output 1

oxxoxoxxxx

In this input, N=10 people took part in the qualification round, and K=3 of them advance to the final.

  • The participant who ranked 1-st in the qualification wants to participate in the final, so the participant advances to the final. 1 participant has advanced so far.
  • The participants who ranked 2-nd and 3-rd in the qualification do not want to participate in the final, so the participants do not advance to the final.
  • The participant who ranked 4-th in the qualification wants to participate in the final, so the participant advances to the final. 2 participants have advanced so far.
  • The participants who ranked 5-th in the qualification does not want to participate in the final, so the participant does not advance to the final.
  • The participant who ranked 6-th in the qualification wants to participate in the final, so the participant advances to the final. 3 participants have advanced so far.
  • Now that 3 people have advanced to the final, no participants ranked 7-th or lower advance to the final.
C - Max MEX

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

配点 : 300

問題文

長さ N の非負整数列 A が与えられます。
A から K 要素を選んで順序を保ったまま抜き出した列を B としたとき、 MEX(B) としてありえる最大値を求めてください。

但し、数列 X に対して MEX(X) は以下の条件を満たす唯一の非負整数 m として定義されます。

  • 0 \le i < m を満たす全ての整数 iX に含まれる。
  • mX に含まれない。

制約

  • 入力は全て整数
  • 1 \le K \le N \le 3 \times 10^5
  • 0 \le A_i \le 10^9

入力

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

N K
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

7 3
2 0 2 3 2 1 9

出力例 1

3

この入力では A=(2,0,2,3,2,1,9) であり、ここから K=3 要素を選んで抜き出して数列 B を得ます。例えば、

  • 1,2,3 要素目を抜き出した時、 MEX(B)=MEX(2,0,2)=1
  • 3,4,6 要素目を抜き出した時、 MEX(B)=MEX(2,3,1)=0
  • 2,6,7 要素目を抜き出した時、 MEX(B)=MEX(0,1,9)=2
  • 2,3,6 要素目を抜き出した時、 MEX(B)=MEX(0,2,1)=3

のようになります。
達成可能な MEX の最大値は 3 です。

Score : 300 points

Problem Statement

You are given a length-N sequence of non-negative integers.
When B is a sequence obtained by choosing K elements from A and concatenating them without changing the order, find the maximum possible value of MEX(B).

Here, for a sequence X, we define MEX(X) as the unique non-negative integer m that satisfies the following conditions:

  • Every integer i such that 0 \le i < m is contained in X.
  • m is not contained in X.

Constraints

  • All values in the input are integers.
  • 1 \le K \le N \le 3 \times 10^5
  • 0 \le A_i \le 10^9

Input

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

N K
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

7 3
2 0 2 3 2 1 9

Sample Output 1

3

In this input, A=(2,0,2,3,2,1,9), and you obtain the sequence B by picking K=3 elements. For example,

  • If the 1-st, 2-nd, and 3-rd elements are chosen, MEX(B)=MEX(2,0,2)=1.
  • If the 3-rd, 4-th, and 6-th elements are chosen, MEX(B)=MEX(2,3,1)=0.
  • If the 2-nd, 6-th, and 7-th elements are chosen, MEX(B)=MEX(0,1,9)=2.
  • If the 2-nd, 3-rd, and 6-th elements are chosen, MEX(B)=MEX(0,2,1)=3.

The maximum possible MEX is 3.

D - Marking

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

配点 : 400

問題文

0 から N-1 までの番号がつけられた N 個のマスが並んでいます。 今から、すぬけくんが以下の手順に従って全てのマスに印をつけていきます。

  1. マス 0 に印をつける。
  2. 次の i - iii の手順を N−1 回繰り返す。
    1. 最後に印をつけたマスの番号を A としたとき、変数 x(A+D) \bmod N で初期化する。
    2. マス x に印が付いている限り、 x(x+1) \bmod N に更新することを繰り返す。
    3. マス x に印をつける。

すぬけくんが K 番目に印をつけるマスの番号を求めてください。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1\leq T \leq 10^5
  • 1\leq K\leq N \leq 10^9
  • 1\leq D \leq 10^9
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。ここで、\mathrm{test}_ii 番目のテストケースを意味する。

T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T

各テストケースは以下の形式で与えられる。

N D K

出力

T 行出力せよ。

i\ (1\leq i \leq T) 行目には、i 番目のテストケースに対する答えを出力せよ。


入力例 1

9
4 2 1
4 2 2
4 2 3
4 2 4
5 8 1
5 8 2
5 8 3
5 8 4
5 8 5

出力例 1

0
2
1
3
0
3
1
4
2

N=4,D=2 のとき、すぬけくんは以下のように印をつけていきます。

  1. マス 0 に印をつける。
  2. (1回目) x=(0+2)\bmod 4=2 と初期化する。マス 2 は印がついていないので、印をつける。
    (2回目) x=(2+2)\bmod 4=0 と初期化する。マス 0 は印がついているので、x=(0+1)\bmod 4=1 と更新する。マス 1 は印がついていないので、印をつける。
    (3回目) x=(1+2)\bmod 4=3 と初期化する。マス 3 は印がついていないので、印をつける。

Score : 400 points

Problem Statement

There are N squares indexed 0 through (N-1) arranged in a line. Snuke is going to mark every square by the following procedure.

  1. Mark square 0.
  2. Repeat the following steps i - iii (N-1) times.
    1. Initialize a variable x with (A+D) \bmod N, where A is the index of the square marked last time.
    2. While square x is marked, repeat replacing x with (x+1) \bmod N.
    3. Mark square x.

Find the index of the square that Snuke marks for the K-th time.

Given T test cases, find the answer for each of them.

Constraints

  • 1\leq T \leq 10^5
  • 1\leq K\leq N \leq 10^9
  • 1\leq D \leq 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{test}_i denotes the i-th test case:

T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T

Each test case is given in the following format:

N D K

Output

Print T lines.

The i-th (1\leq i \leq T) line should contain the answer to the i-th test case.


Sample Input 1

9
4 2 1
4 2 2
4 2 3
4 2 4
5 8 1
5 8 2
5 8 3
5 8 4
5 8 5

Sample Output 1

0
2
1
3
0
3
1
4
2

If N=4 and D=2, Snuke marks the squares as follows.

  1. Mark square 0.
  2. (1-st iteration) Let x=(0+2)\bmod 4=2. Since square 2 is not marked, mark it.
    (2-nd iteration) Let x=(2+2)\bmod 4=0. Since square 0 is marked, let x=(0+1)\bmod 4=1. Since square 1 is not marked, mark it.
    (3-rd iteration) Let x=(1+2)\bmod 4=3. Since square 3 is not marked, mark it.
E - Make it Palindrome

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

配点 : 500

問題文

数列 X に対し、 f(X) = ( X を回文にするために変更する必要のある要素の個数の最小値 ) とします。

与えられた長さ N の数列 A の全ての 連続 部分列 X に対する f(X) の総和を求めてください。

但し、長さ m の数列 X が回文であるとは、全ての 1 \le i \le m を満たす整数 i について、 Xi 項目と m+1-i 項目が等しいことを指します。

制約

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

入力

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

N
A_1 A_2 \dots A_N

出力

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


入力例 1

5
5 2 1 2 2

出力例 1

9
  • f(5) = 0
  • f(2) = 0
  • f(1) = 0
  • f(2) = 0
  • f(2) = 0
  • f(5,2) = 1
  • f(2,1) = 1
  • f(1,2) = 1
  • f(2,2) = 0
  • f(5,2,1) = 1
  • f(2,1,2) = 0
  • f(1,2,2) = 1
  • f(5,2,1,2) = 2
  • f(2,1,2,2) = 1
  • f(5,2,1,2,2) = 1

以上より、求める答えは 9 です。

Score : 500 points

Problem Statement

For a sequence X, let f(X) = (the minimum number of elements one must modify to make X a palindrome).

Given a sequence A of length N, find the sum of f(X) over all contiguous subarrays of A.

Here, a sequence X of length m is said to be a palindrome if and only if the i-th and the (m+1-i)-th elements of X are equal for all 1 \le i \le m.

Constraints

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

Input

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

N
A_1 A_2 \dots A_N

Output

Print the answer as an integer.


Sample Input 1

5
5 2 1 2 2

Sample Output 1

9
  • f(5) = 0
  • f(2) = 0
  • f(1) = 0
  • f(2) = 0
  • f(2) = 0
  • f(5,2) = 1
  • f(2,1) = 1
  • f(1,2) = 1
  • f(2,2) = 0
  • f(5,2,1) = 1
  • f(2,1,2) = 0
  • f(1,2,2) = 1
  • f(5,2,1,2) = 2
  • f(2,1,2,2) = 1
  • f(5,2,1,2,2) = 1

Therefore, the sought answer is 9.

F - Maximum Diameter

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

配点 : 500

問題文

長さ N の正整数列 X=(X_1,X_2\ldots,X_N) に対して、f(X) を以下のように定めます。

  • N 頂点の木であって、i\ (1 \leq i \leq N) 番目の頂点の次数が X_i であるようなものを良い木と呼ぶ。 良い木が存在するならば、f(X) は良い木の直径の最大値。良い木が存在しないならば、f(X)=0

ただし、木の 2 頂点の間の距離は一方から他方へ移動するときに用いる辺の本数の最小値であり、 木の直径は任意の 2 頂点の間の距離の最大値として定められます。

長さ N の正整数列 X としてあり得るもの全てに対する f(X) の総和を 998244353 で割った余りを求めてください。 なお、f(X) の総和は有限値になることが証明できます。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1\leq T \leq 2\times 10^5
  • 2 \leq N \leq 10^6
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。ここで、\mathrm{test}_ii 番目のテストケースを意味する。

T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T

各テストケースは以下の形式で与えられる。

N

出力

T 行出力せよ。

i\ (1\leq i \leq T) 行目には、i 番目のテストケースに対する答えを出力せよ。


入力例 1

10
2
3
5
8
13
21
34
55
89
144

出力例 1

1
6
110
8052
9758476
421903645
377386885
881422708
120024839
351256142

N=3 の場合について、

例えば、

  • X=(1,1,1) のとき、次数が 1,1,1 となる 3 頂点の木は存在しないため、f(X)=0 です。
  • X=(2,1,1) のとき、良い木は以下の図のものに限られます。この木の直径は 2 であるため、f(X)=2 です。


3 頂点の木

X=(2,1,1),(1,2,1),(1,1,2) のとき f(X)=2 であり、それ以外の X のとき f(X)=0 であるため、答えは 6 です。

Score : 500 points

Problem Statement

For a sequence X=(X_1,X_2\ldots,X_N) of length N consisting of positive integers, we define f(X) as follows:

  • A tree with N vertices is said to be good if and only if the degree of the i-th (1 \leq i \leq N) vertex is X_i. If a good tree exists, f(X) is the maximum diameter of a good tree; if it doesn't, f(X)=0.

Here, the distance between two vertices is the minimum number of edges that must be traversed to travel from one vertex to the other, and the diameter of a tree is the maximum distance between two vertices.

Find the sum, modulo 998244353, of f(X) over all possible sequences X of length N consisting of positive integers. We can prove that the sum of f(X) is a finite value.

Given T test cases, find the answer for each of them.

Constraints

  • 1\leq T \leq 2\times 10^5
  • 2 \leq N \leq 10^6
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{test}_i denotes the i-th test case:

T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T

Each test case is given in the following format:

N

Output

Print T lines.

The i-th (1\leq i \leq T) line should contain the answer to the i-th test case.


Sample Input 1

10
2
3
5
8
13
21
34
55
89
144

Sample Output 1

1
6
110
8052
9758476
421903645
377386885
881422708
120024839
351256142

If N=3,

for example,

  • when X=(1,1,1), there is no tree with three vertices whose degrees are 1,1, and 1, so f(X)=0.
  • When X=(2,1,1), the only possible tree is illustrated below. The diameter of this tree is 2, so f(X)=2.


3-vertex tree

For X=(2,1,1),(1,2,1),(1,1,2), we have f(X)=2; for other X, we have f(X)=0. Thus, the answer is 6.

G - Edge Elimination

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

配点 : 600

問題文

T 個のテストケースについて、以下の問題を解いてください。

深さ D の完全 K 分木 ( 1+K+K^2+\dots+K^D 頂点 ) があります。
あなたの目標はこの木の辺を何本か切って、連結成分のうちいずれかを X 頂点にすることです。
目標を達成するために切るべき辺の数の最小値を求めてください。

制約

  • 入力は全て整数
  • 1 \le T \le 100
  • 1 \le D
  • 2 \le K
  • \displaystyle 1 \le X \le \sum_{i=0}^{D} K^i \le 10^{18}

入力

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

T
case_1
\vdots
case_T

但し、 case_ii 個目のテストケースである。
各テストケースは以下の形式である。

D K X

出力

全体で T 行出力せよ。
そのうち i 行目には i 個目のテストケースに対する答えを整数として出力せよ。


入力例 1

11
2 2 1
2 2 2
2 2 3
2 2 4
2 2 5
2 2 6
2 2 7
1 999999999999999999 1
1 999999999999999999 2
1 999999999999999999 999999999999999999
1 999999999999999999 1000000000000000000

出力例 1

1
2
1
1
2
1
0
1
999999999999999998
1
0

Score : 600 points

Problem Statement

Solve the following problem for T test cases.

We have a perfect K-ary tree of depth D (with 1+K+K^2+\dots+K^D vertices).
Your objective is to cut some of the edges to obtain a connected component with exactly X vertices.
At least how many edges must be cut to achieve the objective?

Constraints

  • All values in the input are integers.
  • 1 \le T \le 100
  • 1 \le D
  • 2 \le K
  • \displaystyle 1 \le X \le \sum_{i=0}^{D} K^i \le 10^{18}

Input

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

T
case_1
\vdots
case_T

Here, case_i denotes the i-th test case.
Each test case is given in the following format:

D K X

Output

Print T lines.
The i-th line should contain the answer to the i-th test case as an integer.


Sample Input 1

11
2 2 1
2 2 2
2 2 3
2 2 4
2 2 5
2 2 6
2 2 7
1 999999999999999999 1
1 999999999999999999 2
1 999999999999999999 999999999999999999
1 999999999999999999 1000000000000000000

Sample Output 1

1
2
1
1
2
1
0
1
999999999999999998
1
0
Ex - Bow Meow Optimization

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

配点 : 600

問題文

1 から N までの番号がついた N 匹の犬と、1 から M までの番号がついた M 匹の猫がいます。 今から、これらの N+M 匹を左右一列に好きな順序で並べます。 並べ方に応じて、それぞれの犬と猫には以下のように不満度が生じます。

  • i の不満度は、その犬より左にいる猫の匹数を x、右にいる猫の匹数を y とすると、A_i\times|x-y| である。
  • i の不満度は、その猫より左にいる犬の匹数を x、右にいる犬の匹数を y とすると、B_i\times|x-y| である。

不満度の総和の最小値を求めてください。

制約

  • 1\leq N,M \leq 300
  • 1\leq A_i,B_i \leq 10^9
  • 入力は全て整数

入力

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

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

出力

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


入力例 1

2 2
1 3
2 4

出力例 1

6

左から順に犬 1、猫 2、犬 2、猫 1 と並べたとき、

  • 1 の不満度は 1\times|0-2|=2
  • 2 の不満度は 3\times|1-1|=0
  • 1 の不満度は 2\times|2-0|=4
  • 2 の不満度は 4\times|1-1|=0

となるため、不満度の総和は 6 です。並べ方を変えても不満度の総和が 6 未満となることはないため、6 が答えです。


入力例 2

1 2
100
100 290

出力例 2

390

入力例 3

5 7
522 575 426 445 772
81 447 629 497 202 775 325

出力例 3

13354

Score : 600 points

Problem Statement

There are N dogs numbered 1 through N and M cats numbered 1 through M. You will arrange the (N+M) animals in a line from left to right. An arrangement causes each animal's frustration as follows:

  • The frustration of dog i is A_i\times|x-y|, where x and y are the numbers of cats to the left and right of that dog, respectively.
  • The frustration of cat i is B_i\times|x-y|, where x and y are the numbers of dogs to the left and right of that cat, respectively.

Find the minimum possible sum of the frustrations.

Constraints

  • 1\leq N,M \leq 300
  • 1\leq A_i,B_i \leq 10^9
  • All values in the input are integers.

Input

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

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print the answer as an integer.


Sample Input 1

2 2
1 3
2 4

Sample Output 1

6

Consider the following arrangement: dog 1, cat 2, dog 2, cat 1, from left to right. Then,

  • dog 1's frustration is 1\times|0-2|=2;
  • dog 2's frustration is 3\times|1-1|=0;
  • cat 1's frustration is 2\times|2-0|=4; and
  • cat 2's frustration is 4\times|1-1|=0,

so the sum of the frustrations is 6. Rearranging the animals does not make the sum less than 6, so the answer is 6.


Sample Input 2

1 2
100
100 290

Sample Output 2

390

Sample Input 3

5 7
522 575 426 445 772
81 447 629 497 202 775 325

Sample Output 3

13354