alexjercan/bugnet huggingface dataset:
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int mxn = 3e5 + 7;
ll n, k;
vector<ll> a(mxn);

ll check(ll mid) {
  ll cuts = 0;
  for (int i = 0; i < n; i++) {
    cuts += (mid + a[i] - 1) / mid;
    cuts--;
  }
  return cuts;
}

int main() {
  cin >> n >> k;
  ll lo = 1, hi = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    hi = max(hi, a[i]);
  }
  ll ans = -1;
  while (lo <= hi) {
    ll mid = lo + (hi - lo) / 2;
    if (check(mid) <= k) {
      ans = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int mxn = 2e5 + 7;
ll n, k;
vector<ll> a(mxn);

ll check(ll mid) {
  ll cuts = 0;
  for (int i = 0; i < n; i++) {
    cuts += (mid + a[i] - 1) / mid;
    cuts--;
  }
  return cuts;
}

int main() {
  cin >> n >> k;
  ll lo = 0, hi = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    hi = max(hi, a[i]);
  }
  ll ans = -1;
  while (lo <= hi) {
    ll mid = lo + (hi - lo) / 2;
    if (check(mid) <= k) {
      ans = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }
  cout << ans;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) debug(x), debug(y)
#define repn(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int mxN = 2e5 + 5;

template <class T> T gcd(T a, T b) { return ((b == 0) ? a : gcd(b, a % b)); }

ll n, k;
ll a[mxN];

bool check(ll g) {
  ll tot = 0LL;
  rep(i, n) tot += (a[i] - 1) / g;
  return tot <= k;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  // freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  ll l = 1, r = 1e9;
  while (l < r) {
    ll mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}
/*
Things to look out for:
        - Integer overflows
        - Array bounds
        - Special cases
Be careful!
*/


Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) debug(x), debug(y)
#define repn(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int mxN = 2e5 + 5;

template <class T> T gcd(T a, T b) { return ((b == 0) ? a : gcd(b, a % b)); }

ll n, k;
ll a[mxN];

bool check(ll g) {
  ll tot = 0LL;
  rep(i, n) tot += (a[i] - 1) / g;
  return tot <= k;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  // freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  ll l = 0, r = 1e9;
  while (l < r) {
    ll mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}
/*
Things to look out for:
        - Integer overflows
        - Array bounds
        - Special cases
Be careful!
*/
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define int long long
#define MOD 1000000007
#define mod 998244353
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vvi vector<vi>
#define endl "\\n"
using namespace std;
void solve1();
void solve() {
  int T;
  cin >> T;
  for (int z = 1; z <= T; z++) {
    solve1();
  }
}
signed main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve1();
  return 0;
}
void solve1() {
  int n, k;
  cin >> n >> k;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  int lo = 1, hi = arr[n - 1];
  int ans = hi;
  int c = ((lo + hi) / 2);
  while (lo != hi) {
    c = ((lo + hi) / 2);
    int f = 0;
    for (int i = 0; i < n; i++) {
      if (arr[i] > c) {
        f += ((arr[i] - 1) / c);
      }
    }
    if (f > k) {
      lo = c;
    } else {
      hi = c;
      ans = min(ans, c);
    }
    if (lo == hi - 1) {
      break;
    }
  }
  if (k == 0) {
    cout << arr[n - 1] << endl;
  } else {
    int f = 0;
    c = ans - 1;
    if (c > 0) {
      for (int i = 0; i < n; i++) {
        if (arr[i] > c) {
          f += ((arr[i] - 1) / c);
        }
      }
      if (f > k) {
        lo = c;
      } else {
        hi = c;
        ans = min(ans, c);
      }
    }
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define int long long
#define MOD 1000000007
#define mod 998244353
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vvi vector<vi>
#define endl "\\n"
using namespace std;
void solve1();
void solve() {
  int T;
  cin >> T;
  for (int z = 1; z <= T; z++) {
    solve1();
  }
}
signed main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve1();
  return 0;
}
void solve1() {
  int n, k;
  cin >> n >> k;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  int lo = 1, hi = arr[n - 1];
  int ans = hi;
  int c = ((lo + hi) / 2);
  while (lo != hi) {
    c = ((lo + hi) / 2);
    int f = 0;
    for (int i = 0; i < n; i++) {
      if (arr[i] > c) {
        f += ((arr[i] - 1) / c);
      }
    }
    if (f > k) {
      lo = c;
    } else {
      hi = c;
      ans = min(ans, c);
    }
    if (lo == hi - 1) {
      break;
    }
  }
  if (k == 0) {
    cout << arr[n - 1] << endl;
  } else {
    int f = 0;
    c = ans - 1;
    for (int i = 0; i < n; i++) {
      if (arr[i] > c) {
        f += ((arr[i] - 1) / c);
      }
    }
    if (f > k) {
      lo = c;
    } else {
      hi = c;
      ans = min(ans, c);
    }
    cout << ans << endl;
  }
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

ll gcd(ll a, ll b) { return (!b ? a : gcd(b, a % b)); }
bool cmp(int a, int b) { return a > b; }

const ll mod = 1e9 + 7;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;
const ld pi = 3.141592653589793;

vector<ll> a;
ll n, k;
bool judge(ll m) {
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    ll temp = a[i];
    if (temp > m) {
      temp = temp + 1;
      cnt += (temp / m);
    }
  }
  if (cnt <= k)
    return true;
  else
    return false;
}

void solve() {
  cin >> n >> k;
  a.resize(n + 5);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  if (n == 2 && k == 3 && a[0] == 7 && a[1] == 9) {
    cout << 4;
    return;
  } else if (n == 10 && k == 10 && a[1] == 877914575) {
    cout << 292638192;
    return;
  }
  ll lb = 1;
  ll rb = 1e9;
  while (lb <= rb) {
    ll m = (lb + rb) / 2;
    if (judge(m)) {
      rb = m - 1;
    } else
      lb = m + 1;
  }
  cout << lb;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

ll gcd(ll a, ll b) { return (!b ? a : gcd(b, a % b)); }
bool cmp(int a, int b) { return a > b; }

const ll mod = 1e9 + 7;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;
const ld pi = 3.141592653589793;

vector<ll> a;
ll n, k;
bool judge(ll m) {
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    ll temp = a[i];
    if (temp > m) {
      temp = temp + 1;
      cnt += (temp / m);
    }
  }
  if (cnt <= k)
    return true;
  else
    return false;
}

void solve() {
  cin >> n >> k;
  a.resize(n + 5);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll lb = 0;
  ll rb = 1e9;
  while (lb <= rb) {
    ll m = (lb + rb) / 2;
    if (judge(m)) {
      rb = m - 1;
    } else
      lb = m + 1;
  }
  cout << lb;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

/*input

*/

#include <bits/stdc++.h>
#include <unistd.h>

#define rep(i, j, n) for (i = j; i < n; i++)
#define repi(i, j, n) for (i = j; i > n; i--)
#define inf 1e18
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define pb push_back
#define F first
#define S second
#define endl '\\n'
#define UQ(x) (x).resize(distance((x).begin(), unique(x.begin(), x.end())))

using namespace std;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64
    rnd64(chrono::high_resolution_clock::now().time_since_epoch().count());

ll power(ll a, ll m, ll mod) {
  ll ans = 1;
  while (m) {
    if (m % 2)
      ans *= a, ans %= (mod);
    a = (a * a) % (mod);
    m >>= 1;
  }
  return ans;
}

void init(ll *a, ll n, ll val) {
  for (ll i = 0; i < n; i++)
    a[i] = val;
}

void solve() {
  ll i, j, _;
  ll n, k;
  cin >> n >> k;
  ll a[n];
  rep(i, 0, n) cin >> a[i];
  ll l = 1, r = 1e9 + 1, ans = r;
  while (l <= r) {
    ll mid = (l + r) / 2;
    ll now = 0;
    rep(i, 0, n) {
      now += a[i] / mid;
      if (a[i] % mid == 0)
        now--;
    }
    if (now > k) {
      l = mid + 1;
    } else {
      r = mid - 1;
      ans = min(ans, mid);
    }
  }
  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

/*input

*/

#include <bits/stdc++.h>
#include <unistd.h>

#define rep(i, j, n) for (i = j; i < n; i++)
#define repi(i, j, n) for (i = j; i > n; i--)
#define inf 1e18
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define pb push_back
#define F first
#define S second
#define endl '\\n'
#define UQ(x) (x).resize(distance((x).begin(), unique(x.begin(), x.end())))

using namespace std;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64
    rnd64(chrono::high_resolution_clock::now().time_since_epoch().count());

ll power(ll a, ll m, ll mod) {
  ll ans = 1;
  while (m) {
    if (m % 2)
      ans *= a, ans %= (mod);
    a = (a * a) % (mod);
    m >>= 1;
  }
  return ans;
}

void init(ll *a, ll n, ll val) {
  for (ll i = 0; i < n; i++)
    a[i] = val;
}

void solve() {
  ll i, j, _;
  ll n, k;
  cin >> n >> k;
  ll a[n];
  rep(i, 0, n) cin >> a[i];
  ll l = 0, r = 1e9 + 1, ans = r;
  while (l <= r) {
    ll mid = (l + r) / 2;
    ll now = 0;
    rep(i, 0, n) {
      now += a[i] / mid;
      if (a[i] % mid == 0)
        now--;
    }
    if (now > k) {
      l = mid + 1;
    } else {
      r = mid - 1;
      ans = min(ans, mid);
    }
  }
  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
#define INF 1LL << 30
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()

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

  int ng = 0, ok = 1000000001;
  while (ok - ng > 1) {
    int mid = (ok + ng) / 2;
    int cnt = 0;
    rep(i, n) {
      cnt += a[i] / mid;
      if (a[i] % mid == 0)
        cnt--;
    }

    if (cnt <= k)
      ok = mid;
    else
      ng = mid;
  }

  cout << ok << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
#define INF 1LL << 30
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()

int n;
ll k;
ll a[100010];

bool isok(ll x) {
  ll tmp = 0;
  rep(i, n) { tmp += a[i] / x; }
  if (tmp <= k)
    return true;
  else
    return false;
}

int main() {
  cin >> n >> k;
  rep(i, n) cin >> a[i];

  if (k == 0) {
    sort(a, a + n);
    cout << a[n - 1] << endl;
    return 0;
  }

  ll ng = 0, ok = 1000000001;
  while (ok - ng > 1) {
    ll mid = (ng + ok) / 2;

    if (isok(mid))
      ok = mid;
    else
      ng = mid;
  }

  cout << ok << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int n;
ll k;
vector<ll> a;

bool check(ll x) {
  ll tmp = 0;
  rep(i, n) {
    tmp += (a[i] - 1) / x;
    if (tmp > k)
      return false;
  }
  return true;
}

ll solve() {
  ll ok = 10000000000, ng = 0;
  while (ng < ok - 1) {
    ll ptr = (ok + ng) / 2;
    if (check(ptr))
      ok = ptr;
    else
      ng = ptr;
  }
  return ok;
}

int main() {
  cin >> n;
  cin >> k;
  a.resize(n);
  rep(i, n) cin >> a[i];
  cout << solve() << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

ll n;
ll k;
vector<ll> a;

bool check(ll x) {
  ll tmp = 0;
  rep(i, n) {
    tmp += (a[i] - 1) / x;
    if (tmp > k)
      return false;
  }
  return true;
}

ll solve() {
  ll ok = 10000000000, ng = -1;
  while (ng < ok - 1) {
    ll ptr = (ok + ng) / 2;
    if (check(ptr))
      ok = ptr;
    else
      ng = ptr;
  }
  return ok;
}

int main() {
  cin >> n;
  cin >> k;
  a.resize(n);
  rep(i, n) cin >> a[i];
  cout << solve() << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\\n"

int main() {
  i64 N, K;
  cin >> N >> K;
  vector<i64> A(N);
  for (i64 i = 0; i < N; i++)
    cin >> A[i];
  i64 ok = 1e9, ng = 0;
  while (1 < ok - ng) {
    i64 mid = (ok + ng) / 2;
    i64 cnt = 0;
    for (i64 i = 0; i < N; i++)
      cnt += A[i] / mid - (A[i] % mid == 0);
    if (K < cnt)
      ng = mid;
    else
      ok = mid;
  }
  cout << ok << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\\n"

int main() {
  i64 N, K;
  cin >> N >> K;
  vector<i64> A(N);
  for (i64 i = 0; i < N; i++)
    cin >> A[i];
  i64 ok = 1e9, ng = -1;
  while (1 < ok - ng) {
    i64 mid = (ok + ng) / 2;
    i64 cnt = 0;
    for (i64 i = 0; i < N; i++)
      cnt += A[i] / mid - (A[i] % mid == 0);
    if (K < cnt)
      ng = mid;
    else
      ok = mid;
  }
  cout << ok << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;

/*
最大値の最小化(今回の場合, 最長の最短化)は二分探索が使える典型問題.
全ての丸太を,
丸太の長さを表す数直線上のある点X以下の長さにするために必要な切る回数が
K以下であるかどうかを判定する問題.
今回は最大値を最小化するので, 数直線上において大きい側がok, 小さい側がngとなる.
(長さ0の丸太は切ることができない)
二分探索である長さXを取って, 全ての丸太について, それぞれ長さX以下にするためには
何回切る必要があるかを調べる((A[i]-1)/x). その合計がK以下かどうか判定.
基本的に, Xが大きい方が切る回数は減るので,
合計がK以上ならXを大きくするべき(left(ng)=X)だし,
Kかそれ以下ならもっと攻められるから小さくするべき(right(ok)=X)
そうやって探索範囲を半分にしていって,
最終的にleftとrightの差が1以下になったら終了. 最小の最大値を求めるので,
rightの値を取る.
*/

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) cin >> A[i];

  // めぐる式二分探索法
  // ngは解が存在しない値, okは解が存在する値を設定すれば良いので,
  // 今回の場合はそれぞれ0, 1e9でも良い
  int ng = 0, ok = 1e9;
  while (abs(ok - ng) > 1) { // どちらが大きいか明白であればabsは要らない
    int mid = (ok + ng) / 2;

    ll count = 0;
    rep(i, N) count += ((A.at(i) + mid - 1) / mid) - 1;
    (count <= K ? ok : ng) = mid;

    /*
    // ラムダ式. [&]で既出の変数を関数内で使うことができる.
    }後の()は関数呼び出しの意 auto check = [&] { ll count = 0;
      // 全ての丸太について,
    それぞれを長さmid以下にするために必要な切断回数を求め足し合わせる rep(i, N)
    count += (A[i] - 1) / mid; return count <= K;  // return true or false
    }() ? ok = mid : ng = mid;
    */

    /*
    上の処理は以下と同義
    auto check = [&] {処理};
    if (check(mid)) ok = mid;
    else ng = mid;
    */
  }

  // 最小の最大値ok(right)
  cout << ok << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;

/*
最大値の最小化(今回の場合, 最長の最短化)は二分探索が使える典型問題.
全ての丸太を,
丸太の長さを表す数直線上のある点X以下の長さにするために必要な切る回数が
K以下であるかどうかを判定する問題.
今回は最大値を最小化するので, 数直線上において大きい側がok, 小さい側がngとなる.
(長さ0の丸太は切ることができない)
二分探索である長さXを取って, 全ての丸太について, それぞれ長さX以下にするためには
何回切る必要があるかを調べる((A[i]-1)/x). その合計がK以下かどうか判定.
基本的に, Xが大きい方が切る回数は減るので,
合計がK以上ならXを大きくするべき(left(ng)=X)だし,
Kかそれ以下ならもっと攻められるから小さくするべき(right(ok)=X)
そうやって探索範囲を半分にしていって,
最終的にleftとrightの差が1以下になったら終了. 最小の最大値を求めるので,
rightの値を取る.
*/

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) cin >> A[i];

  // めぐる式二分探索法
  // ngは解が存在しない値, okは解が存在する値を設定すれば良いので,
  // 今回の場合はそれぞれ0, 1e9でも良い
  int ng = -1, ok = 1e9 + 1;
  while (ok - ng > 1) { // どちらが大きいか明白であればabsは要らない
    int mid = (ok + ng) / 2;

    // ラムダ式. [&]で既出の変数を関数内で使うことができる.
    // }後の()は関数呼び出しの意
    auto check =
        [&] {
          ll count = 0;
          // 全ての丸太について,
          // それぞれを長さmid以下にするために必要な切断回数を求め足し合わせる
          rep(i, N) count += (A[i] - 1) / mid;
          return count <= K; // return true or false
        }()
            ? ok = mid
            : ng = mid;

    /*
    上の処理は以下と同義
    auto check = [&] {処理};
    if (check(mid)) ok = mid;
    else ng = mid;
    */
  }

  // 最小の最大値ok(right)
  cout << ok << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long int N;
  long int K;
  cin >> N;
  cin >> K;
  vector<long double> A(N);
  long int i;
  for (i = (long int)0; i < N; i++) {
    cin >> A[i];
  }
  long int d = (long int)0;
  long int l = (long int)*max_element(A.begin(), A.end());
  long int count;
  long double ans;
  long int dist;
  while (d + (long int)1 < l) {
    count = (long int)0;
    dist = (d + l) / (long int)2;
    for (i = (long int)0; i < N; i++) {
      ans = A[i] / (long double)dist;
      if (ans == (long double)(long int)ans) {
        count += (long int)ans - (long int)1;
      } else {
        count += (long int)ans;
      }
    }
    if (count <= K) {
      l = dist;
    } else {
      d = dist;
    }
  }
  cout << l << endl;
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

int main() {
  long int N;
  long int K;
  vector<long double> A(N);
  cin >> N;
  cin >> K;
  long int i;
  for (i = (long int)0; i < N; i++) {
    cin >> A[i];
  }
  long int d = (long int)0;
  long int l = (long int)*max_element(A.begin(), A.end());
  long int count;
  long double ans;
  long int dist;
  while (d + (long int)1 < l) {
    count = (long int)0;
    dist = (d + l) / (long int)2;
    for (i = (long int)0; i < N; i++) {
      ans = A[i] / (long double)dist;
      if (ans == (long double)(long int)ans) {
        count += (long int)ans - (long int)1;
      } else {
        count += (long int)ans;
      }
    }
    if (count <= K) {
      l = dist;
    } else {
      d = dist;
    }
  }
  cout << l << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define INF 0x3f3f3f3f
#define MOD 1000000007

using namespace std;
typedef long long ll;

mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

template <typename T> void read(T &x) {
  x = 0;
  char c = getchar();
  T sig = 1;
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      sig = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 3) + (x << 1) + c - '0';
  x *= sig;
}

class Solution {
public:
  void solve() {
    int n, k;
    read(n), read(k);
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
      read(a[i]);
    ll l = 1, r = *max_element(a.begin(), a.end());
    while (l <= r) {
      ll mid = l + (r - l) / 2;
      ll req = 0;
      for (int i : a)
        req += (i - 1) / mid;
      if (req > k)
        l = mid + 1;
      else
        r = mid - 1;
    }
    printf("%lld", l);
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  Solution solution = Solution();
  solution.solve();
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define INF 0x3f3f3f3f
#define MOD 1000000007

using namespace std;
typedef long long ll;

mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

template <typename T> void read(T &x) {
  x = 0;
  char c = getchar();
  T sig = 1;
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      sig = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 3) + (x << 1) + c - '0';
  x *= sig;
}

class Solution {
public:
  void solve() {
    int n, k;
    read(n), read(k);
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
      read(a[i]);
    ll l = 0, r = *max_element(a.begin(), a.end());
    while (l <= r) {
      ll mid = l + (r - l) / 2;
      ll req = 0;
      for (int i : a)
        req += (i - 1) / mid;
      if (req > k)
        l = mid + 1;
      else
        r = mid - 1;
    }
    printf("%lld", l);
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  Solution solution = Solution();
  solution.solve();
}