A - Similar String

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

二つの文字 xy は以下の 3 つの条件のうちどれか 1 つを満たすとき、似た文字と呼ばれます。

  • xy は同じ文字
  • xy の片方が 1 で、もう片方が l
  • xy の片方が 0 で、もう片方が o

また、長さ N の文字列 ST は以下の条件を満たすとき、似た文字列と呼ばれます。

  • 任意の i\ (1\leq i\leq N) について、 Si 番目の文字と Ti 番目の文字は似た文字

英小文字及び数字からなる長さ N の文字列 S,T が与えられます。 ST が似た文字列か判定してください。

制約

  • N1 以上 100 以下の整数
  • S,T は英小文字及び数字からなる長さ N の文字列

入力

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

N
S
T

出力

ST が似た文字列の場合 Yes を、そうでない場合 No を出力せよ。


入力例 1

3
l0w
1ow

出力例 1

Yes

S1 文字目は lで、T1 文字目は 1です。これらは似た文字です。

S2 文字目は 0で、T2 文字目は oです。これらは似た文字です。

S3 文字目は wで、T3 文字目は wです。これらは似た文字です。

よって ST は似た文字列です。


入力例 2

3
abc
arc

出力例 2

No

S2 文字目は bで、T2 文字目は rです。これらは似た文字ではありません。

よって ST は似た文字列ではありません。


入力例 3

4
nok0
n0ko

出力例 3

Yes

Score : 100 points

Problem Statement

Two characters x and y are called similar characters if and only if one of the following conditions is satisfied:

  • x and y are the same character.
  • One of x and y is 1 and the other is l.
  • One of x and y is 0 and the other is o.

Two strings S and T, each of length N, are called similar strings if and only if:

  • for all i\ (1\leq i\leq N), the i-th character of S and the i-th character of T are similar characters.

Given two length-N strings S and T consisting of lowercase English letters and digits, determine if S and T are similar strings.

Constraints

  • N is an integer between 1 and 100.
  • Each of S and T is a string of length N consisting of lowercase English letters and digits.

Input

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

N
S
T

Output

Print Yes if S and T are similar strings, and No otherwise.


Sample Input 1

3
l0w
1ow

Sample Output 1

Yes

The 1-st character of S is l, and the 1-st character of T is 1. These are similar characters.

The 2-nd character of S is 0, and the 2-nd character of T is o. These are similar characters.

The 3-rd character of S is w, and the 3-rd character of T is w. These are similar characters.

Thus, S and T are similar strings.


Sample Input 2

3
abc
arc

Sample Output 2

No

The 2-nd character of S is b, and the 2-nd character of T is r. These are not similar characters.

Thus, S and T are not similar strings.


Sample Input 3

4
nok0
n0ko

Sample Output 3

Yes
B - Discord

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

1,2,\ldots,N と番号づけられた N 人が M 回、一列に並んで集合写真を撮りました。i 番目の撮影で左から j 番目に並んだ人の番号は a_{i,j} です。

ある二人組は M 回の撮影で一度も連続して並ばなかった場合、不仲である可能性があります。  

不仲である可能性がある二人組の個数を求めてください。なお、人 x と人 y からなる二人組と人 y と人 x からなる二人組は区別しません。

制約

  • 2 \leq N \leq 50
  • 1 \leq M \leq 50
  • 1 \leq a_{i,j} \leq N
  • a_{i,1},\ldots,a_{i,N} には 1,\ldots,N1 回ずつ現れる
  • 入力はすべて整数

入力

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

N M
a_{1,1} \ldots a_{1,N}
\vdots
a_{M,1} \ldots a_{M,N}

出力

答えを出力せよ。


入力例 1

4 2
1 2 3 4
4 3 1 2

出力例 1

2

1 と人 4 からなる二人組と、人 2 と人 4 からなる二人組がそれぞれ不仲である可能性があります。


入力例 2

3 3
1 2 3
3 1 2
1 2 3

出力例 2

0

入力例 3

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

出力例 3

6

Score : 200 points

Problem Statement

N people numbered 1,2,\ldots,N were in M photos. In each of the photos, they stood in a single line. In the i-th photo, the j-th person from the left is person a_{i,j}.

