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

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define fst first
#define snd second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define EPS (1e-9)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

bool LT(double a, double b) { return !equals(a, b) && a < b; }
bool LTE(double a, double b) { return equals(a, b) || a < b; }

const string YES = "";
const string NO = "";

typedef long long ll;
typedef pair<int, int> ii;

struct Data {
  ll x, y, v;
  bool operator<(const Data &data) const {
    if (x != data.x)
      return x < data.x;
    return v < data.v;
  }
};

#define MAX 3010
int H, W, K;
ll vs[MAX][MAX];
ll dp[MAX][MAX][4];

void solve() {
  ll maxi = 0;
  memset(dp, -1, sizeof dp);
  dp[0][0][3] = 0;
  if (vs[0][0] != -1) {
    dp[0][0][2] = vs[0][0];
    maxi = max(maxi, dp[0][0][2]);
  }

  rep(i, H) {
    rep(j, W) {
      rep(rem, 4) {
        if (dp[i][j][rem] == -1)
          continue;
        // do nothing
        if (i + 1 < H) {
          dp[i + 1][j][3] = max(dp[i + 1][j][3], dp[i][j][rem]);
          // maxi = max(maxi,dp[i+1][j][3]);
        }
        if (j + 1 < W) {
          dp[i][j + 1][rem] = max(dp[i][j + 1][rem], dp[i][j][rem]);
          // maxi = max(maxi,dp[i][j+1][rem]);
        }
        // do
        if (i + 1 < H) {
          if (vs[i + 1][j] != -1) {
            ll v = vs[i + 1][j];
            dp[i + 1][j][2] = max(dp[i + 1][j][2], dp[i][j][rem] + v);
            maxi = max(maxi, dp[i + 1][j][2]);
          }
        }
        if (rem - 1 >= 0 && j + 1 < W) {
          if (vs[i][j + 1] != -1) {
            ll v = vs[i][j + 1];
            dp[i][j + 1][rem - 1] =
                max(dp[i][j + 1][rem - 1], dp[i][j][rem] + v);

            maxi = max(maxi, dp[i][j + 1][rem - 1]);
          }
        }
      }
    }
  }
  cout << maxi << endl;
}

int main() {
  cin >> H >> W >> K;
  memset(vs, -1, sizeof vs);
  ll x, y, v;
  rep(i, K) {
    cin >> y >> x >> v;
    --y, --x;
    vs[y][x] = v;
  }
  // rep(i,MAX) sort(ALL(deq[i]));
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define fst first
#define snd second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define EPS (1e-9)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

bool LT(double a, double b) { return !equals(a, b) && a < b; }
bool LTE(double a, double b) { return equals(a, b) || a < b; }

const string YES = "";
const string NO = "";

typedef long long ll;
typedef pair<int, int> ii;

struct Data {
  ll x, y, v;
  bool operator<(const Data &data) const {
    if (x != data.x)
      return x < data.x;
    return v < data.v;
  }
};

#define MAX 3010
#define MAX2 200010
int H, W, K;
ll x[MAX2], y[MAX2], v[MAX2];
ll dp[MAX][MAX][4];

// deque<Data> deq[MAX];
map<ii, ll> S;

void solve() {
  ll maxi = 0;
  memset(dp, -1, sizeof dp);
  dp[0][0][3] = 0;
  if (S.count(ii(0, 0))) {
    dp[0][0][2] = S[ii(0, 0)];
    maxi = max(maxi, dp[0][0][2]);
  }

  rep(i, H) {
    rep(j, W) {
      rep(rem, 4) {
        if (dp[i][j][rem] == -1)
          continue;
        // do nothing
        if (i + 1 < H) {
          dp[i + 1][j][3] = max(dp[i + 1][j][3], dp[i][j][rem]);
          // maxi = max(maxi,dp[i+1][j][3]);
        }
        if (j + 1 < W) {
          dp[i][j + 1][rem] = max(dp[i][j + 1][rem], dp[i][j][rem]);
          // maxi = max(maxi,dp[i][j+1][rem]);
        }
        // do
        if (i + 1 < H) {
          if (S.count(ii(j, i + 1))) {
            ll v = S[ii(j, i + 1)];
            dp[i + 1][j][2] = max(dp[i + 1][j][2], dp[i][j][rem] + v);
            maxi = max(maxi, dp[i + 1][j][2]);
          }
        }
        if (rem - 1 >= 0 && j + 1 < W) {
          if (S.count(ii(j + 1, i))) {
            ll v = S[ii(j + 1, i)];
            dp[i][j + 1][rem - 1] =
                max(dp[i][j + 1][rem - 1], dp[i][j][rem] + v);

            maxi = max(maxi, dp[i][j + 1][rem - 1]);
          }
        }
      }
    }
  }
  cout << maxi << endl;
}

int main() {
  cin >> H >> W >> K;
  rep(i, K) {
    cin >> y[i] >> x[i] >> v[i];
    --y[i], --x[i];
    // deq[y[i]].pb((Data){x[i],y[i],v[i]});
    S[ii(x[i], y[i])] = v[i];
  }
  // rep(i,MAX) sort(ALL(deq[i]));
  solve();
  return 0;
}