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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define sz(x) (int)(x.size())
using namespace std;

const int N = 1e3 + 5, inf = 1e18, mod = 1e9 + 7;

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

int go(int L, int R) {
  if (L > R)
    return 0;

  int &ans = dp[L][R];
  if (~ans)
    return ans;

  int c1 = a[L] - go(L + 1, R);
  int c2 = a[R] - go(L, R - 1);

  return ans = max(c1, c2);
}

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

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

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

const int N = 3e3 + 5, inf = 1e18, mod = 1e9 + 7;

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

int go(int L, int R) {
  if (L > R)
    return 0;

  int &ans = dp[L][R];
  if (~ans)
    return ans;

  int c1 = a[L] - go(L + 1, R);
  int c2 = a[R] - go(L, R - 1);

  return ans = max(c1, c2);
}

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

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
int dp[4000][4000];
int a[8000];
int solve(int l, int r) {
  if (l > r || l <= 0 || r <= 0)
    return 0;
  if (l == r)
    return a[l];
  if (dp[l][r] != -1)
    return dp[l][r];
  if (r - l + 1 == 2)
    return max(a[l], a[r]);
  return max(a[l] + min(solve(l + 2, r), solve(l + 1, r - 1)),
             a[r] + min(solve(l + 1, r - 1), solve(l, r - 2)));
}
signed main() {
  int n;
  cin >> n;
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  for (int i = 0; i < n + 4; i++)
    for (int j = 0; j < n + 4; j++)
      dp[i][j] = -1;
  for (int i = 1; i <= n; i++)
    dp[i][i] = a[i];
  cout << 2 * solve(1, n) - (sum) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int dp[4000][4000];
int a[8000];
int solve(int l, int r) {
  if (l > r || l <= 0 || r <= 0)
    return 0;
  if (l == r)
    return a[l];
  if (dp[l][r] != -1)
    return dp[l][r];
  if (r - l + 1 == 2)
    return max(a[l], a[r]);
  return dp[l][r] = max(a[l] + min(solve(l + 2, r), solve(l + 1, r - 1)),
                        a[r] + min(solve(l + 1, r - 1), solve(l, r - 2)));
}
signed main() {
  int n;
  cin >> n;
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  for (int i = 0; i < n + 4; i++)
    for (int j = 0; j < n + 4; j++)
      dp[i][j] = -1;
  for (int i = 1; i <= n; i++)
    dp[i][i] = a[i];
  cout << 2 * solve(1, n) - (sum) << endl;
}
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;
typedef long long ll;
typedef pair<ll, ll> P;

const int MAXN = 3005;
vector<vector<vector<P>>> dp(MAXN, vector<vector<P>>(2, vector<P>(MAXN)));
vector<int> a(MAXN);
bool visited[MAXN][MAXN][2];

P q(int i, int j, int k) { // k=1太郎, a_i...a_jでkが操作
  if (visited[i][j][k])
    return dp[i][j][k];
  else if (i > j)
    return P(0, 0);
  P front, back;
  if (k == 0) { // 太郎
    front = q(i + 1, j, k + 1);
    front.first += a[i];
    back = q(i, j - 1, k + 1);
    back.first += a[j];
    if (front.first - front.second >= back.first - back.second)
      dp[i][j][k] = front;
    else
      dp[i][j][k] = back;
  } else if (k == 1) {
    front = q(i + 1, j, k - 1);
    front.second += a[i];
    back = q(i, j - 1, k - 1);
    back.second += a[j];
    if (front.first - front.second <= back.first - back.second)
      dp[i][j][k] = front;
    else
      dp[i][j][k] = back;
  }
  visited[i][j][k] = true;
  return dp[i][j][k];
}

int main() {
  int N;
  cin >> N;
  rep(i, N) cin >> a[i];
  P result = q(0, N - 1, 0);
  cout << (result.first - result.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 long long ll;
typedef pair<ll, ll> P;

const int MAXN = 3005;
vector<vector<vector<P>>> dp(MAXN, vector<vector<P>>(MAXN, vector<P>(2)));
vector<int> a(MAXN);
bool visited[MAXN][MAXN][2];

P q(int i, int j, int k) { // k=1太郎, a_i...a_jでkが操作
  if (visited[i][j][k])
    return dp[i][j][k];
  else if (i > j)
    return P(0, 0);
  P front, back;
  if (k == 0) { // 太郎
    front = q(i + 1, j, k + 1);
    front.first += a[i];
    back = q(i, j - 1, k + 1);
    back.first += a[j];
    if (front.first - front.second >= back.first - back.second)
      dp[i][j][k] = front;
    else
      dp[i][j][k] = back;
  } else if (k == 1) {
    front = q(i + 1, j, k - 1);
    front.second += a[i];
    back = q(i, j - 1, k - 1);
    back.second += a[j];
    if (front.first - front.second <= back.first - back.second)
      dp[i][j][k] = front;
    else
      dp[i][j][k] = back;
  }
  visited[i][j][k] = true;
  return dp[i][j][k];
}

int main() {
  int N;
  cin >> N;
  rep(i, N) cin >> a[i];
  P result = q(0, N - 1, 0);
  cout << (result.first - result.second) << endl;
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// #include<bits/extc++.h>
using namespace std;
// using namespace __gnu_pbds;
// typedef
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
// set_t;
#define mp(a, b) make_pair((a), (b))
#define pii pair<int, int>
#define pll pair<LL, LL>
#define pdd pair<double, double>
#define pb push_back
#define x first
#define y second
#define sqr(x) ((x) * (x))
#define EPS 1e-6
#define MEM(x) memset(x, 0, sizeof(x))
#define MEMS(x) memset(x, -1, sizeof(x))
#define pi acos(-1)
#define index Index
#define Line pll
typedef long long LL;
LL dp[3005][3005];
int vis[3005][3005];
int a[3005];
LL DP(int l, int r) {
  if (l > r)
    return 0;
  if (vis[l][r])
    return dp[l][r];
  dp[l][r] = max(a[r] - DP(l, r - 1), a[l] - DP(l + 1, r));
  return dp[l][r];
}
int main() {
  int n;
  scanf("%d", &n);
  MEM(vis);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  printf("%lld\n", DP(1, n));
}


Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// #include<bits/extc++.h>
using namespace std;
// using namespace __gnu_pbds;
// typedef
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
// set_t;
#define mp(a, b) make_pair((a), (b))
#define pii pair<int, int>
#define pll pair<LL, LL>
#define pdd pair<double, double>
#define pb push_back
#define x first
#define y second
#define sqr(x) ((x) * (x))
#define EPS 1e-6
#define MEM(x) memset(x, 0, sizeof(x))
#define MEMS(x) memset(x, -1, sizeof(x))
#define pi acos(-1)
#define index Index
#define Line pll
typedef long long LL;
LL dp[3005][3005];
int vis[3005][3005];
int a[3005];
LL DP(int l, int r) {
  if (l > r)
    return 0;
  if (vis[l][r])
    return dp[l][r];
  dp[l][r] = max(a[r] - DP(l, r - 1), a[l] - DP(l + 1, r));
  vis[l][r] = 1;
  return dp[l][r];
}
int main() {
  int n;
  scanf("%d", &n);
  MEM(vis);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  printf("%lld\n", DP(1, n));
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long dp[2001][2001];
long long a[2001];
long long game(int i, int j, int c) {
  // cout<<i<<" "<<j<<" "<<x<<" "<<y<<endl;
  if (dp[i][j] != -1) {
    return dp[i][j];
  }
  long long ans;
  if (i > j) {
    //  cout<<x<<" "<<y<<"%%%%5"<<endl;
    return 0;
  } else {
    if (c % 2 == 0) {
      // if(i == j)return (x + a[i] - y);
      ans = max(game(i + 1, j, c + 1) + a[i], game(i, j - 1, c + 1) + a[j]);
      //    cout<<game(i + 1, j, x + a[i], y, c + 1)<<"****"<<game(i, j-1, x +
      //    a[j], y, c + 1)<<endl;
    } else {
      // if(i == j)return (x - y - a[i]);
      ans = min(game(i + 1, j, c + 1) - a[i], game(i, j - 1, c + 1) - a[j]);
      //    cout<<game(i + 1, j, x , y + a[i], c + 1)<<"****"<<game(i, j-1, x ,
      //    y + a[j], c + 1)<<endl;
    }
  }
  //  cout<<i<<"#"<<j<<"#"<<c<<"#"<<ans<<endl;
  dp[i][j] = ans;
  return ans;
}

int main() {
  long long mod = 1000000007;
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  // vector<int> a(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  memset(dp, -1, sizeof(dp));
  cout << game(0, n - 1, 0) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long dp[3001][3001];
long long a[3001];
long long game(int i, int j, int c) {
  // cout<<i<<" "<<j<<" "<<x<<" "<<y<<endl;
  if (dp[i][j] != -1) {
    return dp[i][j];
  }
  long long ans;
  if (i > j) {
    //  cout<<x<<" "<<y<<"%%%%5"<<endl;
    return 0;
  } else {
    if (c % 2 == 0) {
      // if(i == j)return (x + a[i] - y);
      ans = max(game(i + 1, j, c + 1) + a[i], game(i, j - 1, c + 1) + a[j]);
      //    cout<<game(i + 1, j, x + a[i], y, c + 1)<<"****"<<game(i, j-1, x +
      //    a[j], y, c + 1)<<endl;
    } else {
      // if(i == j)return (x - y - a[i]);
      ans = min(game(i + 1, j, c + 1) - a[i], game(i, j - 1, c + 1) - a[j]);
      //    cout<<game(i + 1, j, x , y + a[i], c + 1)<<"****"<<game(i, j-1, x ,
      //    y + a[j], c + 1)<<endl;
    }
  }
  //  cout<<i<<"#"<<j<<"#"<<c<<"#"<<ans<<endl;
  dp[i][j] = ans;
  return ans;
}

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

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

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
#define watch(x) cout << (#x) << " is " << x << endl;
#define ll long long int
#define pb push_back
#define in insert
#define mp make_pair
#define F first
#define S second
#define N 1000000
#define MOD 1000000007
#define PI acos(-1)
ll dp[3005][3005];
ll func(ll l, ll r, ll a[]) {
  if (l > r)
    return 0;
  if (l == r)
    return dp[l][r] = a[l];
  return dp[l][r] = max(a[l] - func(l + 1, r, a), a[r] - func(l, r - 1, a));
}
int main() {
  IOS ll n;
  cin >> n;
  ll a[n], i;
  for (i = 0; i < n; i++)
    cin >> a[i];
  memset(dp, -1, sizeof(dp));
  ll ans = func(0, n - 1, a);
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
#define watch(x) cout << (#x) << " is " << x << endl;
#define ll long long int
#define pb push_back
#define in insert
#define mp make_pair
#define F first
#define S second
#define N 1000000
#define MOD 1000000007
#define PI acos(-1)
ll dp[3005][3005];
ll func(ll l, ll r, ll a[]) {
  if (l > r)
    return 0;
  if (dp[l][r] != -1)
    return dp[l][r];
  if (l == r)
    return dp[l][r] = a[l];
  return dp[l][r] = max(a[l] - func(l + 1, r, a), a[r] - func(l, r - 1, a));
}
int main() {
  IOS ll n;
  cin >> n;
  ll a[n], i;
  for (i = 0; i < n; i++)
    cin >> a[i];
  memset(dp, -1, sizeof(dp));
  ll ans = func(0, n - 1, a);
  cout << ans;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
long long int dp[3001][3001];
long long int min(long long int a, long long int b) {
  if (a > b)
    return b;
  return a;
}
long long int max(long long int a, long long int b) {
  if (a > b)
    return a;
  return b;
}
long long int util(vector<long long int> &v, int i, int j) {
  if (i == j)
    return v[i];
  if (i + 1 == j)
    return max(v[i], v[j]);
  long long int ma = max(v[i] + min(util(v, i + 1, j - 1), util(v, i + 2, j)),
                         v[j] + min(util(v, i, j - 2), util(v, i + 1, j - 1)));
  dp[i][j] = ma;
  return ma;
}
int main() {
  int n;
  cin >> n;
  vector<long long int> v(n);
  memset(dp, -1, sizeof(dp));
  long long int tot = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    tot += v[i];
  }
  long long int ret = util(v, 0, n - 1);
  cout << (2 * 1LL * ret - tot) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
long long int dp[3001][3001];
long long int min(long long int a, long long int b) {
  if (a > b)
    return b;
  return a;
}
long long int max(long long int a, long long int b) {
  if (a > b)
    return a;
  return b;
}
long long int util(vector<long long int> &v, int i, int j) {
  if (i == j)
    return v[i];
  if (i + 1 == j)
    return max(v[i], v[j]);
  if (dp[i][j] != -1)
    return dp[i][j];
  long long int ma = max(v[i] + min(util(v, i + 1, j - 1), util(v, i + 2, j)),
                         v[j] + min(util(v, i, j - 2), util(v, i + 1, j - 1)));
  dp[i][j] = ma;
  return ma;
}
int main() {
  int n;
  cin >> n;
  vector<long long int> v(n);
  memset(dp, -1, sizeof(dp));
  long long int tot = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    tot += v[i];
  }
  long long int ret = util(v, 0, n - 1);
  cout << (2 * 1LL * ret - tot) << endl;
  return 0;
}
Problem p03171 (C++)

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

#define int long long
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);

using namespace std;

const int N = 305;
int dp[N][N];

signed main() {
  fastio int n;
  cin >> n;
  int a[n];
  for (auto &x : a)
    cin >> x;
  for (int i = n - 1; i >= 0; i--) {
    for (int j = i; j < n; j++) {
      if (i == j)
        dp[i][j] = a[i];
      else
        dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]);
    }
  }
  cout << dp[0][n - 1] << "\n";
}

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

#define int long long
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);

using namespace std;

const int N = 3005;
int dp[N][N];

signed main() {
  fastio int n;
  cin >> n;
  int a[n];
  for (auto &x : a)
    cin >> x;
  for (int i = n - 1; i >= 0; i--) {
    for (int j = i; j < n; j++) {
      if (i == j)
        dp[i][j] = a[i];
      else
        dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]);
    }
  }
  cout << dp[0][n - 1] << "\n";
}
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>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<int, null_type, less_equal<int>, rb_tree_tag,                           \
       tree_order_statistics_node_update>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define all(c) c.begin(), c.end()
#define llv vector<ll>
#define mp make_pair
#define endl "\n"
const int N = (int)3e3 + 10;
const int M = (int)1000000007;

ll dp[N][N], a[N];
bool mark[N][N] = {};
ll check(int be, int en) {
  if (mark[be][en])
    return dp[be][en];
  if (en == be + 1) {
    mark[be][en] = 1;
    ll d = abs(a[en] - a[be]);
    return dp[be][en] = d;
  }
  mark[be][en] = 1;
  return dp[be][en] = max(a[be] - check(be + 1, en), a[en] - check(be, en - 1));
}

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n, i;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  cout << check(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>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<int, null_type, less_equal<int>, rb_tree_tag,                           \
       tree_order_statistics_node_update>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define all(c) c.begin(), c.end()
#define llv vector<ll>
#define mp make_pair
#define endl "\n"
const int N = (int)3e3 + 10;
const int M = (int)1000000007;

ll dp[N][N], a[N];
bool mark[N][N] = {};
ll check(int be, int en) {
  if (mark[be][en])
    return dp[be][en];
  if (en == be + 1) {
    mark[be][en] = 1;
    ll d = abs(a[en] - a[be]);
    return dp[be][en] = d;
  }
  mark[be][en] = 1;
  return dp[be][en] = max(a[be] - check(be + 1, en), a[en] - check(be, en - 1));
}

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n, i;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (n == 1)
    cout << a[0];
  else
    cout << check(0, n - 1);

  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define f first
#define s second
#define int long long
#define pb push_back
#define pencho                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define N 1005
#define mod 1000000007

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

int solveDp(int index_1, int index_2) {
  if (index_1 > index_2)
    return 0;
  if (dp[index_1][index_2] != -1)
    return dp[index_1][index_2];

  dp[index_1][index_2] = max(a[index_1] - solveDp(index_1 + 1, index_2),
                             a[index_2] - solveDp(index_1, index_2 - 1));
  return dp[index_1][index_2];
}

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

signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("o.txt", "w", stdout);
#endif

  solve();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define f first
#define s second
#define int long long
#define pb push_back
#define pencho                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define N 3005
#define mod 1000000007

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

int solveDp(int index_1, int index_2) {
  if (index_1 > index_2)
    return 0;
  if (dp[index_1][index_2] != -1)
    return dp[index_1][index_2];

  dp[index_1][index_2] = max(a[index_1] - solveDp(index_1 + 1, index_2),
                             a[index_2] - solveDp(index_1, index_2 - 1));
  return dp[index_1][index_2];
}

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

signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("o.txt", "w", stdout);
#endif

  solve();
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define pb push_back
#define F first
#define S second
// cout<<fixed<<setprecision(11);
const string nl = "\n";
const ll MOD = 1e9 + 7;
const ll ARR_MAX = 3e3 + 1;
const ll INF = 1e14;

ll dp[ARR_MAX]
     [ARR_MAX]; // dp[i][j] = maximum value of X-Y when a[i ... j] is left.

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

int main() {
  fastio();
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define pb push_back
#define F first
#define S second
// cout<<fixed<<setprecision(11);
const string nl = "\n";
const ll MOD = 1e9 + 7;
const ll ARR_MAX = 3e3 + 1;
const ll INF = 1e14;

ll dp[ARR_MAX]
     [ARR_MAX]; // dp[i][j] = maximum value of X-Y when a[i ... j] is left.

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

int main() {
  fastio();
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

typedef long long LL;
const int MOD = 1e9 + 7;

LL dfs(int x, int y, vector<vector<LL>> &dp, vector<LL> &sif) {
  if (x > y)
    return 0ll;
  return max(sif[y + 1] - sif[x] - dfs(x + 1, y, dp, sif),
             sif[y + 1] - sif[x] - dfs(x, y - 1, dp, sif));
}
void solve() {
  int n;
  cin >> n;
  vector<int> nums(n);
  for (int i = 0; i < n; i++)
    cin >> nums[i];
  vector<LL> sif(n + 1, 0);
  for (int i = 0; i < n; i++) {
    sif[i + 1] = sif[i] + nums[i];
  }
  vector<vector<LL>> dp(n, vector<LL>(n, 0ll));
  cout << 2ll * dfs(0, n - 1, dp, sif) - sif[n] << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

typedef long long LL;
const int MOD = 1e9 + 7;

LL dfs(int x, int y, vector<vector<LL>> &dp, vector<LL> &sif) {
  if (x > y)
    return 0ll;
  return max(sif[y + 1] - sif[x] - dfs(x + 1, y, dp, sif),
             sif[y + 1] - sif[x] - dfs(x, y - 1, dp, sif));
}
void solve() {
  int n;
  cin >> n;
  vector<int> nums(n);
  for (int i = 0; i < n; i++)
    cin >> nums[i];
  vector<LL> sif(n + 1, 0);
  for (int i = 0; i < n; i++) {
    sif[i + 1] = sif[i] + nums[i];
  }
  vector<vector<LL>> dp(n, vector<LL>(n, 0ll));
  for (int i = 0; i < n; i++) {
    dp[i][i] = nums[i];
  }
  for (int len = 2; len <= n; len++) {
    for (int i = 0; i < (n - len + 1); i++) {
      int j = i + len - 1;
      dp[i][j] = max(sif[j + 1] - sif[i] - dp[i + 1][j],
                     sif[j + 1] - sif[i] - dp[i][j - 1]);
    }
  }
  cout << 2ll * dp[0][n - 1] - sif[n] << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define dd double
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep1(i, b) for (int i = 1; i <= b; i++)
#define pb push_back
#define mp make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define F first
#define S second
#define vec vector<int, int>
#define int long long
#define pii pair<int, int>
int visited[100001];

// int ax,ay,bx,by,t_x0,t_y0;
int dp[1001][1001];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // ios_base& scientific (ios_base& str);

  int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    int arr[n];
    int sum = 0;
    rep(i, n) {
      cin >> arr[i];
      sum += arr[i];
    }
    // sort(arr,arr+n);
    for (int i = 0; i < n; i++) {
      dp[i][i] = arr[i];
    }
    for (int i = 0; i < n - 1; i++) {
      dp[i][i + 1] = max(arr[i], arr[i + 1]);
    }
    int a = 2;
    while (1) {
      if (a >= n)
        break;
      for (int i = 0; i < n - a; i++) {
        int j = i + a;
        dp[i][i + a] = max(arr[i] + min(dp[i + 2][j], dp[i + 1][j - 1]),
                           arr[j] + min(dp[i + 1][j - 1], dp[i][j - 2]));
      }
      a++;
    }
    cout << (2 * dp[0][n - 1] - sum) << endl;
  }
  // exit();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define dd double
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep1(i, b) for (int i = 1; i <= b; i++)
#define pb push_back
#define mp make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define F first
#define S second
#define vec vector<int, int>
#define int long long
#define pii pair<int, int>
int visited[100001];

// int ax,ay,bx,by,t_x0,t_y0;
int dp[3001][3001];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // ios_base& scientific (ios_base& str);

  int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    int arr[n];
    int sum = 0;
    rep(i, n) {
      cin >> arr[i];
      sum += arr[i];
    }
    // sort(arr,arr+n);
    for (int i = 0; i < n; i++) {
      dp[i][i] = arr[i];
    }
    for (int i = 0; i < n - 1; i++) {
      dp[i][i + 1] = max(arr[i], arr[i + 1]);
    }
    int a = 2;
    while (1) {
      if (a >= n)
        break;
      for (int i = 0; i < n - a; i++) {
        int j = i + a;
        dp[i][i + a] = max(arr[i] + min(dp[i + 2][j], dp[i + 1][j - 1]),
                           arr[j] + min(dp[i + 1][j - 1], dp[i][j - 2]));
      }
      a++;
    }
    cout << (2 * dp[0][n - 1] - sum) << endl;
  }
  // exit();
  return 0;
}
Problem p03171 (C++)

Buggy submission (status: Runtime Error):

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int a[5555];
int cache[5555][5555];
void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
int solve(int start, int end) {
  if (start > end) {
    return 0;
  }
  if (start == end) {
    return a[start];
  }
  if (cache[start][end] != -1) {
    return cache[start][end];
  }
  return cache[start][end] = max(
             a[start] + min(solve(start + 2, end), solve(start + 1, end - 1)),
             a[end] + min(solve(start, end - 2), solve(start + 1, end - 1)));
}
int32_t main() {
  c_p_c();
  memset(cache, -1, sizeof(cache));
  int n, ts = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ts += a[i];
  }
  int x = solve(0, n - 1);
  int y = ts - x;
  cout << x - y << endl;
  return 0;
}


Fixed submission:

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int a[5555];
int cache[5555][5555];
void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
int solve(int start, int end) {
  if (start > end) {
    return 0;
  }
  if (start == end) {
    return a[start];
  }
  if (cache[start][end] != -1) {
    return cache[start][end];
  }
  return cache[start][end] = max(
             a[start] + min(solve(start + 2, end), solve(start + 1, end - 1)),
             a[end] + min(solve(start, end - 2), solve(start + 1, end - 1)));
}
int32_t main() {
  // c_p_c();
  memset(cache, -1, sizeof(cache));
  int n, ts = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ts += a[i];
  }
  int x = solve(0, n - 1);
  int y = ts - x;
  cout << x - y << endl;
  return 0;
}
Problem p03172 (C++)

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

#define MEM(var, val) memset(var, (val), sizeof(var))
const int M = 1e9 + 7;
vll a;
ll n, k;
vector<vll> dp;
vll pref;
void dist(ll i) {
  for (int j = 0; j <= k; j++)
    if (j > a[i])
      dp[i][j] = (pref[j] - pref[j - a[i] - 1] + M) % M;
    else
      dp[i][j] = pref[j];
  pref[0] = dp[i][0];
  for (int j = 1; j <= k; j++)
    pref[j] = (pref[j - 1] + dp[i][j]) % M;
  if (i > 0)
    dist(i - 1);
  return;
}
int main() {

  cin >> n >> k;
  dp.resize(n + 1, vll(k + 1, 0));
  a.resize(n + 1);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int j = 0; j <= a[n - 1]; j++)
    dp[n - 1][j] = 1;
  pref.resize(k + 1, 0);
  pref[0] = 1;
  for (int j = 1; j <= k; j++)
    pref[j] = pref[j - 1] + dp[n - 1][j];
  // MEM(dp,-1);
  dist(n - 2);
  cout << dp[0][k] % M;
}

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

