#P11662. Dynamic Grid Coloring

    ID: 13751 Type: Default 1000ms 256MiB

Dynamic Grid Coloring

Dynamic Grid Coloring

You are given an N × N grid. The cell in the i-th row and j-th column is denoted by \((i,j)\).

The cells in the first row and the first column are already colored. Specifically, for every \(1 \le i \le N\):

  • The cell \((i,1)\) is colored with color \(A_i\).
  • The cell \((1,i)\) is colored with color \(B_i\).

It is guaranteed that \(A_1 = B_1\). For the remaining cells, i.e. for \(i,j \ge 2\), the coloring is performed in the following way:

For \(i = 2,3,\dots,N\), for each \(j = 2,3,\dots,N\), the color of cell \((i,j)\) is defined as \[ \operatorname{color}(i,j)= \max\big(\operatorname{color}(i-1,j),\; \operatorname{color}(i,j-1)\big). \]

In other words, the color in any cell \((i,j)\) becomes the maximum among all colors in the first \(i\) cells of the first column and the first \(j\) cells of the first row. After the entire grid is colored, determine which color appears in the maximum number of cells, and output that color together with its frequency. If more than one color attains the same (maximum) frequency, output the largest color value among them.

inputFormat

The first line contains a single integer \(N\) representing the dimensions of the grid. The second line contains \(N\) integers \(A_1, A_2, \dots, A_N\), which specify the colors of the first column. The third line contains \(N\) integers \(B_1, B_2, \dots, B_N\), which specify the colors of the first row. It is guaranteed that \(A_1 = B_1\).

outputFormat

Output two integers separated by a space: the color that appears most frequently in the grid and the number of cells colored with that color. If there are multiple colors with the same frequency, output the largest color value.

sample

2
1 3
1 2
3 2