E - Sum of Product

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。

\displaystyle \sum_{1\leq i< j\leq N} A_iA_j の値を求めてください。

制約

  • 2\leq N \leq 3\times 10^5
  • 1\leq A_i \leq 10^4
  • 入力は全て整数

入力

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

N
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

3
4 2 3

出力例 1

26

\displaystyle \sum_{1\leq i< j\leq N} A_iA_j=A_1A_2+A_1A_3+A_2A_3=4\cdot 2+4\cdot 3+2\cdot 3=26 です。


入力例 2

2
9 45

出力例 2

405

入力例 3

10
7781 8803 8630 9065 8831 9182 8593 7660 7548 8617

出力例 3

3227530139

Score : 300 points

Problem Statement

You are given a length-N integer sequence A = (A_1, A_2, \dots, A_N).

Compute the value of \displaystyle \sum_{1\leq i< j\leq N} A_iA_j.

Constraints

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

Input

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

N
A_1 A_2 \dots A_N

Output

Output the answer.


Sample Input 1

3
4 2 3

Sample Output 1

26

We have \displaystyle \sum_{1\leq i< j\leq N} A_iA_j=A_1A_2+A_1A_3+A_2A_3=4\cdot 2+4\cdot 3+2\cdot 3=26.


Sample Input 2

2
9 45

Sample Output 2

405

Sample Input 3

10
7781 8803 8630 9065 8831 9182 8593 7660 7548 8617

Sample Output 3

3227530139
F - Sum = 0

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

N 個の整数の組 (L_1,R_1),(L_2,R_2),\ldots,(L_N,R_N) が与えられます。

以下の条件を満たす長さ N の整数列 X=(X_1,X_2,\ldots,X_N) が存在するか判定し、存在するならば一つ出力してください。

  • i=1,2,\ldots,N に対して L_i\leq X_i\leq R_i
  • \displaystyle \sum_{i=1}^N X_i=0

制約

  • 1\leq N\leq 2\times 10^5
  • -10^9\leq L_i\leq R_i\leq 10^9
  • 入力は全て整数

入力

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

出力

存在しない場合は No を出力せよ。存在する場合は条件を満たす整数列 X を以下の形式で出力せよ。

Yes
X_1 X_2 \ldots X_N

答えが複数存在する場合、どれを出力しても正解とみなされる。


入力例 1

3
3 5
-4 1
-2 3

出力例 1

Yes
4 -3 -1

数列 X=(4,-3,-1) は問題の条件をすべて満たします。ほかにも (3,-3,0)(5,-4,-1) などが条件を満たします。


入力例 2

3
1 2
1 2
1 2

出力例 2

No

条件を満たす整数列 X は存在しません。


入力例 3

6
-87 12
-60 -54
2 38
-76 6
87 96
-17 38

出力例 3

Yes
-66 -57 31 -6 89 9

Score : 350 points

Problem Statement

You are given N pairs of integers (L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N).

Determine whether there exists a sequence of N integers X = (X_1, X_2, \ldots, X_N) that satisfies the following conditions, and print one such sequence if it exists.

  • L_i \leq X_i \leq R_i for each i = 1, 2, \ldots, N.
  • \displaystyle \sum_{i=1}^N X_i = 0.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • -10^9 \leq L_i \leq R_i \leq 10^9
  • All input values are integers.

Input

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

Output

If no solution exists, print No. Otherwise, print an integer sequence X that satisfies the conditions in the following format:

Yes
X_1 X_2 \ldots X_N

If multiple solutions exist, any of them will be considered correct.


Sample Input 1

3
3 5
-4 1
-2 3

Sample Output 1

Yes
4 -3 -1

The sequence X = (4, -3, -1) satisfies all the conditions. Other valid sequences include (3, -3, 0) and (5, -4, -1).


Sample Input 2

3
1 2
1 2
1 2

Sample Output 2

No

No sequence X satisfies the conditions.


Sample Input 3

6
-87 12
-60 -54
2 38
-76 6
87 96
-17 38

Sample Output 3

Yes
-66 -57 31 -6 89 9
G - Grid Ice Floor

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

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 行目、 N 行目、 1 列目、 M 列目の全てのマス ) は 岩 です。

