A - Bulletin Board

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

A 社主催のプログラミングコンテストの開催が決定したので、その告知を会社の掲示板に張り出すことにしました。

掲示板は NN 列のマス目の形をしています。 また、告知は HW 列ぶんの場所を取ります。

告知をちょうど HW 個のマスを完全に覆うように掲示板に張り出すとき、張り出す場所の選び方は何通りありますか。

制約

  • 1 \leq H, W \leq N \leq 100
  • 入力値はすべて整数である。

入力

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

N
H
W

出力

答えを出力せよ。


入力例 1

3
2
3

出力例 1

2

以下の 2 通りの選び方があります。 ここで、# は掲示板のうち告知に覆われたマスを、. は掲示板のうち告知に覆われていないマスを表します。

###   ...
###   ###
...   ###

入力例 2

100
1
1

出力例 2

10000

入力例 3

5
4
2

出力例 3

8

Score : 100 points

Problem Statement

It has been decided that a programming contest sponsored by company A will be held, so we will post the notice on a bulletin board.

The bulletin board is in the form of a grid with N rows and N columns, and the notice will occupy a rectangular region with H rows and W columns.

How many ways are there to choose where to put the notice so that it completely covers exactly HW squares?

Constraints

  • 1 \leq H, W \leq N \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
H
W

Output

Print the answer.


Sample Input 1

3
2
3

Sample Output 1

2

There are two ways to put the notice, as follows:

###   ...
###   ###
...   ###

Here, # represents a square covered by the notice, and . represents a square not covered.


Sample Input 2

100
1
1

Sample Output 2

10000

Sample Input 3

5
4
2

Sample Output 3

8
B - Contests

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

あなたはプログラミングコンテストを開催するため問題を N 問作成しました。 このうち i 問目をコンテストに出題する場合、配点は P_i 点となります。

これらの問題を使って、以下の条件を満たすコンテストをできるだけ多く開催したいと思います。 異なるコンテストの間で問題の重複があってはいけません。 最大で何回のコンテストを開催できますか。

  • 問題が 3 問出題され、1 問目の配点は A 点以下、2 問目の配点は A + 1 点以上 B 点以下、3 問目の配点は B + 1 点以上である。

制約

  • 3 \leq N \leq 100
  • 1 \leq P_i \leq 20 (1 \leq i \leq N)
  • 1 \leq A < B < 20
  • 入力値はすべて整数である。

入力

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

N
A B
P_1 P_2 ... P_N

出力

答えを出力せよ。


入力例 1

7
5 15
1 10 16 2 7 20 12

出力例 1

2

1, 2, 3 問目の問題、4, 5, 6 問目の問題をそれぞれ組み合わせることで 2 回のコンテストを開催できます。


入力例 2

8
3 8
5 5 5 10 10 10 15 20

出力例 2

0

A = 3 点以下の問題が存在しないので、コンテストを開催できません。


入力例 3

3
5 6
5 6 10

出力例 3

1

Score : 200 points

Problem Statement

You have written N problems to hold programming contests. The i-th problem will have a score of P_i points if used in a contest.

With these problems, you would like to hold as many contests as possible under the following condition:

  • A contest has three problems. The first problem has a score not greater than A points, the second has a score between A + 1 and B points (inclusive), and the third has a score not less than B + 1 points.

The same problem should not be used in multiple contests. At most how many contests can be held?

Constraints

  • 3 \leq N \leq 100
  • 1 \leq P_i \leq 20 (1 \leq i \leq N)
  • 1 \leq A < B < 20
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A B
P_1 P_2 ... P_N

Output

Print the answer.


Sample Input 1

7
5 15
1 10 16 2 7 20 12

Sample Output 1

2

Two contests can be held by putting the first, second, third problems and the fourth, fifth, sixth problems together.


Sample Input 2

8
3 8
5 5 5 10 10 10 15 20

Sample Output 2

0

No contest can be held, because there is no problem with a score of A = 3 or less.


Sample Input 3

3
5 6
5 6 10

Sample Output 3

1
C - Alternating Path

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

HW 列のマス目があり、各マスは黒または白に塗られています。

各マスの色を表す H 個の長さ W の文字列 S_1, S_2, ..., S_H が与えられます。 マス目の上から i 番目、左から j 番目 (1 \leq i \leq H, 1 \leq j \leq W) のマスが黒く塗られているとき 文字列 S_ij 文字目は # となっており、白く塗られているとき文字列 S_ij 文字目は . となっています。

