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

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

int main() {
  long long R, C, K;
  cin >> R >> C >> K;
  long long r, c, v;
  long long i, j, k, l;
  vector<pair<pair<long long, long long>, long long>> vec;
  for (i = 1; i <= K; i++) {
    cin >> r >> c >> v;
    vec.push_back(make_pair(make_pair(r, c), v));
  }
  sort(vec.begin(), vec.end());
  long long dp[2][C + 1][4];
  for (i = 0; i <= 1; i++) {
    for (j = 0; j <= C; j++) {
      for (k = 0; k <= 3; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  long long za;
  for (i = 1; i <= R; i++) {
    for (j = 1; j <= C; j++) {
      za = lower_bound(vec.begin(), vec.end(),
                       make_pair(make_pair(i, j), (long long)0)) -
           vec.begin();
      if (za >= vec.size()) {
        for (k = 0; k <= 3; k++) {
          dp[i % 2][j][k] = dp[i % 2][j - 1][k];
          for (l = 0; l <= 3; l++) {
            dp[i % 2][j][k] = max(dp[i % 2][j][k], dp[!(i % 2)][j][l]);
          }
        }
      } else {
        r = vec[za].first.first;
        c = vec[za].first.second;
        v = vec[za].second;
        if (r == i && c == j) {
          dp[i % 2][j][0] = dp[i % 2][j - 1][0];
          for (l = 0; l <= 3; l++) {
            dp[i % 2][j][0] = max(dp[i % 2][j][0], dp[!(i % 2)][j][l]);
          }
          for (k = 1; k <= 3; k++) {
            dp[i % 2][j][k] = max(dp[i % 2][j][k], dp[i % 2][j - 1][k]);
            dp[i % 2][j][k] = max(dp[i % 2][j][k], dp[i % 2][j - 1][k - 1] + v);
            for (l = 0; l <= 3; l++) {
              dp[i % 2][j][k] = max(dp[i % 2][j][k], dp[!(i % 2)][j][l] + v);
            }
          }
        } else {
          for (k = 0; k <= 3; k++) {
            dp[i % 2][j][k] = dp[i % 2][j - 1][k];
            for (l = 0; l <= 3; l++) {
              dp[i % 2][j][k] = max(dp[i % 2][j][k], dp[!(i % 2)][j][l]);
            }
          }
        }
      }
    }
  }
  long long ans = 0;
  for (k = 0; k <= 3; k++) {
    ans = max(ans, dp[R % 2][C][k]);
  }
  cout << ans << endl;
  system("pause");
  return 0;
}

Error: 0
sh: 1: pause: not found


Fixed submission:

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

int main() {
  long long R, C, K;
  cin >> R >> C >> K;
  long long r, c, v;
  long long i, j, k, l;
  vector<pair<pair<long long, long long>, long long>> vec;
  for (i = 1; i <= K; i++) {
    cin >> r >> c >> v;
    vec.push_back(make_pair(make_pair(r, c), v));
  }
  sort(vec.begin(), vec.end());
  long long dp[R + 1][C + 1][4];
  for (i = 0; i <= R; i++) {
    for (j = 0; j <= C; j++) {
      for (k = 0; k <= 3; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  long long za;
  for (i = 1; i <= R; i++) {
    for (j = 1; j <= C; j++) {
      za = lower_bound(vec.begin(), vec.end(),
                       make_pair(make_pair(i, j), (long long)0)) -
           vec.begin();
      if (za >= vec.size()) {
        for (k = 0; k <= 3; k++) {
          dp[i][j][k] = dp[i][j - 1][k];
          for (l = 0; l <= 3; l++) {
            dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][l]);
          }
        }
      } else {
        r = vec[za].first.first;
        c = vec[za].first.second;
        v = vec[za].second;
        if (r == i && c == j) {
          dp[i][j][0] = dp[i][j - 1][0];
          for (l = 0; l <= 3; l++) {
            dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
          }
          for (k = 1; k <= 3; k++) {
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + v);
            for (l = 0; l <= 3; l++) {
              dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][l] + v);
            }
          }
        } else {
          for (k = 0; k <= 3; k++) {
            dp[i][j][k] = dp[i][j - 1][k];
            for (l = 0; l <= 3; l++) {
              dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][l]);
            }
          }
        }
      }
    }
  }
  long long ans = 0;
  for (k = 0; k <= 3; k++) {
    ans = max(ans, dp[R][C][k]);
  }
  cout << ans << endl;
  system("pause");
  return 0;
}