Two people who did not stand next to each other in any of the photos may be in a bad mood.

How many pairs of people may be in a bad mood? Here, we do not distinguish a pair of person x and person y, and a pair of person y and person x.

Constraints

  • 2 \leq N \leq 50
  • 1 \leq M \leq 50
  • 1 \leq a_{i,j} \leq N
  • a_{i,1},\ldots,a_{i,N} contain each of 1,\ldots,N exactly once.
  • All values in the input are integers.

Input

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

N M
a_{1,1} \ldots a_{1,N}
\vdots
a_{M,1} \ldots a_{M,N}

Output

Print the answer.


Sample Input 1

4 2
1 2 3 4
4 3 1 2

Sample Output 1

2

The pair of person 1 and person 4, and the pair of person 2 and person 4, may be in a bad mood.


Sample Input 2

3 3
1 2 3
3 1 2
1 2 3

Sample Output 2

0

Sample Input 3

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

Sample Output 3

6
C - Dash

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

二次元平面の点 (0,0) に高橋君がいます。初め、高橋君の体力は H です。また、二次元平面には M 個の体力を回復するアイテムがあり、i 個目のアイテムは点 (x_i,y_i) に置いてあります。

高橋君は、これから N 回の移動をします。i 回目の移動は以下の方法で行われます。

  1. 今高橋君がいる点を (x,y) とする。体力を 1 消費し、Si 番目の文字 S_i に応じて以下の点に移動する。

    • S_iR のとき: (x+1,y)
    • S_iL のとき: (x-1,y)
    • S_iU のとき: (x,y+1)
    • S_iD のとき: (x,y-1)
  2. 高橋君の体力が負になった場合、高橋君は倒れてしまい、移動をやめる。そうでない場合、移動した点にアイテムがあり、かつ高橋君の体力が K 未満ならば、移動した点に置かれたアイテムを消費し、高橋君の体力が K になる。

高橋君が一度も倒れることなく N 回の移動を行えるか判定してください。

制約

  • 1\leq N,M,H,K\leq 2\times 10^5
  • SR, L, U, D からなる長さ N の文字列
  • |x_i|,|y_i| \leq 2\times 10^5
  • (x_i,y_i) は互いに異なる
  • S 以外の入力は全て整数

入力

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

N M H K
S
x_1 y_1
\vdots
x_M y_M

出力

高橋君が一度も倒れることなく N 回の移動を行える場合 Yes を、そうでない場合 No を出力せよ。


入力例 1

4 2 3 1
RUDL
-1 -1
1 0

出力例 1

Yes

初め高橋君の体力は 3 です。以下で移動を説明します。

  • 1 回目の移動: S_iR なので点 (1,0) に移動する。高橋君の体力は 2 に減る。点 (1,0) にはアイテムが置いてあるが、高橋君の体力は K=1 以上なのでアイテムは消費されない。

  • 2 回目の移動: S_iU なので点 (1,1) に移動する。高橋君の体力は 1 に減る。

  • 3 回目の移動: S_iD なので点 (1,0) に移動する。高橋君の体力は 0 に減る。点 (1,0) にはアイテムが置いてあり、体力は K=1 未満なのでアイテムを消費し、体力が 1 になる。

  • 4 回目の移動: S_iL なので点 (0,0) に移動する。高橋君の体力は 0 に減る。

以上より、高橋君は倒れずに 4 回の移動を行えるので、Yes を出力してください。体力は 0 になってもいいことに注意してください。


入力例 2

5 2 1 5
LDRLD
0 0
-1 -1

出力例 2

No

初め高橋君の体力は 1 です。以下で移動を説明します。

  • 1 回目の移動: S_iL なので点 (-1,0) に移動する。高橋君の体力は 0 に減る。

  • 2 回目の移動: S_iD なので点 (-1,-1) に移動する。高橋君の体力は -1 に減る。体力が -1 になってしまったので、高橋君は倒れてしまい、移動をやめる。

以上より、高橋君は倒れてしまうので、No を出力してください。

高橋君がはじめいる点 (0,0) にはアイテムが置いてありますが、移動後にアイテムは消費されるので、1 回目の移動前にアイテムを消費しないことに注意してください。

