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

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

// dp[i][j] = max a person can get from strones from i to  j

int main() {
  long long int n;
  cin >> n;
  vector<long long int> x(n);

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

  vector<long long int> ab(n + 1);
  vector<vector<long long int>> dp(n + 1, ab);

  for (long long int i = 1; i < n; i++) {
    dp[i][i] = x[i - 1];

    if (x[i - 1] > x[i])
      dp[i][i + 1] = x[i - 1];
    else
      dp[i][i + 1] = x[i];
  }

  dp[0][0] = 0;
  dp[n][n] = x[n - 1];

  for (long long int len = 2; len <= n; len++) {
    for (long long int i = 1; i <= n - len; i++) {

      long long int j = i + len;

      long long int a = min(dp[i + 2][j], dp[i + 1][j - 1]) + x[i - 1];
      long long int b = min(dp[i][j - 2], dp[i + 1][j - 1]) + x[j - 1];

      dp[i][j] = max(a, b);
    }
  }
  long long int a = dp[1][n];
  long long int b = min(dp[2][n], dp[1][n - 1]);
  cout << a - b;
  return 0;
}

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

// dp[i][j] = max a person can get from strones from i to  j

int main() {
  long long int n;
  cin >> n;
  vector<long long int> x(n);

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

  if (n == 0) {
    cout << 0;
    return 0;
  }
  if (n == 1) {
    cout << x[0];
    return 0;
  }
  vector<long long int> ab(n + 1);
  vector<vector<long long int>> dp(n + 1, ab);

  for (long long int i = 1; i < n; i++) {
    dp[i][i] = x[i - 1];

    if (x[i - 1] > x[i])
      dp[i][i + 1] = x[i - 1];
    else
      dp[i][i + 1] = x[i];
  }

  dp[0][0] = 0;
  dp[n][n] = x[n - 1];

  for (long long int len = 2; len <= n; len++) {
    for (long long int i = 1; i <= n - len; i++) {

      long long int j = i + len;

      long long int a = min(dp[i + 2][j], dp[i + 1][j - 1]) + x[i - 1];
      long long int b = min(dp[i][j - 2], dp[i + 1][j - 1]) + x[j - 1];

      dp[i][j] = max(a, b);
    }
  }
  long long int a = dp[1][n];
  long long int b = min(dp[2][n], dp[1][n - 1]);
  cout << a - b;
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define Mx 1000000000005
#define ll long long
#define pi 3.141592653
#define vi vector<ll>
#define Mo 1000000007
#define mp make_pair
#define M map<ll, ll>
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define vii vector<pair<ll, ll>>
#define fi first
#define se second
#define pb push_back
#define ld long double
#define fill(a, x) memset(a, x, sizeof(a))
using namespace std;
bool comp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return a.second < b.second;
}
/*vi b=v;
   sort(b.begin(), b.end());
  b.resize(unique(b.begin(), b.end()) - b.begin());
  ll t; cin>>t; fr(f,0,t){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
 */
ll func(ll l, ll r, ll a[]) {
  if (l == r)
    return a[l];
  if (r - l == 1)
    return max(a[l], a[r]);

  ll x = a[l] + min(func(l + 1, r - 1, a), func(l + 2, r, a));
  ll y = a[r] + min(func(l, r - 2, a), func(l + 1, r - 1, a));
  return max(x, y);
}
int main() {
  ll n, sum = 0;
  cin >> n;
  ll a[n];
  fr(i, 0, n) {
    cin >> a[i];
    sum += a[i];
  }

  cout << 2 * func(0, n - 1, a) - sum;
}

Fixed submission:
#include <bits/stdc++.h>
#define Mx 1000000000005
#define ll long long
#define pi 3.141592653
#define vi vector<ll>
#define Mo 1000000007
#define mp make_pair
#define M map<ll, ll>
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define vii vector<pair<ll, ll>>
#define fi first
#define se second
#define pb push_back
#define ld long double
#define fill(a, x) memset(a, x, sizeof(a))
using namespace std;
bool comp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return a.second < b.second;
}
/*vi b=v;
   sort(b.begin(), b.end());
  b.resize(unique(b.begin(), b.end()) - b.begin());
  ll t; cin>>t; fr(f,0,t){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
 */
