A - Scoreboard

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

配点 : 100 点

問題文

チーム高橋とチーム青木が N 回の試合を行いました。 i 回め (1\leq i\leq N) の試合ではチーム高橋が X _ i 点、チーム青木が Y _ i 点を獲得しました。

N 回の試合で獲得した得点の合計がより多いチームの勝ちです。

どちらのチームが勝ったか出力してください。 ただし、獲得した得点の合計が等しい場合は引き分けとなります。

制約

  • 1\leq N\leq 100
  • 0\leq X _ i\leq 100\ (1\leq i\leq N)
  • 0\leq Y _ i\leq 100\ (1\leq i\leq N)
  • 入力はすべて整数

入力

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

N
X _ 1 Y _ 1
X _ 2 Y _ 2
\vdots
X _ N Y _ N

出力

チーム高橋が勝った場合 Takahashi を、チーム青木が勝った場合 Aoki を、引き分けの場合 Draw を出力せよ。


入力例 1

4
10 2
10 1
10 2
3 2

出力例 1

Takahashi

4 回の試合で、チーム高橋は 33 点、チーム青木は 7 点を獲得しました。 チーム高橋が勝ったため、Takahashi を出力してください。


入力例 2

6
5 4
4 5
2 4
1 6
7 1
3 2

出力例 2

Draw

いずれのチームも 22 点を獲得しました。 引き分けなので、Draw を出力してください。


入力例 3

4
0 0
10 10
50 50
0 100

出力例 3

Aoki

一方もしくは両方のチームが、一試合のうちに 1 点も取れない場合もあります。

Score: 100 points

Problem Statement

Team Takahashi and Team Aoki played N matches. In the i-th match (1\leq i\leq N), Team Takahashi scored X _ i points, and Team Aoki scored Y _ i points.

The team with the higher total score from the N matches wins.

Print the winner. If the two teams have the same total score, it is a draw.

Constraints

  • 1\leq N\leq 100
  • 0\leq X _ i\leq 100\ (1\leq i\leq N)
  • 0\leq Y _ i\leq 100\ (1\leq i\leq N)
  • All input values are integers.

Input

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

N
X _ 1 Y _ 1
X _ 2 Y _ 2
\vdots
X _ N Y _ N

Output

If Team Takahashi wins, print Takahashi; if Team Aoki wins, print Aoki; if it is a draw, print Draw.


Sample Input 1

4
10 2
10 1
10 2
3 2

Sample Output 1

Takahashi

In four matches, Team Takahashi scored 33 points, and Team Aoki scored 7 points. Team Takahashi wins, so print Takahashi.


Sample Input 2

6
5 4
4 5
2 4
1 6
7 1
3 2

Sample Output 2

Draw

Both teams scored 22 points. It is a draw, so print Draw.


Sample Input 3

4
0 0
10 10
50 50
0 100

Sample Output 3

Aoki

One or both teams may score no points in a match.

B - Job Interview

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

配点 : 100 点

問題文

高橋君はある会社の採用面接を受けました。

面接官の人数 N と、各面接官の高橋君への評価を表す長さ N の文字列 S が与えられます。
i=1,2,\ldots,N に対し S の i 文字目が i 番目の面接官の評価に対応し、o は「良」、- は「可」、x は 「不可」を表します。

高橋君は以下の 2 つの条件を両方満たすならば合格、そうでなければ不合格です。

  • 「良」と評価した面接官が少なくとも 1 人いる
  • 「不可」と評価した面接官がいない

高橋君が合格かどうかを判定してください。

制約

  • 1 \leq N \leq 100
  • S は o, -, x のみからなる長さが N の文字列

入力

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

N
S

出力

高橋君が合格ならば Yes と、そうでなければ No と出力せよ。


入力例 1

4
oo--

出力例 1

Yes

1, 2 番目の面接官が「良」と評価していて、さらに「不可」と評価した面接官がいないため合格です。


入力例 2

3
---

出力例 2

No

「良」と評価した面接官が 1 人もいないため不合格です。


入力例 3

1
o

出力例 3

Yes

入力例 4

100
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooox

出力例 4

No

100 番目の面接官が「不可」と評価しているため不合格です。

Score : 100 points

Problem Statement

Takahashi had a job interview.

You are given the number of interviewers, N, and a string S of length N representing the interviewers' evaluations of him.
For each i=1,2,\ldots,N, the i-th character of S corresponds to the i-th interviewer's evaluation; o means Good, - means Fair, and x means Poor.

