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: Wrong Answer)

#include <iostream>
#include <cctype>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long ll;

int read()
{
	int out = 0;
	char c;
	while (!isdigit(c = getchar()));
	for (; isdigit(c); c = getchar()) out = out * 10 + c - '0';
	return out; 
}

const int N = 5010;
const int M = 205;
const int B = 1000;
const int MAXOP = 2.1e9;

int n, m, d[N], w[N][M], mx[M], cnt;
ll ans, res[N], blres[N];

ll calc(int x)
{
	if (res[x]) return res[x];
	ll out = 0, tmp = 0;
	memset(mx, 0, sizeof(mx));
	cnt += (n - x + 1) * m;
	for (int i = x; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
		    int wij = w[i][j];
		    int & mxj = mx[j];
			if (wij > mxj)
			{
				tmp += wij - mxj;
				mxj = wij;
			}
		}
		out = max(out, tmp);
		tmp -= d[i];
	}
	ans = max(ans, out);
	return res[x] = out;
}

void sa(int l, int r, double speed)
{
	int cur = rand() % (r - l + 1) + l;
	double t = 1e3;
	
	while (t > 0.001)
	{
		ll old = calc(cur);
		ll range = ceil(t);
		int rl = max((ll) l, cur - range);
		int rr = min((ll) r, cur + range);
		int nxt = rand() % (rr - rl + 1) + l;
		ll delta = old - calc(nxt);
		//if (calc(nxt) > old) cerr << exp(-delta / t) << endl;
		if (exp(-delta / t) > (double) rand() / RAND_MAX) cur = nxt;
		t *= speed;
		if (cnt > MAXOP) break;
	}
	
	blres[l] = max(blres[l], calc(cur));
}

int main()
{
	int* qwq = new int;
	srand(time(0) ^ (ll) qwq);
	
	n = read();
	m = read();
	
	for (int i = 1; i < n; ++i) d[i] = read();
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			w[i][j] = read();
		}
	}
	
	if ((ll) n * n * m <= MAXOP) for (int i = 1; i <= n; ++i) calc(i);
	
	else
	{
		/*double sp = 0.8;
		int eq = 0;
		while (cnt < MAXOP)
		{
			int old = cnt;
			sa(1, n, sp *= 1.01);
			if (cnt == old)
			{
				if (++eq > 10) break;
			}
			else eq = 0;
		}*/
		
		/*ll best = 0;
		
		while (cnt < MAXOP / 2)
		{
			int old = cnt;
			for (int i = 1; i <= n; i += B)
			{
				sa(i, min(i + B - 1, n), 0.6);
				best = max(best, blres[i]);
			}
			if (cnt == old) break;
		}*/
		sa(1, n, 0.9);
		
		int eq = 0; 
		while (cnt < MAXOP)
		{
			int old = cnt;
			for (int i = 1; i <= n; i += B)
			{
				sa(i, min(i + B - 1, n), 0.8);
			}
			if (cnt == old)
			{
				if (++eq > 100) break;
			}
			else eq = 0;
		}
	}
	
	cout << ans;
	
	return 0;
}