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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long dp[200][200], sum[200][200];
long long a[200];
long long n, l, r;
int main() {
  for (int i = 0; i <= 200; i++) {
    sum[0][i] = 0;
    sum[i][0] = 1;
    sum[i][i] = 1;
  }
  for (int i = 0; i <= 200; i++) {
    for (int j = 0; j <= 200; j++) {
      dp[i][j] = -1;
    }
  }
  dp[0][0] = 0;
  cin >> n >> l >> r;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    dp[i][0] = 0;
    for (int j = 1; j <= i && j <= r; j++) {
      long long x = dp[i - 1][j];
      long long y = dp[i - 1][j - 1] + a[i];
      if (x == y) {
        dp[i][j] = x;
        sum[i][j] = sum[i - 1][j] + sum[i - 1][j - 1];
      } else if (x > y) {
        dp[i][j] = x;
        sum[i][j] = sum[i - 1][j];
      } else {
        dp[i][j] = y;
        sum[i][j] = sum[i - 1][j - 1];
      }
    }
  }
  int pos = 0;
  long long k = 0;
  for (int i = l; i <= r && i <= n; i++) {
    if (pos == 0) {
      pos = i;
      k = sum[n][i];
    } else if (dp[n][i] * pos > dp[n][pos] * i) {
      pos = i;
      k = sum[n][i];
    } else if (dp[n][i] * pos == dp[n][pos] * i) {
      k += sum[n][i];
    }
  }
  printf("%.6f\n", dp[n][pos] * 1.0 / pos * 1.0);
  cout << k << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long dp[300][300], sum[300][300];
long long a[300];
long long n, l, r;
int main() {
  for (int i = 0; i <= 200; i++) {
    sum[0][i] = 0;
    sum[i][0] = 1;
    sum[i][i] = 1;
  }
  for (int i = 0; i <= 200; i++) {
    for (int j = 0; j <= 200; j++) {
      dp[i][j] = -1;
    }
  }
  dp[0][0] = 0;
  cin >> n >> l >> r;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    dp[i][0] = 0;
    for (int j = 1; j <= i && j <= r; j++) {
      long long x = dp[i - 1][j];
      long long y = dp[i - 1][j - 1] + a[i];
      if (x == y) {
        dp[i][j] = x;
        sum[i][j] = sum[i - 1][j] + sum[i - 1][j - 1];
      } else if (x > y) {
        dp[i][j] = x;
        sum[i][j] = sum[i - 1][j];
      } else {
        dp[i][j] = y;
        sum[i][j] = sum[i - 1][j - 1];
      }
    }
  }
  int pos = 0;
  long long k = 0;
  for (int i = l; i <= r && i <= n; i++) {
    if (pos == 0) {
      pos = i;
      k = sum[n][i];
    } else if (dp[n][i] * pos > dp[n][pos] * i) {
      pos = i;
      k = sum[n][i];
    } else if (dp[n][i] * pos == dp[n][pos] * i) {
      k += sum[n][i];
    }
  }
  printf("%.6f\n", dp[n][pos] * 1.0 / pos * 1.0);
  cout << k << endl;
  return 0;
}
Problem p03777 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <string>
#include <vector>

using namespace std;
// template for creating 2d vector
template <typename T>
vector<vector<T>> make_2d_vector(size_t rows, size_t cols, T init) {
  return vector<vector<T>>(rows, vector<T>(cols, init));
}

#define debug(x) cout << #x << "==" << x << endl;

const int inf = 100000000;

typedef long long ll;

int main() {
  ios::sync_with_stdio(false);

  vector<int> hoge(1e8);

  char a, b;
  cin >> a >> b;
  if (a == b) {
    cout << "H" << endl;
  } else {
    cout << "D" << endl;
  }

  return 0;
}


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

using namespace std;
// template for creating 2d vector
template <typename T>
vector<vector<T>> make_2d_vector(size_t rows, size_t cols, T init) {
  return vector<vector<T>>(rows, vector<T>(cols, init));
}

#define debug(x) cout << #x << "==" << x << endl;

const int inf = 100000000;

typedef long long ll;

int main() {
  ios::sync_with_stdio(false);

  vector<bool> hoge(1e7); // 39296 KB

  char a, b;
  cin >> a >> b;
  if (a == b) {
    cout << "H" << endl;
  } else {
    cout << "D" << endl;
  }

  return 0;
}
Problem p03777 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <string>
#include <vector>

using namespace std;
// template for creating 2d vector
template <typename T>
vector<vector<T>> make_2d_vector(size_t rows, size_t cols, T init) {
  return vector<vector<T>>(rows, vector<T>(cols, init));
}

#define debug(x) cout << #x << "==" << x << endl;

const int inf = 100000000;

typedef long long ll;

int main() {
  ios::sync_with_stdio(false);

  // vector<int> hoge(1e7);        // 39296 KB
  // vector<bool> hoge(1e7);        // 1536 KB KB
  vector<int> hoge(7 * 1e7); // 39296 KB

  char a, b;
  cin >> a >> b;
  if (a == b) {
    cout << "H" << endl;
  } else {
    cout << "D" << endl;
  }

  return 0;
}


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

using namespace std;
// template for creating 2d vector
template <typename T>
vector<vector<T>> make_2d_vector(size_t rows, size_t cols, T init) {
  return vector<vector<T>>(rows, vector<T>(cols, init));
}

#define debug(x) cout << #x << "==" << x << endl;

const int inf = 100000000;

typedef long long ll;

