Language: C++

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef pair<int,int> P;
typedef long long int ll;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}

const int MAX_N = 1 << 19;
#define NUM 2147483647

int nn = 1;
int data[MAX_N][210];

void init(int first_N){
    nn = 1;
    if(first_N < 10) nn = 100;
	while(nn < first_N)nn *= 2;
}

// [left,right]の区間の値をvalueに更新する
// update_(left,right,value,0,0,nn-1)のように呼ぶ
void update_(int update_left,int update_right,int new_value,int node_id,int node_left,int node_right, int mm){
	if(update_right < node_left || update_left > node_right)return;
	else if(update_left <= node_left && update_right >= node_right){
		data[node_id][mm] = new_value;
	}else{
		if(data[node_id][mm] >= 0){
			data[2*node_id+1][mm] = data[node_id][mm];
			data[2*node_id+2][mm] = data[node_id][mm];
			data[node_id][mm] = -1;
		}
		update_(update_left,update_right,new_value,2*node_id+1,node_left,(node_left+node_right)/2,mm);
		update_(update_left,update_right,new_value,2*node_id+2,(node_left+node_right)/2+1,node_right,mm);
	}
}

// query_(ite,ite,0,0,nn-1)のように呼ぶ
int query_(int search_left,int search_right,int node_id,int node_left,int node_right,int mm){
	if(search_right < node_left || search_left > node_right){
		return -1;
	}else if(node_left <= search_left && node_right >= search_right && data[node_id][mm] >= 0){
		return data[node_id][mm];
	}else{
		int left = query_(search_left,search_right,2*node_id+1,node_left,(node_left+node_right)/2,mm);
		int right = query_(search_left,search_right,2*node_id+2,(node_left+node_right)/2+1,node_right,mm);
		return max(left,right);
	}
}

int main(){
    int n, m;
    cin >> n >> m;
    init(n);
    ll a[n] = {};
    srep(i,1,n)cin >> a[i];
    ll b[n][m];
    rep(i,n)rep(j,m)cin >> b[i][j];
    ll sum[n] = {};
    rep(i,n){
        if(i>0)sum[i] += sum[i-1];
        sum[i] += a[i];
    }
    deque<int> que[m];
    ll ans = 0;
    int now = n-1;
    
    drep(i,n){
        // cout << endl;
        rep(j,m){
            while(que[j].size()>0 && b[i][j] >= b[que[j][0]][j]){
                que[j].pop_front();
            }
            int r = n;
            if(que[j].size()>0){
                r = que[j][0];
            }
            update_(i,r-1,b[i][j],0,0,nn-1,j);
            que[j].push_front(i);
        }

        while(i<now){
            ll bef = -(sum[now] - sum[i]);
            rep(j,m){
                bef += query_(now,now,0,0,nn-1,j);
                // cout << query_(now,now,0,0,nn-1,j) << ' ';
            } 
            // cout << endl;
            ll aft = -(sum[now-1] - sum[i]);
            rep(j,m) aft += query_(now-1,now-1,0,0,nn-1,j);
            // cout << i << ' ' << now << ' ' << bef << ' ' << aft << endl;
            if(aft >= bef){
                now--;
            }else{
                break;
            }
        }

        ll tmp = -(sum[now] - sum[i]);
        rep(j,m) tmp += query_(now,now,0,0,nn-1,j);
        ans = max(ans, tmp);

    }
    // cout << now << endl;
    // cout << m << endl;

    cout << ans << endl;

    return 0;
}


Compiler output:
a.cc: In function 'void update_(int, int, int, int, int, int, int)':
a.cc:29:17: error: reference to 'data' is ambiguous
   29 |                 data[node_id][mm] = new_value;
      |                 ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:16:5: note:                 'int data [524288][210]'
   16 | int data[MAX_N][210];
      |     ^~~~
a.cc:31:20: error: reference to 'data' is ambiguous
   31 |                 if(data[node_id][mm] >= 0){
      |                    ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:16:5: note:                 'int data [524288][210]'
   16 | int data[MAX_N][210];
      |     ^~~~
a.cc:32:25: error: reference to 'data' is ambiguous
   32 |                         data[2*node_id+1][mm] = data[node_id][mm];
      |                         ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:16:5: note:                 'int data [524288][210]'
   16 | int data[MAX_N][210];
      |     ^~~~
a.cc:32:49: error: reference to 'data' is ambiguous
   32 |                         data[2*node_id+1][mm] = data[node_id][mm];
      |                                                 ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:16:5: note:                 'int data [524288][210]'
   16 | int data[MAX_N][210];
      |     ^~~~
a.cc:33:25: error: reference to 'data' is ambiguous
   33 |                         data[2*node_id+2][mm] = data[node_id][mm];
      |                         ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:16:5: note:                 'int data [524288][210]'
   16 | int data[MAX_N][210];
      |     ^~~~
a.cc:33:49: error: reference to 'data' is ambiguous
   33 |                         data[2*node_id+2][mm] = data[node_id][mm];
      |                                                 ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:16:5: note:                 'int data [524288][210]'
   16 | int data[MAX_N][210];
      |     ^~~~
a.cc:34:25: error: reference to 'data' is ambiguous
   34 |                         data[node_id][mm] = -1;
      |                         ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:16:5: note:                 'int data [524288][210]'
   16 | int data[MAX_N][210];
      |     ^~~~
a.cc: In function 'int query_(int, int, int, int, int, int)':
a.cc:45:76: error: reference to 'data' is ambiguous
   45 |         }else if(node_left <= search_left && node_right >= search_right && data[node_id][mm] >= 0){
      |                                                                            ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:16:5: note:                 'int data [524288][210]'
   16 | int data[MAX_N][210];
      |     ^~~~
a.cc:46:24: error: reference to 'data' is ambiguous
   46 |                 return data[node_id][mm];
      |                        ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _