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

Buggy submission (status: 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 = 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!
*/


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 = 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!
*/
Problem p02598 (C++)

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

using namespace std;
int arr[200005];

bool isvalid(int m, int n, int k) {
  long long total = 0;
  for (int i = 0; i < n; i++) {
    total += (arr[i]) / m;
    if (arr[i] % m == 0)
      total--;
  }

  if (total <= k)
    return true;
  return false;
}

void solve(int n, int k) {
  int i = 0, j = 1000000001;
  while (i < j) {
    int mid = (i + j) / 2;
    if (isvalid(mid, n, k))
      j = mid;

    else
      i = mid + 1;
  }

  cout << i;
}

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

  for (int i = 0; i < n; i++)
    cin >> arr[i];

  solve(n, k);

  return 0;
}

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

using namespace std;
int arr[200005];

bool isvalid(int m, int n, int k) {
  if (m == 0)
    return false;
  long long total = 0;
  for (int i = 0; i < n; i++) {
    total += (arr[i]) / m;
    if (arr[i] % m == 0)
      total--;
  }

  if (total <= k)
    return true;
  return false;
}

void solve(int n, int k) {
  int i = 0, j = 1000000001;
  while (i < j) {
    int mid = (i + j) / 2;
    if (isvalid(mid, n, k))
      j = mid;

    else
      i = mid + 1;
  }

  cout << i;
}

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

  for (int i = 0; i < n; i++)
    cin >> arr[i];

  solve(n, k);

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<PII> VPI;
typedef vector<PLL> VPL;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

