E - Socks

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

配点 : 300

問題文

N 枚の靴下があります。i 枚目の靴下の色は A_i です。

あなたは以下の操作をできるだけ多い回数行いたいです。最大で何回行うことができますか?

  • まだペアになっていない靴下の中から同じ色の靴下を 2 枚選んでペアにする。

制約

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

入力

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

N
A_1 A_2 \dots A_N

出力

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


入力例 1

6
4 1 7 4 1 4

出力例 1

2

以下のようにして、2 回の操作を行うことができます。

  • 色が 1 である靴下を 2 枚選んでペアにする。
  • 色が 4 である靴下を 2 枚選んでペアにする。

このとき、色が 4 である靴下と 7 である靴下が 1 枚ずつ残るため、これ以上操作はできません。 また、どのように操作をしても 3 回以上操作を行うことはできないため、2 を出力します。


入力例 2

1
158260522

出力例 2

0

入力例 3

10
295 2 29 295 29 2 29 295 2 29

出力例 3

4

Score : 300 points

Problem Statement

You have N socks. The color of the i-th sock is A_i.

You want to perform the following operation as many times as possible. How many times can it be performed at most?

  • Choose two same-colored socks that are not paired yet, and pair them.

Constraints

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

Input

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

N
A_1 A_2 \dots A_N

Output

Print an integer representing the answer.


Sample Input 1

6
4 1 7 4 1 4

Sample Output 1

2

You can do the operation twice as follows.

  • Choose two socks with the color 1 and pair them.
  • Choose two socks with the color 4 and pair them.

Then, you will be left with one sock with the color 4 and another with the color 7, so you can no longer do the operation. There is no way to do the operation three or more times, so you should print 2.


Sample Input 2

1
158260522

Sample Output 2

0

Sample Input 3

10
295 2 29 295 29 2 29 295 2 29

Sample Output 3

4
F - Max Even

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

配点 : 300

問題文

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

A の異なる 2 要素の和として表せる値の中に偶数が存在するか判定し、存在する場合その最大値を求めてください。

制約

  • 2\leq N \leq 2\times 10^5
  • 0\leq A_i\leq 10^9
  • A の要素は相異なる
  • 入力は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

A の異なる 2 要素の和として表せる値の中に偶数が存在しない場合、-1 を出力せよ。

偶数が存在する場合、その最大値を出力せよ。


入力例 1

3
2 3 4

出力例 1

6

A の異なる 2 要素の和として表せる値は 5,6,7 です。この中に偶数は存在し、その最大値は 6 です。


入力例 2

2
1 0

出力例 2

-1

A の異なる 2 要素の和として表せる値は 1 です。この中に偶数は存在しないので、 -1 を出力してください。

Score : 300 points

Problem Statement

You are given a sequence A=(A_1,A_2,\ldots,A_N) of length N consisting of non-negative integers.

Determine if there is an even number represented as the sum of two different elements of A. If it exists, find the maximum such number.

Constraints

  • 2\leq N \leq 2\times 10^5
  • 0\leq A_i\leq 10^9
  • The elements of A are distinct.
  • All values in the input are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print -1 if there is no even number represented as the sum of two different elements of A.

If such an even number exists, print the maximum such number.


Sample Input 1

3
2 3 4

Sample Output 1

6

The values represented as the sum of two distinct elements of A are 5, 6, and 7. We have an even number here, and the maximum is 6.


Sample Input 2

2
1 0

Sample Output 2

-1

The value represented as the sum of two distinct elements of A is 1. We have no even number here, so -1 should be printed.

G - Grid Ice Floor

実行時間制限: 2 sec / メモリ制限: 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 - Erasing Vertices 2

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

配点 : 500

問題文

N 頂点 M 辺の単純無向グラフ(すなわち、自己辺も多重辺もない無向グラフ)が与えられます。i 本目の辺は頂点 U_i と頂点 V_i を結んでいます。頂点 i には正整数 A_i が書かれています。

あなたは、以下の操作を N 回繰り返します。

  • まだ削除されていない頂点 x を選び、「頂点 x 」と「頂点 x を端点に持つ辺全て」を削除する。この操作のコストは、頂点 x と辺で直接結ばれていて、かつまだ削除されていない頂点に書かれている整数の総和である。

N 回の操作全体のコストを、1 回ごとの操作におけるコストのうちの最大値として定めます。操作全体のコストとして取り得る値の最小値を求めてください。

制約

  • 1 \le N \le 2 \times 10^5
  • 0 \le M \le 2 \times 10^5
  • 1 \le A_i \le 10^9
  • 1 \le U_i,V_i \le N
  • 与えられるグラフは単純。
  • 入力は全て整数。

入力

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

N M
A_1 A_2 \dots A_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M

出力

答えを出力せよ。


入力例 1

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

出力例 1

3

以下のように操作を行うことにより、N 回の操作のコストのうちの最大値を 3 にすることができます。

  • 頂点 3 を選ぶ。コストは A_1=3 である。
  • 頂点 1 を選ぶ。コストは A_2+A_4=3 である。
  • 頂点 2 を選ぶ。コストは 0 である。
  • 頂点 4 を選ぶ。コストは 0 である。

N 回の操作のコストのうちの最大値を 2 以下にすることはできないため、解は 3 です。


入力例 2