Takahashi will pass if both of the following conditions are satisfied, and fail otherwise.

  • At least one interviewer's evaluation is Good.
  • No interviewer's evaluation is Poor.

Determine whether Takahashi passes.

Constraints

  • 1 \leq N \leq 100
  • S is a string of length N consisting of o, -, and x.

Input

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

N
S

Output

If Takahashi passes, print Yes; otherwise, print No.


Sample Input 1

4
oo--

Sample Output 1

Yes

The first and second interviewers' evaluations are Good, and no interviewer's evaluation is Poor, so he passes.


Sample Input 2

3
---

Sample Output 2

No

No interviewer's evaluation is Good, so he fails.


Sample Input 3

1
o

Sample Output 3

Yes

Sample Input 4

100
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooox

Sample Output 4

No

The 100-th interviewer's evaluation is Poor, so he fails.

C - ABC-DEF

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

配点 : 200 点

問題文

非負整数 A,B,C,D,E,F があり、A\times B\times C\geq D\times E\times F をみたしています。
(A\times B\times C)-(D\times E\times F) の値を 998244353 で割った余りを求めてください。

制約

  • 0\leq A,B,C,D,E,F\leq 10^{18}
  • A\times B\times C\geq D\times E\times F
  • A,B,C,D,E,F は整数

入力

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

A B C D E F

出力

(A\times B\times C)-(D\times E\times F) を 998244353 で割った余りを整数で出力せよ。


入力例 1

2 3 5 1 2 4

出力例 1

22

A\times B\times C=2\times 3\times 5=30, D\times E\times F=1\times 2\times 4=8 より、
(A\times B\times C)-(D\times E\times F)=22 であり、これを 998244353 で割った余りである 22 を出力します。


入力例 2

1 1 1000000000 0 0 0

出力例 2

1755647

A\times B\times C=1000000000, D\times E\times F=0 より、
(A\times B\times C)-(D\times E\times F)=1000000000 であり、これを 998244353 で割った余りである 1755647 を出力します。


入力例 3

1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000

出力例 3

0

(A\times B\times C)-(D\times E\times F)=0 であり、これを 998244353 で割った余りである 0 を出力します。

Score : 200 points

Problem Statement

There are non-negative integers A, B, C, D, E, and F, which satisfy A\times B\times C\geq D\times E\times F.
Find the remainder when (A\times B\times C)-(D\times E\times F) is divided by 998244353.

Constraints

  • 0\leq A,B,C,D,E,F\leq 10^{18}
  • A\times B\times C\geq D\times E\times F
  • A, B, C, D, E, and F are integers.

Input

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

A B C D E F

Output

Print the remainder when (A\times B\times C)-(D\times E\times F) is divided by 998244353, as an integer.


Sample Input 1

2 3 5 1 2 4

Sample Output 1

22

Since A\times B\times C=2\times 3\times 5=30 and D\times E\times F=1\times 2\times 4=8,
we have (A\times B\times C)-(D\times E\times F)=22. Divide this by 998244353 and print the remainder, which is 22.


Sample Input 2

1 1 1000000000 0 0 0

Sample Output 2

1755647

Since A\times B\times C=1000000000 and D\times E\times F=0,
we have (A\times B\times C)-(D\times E\times F)=1000000000. Divide this by 998244353 and print the remainder, which is 1755647.


Sample Input 3

1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000

Sample Output 3

0

We have (A\times B\times C)-(D\times E\times F)=0. Divide this by 998244353 and print the remainder, which is 0.

D - Substring

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

配点 : 200 点

問題文

英小文字からなる文字列 S が与えられます。S の空でない部分文字列は何種類ありますか?

ただし、部分文字列とは連続する部分列のことを指します。例えば、xxx は yxxxy の部分文字列ですが、xxyxx の部分文字列ではありません。

制約

  • S は英小文字からなる長さ 1 以上 100 以下の文字列

入力

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

S

出力

答えを出力せよ。


入力例 1

yay

出力例 1

5

S の空でない部分文字列は以下の 5 種類です。

  • a
  • y
  • ay
  • ya
  • yay

入力例 2

aababc

出力例 2

17

入力例 3

abracadabra

出力例 3

54

Score: 200 points

Problem Statement

You are given a string S consisting of lowercase English letters. How many different non-empty substrings does S have?

A substring is a contiguous subsequence. For example, xxx is a substring of yxxxy but not of xxyxx.

Constraints

  • S is a string of length between 1 and 100, inclusive, consisting of lowercase English letters.

Input

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

S

