Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FASTIO
#define endl                                                                   \
  "\n" // Since endl forces a buffer flush which slows down large I/O
       // operations.
#define ff first
#define ss second
#define pb push_back
#define input(c, n)                                                            \
  for (int i = 0; i < (n); i++)                                                \
    cin >> (c)[i];
#define exit_message(s) return 0 * printf(s)
#define show(c)                                                                \
  for (auto i : (c))                                                           \
    cout << i << " ";                                                          \
  cout << "\n";
#define display(c, n)                                                          \
  for (int i = 0; i < (n); i++)                                                \
    cout << (c)[i] << " ";                                                     \
  cout << "\n";
#define forl(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define dugbe(k) cout << "-\t> " << #k << " = " << k << "\n";

/* TIPS FOR DEBUGGING
 * Check if values are not overflowing (use long long where required)
 * Check if ranges are inclusive or not.
 * Check properly if greedy will work or not before using it.
 * Check EDGE CASES!! Seriously, even in 2B, saving 8 points by being
 * 2 minutes faster will bite you in the ass if you don't make sure
 * it works on edge cases.
 * Keep adding to this list.
 */

using namespace std;
typedef long long ll;
typedef pair<int, int> pr;
typedef pair<ll, ll> prll;
typedef vector<int> vi;
typedef vector<ll> vill;
typedef vector<pr> vpr;
ifstream in("input.txt");
const ll mod = 1e9 + 7;

ll mod_pow(ll a, ll b) {
  if (b == 0)
    return 1LL;

  ll res = mod_pow(a, b >> 1);
  res = (res * res) % mod;
  if (b & 1LL)
    res = (res * a) % mod;

  return res;
}

const int N = 100 + 5;
const int SZ = 3000 + 5;

int n, k;
int a[N];
ll dp[N][N];

ll solve(int l, int r) {
  if (l > r)
    return 0;

  ll &memo = dp[l][r];
  if (memo != -1)
    return memo;

  memo = max(a[l] - solve(l + 1, r), a[r] - solve(l, r - 1));
  return memo;
}

int32_t main() {
#ifdef FASTIO
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#endif
  // freopen("in.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  memset(dp, -1, sizeof dp);

  cin >> n;
  forl(i, 0, n) { cin >> a[i]; }
  ll ans = solve(0, n - 1);
  cout << ans << "\n";

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FASTIO
#define endl                                                                   \
  "\n" // Since endl forces a buffer flush which slows down large I/O
       // operations.
#define ff first
#define ss second
#define pb push_back
#define input(c, n)                                                            \
  for (int i = 0; i < (n); i++)                                                \
    cin >> (c)[i];
#define exit_message(s) return 0 * printf(s)
#define show(c)                                                                \
  for (auto i : (c))                                                           \
    cout << i << " ";                                                          \
  cout << "\n";
#define display(c, n)                                                          \
  for (int i = 0; i < (n); i++)                                                \
    cout << (c)[i] << " ";                                                     \
  cout << "\n";
#define forl(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define dugbe(k) cout << "-\t> " << #k << " = " << k << "\n";

/* TIPS FOR DEBUGGING
 * Check if values are not overflowing (use long long where required)
 * Check if ranges are inclusive or not.
 * Check properly if greedy will work or not before using it.
 * Check EDGE CASES!! Seriously, even in 2B, saving 8 points by being
 * 2 minutes faster will bite you in the ass if you don't make sure
 * it works on edge cases.
 * Keep adding to this list.
 */

using namespace std;
typedef long long ll;
typedef pair<int, int> pr;
typedef pair<ll, ll> prll;
typedef vector<int> vi;
typedef vector<ll> vill;
typedef vector<pr> vpr;
ifstream in("input.txt");
const ll mod = 1e9 + 7;

ll mod_pow(ll a, ll b) {
  if (b == 0)
    return 1LL;

  ll res = mod_pow(a, b >> 1);
  res = (res * res) % mod;
  if (b & 1LL)
    res = (res * a) % mod;

  return res;
}

const int N = 3000 + 5;

int n, k;
int a[N];
ll dp[N][N];

ll solve(int l, int r) {
  if (l > r)
    return 0;

  ll &memo = dp[l][r];
  if (memo != -1)
    return memo;

  memo = max(a[l] - solve(l + 1, r), a[r] - solve(l, r - 1));
  return memo;
}

int32_t main() {
#ifdef FASTIO
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#endif
  // freopen("in.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  memset(dp, -1, sizeof dp);

  cin >> n;
  forl(i, 0, n) { cin >> a[i]; }
  ll ans = solve(0, n - 1);
  cout << ans << "\n";

  return 0;
}