A - Payment

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

お店で N 円の商品を買います。

1000 円札のみを使って支払いを行う時、お釣りはいくらになりますか?

ただし、必要最小限の枚数の 1000 円札で支払いを行うものとします。

制約

  • 1 \leq N \leq 10000
  • N は整数

入力

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

N

出力

お釣りの金額を整数で出力せよ。


入力例 1

1900

出力例 1

100

1000 円札 2 枚で支払いを行い、お釣りは 100 円になります。


入力例 2

3000

出力例 2

0

ちょうど支払えます。

Score : 100 points

Problem Statement

We will buy a product for N yen (the currency of Japan) at a shop.

If we use only 1000-yen bills to pay the price, how much change will we receive?

Assume we use the minimum number of bills required.

Constraints

  • 1 \leq N \leq 10000
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the amount of change as an integer.


Sample Input 1

1900

Sample Output 1

100

We will use two 1000-yen bills to pay the price and receive 100 yen in change.


Sample Input 2

3000

Sample Output 2

0

We can pay the exact price.

B - Judge Status Summary

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

高橋君は、プログラミングコンテスト AXC002 に参加しており、問題 A にコードを提出しました。

この問題には N 個のテストケースがあります。

各テストケース i (1\leq i \leq N) について、ジャッジ結果を表す文字列 S_i が与えられるので、ジャッジ結果が AC, WA, TLE, RE であったものの個数をそれぞれ求めてください。

出力形式は、出力欄を参照してください。

制約

  • 1 \leq N \leq 10^5
  • S_iAC, WA, TLE, RE のいずれか

入力

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

N
S_1
\vdots
S_N

出力

AC, WA, TLE, RE のテストケースの個数をそれぞれ C_0, C_1, C_2, C_3 としたとき、以下の形式で出力せよ。

AC x C_0
WA x C_1
TLE x C_2
RE x C_3

入力例 1

6
AC
TLE
AC
AC
WA
TLE

出力例 1

AC x 3
WA x 1
TLE x 2
RE x 0

ジャッジ結果が AC であったケースが 3 個、WA であったケースが 1 個、TLE であったケースが 2 個、RE であったケースが 0 個でした。


入力例 2

10
AC
AC
AC
AC
AC
AC
AC
AC
AC
AC

出力例 2

AC x 10
WA x 0
TLE x 0
RE x 0

Score : 200 points

Problem Statement

Takahashi is participating in a programming contest called AXC002, and he has just submitted his code to Problem A.

The problem has N test cases.

For each test case i (1\leq i \leq N), you are given a string S_i representing the verdict for that test case. Find the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively.

See the Output section for the output format.

Constraints

  • 1 \leq N \leq 10^5
  • S_i is AC, WA, TLE, or RE.

Input

Input is given from Standard Input in the following format:

N
S_1
\vdots
S_N

Output

Let C_0, C_1, C_2, and C_3 be the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively. Print the following:

AC x C_0
WA x C_1
TLE x C_2
RE x C_3

Sample Input 1

6
AC
TLE
AC
AC
WA
TLE

Sample Output 1

AC x 3
WA x 1
TLE x 2
RE x 0

We have 3, 1, 2, and 0 test case(s) for which the verdict is AC, WA, TLE, and RE, respectively.


Sample Input 2

10
AC
AC
AC
AC
AC
AC
AC
AC
AC
AC

Sample Output 2

AC x 10
WA x 0
TLE x 0
RE x 0
C - H and V

Time Limit: 1 sec / Memory Limit: 1024 MB

配点 : 300

問題文

HW 列に並ぶマスからなるマス目があります。上から i 行目、左から j 列目 (1 \leq i \leq H, 1 \leq j \leq W) のマスの色は文字 c_{i,j} として与えられ、c_{i,j}. のとき白、# のとき黒です。

次の操作を行うことを考えます。

  • 行を何行か選び (0 行でもよい)、列を何列か選ぶ (0 列でもよい)。そして、選んだ行に含まれるマスと、選んだ列に含まれるマスをすべて赤く塗る。

正の整数 K が与えられます。操作後に黒いマスがちょうど K 個残るような行と列の選び方は何通りでしょうか。ここで、二つの選び方は、一方においてのみ選ばれる行または列が存在するときに異なるとみなされます。

制約

  • 1 \leq H, W \leq 6
  • 1 \leq K \leq HW
  • c_{i,j}. または #

入力

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

H W K
c_{1,1}c_{1,2}...c_{1,W}
c_{2,1}c_{2,2}...c_{2,W}
:
c_{H,1}c_{H,2}...c_{H,W}

出力

条件を満たす行と列の選び方の個数を表す整数を出力せよ。