Output

Print the answer.


Sample Input 1

yay

Sample Output 1

5

S has the following five different non-empty substrings:

  • a
  • y
  • ay
  • ya
  • yay

Sample Input 2

aababc

Sample Output 2

17

Sample Input 3

abracadabra

Sample Output 3

54
E - ~

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

配点 : 350 点

問題文

整数列 A = (A_1, A_2, \ldots, A_{|A|}) に対し、A が チルダ型 とは以下の 4 つの条件をすべて満たすことであると定義します。

  • A の長さ |A| は 4 以上である
  • A_1 < A_2 である
  • A_{i - 1} < A_i > A_{i + 1} を満たす 2 \leq i < |A| なる整数 i はちょうど 1 個である 
  • A_{i - 1} > A_i < A_{i + 1} を満たす 2 \leq i < |A| なる整数 i はちょうど 1 個である 

数列 (1, 2, \ldots, N) を並べ替えて得られる数列 P = (P_1, P_2, \ldots, P_N) が与えられます。P の連続部分列であってチルダ型である数列の個数を求めてください。

制約

  • 4 \leq N \leq 3 \times 10^5
  • P = (P_1, P_2, \ldots, P_N) は (1, 2, \ldots, N) を並べ替えて得られる数列
  • 入力される値はすべて整数

入力

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

N
P_1 P_2 \ldots P_N

出力

答えを出力せよ。


入力例 1

6
1 3 6 4 2 5

出力例 1

2

数列 (1, 3, 6, 4, 2, 5) の連続部分列のうちチルダ型であるものは (3, 6, 4, 2, 5), (1, 3, 6, 4, 2, 5) の 2 つのみです。


入力例 2

6
1 2 3 4 5 6

出力例 2

0

入力例 3

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

出力例 3

4

Score : 350 points

Problem Statement

For an integer sequence A = (A_1, A_2, \ldots, A_{|A|}), we say that A is tilde-shaped if it satisfies all of the following four conditions:

  • The length |A| is at least 4.
  • A_1 < A_2.
  • There exists exactly one integer i with 2 \leq i < |A| such that A_{i-1} < A_i > A_{i+1}.
  • There exists exactly one integer i with 2 \leq i < |A| such that A_{i-1} > A_i < A_{i+1}.

You are given a permutation P = (P_1, P_2, \ldots, P_N) of (1, 2, \ldots, N). Find the number of (contiguous) subarrays of P that are tilde-shaped.

Constraints

  • 4 \leq N \leq 3 \times 10^5
  • P = (P_1, P_2, \ldots, P_N) is a permutation of (1, 2, \ldots, N).
  • All input values are integers.

Input

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

N
P_1 P_2 \ldots P_N

Output

Output the answer.


Sample Input 1

6
1 3 6 4 2 5

Sample Output 1

2

Among the subarrays of (1, 3, 6, 4, 2, 5), exactly two are tilde-shaped: (3, 6, 4, 2, 5) and (1, 3, 6, 4, 2, 5).


Sample Input 2

6
1 2 3 4 5 6

Sample Output 2

0

Sample Input 3

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

Sample Output 3

4
F - Leftover Recipes

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

配点 : 300 点

問題文

冷蔵庫に N 種類の材料があります。これらを材料 1、\dots、材料 N と呼びます。材料 i は Q_i グラムあります。

あなたは 2 種類の料理を作れます。料理 A は、1 人分を作るのに各材料 i (1 \leq i \leq N) が A_i グラム必要です。料理 B は、1 人分を作るのに各材料 i が B_i グラム必要です。どちらも整数人分しか作れません。

冷蔵庫にある材料のみを使って、最大で合計何人分の料理を作れますか。

制約

  • 1 \leq N \leq 10
  • 1 \leq Q_i \leq 10^6
  • 0 \leq A_i \leq 10^6
  • A_i \geq 1 であるような i が存在する。
  • 0 \leq B_i \leq 10^6
  • B_i \geq 1 であるような i が存在する。
  • 入力値はすべて整数である。

入力

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

N
Q_1 Q_2 \dots Q_N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

出力

最大で合計 S 人分の料理を作れるとして、整数 S を出力せよ。


入力例 1

2
800 300
100 100
200 10

出力例 1

5

この冷蔵庫には、800 グラムの材料 1 と 300 グラムの材料 2 があります。

100 グラムの材料 1 と 100 グラムの材料 2 で料理 A を 1 人分作れ、200 グラムの材料 1 と 10 グラムの材料 2 で料理 B を 1 人分作れます。

