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

#include <bits/stdc++.h>

#define rep(i, N) for (int i = 0; i < N; i++)
#define rep2(i, N) for (int i = 1; i <= N; i++)
using namespace std;
long long INF = 1e18;
long long mod = 1e9 + 7;
int dx[2] = {1, 0};
int dy[2] = {0, 1};
// status unsolved
long long block[3010][3010] = {};
long long result[3010][3010][4] = {};
int main() {
  long long r, c, k;
  cin >> r >> c >> k;

  rep(i, k) {
    int r1, c1;
    long long v1;
    cin >> r1 >> c1 >> v1;
    block[r1][c1] = v1;
  }

  queue<int> q;
  // priority_queue< long long,vector<long long>,greater<long long>> Q[10][10];
  q.push(1);
  q.push(1);
  // rep(i,3)result[1][1][i]=block[1][1];
  result[1][1][0] = 0;
  if (block[1][1] != 0) {
    result[1][1][1] = block[1][1];
  }

  // priority_queue< long long,vector<long long>,greater<long long>>start;

  rep2(l, r) rep2(t, c) {
    int nowy = l;
    int nowx = t;
    rep(i, 2) {
      int ny = nowy + dy[i];
      int nx = nowx + dx[i];

      if (nx < 1 || nx > c || ny < 1 || ny > r) {
        continue;
      } else if (i == 0) {
        rep(k, 4) {
          // アイテムを拾わない
          result[ny][nx][k] = max(result[nowy][nowx][k], result[ny][nx][k]);
        }
        if (block[ny][nx] != 0) {
          rep(k, 3) {
            // アイテムを拾う
            result[ny][nx][k + 1] = max(result[nowy][nowx][k] + block[ny][nx],
                                        result[ny][nx][k + 1]);
          }
        }

      } else {
        rep(k, 4) {
          // アイテムを拾わない
          result[ny][nx][0] = max(result[nowy][nowx][k], result[ny][nx][0]);
        }
        if (block[ny][nx] != 0) {
          rep(k, 4) {
            // アイテムを拾う
            result[ny][nx][1] =
                max(result[nowy][nowx][k] + block[ny][nx], result[ny][nx][1]);
          }
        }
      }
    }
  }

  long long ans = -1;
  rep(i, 4) ans = max(ans, result[r][c][i]);
  cout << ans << endl;

  return 0;
}
/*


*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, N) for (int i = 0; i < N; i++)
#define rep2(i, N) for (int i = 1; i <= N; i++)
using namespace std;
long long INF = 1e18;
long long mod = 1e9 + 7;
int dx[2] = {1, 0};
int dy[2] = {0, 1};
// status unsolved
long long block[3010][3010] = {};
long long result[3010][3010][4] = {};
int main() {
  long long r, c, k;
  cin >> r >> c >> k;

  rep(i, k) {
    int r1, c1;
    long long v1;
    cin >> r1 >> c1 >> v1;
    block[r1][c1] = v1;
  }

  queue<int> q;
  // priority_queue< long long,vector<long long>,greater<long long>> Q[10][10];
  q.push(1);
  q.push(1);
  // rep(i,3)result[1][1][i]=block[1][1];
  result[1][1][0] = 0;
  if (block[1][1] != 0) {
    result[1][1][1] = block[1][1];
  }

  // priority_queue< long long,vector<long long>,greater<long long>>start;

  while (!q.empty()) {
    int nowy = q.front();
    q.pop();
    int nowx = q.front();
    q.pop();
    rep(i, 2) {
      int ny = nowy + dy[i];
      int nx = nowx + dx[i];

      if (nx < 1 || nx > c || ny < 1 || ny > r) {
        continue;
      } else if (i == 0) {
        rep(k, 4) {
          // アイテムを拾わない
          result[ny][nx][k] = max(result[nowy][nowx][k], result[ny][nx][k]);
        }
        if (block[ny][nx] != 0) {
          rep(k, 3) {
            // アイテムを拾う
            result[ny][nx][k + 1] = max(result[nowy][nowx][k] + block[ny][nx],
                                        result[ny][nx][k + 1]);
          }
        }

        q.push(ny);
        q.push(nx);
      } else {
        rep(k, 4) {
          // アイテムを拾わない
          result[ny][nx][0] = max(result[nowy][nowx][k], result[ny][nx][0]);
        }
        if (block[ny][nx] != 0) {
          rep(k, 4) {
            // アイテムを拾う
            result[ny][nx][1] =
                max(result[nowy][nowx][k] + block[ny][nx], result[ny][nx][1]);
          }
        }
        q.push(ny);
        q.push(nx);
      }
    }
  }
  long long ans = -1;
  rep(i, 4) ans = max(ans, result[r][c][i]);
  cout << ans << endl;

  return 0;
}
/*


*/