#define MEM(var, val) memset(var, (val), sizeof(var))
const int M = 1e9 + 7;
vll a;
ll n, k;
vector<vll> dp;
vll pref;
void dist(ll i) {
  for (int j = 0; j <= k; j++)
    if (j > a[i])
      dp[i][j] = (pref[j] - pref[j - a[i] - 1] + M) % M;
    else
      dp[i][j] = pref[j];
  pref[0] = dp[i][0];
  for (int j = 1; j <= k; j++)
    pref[j] = (pref[j - 1] + dp[i][j]) % M;
  if (i > 0)
    dist(i - 1);
  return;
}
int main() {

  cin >> n >> k;
  dp.resize(n + 1, vll(k + 1, 0));
  a.resize(n + 1);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int j = 0; j <= a[n - 1]; j++)
    dp[n - 1][j] = 1;
  pref.resize(k + 1, 0);
  pref[0] = 1;
  for (int j = 1; j <= k; j++)
    pref[j] = pref[j - 1] + dp[n - 1][j];
  // MEM(dp,-1);
  if (n >= 2)
    dist(n - 2);
  cout << dp[0][k] % M;
}
Problem p03172 (C++)

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

int N, K;
vector<int> a(110);
vector<vector<ll>> dp(110, vector<ll>(100010, 0));