入力例 1

2 3 2
..#
###

出力例 1

5

以下の 5 通りの選び方が条件を満たします。

  • 1 行目、1 列目
  • 1 行目、2 列目
  • 1 行目、3 列目
  • 1 列目、2 列目
  • 3 列目

入力例 2

2 3 4
..#
###

出力例 2

1

何も選ばないという 1 通りの選び方が条件を満たします。


入力例 3

2 2 3
##
##

出力例 3

0

入力例 4

6 6 8
..##..
.#..#.
#....#
######
#....#
#....#

出力例 4

208

Score : 300 points

Problem Statement

We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is ., and black if c_{i,j} is #.

Consider doing the following operation:

  • Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.

You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.

Constraints

  • 1 \leq H, W \leq 6
  • 1 \leq K \leq HW
  • c_{i,j} is . or #.

Input

Input is given from Standard Input in the following format:

H W K
c_{1,1}c_{1,2}...c_{1,W}
c_{2,1}c_{2,2}...c_{2,W}
:
c_{H,1}c_{H,2}...c_{H,W}

Output

Print an integer representing the number of choices of rows and columns satisfying the condition.


Sample Input 1

2 3 2
..#
###

Sample Output 1

5

Five choices below satisfy the condition.

  • The 1-st row and 1-st column
  • The 1-st row and 2-nd column
  • The 1-st row and 3-rd column
  • The 1-st and 2-nd column
  • The 3-rd column

Sample Input 2

2 3 4
..#
###

Sample Output 2

1

One choice, which is choosing nothing, satisfies the condition.


Sample Input 3

2 2 3
##
##

Sample Output 3

0

Sample Input 4

6 6 8
..##..
.#..#.
#....#
######
#....#
#....#

Sample Output 4

208
D - Chat in a Circle

Time Limit: 2 sec / Memory Limit: 1024 MB

配点: 400

問題文

あなたはオンラインゲーム「ATChat」のチュートリアルを終え、その場に居合わせたプレイヤー N 人で早速とある場所を訪ねることにしました。この N 人には 1 から N の番号が振られており、人 i\ (1 \leq i \leq N)フレンドリーさA_i です。

訪ねる際、N 人は好きな順番で 1 人ずつ到着します。あなたたちは迷子にならないために、既に到着した人たちで環状に並び、新たに到着した人は好きな位置に割り込んで加わるというルールを決めました。

最初に到着した人以外の各人は、割り込んだ位置から到着した時点で「時計回りで最も近い人」と「反時計回りで最も近い人」のフレンドリーさのうち小さい方に等しい 心地よさ を感じます。最初に到着した人の心地よさは 0 です。

N 人が到着する順番や割り込む位置を適切に決めたとき、N 人の心地よさの合計の最大値はいくらになるでしょう?

制約

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

入力

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

N
A_1 A_2 \dots A_N

出力

N 人の心地よさの合計の最大値を出力せよ。


入力例 1

4
2 2 1 3

出力例 1

7

4, 2, 1, 3 がこの順に到着し、図のように輪に割り込むことで、心地よさの合計は 7 になります。

図

心地よさの合計を 7 より大きくすることはできないので、7 が答えになります。


入力例 2

7
1 1 1 1 1 1 1

出力例 2

6

Score: 400 points

Problem Statement

Quickly after finishing the tutorial of the online game ATChat, you have decided to visit a particular place with N-1 players who happen to be there. These N players, including you, are numbered 1 through N, and the friendliness of Player i is A_i.

The N players will arrive at the place one by one in some order. To make sure nobody gets lost, you have set the following rule: players who have already arrived there should form a circle, and a player who has just arrived there should cut into the circle somewhere.

When each player, except the first one to arrive, arrives at the place, the player gets comfort equal to the smaller of the friendliness of the clockwise adjacent player and that of the counter-clockwise adjacent player. The first player to arrive there gets the comfort of 0.

What is the maximum total comfort the N players can get by optimally choosing the order of arrivals and the positions in the circle to cut into?

Constraints

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

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the maximum total comfort the N players can get.


Sample Input 1

4
2 2 1 3

Sample Output 1

7

By arriving at the place in the order Player 4, 2, 1, 3, and cutting into the circle as shown in the figure, they can get the total comfort of 7.

Figure

They cannot get the total comfort greater than 7, so the answer is 7.


Sample Input 2

7
1 1 1 1 1 1 1

Sample Output 2

6
E - Multiplication 4

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 個の整数 A_1,\ldots,A_N が与えられます。

このなかからちょうど K 個の要素を選ぶとき、選んだ要素の積としてありえる最大値を求めてください。

そして、答えを (10^9+7) で割った余りを 0 以上 10^9+6 以下の整数として出力してください。

