A - Exponential Plant

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

高橋君は植物を育てています。その植物の発芽時の高さは 0\,\mathrm{cm} です。発芽した日を 0 日目としたとき、発芽してから i (0 \le i) 日目の夜には 2^i\,\mathrm{cm} 植物の高さが伸びます。

高橋君の身長は H\,\mathrm{cm} です。

高橋君は毎朝この植物と背比べをします。植物の高さが高橋君の身長より高くなるのは発芽から何日目の朝か求めてください。

制約

  • 1 \leq H \leq 10^{9}
  • 入力は全て整数である。

入力

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

H

出力

植物の高さが高橋君の身長より高くなる日は発芽から何日目の朝か出力せよ。


入力例 1

54

出力例 1

6

植物が発芽してからの 1 日ごとの朝の高さは 1\,\mathrm{cm},3\,\mathrm{cm},7\,\mathrm{cm}, 15\,\mathrm{cm},31\,\mathrm{cm},63\,\mathrm{cm} となります。 6 日目の朝に高橋君より高くなるため、 6 を出力します。


入力例 2

7

出力例 2

4

植物が発芽してから 3 日目の朝の高さは 7\,\mathrm{cm} で、 4 日目の朝の高さは 15\,\mathrm{cm} です。 4 日目の朝に高橋君より高くなるため、 4 を出力します。3 日目の朝のときは高橋君と同じ高さですが、高橋君より高くないことに注意してください。


入力例 3

262144

出力例 3

19

Score : 100 points

Problem Statement

Takahashi is growing a plant. Its height at the time of germination is 0\,\mathrm{cm}. Considering the day of germination as day 0, its height increases by 2^i\,\mathrm{cm} day i's night (0 \le i).

Takahashi's height is H\,\mathrm{cm}.

Every morning, Takahashi measures his height against this plant. Find the first day such that the plant's height is strictly greater than Takahashi's height in the morning.

Constraints

  • 1 \leq H \leq 10^{9}
  • All input values are integers.

Input

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

H

Output

Print an integer representing the first day such that the plant's height is greater than Takahashi's height in the morning.


Sample Input 1

54

Sample Output 1

6

The plant's height in the mornings of days 1, 2, 3, 4, 5, 6 will be 1\,\mathrm{cm}, 3\,\mathrm{cm}, 7\,\mathrm{cm}, 15\,\mathrm{cm}, 31\,\mathrm{cm}, 63\,\mathrm{cm}, respectively. The plant becomes taller than Takahashi in the morning day 6, so print 6.


Sample Input 2

7

Sample Output 2

4

The plant's height will be 7\,\mathrm{cm} in the morning of day 3 and 15\,\mathrm{cm} in the morning day 4. The plant becomes taller than Takahashi in the morning of day 4, so print 4. Note that, in the morning of day 3, the plant is as tall as Takahashi, but not taller.


Sample Input 3

262144

Sample Output 3

19
B - AtCoder Janken 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

N 人の AtCoder ユーザーが集まり、 AtCoderじゃんけん2 を行います。i 人目のユーザー名は S_i 、レートは C_i です。

AtCoderじゃんけん2 は以下の手順で行われます。

  • それぞれのユーザーに、ユーザー名の辞書順に 0, 1, \dots ,N - 1 の番号を割り当てる。
  • N 人のレートの総和を T とする。番号 T \bmod N を割り当てられたユーザーが勝者となる。

勝者のユーザー名を出力してください。

辞書順とは?

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

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

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

制約

  • 1 \leq N \leq 100
  • S_i は英小文字からなる長さ 3 以上 16 以下の文字列
  • S_1, S_2, \dots ,S_N は全て異なる
  • 1 \leq C_i \leq 4229
  • C_i は整数

入力

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

N
S_1 C_1
S_2 C_2
\vdots
S_N C_N

出力

答えを一行に出力せよ。


入力例 1

3
takahashi 2
aoki 6
snuke 5

出力例 1

snuke

