A - Middle Letter

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

英小文字からなる長さが奇数の文字列 S が与えられます。

S の中央の文字を出力してください。

中央の文字とは ある長さが奇数の文字列 T について、 T の長さを |T| として、T の前から \frac{|T|+1}{2} 番目の文字を中央の文字とします。

制約

  • S は英小文字からなる長さが奇数の文字列
  • S の長さは 1 以上 99 以下

入力

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

S

出力

答えを出力せよ。


入力例 1

atcoder

出力例 1

o

atcoder の中央の文字は o です。


入力例 2

a

出力例 2

a

Score : 100 points

Problem Statement

You are given an odd-length string S consisting of lowercase English letters.

Print the central character of S.

What is the central character? For an odd-length string T, its central character is the \frac{|T|+1}{2}-th character from the beginning, where |T| is the length of T.

Constraints

  • S is an odd-length string consisting of lowercase English letters.
  • The length of S is between 1 and 99 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.


Sample Input 1

atcoder

Sample Output 1

o

The central character of atcoder is o.


Sample Input 2

a

Sample Output 2

a
B - Jogging

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

高橋君と青木君はジョギングをすることにしました。
高橋君は「A 秒間秒速 B メートルで歩き、C 秒間休む」ことを繰り返します。
青木君は「D 秒間秒速 E メートルで歩き、F 秒間休む」ことを繰り返します。
二人が同時にジョギングを始めてから X 秒後、高橋君と青木君のうちどちらが長い距離を進んでいますか?

制約

  • 1 \leq A, B, C, D, E, F, X \leq 100
  • 入力は全て整数

入力

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

A B C D E F X

出力

二人が同時にジョギングを始めてから X 秒後時点で、高橋君の方が青木君よりも長い距離を進んでいるならば Takahashi、青木君の方が高橋君よりも長い距離を進んでいるならば Aoki、二人が同じ距離を進んでいるならば Draw と出力せよ。


入力例 1

4 3 3 6 2 5 10

出力例 1

Takahashi

二人はジョギングを始めてから 10 秒間の間、以下のように行動します。

  • 高橋君は 4 秒間歩き、3 秒間休んだ後、再び 3 秒間歩く。合計 (4 + 3) \times 3 = 21 メートル歩く。
  • 青木君は 6 秒間歩き、4 秒間休む。合計 6 \times 2 = 12 メートル歩く。

高橋君の方が長い距離を進んでいるので、Takahashi と出力します。


入力例 2

3 1 4 1 5 9 2

出力例 2

Aoki

入力例 3

1 1 1 1 1 1 1

出力例 3

Draw

Score : 100 points

Problem Statement

Takahashi and Aoki decided to jog.
Takahashi repeats the following: "walk at B meters a second for A seconds and take a rest for C seconds."
Aoki repeats the following: "walk at E meters a second for D seconds and take a rest for F seconds."
When X seconds have passed since they simultaneously started to jog, which of Takahashi and Aoki goes ahead?

Constraints

  • 1 \leq A, B, C, D, E, F, X \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D E F X

Output

When X seconds have passed since they simultaneously started to jog, if Takahashi goes ahead of Aoki, print Takahashi; if Aoki goes ahead of Takahashi, print Aoki; if they have advanced the same distance, print Draw.


Sample Input 1

4 3 3 6 2 5 10

Sample Output 1

Takahashi

During the first 10 seconds after they started to jog, they move as follows.

  • Takahashi walks for 4 seconds, takes a rest for 3 seconds, and walks again for 3 seconds. As a result, he advances a total of (4 + 3) \times 3 = 21 meters.
  • Aoki walks for 6 seconds and takes a rest for 4 seconds. As a result, he advances a total of 6 \times 2 = 12 meters.

Since Takahashi goes ahead, Takahashi should be printed.


Sample Input 2

3 1 4 1 5 9 2

Sample Output 2

Aoki

Sample Input 3

1 1 1 1 1 1 1

Sample Output 3

Draw
C - Not All

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) および正整数 M が与えられます。

A の末尾の要素を削除するという操作を 0 回以上 N 回以下行うことで、以下の条件が満たされないようにしたいです。

  • 条件A には 1 以上 M 以下の整数がすべて含まれている。

必要な操作回数の最小値を求めてください。

なお、本問題の制約下において、操作を 0 回以上 N 回以下行うことで上述の条件が満たされないようにすることが必ず可能であることが証明できます。

