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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

typedef long double ld;
typedef long long ll;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define pb push_back
#define mp make_pair
#define sz(x) ((int) (x).size())
#define TASK "text"

const int inf = (int) 1.01e9;
const long long infll = (long long) 1.01e18;
const ld eps = 1e-9;
const ld pi = acos((ld) -1);

mt19937 mrand(random_device{} ()); 

int rnd(int x) {
  return mrand() % x;
}

void precalc() {
}

const int maxn = 5005, maxm = 205;
int n, m;
int a[maxn];
int b[maxn][maxm];

int read() {
  if (scanf("%d%d", &n, &m) < 2) {
    return false;
  }
  for (int i = 0; i < n - 1; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &b[i][j]);
    }
  }
  return true;
}

struct node {
  long long add, val;
};


node st[4 * maxn];

void buildst(int v, int tl, int tr) {
  st[v].add = 0;
  st[v].val = -infll;
  if (tl == tr - 1) {
    return;
  }
  int tm = (tl + tr) / 2;
  buildst(2 * v, tl, tm);
  buildst(2 * v + 1, tm, tr);
}

void push(int v) {
  if (!st[v].add) {
    return;
  }
  st[2 * v].add += st[v].add;
  st[2 * v].val += st[v].add;
  st[2 * v + 1].add += st[v].add;
  st[2 * v + 1].val += st[v].add;
  st[v].add = 0;
}

void add(int v, int tl, int tr, int l, int r, long long toadd) {
  if (tl == l && tr == r) {
    st[v].add += toadd;
    st[v].val += toadd;
    return;
  }
  push(v);
  int tm = (tl + tr) / 2;
  if (l < tm) {
    add(2 * v, tl, tm, l, min(r, tm), toadd);
  }
  if (r > tm) {
    add(2 * v + 1, tm, tr, max(l, tm), r, toadd);
  }
  st[v].val = max(st[2 * v].val, st[2 * v + 1].val);
}

vector< pair<int, int> > stck[maxm];

void solve() {
  for (int i = 0; i < m; i++) {
    stck[i].clear();
  }
  buildst(1, 0, n);
  long long res = -infll;
  for (int i = 0; i < n; i++) {
    long long cur = 0;
    for (int j = 0; j < m; j++) {
      cur += b[i][j];
      int lst = i;
      while (!stck[j].empty() && stck[j].back().second <= b[i][j]) {
        add(1, 0, n, stck[j].back().first, lst, b[i][j] - stck[j].back().second);
        lst = stck[j].back().first;
        stck[j].pop_back();
      }
      stck[j].push_back(make_pair(lst, b[i][j]));
    }
    if (i) {
      add(1, 0, n, 0, i, -a[i - 1]);
    }
    add(1, 0, n, i, i + 1, cur + infll);
    res = max(res, st[1].val);
  }
  printf("%lld\n", res);
}

int main() {
  precalc();
#ifdef LOCAL
  assert(freopen(TASK ".in", "r", stdin));
  assert(freopen(TASK ".out", "w", stdout));
#endif
  while (true) {
    if (!read()) {
      break;
    }
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}