A - Two Abbreviations

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

長さ N の文字列 S と長さ M の文字列 T が与えられます。 どちらの文字列も、英小文字からなります。

文字列 X は、以下の条件をすべて満たす時、よい文字列と呼ばれます。

  • X の長さを L とした時、LNM のどちらでも割り切れる
  • X1,\ \frac{L}{N}+1,\ 2 \times \frac{L}{N}+1,\ ...\ (N-1)\times\frac{L}{N}+1 番目の文字を並べ替えることなく連結すると S になる
  • X1,\ \frac{L}{M}+1,\ 2 \times \frac{L}{M}+1,\ ...\ (M-1)\times\frac{L}{M}+1 番目の文字を並べ替えることなく連結すると T になる

よい文字列が存在するか判定し、存在するならば、その中で最短のものの長さを求めてください。

制約

  • 1 \leq N,M \leq 10^5
  • S, T は英小文字からなる。
  • |S|=N
  • |T|=M

入力

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

N M
S
T

出力

よい文字列が存在しないならば、-1 と出力せよ。 存在するならば、その中で最短のものの長さを出力せよ。


入力例 1

3 2
acp
ae

出力例 1

6

例えば、accept という文字列はよい文字列です。 これより短いよい文字列は存在しないので、答えは 6 です。


入力例 2

6 3
abcdef
abc

出力例 2

-1

入力例 3

15 9
dnsusrayukuaiia
dujrunuma

出力例 3

45

Score : 300 points

Problem Statement

You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.

A string X is called a good string when the following conditions are all met:

  • Let L be the length of X. L is divisible by both N and M.
  • Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.
  • Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.

Determine if there exists a good string. If it exists, find the length of the shortest such string.

Constraints

  • 1 \leq N,M \leq 10^5
  • S and T consist of lowercase English letters.
  • |S|=N
  • |T|=M

Input

Input is given from Standard Input in the following format:

N M
S
T

Output

If a good string does not exist, print -1; if it exists, print the length of the shortest such string.


Sample Input 1

3 2
acp
ae

Sample Output 1

6

For example, the string accept is a good string. There is no good string shorter than this, so the answer is 6.


Sample Input 2

6 3
abcdef
abc

Sample Output 2

-1

Sample Input 3

15 9
dnsusrayukuaiia
dujrunuma

Sample Output 3

45
B - Removing Blocks

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 個のブロックが一列に並んでおり、左から右へ順に 1 から N の番号がついています。 それぞれのブロックには重さが定まっており、ブロック i の重さは A_i です。 すぬけ君は、これらのブロックに対して次の操作を N 回行います。

  • まだ取り除かれていないブロックを 1 つ選んで取り除く。 この操作のコストは、取り除くブロックと連結なブロック(取り除くブロック自身も含む)の重さの総和となる。 2 つのブロック x, y ( x \leq y ) が連結であるとは、すべての z ( x \leq z \leq y ) について、ブロック z が取り除かれていないことを意味する。

ブロックを取り除く順番はちょうど N! 通りあります。 N! 通りのすべての順番について N 回の操作のコストの合計を求め、その総和を求めてください。 ただし、答えは非常に大きくなることがあるので、10^9+7 で割った余りを求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数である。

入力

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

N
A_1 A_2 ... A_N

出力

N! 通りのすべての順番について N 回の操作のコストの合計を求め、その総和を 10^9+7 で割った余りを出力せよ。


入力例 1

2
1 2

出力例 1

9

ブロック 1, 2 の順で取り除く場合を考えます。 まず、最初の操作では、ブロック 12 が連結なので、操作のコストは 1+2=3 です。 次の操作では、ブロック 2 しか残っていないので、操作のコストは 2 です。 よって、この順で取り除く場合のコストの合計は 3+2=5 です。

ブロック 2, 1 の順で取り除く場合を考えます。 まず、最初の操作では、ブロック 12 が連結なので、操作のコストは 1+2=3 です。 次の操作では、ブロック 1 しか残っていないので、操作のコストは 1 です。 よって、この順で取り除く場合のコストの合計は 3+1=4 です。

