E - Rotate and Palindrome

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

配点 : 300

問題文

長さ N の文字列 S が与えられます。S_i\ (1\leq i \leq N)S の左から i 番目の文字とします。

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

  • A 円払う。 S の左端の文字を右端に移動する。すなわち、S_1S_2\ldots S_NS_2\ldots S_NS_1 に変える。

  • B 円払う。 1 以上 N 以下の整数 i を選び、 S_i を好きな英小文字で置き換える。

S を回文にするためには最低で何円必要ですか?

回文とは ある文字列 T について、 T の長さを |T| として、全ての整数 i (1 \le i \le |T|) について、 T の前から i 文字目と後ろから i 文字目が同じであるとき、またそのときに限って、 T は回文です。

制約

  • 1\leq N \leq 5000
  • 1\leq A,B\leq 10^9
  • S は英小文字からなる長さ N の文字列
  • S 以外の入力は全て整数

入力

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

N A B
S

出力

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


入力例 1

5 1 2
rrefa

出力例 1

3

最初に 2 番目の操作を 1 回行います。2 円払い、i=5 として S_5e で置き換えます。 Srrefe となります。

次に 1 番目の操作を 1 回行います。1 円払い、Srefer となります。これは回文です。

よって 3 円払うことで S を回文にすることができました。 2 円以下払うことで S を回文にすることは不可能なので、これが答えです。


入力例 2

8 1000000000 1000000000
bcdfcgaa

出力例 2

4000000000

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

Score : 300 points

Problem Statement

You are given a string S of length N. Let S_i\ (1\leq i \leq N) be the i-th character of S from the left.

You may perform the following two kinds of operations zero or more times in any order:

  • Pay A yen (the currency in Japan). Move the leftmost character of S to the right end. In other words, change S_1S_2\ldots S_N to S_2\ldots S_NS_1.

  • Pay B yen. Choose an integer i between 1 and N, and replace S_i with any lowercase English letter.

How many yen do you need to pay to make S a palindrome?

What is a palindrome? A string T is a palindrome if and only if the i-th character from the left and the i-th character from the right are the same for all integers i (1 \le i \le |T|), where |T| is the length of T.

Constraints

  • 1\leq N \leq 5000
  • 1\leq A,B\leq 10^9
  • S is a string of length N consisting of lowercase English letters.
  • All values in the input except for S are integers.

Input

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

N A B
S

Output

Print the answer as an integer.


Sample Input 1

5 1 2
rrefa

Sample Output 1

3

First, pay 2 yen to perform the operation of the second kind once: let i=5 to replace S_5 with e. S is now rrefe.

Then, pay 1 yen to perform the operation of the first kind once. S is now refer, which is a palindrome.

Thus, you can make S a palindrome for 3 yen. Since you cannot make S a palindrome for 2 yen or less, 3 is the answer.


Sample Input 2

8 1000000000 1000000000
bcdfcgaa

Sample Output 2

4000000000

Note that the answer may not fit into a 32-bit integer type.

F - Slot Strategy 2 (Easy)

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

配点 : 300

問題文

この問題は G 問題の簡易版です。

3 個のリールからなるスロットがあります。
i 番目のリールの配列は文字列 S_i によって表されます。ここで、S_i は数字のみからなる長さ M の文字列です。

それぞれのリールには対応するボタンがついています。高橋君は各非負整数 t について、スロットが回り始めてからちょうど t 秒後にボタンを 1 つ選んで押す、または何もしないことができます。
スロットが回り始めてから t 秒後に i 番目のリールに対応するボタンを押すと、i 番目のリールは S_i(t \bmod M)+1 文字目を表示して止まります。
ただし、t \bmod MtM で割ったあまりを表します。

高橋君は全てのリールを止めた上で、表示されている文字が全て同じであるようにしたいです。
高橋君が目標を達成できるように全てのリールを止めるまでに、スロットが回り始めてから最小で何秒かかるかを求めてください。
そのようなことが不可能であればそのことを報告してください。

制約

  • 1 \leq M \leq 100
  • M は整数
  • S_i は数字のみからなる長さ M の文字列

入力

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

M
S_1
S_2
S_3

出力

