A - Curtain

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

高橋君の家の窓の横方向の長さは A であり、横方向の長さが B のカーテンが 2 枚取り付けられています。(カーテンは縦方向には窓を覆うのに十分な長さがあります。)

窓のうちカーテンで隠されていない部分の横方向の長さが最小になるようにカーテンを閉めます。 このとき、窓のカーテンで隠されていない部分の合計の横方向の長さを求めてください。

制約

  • 1 ≤ A ≤ 100
  • 1 ≤ B ≤ 100
  • A, B は整数である。

入力

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

A B

出力

窓のうちカーテンで隠されていない部分の合計の横方向の長さを出力せよ。


入力例 1

12 4

出力例 1

4

横幅 12 の窓の、例えば両端からの距離が 4 以内の部分が隠されます。カーテンで隠されない部分の横方向の長さは 4 です。


入力例 2

20 15

出力例 2

0

窓がカーテンで完全に隠される場合は 0 と出力してください。


入力例 3

20 30

出力例 3

0

各カーテンが窓の横幅より長い場合があります。

Score : 100 points

Problem Statement

The window of Takahashi's room has a width of A. There are two curtains hung over the window, each of which has a horizontal length of B. (Vertically, the curtains are long enough to cover the whole window.)

We will close the window so as to minimize the total horizontal length of the uncovered part of the window. Find the total horizontal length of the uncovered parts of the window then.

Constraints

  • 1 \leq A \leq 100
  • 1 \leq B \leq 100
  • A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the total horizontal length of the uncovered parts of the window.


Sample Input 1

12 4

Sample Output 1

4

We have a window with a horizontal length of 12, and two curtains, each of length 4, that cover both ends of the window, for example. The uncovered part has a horizontal length of 4.


Sample Input 2

20 15

Sample Output 2

0

If the window is completely covered, print 0.


Sample Input 3

20 30

Sample Output 3

0

Each curtain may be longer than the window.

B - TAKOYAKI FESTIVAL 2019

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

たこ焼きフェスティバル (たこフェス) の季節がやってきました!

今年のたこフェスでは N 個のたこ焼きがふるまわれる予定です。このうち i 個目のたこ焼きのおいしさは d_i です。

ところで、おいしさが xy であるたこ焼きを一緒に食べると、体力が x \times y 回復することが一般に知られています。

たこフェスでふるまわれる N 個のたこ焼きから、2 個を選ぶ方法は \frac{N \times (N - 1)}{2} 通り考えられます。そのそれぞれについて、一緒に食べたときの体力の回復量を求めて、その総和を出力してください。

制約

  • 入力は全て整数である。
  • 2 \leq N \leq 50
  • 0 \leq d_i \leq 100

入力

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

N
d_1 d_2 ... d_N

出力

たこフェスでふるまわれる N 個のたこやきから、2 個を選んで一緒に食べたときの体力の回復量の総和を出力せよ。


入力例 1

3
3 1 2

出力例 1

11

以下の 3 通りの食べ方が考えられます。

  • 1,~2 個目のたこ焼きを選んで一緒に食べる。このとき、体力の回復量は 3 である。
  • 2,~3 個目のたこ焼きを選んで一緒に食べる。このとき、体力の回復量は 2 である。
  • 1,~3 個目のたこ焼きを選んで一緒に食べる。このとき、体力の回復量は 6 である。

体力の回復量の総和は 11 です。


入力例 2

7
5 0 7 8 3 3 2

出力例 2

312

Score : 200 points

Problem Statement

It's now the season of TAKOYAKI FESTIVAL!

This year, N takoyaki (a ball-shaped food with a piece of octopus inside) will be served. The deliciousness of the i-th takoyaki is d_i.

As is commonly known, when you eat two takoyaki of deliciousness x and y together, you restore x \times y health points.

There are \frac{N \times (N - 1)}{2} ways to choose two from the N takoyaki served in the festival. For each of these choices, find the health points restored from eating the two takoyaki, then compute the sum of these \frac{N \times (N - 1)}{2} values.

