Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
//  Suraj Saste

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pi pair<ll, ll>
#define in insert
#define fe first
#define se second
#define ms(x, i) memset(x, i, sizeof(x))
#define prec(x) cout << std::setprecision(x)
#define mod 1000000007
#define all(x) x.begin(), x.end()
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
ll fpow(ll n, ll k, ll p = mod) {
  ll r = 1;
  n = n % p;
  while (k > 0) {
    if (k & 1) {
      r = (r * n) % p;
    }
    k = k >> 1;
    n = (n * n) % p;
  }
  return r;
}
bool isPrime(ll n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (ll i = 5; i * i <= n; i += 6) {
    if (n % i == 0 || n % (i + 2) == 0)
      return false;
  }
  return true;
}

int main() {
  fast;
  ll n;
  cin >> n;
  ll a[n][3];
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < 3; j++)
      cin >> a[i][j];
  }
  ll dp[n][3];
  dp[0][0] = a[0][0];
  dp[0][1] = a[0][1];
  dp[0][2] = a[0][2];
  for (ll i = 1; i < n; i++) {
    for (ll j = 0; j < n; j++) {
      if (j == 0) {
        dp[i][j] = max(a[i][j] + dp[i - 1][1], a[i][j] + dp[i - 1][2]);
      } else if (j == 1) {
        dp[i][j] = max(a[i][j] + dp[i - 1][0], a[i][j] + dp[i - 1][2]);
      } else
        dp[i][j] = max(a[i][j] + dp[i - 1][1], a[i][j] + dp[i - 1][0]);
    }
  }
  ll ans = -1;
  for (ll j = 0; j < 3; j++) {
    ans = max(ans, dp[n - 1][j]);
  }
  cout << ans;
  return 0;
}


Fixed submission:
//  Suraj Saste

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pi pair<ll, ll>
#define in insert
#define fe first
#define se second
#define ms(x, i) memset(x, i, sizeof(x))
#define prec(x) cout << std::setprecision(x)
#define mod 1000000007
#define all(x) x.begin(), x.end()
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
ll fpow(ll n, ll k, ll p = mod) {
  ll r = 1;
  n = n % p;
  while (k > 0) {
    if (k & 1) {
      r = (r * n) % p;
    }
    k = k >> 1;
    n = (n * n) % p;
  }
  return r;
}
bool isPrime(ll n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (ll i = 5; i * i <= n; i += 6) {
    if (n % i == 0 || n % (i + 2) == 0)
      return false;
  }
  return true;
}

int main() {
  fast;
  ll n;
  cin >> n;
  ll a[n][3];
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < 3; j++)
      cin >> a[i][j];
  }
  ll dp[n][3];
  dp[0][0] = a[0][0];
  dp[0][1] = a[0][1];
  dp[0][2] = a[0][2];
  for (ll i = 1; i < n; i++) {
    for (ll j = 0; j < 3; j++) {
      if (j == 0) {
        dp[i][j] = max(a[i][j] + dp[i - 1][1], a[i][j] + dp[i - 1][2]);
      } else if (j == 1) {
        dp[i][j] = max(a[i][j] + dp[i - 1][0], a[i][j] + dp[i - 1][2]);
      } else
        dp[i][j] = max(a[i][j] + dp[i - 1][1], a[i][j] + dp[i - 1][0]);
    }
  }
  ll ans = -1;
  for (ll j = 0; j < 3; j++) {
    ans = max(ans, dp[n - 1][j]);
  }
  cout << ans;
  return 0;
}