Dragon Tree Fields
The members of the Plant Technology Research Institute in Lisabon, Portugal work on a new project.
Large area in the vicinity of the institute has been experimentally planted
with a miniature dwarf variety of the famous
Socotra dragon tree.
The area is in the shape of square and it is divided into elementary squares, each square contains some number
of the trees. Based on preliminary measurements of tree sap in the whole area, each elementary square has been assigned a single whole number expressing the average sap quality in that square.
To be able to compare effectively the outcomes of different experiments the researchers have to choose a pair a sub-areas, called fields, consisting of some elementary squares. Analogous experiments will be conducted in each field and the results will be compared.
The work of the experimenters is under close scrutiny by other experts, therefore the institute has specified some relatively strict rules governing the selection of the fields:
- The shape of each field must be a square, the size of the square might be arbitrary (provided it is non-zero and it fits into the area).
- The two fields must not overlap, i.e. each elementary square in the whole area can belong to at most one field.
- The quality of each field is calculated as the sum of the qualities of all elementary squares in that field.
- The quality of each field has to be within a prescribed range [Q1, Q2].
- The qualities of the two fields should be as close to each other as possible, that is, the absolute value of the difference of the fields' qualities should be minimized.
- If there are more pairs of fields which satisfy the previous conditions then the pair which maximizes the sum of its qualities should be chosen.
- If there are more pairs of fields which satisfy the previous conditions then any of those pairs might be chosen.
Image 1. Scheme of a 6 × 6 area in which two fields are marked. The fields satisfy the institute conditions described in the text above. The fields respective qualities (from top to bottom) are 82 and 80. The scheme illustrates the Example 1 below with the qualities range [Q1, Q2] = [70, 85]. |
Â
The task
You are given quality of each elementary square in the area and the prescribed range qualities [Q1, Q2]. Calculate the qualities of two fields which satisfy the institute conditions.
Input
The first input line contains one integer N, it is the number of rows and columns in the area.
Next, the area is specified by N text lines, each describes one row in the area. The line contains N qualities of the elementary squares in the row, from left to right, separated by spaces.
The last line of input contains two integers Q1, Q2 separated by spaces.
It holds, 2 ≤ N ≤ 120, −1010 ≤ Q1 ≤ Q2 ≤ 1010. All elementary square qualities are integers in range [−109, 109].
Output
The output contains one text line with two integers A, B (A ≤ B) separated by space and representing the qualities of two fields which satisfy the institute conditions described in the text.
Example 1
Input6 12 14 16 22 13 10 10 31 12 16 15 11 -20 19 20 5 9 12 10 15 16 16 -10 23 40 89 67 30 -5 10 20 34 -95 43 18 30 70 85Output
80 82The data of Example 1 are depicted in Image 1a).
Example 2
Input5 19 14 14 19 17 18 13 12 12 19 13 13 12 14 16 20 12 10 20 11 17 15 14 17 19 40 55Output
50 51Illustration
.. .. .. .. .. .. .. 12 12 .. .. .. 12 14 .. .. 12 10 .. .. .. 15 14 .. ..The scheme highlights a possible pair of fields in the area in Example 2. The fields satisfy the institute conditions described in the text.
Example 3
Input10 -7 8 0 -9 1 8 4 7 -6 0 4 6 -1 0 -2 -3 3 -3 8 -9 -6 -1 -5 7 9 2 9 8 4 -4 -2 3 -4 7 -6 9 3 1 3 3 0 5 1 -1 -3 -6 0 7 -9 8 5 -6 -2 5 6 -5 -6 3 -4 -1 9 6 6 -2 -7 1 5 0 -2 -2 -4 8 7 8 8 -9 3 -2 -4 -4 -5 -6 3 3 2 6 -4 6 5 -3 -4 -2 -8 4 -5 1 -9 8 -7 2 -20 -10Output
-10 -10Illustration
.. .. .. -9 1 .. .. .. .. .. .. .. .. 0 -2 .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 6 -5 -6 3 -4 .. .. .. .. .. -7 1 5 0 -2 .. .. .. .. .. 8 -9 3 -2 -4 .. .. .. .. .. 2 6 -4 6 5 .. .. .. .. .. -5 1 -9 8 -7 ..The scheme highlights a possible pair of fields in the area in Example 3. The fields satisfy the institute conditions described in the text.
Public data
The public data set is intended for easier debugging and approximate program correctness checking. The public data set is stored also in the upload system and each time a student submits a solution it is run on the public dataset and the program output to stdout and stderr is available to him/her.
Link to public data set