Constraints

  • All values in input are integers.
  • 2 \leq N \leq 50
  • 0 \leq d_i \leq 100

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 ... d_N

Output

Print the sum of the health points restored from eating two takoyaki over all possible choices of two takoyaki from the N takoyaki served.


Sample Input 1

3
3 1 2

Sample Output 1

11

There are three possible choices:

  • Eat the first and second takoyaki. You will restore 3 health points.
  • Eat the second and third takoyaki. You will restore 2 health points.
  • Eat the first and third takoyaki. You will restore 6 health points.

The sum of these values is 11.


Sample Input 2

7
5 0 7 8 3 3 2

Sample Output 2

312
C - Slimes

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

N 匹のスライムが横一列に並んでいます。これらの色に関する情報が、長さ N の英小文字から成る文字列 S で与えられます。左から i 番目のスライムは、 Si 文字目に対応する色を持っています。

同じ色を持ち隣接するスライムは融合し、色は変わらずに 1 匹のスライムとなります。このとき、融合した後のスライムは、融合する前の各スライムが隣接していた他のスライムと隣接した状態になります。

最終的に存在するスライムは何匹となるでしょうか。

制約

  • 1 ≤ N ≤ 10^5
  • |S| = N
  • S は英小文字から成る

入力

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

N
S

出力

最終的に存在するスライムの数を出力せよ。


入力例 1

10
aabbbbaaca

出力例 1

5

最終的に残るスライムを文字列で表すと、abacaとなります。


入力例 2

5
aaaaa

出力例 2

1

全てのスライムが融合します。


入力例 3

20
xxzaffeeeeddfkkkkllq

出力例 3

10

Score : 300 points

Problem Statement

There are N slimes lining up from left to right. The colors of these slimes will be given as a string S of length N consisting of lowercase English letters. The i-th slime from the left has the color that corresponds to the i-th character of S.

Adjacent slimes with the same color will fuse into one larger slime without changing the color. If there were a slime adjacent to this group of slimes before fusion, that slime is now adjacent to the new larger slime.

Ultimately, how many slimes will be there?

Constraints

  • 1 \leq N \leq 10^5
  • |S| = N
  • S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the final number of slimes.


Sample Input 1

10
aabbbbaaca

Sample Output 1

5

Ultimately, these slimes will fuse into abaca.


Sample Input 2

5
aaaaa

Sample Output 2

1

All the slimes will fuse into one.


Sample Input 3

20
xxzaffeeeeddfkkkkllq

Sample Output 3

10
D - Triangles

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

高橋君は、互いに区別出来る N 本の棒を持っています。i 本目の棒の長さは L_i です。

高橋君は、これらのうち 3 本の棒を使って三角形を作ろうとしています。このとき、棒の長さを a, b, c として、以下の条件がすべて成り立たなければなりません。

  • a < b + c
  • b < c + a
  • c < a + b

作れる三角形は何種類あるでしょうか。ただし、2 つの三角形は、そのうち一方にのみ使われている棒が存在するときに異なるとします。

制約

  • 入力は全て整数
  • 3 ≤ N ≤ 2 \times 10^3
  • 1 \leq L_i \leq 10^3

入力

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

N
L_1 L_2 ... L_N

出力

作れる三角形が何種類あるかを出力せよ。


入力例 1

4
3 4 2 1

出力例 1

1

作れる三角形は、123 番目の棒から成る三角形のみです。


入力例 2

3
1 1000 1

出力例 2

0

作れる三角形はありません。


入力例 3

7
218 786 704 233 645 728 389

出力例 3

23

Score : 400 points

Problem Statement

Takahashi has N sticks that are distinguishable from each other. The length of the i-th stick is L_i.

He is going to form a triangle using three of these sticks. Let a, b, and c be the lengths of the three sticks used. Here, all of the following conditions must be satisfied:

  • a < b + c
  • b < c + a
  • c < a + b

