Gahui and the House
Time limit3.5sMemory limit1024 MB
Find the minimum total boundary cost of a rectangular hole with its bottom on y=1, where the width and height fall within given ranges.
- Level
Medium7 of 10
- Topics
- Prefix sum, Sliding window, Array
- Solved
- No attempts yet
Problem

[Figure 1] A house for Gahui, who was chased outside
Gahui peed in the kitchen so often that she was sent outside, and she whined with a miserable face. Seeing this, Dad decided to build her a house because he worried she would be too cold. A board has height and width . Its four corners are , , , and .
Dad wants to cut a rectangular hole in the board that meets the following conditions, so Gahui can come and go from the house.
- One of the four sides of the rectangle lies on .
- Each side of the rectangle is parallel to the x-axis or the y-axis.
- The x-coordinates of the rectangle's vertices are integers from to , inclusive.
- The y-coordinates of the rectangle's vertices are integers from to , inclusive.
- The width of the rectangle is at least and at most .
- The height of the rectangle is at least and at most .
- The bottom side is parallel to the x-axis.
The cost of cutting a hole is the sum of the costs of the lattice points on the sides of the rectangle being cut. Dad wants to know the minimum cost of cutting the hole. Please help him. Treat the board as a two-dimensional figure on the coordinate plane.
Input
The first line contains and , separated by a space.
The second line contains , , , and , separated by spaces.
Each of the next lines contains integers, one per line. The -th integer on the -th line is the cost of the lattice point .
Output
Print the answer to the problem. If no hole can be cut that meets the conditions, print No.
Limits
- Every cost in the problem is an integer from to , inclusive.
Hint
Python 3 users are advised to submit with PyPy3.