料理 A を 2 人分、料理 B を 3 人分作るのに必要な材料 1 の量は 100 \times 2 + 200 \times 3 = 800 グラム、材料 2 の量は 100 \times 2 + 10 \times 3 = 230 グラムで、いずれも冷蔵庫にある量を超えません。このようにして合計 5 人分の料理を作ることができますが、6 人分を作る方法はなく、答えは 5 です。


入力例 2

2
800 300
100 0
0 10

出力例 2

38

800 グラムの材料 1 で料理 A を 8 人分、300 グラムの材料 2 で料理 B を 30 人分、合計 38 人分作れます。


入力例 3

2
800 300
801 300
800 301

出力例 3

0

何も作れません。


入力例 4

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

出力例 4

222222

Score: 300 points

Problem Statement

Your refrigerator has N kinds of ingredients. Let us call them ingredient 1, \dots, ingredient N. You have Q_i grams of ingredient i.

You can make two types of dishes. To make one serving of dish A, you need A_i grams of each ingredient i (1 \leq i \leq N). To make one serving of dish B, you need B_i grams of each ingredient i. You can only make an integer number of servings of each type of dish.

Using only the ingredients in the refrigerator, what is the maximum total number of servings of dishes you can make?

Constraints

  • 1 \leq N \leq 10
  • 1 \leq Q_i \leq 10^6
  • 0 \leq A_i \leq 10^6
  • There is an i such that A_i \geq 1.
  • 0 \leq B_i \leq 10^6
  • There is an i such that B_i \geq 1.
  • All input values are integers.

Input

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

N
Q_1 Q_2 \dots Q_N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

Output

Assuming that you can make a maximum total of S servings of dishes, print the integer S.


Sample Input 1

2
800 300
100 100
200 10

Sample Output 1

5

This refrigerator has 800 grams of ingredient 1 and 300 grams of ingredient 2.

You can make one serving of dish A with 100 grams of ingredient 1 and 100 grams of ingredient 2, and one serving of dish B with 200 grams of ingredient 1 and 10 grams of ingredient 2.

To make two servings of dish A and three servings of dish B, you need 100 \times 2 + 200 \times 3 = 800 grams of ingredient 1, and 100 \times 2 + 10 \times 3 = 230 grams of ingredient 2, neither of which exceeds the amount available in the refrigerator. In this way, you can make a total of five servings of dishes, but there is no way to make six, so the answer is 5.


Sample Input 2

2
800 300
100 0
0 10

Sample Output 2

38

You can make 8 servings of dish A with 800 grams of ingredient 1, and 30 servings of dish B with 300 grams of ingredient 2, for a total of 38 servings.


Sample Input 3

2
800 300
801 300
800 301

Sample Output 3

0

You cannot make any dishes.


Sample Input 4

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

Sample Output 4

222222
G - Synchronized Players

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

配点 : 400 点

問題文

N 行 N 列のグリッドがあり、各マスは空きマスか障害物のあるマスのいずれかです。グリッドの上から i 行目、左から j 列目のマスを (i, j) と表記します。

また、2 人のプレイヤーがグリッド上の相異なる空きマス上におり、各マスの情報は N 個の長さ N の文字列 S_1, S_2, \ldots, S_N として以下の形式で与えられます。

  • S_i の j 文字目が P であるとき、(i, j) は空きマスであり、プレイヤーがいる

  • S_i の j 文字目が . であるとき、(i, j) は空きマスであり、プレイヤーがいない

  • S_i の j 文字目が # であるとき、(i, j) は障害物のあるマスである

以下の操作を繰り返し 2 人のプレイヤーを同じマスに集めるために必要な操作回数の最小値を求めてください。ただし、操作の繰り返しにより 2 人のプレイヤーを同じマスに集めることができない場合には -1 を出力してください。

  • 上下左右のいずれかの方向を決める。そして各プレイヤーはともにその方向に隣接するマスへの移動を試みる。各プレイヤーは移動先のマスが存在し、かつ空きマスであるならば移動し、そうでないならば移動しない。

制約

  • N は 2 以上 60 以下の整数
  • S_i は長さ N の P, ., # からなる文字列
  • S_i の j 文字目が P であるような組 (i, j) の個数はちょうど 2 つ

入力

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

N
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

5
....#
#..#.
.P...
..P..
....#

出力例 1

3

はじめに (3, 2) にいるプレイヤーをプレイヤー 1、(4, 3) にいるプレイヤーをプレイヤー 2 とします。

