Language: C++

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define vll vector<ll>
#define vvll vector<vll>
#define mll map<ll,ll>
#define f(i,m,n) for(i=m;i<n;i++)
#define ee endl
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define take(v,n) for(i=0;i<n;i++) cin>>v[i];
#define NO() cout<<"NO"<<endl
#define YES() cout<<"YES"<<endl
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sot(v) sort(v.begin(),v.end());
bool isPrime(int n) 
{ 
    // Corner cases 
    if (n <= 1)  return false; 
    if (n <= 3)  return true; 
  
    // This is checked so that we can skip  
    // middle five numbers in below loop 
    if (n%2 == 0 || n%3 == 0) return false; 
  
    for (int i=5; i*i<=n; i=i+6) 
        if (n%i == 0 || n%(i+2) == 0) 
           return false; 
  
    return true; 
}
ll ans=0;
ll a[3001][3001];
map<pair<ll,ll>,ll> val;
ll n,m;
int dp[3001][3001][4];
ll sol(int i,int j,int k)
{
    //cout<<i<<" "<<j<<' '<<k<<ee;
    if(i>=n || j>=m) return 0;
    if(k==0) 
    {
        return 0;
    }
    if(a[i][j]==0)
    {
        ll a1=0;
        ll a2=0;
        if(i+1<n) 
        {
            a1=sol(i+1,j,3);
         
        }
        if(j+1<m ) 
        {
            a2=sol(i,j+1,k);

        }
        return max(a1,a2);
    }
    else
    {
        ll a1=0,a2=0;
        if(i+1<n ) a1=sol(i+1,j,3);
        if(j+1<m) a2=sol(i,j+1,k-1);
        ll a3=val[{i,j}]+max(a1,a2);
        a1=0;a2=0;
        if(i+1<n ) a1=sol(i+1,j,3);
        if(j+1<m ) a2=sol(i,j+1,k);
        a3=max(a3,max(a1,a2));
        //cout<<"in "<<i<<" "<<j<<" "<<k<<" "<<a3<<ee;
        return a3;
    }
    //cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<ee;
    return dp[i][j][k];
}

void solve()
{
    ll i,j,k,l;
    cin>>n>>m>>k;
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=m;j++)
        {
            for(l=0;l<=3;l++)
            dp[i][j][k]=-1;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
            a[i][j]=0;
    }
    while(k--)
    {
        cin>>i>>j>>l;
        a[i-1][j-1]=1;
        val[{i-1,j-1}]=l;
    }
    // for(i=0;i<n;i++)
    // {
    //     for(j=0;j<m;j++)
    //     {
    //         cout<<a[i][j]<<" ";
    //     }
    //     cout<<ee;
    // }
    ans=0;
    k=sol(0,0,3);
    cout<<k<<ee;
}


int main() {
    fast;
    ll i,j,l,k,t,n;
    t=1;
    while(t--)
    solve();
}
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define vll vector<ll>
#define vvll vector<vll>
#define mll map<ll,ll>
#define f(i,m,n) for(i=m;i<n;i++)
#define ee endl
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define take(v,n) for(i=0;i<n;i++) cin>>v[i];
#define NO() cout<<"NO"<<endl
#define YES() cout<<"YES"<<endl
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sot(v) sort(v.begin(),v.end());
bool isPrime(int n) 
{ 
    // Corner cases 
    if (n <= 1)  return false; 
    if (n <= 3)  return true; 
  
    // This is checked so that we can skip  
    // middle five numbers in below loop 
    if (n%2 == 0 || n%3 == 0) return false; 
  
    for (int i=5; i*i<=n; i=i+6) 
        if (n%i == 0 || n%(i+2) == 0) 
           return false; 
  
    return true; 
}
ll ans=0;
ll a[3001][3001];
map<pair<ll,ll>,ll> val;
ll n,m;
int dp[3001][3001][4];
ll sol(int i,int j,int k)
{
    //cout<<i<<" "<<j<<' '<<k<<ee;
    if(i>=n || j>=m) return 0;
    if(k==0) 
    {
        return 0;
    }
    if(a[i][j]==0)
    {
        ll a1=0;
        ll a2=0;
        if(i+1<n) 
        {
            a1=sol(i+1,j,3);
         
        }
        if(j+1<m ) 
        {
            a2=sol(i,j+1,k);

        }
        return max(a1,a2);
    }
    else
    {
        ll a1=0,a2=0;
        if(i+1<n ) a1=sol(i+1,j,3);
        if(j+1<m) a2=sol(i,j+1,k-1);
        ll a3=val[{i,j}]+max(a1,a2);
        a1=0;a2=0;
        if(i+1<n ) a1=sol(i+1,j,3);
        if(j+1<m ) a2=sol(i,j+1,k);
        a3=max(a3,max(a1,a2));
        //cout<<"in "<<i<<" "<<j<<" "<<k<<" "<<a3<<ee;
        return a3;
    }
    //cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<ee;
    return dp[i][j][k];
}

