A - Subscribers

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

私たちは、新聞の購読に関する調査を行いました。 具体的には、調査の対象者 N 人に対し、それぞれ次の 2 つの質問を行いました。

  • 質問 1: あなたは新聞 X を購読しているか?
  • 質問 2: あなたは新聞 Y を購読しているか?

その結果、質問 1 に対して「はい」と回答した人の数は A 人、質問 2 に対して「はい」と回答した人の数は B 人でした。

このとき、調査の対象者のうち新聞 X, Y の両方を購読している人の数は最大で何人であり、また最小で何人であると考えられるでしょうか?

この問いに答えるプログラムを書いてください。

制約

  • 1 \leq N \leq 100
  • 0 \leq A \leq N
  • 0 \leq B \leq N
  • 入力される値はすべて整数である。

入力

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

N A B

出力

両方の新聞を購読している人の数として考えられる最大の人数と最小の人数をこの順に、空白で区切って出力せよ。


入力例 1

10 3 5

出力例 1

3 0

この例では、調査の対象者 10 人のうち 3 人が新聞 X を購読していると回答し、5 人が新聞 Y を購読していると回答しています。

このとき、両方の新聞を購読している人の数は最大で 3 人、最小で 0 人です。


入力例 2

10 7 5

出力例 2

5 2

この例では、調査の対象者 10 人のうち 7 人が新聞 X を購読していると回答し、5 人が新聞 Y を購読していると回答しています。

このとき、両方の新聞を購読している人の数は最大で 5 人、最小で 2 人です。


入力例 3

100 100 100

出力例 3

100 100

Score : 100 points

Problem Statement

We conducted a survey on newspaper subscriptions. More specifically, we asked each of the N respondents the following two questions:

  • Question 1: Are you subscribing to Newspaper X?
  • Question 2: Are you subscribing to Newspaper Y?

As the result, A respondents answered "yes" to Question 1, and B respondents answered "yes" to Question 2.

What are the maximum possible number and the minimum possible number of respondents subscribing to both newspapers X and Y?

Write a program to answer this question.

Constraints

  • 1 \leq N \leq 100
  • 0 \leq A \leq N
  • 0 \leq B \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the maximum possible number and the minimum possible number of respondents subscribing to both newspapers, in this order, with a space in between.


Sample Input 1

10 3 5

Sample Output 1

3 0

In this sample, out of the 10 respondents, 3 answered they are subscribing to Newspaper X, and 5 answered they are subscribing to Newspaper Y.

Here, the number of respondents subscribing to both newspapers is at most 3 and at least 0.


Sample Input 2

10 7 5

Sample Output 2

5 2

In this sample, out of the 10 respondents, 7 answered they are subscribing to Newspaper X, and 5 answered they are subscribing to Newspaper Y.

Here, the number of respondents subscribing to both newspapers is at most 5 and at least 2.


Sample Input 3

100 100 100

Sample Output 3

100 100
B - Touitsu

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

三つの文字列 A, B, C が与えられます。これらはそれぞれ、英小文字からなる長さ N の文字列です。

私たちの目標は、これら三つの文字列をすべて等しくすることです。そのために、あなたは次の操作を繰り返し行うことができます。

  • 操作: 文字列 A, B, C のうち一つを選び、さらに 1 以上 N 以下の整数 i を指定する。そして、選んだ文字列の先頭から i 文字目を別の何らかの英小文字に変更する。

目標を達成するためには最小で何回の操作が必要でしょうか?

制約

  • 1 \leq N \leq 100
  • A, B, C はそれぞれ長さ N の文字列である。
  • A, B, C の各文字は英小文字である。

入力

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

N
A
B
C

出力

必要な最小の操作回数を出力せよ。


入力例 1

4
west
east
wait

出力例 1

3

この例では、はじめ A = westB = eastC = wait です。以下のように 3 回の操作を行うことで、最小の操作回数で目標を達成できます。

  • A2 文字目を a に変更する。Awast となる。
  • B1 文字目を w に変更する。Bwast となる。
  • C3 文字目を s に変更する。Cwast となる。

入力例 2

9
different
different
different

出力例 2

0

はじめから A, B, C がすべて等しい場合、必要な操作回数は 0 となります。


入力例 3

7
zenkoku
touitsu
program

出力例 3

13

Score : 200 points

Problem Statement

You are given three strings A, B and C. Each of these is a string of length N consisting of lowercase English letters.

