Grid Coloring

시간 제한2초메모리 제한2048 MB

요약
첫 행과 첫 열이 주어질 때 각 칸은 위와 왼쪽 이웃 중 큰 값으로 채워진다. 가장 많은 칸을 차지하는 색을 찾고, 동률이면 가장 큰 색을 고른다.
난이도

보통10점 중 7점

유형
정렬, 그리디, 누적 합, 구현
정답자
아직 제출이 없습니다

문제

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≤N1 ≤ i ≤ N) and jj-th column (1≤j≤N1 ≤ 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≤N1 ≤ i ≤ N) is painted with color A_iA\_i and cell (1,j)(1, j) (1≤j≤N1 ≤ j ≤ N) is painted with color B_jB\_j. Note that A_1=B_1A\_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 (i−1,j)(i - 1, j), and
      • The color of cell (i,j−1)(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

A_1A\_1 A_2A\_2 ⋯\cdots A_NA\_N

B_1B\_1 B_2B\_2 ⋯\cdots B_NB\_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.

제한

  • 2≤N≤200,0002 ≤ N ≤ 200\\, 000.
  • 1≤A_i≤1091 ≤ A\_i ≤ 10^9 (1≤i≤N1 ≤ i ≤ N).
  • 1≤B_j≤1091 ≤ B\_j ≤ 10^9 (1≤j≤N1 ≤ j ≤ N).
  • A_1=B_1A\_1 = B\_1.
  • Given values are all integers.

예제3

  1. 예제 1

    입력
    3
    5 2 5
    5 3 1
    
    예상 출력
    5 4
    
  2. 예제 2

    입력
    3
    1 7 8
    1 3 5
    
    예상 출력
    8 3
    
  3. 예제 3

    입력
    4
    2 1 2 1
    2 1 1 2
    
    예상 출력
    2 10