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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define inp 10
#define check exit(0)
#define nl cout << endl;
#define mod 1000000007
#define ll long long int
#define trace(x) cerr << #x << " : " << x << endl;
#define deb(v)                                                                 \
  for (int i = 0; i < v.size(); i++) {                                         \
    cout << v[i];                                                              \
    (i == v.size() - 1) ? cout << "\n" : cout << " ";                          \
  }
#define jaldi                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define ordered_set                                                            \
  tree<int, null_type, less_equal<int>, rb_tree_tag,                           \
       tree_order_statistics_node_update>

using namespace __gnu_pbds;
using namespace std;

// Pick yourself up, 'cause...

int main() {
  jaldi

      int n;
  cin >> n;

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

  ll dp[inp][inp];

  for (int l = n - 1; l >= 0; l--) {
    for (int r = l; r < n; r++) {
      if (r == l) {
        dp[l][r] = v[l];
        continue;
      }
      dp[l][r] = max(v[l] - dp[l + 1][r], v[r] - dp[l][r - 1]);
    }
  }
  cout << dp[0][n - 1];

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define inp 3005
#define check exit(0)
#define nl cout << endl;
#define mod 1000000007
#define ll long long int
#define trace(x) cerr << #x << " : " << x << endl;
#define deb(v)                                                                 \
  for (int i = 0; i < v.size(); i++) {                                         \
    cout << v[i];                                                              \
    (i == v.size() - 1) ? cout << "\n" : cout << " ";                          \
  }
#define jaldi                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define ordered_set                                                            \
  tree<int, null_type, less_equal<int>, rb_tree_tag,                           \
       tree_order_statistics_node_update>

using namespace __gnu_pbds;
using namespace std;

// Pick yourself up, 'cause...

int main() {
  jaldi

      int n;
  cin >> n;

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

  ll dp[inp][inp];

  for (int l = n - 1; l >= 0; l--) {
    for (int r = l; r < n; r++) {
      if (r == l) {
        dp[l][r] = v[l];
        continue;
      }
      dp[l][r] = max(v[l] - dp[l + 1][r], v[r] - dp[l][r - 1]);
    }
  }
  cout << dp[0][n - 1];

  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
#define endl '\n'
#define F first
#define S second
const int MAX_N = 3e3 + 3;

int n;
int a[MAX_N];
ll dp[MAX_N][MAX_N];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int d = 1, j; d <= n; d++)
    for (int i = 1; i <= n; i++) {
      j = i + d - 1;
      if ((n - d) & 1)
        dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j]);
      else
        dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j]);
    }
  printf("%lld\n", dp[1][n]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
#define endl '\n'
#define F first
#define S second
const int MAX_N = 3e3 + 3;

int n;
int a[MAX_N];
ll dp[MAX_N][MAX_N];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int d = 1, j; d <= n; d++)
    for (int i = 1; i + d - 1 <= n; i++) {
      j = i + d - 1;
      if ((n - d) & 1)
        dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j]);
      else
        dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j]);
    }
  printf("%lld\n", dp[1][n]);
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define mp make_pair
#define RFOR(i, a, n) for (int i = n; i > a; i--)
#define pii pair<long long int, long long int>
#define ub upper_bound
#define lb lower_bound
#define MOD 1000000007
typedef long long int ll;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n;
  cin >> n;
  vector<ll> vec;
  ll a;
  ll dp[301][301];
  for (int i = 0; i < n; i++) {
    cin >> a;
    vec.pb(a);
  }

  for (int l = n - 1; l >= 0; l--) {
    for (int r = l; r < n; r++) {
      if (l == r)
        dp[l][r] = vec[l];
      else
        dp[l][r] = max(vec[l] - dp[l + 1][r], vec[r] - dp[l][r - 1]);
    }
  }
  cout << dp[0][n - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define mp make_pair
#define RFOR(i, a, n) for (int i = n; i > a; i--)
#define pii pair<long long int, long long int>
#define ub upper_bound
#define lb lower_bound
#define MOD 1000000007
typedef long long int ll;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n;
  cin >> n;
  vector<ll> vec;
  ll a;
  ll dp[3001][3001];
  for (int i = 0; i < n; i++) {
    cin >> a;
    vec.pb(a);
  }

  for (int l = n - 1; l >= 0; l--) {
    for (int r = l; r < n; r++) {
      if (l == r)
        dp[l][r] = vec[l];
      else
        dp[l][r] = max(vec[l] - dp[l + 1][r], vec[r] - dp[l][r - 1]);
    }
  }
  cout << dp[0][n - 1] << endl;
}
Problem p03171 (C++)

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

using namespace std;

typedef long long ll;

const int MAX_N = 3001;

int N;
ll a[MAX_N];
ll dp[MAX_N][MAX_N][2];

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i)
    scanf("%lld", &a[i]);

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

  for (int d = 1; d < N; ++d) {
    for (int p = 0; p < 2; ++p) {
      for (int i = 0; i < N; ++i) {
        int j = i + d;
        dp[i][j][p] =
            max(a[i] - dp[i + 1][j][1 - p], a[j] - dp[i][j - 1][1 - p]);
      }
    }
  }

  printf("%lld\n", dp[0][N - 1][0]);
  return 0;
}

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

