Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

//  RAKSHIT KADAM

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> 3
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree< int ,  null_type ,  less<int> ,  rb_tree_tag ,
// tree_order_statistics_node_update>
#warning !!!check the size of arrayss!!!

// NUMERIC DEFINITIONS :
#define INF 100000000000000005
#define inf 1000000004
#define MOD 1000000007
#define mod 998244353
#define MAX 300006
#define pi 3.1415926535897932384626433832795
#define ld long double
#define ll long long
#define int long long

#define P(gg)                                                                  \\
  for (auto rax : gg) {                                                        \\
    cout << rax << " ";                                                        \\
  }                                                                            \\
  cout << endl;
#define Need_for_speed(activated)                                              \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define satisfy                                                                \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define inp(n)                                                                 \\
  int n;                                                                       \\
  cin >> n
#define mp make_pair
#define pb push_back
#define endl "\\n"
#define x first
#define y second
#define fore(i, a, b) for (ll i = a; i < b; i++)
#define forci(i, n) for (ll i = 0; i < n; i++)
#define vi vector<ll>
#define Endl endl
#define lb lower_bound
#define ub upper_bound
#define pii pair<ll, ll>

#define input                                                                  \\
  freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define time                                                                   \\
  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
using namespace std;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int power(int x, unsigned int y, unsigned int m) {
  if (y == 0)
    return 1;
  int p = power(x, y / 2, m) % m;
  p = (p * p) % m;
  return (y % 2 == 0) ? p : (x * p) % m;
}
ll modInverse(int a, int m) {
  { return power(a, m - 2, m); }
}
const int maxn = 500003;
// ll fact[1000005];
// ll modIN[1000005];
// void facto()
//{
//    ll lim =1000005;fact[0]=1;
//    for(ll i=1;i<lim;i++)
//    {
//        fact[i]=(fact[i-1]*i)%MOD;
//    }
//    modIN[n]=modInverse(fact[n],MOD);
//    for(ll i=n-1;i>=1;i--)
//    {
//        modIN[i] = (modIN[i+1]*(i+1))%MOD;
//    }
//}
// ll ncr(ll a,ll b)
//{
//    ll ret = 0;
//    ret = fact[a]*(modIN[b]);
//    ret%=MOD;
//    ret*=modIN[a-b];
//    ret%=MOD;
//    return ret;
//}
ll dp[3004][3004][4];
ll a[3004][3004];
ll r, c, k;
ll rec(ll i, ll j, ll k = 0) {
  if (i >= r || j >= c)
    return 0;
  if (dp[i][j][k] != -1)
    return dp[i][j][k];
  dp[i][j][k] = 0;
  if (!a[i][j]) {
    dp[i][j][k] = max(rec(i + 1, j), rec(i, j + 1, k));
  } else {
    if (k < 3) {
      dp[i][j][k] = max(a[i][j] + rec(i, j + 1, k + 1), rec(i, j + 1, k));
      dp[i][j][k] = max(dp[i][j][k], a[i][j] + rec(i + 1, j));
    } else
      dp[i][j][k] = max(dp[i][j][k], rec(i + 1, j));
  }
  return dp[i][j][k];
}
void solve() {
  cin >> r >> c >> k;
  while (k--) {
    ll aa, b, c;
    cin >> aa >> b >> c;
    --b, --aa;
    a[aa][b] = c;
  }
  memset(dp, -1, sizeof dp);
  cout << rec(0, 0) << endl;
}
signed main() {
  Need_for_speed(activated);

  //    satisfy
  //    {
  //
  //    }

  solve();

  return 0;
}


Error: TLE

Fixed submission:

//  RAKSHIT KADAM

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> 3
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree< int ,  null_type ,  less<int> ,  rb_tree_tag ,
// tree_order_statistics_node_update>
#warning !!!check the size of arrayss!!!

// NUMERIC DEFINITIONS :
#define INF 100000000000000005
#define inf 1000000004
#define MOD 1000000007
#define mod 998244353
#define MAX 300006
#define pi 3.1415926535897932384626433832795
#define ld long double
#define ll long long
#define int long long

#define P(gg)                                                                  \\
  for (auto rax : gg) {                                                        \\
    cout << rax << " ";                                                        \\
  }                                                                            \\
  cout << endl;
#define Need_for_speed(activated)                                              \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define satisfy                                                                \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define inp(n)                                                                 \\
  int n;                                                                       \\
  cin >> n
#define mp make_pair
#define pb push_back
#define endl "\\n"
#define x first
#define y second
#define fore(i, a, b) for (ll i = a; i < b; i++)
#define forci(i, n) for (ll i = 0; i < n; i++)
#define vi vector<ll>
#define Endl endl
#define lb lower_bound
#define ub upper_bound
#define pii pair<ll, ll>

#define input                                                                  \\
  freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define time                                                                   \\
  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
using namespace std;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int power(int x, unsigned int y, unsigned int m) {
  if (y == 0)
    return 1;
  int p = power(x, y / 2, m) % m;
  p = (p * p) % m;
  return (y % 2 == 0) ? p : (x * p) % m;
}
ll modInverse(int a, int m) {
  { return power(a, m - 2, m); }
}
const int maxn = 500003;
// ll fact[1000005];
// ll modIN[1000005];
// void facto()
//{
//    ll lim =1000005;fact[0]=1;
//    for(ll i=1;i<lim;i++)
//    {
//        fact[i]=(fact[i-1]*i)%MOD;
//    }
//    modIN[n]=modInverse(fact[n],MOD);
//    for(ll i=n-1;i>=1;i--)
//    {
//        modIN[i] = (modIN[i+1]*(i+1))%MOD;
//    }
//}
// ll ncr(ll a,ll b)
//{
//    ll ret = 0;
//    ret = fact[a]*(modIN[b]);
//    ret%=MOD;
//    ret*=modIN[a-b];
//    ret%=MOD;
//    return ret;
//}
ll r, c, k;
ll a[3004][3004];
ll dp[3004][3004];
// multiset<ll>s;

ll rec(ll x, ll y) {
  if (x == r)
    return 0;
  if (dp[x][y] != -1)
    return dp[x][y];
  dp[x][y] = 0;
  ll arr[3];
  memset(arr, 0, sizeof arr);
  for (ll i = y; i < c; i++) {
    if (arr[0] <= a[x][i]) {
      ll xx = arr[0];
      arr[0] = a[x][i];
      arr[2] = arr[1];
      arr[1] = xx;

    } else if (arr[1] <= a[x][i]) {
      arr[2] = arr[1];
      arr[1] = a[x][i];
    } else
      arr[2] = max(arr[2], a[x][i]);

    dp[x][y] = max(dp[x][y], arr[0] + arr[1] + arr[2] + rec(x + 1, i));
  }

  return dp[x][y];
}
void solve() {
  cin >> r >> c >> k;
  ll aa, bb, cc;
  while (k--) {
    cin >> aa >> bb >> cc;
    --aa, --bb;
    a[aa][bb] = cc;
  }
  memset(dp, -1, sizeof dp);
  rec(0, 0);

  cout << dp[0][0] << endl;
}
signed main() {
  Need_for_speed(activated);

  //    satisfy
  //    {
  //        solve();
  //    }

  solve();

  return 0;
}
// Concentrate while implementing,,,!!!