A - Saturday

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

ある日、学校へ行くのに疲れてしまった高橋くんは、土曜日まであと何日あるかを知りたくなりました。
その日は平日で、曜日を英語で表すと S だったことが分かっています。その日より後の直近の土曜日は何日後かを求めてください。

なお、月曜日、火曜日、水曜日、木曜日、金曜日はそれぞれ英語で Monday, Tuesday, Wednesday, Thursday, Friday です。

制約

  • SMonday, Tuesday, Wednesday, Thursday, Friday のいずれかである

入力

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

S

出力

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


入力例 1

Wednesday

出力例 1

3

この日は水曜日なので、3 日後に土曜日になります。


入力例 2

Monday

出力例 2

5

Score : 100 points

Problem Statement

One day, tired from going to school, Takahashi wanted to know how many days there were until Saturday.
We know that the day was a weekday, and the name of the day of the week was S in English.
How many days were there until the first Saturday after that day (including Saturday but not the starting day)?

Constraints

  • S is Monday, Tuesday, Wednesday, Thursday, or Friday.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer as an integer.


Sample Input 1

Wednesday

Sample Output 1

3

It was Wednesday, so there were 3 days until the first Saturday after that day.


Sample Input 2

Monday

Sample Output 2

5
B - Split?

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

ボウリングのピンは 1 から 10 の番号が付けられており、上から見ると下図のように配置されます。

0

この図の二つの点線に挟まれた部分をと呼ぶことにします。
例えば、ピン 1, 5 とピン 3, 9 はそれぞれ同じ列に存在します。

いくつかのピンが倒れた状態のうち、特殊なものはスプリットと呼ばれます。
ピンの配置がスプリットであるとは、以下の条件が全て成り立つことを言います。

  • ピン 1 が倒れている。
  • ある二つの異なる列であって、次の条件を満たすものが存在する。
    • それぞれの列には、立っているピンが 1 本以上存在する。
    • それらの列の間に、ピンが全て倒れている列が存在する。

具体例は入出力例を参考にしてください。

さて、あるピンの配置が長さ 10 の文字列 S として与えられます。 i = 1, \dots, 10 について、ピン i が倒れているとき Si 文字目は 0 であり、ピン i が立っているとき Si 文字目は 1 です。
S で表されるピンの配置がスプリットかどうか判定してください。

制約

  • S01 からなる長さ 10 の文字列

入力

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

S

出力

S で表されるピンの配置がスプリットなら Yes を、そうでないなら No を出力せよ。


入力例 1

0101110101

出力例 1

Yes

倒れているピンを灰色で、立っているピンを白色で示すと下図のようになります。

ex0

ピン 5 が立っている列とピン 6 が立っている列の間にはピン 3, 9 が置かれている列が存在しますが、ピン 3, 9 はいずれも倒れているので、この配置はスプリットです。


入力例 2

0100101001

出力例 2

Yes

ex1


入力例 3

0000100110

出力例 3

No

ex2

この配置はスプリットではありません。


入力例 4

1101110101

出力例 4

No

ex3

ピン 1 が倒れていないので、スプリットではありません。

Score : 200 points

Problem Statement

Bowling pins are numbered 1 through 10. The following figure is a top view of the arrangement of the pins:

0

Let us call each part between two dotted lines in the figure a column.
For example, Pins 1 and 5 belong to the same column, and so do Pin 3 and 9.

When some of the pins are knocked down, a special situation called split may occur.
A placement of the pins is a split if both of the following conditions are satisfied:

  • Pin 1 is knocked down.
  • There are two different columns that satisfy both of the following conditions:
    • Each of the columns has one or more standing pins.
    • There exists a column between these columns such that all pins in the column are knocked down.

See also Sample Inputs and Outputs for examples.

Now, you are given a placement of the pins as a string S of length 10. For i = 1, \dots, 10, the i-th character of S is 0 if Pin i is knocked down, and is 1 if it is standing.
Determine if the placement of the pins represented by S is a split.

Constraints

  • S is a string of length 10 consisting of 0 and 1.

Input

Input is given from Standard Input in the following format:

S

Output

If the placement of the pins represented by S is a split, print Yes; otherwise, print No.


Sample Input 1

0101110101

Sample Output 1

Yes

In the figure below, the knocked-down pins are painted gray, and the standing pins are painted white:

ex0

Between the column containing a standing pin 5 and the column containing a standing pin 6 is a column containing Pins 3 and 9. Since Pins 3 and 9 are both knocked down, the placement is a split.


Sample Input 2

0100101001

Sample Output 2

Yes

ex1


Sample Input 3

0000100110

Sample Output 3

No

ex2

This placement is not a split.


Sample Input 4

1101110101

Sample Output 4

No

ex3

This is not a split because Pin 1 is not knocked down.

