A - Consecutive Rising Temperatures

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233

問題文

高橋君は気象観測が趣味で、毎日の最高気温を記録しています。彼は N 日間にわたって気温を記録しました。

i 日目(1 \leq i \leq N)に記録された最高気温は A_i ℃です。

高橋君は、連続する日の気温が厳密に増加している期間に注目しています。具体的には、1 \leq l かつ l + k - 1 \leq N を満たす整数 l, k について、l 日目から l+k-1 日目までの k 日間の気温が

A_l < A_{l+1} < \cdots < A_{l+k-1}

を満たすとき、この k 日間を長さ k上昇期間と呼びます。k=1 のとき条件は自明に満たされるため、任意の 1 日は長さ 1 の上昇期間です。

高橋君は、記録した N 日間のデータの中で最も長い上昇期間の長さ(日数)を知りたいと思っています。その最大値を求めてください。

制約

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

入力

N
A_1 A_2 \ldots A_N
  • 1 行目には、記録した日数を表す整数 N が与えられる。
  • 2 行目には、各日の最高気温を表す N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。

出力

最も長い上昇期間の長さ(日数)を 1 行で出力せよ。


入力例 1

7
20 22 21 23 25 27 24

出力例 1

4

入力例 2

12
15 18 20 19 17 18 19 20 21 22 20 21

出力例 2

6

入力例 3

20
-5 -3 0 2 5 8 12 15 14 13 10 11 12 13 14 15 16 17 18 20

出力例 3

10

Score : 233 pts

Problem Statement

Takahashi has a hobby of weather observation and records the daily high temperature. He has recorded temperatures over N days.

The highest temperature recorded on day i (1 \leq i \leq N) is A_i ℃.

Takahashi is interested in periods where the temperatures on consecutive days are strictly increasing. Specifically, for integers l, k satisfying 1 \leq l and l + k - 1 \leq N, if the temperatures over the k days from day l to day l+k-1 satisfy

A_l < A_{l+1} < \cdots < A_{l+k-1}

then these k days are called a rising period of length k. When k=1, the condition is trivially satisfied, so any single day is a rising period of length 1.

Takahashi wants to know the length (in days) of the longest rising period in his N days of recorded data. Find this maximum value.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • -40 \leq A_i \leq 45
  • All inputs are integers

Input

N
A_1 A_2 \ldots A_N
  • The first line contains an integer N representing the number of recorded days.
  • The second line contains N integers A_1, A_2, \ldots, A_N separated by spaces, representing the highest temperature on each day.

Output

Print the length (in days) of the longest rising period in a single line.


Sample Input 1

7
20 22 21 23 25 27 24

Sample Output 1

4

Sample Input 2

12
15 18 20 19 17 18 19 20 21 22 20 21

Sample Output 2

6

Sample Input 3

20
-5 -3 0 2 5 8 12 15 14 13 10 11 12 13 14 15 16 17 18 20

Sample Output 3

10
B - Circular Card Rotation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

N 人の子供たちが円形に並んで座っています。子供たちには 1 から N までの番号が付けられており、時計回りに子供 1, 子供 2, \ldots, 子供 N の順に並んでいます(子供 N の時計回りの隣は子供 1 です)。最初、子供 i は番号 i が書かれたカードを 1 枚持っています。

次の操作を K 回繰り返します。

  • 全員が同時に、自分の持っているカードを時計回りの隣の子供に渡す。すなわち、子供 i1 \leq i \leq N-1)は子供 i+1 に、子供 N は子供 1 に、それぞれ自分のカードを渡す。

各操作において全員がちょうど 1 枚渡してちょうど 1 枚受け取るため、操作後も各子供はちょうど 1 枚のカードを持ちます。

K 回の操作後、各子供が持っているカードに書かれた番号を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq 10^{18}
  • 入力はすべて整数である

入力

入力は以下の形式で与えられます。

N K

子供の人数 N と操作の回数 K がスペース区切りで与えられます。

出力

操作を K 回行った後の、子供 i1 \leq i \leq N)が持っているカードに書かれた番号を、子供 1 から子供 N の順に、1 行に 1 つずつ出力してください。


入力例 1

5 2

出力例 1

4
5
1
2
3

入力例 2

6 0

出力例 2

1
2
3
4
5
6

入力例 3

12 25

出力例 3

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

入力例 4

1 1000000000000000000

出力例 4

1

Score : 300 pts

Problem Statement

N children are sitting in a circle. The children are numbered from 1 to N, and they are arranged in clockwise order as child 1, child 2, \ldots, child N (the clockwise neighbor of child N is child 1). Initially, child i holds exactly one card with the number i written on it.

