동서 방향 H개 도로와 남북 방향 W개 도로의 혼잡도가 모두 다를 때, 교차로에서 가로지르는 도로의 혼잡도가 더 크면 회전하고 아니면 직진하는 규칙으로 차가 움직인다. Q개의 출발 교차로마다 차가 멈추기 전까지 이동할 수 있는 최대 거리를 구한다.
어려움8수학구현시뮬레이션그리디아직 제출이 없습니다시간 제한4초메모리 제한512 MBOn a sunny day, there was an abduction incident at a crossing in a city. It is suspected that the criminals were Anna and Bruno, and they escaped from the abduction scene by a car. The car was not yet found. The police is searching it even now.
The urban area where the criminals was driving the car is a city of rectangular grid with H streets running in the east-west direction and W streets running in the south-north direction. The distance between two adjacent crossings is 1 kilometer.
Each street has an integer, the congestion degree. The congestion degree of the i-th street (1 ≤ i ≤ H) from north running in the east-west direction is Ai , and the congestion degree of the j-th street (1 ≤ j ≤ W) from west running in the south-north direction is Bj. These H + W values are different from each other. For each street, the congestion degree has the same value at any points on it.
The investigation by the police revealed that the criminals moved in the city in the following way:
There are Q candidates of crossings for the abduction scene. These Q candidates are different from each other. To determine the number of investigators, the police wants to know, for each candidate of crossings, the maximum possible travel distance for the criminals assuming the abduction incident was occurred at that place.
For each of Q queries, calculate the maximum possible travel distance from the given candidate of crossings.
Given the congestion degrees of the streets in the city and Q candidates of crossings for the abduction scene, write a program which calculates the maximum possible travel distance from each candidate of crossings.
Read the following data from the standard input.
Write Q lines to the standard output. The k-th line of output should contain an integer, the maximum possible travel distance (in kilometers) from the k-th candidate of crossings for the abduction scene.