C - Index × A(Continuous ver.)

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。

長さ MA の連続部分列 B=(B_1,B_2,\dots,B_M) に対する、\displaystyle \sum_{i=1}^{M} i \times B_i の最大値を求めてください。

注記

数列の連続部分列とは、数列の先頭から 0 個以上、末尾から 0 個以上の要素を削除して得られる列のことをいいます。

例えば (2, 3)(1, 2, 3)(1, 2, 3, 4) の連続部分列ですが、(1, 3)(3,2,1)(1, 2, 3, 4) の連続部分列ではありません。

制約

  • 1 \le M \le N \le 2 \times 10^5
  • - 2 \times 10^5 \le A_i \le 2 \times 10^5
  • 入力は全て整数。

入力

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

N M
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

4 2
5 4 -1 8

出力例 1

15

B=(A_3,A_4) とした場合、\displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times (-1) + 2 \times 8 = 15 となります。16 以上の値を達成することはできないため、解は 15 です。

B=(A_1,A_4) などを選ぶことができないことに注意してください。


入力例 2

10 4
-3 1 -4 1 -5 9 -2 6 -5 3

出力例 2

31

Score : 300 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\dots,A_N) of length N.

Find the maximum value of \displaystyle \sum_{i=1}^{M} i \times B_i for a contiguous subarray B=(B_1,B_2,\dots,B_M) of A of length M.

Notes

A contiguous subarray of a number sequence is a sequence that is obtained by removing 0 or more initial terms and 0 or more final terms from the original number sequence.

For example, (2, 3) and (1, 2, 3) are contiguous subarrays of (1, 2, 3, 4), but (1, 3) and (3,2,1) are not contiguous subarrays of (1, 2, 3, 4).

Constraints

  • 1 \le M \le N \le 2 \times 10^5
  • - 2 \times 10^5 \le A_i \le 2 \times 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

4 2
5 4 -1 8

Sample Output 1

15

When B=(A_3,A_4), we have \displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times (-1) + 2 \times 8 = 15. Since it is impossible to achieve 16 or a larger value, the solution is 15.

Note that you are not allowed to choose, for instance, B=(A_1,A_4).


Sample Input 2

10 4
-3 1 -4 1 -5 9 -2 6 -5 3

Sample Output 2

31
D - Index × A(Not Continuous ver.)

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。

長さ MA の部分列(連続でなくてもよい) B=(B_1,B_2,\dots,B_M) に対する、\displaystyle \sum_{i=1}^{M} i \times B_i の最大値を求めてください。

注記

数列の部分列とは、数列から 0 個以上の要素を取り除いた後、残りの要素を元の順序で連結して得られる数列のことをいいます。

例えば、(10,30)(10,20,30) の部分列ですが、(20,10)(10,20,30) の部分列ではありません。

制約

  • 1 \le M \le N \le 2000
  • - 2 \times 10^5 \le A_i \le 2 \times 10^5
  • 入力は全て整数。

入力

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

N M
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

4 2
5 4 -1 8

出力例 1

21

B=(A_1,A_4) とした場合、\displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times 5 + 2 \times 8 = 21 となります。22 以上の値を達成することはできないため、解は 21 です。


入力例 2

10 4
-3 1 -4 1 -5 9 -2 6 -5 3

出力例 2

54

Score : 400 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\dots,A_N) of length N.

Find the maximum value of \displaystyle \sum_{i=1}^{M} i \times B_i for a (not necessarily contiguous) subsequence B=(B_1,B_2,\dots,B_M) of length M of A.

Notes

A subsequence of a number sequence is a sequence that is obtained by removing 0 or more elements from the original number sequence and concatenating the remaining elements without changing the order.

For example, (10,30) is a subsequence of (10,20,30), but (20,10) is not a subsequence of (10,20,30).

Constraints

  • 1 \le M \le N \le 2000
  • - 2 \times 10^5 \le A_i \le 2 \times 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

4 2
5 4 -1 8

Sample Output 1

21

When B=(A_1,A_4), we have \displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times 5 + 2 \times 8 = 21. Since it is impossible to achieve 22 or a larger value, the solution is 21.


Sample Input 2

10 4
-3 1 -4 1 -5 9 -2 6 -5 3

Sample Output 2

54
E - Erasing Vertices 2

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点 M 辺の単純無向グラフ(すなわち、自己辺も多重辺もない無向グラフ)が与えられます。i 本目の辺は頂点 U_i と頂点 V_i を結んでいます。頂点 i には正整数 A_i が書かれています。

あなたは、以下の操作を N 回繰り返します。

  • まだ削除されていない頂点 x を選び、「頂点 x 」と「頂点 x を端点に持つ辺全て」を削除する。この操作のコストは、頂点 x と辺で直接結ばれていて、かつまだ削除されていない頂点に書かれている整数の総和である。