Our objective is to make all these three strings equal. For that, you can repeatedly perform the following operation:

  • Operation: Choose one of the strings A, B and C, and specify an integer i between 1 and N (inclusive). Change the i-th character from the beginning of the chosen string to some other lowercase English letter.

What is the minimum number of operations required to achieve the objective?

Constraints

  • 1 \leq N \leq 100
  • Each of the strings A, B and C is a string of length N.
  • Each character in each of the strings A, B and C is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

N
A
B
C

Output

Print the minimum number of operations required.


Sample Input 1

4
west
east
wait

Sample Output 1

3

In this sample, initially A = westB = eastC = wait. We can achieve the objective in the minimum number of operations by performing three operations as follows:

  • Change the second character in A to a. A is now wast.
  • Change the first character in B to w. B is now wast.
  • Change the third character in C to s. C is now wast.

Sample Input 2

9
different
different
different

Sample Output 2

0

If A, B and C are already equal in the beginning, the number of operations required is 0.


Sample Input 3

7
zenkoku
touitsu
program

Sample Output 3

13
C - Different Strokes

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

高橋くんと青木さんの前に N 皿の料理が置かれています。 便宜上、これらの料理を料理 1、料理 2、…、料理 N と呼びます。

高橋くんが料理 i を食べると彼は A_i ポイントの 幸福度 を得ます。 また、青木さんが料理 i を食べると彼女は B_i ポイントの幸福度を得ます。

彼らは、高橋くんから始めて交互に、料理を 1 皿ずつ選んで食べることを料理が尽きるまで続けます。 ただし、彼らはともに、「最終的に自分が得る幸福度の総和」から「最終的に相手が得る幸福度の総和」を引いた値を最大化するように料理を選びます。

このとき、「最終的に高橋くんが得る幸福度の総和」から「最終的に青木さんが得る幸福度の総和」を引いた値はいくつになるでしょうか?

制約

  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • 入力される値はすべて整数である。

入力

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

N
A_1 B_1
:
A_N B_N

出力

「最終的に高橋くんが得る幸福度の総和」から「最終的に青木さんが得る幸福度の総和」を引いた値を出力せよ。


入力例 1

3
10 10
20 20
30 30

出力例 1

20

この例では、二人のどちらも、料理 1 を食べると 10 ポイント、料理 2 を食べると 20 ポイント、料理 3 を食べると 30 ポイントの幸福度を得ます。

この場合、高橋くんと青木さんの「好み」が一致しているため、彼らは毎回残っている料理のうち最も幸福度を多く得られる料理を選びます。よって、最初に高橋くんは料理 3 を選び、次に青木さんは料理 2 を選び、最後に高橋くんが料理 1 を選ぶため、答えは (30 + 10) - 20 = 20 です。


入力例 2

3
20 10
20 20
20 30

出力例 2

20

この例では、高橋くんは料理 1,2,3 のいずれを食べても 20 ポイントの幸福度を得ますが、青木さんは料理 1 を食べると 10 ポイント、料理 2 を食べると 20 ポイント、料理 3 を食べると 30 ポイントの幸福度を得ます。

今回は、青木さんのみに料理の好き嫌いがあるため、彼らは毎回残っている料理のうち青木さんが最も幸福度を多く得られる料理を選びます。よって、最初に高橋くんは料理 3 を選び、次に青木さんは料理 2 を選び、最後に高橋くんが料理 1 を選ぶため、答えは (20 + 20) - 20 = 20 です。


入力例 3

6
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

出力例 3

-2999999997

答えは 32 ビット整数に収まらない可能性があります。

Score : 400 points

Problem Statement

There are N dishes of cuisine placed in front of Takahashi and Aoki. For convenience, we call these dishes Dish 1, Dish 2, ..., Dish N.

When Takahashi eats Dish i, he earns A_i points of happiness; when Aoki eats Dish i, she earns B_i points of happiness.

Starting from Takahashi, they alternately choose one dish and eat it, until there is no more dish to eat. Here, both of them choose dishes so that the following value is maximized: "the sum of the happiness he/she will earn in the end" minus "the sum of the happiness the other person will earn in the end".

Find the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".


Sample Input 1

3
10 10
20 20
30 30

Sample Output 1

20

In this sample, both of them earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since Takahashi and Aoki have the same "taste", each time they will choose the dish with which they can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (30 + 10) - 20 = 20.