How many different triangles can be formed? Two triangles are considered different when there is a stick used in only one of them.

Constraints

  • All values in input are integers.
  • 3 \leq N \leq 2 \times 10^3
  • 1 \leq L_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Constraints

Print the number of different triangles that can be formed.


Sample Input 1

4
3 4 2 1

Sample Output 1

1

Only one triangle can be formed: the triangle formed by the first, second, and third sticks.


Sample Input 2

3
1 1000 1

Sample Output 2

0

No triangles can be formed.


Sample Input 3

7
218 786 704 233 645 728 389

Sample Output 3

23
E - Travel by Car

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

1 から N までの番号がついた N 個の町と M 本の道があります。i 本目の道は町 A_i と町 B_i を双方向に結び、その長さは C_i です。

高橋君はこれらの町の間を、道を車で通行することで移動します。車の燃料タンクの容量は L リットルであり、距離 1 を移動する度に燃料を 1 リットル消費します。移動中に町を訪れた場合、燃料をタンクが一杯になるまで補給することが出来ます (補給しないという選択も可能です)。道の途中で燃料が尽きるような移動は出来ません。

以下の Q 個のクエリに答えてください。

  • はじめ、車の燃料タンクは一杯です。町 s_i から町 t_i へ移動するとき、最小で何回途中で燃料を補給する必要があるかを答えてください。町 t_i まで移動出来ない場合は −1 を出力してください。

制約

  • 入力は全て整数
  • 2 ≤ N ≤ 300
  • 0 ≤ M ≤ \frac{N(N-1)}{2}
  • 1 \leq L \leq 10^9
  • 1 ≤ A_i, B_i ≤ N
  • A_i \neq B_i
  • \left(A_i, B_i\right) \neq \left(A_j, B_j\right) ( i \neq j のとき)
  • \left(A_i, B_i\right) \neq \left(B_j, A_j\right) ( i \neq j のとき)
  • 1 \leq C_i \leq 10^9
  • 1 ≤ Q ≤ N\left(N-1\right)
  • 1 ≤ s_i, t_i ≤ N
  • s_i \neq t_i
  • \left(s_i, t_i\right) \neq \left(s_j, t_j\right) ( i \neq j のとき)

入力

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

N M L
A_1 B_1 C_1
:
A_M B_M C_M
Q
s_1 t_1
:
s_Q t_Q

出力

Q 行出力せよ。

i 行目には、町 s_i から町 t_i へ移動するとき、最小で何回燃料を補給する必要があるかを出力せよ。町 t_i まで移動出来ない場合は −1 を出力せよ。


入力例 1

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

出力例 1

0
1

3 から町 2 へ移動するときは、 2 本目の道を使えば、途中で燃料を補給することなく町 2 へ到着することが出来ます。

1 から町 3 へ移動するときは、まず 1 本目の道を使って町 2 へ移動し、燃料をタンク一杯まで補給し、 2 本目の道を使うことにより、町 3 へ到着することが出来ます。


入力例 2

4 0 1
1
2 1

出力例 2

-1

道が無いこともあります。


入力例 3

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

出力例 3

0
0
1
2
0
0
1
2
0
0
0
1
1
1
0
0
2
2
1
0

Score : 500 points

Problem Statement

There are N towns numbered 1 to N and M roads. The i-th road connects Town A_i and Town B_i bidirectionally and has a length of C_i.

Takahashi will travel between these towns by car, passing through these roads. The fuel tank of his car can contain at most L liters of fuel, and one liter of fuel is consumed for each unit distance traveled. When visiting a town while traveling, he can full the tank (or choose not to do so). Travel that results in the tank becoming empty halfway on the road cannot be done.

Process the following Q queries:

  • The tank is now full. Find the minimum number of times he needs to full his tank while traveling from Town s_i to Town t_i. If Town t_i is unreachable, print -1.