制約

  • 1\leq M \leq N \leq 100
  • 1\leq A_i \leq M
  • 入力は全て整数

入力

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

N M
A_1 A_2 \dots A_N

出力

条件が満たされなくなるために必要な操作回数の最小値を出力せよ。


入力例 1

5 3
3 2 3 1 2

出力例 1

2

最初、A=(3,2,3,1,2) です。このとき、A には 1 以上 3 以下の整数がすべて含まれるため条件を満たします。

A の末尾の要素を削除する操作を 1 回行うと、A=(3,2,3,1) となります。このとき、A には 1 以上 3 以下の整数がすべて含まれるため条件を満たします。

A の末尾の要素を削除する操作をもう 1 回行うと、A=(3,2,3) となります。このとき、A には 1 が含まれないため条件を満たしません。

よって、条件が満たされなくなるために必要な操作回数の最小値は 2 です。


入力例 2

4 3
1 3 1 3

出力例 2

0

A には最初から 2 が含まれず条件を満たさないため、操作を 1 回も行う必要がありません。


入力例 3

10 4
1 3 3 4 2 1 3 1 2 4

出力例 3

6

Score : 200 points

Problem Statement

You are given an integer sequence A = (A_1, A_2, \dots, A_N) of length N and a positive integer M.

Your goal is to make the following condition false by performing this operation between 0 and N times (inclusive): remove the last element of A.

  • Condition: A contains every integer from 1 through M.

Find the minimum number of operations required.

Under the constraints of this problem, it can be proved that it is always possible to make the condition false by performing the operation between 0 and N times.

Constraints

  • 1 \le M \le N \le 100
  • 1 \le A_i \le M
  • 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

Output

Output the minimum number of operations required to make the condition false.


Sample Input 1

5 3
3 2 3 1 2

Sample Output 1

2

Initially, A = (3,2,3,1,2). Since A contains every integer from 1 through 3, the condition holds.

If you perform the operation once, A = (3,2,3,1). The condition still holds.

If you perform the operation once more, A = (3,2,3). The integer 1 is missing, so the condition no longer holds.

Therefore, the minimum required number of operations is 2.


Sample Input 2

4 3
1 3 1 3

Sample Output 2

0

Since A initially lacks the integer 2, the condition is already false, so no operation is needed.


Sample Input 3

10 4
1 3 3 4 2 1 3 1 2 4

Sample Output 3

6
D - typo

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

文字列 S, T が与えられます。以下の操作を高々 1行うことで、ST と一致させることができるかを判定してください。

  • S の隣り合う 2 文字を選び、入れ替える。

なお、上記の操作を一度も行わないことも可能です。

制約

  • S, T はそれぞれ英小文字のみからなる、長さ 2 以上 100 以下の文字列
  • S の長さと T の長さは等しい

入力

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

S
T

出力

問題文中の操作を高々 1 回行うことで ST と一致させることができるなら Yes を、できないなら No を出力せよ。


入力例 1

abc
acb

出力例 1

Yes

S2 文字目と 3 文字目を入れ替えることで、ST と一致させることができます。


入力例 2

aabb
bbaa

出力例 2

No

どのように操作を行っても、ST と一致させることはできません。


入力例 3

abcde
abcde

出力例 3

Yes

ST は既に一致しています。

Score : 200 points

Problem Statement

You are given two strings S and T. Determine whether it is possible to make S and T equal by doing the following operation at most once:

  • choose two adjacent characters in S and swap them.

Note that it is allowed to choose not to do the operation.

Constraints

  • Each of S and T is a string of length between 2 and 100 (inclusive) consisting of lowercase English letters.
  • S and T have the same length.

Input

Input is given from Standard Input in the following format:

S
T

Output

If it is possible to make S and T equal by doing the operation in Problem Statement at most once, print Yes; otherwise, print No.


Sample Input 1

abc
acb

Sample Output 1

Yes

You can swap the 2-nd and 3-rd characters of S to make S and T equal.


Sample Input 2

aabb
bbaa

Sample Output 2

No

There is no way to do the operation to make S and T equal.


Sample Input 3

abcde
abcde

Sample Output 3

Yes

S and T are already equal.

E - Centers

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

1,2,\dots,N がちょうど 3 回ずつ現れる長さ 3N の数列 A=(A_1,A_2,\dots,A_{3N}) が与えられます。

