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

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  for (auto it : P) {
    s << "<" << it << "> ";
  }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  for (auto it : P) {
    s << "<" << it.first << "->" << it.second << "> ";
  }
  return s << endl;
}

int R, C, K;
vector<vector<long long>> v;

long long dp[3100][3100][5];

int main() {
  while (cin >> R >> C >> K) {
    v.assign(R + 1, vector<long long>(C + 1, -1));
    for (int i = 0; i < K; ++i) {
      long long r, c, t;
      cin >> r >> c >> t;
      v[r][c] = t;
    }
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i <= R; ++i) {
      for (int j = 0; j <= C; ++j) {
        for (int k = 0; k <= 3; ++k) {
          // ue
          if (i < R) {
            chmax(dp[i + 1][j][0], dp[i][j][k]);
            if (v[i + 1][j] != -1) {
              chmax(dp[i + 1][j][1], dp[i][j][k] + v[i + 1][j]);
            }
          }
          // migi
          if (j < C) {
            chmax(dp[i][j + 1][k], dp[i][j][k]);
            if (v[i][j + 1] != -1 && k + 1 <= 3) {
              chmax(dp[i][j + 1][k + 1], dp[i][j][k] + v[i][j + 1]);
            }
          }
        }
      }
    }
    long long res = 0;
    for (int k = 0; k <= 3; ++k)
      chmax(res, dp[R][C][k]);
    cout << res << endl;
  }
}