例えば以下のようにすることで、3 回の操作で 2 人のプレイヤーが同じマスに集まります。

  • 左を選択する。プレイヤー 1 は (3, 1) に移動し、プレイヤー 2 は (4, 2) に移動する。

  • 上を選択する。プレイヤー 1 は移動せず、プレイヤー 2 は (3, 2) に移動する。

  • 左を選択する。プレイヤー 1 は移動せず、プレイヤー 2 は (3, 1) に移動する。


入力例 2

2
P#
#P

出力例 2

-1

入力例 3

10
..........
..........
..........
..........
....P.....
.....P....
..........
..........
..........
..........

出力例 3

10

Score: 400 points

Problem Statement

There is an N \times N grid, where each cell is either empty or contains an obstacle. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.

There are also two players on distinct empty cells of the grid. The information about each cell is given as N strings S_1, S_2, \ldots, S_N of length N, in the following format:

  • If the j-th character of S_i is P, then (i, j) is an empty cell with a player on it.

  • If the j-th character of S_i is ., then (i, j) is an empty cell without a player.

  • If the j-th character of S_i is #, then (i, j) contains an obstacle.

Find the minimum number of moves required to bring the two players to the same cell by repeating the following operation. If it is impossible to bring the two players to the same cell by repeating the operation, print -1.

  • Choose one of the four directions: up, down, left, or right. Then, each player attempts to move to the adjacent cell in that direction. Each player moves if the destination cell exists and is empty, and does not move otherwise.

Constraints

  • N is an integer between 2 and 60, inclusive.
  • S_i is a string of length N consisting of P, ., and #.
  • There are exactly two pairs (i, j) where the j-th character of S_i is P.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

5
....#
#..#.
.P...
..P..
....#

Sample Output 1

3

Let us call the player starting at (3, 2) Player 1 and the player starting at (4, 3) Player 2.

For example, doing the following brings the two players to the same cell in three moves:

  • Choose left. Player 1 moves to (3, 1), and Player 2 moves to (4, 2).

  • Choose up. Player 1 does not move, and Player 2 moves to (3, 2).

  • Choose left. Player 1 does not move, and Player 2 moves to (3, 1).


Sample Input 2

2
P#
#P

Sample Output 2

-1

Sample Input 3

10
..........
..........
..........
..........
....P.....
.....P....
..........
..........
..........
..........

Sample Output 3

10
H - Tree Distance

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

配点 : 475 点

問題文

N 頂点の重み付き無向木であって、以下の条件を満たすものが存在するか判定して下さい。

  • 1 \le i \lt j \le N を満たす任意の 2 整数 i,j について頂点 i と頂点 j の距離が A_{i,j} である。

ただし、頂点 i と頂点 j の距離とは、この 2 頂点を結ぶ唯一の単純パスに含まれる辺の重みの総和のことです。

制約

  • 2 \le N \le 3000
  • 1 \le A_{i,j} \le 9999
  • 入力される値は全て整数

入力

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

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

出力

条件を満たす木が存在するなら Yes を、存在しないなら No を出力せよ。


入力例 1

4
2 5 4
3 2
5

出力例 1

Yes

例えば以下の辺をもつ木が条件を満たします。

  • 辺 (1, 2) の重みが 2
  • 辺 (2, 3) の重みが 3
  • 辺 (2, 4) の重みが 2

よって Yes と出力してください。


入力例 2

4
2 5 4
3 2
6

出力例 2

No

条件を満たす木は存在しません。よって No と出力してください。


入力例 3

10
1039 1802 3781 231 5828 1944 392 262 1481
763 2742 1270 4789 905 1431 1301 442
1979 2033 5552 142 2194 2064 1205
4012 7531 2121 4173 4043 3184
6059 2175 161 493 1712
5694 6220 6090 5231
2336 2206 1347
654 1873
1743

出力例 3

Yes

Score : 475 points

Problem Statement

Determine whether there exists a weighted undirected tree with N vertices satisfying the following condition.

  • For any two integers i and j with 1 \le i \lt j \le N, the distance between vertices i and j is A_{i,j}.

Here, the distance between vertices i and j is defined as the sum of the weights of the edges on the unique simple path connecting these two vertices.

Constraints

  • 2 \le N \le 3000
  • 1 \le A_{i,j} \le 9999
  • All input values are integers.

Input

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

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

Output

If a tree satisfying the condition exists, output Yes; otherwise, output No.


Sample Input 1

4
2 5 4
3 2
5

Sample Output 1

Yes