最初、プレイヤーはマス (2,2) の上で停止しています。このマスは 氷 です。
プレイヤーは以下の行動を 0 度以上何度でも行うことができます。

  • まず、プレイヤーは上下左右の移動方向を指定する。
  • その後、プレイヤーは岩のマスにぶつかるまでその方向に移動する。厳密には以下の通りである。
    • もし移動方向に隣接するマスが 氷 なら、そのマスに移動し、同じ方向に移動を継続する。
    • もし移動方向に隣接するマスが 岩 なら、今いるマスに留まり、移動を終了する。

プレイヤーが触れる (通過または上で停止する) ことができる 氷 の数を求めてください。

制約

  • 3 \le N,M \le 200
  • S_i#. からなる長さ M の文字列
  • i=1 または i=N または j=1 または j=M であるとき、マス (i,j) は 岩
  • マス (2,2) は 氷

入力

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

N M
S_1
S_2
\vdots
S_N

出力

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


入力例 1

6 6
######
#....#
#.#..#
#..#.#
#....#
######

出力例 1

12

例えばマス (5,5) には以下のように移動することで上で停止することができます。

  • (2,2) \rightarrow (5,2) \rightarrow (5,5)

例えばマス (2,4) には以下のように移動することで通過することができます。

  • (2,2) \rightarrow (2,5) の移動中に (2,4) を通過する。

例えばマス (3,4) は通過することも上で停止することもできません。


入力例 2

21 25
#########################
#..............###...####
#..............#..#...###
#........###...#...#...##
#........#..#..#........#
#...##...#..#..#...#....#
#..#..#..###...#..#.....#
#..#..#..#..#..###......#
#..####..#..#...........#
#..#..#..###............#
#..#..#.................#
#........##.............#
#.......#..#............#
#..........#....#.......#
#........###...##....#..#
#..........#..#.#...##..#
#.......#..#....#..#.#..#
##.......##.....#....#..#
###.............#....#..#
####.................#..#
#########################

出力例 2

215

Score : 400 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 ice or rock, 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 ice;
  • if the j-th character of S_i is #, square (i,j) is rock.

The outer periphery of this grid (all squares in the 1-st row, N-th row, 1-st column, M-th column) is rock.

Initially, the player rests on the square (2,2), which is ice.
The player can make the following move zero or more times.

  • First, specify the direction of movement: up, down, left, or right.
  • Then, keep moving in that direction until the player bumps against a rock. Formally, keep doing the following:
    • if the next square in the direction of movement is ice, go to that square and keep moving;
    • if the next square in the direction of movement is rock, stay in the current square and stop moving.

Find the number of ice squares the player can touch (pass or rest on).

Constraints

  • 3 \le N,M \le 200
  • S_i is a string of length M consisting of # and ..
  • Square (i, j) is rock if i=1, i=N, j=1, or j=M.
  • Square (2,2) is ice.

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

6 6
######
#....#
#.#..#
#..#.#
#....#
######

Sample Output 1

12

For instance, the player can rest on (5,5) by moving as follows:

  • (2,2) \rightarrow (5,2) \rightarrow (5,5).

The player can pass (2,4) by moving as follows:

  • (2,2) \rightarrow (2,5), passing (2,4) in the process.

The player cannot pass or rest on (3,4).


Sample Input 2

21 25
#########################
#..............###...####
#..............#..#...###
#........###...#...#...##
#........#..#..#........#
#...##...#..#..#...#....#
#..#..#..###...#..#.....#
#..#..#..#..#..###......#
#..####..#..#...........#
#..#..#..###............#
#..#..#.................#
#........##.............#
#.......#..#............#
#..........#....#.......#
#........###...##....#..#
#..........#..#.#...##..#
#.......#..#....#..#.#..#
##.......##.....#....#..#
###.............#....#..#
####.................#..#
#########################

Sample Output 2

215
H - Subarray Sum Divisibility

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

長さ N の整数列 A = (A_1, A_2, \ldots, A_N) が与えられます。

あなたの目的は、以下の操作を繰り返し行うことにより、A のすべての長さ L の連続部分列についてその総和が M の倍数であるようにすることです。

  • 1 \leq i \leq N なる整数 i を選び、A_i の値を 1 増やす。

目的を達成するまでの操作回数として考えられる最小値を求めてください。

制約

  • 1 \leq N, M \leq 500
  • 1 \leq L \leq N
  • 0 \leq A_i < M
  • 入力される値はすべて整数

入力

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

N M L
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

4 5 3
4 2 1 3

出力例 1

4

i = 2 を選ぶ操作を 1 回、i = 3 を選ぶ操作を 2 回、i = 4 を選ぶ操作を 1 回行うことで合計 4 回の操作で A = (4, 3, 3, 4) となり、すべての長さ 3 の連続部分列の総和が 5 の倍数となります。