The following operation is repeated K times:

  • Everyone simultaneously passes their card to the child next to them in the clockwise direction. That is, child i (1 \leq i \leq N-1) passes their card to child i+1, and child N passes their card to child 1.

In each operation, everyone passes exactly one card and receives exactly one card, so after the operation each child still holds exactly one card.

After K operations, determine the number written on the card that each child is holding.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq 10^{18}
  • All input values are integers

Input

The input is given in the following format:

N K

The number of children N and the number of operations K are given separated by a space.

Output

After performing the operation K times, output the number written on the card held by child i (1 \leq i \leq N), one per line, in order from child 1 to child N.


Sample Input 1

5 2

Sample Output 1

4
5
1
2
3

Sample Input 2

6 0

Sample Output 2

1
2
3
4
5
6

Sample Input 3

12 25

Sample Output 3

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

Sample Input 4

1 1000000000000000000

Sample Output 4

1
C - Shopping Challenge

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366

問題文

高橋君はスーパーマーケットで買い物をしています。店内には N 個の商品が並んでおり、i 番目の商品(1 \leq i \leq N)には満足度 V_i と価格 C_i 円が定められています。各商品は最大 1 個まで購入できます。

高橋君の手持ちの金額は S 円です。高橋君は、この S 円をちょうど使い切りたいと考えています。

N 個の商品の中から 1 個以上を選んで購入することを考えます。選んだ商品の価格の合計がちょうど S 円となる選び方が存在するとき、そのような選び方すべての中での満足度の合計の最大値を求めてください。

価格の合計がちょうど S 円となる選び方が存在しない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 3000
  • 1 \leq S \leq 10000
  • 1 \leq V_i \leq 10000 (1 \leq i \leq N)
  • 1 \leq C_i \leq 10000 (1 \leq i \leq N)
  • 入力はすべて整数である

入力

N S
V_1 C_1
V_2 C_2
\vdots
V_N C_N
  • 1 行目には、商品の個数を表す整数 N と、手持ちの金額を表す整数 S が、スペース区切りで与えられる。
  • 続く N 行のうち i 行目(1 \leq i \leq N)には、i 番目の商品の満足度を表す整数 V_i と価格を表す整数 C_i が、スペース区切りで与えられる。

出力

価格の合計がちょうど S 円となるように商品を 1 個以上選ぶ方法が存在する場合、選んだ商品の満足度の合計の最大値を 1 行で出力してください。そのような選び方が存在しない場合は -11 行で出力してください。


入力例 1

3 5
3 2
4 3
1 4

出力例 1

7

入力例 2

3 10
1 3
2 3
3 3

出力例 2

-1

入力例 3

8 20
10 5
8 7
6 3
12 8
3 4
7 6
9 10
5 2

出力例 3

31

入力例 4

15 50
20 8
15 12
30 10
25 15
10 5
18 7
12 9
22 14
8 3
35 20
14 6
9 11
27 13
19 16
11 4

出力例 4

124

入力例 5

1 7
5 7

出力例 5

5

Score : 366 pts

Problem Statement

Takahashi is shopping at a supermarket. There are N items in the store, and the i-th item (1 \leq i \leq N) has a satisfaction value of V_i and a price of C_i yen. Each item can be purchased at most once.

Takahashi has S yen on hand. He wants to spend exactly S yen.

Consider selecting and purchasing 1 or more items from the N items. If there exists a way to select items such that the total price is exactly S yen, find the maximum total satisfaction among all such selections.

If no selection of items has a total price of exactly S yen, output -1.

Constraints

  • 1 \leq N \leq 3000
  • 1 \leq S \leq 10000
  • 1 \leq V_i \leq 10000 (1 \leq i \leq N)
  • 1 \leq C_i \leq 10000 (1 \leq i \leq N)
  • All inputs are integers

Input

N S
V_1 C_1
V_2 C_2
\vdots
V_N C_N
  • The first line contains an integer N representing the number of items and an integer S representing the amount of money on hand, separated by a space.
  • The i-th line (1 \leq i \leq N) of the following N lines contains an integer V_i representing the satisfaction value and an integer C_i representing the price of the i-th item, separated by a space.

Output

If there exists a way to select 1 or more items such that the total price is exactly S yen, output the maximum total satisfaction of the selected items on a single line. If no such selection exists, output -1 on a single line.


Sample Input 1

3 5
3 2
4 3
1 4

Sample Output 1

7

Sample Input 2

3 10
1 3
2 3
3 3

Sample Output 2

-1

Sample Input 3

8 20
10 5
8 7
6 3
12 8
3 4
7 6
9 10
5 2

