A - Raise Both Hands

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

配点 : 100 点

問題文

高橋君はたこ焼きを作ってすぬけ君に振る舞うことにしました。高橋君はすぬけ君に、たこ焼きを食べたいならば左手のみを挙げて、そうでないならば右手のみを挙げるよう指示しました。

すぬけ君が挙げた手の情報は整数 L, R によって与えられます。 すぬけ君は L = 1 のとき、またそのときに限り左手を挙げており、R = 1 のとき、またそのときに限り右手を挙げています。すぬけ君は指示に従わず、両手を挙げることも手を挙げないこともあります。

すぬけ君が片方の手のみを挙げている場合、すぬけ君がたこ焼きを食べたいならば Yes を、そうでないならば No を出力してください。すぬけ君が両手を挙げているか、手を挙げていないときは Invalid と出力してください。

ただし、すぬけ君が片方の手のみを挙げている場合は必ず指示に従った行動を取っているものとします。

制約

  • L, R は 0 または 1

入力

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

L R

出力

問題文の指示に従って Yes, No, Invalid のいずれかを出力せよ。


入力例 1

1 0

出力例 1

Yes

すぬけ君はたこ焼きを食べたいので左手のみを挙げています。


入力例 2

1 1

出力例 2

Invalid

すぬけ君は両手を挙げています。

Score : 100 points

Problem Statement

Takahashi decided to make takoyaki (octopus balls) and serve it to Snuke. Takahashi instructed Snuke to raise only his left hand if he wants to eat takoyaki, and only his right hand otherwise.

You are given the information about which hand Snuke is raising as two integers L and R. He is raising his left hand if and only if L = 1, and raising his right hand if and only if R = 1. He might not follow the instructions and could raise both hands or not raise any hand at all.

If Snuke is raising only one hand, print Yes if he wants to eat takoyaki, and No if he does not. If he is raising both hands or not raising any hand, print Invalid.

Assume that if Snuke is raising only one hand, he is always following the instructions.

Constraints

  • Each of L and R is 0 or 1.

Input

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

L R

Output

Print Yes, No, or Invalid according to the instructions in the problem statement.


Sample Input 1

1 0

Sample Output 1

Yes

Snuke wants to eat takoyaki, so he is raising only his left hand.


Sample Input 2

1 1

Sample Output 2

Invalid

Snuke is raising both hands.

B - First ABC

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

配点 : 100 点

問題文

A, B, C からなる文字列 S が与えられます。S は A, B, C を全て含むことが保証されます。

S を左から 1 文字ずつ見ていったときに、はじめて次の条件を満たした状態になるのは、左から何文字目まで見たときですか?

  • A, B, C が全て 1 回以上出現している。

制約

  • 3 \leq N \leq 100
  • S は A, B, C からなる長さ N の文字列
  • S は A, B, C を全て含む

入力

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

N
S

出力

答えを出力せよ。


入力例 1

5
ACABB

出力例 1

4

左から 4 文字目までに A は 2 回, B は 1 回, C は 1 回出現していて、条件を満たします。
3 文字目以前では条件を満たさないので答えは 4 です。


入力例 2

4
CABC

出力例 2

3

左から 3 文字目までに A, B, C は 1 回ずつ出現していて、条件を満たします。


入力例 3

30
AABABBBABABBABABCABACAABCBACCA

出力例 3

17

Score : 100 points

Problem Statement

You are given a string S consisting of A, B, and C. S is guaranteed to contain all of A, B, and C.

If the characters of S are checked one by one from the left, how many characters will have been checked when the following condition is satisfied for the first time?

  • All of A, B, and C have appeared at least once.

Constraints

  • 3 \leq N \leq 100
  • S is a string of length N consisting of A, B, and C.
  • S contains all of A, B, and C.

Input

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

N
S

Output

Print the answer.


Sample Input 1

5
ACABB

Sample Output 1

4

In the first four characters from the left, A, B, and C appear twice, once, and once, respectively, satisfying the condition.
The condition is not satisfied by checking three or fewer characters, so the answer is 4.


Sample Input 2

4
CABC

Sample Output 2

3

In the first three characters from the left, each of A, B, and C appears once, satisfying the condition.


Sample Input 3

30
AABABBBABABBABABCABACAABCBACCA

Sample Output 3

17
C - Unique Nicknames

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

配点 : 200 点

