A - Password

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

高橋君は 3 桁のパスワードを設定しようとしています。

使える文字が 1 以上 N 以下の数字のみであるとき、設定できるパスワードが全部で何種類であるかを答えてください。

制約

  • 1 ≤ N ≤ 9
  • N は整数である。

入力

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

N

出力

設定できるパスワードが何種類であるかを出力せよ。


入力例 1

2

出力例 1

8

設定出来るパスワードは 111, 112, 121, 122, 211, 212, 221, 2228 種類です。


入力例 2

1

出力例 2

1

使える文字が 1 種類しか無い場合、パスワードは 1 種類しか設定出来ません。

Score : 100 points

Problem Statement

Takahashi is going to set a 3-character password.

How many possible passwords are there if each of its characters must be a digit between 1 and N (inclusive)?

Constraints

  • 1 \leq N \leq 9
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of possible passwords.


Sample Input 1

2

Sample Output 1

8

There are eight possible passwords: 111, 112, 121, 122, 211, 212, 221, and 222.


Sample Input 2

1

Sample Output 2

1

There is only one possible password if you can only use one kind of character.

B - Buffet

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

高橋くんは N 種類の料理が食べ放題のビュッフェに行き、全種類の料理 (料理 1, 料理 2, \ldots, 料理 N) を 1 度ずつ食べました。

高橋くんが i (1 \leq i \leq N) 番目に食べた料理は料理 A_i でした。

高橋くんは、料理 i (1 \leq i \leq N) を食べると満足度 B_i を得ます。

また、料理 i (1 \leq i \leq N - 1) を食べた直後に料理 i+1 を食べると満足度 C_i を追加で得ます。

高橋くんが得た満足度の合計を求めてください。

制約

  • 入力は全て整数である。
  • 2 \leq N \leq 20
  • 1 \leq A_i \leq N
  • A_1, A_2, ..., A_N は全て異なる。
  • 1 \leq B_i \leq 50
  • 1 \leq C_i \leq 50

入力

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

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
C_1 C_2 ... C_{N-1}

出力

高橋くんが得た満足度の合計を整数で出力せよ。


入力例 1

3
3 1 2
2 5 4
3 6

出力例 1

14

以下のように高橋くんは合計 14 の満足度を得ました。

  • 高橋くんはまず料理 3 を食べ、満足度 4 を得ました。
  • 高橋くんは次に料理 1 を食べ、満足度 2 を得ました。
  • 高橋くんは最後に料理 2 を食べ、満足度 5 + 3 = 8 を得ました。

入力例 2

4
2 3 4 1
13 5 8 24
45 9 15

出力例 2

74

入力例 3

2
1 2
50 50
50

出力例 3

150

Score : 200 points

Problem Statement

Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all of them (Dish 1, Dish 2, \ldots, Dish N) once.

The i-th dish (1 \leq i \leq N) he ate was Dish A_i.

When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points.

Additionally, when he eats Dish i+1 just after eating Dish i (1 \leq i \leq N - 1), he gains C_i more satisfaction points.

Find the sum of the satisfaction points he gained.

Constraints

  • All values in input are integers.
  • 2 \leq N \leq 20
  • 1 \leq A_i \leq N
  • A_1, A_2, ..., A_N are all different.
  • 1 \leq B_i \leq 50
  • 1 \leq C_i \leq 50

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
C_1 C_2 ... C_{N-1}

Output

Print the sum of the satisfaction points Takahashi gained, as an integer.


Sample Input 1

3
3 1 2
2 5 4
3 6

Sample Output 1

14

Takahashi gained 14 satisfaction points in total, as follows:

  • First, he ate Dish 3 and gained 4 satisfaction points.
  • Next, he ate Dish 1 and gained 2 satisfaction points.
  • Lastly, he ate Dish 2 and gained 5 + 3 = 8 satisfaction points.

Sample Input 2

4
2 3 4 1
13 5 8 24
45 9 15

Sample Output 2

74

Sample Input 3

2
1 2
50 50
50

Sample Output 3

150
C - Maximal Value

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

長さ N の値の分からない整数列 A があります。

長さ N-1 の整数列 B が与えられます。このとき、

B_i \geq \max(A_i, A_{i+1})

が成立することが分かっています。

A の要素の総和として考えられる値の最大値を求めてください。

制約

  • 入力は全て整数
  • 2 ≤ N ≤ 100
  • 0 \leq B_i \leq 10^5

入力

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

N
B_1 B_2 ... B_{N-1}

出力

A の要素の総和として考えられる値の最大値を出力せよ。


入力例 1

3
2 5

出力例 1

9

A として、例えば A = ( 2 , 1 , 5 )や、 A = ( -1 , -2 , -3 ), A = ( 2 , 2 , 5 ) 等が考えられます。これらのうち A の要素の総和が最大となるものは、 A = ( 2 , 2 , 5 ) です。