int main() {
  ll N, K;
  cin >> N >> K;
  VL A(N);
  rep(i, N) cin >> A[i];
  sort(all(A));
  ll ok = A[N - 1];
  ll ng = 0;
  rep(qwe, 100) {
    ll mid = (ok + ng) / 2;
    ll sum = 0;
    rep(i, N) sum += (A[i] + mid - 1) / mid - 1;
    if (sum > K)
      ng = mid;
    else
      ok = mid;
  }
  cout << ok << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<PII> VPI;
typedef vector<PLL> VPL;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

int main() {
  ll N, K;
  cin >> N >> K;
  VL A(N);
  rep(i, N) cin >> A[i];
  sort(all(A));
  ll ok = A[N - 1];
  ll ng = 0;
  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    ll sum = 0;
    rep(i, N) sum += (A[i] + mid - 1) / mid - 1;
    if (sum > K)
      ng = mid;
    else
      ok = mid;
  }
  cout << ok << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
/*Always success does not build on success,success build on failure and
 * hardwork*/
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define vec vector<long long>
#define map map<long long, long long>
#define scn(t) scanf("%d", &t);
#define lscn(t) scanf("%lld", &t);
#define mod 1000000007
#define rep(i, a, b) for (long long i = a; i < b; i++)
#define endl "\n"
using namespace std;
const long long N = 2e5 + 5;
long long n, k, arr[N];
bool check(long long mid) {
  long long ans = 0;

  for (long long i = 0; i < n; i++) {
    ans = ans + arr[i] / mid;

    if (arr[i] % mid == 0)
      ans--;
  }

  if (ans <= k)
    return 1;
  else
    return 0;
}
int main() {
  lscn(n);
  lscn(k);

  long long low = 0, high = INT_MAX, mid;

  for (long long i = 0; i < n; i++) {
    lscn(arr[i]);
  }

  while (low < high) {
    mid = (low + high) / 2;

    if (check(mid))
      high = mid;
    else
      low = mid + 1;

    //   cout<<mid<<endl;
  }

  if (check(low))
    cout << low << endl;
  else
    cout << low + 1 << endl;
}

Fixed submission:
/*Always success does not build on success,success build on failure and
 * hardwork*/
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define vec vector<long long>
#define map map<long long, long long>
#define scn(t) scanf("%d", &t);
#define lscn(t) scanf("%lld", &t);
#define mod 1000000007
#define rep(i, a, b) for (long long i = a; i < b; i++)
#define endl "\n"
using namespace std;
const long long N = 2e5 + 5;
long long n, k, arr[N];
bool check(long long mid) {
  long long ans = 0;

  for (long long i = 0; i < n; i++) {
    ans = ans + arr[i] / mid;

    if (arr[i] % mid == 0)
      ans--;
  }

  if (ans <= k)
    return 1;
  else
    return 0;
}
int main() {
  lscn(n);
  lscn(k);

  long long low = 1, high = INT_MAX, mid;

  for (long long i = 0; i < n; i++) {
    lscn(arr[i]);
  }

  while (low < high) {
    mid = (low + high) / 2;

    if (check(mid))
      high = mid;
    else
      low = mid + 1;

    //   cout<<mid<<endl;
  }

  if (check(low))
    cout << low << endl;
  else
    cout << low + 1 << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize("O3")

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

using namespace std;

#define FAST_IO                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(nullptr)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define pb push_back
#define f first
#define s second
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<piii> viii;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 500100, MAXK = 20;
const ll MOD = 1e9 + 7;
const ll INF = 1e16;
const ld PI = asin(1) * 2;

void setIO() { FAST_IO; }

void setIO(string s) {
  setIO();
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

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

ll f(ll x) {
  ll ret = 0;
  FOR(i, 1, n) { ret += ((ll)((a[i] + x - 1) / x) - 1); }
  return ret;
}

int main() {
  setIO();
  // setIO("lmio_1991_3e1_seka_01");

  cin >> n >> k;
  FOR(i, 1, n) cin >> a[i];

  ll le = 0.0, ri = 1e9;
  while (le < ri) {
    ll mid = (le + ri) / 2;
    // cout << " mid=" << mid << "  f = " << f(mid) << endl;
    if (f(mid) > k)
      le = mid + 1;
    else
      ri = mid;
  }

  ll ans = le;
  cout << ans << "\n";

  return 0;
}


Fixed submission:
// #pragma GCC optimize("O3")

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

using namespace std;

#define FAST_IO                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(nullptr)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define pb push_back
#define f first
#define s second
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<piii> viii;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 500100, MAXK = 20;
const ll MOD = 1e9 + 7;
const ll INF = 1e16;
const ld PI = asin(1) * 2;

void setIO() { FAST_IO; }

void setIO(string s) {
  setIO();
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

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

ll f(ll x) {
  ll ret = 0;
  FOR(i, 1, n) { ret += ((ll)((a[i] + x - 1) / x) - 1); }
  return ret;
}

int main() {
  setIO();
  // setIO("lmio_1991_3e1_seka_01");

  cin >> n >> k;
  FOR(i, 1, n) cin >> a[i];

  ll le = 1, ri = 1e9;
  while (le < ri) {
    ll mid = (le + ri) / 2;
    // cout << " mid=" << mid << "  f = " << f(mid) << endl;
    if (f(mid) > k)
      le = mid + 1;
    else
      ri = mid;
  }

  ll ans = le;
  cout << ans << "\n";

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll inf = 1000000000000000000, mod = 1000000007, BS, k;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n, k;
  cin >> n >> k;
  ll arr[n];
  for (ll i = 0; i < n; i++)
    cin >> arr[i];
  ll l = 0, r = 10000000000, mid, sum = 0, mi = inf;
  while (l <= r) {
    mid = (l + r) / 2;
    // cout<<l<<" "<<r<<endl;
    if (l == mid) {
      for (ll i = 0; i < n; i++)
        sum = sum + arr[i] / l + !!(arr[i] % l);
      if (sum <= k)
        mi = min(mi, l);
      sum = 0;
      for (ll i = 0; i < n; i++)
        sum = sum + arr[i] / r + !!(arr[i] % r);
      if (sum <= k)
        mi = min(mi, r);
      break;
    }
    sum = 0;
    for (ll i = 0; i < n; i++)
      sum = sum + arr[i] / mid + !!(arr[i] % mid) - 1;
    // if(mid==4)cout<<sum<<" ";
    if (sum <= k)
      mi = min(mi, mid), r = mid;
    else
      l = mid;
  }
  cout << mi;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll inf = 1000000000000000000, mod = 1000000007, BS, k;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n, k;
  cin >> n >> k;
  ll arr[n];
  for (ll i = 0; i < n; i++)
    cin >> arr[i];
  ll l = 1, r = 10000000000, mid, sum = 0, mi = inf;
  while (l <= r) {
    mid = (l + r) / 2;
    // cout<<l<<" "<<r<<endl;
    if (l == mid) {
      for (ll i = 0; i < n; i++)
        sum = sum + arr[i] / l + !!(arr[i] % l);
      if (sum <= k)
        mi = min(mi, l);
      sum = 0;
      for (ll i = 0; i < n; i++)
        sum = sum + arr[i] / r + !!(arr[i] % r);
      if (sum <= k)
        mi = min(mi, r);
      break;
    }
    sum = 0;
    for (ll i = 0; i < n; i++)
      sum = sum + arr[i] / mid + !!(arr[i] % mid) - 1;
    // if(mid==4)cout<<sum<<" ";
    if (sum <= k)
      mi = min(mi, mid), r = mid;
    else
      l = mid;
  }
  cout << mi;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
#define ROF(i, a, b) for (ll i = a; i >= b; i--)
#define per(i, a) ROF(i, a, 0)
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
using ch = char;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
struct edge {
  ll to, cost;
};
struct edge2 {
  ll from, to, cost;
};

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

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

ll N, K;
vl A;

ll num(ll x) {
  ll res = 0;
  rep(i, N) { res += (A[i] - 1) / x; }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N >> K;
  A.resize(N);
  rep(i, N) { cin >> A[i]; }
  ll ok = 1e9 + 1, ng = -1;
  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    if (num(mid) <= K) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << endl;
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
#define ROF(i, a, b) for (ll i = a; i >= b; i--)
#define per(i, a) ROF(i, a, 0)
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
using ch = char;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
struct edge {
  ll to, cost;
};
struct edge2 {
  ll from, to, cost;
};

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

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

ll N, K;
vl A;

ll num(ll x) {
  ll res = 0;
  rep(i, N) { res += (A[i] - 1) / x; }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N >> K;
  A.resize(N);
  rep(i, N) { cin >> A[i]; }
  ll ok = 1e9 + 1, ng = 0;
  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    if (num(mid) <= K) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << endl;
}
Problem p02598 (C++)

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

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int ans = -1, l = 0, r = (int)1e9;
  auto possible = [&](int x) {
    int cnt = 0;
    for (int len : arr) {
      cnt += (len - 1) / x;
    }
    return cnt <= k;
  };
  while (l <= r) {
    int mid = l + (r - l) / 2;
    if (possible(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans;
}


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

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int ans = -1, l = 1, r = (int)1e9 + 7;
  auto possible = [&](int x) {
    int cnt = 0;
    for (int len : arr) {
      cnt += (len - 1) / x;
    }
    return cnt <= k;
  };
  while (l <= r) {
    int mid = l + (r - l) / 2;
    if (possible(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans;
}
Problem p02598 (C++)

Buggy submission (status: 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 = -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;
}


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 = 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;
}
Problem p02598 (C++)

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

int main() {
  int N, K;
  scanf("%d%d", &N, &K);

  vector<int> A(N);
  for (int i = 0; i < N; ++i)
    scanf("%d", &A[i]);

  int lb = 0;
  int ub = 1 << 30;
  for (int _ = 0; _ < 32; ++_) {
    int mb = (lb + ub) / 2;
    int c = 0;
    for (int i = 0; i < N; ++i) {
      c += (A[i] + mb - 1) / mb - 1;
      if (c > K)
        break;
    }
    (c <= K ? ub : lb) = mb;
  }

  printf("%d\n", ub);

  return 0;
}


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

int main() {
  int N, K;
  scanf("%d%d", &N, &K);

  vector<int> A(N);
  for (int i = 0; i < N; ++i)
    scanf("%d", &A[i]);

  int lb = 0;
  int ub = 1 << 30;
  while (ub - lb > 1) {
    int mb = (lb + ub) / 2;
    int c = 0;
    for (int i = 0; i < N; ++i) {
      c += (A[i] + mb - 1) / mb - 1;
      if (c > K)
        break;
    }
    (c <= K ? ub : lb) = mb;
  }

  printf("%d\n", ub);

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define full(a) a.begin(), a.end()
#define rfull(a) a.rbegin(), a.rend()

using namespace std;
const double pi = acos(-1.0);
const double pii = 2 * pi;
const double eps = 1e-6;
const long long MOD = 1e9 + 7;

bool tf(long long lim, int n, long long k, vector<long long> &l) {
  long long ans = 0;
  for (int i = 0; i < n; i++)
    if (l[i] % lim == 0)
      ans += (l[i] / lim) - 1;
    else
      ans += (l[i] / lim);
  return ans <= k;
}

void solve() {
  int n;
  long long k;
  cin >> n >> k;
  vector<long long> l(n, 0);
  for (int i = 0; i < n; i++)
    cin >> l[i];
  long long ll = 0, rr = MOD, mid;
  while (rr - ll > 1) {
    mid = (ll + rr) / 2;
    if (tf(mid, n, k, l))
      rr = mid;
    else
      ll = mid;
  }
  if (tf(ll, n, k, l))
    rr = ll;
  cout << rr << "\n";
}

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

  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define full(a) a.begin(), a.end()
#define rfull(a) a.rbegin(), a.rend()

using namespace std;
const double pi = acos(-1.0);
const double pii = 2 * pi;
const double eps = 1e-6;
const long long MOD = 1e9 + 7;

bool tf(long long lim, int n, long long k, vector<long long> &l) {
  long long ans = 0;
  for (int i = 0; i < n; i++)
    if (l[i] % lim == 0)
      ans += (l[i] / lim) - 1;
    else
      ans += (l[i] / lim);
  return ans <= k;
}

void solve() {
  int n;
  long long k;
  cin >> n >> k;
  vector<long long> l(n, 0);
  for (int i = 0; i < n; i++)
    cin >> l[i];
  long long ll = 1, rr = MOD, mid;
  while (rr - ll > 1) {
    mid = (ll + rr) / 2;
    if (tf(mid, n, k, l))
      rr = mid;
    else
      ll = mid;
  }
  if (tf(ll, n, k, l))
    rr = ll;
  cout << rr << "\n";
}

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

  solve();
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
/*
Author: Vishal Singh
(singhvishal0304@gmail.com)

"The days that break you are the days that make you."

*/

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

#define fastio()                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

#define md 1000000007

#define lli long long int
#define pb push_back
#define mp make_pair
#define vvi(r, c) vector<vector<lli>> v(r, vector<lli>(c));
#define fi first
#define se second
#define mem(arr, x) memset(arr, x, sizeof(arr))

lli power(lli a, lli b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return ((a % md) * (power(a, b - 1))) % md;
  else {
    lli x = power(a, b / 2);
    return (x * x) % md;
  }
}

lli numCuts(vector<lli> &a, lli mid) {
  int n = a.size();
  lli sum = 0;
  for (int i = 0; i < n; i++) {
    lli x = a[i] / mid;
    if (a[i] % mid)
      x++;
    sum += x - 1;
  }
  return sum;
}
lli bin_search(vector<lli> &a, lli l, lli r, lli k) {
  if (l < r) {
    lli mid = l + (r - l) / 2;
    if (numCuts(a, mid) > k)
      return bin_search(a, mid + 1, r, k);
    return bin_search(a, l, mid, k);
  }
  return l;
}

void solve() {
  lli n, k;
  cin >> n >> k;
  vector<lli> a(n);
  lli r = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    r = max(r, a[i]);
  }
  lli ans = bin_search(a, 0, r, k);
  cout << ans << endl;
}

int main() {
  fastio();
  int t;

  t = 1;
  while (t--) {
    solve();
  }

  return 0;
}

Fixed submission:
/*
Author: Vishal Singh
(singhvishal0304@gmail.com)

"The days that break you are the days that make you."

*/

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

#define fastio()                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

#define md 1000000007

#define lli long long int
#define pb push_back
#define mp make_pair
#define vvi(r, c) vector<vector<lli>> v(r, vector<lli>(c));
#define fi first
#define se second
#define mem(arr, x) memset(arr, x, sizeof(arr))

lli power(lli a, lli b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return ((a % md) * (power(a, b - 1))) % md;
  else {
    lli x = power(a, b / 2);
    return (x * x) % md;
  }
}

lli numCuts(vector<lli> &a, lli mid) {
  int n = a.size();
  lli sum = 0;
  for (int i = 0; i < n; i++) {
    lli x = a[i] / mid;
    if (a[i] % mid)
      x++;
    sum += x - 1;
  }
  return sum;
}
lli bin_search(vector<lli> &a, lli l, lli r, lli k) {
  if (l < r) {
    lli mid = l + (r - l) / 2;
    if (numCuts(a, mid) > k)
      return bin_search(a, mid + 1, r, k);
    return bin_search(a, l, mid, k);
  }
  return l;
}

void solve() {
  lli n, k;
  cin >> n >> k;
  vector<lli> a(n);
  lli r = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    r = max(r, a[i]);
  }
  lli ans = bin_search(a, 1, r, k);
  cout << ans << endl;
}

int main() {
  fastio();
  int t;

  t = 1;
  while (t--) {
    solve();
  }

  return 0;
}
Problem p02598 (C++)

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (auto &i : A)
    cin >> i;

  auto ok = [&](auto x) {
    long long k = 0;
    for (auto &a : A)
      k += (a - 1) / x;
    return k <= K;
  };

  int l = -1, h = 1e9;
  while (1 < h - l) {
    auto m = (l + h) / 2;
    if (ok(m))
      h = m;
    else
      l = m;
  }
  cout << h << endl;
}


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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (auto &i : A)
    cin >> i;

  auto ok = [&](auto x) {
    long long k = 0;
    for (auto &a : A)
      k += (a - 1) / x;
    return k <= K;
  };

  int l = 0, h = 1e9;
  while (1 < h - l) {
    auto m = (l + h) / 2;
    if (ok(m))
      h = m;
    else
      l = m;
  }
  cout << h << endl;
}
Problem p02598 (C++)

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

#define ll long long
#define rep(A, B, C) for (A = B; A < C; ++A)
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

/////////////////////////////////////////////////////

ll N, K;
ll A[202020];

ll i, j, k;

bool calc(ll aa) {
  ll k = K, i;
  rep(i, 0, N) {
    ll c = (A[i] - 1) / aa;
    k -= c;
    if (k < 0)
      return false;
  }
  return true;
}

int main() {
  scanf("%lld%lld", &N, &K);
  rep(i, 0, N) { scanf("%lld", &A[i]); }
  ll l = 0, r = 1e12;
  while (l < r) {
    ll c = (l + r) / 2;
    if (calc(c)) {
      r = c;
    } else {
      l = c + 1;
    }
  }
  printf("%lld\n", l);

  return 0;
}

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

#define ll long long
#define rep(A, B, C) for (A = B; A < C; ++A)
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

/////////////////////////////////////////////////////

ll N, K;
ll A[202020];

ll i, j, k;

bool calc(ll aa) {
  ll k = K, i;
  rep(i, 0, N) {
    ll c = (A[i] - 1) / aa;
    k -= c;
    if (k < 0)
      return false;
  }
  return true;
}

int main() {
  scanf("%lld%lld", &N, &K);
  rep(i, 0, N) { scanf("%lld", &A[i]); }
  ll l = 1, r = 1e12;
  while (l < r) {
    ll c = (l + r) / 2;
    if (calc(c)) {
      r = c;
    } else {
      l = c + 1;
    }
  }
  printf("%lld\n", l);

  return 0;
}
Problem p02598 (C++)

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

#define F first
#define S second
#define MP make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define LCM(a, b) (a) / __gcd((a), (b)) * (b)
#define CEIL(a, b) (a) / (b) + (((a) % (b)) ? 1 : 0)
#define ln '\n'

using namespace std;
using LL = long long;
using ldouble = long double;
using P = pair<int, int>;
using LP = pair<LL, LL>;

static const int INF = INT_MAX;
static const LL LINF = LLONG_MAX;
static const int MIN = INT_MIN;
static const LL LMIN = LLONG_MIN;
static const int MOD = 1e9 + 7;
static const int SIZE = 200005;

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

vector<LL> Div(LL n) {
  vector<LL> ret;
  for (LL i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      ret.pb(i);
      if (i * i != n)
        ret.pb(n / i);
    }
  }
  sort(all(ret));
  return ret;
}

int K, N;
vector<int> v;

bool check(int n) {
  int t = K;
  for (int i = 0; i < N; ++i) {
    t -= v[i] / n;
    if (v[i] >= n && v[i] % n == 0)
      ++t;
  }
  return (t >= 0);
}

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

  cin >> N >> K;

  int mx = 0;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    v.pb(a);
    mx = max(mx, a);
  }

  int l, r;
  l = 0, r = mx;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  if (check(l))
    cout << l;
  else
    cout << r;
  cout << endl;

  return 0;
}


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

#define F first
#define S second
#define MP make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define LCM(a, b) (a) / __gcd((a), (b)) * (b)
#define CEIL(a, b) (a) / (b) + (((a) % (b)) ? 1 : 0)
#define ln '\n'

using namespace std;
using LL = long long;
using ldouble = long double;
using P = pair<int, int>;
using LP = pair<LL, LL>;

static const int INF = INT_MAX;
static const LL LINF = LLONG_MAX;
static const int MIN = INT_MIN;
static const LL LMIN = LLONG_MIN;
static const int MOD = 1e9 + 7;
static const int SIZE = 200005;

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

vector<LL> Div(LL n) {
  vector<LL> ret;
  for (LL i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      ret.pb(i);
      if (i * i != n)
        ret.pb(n / i);
    }
  }
  sort(all(ret));
  return ret;
}

int K, N;
vector<int> v;

bool check(int n) {
  int t = K;
  for (int i = 0; i < N; ++i) {
    t -= v[i] / n;
    if (v[i] >= n && v[i] % n == 0)
      ++t;
  }
  return (t >= 0);
}

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

  cin >> N >> K;

  int mx = 0;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    v.pb(a);
    mx = max(mx, a);
  }

  int l, r;
  l = 0, r = mx;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << endl;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include <cmath>
#include <iomanip>
#include <math.h>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int mod = 1e9 + 7;
const int64_t INF = 1e18;
int64_t N, K;
int64_t A[100005];
int64_t f(int64_t m) {
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] > m) {
      cnt += (A[i] - 1) / m;
    }
  }
  return cnt;
}
int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  int64_t l = 0, r = 1e9 + 1;
  while (r - l > 1) {
    int64_t m = (r + l) / 2; // m=max長さ
    if (f(m) <= K) {
      r = m;
    } // K回以内でｍ以内の長さにできる長さの範囲＝ｒ
    if (f(m) > K) {
      l = m;
    }
  }
  cout << r << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <cmath>