int main() {
  cin >> N >> K;
  rep(i, N) { cin >> a[i]; }

  vector<ll> sum(K + 1, 0);
  dp[0][0] = 1;
  for (int i = 0; i < N; i++) {
    sum[0] = 0;
    for (int j = 0; j <= K; j++)
      sum[j + 1] = (sum[j] + dp[i][j]) % MOD;
    for (int j = 0; j <= K; j++)
      dp[i + 1][j] = (sum[j + 1] - sum[max(0, j - a[i])] + MOD) % MOD;
  }
  cout << dp[N][K] << endl;
}

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

int N, K;
vector<int> a(110);
vector<vector<ll>> dp(110, vector<ll>(100010, 0));

int main() {
  cin >> N >> K;
  rep(i, N) { cin >> a[i]; }

  vector<ll> sum(100010, 0);
  dp[0][0] = 1;
  for (int i = 0; i < N; i++) {
    sum[0] = 0;
    for (int j = 0; j <= K; j++)
      sum[j + 1] = (sum[j] + dp[i][j]) % MOD;
    for (int j = 0; j <= K; j++)
      dp[i + 1][j] = (sum[j + 1] - sum[max(0, j - a[i])] + MOD) % MOD;
  }
  cout << dp[N][K] << endl;
}
Problem p03172 (C++)

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