Score : 300 points

Problem Statement

On a two-dimensional plane, Takahashi is initially at point (0, 0), and his initial health is H. M items to recover health are placed on the plane; the i-th of them is placed at (x_i,y_i).

Takahashi will make N moves. The i-th move is as follows.

  1. Let (x,y) be his current coordinates. He consumes a health of 1 to move to the following point, depending on S_i, the i-th character of S:

    • (x+1,y) if S_i is R;
    • (x-1,y) if S_i is L;
    • (x,y+1) if S_i is U;
    • (x,y-1) if S_i is D.
  2. If Takahashi's health has become negative, he collapses and stops moving. Otherwise, if an item is placed at the point he has moved to, and his health is strictly less than K, then he consumes the item there to make his health K.

Determine if Takahashi can complete the N moves without being stunned.

Constraints

  • 1\leq N,M,H,K\leq 2\times 10^5
  • S is a string of length N consisting of R, L, U, and D.
  • |x_i|,|y_i| \leq 2\times 10^5
  • (x_i, y_i) are pairwise distinct.
  • All values in the input are integers, except for S.

Input

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

N M H K
S
x_1 y_1
\vdots
x_M y_M

Output

Print Yes if he can complete the N moves without being stunned; print No otherwise.


Sample Input 1

4 2 3 1
RUDL
-1 -1
1 0

Sample Output 1

Yes

Initially, Takahashi's health is 3. We describe the moves below.

  • 1-st move: S_i is R, so he moves to point (1,0). His health reduces to 2. Although an item is placed at point (1,0), he do not consume it because his health is no less than K=1.

  • 2-nd move: S_i is U, so he moves to point (1,1). His health reduces to 1.

  • 3-rd move: S_i is D, so he moves to point (1,0). His health reduces to 0. An item is placed at point (1,0), and his health is less than K=1, so he consumes the item to make his health 1.

  • 4-th move: S_i is L, so he moves to point (0,0). His health reduces to 0.

Thus, he can make the 4 moves without collapsing, so Yes should be printed. Note that the health may reach 0.


Sample Input 2

5 2 1 5
LDRLD
0 0
-1 -1

Sample Output 2

No

Initially, Takahashi's health is 1. We describe the moves below.

  • 1-st move: S_i is L, so he moves to point (-1,0). His health reduces to 0.

  • 2-nd move: S_i is D, so he moves to point (-1,-1). His health reduces to -1. Now that the health is -1, he collapses and stops moving.

Thus, he will be stunned, so No should be printed.

Note that although there is an item at his initial point (0,0), he does not consume it before the 1-st move, because items are only consumed after a move.

D - Shift vs. CapsLock

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

あなたのパソコンのキーボードには、a キー・Shift キー・CapsLock キーの 3 種類のキーがあります。また、CapsLock キーにはランプが付いています。 初め、CapsLock キーのランプは OFF であり、パソコンの画面には空文字列が表示されています。

あなたは、以下の 3 種類の操作のうち 1 つを選んで実行するということを 0 回以上何度でも行うことができます。

  • X ミリ秒かけて a キーのみを押す。CapsLock キーのランプが OFF ならば画面の文字列の末尾に a が付け足され、ON ならば A が付け足される。
  • Y ミリ秒かけて Shift キーと a キーを同時に押す。CapsLock キーのランプが OFF ならば画面の文字列の末尾に A が付け足され、 ON ならば a が付け足される。
  • Z ミリ秒かけて CapsLock キーを押す。CapsLock キーのランプが OFF ならば ON に、ON ならば OFF に切り替わる。

Aa からなる文字列 S が与えられます。画面の文字列を S に一致させるのに必要な最短の時間は何ミリ秒かを求めてください。

制約

  • 1 \leq X,Y,Z \leq 10^9
  • X,Y,Z は整数
  • 1 \leq |S| \leq 3 \times 10^5
  • SAa からなる文字列

入力

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

X Y Z
S

出力

答えを出力せよ。


入力例 1

1 3 3
AAaA

出力例 1

9

