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

Buggy submission (status: Runtime Error):
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
const int N = 2005, M = 4e6 + 5;
bitset<N> vis;
int a[N], n, sum, res;
int main() {
  scanf("%d", &n);
  vis[0] = 1;
  fp(i, 1, n) scanf("%d", &a[i]), sum += a[i], vis |= vis << a[i];
  res = (sum + 1) >> 1;
  fp(i, res, sum) if (vis[i]) return printf("%d\n", i), 0;
  puts("QAQ");
  return 0;
}

Fixed submission:
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
const int N = 2005, M = 4e6 + 5;
bitset<M> vis;
int a[N], n, sum, res;
int main() {
  scanf("%d", &n);
  vis[0] = 1;
  fp(i, 1, n) scanf("%d", &a[i]), sum += a[i], vis |= vis << a[i];
  res = (sum + 1) >> 1;
  fp(i, res, sum) if (vis[i]) return printf("%d\n", i), 0;
  puts("QAQ");
  return 0;
}
Problem p03465 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define int long long
using namespace std;

int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + c - 48;
    c = getchar();
  }
  return x * f;
}

int k, m, n;
bitset<4001000> w;
int sum = 0;
signed main() {
  freopen("mid.in", "r", stdin);
  freopen("mid.out", "w", stdout);
  n = read();
  w[0] = 1;
  for (register int i = 1; i <= n; i++) {
    int x = read();
    w |= w << x;
    sum += x;
  }
  for (register int i = (sum + 1) / 2; i <= sum; i++) {
    if (w[i]) {
      printf("%lld\n", i);
      return 0;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define int long long
using namespace std;

int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + c - 48;
    c = getchar();
  }
  return x * f;
}

int k, m, n;
bitset<4001000> w;
int sum = 0;
signed main() {
  n = read();
  w[0] = 1;
  for (register int i = 1; i <= n; i++) {
    int x = read();
    w |= w << x;
    sum += x;
  }
  for (register int i = (sum + 1) / 2; i <= sum; i++) {
    if (w[i]) {
      printf("%lld\n", i);
      return 0;
    }
  }
  return 0;
}
Problem p03465 (C++)

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

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

template <typename T> inline void chkmax(T &x, T y) {
  if (x < y)
    x = y;
}

template <typename T> inline void chkmin(T &x, T y) {
  if (x > y)
    x = y;
}

inline int read() {
#define nc getchar()
  int x = 0;
  char c = nc;
  while (c < 48)
    c = nc;
  while (c > 47)
    x = x * 10 + (c ^ 48), c = nc;
  return x;
#undef nc
}

typedef long long ll;
const int maxn = 2010;
int n, A[maxn];

bitset<maxn> dp;

void solve() {
  cin >> n;
  int sum = 0;
  rep(i, 1, n) { A[i] = read(), sum += A[i]; }
  dp[0] = 1;
  rep(i, 1, n) dp |= dp << A[i];
  int pos = (sum - 1) / 2 + 1;
  while (pos < sum && !dp[pos])
    pos++;
  printf("%d\n", pos);
}

int main() {
  //  int T = read();
  //  while (T--) solve();
  solve();
  return 0;
}

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

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

template <typename T> inline void chkmax(T &x, T y) {
  if (x < y)
    x = y;
}

template <typename T> inline void chkmin(T &x, T y) {
  if (x > y)
    x = y;
}

inline int read() {
#define nc getchar()
  int x = 0;
  char c = nc;
  while (c < 48)
    c = nc;
  while (c > 47)
    x = x * 10 + (c ^ 48), c = nc;
  return x;
#undef nc
}

typedef long long ll;
const int maxn = 2010;
int n, A[maxn];

bitset<maxn * maxn> dp;

void solve() {
  cin >> n;
  int sum = 0;
  rep(i, 1, n) { A[i] = read(), sum += A[i]; }
  dp[0] = 1;
  rep(i, 1, n) dp |= dp << A[i];
  int pos = (sum - 1) / 2 + 1;
  while (pos < sum && !dp[pos])
    pos++;
  printf("%d\n", pos);
}

int main() {
  //  int T = read();
  //  while (T--) solve();
  solve();
  return 0;
}
Problem p03465 (C++)

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

using namespace std;

const int MAX = 2e3 + 10;
int N;
bitset<MAX> bs;

int main() {
  bs = 1;
  cin >> N;
  int s = 0;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    bs |= (bs << a);
    s += a;
  }

  for (int i = (s + 1) / 2; i <= s; i++) {
    if (bs[i]) {
      cout << i << endl;
      return 0;
    }
  }
}

Fixed submission:
#include <bitset>
#include <iostream>

using namespace std;

const int MAX = 2e3 + 10;
int N;
bitset<MAX * MAX> bs;

