eolymp
bolt
Try our new interface for solving problems
Problems

Piramid base

Piramid base

\includegraphics{https://static.e-olymp.com/content/e3/e390003da5a9fb3cf824dbf8a8e4480434029106.jpg} You have been asked to find the largest affordable location for constructing a new pyramid. In order to help you decide, you have been provided with a survey of the available land which has been conveniently divided into an \textbf{M} by \textbf{N} grid of square cells. The base of the pyramid must be a square with sides parallel to those of the grid. The survey has identified a set of \textbf{P} possibly overlapping obstacles, which are described as rectangles in the grid with sides parallel to those of the grid. In order to build the pyramid, all the cells covered by its base must be cleared of any obstacles. Removing the ith obstacle has a cost \textbf{C_i}. Whenever an obstacle is removed, it must be removed completely, that is, you cannot remove only part of an obstacle. Also, please note that removing an obstacle does not affect any other obstacles that overlap it. Write a program that, given the dimensions \textbf{M} and \textbf{N} of the survey, the description of the \textbf{P} obstacles, the cost of removing each of the obstacles, and the budget \textbf{B} you have, finds the maximum possible side length of the base of the pyramid such that the total cost of removing obstacles does not exceed \textbf{B}. Your program will be graded on three disjoint sets of tests. For all of them, the following constraints apply: \textbf{1 <= M, N <= 1,000,000} The dimensions of the grid. \textbf{1 <= C_i <= 7,000} The cost of removing the ith obstacle. \textbf{1 <= Xi1 <= Xi2 <= M} \textbf{X} coordinates of the leftmost and the rightmost cells of the ith obstacle. \textbf{1 <= Yi1 <= Yi2 <= N Y} coordinates of the bottommost and the topmost cells of the ith obstacle. In the first set of tests worth \textbf{35} points: \textbf{B = 0} The budget you have. (You cannot remove any obstacles.) \textbf{1 <= P <= 1,000} The number of obstacles in the grid. In the second set of tests worth \textbf{35} points: \textbf{0 < B <= 2,000,000,000} The budget you have. \textbf{1 <= P <= 30,000} The number of obstacles in the grid. In the third set of tests worth \textbf{30} points: \textbf{B = 0} The budget you have. (You cannot remove any obstacles.) \textbf{1 <= P <= 400,000} The number of obstacles in the grid. \InputFile Your program must read from the standard input the following data: • Line \textbf{1} contains two integers separated by a single space that represent \textbf{M} and \textbf{N} respectively. • Line \textbf{2} contains the integer \textbf{B}, the maximum cost you can afford (i.e., your budget). • Line \textbf{3} contains the integer \textbf{P}, the number of obstacles found in the survey. • Each of the next \textbf{P} lines describes an obstacle. The ith of these lines describes the ith obstacle. Each line consists of \textbf{5} integers: \textbf{X_i1}, \textbf{Y_i1}, \textbf{X_i2}, \textbf{Y_i2}, and \textbf{C_i} separated by single spaces. They represent respectively the coordinates of the bottommost leftmost cell of the obstacle, the coordinates of the topmost rightmost cell of the obstacle, and the cost of removing the obstacle. The bottommost leftmost cell on the grid has coordinates (\textbf{1, 1}) and the topmost rightmost cell has coordinates (\textbf{M, N}). \OutputFile Your program must write to the standard output a single line containing one integer, the maximum possible side length of the base of the pyramid that can be prepared. If it is not possible to build any pyramid, your program should output the number \textbf{0}. During the contest, your submissions for this task will be evaluated on some of the official test data showing you a summary of the results.
Time limit 15 seconds
Memory limit 64 MiB
Input example #1
6 9
42
5
4 1 6 3 12
3 6 5 6 9
1 3 3 8 24
3 8 6 9 21
5 1 6 2 20
Output example #1
4