Problem p03162 (C++)

Buggy submission (status: Runtime Error):
// Includes

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

// Defines

#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define MOD 1000000007
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define alla(a, n) a, a + n
#define mem(name, value) memset(name, value, sizeof(name))
#define f(i, a) for (int i = 0; i < a; i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<string> vs;

int arr[100005][3], dp[100005][3];
int n;

int calc(int i, int k) {
  if (i == -1)
    return 0;
  if (dp[i][k] != -1)
    return dp[i][k];
  return dp[i][k] = max(arr[i][k] + calc(i - 1, (k + 1) % 3),
                        arr[i][k] + calc(i - 1, (k + 2) % 3));
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n;
  f(i, n) { cin >> arr[i][0] >> arr[i][1] >> arr[i][2]; }
  mem(dp, -1);

  int a1 = calc(n - 1, 0);
  mem(dp, -1);
  int a2 = calc(n - 1, 1);
  mem(dp, -1);
  int a3 = calc(n - 1, 2);

  cout << max(a1, max(a2, a3)) << "\n";
  return 0;
}

Fixed submission:
// Includes

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

// Defines

#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define MOD 1000000007
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define alla(a, n) a, a + n
#define mem(name, value) memset(name, value, sizeof(name))
#define f(i, a) for (int i = 0; i < a; i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<string> vs;

int arr[100005][3], dp[100005][3];
int n;

int calc(int i, int k) {
  if (i == -1)
    return 0;
  if (dp[i][k] != -1)
    return dp[i][k];
  return dp[i][k] = max(arr[i][k] + calc(i - 1, (k + 1) % 3),
                        arr[i][k] + calc(i - 1, (k + 2) % 3));
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n;
  f(i, n) { cin >> arr[i][0] >> arr[i][1] >> arr[i][2]; }
  mem(dp, -1);

  int a1 = calc(n - 1, 0);
  mem(dp, -1);
  int a2 = calc(n - 1, 1);
  mem(dp, -1);
  int a3 = calc(n - 1, 2);

  cout << max(a1, max(a2, a3)) << "\n";
  return 0;
}