int main() {
  ios::sync_with_stdio(false);

  // vector<int> hoge(1e7);        // 39296 KB
  // vector<bool> hoge(1e7);        // 1536 KB KB
  vector<int> hoge(5 * 1e7); // 39296 KB

  char a, b;
  cin >> a >> b;
  if (a == b) {
    cout << "H" << endl;
  } else {
    cout << "D" << endl;
  }

  return 0;
}
Problem p03777 (Python)

Buggy submission (status: Runtime Error):
a, b = map(int, input().split())
if a == "H":
    print(b)
else:
    if b == "H":
        print("D")
    else:
        print("H")


Fixed submission:
a, b = input().split()
if a == "H":
    print(b)
else:
    if b == "H":
        print("D")
    else:
        print("H")
Problem p03777 (Python)

Buggy submission (status: Runtime Error):
a, b = list(map(int, input().split()))
if a == "H":
    print(b)
else:
    if b == "H":
        print("D")
    else:
        print("H")


Fixed submission:
a, b = input().split()
if a == "H":
    print(b)
else:
    if b == "H":
        print("D")
    else:
        print("H")
Problem p03777 (C++)

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

int main() {
  char a, b;
  char input[10];
  fgets(input, 10, stdin);
  sscanf(input, "%c%c", &a, &b);
  if ((a == 'H' && b == 'H') || (a == 'D' && b == 'D')) {
    printf("H\n");
  } else if ((a == 'H' && b == 'D') || (a == 'D' && b == 'H')) {
    printf("D\n");
  } else {
    return -1;
  }
  return 0;
}

Fixed submission:
#include <iostream>

int main() {
  char a, b;
  std::cin >> a >> b;
  if ((a == 'H' && b == 'H') || (a == 'D' && b == 'D')) {
    printf("H\n");
  } else if ((a == 'H' && b == 'D') || (a == 'D' && b == 'H')) {
    printf("D\n");
  } else {
    return -1;
  }
  return 0;
}
Problem p03777 (Python)

Buggy submission (status: Runtime Error):
a, b = map(int, input().split())
if a == "H" and b == "H":
    print("H")
elif a == "H" and b == "D":
    print("D")
elif a == "D" and b == "H":
    print("D")
elif a == "D" and b == "D":
    print("H")


Fixed submission:
a, b = map(str, input().split())
if a == "H" and b == "H":
    print("H")
elif a == "H" and b == "D":
    print("D")
elif a == "D" and b == "H":
    print("D")
elif a == "D" and b == "D":
    print("H")
Problem p03777 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int main() {
  // freopen("input.txt", "r", stdin);
  char a, b;
  while (1) {
    getchar();
    scanf("%c %c", &a, &b);
    if (a == 'H' && b == 'H')
      printf("H\n");
    else if (a == 'H' && b == 'D')
      printf("D\n");
    else if (a == 'D' && b == 'H')
      printf("D\n");
    else
      printf("H\n");
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int main() {
  // freopen("input.txt", "r", stdin);
  char a, b;
  scanf("%c %c", &a, &b);
  if (a == 'H' && b == 'H')
    printf("H\n");
  else if (a == 'H' && b == 'D')
    printf("D\n");
  else if (a == 'D' && b == 'H')
    printf("D\n");
  else
    printf("H\n");
  return 0;
}
Problem p03777 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {

  char a, b;
  char res;
  do {
    cin >> a >> b;

  } while (a != 'H' || a != 'D' || b != 'H' || b != 'D');

  if (a == 'H' && b == 'H') {
    res = 'H';
  } else if (a == 'H' && b == 'D') {
    res = 'D';
  } else if (a == 'D' && b == 'D') {
    res = 'H';
  } else {
    res = 'D';
  }

  cout << res << endl;
}

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

int main() {

  char a, b;
  char res;
  do {
    cin >> a >> b;

  } while (a != 'H' && a != 'D' || b != 'H' && b != 'D');

  if (a == 'H' && b == 'H') {
    res = 'H';
  } else if (a == 'H' && b == 'D') {
    res = 'D';
  } else if (a == 'D' && b == 'D') {
    res = 'H';
  } else {
    res = 'D';
  }

  cout << res << endl;
}
Problem p03778 (C++)

Buggy submission (status: Runtime Error):
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
  int n, k, i, t;
  while (scanf("%d %d", n, k, i) == 3) {
    if (k < i) {
      t = k;
      k = i;
      i = t;
    }
    if (k - i > n)
      printf("%d\n", k - i - n);
    else
      printf("0\n");
  }
  return 0;
}


Fixed submission:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
  int n, k, i, t;
  while (scanf("%d %d %d", &n, &k, &i) == 3) {
    if (k < i) {
      t = k;
      k = i;
      i = t;
    }
    if (k - i > n)
      printf("%d\n", k - i - n);
    else
      printf("0\n");
  }
  return 0;
}
Problem p03778 (Python)

Buggy submission (status: Runtime Error):
w, a, b = map(int, input().split())

if b > a + w:
    print(b - a - w)
elif b + w < a:
    print(a - b - w)
else:
    print(0)
w, a, b = map(int, input().split())

if b > a + w:
    print(b - a - w)
elif b + w < a:
    print(a - b - w)
else:
    print(0)


Fixed submission:
w, a, b = map(int, input().split())

if b > a + w:
    print(b - a - w)
elif b + w < a:
    print(a - b - w)
else:
    print(0)
