A - A to Z String 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

AN 個、BN 個、…、ZN 個この順に繋げて得られる文字列の先頭から X 番目の文字を求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq X \leq N\times 26
  • 入力は全て整数

入力

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

N X

出力

答えを出力せよ。


入力例 1

1 3

出力例 1

C

得られる文字列は ABCDEFGHIJKLMNOPQRSTUVWXYZ です。先頭から 3 番目の文字は C です。


入力例 2

2 12

出力例 2

F

得られる文字列は AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ です。先頭から 12 番目の文字は F です。

Score : 100 points

Problem Statement

Find the X-th character from the beginning of the string that is obtained by concatenating these characters: N copies of A's, N copies of B's, …, and N copies of Z's, in this order.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq X \leq N\times 26
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X

Output

Print the answer.


Sample Input 1

1 3

Sample Output 1

C

We obtain the string ABCDEFGHIJKLMNOPQRSTUVWXYZ, whose 3-rd character from the beginning is C.


Sample Input 2

2 12

Sample Output 2

F

We obtain the string AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ, whose 12-th character from the beginning is F.

B - Weekly Records

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

高橋君は N 週間の間、自分が歩いた歩数を記録しました。i 日目に歩いた歩数は A_i でした。

各週に高橋君が歩いた歩数の合計を求めてください。
より正確には、1 週目( 1 日目から 7 日目まで)の 7 日間の歩数の合計、2 週目( 8 日目から 14 日目まで)の 7 日間の歩数の合計、……をそれぞれ求めてください。

制約

  • 1 \leq N \leq 10
  • 0 \leq A_i \leq 10^5
  • 入力は整数

入力

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

N
A_1 A_2 \ldots A_{7N}

出力

i 週目に歩いた歩数を B_i とする。B_1,B_2,\ldots,B_N をこの順に空白区切りで出力せよ。


入力例 1

2
1000 2000 3000 4000 5000 6000 7000 2000 3000 4000 5000 6000 7000 8000

出力例 1

28000 35000

高橋君は 1 週目に 1000+2000+3000+4000+5000+6000+7000=28000 歩あるき、2 週目に 2000+3000+4000+5000+6000+7000+8000=35000 歩あるきました。


入力例 2

3
14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59230 78164 6286 20899 86280 34825 34211 70679 82148

出力例 2

314333 419427 335328

Score : 100 points

Problem Statement

Takahashi has recorded the number of steps he walked for N weeks. He walked A_i steps on the i-th day.

Find the total number of steps Takahashi walked each week.
More precisely, find the sum of the steps for the first week (the 1-st through 7-th day), the sum of the steps for the second week (the 8-th through 14-th day), and so on.

Constraints

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

Input

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

N
A_1 A_2 \ldots A_{7N}

Output

Let B_i be the number of steps walked for the i-th week. Print B_1,B_2,\ldots,B_N in this order, separated by spaces.


Sample Input 1

2
1000 2000 3000 4000 5000 6000 7000 2000 3000 4000 5000 6000 7000 8000

Sample Output 1

28000 35000

For the first week, he walked 1000+2000+3000+4000+5000+6000+7000=28000 steps, and for the second week, he walked 2000+3000+4000+5000+6000+7000+8000=35000 steps.


Sample Input 2

3
14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59230 78164 6286 20899 86280 34825 34211 70679 82148

Sample Output 2

314333 419427 335328
C - Practical Computing

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

以下のような N 個の整数列 A_0,\ldots,A_{N-1} を求めてください。

  • i (0\leq i \leq N-1) について、A_i の長さは i+1 である。
  • i,j (0\leq i \leq N-1, 0 \leq j \leq i) について、A_ij+1 番目の値 a_{i,j} は次のように定められる。

    • j=0 または j=i の時、a_{i,j}=1
    • それ以外の時、a_{i,j} = a_{i-1,j-1} + a_{i-1,j}

制約

  • 1 \leq N \leq 30
  • N は整数

入力

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

N

出力

N 行出力せよ。 i 行目には A_{i-1} の値を順に空白区切りで出力せよ。


入力例 1

3

出力例 1

1
1 1
1 2 1

入力例 2

10

出力例 2

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

Score : 200 points

Problem Statement

Find the N integer sequences A_0,\ldots,A_{N-1} defined as follows.

  • For each i (0\leq i \leq N-1), the length of A_i is i+1.
  • For each i and j (0\leq i \leq N-1, 0 \leq j \leq i), the (j+1)-th term of A_i, denoted by a_{i,j}, is defined as follows.
    • a_{i,j}=1, if j=0 or j=i.
    • a_{i,j} = a_{i-1,j-1} + a_{i-1,j}, otherwise.