3 人のレートの総和は 13 です。また、それぞれの名前を辞書順に並び替えると、 aoki snuke takahashi の順になるので aoki に番号 0 が、 snuke に 番号 1 が、 takahashi に番号 2 が割り当てられます。

13 \bmod 3 = 1 なので、番号 1 を割り当てられた snuke を出力します。


入力例 2

3
takahashi 2813
takahashixx 1086
takahashix 4229

出力例 2

takahashix

Score : 200 points

Problem Statement

N AtCoder users have gathered to play AtCoder RPS 2. The i-th user's name is S_i and their rating is C_i.

AtCoder RPS 2 is played as follows:

  • Assign the numbers 0, 1, \dots, N - 1 to the users in lexicographical order of their usernames.
  • Let T be the sum of the ratings of the N users. The user assigned the number T \bmod N is the winner.

Print the winner's username.

What is lexicographical order?

Lexicographical order, simply put, means "the order in which words appear in a dictionary." More precisely, the algorithm to determine the order of two distinct strings S and T consisting of lowercase English letters is as follows:

Here, "the i-th character of S" is denoted as S_i. If S is lexicographically smaller than T, we write S \lt T, and if S is larger, we write S \gt T.

  1. Let L be the length of the shorter string among S and T. Check if S_i and T_i match for i=1,2,\dots,L.
  2. If there exists an i such that S_i \neq T_i, let j be the smallest such i. Compare S_j and T_j. If S_j is alphabetically smaller than T_j, then S \lt T. Otherwise, S \gt T. The algorithm ends here.
  3. If there is no i such that S_i \neq T_i, compare the lengths of S and T. If S is shorter than T, then S \lt T. If S is longer, then S \gt T. The algorithm ends here.

Constraints

  • 1 \leq N \leq 100
  • S_i is a string consisting of lowercase English letters with length between 3 and 16, inclusive.
  • S_1, S_2, \dots, S_N are all distinct.
  • 1 \leq C_i \leq 4229
  • C_i is an integer.

Input

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

N
S_1 C_1
S_2 C_2
\vdots
S_N C_N

Output

Print the answer on a single line.


Sample Input 1

3
takahashi 2
aoki 6
snuke 5

Sample Output 1

snuke

The sum of the ratings of the three users is 13. Sorting their names in lexicographical order yields aoki, snuke, takahashi, so aoki is assigned number 0, snuke is 1, and takahashi is 2.

Since 13 \bmod 3 = 1, print snuke, who is assigned number 1.


Sample Input 2

3
takahashi 2813
takahashixx 1086
takahashix 4229

Sample Output 2

takahashix
C - AtCoder Magics

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

高橋くんは、カードゲーム「AtCoder Magics」のカードを N 枚持っています。i 番目のカードをカード i と呼ぶことにします。各カードには強さとコストのパラメーターがあり、カード i の強さは A_i で、コストは C_i です。

高橋くんは、弱いカードは要らないので捨てることにしました。具体的には、以下の操作をできなくなるまで繰り返します。

  • 2 つのカード x, y であって、 A_x > A_y かつ C_x < C_y であるようなものを選ぶ。カード y を捨てる。

操作ができなくなったとき、捨てられなかったカードの集合は一意に定まることが証明できます。これを求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, C_i \leq 10^9
  • A_1, A_2, \dots ,A_N は全て異なる
  • C_1, C_2, \dots ,C_N は全て異なる
  • 入力はすべて整数

入力

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

N
A_1 C_1
A_2 C_2
\vdots
A_N C_N

出力

捨てられなかったカードは m 枚あり、それらの番号が昇順に i_1, i_2, \dots ,i_m であったとする。このとき、以下の形式で出力せよ。

m
i_1 i_2 \cdots i_m

入力例 1

3
2 4
1 1
3 2

出力例 1

2
2 3

カード 1, 3 に注目すると、 A_1 < A_3 かつ C_1 > C_3 なのでカード 1 を捨てることができます。

