Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define lli long long int
#define f first
#define s second
#define sc(n) scanf("%d", &n);
#define scl(n) scanf("%lld", &n);
#define pr(n) printf("%d", n);
#define prl(n) printf("%lld", n);
#define nl printf("\n");
#define all(c) (c).begin(), (c).end()
#define sz(a) int((a).size())
#define FN(i, n) for (int i = 0; i < (int)(n); ++i)
#define FEN(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define tr(c, i) for (typeof((c).begin() i = (c).begin(); i != (c).end(); i++))
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

int main() {
  int n;
  cin >> n;
  int arr[11111][3];
  int ans[11111];
  for (int i = 1; i <= n; i++) {
    cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
  }

  int x, y, z;
  x = 0;
  y = 0;
  z = 0;
  for (int i = 1; i <= n; i++) {
    int x1, y1, z1;
    x1 = x;
    y1 = y;
    z1 = z;
    x = arr[i][0] + max(y1, z1);
    y = arr[i][1] + max(x1, z1);
    z = arr[i][2] + max(y1, x1);
    ans[i] = max(x, max(y, z));
  }
  cout << ans[n];
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define lli long long int
#define f first
#define s second
#define sc(n) scanf("%d", &n);
#define scl(n) scanf("%lld", &n);
#define pr(n) printf("%d", n);
#define prl(n) printf("%lld", n);
#define nl printf("\n");
#define all(c) (c).begin(), (c).end()
#define sz(a) int((a).size())
#define FN(i, n) for (int i = 0; i < (int)(n); ++i)
#define FEN(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define tr(c, i) for (typeof((c).begin() i = (c).begin(); i != (c).end(); i++))
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

int main() {
  int n;
  cin >> n;
  int arr[111111][3];
  int ans[111111];
  for (int i = 1; i <= n; i++) {
    cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
  }

  int x, y, z;
  x = 0;
  y = 0;
  z = 0;
  for (int i = 1; i <= n; i++) {
    int x1, y1, z1;
    x1 = x;
    y1 = y;
    z1 = z;
    x = arr[i][0] + max(y1, z1);
    y = arr[i][1] + max(x1, z1);
    z = arr[i][2] + max(y1, x1);
    ans[i] = max(x, max(y, z));
  }
  cout << ans[n];
}