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

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

const ll MAXN = 1e9;
ll n, k;
vector<ll> a;

bool check(ll x) {
  ll operations = 0;
  for (int i = 0; i < n; i++) {
    operations += ((a[i] - 1) / x);
  }
  if (operations > k) {
    return false;
  }
  return true;
}

void search() {
  ll left = 0, right = MAXN, ans = -1;
  while (left < right) {
    ll mid = (left + right) / 2;
    if (!check(mid)) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }
  cout << right << endl;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    ll nums;
    cin >> nums;
    a.push_back(nums);
  }
  sort(a.begin(), a.end());
  search();
  return 0;
}

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

const ll MAXN = 1e9;
ll n, k;
vector<ll> a;

bool check(ll x) {
  ll operations = 0;
  for (int i = 0; i < n; i++) {
    operations += ((a[i] - 1) / x);
  }
  if (operations > k) {
    return false;
  }
  return true;
}

void search() {
  ll left = 1, right = MAXN;
  while (left < right) {
    ll mid = (left + right) / 2;
    if (!check(mid)) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }
  cout << right << endl;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    ll nums;
    cin >> nums;
    a.push_back(nums);
  }
  sort(a.begin(), a.end());
  search();
  return 0;
}
Problem p02598 (C++)

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

typedef long long ll;

using namespace std;

const ll mod = 1000000007;

bool judge(ll x, ll *A, ll N, ll K) {
  ll p = 0;

  for (int i = 0; i < N; ++i) {

    p += (A[i] - 1) / x;
  }
  // cout<<"x: "<<x<<" p:"<<p<<endl;

  return p <= K;
}