それ以上操作をすることはできません。このときカード 2, 3 が残っているので、これらを出力します。


入力例 2

5
1 1
10 2
100 3
1000 4
10000 5

出力例 2

5
1 2 3 4 5

この場合、どのカードも捨てることができません。


入力例 3

6
32 101
65 78
2 29
46 55
103 130
52 40

出力例 3

4
2 3 5 6

Score : 350 points

Problem Statement

Takahashi has N cards from the card game "AtCoder Magics." The i-th card will be called card i. Each card has two parameters: strength and cost. Card i has a strength of A_i and a cost of C_i.

He does not like weak cards, so he will discard them. Specifically, he will repeat the following operation until it can no longer be performed:

  • Choose two cards x and y such that A_x > A_y and C_x < C_y. Discard card y.

It can be proved that the set of remaining cards when the operations can no longer be performed is uniquely determined. Find this set of cards.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, C_i \leq 10^9
  • A_1, A_2, \dots ,A_N are all distinct.
  • C_1, C_2, \dots ,C_N are all distinct.
  • All input values are integers.

Input

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

N
A_1 C_1
A_2 C_2
\vdots
A_N C_N

Output

Let there be m remaining cards, cards i_1, i_2, \dots, i_m, in ascending order. Print these in the following format:

m
i_1 i_2 \cdots i_m

Sample Input 1

3
2 4
1 1
3 2

Sample Output 1

2
2 3

Focusing on cards 1 and 3, we have A_1 < A_3 and C_1 > C_3, so card 1 can be discarded.

No further operations can be performed. At this point, cards 2 and 3 remain, so print them.


Sample Input 2

5
1 1
10 2
100 3
1000 4
10000 5

Sample Output 2

5
1 2 3 4 5

In this case, no cards can be discarded.


Sample Input 3

6
32 101
65 78
2 29
46 55
103 130
52 40

Sample Output 3

4
2 3 5 6
D - AtCoder Wallpaper

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

AtCoder 社の壁紙の模様を xy 平面上に表現すると、以下のようになります。

  • 以下の 3 種類の直線で領域が分割されている。
    • x = n (n は整数)
    • y = n (n は偶数)
    • x + y = n (n は偶数)
  • 各領域は白もしくは黒で塗られている。いずれかの直線で隣接する 2 領域は異なる色で塗られている。
  • (0.5, 0.5) を含む領域は黒で塗られている。

下の図は、模様の一部を表したものです。

整数 A, B, C, D が与えられます。各辺が x, y 軸に平行で、左下の頂点が (A, B) にあり右上の頂点が (C, D) にあるような長方形を考えます。この長方形の内側に存在する黒で塗られた領域の面積を求め、それを 2 倍したものを出力してください。

出力する値は整数になることが証明できます。

制約

  • -10^9 \leq A, B, C, D \leq 10^9
  • A < C かつ B < D
  • 入力はすべて整数

入力

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

A B C D

出力

答えを一行に出力せよ。


入力例 1

0 0 3 3

出力例 1

10

求めるのは、以下の正方形で囲われた領域内の黒く塗られた領域の面積です。

これは 5 なので、 2 倍した 10 を出力します。


入力例 2

-1 -2 1 3

出力例 2

11

面積は 5.5 と小数になりますが、出力するべき値は整数になります。


入力例 3

-1000000000 -1000000000 1000000000 1000000000

出力例 3

4000000000000000000

これは長方形が最大のケースですが、出力は 64bit 符号付き整数の範囲に収まります。

Score : 450 points

Problem Statement

The pattern of AtCoder's wallpaper can be represented on the xy-plane as follows:

  • The plane is divided by the following three types of lines:
    • x = n (where n is an integer)
    • y = n (where n is an even number)
    • x + y = n (where n is an even number)
  • Each region is painted black or white. Any two regions adjacent along one of these lines are painted in different colors.
  • The region containing (0.5, 0.5) is painted black.

The following figure shows a part of the pattern.

