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

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  long long int k;
  cin >> n >> k;
  long long int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long int l = 0;
  long long int r = 1e9;
  while (l < r) {
    long long int m = (l + r) / 2;
    long long int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] > m) {
        cnt += (a[i] - 1) / m;
      }
    }
    if (cnt <= k)
      r = m;
    else
      l = m + 1;
  }
  cout << l << "\n";
  return 0;
}

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  long long int k;
  cin >> n >> k;
  long long int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long int l = 1;
  long long int r = 1e9;
  while (l < r) {
    long long int m = (l + r) / 2;
    long long int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] > m) {
        cnt += (a[i] - 1) / m;
      }
    }
    if (cnt <= k)
      r = m;
    else
      l = m + 1;
  }
  cout << l << "\n";
  return 0;
}
Problem p02598 (C++)

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

using namespace std;
const int maxn = 1e6 + 5;

int n, k;
int a[maxn];

bool check(int mid) {
  int num = 0;
  for (int i = 1; i <= n; i++) {
    num += a[i] / mid + (a[i] % mid != 0) - 1;
    if (num > k)
      return false;
  }
  return true;
}

int main() {
  int l = 0, r, mid;
  r = 0;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    r = max(r, a[i]);
  }
  while (l < r) {
    mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", l);
  return 0;
}


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

using namespace std;
const int maxn = 1e6 + 5;

int n, k;
int a[maxn];

bool check(int mid) {
  int num = 0;
  for (int i = 1; i <= n; i++) {
    num += a[i] / mid + (a[i] % mid != 0) - 1;
    if (num > k)
      return false;
  }
  return true;
}