N 回の操作全体のコストを、1 回ごとの操作におけるコストのうちの最大値として定めます。操作全体のコストとして取り得る値の最小値を求めてください。

制約

  • 1 \le N \le 2 \times 10^5
  • 0 \le M \le 2 \times 10^5
  • 1 \le A_i \le 10^9
  • 1 \le U_i,V_i \le N
  • 与えられるグラフは単純。
  • 入力は全て整数。

入力

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

N M
A_1 A_2 \dots A_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M

出力

答えを出力せよ。


入力例 1

4 3
3 1 4 2
1 2
1 3
4 1

出力例 1

3

以下のように操作を行うことにより、N 回の操作のコストのうちの最大値を 3 にすることができます。

  • 頂点 3 を選ぶ。コストは A_1=3 である。
  • 頂点 1 を選ぶ。コストは A_2+A_4=3 である。
  • 頂点 2 を選ぶ。コストは 0 である。
  • 頂点 4 を選ぶ。コストは 0 である。

N 回の操作のコストのうちの最大値を 2 以下にすることはできないため、解は 3 です。


入力例 2

7 13
464 661 847 514 74 200 188
5 1
7 1
5 7
4 1
4 5
2 4
5 2
1 3
1 6
3 5
1 2
4 6
2 7

出力例 2

1199

Score : 500 points

Problem Statement

You are given a simple undirected graph with N vertices and M edges. The i-th edge connects Vertices U_i and V_i. Vertex i has a positive integer A_i written on it.

You will repeat the following operation N times.

  • Choose a Vertex x that is not removed yet, and remove Vertex x and all edges incident to Vertex x. The cost of this operation is the sum of the integers written on the vertices directly connected by an edge with Vertex x that are not removed yet.

We define the cost of the entire N operations as the maximum of the costs of the individual operations. Find the minimum possible cost of the entire operations.

Constraints

  • 1 \le N \le 2 \times 10^5
  • 0 \le M \le 2 \times 10^5
  • 1 \le A_i \le 10^9
  • 1 \le U_i,V_i \le N
  • The given graph is simple.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \dots A_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M

Output

Print the answer.


Sample Input 1

4 3
3 1 4 2
1 2
1 3
4 1

Sample Output 1

3

By performing the operations as follows, the maximum of the costs of the N operations can be 3.

  • Choose Vertex 3. The cost is A_1=3.
  • Choose Vertex 1. The cost is A_2+A_4=3.
  • Choose Vertex 2. The cost is 0.
  • Choose Vertex 4. The cost is 0.

The maximum of the costs of the N operations cannot be 2 or less, so the solution is 3.


Sample Input 2

7 13
464 661 847 514 74 200 188
5 1
7 1
5 7
4 1
4 5
2 4
5 2
1 3
1 6
3 5
1 2
4 6
2 7

Sample Output 2

1199
F - Exactly K Steps

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点の木が与えられます。頂点には 1, \dots, N の番号が付けられており、i \, (1 \leq i \leq N - 1) 番目の辺は頂点 A_i, B_i を結びます。

この木における頂点 u, v距離を、頂点 u から頂点 v までの最短パス上にある辺の本数と定義します。

Q 個のクエリが与えられます。i \, (1 \leq i \leq Q) 番目のクエリでは、整数 U_i, K_i が与えられるので、頂点 U_i からの距離がちょうど K_i であるような頂点の番号を任意に一つ出力してください。そのような頂点が存在しない場合は、-1 を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq N - 1)
  • 与えられるグラフは木
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq U_i, K_i \leq N \, (1 \leq i \leq Q)
  • 入力は全て整数

入力

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

N
A_1 B_1
\vdots
A_{N-1} B_{N-1}
Q
U_1 K_1
\vdots
U_Q K_Q

出力

Q 行出力せよ。i \, (1 \leq i \leq Q) 行目には、頂点 U_i からの距離がちょうど K_i である頂点が存在するならその一例を、存在しないなら -1 を出力せよ。そのような頂点が複数存在する場合、どれを出力しても正解となる。


入力例 1

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

出力例 1

4
1
-1
  • 頂点 2 からの距離がちょうど 2 であるのは頂点 4, 5 の二つです。
  • 頂点 5 からの距離がちょうど 3 であるのは頂点 1 のみです。
  • 頂点 3 からの距離がちょうど 3 である頂点は存在しません。

入力例 2

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

出力例 2

2
4
10
-1
-1

Score : 500 points

Problem Statement

You are given a tree with N vertices. The vertices are numbered 1, \dots, N, and the i-th (1 \leq i \leq N - 1) edge connects Vertices A_i and B_i.

We define the distance between Vertices u and v on this tree by the number of edges in the shortest path from Vertex u to Vertex v.