#include <iomanip>
#include <math.h>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int mod = 1e9 + 7;
const int64_t INF = 1e18;
int64_t N, K;
int64_t A[1000005];
int64_t f(int64_t m) {
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] > m) {
      cnt += (A[i] - 1) / m;
    }
  }
  return cnt;
}
int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  int64_t l = 0, r = 1e9 + 1;
  while (r - l > 1) {
    int64_t m = (r + l) / 2; // m=max長さ
    if (f(m) <= K) {
      r = m;
    } // K回以内でｍ以内の長さにできる長さの範囲＝ｒ
    if (f(m) > K) {
      l = m;
    }
  }
  cout << r << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define me(a, b) memset(a, b, sizeof(a))
#define INIT() std::ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int MAX_N = 200000 + 5;
const int MAX_M = 100000 + 5;
const int INF = 0x7fffffff;
const int inf = 1000000000;
const double EPS = 1e-6;
const ull base = 123;
const ll mod = 1e9 + 7;
const double pi = 4 * atan(1.0);
int a[MAX_N];
int n, k;
int check(int x) {
  int ans = 0;
  int i;
  for (i = 1; i <= n; i++) {
    if (a[i] > x) {
      ans += a[i] / x;
      if (a[i] % x == 0)
        ans--;
    }
    if (ans > k)
      return 1;
  }
  return 0;
}
int main() {
  cin >> n >> k;
  int i;
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int l = 0;
  int r = 1000000000;
  int mid = (l + r) / 2;
  while (l <= r) {
    if (check(mid)) {
      l = mid + 1;
    } else
      r = mid - 1;
    mid = (l + r) / 2;
  }
  cout << mid + 1 << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define me(a, b) memset(a, b, sizeof(a))
#define INIT() std::ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int MAX_N = 200000 + 5;
const int MAX_M = 100000 + 5;
const int INF = 0x7fffffff;
const int inf = 1000000000;
const double EPS = 1e-6;
const ull base = 123;
const ll mod = 1e9 + 7;
const double pi = 4 * atan(1.0);
int a[MAX_N];
int n, k;
int check(int x) {
  int ans = 0;
  int i;
  for (i = 1; i <= n; i++) {
    if (a[i] > x) {
      ans += a[i] / x;
      if (a[i] % x == 0)
        ans--;
    }
    if (ans > k)
      return 1;
  }
  return 0;
}
int main() {
  cin >> n >> k;
  int i;
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int l = 0;
  int r = 1000000000;
  int mid = (l + r) / 2;
  while (l <= r) {
    if (mid == 0)
      break;
    if (check(mid)) {
      l = mid + 1;
    } else
      r = mid - 1;
    mid = (l + r) / 2;
  }
  cout << mid + 1 << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;

typedef long long ll;

ll a[200005], n, k;

bool possible(double x) {
  ll i, res = 0;

  for (i = 1; i <= n; i++)
    res += (a[i] * 2 / (ll)(x * 2.0) + !(a[i] * 2 % (ll)(x * 2.0) == 0)) - 1;

  return res <= k;
}

int main(void) {
  ll i;

  scanf("%lld %lld", &n, &k);
  for (i = 1; i <= n; i++)
    scanf("%lld", &a[i]);

  double left = 0, right = (double)*max_element(a + 1, a + n + 1);

  for (i = 1; i <= 200; i++) {
    double mid = (left + right) / 2.0;

    if (possible(mid))
      right = mid;
    else
      left = mid + 0.1;
  }

  printf("%lld", (ll)round(left));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;

typedef long long ll;

ll a[200005], n, k;

bool possible(double x) {
  ll i, res = 0;

  for (i = 1; i <= n; i++)
    res += (a[i] * 2 / (ll)(x * 2.0) + !(a[i] * 2 % (ll)(x * 2.0) == 0)) - 1;

  return res <= k;
}

int main(void) {
  ll i;

  scanf("%lld %lld", &n, &k);
  for (i = 1; i <= n; i++)
    scanf("%lld", &a[i]);

  double left = 0.5, right = (double)*max_element(a + 1, a + n + 1);

  for (i = 1; i <= 200; i++) {
    double mid = (left + right) / 2.0;

    if (possible(mid))
      right = mid;
    else
      left = mid + 0.1;
  }

  printf("%lld", (ll)round(left));
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define pi 3.14159265359
#define all(i) i.begin(), i.end()
using namespace std;
const long long INF = 1e9 + 7;
const string alp = "abcdefghijklmnopqrstuvwxyz"; // 26
const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
// const ll MOD=998244353LL;
// const ll MOD=1777777777LL;

int main() {
  ll N, K;
  cin >> N >> K;
  ll a[N];
  rep(i, N) cin >> a[i];

  ll lb = 0, ub = INF;

  for (int i = 0; i < 100; i++) {
    ll mid = (lb + ub) / 2;
    ll num = 0;
    for (int i = 0; i < N; i++) {
      num += (a[i] - 1) / mid;
    }
    if (num > K)
      lb = mid;
    else
      ub = mid;
  }
  int ans = ub;
  if (ub > ans)
    ans++;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define pi 3.14159265359
#define all(i) i.begin(), i.end()
using namespace std;
const long long INF = 1e9 + 7;
const string alp = "abcdefghijklmnopqrstuvwxyz"; // 26
const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
// const ll MOD=998244353LL;
// const ll MOD=1777777777LL;

int main() {
  ll N, K;
  cin >> N >> K;
  ll a[N];
  rep(i, N) cin >> a[i];

  ll lb = 0, ub = INF;

  while (ub - lb > 1) {
    ll mid = (lb + ub) / 2;
    ll num = 0;
    for (int i = 0; i < N; i++) {
      num += (a[i] - 1) / mid;
    }
    if (num > K)
      lb = mid;
    else
      ub = mid;
  }
  int ans = ub;
  if (ub > ans)
    ans++;
  cout << ans << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
// Hail god Yato

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

#define hs                                                                     \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long ll;
const ll mod = 1000000007;
const ll INF = 1e10;
const ll MAX = 100001;
//
//

vector<ll> vec(MAX);
int check(ll mid, ll k) {
  ll cuts = 0;
  for (ll z : vec) {
    cuts += ((z - 1) / mid);
  }
  return cuts <= k;
}
void solve() {
  ll n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> vec[i];
  ll lo = 1, hi = INF;
  while (lo < hi) {
    ll mid = (lo + hi) >> 1;
    if (check(mid, k))
      hi = mid;
    else
      lo = mid + 1;
  }
  cout << lo;
}
int main() {
  hs;
  ll t;
  t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}


Fixed submission:
// Hail god Yato

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

#define hs                                                                     \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long ll;
const ll mod = 1000000007;
const ll INF = 1e9;
const ll MAX = 200005;
//
//

vector<ll> vec(MAX);
int check(ll mid, ll k) {
  ll cuts = 0;
  for (ll z : vec) {
    cuts += ((z - 1) / mid);
  }
  return cuts <= k;
}
void solve() {
  ll n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> vec[i];
  ll lo = 1, hi = INF;
  while (lo < hi) {
    ll mid = (lo + hi) >> 1;
    if (check(mid, k))
      hi = mid;
    else
      lo = mid + 1;
  }
  cout << lo;
}
int main() {
  hs;
  ll t;
  t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}
Problem p02598 (C++)

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

int main() {
  int N, K, m = 0;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    m = max(A[i], m);
  }
  int l = 1, r = m;
  int ans = m;
  for (int j = 0; j < 10000; j++) {
    if (l == r)
      break;
    int a = l + (r - l) / 2;
    int M = 0, k = 0;
    bool ok = true;
    for (int i = 0; i < N; i++) {
      if (A[i] <= a)
        continue;
      k += A[i] / a;
      // if(A[i]/a==0)ok=false;
    }
    if (ok && k <= K) {
      ans = min(a, ans);
      r = a;
    } else {
      l = a;
    }
  }
  cout << ans << endl;
}


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

int main() {
  int N, K, m = 0;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    m = max(A[i], m);
  }
  int l = 1, r = m;
  int ans = m;
  for (int j = 0; j < 100; j++) {
    if (l == r)
      break;
    int a = l + (r - l) / 2;
    int M = 0, k = 0;
    bool ok = true;
    for (int i = 0; i < N; i++) {
      if (A[i] <= a)
        continue;
      k += A[i] / a;
      // if(A[i]/a==0)ok=false;
    }
    if (ok && k <= K) {
      ans = min(a, ans);
      r = a;
    } else {
      l = a;
    }
  }
  cout << ans << endl;
}
Problem p02598 (C++)

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

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(c) (c).begin(), (c).end()

// 区間[a, b)の中で最初に pred(x) = true となるような x を返す。
// そのような x が存在しないときは b を返す。
template <class Pred> int64_t bin_search(int64_t a, int64_t b, Pred pred) {
  // 不変条件: i<a ⇒ pred(i) = false, i>=b ⇒ pred(i) = true
  if (a == b)
    return b;
  int64_t m = a + (b - a) / 2;
  if (pred(m))
    return bin_search(a, m, pred);
  else
    return bin_search(m + 1, b, pred);
}

inline int64_t div_ceil(int64_t a, int64_t b) noexcept {
  return (a + b - 1) / b;
}

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

  int64_t S = 0;
  REP(i, N) S += A[i];

  int64_t ans = bin_search(0, S + 1, [&](int64_t L) {
    // 最大の丸太の長さが L 以下になるように切ることができるか
    int64_t k = 0;
    REP(i, N) k += div_ceil(A[i], L) - 1;
    return k <= K;
  });
  cout << ans << '\n';
}


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

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(c) (c).begin(), (c).end()

// 区間[a, b)の中で最初に pred(x) = true となるような x を返す。
// そのような x が存在しないときは b を返す。
template <class Pred> int64_t bin_search(int64_t a, int64_t b, Pred pred) {
  // 不変条件: i<a ⇒ pred(i) = false, i>=b ⇒ pred(i) = true
  if (a == b)
    return b;
  int64_t m = a + (b - a) / 2;
  if (pred(m))
    return bin_search(a, m, pred);
  else
    return bin_search(m + 1, b, pred);
}

inline int64_t div_ceil(int64_t a, int64_t b) noexcept {
  return (a + b - 1) / b;
}

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

  int64_t S = 0;
  REP(i, N) S += A[i];

  int64_t ans = bin_search(1, S + 1, [&](int64_t L) {
    // 最大の丸太の長さが L 以下になるように切ることができるか
    int64_t k = 0;
    REP(i, N) k += div_ceil(A[i], L) - 1;
    return k <= K;
  });
  cout << ans << '\n';
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e15;

#include <math.h>
#define PI 3.14159265358979323846264338327950L
const int mxN = 1e9 + 7;

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

  int l = 0, r = 1e9;
  int ans = 0;
  while (l <= r) {
    int m = l + (r - l) / 2;
    long long sum = 0;
    for (int i = 0; i < n; i++) {
      sum += (a[i] - 1) / m;
    }
    if (sum > k) {
      l = m + 1;
    } else {
      ans = m;
      r = m - 1;
    }
  }
  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e15;

#include <math.h>
#define PI 3.14159265358979323846264338327950L
const int mxN = 1e9 + 7;

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

  int l = 1, r = 1e9;
  int ans = 0;
  while (l <= r) {
    int m = l + (r - l) / 2;
    long long sum = 0;
    for (int i = 0; i < n; i++) {
      sum += (a[i] - 1) / m;
    }
    if (sum > k) {
      l = m + 1;
    } else {
      ans = m;
      r = m - 1;
    }
  }
  cout << ans;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

struct fast_io {
  fast_io() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
  };
} fio;

int N, K;
vector<int> A;

bool check(int n) {
  int con = 0;

  for (int i = 0; i < N; i++) {
    if (A[i] < n)
      continue;

    con += (A[i]) / n;
  }

  return con <= K;
}

bool check2(int n) {
  int con = 0;
  int z = 0;

  for (int i = 0; i < N; i++) {
    if (A[i] < n)
      continue;

    con += (A[i]) / n;

    if (A[i] % n == 0)
      con--;
  }

  // if(z == N) return true;

  // return false;

  return con > K;
}

signed main() {
  cout << fixed << setprecision(10);

  cin >> N >> K;

  A.resize(N);

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

  int l = 0, h = 1e10, m = 0;

  while (l + 1 < h) {
    m = (l + h) / 2;

    if (!check(m)) {
      l = m;
    } else {
      h = m;
    }
  }

  // cout<<l<<endl;
  if (check2(l))
    l++;

  cout << l << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

struct fast_io {
  fast_io() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
  };
} fio;

int N, K;
vector<int> A;

bool check(int n) {
  int con = 0;

  for (int i = 0; i < N; i++) {
    if (A[i] < n)
      continue;

    con += (A[i]) / n;
  }

  return con <= K;
}

bool check2(int n) {
  int con = 0;
  int z = 0;

  for (int i = 0; i < N; i++) {
    if (A[i] < n)
      continue;

    con += (A[i]) / n;

    if (A[i] % n == 0)
      con--;
  }

  // if(z == N) return true;

  // return false;

  return con > K;
}

signed main() {
  cout << fixed << setprecision(10);

  cin >> N >> K;

  A.resize(N);

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

  int l = 1, h = 1e10, m = 0;

  while (l + 1 < h) {
    m = (l + h) / 2;

    if (!check(m)) {
      l = m;
    } else {
      h = m;
    }
  }

  // cout<<l<<endl;
  if (check2(l))
    l++;

  cout << l << endl;

  return 0;
}
Problem p02598 (C++)

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

int n, k, a[200005];

bool pos(int l) {
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    cnt += (a[i] + l - 1) / l - 1;
  }
  if (cnt > k)
    return false;
  return true;
}

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  int lo = 0, hi = 1000000000, ans = hi;
  while (lo <= hi) {
    int mid = (hi + lo) / 2;
    // printf("%d %d\n", mid, pos(mid));
    if (pos(mid)) {
      ans = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }
  printf("%d", ans);
}


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

int n, k, a[200005];

bool pos(int l) {
  if (l == 0)
    return false;
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    cnt += (a[i] + l - 1) / l - 1;
  }
  if (cnt > k)
    return false;
  return true;
}

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  int lo = 0, hi = 1000000000, ans = hi;
  while (lo <= hi) {
    int mid = (hi + lo) / 2;
    // printf("%d %d\n", mid, pos(mid));
    if (pos(mid)) {
      ans = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }
  printf("%d", ans);
}