A - AtCoder Quiz 3

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

AtCoder で定期的に開催されている、国際的な権威があるコンテストである AtCoder Grand Contest (以下、AGC) は今までに 54 回開催されてきました。

みなさんがちょうど参加している 230 回目の ABC である ABC230 と同様に、 当初は N 回目の AGC のコンテスト名には N3 桁になるようにゼロ埋めした数が割り振られていました。( 1 回目の AGC は AGC001, 2 回目の AGC は AGC002, ...)

ところが、最新の 54 回目の AGC のコンテスト名は AGC055 で、回数より 1 大きい数が割り振られています。これは、AGC042 が社会情勢の影響で中止されて欠番となったため、42 回目以降に開催されたコンテストでは開催された回数より 1 大きい数が割り振られているからです。(入出力例にある説明も参考にしてください。)

さて、ここで問題です。整数 N が与えられるので、N 回目に開催された AGC のコンテスト名を AGCXXX の形式で出力してください。ここで、XXX にはゼロ埋めがなされた 3 桁の数が入ります。

制約

  • 1 \leq N \leq 54
  • N は整数である。

入力

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

N

出力

N 回目に開催された AGC のコンテスト名を所定の形式で出力せよ。


入力例 1

42

出力例 1

AGC043

問題文にある通り、 42 回目以降の AGC には回数より 1 大きい数が割り振られています。
よって 42 回目の AGC のコンテスト名は AGC043 になります。


入力例 2

19

出力例 2

AGC019

41 回目以前の AGC は回数と同じ数が割り振られています。
よって AGC019 が答えとなります。


入力例 3

1

出力例 3

AGC001

問題文にある通り、 1 回目の AGC のコンテスト名は AGC001 です。
数が 3 桁になるようにゼロ埋めを行う必要があるのに注意してください。


入力例 4

50

出力例 4

AGC051

Score : 100 points

Problem Statement

AtCoder Grand Contest (AGC), a regularly held contest with a world authority, has been held 54 times.

Just like the 230-th ABC ― the one you are in now ― is called ABC230, the N-th AGC is initially named with a zero-padded 3-digit number N. (The 1-st AGC is AGC001, the 2-nd AGC is AGC002, ...)

However, the latest 54-th AGC is called AGC055, where the number is one greater than 54. Because AGC042 is canceled and missing due to the social situation, the 42-th and subsequent contests are assigned numbers that are one greater than the numbers of contests held. (See also the explanations at Sample Inputs and Outputs.)

Here is the problem: given an integer N, print the name of the N-th AGC in the format AGCXXX, where XXX is the zero-padded 3-digit number.

Constraints

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

Input

Input is given from Standard Input in the following format:

N

Output

Print the name of the N-th AGC in the specified format.


Sample Input 1

42

Sample Output 1

AGC043

As explained in Problem Statement, the 42-th and subsequent AGCs are assigned numbers that are one greater than the numbers of contests.
Thus, the 42-th AGC is named AGC043.


Sample Input 2

19

Sample Output 2

AGC019

The 41-th and preceding AGCs are assigned numbers that are equal to the numbers of contests.
Thus, the answer is AGC019.


Sample Input 3

1

Sample Output 3

AGC001

As mentioned in Problem Statement, the 1-st AGC is named AGC001.
Be sure to pad the number with zeros into a 3-digit number.


Sample Input 4

50

Sample Output 4

AGC051
B - September

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

英小文字からなる 12 個の文字列 S_1,S_2,\ldots,S_{12} があります。

S_i の長さが i であるような整数 i (1 \leq i \leq 12) がいくつあるか求めてください。

制約

  • S_i は英小文字からなる長さ 1 以上 100 以下の文字列である。(1 \leq i \leq 12)

入力

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

S_1
S_2
\vdots
S_{12}

出力

S_i の長さが i であるような整数 i (1 \leq i \leq 12) がいくつあるか出力せよ。


入力例 1

january
february
march
april
may
june
july
august
september
october
november
december

出力例 1

1

S_i の長さが i であるような整数 i91 つのみです。よって、1 と出力します。


入力例 2