using namespace std;

typedef long long ll;

const int MAX_N = 3001;

int N;
ll a[MAX_N];
ll dp[MAX_N][MAX_N][2];

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i)
    scanf("%lld", &a[i]);

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

  for (int d = 1; d < N; ++d) {
    for (int p = 0; p < 2; ++p) {
      for (int i = 0; i + d < N; ++i) {
        int j = i + d;
        dp[i][j][p] =
            max(a[i] - dp[i + 1][j][1 - p], a[j] - dp[i][j - 1][1 - p]);
      }
    }
  }

  printf("%lld\n", dp[0][N - 1][0]);
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define w while
#define loop(i, l, r) for (int i = l; i < r; i++)
#define ci cin >>
#define co cout <<
#define mm 1000000007
#define ll long long
using namespace std;
ll int helper(vector<vector<ll int>> &dp, vector<ll int> vec, int start,
              int last) {
  if (start > last) {
    return 0;
  }
  if (start == last)
    return vec[start];
  if (last - start == 1)
    return max(vec[last], vec[start]);

  if (dp[start][last] != -1)
    return dp[start][last];

  ll int a = vec[start] + min(helper(dp, vec, start + 2, last),
                              helper(dp, vec, start + 1, last - 1));
  ll int b = vec[last] + min(helper(dp, vec, start + 1, last - 1),
                             helper(dp, vec, start, last - 2));
  dp[start][last] = max(a, b);
  return max(a, b);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll int n;
  cin >> n;
  ll int sum = 0;
  vector<ll int> vec(n);
  for (int i = 0; i < n; i++) {
    cin >> vec[i];
    sum += vec[i];
  }
  vector<vector<ll int>> dp(n, vector<ll int>(n, -1));
  ll int taro = helper(dp, vec, 0, n - 1);
  cout << (taro - sum + taro);
}


Fixed submission:
#include <bits/stdc++.h>
#define w while
#define loop(i, l, r) for (int i = l; i < r; i++)
#define ci cin >>
#define co cout <<
#define mm 1000000007
#define ll long long
using namespace std;
ll int helper(vector<vector<ll int>> &dp, vector<ll int> &vec, int start,
              int last) {
  if (start > last) {
    return 0;
  }
  if (start == last)
    return vec[start];
  if (last - start == 1)
    return max(vec[last], vec[start]);

  if (dp[start][last] != -1)
    return dp[start][last];

  ll int a = vec[start] + min(helper(dp, vec, start + 2, last),
                              helper(dp, vec, start + 1, last - 1));
  ll int b = vec[last] + min(helper(dp, vec, start + 1, last - 1),
                             helper(dp, vec, start, last - 2));
  dp[start][last] = max(a, b);
  return max(a, b);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll int n;
  cin >> n;
  ll int sum = 0;
  vector<ll int> vec(n);
  for (int i = 0; i < n; i++) {
    cin >> vec[i];
    sum += vec[i];
  }
  vector<vector<ll int>> dp(n, vector<ll int>(n, -1));
  ll int taro = helper(dp, vec, 0, n - 1);
  cout << (taro - sum + taro);
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll pref_sum[3000][3000], dp[3000][3000];
int a[3000];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  memset(pref_sum, 0, sizeof(pref_sum));
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++) {
    pref_sum[i][i] = a[i];
    dp[i][i] = a[i];
    for (int j = i + 1; j < n; j++)
      pref_sum[i][j] = pref_sum[i][j - 1] + a[j];
  }
  int i = 0, j = 1;
  int jstart = 1;
  while (!(i == 0 && j == n - 1)) {
    dp[i][j] = pref_sum[i][j] - min(dp[i][j - 1], dp[i + 1][j]);
    i++;
    j++;
    if (j == n) {
      i = 0;
      jstart++;
      j = jstart;
    }
  }
  ll scoreB = min(dp[0][n - 2], dp[1][n - 1]);
  ll scoreA = pref_sum[0][n - 1] - scoreB;
  cout << scoreA - scoreB << endl;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll pref_sum[3000][3000], dp[3000][3000];
int a[3000];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  if (n == 1) {
    cout << a[0] << endl;
    return 0;
  }
  memset(pref_sum, 0, sizeof(pref_sum));
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++) {
    pref_sum[i][i] = a[i];
    dp[i][i] = a[i];
    for (int j = i + 1; j < n; j++)
      pref_sum[i][j] = pref_sum[i][j - 1] + a[j];
  }
  int i = 0, j = 1;
  int jstart = 1;
  while (!(i == 0 && j == n - 1)) {
    dp[i][j] = pref_sum[i][j] - min(dp[i][j - 1], dp[i + 1][j]);
    i++;
    j++;
    if (j == n) {
      i = 0;
      jstart++;
      j = jstart;
    }
  }
  ll scoreB = min(dp[0][n - 2], dp[1][n - 1]);
  ll scoreA = pref_sum[0][n - 1] - scoreB;
  cout << scoreA - scoreB << endl;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<vector<long long int>> ve;
