prithivMLmods/Coder-Stat huggingface dataset:
Problem p03171 (C++)

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

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

const int N = 3e3 + 10;

pll dp[N][N];
int a[N];

pll rec(int l, int r) {
  if (l == r) {
    return {a[l], 0};
  } else {
    pll f = {0, 0};
    if (dp[l][r] != f)
      return dp[l][r];
    int x = a[l], y = a[r];
    pll p1 = rec(l + 1, r);
    pll p2 = rec(l, r - 1);

    swap(p1.first, p1.second);
    swap(p2.first, p2.second);

    p1.first += x;
    p2.first += y;
    return max(p1, p2);
  }
}

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  pll p = rec(0, n - 1);
  cout << p.first - p.second;
}

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

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

const int N = 3e3 + 10;

pll dp[N][N];
int a[N];

pll rec(int l, int r) {
  if (l == r) {
    return {a[l], 0};
  } else {
    pll f = {0, 0};
    if (dp[l][r] != f)
      return dp[l][r];
    int x = a[l], y = a[r];
    pll p1 = rec(l + 1, r);
    pll p2 = rec(l, r - 1);

    swap(p1.first, p1.second);
    swap(p2.first, p2.second);

    p1.first += x;
    p2.first += y;
    pll p = max(p1, p2);
    dp[l][r] = p;
    return p;
  }
}

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  pll p = rec(0, n - 1);
  cout << p.first - p.second;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n;
int a[3005];
ll dp[3005][3005];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int j = i; j < n; j++) {
      if (dp[i] == dp[j])
        dp[i][j] = a[i];
      else
        dp[i][j] = max(a[i] - dp[i - 1][j], a[j] - dp[i][j - 1]);
    }
  }
  cout << dp[0][n - 1] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n;
int a[3005];
ll dp[3005][3005];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int j = i; j < n; j++) {
      if (dp[i] == dp[j])
        dp[i][j] = a[i];
      else
        dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]);
    }
  }
  cout << dp[0][n - 1] << endl;

  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define MOD (int)1e9 + 7
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a, b) for (int i = a; i > b; --i)
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define pii pair<int, int>
#define pll pair<long, long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define PI 3.1415926535897932384626433832795
#define fix(f, n) fixed << setprecision(n) << f
#define all(x) x.begin(), x.end()

ll powM(ll a, ll b, ll m) {
  a %= m;
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = ans * a % m;
    a = a * a % m;
    b >>= 1;
  }

  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;

  vl a(n + 1);

  for (int i = 1; i <= n; i++)
    cin >> a[i];

  vvl dp(n + 2, vl(n + 1, 0));

  // dp[l][r]---all the cards from [l,r] have been distributed .

  for (int l = n; l >= 1; l--) {
    for (int r = l; r <= n; r++) {
      dp[l][r] = max(a[l] - dp[l + 1][r], a[r] - dp[l][r - 1]);
    }
  }

  cout << dp[3][4] << "\n";

  return 0;
}


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

using namespace std;

typedef long long ll;

#define MOD (int)1e9 + 7
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a, b) for (int i = a; i > b; --i)
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define pii pair<int, int>
#define pll pair<long, long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define PI 3.1415926535897932384626433832795
#define fix(f, n) fixed << setprecision(n) << f
#define all(x) x.begin(), x.end()

ll powM(ll a, ll b, ll m) {
  a %= m;
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = ans * a % m;
    a = a * a % m;
    b >>= 1;
  }

  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;

  vl a(n + 1);

  for (int i = 1; i <= n; i++)
    cin >> a[i];

  vvl dp(n + 2, vl(n + 1, 0));

  // dp[l][r]---all the cards from [l,r] have been distributed .

  for (int l = n; l >= 1; l--) {
    for (int r = l; r <= n; r++) {
      dp[l][r] = max(a[l] - dp[l + 1][r], a[r] - dp[l][r - 1]);
    }
  }

  cout << dp[1][n] << "\n";

  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#ifdef DEMETRIO
#define deb(...) fprintf(stderr, __VA_ARGS__)
#define deb1(x) cerr << #x << " = " << x << endl
#else
#define deb(...) 0
#define deb1(x) 0
#endif
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define fore(i, a, b) for (int i = a, ThxDem = b; i < ThxDem; ++i)
#define SZ(x) ((int)(x).size())
#define mset(a, v) memset(a, v, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long ll;

int n;
ll a[3005];
ll f[3005][3005];

int main() {
  scanf("%d", &n);
  fore(i, 0, n) scanf("%lld", a + i);
  fore(l, 1, n + 1) fore(i, 0, n) {
    int j = i + l;
    f[i][j] = max(a[i] - f[i + 1][j], a[j - 1] - f[i][j - 1]);
  }
  printf("%lld\n", f[0][n]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#ifdef DEMETRIO
#define deb(...) fprintf(stderr, __VA_ARGS__)
#define deb1(x) cerr << #x << " = " << x << endl
#else
#define deb(...) 0
#define deb1(x) 0
#endif
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define fore(i, a, b) for (int i = a, ThxDem = b; i < ThxDem; ++i)
#define SZ(x) ((int)(x).size())
#define mset(a, v) memset(a, v, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long ll;

int n;
ll a[3005];
ll f[3005][3005];

int main() {
  scanf("%d", &n);
  fore(i, 0, n) scanf("%lld", a + i);
  fore(l, 1, n + 1) fore(i, 0, n - l + 1) {
    int j = i + l;
    f[i][j] = max(a[i] - f[i + 1][j], a[j - 1] - f[i][j - 1]);
  }
  printf("%lld\n", f[0][n]);
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout << #x << " " << x << "\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define PI 3.141592653589
#define setbits(n) __builtin_popcountll(n)
const ll mod = 1e9 + 7;

const int N = 1e3 + 1;
ll dp[N][N][2];
vector<int> a(N);
ll n;

ll go(int l, int r, int mov) {
  if (l > r)
    return 0;
  if (dp[l][r][mov] != -1)
    return dp[l][r][mov];
  ll ans;
  if (mov == 0)
    ans = max(a[l] + go(l + 1, r, 1 ^ mov), a[r] + go(l, r - 1, 1 ^ mov));
  else
    ans = min(go(l + 1, r, 1 ^ mov) - a[l], go(l, r - 1, 1 ^ mov) - a[r]);
  return dp[l][r][mov] = ans;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll T = clock();

  cin >> n;
  memset(dp, -1, sizeof(dp));
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  cout << go(0, n - 1, 0);

  cerr << "\n\nTIME: " << (double)(clock() - T) / CLOCKS_PER_SEC << " sec\n";
  T = clock();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout << #x << " " << x << "\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define PI 3.141592653589
#define setbits(n) __builtin_popcountll(n)
const ll mod = 1e9 + 7;

const int N = 3e3 + 1;
ll dp[N][N][2];
vector<int> a(N);
ll n;

ll go(int l, int r, int mov) {
  if (l > r)
    return 0;
  if (dp[l][r][mov] != -1)
    return dp[l][r][mov];
  ll ans;
  if (mov == 0)
    ans = max(a[l] + go(l + 1, r, 1 ^ mov), a[r] + go(l, r - 1, 1 ^ mov));
  else
    ans = min(go(l + 1, r, 1 ^ mov) - a[l], go(l, r - 1, 1 ^ mov) - a[r]);
  return dp[l][r][mov] = ans;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll T = clock();

  cin >> n;
  memset(dp, -1, sizeof(dp));
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  cout << go(0, n - 1, 0);

  cerr << "\n\nTIME: " << (double)(clock() - T) / CLOCKS_PER_SEC << " sec\n";
  T = clock();
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <iomanip>
#include <iostream>
using namespace std;

const long long MAXN = 3001;

long long dp[MAXN][MAXN][2];
long long arr[MAXN];
long long f(long long x, long long y, long long turn) {
  if (dp[x][y][turn]) {
    return dp[x][y][turn];
  }
  if (x > y) {
    return 0;
  }
  // cout<<x<<" "<<y<<" "<<turn<<endl;
  if (turn % 2 == 1) {
    return dp[x][y][turn] = max(arr[x] + f(x + 1, y, 1 - turn),
                                arr[y] + f(x, y - 1, 1 - turn));
  } else {
    return dp[x][y][turn] = min(f(x + 1, y, 1 - turn) - arr[x],
                                f(x, y - 1, 1 - turn) - arr[y]);
  }
}
int main() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  cout << f(1, n, 1) << endl;
}


Fixed submission:
#include <iomanip>
#include <iostream>
using namespace std;

const long long MAXN = 3010;

long long dp[MAXN][MAXN][2];
long long arr[MAXN];
long long f(long long x, long long y, long long turn) {
  if (dp[x][y][turn]) {
    return dp[x][y][turn];
  }
  if (x > y) {
    return 0;
  }
  // cout<<x<<" "<<y<<" "<<turn<<endl;
  if (turn % 2 == 1) {
    return dp[x][y][turn] = max(arr[x] + f(x + 1, y, 1 - turn),
                                arr[y] + f(x, y - 1, 1 - turn));
  } else {
    return dp[x][y][turn] = min(f(x + 1, y, 1 - turn) - arr[x],
                                f(x, y - 1, 1 - turn) - arr[y]);
  }
}
int main() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  cout << f(1, n, 1) << endl;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define dbg(n) cout << #n << ' ' << n << endl;
ll dp[101][101];
ll solve(int i, int j, ll a[]) {
  if (i == j)
    return dp[i][j] = a[i];
  else if (j == i + 1)
    return dp[i][j] = max(a[i], a[j]);
  else if (dp[i][j] != 0)
    return dp[i][j];
  return dp[i][j] = max(a[i] + min(solve(i + 2, j, a), solve(i + 1, j - 1, a)),
                        a[j] + min(solve(i, j - 2, a), solve(i + 1, j - 1, a)));
}
int main() {
  for (int i = 0; i < 101; i++)
    for (int j = 0; j < 101; j++)
      dp[i][j] = 0;
  int n;
  cin >> n;
  ll a[n];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  ll mx = solve(0, n - 1, a);
  ll ans = 2 * mx - sum;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define dbg(n) cout << #n << ' ' << n << endl;
ll dp[3001][3001];
ll solve(int i, int j, ll a[]) {
  if (i == j)
    return dp[i][j] = a[i];
  else if (j == i + 1)
    return dp[i][j] = max(a[i], a[j]);
  else if (dp[i][j] != 0)
    return dp[i][j];
  return dp[i][j] = max(a[i] + min(solve(i + 2, j, a), solve(i + 1, j - 1, a)),
                        a[j] + min(solve(i, j - 2, a), solve(i + 1, j - 1, a)));
}
int main() {
  for (int i = 0; i < 101; i++)
    for (int j = 0; j < 101; j++)
      dp[i][j] = 0;
  int n;
  cin >> n;
  ll a[n];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  ll mx = solve(0, n - 1, a);
  ll ans = 2 * mx - sum;
  cout << ans << endl;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int MAX = 3005;
int64_t dp[MAX][MAX];
vector<int> vals(4);

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> vals[i];

  for (int L = n - 1; L >= 0; L--) {
    for (int R = L; R < n; R++) {
      if (L != R) {
        dp[L][R] = max(vals[L] - dp[L + 1][R], vals[R] - dp[L][R - 1]);
      } else {
        dp[L][R] = vals[L];
      }
    }
  }

  cout << dp[0][n - 1];
  return 0;
}


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

using namespace std;

const int MAX = 3005;
int64_t dp[MAX][MAX];
vector<int> vals(MAX, 0);

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> vals[i];

  for (int L = n - 1; L >= 0; L--) {
    for (int R = L; R < n; R++) {
      if (L != R) {
        dp[L][R] = max(vals[L] - dp[L + 1][R], vals[R] - dp[L][R - 1]);
      } else {
        dp[L][R] = vals[L];
      }
    }
  }

  cout << dp[0][n - 1];
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
/*input
6
4 2 9 7 1 5
*/

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int N = 3003;
vector<ll> v;
ll dp[N][N];

ll func(int l, int r) {
  if (l > r) {
    return 0;
  }
  if (dp[l][r] > -1) {
    return dp[l][r];
  }

  ll a = v[l] + min(func(l + 2, r), func(l + 1, r - 1));
  ll b = v[r] + min(func(l + 1, r - 1), func(l, r - 2));
  return max(a, b);
}

int main() {
  int n;
  cin >> n;
  v.resize(n);
  ll sum = 0;
  for (auto &x : v) {
    cin >> x;
    sum += x;
  }

  memset(dp, -1, sizeof(dp));
  ll d = func(0, n - 1);

  sum -= d;
  cout << d - sum;
  return 0;
}

Fixed submission:
/*input
6
4 2 9 7 1 5
*/

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int N = 3003;
vector<ll> v;
ll dp[N][N];

ll func(int l, int r) {
  if (l > r) {
    return 0;
  }
  if (dp[l][r] > -1) {
    return dp[l][r];
  }

  ll a = v[l] + min(func(l + 2, r), func(l + 1, r - 1));
  ll b = v[r] + min(func(l + 1, r - 1), func(l, r - 2));
  return dp[l][r] = max(a, b);
}

int main() {
  int n;
  cin >> n;
  v.resize(n);
  ll sum = 0;
  for (auto &x : v) {
    cin >> x;
    sum += x;
  }

  memset(dp, -1, sizeof(dp));
  ll d = func(0, n - 1);

  sum -= d;
  cout << d - sum;
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int mxN = 3000;
int n, a[mxN];
long long dp[mxN][mxN + 1];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int i = n - 1; i >= 0; --i) {
    for (int j = i + 1; j <= n; ++j) {
      dp[i][j] = max(a[i] - dp[i + 1][j], a[j - 1] - dp[i][j - 1]);
    }
  }
  cout << dp[0][n];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int mxN = 3000;
int n, a[mxN];
long long dp[mxN + 1][mxN + 1];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int i = n - 1; i >= 0; --i) {
    for (int j = i + 1; j <= n; ++j) {
      dp[i][j] = max(a[i] - dp[i + 1][j], a[j - 1] - dp[i][j - 1]);
    }
  }
  cout << dp[0][n];
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define vi vector<int>
#define rep(i, xtz, n) for (int i = xtz; i < n; i++)
#define repx(i, xtz, n) for (int i = xtz; i >= n; i--)
#define lli long long int

using namespace std;

lli dp[305][305];

int main() {
  // freopen("i.in", "r", stdin);
  int n;
  scanf("%d", &n);
  vector<lli> vec(n);
  rep(i, 0, n) scanf("%d", &vec[i]);
  repx(L, n - 1, 0) {
    rep(R, L, n) {
      if (L == R)
        dp[L][R] = vec[L];
      else {
        dp[L][R] = max(vec[L] - dp[L + 1][R], vec[R] - dp[L][R - 1]);
      }
    }
  }
  cout << dp[0][n - 1];
}

Fixed submission:
#include <bits/stdc++.h>
#define vi vector<int>
#define rep(i, xtz, n) for (int i = xtz; i < n; i++)
#define repx(i, xtz, n) for (int i = xtz; i >= n; i--)
#define lli long long int

using namespace std;

lli dp[3005][3005];

int main() {
  // freopen("i.in", "r", stdin);
  int n;
  scanf("%d", &n);
  vector<lli> vec(n);
  rep(i, 0, n) scanf("%d", &vec[i]);
  repx(L, n - 1, 0) {
    rep(R, L, n) {
      if (L == R)
        dp[L][R] = vec[L];
      else {
        dp[L][R] = max(vec[L] - dp[L + 1][R], vec[R] - dp[L][R - 1]);
      }
    }
  }
  cout << dp[0][n - 1];
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize "trapv"
#include <bits/stdc++.h>
#define faster                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
using namespace std;
#define ll long long
#define pb push_back
#define fo(i, a, b) for (int i = a; i < b; i++)
#define foe(i, a, b) for (int i = a; i <= b; i++)
ll dp[1000][1000];
ll fundp(int i, int j, ll *a) {
  // cout<<i<<" "<<j<<endl;
  if (dp[i][j] != 1000000000000001)
    return dp[i][j];
  if (i == j)
    dp[i][j] = a[i];
  else {
    dp[i][j] = max(a[i] - fundp(i + 1, j, a), a[j] - fundp(i, j - 1, a));
  }
  return dp[i][j];
}
void solve() {
  int n;
  cin >> n;
  ll a[n];
  fo(i, 0, n) cin >> a[i];
  fo(i, 0, n) { fo(j, 0, n) dp[i][j] = 1000000000000001; }
  cout << fundp(0, n - 1, a) << endl;
}
signed main() {
  faster;
  // int t;cin>>t;
  // while(t--)
  solve();
}

Fixed submission:
#pragma GCC optimize "trapv"
#include <bits/stdc++.h>
#define faster                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
using namespace std;
#define ll long long
#define pb push_back
#define fo(i, a, b) for (int i = a; i < b; i++)
#define foe(i, a, b) for (int i = a; i <= b; i++)
ll dp[3000][3000];
ll fundp(int i, int j, ll *a) {
  // cout<<i<<" "<<j<<endl;
  if (dp[i][j] != 1000000000000001)
    return dp[i][j];
  if (i == j)
    dp[i][j] = a[i];
  else {
    dp[i][j] = max(a[i] - fundp(i + 1, j, a), a[j] - fundp(i, j - 1, a));
  }
  return dp[i][j];
}
void solve() {
  int n;
  cin >> n;
  ll a[n];
  fo(i, 0, n) cin >> a[i];
  fo(i, 0, n) { fo(j, 0, n) dp[i][j] = 1000000000000001; }
  cout << fundp(0, n - 1, a) << endl;
}
signed main() {
  faster;
  // int t;cin>>t;
  // while(t--)
  solve();
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
// #define INF 1145141919810893364
// #define INF 810114514
#define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \
  do {                                                                         \
    cout << "0" << endl;                                                       \
    return 0;                                                                  \
  } while (false)
#define MM << " " <<
#define Endl endl
#define debug false

int N;
vector<int> A;

const int MEM = 3030;
int dp[MEM][MEM];

int dequeue(int l, int r) {
  if (dp[l][r] != -1) {
    return dp[l][r];
  }
  if (l >= r) {
    return A[l];
  }

  return max(A[l] - dequeue(l + 1, r), A[r] - dequeue(l, r - 1));
}

signed main(void) {
  int a;
  cin >> N;
  REP(i, N) {
    cin >> a;
    A.push_back(a);
  }

  REP(i, MEM) {
    REP(j, MEM) { dp[i][j] = -1; }
  }

  cout << dequeue(0, N - 1) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
// #define INF 1145141919810893364
// #define INF 810114514
#define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \
  do {                                                                         \
    cout << "0" << endl;                                                       \
    return 0;                                                                  \
  } while (false)
#define MM << " " <<
#define Endl endl
#define debug false

int N;
vector<int> A;

const int MEM = 3030;
int dp[MEM][MEM];

int dequeue(int l, int r) {
  if (dp[l][r] != -1) {
    return dp[l][r];
  }
  if (l >= r) {
    return A[l];
  }
  int k = max(A[l] - dequeue(l + 1, r), A[r] - dequeue(l, r - 1));
  dp[l][r] = k;
  return k;
}

signed main(void) {
  int a;
  cin >> N;
  REP(i, N) {
    cin >> a;
    A.push_back(a);
  }

  REP(i, MEM) {
    REP(j, MEM) { dp[i][j] = -1; }
  }

  cout << dequeue(0, N - 1) << endl;

  return 0;
}
Problem p03171 (C++)

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

int n;
vector<int> a;
vector<vector<bool>> vis;
vector<vector<int64_t>> dp;
int64_t go(int i, int j, int p) {
  if (vis[i][j])
    return dp[i][j];
  if (i == j)
    return dp[i][j] = p * a[i];
  int64_t x = go(i + 1, j, -p) + p * a[i];
  int64_t y = go(i, j - 1, -p) + p * a[j];
  return dp[i][j] = p * x > p * y ? x : y;
}

int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  cin >> n;
  a.resize(n);
  vis.assign(n, vector<bool>(n, false));
  dp.assign(n, vector<int64_t>(n));
  for (int &it : a)
    cin >> it;
  // int64_t ans = go(0, n - 1, 1);
  cout << go(0, n - 1, 1) << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int n;
vector<int> a;
vector<vector<bool>> vis;
vector<vector<int64_t>> dp;
int64_t go(int i, int j, int p) {
  if (vis[i][j])
    return dp[i][j];
  if (i == j)
    return dp[i][j] = p * a[i];
  vis[i][j] = true;
  int64_t x = go(i + 1, j, -p) + p * a[i];
  int64_t y = go(i, j - 1, -p) + p * a[j];
  return dp[i][j] = p * x > p * y ? x : y;
}

int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  cin >> n;
  a.resize(n);
  vis.assign(n, vector<bool>(n, false));
  dp.assign(n, vector<int64_t>(n));
  for (int &it : a)
    cin >> it;
  // int64_t ans = go(0, n - 1, 1);
  cout << go(0, n - 1, 1) << '\n';
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
const ll N = 1e2 + 1;
ll n, k;
ll arr[N][N];
bool kek[N][N];
ll a[N];
ll recurs(ll l = 0, ll r = n - 1) {
  if (l == r) {
    arr[l][r] = a[l];
    return a[l];
  }
  if (kek[l][r])
    return arr[l][r];
  kek[l][r] = 1;
  arr[l][r] = max(a[l] - recurs(l + 1, r), a[r] - recurs(l, r - 1));
  return arr[l][r];
}

int main() {
  speed;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  recurs();
  cout << arr[0][n - 1];
}
/*
Timus : 288471KD
Atcoder : Zhabka
Codeforces : Zhabka
mail : dzhon.snoudin@mail.ru
*/


Fixed submission:
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
const ll N = 3e3 + 1;
ll n, k;
ll arr[N][N];
bool kek[N][N];
ll a[N];
ll recurs(ll l = 0, ll r = n - 1) {
  if (l == r) {
    arr[l][r] = a[l];
    return a[l];
  }
  if (kek[l][r])
    return arr[l][r];
  kek[l][r] = 1;
  arr[l][r] = max(a[l] - recurs(l + 1, r), a[r] - recurs(l, r - 1));
  return arr[l][r];
}

int main() {
  speed;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  recurs();
  cout << arr[0][n - 1];
}
/*
Timus : 288471KD
Atcoder : Zhabka
Codeforces : Zhabka
mail : dzhon.snoudin@mail.ru
*/
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define ld long double
#define MAX 100001
#define MOD 1000000007
using namespace std;

int dp[1001][1001][2];

void play(int s, int e, int turn, int *a) {
  if (dp[s][e][turn] != -1)
    return;
  if (s == e) {
    dp[s][e][turn] = a[s];
    dp[s][e][turn ^ 1] = 0;
    return;
  }
  play(s + 1, e, turn ^ 1, a);
  play(s, e - 1, turn ^ 1, a);

  if (dp[s + 1][e][turn] + a[s] > dp[s][e - 1][turn] + a[e]) {
    dp[s][e][turn] = dp[s + 1][e][turn] + a[s];
    dp[s][e][turn ^ 1] = dp[s + 1][e][turn ^ 1];
  } else {
    dp[s][e][turn] = dp[s][e - 1][turn] + a[e];
    dp[s][e][turn ^ 1] = dp[s][e - 1][turn ^ 1];
  }
  return;
}

int32_t main() {
  int n;
  cin >> n;
  int *a = new int[n];
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  memset(dp, -1, sizeof(dp));

  play(0, n - 1, 0, a);

  cout << dp[0][n - 1][0] - dp[0][n - 1][1] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define ld long double
#define MAX 100001
#define MOD 1000000007
using namespace std;

int dp[3001][3001][2];

void play(int s, int e, int turn, int *a) {
  if (dp[s][e][turn] != -1)
    return;
  if (s == e) {
    dp[s][e][turn] = a[s];
    dp[s][e][turn ^ 1] = 0;
    return;
  }
  play(s + 1, e, turn ^ 1, a);
  play(s, e - 1, turn ^ 1, a);

  if (dp[s + 1][e][turn] + a[s] > dp[s][e - 1][turn] + a[e]) {
    dp[s][e][turn] = dp[s + 1][e][turn] + a[s];
    dp[s][e][turn ^ 1] = dp[s + 1][e][turn ^ 1];
  } else {
    dp[s][e][turn] = dp[s][e - 1][turn] + a[e];
    dp[s][e][turn ^ 1] = dp[s][e - 1][turn ^ 1];
  }
  return;
}

int32_t main() {
  int n;
  cin >> n;
  int *a = new int[n];
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  memset(dp, -1, sizeof(dp));

  play(0, n - 1, 0, a);

  cout << dp[0][n - 1][0] - dp[0][n - 1][1] << endl;

  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long n, a[3001], dp[3001][3001];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == 1)
        dp[i][j] = a[j];
      else if (i == 2)
        dp[i][j] = abs(a[j] - a[j + 1]);
      else {
        int l = j;
        int r = i + j - 1;
        long long t1 = a[r] - dp[i - 1][j];
        long long t2 = a[l] - dp[i - 1][j + 1];
        dp[i][j] = max(t1, t2);
      }
    }
  }
  cout << dp[n][1];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, a[3001], dp[3001][3001];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i + j - 1 > n)
        break;
      if (i == 1)
        dp[i][j] = a[j];
      else if (i == 2)
        dp[i][j] = abs(a[j] - a[j + 1]);
      else {
        int l = j;
        int r = i + j - 1;
        long long t1 = a[r] - dp[i - 1][j];
        long long t2 = a[l] - dp[i - 1][j + 1];
        dp[i][j] = max(t1, t2);
      }
    }
  }
  cout << dp[n][1];
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define tani_nachi_ke                                                          \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define M_PI 3.14159265358979323846
#define data data_
#define ff first
#define ss second
int const N = 3009;
long long dp[N][N];
long long a[N];
long long fun(int i, int j) {
  if (i == j)
    return a[i];
  else if (j == i + 1)
    return (a[i] > a[j] ? (a[i] - a[j]) : (a[j] - a[i]));
  else if (dp[i][j] != -1)
    return dp[i][j];
  else
    return max(a[i] + min(-a[i + 1] + fun(i + 2, j), -a[j] + fun(i + 1, j - 1)),
               a[j] +
                   min(-a[i] + fun(i + 1, j - 1), -a[j - 1] + fun(i, j - 2)));
}
int main() {
  // clock_t begin = clock();
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  tani_nachi_ke

      int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  memset(dp, -1, sizeof(dp));
  cout << fun(1, n) << endl;

  // #ifndef ONLINE_JUDGE
  // clock_t end = clock();
  // cout<<endl<<double(end - begin) / CLOCKS_PER_SEC*1000<<" ms";
  // #endif
  // return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define tani_nachi_ke                                                          \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define M_PI 3.14159265358979323846
#define data data_
#define ff first
#define ss second
int const N = 3009;
long long dp[N][N];
long long a[N];
long long fun(int i, int j) {
  if (i == j)
    return a[i];
  else if (j == i + 1)
    return (a[i] > a[j] ? (a[i] - a[j]) : (a[j] - a[i]));
  else if (dp[i][j] != -1)
    return dp[i][j];
  else
    return dp[i][j] = max(
               a[i] + min(-a[i + 1] + fun(i + 2, j), -a[j] + fun(i + 1, j - 1)),
               a[j] +
                   min(-a[i] + fun(i + 1, j - 1), -a[j - 1] + fun(i, j - 2)));
}
int main() {
  // clock_t begin = clock();
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  tani_nachi_ke

      int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  memset(dp, -1, sizeof(dp));
  cout << fun(1, n) << endl;

  // #ifndef ONLINE_JUDGE
  // clock_t end = clock();
  // cout<<endl<<double(end - begin) / CLOCKS_PER_SEC*1000<<" ms";
  // #endif
  // return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define MAX_N 3000
typedef long long int ll;

int n;
ll a[MAX_N];

ll dp[MAX_N + 1][MAX_N + 1];

int main() {
  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    std::cin >> a[i];
  }

  for (int i = 0; i <= n; ++i) {
    dp[i][i] = 0;
  }
  for (int len = 1; len <= n; ++len) {
    for (int i = 0; i < n; ++i) {
      int j = i + len;
      if ((n - len) % 2 == 0) {
        dp[i][j] = std::max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j - 1]);
      } else {
        dp[i][j] = std::min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j - 1]);
      }
    }
  }

  std::cout << dp[0][n] << std::endl;
}


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

#define MAX_N 3000
typedef long long int ll;

int n;
ll a[MAX_N];

ll dp[MAX_N + 1][MAX_N + 1];

int main() {
  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    std::cin >> a[i];
  }

  for (int i = 0; i <= n; ++i) {
    dp[i][i] = 0;
  }
  for (int len = 1; len <= n; ++len) {
    for (int i = 0; i + len <= n; ++i) {
      int j = i + len;
      if ((n - len) % 2 == 0) {
        dp[i][j] = std::max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j - 1]);
      } else {
        dp[i][j] = std::min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j - 1]);
      }
    }
  }

  std::cout << dp[0][n] << std::endl;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define f(i, n) for (ll i = 0; i < n; i++)
#define fz for (ll j = i + 1; j < n; j++)
#define f2(i, n) for (ll i = 1; i <= n; i++)
#define endl "\n"
#define vll vector<ll>
#define sz(a) (ll) a.size()
#define mod 1000000007
#define pb push_back
#define inf 1e13 + 5

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll fexp(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    b /= 2;
    a = a * a % mod;
  }
  return ans;
}
ll inverse(ll a, ll p) { return fexp(a, p - 2); }

ll dp[303][303];
ll game(ll *arr, ll l, ll r) {
  if (l > r)
    return 0;
  if (l == r)
    return arr[l];
  if (dp[l][r] != -(ll)1e18)
    return dp[l][r];
  return (dp[l][r] =
              max(arr[l] - game(arr, l + 1, r), arr[r] - game(arr, l, r - 1)));
}