int main() {
  bs = 1;
  cin >> N;
  int s = 0;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    bs |= (bs << a);
    s += a;
  }

  for (int i = (s + 1) / 2; i <= s; i++) {
    if (bs[i]) {
      cout << i << endl;
      return 0;
    }
  }
}
Problem p03465 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
bitset<1000001> dp;
int main() {
  int n;
  int i, j;
  cin >> n;
  int a[n];
  int sum = 0;
  for (i = 0; i < n; i++) {

    cin >> a[i];
    sum = sum + a[i];
  }

  dp = 1;
  for (i = 0; i < n; i++) {
    dp |= (dp << a[i]);
  }

  for (i = (sum + 1) / 2; i <= sum; i++) {
    if (dp[i] == 1) {
      cout << i << endl;
      break;
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
bitset<4000008> dp;
int main() {
  int n;
  int i, j;
  cin >> n;
  int a[n];
  int sum = 0;
  for (i = 0; i < n; i++) {

    cin >> a[i];
    sum = sum + a[i];
  }

  dp = 1;
  for (i = 0; i < n; i++) {
    dp |= (dp << a[i]);
  }

  for (i = (sum + 1) / 2; i <= sum; i++) {
    if (dp[i] == 1) {
      cout << i << endl;
      break;
    }
  }

  return 0;
}
Problem p03465 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, a[2000];
  int sum = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }
  int s2 = sum - (sum + 1) / 2;
  bitset<2000001> bs;
  bs[0] = 1;
  for (int i = 0; i < n; i++) {
    for (int k = s2 - a[i]; k >= 0; k--) {
      if (bs[k] == 1)
        bs[k + a[i]] = 1;
    }
  }
  for (int i = s2; i >= 0; i--) {
    if (bs[i] == 1) {
      printf("%d\n", sum - i);
      return 0;
    }
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, a[2000];
  int sum = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }
  int s2 = sum - (sum + 1) / 2;
  bitset<2000001> bs;
  bs[0] = 1;
  for (int i = 0; i < n; i++) {
    bs = bs | (bs << a[i]);
  }
  for (int i = s2; i >= 0; i--) {
    if (bs[i] == 1) {
      printf("%d\n", sum - i);
      return 0;
    }
  }
}
Problem p03465 (C++)

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

using namespace std;

#define re return
#define sz(a) (int)a.size()
#define mp(a, b) make_pair(a, b)
#define fi first
#define se second
#define re return
#define forn(i, n) for (int i = 0; i < int(n); i++)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef long double ld;

vector<int> a, b;
int n;
bitset<20000001> q;

int main() {
  iostream::sync_with_stdio(0), cin.tie(0);
  // freopen("a.in", "r", stdin);
  cin >> n;
  q[0] = 1;
  a.resize(n);
  int sum = 0;
  int k = 0;
  forn(i, n) {
    cin >> a[i];
    q |= (q << a[i]);
    sum += a[i];
  }
  int ans = 0;
  forn(i, min(sum, 2000001)) {
    if (q[i] && i <= sum - i)
      ans = sum - i;
  }
  /*forn (i, (1 << n)) {
          int sm = 0;
          forn (j, n)
                  if (i & (1 << j)) sm += a[j];
          b.push_back(sm);
  }
  sort(b.begin(), b.end());
  for (int v : b) cout << v << " ";
  if (b[sz(b) / 2] != ans) assert(0);*/
  cout << ans;
  re 0;
}

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

using namespace std;

#define re return
#define sz(a) (int)a.size()
#define mp(a, b) make_pair(a, b)
#define fi first
#define se second
#define re return
#define forn(i, n) for (int i = 0; i < int(n); i++)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef long double ld;

vector<int> a, b;
int n;
bitset<2000001> q;

int main() {
  iostream::sync_with_stdio(0), cin.tie(0);
  // freopen("a.in", "r", stdin);
  cin >> n;
  q[0] = 1;
  a.resize(n);
  int sum = 0;
  int k = 0;
  forn(i, n) {
    cin >> a[i];
    q |= (q << a[i]);
    sum += a[i];
  }
  int ans = 0;
  forn(i, min(sum, 2000001)) {
    if (q[i] && i <= sum - i)
      ans = sum - i;
  }
  /*forn (i, (1 << n)) {
          int sm = 0;
          forn (j, n)
                  if (i & (1 << j)) sm += a[j];
          b.push_back(sm);
  }
  sort(b.begin(), b.end());
  for (int v : b) cout << v << " ";
  if (b[sz(b) / 2] != ans) assert(0);*/
  cout << ans;
  re 0;
}
Problem p03465 (C++)

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

using namespace std;

const int MAX_N = 2e3 + 5;
const int ZERO = 3e5;
const int MAX_B = 6e5 + 5;

int arr[MAX_N];

vector<char> _last;
char &last(int j) { return _last[j + ZERO]; }

vector<char> _dp;
char &dp(int j) { return _dp[j + ZERO]; }

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

  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  random_shuffle(arr, arr + n);

  _dp = vector<char>(MAX_B, 0);
  dp(0) = 1;
  for (int i = 0; i < n; i++) {
    _last = _dp;
    _dp = vector<char>(MAX_B, 0);
    for (int j = -ZERO; j <= ZERO; j++) {
      if (last(j)) {
        if (j - arr[i] >= -ZERO) {
          dp(j - arr[i]) = 1;
        }
        if (j + arr[i] <= ZERO) {
          dp(j + arr[i]) = 1;
        }
      }
    }
  }

  for (int i = 0; i <= ZERO; i++) {
    if (dp(i)) {
      cout << (sum + i) / 2 << endl;
      return 0;
    }
  }
}


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

using namespace std;

const int MAX_N = 2e3 + 5;
const int ZERO = 2e5 + 5e4;
const int MAX_B = 5e5 + 5;

int arr[MAX_N];

vector<char> _last;
char &last(int j) { return _last[j + ZERO]; }

vector<char> _dp;
char &dp(int j) { return _dp[j + ZERO]; }

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

  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  random_shuffle(arr, arr + n);

  _dp = vector<char>(MAX_B, 0);
  dp(0) = 1;
  for (int i = 0; i < n; i++) {
    _last = _dp;
    _dp = vector<char>(MAX_B, 0);
    for (int j = -ZERO; j <= ZERO; j++) {
      if (last(j)) {
        if (j - arr[i] >= -ZERO) {
          dp(j - arr[i]) = 1;
        }
        if (j + arr[i] <= ZERO) {
          dp(j + arr[i]) = 1;
        }
      }
    }
  }

  for (int i = 0; i <= ZERO; i++) {
    if (dp(i)) {
      cout << (sum + i) / 2 << endl;
      return 0;
    }
  }
}
Problem p03465 (C++)

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