long long int recurse(long long int *arr, int i, int j) {
  if (i > j) {
    return 0;
  } else if (i == j) {
    ve[i][j] = arr[i];
    // cout<<i<<" "<<j<<" "<<ve[i][j]<<endl;
    return arr[i];
  } else if ((i + 1) == j) {
    long long int maximum = max(arr[i], arr[j]);
    ve[i][j] = maximum;
    return maximum;
  } else if (ve[i][j] != 0) {
    return ve[i][j];
  } else {
    long long int max1 = recurse(arr, i + 1, j - 1) + arr[i];
    long long int max2 = recurse(arr, i + 2, j) + arr[i];
    long long int max3 = recurse(arr, i + 1, j - 1) + arr[j];
    long long int max4 = recurse(arr, i, j - 2) + arr[j];
    ve[i][j] = min(max1, max2);
    long long int fake = min(max3, max4);
    ve[i][j] = max(ve[i][j], fake);
  }
}
int main(void) {
  int n;
  cin >> n;
  ve.resize(n);
  for (int i = 0; i < n; i++) {
    ve[i].resize(n);
  }
  long long int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  long long int ans = recurse(arr, 0, n - 1);
  long long int sum = 0;
  for (int i = 0; i < n; i++)
    sum += arr[i];
  long long int fake = sum - ans;
  cout << (ans - fake) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<vector<long long int>> ve;
long long int recurse(long long int *arr, int i, int j) {
  if (i > j) {
    return 0;
  } else if (i == j) {
    ve[i][j] = arr[i];
    // cout<<i<<" "<<j<<" "<<ve[i][j]<<endl;
    return arr[i];
  } else if ((i + 1) == j) {
    long long int maximum = max(arr[i], arr[j]);
    ve[i][j] = maximum;
    return maximum;
  } else if (ve[i][j] != 0) {
    return ve[i][j];
  } else {
    long long int max1 = recurse(arr, i + 1, j - 1) + arr[i];
    long long int max2 = recurse(arr, i + 2, j) + arr[i];
    long long int max3 = recurse(arr, i + 1, j - 1) + arr[j];
    long long int max4 = recurse(arr, i, j - 2) + arr[j];
    ve[i][j] = min(max1, max2);
    long long int fake = min(max3, max4);
    ve[i][j] = max(ve[i][j], fake);
    return ve[i][j];
  }
}
int main(void) {
  int n;
  cin >> n;
  ve.resize(n);
  for (int i = 0; i < n; i++) {
    ve[i].resize(n);
  }
  long long int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  long long int ans = recurse(arr, 0, n - 1);
  long long int sum = 0;
  for (int i = 0; i < n; i++)
    sum += arr[i];
  long long int fake = sum - ans;
  cout << (ans - fake) << endl;
}
Problem p03171 (C++)

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

long long num[maxn];
long long dp[maxn][maxn];
int n;

int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> num[i];
  for (int i = n; i >= 1; --i) {
    for (int j = i; j <= n; ++j) {
      dp[i][j] = max(num[i] - dp[i + 1][j], num[j] - dp[i][j - 1]);
    }
  }
  cout << dp[1][n];
  return 0;
}

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

long long num[maxn];
long long dp[maxn][maxn];
int n;

int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> num[i];
  for (int i = n; i >= 1; --i) {
    for (int j = i; j <= n; ++j) {
      dp[i][j] = max(num[i] - dp[i + 1][j], num[j] - dp[i][j - 1]);
    }
  }
  cout << dp[1][n];
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll dp[3000][3000];

ll topdown(ll *arr, ll l, ll r) {
  if (l > r) {
    dp[l][r] = 0;
    return 0;
  }
  if (dp[l][r] != -1) {
    return dp[l][r];
  }
  ll op1 = arr[l] + min(topdown(arr, l + 1, r - 1), topdown(arr, l + 2, r));
  ll op2 = arr[r] + min(topdown(arr, l, r - 2), topdown(arr, l + 1, r - 1));
  dp[l][r] = max(op1, op2);
  return dp[l][r];
}

