Problem p00089 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int s_to_i(string s) {
  int n = 0;
  for (int i = 0; i < (int)s.size(); i++) {
    n *= 10;
    n += s[i] - '0';
  }
  return n;
}

vector<int> vs_to_vi(vector<string> vs) {
  vector<int> vi;
  for (int i = 0; i < (int)vs.size(); i++) {
    vi.push_back(s_to_i(vs[i]));
  }
  return vi;
}

vector<string> split(string s) {
  vector<string> vc;
  string s_;

  for (int i = 0; i < (int)s.size(); i++) {
    char c = s[i];

    if (s[i] == ',') {
      vc.push_back(s_);
      s_.clear();
    } else {
      s_.push_back(c);
      if (i == (int)s.size() - 1) {
        vc.push_back(s_);
      }
    }
  }
  return vc;
}

int main() {
  string s;
  vector<int> a[101];
  int b[101][101] = {0};
  int i;

  for (i = 0; getline(cin, s); i++) {
    vector<string> vc = split(s);
    a[i] = vs_to_vi(vc);
    // i++;
    // if( i==9 ) break;
  }
  b[0][0] = a[0][0];
  for (int j = 1; j < i; j++) {
    if (j <= i / 2) {
      for (int k = 0; k <= j; k++) {
        if (k == 0) {
          b[j][k] = a[j][0] + b[j - 1][k];
        } else {
          b[j][k] = a[j][k] + max(b[j - 1][k], b[j - 1][k - 1]);
        }
      }
    } else {
      for (int k = 0; k < i - j; k++) {
        b[j][k] = a[j][k] + max(b[j - 1][k], b[j - 1][k + 1]);
      }
    }
  }
  cout << b[i - 1][0] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <boost/tokenizer.hpp>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using namespace boost;

int f[200][200];
int line;
int ans;

int s_to_i(string s) {
  bool flag = false;
  int n = 0;

  for (unsigned int i = 0; i < s.size(); ++i) {
    if (i == 0 && s[i] == '-') {
      flag = true;
    } else if (s[i] >= '0' && s[i] <= '9') {
      n *= 10;
      n += s[i] - '0';
    } else {
      break;
    }
  }
  return (flag) ? -n : n;
}

vector<int> split(string s) {
  vector<int> vc;

  // ¶ñðJ}æØèÅª
  char_separator<char> sep(",");
  tokenizer<char_separator<char>> tokens(s, sep);

  // ªµ½¶ñð\\¦
  typedef tokenizer<char_separator<char>>::iterator Iter;
  for (Iter it = tokens.begin(); it != tokens.end(); ++it)
    vc.push_back(s_to_i(*it));
  return vc;
}

void solve(int x, int y, int sum) {
  sum += f[y][x];
  if (y == line - 1) {
    ans = max(ans, sum);
    return;
  } else {
    if (f[y + 1][x - 1] != -1)
      solve(x - 1, y + 1, sum);
    if (f[y + 1][x + 1] != -1)
      solve(x + 1, y + 1, sum);
  }
}

int main() {
  string s;
  int a;
  for (int y = 0; y < 200; ++y)
    for (int x = 0; x < 200; ++x)
      f[y][x] = -1;

  int x = 100;
  int y = 0;
  int n = 1;
  line = 1;
  ans = 0;
  cin >> a;
  f[y][x] = a;

  while (cin >> s) {
    // if( s == "q" ) break;

    y++;
    line++;
    vector<int> vc = split(s);
    (n < vc.size()) ? x-- : x++;
    n = vc.size();
    for (int i = 0; i < vc.size(); ++i) {
      f[y][x + i * 2] = vc[i];
    }
  }
  /*for(int y=0 ; y<line ; ++y ){
          for(int x=50-line ; x<50+line ; ++x ){
                  ( f[y][x] == -1 )? cout << " " : cout << (int)f[y][x];
          }
          cout << endl;
  }*/
  solve(100, 0, 0);
  cout << ans << endl;
}