ll binarysearch(ll *A, ll N, ll K) {
  ll ok = 1e9;
  ll ng = -1;
  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    if (judge(mid, A, N, K)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  return ok;
}

int main() {
  ll N, K;
  cin >> N >> K;
  ll A[N];

  ll end;

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

  ll ans = binarysearch(A, N, K);
  cout << ans << endl;
}


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

typedef long long ll;

using namespace std;

const ll mod = 1000000007;

bool judge(ll x, ll *A, ll N, ll K) {
  ll p = 0;

  for (int i = 0; i < N; ++i) {

    p += (A[i] - 1) / x;
  }
  // cout<<"x: "<<x<<" p:"<<p<<endl;

  return p <= K;
}

ll binarysearch(ll *A, ll N, ll K) {
  ll ok = 1e9;
  ll ng = 0;
  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    if (judge(mid, A, N, K)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  return ok;
}

int main() {
  ll N, K;
  cin >> N >> K;
  ll A[N];

  ll end;

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

  ll ans = binarysearch(A, N, K);
  cout << ans << endl;
}
Problem p02598 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define err(args...)                                                           \
  {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream &operator>>(istream &is, vector<T> &a) {
  for (T &x : a) {
    is >> x;
  }
  return is;
}
template <typename X, typename Y>
istream &operator>>(istream &is, pair<X, Y> &p) {
  return is >> p.first >> p.second;
}
template <typename T, typename Can>
T bsearch(T L, T R, const Can &can, bool left_feasible = true) {
  static_assert(is_convertible<decltype(can), function<bool(T)>>::value,
                "can must be bool(T)");
  T &feasible = left_feasible ? L : R;
  T &infeasible = left_feasible ? R : L;
  while (R - L > 1) {
    T M = L + (R - L) / 2;
    (can(M) ? feasible : infeasible) = M;
  }
  return feasible;
}
ll ceildiv(ll x, ll y) { return (x + y - 1) / y; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  cin >> a;
  cout << bsearch(
              0, *max_element(a.begin(), a.end()),
              [&](int max_piece) {
                ll needed_cuts = 0;
                for (int ai : a) {
                  needed_cuts += bsearch(
                      -1, ai - 1,
                      [&](int cuts) {
                        return ceildiv(ai, cuts + 1) <= max_piece;
                      },
                      false);
                }
                return needed_cuts <= k;
              },
              false)
       << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define err(args...)                                                           \
  {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream &operator>>(istream &is, vector<T> &a) {
  for (T &x : a) {
    is >> x;
  }
  return is;
}
template <typename X, typename Y>
istream &operator>>(istream &is, pair<X, Y> &p) {
  return is >> p.first >> p.second;
}
template <typename T, typename Can>
T bsearch(T L, T R, const Can &can, bool left_feasible = true) {
  static_assert(is_convertible<decltype(can), function<bool(T)>>::value,
                "can must be bool(T)");
  T &feasible = left_feasible ? L : R;
  T &infeasible = left_feasible ? R : L;
  while (R - L > 1) {
    T M = L + (R - L) / 2;
    (can(M) ? feasible : infeasible) = M;
  }
  return feasible;
}
ll ceildiv(ll x, ll y) { return (x + y - 1) / y; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  cin >> a;
  cout << bsearch(
              0, *max_element(a.begin(), a.end()),
              [&](int max_piece) {
                ll needed_cuts = 0;
                for (int ai : a) {
                  needed_cuts += ceildiv(ai, max_piece) - 1;
                }
                return needed_cuts <= k;
              },
              false)
       << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int N_MAX = 200000;
typedef long long ll;
int main() {
  ll n, i, l, r, c, k, t, s = 0;
  ll a[N_MAX];
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
  }
  sort(a, a + n, greater<ll>());
  l = s / (n + k);
  r = a[0];
  while (l < r) {
    c = (l + r) / 2;
    t = 0;
    for (i = 0; i < n; i++) {
      t += (a[i] + c - 1) / c - 1;
      if (t > k)
        break;
    }
    if (t > k) {
      l = c + 1;
    } else {
      r = c;
    }
  }
  cout << l << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int N_MAX = 200000;
typedef long long ll;
int main() {
  ll n, i, l, r, c, k, t, s = 0;
  ll a[N_MAX];
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
  }
  sort(a, a + n, greater<ll>());
  l = max(s / (n + k), (ll)1);
  r = a[0];
  while (l < r) {
    c = (l + r) / 2;
    t = 0;
    for (i = 0; i < n; i++) {
      t += (a[i] + c - 1) / c - 1;
      if (t > k)
        break;
    }
    if (t > k) {
      l = c + 1;
    } else {
      r = c;
    }
  }
  cout << l << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#define ll long long
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define endl '\n'
#define eps 0.000000001
#define pb push_back
#define mem(a, b) memset(a, b, sizeof(a))
#define IO                                                                     \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
using namespace std;
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 5;
int n, k, a[maxn];
bool check(int x) {
  int ok = 0;
  rep(i, 1, n) { ok += (a[i] - 1) / x; }
  return ok <= k;
}
int main() {
  cin >> n >> k;
  rep(i, 1, n) cin >> a[i];
  int l = 0, r = 1000000000;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#define ll long long
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define endl '\n'
#define eps 0.000000001
#define pb push_back
#define mem(a, b) memset(a, b, sizeof(a))
#define IO                                                                     \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
using namespace std;
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 5;
int n, k, a[maxn];
bool check(int x) {
  int ok = 0;
  rep(i, 1, n) { ok += (a[i] - 1) / x; }
  return ok <= k;
}
int main() {
  cin >> n >> k;
  rep(i, 1, n) cin >> a[i];
  int l = 1, r = 1e9;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
}
Problem p02598 (C++)

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

using namespace std;
typedef long long ll;
typedef long double ld;
#define X first;
#define Y second;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
const ll INF = 2e18;
const int inf = 1e9 + 10;
const int maxN = 2e5 + 10;
const int maxK = 1e4 + 10;
const int maxD = 15;
const int maxH = 100;
const int MOD = 1e9 + 7;

ll n, k;
ll a[maxN];
bool check(ll x) {
  ll cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] >= x) {
      cnt += ((a[i] / x) - (a[i] % x == 0));
    }
  }
  if (cnt <= k) {
    return true;
  } else {
    return false;
  }
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  ll L = 0, R = 1e9;
  ll ans = R;
  while (L <= R) {
    ll mid = (L + R) / 2;
    if (check(mid)) {
      ans = mid;
      R = mid - 1;
    } else {
      L = mid + 1;
    }
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;
typedef long long ll;
typedef long double ld;
#define X first;
#define Y second;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
const ll INF = 2e18;
const int inf = 1e9 + 10;
const int maxN = 2e5 + 10;
const int maxK = 1e4 + 10;
const int maxD = 15;
const int maxH = 100;
const int MOD = 1e9 + 7;

ll n, k;
ll a[maxN];
bool check(ll x) {
  ll cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] >= x) {
      cnt += ((a[i] / x) - (a[i] % x == 0));
    }
  }
  if (cnt <= k) {
    return true;
  } else {
    return false;
  }
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  ll L = 1, R = 1e9;
  ll ans = R;
  while (L <= R) {
    ll mid = (L + R) / 2;
    if (check(mid)) {
      ans = mid;
      R = mid - 1;
    } else {
      L = mid + 1;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02598 (C++)

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

typedef long long int ll;
using ii = pair<ll, ll>;
#define all(o) (o).begin(), (o).end()
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ld long double
#define eps 0.00000000001

class prioritize {
public:
  bool operator()(ii &p1, ii &p2) { return p1.F < p2.F; }
};

int main() {

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

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, k;
  cin >> n >> k;

  ll arr[n];
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
  }

  sort(arr, arr + n);
  ll left = 0;
  ll right = 1e9;
  ll mid = 0;
  ll ans = 1e9;

  while (left <= right) {
    mid = left + (right - left) / 2;

    ll count = 0;

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

    if (count <= k) {
      right = mid - 1;
      ans = min(ans, mid);
    } else
      left = mid + 1;
  }

  if (k == 0 || ans == 1e9)
    cout << arr[n - 1] << "\n";
  else
    cout << ans << "\n";
}

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

typedef long long int ll;
using ii = pair<ll, ll>;
#define all(o) (o).begin(), (o).end()
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ld long double
#define eps 0.00000000001

class prioritize {
public:
  bool operator()(ii &p1, ii &p2) { return p1.F < p2.F; }
};

int main() {

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

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, k;
  cin >> n >> k;

  ll arr[n];
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
  }

  sort(arr, arr + n);
  ll left = 1;
  ll right = 1e9;
  ll mid = 0;
  ll ans = 1e9;

  while (left <= right) {
    mid = left + (right - left) / 2;

    ll count = 0;

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

    if (count <= k) {
      right = mid - 1;
      ans = min(ans, mid);
    } else
      left = mid + 1;
  }

  if (k == 0 || ans == 1e9)
    cout << arr[n - 1] << "\n";
  else
    cout << ans << "\n";
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 10;
int n, k, a[N];
int check(int x) {
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans += (a[i] + x - 1) / x - 1;
  return ans <= k;
}
signed main() {
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  int l = 0, r = 2e9;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 10;
int n, k, a[N];
int check(int x) {
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans += (a[i] + x - 1) / x - 1;
  return ans <= k;
}
signed main() {
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  int l = 1, r = 2e9;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define ff first
#define ss second

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

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

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll maxa = *max_element(a.begin(), a.end());
  ll lo = 0, hi = maxa + 1;

  while (lo < hi) {
    ll mid = (lo + hi) / 2;
    ll m = maxa - mid;
    ll cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += a[i] / m;
      if (a[i] % m == 0)
        cnt -= 1;
    }
    if (cnt <= k)
      lo = mid + 1;
    else
      hi = mid;
  }
  cout << maxa - lo + 1;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define ff first
#define ss second

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

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

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll maxa = *max_element(a.begin(), a.end());
  ll lo = 0, hi = maxa;

  while (lo < hi) {
    ll mid = (lo + hi) / 2;
    ll m = maxa - mid;
    ll cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += a[i] / m;
      if (a[i] % m == 0)
        cnt -= 1;
    }
    if (cnt <= k)
      lo = mid + 1;
    else
      hi = mid;
  }
  cout << maxa - lo + 1;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = (1 ? 1e9 + 7 : 998244353);
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
ll a, b, c, d, e, f, n, T;

double z[200005];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> a >> b;
  double maxx = 0;
  for (int i = 0; i < a; i++)
    cin >> z[i], maxx = max(maxx, z[i]);
  double minn = 0;
  double mid;
  while (maxx - minn > 1e-7) {
    mid = (maxx + minn) / 2;
    c = 0;
    for (int i = 0; i < a; i++)
      c += ceil(z[i] / mid - 1);
    if (c > b)
      minn = mid;
    else
      maxx = mid;
  }
  cout << (ll)ceil(maxx) << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = (1 ? 1e9 + 7 : 998244353);
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
ll a, b, c, d, e, f, n, T;

double z[200005];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> a >> b;
  double maxx = 0;
  for (int i = 0; i < a; i++)
    cin >> z[i], maxx = max(maxx, z[i]);
  double minn = 0;
  double mid;
  while (maxx - minn > 1e-6) {
    mid = (maxx + minn) / 2;
    c = 0;
    for (int i = 0; i < a; i++)
      c += ceil(z[i] / mid - 1);
    if (c > b)
      minn = mid;
    else
      maxx = mid;
  }
  cout << (ll)ceil(maxx) << endl;
}
Problem p02598 (C++)

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

vector<ll> a;
ll n, k;

bool ok(ll x) {
  ll res = 0;
  for (ll i = 0; i < n; i++)
    if (a[i] > x) {
      res += a[i] / x;
      if (a[i] % x == 0)
        res--;
    }
  return res <= k;
}

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

  cin >> n >> k;
  a.resize(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll x = 0;
  for (ll b = 300000; b; b /= 2)
    while (!ok(x + b))
      x += b;
  cout << x + 1 << '\n';
}


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

vector<ll> a;
ll n, k;

bool ok(ll x) {
  ll res = 0;
  for (ll i = 0; i < n; i++)
    if (a[i] > x) {
      res += a[i] / x;
      if (a[i] % x == 0)
        res--;
    }
  return res <= k;
}

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

  cin >> n >> k;
  a.resize(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll x = 0;
  for (ll b = 2000000000; b; b /= 2)
    while (!ok(x + b))
      x += b;
  cout << x + 1 << '\n';
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  int hmax = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > hmax)
      hmax = a[i];
  }
  int l = 0, h = hmax;
  while (l != h && h != 0) {
    int m = l + (h - l) / 2;

    ll cnt = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] % m == 0) {
        cnt += (a[i] / m - 1);
      } else {
        cnt += a[i] / m;
      }
    }
    if (cnt <= k) {
      h = m;
    } else {
      l = m + 1;
    }
  }
  cout << l;
  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  int hmax = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > hmax)
      hmax = a[i];
  }
  int l = 1, h = hmax;
  while (l != h) {
    int m = l + (h - l) / 2;

    ll cnt = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] % m == 0) {
        cnt += (a[i] / m - 1);
      } else {
        cnt += a[i] / m;
      }
    }
    if (cnt <= k) {
      h = m;
    } else {
      l = m + 1;
    }
  }
  cout << l;
  return 0;
}
Problem p02598 (C++)

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