Constraints

  • 1 \leq N \leq 30
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print N lines. The i-th line should contain the terms of A_{i-1} separated by spaces.


Sample Input 1

3

Sample Output 1

1
1 1
1 2 1

Sample Input 2

10

Sample Output 2

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
D - Star or Not

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

N 頂点 N-1 辺の木が与えられます。
頂点には 1,2,\ldots,N の番号がついており、i 本目の辺は頂点 a_i と頂点 b_i を結んでいます。

この木がスターであるか判定してください。

ただしスターとは、1 つの頂点から、他の全ての頂点に 1 本ずつ辺が出ている木のことです。

注記

「木」については、Wikipedia「木(数学)」 を参照してください。

制約

  • 3 \leq N \leq 10^5
  • 1 \leq a_i \lt b_i \leq N
  • 与えられるグラフは木である

入力

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

N
a_1 b_1
\vdots
a_{N-1} b_{N-1}

出力

与えられたグラフがスターであるなら Yes と、スターでないなら No と出力せよ。


入力例 1

5
1 4
2 4
3 4
4 5

出力例 1

Yes

与えられたグラフはスターです。


入力例 2

4
2 4
1 4
2 3

出力例 2

No

与えられたグラフはスターではありません。


入力例 3

10
9 10
3 10
4 10
8 10
1 10
2 10
7 10
6 10
5 10

出力例 3

Yes

Score : 200 points

Problem Statement

You are given a tree with N vertices and N-1 edges.
The vertices are numbered 1,2,\ldots,N. The i-th edge connects Vertex a_i and Vertex b_i.

Determine whether this tree is a star.

Here, a star is a tree where there is a vertex directly connected to all other vertices.

Notes

For the definition of a tree, see Tree (graph theory) - Wikipedia.

Constraints

  • 3 \leq N \leq 10^5
  • 1 \leq a_i \lt b_i \leq N
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
\vdots
a_{N-1} b_{N-1}

Output

If the given graph is a star, print Yes; otherwise, print No.


Sample Input 1

5
1 4
2 4
3 4
4 5

Sample Output 1

Yes

The given graph is a star.


Sample Input 2

4
2 4
1 4
2 3

Sample Output 2

No

The given graph is not a star.


Sample Input 3

10
9 10
3 10
4 10
8 10
1 10
2 10
7 10
6 10
5 10

Sample Output 3

Yes
E - Submask

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

非負整数 N が与えられるので、以下の条件を満たす非負整数 x を昇順に全て出力してください。

  • x2 進数として表記した時に 1 となる位の集合が、 N2 進数として表記した時に 1 となる位の集合の部分集合となる。
    • すなわち、全ての非負整数 k について、「 x2^k の位が 1 ならば、 N2^k の位は 1 」が成り立つ。

制約

  • N は整数
  • 0 \le N < 2^{60}
  • N2 進数として表記した時、 1 となる位は 15 個以下である

入力

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

N

出力

答えを 1 行に 1 つずつ、10 進法の整数として昇順に出力せよ。


入力例 1

11

出力例 1

0
1
2
3
8
9
10
11

N = 11_{(10)}2 進数で表記すると、 1011_{(2)} となります。
条件を満たす非負整数 x は以下の通りです。

  • 0000_{(2)}=0_{(10)}
  • 0001_{(2)}=1_{(10)}
  • 0010_{(2)}=2_{(10)}
  • 0011_{(2)}=3_{(10)}
  • 1000_{(2)}=8_{(10)}
  • 1001_{(2)}=9_{(10)}
  • 1010_{(2)}=10_{(10)}
  • 1011_{(2)}=11_{(10)}

入力例 2

0

出力例 2

0

入力例 3

576461302059761664

出力例 3

0
524288
549755813888
549756338176
576460752303423488
576460752303947776
576461302059237376
576461302059761664

入力は 32bit 符号付き整数に収まらない可能性があります。

Score : 300 points

Problem Statement

You are given a non-negative integer N. Print all non-negative integers x that satisfy the following condition in ascending order.

  • The set of the digit positions containing 1 in the binary representation of x is a subset of the set of the digit positions containing 1 in the binary representation of N.
    • That is, the following holds for every non-negative integer k: if the digit in the "2^ks" place of x is 1, the digit in the 2^ks place of N is 1.

