Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;

const int maxn = 305;
const int Mod = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e = exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;

#define Se second
#define Fi first
#define pb push_back
#define dbg(x) cout << #x << " = " << (x) << endl
#define dbg2(x1, x2)                                                           \
  cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << endl
#define dbg3(x1, x2, x3)                                                       \
  cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << " " << #x3        \
       << " = " << x3 << endl
ll dp[maxn][maxn][2];
ll a[maxn];
int main() {
  // ios::sync_with_stdio(false);
  // freopen("a.txt","r",stdin);
  // freopen("b.txt","w",stdout);
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = n; i >= 1; i--) {
    for (int j = i; j <= n; j++) {
      if (i == j) {
        dp[i][j][0] = a[i];
        dp[i][j][1] = -a[i];
      } else {
        dp[i][j][0] = max(dp[i + 1][j][1] + a[i], dp[i][j - 1][1] + a[j]);
        dp[i][j][1] = min(dp[i + 1][j][0] - a[i], dp[i][j - 1][0] - a[j]);
      }
    }
  }
  printf("%lld\n", dp[1][n][0]);
  // cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" <<
  // endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;

const int maxn = 3005;
const int Mod = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e = exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;

#define Se second
#define Fi first
#define pb push_back
#define dbg(x) cout << #x << " = " << (x) << endl
#define dbg2(x1, x2)                                                           \
  cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << endl
#define dbg3(x1, x2, x3)                                                       \
  cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << " " << #x3        \
       << " = " << x3 << endl
ll dp[maxn][maxn][2];
ll a[maxn];
int main() {
  // ios::sync_with_stdio(false);
  // freopen("a.txt","r",stdin);
  // freopen("b.txt","w",stdout);
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = n; i >= 1; i--) {
    for (int j = i; j <= n; j++) {
      if (i == j) {
        dp[i][j][0] = a[i];
        dp[i][j][1] = -a[i];
      } else {
        dp[i][j][0] = max(dp[i + 1][j][1] + a[i], dp[i][j - 1][1] + a[j]);
        dp[i][j][1] = min(dp[i + 1][j][0] - a[i], dp[i][j - 1][0] - a[j]);
      }
    }
  }
  printf("%lld\n", dp[1][n][0]);
  // cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" <<
  // endl;
  return 0;
}