Problem p03778 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
int main() {
  int W, a, b;
  int a_max, a_min, b_max, b_min, ans = 0;
  char input[10];
  fgets(input, 10, stdin);
  sscanf(input, "%d%d%d", &W, &a, &b);

  if (W < 1 || a < 1 || b < 1) {
    return -1;
  }
  a_max = a + W;
  a_min = a;
  b_max = b + W;
  b_min = b;
  if (2 <= b_max && b_max < a_min) {
    ans = a_min - b_max;
  } else if (2 <= a_max && a_max < b_min) {
    ans = b_min - a_max;
  } else {
    ans = 0;
  }
  printf("%d\n", ans);

  return 0;
}

Fixed submission:
#include <iostream>
int main() {
  int W, a, b;
  int a_max, a_min, b_max, b_min, ans = 0;
  std::cin >> W >> a >> b;

  if (W < 1 || a < 1 || b < 1) {
    return -1;
  }
  a_max = a + W;
  a_min = a;
  b_max = b + W;
  b_min = b;
  if (2 <= b_max && b_max < a_min) {
    ans = a_min - b_max;
  } else if (2 <= a_max && a_max < b_min) {
    ans = b_min - a_max;
  } else {
    ans = 0;
  }
  printf("%d\n", ans);

  return 0;
}
Problem p03778 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define all(cont) cont.begin(), cont.end()
#define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define fore(i, a, b) for (int i = a, almo5t = b; i < almo5t; ++i)
#define SZ(x) ((int)x.size())
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define FIN                                                                    \
  std::ios_base::sync_with_stdio(false);                                       \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

const int N = 0;

typedef long long int ll;

using namespace std;

ll solve(ll w, ll a, ll b) {
  int res;
  int c = min(a, b);
  int d = max(a, b);
  c += w;
  res = d - c < 0 ? 0 : d - c;
  return res;
}