Sample Input 2

3
20 10
20 20
20 30

Sample Output 2

20

In this sample, Takahashi earns 20 points of happiness by eating any one of the dishes 1, 2 and 3, but Aoki earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since only Aoki has likes and dislikes, each time they will choose the dish with which Aoki can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (20 + 20) - 20 = 20.


Sample Input 3

6
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 3

-2999999997

Note that the answer may not fit into a 32-bit integer.

D - Restore the Tree

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点の根付き木 (注記を参照) があり、その頂点には 1 から N までの番号が振られています。 根以外の各頂点には、その親から一本の有向辺が伸びています。 なお、根は頂点 1 とは限りません。

高橋くんは、このグラフに M 本の新たな有向辺を書き加えました。 書き足された各辺 u \rightarrow v は、ある頂点 u からその子孫であるような頂点 v に向かって伸びています。

高橋くんが辺を書き加えたあとの N 頂点 N-1+M 辺の有向グラフが与えられます。 より具体的には、N-1+M 組の整数のペア (A_1, B_1), ..., (A_{N-1+M}, B_{N-1+M}) が与えられ、これらは i 番目の辺が頂点 A_i から頂点 B_i に向かって伸びていることを表します。

元の根付き木を復元してください。

注記

「木」や関連するグラフ理論の用語に関しては、Wikipediaの記事などをご覧ください。

制約

  • 3 \leq N
  • 1 \leq M
  • N + M \leq 10^5
  • 1 \leq A_i, B_i \leq N
  • A_i \neq B_i
  • i \neq j ならば (A_i, B_i) \neq (A_j, B_j)
  • 入力されるグラフは、N 頂点の根付き木に問題文中の条件を満たす M 本の辺を書き足すことで得られる。

入力

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

N M
A_1 B_1
:
A_{N-1+M} B_{N-1+M}

出力

N 行出力せよ。 i 行目には、頂点 i が元の木の根であれば 0 を出力し、そうでなければ元の木で頂点 i の親を表す整数を出力すること。

なお、元の木は一意に定まることが示せる。


入力例 1

3 1
1 2
1 3
2 3

出力例 1

0
1
2

入力されたグラフは次のようなものです。

これは、1 \rightarrow 2 \rightarrow 3 という根付き木に辺 1 \rightarrow 3 を書き足したものであると考えられます。


入力例 2

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

出力例 2

6
4
2
0
6
2

Score : 500 points

Problem Statement

There is a rooted tree (see Notes) with N vertices numbered 1 to N. Each of the vertices, except the root, has a directed edge coming from its parent. Note that the root may not be Vertex 1.

Takahashi has added M new directed edges to this graph. Each of these M edges, u \rightarrow v, extends from some vertex u to its descendant v.

You are given the directed graph with N vertices and N-1+M edges after Takahashi added edges. More specifically, you are given N-1+M pairs of integers, (A_1, B_1), ..., (A_{N-1+M}, B_{N-1+M}), which represent that the i-th edge extends from Vertex A_i to Vertex B_i.

Restore the original rooted tree.

Notes

For "tree" and other related terms in graph theory, see the article in Wikipedia, for example.

Constraints

  • 3 \leq N
  • 1 \leq M
  • N + M \leq 10^5
  • 1 \leq A_i, B_i \leq N
  • A_i \neq B_i
  • If i \neq j, (A_i, B_i) \neq (A_j, B_j).
  • The graph in input can be obtained by adding M edges satisfying the condition in the problem statement to a rooted tree with N vertices.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_{N-1+M} B_{N-1+M}

Output

Print N lines. In the i-th line, print 0 if Vertex i is the root of the original tree, and otherwise print the integer representing the parent of Vertex i in the original tree.

Note that it can be shown that the original tree is uniquely determined.


Sample Input 1

3 1
1 2
1 3
2 3

Sample Output 1

0
1
2

The graph in this input is shown below:

It can be seen that this graph is obtained by adding the edge 1 \rightarrow 3 to the rooted tree 1 \rightarrow 2 \rightarrow 3.


Sample Input 2

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

Sample Output 2

6
4
2
0
6
2
E - Weights on Vertices and Edges

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

N 頂点 M 辺の連結な無向グラフがあります。 頂点には 1 から N までの番号が、辺には 1 から M までの番号がついています。 また、各頂点、各辺にはそれぞれ重みが定められています。 頂点 i の重みは X_i です。 辺 i は頂点 A_iB_i を結ぶ辺で、その重みは Y_i です。

