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

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

int N, P, A[109], dp[109];

int main() {
  cin >> N >> P;
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  for (int i = 1; i <= P; i++) {
    bool used[109];
    for (int j = 0; j < 109; j++)
      used[j] = false;
    for (int j = 1; j <= N; j++) {
      if (i >= A[j])
        used[dp[i - A[j]]] = true;
    }
    for (int j = 0; j < 109; j++) {
      if (used[j] == false) {
        dp[i] = j;
        break;
      }
    }
  }
  if (dp[P] >= 1)
    cout << "First" << endl;
  else
    cout << "Second" << endl;
  return 0;
}

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

int N, P, A[100009], dp[100009];

int main() {
  cin >> N >> P;
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  for (int i = 1; i <= P; i++) {
    bool used[109];
    for (int j = 0; j < 109; j++)
      used[j] = false;
    for (int j = 1; j <= N; j++) {
      if (i >= A[j])
        used[dp[i - A[j]]] = true;
    }
    for (int j = 0; j < 109; j++) {
      if (used[j] == false) {
        dp[i] = j;
        break;
      }
    }
  }
  if (dp[P] >= 1)
    cout << "First" << endl;
  else
    cout << "Second" << endl;
  return 0;
}
Problem p03170 (C++)

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

const long long MAXN = 3001;

int dp[MAXN];
int arr[MAXN];

int main() {
  int n, k;
  cin >> n >> k;
  int mine = 1000000000;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    mine = min(arr[i], mine);
  }
  for (int i = mine; i <= k; i++) {
    for (int j = 0; j < n; j++) {
      if (i - arr[j] >= 0) {
        if (!dp[i - arr[j]]) {
          dp[i] = 1;
        }
      }
    }
  }
  if (dp[k]) {
    cout << "First";
  } else {
    cout << "Second";
  }
}


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

const long long MAXN = 100010;

int dp[MAXN];
int arr[MAXN];

int main() {
  int n, k;
  cin >> n >> k;
  int mine = 1000000000;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    mine = min(arr[i], mine);
  }
  for (int i = mine; i <= k; i++) {
    for (int j = 0; j < n; j++) {
      if (i - arr[j] >= 0) {
        if (!dp[i - arr[j]]) {
          dp[i] = 1;
        }
      }
    }
  }
  if (dp[k]) {
    cout << "First";
  } else {
    cout << "Second";
  }
}
Problem p03170 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const int MOD = 1e9 + 7;
#define INF ((1 << 30) - 1)
#define LINF (1LL << 30)

bool dp[110000];
int a[110];
int n, k;

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i <= k; i++) {
    dp[i] = false;
    for (int j = 0; j < n; j++) {
      if (i - a[i] >= 0)
        dp[i] |= !dp[i - a[j]];
    }
  }
  if (dp[k])
    cout << "First" << endl;
  else
    cout << "Second" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const int MOD = 1e9 + 7;
#define INF ((1 << 30) - 1)
#define LINF (1LL << 30)

bool dp[110000];
int a[110];
int n, k;

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i <= k; i++) {
    dp[i] = false;
    for (int j = 0; j < n; j++) {
      if (i - a[j] >= 0)
        dp[i] |= !dp[i - a[j]];
    }
  }
  if (dp[k])
    cout << "First" << endl;
  else
    cout << "Second" << endl;
  return 0;
}
Problem p03170 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
typedef long long int ll;
using namespace std;
#define P 1000000007
#define rep(i, n) for (i = 0; i < n; ++i)
#define repp(i, a, n) for (i = a; i < n; ++i)
#define repr(i, a, n) for (i = a; i >= n; --i)

ll binpowmod(ll a, ll b, ll m) {
  a %= m;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}

ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}

ll modInverse(ll n, ll p) { return binpowmod(n, p - 2, p); }

ll calc(ll dp[], ll k, ll a[], ll n) {
  if (k < a[1])
    return -1;
  ll i;
  for (i = 1; i <= n; ++i) {
    if (dp[k - a[i]] == 0)
      dp[k - a[i]] = calc(dp, k - a[i], a, n);
    if (dp[k - a[i]] == -1)
      return 1;
  }
  return -1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, i, j, k;
  cin >> n >> k;
  ll a[n + 1];

  repp(i, 1, n + 1) cin >> a[i];
  ll dp[k + 1];
  memset(dp, 0, sizeof(dp));
  ll ans = calc(dp, k, a, n);
  // rep(i,n+1) cout << dp[i] << endl ;
  // cout << ans ;
  if (ans == 1)
    cout << "First";
  else
    cout << "Second";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
typedef long long int ll;
using namespace std;
#define P 1000000007
#define rep(i, n) for (i = 0; i < n; ++i)
#define repp(i, a, n) for (i = a; i < n; ++i)
#define repr(i, a, n) for (i = a; i >= n; --i)

ll binpowmod(ll a, ll b, ll m) {
  a %= m;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}

ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}

ll modInverse(ll n, ll p) { return binpowmod(n, p - 2, p); }

ll calc(ll dp[], ll k, ll a[], ll n) {
  if (k < a[1])
    return -1;
  ll i;
  for (i = 1; i <= n && k >= a[i]; ++i) {
    if (dp[k - a[i]] == 0)
      dp[k - a[i]] = calc(dp, k - a[i], a, n);
    if (dp[k - a[i]] == -1)
      return 1;
  }
  return -1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, i, j, k;
  cin >> n >> k;
  ll a[n + 1];

  repp(i, 1, n + 1) cin >> a[i];
  ll dp[k + 1];
  memset(dp, 0, sizeof(dp));
  ll ans = calc(dp, k, a, n);
  // rep(i,n+1) cout << dp[i] << endl ;
  // cout << ans ;
  if (ans == 1)
    cout << "First";
  else
    cout << "Second";
  return 0;
}
Problem p03170 (C++)

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