問題文

人 1, 人 2, \dots 人 N の N 人の人がいます。人 i の姓は s_i、名は t_i です。

N 人の人すべてにあだ名をつけることを考えます。人 i のあだ名 a_i は以下の条件を満たす必要があります。

  • a_i は人 i の姓あるいは名と一致する。言い換えると、a_i = s_i または a_i = t_i の少なくとも一方が成り立つ。
  • a_i は自分以外の人の姓および名のどちらとも一致しない。言い換えると、1 \leq j \leq N, i \neq j を満たすすべての整数 j について a_i \neq s_j かつ a_i \neq t_j が成り立つ。

N 人全員に条件を満たすあだ名をつけることは可能でしょうか。可能ならば Yes を、そうでないならば No を出力してください。

制約

  • 2 \leq N \leq 100
  • N は整数である。
  • s_i,t_i は英小文字からなる 1 文字以上 10 文字以下の文字列である。

入力

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

N
s_1 t_1
s_2 t_2
\vdots
s_N t_N

出力

N 人すべてにあだ名をつけることが可能ならば Yes を、そうでないならば No を出力せよ。


入力例 1

3
tanaka taro
tanaka jiro
suzuki hanako

出力例 1

Yes

a_1 = taro, a_2 = jiro, a_3 = hanako とすれば、これは問題文にあるあだ名の条件を満たしています。(a_3 は suzuki でもよいです。)
ここで、a_1 = tanaka とはできないことに注意してください。なぜならば 人 2 の姓 s_2 もまた tanaka であるため、あだ名の条件の 2 つ目を満たさなくなるからです。


入力例 2

3
aaa bbb
xxx aaa
bbb yyy

出力例 2

No

問題文の条件を満たすあだ名のつけ方は存在しません。


入力例 3

2
tanaka taro
tanaka taro

出力例 3

No

同姓同名である人の組が存在する場合もあります。


入力例 4

3
takahashi chokudai
aoki kensho
snu ke

出力例 4

Yes

a_1 = chokudai, a_2 = kensho, a_3 = ke とすればよいです。

Score : 200 points

Problem Statement

There are N people numbered Person 1, Person 2, \dots, and Person N. Person i has a family name s_i and a given name t_i.

Consider giving a nickname to each of the N people. Person i's nickname a_i should satisfy all the conditions below.

  • a_i coincides with Person i's family name or given name. In other words, a_i = s_i and/or a_i = t_i holds.
  • a_i does not coincide with the family name and the given name of any other person. In other words, for all integer j such that 1 \leq j \leq N and i \neq j, it holds that a_i \neq s_j and a_i \neq t_j.

Is it possible to give nicknames to all the N people? If it is possible, print Yes; otherwise, print No.

Constraints

  • 2 \leq N \leq 100
  • N is an integer.
  • s_i and t_i are strings of lengths between 1 and 10 (inclusive) consisting of lowercase English alphabets.

Input

Input is given from Standard Input in the following format:

N
s_1 t_1
s_2 t_2
\vdots
s_N t_N

Output

If it is possible to give nicknames to all the N people, print Yes; otherwise, print No.


Sample Input 1

3
tanaka taro
tanaka jiro
suzuki hanako

Sample Output 1

Yes

The following assignment satisfies the conditions of nicknames described in the Problem Statement: a_1 = taro, a_2 = jiro, a_3 = hanako. (a_3 may be suzuki, too.)
However, note that we cannot let a_1 = tanaka, which violates the second condition of nicknames, since Person 2's family name s_2 is tanaka too.


Sample Input 2

3
aaa bbb
xxx aaa
bbb yyy

Sample Output 2

No

There is no way to give nicknames satisfying the conditions in the Problem Statement.


Sample Input 3

2
tanaka taro
tanaka taro

Sample Output 3

No

There may be a pair of people with the same family name and the same given name.


Sample Input 4

3
takahashi chokudai
aoki kensho
snu ke

Sample Output 4

Yes

We can let a_1 = chokudai, a_2 = kensho, and a_3 = ke.

D - String Shifting

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

配点 : 200 点

問題文

空でない文字列に対し、先頭の文字を末尾に移動する操作を左シフト、末尾の文字を先頭に移動する操作を右シフトと呼びます。

例えば、abcde に対して左シフトを 1 回行うと bcdea となり、右シフトを 2 回行うと deabc となります。