ve
inrtfa
npccxva
djiq
lmbkktngaovl
mlfiv
fmbvcmuxuwggfq
qgmtwxmb
jii
ts
bfxrvs
eqvy

出力例 2

2

S_i の長さが i であるような整数 i4,82 つです。よって、2 と出力します。

Score : 100 points

Problem Statement

There are 12 strings S_1, S_2, \ldots, S_{12} consisting of lowercase English letters.

Find how many integers i (1 \leq i \leq 12) satisfy that the length of S_i is i.

Constraints

  • Each S_i is a string of length between 1 and 100, inclusive, consisting of lowercase English letters. (1 \leq i \leq 12)

Input

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

S_1
S_2
\vdots
S_{12}

Output

Print the number of integers i (1 \leq i \leq 12) such that the length of S_i is i.


Sample Input 1

january
february
march
april
may
june
july
august
september
october
november
december

Sample Output 1

1

There is only one integer i such that the length of S_i is i: 9. Thus, print 1.


Sample Input 2

ve
inrtfa
npccxva
djiq
lmbkktngaovl
mlfiv
fmbvcmuxuwggfq
qgmtwxmb
jii
ts
bfxrvs
eqvy

Sample Output 2

2

There are two integers i such that the length of S_i is i: 4 and 8. Thus, print 2.

C - Calculator

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

00, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 のボタンがある電卓があります。
この電卓で文字列 x が表示されている時に b のボタンを押すと、表示される文字列は x の末尾に b を連結したものとなります。

最初、電卓には空文字列 ( 0 文字の文字列 ) が表示されています。
この電卓に文字列 S を表示させるためにボタンを押す回数の最小値を求めてください。

制約

  • S0, 1, 2, 3, 4, 5, 6, 7, 8, 9 からなる長さ 1 以上 1000 以下の列
  • S の先頭は 0 でない

入力

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

S

出力

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


入力例 1

1000000007

出力例 1

6

1000000007 を表示させるには、 1, 00, 00, 00, 00, 7 のボタンをこの順に押せばよく、ボタンを押した回数は 6 回で、これが達成可能な最小値です。


入力例 2

998244353

出力例 2

9

入力例 3

32000

出力例 3

4

Score : 200 points

Problem Statement

There is a calculator with the buttons 00, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
When a string x is displayed on this calculator and you press a button b, the resulting displayed string becomes the string x with b appended to its end.

Initially, the calculator displays the empty string (a string of length 0).
Find the minimum number of button presses required to display the string S on this calculator.

Constraints

  • S is a string of length at least 1 and at most 1000, consisting of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
  • The first character of S is not 0.

Input

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

S

Output

Print the answer as an integer.


Sample Input 1

1000000007

Sample Output 1

6

To display 1000000007, you can press the buttons 1, 00, 00, 00, 00, 7 in this order. The total number of button presses is 6, and this is the minimum possible.


Sample Input 2

998244353

Sample Output 2

9

Sample Input 3

32000

Sample Output 3

4
D - Ancestor

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

N 人の人がいます。N 人の人には人 1,2,\dots,N と番号がついています。

i(2 \le i \le N) の親は人 P_i です。ここで、P_i < i が保証されます。

1 が人 N の何代前か求めてください。

制約

  • 2 \le N \le 50
  • 1 \le P_i < i(2 \le i \le N)
  • 入力は全て整数。

入力

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

N
P_2 P_3 \dots P_N

出力

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


入力例 1

3
1 2

出力例 1

2

2 は人 3 の親であるため、人 31 代前です。

1 は人 2 の親であるため、人 32 代前です。

よって解は 2 です。


入力例 2

10
1 2 3 4 5 6 7 8 9

出力例 2

9

Score : 200 points

Problem Statement

There are N people, called Person 1, Person 2, \ldots, Person N.

The parent of Person i (2 \le i \le N) is Person P_i. Here, it is guaranteed that P_i < i.

How many generations away from Person N is Person 1?