Sample Output 3

31

Sample Input 4

15 50
20 8
15 12
30 10
25 15
10 5
18 7
12 9
22 14
8 3
35 20
14 6
9 11
27 13
19 16
11 4

Sample Output 4

124

Sample Input 5

1 7
5 7

Sample Output 5

5
D - Virus Testing and Infected Terminals

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君は社内ネットワークの管理者です。ネットワークには 1 から N までの番号が付けられた N 台の端末が接続されています。各端末は「感染している」か「感染していない」かのいずれかの状態にありますが、高橋君はどの端末が感染しているか分かっていません。

高橋君は M 回のスキャンを実行しました。各スキャン j1 \leq j \leq M)は、指定された K_j 台の端末の集合 \{S_{j,1}, S_{j,2}, \ldots, S_{j,K_j}\} を一度に検査し、判定結果 R_j を返します。

  • R_j = 1(「感染あり」):集合の中に感染している端末が 少なくとも 1含まれている。
  • R_j = 0(「感染なし」):集合の中に感染している端末が 1 台も含まれていない。

なお、異なるスキャンの検査対象の集合が重複していたり、同一であったりすることもあります。

ここで、各端末に「感染している」「感染していない」のいずれかを割り当てる方法を 感染状況の割り当て と呼びます。ある割り当てが すべてのスキャン結果と矛盾しない とは、すべてのスキャン j1 \leq j \leq M)について次の条件を満たすことを意味します。

  • R_j = 1 のとき:集合 \{S_{j,1}, S_{j,2}, \ldots, S_{j,K_j}\} の中に、感染していると割り当てられた端末が少なくとも 1 台存在する。
  • R_j = 0 のとき:集合 \{S_{j,1}, S_{j,2}, \ldots, S_{j,K_j}\} の中に、感染していると割り当てられた端末が 1 台も存在しない。

すべてのスキャン結果と矛盾しない割り当てが少なくとも 1 つ存在することが保証されます。そのような割り当ての中で、感染している端末の台数の最小値を求めてください。

制約

  • 1 \leq N \leq 16
  • 1 \leq M \leq 100
  • 1 \leq K_j \leq N1 \leq j \leq M
  • 1 \leq S_{j,k} \leq N1 \leq j \leq M, 1 \leq k \leq K_j
  • 各スキャン j において、S_{j,1}, S_{j,2}, \ldots, S_{j,K_j} はすべて異なる。
  • R_j \in \{0, 1\}1 \leq j \leq M
  • すべてのスキャン結果と矛盾しない感染状況の割り当てが少なくとも 1 つ存在する。
  • 入力はすべて整数で与えられる。

入力

N M
K_1 S_{1,1} S_{1,2} \ldots S_{1,K_1} R_1
K_2 S_{2,1} S_{2,2} \ldots S_{2,K_2} R_2
\vdots
K_M S_{M,1} S_{M,2} \ldots S_{M,K_M} R_M
  • 1 行目には、端末の台数 N とスキャンの回数 M がスペース区切りで与えられる。
  • 続く M 行のうち j 行目(1 \leq j \leq M)には、スキャン j の情報として、検査する端末の台数 K_j、検査対象の端末の番号 S_{j,1}, S_{j,2}, \ldots, S_{j,K_j}、および判定結果 R_j がこの順にスペース区切りで与えられる。

出力

すべてのスキャン結果と矛盾しない感染状況の割り当てにおける、感染している端末の台数の最小値を 1 行で出力せよ。


入力例 1

3 2
2 1 2 1
2 2 3 0

出力例 1

1

入力例 2

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

出力例 2

2

入力例 3

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

出力例 3

2

Score : 400 pts

Problem Statement

Takahashi is the administrator of a corporate network. The network has N terminals connected to it, numbered from 1 to N. Each terminal is in one of two states: "infected" or "not infected," but Takahashi does not know which terminals are infected.

Takahashi performed M scans. Each scan j (1 \leq j \leq M) examines a specified set of K_j terminals \{S_{j,1}, S_{j,2}, \ldots, S_{j,K_j}\} at once and returns a result R_j.

  • R_j = 1 ("infection detected"): The set contains at least one infected terminal.
  • R_j = 0 ("no infection"): The set contains no infected terminals.

Note that the sets of terminals examined by different scans may overlap or even be identical.

Here, a method of assigning either "infected" or "not infected" to each terminal is called an infection status assignment. An assignment is said to be consistent with all scan results if, for every scan j (1 \leq j \leq M), the following conditions are satisfied:

  • When R_j = 1: Among the set \{S_{j,1}, S_{j,2}, \ldots, S_{j,K_j}\}, there exists at least one terminal assigned as infected.
  • When R_j = 0: Among the set \{S_{j,1}, S_{j,2}, \ldots, S_{j,K_j}\}, there are no terminals assigned as infected.