英小文字からなる空でない文字列 S が与えられます。S に対し左シフトと右シフトをそれぞれ好きな回数(0 回でもよい)行って得られる文字列のうち、辞書順で最小のものと最大のものを求めてください。

辞書順とは?

辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、英小文字からなる相異なる文字列 S, T の大小を判定するアルゴリズムを以下に説明します。

以下では「 S の i 文字目の文字」を S_i のように表します。また、 S が T より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。

  1. S, T のうち長さが大きくない方の文字列の長さを L とします。i=1,2,\dots,L に対して S_i と T_i が一致するか調べます。
  2. S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_j と T_j を比較して、S_j が T_j よりアルファベット順で小さい場合は S \lt T 、そうでない場合は S \gt T と決定して、アルゴリズムを終了します。
  3. S_i \neq T_i である i が存在しない場合、S と T の長さを比較して、S が T より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。

制約

  • S は英小文字からなる。
  • S の長さは 1 以上 1000 以下である。

入力

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

S

出力

2 行にわたって出力せよ。S に対し左シフトと右シフトをそれぞれ好きな回数(0 回でもよい)行って得られる文字列のうち、辞書順で最小のものと最大のものをそれぞれ S_{\min}, S_{\max} とおいたとき、1 行目には S_{\min} を、2 行目には S_{\max} を出力せよ。


入力例 1

aaba

出力例 1

aaab
baaa

操作によって、aaab , aaba , abaa , baaa の 4 通りの文字列を得ることができます。 これらのうち辞書順で最小、最大のものはそれぞれ aaab , baaa です。


入力例 2

z

出力例 2

z
z

どのように操作を行っても、得られる文字列は z のみです。


入力例 3

abracadabra

出力例 3

aabracadabr
racadabraab

Score : 200 points

Problem Statement

On a non-empty string, a left shift moves the first character to the end of the string, and a right shift moves the last character to the beginning of the string.

For example, a left shift on abcde results in bcdea, and two right shifts on abcde result in deabc.

You are given a non-empty S consisting of lowercase English letters. Among the strings that can be obtained by performing zero or more left shifts and zero or more right shifts on S, find the lexicographically smallest string and the lexicographically largest string.

What is the lexicographical order?

Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. As a more formal definition, here is the algorithm to determine the lexicographical order between different strings S and T.

Below, let S_i denote the i-th character of S. Also, if S is lexicographically smaller than T, we will denote that fact as S \lt T; if S is lexicographically larger than T, we will denote that fact as S \gt T.

  1. Let L be the smaller of the lengths of S and T. For each i=1,2,\dots,L, we check whether S_i and T_i are the same.
  2. If there is an i such that S_i \neq T_i, let j be the smallest such i. Then, we compare S_j and T_j. If S_j comes earlier than T_j in alphabetical order, we determine that S \lt T and quit; if S_j comes later than T_j, we determine that S \gt T and quit.
  3. If there is no i such that S_i \neq T_i, we compare the lengths of S and T. If S is shorter than T, we determine that S \lt T and quit; if S is longer than T, we determine that S \gt T and quit.

Constraints

  • S consists of lowercase English letters.
  • The length of S is between 1 and 1000 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Print two lines. The first line should contain S_{\min}, and the second line should contain S_{\max}. Here, S_{\min} and S_{\max} are respectively the lexicographically smallest and largest strings obtained by performing zero or more left shifts and zero or more right shifts on S.


Sample Input 1

aaba

Sample Output 1

aaab
baaa

By performing shifts, we can obtain four strings: aaab, aaba, abaa, baaa. The lexicographically smallest and largest among them are aaab and baaa, respectively.


Sample Input 2

z

Sample Output 2

z
z

Any sequence of operations results in z.


Sample Input 3

abracadabra

Sample Output 3

aabracadabr
racadabraab
E - Vanish

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

配点 : 300 点

問題文

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

以下の操作をちょうど K 回行った後の A の各要素の和として考えられる最小値を求めてください。

  • 整数 x を選ぶ。A_i = x なる各 i について A_i の値を 0 に置き換える。

制約

  • 1 \leq K \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 入力される値はすべて整数

入力

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

N K
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

6 2
7 2 7 2 2 9

出力例 1

6

はじめ、A = (7, 2, 7, 2, 2, 9) です。

