E - Various Kagamimochi

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

N 個の餅が小さいほうから順に並んでいます。 i 番目 (1\leq i\leq N) の餅の大きさは A _ i です。

2 つの餅 A,B の大きさをそれぞれ a,b としたとき、ab の半分以下であるとき、かつそのときに限り、餅 A を餅 B の上に乗せて鏡餅を 1 つ作ることができます。

N 個の餅から 2 つの餅を選び、一方をもう一方の上に乗せることで鏡餅を 1 つ作ります。

何種類の鏡餅を作ることができるか求めてください。

ただし、鏡餅を構成する餅の大きさが同じでも、少なくとも一方が異なる餅であれば別の種類の鏡餅として数えます。

制約

  • 2\leq N\leq 5\times 10 ^ 5
  • 1\leq A _ i\leq 10 ^ 9\ (1\leq i\leq N)
  • A _ i\leq A _ {i+1}\ (1\leq i\lt N)
  • 入力はすべて整数

入力

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

N
A _ 1 A _ 2 \cdots A _ N

出力

作ることができる鏡餅の種類数を出力せよ。


入力例 1

6
2 3 4 4 7 10

出力例 1

8

与えられた餅の大きさは以下のようになっています。

このとき、以下の 8 種類の鏡餅を作ることができます。

大きさ 4 の餅の上に大きさ 2 の餅を乗せた鏡餅や、大きさ 10 の餅の上に大きさ 4 の餅を乗せた鏡餅は 2 種類あることに注意してください。


入力例 2

3
387 388 389

出力例 2

0

鏡餅を 1 種類も作ることができない場合もあります。


入力例 3

32
1 2 4 5 8 10 12 16 19 25 33 40 50 64 87 101 149 175 202 211 278 314 355 405 412 420 442 481 512 582 600 641

出力例 3

388

Score : 300 points

Problem Statement

There are N mochi (rice cakes) arranged in ascending order of size. The size of the i-th mochi (1 \leq i \leq N) is A_i.

Given two mochi A and B, with sizes a and b respectively, you can make one kagamimochi (a stacked rice cake) by placing mochi A on top of mochi B if and only if a is at most half of b.

You choose two mochi out of the N mochi, and place one on top of the other to form one kagamimochi.

Find how many different kinds of kagamimochi can be made.

Two kagamimochi are distinguished if at least one of the mochi is different, even if the sizes of the mochi are the same.

Constraints

  • 2 \leq N \leq 5 \times 10^5
  • 1 \leq A_i \leq 10^9 \ (1 \leq i \leq N)
  • A_i \leq A_{i+1} \ (1 \leq i < N)
  • All input values are integers.

Input

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

N
A_1 A_2 \cdots A_N

Output

Print the number of different kinds of kagamimochi that can be made.


Sample Input 1

6
2 3 4 4 7 10

Sample Output 1

8

The sizes of the given mochi are as follows:

In this case, you can make the following eight kinds of kagamimochi:

Note that there are two kinds of kagamimochi where a mochi of size 4 is topped by a mochi of size 2, and two kinds where a mochi of size 10 is topped by a mochi of size 4.


Sample Input 2

3
387 388 389

Sample Output 2

0

It is possible that you cannot make any kagamimochi.


Sample Input 3

32
1 2 4 5 8 10 12 16 19 25 33 40 50 64 87 101 149 175 202 211 278 314 355 405 412 420 442 481 512 582 600 641

Sample Output 3

388
F - Buy Balls

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

N 個の黒色のボールと M 個の白色のボールがあります。
ボールにはそれぞれ価値がつけられており、i\ (1\leq i\leq N) 個目の黒色のボールの価値は B_ij\ (1\leq j\leq M) 個目の白色のボールの価値は W_j です。

黒色のボールの個数が白色のボールの個数以上になるようにボールを 0 個以上選ぶとき、選んだボールの価値の総和としてありうる最大値を求めてください。

制約

  • 1\leq N,M\leq 2\times 10^5
  • -10^9\leq B_i,W_j\leq 10^9
  • 入力は全て整数

入力

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

N M
B_1 B_2 \ldots B_N
W_1 W_2 \ldots W_M

出力

答えを出力せよ。


入力例 1

4 3
8 5 -1 3
3 -2 -4

