A - Operations on a Stack

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

長さ N の整数列 (A_1, A_2, \ldots, A_N) が与えられます。 また、列 S があり、S ははじめは空列です。

i = 1, 2, \ldots, N の順に、それぞれの i について、下記の 2 つの操作のうちちょうど 1 つを行います。

  • S の末尾に A_i を要素として追加する。
  • S の末尾の要素を削除する。ただし S が空のときは、この操作を行うことを選択できない。

すべての操作を終えた後の、S の要素の総和としてあり得る最大値を出力してください。

制約

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

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

6
3 -1 -4 5 -9 2

出力例 1

8

S が空列である初期状態から、下記の通りに操作を行うことを考えます。

  • i = 1 について、S の末尾に A_1 = 3 を追加する。その結果、S = (3) となる。
  • i = 2 について、S の末尾に A_2 = -1 を追加する。その結果、S = (3, -1) となる。
  • i = 3 について、S の末尾の要素を削除する。その結果、S = (3) となる。
  • i = 4 について、S の末尾に A_4 = 5 を追加する。その結果、S = (3, 5) となる。
  • i = 5 について、S の末尾に A_5 = -9 を追加する。その結果、S = (3, 5, -9) となる。
  • i = 6 について、S の末尾の要素を削除する。その結果、S = (3, 5) となる。

このとき、すべての操作を終えた後の S の要素の総和は、3 + 5 = 8 であり、これがあり得る最大値です。


入力例 2

1
-1

出力例 2

-1

S が空のときは必ず、要素を追加する方の操作を選ばなければならないことに注意してください。


入力例 3

20
-14 74 -48 38 -51 43 5 37 -39 -29 80 -44 -55 59 17 89 -37 -68 38 -16

出力例 3

369

Score : 400 points

Problem Statement

You are given an integer sequence of length N: (A_1, A_2, \ldots, A_N). There is also a sequence S, which is initially empty.

For each i = 1, 2, \ldots, N in this order, you perform exactly one of the following two operations:

  • Append A_i as an element to the end of S.
  • Delete the last element of S. You cannot choose this operation if S is empty.

Print the maximum possible value of the sum of the elements of S after all operations.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • -10^9 \leq A_i \leq 10^9
  • 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

6
3 -1 -4 5 -9 2

Sample Output 1

8

Starting from the initial state where S is an empty sequence, consider the following operations:

  • For i = 1, append A_1 = 3 to the end of S. Now, S = (3).
  • For i = 2, append A_2 = -1 to the end of S. Now, S = (3, -1).
  • For i = 3, delete the last element of S. Now, S = (3).
  • For i = 4, append A_4 = 5 to the end of S. Now, S = (3, 5).
  • For i = 5, append A_5 = -9 to the end of S. Now, S = (3, 5, -9).
  • For i = 6, delete the last element of S. Now, S = (3, 5).

Here, the sum of the elements of S after all operations is 3 + 5 = 8, which is the maximum possible value.


Sample Input 2

1
-1

Sample Output 2

-1

Note that if S is empty, you must choose to append an element.


Sample Input 3

20
-14 74 -48 38 -51 43 5 37 -39 -29 80 -44 -55 59 17 89 -37 -68 38 -16

Sample Output 3

369
B - Minimum Cost Sort

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 600

問題文

(1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N) が与えられます。 高橋君は P に対して次の操作を繰り返し(0回でも良い)行う事ができます。

  • 1\leq i\leq N-1 をみたす整数を 1 つ選ぶ。コスト i を支払い、P_iP_{i+1} を交換する。

P を昇順にソートするために必要なコストの総和の最小値を求めてください。

制約

  • 2 \leq N \leq 2\times 10^5
  • (P_1,P_2,\ldots,P_N)(1,2,\ldots,N) の順列
  • 入力はすべて整数

入力

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

N
P_1 P_2 \ldots P_N

出力

P を昇順にソートするために必要なコストの総和の最小値を出力せよ。


入力例 1

3
3 2 1

出力例 1

4

高橋君のは次のようにして P を昇順にソートすることができます。

  • コストを 1 支払い、P_1=3P_2=2 を交換する。P=(2,3,1) となる。
  • コストを 2 支払い、P_2=3P_3=1 を交換する。P=(2,1,3) となる。
  • コストを 1 支払い、P_1=2P_2=1 を交換する。P=(1,2,3) となる。