#define ll long long
ll mod = 1000000007;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll T;
  T = 6;
start:
  while (T--) {
    ll n, m;
    cin >> n >> m;
    ll a[n];
    for (ll i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    ll dp[m + 1];
    for (ll i = 0; i < a[0]; i++) {
      dp[i] = 2;
    }
    for (ll i = a[0]; i <= m; i++) {
      dp[i] = 2;
      for (ll j = 0; j < n; j++) {
        if (a[j] <= i) {
          ll t = dp[i - a[j]];
          if (t == 2) {
            dp[i] = 1;
            break;
          }
        } else {
          break;
        }
      }
      // cout<<i<<"--"<<dp[i]<<endl;
    }
    if (dp[m] == 2) {
      cout << "Second" << endl;
    } else {
      cout << "First" << endl;
    }
  }
  return 0;
}

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

#define ll long long
ll mod = 1000000007;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll T;
  T = 1;
start:
  while (T--) {
    ll n, m;
    cin >> n >> m;
    ll a[n];
    for (ll i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    ll dp[m + 1];
    for (ll i = 0; i < a[0]; i++) {
      dp[i] = 2;
    }
    for (ll i = a[0]; i <= m; i++) {
      dp[i] = 2;
      for (ll j = 0; j < n; j++) {
        if (a[j] <= i) {
          ll t = dp[i - a[j]];
          if (t == 2) {
            dp[i] = 1;
            break;
          }
        } else {
          break;
        }
      }
      // cout<<i<<"--"<<dp[i]<<endl;
    }
    if (dp[m] == 2) {
      cout << "Second" << endl;
    } else {
      cout << "First" << endl;
    }
  }
  return 0;
}
Problem p03170 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, k;
const int MAXN = 1e5 + 5;
int vis[MAXN];
vector<int> arr;
int main() {
  cin >> n >> k;
  arr.resize(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  for (int i = 0; i <= k; i++)
    for (auto val : arr)
      if (!vis[i - val] && i - val >= 0)
        vis[i] = true;
  puts(vis[k] ? "First\n" : "Second\n");
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, k;
const int MAXN = 1e5 + 5;
int vis[MAXN];
vector<int> arr;
int main() {
  cin >> n >> k;
  arr.resize(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  for (int i = 0; i <= k; i++)
    for (auto val : arr)
      if (i - val >= 0 && !vis[i - val])
        vis[i] = true;
  puts(vis[k] ? "First\n" : "Second\n");
}
Problem p03170 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

int N, K;
int a[101];
bool dp[2][100010], vis[101][100010];

// true -> sente, false -> gote
bool rec(int cur, int k) {
  if (vis[cur][k])
    return dp[cur][k];
  if (k == K) {
    return cur % 2 == 1;
  }

  if (cur % 2 == 0) {
    bool sente = false;
    for (int i = 0; i < N; ++i) {
      if (k + a[i] > K)
        break;
      sente |= rec((cur + 1) % 2, k + a[i]);
    }
    return dp[cur][k] = sente;
  } else {
    bool sente = true;
    for (int i = 0; i < N; ++i) {
      if (k + a[i] > K)
        break;
      sente &= rec((cur + 1) % 2, k + a[i]);
    }
    return dp[cur][k] = sente;
  }
}

signed main() {
  cin >> N >> K;

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

  if (rec(0, 0)) {
    cout << "First" << endl;
  } else {
    cout << "Second" << endl;
  }
}


Fixed submission:
#include <iostream>

using namespace std;

int N, K;
int a[101];
bool dp[2][100010], vis[101][100010];

// true -> sente, false -> gote
bool rec(int cur, int k) {
  if (vis[cur][k])
    return dp[cur][k];
  if (k == K) {
    return cur % 2 == 1;
  }

  vis[cur][k] = true;

  if (cur % 2 == 0) {
    bool sente = false;
    for (int i = 0; i < N; ++i) {
      if (k + a[i] > K)
        break;
      sente |= rec((cur + 1) % 2, k + a[i]);
    }
    return dp[cur][k] = sente;
  } else {
    bool sente = true;
    for (int i = 0; i < N; ++i) {
      if (k + a[i] > K)
        break;
      sente &= rec((cur + 1) % 2, k + a[i]);
    }
    return dp[cur][k] = sente;
  }
}

signed main() {
  cin >> N >> K;

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

  if (rec(0, 0)) {
    cout << "First" << endl;
  } else {
    cout << "Second" << endl;
  }
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp1[3001][3001], dp2[3001][3001];
ll a[3001];
ll f(ll i, ll j, ll y) {
  if (y == -1) {
    if (dp2[i][j] != -1) {
      return dp2[i][j];
    }
    if (i == j) {
      return dp2[i][j] = y * a[i];
    }
    return dp2[i][j] =
               min(y * a[i] + f(i + 1, j, -y), y * a[j] + f(i, j - 1, -y));
  }
  if (y == 1) {
    if (dp1[i][j] != -1) {
      return dp1[i][j];
    }
    if (i == j) {
      return dp1[i][j] = y * a[i];
    }
    return dp1[i][j] =
               max(y * a[i] + f(i + 1, j, -y), y * a[j] + f(i, j - 1, -y));
  }
}
signed main() {
  memset(dp1, -1, sizeof dp1);
  memset(dp2, -1, sizeof dp2);
  ll n;
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  cout << max(a[0] + f(1, n - 1, -1), a[n - 1] + f(0, n - 2, -1));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp1[3001][3001], dp2[3001][3001];
ll a[3001];
ll f(ll i, ll j, ll y) {
  if (y == -1) {
    if (dp2[i][j] != -1) {
      return dp2[i][j];
    }
    if (i == j) {
      return dp2[i][j] = y * a[i];
    }
    return dp2[i][j] =
               min(y * a[i] + f(i + 1, j, -y), y * a[j] + f(i, j - 1, -y));
  }
  if (y == 1) {
    if (dp1[i][j] != -1) {
      return dp1[i][j];
    }
    if (i == j) {
      return dp1[i][j] = y * a[i];
    }
    return dp1[i][j] =
               max(y * a[i] + f(i + 1, j, -y), y * a[j] + f(i, j - 1, -y));
  }
}
signed main() {
  memset(dp1, -1, sizeof dp1);
  memset(dp2, -1, sizeof dp2);
  ll n;
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  if (n == 1) {
    cout << a[0] << '\n';
    return 0;
  }
  cout << max(a[0] + f(1, n - 1, -1), a[n - 1] + f(0, n - 2, -1));
  return 0;
}
Problem p03171 (C++)

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

// def
#define debug(x) cout << #x << ": " << x << endl
#define out(x) cout << x << endl
#define repeat(i, a, b) for (int i = (a); i < (b); i++)
#define revrepeat(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define CYES cout << "Yes" << endl
#define CNO cout << "No" << endl
#define SPC(x) cout << fixed << setprecision(x)
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<ll> vl;
typedef vector<vector<ll>> vll;
typedef pair<int, int> P;
typedef complex<ld> Point;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MAX_V = 1e5 + 5;
const int MAX_N = 1e5 + 5;
const double PI = acos(-1);

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;
}

ll a[3005];
ll dp[3005][3005];
ll f(int l, int r) {
  if (l == r)
    return a[l];

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

int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  out(f(0, n - 1));
}


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

// def
#define debug(x) cout << #x << ": " << x << endl
#define out(x) cout << x << endl
#define repeat(i, a, b) for (int i = (a); i < (b); i++)
#define revrepeat(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define CYES cout << "Yes" << endl
#define CNO cout << "No" << endl
#define SPC(x) cout << fixed << setprecision(x)
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<ll> vl;
typedef vector<vector<ll>> vll;
typedef pair<int, int> P;
typedef complex<ld> Point;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MAX_V = 1e5 + 5;
const int MAX_N = 1e5 + 5;
const double PI = acos(-1);

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;
}

ll a[3005];
ll dp[3005][3005];
ll f(int l, int r) {
  if (dp[l][r])
    return dp[l][r];
  if (l == r)
    return a[l];

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

int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  out(f(0, n - 1));
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define int long long
#define pb push_back
#define mod 1000000007
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define inf 1e18
#define pre(x, y) fixed << setprecision(y) << x
#define pq priority_queue<int>
#define mpq priority_queue<int, vector<int>, greater<int>>
#define gcd(x, y) __gcd(x, y)
#define mp make_pair

int solveGame(int a[], vector<vector<int>> dp, int i, int j, int n) {
  if (i > j)
    return 0;
  if (i == j)
    return dp[i][i] = a[i];
  else if (dp[i][j] != -1)
    return dp[i][j];
  else {
    int x = a[i] + min(solveGame(a, dp, i + 1, j - 1, n),
                       solveGame(a, dp, i + 2, j, n));
    int y = a[j] + min(solveGame(a, dp, i + 1, j - 1, n),
                       solveGame(a, dp, i, j - 2, n));
    return dp[i][j] = max(x, y);
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, i, j, k;
  cin >> n;
  int a[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  vector<vector<int>> dp(n, vector<int>(n));
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++)
      dp[i][j] = -1;
  }
  k = solveGame(a, dp, 0, n - 1, n);
  int sum = 0;
  for (i = 0; i < n; i++)
    sum += a[i];
  cout << k - (sum - k) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define int long long
#define pb push_back
#define mod 1000000007
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define inf 1e18
#define pre(x, y) fixed << setprecision(y) << x
#define pq priority_queue<int>
#define mpq priority_queue<int, vector<int>, greater<int>>
#define gcd(x, y) __gcd(x, y)
#define mp make_pair

int solveGame(int a[], vector<vector<int>> &dp, int i, int j, int n) {
  if (i > j)
    return 0;
  if (i == j)
    return dp[i][i] = a[i];
  else if (dp[i][j] != -1)
    return dp[i][j];
  else {
    int x = a[i] + min(solveGame(a, dp, i + 1, j - 1, n),
                       solveGame(a, dp, i + 2, j, n));
    int y = a[j] + min(solveGame(a, dp, i + 1, j - 1, n),
                       solveGame(a, dp, i, j - 2, n));
    return dp[i][j] = max(x, y);
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, i, j, k;
  cin >> n;
  int a[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  vector<vector<int>> dp(n, vector<int>(n));
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++)
      dp[i][j] = -1;
  }
  k = solveGame(a, dp, 0, n - 1, n);
  int sum = 0;
  for (i = 0; i < n; i++)
    sum += a[i];
  cout << k - (sum - k) << endl;
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
const ll mod = 1e9 + 7;
const ll inf = 9223372036854775807;

ll arr[2000], memo[2000][2000], bl[2000][2000], maxi, n, ans, dif, sum;

ll dp(ll x, ll y) {
  ll depan = 0;
  ll belakang = 0;
  if (x == y) {
    return arr[x];
  }
  if (x > y) {
    return 0;
  }
  if (bl[x][y] == 1) {
    return memo[x][y];
  }
  bl[x][y] = 1;
  depan = arr[x] + min(dp(x + 2, y), dp(x + 1, y - 1));
  belakang = arr[y] + min(dp(x, y - 2), dp(x + 1, y - 1));
  memo[x][y] = max(depan, belakang);
  return memo[x][y];
}

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

  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  ans = dp(1, n);
  dif = sum - ans;
  dif = ans - dif;
  cout << dif << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
const ll mod = 1e9 + 7;
const ll inf = 9223372036854775807;

ll arr[3000], memo[3000][3000], bl[3000][3000], maxi, n, ans, dif, sum;

ll dp(ll x, ll y) {
  ll depan = 0;
  ll belakang = 0;
  if (x == y) {
    return arr[x];
  }
  if (x > y) {
    return 0;
  }
  if (bl[x][y] == 1) {
    return memo[x][y];
  }
  bl[x][y] = 1;
  depan = arr[x] + min(dp(x + 2, y), dp(x + 1, y - 1));
  belakang = arr[y] + min(dp(x, y - 2), dp(x + 1, y - 1));
  memo[x][y] = max(depan, belakang);
  return memo[x][y];
}

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

  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  ans = dp(1, n);
  dif = sum - ans;
  dif = ans - dif;
  cout << dif << '\n';
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define exrep(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;

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

  vl a(n);
  rep(i, n) { cin >> a[i]; }

  vvl dp(
      n,
      vl(n + 1)); // dp[i][j] :
                  // 区間a[i]～a[j-1]から始めて二人が最適に行動したときのx-yの値
  exrep(len, 1, n) {
    for (ll i = 0; i + len <= n; i++) {
      ll j = i + len;
      // 先手
      if ((n - len) % 2 == 0) {
        dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j - 1]);
      }
      // 後手
      else {
        dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j - 1]);
      }
    }
  }

  out(dp[0][n]);
  re0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define exrep(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;

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

  vl a(n);
  rep(i, n) { cin >> a[i]; }

  vvl dp(
      n + 1,
      vl(n + 1)); // dp[i][j] :
                  // 区間a[i]～a[j-1]から始めて二人が最適に行動したときのx-yの値
  exrep(len, 1, n) {
    for (ll i = 0; i + len <= n; i++) {
      ll j = i + len;
      // 先手
      if ((n - len) % 2 == 0) {
        dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j - 1]);
      }
      // 後手
      else {
        dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j - 1]);
      }
    }
  }

  out(dp[0][n]);
  re0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
#define li long long int
#define mod 1000000000 + 7
li n, k;
li a[3005];
li dp[3005][3005];
li f(li i, li j) {
  if (i == j) {
    return a[i];
  }
  if (i == j - 1) {
    return max(a[i], a[j]);
  }
  li op1 = a[i] + min(f(i + 2, j), f(i + 1, j - 1));
  li op2 = a[j] + min(f(i + 1, j - 1), f(i, j - 2));
  li ans = max(op1, op2);
  return dp[i][j] = ans;
}

int main() {
  cin >> n;

  memset(dp, -1, sizeof(dp));
  li i, s = 0;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
  }

  cout << 2 * f(0, n - 1) - s;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
#define li long long int
#define mod 1000000000 + 7
li n, k;
li a[3005];
li dp[3005][3005];
li f(li i, li j) {
  if (i == j) {
    return a[i];
  }
  if (i == j - 1) {
    return max(a[i], a[j]);
  }
  if (dp[i][j] != -1) {
    return dp[i][j];
  }
  li op1 = a[i] + min(f(i + 2, j), f(i + 1, j - 1));
  li op2 = a[j] + min(f(i + 1, j - 1), f(i, j - 2));
  li ans = max(op1, op2);
  return dp[i][j] = ans;
}

int main() {
  cin >> n;

  memset(dp, -1, sizeof(dp));
  li i, s = 0;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
  }

  cout << 2 * f(0, n - 1) - s;

  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)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A[i];
  vector<vector<ll>> dp(N + 100, vector<ll>(N + 100));
  for (int width = 1; width <= N; width++) {
    for (int l = 0; l < N; l++) {
      int r = l + width;
      if ((N & 1) ^ (width & 1)) {
        // 後攻
        dp[l][r] = min(dp[l + 1][r] - A[l], dp[l][r - 1] - A[r - 1]);
      } else {
        // 先行
        dp[l][r] = max(dp[l + 1][r] + A[l], dp[l][r - 1] + A[r - 1]);
      }
    }
  }
  cout << dp[0][N] << 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;
