Problem:
Score : 1000 points

Problem Statement

There are N barbecue restaurants along a street.
The restaurants are numbered 1 through N from west to east, and the distance between restaurant i and restaurant i+1 is A_i.

Joisino has M tickets, numbered 1 through M.
Every barbecue restaurant offers barbecue meals in exchange for these tickets.
Restaurant i offers a meal of deliciousness B_{i,j} in exchange for ticket j.
Each ticket can only be used once, but any number of tickets can be used at a restaurant.

Joisino wants to have M barbecue meals by starting from a restaurant of her choice, then repeatedly traveling to another barbecue restaurant and using unused tickets at the restaurant at her current location.
Her eventual happiness is calculated by the following formula: "(The total deliciousness of the meals eaten) - (The total distance traveled)".
Find her maximum possible eventual happiness.

Constraints

All input values are integers.

2≤N≤5×10^3

1≤M≤200

1≤A_i≤10^9

1≤B_{i,j}≤10^9

Input

The input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_{N-1}
B_{1,1} B_{1,2} ... B_{1,M}
B_{2,1} B_{2,2} ... B_{2,M}
:
B_{N,1} B_{N,2} ... B_{N,M}

Output

Print Joisino's maximum possible eventual happiness.

Sample Input 1

3 4
1 4
2 2 5 1
1 3 3 2
2 2 5 1

Sample Output 1

11

The eventual happiness can be maximized by the following strategy: start from restaurant 1 and use tickets 1 and 3, then move to restaurant 2 and use tickets 2 and 4.

Sample Input 2

5 3
1 2 3 4
10 1 1
1 1 1
1 10 1
1 1 1
1 1 10

Sample Output 2

20

Solution in Lua (LuaJIT 2.1.0) (judge verdict: Accepted)

local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = bls(1, i - 1)
    for j = 1, cnt do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.stage = {{}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.stage[stagenum] = {}
  end
  self.stagenum = stagenum
  self.left_stage = {}
  for i = 1, n do
    local sp, sz = 1, bls(1, stagenum - 1)
    while(i - 1) % sz ~= 0 do
      sp, sz = sp + 1, brs(sz, 1)
    end
    self.left_stage[i] = sp
  end
  self.sz_stage = {}
  local tmp, sp = 1, stagenum
  for i = 1, n do
    if tmp * 2 == i then tmp, sp = tmp * 2, sp - 1 end
    self.sz_stage[i] = sp
  end
  for i = 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.right_bound = function(self, val, left, right)
  local retpos = left - 1
  local l, r = left, right
  local stage = mma(self.left_stage[left], self.sz_stage[right - left + 1])
  local stagenum = self.stagenum
  while true do
    local sz = bls(1, stagenum - stage)
    if self.stage[stage][mce(l / sz)] < val then
      retpos = l + sz - 1
      if retpos == right then break end
      if l + sz <= r then
        l = l + sz
        stage = mma(self.left_stage[l], self.sz_stage[r - l + 1])
      else break end
    else
      if sz ~= 1 then stage, r = stage + 1, l + sz - 2
      else break end
    end
  end
  return retpos + 1
end
SegTree.left_bound = function(self, val, left, right)
  local retpos = right + 1
  local stage, l, r = 1, left, right
  local stagenum = self.stagenum
  while true do
    local sz = bls(1, stagenum - stage)
    while r % sz ~= 0 or r + 1 - l < sz do
      stage = stage + 1
      sz = bls(1, stagenum - stage)
    end
    if self.stage[stage][mfl(r / sz)] < val then
      retpos = r - sz + 1
      if l + sz <= r then stage, l, r = 1, l, r - sz
      else break end
    else
      if sz ~= 1 then stage, l, r = stage + 1, r - sz + 2, r
      else break end
    end
  end
  return retpos - 1
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, m = io.read("*n", "*n", "*l")
local pos = {0}
do
  local s = io.read()
  local i = 1
  for d in s:gmatch("%d+") do
    pos[i + 1] = pos[i] + tonumber(d)
    i = i + 1
  end
end
local imos = {}
for i = 1, n do
  imos[i] = {}
  for j = i, n do
    imos[i][j - i + 1] = 0
  end
end
local t = {}

for i = 1, m do
  t[i] = {}
end
for i = 1, n do
  local s = io.read()
  local j = 1
  for d in s:gmatch("%d+") do
    t[j][i] = tonumber(d)
    j = j + 1
  end
end
local idx = {}
for i = 1, n do idx[i] = i end

local st = SegTree.new(n, mma, 0)
local stend = st.stage[st.stagenum]
for i = 1, m do
  table.sort(idx, function(a, b) return t[i][a] < t[i][b] end)
  for j = 1, n do
    stend[idx[j]] = j
  end
  st:updateAll()
  for j = 1, n - 1 do
    local tj = t[i][j]
    local rk = stend[j]
    local right = st:right_bound(rk + 1, j + 1, n) - 1
    local left = j == 1 and 1 or st:left_bound(rk + 1, 1, j - 1) + 1
    imos[left][j - left + 1] = imos[left][j - left + 1] + tj
    imos[j + 1][1] = imos[j + 1][1] - tj
    if right < n then
      imos[left][right - left + 2] = imos[left][right - left + 2] - tj
      imos[j + 1][right + 1 - j] = imos[j + 1][right + 1 - j] + tj
    end
  end
  do
    local tj = t[i][n]
    local rk = stend[n]
    local left = st:left_bound(rk + 1, 1, n - 1) + 1
    imos[left][n - left + 1] = imos[left][n - left + 1] + tj
  end
end
for i = 1, n do
  for j = 2, n - i + 1 do
    imos[i][j] = imos[i][j] + imos[i][j - 1]
  end
end
for i = 2, n do
  for j = 1, n - i + 1 do
    imos[i][j] = imos[i][j] + imos[i - 1][j + 1]
  end
end

local score = 0
for i = 1, n do
  local imi, posi = imos[i], pos[i]
  for j = i, n do
    score = mma(score, imi[j - i + 1] - pos[j] + posi)
  end
end
print(score)