You are given integers A, B, C, D. Consider a rectangle whose sides are parallel to the x- and y-axes, with its bottom-left vertex at (A, B) and its top-right vertex at (C, D). Calculate the area of the regions painted black inside this rectangle, and print twice that area.

It can be proved that the output value will be an integer.

Constraints

  • -10^9 \leq A, B, C, D \leq 10^9
  • A < C and B < D.
  • All input values are integers.

Input

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

A B C D

Output

Print the answer on a single line.


Sample Input 1

0 0 3 3

Sample Output 1

10

We are to find the area of the black-painted region inside the following square:

The area is 5, so print twice that value: 10.


Sample Input 2

-1 -2 1 3

Sample Output 2

11

The area is 5.5, which is not an integer, but the output value is an integer.


Sample Input 3

-1000000000 -1000000000 1000000000 1000000000

Sample Output 3

4000000000000000000

This is the case with the largest rectangle, where the output still fits into a 64-bit signed integer.

E - Remove Pairs

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

高橋君と青木君は N 枚のカードを使ってとあるゲームをします。i 枚目のカードの表面には A_i が、裏面には B_i が書かれています。初めに場には N 枚のカードが並べられており、高橋君を先手として、2 人は以下の操作を交互に繰り返します。

  • 場にあるカードの中から表面同士に同じ数が書かれている、または裏面同士に同じ数が書かれている 2 枚のカードの組を選び、そのカードを場から取り除く。このようなカードの組が存在しない場合、操作を行えない。

先に操作を行えなくなった方が負けとなり、もう一方が勝ちとなります。 両者がそれぞれ勝つために最適な操作を選ぶ時、どちらが勝つか求めてください。

制約

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

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

2 人とも最適に操作を行なったとき、高橋君が勝つ場合は Takahashi と、青木君が勝つ場合は Aoki と出力せよ。


入力例 1

5
1 9
2 5
4 9
1 4
2 5

出力例 1

Aoki

髙橋君が最初に取り除いた 2 枚が

  • 1 枚目と 3 枚目のカードだった場合: 青木君は 2 枚目と 5 枚目のカードを取り除くことで勝つことができる。

  • 1 枚目と 4 枚目のカードだった場合: 青木君は 2 枚目と 5 枚目のカードを取り除くことで勝つことができる。

  • 2 枚目と 5 枚目のカードだった場合: 青木君は 1 枚目と 3 枚目のカードを取り除くことで勝つことができる。

髙橋君が最初の操作で取り除くことができるカードの組み合わせは以上の 3 通りのみで、髙橋君がどのような操作を行っても青木君が勝つことができるため、青木君が答えとなります。


入力例 2

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

出力例 2

Takahashi

Score : 475 points

Problem Statement

Takahashi and Aoki are playing a game using N cards. The front side of the i-th card has A_i written on it, and the back side has B_i written on it. Initially, the N cards are laid out on the table. With Takahashi going first, the two players take turns performing the following operation:

  • Choose a pair of cards from the table such that either the numbers on their front sides are the same or the numbers on their back sides are the same, and remove these two cards from the table. If no such pair of cards exists, the player cannot perform the operation.

The player who is first to be unable to perform the operation loses, and the other player wins. Determine who wins if both players play optimally.

Constraints

  • 1 \leq N \leq 18
  • 1 \leq A_i, B_i \leq 10^9
  • 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

Print Takahashi if Takahashi wins when both players play optimally, and Aoki otherwise.


Sample Input 1

5
1 9
2 5
4 9
1 4
2 5

Sample Output 1

Aoki

If Takahashi first removes

  • the first and third cards: Aoki can win by removing the second and fifth cards.

  • the first and fourth cards: Aoki can win by removing the second and fifth cards.

  • the second and fifth cards: Aoki can win by removing the first and third cards.

These are the only three pairs of cards Takahashi can remove in his first move, and Aoki can win in all cases. Therefore, the answer is Aoki.


Sample Input 2

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

Sample Output 2

Takahashi
F - Useless for LIS

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 525

問題文