long long dp[110][1000010], sdp[1000010], a[1100], sa[1100], n, m;

int main() {
  sa[0] = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> a[i], sa[i] = max(sa[i - 1] + a[i], m);
  memset(dp, 0, sizeof dp);
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    sdp[0] = 1;
    for (long long k = 1; k <= (sa[i] + 100); k++)
      sdp[k] = sdp[k - 1] + dp[i - 1][k];

    for (long long j = 0; j <= sa[i]; j++) {
      if (j <= sa[i])
        dp[i][j] = sdp[j] - ((j - a[i] - 1) >= 0 ? sdp[j - a[i] - 1] : 0);
      dp[i][j] %= 1000000007;
    }
  }
  cout << dp[n][m];
}

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

long long dp[110][1000010], sdp[1000010], a[1100], sa[1100], n, m;

int main() {
  sa[0] = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> a[i], sa[i] = min(sa[i - 1] + a[i], m);
  memset(dp, 0, sizeof dp);
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    sdp[0] = 1;
    for (long long k = 1; k <= (sa[i] + 100); k++)
      sdp[k] = sdp[k - 1] + dp[i - 1][k];

    for (long long j = 0; j <= sa[i]; j++) {
      if (j <= sa[i])
        dp[i][j] = sdp[j] - ((j - a[i] - 1) >= 0 ? sdp[j - a[i] - 1] : 0);
      dp[i][j] %= 1000000007;
    }
  }
  cout << dp[n][m];
}
Problem p03172 (C++)

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