For example, the tree with the following edges satisfies the condition.

  • Edge (1, 2) has weight 2.
  • Edge (2, 3) has weight 3.
  • Edge (2, 4) has weight 2.

Thus, output Yes.


Sample Input 2

4
2 5 4
3 2
6

Sample Output 2

No

No tree satisfying the condition exists. Thus, output No.


Sample Input 3

10
1039 1802 3781 231 5828 1944 392 262 1481
763 2742 1270 4789 905 1431 1301 442
1979 2033 5552 142 2194 2064 1205
4012 7531 2121 4173 4043 3184
6059 2175 161 493 1712
5694 6220 6090 5231
2336 2206 1347
654 1873
1743

Sample Output 3

Yes
I - InterSections

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

配点 : 550 点

問題文

1 から N までの番号のついた N 個の区間が与えられます。 区間 i は [L_i,R_i] です。

区間 [l_a,r_a] と区間 [l_b,r_b] は (l_a < l_b < r_a < r_b) または (l_b < l_a < r_b < r_a) を満たすとき、交差するといいます。

f(l,r) を 1 \leq i \leq N を満たし、区間 [l,r] と区間 i が交差する i の個数と定義します。

0 \leq l < r \leq 10^{9} を満たす整数の組 (l,r) において、 f(l,r) の最大値を達成する (l,r) の組のうち l が最小のものを答えてください。そのような組が複数存在する場合はさらにそのうちで r が最小のものを答えてください (0 \leq l < r より、 答えるべき (l,r) の組は一意に定まります)。

制約

  • 1 \leq N \leq 10^{5}
  • 0 \leq L_i < R_i \leq 10^{9} (1 \leq i \leq N)
  • 入力は全て整数である

入力

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

出力

答えとなる組 (l,r) を次の形式で出力せよ。

l r

入力例 1

5
1 7
3 9
7 18
10 14
15 20

出力例 1

4 11

f(l,r) の最大値は 4 であり、f(l,r)=4 となる (l,r) のうち l の最小値は 4 です。 f(l,r)=4 かつ l=4 を満たす (l,r) は以下の 5 通りです。

  • (l,r)=(4,11)
  • (l,r)=(4,12)
  • (l,r)=(4,13)
  • (l,r)=(4,16)
  • (l,r)=(4,17)

このうち、r の最小値は 11 であるため、4 と 11 を出力します。


入力例 2

11
856977192 996441446
298251737 935869360
396653206 658841528
710569907 929136831
325371222 425309117
379628374 697340458
835681913 939343451
140179224 887672320
375607390 611397526
93530028 581033295
249611310 775998537

出力例 2

396653207 887672321

Score : 550 points

Problem Statement

You are given N intervals numbered 1 to N. Interval i is [L_i, R_i].

Two intervals [l_a, r_a] and [l_b, r_b] are said to intersect if and only if they satisfy either (l_a < l_b < r_a < r_b) or (l_b < l_a < r_b < r_a).

Define f(l, r) as the number of intervals i (1 \leq i \leq N) that intersect with the interval [l, r].

Among all pairs of integers (l, r) satisfying 0 \leq l < r \leq 10^{9}, find the pair (l, r) that maximizes f(l, r). If there are multiple such pairs, choose the one with the smallest l. If there are still multiple pairs, choose the one with the smallest r among them. (Since 0 \leq l < r, the pair (l, r) to be answered is uniquely determined.)

Constraints

  • 1 \leq N \leq 10^{5}
  • 0 \leq L_i < R_i \leq 10^{9} (1 \leq i \leq N)
  • All input values are integers.

Input

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

Output

Print the sought pair (l, r) in the following format:

l r

Sample Input 1

5
1 7
3 9
7 18
10 14
15 20

Sample Output 1

4 11

The maximum value of f(l, r) is 4, and among the pairs (l, r) that achieve f(l, r) = 4, the smallest l is 4. The pairs (l, r) that satisfy f(l, r) = 4 and l = 4 are the following five:

  • (l, r) = (4, 11)
  • (l, r) = (4, 12)
  • (l, r) = (4, 13)
  • (l, r) = (4, 16)
  • (l, r) = (4, 17)

Among these, the smallest r is 11, so print 4 and 11.


Sample Input 2

11
856977192 996441446
298251737 935869360
396653206 658841528
710569907 929136831
325371222 425309117
379628374 697340458
835681913 939343451
140179224 887672320
375607390 611397526
93530028 581033295
249611310 775998537

Sample Output 2

396653207 887672321