Problem p03162 (C++)

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

#define endl '\n'
#define ll long long
#define ld long double
#define ull unsigned long long int
#define MOD (ll)(1e9 + 7)
#define vi vector<int>
#define vl vector<long>
#define vll vector<ll>
#define vs vector<string>
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define len(x) x.length()
#define all(x) x.begin(), x.end()
#define deb1(x) cout << #x << " " << x << endl;
#define deb2(x, y) cout << #x << " " << x << '	' << #y << " " << y << endl;
#define deb3(x, y, z)                                                          \
  cout << #x << " " << x << '	' << #y << " " << y << '	' << #z      \
       << " " << z << endl;
#define debug1(x) cerr << #x << " " << x << endl;
#define debug2(x, y)                                                           \
  cerr << #x << " " << x << '	' << #y << " " << y << endl;
#define debug3(x, y, z)                                                        \
  cerr << #x << " " << x << '	' << #y << " " << y << '	' << #z      \
       << " " << z << endl;
#define info(s) cerr << s << " " << endl;
#define fr(a, b, c) for (auto a = b; b < c ? a < c : a > c; b < c ? a++ : a--)
#define frc(a, b, c) for (auto a = b; a != c; a++)
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define lb(v, x) lower_bound(v.begin(), v.end(), x)
#define ub(v, x) upper_bound(v.begin(), v.end(), x)
#define clr(x) memset(x, 0, sizeof(x))
#define GR (double)(1 + sqrt(5)) / 2
#define fib(n) (ll) round(pow(GR, n) / sqrt(5))
#define FIO                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define out1(x) cout << x << " " << endl
#define out2(x, y) cout << x << " " << y << endl
#define SORT(a) sort(all(a));
#define SORTR(a) sort(all(a), greater<ll>());
#define ARRAY(a, n)                                                            \
  vll a;                                                                       \
  fr(i, 0, n) {                                                                \
    ll temp;                                                                   \
    cin >> temp;                                                               \
    a.pb(temp);                                                                \
  }
#define PRINT(a)                                                               \
  for (auto x : a)                                                             \
    cout << x << " ";                                                          \
  cout << endl
#define MAX 100050
#define pb push_back
#define mp make_pair
#define F first
#define S second

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v);

template <typename T> istream &operator>>(istream &in, vector<T> &v);

int dp[100004][3];

int happniess(vi &a, vi &b, vi &c, int n, int x) {
  if (n == 0)
    return 0;
  int ans1, ans2, ans3;
  ans1 = ans2 = ans3 = INT_MIN;
  if (x != 1)
    ans1 = a[n - 1] + happniess(a, b, c, n - 1, 1);
  if (x != 2)
    ans2 = b[n - 1] + happniess(a, b, c, n - 1, 2);
  if (x != 3)
    ans3 = c[n - 1] + happniess(a, b, c, n - 1, 3);
  return max({ans1, ans2, ans3});
}

void solve() {
  int n;
  cin >> n;
  vi a(n), b(n), c(n);
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];
  memset(dp, -1, sizeof(dp));
  int ans = happniess(a, b, c, n, 0);
  cout << ans << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  FIO;
  ll test;
  test = 1;
  // cin>>test;for(ll i=1;i<=test;i++)
  solve();
  return 0;
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  int i = 0;
  for (T x : v) {
    out << x;
    (i++ != v.size()) ? out << ' ' : out << '\n';
  }
  return out;
}

template <typename T> istream &operator>>(istream &in, vector<T> &v) {
  for (T &i : v)
    in >> i;
  return in;
}

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

#define endl '\n'
#define ll long long
#define ld long double
#define ull unsigned long long int
#define MOD (ll)(1e9 + 7)
#define vi vector<int>
#define vl vector<long>
#define vll vector<ll>
#define vs vector<string>
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define len(x) x.length()
#define all(x) x.begin(), x.end()
#define deb1(x) cout << #x << " " << x << endl;
#define deb2(x, y) cout << #x << " " << x << '	' << #y << " " << y << endl;
#define deb3(x, y, z)                                                          \
  cout << #x << " " << x << '	' << #y << " " << y << '	' << #z      \
       << " " << z << endl;
#define debug1(x) cerr << #x << " " << x << endl;
#define debug2(x, y)                                                           \
  cerr << #x << " " << x << '	' << #y << " " << y << endl;
#define debug3(x, y, z)                                                        \
  cerr << #x << " " << x << '	' << #y << " " << y << '	' << #z      \
       << " " << z << endl;
#define info(s) cerr << s << " " << endl;
#define fr(a, b, c) for (auto a = b; b < c ? a < c : a > c; b < c ? a++ : a--)
#define frc(a, b, c) for (auto a = b; a != c; a++)
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define lb(v, x) lower_bound(v.begin(), v.end(), x)
#define ub(v, x) upper_bound(v.begin(), v.end(), x)
#define clr(x) memset(x, 0, sizeof(x))
#define GR (double)(1 + sqrt(5)) / 2
#define fib(n) (ll) round(pow(GR, n) / sqrt(5))
#define FIO                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define out1(x) cout << x << " " << endl
#define out2(x, y) cout << x << " " << y << endl
#define SORT(a) sort(all(a));
#define SORTR(a) sort(all(a), greater<ll>());
#define ARRAY(a, n)                                                            \
  vll a;                                                                       \
  fr(i, 0, n) {                                                                \
    ll temp;                                                                   \
    cin >> temp;                                                               \
    a.pb(temp);                                                                \
  }
#define PRINT(a)                                                               \
  for (auto x : a)                                                             \
    cout << x << " ";                                                          \
  cout << endl
#define MAX 100050
#define pb push_back
#define mp make_pair
#define F first
#define S second

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v);

template <typename T> istream &operator>>(istream &in, vector<T> &v);

int dp[100004][3];

int happniess(vi &a, vi &b, vi &c, int n, int x) {
  // if(n==0)
  //     return 0;
  // int ans1,ans2,ans3;
  // ans1=ans2=ans3=INT_MIN;
  // if(x!=1)
  //     ans1 = a[n-1]+happniess(a,b,c,n-1,1);
  // if(x!=2)
  //     ans2 = b[n-1]+happniess(a,b,c,n-1,2);
  // if(x!=3)
  //     ans3 = c[n-1]+happniess(a,b,c,n-1,3);
  // return max({ans1,ans2,ans3});

  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];

  for (int i = 1; i < n; i++) {
    dp[i][0] = a[i] + max(dp[i - 1][1], dp[i - 1][2]);
    dp[i][1] = b[i] + max(dp[i - 1][0], dp[i - 1][2]);
    dp[i][2] = c[i] + max(dp[i - 1][1], dp[i - 1][0]);
  }
  return max({dp[n - 1][0], dp[n - 1][1], dp[n - 1][2]});
}

void solve() {
  int n;
  cin >> n;
  vi a(n), b(n), c(n);
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];
  memset(dp, -1, sizeof(dp));
  int ans = happniess(a, b, c, n, 0);
  cout << ans << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  FIO;
  ll test;
  test = 1;
  // cin>>test;for(ll i=1;i<=test;i++)
  solve();
  return 0;
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  int i = 0;
  for (T x : v) {
    out << x;
    (i++ != v.size()) ? out << ' ' : out << '\n';
  }
  return out;
}

template <typename T> istream &operator>>(istream &in, vector<T> &v) {
  for (T &i : v)
    in >> i;
  return in;
}