int main() {
  FIN;
#ifndef ONLINE_JUDGE
  freopen("../in.txt", "r", stdin);
  freopen("../out.txt", "w", stdout);
#endif
  ll w, a, b;
  cin >> w >> a >> b;
  ll out = solve(w, a, b);
  cout << out;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define all(cont) cont.begin(), cont.end()
#define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define fore(i, a, b) for (int i = a, almo5t = b; i < almo5t; ++i)
#define SZ(x) ((int)x.size())
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define FIN                                                                    \
  std::ios_base::sync_with_stdio(false);                                       \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

const int N = 0;

typedef long long int ll;

using namespace std;

ll solve(ll w, ll a, ll b) {
  int res;
  int c = min(a, b);
  int d = max(a, b);
  c += w;
  res = d - c < 0 ? 0 : d - c;
  return res;
}

int main() {
  ll w, a, b;
  cin >> w >> a >> b;
  ll out = solve(w, a, b);
  cout << out;
  return 0;
}
Problem p03779 (Python)

Buggy submission (status: Runtime Error):
import numpy as np

X = int(input())
arr = np.arange(1, 10)
arr = arr * (arr + 1) / 2
# print(arr)
# print(arr >= X)
ans = np.nonzero(arr >= X)[0].min() + 1
print(ans)


Fixed submission:
import numpy as np

X = int(input())
arr = np.arange(1, 100000)
arr = arr * (arr + 1) / 2
# print(arr)
# print(arr >= X)
ans = np.nonzero(arr >= X)[0].min() + 1
print(ans)
Problem p03779 (C++)

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

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

// 最大公約数
long gcd(long a, long b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}

// 最小公倍数
long lcm(long a, long b) { return (a * b) / gcd(a, b); }

typedef long long ll;

int l[100000001];

int main() {
  int x;

  cin >> x;

  x = abs(x);

  int t;

  FOR(i, 1, x + 1) {
    l[i] = l[i - 1] + i;
    /*if (l[i] >= x){
            t = i;
            break;
    }*/
  }

  cout << t << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

// 最大公約数
long gcd(long a, long b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}

// 最小公倍数
long lcm(long a, long b) { return (a * b) / gcd(a, b); }

typedef long long ll;

int l[100000001];

int main() {
  int x;

  cin >> x;

  x = abs(x);

  int t;

  FOR(i, 1, x + 1) {
    l[i] = l[i - 1] + i;
    if (l[i] >= x) {
      t = i;
      break;
    }
  }

  cout << t << endl;

  return 0;
}
Problem p03779 (Python)

Buggy submission (status: Runtime Error):
n, m = map(int, input().split())
print([2, 1][m > 1])


Fixed submission:
x = int(input())
ans = i = 0
while i < x:
    ans += 1
    i += ans
print(ans)
Problem p03779 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const ll mod = (ll)1e9 + 7;
const ll INF = 0x3f3f3f3f;

void add(ll &x, ll y) {
  x += y;
  if (x >= mod) {
    x -= mod;
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("A.txt", "r", stdin);
#endif
  int n, c = 0;
  cin >> n;
  for (int i = 1;; i++) {
    c += i;
    if (c >= n) {
      cout << i;
      break;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const ll mod = (ll)1e9 + 7;
const ll INF = 0x3f3f3f3f;

void add(ll &x, ll y) {
  x += y;
  if (x >= mod) {
    x -= mod;
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
// freopen("A.txt", "r", stdin);
#endif
  int n, c = 0;
  cin >> n;
  for (int i = 1;; i++) {
    c += i;
    if (c >= n) {
      cout << i;
      break;
    }
  }
}
Problem p03779 (C++)

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

using namespace std;

int main() {
  int a;
  cin >> a;
  int buf = max(1, (int)floor(sqrt(a * 2)) - 10);
  int smallest = 0;
  while (true) {
    if ((buf * (buf + 1)) / 2 >= a) {
      smallest = buf;
      break;
    }
  }
  cout << smallest << endl;
}


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

using namespace std;

int main() {
  int a;
  cin >> a;
  int buf = max(1, (int)floor(sqrt(a * 2)) - 10);
  int smallest = 0;
  while (true) {
    if ((buf * (buf + 1)) / 2 >= a) {
      smallest = buf;
      break;
    }
    buf++;
  }
  cout << smallest << endl;
}
Problem p03779 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-indexed昇順

int main() {
  ll X;
  cin >> X;
  ll i = 1;
  for (; i < X; i++) {
    ll sum = i * (i + 1) / 2;
    if (sum == X) {
      cout << i << endl;
      return 0;
    } else if (sum > X) {
      break;
    }
  }

  ll msb = 0;
  for (ll bit = 1; bit < 1 << i; bit++) {
    ll sum = 0;
    for (ll k = 0; k < i; k++) {
      if (bit & 1 << k) {
        sum += k + 1;
        msb = k + 1;
      }
    }
    if (sum == X) {
      break;
    }
  }
  cout << msb << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-indexed昇順

int main() {
  ll X;
  cin >> X;
  ll i = 1;
  for (; i < X; i++) {
    ll sum = i * (i + 1) / 2;
    if (sum == X) {
      cout << i << endl;
      return 0;
    } else if (sum > X) {
      break;
    }
  }
  cout << i << endl;
}
Problem p03779 (C++)

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

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}
template <class T> void operator>>(istream &ist, vector<T> &vs) {
  for (auto &e : vs)
    cin >> e;
}

typedef long long ll;
int const inf = 1 << 29;

int main() {

  ll X;
  cin >> X;
  for (int i = 1;; i++) {
    if (2 * X <= i * (i + 1)) {
      auto wa = (i * (i + 1) / 2);
      assert(i < X - wa);
      std::cout << i << "\n";
      return 0;
    }
  }

  return 0;
}

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

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}
template <class T> void operator>>(istream &ist, vector<T> &vs) {
  for (auto &e : vs)
    cin >> e;
}

typedef long long ll;
int const inf = 1 << 29;

int main() {

  ll X;
  cin >> X;
  for (int i = 1;; i++) {
    if (2 * X <= i * (i + 1)) {
      auto K = i * (i + 1) / 2;
      assert(K - X < i);
      std::cout << i << "\n";
      return 0;
    }
  }

  return 0;
}
Problem p03779 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
using ll = long long;

int main() {
  ll x;
  cin >> x;
  map<ll, int> mp;
  mp[0] = 0;
  for (int i = 1;; i++) {
    map<ll, int> tmp;
    for (auto it : mp) {
      ll y = it.first;
      tmp[y] = i;
      if (y + i == x or y - i == x)
        return !printf("%d\n", i);
      tmp[y + i] = i;
      tmp[y - i] = i;
    }
    mp = tmp;
  }
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;

int main() {
  ll x;
  cin >> x;
  ll n = 0;
  while (n * (n + 1) / 2 <= x)
    n++;
  if (n * (n - 1) / 2 == x)
    n--;
  cout << n << endl;
}
Problem p03779 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const char wall = '#';
vector<vector<char>> fields;
vector<vector<bool>> used;
vector<vector<ll>> dist;
const int dx[4] = {0, -1, 0, 1};
const int dy[4] = {1, 0, -1, 0};

vector<ll> A;
// vectorのリサイズ
// A.resize(N);

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return b / gcd(a, b) * b; }
ll ans = 0;

void bfs(ll t, ll x) {
  queue<P> q;
  q.push({0, 0});
  while (!q.empty()) {
    P end = q.front();
    q.pop();
    if (end.first - (end.second + 1) == x) {
      ans = end.second + 1;
      break;
    }
    q.push({end.first - (end.second + 1), end.second + 1});

    if (end.first == x) {
      ans = end.second + 1;
      break;
    }
    q.push({end.first, end.second + 1});

    if (end.first + (end.second + 1) == x) {
      ans = end.second + 1;
      break;
    }
    q.push({end.first + (end.second + 1), end.second + 1});
  }
}

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

  ll x;
  cin >> x;
  bfs(0, x);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const char wall = '#';
vector<vector<char>> fields;
vector<vector<bool>> used;
vector<vector<ll>> dist;
const int dx[4] = {0, -1, 0, 1};
const int dy[4] = {1, 0, -1, 0};

vector<ll> A;
// vectorのリサイズ
// A.resize(N);

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return b / gcd(a, b) * b; }
ll ans = 0;

void bfs(ll t, ll x) {
  queue<P> q;
  q.push({0, 0});
  while (!q.empty()) {
    P end = q.front();
    q.pop();
    if (end.first - (end.second + 1) == x) {
      ans = end.second + 1;
      break;
    }
    q.push({end.first - (end.second + 1), end.second + 1});

    if (end.first == x) {
      ans = end.second + 1;
      break;
    }
    q.push({end.first, end.second + 1});

    if (end.first + (end.second + 1) == x) {
      ans = end.second + 1;
      break;
    }
    q.push({end.first + (end.second + 1), end.second + 1});
  }
}

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

  ll x;
  cin >> x;
  cout << ceil((-1 + sqrt(1 + 8 * x)) / 2.0) << endl;
  return 0;
}
Problem p03779 (C++)

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

using namespace std;

const int MOD = 1e9 + 7;

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

  long long csum = 0, x;
  cin >> x;

  for (int i = 1; i <= x; ++i) {
    int nr = i * (i + 1) / 2;
    for (int j = 0; j <= csum; ++j)
      if (nr - j == x) {
        cout << i;
        return 0;
      }
    csum += i;
  }
  return 0;
}


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

using namespace std;

const int MOD = 1e9 + 7;

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

  long long csum = 0, x;
  cin >> x;

  for (int i = 1; i <= x; ++i) {
    if (i * (i + 1) / 2 >= x) {
      cout << i;
      return 0;
    }
  }
  return 0;
}
Problem p03779 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)

int main() {
  LL X;
  cin >> X;

  LL N = (LL)(-1 + round(sqrt(1 + 8 * X))) / 2;

  while (true) {
    if (N * (N + 1) == 2 * X) {
      cout << N << endl;
      return 0;
    }

    if (N * (N + 1) < 2 * X && (N + 1) * (N + 2) > 2 * X) {
      cout << N + 1 << endl;
      return 0;
    }

    N++;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)

int main() {
  LL X;
  cin >> X;

  LL N = (LL)(-1 + round(sqrt(1 + 8 * X))) / 2;

  while (true) {
    if (N * (N + 1) == 2 * X) {
      cout << N << endl;
      return 0;
    }

    if (N * (N + 1) < 2 * X && (N + 1) * (N + 2) > 2 * X) {
      cout << N + 1 << endl;
      return 0;
    }

    if (N * (N + 1) < 2 * X) {
      N++;
    }

    if (N * (N + 1) > 2 * X) {
      N--;
    }
  }

  return 0;
}
Problem p03779 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<ll, ll, ll> T;

int x;

int main(void) {

  scanf("%d", &x);

  vector<int> v;
  v.push_back(0);
  int sum = 0;
  while (v.back() <= x) {
    ++sum;
    v.push_back(v.back() + sum);
  }

  int ans = INT_MAX;
  for (int i = 0; i < v.size(); ++i) {
    if (v[i] == x)
      ans = min(ans, v[i]);
    for (int n = i + 1; v[i] + n <= x; ++n)
      if ((v[i] + n) == x)
        ans = min(ans, n);
  }
  printf("%d\n", ans);

  return 0;
}

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

int x;

int main(void) {

  scanf("%d", &x);

  vector<int> v;
  v.push_back(0);
  int sum = 0;
  while (v.back() <= x) {
    ++sum;
    v.push_back(v.back() + sum);
  }

  for (int i = 0; i < v.size(); ++i)
    if (x <= v[i]) {
      printf("%d\n", i);
      return 0;
    }

  return 0;
}
Problem p03779 (C++)

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

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

  int x;
  cin >> x;
  vector<int> t(x + 1);
  for (int i = 0; i < x + 1; ++i) {
    t[i] = i;
  }

  for (int i = 1; i < x + 1; ++i) {
    t[i] += t[i - 1];
    if (t[i] > x) {
      cout << i << endl;
      return 0;
    }
  }
}

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

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

  int x;
  cin >> x;
  for (int t = 1; t < x + 1; ++t) {
    if (t * (t + 1) / 2 >= x) {
      cout << t << endl;
      return 0;
    }
  }
}
Problem p03779 (C++)

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