全てのリールを止めた上で、表示されている文字が全て同じであるようにすることができないなら -1 を出力せよ。
できるなら、スロットが回り始めてからそのような状態にするまでに最小で何秒かかるか出力せよ。


入力例 1

10
1937458062
8124690357
2385760149

出力例 1

6

高橋君は次のようにそれぞれのリールを止めることでスロットが回り始めてから 6 秒後にリールに表示される文字を 8 で揃えることができます。

  • スロットの回転開始から 0 秒後に 2 番目のリールに対応するボタンを押します。2 番目のリールは S_2(0 \bmod 10)+1=1 文字目である 8 を表示して止まります。
  • スロットの回転開始から 2 秒後に 3 番目のリールに対応するボタンを押します。3 番目のリールは S_3(2 \bmod 10)+1=3 文字目である 8 を表示して止まります。
  • スロットの回転開始から 6 秒後に 1 番目のリールに対応するボタンを押します。1 番目のリールは S_1(6 \bmod 10)+1=7 文字目である 8 を表示して止まります。

5 秒以下で全てのリールに表示されている文字を揃える方法はないため、6 を出力します。


入力例 2

20
01234567890123456789
01234567890123456789
01234567890123456789

出力例 2

20

全てのリールを止めた上で、表示されている文字を揃える必要がある事に注意してください。


入力例 3

5
11111
22222
33333

出力例 3

-1

表示されている文字が全て同じであるようにリールを止めることはできません。
このとき -1 を出力してください。

Score : 300 points

Problem Statement

This problem is an easier version of Problem G.

There is a slot machine with three reels.
The arrangement of symbols on the i-th reel is represented by the string S_i. Here, S_i is a string of length M consisting of digits.

Each reel has a corresponding button. For each non-negative integer t, Takahashi can either choose and press one button or do nothing exactly t seconds after the reels start spinning.
If he presses the button corresponding to the i-th reel exactly t seconds after the reels start spinning, the i-th reel will stop and display the ((t \bmod M)+1)-th character of S_i.
Here, t \bmod M denotes the remainder when t is divided by M.

Takahashi wants to stop all the reels so that all the displayed characters are the same.
Find the minimum possible number of seconds from the start of the spin until all the reels are stopped so that his goal is achieved.
If this is impossible, report that fact.

Constraints

  • 1 \leq M \leq 100
  • M is an integer.
  • S_i is a string of length M consisting of digits.

Input

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

M
S_1
S_2
S_3

Output

If it is impossible to stop all the reels so that all the displayed characters are the same, print -1.
Otherwise, print the minimum possible number of seconds from the start of the spin until such a state is achieved.


Sample Input 1

10
1937458062
8124690357
2385760149

Sample Output 1

6

Takahashi can stop each reel as follows so that 6 seconds after the reels start spinning, all the reels display 8.

  • Press the button corresponding to the second reel 0 seconds after the reels start spinning. The second reel stops and displays 8, the ((0 \bmod 10)+1=1)-st character of S_2.
  • Press the button corresponding to the third reel 2 seconds after the reels start spinning. The third reel stops and displays 8, the ((2 \bmod 10)+1=3)-rd character of S_3.
  • Press the button corresponding to the first reel 6 seconds after the reels start spinning. The first reel stops and displays 8, the ((6 \bmod 10)+1=7)-th character of S_1.

There is no way to make the reels display the same character in 5 or fewer seconds, so print 6.


Sample Input 2

20
01234567890123456789
01234567890123456789
01234567890123456789

Sample Output 2

20

Note that he must stop all the reels and make them display the same character.


Sample Input 3

5
11111
22222
33333

Sample Output 3

-1

It is impossible to stop the reels so that all the displayed characters are the same.
In this case, print -1.

G - Island Tour

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

配点 : 425

問題文

AtCoder 諸島は N 個の島からなり、これらの島々は N 本の橋によって結ばれています。 島には 1 から N までの番号が付けられていて、i\ (1\leq i\leq N-1) 本目の橋は島 i と島 i+1 を、N 本目の橋は島 N と島 1 を双方向に結んでいます。 橋を渡る以外に島の間を行き来する方法は存在しません。