int main() {
  int n, k;
  cin >> n >> k;
  int a[n], ng = -1, ok = 1e9 + 1, mid, tk;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  while (ok - ng > 1) {
    mid = ng + (ok - ng) / 2;
    tk = 0;
    for (int i = 0; i < n; i++)
      tk += max(0, a[i] / mid - (a[i] % mid == 0 ? 1 : 0));
    (tk <= k ? ok : ng) = mid;
  }
  cout << ok << endl;
}


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

int main() {
  int n, k;
  cin >> n >> k;
  int a[n], ng = 0, ok = 1e9 + 1, mid, tk;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  while (ok - ng > 1) {
    mid = ng + (ok - ng) / 2;
    tk = 0;
    for (int i = 0; i < n; i++)
      tk += max(0, a[i] / mid - (a[i] % mid == 0 ? 1 : 0));
    (tk <= k ? ok : ng) = mid;
  }
  cout << ok << 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 rep1(i, n) for (int i = 1; i <= n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define repr1(i, n) for (int i = n; i >= 1; i--)
#define all(v) v.begin(), v.end()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos(-1);
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;
}

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

  if (k == 0) {
    ll m = -1;
    rep(i, n) chmax(m, a[i]);
    cout << m << endl;
    return 0;
  }

  ll ng = -1, ok = 2e18;
  while (ok - ng > 1) {
    ll mid = (ok + ng) / 2;
    ll sum = 0;
    rep(i, n) {
      if (a[i] <= mid)
        continue;
      sum += a[i] / mid;
    }
    if (sum > k)
      ng = mid; // 短く切りすぎ
    else
      ok = mid;
  }
  cout << ok << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define repr1(i, n) for (int i = n; i >= 1; i--)
#define all(v) v.begin(), v.end()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos(-1);
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;
}

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

  if (k == 0) {
    ll m = -1;
    rep(i, n) chmax(m, a[i]);
    cout << m << endl;
    return 0;
  }

  ll ng = 0, ok = 2e18;
  while (ok - ng > 1) {
    ll mid = (ok + ng) / 2;
    ll sum = 0;
    rep(i, n) {
      if (a[i] <= mid)
        continue;
      sum += a[i] / mid;
    }
    if (sum > k)
      ng = mid; // 短く切りすぎ
    else
      ok = mid;
  }
  cout << ok << endl;
}
Problem p02598 (C++)

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

