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

// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("Ofast")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <typename T>
// using ordered_set = tree <T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>; #define sfind(i) (*find_by_order(i))
// #define ord(i) order_of_key(i)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
#define mod 1000000007
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repc(i, a, b, c)                                                       \\
  for (ll i = a; (c > 0 ? i < b : i > b) and c != 0; i += c)
#define vi vector<int>
#define vl vector<ll>
#define _max(vec) (*max_element(all(vec)))
#define _min(vec) (*min_element(all(vec)))
#define fi first
#define si second
#define pb push_back
#define all(name) name.begin(), name.end()
#define ral(name) name.rbegin(), name.rend()
#define fact(n) rep(i, 1, n + 1) ft.pb((ft[i - 1] * i) % mod)
#define mod_in(a) power(a, mod - 2)
#define ncr(n, r)                                                              \\
  (n >= r ? ((ft[n] * mod_in((ft[r] * ft[(n) - (r)]) % mod)) % mod) : 0LL)
#define deb(x) cerr << "[" << #x << " " << x << "]" << endl;
#define endl '\\n' //....????
#define int ll
vector<ll> ft(1, 1LL);

/*======================================================================*/

int val[3005][3005];
int dp[3005][3005][4];
void solve() {
  // int n, k, ans = -1e18;
  // cin >> n >> k;
  // vi v(n+1), c(n+1);
  // rep(i, 1, n+1)cin >> v[i];
  // rep(i, 1, n+1)cin >> c[i];
  // rep(i, 1, n+1){
  //     int cur = i, temp = 0;
  //     rep(j, 0, k){
  //         cur = v[cur];
  //         if(v[cur] == cur){
  //             if(c[cur] < 0){
  //                 if(j == 0)temp += c[cur];
  //             }
  //             else temp += (k-j)*c[cur];
  //             break;
  //         }
  //         else temp += c[cur];
  //         ans = max(temp, ans);
  //     }
  //     ans = max(temp, ans);
  // }
  // cout << ans << endl;
  int r, c, k;
  cin >> r >> c >> k;
  rep(i, 0, r + 1) rep(j, 0, c + 1) val[i][j] = 0;
  rep(i, 0, k) {
    int a, b, c;
    cin >> a >> b >> c;
    val[a][b] = c;
  }
  rep(i, 0, r + 1) rep(j, 0, c + 1) rep(k, 0, 4) dp[i][j][k] = 0;
  int ans = 0;
  rep(i, 1, r + 1) {
    rep(j, 1, c + 1) {
      dp[i][j][0] = max({dp[i - 1][j][0], dp[i - 1][j][1], dp[i - 1][j][2],
                         dp[i - 1][j][3], dp[i][j - 1][0]});
      dp[i][j][1] = max({dp[i - 1][j][0], dp[i - 1][j][1], dp[i - 1][j][2],
                         dp[i - 1][j][3]}) +
                    val[i][j];
      rep(k, 1, 4) {
        dp[i][j][k] = max(
            {dp[i][j - 1][k - 1] + val[i][j], dp[i][j][k], dp[i][j - 1][k]});
      }
    }
  }
  rep(i, 0, r + 1) rep(j, 0, c + 1) rep(k, 0, 4) ans = max(dp[i][j][k], ans);
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}

Error: 0

Fixed submission:

// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("Ofast")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <typename T>
// using ordered_set = tree <T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>; #define sfind(i) (*find_by_order(i))
// #define ord(i) order_of_key(i)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
#define mod 1000000007
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repc(i, a, b, c)                                                       \\
  for (ll i = a; (c > 0 ? i < b : i > b) and c != 0; i += c)
#define vi vector<int>
#define vl vector<ll>
#define _max(vec) (*max_element(all(vec)))
#define _min(vec) (*min_element(all(vec)))
#define fi first
#define si second
#define pb push_back
#define all(name) name.begin(), name.end()
#define ral(name) name.rbegin(), name.rend()
#define fact(n) rep(i, 1, n + 1) ft.pb((ft[i - 1] * i) % mod)
#define mod_in(a) power(a, mod - 2)
#define ncr(n, r)                                                              \\
  (n >= r ? ((ft[n] * mod_in((ft[r] * ft[(n) - (r)]) % mod)) % mod) : 0LL)
#define deb(x) cerr << "[" << #x << " " << x << "]" << endl;
#define endl '\\n' //....????
#define int ll
vector<ll> ft(1, 1LL);

/*======================================================================*/

void solve() {
  // int n, k, ans = -1e18;
  // cin >> n >> k;
  // vi v(n+1), c(n+1);
  // rep(i, 1, n+1)cin >> v[i];
  // rep(i, 1, n+1)cin >> c[i];
  // rep(i, 1, n+1){
  //     int cur = i, temp = 0;
  //     rep(j, 0, k){
  //         cur = v[cur];
  //         if(v[cur] == cur){
  //             if(c[cur] < 0){
  //                 if(j == 0)temp += c[cur];
  //             }
  //             else temp += (k-j)*c[cur];
  //             break;
  //         }
  //         else temp += c[cur];
  //         ans = max(temp, ans);
  //     }
  //     ans = max(temp, ans);
  // }
  // cout << ans << endl;
  int r, c, k;
  cin >> r >> c >> k;
  int dp[r + 1][c + 1][4];
  int val[r + 1][c + 1];
  rep(i, 0, r + 1) rep(j, 0, c + 1) val[i][j] = 0;
  rep(i, 0, k) {
    int a, b, c;
    cin >> a >> b >> c;
    val[a][b] = c;
  }
  rep(i, 0, r + 1) rep(j, 0, c + 1) rep(k, 0, 4) dp[i][j][k] = 0;
  int ans = 0;
  rep(i, 1, r + 1) {
    rep(j, 1, c + 1) {
      dp[i][j][0] = max({dp[i - 1][j][0], dp[i - 1][j][1], dp[i - 1][j][2],
                         dp[i - 1][j][3], dp[i][j - 1][0]});
      dp[i][j][1] = max({dp[i - 1][j][0], dp[i - 1][j][1], dp[i - 1][j][2],
                         dp[i - 1][j][3]}) +
                    val[i][j];
      rep(k, 1, 4) {
        dp[i][j][k] = max(
            {dp[i][j - 1][k - 1] + val[i][j], dp[i][j][k], dp[i][j - 1][k]});
      }
    }
  }
  rep(i, 0, r + 1) rep(j, 0, c + 1) rep(k, 0, 4) ans = max(dp[i][j][k], ans);
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}