using P = pair<int, int>;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A[i];
  vector<vector<ll>> dp(N + 100, vector<ll>(N + 100));
  for (int width = 1; width <= N; width++) {
    for (int l = 0; l < N; l++) {
      int r = l + width;
      if (r > N)
        continue;
      if ((N & 1) ^ (width & 1)) {
        // 後攻
        dp[l][r] = min(dp[l + 1][r] - A[l], dp[l][r - 1] - A[r - 1]);
      } else {
        // 先行
        dp[l][r] = max(dp[l + 1][r] + A[l], dp[l][r - 1] + A[r - 1]);
      }
    }
  }
  cout << dp[0][N] << endl;
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<ll, ll> P;
#define M 1000000007
#define F first
#define S second
#define PB push_back
#define INF 100000000000000000
ll n, a[3005], dp[3005][3005][2];
int main(void) {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", a + i);
  for (int k = 0; k < n; k++) {
    for (int i = 1; i <= n; i++) {
      int l = i, r = i + k;
      dp[l][r][0] = max(dp[l + 1][r][1] + a[l], dp[l][r - 1][1] + a[r]);
      dp[l][r][1] = min(dp[l + 1][r][0] - a[l], dp[l][r - 1][0] - a[r]);
    }
  }
  printf("%lld\n", dp[1][n][0]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<ll, ll> P;
#define M 1000000007
#define F first
#define S second
#define PB push_back
#define INF 100000000000000000
ll n, a[3005], dp[3005][3005][2];
int main(void) {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", a + i);
  for (int k = 0; k < n; k++) {
    for (int i = 1; i + k <= n; i++) {
      int l = i, r = i + k;
      dp[l][r][0] = max(dp[l + 1][r][1] + a[l], dp[l][r - 1][1] + a[r]);
      dp[l][r][1] = min(dp[l + 1][r][0] - a[l], dp[l][r - 1][0] - a[r]);
    }
  }
  printf("%lld\n", dp[1][n][0]);
}
Problem p03171 (C++)

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

#define y1 as214
#define ii pair<int, int>
#define iii pair<int, ii>
#define iv pair<ii, ii>

#define fi first
#define se second
#define fr front()
#define pb push_back

#define FOR(i, x, n) for (int i = x; i <= n; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FORD(i, x, n) for (int i = x; i >= n; --i)

#define ll long long
#define oo INT_MAX

const int N = 1e3 + 5;
ll n, res;
ll a[N];
ll dp[N][N];
/// dp[i][j] là số điểm lớn nhất của người chơi thứ 1 có thẻ nhận được nếu khởi
/// đầu trong đoạn [i , j]

int main() {
  // freopen("L.inp","r",stdin);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  FOR(i, 1, n) {
    cin >> a[i];
    res += a[i];
  }
  FOR(i, 1, n)
  dp[i][i] = a[i];
  FOR(i, 1, n - 1)
  dp[i][i + 1] = max(a[i], a[i + 1]);
  FOR(r, 3, n)
  FORD(l, r - 2, 1)
  dp[l][r] = max(min(dp[l + 2][r], dp[l + 1][r - 1]) + a[l],
                 min(dp[l][r - 2], dp[l + 1][r - 1]) + a[r]);
  cout << dp[1][n] - (res - dp[1][n]) << endl;
}


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

#define y1 as214
#define ii pair<int, int>
#define iii pair<int, ii>
#define iv pair<ii, ii>

#define fi first
#define se second
#define fr front()
#define pb push_back

#define FOR(i, x, n) for (int i = x; i <= n; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FORD(i, x, n) for (int i = x; i >= n; --i)

#define ll long long
#define oo INT_MAX

const int N = 3e3 + 5;
ll n, res;
ll a[N];
ll dp[N][N];
/// dp[i][j] là số điểm lớn nhất của người chơi thứ 1 có thẻ nhận được nếu khởi
/// đầu trong đoạn [i , j]

int main() {
  // freopen("L.inp","r",stdin);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  FOR(i, 1, n) {
    cin >> a[i];
    res += a[i];
  }
  FOR(i, 1, n)
  dp[i][i] = a[i];
  FOR(i, 1, n - 1)
  dp[i][i + 1] = max(a[i], a[i + 1]);
  FOR(r, 3, n)
  FORD(l, r - 2, 1)
  dp[l][r] = max(min(dp[l + 2][r], dp[l + 1][r - 1]) + a[l],
                 min(dp[l][r - 2], dp[l + 1][r - 1]) + a[r]);
  cout << dp[1][n] - (res - dp[1][n]) << endl;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
//   Rishabh Agarwal
#include <bits/stdc++.h>
#define F first
#define S second
#define MAX 10000003
using namespace std;

typedef long long int ll;
const ll mod = 1e9 + 7;
const ll INF = 1e18;
long double PI = 3.1415926;

ll power(ll a, ll b) {
  if (b == 0) {
    return 1;
  }
  ll temp = power(a, b / 2) % mod;
  if (b % 2 == 0) {
    return (temp * temp) % mod;
  } else {
    return ((a * temp) % mod * temp) % mod;
  }
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

vector<ll> v;
vector<ll>::iterator it;
map<ll, ll> mp;

void primeFactors(ll n) {
  while (n % 2 == 0) {
    mp[2]++;
    n = n / 2;
  }
  for (ll i = 3; i <= sqrt(n); i = i + 2) {
    while (n % i == 0) {
      mp[i]++;
      n = n / i;
    }
  }
  if (n > 2) {
    mp[n]++;
  }
}

ll dp[3000][3000];

ll dp1(ll s, ll e, ll a[]) {
  if (s > e) {
    return 0;
  } else if (dp[s][e] != -1) {
    return dp[s][e];
  } else {
    return max(a[s] - dp1(s + 1, e, a), a[e] - dp1(s, e - 1, a));
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t;
  // cin>>t;
  t = 1;
  // ll c=1;
  while (t--) {
    ll n;
    cin >> n;
    ll a[n];
    for (ll x = 0; x < n; x++) {
      cin >> a[x];
    }
    memset(dp, -1, sizeof(dp));
    cout << dp1(0, n - 1, a) << "\n";
    mp.clear();
    v.clear();
    // cout<<"Case #"<<c<<": "<<ans<<"\n";
    // c++;
  }
  return 0;
}

Fixed submission:
//   Rishabh Agarwal
#include <bits/stdc++.h>
#define F first
#define S second
#define MAX 10000003
using namespace std;

typedef long long int ll;
const ll mod = 1e9 + 7;
const ll INF = 1e18;
long double PI = 3.1415926;

ll power(ll a, ll b) {
  if (b == 0) {
    return 1;
  }
  ll temp = power(a, b / 2) % mod;
  if (b % 2 == 0) {
    return (temp * temp) % mod;
  } else {
    return ((a * temp) % mod * temp) % mod;
  }
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

vector<ll> v;
vector<ll>::iterator it;
map<ll, ll> mp;

void primeFactors(ll n) {
  while (n % 2 == 0) {
    mp[2]++;
    n = n / 2;
  }
  for (ll i = 3; i <= sqrt(n); i = i + 2) {
    while (n % i == 0) {
      mp[i]++;
      n = n / i;
    }
  }
  if (n > 2) {
    mp[n]++;
  }
}

ll dp[3000][3000];

ll dp1(ll s, ll e, ll a[]) {
  if (s > e) {
    return 0;
  } else if (dp[s][e] != -1) {
    return dp[s][e];
  } else {
    dp[s][e] = max(a[s] - dp1(s + 1, e, a), a[e] - dp1(s, e - 1, a));
    return dp[s][e];
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t;
  // cin>>t;
  t = 1;
  // ll c=1;
  while (t--) {
    ll n;
    cin >> n;
    ll a[n];
    for (ll x = 0; x < n; x++) {
      cin >> a[x];
    }
    memset(dp, -1, sizeof(dp));
    cout << dp1(0, n - 1, a) << "\n";
    mp.clear();
    v.clear();
    // cout<<"Case #"<<c<<": "<<ans<<"\n";
    // c++;
  }
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
// #include <map>
#include <cmath>
#include <cstring>
#include <iomanip>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned int uii;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
typedef vector<int> vii;

const int maxn = 3333;
LL d[maxn][maxn], vis[maxn][maxn], a[maxn];

LL dp(int L, int R, int h) {
  if (vis[L][R])
    return d[L][R];
  if (L == R) {
    return d[L][R] = h * a[L];
  }
  if (h == 1) {
    d[L][R] = max(dp(L + 1, R, -h) + h * a[L], dp(L, R - 1, -h) + h * a[R]);
  } else {
    d[L][R] = min(dp(L + 1, R, -h) + h * a[L], dp(L, R - 1, -h) + h * a[R]);
  }
  return d[L][R];
}

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  dp(1, n, 1);
  cout << d[1][n] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
// #include <map>
#include <cmath>
#include <cstring>
#include <iomanip>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned int uii;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
typedef vector<int> vii;

const int maxn = 3333;
LL d[maxn][maxn], vis[maxn][maxn], a[maxn];

LL dp(int L, int R, int h) {
  if (vis[L][R])
    return d[L][R];
  vis[L][R] = 1;
  if (L == R) {
    return d[L][R] = h * a[L];
  }
  if (h == 1) {
    d[L][R] = max(dp(L + 1, R, -h) + h * a[L], dp(L, R - 1, -h) + h * a[R]);
  } else {
    d[L][R] = min(dp(L + 1, R, -h) + h * a[L], dp(L, R - 1, -h) + h * a[R]);
  }
  return d[L][R];
}

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  dp(1, n, 1);
  cout << d[1][n] << endl;
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
constexpr int maxn = 3000;
constexpr ll inf = (ll)1000000000000000;
ll dp[maxn + 1][maxn + 1][2];

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

  vector<int> v(n);
  for (int &e : v)
    cin >> e;

  for (int i = 0; i <= n; i++)
    dp[i][i][0] = dp[i][i][1] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n; j++) {
      int b = j, e = j + i;
      for (int k = 0; k < 2; k++) {
        ll &ans = dp[b][e][k];
        ans = (k == 0) ? -inf : inf;

        if (k == 0) {
          ans = max(ans, v[b] + dp[b + 1][e][!k]);
          ans = max(ans, v[e - 1] + dp[b][e - 1][!k]);
        } else {
          ans = min(ans, dp[b + 1][e][!k] - v[b]);
          ans = min(ans, dp[b][e - 1][!k] - v[e - 1]);
        }
      }
    }
  }
  cout << dp[0][n][0] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
constexpr int maxn = 3000;
constexpr ll inf = (ll)1000000000000000;
ll dp[maxn + 1][maxn + 1][2];

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

  vector<int> v(n);
  for (int &e : v)
    cin >> e;

  for (int i = 0; i <= n; i++)
    dp[i][i][0] = dp[i][i][1] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j + i <= n; j++) {
      int b = j, e = j + i;
      for (int k = 0; k < 2; k++) {
        ll &ans = dp[b][e][k];
        ans = (k == 0) ? -inf : inf;

        if (k == 0) {
          ans = max(ans, v[b] + dp[b + 1][e][!k]);
          ans = max(ans, v[e - 1] + dp[b][e - 1][!k]);
        } else {
          ans = min(ans, dp[b + 1][e][!k] - v[b]);
          ans = min(ans, dp[b][e - 1][!k] - v[e - 1]);
        }
      }
    }
  }
  cout << dp[0][n][0] << endl;
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  ll n;
  cin >> n;
  ll a[n];
  ll sum = 0;
  ll dp[3000][3000][2];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    dp[i][i][0] = a[i];
    dp[i][i][1] = 0;
  }
  for (ll rml = 1; rml < n; rml++) {
    for (ll L = 0; L < rml + L; L++) {
      int R = rml + L;
      dp[L][R][0] = max(a[L] + dp[L + 1][R][1], a[R] + dp[L][R - 1][1]);
      dp[L][R][1] = min(dp[L + 1][R][0], dp[L][R - 1][0]);
    }
  }
  ll X = dp[0][n - 1][0];
  cout << 2 * X - sum;
}


