Language: C++

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

int n, m, k;
vector<vector<ll> > arr;

vector<vector<vector<ll> > > mp;

int solve(int r, int c, int step){
    if(mp[r][c][step] != -1)return mp[r][c][step];
    if(r == n && c == m){
        return arr[r][c];
    }
    int down = INT_MIN;
    if(r != n){
        down = solve(r + 1, c, 2) + arr[r][c];
    }
    int right = INT_MIN;
    if(step > 0 && c + 1 <= m){
        right = max(solve( r, c + 1, step),solve(r, c + 1, step - 1) + arr[r][c]);
    }else if(c + 1<= m){
        right = solve(r, c + 1, step);
    }
    mp[r][c][step] = max(right, down);
    return mp[r][c][step];
}

int main() {
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    cin >> n >> m >> k;
    mp.assign(n + 1, vector<vector<ll> > (m + 1, vector<ll>(4, -1)));
    arr.assign(n + 1, vector<ll>(m + 1));
    for(int i = 0; i < k; i++){
        int r, c, v;
        cin >> r >> c >> v;
        arr[r][c] = v;
    }
    int ans = solve(1, 1, 2);
    cout << ans << endl;
}


Compiler output:
a.cc: In function 'int solve(int, int, int)':
a.cc:22:20: error: no matching function for call to 'max(int, __gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type)'
   22 |         right = max(solve( r, c + 1, step),solve(r, c + 1, step - 1) + arr[r][c]);
      |                 ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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:1:
/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:22:20: note:   deduced conflicting types for parameter 'const _Tp' ('int' and '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'})
   22 |         right = max(solve( r, c + 1, step),solve(r, c + 1, step - 1) + arr[r][c]);
      |                 ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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:22:20: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   22 |         right = max(solve( r, c + 1, step),solve(r, c + 1, step - 1) + arr[r][c]);
      |                 ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~