PivotOJ

Grid Coloring

시간 제한: 2000ms메모리 제한: 2048MB출처: JOI 2024-2025 본선BOJ 33724

문제

President K is designing a pattern represented by a grid with NN rows and NN 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 ii-th row (1 ≤ i ≤ N) and jj-th column (1 ≤ j ≤ N) as cell (i,j)(i, j).

Currently, the cells in the first column and first row are already painted. Specifically, cell (i,1)(i, 1) (1 ≤ i ≤ N) is painted with color AiA_i and cell (1,j)(1, j) (1 ≤ j ≤ N) is painted with color BjB_j. Note that A1=B1A_1 = B_1.

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

  • For each i=2,3,,Ni = 2, 3, \dots , N in order, paint the cells in the ii-th row as follows:
    • For each j=2,3,,Nj = 2, 3, \dots , N in order, paint cell (i,j)(i, j) with the color that has the larger number between:
      • The color of cell (i1,j)(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 N2N^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.

입력

Read the following data from the standard input.

NN

A1A_1 A2A_2 \cdots ANA_N

B1B_1 B2B_2 \cdots BNB_N

출력

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.

예제

예제 1

입력
3
5 2 5
5 3 1
출력
5 4

예제 2

입력
3
1 7 8
1 3 5
출력
8 3

예제 3

입력
4
2 1 2 1
2 1 1 2
출력
2 10
코드를 제출하려면 로그인하세요.