AtCoder 諸島では、島 X_1 から始めて島 X_2,X_3,\dots,X_M を順に訪れるツアーが定期的に催行されています。 移動の過程で訪れる島とは別の島を経由することもあり、ツアー中に橋を通る回数の合計がツアーの長さと定義されます。

厳密には、ツアーとは以下の条件を全て満たす l+1 個の島の列 a_0,a_1,\dots,a_l のことであり、その長さl として定義されます。

  • 全ての j\ (0\leq j\leq l-1) について、島 a_j と島 a_{j+1} は橋で直接結ばれている
  • ある 0 = y_1 < y_2 < \dots < y_M = l が存在して、全ての k\ (1\leq k\leq M) について a_{y_k} = X_k

財政難に苦しむ AtCoder 諸島では、維持費削減のため橋を 1 本封鎖することになりました。 封鎖する橋をうまく選んだとき、ツアーの長さの最小値がいくつになるか求めてください。

制約

  • 3\leq N \leq 2\times 10^5
  • 2\leq M \leq 2\times 10^5
  • 1\leq X_k\leq N
  • X_k\neq X_{k+1}\ (1\leq k\leq M-1)
  • 入力は全て整数

入力

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

N M
X_1 X_2 \dots X_M

出力

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


入力例 1

3 3
1 3 2

出力例 1

2
  • 1 本目の橋を封鎖した場合:通る島の列として (a_0,a_1,a_2)=(1,3,2) を取ることで島 1,3,2 を順に訪れることができ、長さ 2 のツアーが催行できます。これより短いツアーは存在しません。
  • 2 本目の橋を封鎖した場合:通る島の列として (a_0,a_1,a_2,a_3)=(1,3,1,2) を取ることで島 1,3,2 を順に訪れることができ、長さ 3 のツアーが催行できます。これより短いツアーは存在しません。
  • 3 本目の橋を封鎖した場合:通る島の列として (a_0,a_1,a_2,a_3)=(1,2,3,2) を取ることで島 1,3,2 を順に訪れることができ、長さ 3 のツアーが催行できます。これより短いツアーは存在しません。

よって、封鎖する橋をうまく選んだときのツアーの長さの最小値は 2 です。

以下の図は左から順に橋 1,2,3 を封鎖した場合を表し、数字の書かれた丸が島、丸同士を結ぶ線が橋、青い矢印が最短のツアーの経路を表します。


入力例 2

4 5
2 4 2 4 2

出力例 2

8

X_1,X_2,\dots,X_M の中に同じ島が複数回現れることもあります。


入力例 3

163054 10
62874 19143 77750 111403 29327 56303 6659 18896 64175 26369

出力例 3

390009

Score: 425 points

Problem Statement

The AtCoder Archipelago consists of N islands connected by N bridges. The islands are numbered from 1 to N, and the i-th bridge (1\leq i\leq N-1) connects islands i and i+1 bidirectionally, while the N-th bridge connects islands N and 1 bidirectionally. There is no way to travel between islands other than crossing the bridges.

On the islands, a tour that starts from island X_1 and visits islands X_2, X_3, \dots, X_M in order is regularly conducted. The tour may pass through islands other than those being visited, and the total number of times bridges are crossed during the tour is defined as the length of the tour.

More precisely, a tour is a sequence of l+1 islands a_0, a_1, \dots, a_l that satisfies all the following conditions, and its length is defined as l:

  • For all j\ (0\leq j\leq l-1), islands a_j and a_{j+1} are directly connected by a bridge.
  • There are some 0 = y_1 < y_2 < \dots < y_M = l such that for all k\ (1\leq k\leq M), a_{y_k} = X_k.

Due to financial difficulties, the islands will close one bridge to reduce maintenance costs. Determine the minimum possible length of the tour when the bridge to be closed is chosen optimally.

Constraints

  • 3\leq N \leq 2\times 10^5
  • 2\leq M \leq 2\times 10^5
  • 1\leq X_k\leq N
  • X_k\neq X_{k+1}\ (1\leq k\leq M-1)
  • All input values are integers.

Input

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

N M
X_1 X_2 \dots X_M

Output

Print the answer as an integer.


