Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

#define mx 4

typedef long long int ll;

ll max(ll a, ll b, ll c) { return max(max(a, b), c); }

int main() {
  int r, c, k;
  scanf("%d %d %d", &r, &c, &k);
  int tab[r][c];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      tab[i][j] = 0;
    }
  }

  for (int i = 0; i < k; i++) {
    int x, y, v;
    scanf("%d %d %d", &x, &y, &v);
    x--;
    y--;
    tab[x][y] = v;
  }

  ll old[c][mx];
  ll dp[c][mx];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int l = 0; l < mx; l++) {
        dp[j][l] = 0;
        if (i == 0 && j == 0) {
          dp[j][l] = l > 0 ? tab[i][j] : 0;
        }
        if (i == 0 && j != 0) {
          if (l == 0) {
            dp[j][l] = 0;
          } else {
            dp[j][l] = max(tab[i][j] + dp[j - 1][l - 1], dp[j - 1][l]);
          }
        }
        if (i != 0 && j == 0) {
          if (l == 0)
            dp[j][0] = old[j][mx - 1];
          else
            dp[j][l] = tab[i][j] + old[j][mx - 1];
        }
        if (i != 0 && j != 0) {
          if (l == 0)
            dp[j][0] = old[j][mx - 1];
          else {
            dp[j][l] = max(dp[j - 1][l - 1] + tab[i][j],
                           old[j][mx - 1] + tab[i][j], dp[j - 1][l]);
          }
        }
      }
    }
    for (int j = 0; j < c; j++) {
      for (int l = 0; l < mx; l++) {
        old[j][l] = dp[j][l];
      }
    }
  }

  //~ for(int i=0;i<r;i++){
  //~ for(int j=0;j<c;j++){
  //~ for(int l=0;l<mx;l++){
  //~ printf("%d ", dp[i][j][l]);
  //~ }
  //~ printf("\\t");
  //~ }
  //~ printf("\\n");
  //~ }
  printf("%lld", dp[c - 1][mx - 1]);
}


Error: WA

Fixed submission:

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

#define mx 4

typedef long long int ll;

ll max(ll a, ll b, ll c) { return max(max(a, b), c); }

int main() {
  int r, c, k;
  scanf("%d %d %d", &r, &c, &k);
  int tab[r][c];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      tab[i][j] = 0;
    }
  }

  for (int i = 0; i < k; i++) {
    int x, y, v;
    scanf("%d %d %d", &x, &y, &v);
    x--;
    y--;
    tab[x][y] = v;
  }

  ll dp[r][c][mx];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int l = 0; l < mx; l++) {
        dp[i][j][l] = 0;
        if (i == 0 && j == 0) {
          dp[i][j][l] = l > 0 ? tab[i][j] : 0;
        }
        if (i == 0 && j != 0) {
          if (l == 0) {
            dp[i][j][l] = 0;
          } else {
            dp[i][j][l] = max(tab[i][j] + dp[i][j - 1][l - 1], dp[i][j - 1][l]);
          }
        }
        if (i != 0 && j == 0) {
          if (l == 0)
            dp[i][j][0] = dp[i - 1][j][mx - 1];
          else
            dp[i][j][l] = tab[i][j] + dp[i - 1][j][mx - 1];
        }
        if (i != 0 && j != 0) {
          if (l == 0)
            dp[i][j][0] = dp[i - 1][j][mx - 1];
          else {
            dp[i][j][l] =
                max(dp[i][j - 1][l - 1] + tab[i][j],
                    dp[i - 1][j][mx - 1] + tab[i][j], dp[i][j - 1][l]);
          }
        }
      }
    }
  }

  //~ for(int i=0;i<r;i++){
  //~ for(int j=0;j<c;j++){
  //~ for(int l=0;l<mx;l++){
  //~ printf("%d ", dp[i][j][l]);
  //~ }
  //~ printf("\\t");
  //~ }
  //~ printf("\\n");
  //~ }
  printf("%lld", dp[r - 1][c - 1][mx - 1]);
}