using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;

#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i, k, N) for (int i = k; i < N; i++)
#define MP make_pair
#define MT make_tuple // tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(), (x).end()

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

struct wood {
  double L;
  ll N;
  double M;
  bool operator<(const wood &a) const { return L < a.L; }
};

int main() {
  ll N, K;
  cin >> N >> K;
  v A(N);
  rep(i, 0, N) cin >> A[i];
  ll l = 0;
  ll r = 1000000000;

  while (l != r) {
    ll sum = 0;
    ll now = (l + r) / 2;
    rep(i, 0, N) { sum += ((A[i] - 1) / now); }
    if (sum > K) {
      l = now + 1;
    } else {
      r = now;
    }
  }
  cout << r;

  return 0;
}

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

using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;

#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i, k, N) for (int i = k; i < N; i++)
#define MP make_pair
#define MT make_tuple // tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(), (x).end()

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

struct wood {
  double L;
  ll N;
  double M;
  bool operator<(const wood &a) const { return L < a.L; }
};

int main() {
  ll N, K;
  cin >> N >> K;
  v A(N);
  rep(i, 0, N) cin >> A[i];
  ll l = 0;
  ll r = 1000000000;

  while (l != r) {
    ll sum = 0;
    ll now = (l + r) / 2;
    if (now == 0) {
      cout << 1;
      return 0;
    }
    rep(i, 0, N) { sum += ((A[i] - 1) / now); }
    if (sum > K) {
      l = now + 1;
    } else {
      r = now;
    }
  }
  cout << r;

  return 0;
}
Problem p02598 (C++)

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

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

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

