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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T> void read(T &x) {
  char ch = getchar();
  int f = 1;
  for (x = 0; ch != '-' && (ch < '0' || ch > '9'); ch = getchar())
    ;
  if (ch == '-')
    f = -1, ch = getchar();
  for (; ch >= '0' && ch <= '9'; x = x * 10 + ch - '0', ch = getchar())
    ;
  x *= f;
}
int n, ans;
int v[110], f[110][410];
ll m, w[110], base;
int main() {
  read(n), read(m);
  for (int i = 1; i <= n; i++)
    read(w[i]), read(v[i]);
  base = w[1] - 1;
  for (int i = 1; i <= n; i++)
    w[i] -= base;
  for (int i = 1; i <= n; i++)
    for (int j = i; j >= 1; j--)
      for (int k = min(4LL * n, m - base * j); k >= w[i]; k--)
        f[j][k] = max(f[j][k], f[j - 1][k - w[i]] + v[i]);
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= 4 * n; j++)
      ans = max(ans, f[i][j]);
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T> void read(T &x) {
  char ch = getchar();
  int f = 1;
  for (x = 0; ch != '-' && (ch < '0' || ch > '9'); ch = getchar())
    ;
  if (ch == '-')
    f = -1, ch = getchar();
  for (; ch >= '0' && ch <= '9'; x = x * 10 + ch - '0', ch = getchar())
    ;
  x *= f;
}
int n, ans;
int v[110], f[110][410];
ll m, w[110], base;
int main() {
  read(n), read(m);
  for (int i = 1; i <= n; i++)
    read(w[i]), read(v[i]);
  base = w[1] - 1;
  for (int i = 1; i <= n; i++)
    w[i] -= base;
  for (int i = 1; i <= n; i++)
    for (int j = i; j >= 1; j--)
      for (ll k = min(4LL * n, m - base * j); k >= w[i]; k--)
        f[j][k] = max(f[j][k], f[j - 1][k - w[i]] + v[i]);
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= 4 * n; j++)
      ans = max(ans, f[i][j]);
  printf("%d", ans);
  return 0;
}
Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)

using namespace std;

typedef long long ll;

int N;
ll W, w[200], v[200];
ll base;
ll dp[110][310];

void init() {
  base = w[0];
  rep(i, N) w[i] -= base;
}

void compute() {
  init();
  ll ans = 0;
  REP(use, 1, N + 1) {
    ll new_W = W - use * base;
    if (new_W < 0LL)
      break;
    ll size = min(new_W + 1LL, 3LL * use + 10LL);
    memset(dp, -1, sizeof dp);
    dp[0][0] = 0;
    rep(i, N) {
      for (int j = use - 1; j >= 0; --j) {
        rep(k, size + 1) {
          if (dp[j][k] == -1)
            continue;
          if (k + w[i] <= size) {
            dp[j + 1][k + w[i]] = max(dp[j + 1][k + w[i]], dp[j][k] + v[i]);
          }
        }
      }
    }
    ll maxi = -1;
    rep(i, new_W + 1) maxi = max(maxi, dp[use][i]);
    if (maxi != -1)
      ans = max(ans, maxi);
  }
  cout << ans << endl;
}

int main() {
  cin >> N >> W;
  rep(i, N) cin >> w[i] >> v[i];
  compute();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)

using namespace std;

typedef long long ll;

int N;
ll W, w[200], v[200];
ll base;
ll dp[110][310];

void init() {
  base = w[0];
  rep(i, N) w[i] -= base;
}

void compute() {
  init();
  ll ans = 0;
  REP(use, 1, N + 1) {
    ll new_W = W - use * base;
    if (new_W < 0LL)
      break;
    ll size = min(new_W + 1LL, 3LL * use + 10LL);
    memset(dp, -1, sizeof dp);
    dp[0][0] = 0;
    rep(i, N) {
      for (int j = use - 1; j >= 0; --j) {
        rep(k, size + 1) {
          if (dp[j][k] == -1)
            continue;
          if (k + w[i] <= size) {
            dp[j + 1][k + w[i]] = max(dp[j + 1][k + w[i]], dp[j][k] + v[i]);
          }
        }
      }
    }
    ll maxi = -1;
    rep(i, size) maxi = max(maxi, dp[use][i]);
    if (maxi != -1)
      ans = max(ans, maxi);
  }
  cout << ans << endl;
}