x = 9 として操作を行うと、A = (7, 2, 7, 2, 2, 0) となります。

次に x = 7 として操作を行うと、A = (0, 2, 0, 2, 2, 0) となります。

このとき、A の各要素の和は 0 + 2 + 0 + 2 + 2 + 0 = 6 となります。


入力例 2

8 6
1 2 3 4 1 2 3 4

出力例 2

0

入力例 3

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

出力例 3

8

Score : 300 points

Problem Statement

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

Find the minimum possible sum of all elements of A after performing the following operation exactly K times.

  • Choose an integer x. For each i such that A_i = x, replace the value of A_i with 0.

Constraints

  • 1 \leq K \leq N \leq 3 \times 10^5
  • 1 \leq A_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

Output

Output the answer.


Sample Input 1

6 2
7 2 7 2 2 9

Sample Output 1

6

Initially, A = (7, 2, 7, 2, 2, 9).

Performing the operation with x = 9 gives A = (7, 2, 7, 2, 2, 0).

Next, performing the operation with x = 7 gives A = (0, 2, 0, 2, 2, 0).

At this point, the sum of all elements of A is 0 + 2 + 0 + 2 + 2 + 0 = 6.


Sample Input 2

8 6
1 2 3 4 1 2 3 4

Sample Output 2

0

Sample Input 3

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

Sample Output 3

8
F - Inverse of Permutation

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

配点 : 300 点

問題文

1,2,\dots,N が 1 回ずつ現れる長さ N の数列を「長さ N の順列」と呼びます。
長さ N の順列 P = (p_1, p_2,\dots,p_N) が与えられるので、以下の条件を満たす長さ N の順列 Q = (q_1,\dots,q_N) を出力してください。

  • 全ての i (1 \leq i \leq N) に対して Q の p_i 番目の要素が i である。

ただし、条件を満たす Q は必ずただ 1 つ存在することが証明できます。

制約

  • 1 \leq N \leq 2 \times 10^5
  • (p_1,p_2,\dots,p_N) は長さ N の順列である。
  • 入力は全て整数である。

入力

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

N
p_1 p_2 \dots p_N

出力

数列 Q を空白区切りで 1 行で出力せよ。

q_1 q_2 \dots q_N

入力例 1

3
2 3 1

出力例 1

3 1 2

以下に説明する通り、 Q=(3,1,2) は条件を満たす順列です。

  • i = 1 のとき p_i = 2, q_2 = 1
  • i = 2 のとき p_i = 3, q_3 = 2
  • i = 3 のとき p_i = 1, q_1 = 3

入力例 2

3
1 2 3

出力例 2

1 2 3

全ての i (1 \leq i \leq N) に対して p_i = i が成り立つときは P = Q になります。


入力例 3

5
5 3 2 4 1

出力例 3

5 3 2 4 1

Score : 300 points

Problem Statement

We will call a sequence of length N where each of 1,2,\dots,N occurs once as a permutation of length N.
Given a permutation of length N, P = (p_1, p_2,\dots,p_N), print a permutation of length N, Q = (q_1,\dots,q_N), that satisfies the following condition.

  • For every i (1 \leq i \leq N), the p_i-th element of Q is i.

It can be proved that there exists a unique Q that satisfies the condition.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • (p_1,p_2,\dots,p_N) is a permutation of length N (defined in Problem Statement).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 \dots p_N

Output

Print the sequence Q in one line, with spaces in between.

q_1 q_2 \dots q_N

Sample Input 1

3
2 3 1

Sample Output 1

3 1 2

The permutation Q=(3,1,2) satisfies the condition, as follows.

  • For i = 1, we have p_i = 2, q_2 = 1.
  • For i = 2, we have p_i = 3, q_3 = 2.
  • For i = 3, we have p_i = 1, q_1 = 3.

Sample Input 2

3
1 2 3

Sample Output 2

1 2 3

If p_i = i for every i (1 \leq i \leq N), we will have P = Q.


Sample Input 3

5
5 3 2 4 1

Sample Output 3

5 3 2 4 1
G - Freefall

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

配点 : 400 点

問題文

スーパーマンである高橋くんは、地上で困っている人を助けるため、あるビルの屋上から飛び降りようとしています。 高橋くんがいる星には重力の大きさを表す g という値が定まっており、 高橋くんが落下を開始してから地面に到達するまでにかかる時間は \frac{A}{\sqrt{g}} です。