グラフから辺を 0 本以上削除して、次の条件が満たされるようにしたいです。

  • 削除されていない任意の辺について、その辺を含む連結成分の頂点の重みの総和が、その辺の重み以上である。

最小で何本の辺を消す必要があるかを求めてください。

制約

  • 1 \leq N \leq 10^5
  • N-1 \leq M \leq 10^5
  • 1 \leq X_i \leq 10^9
  • 1 \leq A_i < B_i \leq N
  • 1 \leq Y_i \leq 10^9
  • (A_i,B_i) \neq (A_j,B_j) (i \neq j)
  • 与えられるグラフは連結である。
  • 入力される値はすべて整数である。

入力

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

N M
X_1 X_2 ... X_N
A_1 B_1 Y_1
A_2 B_2 Y_2
:
A_M B_M Y_M

出力

最小で何本の辺を消す必要があるかを出力せよ。


入力例 1

4 4
2 3 5 7
1 2 7
1 3 9
2 3 12
3 4 18

出力例 1

2

3,4 を削除したとします。 このとき、辺 1 を含む連結成分は、頂点 1,2,3 からなる連結成分であり、頂点の重みの総和は 2+3+5=10 です。 辺 1 の重みは 7 なので、辺 1 については条件を満たしています。 また同様に、辺 2 についても条件を満たしています。 よって、辺を 2 本削除することで条件を満たすグラフが得られます。

辺を 1 本以下削除することによって条件を満たすことはできないので、答えは 2 になります。


入力例 2

6 10
4 4 1 1 1 7
3 5 19
2 5 20
4 5 8
1 6 16
2 3 9
3 6 16
3 4 1
2 6 20
2 4 19
1 2 9

出力例 2

4

入力例 3

10 9
81 16 73 7 2 61 86 38 90 28
6 8 725
3 10 12
1 4 558
4 9 615
5 6 942
8 9 918
2 7 720
4 7 292
7 10 414

出力例 3

8

Score : 800 points

Problem Statement

There is a connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the edges are numbered 1 to M. Also, each of these vertices and edges has a specified weight. Vertex i has a weight of X_i; Edge i has a weight of Y_i and connects Vertex A_i and B_i.

We would like to remove zero or more edges so that the following condition is satisfied:

  • For each edge that is not removed, the sum of the weights of the vertices in the connected component containing that edge, is greater than or equal to the weight of that edge.

Find the minimum number of edges that need to be removed.

Constraints

  • 1 \leq N \leq 10^5
  • N-1 \leq M \leq 10^5
  • 1 \leq X_i \leq 10^9
  • 1 \leq A_i < B_i \leq N
  • 1 \leq Y_i \leq 10^9
  • (A_i,B_i) \neq (A_j,B_j) (i \neq j)
  • The given graph is connected.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
X_1 X_2 ... X_N
A_1 B_1 Y_1
A_2 B_2 Y_2
:
A_M B_M Y_M

Output

Find the minimum number of edges that need to be removed.


Sample Input 1

4 4
2 3 5 7
1 2 7
1 3 9
2 3 12
3 4 18

Sample Output 1

2

Assume that we removed Edge 3 and 4. In this case, the connected component containing Edge 1 contains Vertex 1, 2 and 3, and the sum of the weights of these vertices is 2+3+5=10. The weight of Edge 1 is 7, so the condition is satisfied for Edge 1. Similarly, it can be seen that the condition is also satisfied for Edge 2. Thus, a graph satisfying the condition can be obtained by removing two edges.

The condition cannot be satisfied by removing one or less edges, so the answer is 2.


Sample Input 2

6 10
4 4 1 1 1 7
3 5 19
2 5 20
4 5 8
1 6 16
2 3 9
3 6 16
3 4 1
2 6 20
2 4 19
1 2 9

Sample Output 2

4

Sample Input 3

10 9
81 16 73 7 2 61 86 38 90 28
6 8 725
3 10 12
1 4 558
4 9 615
5 6 942
8 9 918
2 7 720
4 7 292
7 10 414

Sample Output 3

8
F - Jewels

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1200

問題文

N 個の宝石があり、1 から N までの番号がついています。 それぞれの宝石の色は 1 以上 K 以下の整数で表され、宝石 i の色は C_i です。 また、それぞれの宝石には価値が定まっており、宝石 i の価値は V_i です。