黒く塗られたマス c_1 と白く塗られたマス c_2 の組であって、以下の条件を満たすものの個数を求めてください。

  • 上下左右に隣り合うマスへの移動を繰り返してマス c_1 からマス c_2 へ行く方法であって、通るマスの色が黒、白、黒、白・・・と交互になっているものが存在する。

制約

  • 1 \leq H, W \leq 400
  • |S_i| = W (1 \leq i \leq H)
  • i (1 \leq i \leq H) に対して、文字列 S_i は文字 # と文字 . だけからなる。

入力

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

H W
S_1
S_2
:
S_H

出力

答えを出力せよ。


入力例 1

3 3
.#.
..#
#..

出力例 1

10

上から i 行目、左から j 列目のマスを (i, j) と書くとき、条件を満たすマスの組として ((1, 2), (3, 3))((3, 1), (3, 2)) などがあります。


入力例 2

2 4
....
....

出力例 2

0

入力例 3

4 3
###
###
...
###

出力例 3

6

Score : 300 points

Problem Statement

There is a grid with H rows and W columns, where each square is painted black or white.

You are given H strings S_1, S_2, ..., S_H, each of length W. If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is #; if that square is painted white, the j-th character in the string S_i is ..

Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:

  • There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...

Constraints

  • 1 \leq H, W \leq 400
  • |S_i| = W (1 \leq i \leq H)
  • For each i (1 \leq i \leq H), the string S_i consists of characters # and ..

Input

Input is given from Standard Input in the following format:

H W
S_1
S_2
:
S_H

Output

Print the answer.


Sample Input 1

3 3
.#.
..#
#..

Sample Output 1

10

Some of the pairs satisfying the condition are ((1, 2), (3, 3)) and ((3, 1), (3, 2)), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.


Sample Input 2

2 4
....
....

Sample Output 2

0

Sample Input 3

4 3
###
###
...
###

Sample Output 3

6
D - Nearest Card Game

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 枚のカードがあり、このうち i 枚目のカードには整数 A_i が書かれています。 どの 2 枚のカードに書かれた整数も相異なります。

高橋くんと青木くんはこれらのカードを用いて以下のようなゲームをすることにしました。

  • 青木くんは整数 x を一つ決める。
  • 高橋くんからはじめて、交互にカードを一枚ずつ取っていく。その際、取るカードは以下のように選ぶ。
    • 高橋くんは残っているカードのうち書かれている整数が最も大きいカードを取る。
    • 青木くんは残っているカードのうち書かれている整数が x に最も近いカードを取る。ただしそのようなカードが複数ある場合は、それらのうち書かれている整数が最も小さいカードを取る。
  • 残っているカードがなくなったときゲームは終了する。

Q 個の x の値の候補 X_1, X_2, ..., X_Q が与えられます。 各 i (1 \leq i \leq Q) に対して、青木くんが x = X_i としたときに高橋くんが取ることになるカードに書かれた整数の和を求めてください。

制約

  • 2 \leq N \leq 100,000
  • 1 \leq Q \leq 100,000
  • 1 \leq A_1 < A_2 < ... < A_N \leq 10^9
  • 1 \leq X_i \leq 10^9 (1 \leq i \leq Q)
  • 入力値はすべて整数である。

入力

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

N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q

出力

Q 行出力せよ。このうち i 行目 (1 \leq i \leq Q) には x = X_i のときの答えを出力せよ。


入力例 1

5 5
3 5 7 11 13
1
4
9
10
13

出力例 1

31
31
27
23
23

たとえば、x = X_3(= 9) のとき、ゲームは以下のように進行します。

  • 高橋くんが 13 が書かれたカードを取る。
  • 青木くんが 7 が書かれたカードを取る。
  • 高橋くんが 11 が書かれたカードを取る。
  • 青木くんが 5 が書かれたカードを取る。
  • 高橋くんが 3 が書かれたカードを取る。

よって、3 行目には 13 + 11 + 3 = 27 を出力します。


入力例 2

4 3
10 20 30 40
2
34
34

出力例 2

70
60
60

Score : 500 points

Problem Statement

There are N cards. The i-th card has an integer A_i written on it. For any two cards, the integers on those cards are different.

Using these cards, Takahashi and Aoki will play the following game:

  • Aoki chooses an integer x.
  • Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:
    • Takahashi should take the card with the largest integer among the remaining card.
    • Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.
  • The game ends when there is no card remaining.

You are given Q candidates for the value of x: X_1, X_2, ..., X_Q. For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.

Constraints

  • 2 \leq N \leq 100 000
  • 1 \leq Q \leq 100 000
  • 1 \leq A_1 < A_2 < ... < A_N \leq 10^9
  • 1 \leq X_i \leq 10^9 (1 \leq i \leq Q)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q