int main() {
  ll N, K;
  cin >> N >> K;
  ll ok = 1e9;
  ll ng = 1;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int t = 40;
  while (t--) {
    ll mid = (ok + ng) / 2;
    ll count = 0;
    for (int i = 0; i < N; i++) {
      count += A[i] / mid;
      if (A[i] % mid == 0)
        count--;
    }
    if (count > K)
      ng = mid;
    else
      ok = mid;
  }
  cout << ok << endl;
}
Problem p02598 (C++)

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

using namespace std;
using ll = long long;

ll A[100006];
int N, K;

bool f(ll x) {
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] <= x)
      continue;
    cnt += (int)ceil(1.0 * A[i] / x) - 1;
  }
  return cnt <= K;
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  ll ub = 1LL << 60;
  ll lb = 0;
  while (ub - lb > 1) {
    ll mid = (ub + lb) / 2;
    if (f(mid))
      ub = mid;
    else
      lb = mid;
  }
  cout << ub << endl;
}

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

using namespace std;
using ll = long long;

ll A[200006];
int N, K;

bool f(ll x) {
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] <= x)
      continue;
    cnt += (int)ceil(1.0 * A[i] / x) - 1;
  }
  return cnt <= K;
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  ll ub = 1LL << 60;
  ll lb = 0;
  while (ub - lb > 1) {
    ll mid = (ub + lb) / 2;
    if (f(mid))
      ub = mid;
    else
      lb = mid;
  }
  cout << ub << endl;
}
Problem p02598 (C++)

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

#define fi first
#define se second
#define pb push_back
#define mod(n, k) ((((n) % (k)) + (k)) % (k))
#define forn(i, a, b) for (int i = a; i < b; i++)
#define forr(i, a, b) for (int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const int maxn = 200000 + 10;
int N, K, A[maxn];

int get(int act, int x) {
  if (act <= x)
    return 0;
  return 1 + get(act - x, x);
}
int can(int x) {
  int cnt = 0;
  forn(i, 0, N) {
    if (A[i] <= x)
      continue;
    cnt += get(A[i], x);
  }
  if (cnt > K)
    return 0;
  return 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> K;
  forn(i, 0, N) cin >> A[i];
  int ini = 1, fin = 1e9 + 1;
  while (ini < fin) {
    int mit = (ini + fin) >> 1;
    if (can(mit))
      fin = mit;
    else
      ini = mit + 1;
  }
  cout << ini << '\n';
  return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/


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

#define fi first
#define se second
#define pb push_back
#define mod(n, k) ((((n) % (k)) + (k)) % (k))
#define forn(i, a, b) for (int i = a; i < b; i++)
#define forr(i, a, b) for (int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const int maxn = 200000 + 10;
int N, K, A[maxn];

int get(int act, int x) {
  // if(act <= x)return 0;
  // return 1+get(act-x,x);
  return act / x;
}
int can(int x) {
  int cnt = 0;
  forn(i, 0, N) {
    if (A[i] <= x)
      continue;
    cnt += get(A[i], x);
  }
  if (cnt > K)
    return 0;
  return 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> K;
  forn(i, 0, N) cin >> A[i];
  int ini = 1, fin = 1e9 + 1;
  while (ini < fin) {
    int mit = (ini + fin) >> 1;
    if (can(mit))
      fin = mit;
    else
      ini = mit + 1;
  }
  cout << ini << '\n';
  return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/
Problem p02598 (C++)

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

using namespace std;
typedef long long int ll;
typedef long double ld;
#define REP(i, n) for (ll i = 0LL; i < (ll)(n); i++)
#define REPR(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, n, m) for (ll i = (ll)n; i < (ll)(m); i++)
#define ALL(x) (x).begin(), (x).end()
#define MOD 1000000007
#define INF (1LL << 62)

#define PI (acos(-1))
#define PRINT(x) std::cout << x << endl

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll countDigit(ll n) { return floor(log10(n) + 1); }
typedef pair<ll, ll> P;
static const ll dx[8] = {0, 1, 0, -1, 1, -1, 1, -1},
                dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); ++i)
    os << v[i] << " ";
  return os;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &m) {
  for (auto p : m)
    os << "<" << p.first << ", " << p.second << "> ";
  return os;
}

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

  vector<ll> A(N);
  REP(i, N) { cin >> A[i]; }

  ll left = 0.;
  ll right = 1e9;
  while (right >= left) {
    ll mid = (left + right) / 2;
    ll tK = K + N;
    bool flag = false;
    REP(i, N) {
      ll n = (A[i] + mid - 1) / mid;
      if (n <= tK) {
        tK -= n;
      } else {
        i = N;
        flag = true;
      }
    }
    if (flag) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  cout << left << endl;
  return 0;
}


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