You are given Q queries. In the i-th (1 \leq i \leq Q) query, given integers U_i and K_i, print the index of any vertex whose distance from Vertex U_i is exactly K_i. If there is no such vertex, print -1.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq N - 1)
  • The given graph is a tree.
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq U_i, K_i \leq N \, (1 \leq i \leq Q)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
\vdots
A_{N-1} B_{N-1}
Q
U_1 K_1
\vdots
U_Q K_Q

Output

Print Q lines. The i-th (1 \leq i \leq Q) line should contain the index of any vertex whose distance from Vertex U_i is exactly K_i if such a vertex exists; if not, it should contain -1. If there are multiple such vertices, you may print any of them.


Sample Input 1

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

Sample Output 1

4
1
-1
  • Two vertices, Vertices 4 and 5, have a distance exactly 2 from Vertex 2.
  • Only Vertex 1 has a distance exactly 3 from Vertex 5.
  • No vertex has a distance exactly 3 from Vertex 3.

Sample Input 2

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

Sample Output 2

2
4
10
-1
-1
G - Increasing K Times

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

長さ N の整数列 A = (A_1, \dots, A_N) が与えられます。

(1, 2, \dots, N) を並べ替えて得られる列 P = (P_1, \dots, P_N) であって、次の条件を満たすものの総数を 998244353 で割った余りを求めてください。

  • A_{P_i} \lt A_{P_{i + 1}} となるような 1 以上 N-1 以下の整数 i がちょうど K 個存在する。

制約

  • 2 \leq N \leq 5000
  • 0 \leq K \leq N - 1
  • 1 \leq A_i \leq N \, (1 \leq i \leq N)
  • 入力は全て整数

入力

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

N K
A_1 \ldots A_N

出力

答えを出力せよ。


入力例 1

4 2
1 1 2 2

出力例 1

4

P = (1, 3, 2, 4), (1, 4, 2, 3), (2, 3, 1, 4), (2, 4, 1, 3)4 通りが条件を満たします。


入力例 2

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

出力例 2

697112

Score : 600 points

Problem Statement

You are given an integer sequence A = (A_1, \dots, A_N) of length N.

Find the number, modulo 998244353, of permutations P = (P_1, \dots, P_N) of (1, 2, \dots, N) such that:

  • there exist exactly K integers i between 1 and (N-1) (inclusive) such that A_{P_i} \lt A_{P_{i + 1}}.

Constraints

  • 2 \leq N \leq 5000
  • 0 \leq K \leq N - 1
  • 1 \leq A_i \leq N \, (1 \leq i \leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 \ldots A_N

Output

Print the answer.


Sample Input 1

4 2
1 1 2 2

Sample Output 1

4

Four permutations satisfy the condition: P = (1, 3, 2, 4), (1, 4, 2, 3), (2, 3, 1, 4), (2, 4, 1, 3).


Sample Input 2

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

Sample Output 2

697112
Ex - Odd Sum

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 600

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。

A から要素を奇数個選ぶ方法のうち、選んだ要素の総和が M になるものの個数を 998244353 で割ったあまりを求めてください。

ただし、2 つの選び方が異なるとは、ある整数 i (1 \le i \le N) が存在して、一方の選び方では A_i を選び、もう一方では選んでいないことを言います。

制約

  • 1 \le N \le 10^5
  • 1 \le M \le 10^6
  • 1 \le A_i \le 10
  • 入力は全て整数。

入力

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

N M
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

5 6
1 2 3 3 6

出力例 1

3

条件を満たす選び方は以下の 3 通りです。

  • A_1,A_2,A_3 を選ぶ。
  • A_1,A_2,A_4 を選ぶ。
  • A_5 を選ぶ。

A_3,A_4 を選んだ場合、総和は 6 ですが選んだ要素の個数が奇数個でないため条件を満たしません。


入力例 2

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

出力例 2

18

Score : 600 points

Problem Statement

You are given a sequence A=(A_1,A_2,\dots,A_N) of length N.

Find the number, modulo 998244353, of ways to choose an odd number of elements from A so that the sum of the chosen elements equals M.

Two choices are said to be different if there exists an integer i (1 \le i \le N) such that one chooses A_i but the other does not.

Constraints

  • 1 \le N \le 10^5
  • 1 \le M \le 10^6
  • 1 \le A_i \le 10
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

5 6
1 2 3 3 6

Sample Output 1

3

The following 3 choices satisfy the condition:

  • Choosing A_1, A_2, and A_3.
  • Choosing A_1, A_2, and A_4.
  • Choosing A_5.

Choosing A_3 and A_4 does not satisfy the condition because, although the sum is 6, the number of chosen elements is not odd.


Sample Input 2

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

Sample Output 2

18