Constraints

  • 2 \le N \le 50
  • 1 \le P_i < i(2 \le i \le N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_2 P_3 \dots P_N

Output

Print the answer as a positive integer.


Sample Input 1

3
1 2

Sample Output 1

2

Person 2 is a parent of Person 3, and thus is one generation away from Person 3.

Person 1 is a parent of Person 2, and thus is two generations away from Person 3.

Therefore, the answer is 2.


Sample Input 2

10
1 2 3 4 5 6 7 8 9

Sample Output 2

9
E - New Skill Acquired

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君はゲームをしています。このゲームには 1 から N の番号がついた N 個のスキルがあります。

N 個の整数の組 (A_1,B_1), \dots,(A_N,B_N) が与えられます。
(A_i,B_i)=(0,0) のとき高橋君はスキル i を習得済みです。
そうでないとき、スキル A_i,B_i の少なくとも一方を習得済みのときかつそのときに限りスキル i を習得することができます。

既に取得済みのスキルも含め、高橋君が最終的に習得することができるスキルの個数を求めてください。

制約

  • 1\leq N \leq 2\times 10^5
  • (A_i,B_i)=(0,0) または 1\leq A_i,B_i \leq N
  • 入力は全て整数

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

答えを出力せよ。


入力例 1

6
0 0
1 3
3 2
5 5
4 6
6 4

出力例 1

3

最初、高橋君はスキル 1 を習得済みです。スキル 1 を習得済みのためスキル 2 を習得することができ、スキル 2 を習得したことでスキル 3 を習得できます。
スキル 4,5,6 を習得することはできないため、答えは 3 となります。


入力例 2

4
0 0
0 0
0 0
0 0

出力例 2

4

Score : 300 points

Problem Statement

Takahashi is playing a game. This game has N skills numbered 1 through N.

You are given N pairs of integers (A_1,B_1), \dots,(A_N,B_N).
If (A_i,B_i)=(0,0), then Takahashi has already learned skill i.
Otherwise, Takahashi can learn skill i if and only if at least one of skills A_i and B_i has already been learned.

Including the skills already learned, find the number of skills that Takahashi can ultimately learn.

Constraints

  • 1\leq N \leq 2\times 10^5
  • (A_i,B_i)=(0,0) or 1\leq A_i,B_i \leq N
  • 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

Output the answer.


Sample Input 1

6
0 0
1 3
3 2
5 5
4 6
6 4

Sample Output 1

3

At first, Takahashi has already learned skill 1. Because skill 1 has been learned, skill 2 can be learned, and learning skill 2 enables learning skill 3. Since it is impossible to learn skills 4,5,6, the answer is 3.


Sample Input 2

4
0 0
0 0
0 0
0 0

Sample Output 2

4
F - Odd One Subsequence

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
1\leq i<j<k\leq N をみたす整数の組 (i,j,k) であって、 次の条件をみたすものの個数を求めてください。

A_i,A_j,A_k の中にちょうど 2 種類の値が含まれる。すなわち、A_i,A_j,A_k の中のいずれか 2 つは等しく、残りの 1 つは異なる。

制約

  • 3 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq N
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

条件をみたす整数の組の個数を出力せよ。


入力例 1

5
3 2 5 2 2

出力例 1

6

例えば、(i,j,k)=(1,2,4) は、A_1=3, A_2=2, A_4=2 の中に 2,3 のちょうど 2 種類の値が含まれるため、条件をみたします。
これを含めて、(i,j,k)=(1,2,4),(1,2,5),(1,4,5),(2,3,4),(2,3,5),(3,4,5)6 組が条件をみたします。
よって、6 を出力します。


入力例 2

3
1 1 1

出力例 2

0

条件をみたす組が存在しない可能性もあります。

Score : 300 points

Problem Statement

You are given an integer sequence of length N, A=(A_1,A_2,\ldots,A_N).
Find the number of triples of integers (i,j,k) satisfying 1\leq i<j<k\leq N that satisfy the following condition:

Exactly two distinct values are contained in A_i,A_j,A_k. That is, two of A_i,A_j,A_k are equal, and the remaining one is different.

Constraints

  • 3 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq N
  • 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 number of triples of integers that satisfy the condition.


Sample Input 1

5
3 2 5 2 2

Sample Output 1

6

For example, (i,j,k)=(1,2,4) satisfies the condition because exactly two distinct values, 2 and 3, are contained in A_1=3, A_2=2, and A_4=2.
Including this, the six triples (i,j,k)=(1,2,4),(1,2,5),(1,4,5),(2,3,4),(2,3,5),(3,4,5) satisfy the condition.
Therefore, print 6.


Sample Input 2

3
1 1 1

Sample Output 2

0

There may be no triples that satisfy the condition.

G - Polyomino

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

いくつかの正方形を辺でつなげてできる、連結な多角形の形をしたパズルのピースのことを ポリオミノ と呼びます。

4 マス、横 4 マスのグリッドと、グリッドに収まる大きさの 3 個のポリオミノがあります。
i 番目のポリオミノの形は 16 個の文字 P_{i,j,k} (1 \leq j, k \leq 4) によって表されます。P_{i, j, k} は何も置かれていないグリッドに i 番目のポリオミノを置いたときの状態を意味して、P_{i, j, k}# の場合は上から j 行目、左から k 列目のマスにポリオミノが置かれていることを、. の場合は置かれていないことを意味します。(入出力例 1 の図も参考にしてください。)

あなたは次の条件を全て満たすように 3 個のポリオミノ全てをグリッドに敷き詰めることにしました。

  • グリッドの全てのマスはポリオミノで覆われている。
  • ポリオミノ同士が重なるように置くことはできない。
  • ポリオミノがグリッドからはみ出るように置くことはできない。
  • ポリオミノの平行移動と回転は自由に行うことができるが、裏返すことはできない。

条件を満たすようにグリッドにポリオミノを敷き詰めることは可能ですか?

制約

  • P_{i, j, k}# または .
  • 与えられるポリオミノは連結である。つまり、ポリオミノを構成する正方形同士は、正方形のみを上下左右に辿って互いに行き来できる
  • 与えられるポリオミノは空でない

入力

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

P_{1,1,1}P_{1,1,2}P_{1,1,3}P_{1,1,4}
P_{1,2,1}P_{1,2,2}P_{1,2,3}P_{1,2,4}
P_{1,3,1}P_{1,3,2}P_{1,3,3}P_{1,3,4}
P_{1,4,1}P_{1,4,2}P_{1,4,3}P_{1,4,4}
P_{2,1,1}P_{2,1,2}P_{2,1,3}P_{2,1,4}
P_{2,2,1}P_{2,2,2}P_{2,2,3}P_{2,2,4}
P_{2,3,1}P_{2,3,2}P_{2,3,3}P_{2,3,4}
P_{2,4,1}P_{2,4,2}P_{2,4,3}P_{2,4,4}
P_{3,1,1}P_{3,1,2}P_{3,1,3}P_{3,1,4}
P_{3,2,1}P_{3,2,2}P_{3,2,3}P_{3,2,4}
P_{3,3,1}P_{3,3,2}P_{3,3,3}P_{3,3,4}
P_{3,4,1}P_{3,4,2}P_{3,4,3}P_{3,4,4}

出力

問題文の条件を満たすようにポリオミノを敷き詰めることが可能である場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

....
###.
.#..
....
....
.###
.##.
....
..#.
.##.
.##.
.##.

出力例 1

Yes

入力例 1 に対応するポリオミノの形は次の図のようになります。

image1

この場合、次の図のようにポリオミノを配置することで、問題文の条件を満たすようにグリッドにポリオミノを敷き詰めることができます。

image2

よって答えは Yes になります。


入力例 2

###.
#.#.
##..
....
....
..#.
....
....
####
##..
#...
#...

出力例 2

Yes

入力例 21 番目のポリオミノのように、ポリオミノは穴の空いた多角形の形をしている場合があります。


入力例 3

##..
#..#
####
....
....
##..
.##.
....
.#..
.#..
.#..
.#..

出力例 3

No

ポリオミノを敷き詰めるときに、ポリオミノを裏返してはならないのに注意してください。


入力例 4

....
..#.
....
....
....
..#.
....
....
....
..#.
....
....

出力例 4

No

入力例 5

....
####
#...
#...
....
####
...#
..##
....
..##
..#.
..##

出力例 5

No

入力例 6

###.
.##.
..#.
.###
....
...#
..##
...#
....
#...
#...
#...

出力例 6

Yes

Score : 400 points

Problem Statement

A polyomino is a puzzle piece in the shape of a connected polygon made by connecting several squares by their edges.

There is a grid with four rows and four columns, and three polyominoes that fit within the grid.
The shape of the i-th polyomino is represented by 16 characters P_{i,j,k} (1 \leq j, k \leq 4). They describe the state of the grid when the i-th polyomino is placed on it. If P_{i, j, k} is #, the square at the j-th row from the top and k-th column from the left is occupied by the polyomino; if it is ., the square is not occupied. (Refer to the figures at Sample Input/Output 1.)

You want to fill the grid with all three polyominoes so that all of the following conditions are satisfied.

  • All squares of the grid are covered by the polyominoes.
  • The polyominoes must not overlap each other.
  • The polyominoes must not stick out of the grid.
  • The polyominoes may be freely translated and rotated but may not be flipped over.

Can the grid be filled with the polyominoes to satisfy these conditions?

Constraints

  • P_{i, j, k} is # or ..
  • The given polyominoes are connected. In other words, the squares that make up a polyomino can be reached from each other by following only the squares up, down, left, and right.
  • The given polyominoes are not empty.

Input

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

P_{1,1,1}P_{1,1,2}P_{1,1,3}P_{1,1,4}
P_{1,2,1}P_{1,2,2}P_{1,2,3}P_{1,2,4}
P_{1,3,1}P_{1,3,2}P_{1,3,3}P_{1,3,4}
P_{1,4,1}P_{1,4,2}P_{1,4,3}P_{1,4,4}
P_{2,1,1}P_{2,1,2}P_{2,1,3}P_{2,1,4}
P_{2,2,1}P_{2,2,2}P_{2,2,3}P_{2,2,4}
P_{2,3,1}P_{2,3,2}P_{2,3,3}P_{2,3,4}
P_{2,4,1}P_{2,4,2}P_{2,4,3}P_{2,4,4}
P_{3,1,1}P_{3,1,2}P_{3,1,3}P_{3,1,4}
P_{3,2,1}P_{3,2,2}P_{3,2,3}P_{3,2,4}
P_{3,3,1}P_{3,3,2}P_{3,3,3}P_{3,3,4}
P_{3,4,1}P_{3,4,2}P_{3,4,3}P_{3,4,4}

Output

If it is possible to fill the grid with the polyominoes to satisfy the conditions in the problem statement, print Yes; otherwise, print No.


Sample Input 1

....
###.
.#..
....
....
.###
.##.
....
..#.
.##.
.##.
.##.

Sample Output 1

Yes

The figure below shows the shapes of the polyominoes corresponding to Sample Input 1.

image1

In this case, you can fill the grid with them to satisfy the conditions in the problem statement by placing them as shown in the figure below.

image2

Thus, the answer is Yes.


Sample Input 2

###.
#.#.
##..
....
....
..#.
....
....
####
##..
#...
#...

Sample Output 2

Yes

As in the first polyomino in Sample Input 2, a polyomino may be in the shape of a polygon with a hole.


Sample Input 3

##..
#..#
####
....
....
##..
.##.
....
.#..
.#..
.#..
.#..

Sample Output 3

No

Note that the polyominoes may not be flipped over when filling the grid.


Sample Input 4

....
..#.
....
....
....
..#.
....
....
....
..#.
....
....

Sample Output 4

No

Sample Input 5

....
####
#...
#...
....
####
...#
..##
....
..##
..#.
..##

Sample Output 5

No

Sample Input 6

###.
.##.
..#.
.###
....
...#
..##
...#
....
#...
#...
#...

Sample Output 6

Yes
H - Bowls and Beans

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

N 個の大きな茶碗が横一列に並んでおり、左から茶碗 0,1,\dots,N-1 と番号付けされています。
茶碗 i ( 1 \le i \le N-1 ) には整数 C_i が書かれており、初めに中には A_i 個の豆が入っています。
茶碗 0 に整数は書かれておらず、初めは豆も入っていません。

以下の操作を繰り返すことを考えます。

  • ひとつの茶碗 i ( 1 \le i \le N-1 ) を選び、そこから 1 個以上の豆を取り出す。
  • 取り出した豆を、茶碗 i-C_i,i-C_i+1,\dots,i-1 に自由に振り分けて入れる。
    • 厳密には、豆を k 個取り出した際、茶碗 i-C_i,i-C_i+1,\dots,i-1 に合計 k 個の豆を入れる。このとき、どの茶碗にいくつ豆を入れるかの振り分け方を任意に指定して入れる。

全ての豆が茶碗 0 にある状態にするために必要な最小の操作回数を求めてください。

制約

  • 入力は全て整数
  • 2 \le N \le 2000
  • 1 \le C_i \le i
  • 0 \le A_i \le 1
  • \displaystyle \sum_{i=1}^{N-1} A_i > 0

入力

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

N
C_1 C_2 \dots C_{N-1}
A_1 A_2 \dots A_{N-1}

出力

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


入力例 1

5
1 1 2 1
1 0 0 1

出力例 1

3

例えば以下の 3 回の操作で全ての豆が茶碗 0 にある状態にすることができ、これが最小です。

  • 茶碗 4 を選択する。この茶碗の中には豆が 1 つ入っている。
    • 豆のうち 1 つを茶碗 3 に入れる。
  • 茶碗 3 を選択する。この茶碗の中には豆が 1 つ入っている。
    • 豆のうち 1 つを茶碗 1 に入れる。
  • 茶碗 1 を選択する。この茶碗の中には豆が 2 つ入っている。
    • 豆のうち 2 つを茶碗 0 に入れる。

入力例 2

6
1 2 1 3 1
1 1 0 1 1

出力例 2

4

例えば以下の 4 回の操作で全ての豆が茶碗 0 にある状態にすることができ、これが最小です。

  • 茶碗 5 を選択する。この茶碗の中には豆が 1 つ入っている。
    • 豆のうち 1 つを茶碗 4 に入れる。
  • 茶碗 4 を選択する。この茶碗の中には豆が 2 つ入っている。
    • 豆のうち 1 つを茶碗 1 に入れる。
    • 豆のうち 1 つを茶碗 2 に入れる。
  • 茶碗 1 を選択する。この茶碗の中には豆が 2 つ入っている。
    • 豆のうち 2 つを茶碗 0 に入れる。
  • 茶碗 2 を選択する。この茶碗の中には豆が 2 つ入っている。
    • 豆のうち 2 つを茶碗 0 に入れる。

入力例 3

16
1 1 1 2 5 1 1 3 4 1 4 3 1 1 2
1 0 0 0 1 0 0 1 1 0 0 0 0 0 1

出力例 3

7

Score : 475 points

Problem Statement

There are N large bowls arranged in a row, numbered 0,1,\dots,N-1 from the left.
For each bowl i ( 1\le i\le N-1 ), an integer C_i is written on it, and initially it contains A_i beans.
Bowl 0 has no integer written on it and initially contains no beans.

Consider repeating the following operation any number of times:

  • Choose one bowl i (1\le i\le N-1) and take out one or more beans from it.
  • Distribute the taken beans freely among bowls i-C_i,i-C_i+1,\dots,i-1.
    • Formally, when you take out k beans, you must put a total of k beans into bowls i-C_i,i-C_i+1,\dots,i-1, and you may choose how many beans go into each bowl.

Find the minimum number of operations required to put all the beans into bowl 0.

Constraints

  • All input values are integers.
  • 2 \le N \le 2000
  • 1 \le C_i \le i
  • 0 \le A_i \le 1
  • \displaystyle \sum_{i=1}^{N-1} A_i > 0

Input

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

N
C_1 C_2 \dots C_{N-1}
A_1 A_2 \dots A_{N-1}

Output

Output the answer as an integer.


Sample Input 1

5
1 1 2 1
1 0 0 1

Sample Output 1

3

For example, the following three operations put all the beans into bowl 0, and this is the minimum:

  • Choose bowl 4. It has 1 bean.
    • Put 1 bean into bowl 3.
  • Choose bowl 3. It has 1 bean.
    • Put 1 bean into bowl 1.
  • Choose bowl 1. It has 2 beans.
    • Put 2 beans into bowl 0.

Sample Input 2

6
1 2 1 3 1
1 1 0 1 1

Sample Output 2

4

For example, the following four operations put all the beans into bowl 0, and this is the minimum:

  • Choose bowl 5. It has 1 bean.
    • Put 1 bean into bowl 4.
  • Choose bowl 4. It has 2 beans.
    • Put 1 bean into bowl 1.
    • Put 1 bean into bowl 2.
  • Choose bowl 1. It has 2 beans.
    • Put 2 beans into bowl 0.
  • Choose bowl 2. It has 2 beans.
    • Put 2 beans into bowl 0.

Sample Input 3

16
1 1 1 2 5 1 1 3 4 1 4 3 1 1 2
1 0 0 0 1 0 0 1 1 0 0 0 0 0 1

Sample Output 3

7
I - Two Strings

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

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

文字列 X と整数 i に対し、f(X,i)X に対して以下の操作を i 回行い得られる文字列とします。

  • X の先頭の文字を削除し、同じ文字を X の末尾に挿入する。

0 \le i,j \le N-1 を満たす正整数の組 (i,j) のうち、辞書順で f(S,i)f(T,j) より小さいか同じであるものの個数を求めてください。

辞書順とは?

辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、英小文字からなる相異なる文字列 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 \le N \le 2 \times 10^5
  • S,T は英小文字からなる長さ N の文字列
  • N は整数

入力

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

N
S
T

出力

答えを出力せよ。


入力例 1

3
adb
cab

出力例 1

4

条件を満たす (i,j) の組は (0,0),(0,2),(2,0),(2,2)4 個があります。

(i,j)=(1,2) は、f(S,i)=dba,f(T,j)=bca であるため条件を満たしません。


入力例 2

10
wsiuhwijsl
pwqoketvun

出力例 2

56

Score : 500 points

Problem Statement

You are given strings S and T of length N each, consisting of lowercase English letters.

For a string X and an integer i, let f(X,i) be the string obtained by performing on X the following operation i times:

  • Remove the first character of X and append the same character to the end of X.

Find the number of integer pairs (i,j) satisfying 0 \le i,j \le N-1 such that f(S,i) is lexicographically smaller than or equal to f(T,j).

What is the lexicographical order?

Simply put, the lexicographical order is the order in which the words are arranged in a dictionary. Let us define it formally by describing an algorithm of finding the ordering of two distinct strings S and T consisting of lowercase English letters.

Here, we denote "the i-th character of a string S" by S_i. Also, we write S \lt T and S \gt T if S is lexicographically smaller and larger than T, respectively.

  1. Let L be the smallest of the lengths of S and T. For i=1,2,\dots,L, we check if S_i equals T_i.
  2. If there is an i such that S_i \neq T_i, let j be the smallest such i. Comparing S_j and T_j, we terminate the algorithm by determining that S \lt T if S_j is smaller than T_j in the alphabetical order, and that S \gt T otherwise.
  3. If there is no i such that S_i \neq T_i, comparing the lengths of S and T, we terminate the algorithm by determining that S \lt T if S is shorter than T, and that S \gt T otherwise.

Constraints

  • 1 \le N \le 2 \times 10^5
  • S and T are strings of length N each, consisting of lowercase English letters.
  • N is an integer.

Input

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

N
S
T

Output

Print the answer.


Sample Input 1

3
adb
cab

Sample Output 1

4

There are 4 pairs of (i, j) satisfying the conditions: (0,0),(0,2),(2,0), and (2,2).

(i,j)=(1,2) does not satisfy the conditions because f(S,i)=dba and f(T,j)=bca.


Sample Input 2

10
wsiuhwijsl
pwqoketvun

Sample Output 2

56