Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;
int arr[100005][3];
int n;

int solve(int sum, int call, int step) {
  if (step == n) {
    if (call == 1)
      return max(arr[n][0], arr[n][2]);
    else if (call == 2)
      return max(arr[n][0], arr[n][1]);
    else
      return max(arr[n][1], arr[n][2]);
  }

  ++step;
  if (call == 0) {
    return sum +
           max(solve(arr[step][1], 1, step), solve(arr[step][2], 2, step));
  }
  if (call == 1) {

    return sum +
           max(solve(arr[step][0], 0, step), solve(arr[step][2], 2, step));
  }
  if (call == 2) {

    return sum +
           max(solve(arr[step][1], 1, step), solve(arr[step][0], 0, step));
  }
}

int main() {
  std::ios::sync_with_stdio(false);

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

  int a1 = solve(arr[1][0], 0, 1);
  int a2 = solve(arr[1][1], 1, 1);
  int a3 = solve(arr[1][2], 2, 1);
  cout << max(a1, max(a2, a3));
  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;
int arr[100005][3];
int n;

int solve(int sum, int call, int step) {
  if (step == n) {
    if (call == 1)
      return max(arr[n][0], arr[n][2]);
    else if (call == 2)
      return max(arr[n][0], arr[n][1]);
    else
      return max(arr[n][1], arr[n][2]);
  }

  ++step;
  if (call == 0) {
    return sum +
           max(solve(arr[step][1], 1, step), solve(arr[step][2], 2, step));
  }
  if (call == 1) {

    return sum +
           max(solve(arr[step][0], 0, step), solve(arr[step][2], 2, step));
  }
  if (call == 2) {

    return sum +
           max(solve(arr[step][1], 1, step), solve(arr[step][0], 0, step));
  }
}

int main() {
  std::ios::sync_with_stdio(false);

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

  // int a1 = solve(arr[1][0], 0, 1);
  // int a2 = solve(arr[1][1], 1, 1);
  // int a3 = solve(arr[1][2], 2, 1);
  // cout<<max(a1, max(a2, a3))<<endl;

  int dp[n + 1][3];
  dp[1][0] = arr[1][0];
  dp[1][1] = arr[1][1];
  dp[1][2] = arr[1][2];
  for (int i = 2; i <= n; i++) {
    int A = arr[i][0] + max(dp[i - 1][1], dp[i - 1][2]);
    int B = arr[i][1] + max(dp[i - 1][0], dp[i - 1][2]);
    int C = arr[i][2] + max(dp[i - 1][0], dp[i - 1][1]);
    dp[i][0] = A;
    dp[i][1] = B;
    dp[i][2] = C;
  }

  cout << max(dp[n][0], max(dp[n][1], dp[n][2])) << endl;
  ;
  return 0;
}