int main() {
  ll n = 0, sum = 0;
  cin >> n;
  ll *arr = new ll[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  memset(dp, -1, sizeof dp);
  cout << 2 * topdown(arr, 0, n - 1) - sum;
  return 0;
}

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll dp[4000][4000];

ll topdown(ll *arr, ll l, ll r) {
  if (l > r) {
    dp[l][r] = 0;
    return 0;
  }
  if (dp[l][r] != -1) {
    return dp[l][r];
  }
  ll op1 = arr[l] + min(topdown(arr, l + 1, r - 1), topdown(arr, l + 2, r));
  ll op2 = arr[r] + min(topdown(arr, l, r - 2), topdown(arr, l + 1, r - 1));
  dp[l][r] = max(op1, op2);
  return dp[l][r];
}

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

Buggy submission (status: Runtime Error):
/*   Abhijith  */
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#define lld long long int
#define ld long double
#define mod 1000000007
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (lld i = a; i <= b; i++)
#define repr(i, a, b) for (lld i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define endl '\n'
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
vector<lld> v(3001);
lld dp[3001][3001][2];
lld ans = 0, n;
lld rec(lld l, lld r, lld pla) {
  if (dp[l][r][pla] != -1)
    return dp[l][r][pla];
  if (l > r) {
    return dp[l][r][pla] = 0;
  }
  if (pla == 0) {
    ans = max(v[l] + rec(l + 1, r, 1), v[r] + rec(l, r - 1, 1));
  } else {
    ans = min(rec(l + 1, r, 0) - v[l], rec(l, r - 1, 0) - v[r]);
  }
  return dp[l][r][pla] = ans;
}
int main() {
  ios;
  cin >> n;
  rep(i, 1, n) { cin >> v[i]; }
  rep(i, 0, 3000) rep(j, 0, 3000) rep(k, 0, 1) dp[i][j][k] = -1;
  lld ans = rec(1, n, 0);
  cout << ans << endl;
  return 0;
}


Fixed submission:
/*   Abhijith  */
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#define lld long long int
#define ld long double
#define mod 1000000007
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (lld i = a; i <= b; i++)
#define repr(i, a, b) for (lld i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define endl '\n'
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
vector<lld> v(3001);
lld dp[3001][3001][2];
lld ans = 0, n;
lld rec(lld l, lld r, lld pla) {
  if (dp[l][r][pla] != -1)
    return dp[l][r][pla];
  if (l > r) {
    return dp[l][r][pla] = 0;
  }
  if (pla == 0) {
    ans = max(v[l] + rec(l + 1, r, 1), v[r] + rec(l, r - 1, 1));
  } else {
    ans = min(rec(l + 1, r, 0) - v[l], rec(l, r - 1, 0) - v[r]);
  }
  return dp[l][r][pla] = ans;
}
int main() {
  ios;
  cin >> n;
  rep(i, 1, n) { cin >> v[i]; }
  rep(l, 1, n) {
    rep(i, 1, n - l + 1) {
      lld j = i + l - 1;
      if (l == 1) {
        dp[i][j][0] = v[i];
        dp[i][j][1] = -v[i];
      } else {
        dp[i][j][0] = max(dp[i + 1][j][1] + v[i], dp[i][j - 1][1] + v[j]);
        dp[i][j][1] = min(dp[i + 1][j][0] - v[i], dp[i][j - 1][0] - v[j]);
      }
    }
  }
  // lld ans= rec(1,n,0);
  cout << dp[1][n][0] << endl;
  return 0;
}
Problem p03171 (C++)

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  for (int &t : a)
    cin >> t;

  long long inv = 10000000000000LL;
  vector<vector<long long>> T(N, vector<long long>(N, inv));

  function<long long(int, int)> f = [&](int p, int q) -> long long {
    if (p > q)
      return 0;
    if (T[p][q] != inv)
      return T[p][q];
    return max(a[p] - f(p + 1, q), a[q] - f(p, q - 1));
  };
  cout << f(0, N - 1) << endl;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  for (int &t : a)
    cin >> t;

  long long inv = 10000000000000LL;
  vector<vector<long long>> T(N, vector<long long>(N, inv));

  function<long long(int, int)> f = [&](int p, int q) -> long long {
    if (p > q)
      return 0;
    if (T[p][q] != inv)
      return T[p][q];
    return T[p][q] = max(a[p] - f(p + 1, q), a[q] - f(p, q - 1));
  };
  cout << f(0, N - 1) << endl;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
#define vi vector<int>
#define vvi vector<vector<int>>
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(NULL);                                             \
  cin.tie(NULL);
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FORE(i, a, b) for (int i = a; i <= b; i++)
#define pb push_back
#define MOD 1000000007

using namespace std;

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

// code from below

ll dp[3001][3001];
void solve() {
  ll n;
  cin >> n;
  vector<ll> arr(n);
  FOR(i, n)
  cin >> arr[i];
  // dp[i][j] stores the x-y value if both players play optimally, considering
  // subarr from i to j of arr thus we have n^2 sub array thus dp is of n^2 size
  for (int L = n - 1; L >= 0; L--) {
    for (int R = L; R < n; R++) {
      if (L == R)
        dp[L][R] = arr[R];
      else {
        dp[L][R] = max(arr[L] - dp[L + 1][R], arr[R] - dp[L][R - 1]);
      }
    }
  }
  cout << dp[0][n - 1] << endl;
}

int main() {
  int t = 1;
  cin >> t;
  // cin.ignore(numeric_limits<streamsize>::max(),'\n');
  while (t--)
    solve();
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
#define vi vector<int>
#define vvi vector<vector<int>>
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(NULL);                                             \
  cin.tie(NULL);
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FORE(i, a, b) for (int i = a; i <= b; i++)
#define pb push_back
#define MOD 1000000007

using namespace std;

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

// code from below

ll dp[3001][3001];
void solve() {
  ll n;
  cin >> n;
  vector<ll> arr(n);
  FOR(i, n)
  cin >> arr[i];
  // dp[i][j] stores the x-y value if both players play optimally, considering
  // subarr from i to j of arr thus we have n^2 sub array thus dp is of n^2 size
  for (int L = n - 1; L >= 0; L--) {
    for (int R = L; R < n; R++) {
      if (L == R)
        dp[L][R] = arr[R];
      else {
        dp[L][R] = max(arr[L] - dp[L + 1][R], arr[R] - dp[L][R - 1]);
      }
    }
  }
  cout << dp[0][n - 1] << endl;
}

int main() {
  int t = 1;
  // cin>>t;
  //  cin.ignore(numeric_limits<streamsize>::max(),'\n');
  while (t--)
    solve();
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)

using namespace std;
const int maxi = 3005;
ll dp[maxi][maxi] = {0};

/**

dp[l][r] = max( arr[l] + dp[l-1][r] , arr[r] -dp[l][r-1] )

maximum of difference of if we choose left and the opponents choice or if we
choose right  and opponents choice

dp[i][j] is storing the maximum difference for every j in arr.
so the answer will be dp[0][n-1]

**/

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

  for (int l = n - 1; l >= 0; l--) {
    for (int r = l; r < n; r++) {
      if (l == r) {
        dp[l][r] = arr[l];
      } else {
        dp[l][r] = max(arr[l] - dp[l - 1][r], arr[r] - dp[l][r - 1]);
      }
    }
  }
  cout << dp[0][n - 1];
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)

using namespace std;
const int maxi = 3005;
ll dp[maxi][maxi] = {0};

/**

dp[l][r] = max( arr[l] + dp[l-1][r] , arr[r] -dp[l][r-1] )

maximum of difference of if we choose left and the opponents choice or if we
choose right  and opponents choice

dp[i][j] is storing the maximum difference for every j in arr.
so the answer will be dp[0][n-1]

**/

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

  for (int l = n - 1; l >= 0; l--) {
    for (int r = l; r < n; r++) {
      if (l == r) {
        dp[l][r] = arr[l];
      } else {
        dp[l][r] = max(arr[l] - dp[l + 1][r], arr[r] - dp[l][r - 1]);
      }
    }
  }
  cout << dp[0][n - 1];
}
Problem p03171 (C++)

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

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

  // dp[i][j]: Taro のターンで数列が a[i]~a[j] のときの X-Y
  long long dp[300][300] = {};
  for (int i = 0; i < N; ++i)
    dp[i][i] = a[i];
  for (int i = N - 2; i >= 0; --i) {
    for (int j = i + 1; j < N; ++j) {
      dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]);
    }
  }
  cout << dp[0][N - 1] << endl;

  return 0;
}

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

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

  // dp[i][j]: Taro のターンで数列が a[i]~a[j] のときの X-Y
  long long dp[3000][3000] = {};
  for (int i = 0; i < N; ++i)
    dp[i][i] = a[i];
  for (int i = N - 2; i >= 0; --i) {
    for (int j = i + 1; j < N; ++j) {
      dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]);
    }
  }
  cout << dp[0][N - 1] << endl;

  return 0;
}
Problem p03171 (C++)

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

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ALL(a) begin(a), end(a)
#define SZ(a) ((int)(a).size())