このように操作を行ったときコストの総和は 4 であり、このときが最小となります。


入力例 2

5
2 4 1 3 5

出力例 2

6

入力例 3

2
1 2

出力例 3

0

Score : 600 points

Problem Statement

You are given a permutation P = (P_1, P_2, \ldots, P_N) of (1, 2, \ldots, N). Takahashi can repeatedly perform the following operation on P (possibly zero times):

  • Choose an integer i satisfying 1 \leq i \leq N-1. Pay a cost of i, and swap P_i and P_{i+1}.

Find the minimum total cost required to sort P in ascending order.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • (P_1, P_2, \ldots, P_N) is a permutation of (1, 2, \ldots, N).
  • All input values are integers.

Input

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

N
P_1 P_2 \ldots P_N

Output

Print the minimum total cost required to sort P in ascending order.


Sample Input 1

3
3 2 1

Sample Output 1

4

Takahashi can sort P in ascending order as follows:

  • Pay a cost of 1 and swap P_1 = 3 and P_2 = 2. Now, P = (2, 3, 1).
  • Pay a cost of 2 and swap P_2 = 3 and P_3 = 1. Now, P = (2, 1, 3).
  • Pay a cost of 1 and swap P_1 = 2 and P_2 = 1. Now, P = (1, 2, 3).

The total cost for these operations is 4, which is the minimum possible.


Sample Input 2

5
2 4 1 3 5

Sample Output 2

6

Sample Input 3

2
1 2

Sample Output 3

0
C - Cost to Flip

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 600

問題文

01 のみからなる 2 つの長さ N の整数列 A = (A_1, A_2, \ldots, A_N)B = (B_1, B_2, \ldots, B_N) が与えられます。

A に対して下記の操作を好きな回数( 0 回でも良い)だけ行うことができます。

  1. まず、1 \leq i \leq N を満たす整数 i を選び、A_i の値を反転する(元の値が 0 ならば 1 に、元の値が 1 ならば 0 に変更する)。
  2. その後、操作のコストとして、\sum_{k=1}^N A_kC_k 円を支払う。

上記の手順 2. におけるコストの計算には、手順 1. で変更が加えられた後の A を用いることに注意してください。

AB に一致させるために支払う合計費用の最小値を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • A_i, B_i \in \lbrace 0, 1\rbrace
  • 1 \leq C_i \leq 10^6
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N
C_1 C_2 \ldots C_N

出力

答えを出力せよ。


入力例 1

4
0 1 1 1
1 0 1 0
4 6 2 9

出力例 1

16

下記の手順を考えます。

  • まず A_4 を反転する。その結果、A = (0, 1, 1, 0) となる。この操作のコストとして 0 \times 4 + 1 \times 6 + 1 \times 2 + 0 \times 9 = 8 円支払う。
  • 次に A_2 を反転する。その結果、A = (0, 0, 1, 0) となる。この操作のコストとして 0 \times 4 + 0 \times 6 + 1 \times 2 + 0 \times 9 = 2 円支払う。
  • 最後に A_1 を反転する。その結果、A = (1, 0, 1, 0) となり、B に一致する。この操作のコストとして 1 \times 4 + 0 \times 6 + 1 \times 2 + 0 \times 9 = 6 円支払う。

このとき、支払う合計費用は 8 + 2 + 6 = 16 円であり、これが考えられる最小値です。


入力例 2

5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

出力例 2

0

AB ははじめから一致しているため、一度も操作を行う必要がありません。


入力例 3

20
1 1 1 1 0 0 1 1 0 0 0 1 0 1 0 1 1 0 1 0
0 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 1 0 0
52 73 97 72 54 15 79 67 13 55 65 22 36 90 84 46 1 2 27 8

出力例 3

2867

Score : 600 points

Problem Statement

You are given two integer sequences of length N, A = (A_1, A_2, \ldots, A_N) and B = (B_1, B_2, \ldots, B_N), each consisting of 0 and 1.

You can perform the following operation on A any number of times (possibly zero):

  1. First, choose an integer i satisfying 1 \leq i \leq N, and flip the value of A_i (if the original value is 0, change it to 1; if it is 1, change it to 0).
  2. Then, pay \sum_{k=1}^N A_k C_k yen as the cost of this operation.

