E - Poem Online Judge

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

ポエムオンラインジャッジ (Poem Online Judge, 以下 POJ と表記) は提出された文字列に得点をつけるオンラインジャッジです。
POJ に N 回の提出がありました。早い方から i 番目の提出では文字列 S_i が提出されて、得点は T_i でした。(同じ文字列が複数回提出される場合もあります)
ただし、POJ では 同じ文字列を提出しても得点が等しいとは限らない のに注意してください。

N 回の提出のうち、その提出よりも早い提出であって文字列が一致するものが存在しないような提出を オリジナル であると呼びます。
また、オリジナルな提出の中で最も得点が高いものを 最優秀賞 と呼びます。ただし、そのような提出が複数ある場合は、最も提出が早いものを最優秀賞とします。

最優秀賞は早い方から何番目の提出ですか?

制約

  • 1 \leq N \leq 10^5
  • S_i は英小文字からなる文字列
  • S_i の長さは 1 以上 10 以下
  • 0 \leq T_i \leq 10^9
  • N, T_i は整数

入力

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

N
S_1 T_1
S_2 T_2
\vdots
S_N T_N

出力

答えを出力せよ。


入力例 1

3
aaa 10
bbb 20
aaa 30

出力例 1

2

以下では早い方から i 番目の提出を提出 i と呼びます。
オリジナルな提出は提出 1 と 提出 2 です。提出 3 は提出 1 と文字列が一致しているためオリジナルではありません。
オリジナルな提出のうち最も得点が高い提出は提出 2 です。よってこれが最優秀賞になります。


入力例 2

5
aaa 9
bbb 10
ccc 10
ddd 10
bbb 11

出力例 2

2

オリジナルな提出は提出 1・提出 2・提出 3・提出 4 です。
その中で最も得点が高い提出は提出 2・提出 3・提出 4 です。この場合はこの中でもっとも提出の早い提出 2 を最優秀賞とします。
このように、オリジナルな提出の中で最も得点が高い提出が複数ある場合は、さらにその中で最も提出が早いものを最優秀賞とするのに注意してください。


入力例 3

10
bb 3
ba 1
aa 4
bb 1
ba 5
aa 9
aa 2
ab 6
bb 5
ab 3

出力例 3

8

Score : 300 points

Problem Statement

Poem Online Judge (POJ) is an online judge that gives scores to submitted strings.
There were N submissions to POJ. In the i-th earliest submission, string S_i was submitted, and a score of T_i was given. (The same string may have been submitted multiple times.)
Note that POJ may not necessarily give the same score to submissions with the same string.

A submission is said to be an original submission if the string in the submission is never submitted in any earlier submission.
A submission is said to be the best submission if it is an original submission with the highest score. If there are multiple such submissions, only the earliest one is considered the best submission.

Find the index of the best submission.

Constraints

  • 1 \leq N \leq 10^5
  • S_i is a string consisting of lowercase English characters.
  • S_i has a length between 1 and 10, inclusive.
  • 0 \leq T_i \leq 10^9
  • N and T_i are integers.

Input

Input is given from Standard Input in the following format:

N
S_1 T_1
S_2 T_2
\vdots
S_N T_N

Output

Print the answer.


Sample Input 1

3
aaa 10
bbb 20
aaa 30

Sample Output 1

2

We will refer to the i-th earliest submission as Submission i.
Original submissions are Submissions 1 and 2. Submission 3 is not original because it has the same string as that in Submission 1.
Among the original submissions, Submission 2 has the highest score. Therefore, this is the best submission.


Sample Input 2

5
aaa 9
bbb 10
ccc 10
ddd 10
bbb 11

Sample Output 2

2

Original submissions are Submissions 1, 2, 3, and 4.
Among them, Submissions 2, 3, and 4 have the highest scores. In this case, the earliest submission among them, Submission 2, is the best.
As in this sample, beware that if multiple original submissions have the highest scores, only the one with the earliest among them is considered the best submission.


Sample Input 3

10
bb 3
ba 1
aa 4
bb 1
ba 5
aa 9
aa 2
ab 6
bb 5
ab 3

Sample Output 3

8
F - Make Takahashi Happy

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