#define REP(a, b) for (int a = 0; a < (b); a++)
#define FOR(a, b, c) for (int a = (b); a <= (c); a++)
#define REPD(a, b) for (int a = (b)-1; a >= 0; a--)
#define FORD(a, b, c) for (int a = (b); a >= (c); a--)

#define MIN(a, b) (a) = min((a), (b))
#define MAX(a, b) (a) = max((a), (b))

#ifdef __DEBUG
#define debug if (true)
#else
#define debug if (false)
#endif

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template <typename T> using VVV = vector<VV<T>>;

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.fi >> p.se;
}

const int N = 3003;

int n;
int a[N];
ll dp[N][N];
bool visit[N][N];

ll go(int l, int r) {
  if (l > r)
    return 0;
  if (visit[l][r])
    return dp[l][r];
  int turn = (n - (r - l + 1)) % 2;
  if (turn == 0) {
    dp[l][r] = max(a[l] + go(l + 1, r), a[r] + go(l, r - 1));
  } else {
    dp[l][r] = min(go(l + 1, r) - a[l], go(l, r - 1) - a[r]);
  }
  return dp[l][r];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  REP(i, n) cin >> a[i];
  cout << go(0, n - 1) << endl;
}


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

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ALL(a) begin(a), end(a)
#define SZ(a) ((int)(a).size())

#define REP(a, b) for (int a = 0; a < (b); a++)
#define FOR(a, b, c) for (int a = (b); a <= (c); a++)
#define REPD(a, b) for (int a = (b)-1; a >= 0; a--)
#define FORD(a, b, c) for (int a = (b); a >= (c); a--)

#define MIN(a, b) (a) = min((a), (b))
#define MAX(a, b) (a) = max((a), (b))

#ifdef __DEBUG
#define debug if (true)
#else
#define debug if (false)
#endif

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template <typename T> using VVV = vector<VV<T>>;

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.fi >> p.se;
}

const int N = 3003;

int n;
int a[N];
ll dp[N][N];
bool visit[N][N];

