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

// g++ -std=gnu++14 a.cpp

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
ll MOD = 1e9 + 7;
int INF = 1 << 30;
ll INFL = 1LL << 60;

ll MODP = 998244353;

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> I(H + 1, 0), J(W + 1, 0);
  vector<int> h(M), w(M);
  rep(i, M) {
    cin >> h[i] >> w[i];
    I[h[i]]++;
    J[w[i]]++;
  }

  int maxinum = 0, maxjnum = 0;
  for (int i = 1; i <= H; i++) {
    if (I[i] > maxinum) {
      maxinum = I[i];
    }
  }
  for (int j = 1; j <= W; j++) {
    if (J[j] > maxjnum) {
      maxjnum = J[j];
    }
  }

  map<int, int> mi, mj;

  for (int i = 1; i <= H; i++) {
    if (I[i] == maxinum) {
      mi[i] = maxinum;
    }
  }
  for (int j = 1; j <= W; j++) {
    if (maxjnum == J[j]) {
      mj[j] = maxjnum;
    }
  }

  ll all = (mi.size()) * (mj.size());

  rep(i, M) {
    if (mi.count(h[i]) && mj.count(w[i])) {
      all--;
    }
  }

  if (all) {
    cout << maxinum + maxjnum << endl;
  } else {
    cout << maxinum + maxjnum - 1 << endl;
  }
}


Error: WA

Fixed submission:

// g++ -std=gnu++14 a.cpp

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
ll MOD = 1e9 + 7;
int INF = 1 << 30;
ll INFL = 1LL << 60;
ll MODP = 998244353;

ll kai(ll x, ll y) { // xのy乗
  if (y == 0)
    return 1;
  if (y == 1)
    return x;
  if (y & 1) {
    return (x * kai(x, y - 1)) % MOD;
  } else {
    ll t = kai(x, y / 2);
    return (t * t) % MOD;
  }
}

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  vector<ll> r(K), c(K), v(K);
  rep(i, K) cin >> r[i] >> c[i] >> v[i];
  map<pair<int, int>, ll> m;
  rep(i, K) { m[make_pair(r[i], c[i])] = v[i]; }
  vector<vector<ll>> ans(R + 10, vector<ll>(C + 10, 0));
  for (int i = 1; i <= R; i++) {
    multiset<ll> s;
    for (int j = 1; j <= C; j++) {
      if (m.count(make_pair(i, j))) {
        s.insert(m[make_pair(i, j)]);
        ans[i][j] = ans[i][j - 1] + m[make_pair(i, j)];
        if (s.size() == 4) {
          ll out = *(s.begin());
          s.erase(s.find(out));
          ans[i][j] -= out;
        }
        if (ans[i - 1][j] + m[make_pair(i, j)] >= ans[i][j]) {
          ans[i][j] = ans[i - 1][j] + m[make_pair(i, j)];
          multiset<ll> ns;
          ns.insert(m[make_pair(i, j)]);
          s = ns;
        }
      } else {
        ans[i][j] = ans[i][j - 1];
        if (ans[i][j] < ans[i - 1][j]) {
          ans[i][j] = ans[i - 1][j];
          multiset<ll> ns;
          s = ns;
        }
      }
    }
  }
  /*
  rep(i,R+1){
    rep(j,C+1){
      cout << ans[i][j] << " ";
    }
    cout << endl;
  }
*/
  cout << ans[R][C] << endl;

  return 0;
}