A - Recounting the Sheep

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 166 点

問題文

高橋君は牧場で羊を飼っています。羊は 2 つの群れに分かれて放牧されており、朝の見回りで 1 つ目の群れには A 匹、 2 つ目の群れには B 匹いました。

夕方、高橋君はすべての羊を 1 か所に集めて数え直したところ、 C 匹いました。羊が 1 匹も増えたり減ったりしていないなら Yes を、そうでないなら No を出力してください。

制約

  • 1 \leq A \leq 1000
  • 1 \leq B \leq 1000
  • 1 \leq C \leq 2000
  • 入力はすべて整数

入力

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

A B C
  • 1 行目には、朝の 1 つ目の群れの数 A 、 2 つ目の群れの数 B 、夕方に数えた数 C がスペース区切りで与えられる。

出力

羊の数が合っているなら Yes を、そうでないなら No を 1 行で出力せよ。


入力例 1

12 8 20

出力例 1

Yes

入力例 2

5 6 12

出力例 2

No

入力例 3

1 1 2

出力例 3

Yes

入力例 4

316 411 1968

出力例 4

No

入力例 5

455 910 1365

出力例 5

Yes

Score : 166 pts

Problem Statement

Takahashi keeps sheep on a ranch. The sheep graze in two separate flocks, and during his morning check there were A sheep in the first flock and B sheep in the second flock.

In the evening, Takahashi gathered all the sheep in one place and counted them again, finding C sheep. If not a single sheep has been added or lost, print Yes; otherwise, print No.

Constraints

  • 1 \leq A \leq 1000
  • 1 \leq B \leq 1000
  • 1 \leq C \leq 2000
  • All input values are integers.

Input

The input is given from standard input in the following format:

A B C
  • The first line contains A, the number in the first flock in the morning, B, the number in the second flock, and C, the number counted in the evening, separated by spaces.

Output

Print Yes in one line if the number of sheep matches, and No otherwise.


Sample Input 1

12 8 20

Sample Output 1

Yes

Sample Input 2

5 6 12

Sample Output 2

No

Sample Input 3

1 1 2

Sample Output 3

Yes

Sample Input 4

316 411 1968

Sample Output 4

No

Sample Input 5

455 910 1365

Sample Output 5

Yes
B - Achieving the Muscle Training Goal

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233 点

問題文

高橋君のジムには H 人の会員がいて、 W 日間の筋トレの記録が付けられています。記録は H 個の文字列 S_1, S_2, \ldots, S_H で表され、 S_i の j 文字目が o なら i 番目の会員は j 日目に目標回数を達成し、 x なら達成しませんでした。

各会員について、初めて目標を達成した日が何日目かを求めてください。 W 日間で一度も達成しなかった会員については -1 を出力してください。

制約

  • 1 \leq H \leq 10
  • 1 \leq W \leq 10
  • S_i は o と x のみからなる長さ W の文字列

入力

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

H W
S_1
S_2
\vdots
S_H
  • 1 行目には、会員の人数 H と日数 W がスペース区切りで与えられる。
  • 2 行目から H 行にわたり、 1 + i 行目には i 番目の会員の記録を表す文字列 S_i が与えられる。

出力

H 行出力せよ。 i 行目には i 番目の会員が初めて目標を達成した日を出力せよ。一度も達成しなかった場合は -1 を出力せよ。


入力例 1

3 4
xxox
xxxx
oxxx

出力例 1

3
-1
1

入力例 2

1 3
xxo

出力例 2

3

入力例 3

2 2
xx
xo

出力例 3

-1
2

入力例 4

5 2
xx
xx
xx
ox
ox

出力例 4

-1
-1
-1
1
1

入力例 5

5 1
x
x
o
x
x

出力例 5

-1
-1
1
-1
-1

Score : 233 pts

Problem Statement

Takahashi's gym has H members, and muscle training records have been kept for W days. The records are represented by H strings S_1, S_2, \ldots, S_H. If the j-th character of S_i is o, member i achieved the target number of repetitions on day j; if it is x, they did not.

For each member, find the first day on which they achieved the goal. If a member never achieved it during the W days, output -1.

Constraints

  • 1 \leq H \leq 10
  • 1 \leq W \leq 10
  • S_i is a string of length W consisting only of o and x.

Input