using namespace std;

const int MOD = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> pref(k), dp(k);
  dp[0] = 1;
  for (int kid = 1; kid <= n; kid++) {
    int c;
    cin >> c;
    pref[0] = dp[0];
    dp[0] = 0;
    for (int i = 1; i <= k; i++) {
      pref[i] = (pref[i - 1] + dp[i]) % MOD;
    }
    for (int i = 0; i <= k; i++) {
      dp[i] = (pref[i] - (i - c >= 0 ? pref[i - c - 1] : 0)) % MOD;
    }
  }

  int ans = dp[k];
  if (ans < 0)
    ans += MOD;
  printf("%d\n", ans);

  return 0;
}


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

using namespace std;

const int MOD = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> pref(k + 1), dp(k + 1);
  dp[0] = 1;
  for (int kid = 1; kid <= n; kid++) {
    int c;
    cin >> c;
    pref[0] = dp[0];
    dp[0] = 0;
    for (int i = 1; i <= k; i++) {
      pref[i] = (pref[i - 1] + dp[i]) % MOD;
    }
    for (int i = 0; i <= k; i++) {
      dp[i] = (pref[i] - (i - c >= 0 ? pref[i - c - 1] : 0)) % MOD;
    }
  }

  int ans = dp[k];
  if (ans < 0)
    ans += MOD;
  printf("%d\n", ans);

  return 0;
}
Problem p03172 (C++)

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