すぬけ君はこれらの宝石のうちいくつかを選んで展示したいです。 ただし、選んだ宝石の集合は、次の条件を満たす必要があります。

  • 選ばれたどの宝石についても、同じ色の選ばれた宝石がそれのほかに 1 つ以上存在する。

1 \leq x \leq N を満たすすべての整数 x について、ちょうど x 個の宝石を選ぶことが可能か判定してください。 また可能な場合は、選んだ宝石の価値の総和としてあり得る最大の値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq \lfloor N/2 \rfloor
  • 1 \leq C_i \leq K
  • 1 \leq V_i \leq 10^9
  • どの色についても、その色の宝石が 2 つ以上存在する。
  • 入力される値はすべて整数である。

入力

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

N K
C_1 V_1
C_2 V_2
:
C_N V_N

出力

N 行出力せよ。 i 行目に、ちょうど i 個の宝石を選ぶことが可能な場合は、その場合の選んだ宝石の価値の総和としてあり得る最大の値を出力し、不可能な場合は -1 を出力せよ。


入力例 1

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

出力例 1

-1
9
6
14
15

ちょうど 1 個の宝石を選ぶことはできません。

ちょうど 2 個の宝石を選ぶ場合は、宝石 4,5 を選ぶことで価値の総和を最大化できます。

ちょうど 3 個の宝石を選ぶ場合は、宝石 1,2,3 を選ぶことで価値の総和を最大化できます。

ちょうど 4 個の宝石を選ぶ場合は、宝石 2,3,4,5 を選ぶことで価値の総和を最大化できます。

ちょうど 5 個の宝石を選ぶ場合は、宝石 1,2,3,4,5 を選ぶことで価値の総和を最大化できます。


入力例 2

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

出力例 2

-1
9
12
12
15

入力例 3

8 4
3 2
2 3
4 5
1 7
3 11
4 13
1 17
2 19

出力例 3

-1
24
-1
46
-1
64
-1
77

入力例 4

15 5
3 87
1 25
1 27
3 58
2 85
5 19
5 39
1 58
3 12
4 13
5 54
4 100
2 33
5 13
2 55

出力例 4

-1
145
173
285
318
398
431
491
524
576
609
634
653
666
678

Score : 1200 points

Problem Statement

There are N jewels, numbered 1 to N. The color of these jewels are represented by integers between 1 and K (inclusive), and the color of Jewel i is C_i. Also, these jewels have specified values, and the value of Jewel i is V_i.

Snuke would like to choose some of these jewels to exhibit. Here, the set of the chosen jewels must satisfy the following condition:

  • For each chosen jewel, there is at least one more jewel of the same color that is chosen.

For each integer x such that 1 \leq x \leq N, determine if it is possible to choose exactly x jewels, and if it is possible, find the maximum possible sum of the values of chosen jewels in that case.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq \lfloor N/2 \rfloor
  • 1 \leq C_i \leq K
  • 1 \leq V_i \leq 10^9
  • For each of the colors, there are at least two jewels of that color.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
C_1 V_1
C_2 V_2
:
C_N V_N

Output

Print N lines. In the i-th line, if it is possible to choose exactly i jewels, print the maximum possible sum of the values of chosen jewels in that case, and print -1 otherwise.


Sample Input 1

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

Sample Output 1

-1
9
6
14
15

We cannot choose exactly one jewel.

When choosing exactly two jewels, the total value is maximized when Jewel 4 and 5 are chosen.

When choosing exactly three jewels, the total value is maximized when Jewel 1, 2 and 3 are chosen.

When choosing exactly four jewels, the total value is maximized when Jewel 2, 3, 4 and 5 are chosen.

When choosing exactly five jewels, the total value is maximized when Jewel 1, 2, 3, 4 and 5 are chosen.


Sample Input 2

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

Sample Output 2

-1
9
12
12
15

Sample Input 3

8 4
3 2
2 3
4 5
1 7
3 11
4 13
1 17
2 19

Sample Output 3

-1
24
-1
46
-1
64
-1
77

Sample Input 4

15 5
3 87
1 25
1 27
3 58
2 85
5 19
5 39
1 58
3 12
4 13
5 54
4 100
2 33
5 13
2 55

Sample Output 4

-1
145
173
285
318
398
431
491
524
576
609
634
653
666
678