A - Hell, World!

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

配点 : 100 点

問題文

1 以上 10 以下の整数 X が与えられます。

HelloWorld という文字列から X 文字目だけを削除した文字列を出力してください。

制約

  • X は 1 以上 10 以下の整数

入力

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

X

出力

答えを出力せよ。


入力例 1

5

出力例 1

HellWorld

HelloWorld の 5 文字目を削除すると HellWorld になります。したがって、HellWorld を出力してください。


入力例 2

9

出力例 2

HelloWord

入力例 3

1

出力例 3

elloWorld

Score : 100 points

Problem Statement

You are given an integer X between 1 and 10, inclusive.

Output the string obtained by deleting only the X-th character from the string HelloWorld.

Constraints

  • X is an integer between 1 and 10, inclusive.

Input

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

X

Output

Output the answer.


Sample Input 1

5

Sample Output 1

HellWorld

Deleting the 5-th character of HelloWorld gives HellWorld. Thus, output HellWorld.


Sample Input 2

9

Sample Output 2

HelloWord

Sample Input 3

1

Sample Output 3

elloWorld
B - Edge Checker 2

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

配点 : 100 点

問題文

下の画像で示す図において、a 番の点と b 番の点が線で直接結ばれているかを答えてください。

制約

  • 1 \leq a \lt b \leq 15
  • a,b は整数

入力

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

a b

出力

a 番の点と b 番の点が線で直接結ばれているなら Yes、結ばれていないなら No を出力せよ。


入力例 1

1 2

出力例 1

Yes

問題文で示した図において、1 番の点と 2 番の点は線で直接結ばれています。 よって、Yes を出力します。


入力例 2

2 8

出力例 2

No

問題文で示した図において、2 番の点と 8 番の点は線で直接結ばれていません。 よって、No を出力します。


入力例 3

14 15

出力例 3

No

Score : 100 points

Problem Statement

Determine if there is a segment that directly connects the points numbered a and b in the figure below.

Constraints

  • 1 \leq a \lt b \leq 15
  • a and b are integers.

Input

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

a b

Output

Print Yes if there is a segment that directly connects the points numbered a and b; print No otherwise.


Sample Input 1

1 2

Sample Output 1

Yes

In the figure in the Problem Statement, there is a segment that directly connects the points numbered 1 and 2, so Yes should be printed.


Sample Input 2

2 8

Sample Output 2

No

In the figure in the Problem Statement, there is no segment that directly connects the points numbered 2 and 8, so No should be printed.


Sample Input 3

14 15

Sample Output 3

No
C - Chessboard

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

配点 : 200 点

問題文

チェス盤のどこにコマが置かれているか答えてください。

縦 8 マス、横 8 マスのグリッドがあります。グリッドの各マスには、次のルールで定められる長さ 2 の文字列の名前がついています。

  • 左から 1 列目にあるマスの名前の 1 文字目は a である。同様に、左から 2,3,\ldots,8 列目にあるマスの名前の 1 文字目は b, c, d, e, f, g, h である。
  • 下から 1 行目にあるマスの名前の 2 文字目は 1 である。同様に、下から 2,3,\ldots,8 行目にあるマスの名前の 2 文字目は 2, 3, 4, 5, 6, 7, 8 である。

例えば、グリッドの左下のマスの名前は a1、右下のマスの名前は h1、右上のマスの名前は h8 です。

グリッドの状態を表す長さ 8 の 8 つの文字列 S_1,\ldots,S_8 が与えられます。
S_i の j 文字目は、グリッドの上から i 行目 左から j 列目のマスにコマが置かれているとき *、置かれていないとき . であり、S_1,\ldots,S_8 の中に文字 * はちょうど 1 つ存在します。
コマが置かれているマスの名前を求めてください。

制約

  • S_i は . および * のみからなる長さ 8 の文字列である
  • S_1,\ldots,S_8 の中に文字 * はちょうど 1 つ存在する。

入力

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

S_1
S_2
S_3
S_4
S_5
S_6
S_7
S_8

出力

答えを出力せよ。