Constraints

  • N is an integer.
  • 0 \le N < 2^{60}
  • In the binary representation of N, at most 15 digit positions contain 1.

Input

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

N

Output

Print the answer as decimal integers in ascending order, each in its own line.


Sample Input 1

11

Sample Output 1

0
1
2
3
8
9
10
11

The binary representation of N = 11_{(10)} is 1011_{(2)}.
The non-negative integers x that satisfy the condition are:

  • 0000_{(2)}=0_{(10)}
  • 0001_{(2)}=1_{(10)}
  • 0010_{(2)}=2_{(10)}
  • 0011_{(2)}=3_{(10)}
  • 1000_{(2)}=8_{(10)}
  • 1001_{(2)}=9_{(10)}
  • 1010_{(2)}=10_{(10)}
  • 1011_{(2)}=11_{(10)}

Sample Input 2

0

Sample Output 2

0

Sample Input 3

576461302059761664

Sample Output 3

0
524288
549755813888
549756338176
576460752303423488
576460752303947776
576461302059237376
576461302059761664

The input may not fit into a 32-bit signed integer.

F - Merge Sequences

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

長さ N の狭義単調増加列 A=(A _ 1,A _ 2,\ldots,A _ N) と、長さ M の狭義単調増加列 B=(B _ 1,B _ 2,\ldots,B _ M) が与えられます。 ここで、すべての i,j\ (1\leq i\leq N,1\leq j\leq M) について A _ i\neq B _ j が成り立っています。

長さ N+M の狭義単調増加列 C=(C _ 1,C _ 2,\ldots,C _ {N+M}) を、次の操作を行った結果得られる列として定めます。

  • CAB を連結した列とする。厳密には、i=1,2,\ldots,N について C _ i=A _ i とし、i=N+1,N+2,\ldots,N+M について C _ i=B _ {i-N} とする。
  • C を昇順にソートする。

A _ 1,A _ 2,\ldots,A _ NB _ 1,B _ 2,\ldots,B _ M がそれぞれ C では何番目にあるか求めてください。 より厳密には、まず i=1,2,\ldots,N について C _ k=A _ i を満たす k を順に求めたのち、j=1,2,\ldots,M について C _ k=B _ j を満たす k を順に求めてください。

制約

  • 1\leq N,M\leq 10^5
  • 1\leq A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq 10^9
  • 1\leq B _ 1\lt B _ 2\lt\cdots\lt B _ M\leq 10^9
  • すべての i,j\ (1\leq i\leq N,1\leq j\leq M) について A _ i\neq B _ j
  • 入力はすべて整数

入力

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

N M
A _ 1 A _ 2 \ldots A _ N
B _ 1 B _ 2 \ldots B _ M

出力

答えを 2 行で出力せよ。
1 行目には A _ 1,A _ 2,\ldots,A _ N がそれぞれ C では何番目にあるかを空白区切りで出力せよ。
2 行目には B _ 1,B _ 2,\ldots,B _ M がそれぞれ C では何番目にあるかを空白区切りで出力せよ。


入力例 1

4 3
3 14 15 92
6 53 58

出力例 1

1 3 4 7
2 5 6

C(3,6,14,15,53,58,92) となります。 A=(3,14,15,92) の要素はそれぞれ 1,3,4,7 番目にあり、B=(6,53,58) の要素はそれぞれ 2,5,6 番目にあります。


入力例 2

4 4
1 2 3 4
100 200 300 400

出力例 2

1 2 3 4
5 6 7 8

入力例 3

8 12
3 4 10 15 17 18 22 30
5 7 11 13 14 16 19 21 23 24 27 28

出力例 3

1 2 5 9 11 12 15 20
3 4 6 7 8 10 13 14 16 17 18 19

Score : 300 points

Problem Statement

You are given strictly increasing sequences of length N and M: A=(A _ 1,A _ 2,\ldots,A _ N) and B=(B _ 1,B _ 2,\ldots,B _ M). Here, A _ i\neq B _ j for every i and j (1\leq i\leq N,1\leq j\leq M).

Let C=(C _ 1,C _ 2,\ldots,C _ {N+M}) be a strictly increasing sequence of length N+M that results from the following procedure.

  • Let C be the concatenation of A and B. Formally, let C _ i=A _ i for i=1,2,\ldots,N, and C _ i=B _ {i-N} for i=N+1,N+2,\ldots,N+M.
  • Sort C in ascending order.