現在の時刻は 0 であり、g = 1 が成り立ちます。 高橋くんは、今から次の操作を好きな回数(0 回でもよい)行います。

  • 超能力により g の値を 1 増やす。時間が B 経過する。

その後、高橋くんはビルから飛び降ります。落下を開始した後は g の値を変えることはできません。 また、操作によって経過する時間と落下にかかる時間以外は考えないものとします。

高橋くんが地面に到達できる最も早い時刻を求めてください。

制約

  • 1 \leq A \leq 10^{18}
  • 1 \leq B \leq 10^{18}
  • 入力は全て整数

入力

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

A B

出力

高橋くんが地面に到達できる最も早い時刻を出力せよ。 出力は、真の値との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定される。


入力例 1

10 1

出力例 1

7.7735026919
  • 操作を 0 回行うとき、地面に到達する時刻は 1\times 0+\frac{10}{\sqrt{1}} = 10 です。
  • 操作を 1 回行うとき、地面に到達する時刻は 1\times 1+\frac{10}{\sqrt{2}} \fallingdotseq 8.07 です。
  • 操作を 2 回行うとき、地面に到達する時刻は 1\times 2+\frac{10}{\sqrt{3}} \fallingdotseq 7.77 です。
  • 操作を 3 回行うとき、地面に到達する時刻は 1\times 3+\frac{10}{\sqrt{4}} = 8 です。

操作を 4 回以上行っても、地面への到達時刻は遅くなるのみです。 よって、操作を 2 回行ってから飛び降りるのが最適で、答えは 2+\frac{10}{\sqrt{3}} です。


入力例 2

5 10

出力例 2

5.0000000000

操作を 1 回も行わないのが最適です。


入力例 3

1000000000000000000 100

出力例 3

8772053214538.5976562500

Score : 400 points

Problem Statement

A superman, Takahashi, is about to jump off the roof of a building to help a person in trouble on the ground. Takahashi's planet has a constant value g that represents the strength of gravity, and the time it takes for him to reach the ground after starting to fall is \frac{A}{\sqrt{g}}.

It is now time 0, and g = 1. Takahashi will perform the following operation as many times as he wants (possibly zero).

  • Use a superpower to increase the value of g by 1. This takes a time of B.

Then, he will jump off the building. After starting to fall, he cannot change the value of g. Additionally, we only consider the time it takes to perform the operation and fall.

Find the earliest time Takahashi can reach the ground.

Constraints

  • 1 \leq A \leq 10^{18}
  • 1 \leq B \leq 10^{18}
  • All values in the input are integers.

Input

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

A B

Output

Print the earliest time Takahashi can reach the ground. Your output will be accepted when its absolute or relative error from the true value is at most 10^{-6}.


Sample Input 1

10 1

Sample Output 1

7.7735026919
  • If he performs the operation zero times, he will reach the ground at time 1\times 0+\frac{10}{\sqrt{1}} = 10.
  • If he performs the operation once, he will reach the ground at time 1\times 1+\frac{10}{\sqrt{2}} \fallingdotseq 8.07.
  • If he performs the operation twice, he will reach the ground at time 1\times 2+\frac{10}{\sqrt{3}} \fallingdotseq 7.77.
  • If he performs the operation three times, he will reach the ground at time 1\times 3+\frac{10}{\sqrt{4}} = 8.

Performing the operation four or more times will only delay the time to reach the ground. Therefore, it is optimal to perform the operation twice before jumping off, and the answer is 2+\frac{10}{\sqrt{3}}.


Sample Input 2

5 10

Sample Output 2

5.0000000000

It is optimal not to perform the operation at all.


Sample Input 3

1000000000000000000 100

Sample Output 3

8772053214538.5976562500
H - 3 Team Division

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

配点 : 450 点

問題文

N 人の人がおり、3 つのチームに分かれています。

人には 1, 2, \ldots, N の番号、チームには 1, 2, 3 の番号がついており、現在人 i はチーム A_i に所属しています。

各人には強さという値が定まっており、人 i の強さは B_i です。また、チームの強さをチームに所属する人の強さの和として定めます。

0 人以上の人が所属するチームを変更することですべてのチームの強さが等しくなるようにできるか判定してください。すべてのチームの強さが等しくなるようにできる場合は所属するチームを変更する人数として考えられる最小値を求めてください。

