Problem p00089 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

vector<string> split(const string &str, char delim) {
  vector<string> res;
  size_t current = 0, found;
  while ((found = str.find_first_of(delim, current)) != string::npos) {
    res.push_back(string(str, current, found - current));
    current = found + 1;
  }
  res.push_back(string(str, current, str.size() - current));
  return res;
}

int main() {
  vector<vector<int>> v;
  vector<vector<int>> dp;
  string s;
  while (cin >> s) {
    vector<string> ret = split(s, ',');
    vector<int> res(ret.size());

    rep(i, ret.size()) {
      stringstream ss;
      ss << ret[i];
      ss >> res[i];
    }

    v.push_back(res);
    dp.push_back(res);
  }

  int mid = v.size() / 2;
  rep(i, mid) {
    rep(j, v[i].size()) {
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + v[i + 1][j]);
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + v[i + 1][j + 1]);
    }
  }

  REP(i, mid, v.size() - 1) {
    rep(j, v[i].size()) {
      if (j == 0) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + v[i + 1][j]);
      } else if (j == v[i].size() - 1) {
        dp[i + 1][j - 1] = max(dp[i + 1][j - 1], dp[i][j] + v[i + 1][j - 1]);
      } else {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + v[i + 1][j]);
        dp[i + 1][j - 1] = max(dp[i + 1][j - 1], dp[i][j] + v[i + 1][j - 1]);
      }
    }
  }

  cout << dp[v.size() - 1][0] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int memo3[105][105];
int len[105];
int i;
vector<int> vec;

vector<string> split(const string &str, char delim) {
  vector<string> res;
  size_t current = 0, found;
  while ((found = str.find_first_of(delim, current)) != string::npos) {
    res.push_back(string(str, current, found - current));
    current = found + 1;
  }
  res.push_back(string(str, current, str.size() - current));
  return res;
}

void dfs(int dep, int j, int sum) {
  sum += memo3[dep][j];
  if (dep == i - 1) {
    vec.push_back(sum);
    return;
  }

  if (dep < (i - 1) / 2) {
    dfs(dep + 1, j, sum);
    dfs(dep + 1, j + 1, sum);
  }

  else {
    if (j == 0) {
      dfs(dep + 1, 0, sum);
    } else if (j == len[dep] - 1) {
      dfs(dep + 1, j - 1, sum);
    } else {
      dfs(dep + 1, j, sum);
      dfs(dep + 1, j - 1, sum);
    }
  }
}

int main() {
  memset(memo3, 0, sizeof(memo3));
  string s;
  i = 0;
  int ans = 0;
  bool flag = false;
  while (cin >> s) {
    vector<string> _s = split(s, ',');
    len[i] = _s.size();
    rep(j, _s.size()) { memo3[i][j] = atoi(_s[j].c_str()); }
    i++;
  }
  dfs(0, 0, 0);
  rep(c, vec.size()) { ans = max(ans, vec[c]); }
  cout << ans << endl;
  return 0;
}