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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<vector<vector<long long>>> dp;
vector<vector<long long>> items;
int R, C, K;

void step(int i, int j, int k) {
  if (j < C) {
    if ((items.at(i).at(j + 1) > 0) && (k <= 2)) {
      dp.at(i).at(j + 1).at(k + 1) =
          max(dp.at(i).at(j + 1).at(k + 1),
              dp.at(i).at(j).at(k) + items.at(i).at(j + 1));
    }
    dp.at(i).at(j + 1).at(k) =
        max(dp.at(i).at(j + 1).at(k), dp.at(i).at(j).at(k));
  }
  if (i < R) {
    if (items.at(i + 1).at(j) > 0) {
      dp.at(i + 1).at(j).at(1) =
          max(dp.at(i + 1).at(j).at(1),
              dp.at(i).at(j).at(k) + items.at(i + 1).at(j));
    }
    dp.at(i + 1).at(j).at(0) =
        max(dp.at(i + 1).at(j).at(0), dp.at(i).at(j).at(k));
  }
}

int main() {
  cin >> R >> C >> K;
  dp = vector<vector<vector<long long>>>(
      R + 1, vector<vector<long long>>(C + 1, vector<long long>(4, 0)));
  items = vector<vector<long long>>(R + 1, vector<long long>(C + 1, 0));
  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    items.at(r).at(c) = v;
  }
  for (int i = 0; i <= R; i++) {
    for (int j = 0; j <= C; j++) {
      for (int k = 0; k <= 3; k++) {
        step(i, j, k);
      }
    }
  }
  long long ans = 0;
  for (int k = 0; k <= 3; k++) {
    ans = max(ans, dp.at(R).at(C).at(k));
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  vector<vector<int>> V(R + 1, vector<int>(0)), J(R + 1, vector<int>(0));
  vector<vector<pair<int, int>>> VJ(R + 1);
  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    VJ.at(r).push_back(make_pair(c, v));
  }
  for (int r = 1; r < R + 1; r++) {
    sort(VJ.at(r).begin(), VJ.at(r).end());
  }
  for (int r = 1; r < R + 1; r++) {
    for (int j = 0; j < VJ.at(r).size(); j++) {
      V.at(r).push_back(VJ.at(r).at(j).second);
      J.at(r).push_back(VJ.at(r).at(j).first);
    }
  }
  vector<vector<int>> nex(R + 1, vector<int>(C + 1, 0));
  for (int i = 1; i < R + 1; i++) {
    int k = 1;
    for (int j = 0; j < J.at(i).size(); j++) {
      for (int l = k; l <= J.at(i).at(j); l++) {
        nex.at(i).at(l) = j;
      }
      k = J.at(i).at(j) + 1;
    }
    for (int k = J.at(i).at(J.at(i).size() - 1) + 1; k < C + 1; k++) {
      nex.at(i).at(k) = -1;
    }
  }
  vector<vector<vector<long long>>> kukan(R + 1);
  for (int r = 1; r < R + 1; r++) {
    int SizeOfR = V.at(r).size();
    vector<vector<long long>> temp(SizeOfR, vector<long long>(SizeOfR));
    for (int i = 0; i < SizeOfR; i++) {
      vector<long long> vs(0);
      temp.at(i).at(i) = V.at(r).at(i);
      vs.push_back(V.at(r).at(i));
      for (int j = i + 1; j < SizeOfR; j++) {
        vs.push_back(V.at(r).at(j));
        if (j - i <= 2) {
          temp.at(i).at(j) = temp.at(i).at(j - 1) + V.at(r).at(j);
        } else if (j - i > 2) {
          sort(vs.begin(), vs.end());
          long long t = 0;
          for (int x = 1; x <= 3; x++) {
            t += vs.at(vs.size() - x);
          }
          temp.at(i).at(j) = t;
        }
      }
    }
    kukan.at(r) = temp;
  }
  /*
  for(int r=1;r<R+1;r++){
      int SizeOfR=V.at(r).size();
      for(int i=0;i<SizeOfR;i++){
          for(int j=0;j<SizeOfR;j++){
              cout << kukan.at(r).at(i).at(j) << " ";
          }
          cout << endl;
      }
      cout << endl;
  }
  */

  // cout <<
  // "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  // << endl;

  vector<vector<long long>> dp(R + 1, vector<long long>(C + 1, -1));
  dp.at(1).at(1) = 0;
  for (int r = 1; r < R; r++) {
    for (int j = 1; j < C + 1; j++) {
      if (dp.at(r).at(j) >= 0) {
        dp.at(r + 1).at(j) = max(dp.at(r + 1).at(j), dp.at(r).at(j));
        if (nex.at(r).at(j) == -1)
          continue;
        for (int k = nex.at(r).at(j); k < J.at(r).size(); k++) {
          int next_j = J.at(r).at(k);
          dp.at(r + 1).at(next_j) =
              max(dp.at(r + 1).at(next_j),
                  dp.at(r).at(j) + kukan.at(r).at(nex.at(r).at(j)).at(k));
        }
      }
    }
  }
  /*
  for(int i=1;i<R+1;i++){
      for(int j=0;j<J.at(i).size();j++){
          cout << J.at(i).at(j) << " ";
      }
      cout << endl;
  }
  cout << endl;
  for(int i=1;i<R+1;i++){
      for(int j=1;j<C+1;j++){
          cout << nex.at(i).at(j) << " ";
      }
      cout << endl;
  }
  cout << endl;
  for(int i=1;i<R+1;i++){
      for(int j=1;j<C+1;j++){
          cout << dp.at(i).at(j) << " ";
      }
      cout << endl;
  }
  cout << "annda-hougeru" << endl;
  */
  long long ans = 0;
  for (int j = 1; j < C + 1; j++) {
    if (dp.at(R).at(j) >= 0) {
      int next_j = nex.at(R).at(j);
      if (next_j != -1) {
        ans = max(ans, dp.at(R).at(j) + kukan.at(R).at(next_j).at(
                                            kukan.at(R).at(next_j).size() - 1));
      } else if (next_j == -1) {
        ans = max(ans, dp.at(R).at(j));
      }
    }
  }
  cout << ans << endl;
  /*
  for(int i=1;i<R+1;i++){
      for(int j=1;j<C+1;j++){

      }
  }
  */
}