using namespace std;
typedef long long int ll;
typedef long double ld;
#define REP(i, n) for (ll i = 0LL; i < (ll)(n); i++)
#define REPR(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, n, m) for (ll i = (ll)n; i < (ll)(m); i++)
#define ALL(x) (x).begin(), (x).end()
#define MOD 1000000007
#define INF (1LL << 62)

#define PI (acos(-1))
#define PRINT(x) std::cout << x << endl

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll countDigit(ll n) { return floor(log10(n) + 1); }
typedef pair<ll, ll> P;
static const ll dx[8] = {0, 1, 0, -1, 1, -1, 1, -1},
                dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); ++i)
    os << v[i] << " ";
  return os;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &m) {
  for (auto p : m)
    os << "<" << p.first << ", " << p.second << "> ";
  return os;
}

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

  vector<ll> A(N);
  REP(i, N) { cin >> A[i]; }

  ll left = 1;
  ll right = 1e9;
  while (right >= left) {
    ll mid = (left + right) / 2;
    ll tK = K + N;
    bool flag = false;
    REP(i, N) {
      ll n = (A[i] + mid - 1) / mid;
      if (n <= tK) {
        tK -= n;
      } else {
        i = N;
        flag = true;
      }
    }
    if (flag) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  cout << left << endl;
  return 0;
}
Problem p02598 (C++)

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

using namespace std;

#define int long long
#define endl '\n'
#define maxn 200010
const int MOD = 1000000007;

int v[maxn];
int n;
int k;

bool check(int m) {
  int times = 0;
  for (int i = 0; i < n; i++) {
    times += v[i] / m + (v[i] % m > 0) - 1;
  }
  return times <= k;
}

void solve() {
  cin >> n >> k;

  for (int i = 0; i < n; ++i)
    cin >> v[i];
  int lo = 0, hi = 1e12, mid, ans = 0;
  while (lo <= hi) {
    mid = lo + hi >> 1;
    if (check(mid)) {
      ans = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }
  cout << ans << endl;
}

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

  int t = 1;
  //    cin >> t;

  while (t--)
    solve();
  return 0;
}

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

using namespace std;

#define int long long
#define endl '\n'
#define maxn 200010
const int MOD = 1000000007;

int v[maxn];
int n;
int k;

bool check(int m) {
  int times = 0;
  for (int i = 0; i < n; i++) {
    times += v[i] / m + (v[i] % m > 0) - 1;
  }
  return times <= k;
}

void solve() {
  cin >> n >> k;

  for (int i = 0; i < n; ++i)
    cin >> v[i];
  int lo = 1, hi = 1e12, mid, ans = 0;
  while (lo <= hi) {
    mid = lo + hi >> 1;
    if (check(mid)) {
      ans = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }
  cout << ans << endl;
}

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

  int t = 1;
  //    cin >> t;

  while (t--)
    solve();
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;

ll N, K;
ll A[200200];

bool IsCuttable(ll len) {
  ll count = 0;
  for (int i = 0; i < N; i++) {
    ll count_ = A[i] / len;
    if (A[i] % len != 0 && A[i] > len) {
      count_++;
    }
    if (count_ > 0) {
      count_--;
    }
    count += count_;
  }
  if (count <= K) {
    return true;
  } else {
    return false;
  }
}

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

  ll min = 0;
  ll max = 1000000000;
  while (max - min > 0) {
    ll mid = min + (max - min) / 2;
    // cout << mid << endl;
    if (IsCuttable(mid)) {
      max = mid;
    } else {
      min = mid + 1;
    }
  }
  cout << min << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;

ll N, K;
ll A[200200];

bool IsCuttable(ll len) {
  ll count = 0;
  for (int i = 0; i < N; i++) {
    ll count_ = A[i] / len;
    if (A[i] % len != 0 && A[i] > len) {
      count_++;
    }
    if (count_ > 0) {
      count_--;
    }
    count += count_;
  }
  if (count <= K) {
    return true;
  } else {
    return false;
  }
}

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

  ll min = 1;
  ll max = 1000000000;
  while (max - min > 0) {
    ll mid = min + (max - min) / 2;
    // cout << mid << endl;
    if (IsCuttable(mid)) {
      max = mid;
    } else {
      min = mid + 1;
    }
  }
  cout << min << endl;
  return 0;
}
Problem p02598 (C++)

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