Fixed submission:
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  ll n;
  cin >> n;
  ll a[n];
  ll sum = 0;
  ll dp[3000][3000][2];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    dp[i][i][0] = a[i];
    dp[i][i][1] = 0;
  }
  for (ll rml = 1; rml < n; rml++) {
    for (ll L = 0; L + rml < n; L++) {
      int R = rml + L;
      dp[L][R][0] = max(a[L] + dp[L + 1][R][1], a[R] + dp[L][R - 1][1]);
      dp[L][R][1] = min(dp[L + 1][R][0], dp[L][R - 1][0]);
    }
  }
  ll X = dp[0][n - 1][0];
  cout << 2 * X - sum;
}
Problem p03171 (C++)

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

#define int long long

int solve(vector<int> &coins, int l, int r, vector<vector<int>> &dp) {
  if (l > r)
    return 0;
  if (dp[l][r] != -1)
    return dp[l][r];

  int a1 = dp[l + 2][r] != -1 ? dp[l + 2][r] : solve(coins, l + 2, r, dp);
  int a2 = dp[l + 1][r - 1] != -1 ? dp[l + 1][r - 1]
                                  : solve(coins, l + 1, r - 1, dp);

  int a = coins[l] + min(a1, a2);

  int b1 = dp[l + 1][r - 1] != -1 ? dp[l + 1][r - 1]
                                  : solve(coins, l + 1, r - 1, dp);
  int b2 = dp[l][r - 2] != -1 ? dp[l][r - 2] : solve(coins, l, r - 2, dp);

  int b = coins[r] + min(b1, b2);
  return dp[l][r] = max(a, b);
}