Note that the cost calculation in step 2 uses the A after the change in step 1.

Print the minimum total cost required to make A identical to B.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • A_i, B_i \in {0, 1}
  • 1 \leq C_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
B_1 B_2 \ldots B_N
C_1 C_2 \ldots C_N

Output

Print the answer.


Sample Input 1

4
0 1 1 1
1 0 1 0
4 6 2 9

Sample Output 1

16

Consider the following procedure:

  • First, flip A_4. Now, A = (0, 1, 1, 0). The cost of this operation is 0 \times 4 + 1 \times 6 + 1 \times 2 + 0 \times 9 = 8 yen.
  • Next, flip A_2. Now, A = (0, 0, 1, 0). The cost of this operation is 0 \times 4 + 0 \times 6 + 1 \times 2 + 0 \times 9 = 2 yen.
  • Finally, flip A_1. Now, A = (1, 0, 1, 0), which matches B. The cost of this operation is 1 \times 4 + 0 \times 6 + 1 \times 2 + 0 \times 9 = 6 yen.

In this case, the total cost is 8 + 2 + 6 = 16 yen, which is the minimum possible.


Sample Input 2

5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

Sample Output 2

0

A and B are already identical initially, so there is no need to perform any operations.


Sample Input 3

20
1 1 1 1 0 0 1 1 0 0 0 1 0 1 0 1 1 0 1 0
0 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 1 0 0
52 73 97 72 54 15 79 67 13 55 65 22 36 90 84 46 1 2 27 8

Sample Output 3

2867
D - Reverse Brackets

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 700

問題文

以下のいずれかの条件を満たす文字列を正しい括弧列と定義します。

  • 空文字列
  • ある正しい括弧列 A が存在して、(, A, ) をこの順に連結した文字列
  • ある空でない正しい括弧列 A, B が存在して、A, B をこの順に連結した文字列

長さ N の正しい括弧列 S が与えられます。あなたは以下の操作を何度でも行うことができます。

  • S の連続部分文字列のうち、正しい括弧列であるものを 1 個選び、反転させる。