i=1,2,\dots,N について、A の中にある i のうち真ん中にあるものの添字を f(i) と定めます。 1,2,\dots,Nf(i) の昇順に並べ替えてください。

f(i) の定義は厳密には以下の通りです。

  • A_j = i を満たす jj=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma) であるとする。このとき、f(i) = \beta である。

制約

  • 1\leq N \leq 10^5
  • 1 \leq A_j \leq N
  • i=1,2,\dots,N それぞれについて、A の中に i はちょうど 3 回現れる
  • 入力は全て整数

入力

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

N
A_1 A_2 \dots A_{3N}

出力

1,2,\dots,Nf(i) の昇順に並べ替えてできる長さ N の数列を空白区切りで出力せよ。


入力例 1

3
1 1 3 2 3 2 2 3 1

出力例 1

1 3 2
  • A の中にある 1A_1,A_2,A_9 なので、f(1) = 2 です。
  • A の中にある 2A_4,A_6,A_7 なので、f(2) = 6 です。
  • A の中にある 3A_3,A_5,A_8 なので、f(3) = 5 です。

よって、f(1) < f(3) < f(2) であるため 1,3,2 の順に出力します。


入力例 2

1
1 1 1

出力例 2

1

入力例 3

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

出力例 3

3 4 1 2

Score : 250 points

Problem Statement

You are given a sequence A=(A_1,A_2,\dots,A_{3N}) of length 3N where each of 1,2,\dots, and N occurs exactly three times.

For i=1,2,\dots,N, let f(i) be the index of the middle occurrence of i in A. Sort 1,2,\dots,N in ascending order of f(i).

Formally, f(i) is defined as follows.

  • Suppose that those j such that A_j = i are j=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma). Then, f(i) = \beta.

Constraints

  • 1\leq N \leq 10^5
  • 1 \leq A_j \leq N
  • i occurs in A exactly three times, for each i=1,2,\dots,N.
  • All input values are integers.

Input

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

N
A_1 A_2 \dots A_{3N}

Output

Print the sequence of length N obtained by sorting 1,2,\dots,N in ascending order of f(i), separated by spaces.


Sample Input 1

3
1 1 3 2 3 2 2 3 1

Sample Output 1

1 3 2
  • 1 occurs in A at A_1,A_2,A_9, so f(1) = 2.
  • 2 occurs in A at A_4,A_6,A_7, so f(2) = 6.
  • 3 occurs in A at A_3,A_5,A_8, so f(3) = 5.

Thus, f(1) < f(3) < f(2), so 1,3, and 2 should be printed in this order.


Sample Input 2

1
1 1 1

Sample Output 2

1

Sample Input 3

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

Sample Output 3

3 4 1 2
F - Sum of Numbers Greater Than Me

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

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

i=1,\ldots,N のそれぞれについて次の問題を解いてください。

問題:A の要素のうち A_i より大きな要素全ての和を求めよ。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^6
  • 入力は全て整数である

入力

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

N
A_1 \ldots A_N

出力

1\leq k\leq N について、i=k に対する問題の答えを B_k とする。B_1,\ldots,B_N をこの順に空白区切りで出力せよ。


入力例 1

5
1 4 1 4 2

出力例 1

10 0 10 0 8
  • i=1 のとき A_1=1 より大きな要素の和は 4+4+2=10
  • i=2 のとき A_2=4 より大きな要素の和は 0
  • i=3 のとき A_3=1 より大きな要素の和は 4+4+2=10
  • i=4 のとき A_4=4 より大きな要素の和は 0
  • i=5 のとき A_5=2 より大きな要素の和は 4+4=8

入力例 2

10
31 42 59 26 53 58 97 93 23 54

出力例 2

456 414 190 487 361 249 0 97 513 307

入力例 3

50
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

出力例 3

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Score : 300 points

Problem Statement

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

For each i=1,\ldots,N, solve the following problem.

Problem: Find the sum of all elements in A that are greater than A_i.

Constraints

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

Input

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

N
A_1 \ldots A_N

Output

For each 1\leq k\leq N, let B_k be the answer to the problem when i=k. Print B_1,\ldots,B_N in this order, separated by spaces.


Sample Input 1

5
1 4 1 4 2

Sample Output 1

