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

Buggy submission (status: Runtime Error):
#include "algorithm"
#include "climits"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "math.h"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "utility"

using namespace std;
const long long int MOD = 1000000007;

long long int N, T;
long long int num[100000];
long long int ans;

int main() {
  ios::sync_with_stdio(false);
  cin >> N >> T;
  for (int i = 0; i < N; i++)
    cin >> num[i];
  for (int i = 1; i < N; i++)
    ans += min(T, num[i] - num[i - 1]);
  ans += T;
  cout << ans << endl;
}

Fixed submission:
#include "algorithm"
#include "climits"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "math.h"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "utility"

using namespace std;
const long long int MOD = 1000000007;

long long int N, T;
long long int num[200000];
long long int ans;

int main() {
  ios::sync_with_stdio(false);
  cin >> N >> T;
  for (int i = 0; i < N; i++)
    cin >> num[i];
  for (int i = 1; i < N; i++)
    ans += min(T, num[i] - num[i - 1]);
  ans += T;
  cout << ans << endl;
}
Problem p03733 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

int g[120000];
int i, m, n, s, t;

int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++)
    scanf("%d", &g[i]);
  t = 1;
  for (i = 2; i <= n; i++)
    if (g[i] - g[t] >= m)
      if (g[i] - g[i - 1] >= m)
        s = s + g[i - 1] - g[t] + m, t = i;
      else
        s = s + g[i - 1] - g[t], t = i - 1;
  if (t != n)
    s = s + g[n] - g[t];
  s = s + m;
  printf("%d", s);
  return 0;
}

Fixed submission:
#include <cstdio>

int g[300000];
int i, m, n, s, t;

