Problem p03162 (C++)

Buggy submission (status: Runtime Error):
///   Journey to the End of earth   ///
/// Gunjan Kumar ///
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <functional> // for less
// using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define s(n) scanf("%d", &n)
#define ls(n) scanf("%lld", &n)
#define p(n) printf("%d", n)
// #define ln() printf("\n")
#define pln(n) printf("%d\n", n)
#define lpln(n) printf("%lld\n", n)
#define rep(i, a, n) for (int i = a; i < n; i++)
#define rev(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define tc                                                                     \
  ll t1;                                                                       \
  cin >> t1;                                                                   \
  while (t1--)
#define inp                                                                    \
  ll n;                                                                        \
  cin >> n;                                                                    \
  ll arr[n];                                                                   \
  rep(i, 0, n) cin >> arr[i];
#define vect vector<ll>
#define sortv(v) sort(v.begin(), v.end())
#define lower(v, n) lower_bound(v.begin(), v.end(), n) - v.begin()
#define upper(v, n) upper_bound(v.begin(), v.end(), n) - v.begin()
#define bitcount(n) __builtin_popcount(n)
#define ln << endl
#define inf LONG_MAX
#define ninf LONG_MIN
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
const ll mod = 1e9 + 7;
const ll N = 1e5 + 5;

int main() {

  ios_base::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("new.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  ll arr[n][3];
  rep(i, 0, n) rep(j, 0, 3) cin >> arr[i][j];
  ll dp[n][3];
  memset(dp, 0, sizeof(dp));
  dp[0][0] = arr[0][0];
  dp[0][1] = arr[0][1];
  dp[0][2] = arr[0][2];
  rep(i, 1, n) {
    dp[i][0] = max(arr[i][0] + dp[i - 1][1], arr[i][0] + dp[i - 1][2]);
    dp[i][1] = max(arr[i][1] + dp[i - 1][0], arr[i][1] + dp[i - 1][2]);
    dp[i][2] = max(arr[i][2] + dp[i - 1][1], arr[i][2] + dp[i - 1][0]);
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]));

  return 0;
}


Fixed submission:
///   Journey to the End of earth   ///
/// Gunjan Kumar ///
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <functional> // for less
// using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define s(n) scanf("%d", &n)
#define ls(n) scanf("%lld", &n)
#define p(n) printf("%d", n)
// #define ln() printf("\n")
#define pln(n) printf("%d\n", n)
#define lpln(n) printf("%lld\n", n)
#define rep(i, a, n) for (int i = a; i < n; i++)
#define rev(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define tc                                                                     \
  ll t1;                                                                       \
  cin >> t1;                                                                   \
  while (t1--)
#define inp                                                                    \
  ll n;                                                                        \
  cin >> n;                                                                    \
  ll arr[n];                                                                   \
  rep(i, 0, n) cin >> arr[i];
#define vect vector<ll>
#define sortv(v) sort(v.begin(), v.end())
#define lower(v, n) lower_bound(v.begin(), v.end(), n) - v.begin()
#define upper(v, n) upper_bound(v.begin(), v.end(), n) - v.begin()
#define bitcount(n) __builtin_popcount(n)
#define ln << endl
#define inf LONG_MAX
#define ninf LONG_MIN
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
const ll mod = 1e9 + 7;
const ll N = 1e5 + 5;

int main() {

  int n;
  cin >> n;
  ll arr[n][3];
  rep(i, 0, n) rep(j, 0, 3) cin >> arr[i][j];
  ll dp[n][3];
  memset(dp, 0, sizeof(dp));
  dp[0][0] = arr[0][0];
  dp[0][1] = arr[0][1];
  dp[0][2] = arr[0][2];
  rep(i, 1, n) {
    dp[i][0] = max(arr[i][0] + dp[i - 1][1], arr[i][0] + dp[i - 1][2]);
    dp[i][1] = max(arr[i][1] + dp[i - 1][0], arr[i][1] + dp[i - 1][2]);
    dp[i][2] = max(arr[i][2] + dp[i - 1][1], arr[i][2] + dp[i - 1][0]);
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]));

  return 0;
}