ll func(ll l, ll r, ll a[]) {
  if (l == r)
    return a[l];
  if (r - l == 1)
    return max(a[l], a[r]);

  ll x = a[l] + min(func(l + 1, r - 1, a), func(l + 2, r, a));
  ll y = a[r] + min(func(l, r - 2, a), func(l + 1, r - 1, a));
  return max(x, y);
}
int main() {
  ll n, sum = 0;
  cin >> n;
  ll a[n];
  fr(i, 0, n) {
    cin >> a[i];
    sum += a[i];
  }

  ll dp[n][n], x, y, z;

  fr(i, 0, n) dp[i][i] = a[i];

  fr(l, 2, n + 1) {
    fr(i, 0, n - l + 1) {
      ll j = i + l - 1;
      if (j - i == 1) {
        dp[i][j] = max(a[i], a[j]);
        continue;
      }

      x = dp[i + 1][j - 1];
      y = dp[i + 2][j];
      z = dp[i][j - 2];
      dp[i][j] = max(a[i] + min(x, y), a[j] + min(z, x));
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define vi vector<int>
#define pii pair<long long, long long>
#define pll pair<int, int>
#define mod 1000000007
#define hell 998244353
#define all(x) x.begin(), x.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb push_back
#define ff first
#define ss second
#define rep(n) for (int i = 0; i < n; i++)
#define lb lower_bound
#define up upper_bound
int dp[3005][3005];
void solve() {

  int n;
  cin >> n;
  int sum = 0;
  vi v(n);
  rep(n) {
    cin >> v[i];
    sum += v[i];
  }

  for (int gap = 0; gap < n; gap++) {
    for (int i = 0, j = gap; j < n; i++, j++) {
      // f(i,j)=max(vi,min(f(i+2,j),f(i+1,j-1)),vj+min(f(i+1,j-1),f(i,j-2)))

      int x = i + 2 <= j ? dp[i + 2][j] : 0;
      int y = i + 1 <= j - 1 ? dp[i + 1][j - 1] : 0;
      int z = i <= j - 2 ? dp[i][j - 2] : 0;

      dp[i][j] = max(v[i] + min(x, y), v[j] + min(y, z));
    }
  }

  cout << dp[0][n - 1] - (sum - dp[0][n - 1]);
}

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

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

  int T;
  T = 1;

  while (T--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define vi vector<int>
#define pii pair<long long, long long>
#define pll pair<int, int>
#define mod 1000000007
#define hell 998244353
#define all(x) x.begin(), x.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb push_back
#define ff first
#define ss second
#define rep(n) for (int i = 0; i < n; i++)
#define lb lower_bound
#define up upper_bound
int dp[3005][3005];
void solve() {

  int n;
  cin >> n;
  int sum = 0;
  vi v(n);
  rep(n) {
    cin >> v[i];
    sum += v[i];
  }

  for (int gap = 0; gap < n; gap++) {
    for (int i = 0, j = gap; j < n; i++, j++) {
      // f(i,j)=max(vi,min(f(i+2,j),f(i+1,j-1)),vj+min(f(i+1,j-1),f(i,j-2)))

      int x = i + 2 <= j ? dp[i + 2][j] : 0;
      int y = i + 1 <= j - 1 ? dp[i + 1][j - 1] : 0;
      int z = i <= j - 2 ? dp[i][j - 2] : 0;

      dp[i][j] = max(v[i] + min(x, y), v[j] + min(y, z));
    }
  }

  cout << dp[0][n - 1] - (sum - dp[0][n - 1]);
}

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int T;
  T = 1;

  while (T--) {
    solve();
  }
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define err(args...)                                                           \
  {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream &operator>>(istream &is, vector<T> &a) {
  for (T &x : a) {
    is >> x;
  }
  return is;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  const int N = 3000;
  static ll opt[N + 1][N + 1][2];
  for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= n; j++) {
      if (i <= j) {
        opt[i][j][0] = max(a[i] + opt[i + 1][j][1], a[j] + opt[i][j - 1][1]);
        opt[i][j][1] = min(opt[i + 1][j][0] - a[i], opt[i][j - 1][0] - a[j]);
      }
    }
  }
  cout << opt[1][n][0] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define err(args...)                                                           \
  {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream &operator>>(istream &is, vector<T> &a) {
  for (T &x : a) {
    is >> x;
  }
  return is;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  const int N = 3000;
  static ll opt[N + 2][N + 1][2];
  for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= n; j++) {
      if (i <= j) {
        opt[i][j][0] = max(a[i] + opt[i + 1][j][1], a[j] + opt[i][j - 1][1]);
        opt[i][j][1] = min(opt[i + 1][j][0] - a[i], opt[i][j - 1][0] - a[j]);
      }
    }
  }
  cout << opt[1][n][0] << endl;
  return 0;
}
Problem p03171 (C++)

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

#define ll long long
#define ff first
#define ss second
#define pb push_back

using namespace std;

const int N = 1e6 + 69, INF = 1e9;

const ll INFLL = 1e18;

int n, k;
ll dp[3009][3009], A[N];

int main() {

  cin >> n;

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

  for (int cnt = 1; cnt <= n; cnt++) {
    for (int l = 1; l <= n; l++) {
      int r = cnt - l + 1;
      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]);
      }
    }
  }

  cout << dp[1][n] << endl;
}


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

#define ll long long
#define ff first
#define ss second
#define pb push_back

using namespace std;

const int N = 1e6 + 69, INF = 1e9;

const ll INFLL = 1e18;

int n, k;
ll dp[3009][3009], A[N];

int main() {

  cin >> n;

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

  for (int cnt = 1; cnt <= n; cnt++) {
    for (int l = 1; l <= n; l++) {
      int r = l + cnt - 1;
      if (r > n)
        continue;
      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]);
      }
    }
  }

  cout << dp[1][n] << endl;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mod 1000000007
#define pb push_back
#define ll long long
#define ld long double
#define ft first
#define sd second
using namespace std;

const int N = 1e3 + 9;
ll a[N];
ll dp[N][N][2];