ここで Sl 文字目から r 文字目までからなる連続部分文字列を反転させるとは、以下を行うことを指します。

  • l \le i \le r を満たす整数 i に対して同時に S_iS_{l+r-i}( なら ) に、) なら ( に置き換える。(ここでの反転は、通常の反転の定義とは異なる点に注意してください。)

操作終了時にあり得る S の個数を 998244353 で割ったあまりを求めてください。

制約

  • 1 \le N \le 5000
  • |S|=N
  • S は正しい括弧列

入力

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

N
S

出力

答えを出力せよ。


入力例 1

6
(())()

出力例 1

2

例えば、以下のように操作をすることで、S()(()) にすることができます。

  • S の連続部分列として、S1 文字目から 6 文字目を選ぶ。これは正しい括弧列である。S()(()) となる。

他に作ることのできる S(())() のみです。よって答えは 2 です。


入力例 2

2
()

出力例 2

1

Score : 700 points

Problem Statement

A string is defined to be a valid parenthesis sequence if and only if it satisfies one of the following conditions:

  • It is an empty string.
  • There exists a valid parenthesis sequence A such that the string is obtained by concatenating (, A, and ) in this order.
  • There exist non-empty valid parenthesis sequences A and B such that the string is obtained by concatenating A and B in this order.

You are given a valid parenthesis sequence S of length N. You can perform the following operation any number of times:

  • Choose a contiguous substring of S that is a valid parenthesis sequence, and reverse it.

Here, reversing the substring of S from the l-th character to the r-th character means the following:

  • For every integer i satisfying l \leq i \leq r, simultaneously replace S_i with ) if S_{l+r-i} is (, and with ( if S_{l+r-i} is ).(Note that reversing here is different from the usual definition of reversing.)

Find the number, modulo 998244353, of distinct strings S that you can have at the end of the process.

Constraints

  • 1 \leq N \leq 5000
  • |S| = N
  • S is a valid parenthesis sequence.

Input

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

N
S

Output

Print the answer.


Sample Input 1

6
(())()

Sample Output 1

2

For example, you can transform S into ()(()) by doing the following:

  • Choose the substring from the 1st to the 6th character of S. This is a valid parenthesis sequence. S becomes ()(()).

The only other string that can be formed is (())(). Thus, the answer is 2.


Sample Input 2

2
()

Sample Output 2

1
E - Swap 0^X and 1^Y

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 900

問題文

01 のみからなる長さ N の文字列 S, T と、正整数 X, Y が与えられます。 i = 1, 2, \ldots, N について、Si 文字目を S_i で表します。

「下記の操作 A と操作 B のどちらかを行う」ことを好きな回数( 0 回でも良い)だけ繰り返すことで、ST に一致させることができるかどうかを判定してください。

  • (操作 A)1 \leq i \leq N-(X+Y)+1, S_{i} = S_{i+1} = \cdots = S_{i+X-1} = 0, S_{i+X} = S_{i+X+1} = \cdots = S_{i+X+Y-1} = 1 を満たす整数 i を選び、S_{i}, S_{i+1}, \ldots, S_{i+Y-1} をそれぞれ 1 に、S_{i+Y}, S_{i+Y+1}, \ldots, S_{i+Y+X-1} をそれぞれ 0 に変更する。

  • (操作 B)1 \leq i \leq N-(X+Y)+1, S_{i} = S_{i+1} = \cdots = S_{i+Y-1} = 1, S_{i+Y} = S_{i+Y+1} = \cdots = S_{i+Y+X-1} = 0 を満たす整数 i を選び、S_{i}, S_{i+1}, \ldots, S_{i+X-1} をそれぞれ 0 に、S_{i+X}, S_{i+X+1}, \ldots, S_{i+X+Y-1} をそれぞれ 1 に変更する。

制約

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq X, Y \leq N
  • S, T はそれぞれ 01 のみからなる長さ N の文字列
  • 入力はすべて整数

入力

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

N X Y
S
T

出力

ST に一致させることが可能な場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

9 2 1
000111001
011000011

出力例 1

Yes

下記の手順によって ST に一致させることができます。

  • まず、i = 2 として操作 A を行う。その結果、S = 010011001 となる。
  • 次に、i = 6 として操作 B を行う。その結果、S = 010010011 となる。
  • 最後に、i = 3 として操作 A を行う。その結果、S = 011000011 となる。

よって、Yes を出力します。


入力例 2

1 1 1
0
1

出力例 2

No

ST に一致させることはできません。よって、No を出力します。

Score : 900 points

Problem Statement

You are given two strings S and T, each of length N and consisting of 0 and 1, as well as two positive integers X and Y. For i = 1, 2, \ldots, N, let S_i denote the i-th character of S.

Determine whether it is possible to make S identical to T by repeatedly performing Operations A and B below any number of times (possibly zero) in any order:

  • (Operation A) Choose an integer i satisfying 1 \leq i \leq N-(X+Y)+1, S_{i} = S_{i+1} = \cdots = S_{i+X-1} = 0, and S_{i+X} = S_{i+X+1} = \cdots = S_{i+X+Y-1} = 1, then change each of S_{i}, S_{i+1}, \ldots, S_{i+Y-1} to 1 and each of S_{i+Y}, S_{i+Y+1}, \ldots, S_{i+Y+X-1} to 0.

  • (Operation B) Choose an integer i satisfying 1 \leq i \leq N-(X+Y)+1, S_{i} = S_{i+1} = \cdots = S_{i+Y-1} = 1, and S_{i+Y} = S_{i+Y+1} = \cdots = S_{i+Y+X-1} = 0, then change each of S_{i}, S_{i+1}, \ldots, S_{i+X-1} to 0 and each of S_{i+X}, S_{i+X+1}, \ldots, S_{i+X+Y-1} to 1.

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq X, Y \leq N
  • S and T are strings of length N consisting of 0 and 1.
  • All input values are integers.

Input

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

N X Y
S
T

Output

If it is possible to make S identical to T, print Yes; otherwise, print No.


Sample Input 1

9 2 1
000111001
011000011

Sample Output 1

Yes

The following procedure can transform S into T:

  • First, perform Operation A with i = 2. Now, S = 010011001.
  • Next, perform Operation B with i = 6. Now, S = 010010011.
  • Finally, perform Operation A with i = 3. Now, S = 011000011.

Thus, print Yes.


Sample Input 2

1 1 1
0
1

Sample Output 2

No

It is impossible to make S identical to T. Thus, print No.