Problem p03162 (C++)

Buggy submission (status: Runtime Error):
/*
                        ███╗   ███╗ █████╗ ███████╗███╗   ███╗ ██████╗ ██╗
   ██╗██████╗ ███████╗ █████╗ ███╗   ███╗██████╗ ██╗   ██╗ ████╗
   ████║██╔══██╗╚════██║████╗ ████║██╔═══██╗██║ ██║██╔══██╗╚════██║██╔══██╗████╗
   ████║██╔══██╗╚██╗ ██╔╝ ██╔████╔██║███████║    ██╔╝██╔████╔██║██║   ██║██║
   ██║██║  ██║    ██╔╝███████║██╔████╔██║██║  ██║ ╚████╔╝ ██║╚██╔╝██║██╔══██║
   ██╔╝ ██║╚██╔╝██║██║   ██║██║   ██║██║  ██║   ██╔╝ ██╔══██║██║╚██╔╝██║██║  ██║
   ╚██╔╝ ██║ ╚═╝ ██║██║  ██║   ██║  ██║ ╚═╝ ██║╚██████╔╝╚██████╔╝██████╔╝██╗██║
   ██║  ██║██║ ╚═╝ ██║██████╔╝   ██║ ╚═╝     ╚═╝╚═╝  ╚═╝   ╚═╝  ╚═╝     ╚═╝
   ╚═════╝  ╚═════╝ ╚═════╝ ╚═╝╚═╝  ╚═╝  ╚═╝╚═╝     ╚═╝╚═════╝    ╚═╝

                        “Nobody but you have to believe in your dreams to make
   them a reality.” ― Germany Kent
*/
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define Ma7moud_7amdy                                                          \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define Open_Sesame Open()
#define pb push_back
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define clr(arr, x) memset(arr, x, sizeof arr)
#define endl "\n"
#define Accepted 0
#define watch(x) cout << #x << " = " << x << endl;
#define un_map unordered_map
#define RT(x) return cout << (x), 0;
#define sc(a) scanf("%d", &a)
#define scll(x) scanf("%I64d", &x)
#define scan(s) scanf("%[^\n]%*c", &s)
#define pr(s) printf("%s", s.c_str()); // print string
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double EPS = -1e-2;
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
const ll mod = 998244353;
const int Mx = INT_MAX;
const int Mn = INT_MIN;
const ll MX = LLONG_MAX;
const ll MN = LLONG_MIN;
const int N = 1e5 + 5;
void Open() {
#ifndef ONLINE_JUDGE
  freopen("Input.txt", "r", stdin);
  freopen("Output.txt", "w", stdout);
#endif
}
struct Vacation {
  int Swim, bugs, homework;
};
int n;
vector<Vacation> v;
int mem[N][4];
int solve(int idx = 0, int prev = 0) {
  if (idx == n)
    return 0;
  int &ret = mem[idx][prev];
  if (~ret)
    return ret;
  ret = 0;
  if (prev != 1)
    ret = max(ret, solve(idx + 1, 1) + v[idx].Swim);
  if (prev != 2)
    ret = max(ret, solve(idx + 1, 2) + v[idx].bugs);
  if (prev != 3)
    ret = max(ret, solve(idx + 1, 3) + v[idx].homework);

  return ret;
}
int main() {
  Ma7moud_7amdy;
  Open_Sesame;
  clr(mem, -1);
  cin >> n;
  v = vector<Vacation>(n);
  for (int i = 0; i < n; i++)
    cin >> v[i].Swim >> v[i].bugs >> v[i].homework;
  cout << solve();
}


Fixed submission:
/*
                        ███╗   ███╗ █████╗ ███████╗███╗   ███╗ ██████╗ ██╗
   ██╗██████╗ ███████╗ █████╗ ███╗   ███╗██████╗ ██╗   ██╗ ████╗
   ████║██╔══██╗╚════██║████╗ ████║██╔═══██╗██║ ██║██╔══██╗╚════██║██╔══██╗████╗
   ████║██╔══██╗╚██╗ ██╔╝ ██╔████╔██║███████║    ██╔╝██╔████╔██║██║   ██║██║
   ██║██║  ██║    ██╔╝███████║██╔████╔██║██║  ██║ ╚████╔╝ ██║╚██╔╝██║██╔══██║
   ██╔╝ ██║╚██╔╝██║██║   ██║██║   ██║██║  ██║   ██╔╝ ██╔══██║██║╚██╔╝██║██║  ██║
   ╚██╔╝ ██║ ╚═╝ ██║██║  ██║   ██║  ██║ ╚═╝ ██║╚██████╔╝╚██████╔╝██████╔╝██╗██║
   ██║  ██║██║ ╚═╝ ██║██████╔╝   ██║ ╚═╝     ╚═╝╚═╝  ╚═╝   ╚═╝  ╚═╝     ╚═╝
   ╚═════╝  ╚═════╝ ╚═════╝ ╚═╝╚═╝  ╚═╝  ╚═╝╚═╝     ╚═╝╚═════╝    ╚═╝

                        “Nobody but you have to believe in your dreams to make
   them a reality.” ― Germany Kent
*/
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define Ma7moud_7amdy                                                          \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define Open_Sesame Open()
#define pb push_back
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define clr(arr, x) memset(arr, x, sizeof arr)
#define endl "\n"
#define Accepted 0
#define watch(x) cout << #x << " = " << x << endl;
#define un_map unordered_map
#define RT(x) return cout << (x), 0;
#define sc(a) scanf("%d", &a)
#define scll(x) scanf("%I64d", &x)
#define scan(s) scanf("%[^\n]%*c", &s)
#define pr(s) printf("%s", s.c_str()); // print string
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double EPS = -1e-2;
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
const ll mod = 998244353;
const int Mx = INT_MAX;
const int Mn = INT_MIN;
const ll MX = LLONG_MAX;
const ll MN = LLONG_MIN;
const int N = 1e5 + 5;
void Open() {
#ifndef ONLINE_JUDGE
  freopen("Input.txt", "r", stdin);
  freopen("Output.txt", "w", stdout);
#endif
}
struct Vacation {
  int Swim, bugs, homework;
};
int n;
vector<Vacation> v;
int mem[N][4];
int solve(int idx = 0, int prev = 0) {
  if (idx == n)
    return 0;
  int &ret = mem[idx][prev];
  if (~ret)
    return ret;
  ret = 0;
  if (prev != 1)
    ret = max(ret, solve(idx + 1, 1) + v[idx].Swim);
  if (prev != 2)
    ret = max(ret, solve(idx + 1, 2) + v[idx].bugs);
  if (prev != 3)
    ret = max(ret, solve(idx + 1, 3) + v[idx].homework);

  return ret;
}
int main() {
  Ma7moud_7amdy;
  // Open_Sesame;
  clr(mem, -1);
  cin >> n;
  v = vector<Vacation>(n);
  for (int i = 0; i < n; i++)
    cin >> v[i].Swim >> v[i].bugs >> v[i].homework;
  cout << solve();
}