以下のように操作を行うと 9 ミリ秒で画面の文字列を AAaA に一致させられます。これが最短の時間です。

  • Z(=3) ミリ秒かけて CapsLock キーを押す。CapsLock キーのランプが ON になる。
  • X(=1) ミリ秒かけて a キーを押す。A が画面の文字列の末尾に付け足される。
  • X(=1) ミリ秒かけて a キーを押す。A が画面の文字列の末尾に付け足される。
  • Y(=3) ミリ秒かけて Shift キーと a キーを同時に押す。a が画面の文字列の末尾に付け足される。
  • X(=1) ミリ秒かけて a キーを押す。A が画面の文字列の末尾に付け足される。

入力例 2

1 1 100
aAaAaA

出力例 2

6

入力例 3

1 2 4
aaAaAaaAAAAaAaaAaAAaaaAAAAA

出力例 3

40

Score : 400 points

Problem Statement

Your computer has a keyboard with three keys: 'a' key, Shift key, and Caps Lock key. The Caps Lock key has a light on it. Initially, the light on the Caps Lock key is off, and the screen shows an empty string.

You can do the following three actions any number of times in any order:

  • Spend X milliseconds to press only the 'a' key. If the light on the Caps Lock key is off, a is appended to the string on the screen; if it is on, A is.
  • Spend Y milliseconds to press the 'a' key and Shift key simultaneously. If the light on the Caps Lock key is off, A is appended to the string on the screen; if it is on, a is.
  • Spend Z milliseconds to press the Caps Lock key. If the light on the Caps Lock key is off, it turns on; if it is on, it turns off.

Given a string S consisting of A and a, determine at least how many milliseconds you need to spend to make the string shown on the screen equal to S.

Constraints

  • 1 \leq X,Y,Z \leq 10^9
  • X, Y, and Z are integers.
  • 1 \leq |S| \leq 3 \times 10^5
  • S is a string consisting of A and a.

Input

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

X Y Z
S

Output

Print the answer.


Sample Input 1

1 3 3
AAaA

Sample Output 1

9

The following sequence of actions makes the string on the screen equal to AAaA in 9 milliseconds, which is the shortest possible.

  • Spend Z(=3) milliseconds to press the CapsLock key. The light on the Caps Lock key turns on.
  • Spend X(=1) milliseconds to press the 'a' key. A is appended to the string on the screen.
  • Spend X(=1) milliseconds to press the 'a' key. A is appended to the string on the screen.
  • Spend Y(=3) milliseconds to press the Shift key and 'a' key simultaneously. a is appended to the string on the screen.
  • Spend X(=1) milliseconds to press the 'a' key. A is appended to the string on the screen.

Sample Input 2

1 1 100
aAaAaA

Sample Output 2

6

Sample Input 3

1 2 4
aaAaAaaAAAAaAaaAaAAaaaAAAAA

Sample Output 3

40
E - A Gift From the Stars

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 475

問題文

以下の条件を満たす k+1 頂点 k 辺のグラフをレベル k\ (k\geq 2) の星と呼びます。

  • ある 1 つの頂点が、他の k 個の頂点と 1 本ずつ辺で結ばれている。それ以外の辺は存在しない。

高橋君は、はじめ何個かの星からなるグラフを持っていました。そして、以下の手続きを全てのグラフの頂点が連結になるまでくり返し行いました。

  • 持っているグラフの頂点から二つの頂点を選ぶ。このとき、選んだ二つの頂点の次数は共に 1 であり、かつ選んだ二つの頂点は非連結である必要がある。選んだ二つの頂点を結ぶ辺を張る。

その後、高橋君は手続きが終了した後のグラフの頂点に、適当に 1 から N の番号を付けました。このグラフは木となっており、これを T と呼びます。T には N-1 本の辺があり、 i 番目の辺は u_iv_i を結んでいました。

その後高橋君は、はじめ持っていた星の個数とレベルを忘れてしまいました。T の情報からはじめ持っていた星の個数とレベルを求めてください。

制約

  • 3\leq N\leq 2\times 10^5
  • 1\leq u_i, v_i\leq N
  • 与えられるグラフは、問題文中の手続きによって得られる N 頂点の木
  • 入力は全て整数

入力

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

N
u_1 v_1
\vdots
u_{N-1} v_{N-1}