Constraints

  • All values in input are integers.
  • 2 \leq N \leq 300
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq L \leq 10^9
  • 1 \leq A_i, B_i \leq N
  • A_i \neq B_i
  • \left(A_i, B_i\right) \neq \left(A_j, B_j\right) (if i \neq j)
  • \left(A_i, B_i\right) \neq \left(B_j, A_j\right) (if i \neq j)
  • 1 \leq C_i \leq 10^9
  • 1 \leq Q \leq N\left(N-1\right)
  • 1 \leq s_i, t_i \leq N
  • s_i \neq t_i
  • \left(s_i, t_i\right) \neq \left(s_j, t_j\right) (if i \neq j)

Input

Input is given from Standard Input in the following format:

N M L
A_1 B_1 C_1
:
A_M B_M C_M
Q
s_1 t_1
:
s_Q t_Q

Output

Print Q lines.

The i-th line should contain the minimum number of times the tank needs to be fulled while traveling from Town s_i to Town t_i. If Town t_i is unreachable, the line should contain -1 instead.


Sample Input 1

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

Sample Output 1

0
1

To travel from Town 3 to Town 2, we can use the second road to reach Town 2 without fueling the tank on the way.

To travel from Town 1 to Town 3, we can first use the first road to get to Town 2, full the tank, and use the second road to reach Town 3.


Sample Input 2

4 0 1
1
2 1

Sample Output 2

-1

There may be no road at all.


Sample Input 3

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

Sample Output 3

0
0
1
2
0
0
1
2
0
0
0
1
1
1
0
0
2
2
1
0
F - Distinct Numbers

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

高橋くんは N 枚のカードを持っています。 i 番目のカードには整数 A_i が書かれています。

高橋くんは整数 K を選びます。そして、以下の操作を何度か繰り返します。

  • 書かれている整数が互いに異なるちょうど K 枚のカードを選び、食べる(食べたカードは消滅する)

K = 1,2, \ldots, N のそれぞれに対して、操作を行える最大の回数を求めてください。

制約

  • 1 \le N \le 3 \times 10^5
  • 1 \le A_i \le N
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

N 個の整数を出力せよ。 t (1 \le t \le N) 個目には、K=t のときの答えを出力せよ。


入力例 1

3
2 1 2

出力例 1

3
1
0

K = 1 のとき、操作を以下のように行うことができます。

  • 1 枚目のカードを選び、食べる
  • 2 枚目のカードを選び、食べる
  • 3 枚目のカードを選び、食べる

また、K = 2 のとき、操作を以下のように行うことができます。

  • 1 枚目のカードと 2 枚目のカードを選び、食べる

K = 3 のときは、操作を行うことができません。1 枚目のカードと 3 枚目のカードを同時に選べないことに注意してください。


入力例 2

5
1 2 3 4 5

出力例 2

5
2
1
1
1

入力例 3

4
1 3 3 3

出力例 3

4
1
0
0

Score : 600 points

Problem Statement

Takahashi has N cards. The i-th of these cards has an integer A_i written on it.

Takahashi will choose an integer K, and then repeat the following operation some number of times:

  • Choose exactly K cards such that the integers written on them are all different, and eat those cards. (The eaten cards disappear.)

For each K = 1,2, \ldots, N, find the maximum number of times Takahashi can do the operation.

Constraints

  • 1 \le N \le 3 \times 10^5
  • 1 \le A_i \le N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

Print N integers. The t-th (1 \le t \le N) of them should be the answer for the case K=t.


Sample Input 1

3
2 1 2

Sample Output 1

3
1
0

For K = 1, we can do the operation as follows:

  • Choose the first card to eat.
  • Choose the second card to eat.
  • Choose the third card to eat.

For K = 2, we can do the operation as follows:

  • Choose the first and second cards to eat.

For K = 3, we cannot do the operation at all. Note that we cannot choose the first and third cards at the same time.


Sample Input 2

5
1 2 3 4 5

Sample Output 2

5
2
1
1
1

Sample Input 3

4
1 3 3 3

Sample Output 3

4
1
0
0