入力例 2

2
3

出力例 2

6

入力例 3

6
0 153 10 10 23

出力例 3

53

Score : 300 points

Problem Statement

There is an integer sequence A of length N whose values are unknown.

Given is an integer sequence B of length N-1 which is known to satisfy the following:

B_i \geq \max(A_i, A_{i+1})

Find the maximum possible sum of the elements of A.

Constraints

  • All values in input are integers.
  • 2 \leq N \leq 100
  • 0 \leq B_i \leq 10^5

Input

Input is given from Standard Input in the following format:

N
B_1 B_2 ... B_{N-1}

Output

Print the maximum possible sum of the elements of A.


Sample Input 1

3
2 5

Sample Output 1

9

A can be, for example, ( 2 , 1 , 5 ), ( -1 , -2 , -3 ), or ( 2 , 2 , 5 ). Among those candidates, A = ( 2 , 2 , 5 ) has the maximum possible sum.


Sample Input 2

2
3

Sample Output 2

6

Sample Input 3

6
0 153 10 10 23

Sample Output 3

53
D - Face Produces Unhappiness

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

東西一列に N 人の人が並んでいます。

各人の状態を表す長さ N の文字列 S が与えられます。 西から i 番目の人は、文字列 Si 文字目が L ならば西を、R ならば東を向いています。

どの人も、目の前の人が自分と同じ方向を向いていれば幸福です。 ただし、目の前に人が居ない場合、幸福ではありません。

あなたは、以下の操作を 0 回以上 K 回以下の好きな回数だけ行います。

操作: 1 \leq l \leq r \leq N を満たす整数 l, r を選ぶ。西から l, l+1, ..., r 番目の人の列を 180 度回転する。すなわち、i = 0, 1, ..., r-l について、西から l + i 番目の人は操作後には西から r - i 番目に移動し、元々西を向いていれば東を、東を向いていれば西を向く。

幸福である人は最大で何人にできるでしょうか。

制約

  • N1 \leq N \leq 10^5 を満たす整数である。
  • K1 \leq K \leq 10^5 を満たす整数である。
  • |S| = N
  • S の各文字は L または R である。

入力

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

N K
S

出力

K 回以下の操作後に幸福である人数の最大値を出力せよ。


入力例 1

6 1
LRLRRL

出力例 1

3

(l, r) = (2, 5) と選べば LLLRLL となり、西から 2, 3, 6 番目の人が幸福です。


入力例 2

13 3
LRRLRLRRLRLLR

出力例 2

9

入力例 3

10 1
LLLLLRRRRR

出力例 3

9

入力例 4

9 2
RRRLRLRLL

出力例 4

7

Score : 400 points

Problem Statement

There are N people standing in a queue from west to east.

Given is a string S of length N representing the directions of the people. The i-th person from the west is facing west if the i-th character of S is L, and east if that character of S is R.

A person is happy if the person in front of him/her is facing the same direction. If no person is standing in front of a person, however, he/she is not happy.

You can perform the following operation any number of times between 0 and K (inclusive):

Operation: Choose integers l and r such that 1 \leq l \leq r \leq N, and rotate by 180 degrees the part of the queue: the l-th, (l+1)-th, ..., r-th persons. That is, for each i = 0, 1, ..., r-l, the (l + i)-th person from the west will stand the (r - i)-th from the west after the operation, facing east if he/she is facing west now, and vice versa.

What is the maximum possible number of happy people you can have?

Constraints

  • N is an integer satisfying 1 \leq N \leq 10^5.
  • K is an integer satisfying 1 \leq K \leq 10^5.
  • |S| = N
  • Each character of S is L or R.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of happy people after at most K operations.


Sample Input 1

6 1
LRLRRL

Sample Output 1

3

If we choose (l, r) = (2, 5), we have LLLRLL, where the 2-nd, 3-rd, and 6-th persons from the west are happy.


Sample Input 2

13 3
LRRLRLRRLRLLR

Sample Output 2

9

Sample Input 3

10 1
LLLLLRRRRR

Sample Output 3

9

Sample Input 4

9 2
RRRLRLRLL

Sample Output 4

7
E - Second Sum

Time Limit: 2 sec / Memory Limit: 1024 MB

配点: 500

問題文

\{1, 2, \ldots, N\} の順列 P が与えられます。

ペア (L, R) (1 \le L \lt R \le N)について、P_L, P_{L+1}, \ldots, P_R の中で 2 番目に大きいものを X_{L, R} とします。

\displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R} を求めてください。

制約

  • 2 \le N \le 10^5
  • 1 \le P_i \le N
  • P_i \neq P_j (i \neq j)
  • 入力はすべて整数

入力

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

