Under construction

Still being built. Things may change

Gahui and the House

Time limit3.5sMemory limit1024 MB

Summary
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 H+1H+1 and width W+1W+1. Its four corners are (0,0)(0, 0), (W+1,0)(W+1, 0), (0,H+1)(0, H+1), and (W+1,H+1)(W+1, H+1).

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 y=1y = 1.
  • 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 11 to WW, inclusive.
  • The y-coordinates of the rectangle's vertices are integers from 11 to HH, inclusive.
  • The width of the rectangle is at least w1w_1 and at most w2w_2.
  • The height of the rectangle is at least h1h_1 and at most h2h_2.
  • 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 HH and WW, separated by a space.

The second line contains h1h_1, h2h_2, w1w_1, and w2w_2, separated by spaces.

Each of the next HH lines contains WW integers, one per line. The xx-th integer on the yy-th line is the cost of the lattice point (x,y)(x, y).

Output

Print the answer to the problem. If no hole can be cut that meets the conditions, print No.

Limits

  • 3≤H≤30003 \le H \le 3000
  • 3≤W≤30003 \le W \le 3000
  • 3≤h1≤h2≤H3 \le h_1 \le h_2 \le H
  • 3≤w1≤w2≤W3 \le w_1 \le w_2 \le W
  • Every cost in the problem is an integer from −109-10^9 to 10910^9, inclusive.

Hint

Python 3 users are advised to submit with PyPy3.

Examples3

  1. Example 1

    Input
    4 4
    3 3 3 3
    5 4 2 1
    1000 100 10 1
    1000 100 10 1
    6 8 2 1
    
    Expected output
    2031
    
  2. Example 2

    Input
    5 5
    3 4 3 3
    2 -1 2 3 2
    2 -5 3 1 2
    3 1 5 2 4
    2 2 3 1 5
    3 1 3 1 2
    
    Expected output
    19
    
  3. Example 3

    Input
    5 5
    4 5 5 5
    2 -1 2 3 2
    2 -5 3 1 2
    3 1 5 2 4
    2 2 3 1 5
    3 1 3 1 2
    
    Expected output
    No