using namespace std;

#define mp make_pair
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define allarr(a) a, a + n
#define ll long long
#define ull unsigned long long
#define pb push_back
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<int, pi> trp;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
// int ab  (int  x ) { return (x>0?x:-x); }

const int F = 3e5 + 5;
const ll mod = 1e9 + 7;
ll dp[105][F];
int a[105];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= k; j++) {

      dp[i + 1][j] += dp[i][j]; // dp[i+1][j]%=mod;
      // if(j+a[i]<=k){
      dp[i + 1][j + a[i] + 1] -= dp[i][j]; // dp[i+1][j+a[i]+1]%=mod;
    }
    dp[i + 1][0] %= mod;
    for (int j = 1; j <= k; j++)
      dp[i + 1][j] += dp[i + 1][j - 1], assert(dp[i + 1][j] >= 0),
          dp[i + 1][j] %= mod;
  }
  // for(int i = 1 ; i <= n ; i++ ){for(int j = 0 ; j <= k ; j++ )cout <<
  // dp[i][j]<<" " ; cout<<endl; }
  cout << dp[n][k] << endl;
}

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

using namespace std;

#define mp make_pair
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define allarr(a) a, a + n
#define ll long long
#define ull unsigned long long
#define pb push_back
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<int, pi> trp;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
// int ab  (int  x ) { return (x>0?x:-x); }