上記より、答えは 5+4=9 となります。


入力例 2

4
1 1 1 1

出力例 2

212

入力例 3

10
1 2 4 8 16 32 64 128 256 512

出力例 3

880971923

Score : 600 points

Problem Statement

There are N blocks arranged in a row, numbered 1 to N from left to right. Each block has a weight, and the weight of Block i is A_i. Snuke will perform the following operation on these blocks N times:

  • Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself). Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed.

There are N! possible orders in which Snuke removes the blocks. For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs. As the answer can be extremely large, compute the sum modulo 10^9+7.

Constraints

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

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7.


Sample Input 1

2
1 2

Sample Output 1

9

First, we will consider the order "Block 1 -> Block 2". In the first operation, the cost of the operation is 1+2=3, as Block 1 and 2 are connected. In the second operation, the cost of the operation is 2, as only Block 2 remains. Thus, the total cost of the two operations for this order is 3+2=5.

Then, we will consider the order "Block 2 -> Block 1". In the first operation, the cost of the operation is 1+2=3, as Block 1 and 2 are connected. In the second operation, the cost of the operation is 1, as only Block 1 remains. Thus, the total cost of the two operations for this order is 3+1=4.

Therefore, the answer is 5+4=9.


Sample Input 2

4
1 1 1 1

Sample Output 2

212

Sample Input 3

10
1 2 4 8 16 32 64 128 256 512

Sample Output 3

880971923
C - Min Cost Cycle

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

N 頂点の重み付き有向グラフがあります。 各頂点には 2 つの整数が書かれており、頂点 i には A_iB_i が書かれています。

このグラフには、任意の 1 \leq x,y \leq N について 頂点 x から頂点 y へ向かう辺があり、その重みは {\rm min}(A_x,B_y) です。

このグラフの有向サイクルであって、すべての頂点をちょうど 1 度ずつ通るものを考えます。 そのようなサイクルの辺の重みの総和の最小値を求めてください。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • 入力はすべて整数である。

入力

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

N
A_1 B_1
A_2 B_2
:
A_N B_N

出力

すべての頂点をちょうど 1 度ずつ通るサイクルの辺の重みの総和の最小値を出力せよ。


入力例 1

3
1 5
4 2
6 3

出力例 1

7

頂点 1→3→2→1 というサイクルを考えると、その辺の重みは、{\rm min}(A_1,B_3)=1, {\rm min}(A_3,B_2)=2, {\rm min}(A_2,B_1)=4 となり、 その総和は 7 になります。 辺の重みの総和を 7 より小さくすることは出来ないので、答えは 7 になります。


入力例 2

4
1 5
2 6
3 7
4 8

出力例 2

10

入力例 3

6
19 92
64 64
78 48
57 33
73 6
95 73

出力例 3

227

Score : 700 points

Problem Statement

We have a directed weighted graph with N vertices. Each vertex has two integers written on it, and the integers written on Vertex i are A_i and B_i.

In this graph, there is an edge from Vertex x to Vertex y for all pairs 1 \leq x,y \leq N, and its weight is {\rm min}(A_x,B_y).

We will consider a directed cycle in this graph that visits every vertex exactly once. Find the minimum total weight of the edges in such a cycle.

Constraints

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

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
:
A_N B_N

Output

Print the minimum total weight of the edges in such a cycle.


Sample Input 1

3
1 5
4 2
6 3

Sample Output 1

7

Consider the cycle 1→3→2→1. The weights of those edges are {\rm min}(A_1,B_3)=1, {\rm min}(A_3,B_2)=2 and {\rm min}(A_2,B_1)=4, for a total of 7. As there is no cycle with a total weight of less than 7, the answer is 7.


Sample Input 2

4
1 5
2 6
3 7
4 8

Sample Output 2

10

Sample Input 3

6
19 92
64 64
78 48
57 33
73 6
95 73

Sample Output 3

227
D - Chords

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 900

問題文