int main() {
  cin >> N >> W;
  rep(i, N) cin >> w[i] >> v[i];
  compute();
  return 0;
}
Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, w;
int arr[100][2];
int dp[100][101][300];
int w1;
int get(int cur, int num, int a) {
  if (cur == n)
    return 0;
  if (dp[cur][num][a] >= 0)
    return dp[cur][num][a];
  int remain = w - w1 * num - a;
  int res = get(cur + 1, num, a);
  if (remain >= arr[cur][0])
    res = max(res, arr[cur][1] + get(cur + 1, num + 1, a + (arr[cur][0] - w1)));
  return dp[cur][num][a] = res;
}
int main(void) {
  cin >> n >> w;
  for (int i = 0; i < n; i++)
    cin >> arr[i][0] >> arr[i][1];
  memset(dp, -1, sizeof(dp));
  cout << get(0, 0, 0);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, w;
int arr[100][2];
int dp[100][101][300];
int w1;
int get(int cur, int num, int a) {
  if (cur == n)
    return 0;
  if (dp[cur][num][a] >= 0)
    return dp[cur][num][a];
  int remain = w - w1 * num - a;
  int res = get(cur + 1, num, a);
  if (remain >= arr[cur][0])
    res = max(res, arr[cur][1] + get(cur + 1, num + 1, a + (arr[cur][0] - w1)));
  return dp[cur][num][a] = res;
}
int main(void) {
  cin >> n >> w;
  for (int i = 0; i < n; i++)
    cin >> arr[i][0] >> arr[i][1];
  memset(dp, -1, sizeof(dp));
  w1 = arr[0][0];
  cout << get(0, 0, 0);
}
Problem p03734 (C++)

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

#define FOR(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define ALL(x) x.begin(), x.end()
template <typename T> bool chmax(T &a, const T &b) {
  return a < b ? (a = b, true) : false;
}
template <typename T> bool chmin(T &a, const T &b) {
  return b < a ? (a = b, true) : false;
}
typedef long long ll;

ll N, W;
ll dp[101][101][301];
const ll INF = 1e18;

int main() {
  scanf("%lld%lld", &N, &W);
  vector<ll> C(N), V(N);
  FOR(i, 0, N) { scanf("%lld%lld", &C[i], &V[i]); }
  ll ans = 0;
  for (int i = 0; i <= N; i++)
    if (i * C.front() <= W) {
      int lim = W - i * C.front();
      fill(dp[0][0], dp[101][0], -INF);
      dp[0][0][0] = 0;
      FOR(j, 0, N) FOR(k, 0, i + 1) FOR(l, 0, lim + 1) if (dp[j][k][l] >= 0) {
        chmax(dp[j + 1][k][l], dp[j][k][l]);
        if (k + 1 <= i && l + C[j] - C.front() <= lim) {
          chmax(dp[j + 1][k + 1][l + C[j] - C.front()], dp[j][k][l] + V[j]);
        }
      }
      chmax(ans, *max_element(dp[N][0], dp[N + 1][0]));
    }

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

  return 0;
}


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

#define FOR(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define ALL(x) x.begin(), x.end()
template <typename T> bool chmax(T &a, const T &b) {
  return a < b ? (a = b, true) : false;
}
template <typename T> bool chmin(T &a, const T &b) {
  return b < a ? (a = b, true) : false;
}
typedef long long ll;

ll N, W;
ll dp[101][101][301];
const ll INF = 1e18;

int main() {
  scanf("%lld%lld", &N, &W);
  vector<ll> C(N), V(N);
  FOR(i, 0, N) { scanf("%lld%lld", &C[i], &V[i]); }
  ll ans = 0;
  for (int i = 0; i <= N; i++)
    if (i * C.front() <= W) {
      ll lim = W - i * C.front();
      chmin(lim, 300ll);
      fill(dp[0][0], dp[101][0], -INF);
      dp[0][0][0] = 0;
      FOR(j, 0, N) FOR(k, 0, i + 1) FOR(l, 0, lim + 1) if (dp[j][k][l] >= 0) {
        chmax(dp[j + 1][k][l], dp[j][k][l]);
        if (k + 1 <= i && l + C[j] - C.front() <= lim) {
          chmax(dp[j + 1][k + 1][l + C[j] - C.front()], dp[j][k][l] + V[j]);
        }
      }
      chmax(ans, *max_element(dp[N][0], dp[N + 1][0]));
    }

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

  return 0;
}
Problem p03734 (C++)

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

#define int long long

#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int N, W;
vint lis[4];
signed main() {
  cin >> N >> W;

  int w, v;
  cin >> w >> v;
  lis[0].pb(v);
  for (int i = 1; i < N; i++) {
    int x, y;
    cin >> x >> y;
    lis[x - w].pb(y);
  }

  rep(i, 4) sort(all(lis[i]));
  rep(i, 4) reverse(all(lis[i]));
  rep(i, 5) lis[i].insert(lis[i].begin(), 0);
  rep(i, 4) {
    for (int j = 0; j + 1 < lis[i].size(); j++)
      lis[i][j + 1] += lis[i][j];
  }

  int ans = 0;
  for (int i = 0; i < lis[0].size(); i++) {
    for (int j = 0; j < lis[1].size(); j++) {
      for (int k = 0; k < lis[2].size(); k++) {
        for (int l = 0; l < lis[3].size(); l++) {
          if (w * i + (w + 1) * j + (w + 2) * k + (w + 3) * l > W)
            continue;
          chmax(ans, lis[0][i] + lis[1][j] + lis[2][k] + lis[3][l]);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}


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

#define int long long

#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int N, W;
vint lis[4];
signed main() {
  cin >> N >> W;

  int w, v;
  cin >> w >> v;
  lis[0].pb(v);
  for (int i = 1; i < N; i++) {
    int x, y;
    cin >> x >> y;
    lis[x - w].pb(y);
  }

  rep(i, 4) sort(all(lis[i]));
  rep(i, 4) reverse(all(lis[i]));
  rep(i, 4) lis[i].insert(lis[i].begin(), 0);
  rep(i, 4) {
    for (int j = 0; j + 1 < lis[i].size(); j++)
      lis[i][j + 1] += lis[i][j];
  }

  int ans = 0;
  for (int i = 0; i < lis[0].size(); i++) {
    for (int j = 0; j < lis[1].size(); j++) {
      for (int k = 0; k < lis[2].size(); k++) {
        for (int l = 0; l < lis[3].size(); l++) {
          if (w * i + (w + 1) * j + (w + 2) * k + (w + 3) * l > W)
            continue;
          chmax(ans, lis[0][i] + lis[1][j] + lis[2][k] + lis[3][l]);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

int main() {
  ll n, W;
  cin >> n >> W;
  vec w(n), v(n);
  for (ll i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  ll tw = w[0];
  for (ll i = n - 1; i >= 0; i--)
    w[i] -= tw;
  mat dp(n + 1, vec(3000));
  for (ll i = 0; i < n; i++) {
    for (ll j = n - 1; j >= 0; j--) {
      for (ll k = 0; k <= 300; k++) {
        if (k + w[i] <= 300) {
          dp[j + 1][k + w[i]] = max(dp[j + 1][k + w[i]], dp[j][k] + v[i]);
        }
      }
    }
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    if (W - i * tw < 0)
      break;
    ans = max(ans, dp[i][max(300LL, W - i * tw)]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

int main() {
  ll n, W;
  cin >> n >> W;
  vec w(n), v(n);
  for (ll i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  ll tw = w[0];
  for (ll i = n - 1; i >= 0; i--)
    w[i] -= tw;
  mat dp(n + 1, vec(3000));
  for (ll i = 0; i < n; i++) {
    for (ll j = n - 1; j >= 0; j--) {
      for (ll k = 0; k <= 300; k++) {
        if (k + w[i] <= 300) {
          dp[j + 1][k + w[i]] = max(dp[j + 1][k + w[i]], dp[j][k] + v[i]);
        }
      }
    }
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    if (W - i * tw < 0)
      break;
    ans = max(ans, dp[i][min(300LL, W - i * tw)]);
  }
  cout << ans << endl;
}
Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, m, n) for (int i = m; i < n; ++i)
#define rem(i, m, n) for (int i = m; i >= n; --i)
typedef long long ll;
#define mod 1000000007
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int dp[110][404][110];
int main() {
  int N, W;
  cin >> N >> W;
  vector<int> w(N), v(N);
  rep(i, 0, N) cin >> w[i] >> v[i];
  int b = w[0] - 1;
  rep(i, 0, N) w[i] -= b;
  rep(i, 0, N) rep(j, 0, 400) rep(k, 0, N + 1) {
    chmax(dp[i + 1][j][k], dp[i][j][k]);
    if (j + w[i] <= 400)
      chmax(dp[i + 1][j + w[i]][k + 1], dp[i][j][k] + v[i]);
  }
  int ans = 0;
  rep(i, 0, W + 1) rep(j, 0, N + 1) {
    if (i + j * (ll)b > (ll)W)
      continue;
    chmax(ans, dp[N][i][j]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, m, n) for (int i = m; i < n; ++i)
#define rem(i, m, n) for (int i = m; i >= n; --i)
typedef long long ll;
#define mod 1000000007
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int dp[110][404][110];
int main() {
  int N, W;
  cin >> N >> W;
  vector<int> w(N), v(N);
  rep(i, 0, N) cin >> w[i] >> v[i];
  int b = w[0] - 1;
  rep(i, 0, N) w[i] -= b;
  rep(i, 0, N) rep(j, 0, 400) rep(k, 0, N + 1) {
    chmax(dp[i + 1][j][k], dp[i][j][k]);
    if (j + w[i] <= 400)
      chmax(dp[i + 1][j + w[i]][k + 1], dp[i][j][k] + v[i]);
  }
  int ans = 0;
  rep(i, 0, 404) rep(j, 0, N + 1) {
    if (i + (ll)j * b > (ll)W)
      continue;
    chmax(ans, dp[N][i][j]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03734 (C++)

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

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)

using ll = long long;
using P = std::tuple<int, int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1},
          dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

int N, W;
ll w[100], wws[100];
int vs[100];
int dp[101][101][400];

void f() {
  for (int i = 0; i < N; ++i) {
    for (int k = 0; k <= N; ++k) {
      for (int j = 0; j <= 300; ++j) {
        dp[i + 1][k][j] = max(dp[i + 1][k][j], dp[i][k][j]);
        if (j > 0) {
          dp[i + 1][k][j] = max(dp[i + 1][k][j], dp[i + 1][k][j - 1]);
        }

        if (k + 1 <= N && j + wws[i] <= 300) {
          dp[i + 1][k + 1][j + wws[i]] =
              max(dp[i + 1][k + 1][j + wws[i]], dp[i][k][j] + vs[i]);
        }
      }
    }
  }
}

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

  std::cin >> N >> W;

  for (int i = 0; i < N; ++i) {
    std::cin >> w[i] >> vs[i];
    wws[i] = w[i] - w[0];
  }

  f();

  int res = 0;
  for (int i = 1; i <= N; ++i) {
    if (w[0] * i > W) {
      continue;
    }

    int mx = W - w[0] * i;
    res = max(res, dp[N][i][mx]);
  }

  std::cout << res << std::endl;
}


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

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)

using ll = long long;
using P = std::tuple<int, int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1},
          dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

int N, W;
ll w[100], wws[100];
int vs[100];
int dp[101][101][400];

void f() {
  for (int i = 0; i < N; ++i) {
    for (int k = 0; k <= N; ++k) {
      for (int j = 0; j <= 300; ++j) {
        dp[i + 1][k][j] = max(dp[i + 1][k][j], dp[i][k][j]);
        if (j > 0) {
          dp[i + 1][k][j] = max(dp[i + 1][k][j], dp[i + 1][k][j - 1]);
        }

        if (k + 1 <= N && j + wws[i] <= 300) {
          dp[i + 1][k + 1][j + wws[i]] =
              max(dp[i + 1][k + 1][j + wws[i]], dp[i][k][j] + vs[i]);
        }
      }
    }
  }
}

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

  std::cin >> N >> W;

  for (int i = 0; i < N; ++i) {
    std::cin >> w[i] >> vs[i];
    wws[i] = w[i] - w[0];
  }

  f();

  int res = 0;
  for (int i = 1; i <= N; ++i) {
    if (w[0] * i > W) {
      continue;
    }

    int mx = min<int>(W - w[0] * i, 300);
    res = max(res, dp[N][i][mx]);
  }

  std::cout << res << std::endl;
}
Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define int long long

int dp[101][101][401];
int w[200], v[200];
signed main() {
  int n, W;
  scanf("%lld%lld", &n, &W);
  rep(i, n) scanf("%lld%lld", &w[i], &v[i]);
  rep(i, n) rep(j, n) rep(k, 3 * n + 1) {
    dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
    dp[i + 1][j + 1][k + w[i] - w[0]] =
        max(dp[i + 1][j + 1][k + w[i] - w[0]], dp[i][j][k] + v[i]);
  }
  int Max = 0;
  rep(i, n + 1) {
    if (W < w[0] * i)
      continue;
    Max = max(Max, dp[n][i][W - w[0] * i]);
  }
  printf("%lld\n", Max);
}

Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define int long long

int dp[101][101][401];
int w[200], v[200];
signed main() {
  int n, W;
  scanf("%lld%lld", &n, &W);
  rep(i, n) scanf("%lld%lld", &w[i], &v[i]);
  rep(i, n) rep(j, n) rep(k, 3 * n + 1) {
    dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
    dp[i + 1][j + 1][k + w[i] - w[0]] =
        max(dp[i + 1][j + 1][k + w[i] - w[0]], dp[i][j][k] + v[i]);
  }
  int Max = 0;
  rep(i, n + 1) {
    if (W < w[0] * i)
      continue;
    Max = max(Max,
              *max_element(dp[n][i], dp[n][i] + min(401LL, W - w[0] * i + 1)));
  }
  printf("%lld\n", Max);
}
Problem p03734 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
#define ll long long
const int maxn = 1000 + 5;
map<ll, ll> dp;
ll w[maxn], v[maxn];
ll n, W;
int main() {
  cin >> n >> W;
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &w[i], &v[i]);
  }
  if (w[1] <= 1e3) {
    ll ans = min(W, (w[1] + 3) * n);
    for (ll i = 1; i <= n; i++) {
      for (ll j = ans; j >= 1; j--) {
        if (j >= w[i])
          dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
      }
    }
    cout << dp[ans] << endl;
  } else {
    ll ans = min(W, (w[1] + 3) * n);
    for (ll i = 1; i <= n; i++) {
      for (ll j = ans; j >= w[i]; j--) {
        if (j >= w[i])
          dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
      }
    }
    cout << dp[ans] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
#define ll long long
const int maxn = 1000 + 5;
map<ll, ll> dp;
ll w[maxn], v[maxn];
ll n, W;
int main() {
  cin >> n >> W;
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &w[i], &v[i]);
  }
  if (w[1] <= 1e3) {
    ll ans = min(W, (w[1] + 3) * n);
    for (ll i = 1; i <= n; i++) {
      for (ll j = ans; j >= 1; j--) {
        if (j >= w[i])
          dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
      }
    }
    cout << dp[ans] << endl;
  } else {
    ll ans = min(W, (w[1] + 3) * n);
    for (ll i = 1; i <= n; i++) {
      for (ll j = ans; j >= w[1]; j -= w[1]) {
        if (j >= w[i])
          dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
      }
    }
    cout << dp[ans] << endl;
  }
  return 0;
}
Problem p03734 (C++)

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

int main() {
  ll n, nw;
  cin >> n >> nw;
  vector<ll> w(n);
  vector<ll> v(n);
  for (ll i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  ll bw = w[0];
  for (ll i = 0; i < n; i++) {
    w[i] -= bw;
  }
  vector<vector<vector<ll>>> dp(n + 1,
                                vector<vector<ll>>(301, vector<ll>(n + 1)));
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j <= 300; j++) {
      for (ll k = 0; k <= i + 1; k++) {
        dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
        if (j + w[i] <= 300 && k + 1 <= n) {
          dp[i + 1][j + w[i]][k + 1] =
              max(dp[i + 1][j + w[i]][k + 1], dp[i][j][k] + v[i]);
        }
      }
    }
  }
  ll ans = 0;
  for (ll i = 0; i <= min(n, nw / bw); i++) {
    ans = max(ans, dp[n][nw - i * bw][i]);
  }
  cout << ans << endl;
}

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

int main() {
  ll n, nw;
  cin >> n >> nw;
  vector<ll> w(n);
  vector<ll> v(n);
  for (ll i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  ll bw = w[0];
  for (ll i = 0; i < n; i++) {
    w[i] -= bw;
  }
  vector<vector<vector<ll>>> dp(n + 1,
                                vector<vector<ll>>(301, vector<ll>(n + 1)));
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j <= 300; j++) {
      for (ll k = 0; k <= i + 1; k++) {
        dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
        if (j + w[i] <= 300 && k + 1 <= n) {
          dp[i + 1][j + w[i]][k + 1] =
              max(dp[i + 1][j + w[i]][k + 1], dp[i][j][k] + v[i]);
        }
      }
    }
  }
  ll ans = 0;
  for (ll i = 0; i <= min(n, nw / bw); i++) {
    ll temp = 300;
    ans = max(ans, dp[n][min(temp, nw - i * bw)][i]);
  }
  cout << ans << endl;
}
Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;
typedef long long li;
typedef pair<li, li> pi;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main() {
  li n, w;
  cin >> n >> w;

  vector<li> ws(n), vs(n);
  rep(i, n) { cin >> ws[i] >> vs[i]; }

  const li d = 4 * (n + 1);
  li ans = 0;
  vector<vector<vector<li>>> dp(n + 1,
                                vector<vector<li>>(n + 1, vector<li>(d, 0)));
  rep(i, n) {
    rep(j, i + 1) {
      rep(k, d) {
        dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
        ans = max(ans, dp[i + 1][j][k]);
        const int ni = i + 1;
        const int nj = j + 1;
        const int nk = k + ws[i] - ws[0];
        if (nj * ws[0] + nk <= w) {
          dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k] + vs[i]);
          ans = max(dp[ni][nj][nk], ans);
        }
      }
    }
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;
typedef long long li;
typedef pair<li, li> pi;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main() {
  li n, w;
  cin >> n >> w;

  vector<li> ws(n), vs(n);
  rep(i, n) { cin >> ws[i] >> vs[i]; }

  const li d = 4 * (n + 1);
  li ans = 0;
  vector<vector<vector<li>>> dp(n + 1,
                                vector<vector<li>>(n + 1, vector<li>(d, 0)));
  rep(i, n) {
    rep(j, i + 1) {
      rep(k, d) {
        dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
        ans = max(ans, dp[i + 1][j][k]);
        const int ni = i + 1;
        const int nj = j + 1;
        const int nk = k + ws[i] - ws[0];
        if (nk < d && nj * ws[0] + nk <= w) {
          dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k] + vs[i]);
          ans = max(dp[ni][nj][nk], ans);
        }
      }
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
const ll MOD(1e9 + 7);
const ll INF(2e9);
const int MAX_N(200001);

int main(void) {
  int N, W;
  cin >> N >> W;
  int w[100];
  int v[100];
  cin >> w[0] >> v[0];
  for (int i = 1; i < N; i++) {
    cin >> w[i] >> v[i];
    w[i] -= (w[0] - 1);
  }
  ll wmemo = w[0];
  w[0] = 1;
  int **dp = new int *[101];
  for (int i = 0; i < 100; i++) {
    dp[i] = new int[404];
  } // dp[j][k]=:個数、重さ　最大価値

  for (int j = 0; j <= N; j++) {
    for (int k = 0; k <= 4 * N; k++) {
      dp[j][k] = -INF;
    }
  }

  dp[0][0] = 0;

  for (int i = 0; i < N; i++) { // 賞品iについて
    for (int j = N - 1; j >= 0; j--) {
      for (int k = 4 * N - 1; k >= 0; k--) {
        dp[j + 1][k + 1] =
            max((ll)dp[j + 1][k + 1],
                (k + 1 - w[i] >= 0 ? dp[j][k + 1 - w[i]] : -INF) + v[i]);
      }
    }
  }
  // cout<<endl<<endl;
  int ans = 0;
  for (int j = 0; j <= N; j++) {
    for (int k = 0; k <= 4 * N; k++) {
      // cout<<j<<","<<k<<","<<dp[j][k]<<endl;
      if (j * (wmemo - 1) + k <= W) {
        ans = max(ans, dp[j][k]);
        // cout<<ans<<endl;
      }
    }
  }
  // cout<<wmemo<<"a";
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
const ll MOD(1e9 + 7);
const ll INF(2e9);
const int MAX_N(200001);

int main(void) {
  int N, W;
  cin >> N >> W;
  int w[100];
  int v[100];
  cin >> w[0] >> v[0];
  for (int i = 1; i < N; i++) {
    cin >> w[i] >> v[i];
    w[i] -= (w[0] - 1);
  }
  ll wmemo = w[0];
  w[0] = 1;
  int **dp = new int *[101];
  for (int i = 0; i < 101; i++) {
    dp[i] = new int[404];
  } // dp[j][k]=:個数、重さ　最大価値

  for (int j = 0; j <= N; j++) {
    for (int k = 0; k <= 4 * N; k++) {
      dp[j][k] = -INF;
    }
  }

  dp[0][0] = 0;

  for (int i = 0; i < N; i++) { // 賞品iについて
    for (int j = N - 1; j >= 0; j--) {
      for (int k = 4 * N - 1; k >= 0; k--) {
        dp[j + 1][k + 1] =
            max((ll)dp[j + 1][k + 1],
                (k + 1 - w[i] >= 0 ? dp[j][k + 1 - w[i]] : -INF) + v[i]);
      }
    }
  }
  // cout<<endl<<endl;
  int ans = 0;
  for (int j = 0; j <= N; j++) {
    for (int k = 0; k <= 4 * N; k++) {
      // cout<<j<<","<<k<<","<<dp[j][k]<<endl;
      if (j * (wmemo - 1) + k <= W) {
        ans = max(ans, dp[j][k]);
        // cout<<ans<<endl;
      }
    }
  }
  // cout<<wmemo<<"a";
  cout << ans << endl;
  return 0;
}
Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define ll long long

unordered_map<ll, vector<ll>> ww;
ll res = 0;
vector<ll> sum;

void dfs(vector<ll> &ws, int k, ll left_w, ll tot_v) {
  if (k + 1 == ws.size()) {
    if (left_w < 0)
      return;
    int n = (int)min((ll)ww[ws[k]].size(), left_w / ws[k]);
    tot_v += sum[n];
    res = max(res, tot_v);
  } else {
    dfs(ws, k + 1, left_w, tot_v);
    for (int i = 0; i < ww[ws[k]].size(); ++i) {
      left_w -= ws[k];
      if (left_w < 0)
        break;
      tot_v += ww[ws[k]][i];
      dfs(ws, k + 1, left_w, tot_v);
    }
  }
}

int main() {
  int N;
  ll W;
  cin >> N >> W;
  ll w, v;
  for (int i = 0; i < N; ++i) {
    cin >> w >> v;
    ww[w].push_back(v);
  }
  vector<ll> ws;
  for (auto &p : ww) {
    ws.push_back(p.first);
    sort(begin(p.second), end(p.second), greater<ll>());
  }
  int k = ws.size() - 1;
  sum.resize(ww[k].size() + 1, 0);
  for (int i = 1; i <= ww[ws[k]].size(); ++i) {
    sum[i] = sum[i - 1] + ww[ws[k]][i - 1];
  }
  dfs(ws, 0, W, 0);
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define ll long long

unordered_map<ll, vector<ll>> ww;
ll res = 0;
vector<ll> sum;

void dfs(vector<ll> &ws, int k, ll left_w, ll tot_v) {
  if (k + 1 == ws.size()) {
    if (left_w < 0)
      return;
    int n = (int)min((ll)ww[ws[k]].size(), left_w / ws[k]);
    tot_v += sum[n];
    res = max(res, tot_v);
  } else {
    dfs(ws, k + 1, left_w, tot_v);
    for (int i = 0; i < ww[ws[k]].size(); ++i) {
      left_w -= ws[k];
      if (left_w < 0)
        break;
      tot_v += ww[ws[k]][i];
      dfs(ws, k + 1, left_w, tot_v);
    }
  }
}

int main() {
  int N;
  ll W;
  cin >> N >> W;
  ll w, v;
  for (int i = 0; i < N; ++i) {
    cin >> w >> v;
    ww[w].push_back(v);
  }
  vector<ll> ws;
  for (auto &p : ww) {
    ws.push_back(p.first);
    sort(begin(p.second), end(p.second), greater<ll>());
  }
  int k = ws.size() - 1;
  sum.resize(ww[ws[k]].size() + 1, 0);
  for (int i = 1; i <= ww[ws[k]].size(); ++i) {
    sum[i] = sum[i - 1] + ww[ws[k]][i - 1];
  }
  dfs(ws, 0, W, 0);
  cout << res << endl;
  return 0;
}
Problem p03734 (C++)

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

long long v[4][101];

int main() {
  int n;
  long long W;
  long long w1;
  long long count[4] = {1, 0, 0, 0};
  scanf("%d%lld%lld%lld", &n, &W, &w1, &v[0][1]);
  for (int i = 1; i < n; ++i) {
    long long a, b;
    scanf("%lld%lld", &a, &b);
    count[a - w1]++;
    v[a - w1][count[a - w1]] = b;
  }
  for (int i = 0; i < 4; ++i) {
    std::sort(v[i] + 1, v[i] + 1 + count[i], std::greater<long long>());
    for (int j = 1; j <= count[i]; ++j) {
      v[i][j] += v[i][j - 1];
    }
  }

  long long ans = 0;
  for (long long a = 0; a <= count[0]; ++a) {
    for (long long b = 0; b <= count[1]; ++b) {
      for (long long c = 0; c <= count[2]; ++c) {
        if (a * w1 + b * (w1 + 1) + c * (w1 + 2) > W)
          break;
        long long d = W - (a * w1 + b * (w1 + 1) + c * (w1 + 2)) / (w1 + 3);
        ans = std::max(ans, v[0][a] + v[1][b] + v[2][c] + v[3][d]);
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}

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

long long v[4][101];

int main() {
  int n;
  long long W;
  long long w1;
  long long count[4] = {1, 0, 0, 0};
  scanf("%d%lld%lld%lld", &n, &W, &w1, &v[0][1]);
  for (int i = 1; i < n; ++i) {
    long long a, b;
    scanf("%lld%lld", &a, &b);
    count[a - w1]++;
    v[a - w1][count[a - w1]] = b;
  }
  for (int i = 0; i < 4; ++i) {
    std::sort(v[i] + 1, v[i] + 1 + count[i], std::greater<long long>());
    for (int j = 1; j <= count[i]; ++j) {
      v[i][j] += v[i][j - 1];
    }
  }

  long long ans = 0;
  for (long long a = 0; a <= count[0]; ++a) {
    for (long long b = 0; b <= count[1]; ++b) {
      for (long long c = 0; c <= count[2]; ++c) {
        if (a * w1 + b * (w1 + 1) + c * (w1 + 2) > W)
          break;
        long long d = (W - (a * w1 + b * (w1 + 1) + c * (w1 + 2))) / (w1 + 3);
        ans = std::max(ans, v[0][a] + v[1][b] + v[2][c] + v[3][d]);
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)

#define maxn 110
int w[maxn], v[maxn], f[maxn * 10][maxn];
int main() {
  int n, W, w1;
  scanf("%d%d", &n, &W);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &w[i], &v[i]);
    i == 1 ? w1 = w[1] : 0;
    w[i] -= w1;
  }
  int ans = 0;
  for (int i = 0; i <= n; ++i, W -= w1) {
    if (W < 0)
      break;
    memset(f, -63, sizeof(f));
    f[0][0] = 0;
    for (int j = 1; j <= n; ++j)
      for (int jj = i; jj; --jj)
        for (int k = 0; k <= 3 * i; ++k)
          cmax(f[k + w[j]][jj], f[k][jj - 1] + v[j]);
    for (int k = 0; k <= W; ++k) {
      cmax(ans, f[k][i]);
      //			printf("%d %d\n", i, f[k][i]);
    }
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)

#define maxn 110
int w[maxn], v[maxn], f[maxn * 10][maxn];
int main() {
  int n, W, w1;
  scanf("%d%d", &n, &W);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &w[i], &v[i]);
    i == 1 ? w1 = w[1] : 0;
    w[i] -= w1;
  }
  int ans = 0;
  for (int i = 0; i <= n; ++i, W -= w1) {
    if (W < 0)
      break;
    memset(f, -63, sizeof(f));
    f[0][0] = 0;
    for (int j = 1; j <= n; ++j)
      for (int jj = i; jj; --jj)
        for (int k = 0; k <= 3 * i; ++k)
          cmax(f[k + w[j]][jj], f[k][jj - 1] + v[j]);
    for (int k = 0; k <= W && k <= 3 * i; ++k) {
      cmax(ans, f[k][i]);
      //			printf("%d %d\n", i, f[k][i]);
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define int long long
#define double long double
#define mod 1000000007
#define F first
#define S second
#define P pair<long long, long long>
#define all(a) a.begin(), a.end()
#define INF 10000000000000000
#define endl '\n'
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 namespace std;

signed main(void) {
  int n, W;
  cin >> n >> W;

  int first_w, first_v;
  cin >> first_w >> first_v;
  vector<vector<int>> vs(4);
  vs.at(0).push_back(first_v);
  rep(i, n - 1) {
    int w, v;
    cin >> w >> v;
    vs.at(w - first_w).push_back(v);
  }

  rep(i, 4) { sort(all(vs.at(i)), greater<int>()); }

  vector<vector<int>> vsum(4);
  rep(i, 4) {
    int sum = 0;
    vsum.at(i).push_back(0);
    rep(j, vs.at(i).size()) {
      sum += vs.at(i).at(j);
      vsum.at(i).push_back(sum);
    }
  }

  int ans = 0;
  rep(i, vs.at(0).size() + 1) {
    rep(j, vs.at(1).size() + 1) {
      rep(k, vs.at(2).size() + 1) {
        rep(l, vs.at(3).size() + 1) {
          if (first_w * i + (first_w + 1) * j + (first_w + 2) * k +
                  (first_w + 3) * l <=
              W) {
            chmax(ans, vs.at(0).at(i) + vs.at(1).at(j) + vs.at(2).at(k) +
                           vs.at(3).at(l));
          }
        }
      }
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define int long long
#define double long double
#define mod 1000000007
#define F first
#define S second
#define P pair<long long, long long>
#define all(a) a.begin(), a.end()
#define INF 10000000000000000
#define endl '\n'
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 namespace std;

signed main(void) {
  int n, W;
  cin >> n >> W;

  int first_w, first_v;
  cin >> first_w >> first_v;
  vector<vector<int>> vs(4);
  vs.at(0).push_back(first_v);
  rep(i, n - 1) {
    int w, v;
    cin >> w >> v;
    vs.at(w - first_w).push_back(v);
  }

  rep(i, 4) { sort(all(vs.at(i)), greater<int>()); }

  vector<vector<int>> vsum(4);
  rep(i, 4) {
    int sum = 0;
    vsum.at(i).push_back(0);
    rep(j, vs.at(i).size()) {
      sum += vs.at(i).at(j);
      vsum.at(i).push_back(sum);
    }
  }

  int ans = 0;
  rep(i, vs.at(0).size() + 1) {
    rep(j, vs.at(1).size() + 1) {
      rep(k, vs.at(2).size() + 1) {
        rep(l, vs.at(3).size() + 1) {
          if (first_w * i + (first_w + 1) * j + (first_w + 2) * k +
                  (first_w + 3) * l <=
              W) {
            chmax(ans, vsum.at(0).at(i) + vsum.at(1).at(j) + vsum.at(2).at(k) +
                           vsum.at(3).at(l));
          }
        }
      }
    }
  }

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

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

#define pb push_back
#define mp make_pair
#define taskname "A"

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
const int maxn = 1e5 + 5;
ii a[maxn];
int n;
multiset<int> s, b;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (fopen(taskname ".INP", "r")) {
    freopen(taskname ".INP", "r", stdin);
    freopen(taskname ".OUT", "w", stdout);
  }
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].first >> a[i].second;
    if (a[i].first > a[i].second)
      swap(a[i].first, a[i].second);
    s.insert(a[i].first);
    b.insert(a[i].second);
  }
  sort(a + 1, a + n + 1);
  ll res = 1e18;
  for (int i = 1; i <= n; ++i) {
    res = min(res, (ll)(*s.rbegin() - *s.begin()) * (*b.rbegin() - *b.begin()));
    s.erase(s.find(a[i].first));
    s.insert(a[i].second);
    b.erase(b.find(a[i].second));
    b.insert(a[i].first);
  }
  cout << res;
}


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

#define pb push_back
#define mp make_pair
#define taskname "A"

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
const int maxn = 2e5 + 5;
ii a[maxn];
int n;
multiset<int> s, b;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (fopen(taskname ".INP", "r")) {
    freopen(taskname ".INP", "r", stdin);
    freopen(taskname ".OUT", "w", stdout);
  }
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].first >> a[i].second;
    if (a[i].first > a[i].second)
      swap(a[i].first, a[i].second);
    s.insert(a[i].first);
    b.insert(a[i].second);
  }
  sort(a + 1, a + n + 1);
  ll res = 1e18;
  for (int i = 1; i <= n; ++i) {
    res = min(res, (ll)(*s.rbegin() - *s.begin()) * (*b.rbegin() - *b.begin()));
    s.erase(s.find(a[i].first));
    s.insert(a[i].second);
    b.erase(b.find(a[i].second));
    b.insert(a[i].first);
  }
  cout << res;
}
Problem p03735 (C++)

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

typedef long long LL;
struct node {
  int x, y;
} a[200010];
int n;
LL ans;
set<int> S1, S2;

bool cmp(node x, node y) { return x.x < y.x; }

void work() {
  scanf("%d", &n);
  if (n == 1) {
    puts("0");
    return;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &a[i].x, &a[i].y);
    if (a[i].x > a[i].y)
      swap(a[i].x, a[i].y);
    S1.insert(a[i].x), S2.insert(a[i].y);
  }
  ans = 1LL * (*S1.rbegin() - *S1.begin()) * (*S2.rbegin() - *S2.begin());
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    S1.erase(S1.find(a[i].x)), S1.insert(a[i].y);
    S2.erase(S2.find(a[i].y)), S2.insert(a[i].x);
    ans = min(ans, 1LL * (*S1.rbegin() - *S1.begin()) *
                       (*S2.rbegin() - *S2.begin()));
  }
  printf("%lld", ans);
}

int main() {
  work();
  return 0;
}


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

typedef long long LL;
struct node {
  int x, y;
} a[200010];
int n;
LL ans;
multiset<int> S1, S2;

bool cmp(node x, node y) { return x.x < y.x; }

void work() {
  scanf("%d", &n);
  if (n == 1) {
    puts("0");
    return;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &a[i].x, &a[i].y);
    if (a[i].x > a[i].y)
      swap(a[i].x, a[i].y);
    S1.insert(a[i].x), S2.insert(a[i].y);
  }
  ans = 1LL * (*S1.rbegin() - *S1.begin()) * (*S2.rbegin() - *S2.begin());
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    S1.erase(S1.find(a[i].x)), S1.insert(a[i].y);
    S2.erase(S2.find(a[i].y)), S2.insert(a[i].x);
    ans = min(ans, 1LL * (*S1.rbegin() - *S1.begin()) *
                       (*S2.rbegin() - *S2.begin()));
  }
  printf("%lld", ans);
}

int main() {
  work();
  return 0;
}
Problem p03735 (C++)

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

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

vector<pll> V;
ll S[101010];
ll n, l1, l2, r1, r2, cnt, ans;

int main() {
  ll i, j, a, b;

  scanf("%lld", &n);

  l1 = l2 = 1e9;
  r1 = r2 = 0;

  for (i = 1; i <= n; i++) {
    scanf("%lld%lld", &a, &b);
    V.emplace_back(a, i);
    V.emplace_back(b, i);
    l1 = min(l1, min(a, b));
    l2 = min(l2, max(a, b));
    r1 = max(r1, min(a, b));
    r2 = max(r2, max(a, b));
  }

  ans = (r1 - l1) * (r2 - l2);

  sort(V.begin(), V.end());

  for (i = 0, j = 0; i < n + n; i++) {
    for (; j < n + n && cnt < n; j++) {
      S[V[j].second]++;
      if (S[V[j].second] == 1)
        cnt++;
    }

    if (cnt == n) {
      ans = min(ans, (r2 - l1) * (V[j - 1].first - V[i].first));
    }

    S[V[i].second]--;
    if (S[V[i].second] == 0)
      cnt--;
  }

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

  return 0;
}

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

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

vector<pll> V;
ll S[202020];
ll n, l1, l2, r1, r2, cnt, ans;

int main() {
  ll i, j, a, b;

  scanf("%lld", &n);

  l1 = l2 = 1e9;
  r1 = r2 = 0;

  for (i = 1; i <= n; i++) {
    scanf("%lld%lld", &a, &b);
    V.emplace_back(a, i);
    V.emplace_back(b, i);
    l1 = min(l1, min(a, b));
    l2 = min(l2, max(a, b));
    r1 = max(r1, min(a, b));
    r2 = max(r2, max(a, b));
  }

  ans = (r1 - l1) * (r2 - l2);

  sort(V.begin(), V.end());

  for (i = 0, j = 0; i < n + n; i++) {
    for (; j < n + n && cnt < n; j++) {
      S[V[j].second]++;
      if (S[V[j].second] == 1)
        cnt++;
    }

    if (cnt == n) {
      ans = min(ans, (r2 - l1) * (V[j - 1].first - V[i].first));
    }

    S[V[i].second]--;
    if (S[V[i].second] == 0)
      cnt--;
  }

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

  return 0;
}
Problem p03735 (C++)

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

using namespace std;

const int MaxN = 100005;

int n;
pair<int, int> a[MaxN];
multiset<int> s;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  if (n < 2) {
    cout << 0;
    return 0;
  }
  int Low = 1e9, High = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].first >> a[i].second;
    if (a[i].first > a[i].second) {
      swap(a[i].first, a[i].second);
    }
    Low = min(Low, a[i].first);
    High = max(High, a[i].second);
  }
  int RedMin = Low, RedMax = 0;
  int BlueMin = 1e9, Bluemax = High;
  for (int i = 1; i <= n; i++) {
    RedMax = max(RedMax, a[i].first);
    BlueMin = min(BlueMin, a[i].second);
  }
  long long Res = 1LL * (RedMax - RedMin) * (Bluemax - BlueMin);
  sort(a + 1, a + n + 1);
  Res = min(Res, 1LL * (High - Low) * (a[n].first - a[1].first));
  s.clear();
  for (int i = 1; i <= n; i++) {
    s.insert(a[i].first);
  }
  for (int i = 1; i <= n; i++) {
    s.erase(s.find(a[i].first));
    s.insert(a[i].second);
    Res = min(Res, 1LL * (High - Low) * (*s.rbegin() - *s.begin()));
  }
  cout << Res;
  return 0;
}


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

using namespace std;

const int MaxN = 200005;

int n;
pair<int, int> a[MaxN];
multiset<int> s;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  if (n < 2) {
    cout << 0;
    return 0;
  }
  int Low = 1e9, High = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].first >> a[i].second;
    if (a[i].first > a[i].second) {
      swap(a[i].first, a[i].second);
    }
    Low = min(Low, a[i].first);
    High = max(High, a[i].second);
  }
  int RedMin = Low, RedMax = 0;
  int BlueMin = 1e9, Bluemax = High;
  for (int i = 1; i <= n; i++) {
    RedMax = max(RedMax, a[i].first);
    BlueMin = min(BlueMin, a[i].second);
  }
  long long Res = 1LL * (RedMax - RedMin) * (Bluemax - BlueMin);
  sort(a + 1, a + n + 1);
  Res = min(Res, 1LL * (High - Low) * (a[n].first - a[1].first));
  s.clear();
  for (int i = 1; i <= n; i++) {
    s.insert(a[i].first);
  }
  for (int i = 1; i <= n; i++) {
    s.erase(s.find(a[i].first));
    s.insert(a[i].second);
    Res = min(Res, 1LL * (High - Low) * (*s.rbegin() - *s.begin()));
  }
  cout << Res;
  return 0;
}
Problem p03735 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <set>
#include <vector>
#define ll long long
using namespace std;
ll dp[200010];
ll dp1[200010];
vector<pair<ll, ll>> vt;
pair<ll, ll> a[200010];
ll visited[200010];
int main() {
  ll n;
  scanf("%lld", &n);
  ll xx = 0;
  ll yy = 1234567890;
  for (int i = 1; i <= n; i++) {
    scanf("%lld %lld", &a[i].first, &a[i].second);
    vt.push_back({min(a[i].first, a[i].second), -i});
    vt.push_back({max(a[i].second, a[i].first), i});
    visited[i] = max(a[i].first, a[i].second);
  }
  sort(vt.begin(), vt.end());
  int ch = 0;
  int t = vt.size();
  ll fi, la;
  for (int i = 0; i < t; i++) {
    if (vt[i].second > 0)
      visited[vt[i].second] = i;
  }
  for (int i = 0; i < t; i++) {
    if (vt[i].second > 0) {
      fi = i;
      break;
    }
  }
  ch = 0;
  for (int i = t - 1; i >= 0; i--) {
    if (vt[i].second < 0) {
      la = i;
      break;
    }
  }
  ll dap = (vt[t - 1].first - vt[fi].first) * (vt[la].first - vt[0].first);
  la = max(la, visited[-vt[0].second]);
  for (int i = 1; i <= fi; i++) {
    if (la == t - 1)
      break;
    dap = min(dap,
              (vt[t - 1].first - vt[0].first) * (vt[la].first - vt[i].first));
    la = max(la, visited[-vt[i].second]);
  }
  printf("%lld", dap);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <set>
#include <vector>
#define ll long long
using namespace std;
ll dp[200010];
ll dp1[200010];
vector<pair<ll, ll>> vt;
pair<ll, ll> a[200010];
ll visited[200010];
int main() {
  ll n;
  scanf("%lld", &n);
  ll xx = 0;
  ll yy = 1234567890;
  for (int i = 1; i <= n; i++) {
    scanf("%lld %lld", &a[i].first, &a[i].second);
    vt.push_back({min(a[i].first, a[i].second), -i});
    vt.push_back({max(a[i].second, a[i].first), i});
    visited[i] = max(a[i].first, a[i].second);
  }
  sort(vt.begin(), vt.end());
  int ch = 0;
  int t = vt.size();
  ll fi, la;
  for (int i = 0; i < t; i++) {
    if (vt[i].second > 0)
      visited[vt[i].second] = i;
  }
  for (int i = 0; i < t; i++) {
    if (vt[i].second > 0) {
      fi = i;
      break;
    }
  }
  ch = 0;
  for (int i = t - 1; i >= 0; i--) {
    if (vt[i].second < 0) {
      la = i;
      break;
    }
  }
  ll dap = (vt[t - 1].first - vt[fi].first) * (vt[la].first - vt[0].first);
  la = max(la, visited[-vt[0].second]);
  for (int i = 1; i <= fi; i++) {
    if (la == t - 1)
      break;
    dap = min(dap,
              (vt[t - 1].first - vt[0].first) * (vt[la].first - vt[i].first));
    if (i == fi)
      continue;
    la = max(la, visited[-vt[i].second]);
  }
  printf("%lld", dap);
}