The input is given from standard input in the following format:

H W
S_1
S_2
\vdots
S_H
  • The first line contains the number of members H and the number of days W, separated by a space.
  • The following H lines each contain a string: line 1 + i contains the string S_i representing the record of member i.

Output

Output H lines. The i-th line should contain the first day on which member i achieved the goal. If they never achieved it, output -1.


Sample Input 1

3 4
xxox
xxxx
oxxx

Sample Output 1

3
-1
1

Sample Input 2

1 3
xxo

Sample Output 2

3

Sample Input 3

2 2
xx
xo

Sample Output 3

-1
2

Sample Input 4

5 2
xx
xx
xx
ox
ox

Sample Output 4

-1
-1
-1
1
1

Sample Input 5

5 1
x
x
o
x
x

Sample Output 5

-1
-1
1
-1
-1
C - Replanting the Flower Bed

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

高橋君は、N 行 M 列の区画に分かれた花壇を管理しています。現在、上から i 行目、左から j 列目の区画には、品種番号 A_{i,j} の花が植えられています。

高橋君は、花壇全体の見栄えを統一するため、すべての区画を同じ品種の花にそろえたいと考えています。高橋君は、次の操作を何回でも行うことができます。

  • 区画を一つ選び、その区画の花を、好きな品種番号の花に植え替える。植え替え先の品種番号は、現在花壇に植えられているものに限らず、任意に選ぶことができる。

すべての区画の花を同じ品種にそろえるために必要な、操作回数の最小値を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq N \times M \leq 5 \times 10^5
  • 1 \leq A_{i,j} \leq 10^9
  • 入力はすべて整数である。

入力

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

N M
A_{1,1} A_{1,2} \dots A_{1,M}
A_{2,1} A_{2,2} \dots A_{2,M}
\vdots
A_{N,1} A_{N,2} \dots A_{N,M}

1 行目には、花壇の行数 N と列数 M がスペース区切りで与えられる。続く N 行のうち i 行目には、花壇の i 行目にある M 個の区画の品種番号 A_{i,1}, A_{i,2}, \dots, A_{i,M} がスペース区切りで与えられる。

出力

すべての区画の花を同じ品種にそろえるために必要な操作回数の最小値を 1 行で出力してください。


入力例 1

2 3
1 2 1
3 1 4

出力例 1

3

入力例 2

3 2
7 7
7 7
7 7

出力例 2

0

入力例 3

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

出力例 3

10

入力例 4

8 9
42 5 7 42 9 42 11 13 42
1 42 3 4 42 6 42 8 10
42 12 42 14 15 42 17 18 42
19 20 21 42 23 24 42 26 27
42 28 29 30 42 32 33 42 35
36 42 38 39 40 42 42 43 44
45 46 42 48 49 50 42 52 53
42 54 55 42 57 58 59 42 42

出力例 4

47

入力例 5

1 1
1000000000

出力例 5

0

Score : 300 pts

Problem Statement

Takahashi manages a flower bed divided into a grid of N rows and M columns. Currently, the plot in the i-th row from the top and j-th column from the left has a flower of variety number A_{i,j} planted in it.

To unify the appearance of the entire flower bed, Takahashi wants to make all plots have the same variety of flower. Takahashi can perform the following operation any number of times:

  • Choose one plot and replace the flower in that plot with a flower of any variety number. The variety number of the replacement flower can be chosen arbitrarily and is not limited to varieties currently planted in the flower bed.

Find the minimum number of operations required to make all plots have the same variety of flower.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq N \times M \leq 5 \times 10^5
  • 1 \leq A_{i,j} \leq 10^9
  • All input values are integers.

Input

Input is given from standard input in the following format:

N M
A_{1,1} A_{1,2} \dots A_{1,M}
A_{2,1} A_{2,2} \dots A_{2,M}
\vdots
A_{N,1} A_{N,2} \dots A_{N,M}

The first line contains the number of rows N and the number of columns M of the flower bed, separated by a space. In the following N lines, the i-th line contains the variety numbers A_{i,1}, A_{i,2}, \dots, A_{i,M} of the M plots in the i-th row of the flower bed, separated by spaces.

Output

Print in one line the minimum number of operations required to make all plots have the same variety of flower.


Sample Input 1

2 3
1 2 1
3 1 4