Output

Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.


Sample Input 1

5 5
3 5 7 11 13
1
4
9
10
13

Sample Output 1

31
31
27
23
23

For example, when x = X_3(= 9), the game proceeds as follows:

  • Takahashi takes the card with 13.
  • Aoki takes the card with 7.
  • Takahashi takes the card with 11.
  • Aoki takes the card with 5.
  • Takahashi takes the card with 3.

Thus, 13 + 11 + 3 = 27 should be printed on the third line.


Sample Input 2

4 3
10 20 30 40
2
34
34

Sample Output 2

70
60
60
E - Attack to a Tree

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

A 社のサーバーは 1, 2, ..., N の番号がついた N 個のデバイスを N - 1 本のケーブルで接続した構造をしています。 i 本目のケーブルはデバイス U_i とデバイス V_i を接続しています。 どの異なる 2 つのデバイスも何本かのケーブルを介して接続されています。

各デバイス v (1 \leq v \leq N) には 0 でない整数 A_v が割り当てられています。 これらの整数は以下のことを表します。

  • A_v < 0 のとき、デバイス v はコンピュータであり、大きさ -A_v の電力を消費することを表す。
  • A_v > 0 のとき、デバイス v はバッテリーであり、大きさ A_v の電力を供給できることを表す。

あなたは何本か (0 本以上) のケーブルを切断することで A 社のサーバーの機能を停止させることにしました。 ケーブルを切断するとデバイスはいくつかの連結成分に分かれます。 これら連結成分がすべて以下のいずれかの条件を満たすようになれば、サーバーは機能を停止します。

  • 連結成分に属する各デバイス v に対する A_v の値がすべて正である。すなわち、連結成分にコンピュータが存在しない。
  • 連結成分に属する各デバイス v に対する A_v の値の和が負である。すなわち、連結成分における電力供給が不足している。

サーバーの機能を停止させるためには、最小で何本のケーブルを切断すれば良いでしょうか。

制約

  • 1 \leq N \leq 5,000
  • 1 \leq |A_i| \leq 10^9 (1 \leq i \leq N)
  • 1 \leq U_i, V_i \leq N (1 \leq i \leq N - 1)
  • U_i \neq V_i (1 \leq i \leq N - 1)
  • どの異なる 2 つのデバイスも何本かのケーブルを介して接続されている。
  • 入力値はすべて整数である。

入力

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

N
A_1 A_2 ... A_N
U_1 V_1
U_2 V_2
:
U_{N - 1} V_{N - 1}

出力

答えを出力せよ。


入力例 1

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

出力例 1

1

デバイス 1 とデバイス 2 を結ぶケーブルを切断すればよいです。


入力例 2

4
1 2 3 4
1 2
1 3
1 4

出力例 2

0

入力例 3

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

出力例 3

5

入力例 4

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

出力例 4

3

入力例 5

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

出力例 5

3

Score : 600 points

Problem Statement

The server in company A has a structure where N devices numbered 1, 2, ..., N are connected with N - 1 cables. The i-th cable connects Device U_i and Device V_i. Any two different devices are connected through some number of cables.

Each device v (1 \leq v \leq N) has a non-zero integer A_v, which represents the following:

  • If A_v < 0, Device v is a computer that consumes an electric power of -A_v.
  • If A_v > 0, Device v is a battery that supplies an electric power of A_v.

You have decided to disconnect some number of cables (possibly zero) to disable the server. When some cables are disconnected, the devices will be divided into some number of connected components. The server will be disabled if all of these connected components satisfy one of the following conditions:

  • There is no computer in the connected component. That is, A_v is positive for every device v that belongs to the connected component.
  • There is not enough supply of electric power in the connected component. That is, the sum of A_v over all devices v that belong to the connected component is negative.

At least how many cables do you need to disconnect in order to disable the server?

Constraints

  • 1 \leq N \leq 5 000
  • 1 \leq |A_i| \leq 10^9 (1 \leq i \leq N)
  • 1 \leq U_i, V_i \leq N (1 \leq i \leq N - 1)
  • U_i \neq V_i (1 \leq i \leq N - 1)
  • Any two different devices are connected through some number of cables.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
U_1 V_1
U_2 V_2
:
U_{N - 1} V_{N - 1}

Output

Print the answer.


Sample Input 1

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

Sample Output 1

1

We should disconnect the cable connecting Device 1 and Device 2.


Sample Input 2

4
1 2 3 4
1 2
1 3
1 4

Sample Output 2

0

Sample Input 3

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

Sample Output 3

5

Sample Input 4

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

Sample Output 4

3

Sample Input 5

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

Sample Output 5

3