Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;

long long add(long long a, long long b) {
  long long res = a + b;
  if (res >= mod)
    res -= mod;
  return res;
}

long long sub(long long a, long long b) {
  long long res = a - b + mod;
  if (res >= mod)
    res -= mod;
  return res;
}

long long mul(long long a, long long b) {
  return (((a % mod) * (b % mod)) % mod);
}

long long gcd(long long x, long long y) {
  if (y == 0) {
    return x;
  }
  if (x > y) {
    return gcd(y, x % y);
  } else {
    return gcd(x, y % x);
  }
}

void dfs(vector<vector<int>> &v, vector<int> &visited, int node, int &ans) {
  if (visited[node]) {
    return;
  }
  visited[node] = 1;
  ans++;
  for (auto i : v[node]) {
    dfs(v, visited, i, ans);
  }
}

/*bool good(vector<int> &v, int d, int mid){
    vector<int> p(v.size());
    p[0] = v[0] - mid;
    for(int i = 1;i < v.size();i++){
        p[i] = p[i - 1] + (v[i] - mid);
    }
    vector<int> m(v.size());
    m[0] = p[0];
    int mi = m[0];
    for(int i = 1;i < v.size();i++){
        mi = min(mi, p[i]);
        m[i] = mi;
    }
    for(int r = 0;r < v.size();r++){
        if(r - d > 0){
            if(m[r - d - 1] <= p[r]){
                L = r - d;
                R = r;
                return true;
            }
        }
    }
    return false;
}*/

bool comp(pair<int, int> x, pair<int, int> y) {
  if (x.first < y.first)
    return true;
  else if (x.first == y.first) {
    if (x.second < y.second)
      return true;
    else
      return false;
  } else {
    return false;
  }
}

long long a[10001], b[10001], c[10001], dp[10001][3];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];
  for (int i = 1; i <= n; i++) {
    dp[i][0] += max(dp[i - 1][1], dp[i - 1][2]) + a[i];
    dp[i][1] += max(dp[i - 1][0], dp[i - 1][2]) + b[i];
    dp[i][2] += max(dp[i - 1][1], dp[i - 1][0]) + c[i];
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2])) << endl;
}
/*
5 2
-1 -2 -3 4 5
2 3
4

-3 -2 -1 4 5
2 3
5 + (-3) = 2
4 + -2 = 2
ans = 4

-3 -2 -1 4 5
3 2
5 + -3 = 2
4 + -1 = 3
ans = 5
*/


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;

long long add(long long a, long long b) {
  long long res = a + b;
  if (res >= mod)
    res -= mod;
  return res;
}

long long sub(long long a, long long b) {
  long long res = a - b + mod;
  if (res >= mod)
    res -= mod;
  return res;
}

long long mul(long long a, long long b) {
  return (((a % mod) * (b % mod)) % mod);
}

long long gcd(long long x, long long y) {
  if (y == 0) {
    return x;
  }
  if (x > y) {
    return gcd(y, x % y);
  } else {
    return gcd(x, y % x);
  }
}

void dfs(vector<vector<int>> &v, vector<int> &visited, int node, int &ans) {
  if (visited[node]) {
    return;
  }
  visited[node] = 1;
  ans++;
  for (auto i : v[node]) {
    dfs(v, visited, i, ans);
  }
}

/*bool good(vector<int> &v, int d, int mid){
    vector<int> p(v.size());
    p[0] = v[0] - mid;
    for(int i = 1;i < v.size();i++){
        p[i] = p[i - 1] + (v[i] - mid);
    }
    vector<int> m(v.size());
    m[0] = p[0];
    int mi = m[0];
    for(int i = 1;i < v.size();i++){
        mi = min(mi, p[i]);
        m[i] = mi;
    }
    for(int r = 0;r < v.size();r++){
        if(r - d > 0){
            if(m[r - d - 1] <= p[r]){
                L = r - d;
                R = r;
                return true;
            }
        }
    }
    return false;
}*/

bool comp(pair<int, int> x, pair<int, int> y) {
  if (x.first < y.first)
    return true;
  else if (x.first == y.first) {
    if (x.second < y.second)
      return true;
    else
      return false;
  } else {
    return false;
  }
}

long long a[100001], b[100001], c[100001], dp[100001][3];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];
  for (int i = 1; i <= n; i++) {
    dp[i][0] += max(dp[i - 1][1], dp[i - 1][2]) + a[i];
    dp[i][1] += max(dp[i - 1][0], dp[i - 1][2]) + b[i];
    dp[i][2] += max(dp[i - 1][1], dp[i - 1][0]) + c[i];
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2])) << endl;
}
/*
5 2
-1 -2 -3 4 5
2 3
4

-3 -2 -1 4 5
2 3
5 + (-3) = 2
4 + -2 = 2
ans = 4

-3 -2 -1 4 5
3 2
5 + -3 = 2
4 + -1 = 3
ans = 5
*/