円周上に 2N 個の点が等間隔に並んでいます。 これらの点はある点を基準に、時計回りに 1 から 2N までの番号が付けられています。

すぬけ君は、これらの点を N 個のペアに分けて、各ペアについてペアの点対を結ぶ線分を書きます。 線分を書き終えた後で、ある 2 つの点が連結であるとは、一方の点からスタートし、書かれた線分上のみを移動することによって、他方の点に到達できることを意味します。 また、ここでの連結成分の個数は、2N 個の点を頂点とみなし、連結な点対全てについて辺を張ったグラフにおける連結成分の個数を意味します。

すぬけ君はすでに K 個のペアを決定しており、そのうち i 番目のペアは、点 A_i と点 B_i のペアです。

すぬけ君は残りの N-K ペアの作り方を全通り試して、それら全てについて連結成分の個数を数えようとしています。 これらの連結成分の個数の総和がいくらになるかを求めてください。 ただし、答えは非常に大きくなることがあるので、10^9+7 で割った余りを求めてください。

制約

  • 1 \leq N \leq 300
  • 0 \leq K \leq N
  • 1 \leq A_i,B_i \leq 2N
  • A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K はすべて異なる。
  • 入力は全て整数である。

入力

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

N K
A_1 B_1
A_2 B_2
:
A_K B_K

出力

残りの N-K ペアの作り方を全通り試し、それら全てについて連結成分の個数を求めた際のその総和を 10^9+7 で割った余りを出力せよ。


入力例 1

2 0

出力例 1

5

線分の書き方は以下の 3 通りで、それぞれの連結成分の個数は 2, 2, 1 です。 よって答えは 2+2+1=5 になります。


入力例 2

4 2
5 2
6 1

出力例 2

6

入力例 3

20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39

出力例 3

27087418

Score : 900 points

Problem Statement

There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.

Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.

Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.

He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.

Constraints

  • 1 \leq N \leq 300
  • 0 \leq K \leq N
  • 1 \leq A_i,B_i \leq 2N
  • A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 B_1
A_2 B_2
:
A_K B_K

Output

Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.


Sample Input 1

2 0

Sample Output 1

5

There are three ways to draw line segments, as shown below, and the number of the connected parts for these ways are 2, 2 and 1, respectively. Thus, the answer is 2+2+1=5.


Sample Input 2

4 2
5 2
6 1

Sample Output 2

6

Sample Input 3

20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39

Sample Output 3

27087418
E - High Elements

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1400

問題文

(1,\ 2,\ ...\ N) の順列 P が与えられます。

0, 1 からなる長さ N の文字列 Sよい文字列であるかどうかは、以下の様に判定されます。

  • 数列 X, Y を次のように構築する。
    • まず、X, Y を空の数列とする。
    • i=1,\ 2,\ ...\ N について、この順に、S_i= 0 なら X の末尾に、S_i= 1 なら Y の末尾に P_i を追加する。
  • X の中にある高い項の個数と Y の中にある高い項の個数が等しいならば、S はよい文字列である。 ここで、ある数列の i 番目の項が高いとは、その項が数列の 1 番目から i 番目の項の中で最大であることを意味する。

よい文字列が存在するか判定し、存在するならその中で辞書順最小のものを求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \leq N
  • P_1,\ P_2,\ ...\ P_N はすべて異なる。
  • 入力はすべて整数である。

入力

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

N
P_1 P_2 ... P_N

出力

よい文字列が存在しないならば -1 と出力せよ。 存在するならば、その中で辞書順最小のものを出力せよ。


入力例 1

6
3 1 4 6 2 5

出力例 1

001001

S= 001001 とすると、X=(3,\ 1,\ 6,\ 2), Y=(4,\ 5) となります。 X の中で高い項は、1 項目と 3 項目です。 また、Y の中で高い項は、1 項目と 2 項目です。 高い項の数が等しいので、001001 はよい文字列です。 これより辞書順で小さいよい文字列は存在しないので、001001 が答えになります。


入力例 2

5
1 2 3 4 5

出力例 2

-1

入力例 3