ll solve(int l, int r, bool turn) {
  if (l > r)
    return 0;
  ll &ret = dp[l][r][turn];
  if (ret != -1)
    return ret;
  if (turn) {
    ret =
        min(solve(l + 1, r, turn ^ 1) - a[l], solve(l, r - 1, turn ^ 1) - a[r]);

  } else {
    ret =
        max(solve(l + 1, r, turn ^ 1) + a[l], solve(l, r - 1, turn ^ 1) + a[r]);
  }
  return ret;
}

signed main() {

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007
#define pb push_back
#define ll long long
#define ld long double
#define ft first
#define sd second
using namespace std;

const int N = 3e3 + 9;
ll a[N];
ll dp[N][N][2];

ll solve(int l, int r, bool turn) {
  if (l > r)
    return 0;
  ll &ret = dp[l][r][turn];
  if (ret != -1)
    return ret;
  if (turn) {
    ret =
        min(solve(l + 1, r, turn ^ 1) - a[l], solve(l, r - 1, turn ^ 1) - a[r]);

  } else {
    ret =
        max(solve(l + 1, r, turn ^ 1) + a[l], solve(l, r - 1, turn ^ 1) + a[r]);
  }
  return ret;
}

signed main() {

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

  return 0;
}
Problem p03171 (C++)

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

long long max(long long a, long long b) { return a > b ? a : b; }

void get_opt_scoredelta(int N, long long *A) {
  long long dp[N + 1][N + 1];
  // dp[i][j] = score difference between current player and the other player for
  // the subsequence ai....aj dp[i][j] = max(a[i] - dp[i+1][j], a[j] -
  // dp[i][j-1]) return dp[0][N-1]
  for (int k = 0; k < N; k++)
    dp[k][k] = A[k];
  for (int p = 2; p <= N; p++) {
    for (int i = 0; i < N - 1; i++) {
      int j = i + p - 1;
      dp[i][j] = max(A[i] - dp[i + 1][j], A[j] - dp[i][j - 1]);
    }
  }
  cout << dp[0][N - 1] << endl;
}

int main() {
  int N;
  cin >> N;
  long long A[N];
  for (int i = 0; i < N; i++)
    cin >> A[i];
  /*for(int i=0; i<N; i++)
      cout << P[i] << " ";
  cout << endl;
  */
  get_opt_scoredelta(N, A);
  return 0;
}


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

long long max(long long a, long long b) { return a > b ? a : b; }

void get_opt_scoredelta(int N, long long *A) {
  long long dp[N + 1][N + 1];
  // dp[i][j] = score difference between current player and the other player for
  // the subsequence ai....aj dp[i][j] = max(a[i] - dp[i+1][j], a[j] -
  // dp[i][j-1]) return dp[0][N-1]
  for (int k = 0; k < N; k++)
    dp[k][k] = A[k];
  for (int p = 2; p <= N; p++) {
    for (int i = 0; i < N - 1; i++) {
      int j = i + p - 1;
      if (j < N)
        dp[i][j] = max(A[i] - dp[i + 1][j], A[j] - dp[i][j - 1]);
    }
  }
  cout << dp[0][N - 1] << endl;
}

int main() {
  int N;
  cin >> N;
  long long A[N];
  for (int i = 0; i < N; i++)
    cin >> A[i];
  /*for(int i=0; i<N; i++)
      cout << P[i] << " ";
  cout << endl;
  */
  get_opt_scoredelta(N, A);
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define lli long long int
#define vec2 vector<vector<lli>>
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define FRE                                                                    \
  freopen("explicit.in", "r", stdin);                                          \
  freopen("explicit.out", "w", stdout)
#define mp make_pair
#define pll pair<lli, lli>
#define vll vector<lli>
#define pq2 priority_queue<pair<lli, lli>>
#define f first
#define s second
#define pb push_back
#define pf push_front
#define minii LLONG_MAX
#define ld long double
const long double pi = 3.1415926535898;
#define dec greater<lli>()
// const lli mod=1000000007;
const lli mod = 100000000;
using namespace std;

int main() {
  fio;
  lli n;
  cin >> n;
  lli a[n];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  lli dp[300005][300005];
  memset(dp, 0, sizeof(dp));
  for (int i = n; i >= 1; i--) {
    for (int j = i; j <= n; j++) {
      dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]);
    }
  }
  cout << dp[1][n];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define lli long long int
#define vec2 vector<vector<lli>>
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define FRE                                                                    \
  freopen("explicit.in", "r", stdin);                                          \
  freopen("explicit.out", "w", stdout)
#define mp make_pair
#define pll pair<lli, lli>
#define vll vector<lli>
#define pq2 priority_queue<pair<lli, lli>>
#define f first
#define s second
#define pb push_back
#define pf push_front
#define minii LLONG_MAX
#define ld long double
const long double pi = 3.1415926535898;
#define dec greater<lli>()
// const lli mod=1000000007;
const lli mod = 100000000;
using namespace std;

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

Buggy submission (status: Runtime Error):
// In the name of GOD

#include <bits/stdc++.h>
#define ll long long
#define pp pair<int, int>
using namespace std;
const int N = 3e2 + 10, MOD = 1e9 + 7;
const ll INF = 1e18;
vector<int> a(N);
ll dp[N][N];
bool vis[N][N] = {0};

ll f(int l, int r) {
  if (vis[l][r])
    return dp[l][r];

  if (l == r)
    return a[l];

  ll t1 = a[l] - f(l + 1, r);
  ll t2 = a[r] - f(l, r - 1);
  dp[l][r] = max(t1, t2);
  vis[l][r] = true;
  return dp[l][r];
}

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tt = 1;
  // cin >> tt;
  for (int tc = 1; tc <= tt; ++tc) {
    solve();
  }
}

