Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define int long long
// #define ll              long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 998244353
#define endl "\n"
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
int min(int a, int b) {
  if (a < b)
    return a;
  else
    return b;
}
// #DEBUG
void __print(int x) { cout << x; }
void __print(char x) { cout << '\'' << x << '\''; }
void __print(const char *x) { cout << '\"' << x << '\"'; }
void __print(const string &x) { cout << '\"' << x << '\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cout << '{';
  __print(x.first);
  cout << ',';
  __print(x.second);
  cout << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x)
    cout << (f++ ? "," : ""), __print(i);
  cout << "}";
}
void _print() { cout << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cout << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cout << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif
// ###################################################################
int pow(int a, int b, int m) {
  int r = 1, e = a;
  while (b) {
    if (b & 1)
      r = (r * e) % m;
    e = (e * e) % m;
    b >>= 1;
  }
  return r;
}
void fast() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
// First write recursive with no ristrictions;
// Then apply ristrictions; (What can we do? and Can we do it?)
// We are adding mod because sometimes ans could be negeative (So always add mod
// when substracting 2 mod answers)
int n;
int a[100005][4];
int dp[100005][4];

int solve(int day, int flag) {
  if (day == n) {
    return 0;
  }
  if (dp[day][flag] != -1) {
    return dp[day][flag];
  }
  int maxi = 0;
  if (flag == 0) {
    maxi = max(maxi, a[day][1] + solve(day + 1, 1));
    maxi = max(maxi, a[day][2] + solve(day + 1, 2));
    maxi = max(maxi, a[day][3] + solve(day + 1, 3));
  }
  if (flag == 1) {
    maxi = max(maxi, a[day][2] + solve(day + 1, 2));
    maxi = max(maxi, a[day][3] + solve(day + 1, 3));
  }
  if (flag == 2) {
    maxi = max(maxi, a[day][1] + solve(day + 1, 1));
    maxi = max(maxi, a[day][3] + solve(day + 1, 3));
  }
  if (flag == 3) {
    maxi = max(maxi, a[day][1] + solve(day + 1, 1));
    maxi = max(maxi, a[day][2] + solve(day + 1, 2));
  }
  return dp[day][flag] = maxi;
}
int32_t main() {
  fast();
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= 3; j++) {
      cin >> a[i][j];
    }
  }
  memset(dp, -1, sizeof(dp));
  cout << solve(0, 0) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define int long long
// #define ll              long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 998244353
#define endl "\n"
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
int min(int a, int b) {
  if (a < b)
    return a;
  else
    return b;
}
// #DEBUG
void __print(int x) { cout << x; }
void __print(char x) { cout << '\'' << x << '\''; }
void __print(const char *x) { cout << '\"' << x << '\"'; }
void __print(const string &x) { cout << '\"' << x << '\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cout << '{';
  __print(x.first);
  cout << ',';
  __print(x.second);
  cout << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x)
    cout << (f++ ? "," : ""), __print(i);
  cout << "}";
}
void _print() { cout << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cout << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cout << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif
// ###################################################################
int pow(int a, int b, int m) {
  int r = 1, e = a;
  while (b) {
    if (b & 1)
      r = (r * e) % m;
    e = (e * e) % m;
    b >>= 1;
  }
  return r;
}
void fast() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
// First write recursive with no ristrictions;
// Then apply ristrictions; (What can we do? and Can we do it?)
// We are adding mod because sometimes ans could be negeative (So always add mod
// when substracting 2 mod answers)
int n;
int a[100005][4];
int dp[100005][4];

int solve(int day, int flag) {
  if (day == n) {
    return 0;
  }
  if (dp[day][flag] != -1) {
    return dp[day][flag];
  }
  int maxi = 0;
  if (flag == 0) {
    maxi = max(maxi, a[day][1] + solve(day + 1, 1));
    maxi = max(maxi, a[day][2] + solve(day + 1, 2));
    maxi = max(maxi, a[day][3] + solve(day + 1, 3));
  }
  if (flag == 1) {
    maxi = max(maxi, a[day][2] + solve(day + 1, 2));
    maxi = max(maxi, a[day][3] + solve(day + 1, 3));
  }
  if (flag == 2) {
    maxi = max(maxi, a[day][1] + solve(day + 1, 1));
    maxi = max(maxi, a[day][3] + solve(day + 1, 3));
  }
  if (flag == 3) {
    maxi = max(maxi, a[day][1] + solve(day + 1, 1));
    maxi = max(maxi, a[day][2] + solve(day + 1, 2));
  }
  return dp[day][flag] = maxi;
}
int32_t main() {
  fast();
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= 3; j++) {
      cin >> a[i][j];
    }
  }
  memset(dp, -1, sizeof(dp));
  cout << solve(0, 0) << endl;
}