入力例 1

........
........
........
........
........
........
........
*.......

出力例 1

a1

問題文中で説明したとおり、グリッドの左下のマスの名前は a1 です。


入力例 2

........
........
........
........
........
.*......
........
........

出力例 2

b3

Score : 200 points

Problem Statement

Locate a piece on a chessboard.

We have a grid with 8 rows and 8 columns of squares. Each of the squares has a 2-character name determined as follows.

  • The first character of the name of a square in the 1-st column from the left is a. Similarly, the first character of the name of a square in the 2-nd, 3-rd, \ldots, 8-th column from the left is b, c, d, e, f, g, h, respectively.
  • The second character of the name of a square in the 1-st row from the bottom is 1. Similarly, the second character of the name of a square in the 2-nd, 3-rd, \ldots, 8-th row from the bottom is 2, 3, 4, 5, 6, 7, 8, respectively.

For instance, the bottom-left square is named a1, the bottom-right square is named h1, and the top-right square is named h8.

You are given 8 strings S_1,\ldots,S_8, each of length 8, representing the state of the grid.
The j-th character of S_i is * if the square at the i-th row from the top and j-th column from the left has a piece on it, and . otherwise. The character * occurs exactly once among S_1,\ldots,S_8. Find the name of the square that has a piece on it.

Constraints

  • S_i is a string of length 8 consisting of . and*.
  • The character * occurs exactly once among S_1,\ldots,S_8.

Input

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

S_1
S_2
S_3
S_4
S_5
S_6
S_7
S_8

Output

Print the answer.


Sample Input 1

........
........
........
........
........
........
........
*.......

Sample Output 1

a1

As explained in the problem statement, the bottom-left square is named a1.


Sample Input 2

........
........
........
........
........
.*......
........
........

Sample Output 2

b3
D - Deconstruct Chocolate

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

配点 : 200 点

問題文

H 行 W 列のブロックからなる長方形状のチョコレートがあります。

Q 個のクエリが与えられるので、順に処理したときの各クエリの答えを求めてください。各クエリは、以下のいずれかの形式です。

  • タイプ 1 : 整数 R が与えられる。下 R 行のチョコレートのブロックの個数を求め、それらを食べる。

  • タイプ 2 : 整数 C が与えられる。右 C 列のチョコレートのブロックの個数を求め、それらを食べる。

なお、クエリを順に処理したとき、各クエリを処理した後もチョコレートは長方形状であり、タイプ 1 のクエリを処理する直前の時点でチョコレートは R + 1 行以上存在し、タイプ 2 のクエリを処理する直前の時点でチョコレートは C + 1 列以上存在します。

制約

  • 2 \leq H, W \leq 100
  • 1 \leq Q \leq 100
  • タイプ 1 のクエリについて、1 \leq R
  • クエリを順に処理したとき、タイプ 1 のクエリを処理する直前にチョコレートは R + 1 行以上存在する
  • タイプ 2 のクエリについて、1 \leq C
  • クエリを順に処理したとき、タイプ 2 のクエリを処理する直前にチョコレートは C + 1 列以上存在する
  • 入力される値はすべて整数

入力

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

H W Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

ただし、\text{query}_i は i 番目のクエリであり、以下のいずれかの形式で与えられる。

1 R
2 C

出力

Q 行出力せよ。 i (1 \leq i \leq Q) 行目には i 番目のクエリに対する答えを出力せよ。


入力例 1

7 9 5
2 4
1 3
2 1
2 1
1 3

出力例 1

28
15
4
4
9

はじめ、チョコレートは 7 行 9 列の長方形状です。

1 番目のクエリでは、右 4 列のチョコレートの個数は 28 個であるため 28 を出力します。チョコレートは 7 行 5 列となります。

2 番目のクエリでは、下 3 行のチョコレートの個数は 15 個であるため 15 を出力します。チョコレートは 4 行 5 列となります。

3 番目のクエリでは、右 1 列のチョコレートの個数は 4 個であるため 4 を出力します。チョコレートは 4 行 4 列となります。