HW 列のマス目があります。 1 \leq i \leq H かつ 1 \leq j \leq W を満たす 2 つの整数 i, j について、 上から i 行目、左から j 列目のマス(以下、(i, j) と表す)には、整数 A_{i, j} が書かれています。

いま、高橋君は (1, 1) にいます。 これから高橋君は「いまいるマスから右または下に隣接するマスに移動する」ことを繰り返して、(H, W) まで移動します。 ただし、その過程でマス目の外部に移動することは出来ません。

その結果、高橋君が通ったマス(始点 (1, 1) と終点 (H, W) を含む)に書かれた整数がすべて異なるとき、高橋君は嬉しくなります。 高橋君の移動経路として考えられるもののうち、高橋君が嬉しくなるものの個数を出力してください。

制約

  • 2 \leq H, W \leq 10
  • 1 \leq A_{i, j} \leq 10^9
  • 入力はすべて整数

入力

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

H W
A_{1, 1} A_{1, 2} \ldots A_{1, W}
A_{2, 1} A_{2, 2} \ldots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \ldots A_{H, W}

出力

答えを出力せよ。


入力例 1

3 3
3 2 2
2 1 3
1 5 4

出力例 1

3

高橋君の移動経路として考えられるものは下記の 6 通りです。

  • (1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 2, 3, 4 であり、高橋君は嬉しくなりません
  • (1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 3, 4 であり、高橋君は嬉しくなりません
  • (1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 5, 4 であり、高橋君は嬉しくなります
  • (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 3, 4 であり、高橋君は嬉しくなりません
  • (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 5, 4 であり、高橋君は嬉しくなります
  • (1, 1) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 2) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 5, 4 であり、高橋君は嬉しくなります

よって、高橋君が嬉しくなる移動経路は、上で 3, 5, 6 番目に述べた 3 個です。


入力例 2

10 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100

出力例 2

48620

この例では、高橋君は考えられるどの経路を通っても嬉しくなります。

Score : 300 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns. For two integers i and j such that 1 \leq i \leq H and 1 \leq j \leq W, the square at the i-th row from the top and j-th column from the left (which we denote by (i, j)) has an integer A_{i, j} written on it.

Takahashi is currently at (1,1). From now on, he repeats moving to an adjacent square to the right of or below his current square until he reaches (H, W). When he makes a move, he is not allowed to go outside the grid.

Takahashi will be happy if the integers written on the squares he visits (including initial (1, 1) and final (H, W)) are distinct. Find the number of his possible paths that make him happy.

Constraints

  • 2 \leq H, W \leq 10
  • 1 \leq A_{i, j} \leq 10^9
  • All values in the input are integers.

Input

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

H W
A_{1, 1} A_{1, 2} \ldots A_{1, W}
A_{2, 1} A_{2, 2} \ldots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \ldots A_{H, W}

Output

Print the answer.


Sample Input 1

3 3
3 2 2
2 1 3
1 5 4

Sample Output 1

3

There are six possible paths:

  • (1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 2, 3, 4, so he will not be happy.
  • (1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 3, 4, so he will not be happy.
  • (1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 5, 4, so he will be happy.
  • (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 3, 4, so he will not be happy.
  • (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 5, 4, so he will be happy.
  • (1, 1) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 2) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 5, 4, so he will be happy.

Thus, the third, fifth, and sixth paths described above make him happy.


Sample Input 2

10 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100

Sample Output 2

48620

In this example, every possible path makes him happy.

G - Cheating Gomoku Narabe

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

HW 列のグリッドがあります。上から i 行目、左から j 列目のマスをマス (i, j) と呼びます。

各マスには ox. のうちいずれかの文字が書かれています。 各マスに書かれた文字は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H で表され、 マス (i, j) に書かれた文字は、文字列 S_ij 文字目と一致します。

このグリッドに対して、下記の操作を 0 回以上好きな回数だけ繰り返します。

  • . が書かれているマスを 1 個選び、そのマスに書かれた文字を o に変更する。

その結果、縦方向または横方向に連続した K 個のマスであってそれらに書かれた文字がすべて o であるようなものが存在する( すなわち、下記の 2 つの条件のうち少なくとも一方を満たす)ようにすることが可能かを判定し、可能な場合はそのために行う操作回数の最小値を出力してください。

  • 1 \leq i \leq H かつ 1 \leq j \leq W-K+1 を満たす整数の組 (i, j) であって、マス (i, j), (i, j+1), \ldots, (i, j+K-1) に書かれた文字が o であるものが存在する。
  • 1 \leq i \leq H-K+1 かつ 1 \leq j \leq W を満たす整数の組 (i, j) であって、マス (i, j), (i+1, j), \ldots, (i+K-1, j) に書かれた文字が o であるものが存在する。

制約

  • H, W, K は整数
  • 1 \leq H
  • 1 \leq W
  • H \times W \leq 2 \times 10^5
  • 1 \leq K \leq \max\lbrace H, W \rbrace
  • S_iox. のみからなる長さ W の文字列

入力

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

H W K
S_1
S_2
\vdots
S_H

出力

問題文中の条件を満たすことが不可能な場合は -1 を、可能な場合はそのために行う操作回数の最小値を出力せよ。


入力例 1

3 4 3
xo.x
..o.
xx.o

出力例 1

2

操作を 2 回行って、例えばマス (2, 1) とマス (2, 2) に書かれた文字をそれぞれ o に変更することで問題文中の条件を満たすことができ、これが最小の操作回数です。


入力例 2

4 2 3
.o
.o
.o
.o

出力例 2

0

操作を一度も行わなくても問題文中の条件を満たします。


入力例 3

3 3 3
x..
..x
.x.

出力例 3

-1

問題文中の条件を満たすことは不可能なので、-1 を出力します。


入力例 4

10 12 6
......xo.o..
x...x.....o.
x...........
..o...x.....
.....oo.....
o.........x.
ox.oox.xx..x
....o...oox.
..o.....x.x.
...o........

出力例 4

3

Score: 400 points

Problem Statement

There is a grid with H rows and W columns. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.

Each cell contains one of the characters o, x, and .. The characters written in each cell are represented by H strings S_1, S_2, \ldots, S_H of length W; the character written in cell (i, j) is the j-th character of the string S_i.

For this grid, you may repeat the following operation any number of times, possibly zero:

  • Choose one cell with the character . and change the character in that cell to o.

Determine if it is possible to have a sequence of K horizontally or vertically consecutive cells with o written in all cells (in other words, satisfy at least one of the following two conditions). If it is possible, print the minimum number of operations required to achieve this.

  • There is an integer pair (i, j) satisfying 1 \leq i \leq H and 1 \leq j \leq W-K+1 such that the characters in cells (i, j), (i, j+1), \ldots, (i, j+K-1) are all o.
  • There is an integer pair (i, j) satisfying 1 \leq i \leq H-K+1 and 1 \leq j \leq W such that the characters in cells (i, j), (i+1, j), \ldots, (i+K-1, j) are all o.

Constraints

  • H, W, and K are integers.
  • 1 \leq H
  • 1 \leq W
  • H \times W \leq 2 \times 10^5
  • 1 \leq K \leq \max\lbrace H, W \rbrace
  • S_i is a string of length W consisting of the characters o, x, and ..

Input

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

H W K
S_1
S_2
\vdots
S_H

Output

If it is impossible to satisfy the condition in the problem statement, print -1. Otherwise, print the minimum number of operations required to do so.


Sample Input 1

3 4 3
xo.x
..o.
xx.o

Sample Output 1

2

By operating twice, for example, changing the characters in cells (2, 1) and (2, 2) to o, you can satisfy the condition in the problem statement, and this is the minimum number of operations required.


Sample Input 2

4 2 3
.o
.o
.o
.o

Sample Output 2

0

The condition is satisfied without performing any operations.


Sample Input 3

3 3 3
x..
..x
.x.

Sample Output 3

-1

It is impossible to satisfy the condition, so print -1.


Sample Input 4

10 12 6
......xo.o..
x...x.....o.
x...........
..o...x.....
.....oo.....
o.........x.
ox.oox.xx..x
....o...oox.
..o.....x.x.
...o........

Sample Output 4

3
H - Make it Palindrome

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

数列 X に対し、 f(X) = ( X を回文にするために変更する必要のある要素の個数の最小値 ) とします。

与えられた長さ N の数列 A の全ての 連続 部分列 X に対する f(X) の総和を求めてください。

但し、長さ m の数列 X が回文であるとは、全ての 1 \le i \le m を満たす整数 i について、 Xi 項目と m+1-i 項目が等しいことを指します。

制約

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

入力

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

N
A_1 A_2 \dots A_N

出力

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


入力例 1

5
5 2 1 2 2

出力例 1

9
  • f(5) = 0
  • f(2) = 0
  • f(1) = 0
  • f(2) = 0
  • f(2) = 0
  • f(5,2) = 1
  • f(2,1) = 1
  • f(1,2) = 1
  • f(2,2) = 0
  • f(5,2,1) = 1
  • f(2,1,2) = 0
  • f(1,2,2) = 1
  • f(5,2,1,2) = 2
  • f(2,1,2,2) = 1
  • f(5,2,1,2,2) = 1

以上より、求める答えは 9 です。

Score : 500 points

Problem Statement

For a sequence X, let f(X) = (the minimum number of elements one must modify to make X a palindrome).

Given a sequence A of length N, find the sum of f(X) over all contiguous subarrays of A.

Here, a sequence X of length m is said to be a palindrome if and only if the i-th and the (m+1-i)-th elements of X are equal for all 1 \le i \le m.

Constraints

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

Input

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

N
A_1 A_2 \dots A_N

Output

Print the answer as an integer.


Sample Input 1

5
5 2 1 2 2

Sample Output 1

9
  • f(5) = 0
  • f(2) = 0
  • f(1) = 0
  • f(2) = 0
  • f(2) = 0
  • f(5,2) = 1
  • f(2,1) = 1
  • f(1,2) = 1
  • f(2,2) = 0
  • f(5,2,1) = 1
  • f(2,1,2) = 0
  • f(1,2,2) = 1
  • f(5,2,1,2) = 2
  • f(2,1,2,2) = 1
  • f(5,2,1,2,2) = 1

Therefore, the sought answer is 9.

I - Tree Degree Optimization

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 550

問題文

整数列 A=(A_1,\ldots,A_N) が与えられます。 N 頂点の木 T に対して、 f(T) を以下で定めます。

  • T の頂点 i の次数を d_i とする。このとき、f(T)=\sum_{i=1}^N {d_i}^2 A_i とする。

f(T) として考えられる最小値を求めてください。

なお、制約下において答えが 2^{63} 未満となることは保証されています。

制約

  • 2\leq N\leq 2\times 10^5
  • 1\leq A_i \leq 10^9
  • 入力される数値は全て整数

入力

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

N 
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

4
3 2 5 2

出力例 1

24

頂点 1 と頂点 2 を結ぶ辺、頂点 2 と頂点 4 を結ぶ辺、頂点 4 と頂点 3 を結ぶ辺からなるような木 T を考えます。

このとき f(T) = 1^2\times 3 + 2^2\times 2+1^2\times 5 +2^2\times 2 = 24 です。これが f(T) の最小値であることが証明できます。


入力例 2

3
4 3 2

出力例 2

15

入力例 3

7
10 5 10 2 10 13 15

出力例 3

128

Score : 550 points

Problem Statement

You are given a sequence of integers A=(A_1,\ldots,A_N). For a tree T with N vertices, define f(T) as follows:

  • Let d_i be the degree of vertex i in T. Then, f(T)=\sum_{i=1}^N {d_i}^2 A_i.

Find the minimum possible value of f(T).

The constraints guarantee the answer to be less than 2^{63}.

Constraints

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

Input

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

N 
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

4
3 2 5 2

Sample Output 1

24

Consider a tree T with an edge connecting vertices 1 and 2, an edge connecting vertices 2 and 4, and an edge connecting vertices 4 and 3.

Then, f(T) = 1^2\times 3 + 2^2\times 2 + 1^2\times 5 + 2^2\times 2 = 24. It can be proven that this is the minimum value of f(T).


Sample Input 2

3
4 3 2

Sample Output 2

15

Sample Input 3

7
10 5 10 2 10 13 15

Sample Output 3

128