Fixed submission:
// In the name of GOD

#include <bits/stdc++.h>
#define ll long long
#define pp pair<int, int>
using namespace std;
const int N = 3e3 + 10, MOD = 1e9 + 7;
const ll INF = 1e18;
vector<int> a(N);
ll dp[N][N];
bool vis[N][N] = {0};

ll f(int l, int r) {
  if (vis[l][r])
    return dp[l][r];

  if (l == r)
    return a[l];

  ll t1 = a[l] - f(l + 1, r);
  ll t2 = a[r] - f(l, r - 1);
  dp[l][r] = max(t1, t2);
  vis[l][r] = true;
  return dp[l][r];
}

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tt = 1;
  // cin >> tt;
  for (int tc = 1; tc <= tt; ++tc) {
    solve();
  }
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  int T = 1;
  // cin>>T;
  //  cin.ignore(); must be there when using getline(cin, s)
  while (T--) {
    ll n;
    cin >> n;
    ll a[n];
    for (ll i = 0; i < n; i++)
      cin >> a[i];
    ll dp[n + 1][n + 1];
    for (ll l = n - 1; l >= 0; l++) {
      for (ll 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]);
      }
    }
    cout << dp[0][n - 1];
  }
  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  int T = 1;
  // cin>>T;
  //  cin.ignore(); must be there when using getline(cin, s)
  while (T--) {
    ll n;
    cin >> n;
    ll a[n];
    for (ll i = 0; i < n; i++)
      cin >> a[i];
    ll dp[n + 1][n + 1];
    for (ll l = n - 1; l >= 0; l--) {
      for (ll 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]);
      }
    }
    cout << dp[0][n - 1];
  }
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <stdio.h>
using namespace std;

const int MAXN = 3005;

int a[MAXN];
long long dp[MAXN][MAXN];
bool used[MAXN][MAXN];
int n;

long long solve(int l, int r) {
  if (l > r)
    return 0;
  if (used[l][r])
    return dp[l][r];

  long long &ans = dp[l][r];
  if ((r - l) % 2 == (n - 1) % 2) {
    ans = max(a[l] + solve(l + 1, r), a[r] + solve(l, r - 1));
  } else {
    ans = min(-a[l] + solve(l + 1, r), -a[r] + solve(l, r - 1));
  }
  return ans;
}

int main(void) {
  scanf(" %d", &n);
  for (int i = 0; i < n; i++) {
    scanf(" %d", &a[i]);
  }
  long long ans = solve(0, n - 1);
  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <stdio.h>
using namespace std;

const int MAXN = 3005;

int a[MAXN];
long long dp[MAXN][MAXN];
bool used[MAXN][MAXN];
int n;

long long solve(int l, int r) {
  if (l > r)
    return 0;
  if (used[l][r])
    return dp[l][r];

  long long &ans = dp[l][r];
  if ((r - l) % 2 == (n - 1) % 2) {
    ans = max(a[l] + solve(l + 1, r), a[r] + solve(l, r - 1));
  } else {
    ans = min(-a[l] + solve(l + 1, r), -a[r] + solve(l, r - 1));
  }
  used[l][r] = true;
  return ans;
}

int main(void) {
  scanf(" %d", &n);
  for (int i = 0; i < n; i++) {
    scanf(" %d", &a[i]);
  }
  long long ans = solve(0, n - 1);
  printf("%lld\n", ans);

  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define endl '\n'
const int mod = 1e9 + 7, u = 1e3 + 5;

int n, arr[u], dp[u][u];
int solve(int low, int high) {
  if (dp[low][high] == -1) {
    if (low == high) {
      dp[low][high] = arr[low];
    } else {
      dp[low][high] = max(arr[low] - solve(low + 1, high),
                          arr[high] - solve(low, high - 1));
    }
  }
  return dp[low][high];
}

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

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

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define endl '\n'
const int mod = 1e9 + 7, u = 3e3 + 5;

int n, arr[u], dp[u][u];
int solve(int low, int high) {
  if (dp[low][high] == -1) {
    if (low == high) {
      dp[low][high] = arr[low];
    } else {
      dp[low][high] = max(arr[low] - solve(low + 1, high),
                          arr[high] - solve(low, high - 1));
    }
  }
  return dp[low][high];
}

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

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

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

  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int N = 3e3 + 5;
ll a[N], dp[N][N];
int main() {
  int n, i, j, L, R;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%lld", a[i]);
  for (L = n; i >= 1; L--) {
    for (R = L; R <= n; R++) {
      dp[L][R] = max(a[L] - dp[L + 1][R], a[R] - dp[L][R - 1]);
    }
  }
  printf("%lld\n", dp[1][n]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int N = 3e3 + 5;
ll a[N], dp[N][N];
int main() {
  int n, i, j, L, R;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (L = n; L >= 1; L--) {
    for (R = L; R <= n; R++) {
      dp[L][R] = max(a[L] - dp[L + 1][R], a[R] - dp[L][R - 1]);
    }
  }
  printf("%lld\n", dp[1][n]);
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb push_back
typedef long long ll;
typedef long double ld;

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

  ll N = 3e2 + 10, n, i, j;
  cin >> n;
  ll a[n], dp[N][N];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (i = 0; i < N; i++) {
    for (j = 0; j < N; j++) {
      dp[i][j] = 0;
    }
  }

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

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb push_back
typedef long long ll;
typedef long double ld;

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

  ll N = 3e3 + 10, n, i, j;
  cin >> n;
  ll a[n], dp[N][N];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (i = 0; i < N; i++) {
    for (j = 0; j < N; j++) {
      dp[i][j] = 0;
    }
  }

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

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

  return 0;
}
Problem p03171 (C++)

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

ll dp[3001][3001] = {};
ll a[3001];

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = n; i >= 1; i--) {
    for (int j = i; j <= n; j++) {
      int taken = n - i - j - 1;
      if (taken % 2) {
        // Jiro
        dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j]);
      } else {
        // Taro
        dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j]);
      }
    }
  }
  cout << dp[1][n] << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