ll go(int l, int r) {
  if (l > r)
    return 0;
  if (visit[l][r])
    return dp[l][r];
  int turn = (n - (r - l + 1)) % 2;
  if (turn == 0) {
    dp[l][r] = max(a[l] + go(l + 1, r), a[r] + go(l, r - 1));
  } else {
    dp[l][r] = min(go(l + 1, r) - a[l], go(l, r - 1) - a[r]);
  }
  visit[l][r] = true;
  return dp[l][r];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  REP(i, n) cin >> a[i];
  cout << go(0, n - 1) << endl;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define REPP(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define PER(i, a) for (int i = (a)-1; i >= 0; --i)
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define MP make_pair

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

const int maxn = 3005;
ll a[maxn], dp[maxn][maxn];

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


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define REPP(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define PER(i, a) for (int i = (a)-1; i >= 0; --i)
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define MP make_pair

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

const int maxn = 3005;
ll a[maxn], dp[maxn][maxn];

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

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

using namespace std;

void rwFile() {
  freopen("INP.inp", "r", stdin);
  freopen("OUT.out", "w", stdout);
}

long long arr[3001];
long long res[3001][3001];

void findRes(int a, int b) {
  if (a == b) {
    res[a][b] = arr[a];
    return;
  }
  findRes(a, b - 1);
  findRes(a + 1, b);
  res[a][b] = max(arr[b] - res[a][b - 1], arr[a] - res[a + 1][b]);
}

void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  findRes(1, n);
  //    for (int i = 1; i <= n; i++) {
  //        for (int j = 1; j <= n; j++) cout << res[i][j] << " ";
  //        cout << endl;
  //    }
  cout << res[1][n];
}

int main() {
  // rwFile();
  solve();
}


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

using namespace std;

void rwFile() {
  freopen("INP.inp", "r", stdin);
  freopen("OUT.out", "w", stdout);
}

long long arr[3001];
long long res[3001][3001];

void findRes(int a, int b) {
  if (res[a][b] != 0)
    return;
  if (a == b) {
    res[a][b] = arr[a];
    return;
  }
  findRes(a, b - 1);
  findRes(a + 1, b);
  res[a][b] = max(arr[b] - res[a][b - 1], arr[a] - res[a + 1][b]);
}

void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  findRes(1, n);
  //    for (int i = 1; i <= n; i++) {
  //        for (int j = 1; j <= n; j++) cout << res[i][j] << " ";
  //        cout << endl;
  //    }
  cout << res[1][n];
}

int main() {
  // rwFile();
  solve();
}
Problem p03171 (C++)

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

using namespace std;
#define ll long long
const long double EPS = 1e-8;
const long double PI = acos(-1);
const int N = 3e3 + 2;
const int mod = 1e5 + 7;
ll mem[N][N][2];
int arr[N];
int n, k;

ll solve(int i = 0, int j = n - 1, int player = 0) {
  if (i > j)
    return 0;
  //    if (~mem[i][j][player])return mem[i][j][player];
  if (!player) {
    return mem[i][j][player] = max(solve(i + 1, j, !player) + arr[i],
                                   solve(i, j - 1, !player) + arr[j]);
  } else {
    return mem[i][j][player] = min(solve(i + 1, j, !player) - arr[i],
                                   solve(i, j - 1, !player) - arr[j]);
  }
}

int main() {
  cin >> n;
  memset(mem, -1, sizeof mem);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  cout << solve() << endl;
}


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

using namespace std;
#define ll long long
const long double EPS = 1e-8;
const long double PI = acos(-1);
const int N = 3e3 + 2;
const int mod = 1e5 + 7;
ll mem[N][N][2];
int arr[N];
int n, k;

ll solve(int i = 0, int j = n - 1, int player = 0) {
  if (i > j)
    return 0;
  if (~mem[i][j][player])
    return mem[i][j][player];
  if (!player) {
    return mem[i][j][player] = max(solve(i + 1, j, !player) + arr[i],
                                   solve(i, j - 1, !player) + arr[j]);
  } else {
    return mem[i][j][player] = min(solve(i + 1, j, !player) - arr[i],
                                   solve(i, j - 1, !player) - arr[j]);
  }
}

int main() {
  cin >> n;
  memset(mem, -1, sizeof mem);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  cout << solve() << endl;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using lli = long long int;
template <class T, class U> void init_n(vector<T> &v, size_t n, U x) {
  v = vector<T>(n, x);
}
template <class T> void init_n(vector<T> &v, size_t n) { init_n(v, n, T()); }
template <class T> void read_n(vector<T> &v, size_t n, size_t o = 0) {
  v = vector<T>(n + o);
  for (lli i = o; i < n + o; ++i)
    cin >> v[i];
}
template <class T> void read_n(T a[], size_t n, size_t o = 0) {
  for (lli i = o; i < n + o; ++i)
    cin >> a[i];
}
template <class T> T gabs(const T &x) { return max(x, -x); }
#define abs gabs

lli n, k, a[3000];
lli memo[3000][3000];

lli dfs(lli t, lli l, lli r) {
  if (memo[l][r] != 1e18)
    return memo[l][r];
  lli yl = dfs(-t, l + 1, r);
  lli yr = dfs(-t, l, r - 1);
  lli res;
  if (t == 1)
    res = max(yl + a[l], yr + a[r]);
  else
    res = min(yl - a[l], yr - a[r]);
  return memo[l][r] = res;
}

int main() {
  cin >> n;
  read_n(a, n);
  for (lli i = 0; i < n; ++i)
    fill(&memo[i][i], &memo[i][n + 1], 1e18);
  cout << dfs(1, 0, n - 1) << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using lli = long long int;
template <class T, class U> void init_n(vector<T> &v, size_t n, U x) {
  v = vector<T>(n, x);
}
template <class T> void init_n(vector<T> &v, size_t n) { init_n(v, n, T()); }
template <class T> void read_n(vector<T> &v, size_t n, size_t o = 0) {
  v = vector<T>(n + o);
  for (lli i = o; i < n + o; ++i)
    cin >> v[i];
}
template <class T> void read_n(T a[], size_t n, size_t o = 0) {
  for (lli i = o; i < n + o; ++i)
    cin >> a[i];
}
template <class T> T gabs(const T &x) { return max(x, -x); }
#define abs gabs

lli n, k, a[3000];
lli memo[3001][3001];

lli dfs(lli t, lli l, lli r) {
  if (memo[l][r] != 1e18)
    return memo[l][r];
  lli yl = dfs(-t, l + 1, r);
  lli yr = dfs(-t, l, r - 1);
  lli res;
  if (t == 1)
    res = max(yl + a[l], yr + a[r]);
  else
    res = min(yl - a[l], yr - a[r]);
  return memo[l][r] = res;
}

int main() {
  cin >> n;
  read_n(a, n);
  for (lli i = 0; i < n; ++i)
    fill(&memo[i][i], &memo[i][n + 1], 1e18);
  cout << dfs(1, 0, n - 1) << '\n';
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define Elhabashy                                                              \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
void input() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
}
const int N = 50 + 5, mod = 1e9 + 7;
typedef long long ll;
ll n, arr[N], dp[N][N][3];

ll solve(int i = 1, int j = n, bool f = 0) {
  if (i > j)
    return 0;
  ll &ret = dp[i][j][f];
  if (~ret)
    return ret;
  if (f == 0) {
    ret = arr[i] + solve(i + 1, j, 1);
    ret = max(ret, arr[j] + solve(i, j - 1, 1));
  } else {
    ret = -arr[i] + solve(i + 1, j, 0);
    ret = min(ret, -arr[j] + solve(i, j - 1, 0));
  }
  return ret;
}
int main() {
  Elhabashy input();
  memset(dp, -1, sizeof dp);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  cout << solve() << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define Elhabashy                                                              \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
void input() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
}
const int N = 3000 + 5, mod = 1e9 + 7;
typedef long long ll;
ll n, arr[N], dp[N][N][3];

ll solve(int i = 1, int j = n, bool f = 0) {
  if (i > j)
    return 0;
  ll &ret = dp[i][j][f];
  if (~ret)
    return ret;
  if (f == 0) {
    ret = arr[i] + solve(i + 1, j, 1);
    ret = max(ret, arr[j] + solve(i, j - 1, 1));
  } else {
    ret = -arr[i] + solve(i + 1, j, 0);
    ret = min(ret, -arr[j] + solve(i, j - 1, 0));
  }
  return ret;
}
int main() {
  Elhabashy input();
  memset(dp, -1, sizeof dp);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  cout << solve() << endl;
}
Problem p03171 (C++)

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

const int N = 305, MOD = 1e9 + 7;
const long long INF = 9e18;

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

long long rec(int l, int r, bool turn) {
  if (l > r) {
    return 0;
  }
  if (dp[l][r][turn] != -INF) {
    return dp[l][r][turn];
  }
  dp[l][r][turn] =
      max(a[l] - rec(l + 1, r, turn ^ 1), a[r] - rec(l, r - 1, turn ^ 1));
  return dp[l][r][turn];
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j][0] = dp[i][j][1] = -INF;
    }
  }
  cout << rec(1, n, 0);
}


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

