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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
#define per(i, a) ROF(i, a, 0)
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
using ch = char;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
struct edge {
  ll to, cost;
};
struct edge2 {
  ll from, to, cost;
};

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll R, C, K;
  cin >> R >> C >> K;
  vector<vvl> dp(R, vvl(C, vl(4, 0)));
  rep(i, R) {
    rep(j, C) {
      rep(k, 4) { dp[i][j][k] = 0; }
    }
  }
  vvl mp(R, vl(C, 0));
  rep(i, K) {
    ll r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    mp[r][c] = v;
  }
  rep(i, R) {
    rep(j, C) {
      rep(k, 4) {
        if (i == 0 && j == 0) {
          dp[i][j][0] = 0;
          if (k > 0 && mp[i][j] != 0) {
            dp[i][j][k] = mp[i][j];
          }
        } else if (i == 0) {
          dp[i][j][k] = dp[i][j - 1][k];
          if (k > 0)
            chmax(dp[i][j][k], dp[i][j - 1][k - 1] + mp[i][j]);
        } else if (j == 0) {
          dp[i][j][k] = dp[i - 1][j][3];
          if (k > 0 && mp[i][j] > 0) {
            dp[i][j][k] = dp[i - 1][j][3] + mp[i][j];
          }
        } else {
          dp[i][j][k] = dp[i][j - 1][k];
          if (k > 0)
            chmax(dp[i][j][k], dp[i][j - 1][k - 1] + mp[i][j]);
          if (k == 0)
            chmax(dp[i][j][3], dp[i - 1][j][k]);
          if (k > 0)
            chmax(dp[i][j][k], dp[i - 1][j][3] + mp[i][j]);
        }
      }
    }
  }
  ll ans = 0;
  rep(i, R) {
    rep(j, C) {
      rep(k, 4) { chmax(ans, dp[i][j][k]); }
    }
  }
  cout << ans << endl;
}
/*
overflow checked?
corner case checked?
boundary checked?
not TLE in worst case checked?
*/

Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
#define per(i, a) ROF(i, a, 0)
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
using ch = char;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
struct edge {
  ll to, cost;
};
struct edge2 {
  ll from, to, cost;
};

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll R, C, K;
  cin >> R >> C >> K;
  vector<vvl> dp(R, vvl(C, vl(4, 0)));
  rep(i, R) {
    rep(j, C) {
      rep(k, 4) { dp[i][j][k] = 0; }
    }
  }
  map<P, ll> mp;
  rep(i, K) {
    ll r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    mp[{r, c}] = v;
  }
  rep(i, R) {
    rep(j, C) {
      rep(k, 4) {
        if (i == 0 && j == 0) {
          dp[i][j][0] = 0;
          if (k > 0 && mp[{i, j}] != 0) {
            dp[i][j][k] = mp[{i, j}];
          }
        } else if (i == 0) {
          dp[i][j][k] = dp[i][j - 1][k];
          if (k > 0)
            chmax(dp[i][j][k], dp[i][j - 1][k - 1] + mp[{i, j}]);
        } else if (j == 0) {
          dp[i][j][k] = dp[i - 1][j][3];
          if (k > 0 && mp[{i, j}] > 0) {
            dp[i][j][k] = dp[i - 1][j][3] + mp[{i, j}];
          }
        } else {
          dp[i][j][k] = dp[i][j - 1][k];
          if (k > 0)
            chmax(dp[i][j][k], dp[i][j - 1][k - 1] + mp[{i, j}]);
          if (k == 0)
            chmax(dp[i][j][3], dp[i - 1][j][k]);
          if (k > 0)
            chmax(dp[i][j][k], dp[i - 1][j][3] + mp[{i, j}]);
        }
      }
    }
  }
  ll ans = 0;
  rep(i, R) {
    rep(j, C) {
      rep(k, 4) { chmax(ans, dp[i][j][k]); }
    }
  }
  cout << ans << endl;
}
/*
overflow checked?
corner case checked?
boundary checked?
not TLE in worst case checked?
*/