const int F = 3e5 + 5;
const ll mod = 1e9 + 7;
ll dp[105][F];
int a[105];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= k; j++) {

      dp[i + 1][j] += dp[i][j]; // dp[i+1][j]%=mod;
      // if(j+a[i]<=k){
      dp[i + 1][j + a[i] + 1] -= dp[i][j]; // dp[i+1][j+a[i]+1]%=mod;
    }
    dp[i + 1][0] %= mod;
    for (int j = 1; j <= k; j++)
      dp[i + 1][j] += dp[i + 1][j - 1] + mod, assert(dp[i + 1][j] >= 0),
          dp[i + 1][j] %= mod;
  }
  // for(int i = 1 ; i <= n ; i++ ){for(int j = 0 ; j <= k ; j++ )cout <<
  // dp[i][j]<<" " ; cout<<endl; }
  cout << dp[n][k] << endl;
}
Problem p03172 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, double> Pid;
typedef pair<double, int> Pdi;
const double PI = 3.1415926535897932; // acos(-1)
const double EPS = 1e-15;
const int INF = 1001001001;
const int mod = 1e+9 + 7;

ll dp[105][100005];

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

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

  dp[0][k] = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= k; ++j) {
      int pos = max(0, j - a[i]);
      dp[i + 1][pos] = (dp[i + 1][pos] + dp[i][j]) % mod;
      dp[i + 1][j + 1] = (dp[i + 1][j + 1] - dp[i][j]) % mod;
      while (dp[i + 1][j + 1] < 0)
        dp[i + 1][j + 1] += mod;
    }
    for (int j = 0; j <= k; ++j) {
      dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i + 1][j]) % mod;
    }
  }
  cout << dp[n][0] << endl;
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= k + 1; ++j) {
      cerr << dp[i][j] << " ";
    }
    cerr << "\n";
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, double> Pid;
typedef pair<double, int> Pdi;
const double PI = 3.1415926535897932; // acos(-1)
const double EPS = 1e-15;
const int INF = 1001001001;
const int mod = 1e+9 + 7;

