Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
#define int long long
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define localin                                                                \
  freopen("C:\\Users\\hp\\Desktop\\CPP\\inout\\inputf.in", "r", stdin);
#define localout                                                               \
  freopen("C:\\Users\\hp\\Desktop\\CPP\\inout\\outputf.in", "w", stdout);
const int MAX = 2e5 + 5;
#define ld long double
#define endl "\n"
#define pb push_back
#define ff first
#define ss second
#define vi vector<int>
#define tot(v) v.begin(), v.end()
#define rtcheck                                                                \
  cout << "saurabh\n";                                                         \
  exit(0);
#define vcheck(x) cout << #x << " " << x << endl;
#define pint pair<int, int>
#define forn for (int i = 0; i < n; i++)
const int modu = 1e9 + 7;
const int inf = 1e17;

struct activity {
  int A, B, C;
};

int solve(vector<activity> ar, int n) {
  int dp[n][3];
  dp[0][0] = ar[0].A;
  dp[0][1] = ar[0].B;
  dp[0][2] = ar[0].C;

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

int32_t main() {
  fast;
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    vector<activity> ar;
    for (int i = 0; i < n; i++) {
      cin >> ar[i].A;
      cin >> ar[i].B;
      cin >> ar[i].C;
    }
    int ans = solve(ar, n);
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
#define int long long
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define localin                                                                \
  freopen("C:\\Users\\hp\\Desktop\\CPP\\inout\\inputf.in", "r", stdin);
#define localout                                                               \
  freopen("C:\\Users\\hp\\Desktop\\CPP\\inout\\outputf.in", "w", stdout);
const int MAX = 2e5 + 5;
#define ld long double
#define endl "\n"
#define pb push_back
#define ff first
#define ss second
#define vi vector<int>
#define tot(v) v.begin(), v.end()
#define rtcheck                                                                \
  cout << "saurabh\n";                                                         \
  exit(0);
#define vcheck(x) cout << #x << " " << x << endl;
#define pint pair<int, int>
#define forn for (int i = 0; i < n; i++)
const int modu = 1e9 + 7;
const int inf = 1e17;

struct activity {
  int A, B, C;
};

int solve(vector<activity> ar, int n) {
  int dp[n][3];
  dp[0][0] = ar[0].A;
  dp[0][1] = ar[0].B;
  dp[0][2] = ar[0].C;

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

int32_t main() {
  fast;
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    vector<activity> ar(n + 1);
    for (int i = 0; i < n; i++) {
      cin >> ar[i].A;
      cin >> ar[i].B;
      cin >> ar[i].C;
    }
    int ans = solve(ar, n);
    cout << ans << endl;
  }
}