signed main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int N;
  cin >> N;
  int sum = 0;
  vector<int> coins(N);
  for (int i = 0; i < N; i++) {
    cin >> coins[i];
    sum += coins[i];
  }
  vector<vector<int>> dp(N + 1, vector<int>(N + 1, -1));
  int X = solve(coins, 0, N - 1, dp);
  int Y = sum - X;
  cout << X - Y << endl;
  return 0;
}


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

#define int long long

int solve(vector<int> &coins, int l, int r, vector<vector<int>> &dp) {
  if (l > r)
    return 0;
  if (dp[l][r] != -1)
    return dp[l][r];
  return dp[l][r] = max(coins[l] + min(solve(coins, l + 2, r, dp),
                                       solve(coins, l + 1, r - 1, dp)),
                        coins[r] + min(solve(coins, l + 1, r - 1, dp),
                                       solve(coins, l, r - 2, dp)));
}

signed main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int N;
  cin >> N;
  int sum = 0;
  vector<int> coins(N);
  for (int i = 0; i < N; i++) {
    cin >> coins[i];
    sum += coins[i];
  }
  vector<vector<int>> dp(N + 1, vector<int>(N + 1, -1));
  int X = solve(coins, 0, N - 1, dp);
  int Y = sum - X;
  cout << X - Y << endl;
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl '\n'
#define PI acos(-1)
typedef long long ll;
//  Table is the complete reverse of dp
//  Always think of the pattern (LCM, GCD)
//  Be careful of unsigned
//  double epsilon = FLT_EPSILON;
//	Think of a solution that gets TLE and optimize it
int n, *x;
ll **dp[2], flag;
ll solve(bool turn = 0, int i = 0, int j = n - 1) {
  if (i == j)
    return dp[turn][i][j] = (turn ? -1 : 1) * x[i];
  ll &ret = dp[turn][i][j];
  if (ret != flag)
    return ret;
  if (!turn)
    return ret = max(x[i] + solve(!turn, i + 1, j),
                     x[j] + solve(!turn, i, j - 1));
  else
    return ret = min(-x[i] + solve(!turn, i + 1, j),
                     -x[j] + solve(!turn, i, j - 1));
}
int main() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cin >> n;
  memset(&flag, -127, n * 8);
  x = new int[n];
  dp[0] = new ll *[n];
  dp[1] = new ll *[n];
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    dp[0][i] = new ll[n];
    dp[1][i] = new ll[n];
    memset(dp[0][i], -127, n * 8);
    memset(dp[1][i], -127, n * 8);
  }
  cout << solve();
}


