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++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define v vector
#define ALL(x) x.begin(),x.end()
#define rALL(x) x.rbegin(),x.rend()
#define DEBUG

template<typename T> void DBG(const char* name, T&& H) {
	cerr << name << " = " << H << ')' << endl;
}

template<typename T, typename... Args> void DBG(const char* names, T&& H, Args&&... args) {
	const char* NEXT = strchr(names+1, ',');
	cerr.write(names, NEXT-names) << " = " << H << " |";
	DBG(NEXT+1, args...);
}

#ifdef DEBUG
	#define dbg(...) cerr << __FUNCTION__ << ' ' <<__LINE__ << " : (", DBG(#__VA_ARGS__, __VA_ARGS__)
#else
	#define dbg(...)
#endif

using ll = long long;
using ld = long double;

const long long mod = 1e9+7;
const long double PI = acos(-1.0);
const long long INF = 1e18;
const int maxN = 2e5+1;

struct SparseTable {
	using S = int;
	using T = long long;
	S n, k, m;
	vector<vector<vector<T>>> table;
	void init(S N, S M) {
		n = N;
		m = M;
		k = (S)(log2(n)) + 1;
		table.assign(k, vector<vector<T>> (m, vector<T>(n)));
	}
	inline T func(T a, T b) {
		return max(a, b);
	}
	void build(vector<vector<T>> &arr) {
		for(S i = 0; i < n; i ++) for(int j = 0; j < m; j ++) table[0][j][i] = arr[j][i];
		for(S j = 1; j <= k; j ++) {
			for(S l = 0; l < m; l ++) {
				for(S i = 0; i + (1<<j) <= n; i ++) {
					table[j][l][i] = func(table[j-1][l][i], table[j-1][l][i + (1 << (j-1))]);
				}
			}
		}
	}
	T query(S left, S right) {
		S length = (right - left + 1);
		S K = (S)(log2(length));
		T ans = 0;
		for(int i = 0; i < m; i ++) {
			ans += func(table[K][i][left], table[K][i][right + 1 - (1 << K)]);
		}
		return ans;
	}
};

int N, M;
vector<long long> A(int(5e3), 0);
vector<vector<long long>> mat(200, vector<long long>(int(5e3)));
SparseTable st;

long long CostFunction(long long L, long long R) {
	long long happy = st.query(L, R);
	happy -= A[R];
	if(L-1 >= 0) happy += A[L-1];
	return happy;
} 

long long DivideAndConquerDP(int L, int R, int optL, int optR) {
	if(L > R) return 0;
	int M = (L+R)/2, nextOpt = -1;
	long long best = -1e9;
	for(int i = max(M, optL); i <= optR; i ++) {
		long long val = CostFunction(M, i);
		if(val >= best) {
			best = val;
			nextOpt = i;
		}
	}
	best = max({best, DivideAndConquerDP(L, M-1, optL, nextOpt), DivideAndConquerDP(M+1, R, nextOpt, optR)});
	return best;
}

void Solve() {
	cin >> N >> M;
	for(int i = 1; i < N; i ++) {
		cin >> A[i];
		A[i] += A[i-1];
	}
	for(int i = 0; i < N; i ++) {
		for(int j = 0; j < M; j ++) {
			cin >> mat[j][i];
		}
	}
	st.init(N, M);
	st.build(mat);
	long long ans = DivideAndConquerDP(0, N-1, 0, N-1);
	cout << ans << endl;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int tt = 1; 
	for(int i = 1; i <= tt; i ++) {
		Solve();
	}
	
	return 0;
}