#define ll long long

int main() {

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

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

  int n, k;
  cin >> n >> k;
  int A[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];
  int a = 0, b = 1e9;
  while (a < b) {
    // cout<<a<<" "<<b<<endl;
    int mid = (a + b) / 2;
    int times = 0;
    for (int i = 0; i < n; i++) {
      int y = A[i] / mid;
      if (A[i] % mid == 0)
        y--;
      times += y;
    }
    if (times > k)
      a = mid + 1;
    else
      b = mid;
  }
  cout << a;
}

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

#define ll long long

int main() {

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

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

  int n, k;
  cin >> n >> k;
  int A[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];
  int a = 1, b = 1e9;
  while (a < b) {
    // cout<<a<<" "<<b<<endl;
    int mid = (a + b) / 2;
    int times = 0;
    for (int i = 0; i < n; i++) {
      int y = A[i] / mid;
      if (A[i] % mid == 0)
        y--;
      times += y;
    }
    if (times > k)
      a = mid + 1;
    else
      b = mid;
  }
  cout << a;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 100000000;
#define rep1(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, k, n) for (ll i = k; i < (n); i++)
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;
}

ll n, k;
vector<ll> a;
bool check(ll x) {
  ll count = 0;
  rep1(i, n) count += (a[i] - 1) / x;
  if (count <= k)
    return true;
  else
    return false;
}

int main() {
  cin >> n >> k;
  a.resize(n);
  rep1(i, n) cin >> a[i];
  ll ans = 0;
  ll left = -1, right = 1e9 + 1;
  while (right > left) {
    ll mid = (right + left) / 2;
    if (check(mid))
      right = mid;
    else
      left = mid + 1;
  }
  cout << left << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 100000000;
#define rep1(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, k, n) for (ll i = k; i < (n); i++)
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;
}

ll n, k;
vector<ll> a;
bool check(ll x) {
  ll count = 0;
  rep1(i, n) count += (a[i] - 1) / x;
  if (count <= k)
    return true;
  else
    return false;
}

int main() {
  cin >> n >> k;
  a.resize(n);
  rep1(i, n) cin >> a[i];
  ll ans = 0;
  ll left = 1, right = 1e9;
  while (right > left) {
    ll mid = (right + left) / 2;
    if (check(mid))
      right = mid;
    else
      left = mid + 1;
  }
  cout << left << endl;
}
Problem p02598 (C++)

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

using namespace std;
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d %d", &a, &b)
#define sll(a) scanf("%lld", &a)
#define sll2(a, b) scanf("%lld %lld", &a, &b)
#define mem(a, i) memset(a, i, sizeof(a))
#define pb push_back
#define int long long
#define ll long long
#define lson node << 1
#define rson (node << 1) + 1
#define endl "\n"

const int maxn = 2e6 + 10;
const ll mod = 1e9 + 7;
const double pi = acos(-1);

ll qpow(ll a, ll n) {
  ll b = 1;
  while (n) {
    if (n & 1)
      b = b * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return b;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
// int prime[maxn],s,v[maxn];
// void prime_init()
//{
//     for(int i=2; i<=maxn; i++)
//     {
//         if(!v[i])
//         {
//             prime[s++]=i;
//             for(int j=1; j*i<=maxn; j++)
//                 v[i*j]=1;
//         }
//     }
// }
int n, k;
int a[maxn];
int check(int x) {
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i] / x;
    if (a[i] % x == 0)
      sum--;
    if (sum > k)
      return 0;
  }
  return 1;
}
signed main() {
  sll2(n, k);
  int l = 0, r = 0;
  for (int i = 0; i < n; i++) {
    sll(a[i]);
    r = max(r, a[i]);
  }
  r++;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%lld", r);
  return 0;
}


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