int main() {
  long long x;
  cin >> x;
  long long i = floor(sqrt(x));
  long long sum = i * (i + 1) / 2;
  while (!(sum < x && x <= sum + i + 1)) {
    sum += ++i;
  }
  cout << i + 1 << endl;
  return 0;
}

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

int main() {
  long long x;
  cin >> x;
  long long i = floor(sqrt(x)) - 1;
  long long sum = i * (i + 1) / 2;
  while (!(sum < x && x <= sum + i + 1)) {
    sum += ++i;
  }
  cout << i + 1 << endl;
  return 0;
}
Problem p03779 (C++)

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

int main() {
  int X;
  cin >> X;
  int sum = 0;
  for (int i = 0;; i++) {
    sum += i;
    if (sum >= X) {
      cout << i << endl;
    }
  }
}

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

int main() {
  int X;
  cin >> X;
  int sum = 0;
  for (int i = 0;; i++) {
    sum += i;
    if (sum >= X) {
      cout << i << endl;
      break;
    }
  }
}
Problem p03779 (C++)

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

#include <math.h>
#include <stdlib.h>

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

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

// const int INF = 1001001001;
// long long max => LLONG_MAX

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

  int x;
  cin >> x;
  set<int> st;
  set<int> st2;
  st.insert(0);
  int cnt = 0;

  while (st.count(x) == 0) {
    cnt++;
    for (auto i : st) {
      st2.insert(i);
      st2.insert(i + cnt);
      st2.insert(i - cnt);
    }
    swap(st2, st);
  }
  cout << cnt << endl;

  return 0;
}


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

#include <math.h>
#include <stdlib.h>

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

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

// const int INF = 1001001001;
// long long max => LLONG_MAX

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

  int x;
  cin >> x;
  int ans = 0;
  while (ans * (ans + 1) < 2 * x)
    ans++;
  cout << ans << endl;
  return 0;
}
Problem p03780 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define l_ength size
const int inf = (1 << 30);
const int mod = 1000000007;
using ll = long long;
using namespace std;

const int bound = 5050;
bool dp[bound][bound];

