Language: C++

#include <bits/stdc++.h>
#define ll long long
#define INF (1LL << 60)
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 5003;

int b[MAX_N][200];
ll dp[MAX_N][MAX_N];
ll sm[MAX_N];
int n,m;

template<typename T> class SparseTable {
private:
    vector<T> arr;
    vector<int> logTable;
    vector<vector<int> > table; //最小値のインデックスを保持
    int sz;
public:
    SparseTable(vector<T>& v){
        sz = (int)v.size();
        arr = v;
        logTable.resize(sz+1);
        for(int i = 2; i < sz + 1; i++){
            logTable[i] = logTable[i >> 1] + 1;
        }
        table.resize(sz,vector<T>(logTable[sz]+1));
        for(int i = 0; i < sz; i++){
            table[i][0] = i;
        }
        for(int k = 1; (1 << k) <= sz; k++){
            for(int i = 0; i + (1 << k) <= sz; i++){
                int s = table[i][k-1];
                int t = table[i + (1 << (k-1))][k-1];
                if(arr[s] < arr[t]){
                    table[i][k] = s;
                }else{
                    table[i][k] = t;
                }
            }
        }
    }
    pair<T,int> query(int l,int r){
        int k = logTable[r-l];
        if(arr[table[l][k]] < arr[table[r-(1<<k)][k]]){
            return make_pair(arr[table[l][k]],table[l][k]);
        }else{
            return make_pair(arr[table[r-(1<<k)][k]],table[r-(1<<k)][k]);
        }
    }
};

inline void dfs(int l, int r, SparseTable<int>& sp)
{
    if(r <= l) return;
    P p = sp.query(l,r);
    dp[l][r] += p.fi, dp[l][p.se] -= p.fi, dp[p.se+1][r] -= p.fi, dp[p.se+1][p.se] += p.fi;
    dfs(l,p.se,sp), dfs(p.se+1,r,sp);
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> m;
    rep(i,n-1){
        int a;
        cin >> a;
        sm[i+2] = sm[i+1] + a;
    }
    rep(i,n){
        rep(j,m){
            cin >> b[i][j];
        }
    }
    rep(i,m){
        SparseTable<int> sp(i);
        dfs(0,n,sp);
    }
    rep(i,n+1){
        rrep(j,n+1){
            dp[i][j] += dp[i][j+1];
        }
    }
    rrep(j,n+1){
        rep(i,n+1){
            dp[i+1][j] += dp[i][j];
        }
    }
    ll ans = -INF;
    rep(i,n){
        srep(j,i+1,n+1){
            cmx(ans,dp[i][j]-(sm[j]-sm[i+1]));
        }
    }
    cout << ans << "\n";
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:109:30: error: no matching function for call to 'SparseTable<int>::SparseTable(int&)'
  109 |         SparseTable<int> sp(i);
      |                              ^
a.cc:52:5: note: candidate: 'SparseTable<T>::SparseTable(std::vector<_Tp>&) [with T = int]'
   52 |     SparseTable(vector<T>& v){
      |     ^~~~~~~~~~~
a.cc:52:28: note:   no known conversion for argument 1 from 'int' to 'std::vector<int>&'
   52 |     SparseTable(vector<T>& v){
      |                 ~~~~~~~~~~~^
a.cc:45:28: note: candidate: 'SparseTable<int>::SparseTable(const SparseTable<int>&)'
   45 | template<typename T> class SparseTable {
      |                            ^~~~~~~~~~~
a.cc:45:28: note:   no known conversion for argument 1 from 'int' to 'const SparseTable<int>&'
a.cc:45:28: note: candidate: 'SparseTable<int>::SparseTable(SparseTable<int>&&)'
a.cc:45:28: note:   no known conversion for argument 1 from 'int' to 'SparseTable<int>&&'