Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <deque>
#include <iostream>
#include <stdlib.h>
#include <string>
#define N 10005
#define ul unsigned long long
#define ll long long int
#define loop0(i, a, b) for (int i = a; i < b; ++i)
#define loop1(i, a, b) for (int i = a; i <= b; ++i)
#define ini0(i, b) for (int i = b; i >= 0; --i)
#define ini1(i, b) for (int i = b - 1; i >= 0; --i)
#define pl pair<ll, ll>
#define pi pair<int, int>
#define ff first
#define ss second
#define bg begin()
#define ed end()

#define READ freopen("input.txt", "r", stdin);
#define WRITE freopen("output.txt", "w", stdout);

#define test1(x) cout << #x << " : " << x << endl;
#define test2(x, y) cout << #x << " : " << x << " " << #y << " : " << y << endl;
#define test3(x, y, z)                                                         \
  cout << #x << " : " << x << "   " << #y << " : " << y << "  " << #z << " : " \
       << z << endl;

int bgval(int n, int a) {
  int rt = n / a;
  if (n % a != 0)
    rt++;
  return rt;
}

using namespace std;

map<string, int> mp;

ll phi(ll n) {
  ll result = n; // Initialize result as n

  // Consider all prime factors of n and subtract their
  // multiples from result
  for (ll p = 2; p * p <= n; ++p) {

    // Check if p is a prime factor.
    if (n % p == 0) {

      // If yes, then update n and result
      while (n % p == 0)
        n /= p;
      result -= result / p;
    }
  }

  // If n has a prime factor greater than sqrt(n)
  // (There can be at-most one such prime factor)
  if (n > 1)
    result -= result / n;
  return result;
}

bool isprime(ll n) {
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);

  int t = 1;
  // cin >> t;
  while (t--) {
    int n, k = 0;
    cin >> n;
    /*vector<ll> vc(n,0), dp(n,INT_MAX);
    for (auto &it : vc) cin >> it;
    dp[0] = 0;*/
    ll a, b, c;
    ll ans[N][4] = {0};

    for (int i = 1; i <= n; i++) {
      cin >> a >> b >> c;
      if (i == 1) {
        ans[1][1] = a;
        ans[1][2] = b;
        ans[1][3] = c;
      } else {

        ans[i][1] += max(ans[i - 1][2], ans[i - 1][3]) + a;
        ans[i][2] += max(ans[i - 1][1], ans[i - 1][3]) + b;
        ans[i][3] += max(ans[i - 1][2], ans[i - 1][1]) + c;
      }
    }

    cout << max(ans[n][1], max(ans[n][2], ans[n][3])) << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <deque>
#include <iostream>
#include <stdlib.h>
#include <string>
#define N 100005
#define ul unsigned long long
#define ll long long int
#define loop0(i, a, b) for (int i = a; i < b; ++i)
#define loop1(i, a, b) for (int i = a; i <= b; ++i)
#define ini0(i, b) for (int i = b; i >= 0; --i)
#define ini1(i, b) for (int i = b - 1; i >= 0; --i)
#define pl pair<ll, ll>
#define pi pair<int, int>
#define ff first
#define ss second
#define bg begin()
#define ed end()

#define READ freopen("input.txt", "r", stdin);
#define WRITE freopen("output.txt", "w", stdout);

#define test1(x) cout << #x << " : " << x << endl;
#define test2(x, y) cout << #x << " : " << x << " " << #y << " : " << y << endl;
#define test3(x, y, z)                                                         \
  cout << #x << " : " << x << "   " << #y << " : " << y << "  " << #z << " : " \
       << z << endl;

int bgval(int n, int a) {
  int rt = n / a;
  if (n % a != 0)
    rt++;
  return rt;
}

using namespace std;

map<string, int> mp;

ll phi(ll n) {
  ll result = n; // Initialize result as n

  // Consider all prime factors of n and subtract their
  // multiples from result
  for (ll p = 2; p * p <= n; ++p) {

    // Check if p is a prime factor.
    if (n % p == 0) {

      // If yes, then update n and result
      while (n % p == 0)
        n /= p;
      result -= result / p;
    }
  }

  // If n has a prime factor greater than sqrt(n)
  // (There can be at-most one such prime factor)
  if (n > 1)
    result -= result / n;
  return result;
}

bool isprime(ll n) {
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);

  int t = 1;
  // cin >> t;
  while (t--) {
    int n, k = 0;
    cin >> n;
    /*vector<ll> vc(n,0), dp(n,INT_MAX);
    for (auto &it : vc) cin >> it;
    dp[0] = 0;*/
    ll a, b, c;
    ll ans[N][4] = {0};

    for (int i = 1; i <= n; i++) {
      cin >> a >> b >> c;
      if (i == 1) {
        ans[1][1] = a;
        ans[1][2] = b;
        ans[1][3] = c;
      } else {

        ans[i][1] += max(ans[i - 1][2], ans[i - 1][3]) + a;
        ans[i][2] += max(ans[i - 1][1], ans[i - 1][3]) + b;
        ans[i][3] += max(ans[i - 1][2], ans[i - 1][1]) + c;
      }
    }

    cout << max(ans[n][1], max(ans[n][2], ans[n][3])) << endl;
  }

  return 0;
}