Problem p03162 (C++)

Buggy submission (status: Runtime Error):
// *
// *
// *  Created By :hozaifa wahid
// * "mai maut ko takia aur kafan ko chadar banakar sota hu"
// *------------------------------------
// *  OS : Ubuntu 16.04
// *  Language : CPP14
// *  Editor : Sublime Text 3
// *  C++ compiler : g++
// *
// *
// https://www.geeksforgeeks.org/find-the-point-where-maximum-intervals-overlap/
#include <bits/stdc++.h>
#define pb push_back
// #define mp make_pair
#define inf 1000000007
#define fr first
#define sc second
#define eps 1e-9
#define clr(a) memset(a, 0, sizeof(a))
#define sz(x) x.size()
#define sni(x) scanf("%d", &x)
#define snl(x) scanf("%lld", &x)
#define snc(x) scanf("%c", &c);
#define rep(n) for (int i = 0; i < n; i++)
#define repc(i, n) for (int i = 0; i < n; i++)
#define FOR(i, x, y) for (int i = x; i < y; i++)
#define DEC(i, x, y) for (int i = x; i >= y; i--)
#define all(v) v.begin(), v.end()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define alla(a, n) a, a + n
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
ll MOD = 1000000007;
vector<ll> v[100055];
void sieve() {
  for (int n = 1; n <= 100055; n++) {
    for (int i = 1; i <= sqrt(n); i++) {
      if (n % i == 0) {
        // If divisors are equal, print only one
        if (n / i == i)
          v[n].push_back(i);

        else // Otherwise print both
        {
          v[n].push_back(i);
          v[n].push_back(n / i);
        }
      }
    }
  }
}
ll pw(ll a, ll b) {
  if (b == 0)
    return 1;
  ll x = pw(a, b / 2);
  x = (x * x) % MOD;
  if (b % 2)
    x = (x * a) % MOD;
  return x;
}
ll n, s;
ll a[100005][3];
int ok = 0;
// ll res=0;
ll dp[100005];
ll mpi[3][100005];
ll go(ll i, ll prev) {
  if (i == n - 1) {
    // s=0;
    return a[i][prev];
  }
  if (mpi[i][prev])
    return mpi[i][prev];

  else if (prev == 0) {
    s = a[i][prev] + (max(go(i + 1, 2), go(i + 1, 1)));
    mpi[i][prev] = s;
  } else if (prev == 1) {
    s = a[i][prev] + (max(go(i + 1, 2), go(i + 1, 0)));
    mpi[i][prev] = s;
  } else if (prev == 2) {
    s = a[i][prev] + (max(go(i + 1, 0), go(i + 1, 1)));
    mpi[i][prev] = s;
  }

  return s;
}

void solve() {
  ll t;
  t = 1;
  while (t--) {
    // ll n;
    cin >> n;
    // ll a[n];
    FOR(i, 0, n)
    FOR(j, 0, 3)
    cin >> a[i][j], mpi[i][j] = 0;
    ll res = max(go(0, 0), max(go(0, 1), go(0, 2)));
    cout << res;
  }
}

int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int start = clock();
#ifdef ONLINE_JUDGE
#else
  // freopen("input.in" , "r" , stdin);
  //  freopen("output.txt", "w", stdout);
#endif
  solve();
  int stop = clock();
#ifdef ONLINE_JUDGE
#else
  // cout << setprecision(6) << fixed << (stop - start) * 1000.00 /
  // double(CLOCKS_PER_SEC)<< endl;
#endif
}


Fixed submission:
// *
// *
// *  Created By :hozaifa wahid
// * "mai maut ko takia aur kafan ko chadar banakar sota hu"
// *------------------------------------
// *  OS : Ubuntu 16.04
// *  Language : CPP14
// *  Editor : Sublime Text 3
// *  C++ compiler : g++
// *
// *
// https://www.geeksforgeeks.org/find-the-point-where-maximum-intervals-overlap/
#include <bits/stdc++.h>
#define pb push_back
// #define mp make_pair
#define inf 1000000007
#define fr first
#define sc second
#define eps 1e-9
#define clr(a) memset(a, 0, sizeof(a))
#define sz(x) x.size()
#define sni(x) scanf("%d", &x)
#define snl(x) scanf("%lld", &x)
#define snc(x) scanf("%c", &c);
#define rep(n) for (int i = 0; i < n; i++)
#define repc(i, n) for (int i = 0; i < n; i++)
#define FOR(i, x, y) for (int i = x; i < y; i++)
#define DEC(i, x, y) for (int i = x; i >= y; i--)
#define all(v) v.begin(), v.end()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define alla(a, n) a, a + n
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
ll MOD = 1000000007;
vector<ll> v[100055];
void sieve() {
  for (int n = 1; n <= 100055; n++) {
    for (int i = 1; i <= sqrt(n); i++) {
      if (n % i == 0) {
        // If divisors are equal, print only one
        if (n / i == i)
          v[n].push_back(i);

        else // Otherwise print both
        {
          v[n].push_back(i);
          v[n].push_back(n / i);
        }
      }
    }
  }
}
ll pw(ll a, ll b) {
  if (b == 0)
    return 1;
  ll x = pw(a, b / 2);
  x = (x * x) % MOD;
  if (b % 2)
    x = (x * a) % MOD;
  return x;
}
ll n, s;
ll a[100005][3];
int ok = 0;
// ll res=0;
ll dp[100005];
ll mpi[100005][3];
ll go(ll i, ll prev) {
  if (i == n - 1) {
    // s=0;
    return a[i][prev];
  }
  if (mpi[i][prev])
    return mpi[i][prev];

  else if (prev == 0) {
    s = a[i][prev] + (max(go(i + 1, 2), go(i + 1, 1)));
    mpi[i][prev] = s;
  } else if (prev == 1) {
    s = a[i][prev] + (max(go(i + 1, 2), go(i + 1, 0)));
    mpi[i][prev] = s;
  } else if (prev == 2) {
    s = a[i][prev] + (max(go(i + 1, 0), go(i + 1, 1)));
    mpi[i][prev] = s;
  }

  return s;
}

void solve() {
  ll t;
  t = 1;
  while (t--) {
    // ll n;
    cin >> n;
    // ll a[n];
    FOR(i, 0, n)
    FOR(j, 0, 3)
    cin >> a[i][j], mpi[i][j] = 0;
    ll res = max(go(0, 0), max(go(0, 1), go(0, 2)));
    cout << res;
  }
}

int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int start = clock();
#ifdef ONLINE_JUDGE
#else
  // freopen("input.in" , "r" , stdin);
  //  freopen("output.txt", "w", stdout);
#endif
  solve();
  int stop = clock();
#ifdef ONLINE_JUDGE
#else
  // cout << setprecision(6) << fixed << (stop - start) * 1000.00 /
  // double(CLOCKS_PER_SEC)<< endl;
#endif
}