出力

高橋君が持っていた星が M 個であり、星のレベルがそれぞれ L=(L_1,L_2,\ldots,L_M) であったとする。 このとき、L を昇順に並び替え空白区切りで出力せよ。

なお、この問題では常に解が一意に定まることが証明できる。


入力例 1

6
1 2
2 3
3 4
4 5
5 6

出力例 1

2 2

以下の図のように、2 つのレベル 2 の星から T は得られます。


入力例 2

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

出力例 2

2 2 2

入力例 3

20
8 3
8 18
2 19
8 20
9 17
19 7
8 7
14 12
2 15
14 10
2 13
2 16
2 1
9 5
10 15
14 6
2 4
2 11
5 12

出力例 3

2 3 4 7

Score : 475 points

Problem Statement

A graph with (k+1) vertices and k edges is called a level-k\ (k\geq 2) star if and only if:

  • it has a vertex that is connected to each of the other k vertices with an edge, and there are no other edges.

At first, Takahashi had a graph consisting of stars. He repeated the following operation until every pair of vertices in the graph was connected:

  • choose two vertices in the graph. Here, the vertices must be disconnected, and their degrees must be both 1. Add an edge that connects the chosen two vertices.

He then arbitrarily assigned an integer from 1 through N to each of the vertices in the graph after the procedure. The resulting graph is a tree; we call it T. T has (N-1) edges, the i-th of which connects u_i and v_i.

Takahashi has now forgotten the number and levels of the stars that he initially had. Find them, given T.

Constraints

  • 3\leq N\leq 2\times 10^5
  • 1\leq u_i, v_i\leq N
  • The given graph is an N-vertex tree obtained by the procedure in the problem statement.
  • All values in the input are integers.

Input

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

N
u_1 v_1
\vdots
u_{N-1} v_{N-1}

Output

Suppose that Takahashi initially had M stars, whose levels were L=(L_1,L_2,\ldots,L_M). Sort L in ascending order, and print them with spaces in between.

We can prove that the solution is unique in this problem.


Sample Input 1

6
1 2
2 3
3 4
4 5
5 6

Sample Output 1

2 2

Two level-2 stars yield T, as the following figure shows:


Sample Input 2

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

Sample Output 2

2 2 2

Sample Input 3

20
8 3
8 18
2 19
8 20
9 17
19 7
8 7
14 12
2 15
14 10
2 13
2 16
2 1
9 5
10 15
14 6
2 4
2 11
5 12

Sample Output 3

2 3 4 7
F - Damage over Time

Time Limit: 3.5 sec / Memory Limit: 1024 MB

配点 : 525

問題文

あなたの前に体力 H のモンスターが現れ、ターン制の戦闘が開始しました。  

あなたは、ターン 1,2,… のそれぞれに呪文 1,…,NN 種類の呪文のうち一つを唱えます。  

ターン i に呪文 j を唱えると、その呪文の効果としてターン i,i+1,…,i+t_j -1 のそれぞれでモンスターの体力が d_j 減ります。

モンスターの体力を 0 以下にすることが可能な最も早いターンを求めてください。

制約

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq H \leq 10^{18}
  • 1 \leq t_i,d_i \leq 10^9
  • 入力はすべて整数

入力

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

N H
t_1 d_1
\vdots
t_N d_N

出力

答えを出力せよ。


入力例 1

2 20
2 2
5 1

出力例 1

6

以下のようにするとターン 6 にモンスターの体力を 0 以下に出来ます。これが最も早いターンです。

  • ターン 1 に魔法 1 を使う。ターン 1 に使った魔法の効果でモンスターの体力が 2 減り、18 になる。
  • ターン 2 に魔法 2 を使う。ターン 1,2 に使った魔法の効果でモンスターの体力が 2+1=3 減り、15 になる。
  • ターン 3 に魔法 1 を使う。ターン 2,3 に使った魔法の効果でモンスターの体力が 1+2=3 減り、12 になる。
  • ターン 4 に魔法 2 を使う。ターン 2,3,4 に使った魔法の効果でモンスターの体力が 1+2+1=4 減り、8 になる。
  • ターン 5 に魔法 1 を使う。ターン 2,4,5 に使った魔法の効果でモンスターの体力が 1+1+2=4 減り、4 になる。
  • ターン 6 に魔法 2 を使う。ターン 2,4,5,6 に使った魔法の効果でモンスターの体力が 1+1+2+1=5 減り、-1 になる。

