A - 色塗り (Grid Coloring) Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

Score: 100 points

Problem Statement

President K is designing a pattern represented by a grid with N rows and N columns. To achieve this, he has decided to paint each cell with a color represented by an integer number. Let us refer to the cell in the i-th row (1 \leq i \leq N) and j-th column (1 \leq j \leq N) as cell (i,j).

Currently, the cells in the first column and first row are already painted. Specifically, cell (i,1) (1 \leq i \leq N) is painted with color A_i and cell (1,j) (1 \leq j \leq N) is painted with color B_j. Note that A_1 = B_1.

For the remaining unpainted cells, President K is going to paint them by the following procedure:

  • For each i=2,3,\dots,N in order, paint the cells in the i-th row as follows:
    • For each j=2,3,\dots,N in order, paint cell (i,j) with the color that has the larger number between:
      • The color of cell (i-1,j), and
      • The color of cell (i,j-1).
      If both colors have the same number, paint the cell with that color.

President K would like to determine the color that is painted on the largest number of cells after all N^2 cells have been painted, as well as the number of cells painted with that color.

Write a program that, given the size of the grid and the color information for the first column and first row, determines the color number painted on the largest number of cells and the number of cells painted with that color. If multiple colors are painted on the largest number of cells, output the largest color number among them.


Input

Read the following data from the standard input.

N
A_1 A_2 \cdots A_N
B_1 B_2 \cdots B_N

Output

Write one line to the standard output containing two integers separated by a space:

  1. The color number that is painted on the largest number of cells, and
  2. The number of cells painted with that color.

If multiple colors are painted on the largest number of cells, output the largest color number among them.


Constraints

  • 2 \leq N \leq 200\,000.
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N).
  • 1 \leq B_j \leq 10^9 (1 \leq j \leq N).
  • A_1 = B_1.
  • Given values are all integers.

Subtasks

  1. (15 points) N \leq 500, A_i \leq 100\,000 (1 \leq i \leq N), B_j \leq 100\,000 (1 \leq j \leq N).
  2. (10 points) N \leq 500.
  3. (20 points) A_i \leq 2 (1 \leq i \leq N), B_j \leq 2 (1 \leq j \leq N).
  4. (25 points) A_i < A_{i+1} (1 \leq i \leq N-1), B_j < B_{j+1} (1 \leq j \leq N-1).
  5. (30 points) No additional constraints.

Sample Input 1

3
5 2 5
5 3 1

Sample Output 1

5 4

In this sample, the color of each cell in the grid will be as follows:

5 3 1
2 3 3
5 5 5

The color number painted on the largest number of cells is 5, which appears on 4 cells. Thus, print 5 and 4 in this order, separated by a space.

This sample input satisfies the constraints of Subtasks 1, 2, and 5.


Sample Input 2

3
1 7 8
1 3 5

Sample Output 2

8 3

In this sample, the color of each cell in the grid will be as follows:

1 3 5
7 7 7
8 8 8

The color numbers painted on the largest number of cells are 7 and 8, each painted on 3 cells. In this case, output the larger color number, 8, followed by the number of cells, 3, separated by a space.

This sample input satisfies the constraints of Subtasks 1, 2, 4, and 5.


Sample Input 3

4
2 1 2 1
2 1 1 2

Sample Output 3

2 10

This sample input satisfies the constraints of Subtasks 1, 2, 3, and 5.

配点: 100

問題文

K 理事長は縦 N 行,横 N 列のマス目で表される模様を作ろうとしている.そのために,各マスに整数の番号で表される色を塗ることにした.以降,上から i 行目 (1 \leqq i \leqq N),左から j 列目 (1 \leqq j \leqq N) のマスをマス (i,j) と呼ぶことにする.

現時点で,1 列目と 1 行目のマスには既に色が塗られている.具体的には,マス (i,1) (1 \leqq i \leqq N) は色 A_i で,マス (1,j) (1 \leqq j \leqq N) は色 B_j で塗られている.ここで A_1 = B_1 である.

残りのマスについて,K 理事長は以下の手順で色を塗っていく.

  • i=2,3,\dots,N の順に,以下の手順で i 行目のマスに色を塗る.
    • j=2,3,\dots,N の順に,マス (i,j)
      • マス (i-1,j) に塗られている色
      • マス (i,j-1) に塗られている色
      のうち番号が大きい方の色で塗る.番号が同じ場合は,その色で塗る.

K 理事長は,最終的に N^2 個のマスすべてに色が塗られたとき,最も多くのマスに塗られた色の番号,およびその色が塗られているマスの個数を求めたい.

マス目の大きさおよび 1 列目と 1 行目のマスの情報が与えられたとき,最も多くのマスに塗られた色の番号とその色が塗られているマスの個数を求めるプログラムを作成せよ.最も多くのマスに塗られた色の番号が複数存在する場合,そのうち最も番号の大きいものを求め出力すること.


入力

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

N
A_1 A_2 \cdots A_N
B_1 B_2 \cdots B_N

出力

標準出力に,最も多くのマスに塗られた色の番号と,その色が塗られているマスの個数を空白区切りで 1 行に出力せよ.最も多くのマスに塗られた色の番号が複数存在する場合,そのうち最も番号の大きいものを出力すること.


制約

  • 2 \leqq N \leqq 200\,000
  • 1 \leqq A_i \leqq 10^9 (1 \leqq i \leqq N).
  • 1 \leqq B_j \leqq 10^9 (1 \leqq j \leqq N).
  • A_1 = B_1
  • 入力される値はすべて整数である.

小課題

  1. (15 点) N \leqq 500A_i \leqq 100\,000 (1 \leqq i \leqq N),B_j \leqq 100\,000 (1 \leqq j \leqq N).
  2. (10 点) N \leqq 500
  3. (20 点) A_i \leqq 2 (1 \leqq i \leqq N),B_j \leqq 2 (1 \leqq j \leqq N).
  4. (25 点) A_i < A_{i+1} (1 \leqq i \leqq N-1),B_j < B_{j+1} (1 \leqq j \leqq N-1).
  5. (30 点) 追加の制約はない.

入力例 1

3
5 2 5
5 3 1

出力例 1

5 4

最終的に各マスに塗られる色の番号は以下のようになる.

5 3 1
2 3 3
5 5 5

最も多くのマスに塗られた色の番号は 5 であり,これは 4 個のマスに塗られているため,54 を空白区切りで出力する.

この入力例は小課題 1,2,5 の制約を満たす.


入力例 2

3
1 7 8
1 3 5

出力例 2

8 3

最終的に各マスに塗られる色の番号は以下のようになる.

1 3 5
7 7 7
8 8 8

最も多くのマスに塗られた色の番号は 78 であり,それぞれ 3 個のマスに塗られている.2 つの色のうち最も番号が大きい色は 8 であるため,83 を空白区切りで出力する.

この入力例は小課題 1,2,4,5 の制約を満たす.


入力例 3

4
2 1 2 1
2 1 1 2

出力例 3

2 10

この入力例は小課題 1,2,3,5 の制約を満たす.