It is guaranteed that at least one assignment consistent with all scan results exists. Among all such assignments, find the minimum number of infected terminals.

Constraints

  • 1 \leq N \leq 16
  • 1 \leq M \leq 100
  • 1 \leq K_j \leq N (1 \leq j \leq M)
  • 1 \leq S_{j,k} \leq N (1 \leq j \leq M, 1 \leq k \leq K_j)
  • For each scan j, the values S_{j,1}, S_{j,2}, \ldots, S_{j,K_j} are all distinct.
  • R_j \in \{0, 1\} (1 \leq j \leq M)
  • There exists at least one infection status assignment consistent with all scan results.
  • All input values are integers.

Input

N M
K_1 S_{1,1} S_{1,2} \ldots S_{1,K_1} R_1
K_2 S_{2,1} S_{2,2} \ldots S_{2,K_2} R_2
\vdots
K_M S_{M,1} S_{M,2} \ldots S_{M,K_M} R_M
  • The first line contains the number of terminals N and the number of scans M, separated by a space.
  • Each of the following M lines, the j-th line (1 \leq j \leq M), contains the information for scan j: the number of terminals to examine K_j, the terminal numbers S_{j,1}, S_{j,2}, \ldots, S_{j,K_j}, and the result R_j, in this order, separated by spaces.

Output

Print in one line the minimum number of infected terminals among all infection status assignments consistent with all scan results.


Sample Input 1

3 2
2 1 2 1
2 2 3 0

Sample Output 1

1

Sample Input 2

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

Sample Output 2

2

Sample Input 3

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

Sample Output 3

2
E - The Adventurer's Journey

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 433

問題文

高橋君は冒険者です。彼は N 個の町を含む大陸を旅しています。

N 個の町には 1 から N までの番号がついており、町同士を結ぶ M 本の道があります。i 番目の道は町 U_i と町 V_i を双方向に結んでおり、この道を通るには体力を W_i 消費します。

高橋君の体力の初期値は F です。高橋君は町 1 を出発し、町 N にたどり着きたいと考えています。

各町 j には宿屋があり、回復量 R_j が定められています。高橋君がある町に初めて訪れたとき、その町の宿屋によって体力が R_j だけ回復します。この回復は各町につき最初の 1 回のみ適用され、同じ町を 2 回以上訪れても 2 回目以降は回復しません。なお、体力に上限値は設けられていません(回復によっていくらでも大きくなり得ます)。

1 については、出発時に初めて訪れたものとして扱います。したがって、最初の移動を行う前の時点での高橋君の体力は F + R_1 です。

高橋君の移動は、以下の手順を繰り返すことで行われます。

  1. 高橋君は、現在いる町に接続する道を 1 つ選びます。ただし、その道の通行に必要な体力(コスト W_i)が現在の体力以下である道しか選ぶことができません。条件を満たす道が 1 つも存在しない場合、高橋君はそれ以上移動できません。
  2. 選んだ道を通り、体力が W_i だけ減少します。体力がちょうど 0 になっても構いません。
  3. 道の先の町に到着した直後、その町を初めて訪れた場合に限り、その町の宿屋の回復量 R_j が体力に加算されます。

高橋君は同じ道を何度でも通ることができ、同じ町を何度でも訪れることができます(ただし、宿屋の回復は前述の通り各町につき 1 回のみです)。

高橋君が町 1 から出発して町 N に到着できるすべての移動の仕方を考えたとき、町 N 到着時の体力の最大値を求めてください。ここで、町 N 到着時の体力とは、上記の手順 3 まで適用した後の体力を指します(町 N を初めて訪れた場合、町 N の宿屋の回復も適用されます)。

どのように移動しても町 N に到着できない場合は -1 を出力してください。

制約

  • 2 \leq N \leq 10
  • 1 \leq M \leq \frac{N(N-1)}{2}
  • 0 \leq F \leq 1000
  • 0 \leq R_j \leq 1000 (1 \leq j \leq N)
  • 1 \leq U_i < V_i \leq N (1 \leq i \leq M)
  • 1 \leq W_i \leq 1000 (1 \leq i \leq M)
  • 同じ町の組を結ぶ道は高々 1 本である
  • 入力はすべて整数である

入力