入力例 2

10 200
1 21
1 1
1 1
8 4
30 1
3 1
10 2
8 1
9 1
4 4

出力例 2

9

Score : 525 points

Problem Statement

A monster with health H has appeared in front of you, and a turn-based battle has started.

In each turn 1,2,…, you cast one of N spells, spells 1,…,N.

If you cast spell i in turn j, the monster's health reduces by d_i in each turn j,j+1,…,j+t_i -1.

Find the earliest turn that you can make the monster's health 0 or less.

Constraints

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq H \leq 10^{18}
  • 1 \leq t_i,d_i \leq 10^9
  • All values in the input are integers.

Input

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

N H
t_1 d_1
\vdots
t_N d_N

Output

Print the answer.


Sample Input 1

2 20
2 2
5 1

Sample Output 1

6

The following procedure makes the monster's health 0 or less in turn 6, which is the earliest.

  • Cast spell 1 in turn 1. Due to the spell cast in turn 1, the monster's health reduces by 2 and becomes 18.
  • Cast spell 2 in turn 2. Due to the spells cast in turns 1 and 2, the monster's health reduces by 2+1=3 and becomes 15.
  • Cast spell 1 in turn 3. Due to the spells cast in turns 2 and 3, the monster's health reduces by 1+2=3 and becomes 12.
  • Cast spell 2 in turn 4. Due to the spells cast in turns 2,3 and 4, the monster's health reduces by 1+2+1=4 and becomes 8.
  • Cast spell 1 in turn 5. Due to the spells cast in turns 2,4 and 5, the monster's health reduces by 1+1+2=4 and becomes 4.
  • Cast spell 2 in turn 6. Due to the spells cast in turns 2,4,5 and 6, the monster's health reduces by 1+1+2+1=5 and becomes -1.

Sample Input 2

10 200
1 21
1 1
1 1
8 4
30 1
3 1
10 2
8 1
9 1
4 4

Sample Output 2

9
G - Bags Game

Time Limit: 2.5 sec / Memory Limit: 1024 MB

配点 : 550

問題文

N 個の袋が左右一列に並んでいて、左から i 番目の袋には x_i 円が入っています。

十分多くのお金を持っている高橋君と青木君が、高橋君を先手として交互に次の操作をします。

  • 以下の 3 種類の操作のうち 1 つを選んで行う。
    • 右端または左端の袋を 1 個選んで取る。
    • A 円をすぬけ君に支払う。そして、「右端または左端の袋を 1 個選んで取る」という操作を \min(B,n) (n は残っている袋の個数) 回繰り返す。
    • C 円をすぬけ君に支払う。そして、「右端または左端の袋を 1 個選んで取る」という操作を \min(D,n) (n は残っている袋の個数) 回繰り返す。

残っている袋が無くなった時点での高橋君の利得を「(高橋君が取った袋に入っている金額の総和) - (高橋君がすぬけ君に支払った金額の総和)」とし、これを X 円とします。また、青木君の利得についても同様に定め、Y 円とします。

高橋君が X-Y を最大化、青木君が X-Y を最小化することを目的に最適な操作をしたときの X-Y を求めてください。

制約

  • 1 \leq N \leq 3000
  • 1 \leq x_i \leq 10^9
  • 1 \leq A,C \leq 10^9
  • 1 \leq B,D \leq N
  • 入力はすべて整数

入力

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

N A B C D
x_1 x_2 \ldots x_N

出力

答えを出力せよ。


入力例 1

5 10 2 1000000000 1
1 100 1 1 1

出力例 1

90

高橋君と青木君が最適な操作をしたとき、X=92, Y=2 となります。


入力例 2

10 45 3 55 4
5 10 15 20 25 30 35 40 45 50

出力例 2

85

入力例 3

15 796265 10 165794055 1
18804175 185937909 1934689 18341 68370722 1653 1 2514380 31381214 905 754483 11 5877098 232 31600