void init() {
  for (int i = 0; i < bound; ++i) {
    for (int j = 0; j < bound; ++j) {
      dp[i][j] = false;
    }
  }
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (auto &k : a)
    cin >> k;
  sort(a.begin(), a.end());
  int ok = 0, ng = n;
  int target;
  while (abs(ok - ng) > 1) {
    init();
    dp[0][0] = true;
    target = (ok + ng) / 2;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j <= 5000; ++j) {
        dp[i + 1][j] = dp[i][j];
        if (i == target)
          continue;
        if (j >= a[i])
          dp[i + 1][j] |= dp[i][j - a[i]];
      }
    }
    bool flag = false;
    for (int j = k - a[target]; j < k; ++j) {
      if (dp[n][j])
        flag = true;
    }
    if (flag) {
      ng = target;
    } else {
      ok = target;
    }
  }
  cout << ok + 1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define l_ength size
const int inf = (1 << 30);
const int mod = 1000000007;
using ll = long long;
using namespace std;

const int bound = 5050;
bool dp[bound][bound];

void init() {
  for (int i = 0; i < bound; ++i) {
    for (int j = 0; j < bound; ++j) {
      dp[i][j] = false;
    }
  }
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (auto &k : a)
    cin >> k;
  sort(a.begin(), a.end());
  int ok = -1, ng = lower_bound(a.begin(), a.end(), k) - a.begin();
  int target;
  while (abs(ok - ng) > 1) {
    init();
    dp[0][0] = true;
    target = (ok + ng) / 2;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j <= 5000; ++j) {
        dp[i + 1][j] = dp[i][j];
        if (i == target)
          continue;
        if (j >= a[i])
          dp[i + 1][j] |= dp[i][j - a[i]];
      }
    }
    bool flag = false;
    for (int j = k - a[target]; j < k; ++j) {
      if (dp[n][j])
        flag = true;
    }
    if (flag) {
      ng = target;
    } else {
      ok = target;
    }
  }
  cout << ok + 1 << endl;
}
Problem p03780 (C++)

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

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

#define MAX 5000

int n, k;
ll a[MAX];

bool need(int p) {
  vector<ll> v;
  v.push_back(0);
  for (int i = 0; i < n; i++) {
    if (i != p)
      v.push_back(a[i]);
  }

  bool b[n][k + 1];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= k; j++) {
      if (j == 0)
        b[i][j] = true;
      else
        b[i][j] = false;
    }
  }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= k; j++) {
      b[i][j] = b[i - 1][j];
    }
    for (int j = 0; j <= k; j++) {
      if (j + v[i] <= k) {
        b[i][j + v[i]] |= b[i - 1][j];
      }
    }
  }

  for (int j = k - a[p]; j < k; j++) {
    if (b[n - 1][j])
      return true;
  }
  return false;
}

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

  int l = -1, r = n;
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (need(m))
      r = m;
    else
      l = m;
  }
  cout << r << endl;
  return 0;
}

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

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

#define MAX 5000

int n, k;
ll a[MAX];

bool need(int p) {
  vector<ll> v;
  v.push_back(0);
  for (int i = 0; i < n; i++) {
    if (i != p)
      v.push_back(a[i]);
  }

  bool b[n][k + 1];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= k; j++) {
      if (j == 0)
        b[i][j] = true;
      else
        b[i][j] = false;
    }
  }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= k; j++) {
      b[i][j] = b[i - 1][j];
    }
    for (int j = 0; j <= k; j++) {
      if (j + v[i] <= k) {
        b[i][j + v[i]] |= b[i - 1][j];
      }
    }
  }

  for (int j = max(k - a[p], 0ll); j < k; j++) {
    if (b[n - 1][j])
      return true;
  }
  return false;
}

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

  int l = -1, r = n;
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (need(m))
      r = m;
    else
      l = m;
  }
  cout << r << endl;
  return 0;
}
Problem p03780 (C++)

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

using namespace std;

using ll = long long;

ll a[5000];
bool dp[5001][5000];

int main() {
  ll N, K;

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

  sort(a, a + N);

  int l, r, m;

  l = 0;
  r = N - 1;

  /*
      for (int i = 0; i < N;i++)
      {
          cout << a[i] << " ";
      }
      cout << endl;
  */
  while (true) {
    for (int i = 0; i < 5000; i++) {
      for (int j = 0; j < 5000; j++) {
        dp[i][j] = false;
      }
    }
    dp[0][0] = true;

    m = (r + l) / 2;

    // cout << "l = " << l << " m = " << m  << " r = " << r << endl;

    for (int x = 1; x <= N; x++) {
      for (int y = 0; y < K; y++) {
        if (x == m + 1 || y - a[x - 1] < 0) {
          dp[x][y] = dp[x - 1][y];
        } else {
          dp[x][y] = (dp[x - 1][y - a[x - 1]] || dp[x - 1][y]);
        }
      }
    }

    bool t = false;

    for (int y = K - a[m]; y < K; y++) {
      if (y == 10) {
        // cout << "dp : " << dp[N][y] << endl;
      }
      if (dp[N][y] == true) {
        // cout << "y = " << y << endl;
        t = true;
        break;
      }
    }

    if (l >= r) {
      if (t == true) {
        cout << m << endl;
      } else {
        cout << m + 1 << endl;
      }
      break;
    }

    if (t == true) {
      r = m - 1;
    } else {
      l = m + 1;
    }
  }

  return 0;
}

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

using namespace std;

using ll = long long;

ll a[5000];
bool dp[5001][5000];