入力例 2

7 10 4
7 0 9 1 6 4 2

出力例 2

10

Score : 475 points

Problem Statement

You are given a length-N integer sequence A = (A_1, A_2, \ldots, A_N).

Your goal is to perform the following operation repeatedly so that for every length-L contiguous subarray of A, the sum is a multiple of M.

  • Choose an integer i such that 1 \leq i \leq N, and increase the value of A_i by 1.

Find the minimum possible number of operations before achieving the goal.

Constraints

  • 1 \leq N, M \leq 500
  • 1 \leq L \leq N
  • 0 \leq A_i < M
  • All input values are integers.

Input

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

N M L
A_1 A_2 \ldots A_N

Output

Output the answer.


Sample Input 1

4 5 3
4 2 1 3

Sample Output 1

4

By performing the operation once choosing i = 2, twice choosing i = 3, and once choosing i = 4, you get A = (4, 3, 3, 4) with a total of four operations, where every length-3 contiguous subarray sums to a multiple of 5.


Sample Input 2

7 10 4
7 0 9 1 6 4 2

Sample Output 2

10
I - Construct Highway

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

Atcoder 国には 1 から N の番号がついた N 個の街と 1 から M の番号がついた M 個の高速道路があります。
高速道路 i は街 A_i と街 B_i を双方向に結んでいます。

国王の高橋君は、新たに N-M-1 本の高速道路を建設し、次の 2 つの条件をともに満たそうとしています。

  • すべての街同士は、高速道路をいくつか通って互いに行き来できる
  • i=1,\ldots,N について、街 i はちょうど D_i 本の高速道路と直接つながっている

条件を満たすような建設方法が存在するか判定し、存在するなら 1 つ出力してください。

制約

  • 2 \leq N \leq 2\times 10^5
  • 0 \leq M \lt N-1
  • 1 \leq D_i \leq N-1
  • 1\leq A_i \lt B_i \leq N
  • i\neq j ならば、(A_i, B_i) \neq (A_j,B_j)
  • 入力に含まれる値は全て整数である

入力

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

N M
D_1 \ldots D_N
A_1 B_1
\vdots
A_M B_M

出力

条件を満たすような建設方法が存在しないとき -1 を出力せよ。
存在するとき、N-M-1 行出力せよ。i 行目には、i 番目に建設する高速道路が結ぶ 2 つの街の番号を空白区切りで出力せよ。


入力例 1

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

出力例 1

6 2
5 6
4 5

出力例のように、街 62、街 56、街 45 をそれぞれ結ぶ高速道路を建設すると条件を満たすことができます。

この他にも、例えば 街 64、街 56、街 25 を結ぶような高速道路を建設しても条件を満たすことができます。


入力例 2

5 1
1 1 1 1 4
2 3

出力例 2

-1

入力例 3

4 0
3 3 3 3

出力例 3

-1

Score : 500 points

Problem Statement

The Republic of Atcoder has N towns numbered 1 through N, and M highways numbered 1 through M.
Highway i connects Town A_i and Town B_i bidirectionally.

King Takahashi is going to construct (N-M-1) new highways so that the following two conditions are satisfied:

  • One can travel between every pair of towns using some number of highways
  • For each i=1,\ldots,N, exactly D_i highways are directly connected to Town i

Determine if there is such a way of construction. If it exists, print one.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • 0 \leq M \lt N-1
  • 1 \leq D_i \leq N-1
  • 1\leq A_i \lt B_i \leq N
  • If i\neq j, then (A_i, B_i) \neq (A_j,B_j).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
D_1 \ldots D_N
A_1 B_1
\vdots
A_M B_M

Output

If there isn't a way of construction that satisfies the conditions, print -1.
If there is, print (N-M-1) lines. The i-th line should contain the indices of the two towns connected by the i-th highway to be constructed.


Sample Input 1

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

Sample Output 1

6 2
5 6
4 5

As in the Sample Output, the conditions can be satisfied by constructing highways connecting Towns 6 and 2, Towns 5 and 6, and Towns 4 and 5, respectively.

Another example to satisfy the conditions is to construct highways connecting Towns 6 and 4, Towns 5 and 6, and Towns 2 and 5, respectively.


Sample Input 2

5 1
1 1 1 1 4
2 3

Sample Output 2

-1

Sample Input 3

4 0
3 3 3 3

Sample Output 3

-1