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

#include <bits/stdc++.h>
#include <queue>
#include <set>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = s; i < (int)(n); i++)
#define ll long long
#define ld long double
#define repl(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repsl(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define outdeb(fir, sec, thr) cout << fir << ":" << sec << ":" << thr << endl

#define lpair pair<ll, ll>
#define llvecvec vector<vector<vector<ll>>>
#define INF 3000000000

ll item[3001][3001];
ll mydata[3001][3001][4];

ll getMax(ll r, ll c) {
  ll maxv = 0;
  rep(i, 4) { maxv = max(maxv, mydata[r][c][i]); }

  return maxv;
}

int main() {
  ll r, c, k;
  cin >> r >> c >> k;

  repl(i, r) {
    repl(j, c) {
      item[i][j] = 0;
      repl(k, 4) { mydata[i][j][k] = -INF; }
    }
  }

  repl(i, k) {
    ll a, b, c;
    cin >> a >> b >> c;
    item[a - 1][b - 1] = c;
  }

  repl(i, r) repl(j, c) repl(k, 4) {
    if (i == 0 && j == 0) {
      if (k == 1 && item[i][j] > 0) {
        mydata[i][j][k] = item[i][j];
      } else if (k == 0) {
        mydata[i][j][k] = 0;
      } else {
        mydata[i][j][k] = -INF;
      }
    } else if (j == 0) {
      if (k == 1 && item[i][j] > 0) {
        mydata[i][j][k] = getMax(i - 1, j) + item[i][j];
      } else if (k == 0) {
        mydata[i][j][k] = getMax(i - 1, j);
      } else {
        mydata[i][j][k] = -INF;
      }
    } else if (i == 0) {
      if (k == 0) {
        mydata[i][j][k] = 0;
        continue;
      }

      ll tmp = -INF;
      if (mydata[i][j - 1][k - 1] >= 0 && item[i][j] > 0) {
        tmp = mydata[i][j - 1][k - 1] + item[i][j];
      }

      mydata[i][j][k] = max(mydata[i][j - 1][k], tmp);
    } else {
      ll tmp = -INF;
      if (k > 0 && mydata[i][j - 1][k - 1] >= 0 && item[i][j] > 0) {
        tmp = mydata[i][j - 1][k - 1] + item[i][j];
      }
      tmp = max(tmp, mydata[i][j - 1][k]);
      if (k == 1 && item[i][j] > 0) {
        tmp = max(tmp, getMax(i - 1, j) + item[i][j]);
      } else if (k == 0) {
        tmp = max(tmp, getMax(i - 1, j));
      }
      mydata[i][j][k] = tmp;
    }
  }

  cout << fixed << getMax(r - 1, c - 1) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <queue>
#include <set>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = s; i < (int)(n); i++)
#define ll long long
#define ld long double
#define repl(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repsl(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define outdeb(fir, sec, thr) cout << fir << ":" << sec << ":" << thr << endl

#define lpair pair<ll, ll>
#define llvecvec vector<vector<vector<ll>>>
#define INF 3000000000
ll getMax(llvecvec data, ll r, ll c) {
  ll maxv = 0;
  rep(i, 4) { maxv = max(maxv, data[r][c][i]); }

  return maxv;
}

int main() {
  ll r, c, k;
  cin >> r >> c >> k;

  vector<vector<ll>> item;
  llvecvec data;

  repl(i, r) {
    data.push_back(vector<vector<ll>>());
    item.push_back(vector<ll>());
    repl(j, c) {
      data[i].push_back(vector<ll>());
      item[i].push_back(0);
      repl(k, 4) { data[i][j].push_back(-INF); }
    }
  }

  repl(i, k) {
    ll a, b, c;
    cin >> a >> b >> c;
    item[a - 1][b - 1] = c;
  }

  repl(i, r) repl(j, c) repl(k, 4) {
    if (i == 0 && j == 0) {
      if (k == 1 && item[i][j] > 0) {
        data[i][j][k] = item[i][j];
      } else if (k == 0) {
        data[i][j][k] = 0;
      } else {
        data[i][j][k] = -INF;
      }
    } else if (j == 0) {
      if (k == 1 && item[i][j] > 0) {
        data[i][j][k] = getMax(data, i - 1, j) + item[i][j];
      } else if (k == 0) {
        data[i][j][k] = getMax(data, i - 1, j);
      } else {
        data[i][j][k] = -INF;
      }
    } else if (i == 0) {
      if (k == 0) {
        data[i][j][k] = 0;
        continue;
      }

      ll tmp = -INF;
      if (data[i][j - 1][k - 1] >= 0 && item[i][j] > 0) {
        tmp = data[i][j - 1][k - 1] + item[i][j];
      }

      data[i][j][k] = max(data[i][j - 1][k], tmp);
    } else {
      ll tmp = -INF;
      if (k > 0 && data[i][j - 1][k - 1] >= 0 && item[i][j] > 0) {
        tmp = data[i][j - 1][k - 1] + item[i][j];
      }
      tmp = max(tmp, data[i][j - 1][k]);
      if (k == 1 && item[i][j] > 0) {
        tmp = max(tmp, getMax(data, i - 1, j) + item[i][j]);
      } else if (k == 0) {
        tmp = max(tmp, getMax(data, i - 1, j));
      }
      data[i][j][k] = tmp;
    }
  }

  cout << fixed << getMax(data, r - 1, c - 1) << endl;

  return 0;
}