Time limit
2s
Memory limit
128 MB
Unit cube blocks are stacked on a board with N positions in the front direction and M positions in the side direction. Let H(i, j) be the height at cell (i, j), a nonnegative integer.
From the front, the i-th visible height from left to right is A_i = max_j H(i, j). From the side, the j-th visible height is B_j = max_i H(i, j).
Given the front heights A and side heights B, decide whether some block arrangement matches both views. If it exists, find the minimum and maximum possible total number of blocks.
The first line contains two integers N and M (1 ≤ N, M ≤ 100,000).
The next N lines contain A_1, ..., A_N, the front-view heights from left to right, one per line. The following M lines contain B_1, ..., B_M, the side-view heights, one per line.
Every height is an integer from 0 to 2^31 - 1, inclusive.
If no arrangement can match both views, output -1.
Otherwise, output the minimum and maximum possible number of blocks on one line, separated by a space. The answer does not exceed 2^31 - 1.