/*
using Item = pair<int, long long>;
int R, C, K;
vector<vector<Item>> items;
vector<vector<int>> ys;

long long solve() {
    vector<vector<long long>> dp(R+1, vector<long long>(C+1, 0));
    for (int r = 1; r <= R; ++r) {
        for (int c = 0; c <= C; ++c) {
            if (dp[r][c] > 0) {
                cout << r << ", " << c << ": " << dp[r][c] << endl;
            }
            auto it = lower_bound(ys[r].begin(),
            ys[r].end(), c) - ys[r].begin();

            chmax(dp[r][c], dp[r-1][c]);
            long long vadd = 0;
            for (int add = 0; add <= 2; ++add) {
                if (it + add < ys[r].size()) {
                    int it2 = it + add;
                    int c2 = ys[r][it2];
                    vadd += items[r][it2].second;
                    chmax(dp[r][c2], dp[r-1][c] + vadd);
                }
            }
        }
    }
    long long res = 0;
    for (int r = 0; r <= R; ++r) {
        for (int c = 0; c <= C; ++c) {
            chmax(res, dp[r][c]);
        }
    }
    return res;
}

int main() {
    while (cin >> R >> C >> K) {
        items.assign(R+1, vector<Item>());
        ys.assign(R+1, vector<int>());
        for (int i = 0; i < K; ++i) {
            int r, c;
            long long v;
            cin >> r >> c >> v;
            items[r].push_back(Item(c, v));
        }
        for (int r = 0; r <= R; ++r) {
            sort(items[r].begin(), items[r].end());
            for (int i = 0; i < items[r].size(); ++i) {
                ys[r].push_back(items[r][i].first);
            }
        }
        cout << solve() << endl;
    }
}




*/


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, start, end) for (int i = start; i <= end; i++)
const int INF = 1001001001;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define Rort(a) sort(a.rbegin(), a.rend())
typedef long long ll;
const ll MOD = 1000000007;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> auto MAX(const T &a) {
  return *max_element(a.begin(), a.end());
}
template <class T> auto MIN(const T &a) {
  return *min_element(a.begin(), a.end());
}
template <class T, class U> U SUM(const T &a, const U &v) {
  return accumulate(a.begin(), a.end(), v);
}
template <class T, class U> U COUNT(const T &a, const U &v) {
  return count(a.begin(), a.end(), v);
}
template <class T, class U> int LOWER(const T &a, const U &v) {
  return lower_bound(a.begin(), a.end(), v) - a.begin();
}
template <class T, class U> int UPPER(const T &a, const U &v) {
  return upper_bound(a.begin(), a.end(), v) - a.begin();
}
int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }
int LCM(int a, int b) {
  int g = GCD(a, b);
  return a / g * b;
}
//---------------------------------------------------------------------------------------------------
template <int MOD> struct ModInt {
  static const int Mod = MOD;
  unsigned x;
  ModInt() : x(0) {}
  ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
  ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
  int get() const { return (int)x; }
  ModInt &operator+=(ModInt that) {
    if ((x += that.x) >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator-=(ModInt that) {
    if ((x += MOD - that.x) >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator*=(ModInt that) {
    x = (unsigned long long)x * that.x % MOD;
    return *this;
  }
  ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
  ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
  ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
  ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
  ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
  ModInt inverse() const {
    long long a = x, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      std::swap(a, b);
      u -= t * v;
      std::swap(u, v);
    }
    return ModInt(u);
  }
  bool operator==(ModInt that) const { return x == that.x; }
  bool operator!=(ModInt that) const { return x != that.x; }
  ModInt operator-() const {
    ModInt t;
    t.x = x == 0 ? 0 : Mod - x;
    return t;
  }
};
template <int MOD> ostream &operator<<(ostream &st, const ModInt<MOD> a) {
  st << a.get();
  return st;
};
template <int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
  ModInt<MOD> r = 1;
  while (k) {
    if (k & 1)
      r *= a;
    a *= a;
    k >>= 1;
  }
  return r;
}
template <typename T, int FAC_MAX> struct Comb {
  vector<T> fac, ifac;
  Comb() {
    fac.resize(FAC_MAX, 1);
    ifac.resize(FAC_MAX, 1);
    FOR(i, 1, FAC_MAX - 1) fac[i] = fac[i - 1] * i;
    ifac[FAC_MAX - 1] = T(1) / fac[FAC_MAX - 1];
    for (int i = FAC_MAX - 2; i >= 1; i--)
      ifac[i] = ifac[i + 1] * T(i + 1);
  }
  T aPb(int a, int b) {
    if (b < 0 || a < b)
      return T(0);
    return fac[a] * ifac[a - b];
  }
  T aCb(int a, int b) {
    if (b < 0 || a < b)
      return T(0);
    return fac[a] * ifac[a - b] * ifac[b];
  }
  T nHk(int n, int k) {
    if (n == 0 && k == 0)
      return T(1);
    if (n <= 0 || k < 0)
      return 0;
    return aCb(n + k - 1, k);
  } // nHk = (n+k-1)Ck : n is separator
  T pairCombination(int n) {
    if (n % 2 == 1)
      return T(0);
    return fac[n] * ifac[n / 2] / (T(2) ^ (n / 2));
  }
  // combination of paris for n
};
typedef ModInt<1000000007> mint;

int main(void) {
  // Your code here!
  int R, C, K;
  cin >> R >> C >> K;
  vector<vector<ll>> V(R + 1, vector<ll>(C + 1, -1));
  rep(i, K) {
    ll r, c, v;
    cin >> r >> c >> v;
    V[r][c] = v;
  }
  ll dp[3100][3100][5];
  memset(dp, 0, sizeof(dp));
  FOR(i, 0, R) {
    FOR(j, 0, C) {
      rep(k, 4) {
        // ue
        if (i < R) {
          chmax(dp[i + 1][j][0], dp[i][j][k]);
          if (V[i + 1][j] != -1) {
            chmax(dp[i + 1][j][1], dp[i][j][k] + V[i + 1][j]);
          }
        }

        if (j < C) {
          chmax(dp[i][j + 1][k], dp[i][j][k]);
          if (V[i][j + 1] != -1 && k + 1 <= 3) {
            chmax(dp[i][j + 1][k + 1], dp[i][j][k] + V[i][j + 1]);
          }
        }
      }
    }
  }
  ll res = 0;
  rep(k, 4) chmax(res, dp[R][C][k]);
  cout << res << endl;
}