typedef long long ll;

ll dp[3002][3002] = {};
ll a[3001];

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = n; i >= 1; i--) {
    for (int j = i; j <= n; j++) {
      int taken = n - i - j - 1;
      if (taken % 2) {
        // Jiro
        dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j]);
      } else {
        // Taro
        dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j]);
      }
    }
  }
  cout << dp[1][n] << endl;
  return 0;
}
Problem p03171 (C++)

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

#define MOD 1000000007
#define ll long long int
#define off                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)

ll dp[3000][3000];

int main() {
  off;

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

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

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

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

  return 0;
}


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

#define MOD 1000000007
#define ll long long int
#define off                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)

ll dp[3000][3000];

int main() {
  off;

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

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

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

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

  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
// L - Deque  https://atcoder.jp/contests/dp/tasks/dp_l
/* 入力例
4
10 80 90 30
// →出力 10
*/
#include <bits/stdc++.h>
using namespace std;

int N;
int a[4];
long long int mem[4][4];
int memflag[4][4];

long long int dp(int l, int r) {
  if (memflag[l][r] == 1) {
    return mem[l][r];
  } else if (l == r) {
    mem[l][r] = a[l];
    memflag[l][r] = 1;
    return mem[l][r];
  } else {
    mem[l][r] = max(dp(l + 1, r) * (-1) + a[l], dp(l, r - 1) * (-1) + a[r]);
    memflag[l][r] = 1;
    return mem[l][r];
  }
}

int main(void) {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    for (int j = 0; j < N; j++) {
      memflag[i][j] = 0;
    }
  }

  cout << dp(0, N - 1);
  cout << "";
  return 0;
}


Fixed submission:
// L - Deque  https://atcoder.jp/contests/dp/tasks/dp_l
/* 入力例
4
10 80 90 30
// →出力 10
*/
#include <bits/stdc++.h>
using namespace std;

int N;
int a[3000];
long long int mem[3000][3000];
int memflag[3000][3000];

long long int dp(int l, int r) {
  if (memflag[l][r] == 1) {
    return mem[l][r];
  } else if (l == r) {
    mem[l][r] = a[l];
    memflag[l][r] = 1;
    return mem[l][r];
  } else {
    mem[l][r] = max(dp(l + 1, r) * (-1) + a[l], dp(l, r - 1) * (-1) + a[r]);
    memflag[l][r] = 1;
    return mem[l][r];
  }
}