10 0 10 0 8
  • For i=1, the sum of elements greater than A_1=1 is 4+4+2=10.
  • For i=2, the sum of elements greater than A_2=4 is 0.
  • For i=3, the sum of elements greater than A_3=1 is 4+4+2=10.
  • For i=4, the sum of elements greater than A_4=4 is 0.
  • For i=5, the sum of elements greater than A_5=2 is 4+4=8.

Sample Input 2

10
31 42 59 26 53 58 97 93 23 54

Sample Output 2

456 414 190 487 361 249 0 97 513 307

Sample Input 3

50
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Sample Output 3

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
G - Flipping and Bonus

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君が N 回コイントスを行います。 また、高橋君はカウンタを持っており、最初カウンタの数値は 0 です。

i 回目のコイントスで表裏のどちらが出たかによって、次のことが起こります。

  • 表が出たとき:高橋君はカウンタの数値を 1 増やし、X_i 円もらう。
  • 裏が出たとき:高橋君はカウンタの数値を 0 に戻す。お金をもらうことは出来ない。

また、M 種類の連続ボーナスがあり、i 種類目の連続ボーナスではカウンタの数値が C_i になるたびに Y_i 円もらうことができます。

高橋君は最大で何円もらうことができるかを求めてください。

制約

  • 1\leq M\leq N\leq 5000
  • 1\leq X_i\leq 10^9
  • 1\leq C_i\leq N
  • 1\leq Y_i\leq 10^9
  • C_1,C_2,\ldots,C_M はすべて異なる。
  • 入力はすべて整数

入力

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

N M
X_1 X_2 \ldots X_N
C_1 Y_1
C_2 Y_2
\vdots
C_M Y_M

出力

高橋君がもらうことのできる金額の最大値を整数で出力せよ。


入力例 1

6 3
2 7 1 8 2 8
2 10
3 1
5 5

出力例 1

48

順に 表, 表, 裏, 表, 表, 表 が出た時、もらえる金額は次のようになります。

  • 1 回目のコイントスで表が出る。カウンタの数値を 0 から 1 にして、2 円もらう。
  • 2 回目のコイントスで表が出る。カウンタの数値を 1 から 2 にして、7 円もらう。さらに、連続ボーナスとして 10 円もらう。
  • 3 回目のコイントスで裏が出る。カウンタの数値を 2 から 0 にする。
  • 4 回目のコイントスで表が出る。カウンタの数値を 0 から 1 にして、8 円もらう。
  • 5 回目のコイントスで表が出る。カウンタの数値を 1 から 2 にして、2 円もらう。さらに、連続ボーナスとして 10 円もらう。
  • 6 回目のコイントスで表が出る。カウンタの数値を 2 から 3 にして、8 円もらう。さらに、連続ボーナスとして 1 円もらう。

このとき高橋君は合計で 2+(7+10)+0+8+(2+10)+(8+1)=48 円もらうことができ、このときが最大です。
連続ボーナスはカウンタの数値が C_i になるたびに何度でももらえることに注意してください。
ちなみに、6 回のコイントスで全部表が出た時は 2+(7+10)+(1+1)+8+(2+5)+8=44 円しかもらうことが出来ず、この時は最大ではありません。


入力例 2

3 2
1000000000 1000000000 1000000000
1 1000000000
3 1000000000

出力例 2

5000000000

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

Score : 400 points

Problem Statement

Takahashi will toss a coin N times. He also has a counter, which initially shows 0.

Depending on the result of the i-th coin toss, the following happens:

  • If it heads: Takahashi increases the counter's value by 1 and receives X_i yen (Japanese currency).
  • If it tails: he resets the counter's value to 0, without receiving money.

Additionally, there are M kinds of streak bonuses. The i-th kind of streak bonus awards Y_i yen each time the counter shows C_i.

Find the maximum amount of money that Takahashi can receive.

Constraints

  • 1\leq M\leq N\leq 5000
  • 1\leq X_i\leq 10^9
  • 1\leq C_i\leq N
  • 1\leq Y_i\leq 10^9
  • C_1,C_2,\ldots,C_M are all different.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
X_1 X_2 \ldots X_N
C_1 Y_1
C_2 Y_2
\vdots
C_M Y_M

Output

Print the maximum amount of money that Takahashi can receive, as an integer.


Sample Input 1

6 3
2 7 1 8 2 8
2 10
3 1
5 5

Sample Output 1

48

