Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long lli;
typedef long double lld;
#define mod 1000000007
#define mod2 998244353
#define fori(n) for (int i = 0; i < n; i++)
#define forj(n) for (int j = 0; j < n; j++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define sortvec(a) sort(a.begin(), a.end())
#define watch(x) cerr << "\n" << (#x) << " is " << (x) << endl
#define cerr                                                                   \
  if (true)                                                                    \
  cerr
void print() { cout << endl; }
template <typename T, typename... Args> void print(T a, Args... args) {
  cout << a << " ";
  print(args...);
}
lli llipowerp(lli x, lli y, lli p = LLONG_MAX) {
  lli res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
lli binarySearch(vector<int> arr, int l, int r, int x) {
  if (r >= l) {
    lli mid = l + (r - l) / 2;
    if (arr[mid] == x)
      return mid;
    if (arr[mid] > x)
      return binarySearch(arr, l, mid - 1, x);
    return binarySearch(arr, mid + 1, r, x);
  }
  return -1;
}
template <typename T> inline void printvec(vector<T> inp) {
  for (auto it : inp)
    cout << it << " ";
  cout << endl;
}
//---------------------------------------------------------------------------------
vector<vector<int>> h;

int dp[(int)1e5 + 5][3];
int fill(int n, int i) {
  if (dp[n][i] != -1)
    return dp[n][i];
  if (n == 1) {
    return h[1][i];
  }
  return max(h[n][i] + fill(n - 1, (i + 1) % 3),
             h[n][i] + fill(n - 1, (i + 2) % 3));
}
int main() {
  int n;
  cin >> n;
  h.resize(n + 1);
  fori(n + 1) h[i].resize(3);
  fori(n) { cin >> h[i + 1][0] >> h[i + 1][1] >> h[i + 1][2]; }
  memset(dp, -1, sizeof(dp));
  cout << max({fill(n, 0), fill(n, 1), fill(n, 2)}) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long lli;
typedef long double lld;
#define mod 1000000007
#define mod2 998244353
#define fori(n) for (int i = 0; i < n; i++)
#define forj(n) for (int j = 0; j < n; j++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define sortvec(a) sort(a.begin(), a.end())
#define watch(x) cerr << "\n" << (#x) << " is " << (x) << endl
#define cerr                                                                   \
  if (true)                                                                    \
  cerr
void print() { cout << endl; }
template <typename T, typename... Args> void print(T a, Args... args) {
  cout << a << " ";
  print(args...);
}
lli llipowerp(lli x, lli y, lli p = LLONG_MAX) {
  lli res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
lli binarySearch(vector<int> arr, int l, int r, int x) {
  if (r >= l) {
    lli mid = l + (r - l) / 2;
    if (arr[mid] == x)
      return mid;
    if (arr[mid] > x)
      return binarySearch(arr, l, mid - 1, x);
    return binarySearch(arr, mid + 1, r, x);
  }
  return -1;
}
template <typename T> inline void printvec(vector<T> inp) {
  for (auto it : inp)
    cout << it << " ";
  cout << endl;
}
//---------------------------------------------------------------------------------
vector<vector<int>> h;

int dp[(int)1e5 + 5][3];
int fill(int n, int i) {
  if (dp[n][i] != -1)
    return dp[n][i];
  if (n == 1) {
    return h[1][i];
  }
  dp[n][i] = max(h[n][i] + fill(n - 1, (i + 1) % 3),
                 h[n][i] + fill(n - 1, (i + 2) % 3));
  return dp[n][i];
}
int main() {
  int n;
  cin >> n;
  h.resize(n + 1);
  fori(n + 1) h[i].resize(3);
  fori(n) { cin >> h[i + 1][0] >> h[i + 1][1] >> h[i + 1][2]; }
  memset(dp, -1, sizeof(dp));
  cout << max({fill(n, 0), fill(n, 1), fill(n, 2)}) << endl;
}