4 番目のクエリでは、右 1 列のチョコレートの個数は 4 個であるため 4 を出力します。チョコレートは 4 行 3 列となります。

5 番目のクエリでは、下 3 行のチョコレートの個数は 9 個であるため 9 を出力します。チョコレートは 1 行 3 列となります。

Score : 200 points

Problem Statement

There is a rectangular chocolate consisting of H rows and W columns of blocks.

You are given Q queries; process them in order and find the answer to each query. Each query is in one of the following formats:

  • Type 1: An integer R is given. Find the number of chocolate blocks in the bottom R rows, then eat them.

  • Type 2: An integer C is given. Find the number of chocolate blocks in the rightmost C columns, then eat them.

When the queries are processed in order, the chocolate remains rectangular after each query is processed, and it has at least R + 1 rows immediately before processing a type 1 query and has at least C + 1 columns immediately before processing a type 2 query.

Constraints

  • 2 \leq H, W \leq 100
  • 1 \leq Q \leq 100
  • For type 1 queries, 1 \leq R.
  • When the queries are processed in order, the chocolate has at least R + 1 rows immediately before processing a type 1 query.
  • For type 2 queries, 1 \leq C.
  • When the queries are processed in order, the chocolate has at least C + 1 columns immediately before processing a type 2 query.
  • All input values are integers.

Input

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

H W Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Here, \text{query}_i is the i-th query, given in one of the following formats:

1 R
2 C

Output

Output Q lines. The i-th line (1 \leq i \leq Q) should contain the answer to the i-th query.


Sample Input 1

7 9 5
2 4
1 3
2 1
2 1
1 3

Sample Output 1

28
15
4
4
9

Initially, the chocolate is a rectangle with 7 rows and 9 columns.

For the first query, the number of chocolate blocks in the rightmost 4 columns is 28, so output 28. The chocolate becomes 7 rows and 5 columns.

For the second query, the number of chocolate blocks in the bottom 3 rows is 15, so output 15. The chocolate becomes 4 rows and 5 columns.

For the third query, the number of chocolate blocks in the rightmost 1 column is 4, so output 4. The chocolate becomes 4 rows and 4 columns.

For the fourth query, the number of chocolate blocks in the rightmost 1 column is 4, so output 4. The chocolate becomes 4 rows and 3 columns.

For the fifth query, the number of chocolate blocks in the bottom 3 rows is 9, so output 9. The chocolate becomes 1 row and 3 columns.

E - Buy Balls

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

配点 : 300 点

問題文