int main() {
  int l = 1, r, mid;
  r = 0;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    r = max(r, a[i]);
  }
  while (l < r) {
    mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", l);
  return 0;
}
Problem p02598 (C++)

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

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

  auto solve = [&](int x) {
    int cnt = 0;
    rep(i, n) {
      double now = a[i];
      int div = 1;
      while (x < now / div) {
        cnt++;
        div++;
      }
    }

    if (cnt <= k)
      return true;
    else
      return false;
  };
  int ok = 1e9; // 解が存在する値
  int ng = 0;   // 解が存在しない値
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (solve(mid)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

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

  auto solve = [&](int x) {
    int cnt = 0;
    rep(i, n) {
      // x = 3
      // a: 1, 2, 3, 4, 5, 6, 7...
      // b: 0, 0, 0, 1, 1, 1, 2...
      cnt += (a[i] - 1) / x;
    }

    if (cnt <= k)
      return true;
    else
      return false;
  };
  int ok = 1e9; // 解が存在する値
  int ng = 0;   // 解が存在しない値
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (solve(mid)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;
using dd = long double;
int main() {
  ll N, K;
  cin >> N >> K;

  vector<ll> log(N);
  for (ll i = 0; i < N; i++) {
    cin >> log[i];
  }
  ll l = 0;
  ll r = 1e9;

  while ((r - l) > 1) {
    ll mid = (l + r) / 2;
    ll sum = 0;
    for (ll i = 0; i < N; i++) {
      if (log[i] <= mid) {
        continue;
      }
      sum += (log[i] / mid) + (log[i] % mid == 0 ? -1 : 0);
    }
    if (sum > K) {
      l = mid + 1;
    } else {
      r = mid;
    }
  }
  ll l_sum = 0;
  for (ll i = 0; i < N; i++) {
    if (log[i] <= l) {
      continue;
    }
    l_sum += (log[i] / l) + (log[i] % l == 0 ? -1 : 0);
  }

  if (l_sum <= K) {
    cout << l << endl;
  } else {
    cout << r << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;
using dd = long double;
int main() {
  ll N, K;
  cin >> N >> K;

  vector<ll> log(N);
  for (ll i = 0; i < N; i++) {
    cin >> log[i];
  }
  ll l = 1;
  ll r = 1e9;

  while ((r - l) > 1) {
    ll mid = (l + r) / 2;
    ll sum = 0;
    for (ll i = 0; i < N; i++) {
      if (log[i] <= mid) {
        continue;
      }
      sum += (log[i] / mid) + (log[i] % mid == 0 ? -1 : 0);
    }
    if (sum > K) {
      l = mid + 1;
    } else {
      r = mid;
    }
  }
  ll l_sum = 0;
  for (ll i = 0; i < N; i++) {
    if (log[i] <= l) {
      continue;
    }
    l_sum += (log[i] / l) + (log[i] % l == 0 ? -1 : 0);
  }

  if (l_sum <= K) {
    cout << l << endl;
  } else {
    cout << r << endl;
  }
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):

// E - Logs

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

int N, K;
ll A[200000];

// 長さt以下にできるかどうか
bool is_ok(ll t) {
  ll cnt = 0;
  for (int i = 0; i < N; i++) {
    cnt += (A[i] - 1) / t; // 切る回数
  }

  return cnt <= K;
}

ll find_min_ok(ll lower, ll upper) {
  if (!is_ok(upper - 1))
    return -1; // 存在しない

  ll ok = upper - 1;
  ll ng = lower - 1;

  while (ok - ng > 1) {
    ll trying = (ng + ok) / 2;

    if (is_ok(trying)) {
      ok = trying;
    } else {
      ng = trying;
    }
  }

  return ok;
}

int main() {
  cin >> N >> K;

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

  ll ans = find_min_ok(0, 1e9 + 1);

  cout << ans << endl;

  return 0;
}

Fixed submission:

// E - Logs

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

int N, K;
ll A[200000];

// 長さt以下にできるかどうか
bool is_ok(ll t) {
  if (t == 0)
    return false;

  ll cnt = 0;
  for (int i = 0; i < N; i++) {
    cnt += (A[i] - 1) / t; // 切る回数
  }

  return cnt <= K;
}

ll find_min_ok(ll lower, ll upper) {
  if (!is_ok(upper - 1))
    return -1; // 存在しない

  ll ok = upper - 1;
  ll ng = lower - 1;

  while (ok - ng > 1) {
    ll trying = (ng + ok) / 2;

    if (is_ok(trying)) {
      ok = trying;
    } else {
      ng = trying;
    }
  }

  return ok;
}

int main() {
  cin >> N >> K;

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

  ll ans = find_min_ok(0, 1e9 + 1);

  cout << ans << endl;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
// g++  7.4.0

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, k;
  cin >> n >> k;
  vector<int> ar(n);
  for (int &i : ar)
    cin >> i;

  int l = -1;
  int h = *max_element(ar.begin(), ar.end());

  auto fun = [&](int x) { // want the largest length to be x , minimize x
    int cuts = 0;
    for (const int i : ar) {
      if (x >= i)
        continue;
      int rem = i - x;
      cuts++;
      if (rem > x)
        cuts += ((rem + x - 1) / x - 1);
    }
    return cuts <= k;
  };

  while (l + 1 < h) {
    int mid = (l + h) >> 1;
    if (fun(mid))
      h = mid;
    else
      l = mid;
  }
  cout << h << '\n';

  return 0;
}

Fixed submission:
// g++  7.4.0

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, k;
  cin >> n >> k;
  vector<int> ar(n);
  for (int &i : ar)
    cin >> i;

  int l = 0;
  int h = *max_element(ar.begin(), ar.end());

  auto fun = [&](int x) { // want the largest length to be x , minimize x
    int cuts = 0;
    for (const int i : ar) {
      if (x >= i)
        continue;
      int rem = i - x;
      cuts++;
      if (rem > x)
        cuts += ((rem + x - 1) / x - 1);
    }
    return cuts <= k;
  };

  while (l + 1 < h) {
    int mid = (l + h) >> 1;
    if (fun(mid))
      h = mid;
    else
      l = mid;
  }
  cout << h << '\n';

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
// #include <fsociety>
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// end of libraries ;
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
//  ll lmax(ll o , ll w) {if(o>w)return o; return w;}
//  ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define endl "\n"
#define F first
#define S second
#define pb push_back
#define ll long long
#define all(c) (c).begin(), (c).end()
#define sz(c) (c).size()
#define fcin ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
using namespace std;
ll n, k, a[N], ans;
int main() {
  fcin;
  cin >> n >> k;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll l = 0, r = 10e10;
  while (l <= r) {
    ll mid = (l + r) / 2, c = 0;
    for (ll i = 0; i < n; i++) {
      if (a[i] < mid)
        continue;
      c += ((a[i] / mid + (a[i] % mid != 0)) - 1);
    }
    if (c > k) {
      l = mid + 1;
    } else {
      ans = mid;
      r = mid - 1;
    }
  }
  cout << ans << "\n";
}

Fixed submission:
// #include <fsociety>
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// end of libraries ;
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
//  ll lmax(ll o , ll w) {if(o>w)return o; return w;}
//  ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define endl "\n"
#define F first
#define S second
#define pb push_back
#define ll long long
#define all(c) (c).begin(), (c).end()
#define sz(c) (c).size()
#define fcin ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
using namespace std;
ll n, k, a[N], ans;
int main() {
  fcin;
  cin >> n >> k;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll l = 1, r = 10e8;
  while (l <= r) {
    ll mid = (l + r) / 2, c = 0;
    for (ll i = 0; i < n; i++) {
      if (a[i] < mid)
        continue;
      c += ((a[i] / mid + (a[i] % mid != 0)) - 1);
    }
    if (c > k) {
      l = mid + 1;
    } else {
      ans = mid;
      r = mid - 1;
    }
  }
  cout << ans << "\n";
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define loopi(x, y) for (int i = x; i < (y); i++)
#define loopj(x, y) for (int j = x; j < (y); j++)
#define rloopi(x, y) for (int i = x; i >= (y); i--)
#define rloopj(x, y) for (int j = x; j >= (y); j--)

#define nl cout << "\n";
#define ll long long int
#define mod 1000000007

using namespace std;

int main() {

  ios ::sync_with_stdio(false);
  cin.tie(nullptr);

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

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

  ll p = 0;
  while (p < q) {
    ll mid = (p + q) / 2;
    ll count = 0;
    for (int i = 0; i < n; i++) {
      count += (a[i] - 1) / mid;
    }

    if (count <= k) {
      q = mid;
    } else {
      p = mid + 1;
    }
  }

  cout << p;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define loopi(x, y) for (int i = x; i < (y); i++)
#define loopj(x, y) for (int j = x; j < (y); j++)
#define rloopi(x, y) for (int i = x; i >= (y); i--)
#define rloopj(x, y) for (int j = x; j >= (y); j--)

#define nl cout << "\n";
#define ll long long int
#define mod 1000000007

using namespace std;

int main() {

  ios ::sync_with_stdio(false);
  cin.tie(nullptr);

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

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

  ll p = 1;
  while (p < q) {
    ll mid = (p + q) / 2;
    ll count = 0;
    for (int i = 0; i < n; i++) {
      count += (a[i] - 1) / mid;
    }

    if (count <= k) {
      q = mid;
    } else {
      p = mid + 1;
    }
  }

  cout << p;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n - 1); i >= 0; i--)
#define all(x) x.begin(), x.end()
#define br cout << "\n";
using namespace std;
const long long INF = 1e10;
const long long MOD = 1e9 + 7;
using Graph = vector<vector<ll>>;
using pll = pair<ll, 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;
}

// 0 false, 1 true
// string number to int : -48
// a to A : -32
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp

ll n, k;
vector<ll> vec(n);

// return the number of ng
// left always ng. right always ok
long long nibun_search(ll val) {
  long long left = -1;
  long long right = val;
  while (right - left > 1) {
    long long mid = (left + right) / 2;
    if (mid == 0) {
      mid = 1;
    }
    ll count = 0;
    rep(i, n) {
      if (vec[i] % mid == 0) {
        count += ((vec[i] / mid) - 1);

      } else {
        count += (vec[i] / mid);
      }
    }
    if (count <= k) {
      right = mid;
    } else {
      left = mid;
    }
  }
  return right;
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  cin >> n >> k;
  vec.resize(n);

  for (long long i = 0; i < n; i++) {
    cin >> vec[i];
  }
  cout << nibun_search(1000000000) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n - 1); i >= 0; i--)
#define all(x) x.begin(), x.end()
#define br cout << "\n";
using namespace std;
const long long INF = 1e10;
const long long MOD = 1e9 + 7;
using Graph = vector<vector<ll>>;
using pll = pair<ll, 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;
}

// 0 false, 1 true
// string number to int : -48
// a to A : -32
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp

ll n, k;
vector<ll> vec(n);

// return the number of ng
// left always ng. right always ok
long long nibun_search(ll val) {
  long long left = -1;
  long long right = val;
  while (right - left > 1) {
    long long mid = (left + right) / 2;
    if (mid == 0) {
      return 1;
    }
    ll count = 0;
    rep(i, n) {
      if (vec[i] % mid == 0) {
        count += ((vec[i] / mid) - 1);

      } else {
        count += (vec[i] / mid);
      }
    }
    if (count <= k) {
      right = mid;
    } else {
      left = mid;
    }
  }
  return right;
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  cin >> n >> k;
  vec.resize(n);

  for (long long i = 0; i < n; i++) {
    cin >> vec[i];
  }
  cout << nibun_search(1000000000) << endl;
}
Problem p02598 (C++)

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

#define N 1000000007

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  ll sum = 0;
  ll max_a = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    max_a = max(max_a, a[i]);
  }
  ll bound_max = max_a;
  ll bound_min = sum / (n + k);
  ll cur = (bound_max + bound_min) / 2;

  while (true) {
    if (bound_max - bound_min == 1)
      break;
    ll ncut = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] <= cur)
        continue;
      ll m = a[i] / cur + (a[i] % cur ? 1 : 0);
      ncut += m - 1;
    }
    if (ncut <= k) {
      bound_max = cur;
    } else {
      bound_min = cur;
    }
    cur = (bound_max + bound_min) / 2;
  }
  cout << bound_max << endl;
  return 0;
}

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

