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

Buggy submission (status: Runtime Error):
// https://atcoder.jp/contests/agc020/tasks/agc020_b
// B - Ice Rink Game
#include <bits/stdc++.h>
#if LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
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 main() {
#if LOCAL & 01
  std::ifstream in("./test/sample-1.in");
  // std::ifstream in("./input.txt");
  std::cin.rdbuf(in.rdbuf());
#else
  cin.tie(0);
  ios::sync_with_stdio(false);
#endif
  ll K;
  cin >> K;
  vector<ll> ak(K);
  for (auto &&a : ak) {
    cin >> a;
  }
  // 反対から
  ll min = 2, max = 3;
  assert(ak.back() == 2);
  for (ll i = ak.size() - 1; i >= 0; --i) {
    ll a = ak.at(i);
    ll lv = (min + a - 1) / a;
    min = lv * a;
    ll rv = max / a;
    max = rv * a + a - 1;
    if (min > max || min <= 0 || max <= 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << min << " " << max << endl;
  return 0;
}

Fixed submission:
// https://atcoder.jp/contests/agc020/tasks/agc020_b
// B - Ice Rink Game
#include <bits/stdc++.h>
#if LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
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 main() {
#if LOCAL & 01
  std::ifstream in("./test/sample-1.in");
  // std::ifstream in("./input.txt");
  std::cin.rdbuf(in.rdbuf());
#else
  cin.tie(0);
  ios::sync_with_stdio(false);
#endif
  ll K;
  cin >> K;
  vector<ll> ak(K);
  for (auto &&a : ak) {
    cin >> a;
  }
  // 反対から
  ll min = 2, max = 3;
  // assert(ak.back()==2);
  if (ak.back() != 2) {
    cout << -1 << endl;
    return 0;
  }
  for (ll i = ak.size() - 1; i >= 0; --i) {
    ll a = ak.at(i);
    ll lv = (min + a - 1) / a;
    min = lv * a;
    ll rv = max / a;
    max = rv * a + a - 1;
    if (min > max || min <= 0 || max <= 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << min << " " << max << endl;
  return 0;
}
Problem p03464 (C++)

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)

using ll = long long;
ll INF = LLONG_MAX;

using vc = vector<char>;
using vi = vector<int>;

int K;
vi arr;

ll rem(ll start) {
  for (int item : arr) {
    start = start / item * 1LL * item;
  }
  return start;
}

ll find(int end) {
  ll lo = 0;
  ll hi = 1e20;
  while (lo < hi) {
    ll mid = lo + (hi - lo + 1) / 2;
    if (rem(mid) <= end)
      lo = mid;
    else
      hi = mid - 1;
  }
  return lo;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> K;
  arr.resize(K);
  F0R(i, K) cin >> arr[i];
  ll l = find(1) + 1;
  ll h = find(2);
  if (rem(h) == 2) {
    cout << l << " " << h << endl;
  } else {
    cout << -1 << endl;
  }
}

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)

using ll = long long;
ll INF = LLONG_MAX;

using vc = vector<char>;
using vi = vector<int>;

int K;
vi arr;

ll rem(ll start) {
  for (int item : arr) {
    start = start / item * 1LL * item;
  }
  return start;
}

ll find(int end) {
  ll lo = 0;
  ll hi = 1e15;
  while (lo < hi) {
    ll mid = lo + (hi - lo + 1) / 2;
    if (rem(mid) <= end)
      lo = mid;
    else
      hi = mid - 1;
  }
  return lo;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> K;
  arr.resize(K);
  F0R(i, K) cin >> arr[i];
  ll l = find(1) + 1;
  ll h = find(2);
  if (rem(h) == 2) {
    cout << l << " " << h << endl;
  } else {
    cout << -1 << endl;
  }
}
Problem p03464 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define itn int
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrep1(i, n) for (int i = (int)(n); i >= 1; i--)
#define all(vec) vec.begin(), vec.end()
#define sortt(vec) sort((vec).begin(), (vec).end())
#define rsort(vec) sort((vec).rbegin(), (vec).rend())
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 2e9;
const ll lnf = 9e18;

ll kiri(ll a, ll b) { return a % b == 0 ? a / b : a / b + 1; }

int main() {
  itn n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  reverse(all(a));
  if (a[0] != 2) {
    cout << -1 << endl;
    return 0;
  }
  ll l = 2, r = 3;
  rep1(i, n) {
    l = a[i] * kiri(l, a[i]);
    r = a[i] * (r / a[i]) + a[i] - 1;
    if (l > r) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << l << " " << r << endl;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define itn int
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrep1(i, n) for (int i = (int)(n); i >= 1; i--)
#define all(vec) vec.begin(), vec.end()
#define sortt(vec) sort((vec).begin(), (vec).end())
#define rsort(vec) sort((vec).rbegin(), (vec).rend())
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 2e9;
const ll lnf = 9e18;

ll kiri(ll a, ll b) { return a % b == 0 ? a / b : a / b + 1; }

int main() {
  itn n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  reverse(all(a));
  if (a[0] != 2) {
    cout << -1 << endl;
    return 0;
  }
  ll l = 2, r = 3;
  rep(i, n) {
    l = a[i] * kiri(l, a[i]);
    r = a[i] * (r / a[i]) + a[i] - 1;
    if (l > r) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << l << " " << r << endl;
}
Problem p03464 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
long long m, a, b, n, res, k, g;
long long gru[10000];
long long sma, lar;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> k;
  for (int i = 0; i < k; i++) {
    cin >> gru[i];
  }
  sma = lar = 2;
  for (int i = k - 1; i >= 0; i--) {
    g = gru[i];
    if (sma % g != 0)
      sma = sma - sma % g + g;
    lar -= lar % g;
    lar = lar + g - 1;
  }
  if (sma <= lar)
    cout << sma << " " << lar << endl;
  else
    cout << -1 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
long long m, a, b, n, res, k, g;
long long gru[100000];
long long sma, lar;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> k;
  for (int i = 0; i < k; i++) {
    cin >> gru[i];
  }
  sma = lar = 2;
  for (int i = k - 1; i >= 0; i--) {
    g = gru[i];
    if (sma % g != 0)
      sma = sma - sma % g + g;
    lar -= lar % g;
    lar = lar + g - 1;
  }
  if (sma <= lar)
    cout << sma << " " << lar << endl;
  else
    cout << -1 << endl;
  return 0;
}
Problem p03464 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
#define ull long long

using namespace std;

int main() {
  int K, tmp;
  ull tmpl, tmpu;
  ull max = 2LL, min = 2LL;
  cin >> K;
  vector<ull> vect(K);

  for (int i = 0; i < K; i++) {
    cin >> vect[i];
  }
  if (vect[K - 1] != 2LL) {
    cout << "-1" << endl;
  } else {

    for (int i = K - 1; i >= 0; i++) {
      /*          if (max+1 - min < vect[i]){
                    cout << "-1" << endl;
                    return 0;
                }else{*/
      tmpl = min / vect[i];
      tmpu = max / vect[i];
      if (tmpl * vect[i] != min &&
          ((tmpl + 1) * vect[i] > max || (tmpl + 1) * vect[i] < min)) {
        cout << -1 << endl;
        break;
      }
      if (tmpl * vect[i] != min) {
        min = (tmpl + 1) * vect[i];
      }
      max = tmpu * vect[i] + vect[i] - 1;
      if (i == 0) {
        cout << min << " " << max << endl;
      }
    }
  }

  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
#define ull long long

using namespace std;

int main() {
  int K, tmp;
  ull tmpl, tmpu;
  ull max = 2LL, min = 2LL;
  cin >> K;
  vector<ull> vect(K);

  for (int i = 0; i < K; i++) {
    cin >> vect[i];
  }
  if (vect[K - 1] != 2LL) {
    cout << "-1" << endl;
  } else {

    for (int i = K - 1; i >= 0; i--) {
      tmpl = min / vect[i];
      tmpu = max / vect[i];
      if (tmpl * vect[i] != min &&
          ((tmpl + 1) * vect[i] > max || (tmpl + 1) * vect[i] < min)) {
        cout << -1 << endl;
        break;
      }
      if (tmpl * vect[i] != min) {
        min = (tmpl + 1) * vect[i];
      }
      max = tmpu * vect[i] + vect[i] - 1;
      if (i == 0) {
        cout << min << " " << max << endl;
      }
    }
  }

  return 0;
}
Problem p03464 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define pdi pair<db, int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define eps 1e-8
#define mo 974711
#define MAXN 2005
// #define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template <class T> void read(T &res) {
  res = 0;
  char c = getchar();
  T f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}
int K;
int64 A[MAXN];
int64 check(int64 x) {
  for (int i = 1; i <= K; ++i) {
    x -= x % A[i];
  }
  return x;
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  read(K);
  for (int i = 1; i <= K; ++i)
    read(A[i]);
  int64 L = 2, R = 1e18;
  while (L < R) {
    int64 mid = (L + R) >> 1;
    if (check(mid) >= 2)
      R = mid;
    else
      L = mid + 1;
  }
  if (check(R) != 2) {
    puts("-1");
    return 0;
  }
  out(R);
  space;
  R = 1e18;
  while (L < R) {
    int64 mid = (L + R + 1) >> 1;
    if (check(mid) <= 2)
      L = mid;
    else
      R = mid - 1;
  }
  out(R);
  enter;
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define pdi pair<db, int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define eps 1e-8
#define mo 974711
#define MAXN 100005
// #define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template <class T> void read(T &res) {
  res = 0;
  char c = getchar();
  T f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}
int K;
int64 A[MAXN];
int64 check(int64 x) {
  for (int i = 1; i <= K; ++i) {
    x -= x % A[i];
  }
  return x;
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  read(K);
  for (int i = 1; i <= K; ++i)
    read(A[i]);
  int64 L = 2, R = 1e18;
  while (L < R) {
    int64 mid = (L + R) >> 1;
    if (check(mid) >= 2)
      R = mid;
    else
      L = mid + 1;
  }
  if (check(R) != 2) {
    puts("-1");
    return 0;
  }
  out(R);
  space;
  R = 1e18;
  while (L < R) {
    int64 mid = (L + R + 1) >> 1;
    if (check(mid) <= 2)
      L = mid;
    else
      R = mid - 1;
  }
  out(R);
  enter;
}
Problem p03464 (C++)

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

int main() {
  long long a[N], x = 2, y = 2, z, w;
  int k;
  //    bool ok=1;
  cin >> k;
  for (int i = 0; i < k; i++) {
    cin >> a[i];
  }
  for (int i = k - 1; i >= 0; i--) {
    z = (x / a[i] + (x % a[i] != 0)) * a[i];
    w = (y / a[i]) * a[i];
    //        cout<<z<<' '<<w<<'\n';
    //        if((z<x||y<z){
    //            ok=0;
    //            break;
    //        }
    x = z;
    y = w + a[i] - 1;
    //        cout<<x<<' '<<y<<'\n';
  }
  if (x < y) {
    cout << x << ' ' << y << '\n';
  } else {
    cout << "-1\n";
  }
}
// 6 8
// 4 7
// 3 5
// 2 3
// 2 2


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;

int main() {
  long long a[N], x = 2, y = 2, z, w;
  int k;
  //    bool ok=1;
  cin >> k;
  for (int i = 0; i < k; i++) {
    cin >> a[i];
  }
  for (int i = k - 1; i >= 0; i--) {
    z = (x / a[i] + (x % a[i] != 0)) * a[i];
    w = (y / a[i]) * a[i];
    //        cout<<z<<' '<<w<<'\n';
    //        if((z<x||y<z){
    //            ok=0;
    //            break;
    //        }
    x = z;
    y = w + a[i] - 1;
    //        cout<<x<<' '<<y<<'\n';
  }
  if (x < y) {
    cout << x << ' ' << y << '\n';
  } else {
    cout << "-1\n";
  }
}
// 6 8
// 4 7
// 3 5
// 2 3
// 2 2
Problem p03464 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

int main(void) {
  ll K;
  vector<ll> A;
  cin >> K;
  for (ll i = 0; i < K; i++) {
    ll a;
    cin >> a;
    A.push_back(a);
  }

  while (A[K - 1] != 2) {
  }

  ll low = 2;
  ll high = 3;
  ll n = 2;

  for (ll i = K - 2; i >= 0; i--) {
    ll nlow = (low + A[i] - 1) / A[i] * A[i];
    if (high < nlow) {
      cout << "-1" << endl;
      return 0;
    }
    low = nlow;
    n = A[i];
    high = high / n * n + n - 1;
  }

  cout << low << " " << high << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

int main(void) {
  ll K;
  vector<ll> A;
  cin >> K;
  for (ll i = 0; i < K; i++) {
    ll a;
    cin >> a;
    A.push_back(a);
  }

  if (A[K - 1] != 2) {
    cout << "-1" << endl;
    return 0;
  }

  ll low = 2;
  ll high = 3;
  ll n = 2;

  for (ll i = K - 2; i >= 0; i--) {
    ll nlow = (low + A[i] - 1) / A[i] * A[i];
    if (high < nlow) {
      cout << "-1" << endl;
      return 0;
    }
    low = nlow;
    n = A[i];
    high = high / n * n + n - 1;
  }

  cout << low << " " << high << endl;
  return 0;
}
Problem p03464 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
long long n, m, i, j, k, t, a, b;
long long high, low;
long long A[10010];
int main() {
  cin >> k;
  for (i = 1; i <= k; i++) {
    cin >> A[i];
  }
  high = 2;
  low = 2;
  for (i = k; i >= 1; i--) {
    if (low > high) {
      cout << -1;
      return 0;
    }
    high = (high / A[i]) * A[i] + (A[i] - 1);
    low = ((low - 1) / A[i] + 1) * A[i];
    if (low > high) {
      cout << -1;
      return 0;
    }
  }
  cout << low << " " << high;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
long long n, m, i, j, k, t, a, b;
long long high, low;
long long A[100010];
int main() {
  cin >> k;
  for (i = 1; i <= k; i++) {
    cin >> A[i];
  }
  high = 2;
  low = 2;
  for (i = k; i >= 1; i--) {
    if (low > high) {
      cout << -1;
      return 0;
    }
    high = (high / A[i]) * A[i] + (A[i] - 1);
    low = ((low - 1) / A[i] + 1) * A[i];
    if (low > high) {
      cout << -1;
      return 0;
    }
  }
  cout << low << " " << high;
  return 0;
}
Problem p03464 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll k, a[100010], mi = 2, ma = 3;
  cin >> k;
  for (int i = 1; i <= k; i++)
    cin >> a[i];
  if (a[k] != 2)
    cout << -1 << endl;
  else {
    for (int i = k - 1; k > 0; k--) {
      if (ma < a[i]) {
        cout << -1 << endl;
        return 0;
      } else
        ma = ma / a[i] * a[i] + a[i] - 1;
      if (mi <= a[i])
        mi = a[i];
      else
        mi = (mi + a[i] - 1) / a[i] * a[i];
      if (mi > ma) {
        cout << -1 << endl;
        return 0;
      }
    }
    cout << mi << " " << ma << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll k, a[100010], mi = 2, ma = 3;
  cin >> k;
  for (int i = 1; i <= k; i++)
    cin >> a[i];
  if (a[k] != 2)
    cout << -1 << endl;
  else {
    for (int i = k - 1; i > 0; i--) {
      if (ma < a[i]) {
        cout << -1 << endl;
        return 0;
      } else
        ma = ma / a[i] * a[i] + a[i] - 1;
      if (mi <= a[i])
        mi = a[i];
      else
        mi = (mi + a[i] - 1) / a[i] * a[i];
      if (mi > ma) {
        cout << -1 << endl;
        return 0;
      }
    }
    cout << mi << " " << ma << endl;
  }
  return 0;
}
Problem p03464 (C++)

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

using namespace std;

#define X first
#define Y second
#define ll long long
#define inf (ll)1e18
#define all(x) x.begin(), x.end()

const int MX = 200020;
const int N = 1000060;
const int mod = (int)1e9 + 7;

int main() {
  /*
       ___     ___
      (^_^)   (^_^)
     /( | )\ /( | )\
       | |     | |
  */
  cin.tie(0);
  cin.sync_with_stdio(0);

#ifdef Cyborg101
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int k;
  cin >> k;

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

  ll mn = 2, mx = 2;
  for (int i = k - 1; i >= 0; --i) {
    mn = (mn + a[i] - 1) / a[i] * a[i];
    mx = mx / a[i] * a[i] + a[i] - 1;
    if (mn > mx)
      return 1 - puts("-1");
  }

  cout << mn << " " << mx << endl;

  return 0;
}

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

using namespace std;

#define X first
#define Y second
#define ll long long
#define inf (ll)1e18
#define all(x) x.begin(), x.end()

const int MX = 200020;
const int N = 1000060;
const int mod = (int)1e9 + 7;

int main() {
  /*
       ___     ___
      (^_^)   (^_^)
     /( | )\ /( | )\
       | |     | |
  */
  cin.tie(0);
  cin.sync_with_stdio(0);

#ifdef Cyborg101
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int k;
  cin >> k;

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

  ll mn = 2, mx = 2;
  for (int i = k - 1; i >= 0; --i) {
    mn = (mn + a[i] - 1) / a[i] * a[i];
    mx = mx / a[i] * a[i] + a[i] - 1;
    if (mn > mx)
      return puts("-1"), 0;
  }

  cout << mn << " " << mx << endl;

  return 0;
}
Problem p03464 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define pye cout << "yes" << endl
#define pno cout << "no" << endl
#define pYe cout << "Yes" << endl
#define pNo cout << "No" << endl
#define pYE cout << "YES" << endl
#define pNO cout << "NO" << endl
#define prin(num) cout << num << endl
#define REP(i, n) for (i = 0; i < n; i++)
#define REPR(i, n) for (i = n; i >= 0; i--)
#define FOR(i, m, n) for (i = m; i < n; i++)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define INF 1000000000000000000
#define MOD 1000000007LL
#define MAX_V 100005
#define MAX_E 100005
#define NIL -1
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef priority_queue<P> PQ;

int main(void) {
  ll K;
  cin >> K;
  ll a[100001];
  ll i, j;
  for (i = 1; i <= K; i++) {
    cin >> a[i];
  }
  ll L = 2, R = 2;
  for (i = K - 1; i >= 0; i--) {
    L = (L + a[i] - 1) / a[i] * a[i];
    if (R < L) {
      prin(-1);
      return 0;
    }
    R = (R / a[i] + 1) * a[i] - 1;
  }

  cout << L << " " << R << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define pye cout << "yes" << endl
#define pno cout << "no" << endl
#define pYe cout << "Yes" << endl
#define pNo cout << "No" << endl
#define pYE cout << "YES" << endl
#define pNO cout << "NO" << endl
#define prin(num) cout << num << endl
#define REP(i, n) for (i = 0; i < n; i++)
#define REPR(i, n) for (i = n; i >= 0; i--)
#define FOR(i, m, n) for (i = m; i < n; i++)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define INF 1000000000000000000
#define MOD 1000000007LL
#define MAX_V 100005
#define MAX_E 100005
#define NIL -1
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef priority_queue<P> PQ;

int main(void) {
  ll K;
  cin >> K;
  ll a[100001];
  ll i, j;
  for (i = 0; i < K; i++) {
    cin >> a[i];
  }
  ll L = 2, R = 2;
  for (i = K - 1; i >= 0; i--) {
    L = (L + a[i] - 1) / a[i] * a[i];
    if (R < L) {
      prin(-1);
      return 0;
    }
    R = (R / a[i] + 1) * a[i] - 1;
  }

  cout << L << " " << R << endl;
}
Problem p03464 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

int main() {
  int k;
  cin >> k;
  int a[k];
  ll max = 2;
  ll min = 2;
  for (int i = k - 1; i >= 0; i--) {
    if (max / a[i] * a[i] >= min) {
      if (min % a[i] == 0)
        min = min;
      else
        min = (min / a[i] + 1) * a[i];
      max = max / a[i] * a[i] + a[i] - 1;
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << min << " " << max << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

int main() {
  int k;
  cin >> k;
  int a[k];
  for (int i = 0; i < k; i++) {
    cin >> a[i];
  }
  ll max = 2;
  ll min = 2;
  for (int i = k - 1; i >= 0; i--) {
    if (max / a[i] * a[i] >= min) {
      if (min % a[i] == 0)
        min = min;
      else
        min = (min / a[i] + 1) * a[i];
      max = max / a[i] * a[i] + a[i] - 1;
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << min << " " << max << endl;
  return 0;
}
Problem p03465 (C++)

Buggy submission (status: Memory Limit Exceeded):
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define MAXN 2005
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> P;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef __gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> pq;
int n, s, a[MAXN];
bitset<2000 * 2000> bs[MAXN];
int main() {
  scanf("%d", &n);
  s = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    s += a[i];
  }
  bs[0].set(0);
  for (int i = 1; i <= n; i++)
    bs[i] = bs[i - 1] | (bs[i - 1] << a[i]);
  for (int i = (s + 1) / 2; i <= s; i++) {
    if (bs[n][i]) {
      printf("%d\n", i);
      return 0;
    }
  }
  return 0;
}


Fixed submission:
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define MAXN 2005
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> P;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef __gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> pq;
int n, s, a[MAXN];
bitset<2000 * 1001> bs[MAXN];
int main() {
  scanf("%d", &n);
  s = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    s += a[i];
  }
  bs[0].set(0);
  for (int i = 1; i <= n; i++)
    bs[i] = bs[i - 1] | (bs[i - 1] << a[i]);
  for (int i = (s + 1) / 2; i <= s; i++) {
    if (bs[n][i]) {
      printf("%d\n", i);
      return 0;
    }
  }
  return 0;
}
Problem p03465 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[100000];
bitset<1000010> f;
int main() {
  long long a, c, j, e = 0, b, i, k = 0;
  scanf("%d\n", &a);
  f[0] = 1;
  for (i = 1; i <= a; i++) {
    scanf("%d", &j);
    f |= f << j;
    k += j;
  }
  for (i = (k + 1) / 2; !f[i]; i++)
    ;
  cout << i;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bitset<4000010> f;
int main() {
  long long a, c, j, e = 0, b, i, k = 0;
  scanf("%d\n", &a);
  f[0] = 1;
  for (i = 1; i <= a; i++) {
    scanf("%d", &j);
    f |= f << j;
    k += j;
  }
  for (i = (k + 1) / 2; !f[i]; i++)
    ;
  cout << i;
  return 0;
}
Problem p03465 (C++)

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

int main() {
  cin >> N;
  A.resize(N);
  int sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    sum += A[i];
  }

  vector<bool> dp(sum + 1, false);
  dp[0] = true;
  for (auto a : A) {
    for (int i = sum; i > 0; i--) {
      if (i >= a)
        dp[i] = dp[i] | dp[i - a];
    }
  }
  for (int i = (sum + 1) / 2;; i++) {
    if (dp[i]) {
      cout << i << endl;
      return 0;
    }
  }
}

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

int main() {
  cin >> N;
  A.resize(N);
  int sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    sum += A[i];
  }

  bitset<2000 * 2000 + 1> dp(1);
  for (auto a : A)
    dp = dp | (dp << a);

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

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

using namespace std;

typedef long long ll;

int N, A[2005], pref[2005];

bitset<4000005> num[2];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    cin >> A[i];
    pref[i] = pref[i - 1] + A[i];
  }
  num[0][0] = 1;
  for (int i = 1; i <= N; ++i) {
    num[i & 1] = (num[(i - 1) & 1] | (num[(i - 1) & 1] << A[i]));
  }
  for (int i = (pref[N] + 1) / 2; i <= pref[N]; ++i) {
    if (num[i][N & 1]) {
      cout << i << '\n';
      cout << flush;
      // for (int i = 0; i < 1e9; ++i){for (int j = 0; j < 1e9; ++j){}}
      return 0;
    }
  }
}

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

using namespace std;

typedef long long ll;

int N, A[2005], pref[2005];

bitset<4000005> num[2];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    cin >> A[i];
    pref[i] = pref[i - 1] + A[i];
  }
  num[0][0] = 1;
  for (int i = 1; i <= N; ++i) {
    num[i & 1] = (num[(i - 1) & 1] | (num[(i - 1) & 1] << A[i]));
  }
  for (int i = (pref[N] + 1) / 2; i <= pref[N]; ++i) {
    if (num[N & 1][i]) {
      cout << i << '\n';
      cout << flush;
      // for (int i = 0; i < 1e9; ++i){for (int j = 0; j < 1e9; ++j){}}
      return 0;
    }
  }
}
Problem p03465 (C++)

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;

template <class T, class U> void chmax(T &a, U b) {
  if (a < b)
    a = b;
}
template <class T, class U> void chmin(T &a, U b) {
  if (b < a)
    a = b;
}

#define rep(i, j) for (int i = 0; i < j; i++)
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
#define debug(x) cerr << #x << ": " << x << endl;
#define int ll

const ll mod = 1e9 + 7;
const ll inf = (1LL << 50);

void solve(long long N, std::vector<long long> A) {
  int sum = 0;
  rep(i, N) sum += A[i];

  bitset<10000> bits;
  bits[0] = 1;
  rep(i, N) { bits |= (bits << A[i]); }

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

signed main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;

  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}


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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;

template <class T, class U> void chmax(T &a, U b) {
  if (a < b)
    a = b;
}
template <class T, class U> void chmin(T &a, U b) {
  if (b < a)
    a = b;
}

#define rep(i, j) for (int i = 0; i < j; i++)
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
#define debug(x) cerr << #x << ": " << x << endl;
#define int ll

const ll mod = 1e9 + 7;
const ll inf = (1LL << 50);

void solve(long long N, std::vector<long long> A) {
  int sum = 0;
  rep(i, N) sum += A[i];

  bitset<2020 * 2020> bits;
  bits[0] = 1;
  rep(i, N) { bits |= (bits << A[i]); }

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

signed main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;

  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}
Problem p03465 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, nr, sum = 0;
  cin >> n;
  bitset<400006> bit(1);
  for (i = 1; i <= n; i++) {
    cin >> nr;
    bit |= (bit << nr);
    sum += nr;
  }
  for (i = sum / 2 + sum % 2; i <= sum; i++) {
    if (bit[i] == 1) {
      cout << i << '\n';
      break;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, nr, sum = 0;
  cin >> n;
  bitset<4000006> bit(1);
  for (i = 1; i <= n; i++) {
    cin >> nr;
    bit |= (bit << nr);
    sum += nr;
  }
  for (i = sum / 2 + sum % 2; i <= sum; i++) {
    if (bit[i] == 1) {
      cout << i << '\n';
      break;
    }
  }
  return 0;
}
Problem p03465 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<char, int> P;
typedef complex<double> comp;
typedef vector<vector<ld>> matrix;
struct pairhash {
public:
  template <typename T, typename U>
  size_t operator()(const pair<T, U> &x) const {
    size_t seed = hash<T>()(x.first);
    return hash<U>()(x.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  }
};
const int inf = 1e9 + 9;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);

int n;
int a[55];

int solve() {
  int s = 0;
  for (int i = 0; i < n; i++) {
    s += a[i];
  }
  bitset<4000100> b;
  b[0] = 1;
  for (int i = 0; i < n; i++) {
    b |= (b << a[i]);
  }
  int t = (s + 1) / 2;
  while (true) {
    if (b.test(t)) {
      return t;
    }
    t++;
  }
  return -1;
}

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);

  input();
  cout << solve() << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<char, int> P;
typedef complex<double> comp;
typedef vector<vector<ld>> matrix;
struct pairhash {
public:
  template <typename T, typename U>
  size_t operator()(const pair<T, U> &x) const {
    size_t seed = hash<T>()(x.first);
    return hash<U>()(x.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  }
};
const int inf = 1e9 + 9;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);

int n;
int a[2010];

int solve() {
  int s = 0;
  for (int i = 0; i < n; i++) {
    s += a[i];
  }
  bitset<4000100> b;
  b[0] = 1;
  for (int i = 0; i < n; i++) {
    b |= (b << a[i]);
  }
  int t = (s + 1) / 2;
  while (true) {
    if (b.test(t)) {
      return t;
    }
    t++;
  }
  return -1;
}

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);

  input();
  cout << solve() << endl;
}
Problem p03465 (C++)

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

using namespace std;

#define endl '\n'

int main() {
#ifdef jcg
  assert(freopen("input.in", "r", stdin));
  // assert(freopen("output.out", "w", stdout));
#else
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);

  using BitSet = bitset<2000 * 2000 / 2>;
  BitSet dp;
  dp.set(0);

  int N;
  cin >> N;

  int sum = 0;
  for (int i = 0; i < N; ++i) {
    int X;
    cin >> X;
    dp |= (dp << X);
    sum += X;
  }

  sum = (sum + 1) / 2;
  while (!dp.test(sum))
    ++sum;

  cout << sum << endl;

  return 0;
}

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

using namespace std;

#define endl '\n'

int main() {
#ifdef jcg
  assert(freopen("input.in", "r", stdin));
  // assert(freopen("output.out", "w", stdout));
#else
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);

  using BitSet = bitset<2000 * 2000>;
  BitSet dp;
  dp.set(0);

  int N;
  cin >> N;

  int sum = 0;
  for (int i = 0; i < N; ++i) {
    int X;
    cin >> X;
    dp |= (dp << X);
    sum += X;
  }

  sum = (sum + 1) / 2;
  while (!dp.test(sum))
    ++sum;

  cout << sum << endl;

  return 0;
}
Problem p03465 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, sum, a[2020];
bitset<404040> dp, yl;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  dp |= 1;
  for (int i = 0; i < n; i++) {
    dp |= (dp << a[i]);
  }
  for (int i = 0; i <= sum; i++) {
    if (dp[i] && i >= (sum + 1) / 2) {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, sum, a[2020];
bitset<4040404> dp, yl;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  dp |= 1;
  for (int i = 0; i < n; i++) {
    dp |= (dp << a[i]);
  }
  for (int i = 0; i <= sum; i++) {
    if (dp[i] && i >= (sum + 1) / 2) {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}
Problem p03465 (C++)

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

using namespace std;

typedef long long ll;

int max(int a, int b) { return (a > b) ? a : b; }

const int MAXN = 2000 * 1000 + 1;

const int INF = 1e9;

vector<int> a;

bool check(vector<int> &a, ll x) {
  for (int i = 0; i < a.size(); i++) {
    x = x / a[i] * a[i];
  }
  return (x == 2);
}

bitset<MAXN> can = 0;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  int k;
  cin >> n;
  a.resize(n);

  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  //    vector<int> b;
  //    set<int> myset;
  int all = sum;
  sum /= 2;

  can.set(0);
  //    cerr << sum;
  //    sort(a.begin(), a.end());
  //    reverse(a.begin(), a.end());
  for (int k = 0; k < n; k++) {
    for (int x = sum - a[k]; x >= 0; x--) {
      if (can.test(x))
        can.set(x + a[k]);
    }
  }

  for (int i = sum; i >= 0; i--) {
    if (can.test(i)) {
      cout << all - i;
      return 0;
    }
  }

  return 0;
}

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

using namespace std;

typedef long long ll;

int max(int a, int b) { return (a > b) ? a : b; }

const int MAXN = 2000 * 1000 + 1;

const int INF = 1e9;

vector<int> a;

bool check(vector<int> &a, ll x) {
  for (int i = 0; i < a.size(); i++) {
    x = x / a[i] * a[i];
  }
  return (x == 2);
}

bitset<MAXN> can = 0;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  int k;
  cin >> n;
  a.resize(n);

  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  //    vector<int> b;
  //    set<int> myset;
  int all = sum;
  sum /= 2;

  can.set(0);
  //    cerr << sum;
  //    sort(a.begin(), a.end());
  //    reverse(a.begin(), a.end());
  for (int k = 0; k < n; k++) {
    can |= can << a[k];
  }

  for (int i = sum; i >= 0; i--) {
    if (can.test(i)) {
      cout << all - i;
      return 0;
    }
  }

  return 0;
}
Problem p03465 (C++)

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

std::bitset<200010> lty;

int main() {
  int n;
  int tot = 0;
  scanf("%d", &n);
  lty[0] = 1;
  for (int i = 1; i <= n; i++) {
    int tmp;
    scanf("%d", &tmp);
    lty |= lty << tmp;
    tot += tmp;
  }

  for (int i = (tot + 1) / 2; i <= tot; i++) {
    if (lty[i]) {
      printf("%d\n", i);
      exit(0);
    }
  }
  return 0;
}

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

std::bitset<2000010> lty;

int main() {
  int n;
  int tot = 0;
  scanf("%d", &n);
  lty[0] = 1;
  for (int i = 1; i <= n; i++) {
    int tmp;
    scanf("%d", &tmp);
    lty |= lty << tmp;
    tot += tmp;
  }

  for (int i = (tot + 1) / 2; i <= tot; i++) {
    if (lty[i]) {
      printf("%d\n", i);
      exit(0);
    }
  }
  return 0;
}
Problem p03465 (C++)

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

int N;
int A[2010];
int memo[2100000];
int sum[2010];
int res;

void solve() {
  int i, j, k, l;
  sort(A, A + N);
  sum[N] = 0;
  for (i = N - 1; i >= 0; i--) {
    sum[i] = sum[i + 1] + A[i];
  }
  memo[0] = 1;
  k = 0;
  for (i = 0; i < N; i++) {
    l = sum[0] / 2 + 1 - sum[i];
    if (l < 0)
      l = 0;
    for (j = k; j >= l; j--) {
      if (memo[j] == 1) {
        memo[j + A[i]] = 1;
        k = j + A[i];
        break;
      }
    }
    for (; j >= l; j--) {
      if (memo[j] == 1) {
        memo[j + A[i]] = 1;
      }
    }
  }
  if (sum[0] % 2 == 1)
    l = sum[0] / 2 + 1;
  else
    l = sum[0] / 2;
  for (i = l; i <= sum[0]; i++) {
    if (memo[i] == 1) {
      res = i;
      break;
    }
  }
  printf("%d\n", res);
}

void test1() {
  int i;
  N = 2000;
  for (i = 0; i < N; i++)
    A[i] = rand() % 2000 + 1;
  solve();
}

void test() {
  int i;
  for (i = 0; i < 20; i++)
    test1();
}

int main() {
  // test();
  int i;
  scanf("%d", &N);
  for (i = 0; i < N; i++)
    scanf("%d", &A[i]);
  solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int N;
int A[2010];
int memo[2100000];
int sum[2010];
int res;

void solve() {
  int i, j, k, l;
  sort(A, A + N);
  sum[N] = 0;
  for (i = N - 1; i >= 0; i--) {
    sum[i] = sum[i + 1] + A[i];
  }
  memo[0] = 1;
  k = 0;
  for (i = 0; i < N; i++) {
    l = sum[0] / 2 + 1 - sum[i];
    if (l < 0)
      l = 0;
    for (j = k; j >= l; j--) {
      if (memo[j] == 1) {
        memo[j + A[i]] = 1;
        if (j + A[i] <= sum[0] / 2 + 1)
          k = j + A[i];
        break;
      }
    }
    for (; j >= l; j--) {
      if (memo[j] == 1) {
        memo[j + A[i]] = 1;
      }
    }
  }
  if (sum[0] % 2 == 1)
    l = sum[0] / 2 + 1;
  else
    l = sum[0] / 2;
  for (i = l; i <= sum[0]; i++) {
    if (memo[i] == 1) {
      res = i;
      break;
    }
  }
  printf("%d\n", res);
}

void test1() {
  int i;
  N = 2000;
  for (i = 0; i < N; i++)
    A[i] = rand() % 2000 + 1;
  solve();
}

void test() {
  int i;
  for (i = 0; i < 20; i++)
    test1();
}

int main() {
  // test();
  int i;
  scanf("%d", &N);
  for (i = 0; i < N; i++)
    scanf("%d", &A[i]);
  solve();
  return 0;
}
Problem p03465 (C++)

Buggy submission (status: Runtime Error):
#include <bitset>
#include <stdio.h>

std::bitset<4000400> b;

int main() {
  int N, a[2020], s, i;
  for (scanf("%d", &N), b[0] = 1, s = i = 0; i != N; i++)
    scanf("%d", &a + i), s += a[i], b |= b << a[i];
  for (i = s >> 1; !b[i]; i--)
    ;
  printf("%d\n", s - i);
}

Fixed submission:
#include <bitset>
#include <stdio.h>

std::bitset<4000400> b;

int main() {
  int N, a[2020], s, i;
  for (scanf("%d", &N), b[0] = 1, s = i = 0; i != N; i++)
    scanf("%d", a + i), s += a[i], b |= b << a[i];
  for (i = s >> 1; !b[i]; i--)
    ;
  printf("%d\n", s - i);
}
Problem p03465 (C++)

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

#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(v) int(v.size())
#define pii pair<int, int>
#define mp make_pair
#define f first
#define ll long long
#define ld long double
#define s second
#define vec vector<int>

using namespace std;

const int N = (int)1e5 + 10;
const int M = (int)2018 * 2018;
const int K = (int)500 + 10;
const int INF = (int)1e9 + 7;
const int mod = (int)1e9 + 7;
const ld EPS = (ld)1e-9;
const ll LINF = (ll)1e18;

int n;
bitset<N> dp;

int main() {
#ifdef sony
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  srand(time(0));
  cin >> n;
  dp[0] = 1;
  int sum = 0;
  for (int i = 1, x; i <= n; i++) {
    cin >> x;
    sum += x;
    dp |= dp << x;
  }
  for (int i = (sum + 1) / 2;; i++) {
    if (dp[i]) {
      cout << i;
      return 0;
    }
  }
  return 0;
}

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

#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(v) int(v.size())
#define pii pair<int, int>
#define mp make_pair
#define f first
#define ll long long
#define ld long double
#define s second
#define vec vector<int>

using namespace std;

const int N = (int)1e5 + 10;
const int M = (int)2018 * 2018;
const int K = (int)500 + 10;
const int INF = (int)1e9 + 7;
const int mod = (int)1e9 + 7;
const ld EPS = (ld)1e-9;
const ll LINF = (ll)1e18;

int n;
bitset<M> dp;

int main() {
#ifdef sony
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  srand(time(0));
  cin >> n;
  dp[0] = 1;
  int sum = 0;
  for (int i = 1, x; i <= n; i++) {
    cin >> x;
    sum += x;
    dp |= dp << x;
  }
  for (int i = (sum + 1) / 2;; i++) {
    if (dp[i]) {
      cout << i;
      return 0;
    }
  }
  return 0;
}
Problem p03465 (C++)

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

using namespace std;

bitset<1000010> dp, aux;

int main() {
  //   freopen ("file.in", "r", stdin);

  int n;
  scanf("%d", &n);

  dp[0] = 1;

  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    sum += x;

    aux = dp;
    aux <<= x;
    dp |= aux;
  }

  for (int i = (sum + 1) / 2;; ++i)
    if (dp[i]) {
      printf("%d\n", i);
      return 0;
    }

  return 0;
}


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

using namespace std;

bitset<4000010> dp, aux;

int main() {
  //   freopen ("file.in", "r", stdin);

  int n;
  scanf("%d", &n);

  dp[0] = 1;

  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    sum += x;

    aux = dp;
    aux <<= x;
    dp |= aux;
  }

  for (int i = (sum + 1) / 2;; ++i)
    if (dp[i]) {
      printf("%d\n", i);
      return 0;
    }

  return 0;
}