N 個の黒色のボールと M 個の白色のボールがあります。
ボールにはそれぞれ価値がつけられており、i\ (1\leq i\leq N) 個目の黒色のボールの価値は B_i、j\ (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.

F - AtCoder Riko

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

配点 : 350 点

問題文

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

以下のようなことが起こりうる正整数 L をすべて求めてください。

AtCoder 社は棒状のスナック菓子「AtCoderりこ」を発売しました。 カップの中に長さ L の AtCoderりこが何本か入っています。 高橋君がこのカップをシェイクしたところ、それぞれの AtCoderりこは以下のいずれかの状態になりました。

  • 長さが L である 1 本の AtCoderりことしてそのまま残った。
  • 長さの和が L であるような 2 本の AtCoderりこに分かれた。ただし、各 AtCoderりこの長さは正整数である。
カップをシェイクした後、カップの中には N 本の AtCoderりこが入っており、i 本目の AtCoderりこの長さは A_i でした。

ただし、このようなことが起こりうる正整数 L が少なくとも 1 つ存在するような入力が与えられます。

制約

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 条件を満たすような L が少なくとも 1 つ存在する
  • 入力される値は全て整数

入力

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

N  
A_1 A_2 \ldots A_N  

出力

条件を満たすような L を、空白区切りで昇順に 1 行で出力せよ。


入力例 1

4
10 5 5 10

出力例 1

10 15

最初、カップには長さ 10 の AtCoderりこが 3 本入っていて、そのうち 1 本が 長さ 5 の 2 本の AtCoderりこに分かれると、条件を満たします。
最初、カップには長さ 15 の AtCoderりこが 2 本入っていて、それぞれの AtCoderりこが長さ 5,10 の 2 本の AtCoderりこに分かれると、条件を満たします。
これ以外の L では条件を満たしません。


入力例 2

3
4 4 4

出力例 2

4

入力例 3

6
10 187 344 100 434 257

出力例 3

444

Score : 350 points

Problem Statement

You are given a sequence of N positive integers A=(A_1,A_2,\dots,A_N).

Find all positive integers L for which the following can occur:

AtCoder Inc. has released a stick-shaped snack called "AtCoderiko." A cup contains one or more AtCoderikos, each of length L. When Takahashi shook the cup, each AtCoderiko ended up in one of the following states:

  • It remained as one AtCoderiko of length L.
  • It broke into two AtCoderikos whose lengths sum to L. Here, the length of each AtCoderiko is a positive integer.
After shaking the cup, there were N AtCoderikos in the cup, and the length of the i-th AtCoderiko was A_i.

The given input guarantees that there exists at least one positive integer L for which this can occur.

Constraints

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9
  • There exists at least one L satisfying the condition.
  • 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

Output all values of L satisfying the condition in ascending order, separated by spaces, in one line.


Sample Input 1

4
10 5 5 10

Sample Output 1

10 15

If the cup initially contained three AtCoderikos of length 10, and one of them broke into two AtCoderikos of length 5, the condition is satisfied.
If the cup initially contained two AtCoderikos of length 15, and each of them broke into two AtCoderikos of lengths 5 and 10, the condition is satisfied.
No other values of L satisfy the condition.


Sample Input 2

3
4 4 4

Sample Output 2

4

Sample Input 3

6
10 187 344 100 434 257

Sample Output 3

444
G - Three Days Ago

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

配点 : 400 点

問題文

20230322 は並べ替えると 02320232 となり、これは 0232 を 2 度繰り返しています。
このように、数字のみからなる文字列であって、適切に文字を並び替える (そのままでもよい) ことによって同じ列を 2 度繰り返すようにできるものを 嬉しい列 と呼びます。
数字のみからなる文字列 S が与えられるので、以下の条件を全て満たす整数の組 (l,r) はいくつあるか求めてください。

  • 1 \le l \le r \le |S| ( |S| は S の長さ)
  • S の l 文字目から r 文字目までの (連続する) 部分文字列は嬉しい列である。

制約

  • S は数字のみからなる長さ 1 以上 5 \times 10^5 以下の文字列

入力

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

S

出力

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


入力例 1

20230322

出力例 1

4

S= 20230322 です。
条件を満たす整数組 (l,r) は (1,6),(1,8),(2,7),(7,8) の 4 つです。


入力例 2

0112223333444445555556666666777777778888888889999999999

出力例 2

185

S の先頭が 0 である場合もあります。


入力例 3

3141592653589793238462643383279502884197169399375105820974944

出力例 3

9

Score : 400 points

Problem Statement

The string 20230322 can be rearranged into 02320232, which is a repetition of 0232 twice.
Similarly, a string consisting of digits is said to be happy when it can be rearranged into (or already is) a repetition of some string twice.
You are given a string S consisting of digits. Find the number of pairs of integers (l,r) satisfying all of the following conditions.

  • 1 \le l \le r \le |S|. (|S| is the length of S.)
  • The (contiguous) substring formed of the l-th through r-th characters of S is happy.

Constraints

  • S is a string consisting of digits whose length is between 1 and 5 \times 10^5, inclusive.

Input

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

S

Output

Print an integer representing the answer.


Sample Input 1

20230322

Sample Output 1

4

We have S= 20230322.

Here are the four pairs of integers (l,r) that satisfy the condition: (1,6), (1,8), (2,7), and (7,8).


Sample Input 2

0112223333444445555556666666777777778888888889999999999

Sample Output 2

185

S may begin with 0.


Sample Input 3

3141592653589793238462643383279502884197169399375105820974944

Sample Output 3

9
H - Palindromic Shortest Path

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

配点 : 450 点

問題文

N 頂点の有向グラフがあり、頂点には 1, 2, \ldots, N の番号が付いています。

辺の情報は N^2 個の文字 C_{1, 1}, C_{1, 2}, \ldots, C_{1, N}, C_{2, 1}, \ldots, C_{N, N} によって与えられます。ここで、C_{i, j} は英小文字あるいは - です。

C_{i, j} が英小文字のとき頂点 i から頂点 j へ向かう辺がちょうど 1 つ存在してラベル C_{i, j} が付いており、C_{i, j} が - のとき頂点 i から頂点 j へ向かう辺は存在しません。

1 \leq i, j \leq N を満たす各整数組 (i, j) について、以下の問題に対する答えを求めてください。

  • 頂点 i から頂点 j に向かう(単純とは限らない)パスのうち、辺に付けられたラベルの文字を順に結合した文字列が回文となるようなものの中で最も短いものの長さを求めよ。ただし、そのようなパスがない場合は答えは -1 とせよ。

制約

  • 1 \leq N \leq 100
  • N は整数
  • C_{i, j} は英小文字あるいは -

入力

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

N
C_{1, 1}C_{1, 2}\ldotsC_{1, N}
C_{2, 1}C_{2, 2}\ldotsC_{2, N}
\vdots
C_{N, 1}C_{N, 2}\ldotsC_{N, N}

出力

整数組 (i, j) に対する答えを A_{i, j} として以下の形式で出力せよ。

A_{1, 1} A_{1, 2} \ldots A_{1, N}
A_{2, 1} A_{2, 2} \ldots A_{2, N}
\vdots
A_{N, 1} A_{N, 2} \ldots A_{N, N}

入力例 1

4
ab--
--b-
---a
c---

出力例 1

0 1 2 4
-1 0 1 -1
3 -1 0 1
1 -1 -1 0

例として (i, j) = (1, 4) の場合について説明します。

頂点 1 \to 1 \to 2 \to 3 \to 4 というパスにおける辺に付けられた文字のラベルを順に結合すると文字列 abba が得られます。これは回文であり、頂点 1 から頂点 4 へ向かう長さ 3 以下のパスであって辺に付けられたラベルの文字を順に結合した文字列が回文となるものは存在しないため (i, j) = (1, 4) に対する答えは 4 となります。

空文字列も回文であることに注意してください。


入力例 2

5
us---
-st--
--s--
u--s-
---ts

出力例 2

0 1 3 -1 -1
-1 0 1 -1 -1
-1 -1 0 -1 -1
1 3 -1 0 -1
-1 -1 5 1 0

Score : 450 points

Problem Statement

We have a directed graph with N vertices, numbered 1, 2, \ldots, N.

Information about the edges is given by N^2 characters C_{1, 1}, C_{1, 2}, \ldots, C_{1, N}, C_{2, 1}, \ldots, C_{N, N}. Here, each C_{i, j} is either a lowercase English letter or -.

If C_{i, j} is a lowercase English letter, then there is exactly one directed edge from vertex i to vertex j labeled C_{i, j}. If C_{i, j} is -, there is no edge from vertex i to vertex j.

For each integer pair (i, j) with 1 \leq i, j \leq N, answer the following question:

  • Among all (not necessarily simple) paths from vertex i to vertex j whose concatenation of labels on the edges forms a palindrome, what is the length of the shortest such path? If there is no such path, the answer is -1.

Constraints

  • 1 \leq N \leq 100
  • N is an integer.
  • Each C_{i, j} is either a lowercase English letter or -.

Input

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

N
C_{1, 1}C_{1, 2}\ldotsC_{1, N}
C_{2, 1}C_{2, 2}\ldotsC_{2, N}
\vdots
C_{N, 1}C_{N, 2}\ldotsC_{N, N}

Output

Let A_{i, j} be the answer to the question for the pair (i, j). Print them in the following format:

A_{1, 1} A_{1, 2} \ldots A_{1, N}
A_{2, 1} A_{2, 2} \ldots A_{2, N}
\vdots
A_{N, 1} A_{N, 2} \ldots A_{N, N}

Sample Input 1

4
ab--
--b-
---a
c---

Sample Output 1

0 1 2 4
-1 0 1 -1
3 -1 0 1
1 -1 -1 0

For example, consider the case (i, j) = (1, 4).
By taking the path 1 \to 1 \to 2 \to 3 \to 4, and concatenating the labels on its edges in order, we get the string abba, which is a palindrome.
There is no path of length at most 3 from vertex 1 to vertex 4 whose concatenation of labels is a palindrome. Thus, the answer for (1, 4) is 4.

Note that the empty string is also a palindrome.


Sample Input 2

5
us---
-st--
--s--
u--s-
---ts

Sample Output 2

0 1 3 -1 -1
-1 0 1 -1 -1
-1 -1 0 -1 -1
1 3 -1 0 -1
-1 -1 5 1 0
I - Rook Score

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

配点 : 500 点

問題文

縦 10^9 マス、横 10^9 マスのマス目があります。上から i 番目、左から j 番目のマスを (i,j) と表記します。

i=1,2,\ldots,N に対し (r_i,c_i) には正整数 x_i が、他の 10^{18}-N 個のマスには 0 が書かれています。

あなたはあるマス (R,C) を選び、 (R,C) と行または列が同じ 2 \times 10^9 - 1 個のマスに書かれた整数の総和 S を求めました。

S として考えられる最大値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq r_i,c_i,x_i \leq 10^9
  • i \neq j ならば (r_i,c_i) \neq (r_j,c_j)
  • 入力はすべて整数

入力

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

N
r_1 c_1 x_1
\vdots
r_N c_N x_N

出力

答えを出力せよ。


入力例 1

4
1 1 2
1 2 9
2 1 8
3 2 3

出力例 1

20

(R,C) として (2,2) を選ぶと S が 20 となります。これが最大値です。


入力例 2

1
1 1000000000 1

出力例 2

1

入力例 3

15
158260522 877914575 602436426
24979445 861648772 623690081
433933447 476190629 262703497
211047202 971407775 628894325
731963982 822804784 450968417
430302156 982631932 161735902
880895728 923078537 707723857
189330739 910286918 802329211
404539679 303238506 317063340
492686568 773361868 125660016
650287940 839296263 462224593
492601449 384836991 191890310
576823355 782177068 404011431
818008580 954291757 160449218
155374934 840594328 164163676

出力例 3

1510053068

Score : 500 points

Problem Statement

We have a grid with 10^9 rows and 10^9 columns. Let (i,j) denote the square at the i-th row from the top and j-th column from the left.

For i=1,2,\ldots,N, a positive integer x_i is written on (r_i,c_i). On the other 10^{18}-N squares, 0 is written.

You choose a square (R,C) and compute the sum S of the integers written on the 2 \times 10^9 - 1 squares that share a row or column with (R,C).

Find the maximum possible value of S.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq r_i,c_i,x_i \leq 10^9
  • (r_i,c_i) \neq (r_j,c_j) if i \neq j.
  • All values in the input are integers.

Input

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

N
r_1 c_1 x_1
\vdots
r_N c_N x_N

Output

Print the answer.


Sample Input 1

4
1 1 2
1 2 9
2 1 8
3 2 3

Sample Output 1

20

If you choose (2,2) as (R,C), then S will be 20, which is the maximum possible value.


Sample Input 2

1
1 1000000000 1

Sample Output 2

1

Sample Input 3

15
158260522 877914575 602436426
24979445 861648772 623690081
433933447 476190629 262703497
211047202 971407775 628894325
731963982 822804784 450968417
430302156 982631932 161735902
880895728 923078537 707723857
189330739 910286918 802329211
404539679 303238506 317063340
492686568 773361868 125660016
650287940 839296263 462224593
492601449 384836991 191890310
576823355 782177068 404011431
818008580 954291757 160449218
155374934 840594328 164163676

Sample Output 3

1510053068