const int N = 3005, MOD = 1e9 + 7;
const long long INF = 9e18;

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

long long rec(int l, int r, bool turn) {
  if (l > r) {
    return 0;
  }
  if (dp[l][r][turn] != -INF) {
    return dp[l][r][turn];
  }
  dp[l][r][turn] =
      max(a[l] - rec(l + 1, r, turn ^ 1), a[r] - rec(l, r - 1, turn ^ 1));
  return dp[l][r][turn];
}

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define maxn (int)3010
#define int long long
int tab[maxn], dp[maxn][maxn][3], n;
bool odw[maxn][maxn][3];

int rob(int start, int stop, int kto) {
  if (odw[start][stop][kto])
    return dp[start][stop][kto];
  // printf("Przedzial %lld %lld %lld\n", start, stop, kto);
  if (kto == 1) {
    if (start == stop) {
      dp[start][stop][kto] = tab[stop] * (-1);
      // printf("DLa przedzialu %lld %lld %lld mamy %lld\n", start, stop, kto,
      // dp[start][stop][kto]);
      return dp[start][stop][kto];
    }
    dp[start][stop][kto] = min(rob(start, stop - 1, 2) - tab[stop],
                               rob(start + 1, stop, 2) - tab[start]);
    // printf("DLa przedzialu %lld %lld %lld mamy %lld\n", start, stop, kto,
    // dp[start][stop][kto]);
    return dp[start][stop][kto];
  } else {
    if (start == stop) {
      dp[start][stop][kto] = tab[stop];
      // printf("DLa przedzialu %lld %lld %lld mamy %lld\n", start, stop, kto,
      // dp[start][stop][kto]);
      return dp[start][stop][kto];
    }
    dp[start][stop][kto] = max(rob(start, stop - 1, 1) + tab[stop],
                               rob(start + 1, stop, 1) + tab[start]);
    // printf("DLa przedzialu %lld %lld %lld mamy %lld\n", start, stop, kto,
    // dp[start][stop][kto]);
    return dp[start][stop][kto];
  }
}