int main(void) {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    for (int j = 0; j < N; j++) {
      memflag[i][j] = 0;
    }
  }

  cout << dp(0, N - 1);
  cout << "";
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define endl '\n'
#define pll pair<int, int>
#define vll vector<int>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define hell 1000000007
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define int int64_t
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, i, j, k, l, sum = 0, flag = 0, ans = 0, a[101], v[101], m;
int dp[3005][3005][2];
int is[3005][3005][2];
int f(int i, int j, int turn) {
  if (i > j)
    return 0;
  if (i == j)
    return (turn == 1 ? a[i] : -a[i]);
  if (is[i][j][turn] != -1)
    return dp[i][j][turn];
  is[i][j][turn] = 1;
  if (turn) {
    return dp[i][j][turn] =
               max(a[i] + f(i + 1, j, turn ^ 1), a[j] + f(i, j - 1, turn ^ 1));
  } else {
    return dp[i][j][turn] = min(-a[i] + f(i + 1, j, turn ^ 1),
                                -a[j] + f(i, j - 1, turn ^ 1));
  }
}
void solve() {
  memset(is, -1, sizeof dp);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cout << f(0, n - 1, 1) << endl;
}
signed main() {
  ios int test = 1;
  // cin>>test;
  while (test--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define endl '\n'
#define pll pair<int, int>
#define vll vector<int>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define hell 1000000007
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define int int64_t
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, i, j, k, l, sum = 0, flag = 0, ans = 0, a[3001], v[101], m;
int dp[3005][3005][2];
int is[3005][3005][2];
int f(int i, int j, int turn) {
  if (i > j)
    return 0;
  if (i == j)
    return (turn == 1 ? a[i] : -a[i]);
  if (is[i][j][turn] != -1)
    return dp[i][j][turn];
  is[i][j][turn] = 1;
  if (turn) {
    return dp[i][j][turn] =
               max(a[i] + f(i + 1, j, turn ^ 1), a[j] + f(i, j - 1, turn ^ 1));
  } else {
    return dp[i][j][turn] = min(-a[i] + f(i + 1, j, turn ^ 1),
                                -a[j] + f(i, j - 1, turn ^ 1));
  }
}
void solve() {
  memset(is, -1, sizeof dp);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cout << f(0, n - 1, 1) << endl;
}
signed main() {
  ios int test = 1;
  // cin>>test;
  while (test--) {
    solve();
  }
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll N = 3090, OO = 1e9;
ll a[N], n, dp[N][N], sum;
ll Dp(int i, int j) {

  if (i > j) {

    return 0;
  }
  ll &ret = dp[i][j];
  ret = max(a[i] - Dp(i + 1, j), a[j] - Dp(i, j - 1));

  return ret;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {

    cin >> a[i], sum += a[i];
  }
  memset(dp, -1, sizeof dp);
  cout << Dp(0, n - 1);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll N = 3090, OO = 1e9;
ll a[N], n, dp[N][N], sum;
ll Dp(int i, int j) {

  if (i > j) {

    return 0;
  }
  ll &ret = dp[i][j];
  if (ret != -1)
    return ret;
  ret = 0;
  ret = max(a[i] - Dp(i + 1, j), a[j] - Dp(i, j - 1));

  return ret;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {

    cin >> a[i], sum += a[i];
  }
  memset(dp, -1, sizeof dp);
  cout << Dp(0, n - 1);
  return 0;
}
Problem p03171 (C++)

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

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

  vector<vector<ll>> dp(N + 1, vector<ll>(N + 1, 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) = max(dp[i,j-1)+a[i] , a[i]+dp[i+1, j))
        dp[i][j] = max(dp[i][j - 1] + a[j - 1], a[i] + dp[i + 1][j]);
      } else {
        // dp[i,j) = min(dp[i,j-1)-a[i] , -a[i]+dp[i+1, j))
        dp[i][j] = min(dp[i][j - 1] - a[j - 1], -a[i] + dp[i + 1][j]);
      }
    }
  }
  cout << dp[0][N] << endl;

  return 0;
}


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

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

  vector<vector<ll>> dp(N + 1, vector<ll>(N + 1, 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) = max(dp[i,j-1)+a[i] , a[i]+dp[i+1, j))
        dp[i][j] = max(dp[i][j - 1] + a[j - 1], a[i] + dp[i + 1][j]);
      } else {
        // dp[i,j) = min(dp[i,j-1)-a[i] , -a[i]+dp[i+1, j))
        dp[i][j] = min(dp[i][j - 1] - a[j - 1], -a[i] + dp[i + 1][j]);
      }
    }
  }
  cout << dp[0][N] << endl;

  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
// deque
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const int mod = 1e9 + 7;
const int inf = (1 << 30);
string toBin(ll a) {
  int i;
  string s1 = "";
  while (a > 0) {
    if (a & 1) {
      s1 = "1" + s1;
    } else {
      s1 = "0" + s1;
    }
    a >>= 1;
  }
  return s1;
}
ll dp[5000][5000];

ll fun(ll arr[], ll s, ll e, bool t) {
  if (s > e) {
    return 0;
  }
  if (dp[s][e] != -1) {
    return dp[s][e];
  }
  ll c = 0;
  if (t) {
    ll a = arr[s] + fun(arr, s + 1, e, !t);
    ll b = arr[e] + fun(arr, s, e - 1, !t);
    c = max(a, b);
  } else {
    ll a = fun(arr, s + 1, e, !t);
    ll b = fun(arr, s, e - 1, !t);
    c = min(a, b);
  }
  return dp[s][e] = c;
}