For each of A _ 1,A _ 2,\ldots,A _ N, B _ 1,B _ 2,\ldots,B _ M, find its position in C. More formally, for each i=1,2,\ldots,N, find k such that C _ k=A _ i, and for each j=1,2,\ldots,M, find k such that C _ k=B _ j.

Constraints

  • 1\leq N,M\leq 10^5
  • 1\leq A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq 10^9
  • 1\leq B _ 1\lt B _ 2\lt\cdots\lt B _ M\leq 10^9
  • A _ i\neq B _ j for every i and j (1\leq i\leq N,1\leq j\leq M).
  • All values in the input are integers.

Input

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

N M
A _ 1 A _ 2 \ldots A _ N
B _ 1 B _ 2 \ldots B _ M

Output

Print the answer in two lines.
The first line should contain the positions of A _ 1,A _ 2,\ldots,A _ N in C, with spaces in between.
The second line should contain the positions of B _ 1,B _ 2,\ldots,B _ M in C, with spaces in between.


Sample Input 1

4 3
3 14 15 92
6 53 58

Sample Output 1

1 3 4 7
2 5 6

C will be (3,6,14,15,53,58,92). Here, the 1-st, 3-rd, 4-th, 7-th elements are from A=(3,14,15,92), and the 2-nd, 5-th, 6-th elements are from B=(6,53,58).


Sample Input 2

4 4
1 2 3 4
100 200 300 400

Sample Output 2

1 2 3 4
5 6 7 8

Sample Input 3

8 12
3 4 10 15 17 18 22 30
5 7 11 13 14 16 19 21 23 24 27 28

Sample Output 3

1 2 5 9 11 12 15 20
3 4 6 7 8 10 13 14 16 17 18 19
G - Flip and Adjust

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

両面に整数が書かれたカードが N 枚あり、i \, (1 \leq i \leq N) 枚目のカードの表には a_i が、裏には b_i が書かれています。

あなたは、それぞれのカードについて、表を上に向けて置くか裏を上に向けて置くかを自由に決めることができます。

上に向けられた面に書かれた整数の総和がちょうど S となるようにカードを置くことができるか判定し、可能ならそのようなカードの置き方の一例を求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq S \leq 10000
  • 1 \leq a_i, b_i \leq 100 \, (1 \leq i \leq N)
  • 入力は全て整数

入力

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

N S
a_1 b_1
\vdots
a_N b_N

出力

まず、上に向けられた面に書かれた整数の総和がちょうど S となるようにカードを置くことができるならば Yes を、できなければ No を出力し、改行せよ。

さらに、そのようにカードを置くことができるならば、カードの置き方を表す H および T のみからなる長さ N の文字列を出力せよ。
この文字列の i \, (1 \leq i \leq N) 文字目は、i 枚目のカードを表を上に向けて置くなら H、裏を上に向けて置くなら T でなければならない。
条件を満たすカードの置き方が複数考えられる場合、どれを出力しても正解となる。


入力例 1

3 11
1 4
2 3
5 7

出力例 1

Yes
THH

例えば次のように置くことで、上に向けられた面に書かれた整数の総和がちょうど S (= 11) となります。

  • 1 枚目は表、2 枚目は裏、3 枚目は裏を上に向けて置く。
  • 1 枚目は裏、2 枚目は表、3 枚目は表を上に向けて置く。

よって、HTTTHH といった出力が正解となります。


入力例 2

5 25
2 8
9 3
4 11
5 1
12 6

出力例 2

No

上に向けられた面に書かれた整数の総和がちょうど S (= 25) となるようにカードを置くことはできません。

Score : 400 points

Problem Statement

There are N cards with an integer written on each side. Card i (1 \leq i \leq N) has an integer a_i written on the front and an integer b_i written on the back.

You may choose whether to place each card with its front or back side visible.

Determine if you can place the cards so that the sum of the visible integers exactly equals S. If possible, find a placement of the cards to realize it.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq S \leq 10000
  • 1 \leq a_i, b_i \leq 100 \, (1 \leq i \leq N)
  • All values in the input are integers.

Input

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

N S
a_1 b_1
\vdots
a_N b_N

Output

First, print Yes if you can make the sum of the visible integers exactly equal S, and No otherwise, followed by a newline.

Additionally, if such a placement is possible, print a string of length N consisting of H and T that represents the placement of the cards to realize it.
The i-th (1 \leq i \leq N) character should be H if the i-th card should be placed with its front side visible, and T with its back.
If there are multiple possible placements to realize the sum, printing any of them is accepted.


Sample Input 1

