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

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t r, c, k;
  cin >> r >> c >> k;
  vector<vector<int64_t>> C(k, vector<int64_t>(3, 0));
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> C[i][j];
    }
  }
  vector<vector<int64_t>> I(r, vector<int64_t>(c, 0));
  for (int i = 0; i < k; i++) {
    I[C[i][0] - 1][C[i][1] - 1] = C[i][2];
  }

  vector<vector<vector<int64_t>>> D(
      r, vector<vector<int64_t>>(c, vector<int64_t>(4, 0)));
  int64_t aa = 0;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int l = 0; l < 4; l++) {
        aa = 0;
        if (l == 0) {
          if (i - 1 >= 0) {
            aa = max(aa, max(D[i - 1][j][0],
                             max(D[i - 1][j][1],
                                 max(D[i - 1][j][2], D[i - 1][j][3]))));
          }
          if (j - 1 >= 0) {
            aa = max(aa, D[i][j - 1][0]);
          }
        } else {
          if (i - 1 >= 0) {
            aa = max(aa, max(D[i - 1][j][0],
                             max(D[i - 1][j][1],
                                 max(D[i - 1][j][2], D[i - 1][j][3]))) +
                             I[i][j]);
          } else {
            aa = max(aa, I[i][j]);
          }
          if (j - 1 >= 0) {
            aa = max(aa, max(D[i][j - 1][l - 1] + I[i][j], D[i][j - 1][l]));
          } else {
            aa = max(aa, 0 + I[i][j]);
          }
        }
        D[i][j][l] = max(D[i][j][l], aa);
      }
    }
  }
  int64_t ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(D[r - 1][c - 1][i], ans);
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  vector<vector<int>> C(k, vector<int>(3, 0));
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> C[i][j];
    }
  }
  vector<vector<int>> I(r, vector<int>(c, 0));
  for (int i = 0; i < k; i++) {
    I[C[i][0] - 1][C[i][1] - 1] = C[i][2];
  }

  vector<vector<vector<int>>> D(r, vector<vector<int>>(c, vector<int>(4, 0)));
  int aa = 0;
  for (int j = 0; j < c; j++) {
    for (int i = 0; i < r; i++) {
      for (int l = 0; l < 4; l++) {
        aa = 0;
        if (l == 0) {
          if (i - 1 >= 0) {
            aa = max(aa, max(D[i - 1][j][0],
                             max(D[i - 1][j][1],
                                 max(D[i - 1][j][2], D[i - 1][j][3]))));
          }
          if (j - 1 >= 0) {
            aa = max(aa, D[i][j - 1][0]);
          }
        } else {
          if (i - 1 >= 0) {
            aa = max(aa, max(D[i - 1][j][0],
                             max(D[i - 1][j][1],
                                 max(D[i - 1][j][2], D[i - 1][j][3]))) +
                             I[i][j]);
          } else {
            aa = max(aa, I[i][j]);
          }
          if (j - 1 >= 0) {
            aa = max(aa, max(D[i][j - 1][l - 1] + I[i][j], D[i][j - 1][l]));
          } else {
            aa = max(aa, 0 + I[i][j]);
          }
        }
        D[i][j][l] = max(D[i][j][l], aa);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(D[r - 1][c - 1][i], ans);
  }

  cout << ans << endl;
}