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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

vector<string> split(string str, string delim) {
  vector<string> result;
  int cutAt;
  while ((cutAt = str.find_first_of(delim)) != str.npos) {
    if (cutAt > 0) {
      result.push_back(str.substr(0, cutAt));
    }
    str = str.substr(cutAt + 1);
  }
  if (str.length() > 0) {
    result.push_back(str);
  }
  return result;
}

struct P {
  P() : x(0), y(0) {}
  double x, y;
};

#define range(x) (0 <= x && x < table[y - 1].size())

int main() {
  vector<vint> table;
  ;

  string input;
  for (int i = 0; cin >> input; i++) {
    table.push_back(vint());
    vector<string> vs = split(input, ",");

    rep(j, vs.size()) table[i].push_back(atoi(vs[j].c_str()));
  }

  REP(y, 1, table.size()) rep(x, table[y].size()) {
    int dx = (y < table.size() / 2 ? -1 : 1);

    table[y][x] += max((range(x) ? table[y - 1][x] : 0),
                       (range(x + dx) ? table[y - 1][x + dx] : 0));
  }

  cout << table[table.size() - 1][0] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

vector<string> split(string str, string delim) {
  vector<string> result;
  int cutAt;
  while ((cutAt = str.find_first_of(delim)) != str.npos) {
    if (cutAt > 0) {
      result.push_back(str.substr(0, cutAt));
    }
    str = str.substr(cutAt + 1);
  }
  if (str.length() > 0) {
    result.push_back(str);
  }
  return result;
}

struct P {
  P() : x(0), y(0) {}
  double x, y;
};
int dxu[] = {0, 1};
int dxb[] = {0, -1};

int rec(vector<vint> &table, P now) {
  if (now.y == table.size() - 1)
    return 0;

  int res = 0;
  rep(i, 2) {
    P p = now;
    p.y++;
    p.x += (p.y < table.size() / 2 ? dxu[i] : dxb[i]);

    if (0 <= p.x && p.x < table[p.y].size())
      chmax(res, table[now.y][now.x] + rec(table, p));
  }

  return res;
}

int main() {
  vector<vint> table;

  string input;
  for (int i = 0; cin >> input; i++) {
    table.push_back(vint());
    vector<string> vs = split(input, ",");

    rep(j, vs.size()) table[i].push_back(atoi(vs[j].c_str()));
  }

  table.push_back(vint(1, 0));

  cout << rec(table, P()) << endl;
}