出力例 1

19

1,2,4 個目の黒色のボールと 1 個目の白色のボールを選ぶとき、選んだボールの価値の総和は 8+5+3+3=19 となりこれが最大です。


入力例 2

4 3
5 -10 -2 -5
8 1 4

出力例 2

15

1,3 個目の黒色のボールと 1,3 個目の白色のボールを選ぶとき、選んだボールの価値の総和は 5+(-2)+8+4=15 となりこれが最大です。


入力例 3

3 5
-36 -33 -31
12 12 28 24 27

出力例 3

0

ボールを 1 つも選ばないことも可能です。

Score : 300 points

Problem Statement

There are N black balls and M white balls.
Each ball has a value. The value of the i-th black ball (1 \le i \le N) is B_i, and the value of the j-th white ball (1 \le j \le M) is W_j.

Choose zero or more balls so that the number of black balls chosen is at least the number of white balls chosen. Among all such choices, find the maximum possible sum of the values of the chosen balls.

Constraints

  • 1 \leq N,M \leq 2\times 10^5
  • -10^9 \leq B_i, W_j \leq 10^9
  • All input values are integers.

Input

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

N M
B_1 B_2 \ldots B_N
W_1 W_2 \ldots W_M

Output

Print the answer.


Sample Input 1

4 3
8 5 -1 3
3 -2 -4

Sample Output 1

19

If you choose the 1st, 2nd, and 4th black balls, and the 1st white ball, the sum of their values is 8+5+3+3=19, which is the maximum.


Sample Input 2

4 3
5 -10 -2 -5
8 1 4

Sample Output 2

15

If you choose the 1st and 3rd black balls, and the 1st and 3rd white balls, the sum of their values is 5+(-2)+8+4=15, which is the maximum.


Sample Input 3

3 5
-36 -33 -31
12 12 28 24 27

Sample Output 3

0

It is possible to choose no balls.

G - Synchronized Players

実行時間制限: 4 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

NN 列のグリッドがあり、各マスは空きマスか障害物のあるマスのいずれかです。グリッドの上から i 行目、左から j 列目のマスを (i, j) と表記します。

また、2 人のプレイヤーがグリッド上の相異なる空きマス上におり、各マスの情報は N 個の長さ N の文字列 S_1, S_2, \ldots, S_N として以下の形式で与えられます。

  • S_ij 文字目が P であるとき、(i, j) は空きマスであり、プレイヤーがいる

  • S_ij 文字目が . であるとき、(i, j) は空きマスであり、プレイヤーがいない

  • S_ij 文字目が # であるとき、(i, j) は障害物のあるマスである

以下の操作を繰り返し 2 人のプレイヤーを同じマスに集めるために必要な操作回数の最小値を求めてください。ただし、操作の繰り返しにより 2 人のプレイヤーを同じマスに集めることができない場合には -1 を出力してください。

  • 上下左右のいずれかの方向を決める。そして各プレイヤーはともにその方向に隣接するマスへの移動を試みる。各プレイヤーは移動先のマスが存在し、かつ空きマスであるならば移動し、そうでないならば移動しない。

制約

  • N2 以上 60 以下の整数
  • S_i は長さ NP, ., # からなる文字列
  • S_ij 文字目が P であるような組 (i, j) の個数はちょうど 2

入力

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

N
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

5
....#
#..#.
.P...
..P..
....#

出力例 1

3

はじめに (3, 2) にいるプレイヤーをプレイヤー 1(4, 3) にいるプレイヤーをプレイヤー 2 とします。

例えば以下のようにすることで、3 回の操作で 2 人のプレイヤーが同じマスに集まります。

  • 左を選択する。プレイヤー 1(3, 1) に移動し、プレイヤー 2(4, 2) に移動する。

  • 上を選択する。プレイヤー 1 は移動せず、プレイヤー 2(3, 2) に移動する。

  • 左を選択する。プレイヤー 1 は移動せず、プレイヤー 2(3, 1) に移動する。


入力例 2

2
P#
#P

出力例 2

-1

入力例 3

10
..........
..........
..........
..........
....P.....
.....P....
..........
..........
..........
..........

出力例 3

10

Score: 400 points

Problem Statement

There is an N \times N grid, where each cell is either empty or contains an obstacle. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.