If he gets head, head, tail, head, head, head, in this order, the following amounts of money are awarded.

  • In the 1-st coin toss, the coin heads. Change the counter's value from 0 to 1 and receive 2 yen.
  • In the 2-nd coin toss, the coin heads. Change the counter's value from 1 to 2 and receive 7 yen. Additionally, get 10 yen as a streak bonus.
  • In the 3-rd coin toss, the coin tails. Change the counter's value from 2 to 0.
  • In the 4-th coin toss, the coin heads. Change the counter's value from 0 to 1 and receive 8 yen.
  • In the 5-th coin toss, the coin heads. Change the counter's value from 1 to 2 and receive 2 yen. Additionally, get 10 yen as a streak bonus.
  • In the 6-th coin toss, the coin heads. Change the counter's value from 2 to 3 and receive 8 yen. Additionally, get 1 yen as a streak bonus.

In this case, Takahashi receives 2+(7+10)+0+8+(2+10)+(8+1)=48 yen in total, which is the maximum possible.
Note that streak bonuses are awarded any number of times each time the counter shows C_i.
As a side note, if he gets head in all 6 coin tosses, he only receives 2+(7+10)+(1+1)+8+(2+5)+8=44 yen, which is not the maximum.


Sample Input 2

3 2
1000000000 1000000000 1000000000
1 1000000000
3 1000000000

Sample Output 2

5000000000

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

H - Cut in Half

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

長さが A_1,\ldots,A_NN 本の棒が袋に入っています。

あなたは次の操作を K 回繰り返します。

  • 袋に入っている棒の中で最も長いものを 1 本取り出し、二等分して、2 本の棒を袋に戻す

K 回の操作が終わった後、袋の中にある N+K 本の棒のうち、長い方から X 番目のものの長さを求めてください。

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

制約

  • 1 \leq T \leq 10^5
  • それぞれのテストケースについて、
    • 1 \leq N \leq 10^5
    • 1 \leq A_i \leq 10^9
    • 1 \leq K \leq 10^9
    • 1 \leq X \leq N+K
  • 全てのテストケースについての N の総和は 10^5 を超えない
  • 入力は全て整数

入力

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

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

\mathrm{testcase}_ii 番目のテストケースを表し、次の形式で与えられる。

N K X
A_1 \ldots A_N

出力

T 行出力せよ。i 行目 (1 \leq i \leq T) には i 番目のテストケースに対する答えを出力せよ。
真の解との絶対誤差または相対誤差が 10^{-9} 以下のとき正解と判定される。


入力例 1

2
3 4 5
40 20 30
10 100 50
1 2 3 4 5 6 7 8 9 10

出力例 1

10.00000000000000000000
0.50000000000000000000

1 番目のテストケースでは、最初、袋の中には長さが 20,30,403 本の棒が入っています。操作は次のように進行します。

  • 長さ 40 の棒を袋から取り出し、長さ 20 の棒 2 本を袋に戻す。袋の中の棒の長さは 20,20,20,30 となる。
  • 長さ 30 の棒を袋から取り出し、長さ 15 の棒 2 本を袋に戻す。袋の中の棒の長さは 15,15,20,20,20 となる。
  • 長さ 20 の棒を袋から取り出し、長さ 10 の棒 2 本を袋に戻す。袋の中の棒の長さは 10,10,15,15,20,20 となる。
  • 長さ 20 の棒を袋から取り出し、長さ 10 の棒 2 本を袋に戻す。袋の中の棒の長さは 10,10,10,10,15,15,20 となる。

操作後に長い方から 5 番目の棒の長さは 10 です。

Score : 475 points

Problem Statement

There are N sticks in a bag, with lengths A_1,\ldots,A_N.

You repeat the following operation K times.

  • Take out any one of the longest sticks in the bag, split it into two equal halves, and put the two sticks back into the bag.

After K operations, among the N+K sticks in the bag, find the length of the X-th longest one.

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

Constraints

  • 1 \leq T \leq 10^5
  • For each test case:
    • 1 \leq N \leq 10^5
    • 1 \leq A_i \leq 10^9
    • 1 \leq K \leq 10^9
    • 1 \leq X \leq N+K
  • The sum of N over all test cases does not exceed 10^5.
  • All input values are integers.

Input

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

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

\mathrm{testcase}_i represents the i-th test case and is given in the following format:

N K X
A_1 \ldots A_N

Output

Print T lines. In the i-th line (1 \leq i \leq T), output the answer for the i-th test case.
Your answer will be judged correct if the absolute or relative error is at most 10^{-9}.