#define N 1000000007

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  ll sum = 0;
  ll max_a = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    max_a = max(max_a, a[i]);
  }
  ll bound_max = max_a;
  ll bound_min = sum / (n + k);
  ll cur = (bound_max + bound_min) / 2;

  while (true) {
    if (bound_min + 1 >= bound_max)
      break;
    ll ncut = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] <= cur)
        continue;
      ll m = a[i] / cur + (a[i] % cur ? 1 : 0);
      ncut += m - 1;
    }
    if (ncut <= k) {
      bound_max = cur;
    } else {
      bound_min = cur;
    }
    cur = (bound_max + bound_min) / 2;
  }
  cout << bound_max << endl;
  return 0;
}
Problem p02598 (C++)

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

using namespace std;

//----------------------- Print Function ----------------------//

inline void print() { cout << endl; }
template <typename First, typename... Rest>
void print(const First &first, const Rest &...rest) {
  cout << first << ' ';
  print(rest...);
}

template <typename T> void print(const vector<T> &v) {
  for (auto e : v)
    cout << e << ' ';
  cout << endl;
}

//------------------------- Libraries -------------------------//

//--------------------------- Solve ---------------------------//

long long N, K;
vector<long long> A;

bool isOK(long long mid) {
  long long k = 0;
  for (int i = 0; i < N; i++) {
    k += (A[i] - 1) / mid;
  }
  if (k <= K)
    return true;
  else
    return false;
}