3 11
1 4
2 3
5 7

Sample Output 1

Yes
THH

For example, the following placements make the sum of the visible integers exactly equal S (= 11):

  • Place the 1-st card with its front side visible, 2-nd with its back, and 3-rd with its back.
  • Place the 1-st card with its back side visible, 2-nd with its front, and 3-rd with its front.

Therefore, outputs like HTT and THH are accepted.


Sample Input 2

5 25
2 8
9 3
4 11
5 1
12 6

Sample Output 2

No

You cannot place the cards so that the sum of the visible integers exactly equals S (= 25).

H - Warp

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

2 次元平面の原点に高橋君がいます。
高橋君はこれから、ワープを N 回繰り返します。各ワープでは、以下の 3 つのうちいずれか 1 つを行います。

  • 現在いる座標 (x,y) から (x+A,y+B) に移動する
  • 現在いる座標 (x,y) から (x+C,y+D) に移動する
  • 現在いる座標 (x,y) から (x+E,y+F) に移動する

平面上の M 箇所 (X_1,Y_1),\ldots,(X_M,Y_M) には障害物があり、これらの座標に移動することはできません。

N 回のワープによる移動経路として考えられるものは何通りですか? 998244353 で割ったあまりを求めてください。

制約

  • 1 \leq N \leq 300
  • 0 \leq M \leq 10^5
  • -10^9 \leq A,B,C,D,E,F \leq 10^9
  • (A,B),(C,D),(E,F) は相異なる
  • -10^9 \leq X_i,Y_i \leq 10^9
  • (X_i,Y_i)\neq(0,0)
  • (X_i,Y_i) は相異なる
  • 入力に含まれる値は全て整数である

入力

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

N M
A B C D E F
X_1 Y_1
X_2 Y_2
\vdots
X_M Y_M

出力

答えを出力せよ。


入力例 1

2 2
1 1 1 2 1 3
1 2
2 2

出力例 1

5

以下の 5 通りが考えられます。

  • (0,0)\to(1,1)\to(2,3)
  • (0,0)\to(1,1)\to(2,4)
  • (0,0)\to(1,3)\to(2,4)
  • (0,0)\to(1,3)\to(2,5)
  • (0,0)\to(1,3)\to(2,6)

入力例 2

10 3
-1000000000 -1000000000 1000000000 1000000000 -1000000000 1000000000
-1000000000 -1000000000
1000000000 1000000000
-1000000000 1000000000

出力例 2

0

入力例 3

300 0
0 0 1 0 0 1

出力例 3

292172978

Score : 500 points

Problem Statement

Takahashi is at the origin of a two-dimensional plane.
Takahashi will repeat teleporting N times. In each teleportation, he makes one of the following moves:

  • Move from the current coordinates (x,y) to (x+A,y+B)
  • Move from the current coordinates (x,y) to (x+C,y+D)
  • Move from the current coordinates (x,y) to (x+E,y+F)

There are obstacles on M points (X_1,Y_1),\ldots,(X_M,Y_M) on the plane; he cannot teleport to these coordinates.

How many paths are there resulting from the N teleportations? Find the count modulo 998244353.

Constraints

  • 1 \leq N \leq 300
  • 0 \leq M \leq 10^5
  • -10^9 \leq A,B,C,D,E,F \leq 10^9
  • (A,B), (C,D), and (E,F) are distinct.
  • -10^9 \leq X_i,Y_i \leq 10^9
  • (X_i,Y_i)\neq(0,0)
  • (X_i,Y_i) are distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A B C D E F
X_1 Y_1
X_2 Y_2
\vdots
X_M Y_M

Output

Print the answer.


Sample Input 1

2 2
1 1 1 2 1 3
1 2
2 2

Sample Output 1

5

The following 5 paths are possible:

  • (0,0)\to(1,1)\to(2,3)
  • (0,0)\to(1,1)\to(2,4)
  • (0,0)\to(1,3)\to(2,4)
  • (0,0)\to(1,3)\to(2,5)
  • (0,0)\to(1,3)\to(2,6)

Sample Input 2

10 3
-1000000000 -1000000000 1000000000 1000000000 -1000000000 1000000000
-1000000000 -1000000000
1000000000 1000000000
-1000000000 1000000000

Sample Output 2

0

Sample Input 3

300 0
0 0 1 0 0 1

Sample Output 3

292172978
I - Rook Score

Time Limit: 3 sec / Memory Limit: 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) を選ぶと S20 となります。これが最大値です。


入力例 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