int main() {
  ll N, K;

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

  sort(a, a + N);

  int l, r, m;

  l = 0;
  r = N - 1;

  /*
      for (int i = 0; i < N;i++)
      {
          cout << a[i] << " ";
      }
      cout << endl;
  */
  while (true) {
    for (int i = 0; i < 5000; i++) {
      for (int j = 0; j < 5000; j++) {
        dp[i][j] = false;
      }
    }
    dp[0][0] = true;

    m = (r + l) / 2;

    // cout << "l = " << l << " m = " << m  << " r = " << r << endl;

    for (int x = 1; x <= N; x++) {
      for (int y = 0; y < K; y++) {
        if (x == m + 1 || y - a[x - 1] < 0) {
          dp[x][y] = dp[x - 1][y];
        } else {
          dp[x][y] = (dp[x - 1][y - a[x - 1]] || dp[x - 1][y]);
        }
      }
    }

    bool t = false;

    for (int y = K - a[m]; y < K; y++) {
      if (y < 0 || dp[N][y] == true) {
        // cout << "y = " << y << endl;
        t = true;
        break;
      }
    }

    if (l >= r) {
      if (t == true) {
        cout << m << endl;
      } else {
        cout << m + 1 << endl;
      }
      break;
    }

    if (t == true) {
      r = m - 1;
    } else {
      l = m + 1;
    }
  }

  return 0;
}
Problem p03780 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define loop(i, n) for (int i = 0; i < n; i++)
#define rep(i, x, n) for (int i = x; i <= n; i++)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 5e3 + 5, M = 5e5 + 5, MOD = 1e9 + 7;
int memo[N][N];
int n, arr[N], k;
map<int, int> ind;
vector<int> vec;
bool valid(int j) {
  int x = vec[j];
  int bad = ind[x];
  for (int i = n - 1; ~i; --i) {
    for (int sum = k - 1; ~sum; --sum) {
      if (i == bad || sum + arr[i] >= k) {
        memo[i][sum] = memo[i + 1][sum];
      } else {
        memo[i][sum] = max(memo[i + 1][sum + arr[i]], memo[i + 1][sum]);
      }
    }
  }
  return memo[0][0] >= k - x;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "rt", stdin);
#endif
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  set<int> nums;
  loop(i, n) {
    cin >> arr[i];
    nums.insert(arr[i]);
    ind[arr[i]] = i;
  }
  for (int x : nums) {
    vec.push_back(x);
  }
  loop(j, k + 3) { memo[n][j] = j; }
  int st = 0, en = vec.size() - 1;
  while (st <= en) {
    int mid = (st + en) / 2;
    if (valid(mid)) {
      en = mid - 1;
    } else {
      st = mid + 1;
    }
  }
  vec.push_back(1e9 + 5);
  int ans = 0;
  loop(i, n) {
    if (arr[i] < vec[en + 1])
      ans++;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define loop(i, n) for (int i = 0; i < n; i++)
#define rep(i, x, n) for (int i = x; i <= n; i++)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 5e3 + 5, M = 5e5 + 5, MOD = 1e9 + 7;
int memo[N][N];
int n, arr[N], k;
map<int, int> ind;
vector<int> vec;
bool valid(int j) {
  int x = vec[j];
  int bad = ind[x];
  for (int i = n - 1; ~i; --i) {
    for (int sum = k - 1; ~sum; --sum) {
      if (i == bad || sum + arr[i] >= k) {
        memo[i][sum] = memo[i + 1][sum];
      } else {
        memo[i][sum] = max(memo[i + 1][sum + arr[i]], memo[i + 1][sum]);
      }
    }
  }
  return memo[0][0] >= k - x;
}
int main() {
#ifndef ONLINE_JUDGE
  //  freopen("in.txt", "rt", stdin);
#endif
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  set<int> nums;
  loop(i, n) {
    cin >> arr[i];
    nums.insert(arr[i]);
    ind[arr[i]] = i;
  }
  for (int x : nums) {
    vec.push_back(x);
  }
  loop(j, k + 3) { memo[n][j] = j; }
  int st = 0, en = vec.size() - 1;
  while (st <= en) {
    int mid = (st + en) / 2;
    if (valid(mid)) {
      en = mid - 1;
    } else {
      st = mid + 1;
    }
  }
  vec.push_back(1e9 + 5);
  int ans = 0;
  loop(i, n) {
    if (arr[i] < vec[en + 1])
      ans++;
  }
  cout << ans << endl;
}
Problem p03780 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
typedef unsigned int uint;
using namespace std;

int N, K;
int a[5000];

// a_nを除いたカードで数が作れるか
bool jud(int n) {
  bool dp[5001];
  fill(dp, dp + 5001, false);
  dp[0] = true;
  for (int i = 0; i < N; i++) {
    if (i == n)
      continue;

    for (int j = K; j >= 0; j--) {
      if (dp[j] && j + a[i] <= K) {
        dp[j + a[i]] = true;
      }
    }
  }

  for (int i = max(0, K - a[n]); i < K; i++) {
    if (dp[i]) {
      return true;
    }
  }

  return false;
}

int bs(int a, int b) {
  int p = (a + b) / 2;
  bool j = jud(p);

  if (a == b) {
    if (a == 0 && j)
      return -1;
    else if (a == N - 1 && j == 0)
      return N - 1;
    else if (j)
      return a - 1;
    else if (!j)
      return a;
  } else {
    if (j) {
      return bs(a, p - 1);
    } else {
      return bs(p + 1, b);
    }
  }
}

//======================================================
int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> a[i];
  sort(a, a + N);

  // 一番右の不必要
  cout << bs(0, N - 1) + 1 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
typedef unsigned int uint;
using namespace std;

int N, K;
int a[5000];

// a_nを除いたカードで数が作れるか
bool jud(int n) {
  bool dp[5001];
  fill(dp, dp + 5001, false);
  dp[0] = true;
  for (int i = 0; i < N; i++) {
    if (i == n)
      continue;

    for (int j = K; j >= 0; j--) {
      if (dp[j] && j + a[i] <= K) {
        dp[j + a[i]] = true;
      }
    }
  }

  for (int i = max(0, K - a[n]); i < K; i++) {
    if (dp[i]) {
      return true;
    }
  }

  return false;
}

int bs(int a, int b) {
  int p = (a + b) / 2;
  bool j = jud(p);

  if (a == b) {
    if (a == 0 && j)
      return -1;
    else if (a == N - 1 && j == 0)
      return N - 1;
    else if (j)
      return a - 1;
    else if (!j)
      return a;
  } else {
    if (j) {
      return bs(a, p);
    } else {
      return bs(p + 1, b);
    }
  }
}

//======================================================
int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> a[i];
  sort(a, a + N);

  // 一番右の不必要
  cout << bs(0, N - 1) + 1 << endl;
  return 0;
}
Problem p03780 (C++)

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