ll dp[105][100005];

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

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

  dp[0][k] = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= k; ++j) {
      int pos = max(0, j - a[i]);
      dp[i + 1][pos] = (dp[i + 1][pos] + dp[i][j]) % mod;
      dp[i + 1][j + 1] = (dp[i + 1][j + 1] - dp[i][j]) % mod;
      while (dp[i + 1][j + 1] < 0)
        dp[i + 1][j + 1] += mod;
    }
    for (int j = 0; j <= k; ++j) {
      dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i + 1][j]) % mod;
    }
  }
  cout << dp[n][0] << endl;
}
Problem p03172 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define int long long
using namespace std;

int dp[105][200005];
int mod = 1e9 + 7;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("INPUT.txt", "r", stdin);
  freopen("OUTPUT.txt", "w", stdout);
#endif

  clock_t clk;
  clk = clock();

  // --------------Code starts
  // here---------------------------------------------------------------------

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

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

    for (int j = 1; j <= k; j++) {
      dp[i][j] += dp[i][j - 1];
      dp[i][j] %= mod;
    }
  }
  cout << dp[n][k];

  // -------------------------------------Code ends
  // here------------------------------------------------------

  clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk) / CLOCKS_PER_SEC
       << "\n";
  return 0;
}


Fixed submission:
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define int long long
using namespace std;

int dp[105][200005];
int mod = 1e9 + 7;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // #ifndef ONLINE_JUDGE
  //    freopen ("INPUT.txt" , "r" , stdin);
  //    freopen ("OUTPUT.txt" , "w" , stdout);
  // #endif

  clock_t clk;
  clk = clock();

  // --------------Code starts
  // here---------------------------------------------------------------------

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

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

    for (int j = 1; j <= k; j++) {
      dp[i][j] += dp[i][j - 1];
      dp[i][j] %= mod;
    }
  }
  cout << dp[n][k];

  // -------------------------------------Code ends
  // here------------------------------------------------------

  clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk) / CLOCKS_PER_SEC
       << "\n";
  return 0;
}
Problem p03172 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef long double ld;

ll mod = 1e9 + 7;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> arr(n);
  for (int z = 1; z <= n; z++) {
    cin >> arr[z];
  }
  vector<vector<ll>> dp(n + 1, vector<ll>(k + 1, 0));
  dp[0][0] = 1;
  ll count = 0;
  for (int z = 1; z <= n; z++) {
    ll sum = 0;
    count += arr[z];
    for (int x = 0; x <= count && x <= k; x++) {
      if (x > arr[z])
        sum -= dp[z - 1][x - arr[z] - 1];
      sum += dp[z - 1][x];
      if (sum < 0)
        sum += mod;
      dp[z][x] += sum;
      dp[z][x] %= mod;
      sum %= mod;
    }
  }
  // for (int z=0;z<=n;z++){
  // 	for (int x=0;x<=k;x++){
  // 		cout << dp[z][x] << " ";
  // 	} cout << endl;
  // }
  cout << dp[n][k] << endl;
  cin >> n;
}

Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef long double ld;

ll mod = 1e9 + 7;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> arr(n + 1);
  for (int z = 1; z <= n; z++) {
    cin >> arr[z];
  }
  vector<vector<ll>> dp(n + 1, vector<ll>(k + 1, 0));
  dp[0][0] = 1;
  ll count = 0;
  for (int z = 1; z <= n; z++) {
    ll sum = 0;
    count += arr[z];
    for (int x = 0; x <= count && x <= k; x++) {
      if (x > arr[z])
        sum -= dp[z - 1][x - arr[z] - 1];
      sum += dp[z - 1][x];
      if (sum < 0)
        sum += mod;
      dp[z][x] += sum;
      dp[z][x] %= mod;
      sum %= mod;
    }
  }
  // for (int z=0;z<=n;z++){
  // 	for (int x=0;x<=k;x++){
  // 		cout << dp[z][x] << " ";
  // 	} cout << endl;
  // }
  cout << dp[n][k] << endl;
  cin >> n;
}
Problem p03172 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int, int> P;
constexpr int mod = 1e9 + 7;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<int> dp(k + 2, 0);
  dp[0] = 1;
  rep(i, n) {
    vector<int> cum(k + 1, 0);
    rep(j, k + 1)(cum[j + 1] = cum[j] + dp[j]) %= mod;
    rep(j, k + 1)(dp[j] = (cum[j + 1] - cum[max(0, j - a[i])]) + mod) %= mod;
  }
  cout << dp[k] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int, int> P;
constexpr int mod = 1e9 + 7;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<int> dp(k + 2, 0);
  dp[0] = 1;
  rep(i, n) {
    vector<int> cum(k + 2, 0);
    rep(j, k + 1)(cum[j + 1] = cum[j] + dp[j]) %= mod;
    rep(j, k + 1)(dp[j] = (cum[j + 1] - cum[max(0, j - a[i])]) + mod) %= mod;
  }
  cout << dp[k] << endl;
  return 0;
}