Problem p03162 (C++)

Buggy submission (status: Runtime Error):
/***************************************************
 * Problem Name : C - Vacation.cpp
 * Problem Link :
 * OJ           :
 * Verdict      : Trying
 * Date         : 2019-01-26
 * Problem Type :
 * Author Name  : Saikat Sharma
 * University   : CSE, MBSTU
 ***************************************************/
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define __FastIO                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define FileRead freopen("/home/saikat/Desktop/logN/input.txt", "r", stdin);
#define FileWrite freopen("/home/saikat/Desktop/logN/output.txt", "w", stdout);
#define SET(a, v) memset(a, v, sizeof(a))
#define pii pair<int, int>
#define pll pair<ll, ll>
#define debug cout << "#########\n";
#define nl cout << "\n";
#define sp cout << " ";
#define sl(n) scanf("%lld", &n)
#define sf(n) scanf("%lf", &n)
#define si(n) scanf("%d", &n)
#define ss(n) scanf("%s", n)
#define pf(n) scanf("%d", n)
#define pfl(n) scanf("%lld", n)
#define all(v) v.begin(), v.end()
#define Pow2(x) ((x) * (x))
#define Mod(x, m) ((((x) % (m)) + (m)) % (m))
#define Max3(a, b, c) max(a, max(b, c))
#define Min3(a, b, c) min(a, min(b, c))
#define pb push_back
#define mk make_pair
#define MAX 1005
#define INF 1000000000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

template <typename T> std::string NumberToString(T Number) {
  std::ostringstream ss;
  ss << Number;
  return ss.str();
}
ll lcm(ll a, ll b) { return a * b / __gcd(a, b); }
/************************************ Code Start Here
 * ******************************************************/
int ar[MAX][5];
int dp[MAX][5];
int n;
int fun(int i, int t) {
  //~ debug;
  if (i == n - 1) {
    return ar[i][t];
  }

  if (dp[i][t] != -1)
    return dp[i][t];

  int ret1 = 0, ret2 = 0;

  if (t == 0) {
    ret1 = ar[i][t] + fun(i + 1, 1);
    ret2 = ar[i][t] + fun(i + 1, 2);

  } else if (t == 1) {
    ret1 = ar[i][t] + fun(i + 1, 0);
    ret2 = ar[i][t] + fun(i + 1, 2);

  } else {
    ret1 = ar[i][t] + fun(i + 1, 0);
    ret2 = ar[i][t] + fun(i + 1, 1);
  }

  return dp[i][t] = max(ret1, ret2);
}
int main() {
  //~ __FastIO;
  //~ int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> ar[i][j];
    }
  }

  //~ debug;
  SET(dp, -1);
  int a = fun(0, 0);
  //~ debug;
  int b = fun(0, 1);
  int c = fun(0, 2);
  cout << Max3(a, b, c) << "\n";
  return 0;
}


Fixed submission:
/***************************************************
 * Problem Name : C - Vacation.cpp
 * Problem Link :
 * OJ           :
 * Verdict      : Trying
 * Date         : 2019-01-26
 * Problem Type :
 * Author Name  : Saikat Sharma
 * University   : CSE, MBSTU
 ***************************************************/
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define __FastIO                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define FileRead freopen("/home/saikat/Desktop/logN/input.txt", "r", stdin);
#define FileWrite freopen("/home/saikat/Desktop/logN/output.txt", "w", stdout);
#define SET(a, v) memset(a, v, sizeof(a))
#define pii pair<int, int>
#define pll pair<ll, ll>
#define debug cout << "#########\n";
#define nl cout << "\n";
#define sp cout << " ";
#define sl(n) scanf("%lld", &n)
#define sf(n) scanf("%lf", &n)
#define si(n) scanf("%d", &n)
#define ss(n) scanf("%s", n)
#define pf(n) scanf("%d", n)
#define pfl(n) scanf("%lld", n)
#define all(v) v.begin(), v.end()
#define Pow2(x) ((x) * (x))
#define Mod(x, m) ((((x) % (m)) + (m)) % (m))
#define Max3(a, b, c) max(a, max(b, c))
#define Min3(a, b, c) min(a, min(b, c))
#define pb push_back
#define mk make_pair
#define MAX 1000005
#define INF 1000000000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

template <typename T> std::string NumberToString(T Number) {
  std::ostringstream ss;
  ss << Number;
  return ss.str();
}
ll lcm(ll a, ll b) { return a * b / __gcd(a, b); }
/************************************ Code Start Here
 * ******************************************************/
int ar[MAX][5];
int dp[MAX][5];
int n;
int fun(int i, int t) {
  //~ debug;
  if (i == n - 1) {
    return ar[i][t];
  }

  if (dp[i][t] != -1)
    return dp[i][t];

  int ret1 = 0, ret2 = 0;

  if (t == 0) {
    ret1 = ar[i][t] + fun(i + 1, 1);
    ret2 = ar[i][t] + fun(i + 1, 2);

  } else if (t == 1) {
    ret1 = ar[i][t] + fun(i + 1, 0);
    ret2 = ar[i][t] + fun(i + 1, 2);

  } else {
    ret1 = ar[i][t] + fun(i + 1, 0);
    ret2 = ar[i][t] + fun(i + 1, 1);
  }

  return dp[i][t] = max(ret1, ret2);
}
int main() {
  //~ __FastIO;
  //~ int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> ar[i][j];
    }
  }

  //~ debug;
  SET(dp, -1);
  int a = fun(0, 0);
  //~ debug;
  int b = fun(0, 1);
  int c = fun(0, 2);
  cout << Max3(a, b, c) << "\n";
  return 0;
}