7 13
464 661 847 514 74 200 188
5 1
7 1
5 7
4 1
4 5
2 4
5 2
1 3
1 6
3 5
1 2
4 6
2 7

出力例 2

1199

Score : 500 points

Problem Statement

You are given a simple undirected graph with N vertices and M edges. The i-th edge connects Vertices U_i and V_i. Vertex i has a positive integer A_i written on it.

You will repeat the following operation N times.

  • Choose a Vertex x that is not removed yet, and remove Vertex x and all edges incident to Vertex x. The cost of this operation is the sum of the integers written on the vertices directly connected by an edge with Vertex x that are not removed yet.

We define the cost of the entire N operations as the maximum of the costs of the individual operations. Find the minimum possible cost of the entire operations.

Constraints

  • 1 \le N \le 2 \times 10^5
  • 0 \le M \le 2 \times 10^5
  • 1 \le A_i \le 10^9
  • 1 \le U_i,V_i \le N
  • The given graph is simple.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \dots A_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M

Output

Print the answer.


Sample Input 1

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

Sample Output 1

3

By performing the operations as follows, the maximum of the costs of the N operations can be 3.

  • Choose Vertex 3. The cost is A_1=3.
  • Choose Vertex 1. The cost is A_2+A_4=3.
  • Choose Vertex 2. The cost is 0.
  • Choose Vertex 4. The cost is 0.

The maximum of the costs of the N operations cannot be 2 or less, so the solution is 3.


Sample Input 2

7 13
464 661 847 514 74 200 188
5 1
7 1
5 7
4 1
4 5
2 4
5 2
1 3
1 6
3 5
1 2
4 6
2 7

Sample Output 2

1199
I - Cleaning Robot

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

配点 : 500

問題文

無限に広がる二次元グリッドのマス (0, 0) に掃除ロボットが置かれています。

このロボットに、4 種類の文字 LRUD からなる文字列で表されたプログラムが与えられます。
ロボットは、与えられたプログラムの各文字を先頭の文字から順に読み、各文字について以下の行動を行います。

  1. ロボットの現在地をマス (x, y) とする
  2. 読んだ文字に応じて以下の通りに移動する:
    • L を読んだとき: マス (x-1, y) に移動する。
    • R を読んだとき: マス (x+1, y) に移動する。
    • U を読んだとき: マス (x, y-1) に移動する。
    • D を読んだとき: マス (x, y+1) に移動する。

LRUD からなる文字列 S が与えられます。 ロボットが実行するプログラムは、文字列 SK 個連接したものです。

ロボットが一度でも存在したことのあるマス(ロボットの初期位置であるマス (0, 0) を含む)は掃除されます。
ロボットがプログラムの実行を終えた後の時点で、掃除されているマスの個数を出力して下さい。

制約

  • SLRUD からなる長さ 1 以上 2 \times 10^5 以下の文字列
  • 1 \leq K \leq 10^{12}

入力

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

S
K

出力

ロボットがプログラムの実行を終えた後の時点で、掃除されているマスの個数を出力せよ。


入力例 1

RDRUL
2

出力例 1

7

ロボットが実行するプログラムは RDRULRDRUL です。ロボットは初期位置であるマス (0, 0) からはじめ、
(0, 0) \rightarrow (1, 0) \rightarrow (1, 1) \rightarrow (2, 1) \rightarrow (2, 0) \rightarrow (1, 0) \rightarrow (2, 0) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 0) \rightarrow (2, 0) と移動します。
その後掃除されているマスは、(0, 0), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1)7 個です。


入力例 2

LR
1000000000000

出力例 2

2

入力例 3

UUURRDDDRRRUUUURDLLUURRRDDDDDDLLLLLLU
31415926535

出力例 3

219911485785

Score : 500 points

Problem Statement

There is a cleaning robot on the square (0, 0) in an infinite two-dimensional grid.

The robot will be given a program represented as a string consisting of four kind of characters L, R, U, D.
It will read the characters in the program from left to right and perform the following action for each character read.

  1. Let (x, y) be the square where the robot is currently on.
  2. Make the following move according to the character read:
    • if L is read: go to (x-1, y).
    • if R is read: go to (x+1, y).
    • if U is read: go to (x, y-1).
    • if D is read: go to (x, y+1).

You are given a string S consisting of L, R, U, D. The program that will be executed by the robot is the concatenation of K copies of S.

Squares visited by the robot at least once, including the initial position (0, 0), will be cleaned.
Print the number of squares that will be cleaned at the end of the execution of the program.

Constraints

  • S is a string of length between 1 and 2 \times 10^5 (inclusive) consisting of L, R, U, D.
  • 1 \leq K \leq 10^{12}

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the number of squares that will be cleaned at the end of the execution of the program.


Sample Input 1

RDRUL
2

Sample Output 1

7

The robot will execute the program RDRULRDRUL. It will start on (0, 0) and travel as follows:
(0, 0) \rightarrow (1, 0) \rightarrow (1, 1) \rightarrow (2, 1) \rightarrow (2, 0) \rightarrow (1, 0) \rightarrow (2, 0) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 0) \rightarrow (2, 0).
In the end, seven squares will get cleaned: (0, 0), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1).


Sample Input 2

LR
1000000000000

Sample Output 2

2

Sample Input 3

UUURRDDDRRRUUUURDLLUURRRDDDDDDLLLLLLU
31415926535

Sample Output 3

219911485785