int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++)
    scanf("%d", &g[i]);
  t = 1;
  for (i = 2; i <= n; i++)
    if (g[i] - g[t] >= m)
      if (g[i] - g[i - 1] >= m)
        s = s + g[i - 1] - g[t] + m, t = i;
      else
        s = s + g[i - 1] - g[t], t = i - 1;
  if (t != n)
    s = s + g[n] - g[t];
  s = s + m;
  printf("%d", s);
  return 0;
}
Problem p03733 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll a[100005];
int main() {

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll a[1000005];
int main() {

  int n, t;
  cin >> n >> t;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll ans = 0;
  for (int i = 2; i <= n; i++) {
    if (a[i] >= a[i - 1] + t) {
      ans += t;
    } else {
      ans += a[i] - a[i - 1];
    }
  }
  cout << ans + t;
  return 0;
}
Problem p03733 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define rep(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007  // 10^9+7
#define INF 99999999999 // 10^12-1
#define P pair<int, int>
#define prique priority_queue
int n, t, ans = 0;
int ti[22222];
signed main() {
  cin >> n >> t;
  rep(i, n) cin >> ti[i];
  rep(i, n - 1) { ans += min(t, ti[i + 1] - ti[i]); }
  ans += t;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define rep(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007  // 10^9+7
#define INF 99999999999 // 10^12-1
#define P pair<int, int>
#define prique priority_queue
int n, t, ans = 0;
int ti[222222];
signed main() {
  cin >> n >> t;
  rep(i, n) cin >> ti[i];
  rep(i, n - 1) { ans += min(t, ti[i + 1] - ti[i]); }
  ans += t;
  cout << ans << endl;
  return 0;
}
Problem p03733 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define int long long
int mas[1000000];
signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  int t;
  cin >> t;
  for (int i = 1; i <= n; ++i) {
    cin >> mas[i];
  }
  int ans = 0;
  int end = mas[1] + t - 1;
  for (int i = 2; i <= n; ++i) {
    if (end >= mas[i] - 1) {
      ans += mas[i] - mas[i - 1];
    } else {
      ans += end - mas[i - 1] + 1;
    }
    end = mas[i] + t - 1;
  }
  ans += end - mas[n] + 1;
  cout << ans << endl;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define int long long
int mas[1000000];
signed main() {
#ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  int t;
  cin >> t;
  for (int i = 1; i <= n; ++i) {
    cin >> mas[i];
  }
  int ans = 0;
  int end = mas[1] + t - 1;
  for (int i = 2; i <= n; ++i) {
    if (end >= mas[i] - 1) {
      ans += mas[i] - mas[i - 1];
    } else {
      ans += end - mas[i - 1] + 1;
    }
    end = mas[i] + t - 1;
  }
  ans += end - mas[n] + 1;
  cout << ans << endl;
}
Problem p03733 (C++)

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

using namespace std;

long long int a, b, c[100010], d[100010], ans = 0, ant = 0;

int main() {
  cin >> a >> b;

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

  ans += b;
  c[0] += b;
  for (long long int i = 1; i < a; i++) {

    if (c[0] >= c[i] && c[0] <= c[i] + b) {
      ans += (c[i] + b - c[0]);
      c[0] = c[i];
      c[0] += b;
    } else if (c[0] < c[i]) {
      c[0] = c[i];
      ans += b;
      c[0] += b;
    }
  }

  cout << ans << endl;
}


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

using namespace std;

long long int a, b, c[2000100], d[100010], ans = 0, ant = 0;

int main() {
  cin >> a >> b;

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

  ans += b;
  c[0] += b;
  for (long long int i = 1; i < a; i++) {

    if (c[0] >= c[i] && c[0] <= c[i] + b) {
      ans += (c[i] + b - c[0]);
      c[0] = c[i];
      c[0] += b;
    } else if (c[0] < c[i]) {
      c[0] = c[i];
      ans += b;
      c[0] += b;
    }
  }

  cout << ans << endl;
}
Problem p03733 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#define maxn 3009
using namespace std;
const int MOD = 1e9 + 7;
#define N 200005
#define fr(i, x, y) for (int i = x; i <= y; i++)
int n, a[N], tt, pos;
void doit() {
  scanf("%d%d", &n, &tt);
  fr(i, 1, n) scanf("%d", &a[i]);
  // a[n+1]=2000000001;
  int tmp = 0, ans = a[n] + tt;
  fr(i, 2, n) if (a[i] - a[i - 1] > tt) ans -= (a[i] - a[i - 1] - tt);
  printf("%d\n", ans);
}

int main() {
  while (1)
    doit();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#define maxn 3009
using namespace std;
const int MOD = 1e9 + 7;
#define N 200005
#define fr(i, x, y) for (int i = x; i <= y; i++)
int n, a[N], tt, pos;
void doit() {
  scanf("%d%d", &n, &tt);
  fr(i, 1, n) scanf("%d", &a[i]);
  // a[n+1]=2000000001;
  int tmp = 0, ans = a[n] + tt;
  fr(i, 2, n) if (a[i] - a[i - 1] > tt) ans -= (a[i] - a[i - 1] - tt);
  printf("%d\n", ans);
}

int main() {
  // while (1)
  doit();
  return 0;
}
Problem p03733 (C++)

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

using namespace std;
typedef long long ll;
const int MAXN = 100100;

int N, T;
int arr[MAXN];

int main() {
  ios_base::sync_with_stdio(0);

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

  ll ans = T;
  for (int i = 0; i < N - 1; i++)
    ans += min(T, arr[i + 1] - arr[i]);
  cout << ans << "\n";
}

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

using namespace std;
typedef long long ll;
const int MAXN = 200100;

int N, T;
int arr[MAXN];

int main() {
  ios_base::sync_with_stdio(0);

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

  ll ans = T;
  for (int i = 0; i < N - 1; i++)
    ans += min(T, arr[i + 1] - arr[i]);
  cout << ans << "\n";
}
Problem p03733 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  ll ans = k;
  for (int i = 1; i < n; i++) {
    if (v[i] - v[i - 1] < k)
      ans += (v[i] - v[i - 1]);
    else
      ans += k;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  ll ans = k;
  for (int i = 1; i < n; i++) {
    if (v[i] - v[i - 1] < k)
      ans += (v[i] - v[i - 1]);
    else
      ans += k;
  }
  cout << ans << endl;
}
Problem p03733 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using ll = long long;
using namespace std;

const ll MOD = 1e9 + 7;
// const double EPS = 1e(-12);
const double pi = acos(-1);
typedef pair<int, int> P;
#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define EPS 1e-4
#define OUTPUT(i) (cout << (ll)i << endl)
#define ALL(a) ((a).begin(), (a).end())

bool operator<(const pair<ll, ll> &a, const pair<ll, ll> &b) {

  if (a.first == b.first)
    return a.second < b.second;

  return a.first < b.first;
}

ll arr[100010];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(10);

  ll N, T;
  cin >> N >> T;

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

  ll ans = 0;

  for (int i = 0; i < N - 1; ++i) {
    if (arr[i + 1] - arr[i] > T) {
      ans += T;
    } else {
      int idx = 0;
      while (i + idx + 1 < N && arr[i + idx + 1] - arr[i + idx] <= T)
        idx++;
      if (idx)
        idx--;
      ans += arr[idx + i + 1] - arr[i];
      i = i + idx;
    }
  }
  cout << ans + T << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using ll = long long;
using namespace std;

const ll MOD = 1e9 + 7;
// const double EPS = 1e(-12);
const double pi = acos(-1);
typedef pair<int, int> P;
#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define EPS 1e-4
#define OUTPUT(i) (cout << (ll)i << endl)
#define ALL(a) ((a).begin(), (a).end())

bool operator<(const pair<ll, ll> &a, const pair<ll, ll> &b) {

  if (a.first == b.first)
    return a.second < b.second;

  return a.first < b.first;
}

ll arr[200010];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(10);

  ll N, T;
  cin >> N >> T;

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

  ll ans = 0;

  for (int i = 0; i < N - 1; ++i) {
    if (arr[i + 1] - arr[i] > T) {
      ans += T;
    } else {
      int idx = 0;
      while (i + idx + 1 < N && arr[i + idx + 1] - arr[i + idx] <= T)
        idx++;
      if (idx)
        idx--;
      ans += arr[idx + i + 1] - arr[i];
      i = i + idx;
    }
  }
  cout << ans + T << endl;

  return 0;
}
Problem p03733 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
#define maxn 20005
int a[maxn];
using namespace std;
int main() {
  int n, t;
  scanf("%d%d", &n, &t);

  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  ll sum = t;
  ll ans = t;

  for (int i = 2; i <= n; i++) {
    if (a[i] <= ans) {
      sum += t - (ans - a[i]);
      ans = a[i] + t;
    }

    else {
      sum += t;
      ans = a[i] + t;
    }
  }
  printf("%lld\n", sum);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
#define maxn 200005
int a[maxn];
using namespace std;
int main() {
  int n, t;
  scanf("%d%d", &n, &t);

  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  ll sum = t;
  ll ans = t;

  for (int i = 2; i <= n; i++) {
    if (a[i] <= ans) {
      sum += t - (ans - a[i]);
      ans = a[i] + t;
    }

    else {
      sum += t;
      ans = a[i] + t;
    }
  }
  printf("%lld\n", sum);
  return 0;
}
Problem p03734 (C++)

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

#define rep(i, n) for (i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using ll = long long;

int main() {
  ll i, j;
  ll n, W;
  cin >> n >> W;
  ll w, v;
  cin >> w >> v;
  vector<vector<ll>> tmpv(4);
  tmpv.at(0).push_back(v);
  for (i = 1; i < n; ++i) {
    ll tw;
    cin >> tw >> v;
    tmpv.at(tw - w).push_back(v);
  }
  for (i = 0; i < 4; ++i) {
    sort(all(tmpv.at(i)), greater<ll>());
  }
  ll k, l;
  ll ans = 0;
  for (i = 0; i <= tmpv.at(0).size(); ++i) {
    rep(j, tmpv.at(1).size() + 1) {
      rep(k, tmpv.at(2).size() + 1) {
        rep(l, tmpv.at(3).size() + 1) {
          ll ti, tj, tk, tl;
          ll sumw = 0;
          ll sumv = 0;
          sumw += i * w + j * (w + 1) + k * (w + 2) + l * (w + 3);
          if (sumw > W)
            continue;
          rep(ti, i) { sumv += tmpv.at(0).at(ti); }
          rep(tj, j) { sumv += tmpv.at(1).at(tj); }
          rep(tk, k) { sumv += tmpv.at(2).at(tk); }
          rep(tl, l) { sumv += tmpv.at(2).at(tl); }
          ans = max(ans, sumv);
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}

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

#define rep(i, n) for (i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using ll = long long;

int main() {
  ll i, j;
  ll n, W;
  cin >> n >> W;
  ll w, v;
  cin >> w >> v;
  vector<vector<ll>> tmpv(4);
  tmpv.at(0).push_back(v);
  for (i = 1; i < n; ++i) {
    ll tw;
    cin >> tw >> v;
    tmpv.at(tw - w).push_back(v);
  }
  for (i = 0; i < 4; ++i) {
    sort(all(tmpv.at(i)), greater<ll>());
  }
  ll k, l;
  ll ans = 0;
  for (i = 0; i <= tmpv.at(0).size(); ++i) {
    rep(j, tmpv.at(1).size() + 1) {
      rep(k, tmpv.at(2).size() + 1) {
        rep(l, tmpv.at(3).size() + 1) {
          ll ti, tj, tk, tl;
          ll sumw = 0;
          ll sumv = 0;
          sumw += i * w + j * (w + 1) + k * (w + 2) + l * (w + 3);
          if (sumw > W)
            continue;
          rep(ti, i) { sumv += tmpv.at(0).at(ti); }
          rep(tj, j) { sumv += tmpv.at(1).at(tj); }
          rep(tk, k) { sumv += tmpv.at(2).at(tk); }
          rep(tl, l) { sumv += tmpv.at(3).at(tl); }
          ans = max(ans, sumv);
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define F(i, x, y) for (int i = (x); i <= (y); ++i)
#define Fr(i, x, y) for (int i = (x); i < (y); ++i)
using namespace std;
typedef long long ll;

const int N = 100 + 5;

// dp[i][j][k]: 前 i 个，pick j, sum = k

int n, W, w[N], v[N];
ll dp[N][N][N * 3];
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> W;
  for (int i = 1; i <= n; ++i)
    cin >> w[i] >> v[i];
  int basis = w[1];
  for (int i = 1; i <= n; ++i)
    w[i] -= basis;
  memset(dp, 0xf3, sizeof(dp));
  dp[0][0][0] = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j <= i; ++j)
      for (int sum = 0; sum <= 3 * i; ++sum) {
        dp[i][j][sum] = max(dp[i - 1][j][sum], dp[i][j][sum - 1]);
        if (sum >= w[i] && j)
          dp[i][j][sum] =
              max(dp[i][j][sum], dp[i - 1][j - 1][sum - w[i]] + v[i]);
      }

  ll res = 0;
  for (int num = 0; num <= n; ++num) {
    if ((ll)num * basis > W)
      break;
    res = max(res, dp[n][num][W - num * basis]);
  }
  cout << res << '\n';
  return 0;
}


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

const int N = 100 + 5;

// dp[i][j][k]: 前 i 个，pick j, sum = k

int n, W, w[N], v[N];
ll dp[N][N][N * 3];
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> W;
  for (int i = 1; i <= n; ++i)
    cin >> w[i] >> v[i];
  int basis = w[1];
  for (int i = 1; i <= n; ++i)
    w[i] -= basis;
  memset(dp, 0xf3, sizeof(dp));
  dp[0][0][0] = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j <= i; ++j)
      for (int sum = 0; sum <= 3 * i; ++sum) {
        dp[i][j][sum] = max(dp[i - 1][j][sum], dp[i][j][sum - 1]);
        if (sum >= w[i] && j)
          dp[i][j][sum] =
              max(dp[i][j][sum], dp[i - 1][j - 1][sum - w[i]] + v[i]);
      }

  ll res = 0;
  for (int num = 0; num <= n; ++num) {
    if ((ll)num * basis > W)
      break;
    res = max(res, dp[n][num][min(W - num * basis, 3 * n)]);
  }
  cout << res << '\n';
  return 0;
}
Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> iint;
typedef pair<ll, ll> llll;
#define ALL(x) (x).begin(), (x).end()
const ll zero = 0;
const ll one = 1;
const ll INF = 9223372036854775807; // 10^18
const int inINF = 2147483647;       // 10^9
const ll MOD = 1000000007;          // 10^9+7
const ll MOD2 = 998244353;
void Yes() { printf("Yes\n"); }
void No() { printf("No\n"); }
void YES() { printf("YES\n"); }
void NO() { printf("NO\n"); }

int main() {
  ll N, W;
  cin >> N >> W;
  vector<ll> w(N), v(N);
  for (ll i = 0; i < N; i++) {
    cin >> w[i] >> v[i];
  }
  ll ww = w[0];
  for (ll i = 0; i < N; i++) {
    w[i] = w[i] - ww;
  }

  vector<vector<ll>> dp(N + 1, vector<ll>(3 * N + 1, 0));
  for (ll j = 0; j < N; j++) {
    for (ll k = N - 1; k >= 0; k--) {
      for (ll i = 3 * N; i >= 0; i--) {
        dp[k + 1][i + w[j]] = max(dp[k + 1][i + w[j]], dp[k][i] + v[j]);
      }
    }
  }

  ll ans = 0;
  for (ll i = 0; i < 3 * N + 1; i++) {
    for (ll j = 0; j < N + 1; j++) {
      if (i + j * ww > W)
        continue;
      ans = max(ans, dp[j][i]);
    }
  }

  printf("%lld\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> iint;
typedef pair<ll, ll> llll;
#define ALL(x) (x).begin(), (x).end()
const ll zero = 0;
const ll one = 1;
const ll INF = 9223372036854775807; // 10^18
const int inINF = 2147483647;       // 10^9
const ll MOD = 1000000007;          // 10^9+7
const ll MOD2 = 998244353;
void Yes() { printf("Yes\n"); }
void No() { printf("No\n"); }
void YES() { printf("YES\n"); }
void NO() { printf("NO\n"); }

int main() {
  ll N, W;
  cin >> N >> W;
  vector<ll> w(N), v(N);
  for (ll i = 0; i < N; i++) {
    cin >> w[i] >> v[i];
  }
  ll ww = w[0];
  for (ll i = 0; i < N; i++) {
    w[i] = w[i] - ww;
  }

  vector<vector<ll>> dp(N + 1, vector<ll>(3 * N + 1, 0));
  for (ll j = 0; j < N; j++) {
    for (ll k = N - 1; k >= 0; k--) {
      for (ll i = 3 * N; i >= 0; i--) {
        if (i + w[j] > 3 * N)
          continue;
        dp[k + 1][i + w[j]] = max(dp[k + 1][i + w[j]], dp[k][i] + v[j]);
      }
    }
  }

  ll ans = 0;
  for (ll i = 0; i < 3 * N + 1; i++) {
    for (ll j = 0; j < N + 1; j++) {
      if (i + j * ww > W)
        continue;
      ans = max(ans, dp[j][i]);
    }
  }

  printf("%lld\n", ans);
}
Problem p03734 (C++)

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

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

//==================================

ll dp[102][102][302];

int main() {
#ifdef FASTIO
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
  ifstream ifs("./in_out/input.txt");
  cin.rdbuf(ifs.rdbuf());
#endif

  ll N, W;
  cin >> N >> W;
  vector<Pll> items(N);
  for (int i = 0; i < N; i++) {
    cin >> items[i].first >> items[i].second;
  }

  ll w0 = items[0].first;
  fill_n(&dp[0][0][0], sizeof(dp) / sizeof(dp[0][0][0]), -L_INF);
  for (int w = 0; w <= 300; w++) {
    dp[0][0][w] = 0;
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= N; j++) {
      for (int w = 0; w <= 300; w++) {
        ll wi, vi;
        tie(wi, vi) = items[i];
        wi -= w0;
        if (w - wi >= 0 && j - 1 >= 0) {
          dp[i + 1][j][w] = max(dp[i][j][w], dp[i][j - 1][w - wi] + vi);
        } else {
          dp[i + 1][j][w] = dp[i][j][w];
        }
      }
    }
  }

  ll ans = 0;
  for (int i = 0; i <= N; i++) {
    ll r = W - w0 * i;
    if (r >= 0)
      ans = max(ans, dp[N][i][r]);
  }

  cout << ans << endl;

  return 0;
}

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

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

//==================================

ll dp[102][102][302];

int main() {
#ifdef FASTIO
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
  ifstream ifs("./in_out/input.txt");
  cin.rdbuf(ifs.rdbuf());
#endif

  ll N, W;
  cin >> N >> W;
  vector<Pll> items(N);
  for (int i = 0; i < N; i++) {
    cin >> items[i].first >> items[i].second;
  }

  ll w0 = items[0].first;
  fill_n(&dp[0][0][0], sizeof(dp) / sizeof(dp[0][0][0]), -L_INF);
  for (int w = 0; w <= 300; w++) {
    dp[0][0][w] = 0;
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= N; j++) {
      for (int w = 0; w <= 300; w++) {
        ll wi, vi;
        tie(wi, vi) = items[i];
        wi -= w0;
        if (w - wi >= 0 && j - 1 >= 0) {
          dp[i + 1][j][w] = max(dp[i][j][w], dp[i][j - 1][w - wi] + vi);
        } else {
          dp[i + 1][j][w] = dp[i][j][w];
        }
      }
    }
  }

  ll ans = 0;
  for (int i = 0; i <= N; i++) {
    ll r = min(W - w0 * i, 300LL);
    if (r >= 0)
      ans = max(ans, dp[N][i][r]);
  }

  cout << ans << endl;

  return 0;
}
Problem p03734 (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 pp pair<int, int>
#define ll long long
int inf = 100000000;
ll INF = 5000000000000000000;
ll MOD = 1000000007;

int main() {
  int n;
  ll w;
  cin >> n >> w;
  vector<ll> a(n), b(n);
  cin >> a.at(0) >> b.at(0);
  ll ww = a.at(0);
  int k = 0;
  for (int i = 1; i < n; i++) {
    cin >> a.at(i) >> b.at(i);
    a.at(i) = a.at(i) - a.at(0);
    k += a.at(i);
  }
  a.at(0) = 0;
  vector<vector<ll>> d(301, vector<ll>(101));

  d.at(0).at(0) = 0;
  ll o = w / ww;
  ll h = 0;
  rep(i, 301) { rep(j, 101) d.at(i).at(j) = -1; }
  d.at(0).at(0) = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 300; j > -1; j--) {
      for (int l = 100; l > -1; l--) {
        if (d.at(j).at(l) != -1 &&
            d.at(j + a.at(i)).at(l + 1) < d.at(j).at(l) + b.at(i))
          d.at(j + a.at(i)).at(l + 1) = d.at(j).at(l) + b.at(i);
      }
    }
  }
  // cout << d.at(0).at(0) << " " << d.at(0).at(1) << endl;
  for (int j = 0; j <= 301; j++) {
    for (int l = 0; l < 101; l++) {
      if (l * ww + j > w)
        continue;
      h = max(h, d.at(j).at(l));
    }
  }
  cout << h << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define pp pair<int, int>
#define ll long long
int inf = 100000000;
ll INF = 5000000000000000000;
ll MOD = 1000000007;

int main() {
  int n;
  ll w;
  cin >> n >> w;
  vector<ll> a(n), b(n);
  cin >> a.at(0) >> b.at(0);
  ll ww = a.at(0);
  int k = 0;
  for (int i = 1; i < n; i++) {
    cin >> a.at(i) >> b.at(i);
    a.at(i) = a.at(i) - a.at(0);
    k += a.at(i);
  }
  a.at(0) = 0;
  vector<vector<ll>> d(301, vector<ll>(101));

  d.at(0).at(0) = 0;
  ll o = w / ww;
  ll h = 0;
  rep(i, 301) { rep(j, 101) d.at(i).at(j) = -1; }
  d.at(0).at(0) = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 300; j > -1; j--) {
      for (int l = 100; l > -1; l--) {
        if (d.at(j).at(l) != -1 &&
            d.at(j + a.at(i)).at(l + 1) < d.at(j).at(l) + b.at(i))
          d.at(j + a.at(i)).at(l + 1) = d.at(j).at(l) + b.at(i);
      }
    }
  }
  // cout << d.at(0).at(0) << " " << d.at(0).at(1) << endl;
  for (int j = 0; j < 301; j++) {
    for (int l = 0; l < 101; l++) {
      if (l * ww + j > w)
        continue;
      h = max(h, d.at(j).at(l));
    }
  }
  cout << h << endl;
}
Problem p03734 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>

using namespace std;

const long long INF = (long long)1e10;
int n;
const int N = 110;
long long w[N], v[N];
map<long long, long long> dp[N];

long long f(int pos, long long r) {
  if (r < 0)
    return -INF;
  if (pos == n)
    return 0;

  if (dp[pos].count(r - w[0] != -1))
    return dp[pos][r - w[0]];

  long long ans = 0;
  ans = max(f(pos + 1, r), v[pos] + f(pos + 1, r - w[pos]));
  return dp[pos][r - w[0]] = ans;
}

int main(void) {

  long long W;

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

  // memset(dp, -1, sizeof(dp));
  cout << f(0, W) << endl;

  return 0;
}

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const long long INF = (long long)1e10;
int n;
const int N = 110;
long long w[N], v[N];
map<long long, long long> dp[N];

long long f(int pos, long long r) {
  if (r < 0)
    return -INF;
  if (pos == n)
    return 0;

  if (dp[pos].count(r - w[0]))
    return dp[pos][r - w[0]];

  long long ans = 0;
  ans = max(f(pos + 1, r), v[pos] + f(pos + 1, r - w[pos]));
  return dp[pos][r - w[0]] = ans;
}

int main(void) {

  long long W;

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

  // memset(dp, -1, sizeof(dp));
  cout << f(0, W) << endl;

  return 0;
}
Problem p03734 (C++)

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

typedef long long ll;

int main() {
  ll N, W;
  cin >> N >> W;
  ll W0;

  vector<ll> v(N);
  vector<ll> w(N);

  for (int i = 0; i < N; i++) {
    cin >> w[i] >> v[i];
    if (i == 0) {
      W0 = w[0];
      w[0] = 0;
    } else {
      w[i] -= W0;
    }
  }

  ll dp[N][3 * N + 1][N + 1];
  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j <= 3 * N; j++) {
      for (ll k = 0; k <= N; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  // dp[i][j][k] : iまで、補正済み重さj、個数kで最大価値

  // v0を使う場合
  dp[0][0][1] = (W0 <= W ? v[0] : 0);

  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j <= 3 * N; j++) {
      for (ll k = 0; k <= N; k++) {
        // i+1を使う
        if (i + 1 < N && j + w[i + 1] + W0 * (k + 1) <= W && k + 1 <= N) {
          dp[i + 1][j + w[i + 1]][k + 1] =
              max(dp[i + 1][j + w[i + 1]][k + 1], dp[i][j][k] + v[i + 1]);
        }
        // i+1を使わない
        if (i + 1 < N) {
          dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
        }
      }
    }
  }

  ll ans = 0;
  for (int j = 0; j <= 3 * N; j++) {
    for (int k = 0; k <= N; k++) {
      ans = max(ans, dp[N - 1][j][k]);
    }
  }
  cout << ans << endl;
}


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

typedef long long ll;

int main() {
  ll N, W;
  cin >> N >> W;
  ll W0;

  vector<ll> v(N);
  vector<ll> w(N);

  for (int i = 0; i < N; i++) {
    cin >> w[i] >> v[i];
    if (i == 0) {
      W0 = w[0];
      w[0] = 0;
    } else {
      w[i] -= W0;
    }
  }

  ll dp[N][3 * N + 1][N + 1];
  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j <= 3 * N; j++) {
      for (ll k = 0; k <= N; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  // dp[i][j][k] : iまで、補正済み重さj、個数kで最大価値

  // v0を使う場合
  dp[0][0][1] = (W0 <= W ? v[0] : 0);

  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j <= 3 * N; j++) {
      for (ll k = 0; k <= N; k++) {
        // i+1を使う
        if (i + 1 < N && j + w[i + 1] <= 3 * N &&
            j + w[i + 1] + W0 * (k + 1) <= W && k + 1 <= N) {
          dp[i + 1][j + w[i + 1]][k + 1] =
              max(dp[i + 1][j + w[i + 1]][k + 1], dp[i][j][k] + v[i + 1]);
        }
        // i+1を使わない
        if (i + 1 < N) {
          dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
        }
      }
    }
  }

  ll ans = 0;
  for (int j = 0; j <= 3 * N; j++) {
    for (int k = 0; k <= N; k++) {
      ans = max(ans, dp[N - 1][j][k]);
    }
  }
  cout << ans << endl;
}
Problem p03734 (C++)

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

using namespace std;

void chmax(Int &a, Int b) {
  if (a < b) {
    a = b;
  }
}

int main() {
  Int N, W;
  cin >> N >> W;
  vector<Int> w(N), v(N);
  for (int i = 0; i < N; ++i) {
    cin >> w[i] >> v[i];
  }

  vector<vector<Int>> dp(N + 1, vector<Int>(3 * N + 1, -1));
  dp[0][0] = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = N; j > 0; --j) {
      for (int k = 3 * N; k >= 0; --k) {
        int kx = k - (w[i] - w[0]);
        if (kx < 0 || dp[j - 1][kx] < 0) {
          continue;
        }
        chmax(dp[j][k], dp[j - 1][kx] + v[i]);
      }
    }
  }

  Int ans = 0;
  for (Int j = 0; j <= N; ++j) {
    for (Int k = 0; k + j * w[0] <= W; ++k) {
      chmax(ans, dp[j][k]);
    }
  }
  cout << ans << endl;

  return 0;
}


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

using namespace std;

void chmax(Int &a, Int b) {
  if (a < b) {
    a = b;
  }
}

int main() {
  Int N, W;
  cin >> N >> W;
  vector<Int> w(N), v(N);
  for (int i = 0; i < N; ++i) {
    cin >> w[i] >> v[i];
  }

  vector<vector<Int>> dp(N + 1, vector<Int>(3 * N + 1, -1));
  dp[0][0] = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = N; j > 0; --j) {
      for (int k = 3 * N; k >= 0; --k) {
        int kx = k - (w[i] - w[0]);
        if (kx < 0 || dp[j - 1][kx] < 0) {
          continue;
        }
        chmax(dp[j][k], dp[j - 1][kx] + v[i]);
      }
    }
  }

  Int ans = 0;
  for (Int j = 1; j <= N; ++j) {
    for (Int k = 0; k <= 3 * N; ++k) {
      if (k + j * w[0] > W) {
        break;
      }
      chmax(ans, dp[j][k]);
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03734 (C++)

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

long long n, w;

int values[110], weights[110];

int sDP[110][10000][100];

int saved;

int dp(short index, int weight, int cnt) {
  int &ret = sDP[index][weight][cnt];
  if (ret != -1)
    return ret;

  if (index == n - 1) {
    weight += weights[index];
    cnt++;
    if (weight + cnt * saved > w) {
      return ret = 0;
    } else {
      return ret = values[index];
    }
  }

  ret = dp(index + 1, weight, cnt);
  if (weight + weights[index] + (cnt + 1) * saved <= w) {
    ret = max(ret,
              dp(index + 1, weight + weights[index], cnt + 1) + values[index]);
  }

  return ret;
}

int main() {
  memset(sDP, -1, sizeof(sDP));

  cin >> n >> w;
  cin >> weights[0] >> values[0];
  saved = weights[0];
  weights[0] = 0;
  for (int i = 1; i < n; i++) {
    cin >> weights[i] >> values[i];
    weights[i] -= saved;
  }
  cout << dp(0, 0, 0);
}

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

long long n, w;

int values[110], weights[110];

int sDP[101][100][101];

int saved;

int dp(short index, int weight, int cnt) {
  int &ret = sDP[index][weight][cnt];
  if (ret != -1)
    return ret;

  if (index == n - 1) {
    weight += weights[index];
    cnt++;
    if (weight + cnt * saved > w) {
      return ret = 0;
    } else {
      return ret = values[index];
    }
  }

  ret = dp(index + 1, weight, cnt);
  if (weight + weights[index] + (cnt + 1) * saved <= w) {
    ret = max(ret,
              dp(index + 1, weight + weights[index], cnt + 1) + values[index]);
  }

  return ret;
}

int main() {
  memset(sDP, -1, sizeof(sDP));

  cin >> n >> w;
  cin >> weights[0] >> values[0];
  saved = weights[0];
  weights[0] = 0;
  for (int i = 1; i < n; i++) {
    cin >> weights[i] >> values[i];
    weights[i] -= saved;
  }
  cout << dp(0, 0, 0);
}
Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define all(a) (a).begin(), (a).end()

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;

ll N, W;
ll w[110], v[110];
ll dp[110][110][330];
ll ans, sum;

int main() {
  std::cin >> N >> W;

  rep(i, N) std::cin >> w[i] >> v[i], sum += w[i] - w[0];

  rep(i, N) rep(j, i + 1) rep(k, std::min(W - j * w[0], sum) + 1) {
    chmax(dp[i + 1][j][k], dp[i][j][k]);

    if (k + w[i] - w[0] + w[0] * (j + 1) <= W)
      chmax(dp[i + 1][j + 1][k + w[i] - w[0]], dp[i][j][k] + v[i]);
  }

  rep(j, N + 1) rep(k, W + 1) chmax(ans, dp[N][j][k]);

  std::cout << ans << std::endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define all(a) (a).begin(), (a).end()

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;

ll N, W;
ll w[110], v[110];
ll dp[110][110][330];
ll ans, sum;

int main() {
  std::cin >> N >> W;

  rep(i, N) std::cin >> w[i] >> v[i], sum += w[i] - w[0];

  rep(i, N) rep(j, i + 1) rep(k, std::min(W - j * w[0], sum) + 1) {
    chmax(dp[i + 1][j][k], dp[i][j][k]);

    if (k + w[i] - w[0] + w[0] * (j + 1) <= W)
      chmax(dp[i + 1][j + 1][k + w[i] - w[0]], dp[i][j][k] + v[i]);
  }

  rep(j, N + 1) rep(k, std::min(W - j * w[0], sum) + 1) chmax(ans, dp[N][j][k]);

  std::cout << ans << std::endl;

  return 0;
}
Problem p03734 (C++)

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

using namespace std;

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

#define pb push_back
#define mp make_pair

int n, w;
int weight[100], value[100];
vector<pii> a;
map<int, int> d[100];
int step = 1000000005;
int ans = 0;

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> w;
  for (int i = 0; i < n; i++) {
    cin >> weight[i] >> value[i];
    step = min(weight[i], step);
  }
  int sumW = 0, sumV = 0;
  for (int i = 0; i < n; i++) {
    sumW += weight[i];
    sumV += value[i];
  }
  if (sumW <= w) {
    cout << sumV << endl;
    exit(0);
  }
  a.pb(mp(0, 0));
  for (int i = 0; i < n; i++) {
    if (weight[i] > w)
      continue;
    a.pb(mp(weight[i], value[i]));
  }
  if (a.size() == 1) {
    cout << 0 << endl;
    exit(0);
  }
  for (int i = 1; i < a.size(); i++) {
    for (int u = 0; u <= w; u += step) {
      for (int j = u; j < u + 4 * i && j <= w; j++) {
        d[i][j] = d[i - 1][j];
        if (j >= a[i].first &&
            (d[i - 1][j - a[i].first] + a[i].second > d[i][j])) {
          d[i][j] = d[i - 1][j - a[i].first] + a[i].second;
          ans = max(ans, d[i][j]);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}


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

using namespace std;

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

#define pb push_back
#define mp make_pair

int n, w;
int weight[100], value[100];
vector<pii> a;
map<int, int> d[110];
int step = 1000000005;
int ans = 0;

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> w;
  for (int i = 0; i < n; i++) {
    cin >> weight[i] >> value[i];
    step = min(weight[i], step);
  }
  int sumW = 0, sumV = 0;
  for (int i = 0; i < n; i++) {
    sumW += weight[i];
    sumV += value[i];
  }
  if (sumW <= w) {
    cout << sumV << endl;
    exit(0);
  }
  a.pb(mp(0, 0));
  for (int i = 0; i < n; i++) {
    if (weight[i] > w)
      continue;
    a.pb(mp(weight[i], value[i]));
  }
  if (a.size() == 1) {
    cout << 0 << endl;
    exit(0);
  }
  for (int i = 1; i < a.size(); i++) {
    for (int u = 0; u <= w; u += step) {
      for (int j = u; j < u + 4 * i && j <= w; j++) {
        d[i][j] = d[i - 1][j];
        if (j >= a[i].first &&
            (d[i - 1][j - a[i].first] + a[i].second > d[i][j])) {
          d[i][j] = d[i - 1][j - a[i].first] + a[i].second;
          ans = max(ans, d[i][j]);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
typedef long long lint;
const int N = 105;
int n, m, w1, dp[N][N * 3];

namespace utils {
template <class T> inline void apn(T &x, const T y) { x > y ? x = y : 0; }
template <class T> inline void apx(T &x, const T y) { x < y ? x = y : 0; }
inline int nxi() {
  int x = 0;
  char c;
  while (((c = getchar()) > '9' || c < '0') && c != '-')
    ;
  const bool f = (c == '-') && (c = getchar());
  while (x = x * 10 - 48 + c, (c = getchar()) >= '0' && c <= '9')
    ;
  return f ? -x : x;
}
} // namespace utils
using namespace utils;

int main() {
  n = nxi(), m = nxi();
  for (int i = 1; i <= n; ++i) {
    int w = nxi(), v = nxi();
    if (i == 1)
      w1 = w;
    int dlt = w - w1;
    for (int j = i; j >= 1; --j) {
      for (int k = j * 3; k >= dlt; --k) {
        apx(dp[j][k], dp[j - 1][k - dlt] + v);
      }
    }
  }
  int ans = 0;
  for (int i = 1; m >= w1 * i && i <= n; ++i) {
    int lim = m - w1 * i;
    apx(ans, dp[i][lim]);
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
typedef long long lint;
const int N = 105;
int n, m, w1, dp[N][N * 3];

namespace utils {
template <class T> inline void apn(T &x, const T y) { x > y ? x = y : 0; }
template <class T> inline void apx(T &x, const T y) { x < y ? x = y : 0; }
inline int nxi() {
  int x = 0;
  char c;
  while (((c = getchar()) > '9' || c < '0') && c != '-')
    ;
  const bool f = (c == '-') && (c = getchar());
  while (x = x * 10 - 48 + c, (c = getchar()) >= '0' && c <= '9')
    ;
  return f ? -x : x;
}
} // namespace utils
using namespace utils;

int main() {
  n = nxi(), m = nxi();
  for (int i = 1; i <= n; ++i) {
    int w = nxi(), v = nxi();
    if (i == 1)
      w1 = w;
    int dlt = w - w1;
    for (int j = i; j >= 1; --j) {
      for (int k = j * 3; k >= dlt; --k) {
        apx(dp[j][k], dp[j - 1][k - dlt] + v);
      }
    }
  }
  int ans = 0;
  for (int i = 1; m >= w1 * i && i <= n; ++i) {
    for (int j = std::min(i * 3, m - w1 * i); ~j; --j) {
      apx(ans, dp[i][j]);
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

int f[120][320], u[120], v[120];
int i, j, k, l, m, n, r, s, t, w;

int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++)
    scanf("%d%d", &u[i], &v[i]);
  t = u[1];
  for (i = 1; i <= n; i++)
    u[i] = u[i] - t, s = s + u[i];
  for (i = 0; i <= n; i++)
    if (m >= i * t) {
      w = min(m - i * t, s);
      memset(f, 0, sizeof(f));
      for (j = 1; j <= n; j++)
        for (l = w; l >= u[j]; l--)
          for (k = i; k > 0; k--)
            f[k][l] = max(f[k][l], f[k - 1][l - u[j]] + v[j]);
      r = max(r, f[i][w]);
    }
  printf("%d", r);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

int f[120][320], u[120], v[120];
int i, j, k, l, m, n, r, s, t, w;

int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++)
    scanf("%d%d", &u[i], &v[i]);
  t = u[1];
  for (i = 1; i <= n; i++)
    u[i] = u[i] - t, s = s + u[i];
  for (i = 0; (i <= n) && (m >= i * t); i++) {
    w = min(m - i * t, s);
    memset(f, 0, sizeof(f));
    for (j = 1; j <= n; j++)
      for (l = w; l >= u[j]; l--)
        for (k = i; k > 0; k--)
          f[k][l] = max(f[k][l], f[k - 1][l - u[j]] + v[j]);
    r = max(r, f[i][w]);
  }
  printf("%d", r);
  return 0;
}
Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 106
#define mo 1000000007
using namespace std;
int n, m, t, ans, v[N], w[N], g[N][4 * N], f[N][N][4 * N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &w[i], &v[i]);
  t = w[1] - 1;
  for (int i = 1; i <= n; i++)
    w[i] -= t;
  for (int k = 1; k <= n; k++) {
    for (int i = k; i <= n; i++)
      for (int j = w[i]; j <= (int)min(1LL * m - 1LL * t * k, 4LL * n); j++)
        f[k][i][j] = max(f[k][i][j], g[i - 1][j - w[i]] + v[i]),
        ans = max(ans, f[k][i][j]);
    memset(g, 0, sizeof g);
    for (int i = k; i <= n; i++)
      for (int j = 0; j <= (int)min(1LL * m - 1LL * t * k, 4LL * n); j++)
        g[i][j] = max(f[k][i][j], g[i - 1][j]);
  }
  printf("%d\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define N 106
#define mo 1000000007
using namespace std;
int n, m, t, ans, v[N], w[N], g[N][4 * N], f[N][N][4 * N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &w[i], &v[i]);
  t = w[1] - 1;
  for (int i = 1; i <= n; i++)
    w[i] -= t;
  for (int k = 1; k <= n; k++) {
    if (1LL * t * k > 1LL * m)
      break;
    for (int i = k; i <= n; i++)
      for (int j = w[i]; j <= (int)min(1LL * m - 1LL * t * k, 4LL * n); j++)
        f[k][i][j] = max(f[k][i][j], g[i - 1][j - w[i]] + v[i]),
        ans = max(ans, f[k][i][j]);
    memset(g, 0, sizeof g);
    for (int i = k; i <= n; i++)
      for (int j = 0; j <= (int)min(1LL * m - 1LL * t * k, 4LL * n); j++)
        g[i][j] = max(f[k][i][j], g[i - 1][j]);
  }
  printf("%d\n", ans);
}
Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const ll inf = 1e14;
ll dp[105][105][105] = {0};
int main() {
  ll n, w;
  cin >> n >> w;
  ll w1, v1;
  cin >> w1 >> v1;
  for (int i = 0; i < 105; i++) {
    for (int j = 0; j < 105; j++) {
      for (int k = 0; k < 105; k++)
        dp[i][j][k] = 0;
    }
  }
  dp[0][0][0] = 0;
  dp[1][0][0] = 0;
  dp[1][1][0] = v1;
  for (ll i = 1; i < n; i++) {
    for (ll j = 0; j < 105; j++) {
      for (ll k = 0; k < 105; k++)
        dp[i + 1][j][k] = dp[i][j][k];
    }
    ll wn, vn;
    cin >> wn >> vn;
    for (ll j = 0; j < 104; j++) {
      for (ll k = 0; k < 105; k++) {
        if (k + wn - w1 < 105) {
          dp[i + 1][j + 1][k + wn - w1] =
              max(dp[i + 1][j + 1][k + wn - w1], dp[i][j][k] + vn);
        }
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < 105; i++) {
    ll w2 = w - i * w1;
    for (int k = 0; k <= w2; k++)
      ans = max(ans, dp[n][i][k]);
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const ll inf = 1e14;
ll dp[105][105][105] = {0};
int main() {
  ll n, w;
  cin >> n >> w;
  ll w1, v1;
  cin >> w1 >> v1;
  for (int i = 0; i < 105; i++) {
    for (int j = 0; j < 105; j++) {
      for (int k = 0; k < 105; k++)
        dp[i][j][k] = 0;
    }
  }
  dp[0][0][0] = 0;
  dp[1][0][0] = 0;
  dp[1][1][0] = v1;
  for (ll i = 1; i < n; i++) {
    for (ll j = 0; j < 105; j++) {
      for (ll k = 0; k < 105; k++)
        dp[i + 1][j][k] = dp[i][j][k];
    }
    ll wn, vn;
    cin >> wn >> vn;
    for (ll j = 0; j < 104; j++) {
      for (ll k = 0; k < 105; k++) {
        if (k + wn - w1 < 105) {
          dp[i + 1][j + 1][k + wn - w1] =
              max(dp[i + 1][j + 1][k + wn - w1], dp[i][j][k] + vn);
        }
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < 105; i++) {
    ll w2 = w - i * w1;
    if (w2 >= 0) {
      for (int k = 0; k <= min(w2, (ll)104); k++)
        ans = max(ans, dp[n][i][k]);
    }
  }
  cout << ans << endl;
}
Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
using namespace std;
int N, G;
int V[105], W[105];
int w;
int DP[105][105][405];
void Read() {
  cin >> N >> G;
  for (int i = 1; i <= N; i++)
    cin >> W[i] >> V[i];
  w = W[1];
  for (int i = 1; i <= N; i++)
    W[i] -= w - 1;
}

void Solve() {
  memset(DP, -1, sizeof(DP));
  DP[0][0][0] = 0;
  int g = 4 * N;
  for (int i = 1; i <= N; i++) {
    DP[i][0][0] = 0;
    for (int j = 1; j <= N; j++) {
      for (int k = 1; k <= g; k++)
        DP[i][j][k] = DP[i - 1][j][k];
      for (int k = W[i]; k <= g; k++) {
        int val = DP[i - 1][j - 1][k - W[i]];
        if (val == -1)
          continue;
        DP[i][j][k] = max(DP[i][j][k], val + V[i]);
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= N; i++)
    for (int j = 1; j + (w - 1) * i <= G; j++) {
      ans = max(ans, DP[N][i][j]);
    }
  cout << ans << "\n";
}
int main() {
  Read();
  Solve();
  return 0;
}


Fixed submission:
#include <cstring>
#include <iostream>
using namespace std;
int N, G;
int V[105], W[105];
int w;
int DP[105][105][405];
void Read() {
  cin >> N >> G;
  for (int i = 1; i <= N; i++)
    cin >> W[i] >> V[i];
  w = W[1];
  for (int i = 1; i <= N; i++)
    W[i] -= w - 1;
}

void Solve() {
  memset(DP, -1, sizeof(DP));
  DP[0][0][0] = 0;
  int g = 4 * N;
  for (int i = 1; i <= N; i++) {
    DP[i][0][0] = 0;
    for (int j = 1; j <= N; j++) {
      for (int k = 1; k <= g; k++)
        DP[i][j][k] = DP[i - 1][j][k];
      for (int k = W[i]; k <= g; k++) {
        int val = DP[i - 1][j - 1][k - W[i]];
        if (val == -1)
          continue;
        DP[i][j][k] = max(DP[i][j][k], val + V[i]);
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= N; i++)
    for (int j = 1; j + 1LL * (w - 1) * i <= G && j <= g; j++) {
      ans = max(ans, DP[N][i][j]);
    }
  cout << ans << "\n";
}
int main() {
  Read();
  Solve();
  return 0;
}