AT_joi2025ho_a 色塗り (Grid Coloring)
Description
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 Format
Read the following data from the standard input.
> $ N $ $ A_1 $ $ A_2 $ $ \cdots $ $ A_N $ $ B_1 $ $ B_2 $ $ \cdots $ $ B_N $
Output Format
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.
---
Explanation/Hint
### 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 Explanation 1
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 Explanation 2
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 Explanation 3
This sample input satisfies the constraints of Subtasks $ 1 $ , $ 2 $ , $ 3 $ , and $ 5 $ .
### 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.