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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports, unused_variables, dead_code)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

trait InputValue {
    fn parse(s: &str) -> Self;
}

fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
	}
}
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
	($($t:ident),*) => {
		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
			fn parse(s: &str) -> ($($t),*) {
				let mut tokens = s.split_whitespace();
				let t = ($($t::parse(tokens.next().unwrap())),*);
				t
			}
		}
	}
}
parse_tuple!(A, B);
parse_tuple!(A, B, C);

// ===

struct BucketArray {
    n: usize,
    bn: usize,
    bucket_size: usize,
    data: Vec<i32>,
    bucket: Vec<i32>,
}

impl BucketArray {
    fn new(n: usize) -> Self {
        let bucket_size = max(50, (n as f64).sqrt() as usize);
        let bn = (n + bucket_size - 1) / bucket_size;

        BucketArray {
            n: n,
            bn: bn,
            bucket_size: bucket_size,
            data: vec![0; n],
            bucket: vec![0; bn]
        }
    }

    fn less(&self, x: usize, default: usize) -> usize {
        let bi = x / self.bucket_size;
        for xi in (bi*self.bucket_size..x).rev() {
            if self.data[xi] == 1 {
                return xi;
            }
        }
        for bi in (0..bi).rev() {
            if self.bucket[bi] >= 1 {
                let from = bi * self.bucket_size;
                for xi in (from..min(from+self.bucket_size, self.n)).rev() {
                    if self.data[xi] >= 1 {
                        return xi;
                    }
                }
            }
        }
        default
    }

    fn more(&self, x: usize, default: usize) -> usize {
        let bi = x / self.bucket_size;
        for bi in bi..self.bn {
            if self.bucket[bi] >= 1 {
                let from = bi * self.bucket_size;
                for xi in max(x+1, from)..min(from+self.bucket_size, self.n) {
                    if self.data[xi] >= 1 {
                        return xi;
                    }
                }
            }
        }
        default
    }

    fn add(&mut self, x: usize) {
        self.bucket[x / self.bucket_size] += 1;
        self.data[x] += 1;
    }

    fn remove(&mut self, x: usize) {
        self.bucket[x / self.bucket_size] -= 1;
        self.data[x] -= 1;
    }
}

fn main() {
    let (n, m): (usize, usize) = read();
    let dist: Vec<i64> = readnc();

    let mut data: Vec<Vec<i64>> = vec![vec![0; n+3]; n+3];

    let mut values: Vec<Vec<i32>> = vec![vec![0; n]; m];
    for i in 0..n {
        let c: Vec<i32> = readnc();
        for j in 0..m {
            values[j][i] = c[j];
        }
    }

    for i in 0..m {
        let mut c: Vec<(i32, usize)> = values[i].clone().into_iter().zip(1..n+1).collect();
        c.sort();
        c.reverse();

        let mut bset = BucketArray::new(n+2);
        bset.add(0);
        bset.add(n+1);

        for ci in c {
            let v = ci.0 as i64;
            let idx = ci.1;
            let fx = bset.less(idx, 0)+1;
            let tx = idx+1;

            let fy = idx;
            let ty = bset.more(idx, n+1);

            data[fx][fy] += v;
            data[tx][fy] -= v;
            data[fx][ty] -= v;
            data[tx][ty] += v;

            bset.add(idx);
        }
    }

    for i in 0..n+2 {
        for j in 1..n+2 {
            data[i][j] += data[i][j-1];
        }
    }
    for j in 0..n+2 {
        for i in 1..n+2 {
            data[i][j] += data[i-1][j];
        }
    }

    let mut dx = vec![0; n];
    for i in 1..n {
        dx[i] += dx[i-1] + dist[i-1];
    }

    // 1 to n : 1 to n
    let mut ans = 0;
    for from in 1..n+1 {
        for to in from..n+1 {
            ans = max(ans, data[from][to] - (dx[to-1] - dx[from-1]));
        }
    }
    println!("{}", ans);
}