void solve()
{
    ll i,j,k,l;
    cin>>n>>m>>k;
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=m;j++)
        {
            for(l=0;l<=3;l++)
            dp[i][j][k]=-1;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
            a[i][j]=0;
    }
    while(k--)
    {
        cin>>i>>j>>l;
        a[i-1][j-1]=1;
        val[{i-1,j-1}]=l;
    }
    // for(i=0;i<n;i++)
    // {
    //     for(j=0;j<m;j++)
    //     {
    //         cout<<a[i][j]<<" ";
    //     }
    //     cout<<ee;
    // }
    ans=0;
    k=sol(0,0,3);
    cout<<k<<ee;
}


int main() {
    fast;
    ll i,j,l,k,t,n;
    t=1;
    while(t--)
    solve();
}


Compiler output:
a.cc:141:6: error: redefinition of 'bool isPrime(int)'
  141 | bool isPrime(int n)
      |      ^~~~~~~
a.cc:18:6: note: 'bool isPrime(int)' previously defined here
   18 | bool isPrime(int n)
      |      ^~~~~~~
a.cc:157:4: error: redefinition of 'll ans'
  157 | ll ans=0;
      |    ^~~
a.cc:34:4: note: 'll ans' previously defined here
   34 | ll ans=0;
      |    ^~~
a.cc:158:4: error: redefinition of 'll a [3001][3001]'
  158 | ll a[3001][3001];
      |    ^
a.cc:35:4: note: 'll a [3001][3001]' previously declared here
   35 | ll a[3001][3001];
      |    ^
a.cc:159:21: error: redefinition of 'std::map<std::pair<long long int, long long int>, long long int> val'
  159 | map<pair<ll,ll>,ll> val;
      |                     ^~~
a.cc:36:21: note: 'std::map<std::pair<long long int, long long int>, long long int> val' previously declared here
   36 | map<pair<ll,ll>,ll> val;
      |                     ^~~
a.cc:160:4: error: redefinition of 'll n'
  160 | ll n,m;
      |    ^
a.cc:37:4: note: 'll n' previously declared here
   37 | ll n,m;
      |    ^
a.cc:160:6: error: redefinition of 'll m'
  160 | ll n,m;
      |      ^
a.cc:37:6: note: 'll m' previously declared here
   37 | ll n,m;
      |      ^
a.cc:161:5: error: redefinition of 'int dp [3001][3001][4]'
  161 | int dp[3001][3001][4];
      |     ^~
a.cc:38:5: note: 'int dp [3001][3001][4]' previously declared here
   38 | int dp[3001][3001][4];
      |     ^~
a.cc:162:4: error: redefinition of 'll sol(int, int, int)'
  162 | ll sol(int i,int j,int k)
      |    ^~~
a.cc:39:4: note: 'll sol(int, int, int)' previously defined here
   39 | ll sol(int i,int j,int k)
      |    ^~~
a.cc:203:6: error: redefinition of 'void solve()'
  203 | void solve()
      |      ^~~~~
a.cc:80:6: note: 'void solve()' previously defined here
   80 | void solve()
      |      ^~~~~
a.cc:240:5: error: redefinition of 'int main()'
  240 | int main() {
      |     ^~~~
a.cc:117:5: note: 'int main()' previously defined here
  117 | int main() {
      |     ^~~~