using namespace std;
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d %d", &a, &b)
#define sll(a) scanf("%lld", &a)
#define sll2(a, b) scanf("%lld %lld", &a, &b)
#define mem(a, i) memset(a, i, sizeof(a))
#define pb push_back
#define int long long
#define ll long long
#define lson node << 1
#define rson (node << 1) + 1
#define endl "\n"

const int maxn = 2e6 + 10;
const ll mod = 1e9 + 7;
const double pi = acos(-1);

ll qpow(ll a, ll n) {
  ll b = 1;
  while (n) {
    if (n & 1)
      b = b * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return b;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
// int prime[maxn],s,v[maxn];
// void prime_init()
//{
//     for(int i=2; i<=maxn; i++)
//     {
//         if(!v[i])
//         {
//             prime[s++]=i;
//             for(int j=1; j*i<=maxn; j++)
//                 v[i*j]=1;
//         }
//     }
// }
int n, k;
int a[maxn];
int check(int x) {
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i] / x;
    if (a[i] % x == 0)
      sum--;
    if (sum > k)
      return 0;
  }
  return 1;
}
signed main() {
  sll2(n, k);
  int l = 1, r = 0;
  for (int i = 0; i < n; i++) {
    sll(a[i]);
    r = max(r, a[i]);
  }
  r++;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%lld", r);
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define init(a, i)                                                             \
  for (int k = 0; k < (i); k++)                                                \
  (a)[k] = 0
#define in(a, i)                                                               \
  for (int k = 0; k < (i); k++)                                                \
  cin >> (a)[k]
#define all(a) (a).begin(), (a).end()
#define el(a) (a).end() - (a).begin()
#define mod 1000000007
#define inf 2147483647
#define range(x, a, b) (a) <= x &&x <= (b)
int n, k;
int a[200000];
bool can(int length) {
  ll WorkCount = 0;
  rep(i, n) { WorkCount += (a[i] + (length - 1)) / length - 1; }
  if (WorkCount > k)
    return 0;
  else
    return 1;
}

int main() {
  cin >> n >> k;
  in(a, n);
  int left = -1, right = 1000000000; // 2分探索(left<anser<=right)
  int middle;
  while (right - left != 1) {
    middle = (left + right) / 2;
    if (can(middle)) {
      right = middle;
    } else {
      left = middle;
    }
  }
  cout << right;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define init(a, i)                                                             \
  for (int k = 0; k < (i); k++)                                                \
  (a)[k] = 0
#define in(a, i)                                                               \
  for (int k = 0; k < (i); k++)                                                \
  cin >> (a)[k]
#define all(a) (a).begin(), (a).end()
#define el(a) (a).end() - (a).begin()
#define mod 1000000007
#define inf 2147483647
#define range(x, a, b) (a) <= x &&x <= (b)
int n, k;
int a[200000];
bool can(int length) {
  ll WorkCount = 0;
  rep(i, n) { WorkCount += (a[i] + (length - 1)) / length - 1; }
  if (WorkCount > k)
    return 0;
  else
    return 1;
}

int main() {
  cin >> n >> k;
  in(a, n);
  int left = 0, right = 1000000000; // 2分探索(left<anser<=right)
  int middle;
  while (right - left != 1) {
    middle = (left + right) / 2;
    if (can(middle)) {
      right = middle;
    } else {
      left = middle;
    }
  }
  cout << right;
  return 0;
}
Problem p02598 (C++)

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

int n, k;
int a[200010];
char check(const int &n);

int main() {
  scanf("%d%d", &n, &k);
  int maxv = 0;
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    sum += a[i], maxv = max(maxv, a[i]);
  }

  int l = sum / (n + k), r = maxv;
  while (l <= r) {
    int m = l + r >> 1;
    if (check(m))
      r = m - 1;
    else
      l = m + 1;
  }
  printf("%d\n", l);
  return 0;
}

char check(const int &m) {
  int sum = 0;
  for (int i = 0; i < n; ++i)
    sum += (a[i] - 1) / m;
  return sum <= k;
}


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

int n, k;
int a[200010];
char check(const int &n);

int main() {
  scanf("%d%d", &n, &k);
  int maxv = 0;
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    sum += a[i], maxv = max(maxv, a[i]);
  }

  int l = (sum + n + k - 1) / (n + k), r = maxv;
  while (l <= r) {
    int m = l + r >> 1;
    if (check(m))
      r = m - 1;
    else
      l = m + 1;
  }
  printf("%d\n", l);
  return 0;
}

char check(const int &m) {
  int sum = 0;
  for (int i = 0; i < n; ++i)
    sum += (a[i] - 1) / m;
  return sum <= k;
}