ただし、チーム 1, 2, 3 の他に新たにチームを作ることはできません。

制約

  • 3 \leq N \leq 100
  • A_i \in \lbrace 1, 2, 3 \rbrace
  • 各 x \in \lbrace 1, 2, 3 \rbrace に対し、ある i が存在して A_i = x
  • 1 \leq B_i
  • \displaystyle\sum_{i = 1}^{N} B_i \leq 1500
  • 入力される値はすべて整数

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

すべてのチームの強さが等しくなるようにできる場合は所属するチームを変更する人数として考えられる最小値を出力せよ。そうでない場合は -1 を出力せよ。


入力例 1

6
1 2
2 5
1 5
3 3
1 3
3 6

出力例 1

2

人 1 がチーム 3、人 4 がチーム 2 へと所属するチームを変更することですべてのチームの強さが 8 となります。


入力例 2

4
1 1
1 2
2 3
3 4

出力例 2

-1

入力例 3

3
1 1
2 1
3 1

出力例 3

0

入力例 4

12
2 5
1 4
3 3
2 3
3 9
1 2
2 2
3 9
2 6
1 9
1 1
3 1

出力例 4

3

Score : 450 points

Problem Statement

There are N people divided into three teams.

The people are numbered 1, 2, \ldots, N, and the teams are numbered 1, 2, 3. Currently, person i belongs to team A_i.

Each person has a value called strength; person i has a strength of B_i. The strength of a team is defined as the sum of the strengths of its members.

Determine whether it is possible for zero or more people to switch teams so that all teams have equal strength. If it is possible, find the minimum number of people who need to switch teams to achieve this.

You cannot create new teams other than teams 1, 2, 3.

Constraints

  • 3 \leq N \leq 100
  • A_i \in \lbrace 1, 2, 3 \rbrace
  • For each x \in \lbrace 1, 2, 3 \rbrace, there exists some i with A_i = x.
  • 1 \leq B_i
  • \displaystyle\sum_{i = 1}^{N} B_i \leq 1500
  • All input values are integers.

Input

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

If it is possible to make all teams have equal strength, print the minimum number of people who need to switch teams. Otherwise, print -1.


Sample Input 1

6
1 2
2 5
1 5
3 3
1 3
3 6

Sample Output 1

2

If person 1 switches to team 3 and person 4 switches to team 2, all teams will have a strength of 8.


Sample Input 2

4
1 1
1 2
2 3
3 4

Sample Output 2

-1

Sample Input 3

3
1 1
2 1
3 1

Sample Output 3

0

Sample Input 4

12
2 5
1 4
3 3
2 3
3 9
1 2
2 2
3 9
2 6
1 9
1 1
3 1

Sample Output 4

3
I - Reordering

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

配点 : 500 点

問題文

文字列 S が与えられます。S の空でない、連続するとは限らない部分列を並び替えて得られる文字列は何種類ありますか?

答えは非常に大きくなる場合があるので、998244353 で割ったあまりを出力してください。

制約

  • S は英小文字のみからなる長さ 1 以上 5000 以下の文字列

入力

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

S

出力

S の部分列を並び替えて得られる文字列の種類数を 998244353 で割ったあまりを出力せよ。


入力例 1

aab

出力例 1

8

S の部分列を並び替えて得られる文字列は、a, b, aa, ab, ba, aab, aba, baa の 8 種類です。


入力例 2

aaa

出力例 2

3

入力例 3

abcdefghijklmnopqrstuvwxyz

出力例 3

149621752

998244353 で割ったあまりを出力することに注意してください。

Score : 500 points

Problem Statement

Given is a string S. How many different strings can be obtained as a permutation of a non-empty, not necessarily contiguous subsequence of S?

Since the count can be enormous, print it modulo 998244353.

Constraints

  • S is a string of length 1 and 5000 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of different strings that can be obtained as a permutation of a subsequence of S, modulo 998244353.


Sample Input 1

aab

Sample Output 1

8

There are 8 different strings that can be obtained as a permutation of a subsequence of S: a, b, aa, ab, ba, aab, aba, baa.


Sample Input 2

aaa

Sample Output 2

3

Sample Input 3

abcdefghijklmnopqrstuvwxyz

Sample Output 3

149621752

Be sure to print the count modulo 998244353.