void solve() {
  cin >> N >> K;
  A.resize(N);
  long long ma = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    ma = max(ma, A[i]);
  }

  long long ok = ma;
  long long ng = -1;

  while (abs(ok - ng) > 1) {
    long long mid = (ok + ng) / 2;
    if (isOK(mid))
      ok = mid;
    else
      ng = mid;
  }

  cout << ok << '\n';
}

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

  solve();

  return 0;
}

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

using namespace std;

//----------------------- Print Function ----------------------//

inline void print() { cout << endl; }
template <typename First, typename... Rest>
void print(const First &first, const Rest &...rest) {
  cout << first << ' ';
  print(rest...);
}

template <typename T> void print(const vector<T> &v) {
  for (auto e : v)
    cout << e << ' ';
  cout << endl;
}

//------------------------- Libraries -------------------------//

//--------------------------- Solve ---------------------------//

long long N, K;
vector<long long> A;

bool isOK(long long mid) {
  long long k = 0;
  for (int i = 0; i < N; i++) {
    k += (A[i] - 1) / mid;
  }
  if (k <= K)
    return true;
  else
    return false;
}

void solve() {
  cin >> N >> K;
  A.resize(N);
  long long ma = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    ma = max(ma, A[i]);
  }

  long long ok = ma;
  long long ng = 0;

  while (abs(ok - ng) > 1) {
    long long mid = (ok + ng) / 2;
    if (isOK(mid))
      ok = mid;
    else
      ng = mid;
  }

  cout << ok << '\n';
}

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

  solve();

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define all(s) (s).begin(), (s).end()

ll n, k;
vl logs;
bool test(ll target) {
  ll output = 0;
  for (ll curr : logs) {
    if (target + curr - 1 == 0)
      cout << "blub" << endl;
    output += ((target + curr - 1) / target) - 1;
  }

  return output <= k;
}

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

  ll mini = 0;
  ll maxi = 0;

  FOR(i, 0, n) {
    ll next;
    cin >> next;
    logs.push_back(next);
    maxi = max(maxi, next);
  }

  while (abs(maxi - mini) >= 1) {
    ll mid = (maxi + mini) / 2;

    if (test(mid)) {
      maxi = mid;
    } else {
      mini = mid + 1;
    }
  }

  cout << maxi << endl;

  /*FOR(i, 0, k) {
      ll next = logs.top();
      logs.pop();

      ll a, b;
      ll other = logs.top();
      if(other == next) {
          a = next / 2;
          b = next - a;
      } else {
          a = other;
          b = next - a;
      }

      a = next / 2;
      b = next - a;
      cerr << a << " " << b << endl;

      logs.push(a);
      logs.push(b);
  }*/

  /*while(!logs.empty()) {
      cout << logs.top() << endl;
      logs.pop();
  }*/

  // cout << logs.top() << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define all(s) (s).begin(), (s).end()