Fixed submission:
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl '\n'
#define PI acos(-1)
typedef long long ll;
//  Table is the complete reverse of dp
//  Always think of the pattern (LCM, GCD)
//  Be careful of unsigned
//  double epsilon = FLT_EPSILON;
//	Think of a solution that gets TLE and optimize it
int n, *x;
ll **dp[2], flag;
ll solve(bool turn = 0, int i = 0, int j = n - 1) {
  if (i == j)
    return dp[turn][i][j] = (turn ? -1 : 1) * x[i];
  ll &ret = dp[turn][i][j];
  if (ret != flag)
    return ret;
  if (!turn)
    return ret = max(x[i] + solve(!turn, i + 1, j),
                     x[j] + solve(!turn, i, j - 1));
  else
    return ret = min(-x[i] + solve(!turn, i + 1, j),
                     -x[j] + solve(!turn, i, j - 1));
}
int main() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cin >> n;
  memset(&flag, -127, 8);
  x = new int[n];
  dp[0] = new ll *[n];
  dp[1] = new ll *[n];
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    dp[0][i] = new ll[n];
    dp[1][i] = new ll[n];
    memset(dp[0][i], -127, n * 8);
    memset(dp[1][i], -127, n * 8);
  }
  cout << solve();
}
Problem p03171 (C++)

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