長さ N の整数列 A が与えられます。

t = 1, 2, \dots ,N について、 A_tA の最長増加部分列に含まれることがあるか判定してください。

A_tA の最長増加部分列に含まれることがあるとは、以下のことをいいます。

  • 最長増加部分列の長さを L とする。各要素が 1 以上 N 以下の単調増加な整数列 i = (i_1, i_2, \dots ,i_L) \ (i_1 < i_2 < \dots < i_L) であって以下をすべて満たすものが存在する。

    • A_{i_1} < A_{i_2} < \dots < A_{i_L}
    • ある k \ (1 \leq k \leq L) が存在して i_k = t である

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

最長増加部分列とは?

A の部分列とは A の要素をいくつか抜き出して元の順に並べてできる列を指します。

A の最長増加部分列とは、 A の狭義単調増加な部分列のうち列の長さが最大のものを指します。

制約

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 全てのテストケースにおける N の総和は 2 \times 10^5 以下

入力

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

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

ここで \mathrm{case_i}i 番目のケースの入力を意味する。各ケースは以下の形式で与えられる。

N
A_1 A_2 \cdots A_N

出力

以下の形式で出力せよ。

\mathrm{answer}_1
\mathrm{answer}_2
\vdots
\mathrm{answer}_T

ここで \mathrm{answer}_ii 番目のケースの出力を意味する。各ケースについては、次の通りである。

A_tA の最長増加部分列に含まれることがある tm 個存在し、昇順に i_1, i_2, \dots ,i_m であったとする。このとき、以下の形式で出力せよ。

m
i_1 i_2 \cdots i_m

入力例 1

1
5
2 1 4 5 3

出力例 1

4
1 2 3 4

最長増加部分列の 1 つは (2, 4, 5) で、長さは 3 です。(1, 4, 5) も最長増加部分列の 1 つです。しかし、 A_5 を含む最長増加部分列はありません。

よって、 1, 2, 3, 4 を出力します。


入力例 2

2
6
2 5 3 4 3 4
5
10000 1000 100 1 10

出力例 2

5
1 3 4 5 6
2
4 5

Score : 525 points

Problem Statement

You are given an integer sequence A of length N.

For each t = 1, 2, \dots, N, determine whether A_t is included in a longest increasing subsequence of A.

Here, A_t is included in a longest increasing subsequence of A if and only if the following holds:

  • Let L be the length of a longest increasing subsequence of A. There exists a strictly increasing integer sequence i = (i_1, i_2, \dots, i_L) \ (i_1 < i_2 < \dots < i_L), where each element is between 1 and N, inclusive, that satisfies all of the following conditions:

    • A_{i_1} < A_{i_2} < \dots < A_{i_L}.
    • i_k = t for some k \ (1 \leq k \leq L).

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

What is a longest increasing subsequence?

A subsequence of a sequence A is a sequence that can be derived by extracting some elements from A without changing the order.

A longest increasing subsequence of a sequence A is a subsequence of A that is strictly increasing with the greatest possible length.

Constraints

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • The sum of N across all test cases is at most 2 \times 10^5.

Input

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

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

Here, \mathrm{case_i} represents the input for the i-th case. Each case is given in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the answers in the following format:

\mathrm{answer}_1
\mathrm{answer}_2
\vdots
\mathrm{answer}_T

Here, \mathrm{answer}_i represents the output for the i-th case. For each case, let there be m indices t such that A_t is included in a longest increasing subsequence of A, which are i_1, i_2, \dots, i_m in ascending order. Print these in the following format:

m
i_1 i_2 \cdots i_m

Sample Input 1

1
5
2 1 4 5 3

Sample Output 1

4
1 2 3 4

One of the longest increasing subsequences is (2, 4, 5), with a length of 3. Another longest increasing subsequence is (1, 4, 5). However, no longest increasing subsequence includes A_5.

Therefore, print 1, 2, 3, 4.


Sample Input 2

2
6
2 5 3 4 3 4
5
10000 1000 100 1 10