void solve() {
  ll i, n, sum = 0;
  cin >> n;
  ll arr[n];
  for (i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  memset(dp, -1, sizeof(dp));
  ll x = fun(arr, 0, n - 1, true);
  sum = accumulate(arr, arr + n, 0LL);
  cout << (2 * x - sum) << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("../input.txt", "r", stdin);
  freopen("../output.txt", "w", stdout);
#endif
  solve();
  return 0;
}

Fixed submission:
// deque
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const int mod = 1e9 + 7;
const int inf = (1 << 30);
string toBin(ll a) {
  int i;
  string s1 = "";
  while (a > 0) {
    if (a & 1) {
      s1 = "1" + s1;
    } else {
      s1 = "0" + s1;
    }
    a >>= 1;
  }
  return s1;
}
ll dp[5000][5000];

ll fun(ll arr[], ll s, ll e, bool t) {
  if (s > e) {
    return 0;
  }
  if (dp[s][e] != -1) {
    return dp[s][e];
  }
  ll c = 0;
  if (t) {
    ll a = arr[s] + fun(arr, s + 1, e, !t);
    ll b = arr[e] + fun(arr, s, e - 1, !t);
    c = max(a, b);
  } else {
    ll a = fun(arr, s + 1, e, !t);
    ll b = fun(arr, s, e - 1, !t);
    c = min(a, b);
  }
  return dp[s][e] = c;
}

void solve() {
  ll i, n, sum = 0;
  cin >> n;
  ll arr[n];
  for (i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  memset(dp, -1, sizeof(dp));
  ll x = fun(arr, 0, n - 1, true);
  sum = accumulate(arr, arr + n, 0LL);
  cout << (2 * x - sum) << endl;
}

int main() {
  solve();
  return 0;
}
Problem p03171 (C++)

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

long long f(vector<long long> &v, int i, int j, vector<vector<long long>> dp) {
  if (i == j)
    return v[i];

  if (dp[i][j] != -1)
    return dp[i][j];

  dp[i][j] = max(v[i] - f(v, i + 1, j, dp), v[j] - f(v, i, j - 1, dp));

  return dp[i][j];
}
int main() {
  int N;
  cin >> N;
  vector<long long> v(N, 0);

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

  vector<vector<long long>> dp(N, vector<long long>(N, -1));
  cout << f(v, 0, N - 1, dp) << endl;
  return 0;
}

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

long long f(vector<long long> &v, int i, int j, vector<vector<long long>> &dp) {
  if (i == j)
    return v[i];

  if (dp[i][j] != -1)
    return dp[i][j];

  dp[i][j] = max(v[i] - f(v, i + 1, j, dp), v[j] - f(v, i, j - 1, dp));

  return dp[i][j];
}
int main() {
  int N;
  cin >> N;
  vector<long long> v(N, 0);

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

  vector<vector<long long>> dp(N, vector<long long>(N, -1));
  cout << f(v, 0, N - 1, dp) << endl;
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
//============================================================================
// Name        : ProjG.cpp
// Author      : Yong Kiat
// Version     :
// Copyright   : By Algotae.
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <bits/stdc++.h>

using namespace std;

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

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

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

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

  cout << dp[n][0];
}


Fixed submission:
//============================================================================
// Name        : ProjG.cpp
// Author      : Yong Kiat
// Version     :
// Copyright   : By Algotae.
// Description : Hello World in C++, Ansi-style
//============================================================================

#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_base::sync_with_stdio(0);
  cin.tie(0);

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

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

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

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

using namespace std;

typedef long long int ll;

ll dp[3000][3000];
bool dpv[3000][3000];
vector<int> deq(3000);

ll solve(int l, int r, int flag) { // flag = 1 -> Taro
  if (dpv[l][r])
    return dp[l][r];
  if (l > r)
    return 0;
  if (l == r) {
    dpv[l][r] = true;
    if (flag)
      return dp[l][r] = deq[l];
    else
      return dp[l][r] = -deq[l];
  }
  if (flag) {
    return dp[l][r] = max((ll)deq[l] + solve(l + 1, r, 1 - flag),
                          (ll)deq[r] + solve(l, r - 1, 1 - flag));
  } else {
    return dp[l][r] = min(solve(l + 1, r, 1 - flag) - (ll)deq[l],
                          solve(l, r - 1, 1 - flag) - (ll)deq[r]);
  }
}

int main() {
  int n, tp;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> deq[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dpv[i][j] = false;
    }
  }
  cout << solve(0, n - 1, 1) << "\n";
}

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

using namespace std;

typedef long long int ll;

ll dp[3000][3000];
bool dpv[3000][3000];
vector<int> deq(3000);

ll solve(int l, int r, int flag) { // flag = 1 -> Taro
  if (dpv[l][r])
    return dp[l][r];
  if (l > r)
    return 0;
  if (l == r) {
    dpv[l][r] = true;
    if (flag)
      return dp[l][r] = deq[l];
    else
      return dp[l][r] = -deq[l];
  }
  dpv[l][r] = true;
  if (flag) {
    return dp[l][r] = max((ll)deq[l] + solve(l + 1, r, 1 - flag),
                          (ll)deq[r] + solve(l, r - 1, 1 - flag));
  } else {
    return dp[l][r] = min(solve(l + 1, r, 1 - flag) - (ll)deq[l],
                          solve(l, r - 1, 1 - flag) - (ll)deq[r]);
  }
}

int main() {
  int n, tp;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> deq[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dpv[i][j] = false;
    }
  }
  cout << solve(0, n - 1, 1) << "\n";
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define f first
#define s second
#define mp make_pair
#define sz size()
#define ll long long
int n;
bool was[3010][3010];
ll dp[3010][3010];
int a[3010];
ll calc(int l, int r) {
  if (l > r)
    return 0;
  if (l == r)
    return a[r];
  if (was[l][r])
    return dp[l][r];
  dp[l][r] = max(a[l] - calc(l + 1, r),
                 a[r] - calc(l, r - 1)); // max(áåð¸ì ïåðâûé, áåð¸ì ïîñë.)
  //	cout << "podotrezok " << l << ' ' << r << " answer is " << dp[l][r] <<
  //'\n';
  return dp[l][r];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", a + i);
  ll ans = calc(1, n);
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define f first
#define s second
#define mp make_pair
#define sz size()
#define ll long long
int n;
bool was[3010][3010];
ll dp[3010][3010];
int a[3010];
ll calc(int l, int r) {
  if (l > r)
    return 0;
  if (l == r)
    return a[r];
  if (was[l][r])
    return dp[l][r];
  was[l][r] = 1;
  dp[l][r] = max(a[l] - calc(l + 1, r),
                 a[r] - calc(l, r - 1)); // max(áåð¸ì ïåðâûé, áåð¸ì ïîñë.)
  //	cout << "podotrezok " << l << ' ' << r << " answer is " << dp[l][r] <<
  //'\n';
  return dp[l][r];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", a + i);
  ll ans = calc(1, n);
  printf("%lld", ans);
  return 0;
}
Problem p03171 (C++)

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

int n;

vector<int> vec;
int dp[3000][3000];

int solve(int i, int j) {
  if (i == j) {
    return vec[i];
  }
  if (dp[i][j] != -1)
    return dp[i][j];
  int ans = max(vec[i] - solve(i + 1, j), vec[j] - solve(i, j - 1));
  return ans;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  vec.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> vec[i];
  }
  for (int i = 0; i < 3000; i++) {
    for (int j = 0; j < 3000; j++) {
      dp[i][j] = -1;
    }
  }
  cout << solve(0, n - 1);
  return 0;
}


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

int n;

vector<int> vec;
int dp[3000][3000];

int solve(int i, int j) {
  if (i == j) {
    return vec[i];
  }
  if (dp[i][j] != -1)
    return dp[i][j];
  int ans = max(vec[i] - solve(i + 1, j), vec[j] - solve(i, j - 1));
  return dp[i][j] = ans;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  vec.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> vec[i];
  }
  for (int i = 0; i < 3000; i++) {
    for (int j = 0; j < 3000; j++) {
      dp[i][j] = -1;
    }
  }
  cout << solve(0, n - 1);
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;

ll N;
ll A[3000];
ll dp[3000 + 1][3000 + 1];

ll taro(ll i, ll j);
ll jiro(ll i, ll j);

ll taro(ll i, ll j) {
  // cout << "taro: " << i << " : " << j << endl;
  if (i > j) {
    return 0;
  }

  dp[i][j] = max(A[i] + jiro(i + 1, j), A[j] + jiro(i, j - 1));

  return dp[i][j];
}

ll jiro(ll i, ll j) {
  // cout << "jiro: " << i << " : " << j << endl;
  if (i > j) {
    return 0;
  }

  return min(taro(i + 1, j) - A[i], taro(i, j - 1) - A[j]);
}

int main() {
  cin >> N;

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

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

  cout << taro(0, N - 1) << endl;
}

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

typedef long long ll;

ll N;
ll A[3000];
ll dp[3000 + 1][3000 + 1];

ll taro(ll i, ll j);
ll jiro(ll i, ll j);

ll taro(ll i, ll j) {
  // cout << "taro: " << i << " : " << j << endl;
  if (i > j) {
    return 0;
  }
  if (dp[i][j] != -1) {
    return dp[i][j];
  }

  dp[i][j] = max(A[i] + jiro(i + 1, j), A[j] + jiro(i, j - 1));

  return dp[i][j];
}

ll jiro(ll i, ll j) {
  // cout << "jiro: " << i << " : " << j << endl;
  if (i > j) {
    return 0;
  }

  return min(taro(i + 1, j) - A[i], taro(i, j - 1) - A[j]);
}

int main() {
  cin >> N;

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

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

  cout << taro(0, N - 1) << endl;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#define SM 3005
using namespace std;
long long n, a[SM], F[SM][SM][3];
long Try(long L, long R, int x) {
  if (L > R)
    return 0;
  if (x == 1) {
    F[L][R][x] = Try(L + 1, R, (x + 1) % 2) + a[L];
    F[L][R][x] = max(F[L][R][x], Try(L, R - 1, (x + 1) % 2) + a[R]);
  } else {
    F[L][R][x] = Try(L + 1, R, (x + 1) % 2) - a[L];
    F[L][R][x] = min(F[L][R][x], Try(L, R - 1, (x + 1) % 2) - a[R]);
  }
  return F[L][R][x];
}
int main() {
  long long i, j, x;
  cin >> n;
  for (i = 1; i <= n; ++i)
    cin >> a[i];
  cout << Try(1, n, 1);
  return 0;
}


Fixed submission:
#include <iostream>
#define SM 3005
using namespace std;
long long n, a[SM], F[SM][SM][3];
long Try(long L, long R, int x) {
  if (L > R)
    return 0;
  if (F[L][R][x] != 0)
    return F[L][R][x];
  if (x == 1) {
    F[L][R][x] = Try(L + 1, R, (x + 1) % 2) + a[L];
    F[L][R][x] = max(F[L][R][x], Try(L, R - 1, (x + 1) % 2) + a[R]);
  } else {
    F[L][R][x] = Try(L + 1, R, (x + 1) % 2) - a[L];
    F[L][R][x] = min(F[L][R][x], Try(L, R - 1, (x + 1) % 2) - a[R]);
  }
  return F[L][R][x];
}
int main() {
  long long i, j, x;
  cin >> n;
  for (i = 1; i <= n; ++i)
    cin >> a[i];
  cout << Try(1, n, 1);
  return 0;
}