There are also two players on distinct empty cells of the grid. The information about each cell is given as N strings S_1, S_2, \ldots, S_N of length N, in the following format:

  • If the j-th character of S_i is P, then (i, j) is an empty cell with a player on it.

  • If the j-th character of S_i is ., then (i, j) is an empty cell without a player.

  • If the j-th character of S_i is #, then (i, j) contains an obstacle.

Find the minimum number of moves required to bring the two players to the same cell by repeating the following operation. If it is impossible to bring the two players to the same cell by repeating the operation, print -1.

  • Choose one of the four directions: up, down, left, or right. Then, each player attempts to move to the adjacent cell in that direction. Each player moves if the destination cell exists and is empty, and does not move otherwise.

Constraints

  • N is an integer between 2 and 60, inclusive.
  • S_i is a string of length N consisting of P, ., and #.
  • There are exactly two pairs (i, j) where the j-th character of S_i is P.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

5
....#
#..#.
.P...
..P..
....#

Sample Output 1

3

Let us call the player starting at (3, 2) Player 1 and the player starting at (4, 3) Player 2.

For example, doing the following brings the two players to the same cell in three moves:

  • Choose left. Player 1 moves to (3, 1), and Player 2 moves to (4, 2).

  • Choose up. Player 1 does not move, and Player 2 moves to (3, 2).

  • Choose left. Player 1 does not move, and Player 2 moves to (3, 1).


Sample Input 2

2
P#
#P

Sample Output 2

-1

Sample Input 3

10
..........
..........
..........
..........
....P.....
.....P....
..........
..........
..........
..........

Sample Output 3

10
H - Road Reduction

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 500

問題文

AtCoder 王国には都市 1,2,\ldots,NN 個の都市と、道路 1,2,\ldots,MM 本の道路があります。
道路 i は都市 A_iB_i を双方向に結び、距離は C_i です。
どの都市間もいくつかの道路を通って行き来することができます。

財政難である王国は、どの都市間もいくつかの道路を通って行き来できるという条件を満たすように N-1 本の道路を保守し、それ以外の道路を廃道にすることにしました。

保守する道路のみを通って都市 1 から都市 i へ移動するときの距離を d_i とするとき、保守する道路の選び方であって、d_2+d_3+\ldots+d_N を最小化するようなものを 1 つ出力してください。

制約

  • 2 \leq N \leq 2\times 10^5
  • N-1 \leq M \leq 2\times 10^5
  • 1 \leq A_i < B_i \leq N
  • i\neq j のとき、(A_i,B_i)\neq(A_j,B_j)
  • 1\leq C_i \leq 10^9
  • どの都市間もいくつかの道路を通って行き来することができる
  • 入力に含まれる値は全て整数である

入力

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

N M
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M

出力

保守するような道路の番号を空白区切りで出力せよ。出力の順序は問わない。
答えが複数存在する場合、どれを出力しても正解とみなされる。


入力例 1

3 3
1 2 1
2 3 2
1 3 10

出力例 1

1 2

保守する道路の選び方と d_i の値は次のようになります。

  • 道路 1,2 を保守するとき、d_2=1, d_3=3
  • 道路 1,3 を保守するとき、d_2=1, d_3=10
  • 道路 2,3 を保守するとき、d_2=12, d_3=10

よって、道路 1,2 を保守するときに d_2+d_3 が最小になります。


入力例 2

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

出力例 2

3 1 2

Score : 500 points

Problem Statement

The Kingdom of AtCoder has N cities called City 1,2,\ldots,N and M roads called Road 1,2,\ldots,M.
Road i connects Cities A_i and B_i bidirectionally and has a length of C_i.
One can travel between any two cities using some roads.

Under financial difficulties, the kingdom has decided to maintain only N-1 roads so that one can still travel between any two cities using those roads and abandon the rest.

Let d_i be the total length of the roads one must use when going from City 1 to City i using only maintained roads. Print a choice of roads to maintain that minimizes d_2+d_3+\ldots+d_N.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • N-1 \leq M \leq 2\times 10^5
  • 1 \leq A_i < B_i \leq N
  • (A_i,B_i)\neq(A_j,B_j) if i\neq j.
  • 1\leq C_i \leq 10^9
  • One can travel between any two cities using some roads.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M