Sample Output 2

5
1 3 4 5 6
2
4 5
G - Select Strings

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 625

問題文

N 個の英小文字からなる文字列 S_1,S_2,\ldots,S_NN 個の正整数 A_1,A_2,\ldots,A_N があります。

ある \lbrace 1,2,\ldots,N \rbrace の部分集合 Ti,j \in T (i \neq j)S_iS_j の部分文字列となるような i,j の組がないとき良い集合であるといいます。

良い集合 T を選んだ時 \displaystyle \sum_{i \in T} A_i としてありえる値の最大値を求めてください。

部分文字列とは? S部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。 例えば、ababc の部分文字列ですが、acabc の部分文字列ではありません。

制約

  • 1 \leq N \leq 100
  • S_i は英小文字からなる文字列である
  • 1 \leq |S_i|
  • |S_1|+|S_2| + \ldots + |S_N| \leq 5000
  • 1 \leq A_i \leq 10^9

入力

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

N
S_1
S_2
\vdots
S_N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

4
atcoder
at
coder
code
5 2 3 4

出力例 1

6

良い集合 としてありえる T とそれぞれに対する \displaystyle \sum_{i \in T} A_i は以下の通りです。

  • T = \lbrace 1 \rbrace のとき \displaystyle \sum_{i \in T} A_i = 5
  • T = \lbrace 2 \rbrace のとき \displaystyle \sum_{i \in T} A_i = 2
  • T = \lbrace 3 \rbrace のとき \displaystyle \sum_{i \in T} A_i = 3
  • T = \lbrace 4 \rbrace のとき \displaystyle \sum_{i \in T} A_i = 4
  • T = \lbrace 2,3 \rbrace のとき \displaystyle \sum_{i \in T} A_i = 5
  • T = \lbrace 2,4 \rbrace のとき \displaystyle \sum_{i \in T} A_i = 6

このうち最大値は 6 なので、6 を出力します。


入力例 2

10
abcd
abc
ab
a
b
c
d
ab
bc
cd
100 10 50 30 60 90 80 70 40 20

出力例 2

260

Score : 625 points

Problem Statement

You are given N strings S_1, S_2, \ldots, S_N consisting of lowercase English letters and N positive integers A_1, A_2, \ldots, A_N.

A subset T of \lbrace 1, 2, \ldots, N \rbrace is called a good set if there is no pair i, j \in T (i \neq j) such that S_i is a substring of S_j.

Find the maximum possible value of \displaystyle \sum_{i \in T} A_i for a good set T.

What is a substring? A substring of a string S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S. For example, ab is a substring of abc, but ac is not a substring of abc.

Constraints

  • 1 \leq N \leq 100
  • S_i is a string consisting of lowercase English letters.
  • 1 \leq |S_i|
  • |S_1| + |S_2| + \ldots + |S_N| \leq 5000
  • 1 \leq A_i \leq 10^9

Input

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

N
S_1
S_2
\vdots
S_N
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

4
atcoder
at
coder
code
5 2 3 4

Sample Output 1

6

The possible good sets T and their corresponding \displaystyle \sum_{i \in T} A_i are as follows:

  • T = \lbrace 1 \rbrace: \displaystyle \sum_{i \in T} A_i = 5
  • T = \lbrace 2 \rbrace: \displaystyle \sum_{i \in T} A_i = 2
  • T = \lbrace 3 \rbrace: \displaystyle \sum_{i \in T} A_i = 3
  • T = \lbrace 4 \rbrace: \displaystyle \sum_{i \in T} A_i = 4
  • T = \lbrace 2, 3 \rbrace: \displaystyle \sum_{i \in T} A_i = 5
  • T = \lbrace 2, 4 \rbrace: \displaystyle \sum_{i \in T} A_i = 6

The maximum among them is 6, so print 6.


Sample Input 2

10
abcd
abc
ab
a
b
c
d
ab
bc
cd
100 10 50 30 60 90 80 70 40 20

Sample Output 2

260