Language: C++

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
     
using namespace std;
     
typedef long long ll;
typedef pair<ll, int> pii;
     
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
     
mt19937 rnd(chrono::steady_clock().now().time_since_epoch().count());

const int N = 5009;

struct Node{    
    ll value;
    ll lazy;
};

Node T[N * 4 + 512];

void push(int node, int cl, int cr){
    T[node].value += T[node].lazy;
    if(cl != cr){
        T[node * 2].lazy += T[node].lazy;   
        T[node * 2 + 1].lazy += T[node].lazy;
    }
    T[node].lazy = 0;
}

void update(int node, int cl, int cr, int tl, int tr, ll vl){
    push(node, cl, cr);
    if(cr < tl)
        return;
    if(cl > tr)
        return;
    if(cl >= tl && cr <= tr){
        T[node].lazy = vl;
        push(node, cl, cr);
        return;
    }
    int mid = (cl + cr) / 2;
    update(node * 2, cl, mid, tl, tr, vl);
    update(node * 2 + 1, mid + 1, cr, tl, tr, vl);
    T[node].value = max(T[node * 2].value, T[node * 2 + 1].value);
}

const ll inf = (ll)1e16;

int query(int node, int cl, int cr, int tl, int tr){
    push(node, cl, cr);
    if(cr < tl)
        return -inf;
    if(cl > tr)
        return -inf;
    if(cl >= tl && cr <= tr)
        return T[node].value;
    int mid = (cl + cr) / 2;
    return max(query(node * 2, cl, mid, tl, tr), query(node * 2 + 1, mid + 1, cr, tl, tr));
}

const int M = 209;
ll c[N][M];
ll a[M];
stack<pii> ff[M];

int main(){
    fastIO;
    int n, m;
    cin >> n >> m;
    for(int i = 2; i <= n; i ++ )
        cin >> a[i];
    for(int i = 1; i <= n ; i ++ ){
        for(int j = 1; j <= m ; j ++ )
            cin >> c[i][j];
    }
    ll res = 0;
    int id;
    for(int i = 1; i <= n; i ++ ){
        if(i)
            update(1, 1, n, 1, i - 1, -a[i]);
        for(int j = 1; j <= m ; j ++ ){
            id = i;
            update(1, 1, n, id, id, c[i][j]);
            while(!ff[j].empty() && c[i][j] > ff[j].top().fi){
                update(1, 1, n, ff[j].top().se, id - 1, c[i][j] - ff[j].top().fi);
                id = ff[j].top().se;
                ff[j].pop();
            }
            ff[j].push(mp(c[i][j], i));
        }
        res = max(res, query(1, 1, n, 1, i));
    }
    cout << res << "\n";
    return 0;
}


Compiler output:
a.cc: In function 'int query(int, int, int, int, int)':
a.cc:56:16: warning: overflow in conversion from 'long long int' to 'int' changes value from '-10000000000000000' to '-1874919424' [-Woverflow]
   56 |         return -inf;
      |                ^~~~
a.cc:58:16: warning: overflow in conversion from 'long long int' to 'int' changes value from '-10000000000000000' to '-1874919424' [-Woverflow]
   58 |         return -inf;
      |                ^~~~
a.cc: In function 'int main()':
a.cc:95:18: error: no matching function for call to 'max(ll&, int)'
   95 |         res = max(res, query(1, 1, n, 1, i));
      |               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:95:18: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   95 |         res = max(res, query(1, 1, n, 1, i));
      |               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:95:18: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   95 |         res = max(res, query(1, 1, n, 1, i));
      |               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~