出力例 3

302361955

Score : 550 points

Problem Statement

N bags are arranged in a row. The i-th bag contains x_i yen (the currency in Japan).

Takahashi and Aoki, who have sufficient money, take alternating turns to perform the following action:

  • choose one of the following three actions and perform it.
    • choose the leftmost or rightmost bag and take it.
    • pay A yen to Snuke. Then, repeat the following action \min(B,n) times (where n is the number of the remaining bags): choose the leftmost or rightmost bag and take it.
    • pay C yen to Snuke. Then, repeat the following action \min(D,n) times (where n is the number of the remaining bags): choose the leftmost or rightmost bag and take it.

When all the bags are taken, Takahashi's benefit is defined by "(total amount of money in the bags that Takahashi took) - (total amount of money that Takahashi paid to snuke)"; let this amount be X yen. We similarly define Aoki's benefit, denoting the amount by Y yen.

Find X-Y if Takahashi and Aoki make optimal moves to respectively maximize and minimize X-Y.

Constraints

  • 1 \leq N \leq 3000
  • 1 \leq x_i \leq 10^9
  • 1 \leq A,C \leq 10^9
  • 1 \leq B,D \leq N
  • All values in the input are integers.

Input

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

N A B C D
x_1 x_2 \ldots x_N

Output

Print the answer.


Sample Input 1

5 10 2 1000000000 1
1 100 1 1 1

Sample Output 1

90

If Takahashi and Aoki make optimal moves, it ends up being X=92 and Y=2.


Sample Input 2

10 45 3 55 4
5 10 15 20 25 30 35 40 45 50

Sample Output 2

85

Sample Input 3

15 796265 10 165794055 1
18804175 185937909 1934689 18341 68370722 1653 1 2514380 31381214 905 754483 11 5877098 232 31600

Sample Output 3

302361955
Ex - Constrained Tree Degree

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 600

問題文

整数 N 及び 1 以上 N-1 以下の整数からなる集合 S=\lbrace S_1,S_2,\ldots,S_K\rbrace が与えられます。

頂点に 1 から N の番号がついた N 頂点の木 T のうち、以下の条件を満たすものの個数を 998244353 で割った余りを答えてください。

  • 任意の i\ (1\leq i \leq N) について、T の頂点 i の次数を d_i としたとき、 d_i\in S

制約

  • 2\leq N \leq 2\times 10^5
  • 1\leq K \leq N-1
  • 1\leq S_1 < S_2 < \ldots < S_K \leq N-1
  • 入力は全て整数

入力

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

N K
S_1 \ldots S_K

出力

条件を満たす木 T の個数を 998244353 で割った余りを出力せよ。


入力例 1

4 2
1 3

出力例 1

4

ある 1 つの頂点の次数が 3 であり、ほかの頂点の次数が 1 であるような木が条件を満たします。よって答えは 4 個です。


入力例 2

10 5
1 2 3 5 6

出力例 2

68521950

入力例 3

100 5
1 2 3 14 15

出力例 3

888770956

個数を 998244353 で割った余りを出力してください。

Score : 600 points

Problem Statement

You are given an integer N and a set S=\lbrace S_1,S_2,\ldots,S_K\rbrace consisting of integers between 1 and N-1.

Find the number, modulo 998244353, of trees T with N vertices numbered 1 through N such that:

  • d_i\in S for all i\ (1\leq i \leq N), where d_i is the degree of vertex i in T.

Constraints

  • 2\leq N \leq 2\times 10^5
  • 1\leq K \leq N-1
  • 1\leq S_1 < S_2 < \ldots < S_K \leq N-1
  • All values in the input are integers.

Input

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

N K
S_1 \ldots S_K

Output

Print the number, modulo 998244353, of the conforming trees T.


Sample Input 1

4 2
1 3

Sample Output 1

4

A tree satisfies the condition if the degree of one vertex is 3 and the others' are 1. Thus, the answer is 4.


Sample Input 2

10 5
1 2 3 5 6

Sample Output 2

68521950

Sample Input 3

100 5
1 2 3 14 15

Sample Output 3

888770956

Print the count modulo 998244353.