N M F
R_1 R_2 \ldots R_N
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
  • 1 行目には、町の数を表す N、道の数を表す M、初期体力を表す F が、スペース区切りで与えられる。
  • 2 行目には、各町の宿屋の回復量を表す R_1, R_2, \ldots, R_N が、スペース区切りで与えられる。
  • 3 行目から M 行にわたって、各道の情報が与えられる。
  • 2 + i 行目では、i 番目の道が結ぶ町 U_i, V_i と、その道を通るために消費する体力 W_i が、スペース区切りで与えられる。

出力

高橋君が町 1 から町 N に到着したときの体力の最大値を 1 行で出力してください。どのように移動しても町 N に到着することが不可能な場合は -1 を出力してください。


入力例 1

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

出力例 1

11

入力例 2

3 1 5
0 0 0
1 2 3

出力例 2

-1

入力例 3

5 6 10
5 100 3 2 8
1 2 12
1 3 5
2 3 8
2 5 10
3 4 3
4 5 3

出力例 3

103

入力例 4

8 10 50
10 20 30 40 50 15 25 100
1 2 30
1 3 40
1 6 35
2 3 25
3 4 20
3 8 80
4 5 15
5 8 10
6 7 20
7 8 30

出力例 4

200

入力例 5

2 1 0
1000 1000
1 2 1000

出力例 5

1000

Score : 433 pts

Problem Statement

Takahashi is an adventurer. He is traveling across a continent containing N towns.

The N towns are numbered from 1 to N, and there are M roads connecting the towns. The i-th road bidirectionally connects town U_i and town V_i, and traveling along this road costs W_i stamina.

Takahashi's initial stamina is F. He starts at town 1 and wants to reach town N.

Each town j has an inn with a recovery value R_j. When Takahashi visits a town for the first time, the inn in that town restores R_j stamina. This recovery is applied only once per town — if he visits the same town two or more times, no recovery occurs from the second visit onward. Note that there is no upper limit on stamina (it can grow arbitrarily large through recovery).

Town 1 is treated as being visited for the first time at the moment of departure. Therefore, Takahashi's stamina before making the first move is F + R_1.

Takahashi's movement is performed by repeating the following steps:

  1. Takahashi chooses one road connected to the town he is currently in. However, he can only choose a road whose traversal cost (W_i) is less than or equal to his current stamina. If no road satisfies this condition, Takahashi cannot move any further.
  2. He travels along the chosen road, and his stamina decreases by W_i. It is acceptable for his stamina to become exactly 0.
  3. Immediately after arriving at the destination town, if this is his first visit to that town, the inn's recovery value R_j is added to his stamina.

Takahashi may travel along the same road any number of times and may visit the same town any number of times (however, inn recovery is applied only once per town as described above).

Considering all possible ways Takahashi can travel from town 1 to town N, find the maximum stamina upon arrival at town N. Here, the stamina upon arrival at town N refers to the stamina after step 3 above has been applied (if town N is visited for the first time, the inn recovery at town N is also applied).

If it is impossible to reach town N regardless of how he moves, output -1.

Constraints

  • 2 \leq N \leq 10
  • 1 \leq M \leq \frac{N(N-1)}{2}
  • 0 \leq F \leq 1000
  • 0 \leq R_j \leq 1000 (1 \leq j \leq N)
  • 1 \leq U_i < V_i \leq N (1 \leq i \leq M)
  • 1 \leq W_i \leq 1000 (1 \leq i \leq M)
  • There is at most one road connecting any pair of towns
  • All input values are integers

Input

N M F
R_1 R_2 \ldots R_N
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
  • The first line contains N representing the number of towns, M representing the number of roads, and F representing the initial stamina, separated by spaces.
  • The second line contains R_1, R_2, \ldots, R_N representing the inn recovery values for each town, separated by spaces.
  • The following M lines provide information about each road.
  • The (2 + i)-th line contains the towns U_i, V_i connected by the i-th road and the stamina cost W_i for traveling along that road, separated by spaces.

Output

Output in one line the maximum stamina when Takahashi arrives at town N from town 1. If it is impossible to reach town N regardless of how he moves, output -1.


Sample Input 1

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

Sample Output 1

11

Sample Input 2

3 1 5
0 0 0
1 2 3

Sample Output 2

-1

Sample Input 3

5 6 10
5 100 3 2 8
1 2 12
1 3 5
2 3 8
2 5 10
3 4 3
4 5 3

Sample Output 3

103

Sample Input 4

8 10 50
10 20 30 40 50 15 25 100
1 2 30
1 3 40
1 6 35
2 3 25
3 4 20
3 8 80
4 5 15
5 8 10
6 7 20
7 8 30

Sample Output 4

200

Sample Input 5

2 1 0
1000 1000
1 2 1000

Sample Output 5

1000