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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Solve{
    int N;
    long[,] Map;
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        string[] str = Console.ReadLine().Split(' ');
        N = int.Parse(str[0]);
        int M = int.Parse(str[1]);
        long[] A = new long[N];
        str = Console.ReadLine().Split(' ');
        for(int i=0;i<N-1;i++){
            A[i+1] = A[i] + int.Parse(str[i]);
        }
        long[,] B = new long[N,M];
        for(int i=0;i<N;i++){
            str = Console.ReadLine().Split(' ');
            for(int j=0;j<M;j++){
                B[i,j] = int.Parse(str[j]);
            }
        }
        Map = new long[N,N];
        for(int j=0;j<M;j++){
            RangeMinimumQuerry RMQ = new RangeMinimumQuerry(N,this);
            for(int i=2*N-2;i>=0;i--){
                if(i >= RMQ.segf){
                    RMQ.X[i] = B[RMQ.ToPoint(i),j];
                    RMQ.Y[i] = RMQ.ToPoint(i);
                }
                else{
                    int sl = i*2+1;
                    int sr = i*2+2;
                    if(RMQ.X[sl] > RMQ.X[sr]){
                        RMQ.X[i] = RMQ.X[sl];
                        RMQ.Y[i] = RMQ.Y[sl];
                    }
                    else{
                        RMQ.X[i] = RMQ.X[sr];
                        RMQ.Y[i] = RMQ.Y[sr];
                    }
                }
            }
            F(RMQ,0,N-1);
        }
        long max = 0;
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                Map[i,j] += (i == 0 ? 0 : Map[i-1,j]) + (j == 0 ? 0 : Map[i,j-1]) - ((i == 0 || j == 0) ? 0 : Map[i-1,j-1]);
            }
        }
        for(int i=0;i<N;i++){
            for(int j=i;j<N;j++){
                max = Math.Max(max,Map[i,j]+A[i]-A[j]);
            }
        }
        sb.Append(max+"\n");
    }
    void F(RangeMinimumQuerry RMQ,int l,int r){
        RMQ.Querry(l,r);
        int s = RMQ.C;
        Add(l,s,s,r,RMQ.D);
        if(s != l){
            F(RMQ,l,s-1);
        }
        if(s != r){
            F(RMQ,s+1,r);
        }
    }
    void Add(int x1,int x2,int y1,int y2,long C){
        Map[x1,y1] += C;
        if(x2 != N-1){
            Map[x2+1,y1] -= C;
        }
        if(y2 != N-1){
            Map[x1,y2+1] -= C;
        }
        if(x2 != N-1 && y2 != N-1){
            Map[x2+1,y2+1] += C;
        }
    }
}
class RangeMinimumQuerry{
    Solve Sol;
    public long[] X;
    public int[] Y;
    public int C;
    public long D;
    //葉の最初の場所
    public int segf;
    //segfのbit
    public int segfb;
    //葉の深さの内浅い方
    public int depth;
    //浅い葉の内座標が最大のもの
    public int borderP;
    //はみ出すものの数
    public int border;
    public RangeMinimumQuerry(int N,Solve So){
        Sol = So;
        C = 0;
        D = 0;
        X = new long[2*N-1];
        Y = new int[2*N-1];
        for(depth = 1;(1 << depth) <= N;depth++){
            ;
        }
        segf = N-1;
        borderP = (1 << depth) - 2;
        border = 2*N - borderP - 2;
        segfb = border/2;
    }
    public int ToBit(int p){
        if(p >= border){
            return segfb + p - border;
        }
        else{
            return p;
        }
    }
    //葉のある場所
    public int ToLeaf(int p){
        if(p >= border){
            return segf + p - border;
        }
        else{
            return borderP + p + 1;
        }
    }
    //葉の示す頂点
    public int ToPoint(int leaf){
        if(leaf > borderP){
            return leaf - borderP - 1;
        }
        else{
            return leaf - segf + border;
        }
    }
    //vの子の内bより大きいものに(bは子孫,bはToBitしたもの)
    void More(int b,int v,int depth){
        if((b & (1 << depth)) == 0){
            //右の子は全てbより大きい
            if(((b+1) % (1 << depth)) != 0){
                More(b,v*2+1,depth-1);
                Act(v*2+2);
            }
            else{
                Act(v*2+2);
            }
        }
        else{
            //左の子は全てb未満
            More(b,v*2+2,depth-1);
        }
    }
    //vの子の内bより小さなものに(bは子孫,bはToBitしたもの)
    void Less(int b,int v,int depth){
        if((b & (1 << depth)) == 0){
            //右の子は全てbより大きい
            Less(b,v*2+1,depth-1);
        }
        else{
            //左の子は全てb未満
            if(b % (1 << depth) != 0){
                Act(v*2+1);
                Less(b,v*2+2,depth-1);
            }
            else{
                //右の子は全てb以上
                Act(v*2+1);
            }
        }
    }
    void Section(int bl,int br,int v,int dl,int dr){
        if(v >= segf){
            //葉
            Act(v);
        }
        else{
            if((bl & (1 << dl)) == 0 && (br & (1 << dr)) == 0){
                //両方左
                Section(bl,br,v*2+1,dl-1,dr-1);
            }
            else if((bl & (1 << dl)) != 0 && (br & (1 << dr)) != 0){
                //両方右
                Section(bl,br,v*2+2,dl-1,dr-1);
            }
            else{
                //左と右に分かれた
                if(bl % (1 << dl) != 0){
                    More(bl-1,v*2+1,dl-1);
                }
                else{
                    //左は全て含まれる
                    Act(v*2+1);
                }
                if((br + 1) % (1 << dl) != 0){
                    Less(br+1,v*2+2,dr-1);
                }
                else{
                    //右は全て含まれる
                    Act(v*2+2);
                }
            }
        }
    }
    public void Querry(int l0,int r0){
        D = 0;
        int bl = ToBit(l0);
        int br = ToBit(r0);
        int dl = l0 >= border ? depth - 2 : depth - 1;
        int dr = r0 >= border ? depth - 2 : depth - 1;
        Section(bl,br,0,dl,dr);
    }
    void Act(int v){
        if(D < X[v]){
            D = X[v];
            C = Y[v];
        }
    }
}