bitset<20000007> dp;
int main() {
  int n;
  cin >> n;
  dp[0] = 1;
  int x = 0;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> x;
    sum = sum + x;
    dp = (dp | dp << x);
  }
  int target = (sum + 1) / 2;

  for (int i = target; i <= sum; i++) {
    if (dp[i]) {
      cout << i;
      break;
    }
  }

  return 0;
}


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

bitset<2000007> dp;
int main() {
  int n;
  cin >> n;
  dp[0] = 1;
  int x = 0;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> x;
    sum = sum + x;
    dp = (dp | dp << x);
  }
  int target = (sum + 1) / 2;

  for (int i = target; i <= sum; i++) {
    if (dp[i]) {
      cout << i;
      break;
    }
  }

  return 0;
}
Problem p03465 (C++)

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

bitset<2000009> dp;

int main() {
  int n, i, j, a[2009], sum = 0;

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }

  dp[0] = 1;

  for (i = 0; i < n; i++) {
    for (j = sum / 2; j >= a[i]; j--) {
      dp[j] = max(dp[j], dp[j - a[i]]);
    }
  }

  for (i = sum / 2; i >= 0; i--) {
    if (dp[i]) {
      printf("%d", sum - i);
      return 0;
    }
  }
}


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

bitset<2000009> dp;

int main() {
  int n, i, j, a[2009], sum = 0;

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }

  dp[0] = 1;

  for (i = 0; i < n; i++) {
    dp |= (dp << a[i]);
    //        cout << dp << endl;
  }

  for (i = sum / 2; i >= 0; i--) {
    if (dp[i]) {
      printf("%d", sum - i);
      return 0;
    }
  }
}
Problem p03465 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n;
int a[2005];
int sum[2005];
int cnt[2005];
vector<ll> dp[2];

int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    sum[i + 1] += a[i];
    sum[i + 1] += sum[i];
  }
  dp[0].push_back(1);
  int nows = 1, prevs = 0;
  for (int i = 0; i < n; i++) {
    int vi = 0;
    while (vi <= sum[i + 1] + 100) {
      dp[nows].push_back(0);
      vi += 60;
    }
    int siz = a[i] / 60;
    int dif = a[i] % 60;
    for (int j = 0; j < dp[prevs].size(); j++) {
      dp[nows][j] |= dp[i][j];
      if (dif > 0) {
        dp[nows][j + siz + 1] |=
            (dp[prevs][j] & ((1LL << 60) - (1LL << (60 - dif)) + 1LL)) >>
            (60 - dif);
        dp[nows][j + siz] |= (dp[prevs][j] & ((1LL << (60 - dif)) - 1LL))
                             << dif;
      } else {
        dp[nows][j + siz] |= dp[prevs][j];
      }
      // cout << ((dp[i][j].to_ullong()&((1LL<<dif)-1))<<dif)  << endl;
    }
    swap(nows, prevs);
    dp[nows].clear();
  }
  for (int i = 0; i < dp[prevs].size(); i++) {
    for (int j = 0; j < 60; j++) {
      if (dp[prevs][i] >> j & 1) {
        if ((i * 60 + j) * 2 >= sum[n]) {
          printf("%d\n", i * 60 + j);
          return 0;
        }
      }
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n;
int a[2005];
int sum[2005];
int cnt[2005];
vector<ll> dp[2];

int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    sum[i + 1] += a[i];
    sum[i + 1] += sum[i];
  }
  dp[0].push_back(1);
  int nows = 1, prevs = 0;
  for (int i = 0; i < n; i++) {
    int vi = 0;
    while (vi <= sum[i + 1] + 100) {
      dp[nows].push_back(0);
      vi += 60;
    }
    int siz = a[i] / 60;
    int dif = a[i] % 60;
    for (int j = 0; j < dp[prevs].size(); j++) {
      dp[nows][j] |= dp[prevs][j];
      if (dif > 0) {
        dp[nows][j + siz + 1] |=
            (dp[prevs][j] & ((1LL << 60) - (1LL << (60 - dif)) + 1LL)) >>
            (60 - dif);
        dp[nows][j + siz] |= (dp[prevs][j] & ((1LL << (60 - dif)) - 1LL))
                             << dif;
      } else {
        dp[nows][j + siz] |= dp[prevs][j];
      }
      // cout << ((dp[i][j].to_ullong()&((1LL<<dif)-1))<<dif)  << endl;
    }
    swap(nows, prevs);
    dp[nows].clear();
  }
  for (int i = 0; i < dp[prevs].size(); i++) {
    for (int j = 0; j < 60; j++) {
      if (dp[prevs][i] >> j & 1) {
        if ((i * 60 + j) * 2 >= sum[n]) {
          printf("%d\n", i * 60 + j);
          return 0;
        }
      }
    }
  }
  return 0;
}
Problem p03465 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
bitset<2000010> f;
int n, sum;
int a[2005];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a[i]);
    sum += a[i];
  }
  f[0] = 1;
  for (int i = n; i >= 1; i--)
    f |= f << a[i];
  for (int i = (sum + 1) >> 1; i <= sum; i++) {
    if (f[i]) {
      printf("%d\n", i);
      return 0;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
bitset<2000010> f;
int n, sum;
int a[2005];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }
  f[0] = 1;
  for (int i = n; i >= 1; i--)
    f |= f << a[i];
  for (int i = (sum + 1) >> 1; i <= sum; i++) {
    if (f[i]) {
      printf("%d\n", i);
      return 0;
    }
  }
  return 0;
}
Problem p03465 (C++)

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

