Problem p03451 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define F first
#define S second
#define f_in freopen("test.in", "r", stdin);
#define f_out freopen("test.in", "w", stdout);
#define debug(x) cerr << #x << " : " << x << "\n";
#define _                                                                      \
  cin.sync_with_stdio(0);                                                      \
  cin.tie(0);
#define tl                                                                     \
  cout << "\nTime elapsed: " << (1000 * clock() / CLOCKS_PER_SEC) << "ms\n"

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;

inline char nc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}

template <typename T = int> inline T nxt() {
  char c = nc();
  T x = 0;
  int f = 1;
  for (; c > '9' || c < '0'; c = nc())
    if (c == '-')
      f = -1;
  for (; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = nc())
    ;
  x *= f;
  return x;
}

const int N = 110;
int a[N][2], dp[N][2], n;

int solve(int x, int y) {
  if (x == n || y == 2)
    return 0;
  int &ans = dp[x][y];
  if (~ans)
    return ans;
  return ans = a[x][y] + max(solve(x + 1, y), solve(x, y + 1));
}

int main() {
#ifndef ONLINE_JUDGE
  f_in
#else
  _
#endif // ONLINE_JUDGE
      n = nxt();
  memset(dp, -1, sizeof dp);
  rep(i, 0, n) a[i][0] = nxt();
  rep(i, 0, n) a[i][1] = nxt();
  cout << solve(0, 0) << '\n';
}


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

using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define F first
#define S second
#define f_in freopen("test.in", "r", stdin);
#define f_out freopen("test.in", "w", stdout);
#define debug(x) cerr << #x << " : " << x << "\n";
#define _                                                                      \
  cin.sync_with_stdio(0);                                                      \
  cin.tie(0);
#define tl                                                                     \
  cout << "\nTime elapsed: " << (1000 * clock() / CLOCKS_PER_SEC) << "ms\n"

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;

inline char nc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}

template <typename T = int> inline T nxt() {
  char c = nc();
  T x = 0;
  int f = 1;
  for (; c > '9' || c < '0'; c = nc())
    if (c == '-')
      f = -1;
  for (; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = nc())
    ;
  x *= f;
  return x;
}

const int N = 110;
int a[N][2], dp[N][2], n;

int solve(int x, int y) {
  if (x == n || y == 2)
    return 0;
  int &ans = dp[x][y];
  if (~ans)
    return ans;
  return ans = a[x][y] + max(solve(x + 1, y), solve(x, y + 1));
}

int main() {
  n = nxt();
  memset(dp, -1, sizeof dp);
  rep(i, 0, n) a[i][0] = nxt();
  rep(i, 0, n) a[i][1] = nxt();
  cout << solve(0, 0) << '\n';
}