Under construction

Still being built. Things may change

Wiring Engineering

Time limit8sMemory limit1024 MB

Summary
Given a weight matrix and building and tower install costs, answer many range queries for the maximum profit of non-crossing wires within chosen index ranges.
Level

Hard9 of 10

Topics
Dynamic programming, Divide and conquer, Matrix
Solved
No attempts yet

Problem

On the north side of Bytestreet, there are nn buildings standing in a row. They are labeled 1,2,…,n1,2,\dots,n from east to west, and the coordinate of the ii-th building is (i,1)(i,1).

On the south side of Bytestreet, there are nn communication towers standing in a row. They are labeled 1,2,…,n1,2,\dots,n from east to west, and the coordinate of the ii-th tower is (i,−1)(i,-1).

You are an electrical engineer in Byteland. Your job is to design a wiring scheme. A wire connects one building and one tower as a straight segment from (i,1)(i,1) to (j,−1)(j,-1). For each pair of building and tower, you can connect at most one wire. Connecting the ii-th building with the jj-th tower earns wi,jw_{i,j} dollars from the building owner.

Each building can have multiple wires, but you must pay uiu_i dollars to connect at least one wire to the ii-th building, because you must first install equipment there. Likewise, each tower can have multiple wires, but you must pay viv_i dollars to connect at least one wire to the ii-th tower. Two wires can only intersect at their endpoints, which prevents short circuits.

Some places cannot have equipment installed, so no wire can connect to them. You are given qq queries. In the ii-th query, you get four integers aia_i, bib_i, cic_i, and did_i. You can install equipment only in buildings with labels in [ai,bi][a_i, b_i] and in towers with labels in [ci,di][c_i, d_i]. For each query, find the wiring scheme that maximizes your earnings.

The answer cannot be negative, because you can choose to do nothing.

Input

The input contains only a single test case.

The first line contains two integers nn and qq (1≤n≤5001 \leq n \leq 500, 1≤q≤300 0001 \leq q \leq 300\,000), the number of buildings (or towers) and the number of queries.

The second line contains nn integers u1,u2,…,unu_1, u_2, \dots, u_n (1≤ui≤10 0001 \leq u_i \leq 10\,000), the cost to install equipment in each building.

The third line contains nn integers v1,v2,…,vnv_1, v_2, \dots, v_n (1≤vi≤10 0001 \leq v_i \leq 10\,000), the cost to install equipment in each tower.

Each of the next nn lines contains nn integers. The jj-th integer on the ii-th line is wi,jw_{i,j} (1≤wi,j≤10 0001 \leq w_{i,j} \leq 10\,000), the money you get for connecting the ii-th building with the jj-th tower.

Each of the next qq lines contains four integers aia_i, bib_i, cic_i, and did_i (1≤ai≤bi≤n1 \leq a_i \leq b_i \leq n, 1≤ci≤di≤n1 \leq c_i \leq d_i \leq n), describing the ii-th query.

Output

For each query, print a single line containing the maximum amount of dollars you can earn.

Examples1

  1. Example 1

    Input
    3 4
    1 2 1
    2 1 2
    1 2 3
    4 5 6
    3 2 1
    1 3 1 3
    2 3 1 2
    1 1 2 3
    1 2 2 3
    
    Expected output
    8
    5
    1
    7