ll n, k;
vl logs;
bool test(ll target) {
  ll output = 0;
  for (ll curr : logs) {
    if (target + curr - 1 == 0)
      cout << "blub" << endl;
    output += ((target + curr - 1) / target) - 1;
  }

  return output <= k;
}

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

  ll mini = 1;
  ll maxi = 0;

  FOR(i, 0, n) {
    ll next;
    cin >> next;
    logs.push_back(next);
    maxi = max(maxi, next);
  }

  while (abs(maxi - mini) >= 1) {
    ll mid = (maxi + mini) / 2;

    if (test(mid)) {
      maxi = mid;
    } else {
      mini = mid + 1;
    }
  }

  cout << maxi << endl;

  /*FOR(i, 0, k) {
      ll next = logs.top();
      logs.pop();

      ll a, b;
      ll other = logs.top();
      if(other == next) {
          a = next / 2;
          b = next - a;
      } else {
          a = other;
          b = next - a;
      }

      a = next / 2;
      b = next - a;
      cerr << a << " " << b << endl;

      logs.push(a);
      logs.push(b);
  }*/

  /*while(!logs.empty()) {
      cout << logs.top() << endl;
      logs.pop();
  }*/

  // cout << logs.top() << endl;
}
Problem p02598 (C++)

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

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (auto &&i : a)
    cin >> i;
  int L = -1, R = *max_element(a.begin(), a.end());
  while (R - L > 1) {
    int M = (L + R) / 2;
    int cnt = 0;
    for (auto &&i : a)
      cnt += (i - 1) / M;
    if (cnt > k)
      L = M;
    else
      R = M;
  }
  cout << R << "\n"s;
}


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

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (auto &&i : a)
    cin >> i;
  int L = 0, R = *max_element(a.begin(), a.end());
  while (R - L > 1) {
    int M = (L + R) / 2;
    int cnt = 0;
    for (auto &&i : a)
      cnt += (i - 1) / M;
    if (cnt > k)
      L = M;
    else
      R = M;
  }
  cout << R << "\n"s;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int start = 0;
  int end = 1000000001;
  int ans = 1000000000;
  while (start != end) {
    int mid = (start + end) / 2;
    int count = 0;
    rep(i, n) {
      if (a[i] % mid == 0)
        count += a[i] / mid;
      else
        count += a[i] / mid + 1;
      count--;
    }
    if (count > k)
      start = mid + 1;
    else {
      ans = min(mid, ans);
      end = mid;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int start = 1;
  int end = 1000000001;
  int ans = 1000000000;
  while (start != end) {
    int mid = (start + end) / 2;
    int count = 0;
    rep(i, n) {
      if (a[i] % mid == 0)
        count += a[i] / mid;
      else
        count += a[i] / mid + 1;
      count--;
    }
    if (count > k)
      start = mid + 1;
    else {
      ans = min(mid, ans);
      end = mid;
    }
  }
  cout << ans << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const int MAX = 200000;

int N, K;
int A[MAX];

bool check(int len) {
  int c = 0;
  for (int i = 0; i < N; i++) {
    c += (A[i] - 1) / len;
  }
  return c <= K;
}

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

  int l = 0;
  int r = pow(10, 9);
  int m = 0;
  int ans = pow(10, 9);

  while (l < r) {
    m = (l + r) / 2;
    if (check(m)) {
      r = m;
      ans = min(ans, m);
    } else {
      l = m + 1;
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const int MAX = 200000;

int N, K;
int A[MAX];

bool check(int len) {
  int c = 0;
  for (int i = 0; i < N; i++) {
    c += (A[i] - 1) / len;
  }
  return c <= K;
}

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

  int l = 1;
  int r = pow(10, 9);
  int m = 0;
  int ans = pow(10, 9);

  while (l < r) {
    m = (l + r) / 2;
    if (check(m)) {
      r = m;
      ans = min(ans, m);
    } else {
      l = m + 1;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02598 (C++)

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

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

ll INF = 1ll << 60;

vi a;

void chmax(int &a, int b) {
  if (a < b)
    a = b;
}

bool is_ok(int x, int k) {
  int sum = 0;
  rep(i, 0, a.size()) { sum += (a[i] + (x - 1)) / x - 1; }
  if (k >= sum)
    return true;
  else
    return false;
}

int binary_search(int key, int amax) {
  int ng = -1;
  int ok = amax;

  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;

    if (is_ok(mid, key))
      ok = mid;
    else
      ng = mid;
  }

  return ok;
}

int main() {
  int n, k;
  cin >> n >> k;
  int amax = 0;
  rep(i, 0, n) {
    int tmp;
    cin >> tmp;
    a.push_back(tmp);
    chmax(amax, tmp);
  }

  int ans = binary_search(k, amax);

  cout << ans << endl;

  return 0;
}

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

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

ll INF = 1ll << 60;

vi a;

void chmax(int &a, int b) {
  if (a < b)
    a = b;
}

bool is_ok(int x, int k) {
  int sum = 0;
  rep(i, 0, a.size()) { sum += (a[i] + (x - 1)) / x - 1; }
  if (k >= sum)
    return true;
  else
    return false;
}

int binary_search(int key, int amax) {
  int ng = 0;
  int ok = amax;

  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;

    if (is_ok(mid, key))
      ok = mid;
    else
      ng = mid;
  }

  return ok;
}

int main() {
  int n, k;
  cin >> n >> k;
  int amax = 0;
  rep(i, 0, n) {
    int tmp;
    cin >> tmp;
    a.push_back(tmp);
    chmax(amax, tmp);
  }

  int ans = binary_search(k, amax);

  cout << ans << endl;

  return 0;
}
Problem p02598 (C++)

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

#define ll long long int
int main() {
  ll n, p, q, x, y, i, k, a, b;

  cin >> n >> k;
  ll ara[n];
  for (i = 0; i < n; i++)
    cin >> ara[i];

  if (k == 0) {
    cout << *max_element(ara, ara + n) << endl;
    return 0;
  }

  ll mx = 1e18 + 100;

  ll l = 0, r = mx;
  bool bl = false;

  while (l <= r) {
    ll mid = (l + r) / 2ll;
    ll tot = 0;

    for (i = 0; i < n; i++) {
      ll x = (ara[i] - 1) / mid;
      tot += x;
    }

    if (tot <= k)
      mx = min(mx, mid), r = mid - 1;
    else
      l = mid + 1;
  }

  cout << mx << '\n';
}


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

#define ll long long int
int main() {
  ll n, p, q, x, y, i, k, a, b;

  cin >> n >> k;
  ll ara[n];
  for (i = 0; i < n; i++)
    cin >> ara[i];

  if (k == 0) {
    cout << *max_element(ara, ara + n) << endl;
    return 0;
  }

  ll mx = 1e18 + 100;

  ll l = 1, r = mx;
  bool bl = false;

  while (l <= r) {
    ll mid = (l + r) / 2ll;
    ll tot = 0;

    for (i = 0; i < n; i++) {
      ll x = (ara[i] - 1) / mid;
      tot += x;
    }

    if (tot <= k)
      mx = min(mx, mid), r = mid - 1;
    else
      l = mid + 1;
  }

  cout << mx << '\n';
}
Problem p02598 (C++)

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

int cut_times(int *logs, int n, int min_length) {
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += (logs[i] - 1) / min_length;
  }

  return ans;
}

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

  // sort(a, a+n, greater<int>());

  int left = 0;
  int right = 1000000000;

  while (left < right) {
    int mid = (left + right) / 2;
    if (cut_times(a, n, mid) > k) {
      left = mid + 1;
    } else if (cut_times(a, n, mid) <= k) {
      right = mid;
    }
  }

  cout << left << endl;

  return 0;
}

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

int cut_times(int *logs, int n, int min_length) {
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += (logs[i] - 1) / min_length;
  }

  return ans;
}

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

  // sort(a, a+n, greater<int>());

  int left = 1;
  int right = 1000000000;

  while (left < right) {
    int mid = (left + right) / 2;
    if (cut_times(a, n, mid) > k) {
      left = mid + 1;
    } else if (cut_times(a, n, mid) <= k) {
      right = mid;
    }
  }

  cout << left << endl;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

bool check(int n, vector<ll> &a, ll k, ll x) {
  ll cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (a[i] + x - 1) / x - 1;
  return k >= cnt;
}

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

  ll l = -1, r = 1e9, m;
  while (r - l > 1) {
    m = (r + l) / 2;

    if (check(n, a, k, m))
      r = m;
    else
      l = m;
  }

  cout << r << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

bool check(int n, vector<ll> &a, ll k, ll x) {
  ll cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (a[i] + x - 1) / x - 1;
  return k >= cnt;
}

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

  ll l = 0, r = 1e9, m;
  while (r - l > 1) {
    m = (r + l) / 2;

    if (check(n, a, k, m))
      r = m;
    else
      l = m;
  }

  cout << r << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define F first
#define S second
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pi = pair<int, int>;
using ld = long double;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int lb = -1, ub = 1e+9 + 1;
  while (ub - lb > 1) {
    int mid = (ub + lb) / 2;
    int cnt = 0;
    rep(i, n) { cnt += (a[i] - 1) / mid; }
    (cnt <= k ? ub : lb) = mid;
  }
  cout << ub << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define F first
#define S second
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pi = pair<int, int>;
using ld = long double;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int lb = 0, ub = 1e+9 + 1;
  while (ub - lb > 1) {
    int mid = (ub + lb) / 2;
    int cnt = 0;
    rep(i, n) { cnt += (a[i] - 1) / mid; }
    (cnt <= k ? ub : lb) = mid;
  }
  cout << ub << "\n";
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int nax = 2e5 + 10;
int n, k;
int a[nax];

int main() {
  cin.tie(0)->sync_with_stdio(false);
  cin >> n >> k;

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

  int l = 0, r = 1e9;
  int ans = -1;

  while (l <= r) {
    int mid = l + r >> 1;
    long long tot = 0;

    for (int i = 1; i <= n; ++i) {
      tot += max(0, (a[i] - 1) / mid);
    }

    if (tot <= k) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }

  assert(ans != -1);
  cout << ans << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int nax = 2e5 + 10;
int n, k;
int a[nax];

int main() {
  cin.tie(0)->sync_with_stdio(false);
  cin >> n >> k;

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

  int l = 1, r = 1e9;
  int ans = -1;

  while (l <= r) {
    int mid = l + r >> 1;
    long long tot = 0;

    for (int i = 1; i <= n; ++i) {
      tot += max(0, (a[i] - 1) / mid);
    }

    if (tot <= k) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }

  assert(ans != -1);
  cout << ans << '\n';
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <random>

using namespace std;
using namespace __gnu_pbds;

#define endl '\n'

typedef long long ll;
typedef pair<int, int> pii;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

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

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n;
  ll k;

  cin >> n >> k;

  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  ll lo = 0, hi = 2e9, opt = -1;
  while (lo <= hi) {
    ll mid = (lo + hi) >> 1, sum = 0;

    for (int i = 0; i < n; i++)
      sum += (a[i] + mid - 1) / mid - 1;

    if (sum <= k) {
      opt = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }

  cout << opt << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <random>

using namespace std;
using namespace __gnu_pbds;

#define endl '\n'

typedef long long ll;
typedef pair<int, int> pii;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

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

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n;
  ll k;

  cin >> n >> k;

  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  ll lo = 1, hi = 2e9, opt = -1;
  while (lo <= hi) {
    ll mid = (lo + hi) >> 1, sum = 0;

    for (int i = 0; i < n; i++)
      sum += (a[i] + mid - 1) / mid - 1;

    if (sum <= k) {
      opt = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }

  cout << opt << endl;

  return 0;
}
Problem p02598 (C++)

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

// Some constants don't work
#ifndef INT_MAX
#define INT_MAX 2147483647
#endif
#ifndef INT_MIN
#define INT_MIN -2147483648
#endif
// short hand tricks
typedef long long ll;

using namespace std;

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  ll N, K;
  cin >> N >> K;
  vector<ll> logs(N);
  for (int i = 0; i < N; i++) {
    cin >> logs[i];
  }
  ll low = 0;
  ll high = 1e9;
  ll res = 1e9;
  while (low <= high) {
    ll mid = low + (high - low) / 2;
    // see if mid is a solution
    // count how many cuts to make each log <=mid
    ll kcuts = 0;
    for (ll log : logs) {
      if (log > mid) {
        kcuts += log / mid;
        if (log % mid == 0)
          kcuts--;
      }
    }
    if (kcuts <= K) {
      res = min(mid, res);
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  cout << res << "\n";
  return 0;
}


Fixed submission:
// #include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

// Some constants don't work
#ifndef INT_MAX
#define INT_MAX 2147483647
#endif
#ifndef INT_MIN
#define INT_MIN -2147483648
#endif
// short hand tricks
typedef long long ll;

using namespace std;

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  ll N, K;
  cin >> N >> K;
  vector<ll> logs(N);
  for (int i = 0; i < N; i++) {
    cin >> logs[i];
  }
  ll low = 1;
  ll high = 1e9;
  ll res = 1e9;
  while (low <= high) {
    ll mid = low + (high - low) / 2;
    // see if mid is a solution
    // count how many cuts to make each log <=mid
    ll kcuts = 0;
    for (ll log : logs) {
      if (log > mid) {
        kcuts += log / mid;
        if (log % mid == 0)
          kcuts--;
      }
    }
    if (kcuts <= K) {
      res = min(mid, res);
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  cout << res << "\n";
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
/*
AtCoder Beginner Contest 174 - E - Logs
https://atcoder.jp/contests/abc174/tasks/abc174_e
*/

#include <bits/stdc++.h>
using namespace std;
#define FAST_INP                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
typedef long long ll;
const ll MOD = 1000000007;

int main() {
  FAST_INP;
  // binary search
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  ll l = 0, r = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    r = max(r, a[i]);
  }
  while (l != r) {
    int m = l + (r - l) / 2;
    ll kk = 0;
    for (int i = 0; i < n; i++) {
      kk += (a[i] - 1) / m;
    }
    if (kk <= k)
      r = m;
    else
      l = m + 1;
  }
  cout << l << endl;
  return 0;
}

Fixed submission:
/*
AtCoder Beginner Contest 174 - E - Logs
https://atcoder.jp/contests/abc174/tasks/abc174_e
*/

#include <bits/stdc++.h>
using namespace std;
#define FAST_INP                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
typedef long long ll;
const ll MOD = 1000000007;

int main() {
  FAST_INP;
  // binary search
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  ll l = 1, r = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    r = max(r, a[i]);
  }
  while (l != r) {
    int m = l + (r - l) / 2;
    ll kk = 0;
    for (int i = 0; i < n; i++) {
      kk += (a[i] - 1) / m;
    }
    if (kk <= k)
      r = m;
    else
      l = m + 1;
  }
  cout << l << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, k;
vector<int> a;

bool f(int m) {
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += (a[i] - 1) / m;
  }
  return cnt <= k;
}

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

  int ng = 0, ok = 1e9;
  for (int i = 0; i < 128; i++) {
    double m = (ng + ok) / 2;
    if (f(m))
      ok = m;
    else
      ng = m;
  }
  cout << ok << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, k;
vector<int> a;

bool f(int m) {
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += (a[i] - 1) / m;
  }
  return cnt <= k;
}

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

  int ng = 0, ok = 1e9;
  while (ng + 1 < ok) {
    double m = (ng + ok) / 2;
    if (f(m))
      ok = m;
    else
      ng = m;
  }
  cout << ok << endl;
}
Problem p02598 (C++)

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

typedef long long ll;

using namespace std;

ll is(vector<ll> &a, ll m, ll k) {
  ll count = 0;
  for (ll i = 0; i < a.size(); i++) {
    count = count + a[i] / m;
    if (a[i] % m == 0)
      count--;
  }

  if (count > k)
    return 0;
  else if (count == k)
    return 1;

  return 2;
}

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

  vector<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];

  sort(a.begin(), a.end());

  cout.setf(ios::fixed);
  cout << setprecision(0);
  if (!k) {
    cout << a[n - 1] << endl;
    return 0;
  } else if (k == 1) {
    if (n == 1)
      cout << ((ll)round(a[0] / 2)) << endl;
    else
      cout << max((ll)round(a[n - 1] / 2), a[n - 2]) << endl;
  }

  double ans = 0;
  ll l = 0, r = 1000000000;
  while (l <= r) {
    ll m = l + (r - l) / 2;
    ll count = is(a, m, k);
    if (count >= 1) {
      ans = m;
      r = m - 1;
    } else {
      l = m + 1;
    }
  }

  cout << ans << endl;
}


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

typedef long long ll;

using namespace std;

ll is(vector<ll> &a, ll m, ll k) {
  ll count = 0;
  for (ll i = 0; i < a.size(); i++) {
    count = count + a[i] / m;
    if (a[i] % m == 0)
      count--;
  }

  if (count > k)
    return 0;
  else if (count == k)
    return 1;

  return 2;
}

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

  vector<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];

  sort(a.begin(), a.end());

  cout.setf(ios::fixed);
  cout << setprecision(0);
  if (!k) {
    cout << a[n - 1] << endl;
    return 0;
  } else if (k == 1) {
    if (n == 1)
      cout << ((ll)round(a[0] / 2)) << endl;
    else
      cout << max((ll)round(a[n - 1] / 2), a[n - 2]) << endl;
  }

  double ans = 0;
  ll l = 1, r = 1000000000;
  while (l <= r) {
    ll m = l + (r - l) / 2;
    ll count = is(a, m, k);
    if (count >= 1) {
      ans = m;
      r = m - 1;
    } else {
      l = m + 1;
    }
  }

  cout << ans << endl;
}
Problem p02598 (C++)

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
#define LINF 1e18
typedef long long ll;

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

  int ng = -1;
  int ok = A[N - 1];
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    ll cnt = 0;
    REP(i, N) { cnt += (A[i] - 1) / mid; }
    if (cnt <= K) { // ここに条件をかく
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << endl;

  return 0;
}

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
#define LINF 1e18
typedef long long ll;

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

  int ng = 0;
  int ok = A[N - 1];
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    ll cnt = 0;
    REP(i, N) { cnt += (A[i] - 1) / mid; }
    if (cnt <= K) { // ここに条件をかく
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << endl;

  return 0;
}