int32_t main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &tab[i]);
  }
  rob(1, n, 2);
  printf("%lld", dp[1][n][2]);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define maxn (int)3010
#define int long long
int tab[maxn], dp[maxn][maxn][3], n;
bool odw[maxn][maxn][3];

int rob(int start, int stop, int kto) {
  if (odw[start][stop][kto])
    return dp[start][stop][kto];
  odw[start][stop][kto] = true;
  // printf("Przedzial %lld %lld %lld\n", start, stop, kto);
  if (kto == 1) {
    if (start == stop) {
      dp[start][stop][kto] = tab[stop] * (-1);
      // printf("DLa przedzialu %lld %lld %lld mamy %lld\n", start, stop, kto,
      // dp[start][stop][kto]);
      return dp[start][stop][kto];
    }
    dp[start][stop][kto] = min(rob(start, stop - 1, 2) - tab[stop],
                               rob(start + 1, stop, 2) - tab[start]);
    // printf("DLa przedzialu %lld %lld %lld mamy %lld\n", start, stop, kto,
    // dp[start][stop][kto]);
    return dp[start][stop][kto];
  } else {
    if (start == stop) {
      dp[start][stop][kto] = tab[stop];
      // printf("DLa przedzialu %lld %lld %lld mamy %lld\n", start, stop, kto,
      // dp[start][stop][kto]);
      return dp[start][stop][kto];
    }
    dp[start][stop][kto] = max(rob(start, stop - 1, 1) + tab[stop],
                               rob(start + 1, stop, 1) + tab[start]);
    // printf("DLa przedzialu %lld %lld %lld mamy %lld\n", start, stop, kto,
    // dp[start][stop][kto]);
    return dp[start][stop][kto];
  }
}

int32_t main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &tab[i]);
  }
  rob(1, n, 2);
  printf("%lld", dp[1][n][2]);
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a - 1; (ll)(b) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<vector<ll>> dp(n + 2, vector<ll>(n + 2, 0));
  rep(i, n) {
    cin >> a[i];
    // dp[i][i] = a[i];
  }
  FOR(len, 1, n + 1) {
    for (int i = 0; i <= n; i++) {
      int 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]);
      }
    }
  }
  cout << dp[0][n] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a - 1; (ll)(b) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<vector<ll>> dp(n + 2, vector<ll>(n + 2, 0));
  rep(i, n) {
    cin >> a[i];
    // dp[i][i] = a[i];
  }
  FOR(len, 1, n + 1) {
    for (int i = 0; i <= n; i++) {
      int j = i + len;
      if (n < j)
        continue;
      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]);
      }
    }
  }
  cout << dp[0][n] << endl;
}
Problem p03171 (C++)

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

using namespace std;

typedef long long int lli;

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

  lli n, i, j, size, l, r, rv, lv;

  cin >> n;

  lli a[n];
  lli maximize[n][n];

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

  lli INF = 1e18;

  // cout << INF << '\n';

  /*
  for(i = 0; i < n; ++i)
  {
      for(j = 0; j < n; ++j)
      {
          maximize[i][j] = -INF;
      }
  }
  */

  for (size = 2; size <= n; ++size) {
    for (l = 0; l < n - size + 1; ++l) {
      for (r = l + size - 1; r < n; ++r) {
        // max
        lv = a[l] - maximize[l + 1][r];
        rv = a[r] - maximize[l][r - 1];
        maximize[l][r] = max(lv, rv);
      }
    }
  }
  /*
  for(i = 0; i < n; ++i)
  {
      for(j = 0; j < n; ++j)
      {
          cout << maximize[i][j] << " ";
      }
      cout << '\n';
  }


  for(i = 0; i < n; ++i)
  {
      for(j = 0; j < n; ++j)
      {
          cout << minimize[i][j] << " ";
      }
      cout << '\n';
  }
  */

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

  return 0;
}

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

using namespace std;

typedef long long int lli;

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

  lli n, i, j, size, l, r, rv, lv;

  cin >> n;

  lli a[n];
  lli maximize[n][n];

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

  lli INF = 1e18;

  // cout << INF << '\n';

  /*
  for(i = 0; i < n; ++i)
  {
      for(j = 0; j < n; ++j)
      {
          maximize[i][j] = -INF;
      }
  }
  */

  for (size = 2; size <= n; ++size) {
    for (l = 0; l < n - size + 1; ++l) {
      r = l + size - 1;
      lv = a[l] - maximize[l + 1][r];
      rv = a[r] - maximize[l][r - 1];
      maximize[l][r] = max(lv, rv);
    }
  }
  /*
  for(i = 0; i < n; ++i)
  {
      for(j = 0; j < n; ++j)
      {
          cout << maximize[i][j] << " ";
      }
      cout << '\n';
  }


  for(i = 0; i < n; ++i)
  {
      for(j = 0; j < n; ++j)
      {
          cout << minimize[i][j] << " ";
      }
      cout << '\n';
  }
  */

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

  return 0;
}