int N;
ll A[3005], DP[3005][3005];

ll sol = 0;

// 0 - Taro's turn
// 1 - Jiro's turn

ll rek(int l, int r, int turn) {
  if (l > r)
    return 0;
  if (l == r) {
    return DP[l][r] = A[l] * (1 - turn);
  }
  ll sol1 = rek(l + 1, r, 1 - turn) + A[l];
  ll sol2 = rek(l, r - 1, 1 - turn) + A[r];
  if (turn == 0) {
    DP[l][r] = max(sol1, sol2);
  } else {
    DP[l][r] = min(sol1 - A[l], sol2 - A[r]);
  }
  return DP[l][r];
}

int main() {
  scanf("%d", &N);
  ll sum = 0;
  REP(i, 0, N) {
    scanf("%lld", &A[i]);
    sum += A[i];
  }

  ll x = rek(0, N - 1, 0);
  printf("%lld\n", x - (sum - x));

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, s, t) for (int i = (s); i < (t); i++)

int N;
ll A[3005], DP[3005][3005];

ll sol = 0;

// 0 - Taro's turn
// 1 - Jiro's turn

ll rek(int l, int r, int turn) {
  if (l > r)
    return 0;
  if (l == r) {
    return DP[l][r] = A[l] * (1 - turn);
  }
  if (DP[l][r] != 0)
    return DP[l][r];
  ll sol1 = rek(l + 1, r, 1 - turn) + A[l];
  ll sol2 = rek(l, r - 1, 1 - turn) + A[r];
  if (turn == 0) {
    DP[l][r] = max(sol1, sol2);
  } else {
    DP[l][r] = min(sol1 - A[l], sol2 - A[r]);
  }
  return DP[l][r];
}

int main() {
  scanf("%d", &N);
  ll sum = 0;
  REP(i, 0, N) {
    scanf("%lld", &A[i]);
    sum += A[i];
  }

  ll x = rek(0, N - 1, 0);
  printf("%lld\n", x - (sum - x));

  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
inline void fileio(const char *in, const char *out) {
  freopen(in, "r", stdin);
  freopen(out, "w", stdout);
}
#define TD typedef
#define forx(i, n) for (int i = 0; i < (n); ++i)
#define forxr(i, n) for (int i = (n)-1; i >= 0; --i)
TD long long ll;
TD long double ld;
TD pair<int, int> pii;
TD pair<ll, ll> pll;
TD vector<int> VI;
TD vector<bool> VB;
TD vector<ll> VL;
const char nl = '\n';
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const ld EPS = 1e-9, PI = acos(-1);
#define ff first
#define ss second
#define all(c) c.begin(), c.end()

const int N = 3005;
bool vis[N][N];
ll dp[N][N];
ll a[N], n;

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

int main() { // Emily <3
  atexit([]() { cerr << "Time: " << (ld)clock() / CLOCKS_PER_SEC << nl; });
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(12);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  cout << solve(0, n) << nl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
inline void fileio(const char *in, const char *out) {
  freopen(in, "r", stdin);
  freopen(out, "w", stdout);
}
#define TD typedef
#define forx(i, n) for (int i = 0; i < (n); ++i)
#define forxr(i, n) for (int i = (n)-1; i >= 0; --i)
TD long long ll;
TD long double ld;
TD pair<int, int> pii;
TD pair<ll, ll> pll;
TD vector<int> VI;
TD vector<bool> VB;
TD vector<ll> VL;
const char nl = '\n';
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const ld EPS = 1e-9, PI = acos(-1);
#define ff first
#define ss second
#define all(c) c.begin(), c.end()

const int N = 3005;
bool vis[N][N];
ll dp[N][N];
ll a[N], n;

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

int main() { // Emily <3
  atexit([]() { cerr << "Time: " << (ld)clock() / CLOCKS_PER_SEC << nl; });
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(12);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  cout << solve(0, n) << nl;

  return 0;
}
Problem p03171 (Python)

Buggy submission (status: Time Limit Exceeded):
def kukan(n, a):
    dp = [[0] * n for _ in range(n)]
    for i in range(n):
        dp[i][i] = a[i]
    for i in range(n - 2, -1, -1):
        for j in range(i + 1, n):
            dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1])
    return dp[0][n - 1]


n = int(input())
a = list(map(int, input().split()))
print(kukan(n, a))


Fixed submission:
def kukan(n, a):
    dp = [[0] * n for _ in range(n)]
    for i in range(n):
        dp[i][i] = a[i]
    for i in range(n - 2, -1, -1):
        for j in range(i + 1, n):
            L = a[i] - dp[i + 1][j]
            R = a[j] - dp[i][j - 1]
            dp[i][j] = L if L > R else R
    return dp[0][n - 1]


n = int(input())
a = list(map(int, input().split()))
print(kukan(n, a))