bool dp[2000009];

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

  int n, i, j, a[2009], sum = 0;

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

  dp[0] = 1;

  for (i = 0; i < n; i++) {
    for (j = sum / 2; j >= a[i]; j--) {
      dp[j] |= dp[j - a[i]];
    }
  }

  for (i = sum / 2; i >= 0; i--) {
    if (dp[i]) {
      cout << sum - i;
      return 0;
    }
  }
}


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

bool dp[2000009];

int main() {
  int n, i, j, a[2009], sum = 0;

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

  dp[0] = 1;

  for (i = 0; i < n; i++) {
    for (j = sum / 2; j >= a[i]; j--) {
      dp[j] |= dp[j - a[i]];
    }
  }

  for (i = sum / 2; i >= 0; i--) {
    if (dp[i]) {
      cout << sum - i;
      return 0;
    }
  }
}
Problem p03465 (C++)

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX / 3

ll n;
ll a[2002];
bitset<4002001> dp, tmp, one;

int main() {
  cin >> n;
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  sum = (sum + 1) / 2;
  dp = 1;
  one = 1;
  rep(i, n) {
    tmp = dp << a[i];
    dp |= tmp;
  }
  rep(i, 4000001) {
    if (((dp >> i) & one) == one && i >= sum) {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX / 3

ll n;
ll a[2002];
bitset<4002001> dp, tmp, one;

int main() {
  cin >> n;
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  sum = (sum + 1) / 2;
  dp = 1;
  one = 1;
  rep(i, n) { dp |= (dp << a[i]); }
  repl(i, sum, 4000001) {
    if (((dp >> i) & one) == one) {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}
Problem p03465 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <string>
#define rep(i, a, b) for (long long int i = (a); i < (b); i++)
#define se(a) a.begin(), a.end()
#define ll long long int
#define mll map<ll, ll>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pb push_back
#define mk make_pair
#define F first
#define S second
#define mod 1000000007
#define maxn 100005
#define inf 1e16
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
int main() {
  //	ll tc;
  //	cin>>tc;
  //	while(tc--)
  {
    ll n;
    cin >> n;
    ll a[n];
    rep(i, 0, n) cin >> a[i];
    ll sum = 0;
    rep(i, 0, n) sum += a[i];
    bool dp[n + 1][sum + 1];
    rep(i, 0, n + 1) rep(j, 0, sum + 1) {
      if (j == 0)
        dp[i][j] = 1;
      else if (i == 0)
        dp[i][j] = 0;
      else if (j >= a[i - 1])
        dp[i][j] = dp[i - 1][j - a[i - 1]] || dp[i - 1][j];
      else
        dp[i][j] = dp[i - 1][j];
    }
    sum = (sum + 1) / 2;
    while (dp[n][sum] == 0)
      sum++;
    cout << sum << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#include <string>
#define rep(i, a, b) for (long long int i = (a); i < (b); i++)
#define se(a) a.begin(), a.end()
#define ll long long int
#define mll map<ll, ll>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pb push_back
#define mk make_pair
#define F first
#define S second
#define mod 1000000007
#define maxn 100005
#define inf 1e16
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
int main() {
  //	ll tc;
  //	cin>>tc;
  //	while(tc--)
  {
    ll n;
    cin >> n;
    ll a[n];
    rep(i, 0, n) cin >> a[i];
    ll sum = 0;
    rep(i, 0, n) sum += a[i];
    bitset<4000005> dp;
    dp.set(0);

    for (int i = 0; i < n; i++)
      dp = dp | dp << a[i];
    ll x = (sum + 1) / 2;
    while (dp[x] == 0)
      x++;
    cout << x << endl;
  }
}
Problem p03465 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  // freopen("AT3857.01.inp","r",stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  bitset<40000> f;
  f[0] = 1;
  int sa = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    f |= f << x;
    sa += x;
  }
  for (int i = ((sa + 1) >> 1); i <= sa; i++) {
    if (f[i]) {
      cout << i << '\n';
      return 0;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  // freopen("AT3857.01.inp","r",stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  bitset<4000000> f;
  f[0] = 1;
  int sa = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    f |= f << x;
    sa += x;
  }
  for (int i = ((sa + 1) >> 1); i <= sa; i++) {
    if (f[i]) {
      cout << i << '\n';
      return 0;
    }
  }
}
Problem p03465 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN1 = 2001;
const int MAXN2 = 4000001;
int n, tot = 0, sum = 0;
int a[MAXN1], canget[MAXN2];
bool flag[MAXN2];
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
int main() {
  freopen("testdata.in", "r", stdin);
  memset(flag, 0, sizeof(flag));
  n = read();
  for (int i = 1; i <= n; ++i)
    sum += (a[i] = read());
  if (sum & 1)
    ++sum;
  sum /= 2;
  flag[0] = true;
  canget[++tot] = 0;
  for (int i = 1; i <= n; ++i) {
    int temp = tot;
    for (int j = 1; j <= temp; ++j) {
      int k = a[i] + canget[j];
      if (flag[k] == false) {
        flag[k] = true;
        if (k < sum)
          canget[++tot] = k;
      }
    }
  }
  for (int i = sum; i < MAXN2; ++i)
    if (flag[i]) {
      printf("%d", i);
      return 0;
    }
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN1 = 2001;
const int MAXN2 = 4000001;
int n, tot = 0, sum = 0;
int a[MAXN1], canget[MAXN2];
bool flag[MAXN2];
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
int main() {
  memset(flag, 0, sizeof(flag));
  n = read();
  for (int i = 1; i <= n; ++i)
    sum += (a[i] = read());
  if (sum & 1)
    ++sum;
  sum /= 2;
  flag[0] = true;
  canget[++tot] = 0;
  for (int i = 1; i <= n; ++i) {
    int temp = tot;
    for (int j = 1; j <= temp; ++j) {
      int k = a[i] + canget[j];
      if (flag[k] == false) {
        flag[k] = true;
        if (k < sum)
          canget[++tot] = k;
      }
    }
  }
  for (int i = sum; i < MAXN2; ++i)
    if (flag[i]) {
      printf("%d", i);
      return 0;
    }
}
Problem p03465 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 1000000007;
const int MX = 1e3 + 1;
bitset<MX * MX> bs;
int main() {
  bs[0] = 1;
  int n, s = 0, sc;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &sc);
    bs = bs | (bs << sc);
    s += sc;
  }
  for (int i = (s + 1) / 2; i <= s; i++)
    if (bs[i]) {
      printf("%d", i);
      break;
    }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 1000000007;
const int MX = 2e3 + 1;
bitset<MX * MX> bs;
int main() {
  bs[0] = 1;
  int n, s = 0, sc;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &sc);
    bs = bs | (bs << sc);
    s += sc;
  }
  for (int i = (s + 1) / 2; i <= s; i++)
    if (bs[i]) {
      printf("%d", i);
      break;
    }
  return 0;
}
Problem p03465 (C++)

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

using namespace std;
int main() {
  int i, n, a[2020], sum = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  bitset<80> d, p;
  d.set(0);
  for (i = 0; i < n; i++) {
    p |= d;
    p |= d << a[i];
    swap(d, p);
  }

  for (i = (sum + 1) / 2; i < 4000010; i++) {
    if (d[i]) {
      cout << i << endl;
      return 0;
    }
  }
}

Fixed submission:
#include <bitset>
#include <iostream>

using namespace std;
int main() {
  int i, n, a[2020], sum = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  bitset<4000010> d, p;
  d.set(0);
  for (i = 0; i < n; i++) {
    p |= d;
    p |= d << a[i];
    swap(d, p);
  }

  for (i = (sum + 1) / 2; i < 4000010; i++) {
    if (d[i]) {
      cout << i << endl;
      return 0;
    }
  }
}
Problem p03465 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IL inline
#define RG register
#define gi geti<int>()
#define gl geti<ll>()
#define gc getchar()
#define File(a)                                                                \
  freopen(a ".in", "r", stdin);                                                \
  freopen(a ".out", "w", stdout)
template <typename T> IL bool chkmax(T &x, const T &y) {
  return x < y ? x = y, 1 : 0;
}
template <typename T> IL bool chkmin(T &x, const T &y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T> IL T geti() {
  T xi = 0;
  char ch = gc;
  bool f = 0;
  while (!isdigit(ch))
    ch == '-' ? f = 1 : f, ch = gc;
  while (isdigit(ch))
    xi = xi * 10 + ch - 48, ch = gc;
  return f ? -xi : xi;
}
template <typename T> IL void pi(T k, char ch = 0) {
  if (k < 0)
    k = -k, putchar('-');
  if (k >= 10)
    pi(k / 10);
  putchar(k % 10 + '0');
  if (ch)
    putchar(ch);
}
/*
IL unsigned int LOG2(unsigned int x)
{
        unsigned int ret;
        __asm__ __volatile__ ("bsrl %1, %%eax":"=a"(ret):"m"(x));
        return ret;
}
*/
const int N = 2e3 + 7;
int a[N], n, sum;
bitset<N * N> t, w;
int main(void) {
#ifndef ONLINE_JUDGE
  File("mid");
#endif
  n = gi;
  for (int i = 1; i <= n; ++i)
    a[i] = gi;
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; ++i) {
    sum += a[i];
    t |= t << a[i];
    t[a[i]] = 1;
  }
  int s = t.count() / 2;
  for (int i = sum; i; --i) {
    if (t[i]) {
      if (!s) {
        pi(i);
        break;
      }
      --s;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IL inline
#define RG register
#define gi geti<int>()
#define gl geti<ll>()
#define gc getchar()
#define File(a)                                                                \
  freopen(a ".in", "r", stdin);                                                \
  freopen(a ".out", "w", stdout)
template <typename T> IL bool chkmax(T &x, const T &y) {
  return x < y ? x = y, 1 : 0;
}
template <typename T> IL bool chkmin(T &x, const T &y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T> IL T geti() {
  T xi = 0;
  char ch = gc;
  bool f = 0;
  while (!isdigit(ch))
    ch == '-' ? f = 1 : f, ch = gc;
  while (isdigit(ch))
    xi = xi * 10 + ch - 48, ch = gc;
  return f ? -xi : xi;
}
template <typename T> IL void pi(T k, char ch = 0) {
  if (k < 0)
    k = -k, putchar('-');
  if (k >= 10)
    pi(k / 10);
  putchar(k % 10 + '0');
  if (ch)
    putchar(ch);
}
/*
IL unsigned int LOG2(unsigned int x)
{
        unsigned int ret;
        __asm__ __volatile__ ("bsrl %1, %%eax":"=a"(ret):"m"(x));
        return ret;
}
*/
const int N = 2e3 + 7;
int a[N], n, sum;
bitset<N * N> t, w;
int main(void) {
  n = gi;
  for (int i = 1; i <= n; ++i)
    a[i] = gi;
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; ++i) {
    sum += a[i];
    t |= t << a[i];
    t[a[i]] = 1;
  }
  int s = t.count() / 2;
  for (int i = sum; i; --i) {
    if (t[i]) {
      if (!s) {
        pi(i);
        break;
      }
      --s;
    }
  }
  return 0;
}
Problem p03465 (C++)

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

const double PI = 3.14159265358979323846;
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
// #define rep(i, n) for (ll i = 0; i < (n); ++i)
// typedef pair<ll, ll> P;
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

std::istream &operator>>(std::istream &in, set<int> &o) {
  ll a;
  in >> a;
  o.insert(a);
  return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
  ll a;
  in >> a;
  o.push(a);
  return in;
}

bool contain(set<char> &s, char a) { return s.find(a) != s.end(); }

// ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
// ofstream outfile("log.txt");
// outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0')
// << rank << endl;
//  std::cout << std::bitset<8>(9);

// const ll mod = 1e10;
typedef priority_queue<P, vector<P>, greater<P>> PQ_ASK;

const int MAX = 4040404;

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  rep(i, n) cin >> v[i];

  ll all = accumulate(v.begin(), v.end(), 0ll);

  bitset<MAX> dp;
  dp[0] = true;

  rep(i, n) {

    bitset<MAX> next;

    auto ok = [&](int i, int j) {
      if (j > all)
        return;
      next[j] = true;
    };

    rep(j, all + 1) {
      if (!dp[j])
        continue;
      ll next = j + v[i];
      ok(i + 1, next);
      ok(i + 1, j);
    }

    dp = next;
  }

  for (ll i = (all + 1) / 2; i <= all; i++) {
    if (dp[i]) {
      cout << i << endl;
      ret();
    }
  }
}

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

const double PI = 3.14159265358979323846;
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
// #define rep(i, n) for (ll i = 0; i < (n); ++i)
// typedef pair<ll, ll> P;
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

std::istream &operator>>(std::istream &in, set<int> &o) {
  ll a;
  in >> a;
  o.insert(a);
  return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
  ll a;
  in >> a;
  o.push(a);
  return in;
}

bool contain(set<char> &s, char a) { return s.find(a) != s.end(); }

// ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
// ofstream outfile("log.txt");
// outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0')
// << rank << endl;
//  std::cout << std::bitset<8>(9);

// const ll mod = 1e10;
typedef priority_queue<P, vector<P>, greater<P>> PQ_ASK;

const int MAX = 4040404;

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  rep(i, n) cin >> v[i];

  ll all = accumulate(v.begin(), v.end(), 0ll);

  bitset<MAX> dp;
  dp[0] = true;

  rep(i, n) dp |= (dp << v[i]);

  for (ll i = (all + 1) / 2; i <= all; i++) {
    if (dp[i]) {
      cout << i << endl;
      ret();
    }
  }
}
Problem p03465 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2,tune=native")
#pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("trapv")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using pi = pair<ll, ll>;
const int mod = 1000000007;
const ll inf = 1e18;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
bitset<2020 * 2020> x;
int main() { // DINIC ORZ
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, t;
  cin >> n;
  x.set(0);
  while (n--) {
    cin >> t;
    x |= x << t;
  }
  int cnt = x.count() / 2;
  int a = 0;
  while (cnt) {
    cnt -= x.test(++a);
  }
  cout << a;
}


Fixed submission:
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx2,tune=native")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("trapv")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using pi = pair<ll, ll>;
const int mod = 1000000007;
const ll inf = 1e18;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
bitset<2020 * 2020> x;
int main() { // DINIC ORZ
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, t;
  cin >> n;
  x.set(0);
  while (n--) {
    cin >> t;
    x |= x << t;
  }
  int cnt = x.count() / 2;
  int a = 0;
  while (cnt) {
    cnt -= x.test(++a);
  }
  cout << a;
}
Problem p03465 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, l, r) for (register int i = l; i < r; i++)
#define hrp(i, l, r) for (register int i = l; i <= r; i++)
#define rev(i, r, l) for (register int i = r; i >= l; i--)
#define pb push_back
#define int ll
#ifndef JOEON
#define D(...)
#define R(x, l)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template <typename tn> inline tn next(void);
template <typename tn>
inline ostream &operator<<(ostream &os, const vector<tn> &v);
bitset<405000> bs = 1;
signed main(void) {
#ifdef JOEON
  //		freopen("C:\Users\Joeon\Desktop\IN.txt", "r", stdin);
  //		freopen("C:\Users\Joeon\Desktop\OUT.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T = next<int>(), sum = 0;
  while (T--) {
    int x = next<int>();
    bs |= bs << x;
    sum += x;
  }
  D(sum, bs);
  hrp(i, (sum + 1) / 2, sum) if (bs[i]) {
    cout << i << endl;
    break;
  }
  return 0;
}
template <typename tn> inline tn next(void) {
  tn k;
  cin >> k;
  return k;
}
template <typename tn>
inline ostream &operator<<(ostream &os, const vector<tn> &v) {
  rep(i, 0, v.size()) os << v[i] << ' ';
  return os;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, l, r) for (register int i = l; i < r; i++)
#define hrp(i, l, r) for (register int i = l; i <= r; i++)
#define rev(i, r, l) for (register int i = r; i >= l; i--)
#define pb push_back
#define int ll
#ifndef JOEON
#define D(...)
#define R(x, l)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template <typename tn> inline tn next(void);
template <typename tn>
inline ostream &operator<<(ostream &os, const vector<tn> &v);
bitset<4050000> bs = 1;
signed main(void) {
#ifdef JOEON
  //		freopen("C:\Users\Joeon\Desktop\IN.txt", "r", stdin);
  //		freopen("C:\Users\Joeon\Desktop\OUT.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T = next<int>(), sum = 0;
  while (T--) {
    int x = next<int>();
    bs |= bs << x;
    sum += x;
  }
  D(sum, bs);
  hrp(i, (sum + 1) / 2, sum) if (bs[i]) {
    cout << i << endl;
    break;
  }
  return 0;
}
template <typename tn> inline tn next(void) {
  tn k;
  cin >> k;
  return k;
}
template <typename tn>
inline ostream &operator<<(ostream &os, const vector<tn> &v) {
  rep(i, 0, v.size()) os << v[i] << ' ';
  return os;
}
Problem p03465 (C++)

Buggy submission (status: Runtime Error):
/*等价于求比s/2大的最小的数
为什么呢？我们假定有一个值x<s/2
那么它一定会对应一组s-x>s/2(原来选的都不选，原来不选的都选)
然后全部都选没有对应的，所以求的就是比s/2大的最小的那个
然后就变成了一个背包问题*/
#include <bits/stdc++.h>
#define N 1000005
using namespace std;
bitset<N> v;
int n, a[N], m;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  v[0] = 1;
  for (int i = 1; i <= n; i++)
    m += a[i], v |= v << a[i];
  for (int i = m / 2; i >= 0; i--)
    if (v[i]) {
      printf("%d\n", m - i);
      return 0;
    }
  return 0;
}

Fixed submission:
/*等价于求比s/2大的最小的数
为什么呢？我们假定有一个值x<s/2
那么它一定会对应一组s-x>s/2(原来选的都不选，原来不选的都选)
然后全部都选没有对应的，所以求的就是比s/2大的最小的那个
然后就变成了一个背包问题*/
#include <bits/stdc++.h>
#define N 4000005
using namespace std;
bitset<N> v;
int n, a[N], m;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  v[0] = 1;
  for (int i = 1; i <= n; i++)
    m += a[i], v |= v << a[i];
  for (int i = m / 2; i >= 0; i--)
    if (v[i]) {
      printf("%d\n", m - i);
      return 0;
    }
  return 0;
}
Problem p03465 (C++)

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define F first
#define S second
#define pb push_back
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MP make_pair
#define SORT_UNIQUE(v) v.resize(unique(ALL(v)) - v.begin())
#define GET_POS(v, a) lower_bound(ALL(v), a) - v.begin();

const int maxn = 2005;
const int INF = 0x3f3f3f3f;

int a[maxn];
bitset<2000005> b;

int main() {
  int n;
  cin >> n;
  REP(i, n) cin >> a[i];
  int sum = 0;
  REP(i, n) sum += a[i];
  b[0] = 1;
  REP(i, n) {
    for (int j = 2000004; j >= 0; j--)
      if (b[j] && a[i] + j < 2000005) {
        b[a[i] + j] = 1;
      }
  }
  int tmp = sum / 2;
  while (!b[tmp])
    tmp--;
  cout << sum - tmp << '\n';
}


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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define F first
#define S second
#define pb push_back
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MP make_pair
#define SORT_UNIQUE(v) v.resize(unique(ALL(v)) - v.begin())
#define GET_POS(v, a) lower_bound(ALL(v), a) - v.begin();

const int maxn = 2005;
const int INF = 0x3f3f3f3f;

int a[maxn];
bitset<2000005> b;

int main() {
  int n;
  cin >> n;
  REP(i, n) cin >> a[i];
  int sum = 0;
  REP(i, n) sum += a[i];
  b[0] = 1;
  REP(i, n) { b = b | (b << a[i]); }
  int tmp = sum / 2;
  while (!b[tmp])
    tmp--;
  cout << sum - tmp << '\n';
}
Problem p03465 (C++)

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

using namespace std;

bitset<2035> B;

int main() {
  int n;
  scanf("%d", &n);
  B[0] = 1;
  int sm = 0;
  for (int i = 1; i <= n; i++) {
    int a;
    scanf("%d", &a);
    B |= (B << a);
    sm += a;
  }
  for (int i = (sm + 1) / 2;; i++)
    if (B[i]) {
      printf("%d\n", i);
      return 0;
    }
}

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

using namespace std;

bitset<4444444> B;

int main() {
  int n;
  scanf("%d", &n);
  B[0] = 1;
  int sm = 0;
  for (int i = 1; i <= n; i++) {
    int a;
    scanf("%d", &a);
    B |= (B << a);
    sm += a;
  }
  for (int i = (sm + 1) / 2;; i++)
    if (B[i]) {
      printf("%d\n", i);
      return 0;
    }
}
Problem p03466 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize(2)
#pragma G++ optimize(2)
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MAX 100005
using namespace std;
// char nc()
//{
//	static char buf[100000],*p1=buf,*p2=buf;
//	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
// }
char nc() { return getchar(); }
int read() {
  int x = 0, y = 1;
  char c = nc();
  while (!isdigit(c)) {
    if (c == '-')
      y = -1;
    c = nc();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = nc();
  }
  return x * y;
}
int A, B, C, D, k;
bool check(int mid) {
  int a = A - mid / (k + 1) * k - mid % (k + 1);
  int b = B - mid / (k + 1);
  return b <= 1ll * a * k;
}
int main() {
  int T = read();
  while (T--) {
    A = read();
    B = read();
    C = read();
    D = read();
    k = max(ceil(1.0 * A / (B + 1)), ceil(1.0 * B / (A + 1)));
    int l = 0, r = A + B, mid;
    while (l <= r) {
      mid = l + r >> 1;
      if (check(mid))
        l = mid + 1;
      else
        r = mid;
    }
    int a = A - l / (k + 1) * k - l % (k + 1);
    int b = B - l / (k + 1);
    for (int i = C; i <= min(D, l); i++)
      putchar(i % (k + 1) ? 'A' : 'B');
    int st = l + b - a * k + 1;
    for (int i = max(C, l + 1); i <= D; i++)
      putchar((i - st) % (k + 1) ? 'B' : 'A');
    putchar('\n');
  }
  return 0;
}


Fixed submission:
#pragma GCC optimize(2)
#pragma G++ optimize(2)
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MAX 100005
using namespace std;
// char nc()
//{
//	static char buf[100000],*p1=buf,*p2=buf;
//	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
// }
char nc() { return getchar(); }
int read() {
  int x = 0, y = 1;
  char c = nc();
  while (!isdigit(c)) {
    if (c == '-')
      y = -1;
    c = nc();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = nc();
  }
  return x * y;
}
int A, B, C, D, k;
bool check(int mid) {
  int a = A - mid / (k + 1) * k - mid % (k + 1);
  int b = B - mid / (k + 1);
  return b <= 1ll * a * k;
}
int main() {
  int T = read();
  while (T--) {
    A = read();
    B = read();
    C = read();
    D = read();
    k = max(ceil(1.0 * A / (B + 1)), ceil(1.0 * B / (A + 1)));
    int l = 0, r = A + B, mid;
    while (l < r) {
      mid = l + r >> 1;
      if (check(mid))
        l = mid + 1;
      else
        r = mid;
    }
    int a = A - l / (k + 1) * k - l % (k + 1);
    int b = B - l / (k + 1);
    for (int i = C; i <= min(D, l); i++)
      putchar(i % (k + 1) ? 'A' : 'B');
    int st = l + b - a * k + 1;
    for (int i = max(C, l + 1); i <= D; i++)
      putchar((i - st) % (k + 1) ? 'B' : 'A');
    putchar('\n');
  }
  return 0;
}
Problem p03466 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define MAXN 100005
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
ll q, a, b, c, d, k;
bool check(ll turns) {
  if (b < turns || a < k * turns)
    return false;
  ll restb = b - turns;
  ll resta = a - turns * k;
  if (restb >= k * (resta + 1))
    return false;
  else
    return true;
}
int main() {
  scanf("%lld", &q);
  while (q--) {
    scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
    ll s = a + b;
    k = (max(a, b) - 1) / (min(a, b) + 1) + 1;
    ll l = 0, r = (a - 1) / k + 2;
    while (r - l > 1) {
      ll mid = (l + r) / 2;
      if (check(mid))
        l = mid;
      else
        r = mid;
    }
    ll turns = l;
    a -= turns * k;
    b -= turns;
    ll cnta = 0, cntb = 0;
    while (b <= k * a && a > 0)
      cnta++, a--;
    while (b > k * a && b > 0)
      cntb++, b--;
    assert(b == k * a);
    for (ll i = max(c, 1LL); i <= min(d, turns * (k + 1)); i++) {
      if (i % (k + 1) == 0)
        printf("B");
      else
        printf("A");
    }
    for (ll i = max(c, turns * (k + 1) + 1);
         i <= min(d, turns * (k + 1) + cnta); i++)
      printf("A");
    for (ll i = max(c, turns * (k + 1) + cnta + 1);
         i <= min(d, turns * (k + 1) + cnta + cntb); i++)
      printf("B");
    for (ll i = max(c, turns * (k + 1) + cnta + cntb + 1); i <= min(s, d); i++)
      if ((s - i) % (k + 1) != k)
        printf("B");
      else
        printf("A");
    puts("");
  }
  return 0;
}


Fixed submission:
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define MAXN 100005
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
ll q, a, b, c, d, k;
bool check(ll turns) {
  if (b < turns || a < k * turns)
    return false;
  ll restb = b - turns;
  ll resta = a - turns * k;
  if (restb >= k * (resta + 1))
    return false;
  else
    return true;
}
int main() {
  scanf("%lld", &q);
  while (q--) {
    scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
    ll s = a + b;
    k = (max(a, b) - 1) / (min(a, b) + 1) + 1;
    ll l = 0, r = (a - 1) / k + 2;
    while (r - l > 1) {
      ll mid = (l + r) / 2;
      if (check(mid))
        l = mid;
      else
        r = mid;
    }
    ll turns = l;
    a -= turns * k;
    b -= turns;
    ll cnta = 0, cntb = 0;
    cnta = a - (b - 1) / k;
    a -= cnta;
    cntb = b - k * a;
    b -= cntb;
    assert(b == k * a);
    for (ll i = max(c, 1LL); i <= min(d, turns * (k + 1)); i++) {
      if (i % (k + 1) == 0)
        printf("B");
      else
        printf("A");
    }
    for (ll i = max(c, turns * (k + 1) + 1);
         i <= min(d, turns * (k + 1) + cnta); i++)
      printf("A");
    for (ll i = max(c, turns * (k + 1) + cnta + 1);
         i <= min(d, turns * (k + 1) + cnta + cntb); i++)
      printf("B");
    for (ll i = max(c, turns * (k + 1) + cnta + cntb + 1); i <= min(s, d); i++)
      if ((s - i) % (k + 1) != k)
        printf("B");
      else
        printf("A");
    puts("");
  }
  return 0;
}