Output

Print the indices of roads to maintain, in arbitrary order, with spaces in between.
If multiple solutions exist, you may print any of them.


Sample Input 1

3 3
1 2 1
2 3 2
1 3 10

Sample Output 1

1 2

Here are the possible choices of roads to maintain and the corresponding values of d_i.

  • Maintain Road 1 and 2: d_2=1, d_3=3.
  • Maintain Road 1 and 3: d_2=1, d_3=10.
  • Maintain Road 2 and 3: d_2=12, d_3=10.

Thus, maintaining Road 1 and 2 minimizes d_2+d_3.


Sample Input 2

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

Sample Output 2

3 1 2
I - Yet Another Grid Task

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 525

問題文

N \times M のグリッドがあります。
このグリッドの上から i 行目、左から j 列目のマスを (i,j) と書きます。
このグリッドの各マスは 白 か 黒 であり、その情報は N 個の長さ M の文字列 S_1,S_2,\dots,S_N として与えられます。

  • もし S_ij 文字目が . なら、マス (i,j) は 白 である。
  • もし S_ij 文字目が # なら、マス (i,j) は 黒 である。

以下の条件を満たすグリッドを 美しい グリッドと呼びます。

  • 全ての 1 \le i \le N, 1 \le j \le M を満たす整数組 (i,j) について、マス (i,j) が 黒 であれば、その下と右下のマスも (存在すれば) 黒 である。
  • 厳密には、以下の条件を全て満たす。
    • マス (i,j) が 黒 でありマス (i+1,j) が存在するなら、マス (i+1,j) も 黒 である。
    • マス (i,j) が 黒 でありマス (i+1,j+1) が存在するなら、マス (i+1,j+1) も 黒 である。

高橋くんは、 白 のマスを 0 個以上何個でも 黒 に塗ることができ、この操作によってグリッドを美しくしようとしています。
高橋くんが作ることのできる美しいグリッドの種類数を 998244353 で割った余りを求めてください。
但し、ある 2 つのグリッドが異なるとは、両者で色が異なるマスが存在することを指します。

制約

  • 1 \le N,M \le 2000
  • S_i.# からなる長さ M の文字列

入力

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

N M
S_1
S_2
\vdots
S_N

出力

答えを整数として出力せよ。


入力例 1

2 2
.#
..

出力例 1

3

作ることのできる美しいグリッドは以下の 3 種類です。

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

入力例 2

5 5
....#
...#.
..#..
.#.#.
#...#

出力例 2

92

入力例 3

25 25
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................

出力例 3

604936632

998244353 で割った余りを求めてください。

Score : 525 points

Problem Statement

There is an N \times M grid and a player standing on it.
Let (i,j) denote the square at the i-th row from the top and j-th column from the left of this grid.
Each square of this grid is black or white, which is represented by N strings S_1,S_2,\dots,S_N of length M as follows:

  • if the j-th character of S_i is ., square (i,j) is white;
  • if the j-th character of S_i is #, square (i,j) is black.

The grid is said to be beautiful when the following condition is satisfied.

  • For every pair of integers (i,j) such that 1 \le i \le N, 1 \le j \le M, if square (i,j) is black, the square under (i,j) and the square to the immediate lower right of (i,j) are also black (if they exist).
  • Formally, all of the following are satisfied.
    • If square (i,j) is black and square (i+1,j) exists, square (i+1,j) is also black.
    • If square (i,j) is black and square (i+1,j+1) exists, square (i+1,j+1) is also black.

Takahashi can paint zero or more white squares black, and he will do so to make the grid beautiful.
Find the number of different beautiful grids he can make, modulo 998244353.
Two grids are considered different when there is a square that has different colors in those two grids.

Constraints

  • 1 \le N,M \le 2000
  • S_i is a string of length M consisting of . and #.

Input

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

N M
S_1
S_2
\vdots
S_N

Output

Print the answer as an integer.


Sample Input 1

2 2
.#
..

Sample Output 1

3

He can make the following three different beautiful grids:

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

Sample Input 2

5 5
....#
...#.
..#..
.#.#.
#...#

Sample Output 2

92

Sample Input 3

25 25
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................

Sample Output 3

604936632

Find the count modulo 998244353.