Problem p03162 (C++)

Buggy submission (status: Runtime Error):


#include <bits/stdc++.h>
using namespace std;

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

#define int long long
#define endl '\n'
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define vii vector<int>
#define pii pair<int, int>
#define vpii vector<pii>
#define all(v) v.begin(), v.end()
#define loop(i, s, e) for (int i = s; i < e; i++)
#define rloop(i, e, s) for (int i = e; i >= s; i--)
#define mset(a, f) memset(a, f, sizeof(a))
#define M 1000000007
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define prvec(v)                                                               \
  loop(i_u_j, 0, v.size()) cout << v[i_u_j] << " ";                            \
  cout << endl;
#define prarr(arr, n)                                                          \
  loop(i_u_j, 0, n) cout << arr[i_u_j] << " ";                                 \
  cout << endl;
#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //
const int N = 3e5 + 7;
int n;
int arr[N][3];
int dp[N][4];

int getans(int id, int p) {
  if (dp[id][p] != -1)
    return dp[id][p];
  if (id == n)
    return 0;
  int ans = 0;
  if (p == 0) {
    int a1 = arr[id][1] + getans(id + 1, 1);
    int a2 = arr[id][2] + getans(id + 1, 2);
    ans = max(a1, a2);
  } else if (p == 1) {
    int a1 = arr[id][0] + getans(id + 1, 0);
    int a2 = arr[id][2] + getans(id + 1, 2);
    ans = max(a1, a2);
  } else if (p == 2) {
    int a1 = arr[id][1] + getans(id + 1, 1);
    int a2 = arr[id][0] + getans(id + 1, 0);
    ans = max(a1, a2);
  } else {
    int a1 = arr[id][1] + getans(id + 1, 1);
    int a2 = arr[id][0] + getans(id + 1, 0);
    int a3 = arr[id][2] + getans(id + 1, 2);
    ans = max(a1, max(a3, a2));
  }
  return dp[id][p] = ans;
}

void solve() {
  cin >> n;
  loop(i, 0, n) { cin >> arr[i][0] >> arr[i][1] >> arr[i][2]; }
  mset(dp, -1);
  cout << getans(0, 3);
}

int32_t main() {
  fastio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int t = 1;
  // cin>>t;

  while (t--)
    solve();
}

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //


Fixed submission:


#include <bits/stdc++.h>
using namespace std;

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

#define int long long
#define endl '\n'
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define vii vector<int>
#define pii pair<int, int>
#define vpii vector<pii>
#define all(v) v.begin(), v.end()
#define loop(i, s, e) for (int i = s; i < e; i++)
#define rloop(i, e, s) for (int i = e; i >= s; i--)
#define mset(a, f) memset(a, f, sizeof(a))
#define M 1000000007
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define prvec(v)                                                               \
  loop(i_u_j, 0, v.size()) cout << v[i_u_j] << " ";                            \
  cout << endl;
#define prarr(arr, n)                                                          \
  loop(i_u_j, 0, n) cout << arr[i_u_j] << " ";                                 \
  cout << endl;
#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //
const int N = 3e5 + 7;
int n;
int arr[N][3];
int dp[N][4];

int getans(int id, int p) {
  if (dp[id][p] != -1)
    return dp[id][p];
  if (id == n)
    return 0;
  int ans = 0;
  if (p == 0) {
    int a1 = arr[id][1] + getans(id + 1, 1);
    int a2 = arr[id][2] + getans(id + 1, 2);
    ans = max(a1, a2);
  } else if (p == 1) {
    int a1 = arr[id][0] + getans(id + 1, 0);
    int a2 = arr[id][2] + getans(id + 1, 2);
    ans = max(a1, a2);
  } else if (p == 2) {
    int a1 = arr[id][1] + getans(id + 1, 1);
    int a2 = arr[id][0] + getans(id + 1, 0);
    ans = max(a1, a2);
  } else {
    int a1 = arr[id][1] + getans(id + 1, 1);
    int a2 = arr[id][0] + getans(id + 1, 0);
    int a3 = arr[id][2] + getans(id + 1, 2);
    ans = max(a1, max(a3, a2));
  }
  return dp[id][p] = ans;
}

void solve() {
  cin >> n;
  loop(i, 0, n) { cin >> arr[i][0] >> arr[i][1] >> arr[i][2]; }
  mset(dp, -1);
  cout << getans(0, 3);
}

int32_t main() {
  fastio;

  int t = 1;
  // cin>>t;

  while (t--)
    solve();
}

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //