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;

ll solve(vector<vector<ll>> &dp, vector<ll> &a, int head, int tail) {
  if (head >= tail) {
    return dp[head][tail] = a[head];
  }

  ll best = max(-solve(dp, a, head, tail - 1) + a[tail],
                -solve(dp, a, head + 1, tail) + a[head]);

  return dp[head][tail] = best;
}

int main() {
  int N;
  cin >> N;
  vector<ll> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  vector<vector<ll>> dp(N, vector<ll>(N));
  cout << solve(dp, a, 0, a.size() - 1) << endl;

  return 0;
}


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

ll solve(vector<vector<ll>> &dp, vector<ll> &a, int head, int tail) {
  if (head >= tail) {
    return dp[head][tail] = a[head];
  }
  if (dp[head][tail] != 0)
    return dp[head][tail];

  ll best = max(-solve(dp, a, head, tail - 1) + a[tail],
                -solve(dp, a, head + 1, tail) + a[head]);

  return dp[head][tail] = best;
}

int main() {
  int N;
  cin >> N;
  vector<ll> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  vector<vector<ll>> dp(N, vector<ll>(N));
  cout << solve(dp, a, 0, a.size() - 1) << endl;

  return 0;
}
Problem p03171 (C++)

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

using namespace std;

#define lli long long int
#define REP(i, s, l) for (lli i = s; i < l; i++)
#define DEBUG 0
#define INF (1LL << 50)

lli dp[3010][3010];
lli vis[3010][3010];
lli n;
vector<lli> a;

lli f(lli l, lli r) {
  if (l > r)
    return 0;
  if (vis[l][r])
    return dp[l][r];

  lli diff = n - (r - l + 1);
  lli res;
  if (diff % 2 == 0) {
    res = -INF;
    res = max(res, f(l + 1, r) + a[l]);
    res = max(res, f(l, r - 1) + a[r]);
  } else {
    res = INF;
    res = min(res, f(l + 1, r) - a[l]);
    res = min(res, f(l, r - 1) - a[r]);
  }
  return dp[l][r] = res;
}

signed main() {

  cin >> n;
  a.resize(n);
  REP(i, 0, n) cin >> a[i];

  REP(i, 0, 3010) REP(j, 0, 3010) {
    dp[i][j] = 0;
    vis[i][j] = 0;
  }
  cout << f(0, n - 1) << endl;

  return 0;
}

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

using namespace std;

#define lli long long int
#define REP(i, s, l) for (lli i = s; i < l; i++)
#define DEBUG 0
#define INF (1LL << 50)

lli dp[3010][3010];
lli vis[3010][3010];
lli n;
vector<lli> a;

lli f(lli l, lli r) {
  if (l > r)
    return 0;
  if (vis[l][r])
    return dp[l][r];

  lli diff = n - (r - l + 1);
  lli res;
  if (diff % 2 == 0) {
    res = -INF;
    res = max(res, f(l + 1, r) + a[l]);
    res = max(res, f(l, r - 1) + a[r]);
  } else {
    res = INF;
    res = min(res, f(l + 1, r) - a[l]);
    res = min(res, f(l, r - 1) - a[r]);
  }
  vis[l][r] = 1;
  return dp[l][r] = res;
}

signed main() {

  cin >> n;
  a.resize(n);
  REP(i, 0, n) cin >> a[i];

  REP(i, 0, 3010) REP(j, 0, 3010) {
    dp[i][j] = 0;
    vis[i][j] = 0;
  }
  cout << f(0, n - 1) << endl;

  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 3;
long long dp[N][N][2];
int a[N];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    dp[i][i][0] = dp[i][i][1] = a[i];
  }
  for (int len = 1; len < n; ++len)
    for (int st = 0; (st + len) < n; ++st) {
      int ed = st + len;
      dp[st][ed][0] = max(a[st] - dp[st + 1][ed][1], a[ed] - dp[st][ed - 1][1]);
      dp[st][ed][1] = max(a[st] - dp[st + 1][ed][0], a[ed] - dp[st][ed - 1][0]);
    }
  cout << dp[0][n - 1][0];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 3e3 + 3;
long long dp[N][N][2];
int a[N];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    dp[i][i][0] = dp[i][i][1] = a[i];
  }
  for (int len = 1; len < n; ++len)
    for (int st = 0; (st + len) < n; ++st) {
      int ed = st + len;
      dp[st][ed][0] = max(a[st] - dp[st + 1][ed][1], a[ed] - dp[st][ed - 1][1]);
      dp[st][ed][1] = max(a[st] - dp[st + 1][ed][0], a[ed] - dp[st][ed - 1][0]);
    }
  cout << dp[0][n - 1][0];
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define debug(n, v)                                                            \
  cout << #v << ":";                                                           \
  for (int i = 0; i < n; i++)                                                  \
    cout << v[i] << " ";                                                       \
  cout << endl;
#define INF 1000000000
#define mod 1000000007
typedef long long ll;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int n;
ll a[3001];
ll dp[3001][3001];
bool f[3001][3001];
ll solve(int l, int r) {
  if (f[l][r])
    return dp[l][r];
  if ((l + n - 1 - r) % 2 == 0) {
    if (l == r)
      return dp[l][r] = a[l];
    return dp[l][r] = max(a[l] + solve(l + 1, r), a[r] + solve(l, r - 1));
  } else {
    if (l == r)
      return dp[l][r] = -a[l];
    return dp[l][r] = min(solve(l + 1, r) - a[l], solve(l, r - 1) - a[r]);
  }
}
int main() {
  cin >> n;
  rep(i, n) { cin >> a[i]; }
  solve(0, n - 1);
  cout << dp[0][n - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define debug(n, v)                                                            \
  cout << #v << ":";                                                           \
  for (int i = 0; i < n; i++)                                                  \
    cout << v[i] << " ";                                                       \
  cout << endl;
#define INF 1000000000
#define mod 1000000007
typedef long long ll;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int n;
ll a[3001];
ll dp[3001][3001];
bool f[3001][3001];
ll solve(int l, int r) {
  if (f[l][r])
    return dp[l][r];
  f[l][r] = true;
  if ((l + n - 1 - r) % 2 == 0) {
    if (l == r)
      return dp[l][r] = a[l];
    return dp[l][r] = max(a[l] + solve(l + 1, r), a[r] + solve(l, r - 1));
  } else {
    if (l == r)
      return dp[l][r] = -a[l];
    return dp[l][r] = min(solve(l + 1, r) - a[l], solve(l, r - 1) - a[r]);
  }
}
int main() {
  cin >> n;
  rep(i, n) { cin >> a[i]; }
  solve(0, n - 1);
  cout << dp[0][n - 1] << endl;
  return 0;
}
Problem p03171 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

#define MAXN 3000

ll dp[MAXN + 1][MAXN + 1];

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  bool f = n % 2;
  rep(l, n) rep(i, n - l + 1) {

    // n奇数かつ今見ている長さが奇数なら先手番
    // n偶数かつ今見ている長さが偶数なら先手番
    bool isFirst = (f && !(l % 2)) || (!f && (l % 2));
    if (isFirst) {
      dp[i][i + l] =
          max<ll>(dp[i][i + l - 1] + a[i + l], dp[i + 1][i + l] + a[i]);
    } else {
      dp[i][i + l] =
          min<ll>(dp[i][i + l - 1] - a[i + l], dp[i + 1][i + l] - a[i]);
    }
  }

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

  return 0;

  rep(i, n) {
    rep(j, n) cout << dp[i][j] << ' ';
    cout << endl;
  }

  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

#define MAXN 4000

ll dp[MAXN + 1][MAXN + 1];

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  bool f = n % 2;
  rep(l, n) rep(i, n - l + 1) {

    // n奇数かつ今見ている長さが奇数なら先手番
    // n偶数かつ今見ている長さが偶数なら先手番
    bool isFirst = (f && !(l % 2)) || (!f && (l % 2));
    if (isFirst) {
      dp[i][i + l] =
          max<ll>(dp[i][i + l - 1] + a[i + l], dp[i + 1][i + l] + a[i]);
    } else {
      dp[i][i + l] =
          min<ll>(dp[i][i + l - 1] - a[i + l], dp[i + 1][i + l] - a[i]);
    }
  }

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

  return 0;

  rep(i, n) {
    rep(j, n) cout << dp[i][j] << ' ';
    cout << endl;
  }

  return 0;
}
Problem p03171 (C++)

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

#define show(x) cerr << #x << " = " << x << endl;
#define input_from_file freopen("input.txt", "r", stdin)
#define output_to_file freopen("output.txt", "w", stdout)
#define fastio ios_base::sync_with_stdio(0)
#define endl '\n'

const int N = 3e3 + 1;
long long dp[N][N][2], in[N], pre[N];
int main() {
#ifdef LOCAL
  input_from_file;
  output_to_file;
#endif
  fastio;
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> in[i], pre[i] = pre[i - 1] + in[i];
  for (int dif = 0; dif < n; ++dif) {
    for (int i = 1; i <= n - dif; ++i) {
      dp[i][i + dif][1] = max(dp[i + 1][i + dif][2] + in[i],
                              dp[i][i + dif - 1][2] + in[i + dif]);
      dp[i][i + dif][2] = pre[i + dif] - pre[i - 1] - dp[i][i + dif][1];
      // cerr << i << " "<< i+dif << " "<<dp[i][i+dif][1] << " "<<
      // dp[i][i+dif][2] << endl;
    }
  }

  cout << dp[1][n][1] - (pre[n] - dp[1][n][1]);

  return 0;
}

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

#define show(x) cerr << #x << " = " << x << endl;
#define input_from_file freopen("input.txt", "r", stdin)
#define output_to_file freopen("output.txt", "w", stdout)
#define fastio ios_base::sync_with_stdio(0)
#define endl '\n'

const int N = 3e3 + 5;
long long dp[N][N][2], in[N], pre[N];
int main() {
#ifdef LOCAL
  input_from_file;
  output_to_file;
#endif
  fastio;
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> in[i], pre[i] = pre[i - 1] + in[i];
  for (int dif = 0; dif < n; ++dif) {
    for (int i = 1; i <= n - dif; ++i) {
      dp[i][i + dif][1] = max(dp[i + 1][i + dif][2] + in[i],
                              dp[i][i + dif - 1][2] + in[i + dif]);
      dp[i][i + dif][2] = pre[i + dif] - pre[i - 1] - dp[i][i + dif][1];
      // cerr << i << " "<< i+dif << " "<<dp[i][i+dif][1] << " "<<
      // dp[i][i+dif][2] << endl;
    }
  }

  cout << dp[1][n][1] - (pre[n] - dp[1][n][1]);

  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(a, n) for (ll a = 0; a < (n); ++a)
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<vector<ll>> Graph;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1e18;

ll dp[3010][3010];
vector<ll> a;
ll n;

ll rec(ll i, ll j) {
  if (i == j) {
    dp[i][i] = 0;
    return 0;
  }
  ll len = j - i;
  if ((n - len) % 2 == 0) {
    dp[i][j] = rec(i + 1, j) + a[i];
    chmax(dp[i][j], rec(i, j - 1) + a[j - 1]);
    if (j < n)
      chmax(dp[i][j], rec(i, j - 1) + a[j - 1]);
  } else {
    dp[i][j] = rec(i + 1, j) - a[i];
    chmin(dp[i][j], rec(i, j - 1) - a[j - 1]);
    if (j < n)
      chmin(dp[i][j], rec(i, j - 1) - a[j - 1]);
  }
  return dp[i][j];
}

int main() {
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];
  rep(i, 3010) rep(j, 3010) dp[i][j] = 0;
  cout << rec(0, n) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(a, n) for (ll a = 0; a < (n); ++a)
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<vector<ll>> Graph;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1e18;

ll dp[3010][3010];
vector<ll> a;
ll n;

ll rec(ll i, ll j) {
  if (dp[i][j] != 0)
    return dp[i][j];
  if (i == j) {
    dp[i][i] = 0;
    return 0;
  }
  ll len = j - i;
  if ((n - len) % 2 == 0) {
    dp[i][j] = rec(i + 1, j) + a[i];
    chmax(dp[i][j], rec(i, j - 1) + a[j - 1]);
    if (j < n)
      chmax(dp[i][j], rec(i, j - 1) + a[j - 1]);
  } else {
    dp[i][j] = rec(i + 1, j) - a[i];
    chmin(dp[i][j], rec(i, j - 1) - a[j - 1]);
    if (j < n)
      chmin(dp[i][j], rec(i, j - 1) - a[j - 1]);
  }
  return dp[i][j];
}

int main() {
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];
  rep(i, 3010) rep(j, 3010) dp[i][j] = 0;
  cout << rec(0, n) << endl;

  return 0;
}
Problem p03171 (C++)

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

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

ll dp[3005][3005];
bool solved[3005][3005];
int l[3005];
int n;

ll solve(int lhs, int rhs) {
  if (lhs == rhs) {
    return l[lhs];
  }
  if (solved[lhs][rhs])
    return dp[lhs][rhs];
  dp[lhs][rhs] =
      max(l[lhs] - solve(lhs + 1, rhs), l[rhs] - solve(lhs, rhs - 1));
  return dp[lhs][rhs];
}

void solve() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> l[i];
  }
  cout << solve(0, n - 1) << endl;
}

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


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

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

ll dp[3005][3005];
bool solved[3005][3005];
int l[3005];
int n;

ll solve(int lhs, int rhs) {
  if (lhs == rhs) {
    return l[lhs];
  }
  if (solved[lhs][rhs])
    return dp[lhs][rhs];
  solved[lhs][rhs] = true;
  dp[lhs][rhs] =
      max(l[lhs] - solve(lhs + 1, rhs), l[rhs] - solve(lhs, rhs - 1));
  return dp[lhs][rhs];
}

void solve() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> l[i];
  }
  cout << solve(0, n - 1) << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}
Problem p03171 (C++)

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

using namespace std;

#define ops cout << "ops" << endl;
#define freopens                                                               \
  freopen("cowpatibility.in", "r", stdin);                                     \
  freopen("cowpatibility.out", "w", stdout);
#define fast                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

#define DIM 3007
#define DIMM 100007
#define INF 1000000000007.0
#define X (ll)(1 << 32)
#define eps 0.0000001
#define PI 3.14159265358979323846
#define MAX 25
#define MODULO (long long)1000000007

const long double gr = (1 + sqrt(5)) / 2;

typedef int I;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

typedef pair<I, I> pII;
typedef pair<ll, ll> pllll;
typedef pair<ld, ld> pldld;

typedef vector<I> vI;
typedef vector<ll> vll;
typedef vector<pllll> vpllll;

typedef char cr;
typedef string str;

ll n;
ll a[DIM], dp[DIM][DIM];

int main() {
  // ops;
  // freopens;
  fast;
  // ll x1,y1,xs,ys,x2,y2,x3,x4,y3,y4;

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

  for (int l = 1; l <= n; l++) {
    for (int s = 1; s <= (n - s + 1); s++) {
      dp[s][l] = max((a[s + l - 1] - dp[s][l - 1]), (a[s] - dp[s + 1][l - 1]));
    }
  }
  cout << dp[1][n] << endl;
  return 0;
}


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

using namespace std;

#define ops cout << "ops" << endl;
#define freopens                                                               \
  freopen("cowpatibility.in", "r", stdin);                                     \
  freopen("cowpatibility.out", "w", stdout);
#define fast                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

#define DIM 3007
#define DIMM 100007
#define INF 1000000000007.0
#define X (ll)(1 << 32)
#define eps 0.0000001
#define PI 3.14159265358979323846
#define MAX 25
#define MODULO (long long)1000000007

const long double gr = (1 + sqrt(5)) / 2;

typedef int I;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

typedef pair<I, I> pII;
typedef pair<ll, ll> pllll;
typedef pair<ld, ld> pldld;

typedef vector<I> vI;
typedef vector<ll> vll;
typedef vector<pllll> vpllll;

typedef char cr;
typedef string str;

ll n;
ll a[DIM], dp[DIM][DIM];

int main() {
  // ops;
  // freopens;
  fast;
  // ll x1,y1,xs,ys,x2,y2,x3,x4,y3,y4;

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

  for (int l = 1; l <= n; l++) {
    for (int s = 1; s <= (n - l + 1); s++) {
      dp[s][l] = max((a[s + l - 1] - dp[s][l - 1]), (a[s] - dp[s + 1][l - 1]));
    }
  }
  cout << dp[1][n] << endl;
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;

int n;
int a[3005];
const ll INF = 1e18;

// dp[i][j] 区間[i,j)におけるX-Yの値
ll dp[3005][3005];

ll solve(int i, int j) {
  if (dp[i][j] >= 0)
    return dp[i][j];
  if (j - i == 0)
    return dp[i][j] = 0;

  if ((n - (j - i)) % 2 == 0) {
    // 先手
    dp[i][j] = max(solve(i + 1, j) + a[i], solve(i, j - 1) + a[j - 1]);
  } else {
    // 後手
    dp[i][j] = min(solve(i + 1, j) - a[i], solve(i, j - 1) - a[j - 1]);
  }

  return dp[i][j];
}

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, 3005) rep(j, 3005) dp[i][j] = -INF;

  ll ans = solve(0, n);
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;

int n;
int a[3005];
const ll INF = 1e18;

// dp[i][j] 区間[i,j)におけるX-Yの値
ll dp[3005][3005];

ll solve(int i, int j) {
  if (dp[i][j] != -INF)
    return dp[i][j];
  if (j - i == 0)
    return dp[i][j] = 0;

  if ((n - (j - i)) % 2 == 0) {
    // 先手
    dp[i][j] = max(solve(i + 1, j) + a[i], solve(i, j - 1) + a[j - 1]);
  } else {
    // 後手
    dp[i][j] = min(solve(i + 1, j) - a[i], solve(i, j - 1) - a[j - 1]);
  }

  return dp[i][j];
}

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, 3005) rep(j, 3005) dp[i][j] = -INF;

  ll ans = solve(0, n);
  cout << ans << endl;

  return 0;
}
Problem p03171 (C++)

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

#define fi first
#define se second
#define pb push_back
#define int long long
#define all(x) (x).begin(), (x).end()

using LL = long long;
using LD = long double;
using pii = pair<int, int>;
using vii = vector<pii>;

const int INF = 1e18;
const int MOD = 1e9 + 7;
const int N = 1e3 + 5;

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

int solve(int l, int r) {
  if (l == r) {
    return a[l]; // or a[r]
  }

  if (dp[l][r] != -1) {
    return dp[l][r];
  }

  int ans1 = a[l] - solve(l + 1, r);
  int ans2 = a[r] - solve(l, r - 1);

  return dp[l][r] = max(ans1, ans2);
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n;

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dp[i][j] = -1;
    }
  }

  cout << solve(0, n - 1) << "\n";
  return 0;
}


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

#define fi first
#define se second
#define pb push_back
#define int long long
#define all(x) (x).begin(), (x).end()

using LL = long long;
using LD = long double;
using pii = pair<int, int>;
using vii = vector<pii>;

const int INF = 1e18;
const int MOD = 1e9 + 7;
const int N = 3e3 + 5;

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

int solve(int l, int r) {
  if (l == r) {
    return a[l]; // or a[r]
  }

  if (dp[l][r] != -1) {
    return dp[l][r];
  }

  int ans1 = a[l] - solve(l + 1, r);
  int ans2 = a[r] - solve(l, r - 1);

  return dp[l][r] = max(ans1, ans2);
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n;

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dp[i][j] = -1;
    }
  }

  cout << solve(0, n - 1) << "\n";
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long *a = new long long[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long dp[n][n];
  // memset(dp,0,sizeof(dp));
  for (int l = n - 1; l >= 0; l--) {
    for (int r = l; r < n; r++) {
      if (l == r)
        dp[l][r] = a[l];
      dp[l][r] = max(a[l] - dp[l + 1][r], a[r] - dp[l][r - 1]);
    }
  }
  // for(int i=0;i<n;i++)
  // {
  //     for(int j=0;j<n;j++)
  //     cout<<dp[i][j]<<"\t";
  //     cout<<endl;
  // }
  cout << dp[0][n - 1];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long *a = new long long[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long dp[n][n];
  // memset(dp,0,sizeof(dp));
  for (int l = n - 1; l >= 0; l--) {
    for (int r = l; r < n; r++) {
      if (l == r)
        dp[l][r] = a[l];
      else
        dp[l][r] = max(a[l] - dp[l + 1][r], a[r] - dp[l][r - 1]);
    }
  }
  // for(int i=0;i<n;i++)
  // {
  //     for(int j=0;j<n;j++)
  //     cout<<dp[i][j]<<"\t";
  //     cout<<endl;
  // }
  cout << dp[0][n - 1];
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
long long int grr[3000][3000];

long long int compute(long long int arr[], long long int start,
                      long long int end) {
  if (grr[start][end] != 0)
    return grr[start][end];
  if (start > end)
    return 0;
  if (start == end) {
    grr[start][end] = arr[start];
    return grr[start][end];
  }

  grr[start][end] = max(arr[start] + min(compute(arr, start + 2, end),
                                         compute(arr, start + 1, end - 1)),
                        arr[end] + min(compute(arr, start, end - 2),
                                       compute(arr, start + 1, end - 1)));
  return grr[start][end];
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  long long int arr[n];
  long long int a = 0, b = 0;
  for (long long int i = 0; i < n; i++) {
    cin >> arr[i];
    b += arr[i];
  }
  a = compute(arr, 0, n - 1);

  cout << a - (b - a) << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
long long int grr[3005][3005];

long long int compute(long long int arr[], long long int start,
                      long long int end) {
  if (grr[start][end] != 0)
    return grr[start][end];
  if (start > end)
    return 0;
  if (start == end) {
    grr[start][end] = arr[start];
    return grr[start][end];
  }

  grr[start][end] = max(arr[start] + min(compute(arr, start + 2, end),
                                         compute(arr, start + 1, end - 1)),
                        arr[end] + min(compute(arr, start, end - 2),
                                       compute(arr, start + 1, end - 1)));
  return grr[start][end];
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  long long int arr[n];
  long long int a = 0, b = 0;
  for (long long int i = 0; i < n; i++) {
    cin >> arr[i];
    b += arr[i];
  }
  a = compute(arr, 0, n - 1);

  cout << a - (b - a) << endl;

  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
ll dp[3001][3001];
ll fun(int x, int y, int *a) {
  if (x > y)
    return 0;
  if (x == y)
    return a[x];
  if (dp[x][y] > -1)
    return dp[x][y];
  ll X = a[x] + min(fun(x + 2, y, a), fun(x + 1, y - 1, a));
  ll Y = a[y] + min(fun(x + 1, y - 1, a), fun(x, y - 2, a));
  ll ans = max(X, Y);
  return ans;
}
void solve() {
  int n;
  cin >> n;
  int a[n];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  memset(dp, -1, sizeof dp);
  ll X = fun(0, n - 1, a);
  cout << X - (sum - X);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  t = 1;
  while (t--) {
    solve();
    cout << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
ll dp[3001][3001];
ll fun(int x, int y, int *a) {
  if (x > y)
    return 0;
  if (x == y)
    return a[x];
  if (dp[x][y] > -1)
    return dp[x][y];
  ll X = a[x] + min(fun(x + 2, y, a), fun(x + 1, y - 1, a));
  ll Y = a[y] + min(fun(x + 1, y - 1, a), fun(x, y - 2, a));
  ll ans = max(X, Y);
  return dp[x][y] = ans;
}
void solve() {
  int n;
  cin >> n;
  int a[n];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  memset(dp, -1, sizeof dp);
  ll X = fun(0, n - 1, a);
  cout << X - (sum - X);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  t = 1;
  while (t--) {
    solve();
    cout << endl;
  }
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
/*input
4
10 80 90 30
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
// order_of_key #of elements less than x
//  find_by_order kth element
typedef long long int ll;
#define ld long double
#define pii pair<int, int>
#define f first
#define s second
#define pb emplace_back
#define REP(i, n) for (ll i = 0; i < n; i++)
#define sz(_a) (ll)(_a.size())
#define FILL(n) memset(n, 0, sizeof(n))
#define ALL(_a) _a.begin(), _a.end()
const ll maxn = 100005;
const ll maxlg = __lg(maxn) + 2;
const ll INF64 = 8000000000000000000LL;
const int INF = 0x3f3f3f3f;
const ll MOD = ll(1e9 + 7);
const ld PI = acos(-1);
const ld eps = 1e-9;
ll mypow(ll a, ll b) {
  ll res = 1LL;
  while (b) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}
int dp[1005][1005];
int arr[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  REP(i, n) cin >> arr[i];
  for (int i = n - 1; i >= 0; i--) {
    for (int j = i; j < n; j++) {
      if (i == j)
        dp[i][j] = arr[i];
      else
        dp[i][j] = max(arr[j] - dp[i][j - 1], arr[i] - dp[i + 1][j]);
    }
  }
  cout << dp[0][n - 1];
}

Fixed submission:
/*input
4
10 80 90 30
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
// order_of_key #of elements less than x
//  find_by_order kth element
typedef long long int ll;
#define ld long double
#define pii pair<int, int>
#define f first
#define s second
#define pb emplace_back
#define REP(i, n) for (ll i = 0; i < n; i++)
#define sz(_a) (ll)(_a.size())
#define FILL(n) memset(n, 0, sizeof(n))
#define ALL(_a) _a.begin(), _a.end()
const ll maxn = 100005;
const ll maxlg = __lg(maxn) + 2;
const ll INF64 = 8000000000000000000LL;
const int INF = 0x3f3f3f3f;
const ll MOD = ll(1e9 + 7);
const ld PI = acos(-1);
const ld eps = 1e-9;
ll mypow(ll a, ll b) {
  ll res = 1LL;
  while (b) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}
ll dp[3005][3005];
ll arr[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  REP(i, n) cin >> arr[i];
  for (int i = n - 1; i >= 0; i--) {
    for (int j = i; j < n; j++) {
      if (i == j)
        dp[i][j] = arr[i];
      else
        dp[i][j] = max(arr[j] - dp[i][j - 1], arr[i] - dp[i + 1][j]);
    }
  }
  cout << dp[0][n - 1];
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int INF = 1e9 + 5;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

const int mxs = 302;
int main() {
  ll dp[mxs][mxs] = {}, in[mxs];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> in[i];
    dp[i][i] = in[i];
  }
  for (int l = n - 1; l >= 0; l--)
    for (int r = l; r < n; r++) {
      if (l < r) {
        dp[l][r] = max(in[l] - dp[l + 1][r], in[r] - dp[l][r - 1]);
      }
    }
  cout << dp[0][n - 1] << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int INF = 1e9 + 5;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

const int mxs = 3002;
int main() {
  ll dp[mxs][mxs] = {}, in[mxs];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> in[i];
    dp[i][i] = in[i];
  }
  for (int l = n - 1; l >= 0; l--)
    for (int r = l; r < n; r++) {
      if (l < r) {
        dp[l][r] = max(in[l] - dp[l + 1][r], in[r] - dp[l][r - 1]);
      }
    }
  cout << dp[0][n - 1] << '\n';
}
Problem p03171 (C++)

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

#define pb push_back
#define F first
#define S second
#define y1 dasdasfasfas
#define x1 wqdadfasfasfas
#define All(c) c.begin(), c.end()
#define SZ(A) (int((A).size()))
#define umap unordered_map
#define __ fflush(stdout)
#define FILENAME ""

typedef long long ll;
typedef long double ld;

using namespace std;

void FREOPEN() {
#ifdef LOCAL
  freopen(".in", "r", stdin);
  freopen("1.out", "w", stdout);
#else
  // freopen(FILENAME".in", "r", stdin);
  // freopen(FILENAME".out", "w", stdout);
#endif
}

inline double Time() { return (clock() * 1.0) / CLOCKS_PER_SEC; }

const int N = 3000, inf = 1e9 * 2;

const ll MOD = 1e9 + 7ll, INF = 1e18;

const int dx[] = {1, -1, 0, 0, -1, 1, -1, 1};
const int dy[] = {0, 0, 1, -1, -1, 1, 1, -1};

ll dp[N][N];

bool was[N][N];

int a[N];

ll solve(int l, int r, int player) {
  if (was[l][r])
    return dp[l][r];
  was[l][r] = 1;
  if (l == r) {
    if (player)
      return dp[l][r] = -a[l];
    else
      return dp[l][r] = a[l];
  }
  if (!player) {
    dp[l][r] = max(solve(l + 1, r, 1) + a[l], solve(l, r - 1, 1) + a[r]);
  } else {
    dp[l][r] = min(solve(l + 1, r, 0) - a[l], solve(l, r - 1, 0) - a[r]);
  }
  return dp[l][r];
}

int main() {
  FREOPEN();
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d ", &a[i]);
  printf("%lld ", solve(1, n, 0));
  return 0;
}


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

#define pb push_back
#define F first
#define S second
#define y1 dasdasfasfas
#define x1 wqdadfasfasfas
#define All(c) c.begin(), c.end()
#define SZ(A) (int((A).size()))
#define umap unordered_map
#define __ fflush(stdout)
#define FILENAME ""

typedef long long ll;
typedef long double ld;

using namespace std;

void FREOPEN() {
#ifdef LOCAL
  freopen(".in", "r", stdin);
  freopen("1.out", "w", stdout);
#else
  // freopen(FILENAME".in", "r", stdin);
  // freopen(FILENAME".out", "w", stdout);
#endif
}

inline double Time() { return (clock() * 1.0) / CLOCKS_PER_SEC; }

const int N = 3001, inf = 1e9 * 2;

const ll MOD = 1e9 + 7ll, INF = 1e18;

const int dx[] = {1, -1, 0, 0, -1, 1, -1, 1};
const int dy[] = {0, 0, 1, -1, -1, 1, 1, -1};

ll dp[N][N];

bool was[N][N];

int a[N];

ll solve(int l, int r, int player) {
  if (was[l][r])
    return dp[l][r];
  was[l][r] = 1;
  if (l == r) {
    if (player)
      return dp[l][r] = -a[l];
    else
      return dp[l][r] = a[l];
  }
  if (!player) {
    dp[l][r] = max(solve(l + 1, r, 1) + a[l], solve(l, r - 1, 1) + a[r]);
  } else {
    dp[l][r] = min(solve(l + 1, r, 0) - a[l], solve(l, r - 1, 0) - a[r]);
  }
  return dp[l][r];
}

int main() {
  FREOPEN();
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d ", &a[i]);
  printf("%lld ", solve(1, n, 0));
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
// I am Varun Das from Jadavpur University
#include <bits/stdc++.h>
#define mod 1000000007
#define ll long long
#define dbg(x) cerr << #x << " = " << x << endl
#define INF LLONG_MAX
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

int main() {
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // cout<<fixed<<setprecision(12);
  ll n, a[10004];
  cin >> n;
  ll dp[10004][10004];
  memset(dp, 0, sizeof(dp));
  loop(i, 0, n) cin >> a[i];
  rep(i, n - 1, 0) {
    loop(j, i, n) {
      if (i == 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];
  return 0;
}

Fixed submission:
// I am Varun Das from Jadavpur University
#include <bits/stdc++.h>
#define mod 1000000007
#define ll long long
#define dbg(x) cerr << #x << " = " << x << endl
#define INF LLONG_MAX
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

int main() {
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // cout<<fixed<<setprecision(12);
  ll n, a[10004];
  cin >> n;
  ll dp[3004][3004];
  memset(dp, 0, sizeof(dp));
  loop(i, 0, n) cin >> a[i];
  rep(i, n - 1, 0) {
    loop(j, i, n) {
      if (i == 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];
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
const ll INF = (1LL << 60);
const int MOD = 1000000007;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int n;
vector<int> a(3030);
vector<vector<ll>> dp(3030, vector<ll>(3030, 0));
bool seen[3030][3030];

// [l, r)
ll rec(int l, int r) {
  if (l > r)
    return 0;
  if (seen[l][r])
    return dp[l][r];

  ll res = 0;
  int dif = n - (r - l + 1);
  if (dif % 2 == 0) {
    res = -INF;
    chmax(res, rec(l + 1, r) + a[l]);
    chmax(res, rec(l, r - 1) + a[r]);
  } else {
    res = INF;
    chmin(res, rec(l + 1, r) - a[l]);
    chmin(res, rec(l, r - 1) - a[r]);
  }

  return dp[l][r] = res;
}

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  rec(0, n - 1);
  cout << dp[0][n - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
const ll INF = (1LL << 60);
const int MOD = 1000000007;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int n;
vector<int> a(3030);
vector<vector<ll>> dp(3030, vector<ll>(3030, 0));
bool seen[3030][3030];

// [l, r)
ll rec(int l, int r) {
  if (l > r)
    return 0;
  if (seen[l][r])
    return dp[l][r];

  seen[l][r] = true;

  ll res = 0;
  int dif = n - (r - l + 1);
  if (dif % 2 == 0) {
    res = -INF;
    chmax(res, rec(l + 1, r) + a[l]);
    chmax(res, rec(l, r - 1) + a[r]);
  } else {
    res = INF;
    chmin(res, rec(l + 1, r) - a[l]);
    chmin(res, rec(l, r - 1) - a[r]);
  }

  return dp[l][r] = res;
}

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  rec(0, n - 1);
  cout << dp[0][n - 1] << endl;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define sanyes                                                                 \
  ios_base::sync_with_stdio(0), cout.tie(0);                                   \
  cin.tie(0);
#define llong long long
#define pb push_back
#define bpc __builtin_popcount

const llong mxn = 1e5 + 7;
const llong mod = 1e9 + 7;
const llong inf = 1e18 + 9;

using namespace std;

deque<llong> deq;
vector<llong> v;

llong n, a[mxn], k, dp[1001][1001], sum2, sum1;
bool ok = 0;

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 + i - 1 <= n; j++) {
      dp[i][j] = max(a[j] - dp[i - 1][j + 1], a[j + i - 1] - dp[i - 1][j]);
      /// cout << dp[i][j] << ' ';
    } /// cout << endl;
  }
  cout << dp[n][1];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define sanyes                                                                 \
  ios_base::sync_with_stdio(0), cout.tie(0);                                   \
  cin.tie(0);
#define llong long long
#define pb push_back
#define bpc __builtin_popcount

const llong mxn = 1e5 + 7;
const llong mod = 1e9 + 7;
const llong inf = 1e18 + 9;

using namespace std;

deque<llong> deq;
vector<llong> v;

llong n, a[mxn], k, dp[3001][3001], sum2, sum1;
bool ok = 0;

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 + i - 1 <= n; j++) {
      dp[i][j] = max(a[j] - dp[i - 1][j + 1], a[j + i - 1] - dp[i - 1][j]);
      /// cout << dp[i][j] << ' ';
    } /// cout << endl;
  }
  cout << dp[n][1];
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
#define mp make_pair
#define inlld(x) scanf("%lld", &x)
#define ind(x) scanf("%d", &x)
#define inlld2(x, y) scanf("%lld%lld", &x, &y)
#define inlld3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define ind2(x, y) scanf("%d%d", &x, &y)
#define ind3(x, y, z) scanf("%d%d%d", &x, &y, &z)

const int N = 1e3 + 5;
const int MOD = 1e9 + 7;

typedef long long ll;
typedef long double ld;

using namespace std;

ll n, arr[N], dp[N][N][2];

ll solve(ll i, ll j, ll mov) {
  if (dp[i][j][mov] != -1)
    return dp[i][j][mov];
  if (i == j && mov)
    return arr[i];
  else if (i == j)
    return -arr[i];
  if (mov)
    return dp[i][j][mov] =
               max(solve(i + 1, j, 0) + arr[i], solve(i, j - 1, 0) + arr[j]);
  else
    return dp[i][j][mov] =
               min(solve(i + 1, j, 1) - arr[i], solve(i, j - 1, 1) - arr[j]);
}

int main() {
  inlld(n);
  for (ll a = 1; a <= n; a++)
    inlld(arr[a]);
  memset(dp, -1, sizeof(dp));
  printf("%lld\n", solve(1, n, 1));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
#define mp make_pair
#define inlld(x) scanf("%lld", &x)
#define ind(x) scanf("%d", &x)
#define inlld2(x, y) scanf("%lld%lld", &x, &y)
#define inlld3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define ind2(x, y) scanf("%d%d", &x, &y)
#define ind3(x, y, z) scanf("%d%d%d", &x, &y, &z)

const int N = 3e3 + 5;
const int MOD = 1e9 + 7;

typedef long long ll;
typedef long double ld;

using namespace std;

ll n, arr[N], dp[N][N][2];

ll solve(ll i, ll j, ll mov) {
  if (dp[i][j][mov] != -1)
    return dp[i][j][mov];
  if (i == j && mov)
    return arr[i];
  else if (i == j)
    return -arr[i];
  if (mov)
    return dp[i][j][mov] =
               max(solve(i + 1, j, 0) + arr[i], solve(i, j - 1, 0) + arr[j]);
  else
    return dp[i][j][mov] =
               min(solve(i + 1, j, 1) - arr[i], solve(i, j - 1, 1) - arr[j]);
}

int main() {
  inlld(n);
  for (ll a = 1; a <= n; a++)
    inlld(arr[a]);
  memset(dp, -1, sizeof(dp));
  printf("%lld\n", solve(1, n, 1));
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, a[3009], dp[3009][3009];
ll best(ll l, ll r, bool b) {
  if (l > r)
    return 0;
  ll &ret = dp[l][r];
  if (ret < 1e4)
    return ret;
  if (b)
    return ret = min(best(l + 1, r, 0) - a[l], best(l, r - 1, 0) - a[r]);
  return ret = max(best(l + 1, r, 1) + a[l], best(l, r - 1, 1) + a[r]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll i = 0; i <= n; i++)
    for (ll j = 0; j <= n; j++)
      dp[i][j] = 1e14;
  cout << best(0, n - 1, 0);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, a[3009], dp[3009][3009];
ll best(ll l, ll r, bool b) {
  if (l > r)
    return 0;
  ll &ret = dp[l][r];
  if (ret < 1e14)
    return ret;
  if (b)
    return ret = min(best(l + 1, r, 0) - a[l], best(l, r - 1, 0) - a[r]);
  return ret = max(best(l + 1, r, 1) + a[l], best(l, r - 1, 1) + a[r]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll i = 0; i <= n; i++)
    for (ll j = 0; j <= n; j++)
      dp[i][j] = 1e14;
  cout << best(0, n - 1, 0);
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
ll int n, k, mod = 1e9 + 7;
ll int a[101], dp[3000][3000];
ll int recur(int i, int j) {
  if (i == j)
    return a[i];
  if (j - i == 1)
    return abs(a[i] - a[j]);
  if (dp[i][j] != -1)
    return dp[i][j];
  ll int ans1 = a[i] - a[i + 1] + recur(i + 2, j);
  ll int ans2 = a[i] - a[j] + recur(i + 1, j - 1);
  ll int ans5 = min(ans1, ans2);
  ll int ans3 = a[j] - a[i] + recur(i + 1, j - 1);
  ll int ans4 = a[j] - a[j - 1] + recur(i, j - 2);
  ll int ans6 = min(ans3, ans4);
  return dp[i][j] = max(ans5, ans6);
}

int main() {
  memset(dp, -1, sizeof(dp));
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cout << recur(0, n - 1);
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
ll int n, k, mod = 1e9 + 7;
ll int a[3001], dp[3000][3000];
ll int recur(int i, int j) {
  if (i == j)
    return a[i];
  if (j - i == 1)
    return abs(a[i] - a[j]);
  if (dp[i][j] != -1)
    return dp[i][j];
  ll int ans1 = a[i] - a[i + 1] + recur(i + 2, j);
  ll int ans2 = a[i] - a[j] + recur(i + 1, j - 1);
  ll int ans5 = min(ans1, ans2);
  ll int ans3 = a[j] - a[i] + recur(i + 1, j - 1);
  ll int ans4 = a[j] - a[j - 1] + recur(i, j - 2);
  ll int ans6 = min(ans3, ans4);
  return dp[i][j] = max(ans5, ans6);
}

int main() {
  memset(dp, -1, sizeof(dp));
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cout << recur(0, n - 1);
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)
const int N = 3000;
int n, k, a[100];
ll dp[N][N][2];

ll cal(int i, int j, bool p) {
  if (i > j)
    return 0;
  if (dp[i][j][p] != -1)
    return dp[i][j][p];
  if (p == 0)
    return dp[i][j][p] =
               max(cal(i + 1, j, !p) + a[i], cal(i, j - 1, !p) + a[j]);
  else
    return dp[i][j][p] =
               min(cal(i + 1, j, !p) - a[i], cal(i, j - 1, !p) - a[j]);
}

int main() {
  cin >> n;
  f(i, 0, n) cin >> a[i];
  memset(dp, -1, sizeof dp);
  cout << cal(0, n - 1, 0);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)
const int N = 3000;
int n, k, a[N];
ll dp[N][N][2];

ll cal(int i, int j, bool p) {
  if (i > j)
    return 0;
  if (dp[i][j][p] != -1)
    return dp[i][j][p];
  if (p == 0)
    return dp[i][j][p] =
               max(cal(i + 1, j, !p) + a[i], cal(i, j - 1, !p) + a[j]);
  else
    return dp[i][j][p] =
               min(cal(i + 1, j, !p) - a[i], cal(i, j - 1, !p) - a[j]);
}

int main() {
  cin >> n;
  f(i, 0, n) cin >> a[i];
  memset(dp, -1, sizeof dp);
  cout << cal(0, n - 1, 0);
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#define taskname ""

#include <algorithm>
#include <iostream>

using namespace std;
#define long long long

const int N = 2e3 + 10;

int n, a[N];
long f[N][N];

int main() {
  //    freopen(taskname".INP", "r", stdin);
  //    freopen(taskname".OUT", "w", stdout);
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

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

  for (int r = 1; r <= n; ++r)
    for (int l = r; l > 0; --l)
      f[l][r] = max(a[l] - f[l + 1][r], a[r] - f[l][r - 1]);

  cout << f[1][n];

  return 0;
}


Fixed submission:
#define taskname ""

#include <algorithm>
#include <iostream>

using namespace std;
#define long long long

const int N = 3e3 + 10;

int n, a[N];
long f[N][N];

int main() {
  //    freopen(taskname".INP", "r", stdin);
  //    freopen(taskname".OUT", "w", stdout);
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

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

  for (int r = 1; r <= n; ++r)
    for (int l = r; l > 0; --l)
      f[l][r] = max(a[l] - f[l + 1][r], a[r] - f[l][r - 1]);

  cout << f[1][n];

  return 0;
}