void solve() {
  ll n;
  cin >> n;
  ll a[n];
  f(i, n) cin >> a[i];
  f(i, n) { f(j, n) dp[i][j] = -(ll)1e18; }
  ll ans = game(a, 0, n - 1);
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define f(i, n) for (ll i = 0; i < n; i++)
#define fz for (ll j = i + 1; j < n; j++)
#define f2(i, n) for (ll i = 1; i <= n; i++)
#define endl "\n"
#define vll vector<ll>
#define sz(a) (ll) a.size()
#define mod 1000000007
#define pb push_back
#define inf 1e13 + 5

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll fexp(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    b /= 2;
    a = a * a % mod;
  }
  return ans;
}
ll inverse(ll a, ll p) { return fexp(a, p - 2); }

ll dp[3003][3003];
ll game(ll *arr, ll l, ll r) {
  if (l > r)
    return 0;
  if (l == r)
    return arr[l];
  if (dp[l][r] != -(ll)1e18)
    return dp[l][r];
  return (dp[l][r] =
              max(arr[l] - game(arr, l + 1, r), arr[r] - game(arr, l, r - 1)));
}

void solve() {
  ll n;
  cin >> n;
  ll a[n];
  f(i, n) cin >> a[i];
  f(i, n) { f(j, n) dp[i][j] = -(ll)1e18; }
  ll ans = game(a, 0, n - 1);
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <list>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define MOD 1000000007
#define PI 3.14159265358979323846
#define INF 1 << 30

using namespace std;
typedef long long ll;
typedef pair<int, int> pp;

ll Search(vector<vector<ll>> &dp, vector<vector<char>> &ok, const vector<ll> &d,
          int l, int r, char p = 0) {
  if (l > r)
    return 0;
  if (ok[l][r])
    dp[l][r];
  ll a, b;
  a = Search(dp, ok, d, l + 1, r, !p);
  b = Search(dp, ok, d, l, r - 1, !p);
  ok[l][r] = 1;
  return dp[l][r] = p ? min(a - d[l], b - d[r]) : max(a + d[l], b + d[r]);
}

int main(void) {
  int num, i;
  ll ans;
  cin >> num;
  vector<ll> d(num);
  vector<vector<char>> ok(num, vector<char>(num, 0));
  vector<vector<ll>> dp(num, vector<ll>(num, 0));
  rep(i, num) cin >> d[i];
  ans = Search(dp, ok, d, 0, num - 1);
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <list>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define MOD 1000000007
#define PI 3.14159265358979323846
#define INF 1 << 30

using namespace std;
typedef long long ll;
typedef pair<int, int> pp;

ll Search(vector<vector<ll>> &dp, vector<vector<char>> &ok, const vector<ll> &d,
          int l, int r, char p = 0) {
  if (l > r)
    return 0;
  if (ok[l][r])
    return dp[l][r];
  ll a, b;
  a = Search(dp, ok, d, l + 1, r, !p);
  b = Search(dp, ok, d, l, r - 1, !p);
  ok[l][r] = 1;
  return dp[l][r] = p ? min(a - d[l], b - d[r]) : max(a + d[l], b + d[r]);
}

int main(void) {
  int num, i;
  ll ans;
  cin >> num;
  vector<ll> d(num);
  vector<vector<char>> ok(num, vector<char>(num, 0));
  vector<vector<ll>> dp(num, vector<ll>(num, 0));
  rep(i, num) cin >> d[i];
  ans = Search(dp, ok, d, 0, num - 1);
  cout << ans << "\n";
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll maxn = 1001;
ll n, a[maxn], memo[3001][3001][2];

ll go(ll i, ll j, ll par) {
  if (i > j) {
    return 0;
  }
  ll ans = 0;
  if (memo[i][j][par] != -1)
    return memo[i][j][par];
  if (par == 0) {
    ans += max(a[i] + go(i + 1, j, !par), a[j] + go(i, j - 1, !par));
  } else {
    ans += min(go(i + 1, j, !par) - a[i], go(i, j - 1, !par) - a[j]);
  }
  return memo[i][j][par] = ans;
}

int main() {
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  memset(memo, -1, sizeof(memo));
  cout << go(0, n - 1, 0);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll maxn = 3001;
ll n, a[maxn], memo[maxn][maxn][2];

ll go(ll i, ll j, ll par) {
  if (i > j) {
    return 0;
  }
  ll ans = 0;
  if (memo[i][j][par] != -1)
    return memo[i][j][par];
  if (par == 0) {
    ans += max(a[i] + go(i + 1, j, !par), a[j] + go(i, j - 1, !par));
  } else {
    ans += min(go(i + 1, j, !par) - a[i], go(i, j - 1, !par) - a[j]);
  }
  return memo[i][j][par] = ans;
}

int main() {
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  memset(memo, -1, sizeof(memo));
  cout << go(0, n - 1, 0);
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FASTIO                                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false);
#define loop(i, a, b) for (int i = a; i < b; i++)
#define INF 10000000000099ll
#define mod 1000000007
#define prDouble(x) cout << fixed << setprecision(10) << x
ll dp[3001][3001];

ll solve(vector<int> &arr, int i, int j) {
  if (i > j)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  ll opt1 = arr[i] + min(solve(arr, i + 1, j - 1), solve(arr, i + 2, j));
  ll opt2 = arr[j] + min(solve(arr, i, j - 2), solve(arr, i + 1, j - 1));
  return max(opt1, opt2);
}

int main() {
  FASTIO;
  int n;
  ll sum = 0;
  cin >> n;
  memset(dp, -1, sizeof dp);
  vector<int> arr(n + 1);
  loop(i, 1, n + 1) {
    cin >> arr[i];
    sum += arr[i];
  }
  ll x = solve(arr, 1, n);
  cout << 2 * x - sum;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FASTIO                                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false);
#define loop(i, a, b) for (int i = a; i < b; i++)
#define INF 10000000000099ll
#define mod 1000000007
#define prDouble(x) cout << fixed << setprecision(10) << x
ll dp[3001][3001];

ll solve(vector<int> &arr, int i, int j) {
  if (i > j)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  ll opt1 = arr[i] + min(solve(arr, i + 1, j - 1), solve(arr, i + 2, j));
  ll opt2 = arr[j] + min(solve(arr, i, j - 2), solve(arr, i + 1, j - 1));
  return dp[i][j] = max(opt1, opt2);
}

int main() {
  FASTIO;
  int n;
  ll sum = 0;
  cin >> n;
  memset(dp, -1, sizeof dp);
  vector<int> arr(n + 1);
  loop(i, 1, n + 1) {
    cin >> arr[i];
    sum += arr[i];
  }
  ll x = solve(arr, 1, n);
  cout << 2 * x - sum;
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define se second
#define fr first
#define int long long int
#define pb push_back
#define inf 1e18
#define all(v) v.begin(), v.end()
#define CHAL_BAAP_KO_MT_SIKHA                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mod 1000000007
/*
    Damn Fast:TheFuckinMastermind
    while(How to solve the Question?){
                Read the Question Again!!
           }
        do
            Practise
    #Motivation::::0/0
*/
const int N = 2e3;
int dp[N + 1][N + 1];
int n, a[N + 1];
int solve(int i, int j) {
  if (i > n or j <= 0) {
    return 0;
  }
  if (j < i)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  int ans1 = a[i] + min(solve(i + 2, j), solve(i + 1, j - 1));
  int ans2 = a[j] + min(solve(i + 1, j - 1), solve(i, j - 2));
  return dp[i][j] = max(ans1, ans2);
}
signed main() {

  CHAL_BAAP_KO_MT_SIKHA
  cin >> n;
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  memset(dp, -1, sizeof(dp));
  int x = solve(1, n);
  int y = sum - x;
  cout << x - y << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define se second
#define fr first
#define int long long int
#define pb push_back
#define inf 1e18
#define all(v) v.begin(), v.end()
#define CHAL_BAAP_KO_MT_SIKHA                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mod 1000000007
/*
    Damn Fast:TheFuckinMastermind
    while(How to solve the Question?){
                Read the Question Again!!
           }
        do
            Practise
    #Motivation::::0/0
*/
const int N = 4e3;
int dp[N + 1][N + 1];
int n, a[N + 1];
int solve(int i, int j) {
  if (i > n or j <= 0) {
    return 0;
  }
  if (j < i)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  int ans1 = a[i] + min(solve(i + 2, j), solve(i + 1, j - 1));
  int ans2 = a[j] + min(solve(i + 1, j - 1), solve(i, j - 2));
  return dp[i][j] = max(ans1, ans2);
}
signed main() {

  CHAL_BAAP_KO_MT_SIKHA
  cin >> n;
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  memset(dp, -1, sizeof(dp));
  int x = solve(1, n);
  int y = sum - x;
  cout << x - y << endl;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#pragma region template 1.0
#pragma region def
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> ii;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REP1(i, n) for (ll i = 1; i <= (n); ++i)
#define OUT(x) cout << (x) << endl;
#define OUTA(a)                                                                \
  REP(i, (a).size()) { cout << (a[i]) << (i == (a).size() - 1 ? "\n" : " "); }
#define ALL(a) (a).begin(), (a).end()
#define SORT(a) sort(ALL(a))
#define RSORT(a)                                                               \
  SORT(a);                                                                     \
  reverse(ALL(a))

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
#pragma endregion enddef

int main() {
  ll N;
  cin >> N;
  vi a(N);
  REP(i, N) { cin >> a[i]; }

  vector<vi> dp(N + 1, vi(N + 1));
  REP1(i, N) {
    REP1(j, i + 1) {
      if ((N - j) & 1) {
        dp[i][j] = min(dp[i - 1][j - 1] - a[i - 1], dp[i][j - 1] - a[i - j]);
      } else {
        dp[i][j] = max(dp[i - 1][j - 1] + a[i - 1], dp[i][j - 1] + a[i - j]);
      }
    }
  }
  ll ans = dp[N][N];
  OUT(ans);
}
#pragma endregion template

Fixed submission:
#pragma region template 1.0
#pragma region def
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> ii;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REP1(i, n) for (ll i = 1; i <= (n); ++i)
#define OUT(x) cout << (x) << endl;
#define OUTA(a)                                                                \
  REP(i, (a).size()) { cout << (a[i]) << (i == (a).size() - 1 ? "\n" : " "); }
#define ALL(a) (a).begin(), (a).end()
#define SORT(a) sort(ALL(a))
#define RSORT(a)                                                               \
  SORT(a);                                                                     \
  reverse(ALL(a))

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
#pragma endregion enddef

int main() {
  ll N;
  cin >> N;
  vi a(N);
  REP(i, N) { cin >> a[i]; }

  vector<vi> dp(N + 1, vi(N + 1));
  REP1(i, N) {
    REP1(j, i) {
      if ((N - j) & 1) {
        dp[i][j] = min(dp[i - 1][j - 1] - a[i - 1], dp[i][j - 1] - a[i - j]);
      } else {
        dp[i][j] = max(dp[i - 1][j - 1] + a[i - 1], dp[i][j - 1] + a[i - j]);
      }
    }
  }
  ll ans = dp[N][N];
  OUT(ans);
}
#pragma endregion template
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define pb push_back
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()

ll stoneGame(vector<ll> &piles) {
  // cout << piles[0] << " ";
  ll n = piles.size();
  vector<vector<ll>> dp(n, vector<ll>(n, 0));
  for (int i = 0; i < n; i++)
    dp[i][i] = piles[i];
  for (int d = 1; d < n; d++) {
    for (int i = 0; i < n - d; i++) {
      dp[i][i + d] =
          max(piles[i] - dp[i + 1][i + d], piles[i + d] - dp[i][i + d - 1]);
    }
  }
  return dp[0][n - 1];
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  std::vector<ll> v(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  cout << stoneGame(v) << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define pb push_back
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()

ll stoneGame(vector<ll> &piles) {
  // cout << piles[0] << " ";
  ll n = piles.size();
  vector<vector<ll>> dp(n, vector<ll>(n, 0));
  for (int i = 0; i < n; i++)
    dp[i][i] = piles[i];
  for (int d = 1; d < n; d++) {
    for (int i = 0; i < n - d; i++) {
      dp[i][i + d] =
          max(piles[i] - dp[i + 1][i + d], piles[i + d] - dp[i][i + d - 1]);
    }
  }
  return dp[0][n - 1];
}

int main() {
  int n;
  cin >> n;
  std::vector<ll> v(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  cout << stoneGame(v) << "\n";

  return 0;
}
Problem p03171 (C++)

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

using namespace std;
const int maxn = 3e3 + 5, mod = 1e9 + 7;
int n, c[maxn][maxn], a[maxn];
long long f[maxn][maxn];

long long dp(int l, int r) {
  if (l > r)
    return 0;
  long long &res = f[l][r];
  if (c[l][r])
    return res;
  int turn = (l + (n - r + 1)) % 2;
  if (turn) {
    res = 1e13;
    res = min(dp(l + 1, r) - a[l], res);
    res = min(dp(l, r - 1) - a[r], res);
  } else {
    res = -1e13;
    res = max(dp(l + 1, r) + a[l], res);
    res = max(dp(l, r - 1) + a[r], res);
  }
  return res;
}

int main() {
#ifdef NTMA
  freopen("abc.inp", "r", stdin);
#endif
  // memset(f, 255, sizeof(f));
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  cout << dp(1, n);
  return 0;
}

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

using namespace std;
const int maxn = 3e3 + 5, mod = 1e9 + 7;
int n, c[maxn][maxn], a[maxn];
long long f[maxn][maxn];

long long dp(int l, int r) {
  if (l > r)
    return 0;
  long long &res = f[l][r];
  if (c[l][r])
    return res;
  c[l][r] = true;
  int turn = (l + (n - r + 1)) % 2;
  if (turn) {
    res = 1e13;
    res = min(dp(l + 1, r) - a[l], res);
    res = min(dp(l, r - 1) - a[r], res);
  } else {
    res = -1e13;
    res = max(dp(l + 1, r) + a[l], res);
    res = max(dp(l, r - 1) + a[r], res);
  }
  return res;
}

int main() {
#ifdef NTMA
  freopen("abc.inp", "r", stdin);
#endif
  // memset(f, 255, sizeof(f));
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  cout << dp(1, n);
  return 0;
}