制約

  • 1 \leq K \leq N \leq 2\times 10^5
  • |A_i| \leq 10^9

入力

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

N K
A_1 \ldots A_N

出力

答えを (10^9+7) で割った余りを、0 以上 10^9+6 以下の整数として出力せよ。


入力例 1

4 2
1 2 -3 -4

出力例 1

12

要素を 2 個選んだときの積としてありえる値は 2,-3,-4,-6,-8,12 なので、最大値は 12 です。


入力例 2

4 3
-1 -2 -3 -4

出力例 2

1000000001

要素を 3 個選んだときの積としてありえる値は -24,-12,-8,-6 なので、最大値は -6 です。

これを (10^9+7) で割った余りである 1000000001 を出力します。


入力例 3

2 1
-1 1000000000

出力例 3

1000000000

要素を 1 個選んだときの積としてありえる値は -1,1000000000 なので、最大値は 1000000000 です。


入力例 4

10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1

出力例 4

999983200

答えを (10^9+7) で割った余りを出力してください。

Score : 500 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

We will choose exactly K of these elements. Find the maximum possible product of the chosen elements.

Then, print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Constraints

  • 1 \leq K \leq N \leq 2\times 10^5
  • |A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 \ldots A_N

Output

Print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).


Sample Input 1

4 2
1 2 -3 -4

Sample Output 1

12

The possible products of the two chosen elements are 2, -3, -4, -6, -8, and 12, so the maximum product is 12.


Sample Input 2

4 3
-1 -2 -3 -4

Sample Output 2

1000000001

The possible products of the three chosen elements are -24, -12, -8, and -6, so the maximum product is -6.

We print this value modulo (10^9+7), that is, 1000000001.


Sample Input 3

2 1
-1 1000000000

Sample Output 3

1000000000

The possible products of the one chosen element are -1 and 1000000000, so the maximum product is 1000000000.


Sample Input 4

10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1

Sample Output 4

999983200

Be sure to print the product modulo (10^9+7).

F - Intervals on Tree

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 頂点 N-1 辺から成る木があり、頂点には 1, 2,\cdots, N の番号が、辺には 1, 2, \cdots, N-1 の番号がついています。辺 i は頂点 u_i, v_i を繋いでいます。

整数 1 \leq L \leq R \leq N に対して関数 f(L, R) を次のように定義します。

  • S を番号が L 以上 R 以下の頂点から成る集合とする。頂点集合 S と、両端が S に属する辺のみから成るような部分グラフの連結成分の個数を f(L, R) で表す。

\sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R) を計算してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N
  • 与えられるグラフは木である
  • 入力は全て整数である

入力

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

N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}

出力

\sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R) を出力せよ。


入力例 1

3
1 3
2 3

出力例 1

7

考えられる L, R の組み合わせは以下の 6 通りがあります。

  • L = 1, R = 1 のとき、S = \{1\} であり、連結成分の個数は 1 です。
  • L = 1, R = 2 のとき、S = \{1, 2\} であり、連結成分の個数は 2 です。
  • L = 1, R = 3 のとき、S = \{1, 2, 3\} であり、辺 1, 2 は両端が S に含まれるので、連結成分の個数は 1 です。
  • L = 2, R = 2 のとき、S = \{2\} であり、連結成分の個数は 1 です。
  • L = 2, R = 3 のとき、S = \{2, 3\} であり、辺 2 は両端が S に含まれるので、連結成分の個数は 1 です。
  • L = 3, R = 3 のとき、S = \{3\} であり、連結成分の個数は 1 です。

これらの和は 7 です。


入力例 2

2
1 2

出力例 2

3

入力例 3

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

出力例 3

113

Score : 600 points

Problem Statement

We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.

For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:

  • Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.

Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N
  • The given graph is a tree.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}

Output

Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).


Sample Input 1

3
1 3
2 3

Sample Output 1

7

We have six possible pairs (L, R) as follows:

  • For L = 1, R = 1, S = \{1\} and we have 1 connected component.
  • For L = 1, R = 2, S = \{1, 2\} and we have 2 connected components.
  • For L = 1, R = 3, S = \{1, 2, 3\} and we have 1 connected component, since S contains both endpoints of each of the edges 1, 2.
  • For L = 2, R = 2, S = \{2\} and we have 1 connected component.
  • For L = 2, R = 3, S = \{2, 3\} and we have 1 connected component, since S contains both endpoints of Edge 2.
  • For L = 3, R = 3, S = \{3\} and we have 1 connected component.

The sum of these is 7.


Sample Input 2

2
1 2

Sample Output 2

3

Sample Input 3

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

Sample Output 3

113