7
1 3 2 5 6 4 7

出力例 3

0001101

入力例 4

30
1 2 6 3 5 7 9 8 11 12 10 13 16 23 15 18 14 24 22 26 19 21 28 17 4 27 29 25 20 30

出力例 4

000000000001100101010010011101

Score : 1400 points

Problem Statement

You are given P, a permutation of (1,\ 2,\ ...\ N).

A string S of length N consisting of 0 and 1 is a good string when it meets the following criterion:

  • The sequences X and Y are constructed as follows:
    • First, let X and Y be empty sequences.
    • For each i=1,\ 2,\ ...\ N, in this order, append P_i to the end of X if S_i= 0, and append it to the end of Y if S_i= 1.
  • If X and Y have the same number of high elements, S is a good string. Here, the i-th element of a sequence is called high when that element is the largest among the elements from the 1-st to i-th element in the sequence.

Determine if there exists a good string. If it exists, find the lexicographically smallest such string.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \leq N
  • P_1,\ P_2,\ ...\ P_N are all distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 ... P_N

Output

If a good string does not exist, print -1. If it exists, print the lexicographically smallest such string.


Sample Input 1

6
3 1 4 6 2 5

Sample Output 1

001001

Let S= 001001. Then, X=(3,\ 1,\ 6,\ 2) and Y=(4,\ 5). The high elements in X is the first and third elements, and the high elements in Y is the first and second elements. As they have the same number of high elements, 001001 is a good string. There is no good string that is lexicographically smaller than this, so the answer is 001001.


Sample Input 2

5
1 2 3 4 5

Sample Output 2

-1

Sample Input 3

7
1 3 2 5 6 4 7

Sample Output 3

0001101

Sample Input 4

30
1 2 6 3 5 7 9 8 11 12 10 13 16 23 15 18 14 24 22 26 19 21 28 17 4 27 29 25 20 30

Sample Output 4

000000000001100101010010011101
F - Reachable Cells

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

問題 F と問題 F2 は同じ問題ですが、制約と実行時間制限が異なります。

N 行、横 N 列のマスからなる盤面があります。 上から i 行目、左から j 列目のマスをマス (i,j) とよびます。 それぞれのマスは、空である、もしくは、障害物が置かれている、のいずれかの状態です。 また、すべての空であるマスには数字が書かれています。 A_{i,j}= 1, 2, ... 9 のとき、マス (i,j) は空で、そこに書かれている数字は A_{i,j} です。 A_{i,j}= # のとき、マス (i,j) には障害物が置かれています。

マス X からマス Y に到達可能であるとは、以下の条件をすべて満たすことを意味します。

  • マス X, Y は相異なる。
  • マス X, Y はどちらも空である。
  • マス X から出発し、右または下に隣接する空マスへの移動を繰り返すことで、マス Y に到達できる。

マス X からマス Y に到達可能であるようなマスの組 (X,Y) すべてについて、マス X にかかれている数字とマス Y にかかれている数字の積を求めたときの、その総和を求めてください。

制約

  • 1 \leq N \leq 500
  • A_{i,j}1, 2, ... 9, # のうちいずれかの文字である。

入力

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

N
A_{1,1}A_{1,2}...A_{1,N}
A_{2,1}A_{2,2}...A_{2,N}
:
A_{N,1}A_{N,2}...A_{N,N}

出力

マス X からマス Y に到達可能であるようなマスの組 (X,Y) すべてについて、マス X にかかれている数字とマス Y にかかれている数字の積を求めたときの、その総和を出力せよ。


入力例 1

2
11
11

出力例 1

5

マス X からマス Y に到達可能であるようなマスの組 (X,Y) は、以下の 5 通りあります。

  • X=(1,1), Y=(1,2)
  • X=(1,1), Y=(2,1)
  • X=(1,1), Y=(2,2)
  • X=(1,2), Y=(2,2)
  • X=(2,1), Y=(2,2)

どの組についても、X にかかれている数字と Y にかかれている数字の積は 1 なので、答えは 5 になります。