Sample Output 1

3

Sample Input 2

3 2
7 7
7 7
7 7

Sample Output 2

0

Sample Input 3

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

Sample Output 3

10

Sample Input 4

8 9
42 5 7 42 9 42 11 13 42
1 42 3 4 42 6 42 8 10
42 12 42 14 15 42 17 18 42
19 20 21 42 23 24 42 26 27
42 28 29 30 42 32 33 42 35
36 42 38 39 40 42 42 43 44
45 46 42 48 49 50 42 52 53
42 54 55 42 57 58 59 42 42

Sample Output 4

47

Sample Input 5

1 1
1000000000

Sample Output 5

0
D - Connection Standards and Converters

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

N 台の装置が、N-1 本のケーブルで結ばれています。ケーブル j は装置 U_j と装置 V_j を結んでおり、装置とケーブルは木をなしています。

各装置には、規格 0 または規格 1 のどちらか一方を設定します。装置 i に規格 0 を設定する費用は A_i 円、規格 1 を設定する費用は B_i 円です。

ケーブルの両端の装置に異なる規格を設定した場合、そのケーブルに変換器を一つ取り付ける必要があります。ケーブル j 用の変換器の費用は C_j 円です。両端が同じ規格なら変換器は不要です。すべての装置とケーブルを使用し、ケーブルを取り外すことはできません。

装置の設定費用と変換器の費用の合計としてあり得る最小値を求めてください。

制約

  • 1 \leq N \leq 200000
  • 0 \leq A_i,B_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq U_j,V_j \leq N (1 \leq j \leq N-1)
  • 0 \leq C_j \leq 10^9 (1 \leq j \leq N-1)
  • 装置とケーブルは木をなす。
  • 入力はすべて整数である。

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N
U_1 V_1 C_1
U_2 V_2 C_2
\vdots
U_{N-1} V_{N-1} C_{N-1}
  • 1 行目には装置の台数 N が与えられます。
  • 続く N 行のうち i 行目には、装置 i の二つの設定費用 A_i,B_i が与えられます。
  • 続く N-1 行のうち j 行目には、ケーブル j の両端の装置番号 U_j,V_j と、変換器の費用 C_j が与えられます。
  • N=1 の場合、ケーブルに関する行はありません。

出力

費用の合計の最小値を出力してください。


入力例 1

2
0 5
5 0
1 2 10

出力例 1

5

入力例 2

2
1 9
8 2
1 2 3

出力例 2

6

入力例 3

4
0 9
8 0
8 0
0 7
1 2 2
1 3 2
1 4 10

出力例 3

4

入力例 4

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

出力例 4

15

入力例 5

1
1000000000 1000000000

出力例 5

1000000000

Score : 400 pts

Problem Statement

There are N devices connected by N-1 cables. Cable j connects device U_j and device V_j, and the devices and cables form a tree.

Each device must be configured with either standard 0 or standard 1. Configuring device i with standard 0 costs A_i yen, and configuring it with standard 1 costs B_i yen.

If the devices at both ends of a cable are configured with different standards, a converter must be installed on that cable. The converter for cable j costs C_j yen. If both ends have the same standard, no converter is needed. All devices and cables must be used, and cables cannot be removed.

Find the minimum possible total cost of the device configurations and the converters.

Constraints

  • 1 \leq N \leq 200000
  • 0 \leq A_i,B_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq U_j,V_j \leq N (1 \leq j \leq N-1)
  • 0 \leq C_j \leq 10^9 (1 \leq j \leq N-1)
  • The devices and cables form a tree.
  • 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
U_1 V_1 C_1
U_2 V_2 C_2
\vdots
U_{N-1} V_{N-1} C_{N-1}
  • The first line contains the number of devices N.
  • The i-th of the following N lines contains the two configuration costs A_i, B_i for device i.
  • The j-th of the following N-1 lines contains the device numbers U_j, V_j at both ends of cable j, and the cost of the converter C_j.
  • If N=1, there are no lines regarding cables.

Output

Print the minimum possible total cost.


Sample Input 1

2
0 5
5 0
1 2 10

Sample Output 1

5

Sample Input 2

2
1 9
8 2
1 2 3

Sample Output 2

6

Sample Input 3

4
0 9
8 0
8 0
0 7
1 2 2
1 3 2
1 4 10