Sample Input 1

2
3 4 5
40 20 30
10 100 50
1 2 3 4 5 6 7 8 9 10

Sample Output 1

10.00000000000000000000
0.50000000000000000000

In the 1-st test case, initially the bag contains 3 sticks of lengths 20,30,40. The operations proceed as follows.

  • Take out the stick of length 40 from the bag, split it into two sticks of length 20, and put them back. The stick lengths in the bag become 20,20,20,30.
  • Take out the stick of length 30 from the bag, split it into two sticks of length 15, and put them back. The stick lengths in the bag become 15,15,20,20,20.
  • Take out a stick of length 20 from the bag, split it into two sticks of length 10, and put them back. The stick lengths in the bag become 10,10,15,15,20,20.
  • Take out a stick of length 20 from the bag, split it into two sticks of length 10, and put them back. The stick lengths in the bag become 10,10,10,10,15,15,20.

After the operations, the 5-th longest stick has length 10.

I - Make 10 Again

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

N 個のサイコロがあります。 i = 1, 2, \ldots, N について、i 番目のサイコロを振ると 1 以上 A_i 以下の整数の出目がそれぞれ等確率でランダムにでます。

N 個のサイコロすべてを同時に振るとき、その結果が下記の条件を満たす確率を \text{mod } 998244353 で求めてください。

N 個のサイコロの中からいくつか( N 個全部でも良い)を選ぶ方法であって、選んだサイコロの出目の和がちょうど 10 であるようなものが存在する。

確率 \text{mod } 998244353 の定義

この問題で求める確率は必ず有理数になることが証明できます。 また、この問題の制約下では、求める確率を既約分数 \frac{y}{x} で表したときに x998244353 で割り切れないことが保証されます。

このとき xz \equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まります。この z を答えてください。

制約

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

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

4
1 7 2 9

出力例 1

942786334

例えば、1, 2, 3, 4 個目のサイコロの出目が順に 1, 3, 2, 7 だった場合、この結果は問題文中の条件を満たします。 実際、2, 4 番目のサイコロを選択すると、選んだサイコロの出目の和が 3 + 7 = 10 になります。 また、1, 3, 4 番目のサイコロを選択しても、選んだサイコロの出目の和が 1 + 2 + 7 = 10 になります。

一方、1, 2, 3, 4 個目のサイコロの出目が順に 1, 6, 1, 5 だった場合、 どのようにサイコロを選んでも選んだサイコロの出目の和が 10 にならないため、この結果は問題文中の条件を満たしません。

この入力例では、N 個のサイコロを振った結果が問題文中の条件を満たす確率は \frac{11}{18} です。 よって、その \text{mod } 998244353 における値である 942786334 を出力します。


入力例 2

7
1 10 100 1000 10000 100000 1000000

出力例 2

996117877

Score : 500 points

Problem Statement

We have N dice. For each i = 1, 2, \ldots, N, when the i-th die is thrown, it shows a random integer between 1 and A_i, inclusive, with equal probability.

Find the probability, modulo 998244353, that the following condition is satisfied when the N dice are thrown simultaneously.

There is a way to choose some (possibly all) of the N dice so that the sum of their results is 10.

How to find a probability modulo 998244353

It can be proved that the sought probability is always a rational number. Additionally, the constraints of this problem guarantee that if the sought probability is represented as an irreducible fraction \frac{y}{x}, then x is not divisible by 998244353.

Here, there is a unique integer z such that xz \equiv y \pmod{998244353}. Report this z.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq A_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

Output

Print the answer.


Sample Input 1

4
1 7 2 9

Sample Output 1

942786334

For instance, if the first, second, third, and fourth dice show 1, 3, 2, and 7, respectively, these results satisfy the condition. In fact, if the second and fourth dice are chosen, the sum of their results is 3 + 7 = 10. Alternatively, if the first, third, and fourth dice are chosen, the sum of their results is 1 + 2 + 7 = 10.

On the other hand, if the first, second, third, and fourth dice show 1, 6, 1, and 5, respectively, there is no way to choose some of them so that the sum of their results is 10, so the condition is not satisfied.

In this sample input, the probability of the results of the N dice satisfying the condition is \frac{11}{18}. Thus, print this value modulo 998244353, that is, 942786334.


Sample Input 2

7
1 10 100 1000 10000 100000 1000000

Sample Output 2

996117877