入力例 2

4
1111
11#1
1#11
1111

出力例 2

47

入力例 3

10
76##63##3#
8445669721
75#9542133
3#285##445
749632##89
2458##9515
5952578#77
1#3#44196#
4355#99#1#
#298#63587

出力例 3

36065

入力例 4

10
4177143673
7#########
5#1716155#
6#4#####5#
2#3#597#6#
6#9#8#3#5#
5#2#899#9#
1#6#####6#
6#5359657#
5#########

出力例 4

6525

Score : 1000 points

Problem Statement

Problem F and F2 are the same problem, but with different constraints and time limits.

We have a board divided into N horizontal rows and N vertical columns of square cells. The cell at the i-th row from the top and the j-th column from the left is called Cell (i,j). Each cell is either empty or occupied by an obstacle. Also, each empty cell has a digit written on it. If A_{i,j}= 1, 2, ..., or 9, Cell (i,j) is empty and the digit A_{i,j} is written on it. If A_{i,j}= #, Cell (i,j) is occupied by an obstacle.

Cell Y is reachable from cell X when the following conditions are all met:

  • Cells X and Y are different.
  • Cells X and Y are both empty.
  • One can reach from Cell X to Cell Y by repeatedly moving right or down to an adjacent empty cell.

Consider all pairs of cells (X,Y) such that cell Y is reachable from cell X. Find the sum of the products of the digits written on cell X and cell Y for all of those pairs.

Constraints

  • 1 \leq N \leq 500
  • A_{i,j} is one of the following characters: 1, 2, ... 9 and #.

Input

Input is given from Standard Input in the following format:

N
A_{1,1}A_{1,2}...A_{1,N}
A_{2,1}A_{2,2}...A_{2,N}
:
A_{N,1}A_{N,2}...A_{N,N}

Output

Print the sum of the products of the digits written on cell X and cell Y for all pairs (X,Y) such that cell Y is reachable from cell X.


Sample Input 1

2
11
11

Sample Output 1

5

There are five pairs of cells (X,Y) such that cell Y is reachable from cell X, as follows:

  • X=(1,1), Y=(1,2)
  • X=(1,1), Y=(2,1)
  • X=(1,1), Y=(2,2)
  • X=(1,2), Y=(2,2)
  • X=(2,1), Y=(2,2)

The product of the digits written on cell X and cell Y is 1 for all of those pairs, so the answer is 5.


Sample Input 2

4
1111
11#1
1#11
1111

Sample Output 2

47

Sample Input 3

10
76##63##3#
8445669721
75#9542133
3#285##445
749632##89
2458##9515
5952578#77
1#3#44196#
4355#99#1#
#298#63587

Sample Output 3

36065

Sample Input 4

10
4177143673
7#########
5#1716155#
6#4#####5#
2#3#597#6#
6#9#8#3#5#
5#2#899#9#
1#6#####6#
6#5359657#
5#########

Sample Output 4

6525
F2 - Reachable Cells

Time Limit: 9 sec / Memory Limit: 1024 MB

配点 : 1500

問題文

問題 F と問題 F2 は同じ問題ですが、制約と実行時間制限が異なります。

N 行、横 N 列のマスからなる盤面があります。 上から i 行目、左から j 列目のマスをマス (i,j) とよびます。 それぞれのマスは、空である、もしくは、障害物が置かれている、のいずれかの状態です。 また、すべての空であるマスには数字が書かれています。 A_{i,j}= 1, 2, ... 9 のとき、マス (i,j) は空で、そこに書かれている数字は A_{i,j} です。 A_{i,j}= # のとき、マス (i,j) には障害物が置かれています。

マス X からマス Y に到達可能であるとは、以下の条件をすべて満たすことを意味します。

  • マス X, Y は相異なる。
  • マス X, Y はどちらも空である。
  • マス X から出発し、右または下に隣接する空マスへの移動を繰り返すことで、マス Y に到達できる。

マス X からマス Y に到達可能であるようなマスの組 (X,Y) すべてについて、マス X にかかれている数字とマス Y にかかれている数字の積を求めたときの、その総和を求めてください。