Sample Input 1

3 3
1 3 2

Sample Output 1

2
  • If the first bridge is closed: By taking the sequence of islands (a_0, a_1, a_2) = (1, 3, 2), it is possible to visit islands 1, 3, 2 in order, and a tour of length 2 can be conducted. There is no shorter tour.
  • If the second bridge is closed: By taking the sequence of islands (a_0, a_1, a_2, a_3) = (1, 3, 1, 2), it is possible to visit islands 1, 3, 2 in order, and a tour of length 3 can be conducted. There is no shorter tour.
  • If the third bridge is closed: By taking the sequence of islands (a_0, a_1, a_2, a_3) = (1, 2, 3, 2), it is possible to visit islands 1, 3, 2 in order, and a tour of length 3 can be conducted. There is no shorter tour.

Therefore, the minimum possible length of the tour when the bridge to be closed is chosen optimally is 2.

The following figure shows, from left to right, the cases when bridges 1, 2, 3 are closed, respectively. The circles with numbers represent islands, the lines connecting the circles represent bridges, and the blue arrows represent the shortest tour routes.


Sample Input 2

4 5
2 4 2 4 2

Sample Output 2

8

The same island may appear multiple times in X_1, X_2, \dots, X_M.


Sample Input 3

163054 10
62874 19143 77750 111403 29327 56303 6659 18896 64175 26369

Sample Output 3

390009
H - Max × Sum

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

配点 : 475

問題文

長さ N の数列 A = (A_1, A_2, \dots, A_N), B = (B_1, B_2, \dots, B_N) が与えられます。
\lbrace 1, 2, \dots, N \rbrace の部分集合であって大きさが K のものを 1 つ選び S とします。この時、以下の式の値としてあり得る最小値を求めてください。

\displaystyle \left(\max_{i \in S} A_i\right) \times \left(\sum_{i \in S} B_i\right)

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

制約

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq K \leq N \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq 10^6
  • 全てのテストケースに対する N の総和は 2 \times 10^5 以下
  • 入力される値は全て整数

入力

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

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

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

N K
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

出力

T 行出力せよ。i 行目には i 番目のテストケースの答えを出力せよ。


入力例 1

3
3 2
3 7 6
9 2 4
5 3
6 4 1 5 9
8 6 5 1 7
10 6
61 95 61 57 69 49 46 47 14 43
39 79 48 92 90 76 30 16 30 94

出力例 1

42
60
14579

1 番目のテストケースでは、S = \lbrace 2, 3 \rbrace を選ぶと式の値が 7 \times (2 + 4) = 42 になり、これが最小です。

Score : 475 points

Problem Statement

You are given sequences of length N: A = (A_1, A_2, \dots, A_N) and B = (B_1, B_2, \dots, B_N).
Let S be a subset of \lbrace1, 2, \dots, N\rbrace of size K. Here, find the minimum possible value of the following expression:

\displaystyle \left(\max_{i \in S} A_i\right) \times \left(\sum_{i \in S} B_i\right).

You are given T test cases; solve each of them.

Constraints

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq K \leq N \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq 10^6
  • The sum of N over all test cases is at most 2 \times 10^5.
  • All input values are integers.

Input

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

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

Each test case is given in the following format:

N K
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

Output

Print T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

3
3 2
3 7 6
9 2 4
5 3
6 4 1 5 9
8 6 5 1 7
10 6
61 95 61 57 69 49 46 47 14 43
39 79 48 92 90 76 30 16 30 94

Sample Output 1

42
60
14579

In the first test case, for S = \{2, 3\}, the value of the expression is 7 \times (2 + 4) = 42, which is the minimum.

I - K-th Largest Triplet

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

配点 : 500

問題文

長さ 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) および整数 K が与えられます。

1\leq i,j,k\leq N を満たす整数 i,j,k の選び方 N^3 通りそれぞれに対して A_iB_j+B_jC_k+C_kA_i の値を計算したとき、その中で大きい方から K 番目の値を求めてください。

制約

  • 1\leq N\leq 2\times 10^5
  • 1\leq K\leq \min(N^3,5\times 10^5)
  • 1\leq A_i,B_i,C_i\leq 10^9
  • 入力は全て整数