N
P_1 P_2 \ldots P_N

出力

\displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R} を出力せよ。


入力例 1

3
2 3 1

出力例 1

5

X_{1, 2} = 2, X_{1, 3} = 2, X_{2, 3} = 1 より、総和は 2 + 2 + 1 = 5 となります。


入力例 2

5
1 2 3 4 5

出力例 2

30

入力例 3

8
8 2 7 3 4 5 6 1

出力例 3

136

Score: 500 points

Problem Statement

Given is a permutation P of \{1, 2, \ldots, N\}.

For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest value among P_L, P_{L+1}, \ldots, P_R.

Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.

Constraints

  • 2 \le N \le 10^5
  • 1 \le P_i \le N
  • P_i \neq P_j (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 \ldots P_N

Output

Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.


Sample Input 1

3
2 3 1

Sample Output 1

5

X_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.


Sample Input 2

5
1 2 3 4 5

Sample Output 2

30

Sample Input 3

8
8 2 7 3 4 5 6 1

Sample Output 3

136
F - Many Slimes

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

1 匹のスライムがいます。

あなたはこのスライムの「体力」を自由な整数の値に設定できます。

スライムは 1 秒ごとに、自分より真に小さい整数の体力をもつスライムを 1 匹生成することで増殖していきます。生成されるスライムの体力は、スライムごとに毎回自由に決めることができます。最初の増殖はこれから 1 秒後に起こります。

最初のスライム、および生成されるスライムの体力を適切に定めることで、これから N 秒後に存在する 2^N 匹のスライムの体力の集合を集合 S に一致させることができるか判定してください。

ここで S2^N 要素からなる重複を認める整数の集合で、その要素は S_1,~S_2,~...,~S_{2^N} です。

制約

  • 入力は全て整数である。
  • 1 \leq N \leq 18
  • 1 \leq S_i \leq 10^9

入力

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

N
S_1 S_2 ... S_{2^N}

出力

最初のスライム、および生成されるスライムの体力を適切に定めることで、N 秒後に存在する 2^N 匹のスライムの体力の集合を集合 S に一致させることができるなら Yes を、できないなら No を出力せよ。


入力例 1

2
4 2 3 1

出力例 1

Yes

2 秒後に存在するスライムの体力の集合を S に一致させる一例を示します。

まず最初のスライムの体力を 4 に設定します。

最初のスライムに体力が 3 のスライムを生成させることで、1 秒後に存在するスライムの体力を 4,~3 とできます。

そして最初のスライムに体力が 2 の、2 匹目のスライムに体力が 1 のスライムを生成させることで、2 秒後に存在するスライムの体力を 4,~3,~2,~1 とできます。これは集合として S に一致しています。


入力例 2

2
1 2 3 1

出力例 2

Yes

S は同じ整数を複数含むこともあります。


入力例 3

1
1 1

出力例 3

No

入力例 4

5
4 3 5 3 1 2 7 8 7 4 6 3 7 2 3 6 2 7 3 2 6 7 3 4 6 7 3 4 2 5 2 3

出力例 4

No

Score : 600 points

Problem Statement

We have one slime.

You can set the health of this slime to any integer value of your choice.

A slime reproduces every second by spawning another slime that has strictly less health. You can freely choose the health of each new slime. The first reproduction of our slime will happen in one second.

Determine if it is possible to set the healths of our first slime and the subsequent slimes spawn so that the multiset of the healths of the 2^N slimes that will exist in N seconds equals a multiset S.

Here S is a multiset containing 2^N (possibly duplicated) integers: S_1,~S_2,~...,~S_{2^N}.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 18
  • 1 \leq S_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
S_1 S_2 ... S_{2^N}

Output

If it is possible to set the healths of the first slime and the subsequent slimes spawn so that the multiset of the healths of the 2^N slimes that will exist in N seconds equals S, print Yes; otherwise, print No.


Sample Input 1

2
4 2 3 1

Sample Output 1

Yes

We will show one way to make the multiset of the healths of the slimes that will exist in 2 seconds equal to S.

First, set the health of the first slime to 4.

By letting the first slime spawn a slime whose health is 3, the healths of the slimes that exist in 1 second can be 4,~3.

Then, by letting the first slime spawn a slime whose health is 2, and letting the second slime spawn a slime whose health is 1, the healths of the slimes that exist in 2 seconds can be 4,~3,~2,~1, which is equal to S as multisets.


Sample Input 2

2
1 2 3 1

Sample Output 2

Yes

S may contain multiple instances of the same integer.


Sample Input 3

1
1 1

Sample Output 3

No

Sample Input 4

5
4 3 5 3 1 2 7 8 7 4 6 3 7 2 3 6 2 7 3 2 6 7 3 4 6 7 3 4 2 5 2 3

Sample Output 4

No