制約

  • 1 \leq N \leq 1500
  • A_{i,j}1, 2, ... 9, # のうちいずれかの文字である。

入力

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

N
A_{1,1}A_{1,2}...A_{1,N}
A_{2,1}A_{2,2}...A_{2,N}
:
A_{N,1}A_{N,2}...A_{N,N}

出力

マス X からマス Y に到達可能であるようなマスの組 (X,Y) すべてについて、マス X にかかれている数字とマス Y にかかれている数字の積を求めたときの、その総和を出力せよ。


入力例 1

2
11
11

出力例 1

5

マス X からマス Y に到達可能であるようなマスの組 (X,Y) は、以下の 5 通りあります。

  • X=(1,1), Y=(1,2)
  • X=(1,1), Y=(2,1)
  • X=(1,1), Y=(2,2)
  • X=(1,2), Y=(2,2)
  • X=(2,1), Y=(2,2)

どの組についても、X にかかれている数字と Y にかかれている数字の積は 1 なので、答えは 5 になります。


入力例 2

4
1111
11#1
1#11
1111

出力例 2

47

入力例 3

10
76##63##3#
8445669721
75#9542133
3#285##445
749632##89
2458##9515
5952578#77
1#3#44196#
4355#99#1#
#298#63587

出力例 3

36065

入力例 4

10
4177143673
7#########
5#1716155#
6#4#####5#
2#3#597#6#
6#9#8#3#5#
5#2#899#9#
1#6#####6#
6#5359657#
5#########

出力例 4

6525

Score : 1500 points

Problem Statement

Problem F and F2 are the same problem, but with different constraints and time limits.

We have a board divided into N horizontal rows and N vertical columns of square cells. The cell at the i-th row from the top and the j-th column from the left is called Cell (i,j). Each cell is either empty or occupied by an obstacle. Also, each empty cell has a digit written on it. If A_{i,j}= 1, 2, ..., or 9, Cell (i,j) is empty and the digit A_{i,j} is written on it. If A_{i,j}= #, Cell (i,j) is occupied by an obstacle.

Cell Y is reachable from cell X when the following conditions are all met:

  • Cells X and Y are different.
  • Cells X and Y are both empty.
  • One can reach from Cell X to Cell Y by repeatedly moving right or down to an adjacent empty cell.

Consider all pairs of cells (X,Y) such that cell Y is reachable from cell X. Find the sum of the products of the digits written on cell X and cell Y for all of those pairs.

Constraints

  • 1 \leq N \leq 1500
  • A_{i,j} is one of the following characters: 1, 2, ... 9 and #.

Input

Input is given from Standard Input in the following format:

N
A_{1,1}A_{1,2}...A_{1,N}
A_{2,1}A_{2,2}...A_{2,N}
:
A_{N,1}A_{N,2}...A_{N,N}

Output

Print the sum of the products of the digits written on cell X and cell Y for all pairs (X,Y) such that cell Y is reachable from cell X.


Sample Input 1

2
11
11

Sample Output 1

5

There are five pairs of cells (X,Y) such that cell Y is reachable from cell X, as follows:

  • X=(1,1), Y=(1,2)
  • X=(1,1), Y=(2,1)
  • X=(1,1), Y=(2,2)
  • X=(1,2), Y=(2,2)
  • X=(2,1), Y=(2,2)

The product of the digits written on cell X and cell Y is 1 for all of those pairs, so the answer is 5.


Sample Input 2

4
1111
11#1
1#11
1111

Sample Output 2

47

Sample Input 3

10
76##63##3#
8445669721
75#9542133
3#285##445
749632##89
2458##9515
5952578#77
1#3#44196#
4355#99#1#
#298#63587

Sample Output 3

36065

Sample Input 4

10
4177143673
7#########
5#1716155#
6#4#####5#
2#3#597#6#
6#9#8#3#5#
5#2#899#9#
1#6#####6#
6#5359657#
5#########

Sample Output 4

6525