Sample Output 3

4

Sample Input 4

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

Sample Output 4

15

Sample Input 5

1
1000000000 1000000000

Sample Output 5

1000000000
E - Exhibition Order Jumping Up and Down

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 466 点

問題文

N 個の展示品が番号 1,2,\ldots,N の順に並んでいます。展示品 i の高さは A_i、得点は B_i です。得点は負の場合もあります。

いくつかの展示品を選び、番号の小さい順に展示します。選んだ番号を p_1<p_2<\cdots<p_k としたとき、次の条件を満たす必要があります。

  • すべての 1\leq j<k について、L\leq |A_{p_{j+1}}-A_{p_j}|\leq R である。
  • 隣り合う展示品の高さが上がるか下がるかは、交互でなければならない。すなわち、すべての 1\leq j<k-1 について、A_{p_{j+1}}-A_{p_j} と A_{p_{j+2}}-A_{p_{j+1}} は異なる符号を持つ。

最初は上がっても下がってもかまいません。1 個だけ選ぶ場合は両方の条件を満たします。また、何も選ばないこともでき、その場合の得点の総和は 0 です。

条件を満たす選び方における、選んだ展示品の得点の総和の最大値を求めてください。

制約

  • 1\leq N\leq 200000
  • 1\leq L\leq R\leq 10^9
  • 1\leq A_i\leq 10^9 (1\leq i\leq N)
  • -10^9\leq B_i\leq 10^9 (1\leq i\leq N)
  • 入力はすべて整数である。

入力

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

N L R
A_1 B_1
A_2 B_2
\vdots
A_N B_N
  • 1 行目には展示品の個数 N と、高さの差の下限 L、上限 R が与えられる。
  • 続く N 行のうち i 行目には、展示品 i の高さ A_i と得点 B_i が与えられる。

出力

選んだ展示品の得点の総和の最大値を整数で出力せよ。


入力例 1

3 1 3
1 5
3 4
2 6

出力例 1

15

入力例 2

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

出力例 2

0

入力例 3

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

出力例 3

24

入力例 4

7 1 2
1 1
2 2
3 3
2 4
1 5
3 6
2 7

出力例 4

23

入力例 5

1 1000000000 1000000000
1000000000 1000000000

出力例 5

1000000000

Score : 466 pts

Problem Statement

There are N exhibits lined up in the order of numbers 1, 2, \ldots, N. Exhibit i has a height of A_i and a score of B_i. Scores may be negative.

You choose some exhibits and display them in ascending order of their indices. If the chosen indices are p_1 < p_2 < \cdots < p_k, the following conditions must be satisfied:

  • For all 1 \leq j < k, L \leq |A_{p_{j+1}} - A_{p_j}| \leq R.
  • Whether the height of adjacent exhibits increases or decreases must alternate. That is, for all 1 \leq j < k - 1, A_{p_{j+1}} - A_{p_j} and A_{p_{j+2}} - A_{p_{j+1}} have different signs.

The height may either increase or decrease at the start. If only 1 exhibit is chosen, both conditions are satisfied. You may also choose to display no exhibits at all, in which case the total score is 0.

Find the maximum possible sum of scores of the chosen exhibits in a valid selection.

Constraints

  • 1\leq N\leq 200000
  • 1\leq L\leq R\leq 10^9
  • 1\leq A_i\leq 10^9 (1\leq i\leq N)
  • -10^9\leq B_i\leq 10^9 (1\leq i\leq N)
  • All input values are integers.

Input

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

N L R
A_1 B_1
A_2 B_2
\vdots
A_N B_N
  • The first line contains the number of exhibits N, the lower bound on the height difference L, and the upper bound R.
  • Of the following N lines, the i-th line contains the height A_i and the score B_i of exhibit i.

Output

Print the maximum possible sum of scores of the chosen exhibits as an integer.


Sample Input 1

3 1 3
1 5
3 4
2 6

Sample Output 1

15

Sample Input 2

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

Sample Output 2

0

Sample Input 3

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

Sample Output 3

24

Sample Input 4

7 1 2
1 1
2 2
3 3
2 4
1 5
3 6
2 7

Sample Output 4

23

Sample Input 5

1 1000000000 1000000000
1000000000 1000000000

Sample Output 5

1000000000