using namespace std;

int N, K;
vector<int> a;

bool judge(int m) {
  bool dp[2010] = {false};
  dp[0] = true;
  for (auto i = 0; i < N; i++) {
    if (i == m)
      continue;
    for (auto j = K - a[i] - 1; j >= 0; j--) {
      if (!dp[j])
        continue;
      if (K - a[m] <= j + a[i]) {
        return true;
      }
      dp[j + a[i]] = true;
    }
  }
  return false;
}

int main() {
  int x;

  cin >> N >> K;
  for (auto i = 0; i < N; i++) {
    cin >> x;
    if (x < K) {
      a.push_back(x);
    }
  }
  sort(a.begin(), a.end());

  N = a.size();
  int l, r, m;
  l = -1;
  r = N;
  while (r - l > 1) {
    m = (l + r) / 2;
    if (judge(m)) {
      r = m;
    } else {
      l = m;
    }
  }
  cout << r << endl;
  return 0;
}

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

using namespace std;

int N, K;
vector<int> a;

bool judge(int m) {
  bool dp[5010] = {false};
  dp[0] = true;
  for (auto i = 0; i < N; i++) {
    if (i == m)
      continue;
    for (auto j = K - a[i] - 1; j >= 0; j--) {
      if (!dp[j])
        continue;
      if (K - a[m] <= j + a[i]) {
        return true;
      }
      dp[j + a[i]] = true;
    }
  }
  return false;
}

int main() {
  int x;

  cin >> N >> K;
  for (auto i = 0; i < N; i++) {
    cin >> x;
    if (x < K) {
      a.push_back(x);
    }
  }
  sort(a.begin(), a.end());

  N = a.size();
  int l, r, m;
  l = -1;
  r = N;
  while (r - l > 1) {
    m = (l + r) / 2;
    if (judge(m)) {
      r = m;
    } else {
      l = m;
    }
  }
  cout << r << endl;
  return 0;
}
Problem p03780 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<long> a(n);
  int i, j;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end(), greater<long>());
  vector<set<long>> num(n);
  int tmp = 0;
  vector<bool> chk(n, false);
  for (i = 0; i < n; i++) {
    num[i].insert(a[i]);
    if (a[i] >= k) {
      tmp++;
      chk[i] = true;
    }
  }
  long tmppp = 0;
  for (i = tmp; i < n; i++) {
    tmppp += a[i];
  }
  if (tmppp < k)
    tmp = 99999999;
  for (i = tmp; i < n; i++) {
    for (j = tmp; j < n; j++) {
      if (i != j && !chk[j]) {
        set<long> tmpp;
        for (auto kk : num[j]) {
          if (kk + a[i] >= k && kk + a[i] - a[j] < k)
            chk[j] = true;
          else if (kk + a[i] < k)
            tmpp.insert(kk + a[i]);
        }
        for (auto kk : tmpp)
          num[j].insert(kk);
      }
    }
  }
  int ans = 0;
  for (i = 0; i < n; i++) {
    if (chk[i] == false)
      ans++;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<long> a(n);
  int i, j;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end(), greater<long>());
  vector<set<long>> num(n);
  int tmp = 0;
  vector<bool> chk(n, false);
  for (i = 0; i < n; i++) {
    num[i].insert(a[i]);
    if (a[i] >= k) {
      tmp++;
      chk[i] = true;
    }
  }
  long tmppp = 0;
  for (i = tmp; i < n; i++) {
    tmppp += a[i];
  }
  if (tmppp < k)
    tmp = 99999999;
  long tmppp2 = tmppp;
  for (i = n - 1; i >= tmp; i--) {
    if (tmppp - a[i] < k)
      chk[i] = true;
    if (tmppp2 - a[i] < k)
      chk[i] = true;
    else
      tmppp2 -= a[i];
  }
  for (i = tmp; i < n; i++) {
    for (j = tmp; j < n; j++) {
      if (i != j && !chk[j]) {
        set<long> tmpp;
        for (auto kk : num[j]) {
          if (kk + a[i] >= k && kk + a[i] - a[j] < k)
            chk[j] = true;
          else if (kk + a[i] < k)
            tmpp.insert(kk + a[i]);
        }
        for (auto kk : tmpp)
          num[j].insert(kk);
      }
    }
  }
  int ans = 0;
  for (i = 0; i < n; i++) {
    if (chk[i] == false)
      ans++;
  }
  cout << ans << endl;
}