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

#include <bits/stdc++.h>

using namespace std;

#define ll long long

ll ans[3000][3000][4];
ll a[3000][3000];

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++)
      a[i][j] = 0;
  }
  while (k--) {
    ll a1, a2, v;
    cin >> a1 >> a2 >> v;
    a[a1 - 1][a2 - 1] = v;
  }
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (i == 0 && j == 0) {
        ans[i][j][0] = 0;
        ans[i][j][1] = a[i][j];
        ans[i][j][2] = 0;
        ans[i][j][3] = 0;
        continue;
      }
      if (i == 0) {
        ans[i][j][0] = ans[i][j - 1][0];
        ans[i][j][1] = max(ans[i][j - 1][0] + a[i][j], ans[i][j - 1][1]);
        ans[i][j][2] = max(ans[i][j - 1][1] + a[i][j], ans[i][j - 1][2]);
        ans[i][j][3] = max(ans[i][j - 1][2] + a[i][j], ans[i][j - 1][3]);
        continue;
      }
      if (j == 0) {
        ll val = max(max(ans[i - 1][j][0], ans[i - 1][j][1]),
                     max(ans[i - 1][j][2], ans[i - 1][j][3]));
        ans[i][j][0] = val;
        ans[i][j][1] = val + a[i][j];
        ans[i][j][2] = val + a[i][j];
        ans[i][j][3] = val + a[i][j];
        continue;
      }
      ll val = max(max(ans[i - 1][j][0], ans[i - 1][j][1]),
                   max(ans[i - 1][j][2], ans[i - 1][j][3]));
      ans[i][j][0] = ans[i][j - 1][0];
      ans[i][j][1] = max(ans[i][j - 1][0] + a[i][j], ans[i][j - 1][1]);
      ans[i][j][2] = max(ans[i][j - 1][1] + a[i][j], ans[i][j - 1][2]);
      ans[i][j][3] = max(ans[i][j - 1][2] + a[i][j], ans[i][j - 1][3]);
      ans[i][j][0] = max(ans[i][j][0], val);
      ans[i][j][1] = max(ans[i][j][1], val + a[i][j]);
      ans[i][j][2] = max(ans[i][j][2], val + a[i][j]);
      ans[i][j][3] = max(ans[i][j][3], val + a[i][j]);
    }
  }
  int i = r, j = c;
  ll answer = max(max(ans[i - 1][j - 1][0], ans[i - 1][j - 1][1]),
                  max(ans[i - 1][j - 1][2], ans[i - 1][j - 1][3]));
  cout << answer;
  // for(int k=0;k<4;k++){
  // 	for(int i=0;i<r;i++){
  // 		for(int j=0;j<c;j++)
  // 			cout<<ans[i][j][k]<<" ";
  // 		cout<<endl;
  // 	}
  // 	cout<<endl;
  // }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  ll a[r][c];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++)
      a[i][j] = 0;
  }
  while (k--) {
    ll a1, a2, v;
    cin >> a1 >> a2 >> v;
    a[a1 - 1][a2 - 1] = v;
  }
  ll ans[r][c][4];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (i == 0 && j == 0) {
        ans[i][j][0] = 0;
        ans[i][j][1] = a[i][j];
        ans[i][j][2] = 0;
        ans[i][j][3] = 0;
        continue;
      }
      if (i == 0) {
        ans[i][j][0] = ans[i][j - 1][0];
        ans[i][j][1] = max(ans[i][j - 1][0] + a[i][j], ans[i][j - 1][1]);
        ans[i][j][2] = max(ans[i][j - 1][1] + a[i][j], ans[i][j - 1][2]);
        ans[i][j][3] = max(ans[i][j - 1][2] + a[i][j], ans[i][j - 1][3]);
        continue;
      }
      if (j == 0) {
        ll val = max(max(ans[i - 1][j][0], ans[i - 1][j][1]),
                     max(ans[i - 1][j][2], ans[i - 1][j][3]));
        ans[i][j][0] = val;
        ans[i][j][1] = val + a[i][j];
        ans[i][j][2] = val + a[i][j];
        ans[i][j][3] = val + a[i][j];
        continue;
      }
      ll val = max(max(ans[i - 1][j][0], ans[i - 1][j][1]),
                   max(ans[i - 1][j][2], ans[i - 1][j][3]));
      ans[i][j][0] = ans[i][j - 1][0];
      ans[i][j][1] = max(ans[i][j - 1][0] + a[i][j], ans[i][j - 1][1]);
      ans[i][j][2] = max(ans[i][j - 1][1] + a[i][j], ans[i][j - 1][2]);
      ans[i][j][3] = max(ans[i][j - 1][2] + a[i][j], ans[i][j - 1][3]);
      ans[i][j][0] = max(ans[i][j][0], val);
      ans[i][j][1] = max(ans[i][j][1], val + a[i][j]);
      ans[i][j][2] = max(ans[i][j][2], val + a[i][j]);
      ans[i][j][3] = max(ans[i][j][3], val + a[i][j]);
    }
  }
  int i = r, j = c;
  ll answer = max(max(ans[i - 1][j - 1][0], ans[i - 1][j - 1][1]),
                  max(ans[i - 1][j - 1][2], ans[i - 1][j - 1][3]));
  cout << answer;
  return 0;
}