入力

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

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

出力

答えを出力せよ。


入力例 1

2 5
1 2
3 4
5 6

出力例 1

31

N^3=8 個の整数の値は以下の通りです。

  • (i,j,k)=(1,1,1) : A_1B_1+B_1C_1+C_1A_1=1\times 3+3\times 5+5\times 1=23
  • (i,j,k)=(1,1,2) : A_1B_1+B_1C_2+C_2A_1=1\times 3+3\times 6+6\times 1=27
  • (i,j,k)=(1,2,1) : A_1B_2+B_2C_1+C_1A_1=1\times 4+4\times 5+5\times 1=29
  • (i,j,k)=(1,2,2) : A_1B_2+B_2C_2+C_2A_1=1\times 4+4\times 6+6\times 1=34
  • (i,j,k)=(2,1,1) : A_2B_1+B_1C_1+C_1A_2=2\times 3+3\times 5+5\times 2=31
  • (i,j,k)=(2,1,2) : A_2B_1+B_1C_2+C_2A_2=2\times 3+3\times 6+6\times 2=36
  • (i,j,k)=(2,2,1) : A_2B_2+B_2C_1+C_1A_2=2\times 4+4\times 5+5\times 2=38
  • (i,j,k)=(2,2,2) : A_2B_2+B_2C_2+C_2A_2=2\times 4+4\times 6+6\times 2=44

これらを値の降順に並べ替えると (44,38,36,34,31,29,27,23) となるため、 大きい方から 5 番目の値は 31 です。


入力例 2

3 10
100 100 100
100 100 100
100 100 100

出力例 2

30000

入力例 3

5 54
800516877 573289179 26509423 168629803 696409999
656737335 915059758 201458890 931198638 185928366
140174496 254538849 830992027 305186313 322164559

出力例 3

689589940713840351

Score : 500 points

Problem Statement

You are given three integer sequences of length N, namely 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), and an integer K.

For each of the N^3 choices of integers i,j,k (1\leq i,j,k\leq N), compute the value A_iB_j + B_jC_k + C_kA_i. Among all these values, find the K-th largest value.

Constraints

  • 1\leq N \leq 2\times 10^5
  • 1\leq K \leq \min(N^3,5\times 10^5)
  • 1\leq A_i,B_i,C_i \leq 10^9
  • All input values are integers.

Input

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

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

Output

Print the answer.


Sample Input 1

2 5
1 2
3 4
5 6

Sample Output 1

31

The N^3=8 values are computed as follows:

  • For (i,j,k)=(1,1,1): A_1B_1+B_1C_1+C_1A_1=1\times 3+3\times 5+5\times 1=23
  • For (i,j,k)=(1,1,2): A_1B_1+B_1C_2+C_2A_1=1\times 3+3\times 6+6\times 1=27
  • For (i,j,k)=(1,2,1): A_1B_2+B_2C_1+C_1A_1=1\times 4+4\times 5+5\times 1=29
  • For (i,j,k)=(1,2,2): A_1B_2+B_2C_2+C_2A_1=1\times 4+4\times 6+6\times 1=34
  • For (i,j,k)=(2,1,1): A_2B_1+B_1C_1+C_1A_2=2\times 3+3\times 5+5\times 2=31
  • For (i,j,k)=(2,1,2): A_2B_1+B_1C_2+C_2A_2=2\times 3+3\times 6+6\times 2=36
  • For (i,j,k)=(2,2,1): A_2B_2+B_2C_1+C_1A_2=2\times 4+4\times 5+5\times 2=38
  • For (i,j,k)=(2,2,2): A_2B_2+B_2C_2+C_2A_2=2\times 4+4\times 6+6\times 2=44

Sorting these values in descending order, we have (44,38,36,34,31,29,27,23), so the 5th largest value is 31.


Sample Input 2

3 10
100 100 100
100 100 100
100 100 100

Sample Output 2

30000

Sample Input 3

5 54
800516877 573289179 26509423 168629803 696409999
656737335 915059758 201458890 931198638 185928366
140174496 254538849 830992027 305186313 322164559

Sample Output 3

689589940713840351