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

#include <bits/stdc++.h>
using namespace std;
int n, k, i, mini = 1, maxi = 1e9;
vector<int> A;

bool check(int tar) {
  int j, cur = 0;
  for (j = 0; j < n; j++) {
    cur += (A[j] - 1) / tar;
  }
  if (cur > k)
    return false;
  else
    return true;
}

int main() {
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  while (mini < maxi) {
    int mid = mini + maxi >> 1;
    if (mini == mid) {
      if (!check(mini))
        mini = maxi;
      else
        maxi = mini;
    } else {
      if (check(mid))
        maxi = mid;
      else
        mini = mid + 1;
    }
  }
  cout << mini;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, k, i, mini, maxi = 1e9;
vector<int> A;

bool check(int tar) {
  int j, cur = 0;
  for (j = 0; j < n; j++) {
    cur += (A[j] - 1) / tar;
  }
  if (cur > k)
    return false;
  else
    return true;
}

int main() {
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  while (mini < maxi) {
    int mid = mini + maxi >> 1;
    if (mini == mid) {
      if (!check(mini))
        mini = maxi;
      else
        maxi = mini;
    } else {
      if (check(mid))
        maxi = mid;
      else
        mini = mid + 1;
    }
  }
  cout << mini;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define mk make_pair

template <class key, class cmp = std::less<key>>
using ordered_set =
    tree<key, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 1e9 + 7;

/*
    if you have any question about any part of my code,
    please don't shy to ask anytime :)
*/

ll n, k;
ll x[200005];

bool get(ll mid) {
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += (x[i] - 1) / mid;
  }

  return cnt <= k;
}

void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }

  ll st = 1, en = 1e9;
  ll ans = 0;
  while (st <= en) {
    ll mid = (st + en) >> 1;
    if (get(mid)) {
      ans = mid;
      en = mid - 1;
    } else {
      st = mid + 1;
    }
  }

  cout << ans << endl;
}

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

  // ll t;
  // cin >> t;
  // while (t--)
  // {
  //     solve();
  // }
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define mk make_pair

template <class key, class cmp = std::less<key>>
using ordered_set =
    tree<key, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 1e9 + 7;

/*
    if you have any question about any part of my code,
    please don't shy to ask anytime :)
*/

ll n, k;
ll x[200005];

bool get(ll mid) {
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += (x[i] - 1) / mid;
  }

  return cnt <= k;
}

void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }

  ll st = 0, en = 1e9;
  ll ans = 0;
  while (st <= en) {
    ll mid = (st + en) >> 1;
    if (get(mid)) {
      ans = mid;
      en = mid - 1;
    } else {
      st = mid + 1;
    }
  }

  cout << ans << endl;
}

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

  // ll t;
  // cin >> t;
  // while (t--)
  // {
  //     solve();
  // }
  solve();
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) cin >> A[i];
  sort(all(A), greater<int>());
  int l = 0, r = A[0]; // 左は絶対に無理、右は絶対に可能
  while (r - 1 > l) {
    int middle = (l + r) / 2;
    int tmp = 0;
    rep(i, N) { tmp += (A[i] + middle - 1) / middle - 1; }
    if (tmp <= K)
      r = middle;
    else
      l = middle;
  }
  cout << r << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) cin >> A[i];
  sort(all(A), greater<int>());
  int maxlen = A[0];
  int ans = maxlen / 2;
  while (1) {
    // cout << ans << endl;
    int tmp = K;
    rep(i, N) {
      tmp -= (A[i] + ans - 1) / ans - 1;
      // cout << i << " " << (A[i] + ans - 1) / ans - 1 << endl;
    }
    // cout << tmp << endl;
    if (tmp < 0) {
      ans = (maxlen + ans + 1) / 2;
    } else if (tmp > 0) {
      ans = (ans + 1) / 2;
    } else
      break;
  }
  cout << ans << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define ll unsigned long long
#define gmax(x, y) x = max(x, y)
#define gmin(x, y) x = min(x, y)
#define F first
#define S second
#define P pair
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define V vector
#define RE return
#define ALL(a) a.begin(), a.end()
#define MP make_pair
#define PB emplace_back
#define PF emplace_front
#define FILL(a, b) memset(a, b, sizeof(a))
#define lwb lower_bound
#define upb upper_bound
using namespace std;
int n;
ll a[200005], k;
bool check(ll x) {
  ll cnt = 0;
  FOR(i, 1, n) {
    cnt += (a[i] % x != 0) + (a[i] / x);
    cnt--;
    if (cnt > k)
      RE 0;
  }
  RE 1;
}
signed main() {
  cin >> n >> k;
  FOR(i, 1, n) { cin >> a[i]; }
  ll l = 1, r = 1e9, mid, ans = -1;
  while (r >= l) {
    mid = (l + r) >> 1;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans;
  RE 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define ll unsigned long long
#define gmax(x, y) x = max(x, y)
#define gmin(x, y) x = min(x, y)
#define F first
#define S second
#define P pair
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define V vector
#define RE return
#define ALL(a) a.begin(), a.end()
#define MP make_pair
#define PB emplace_back
#define PF emplace_front
#define FILL(a, b) memset(a, b, sizeof(a))
#define lwb lower_bound
#define upb upper_bound
using namespace std;
int n;
ll a[200005], k;
bool check(ll x) {
  ll cnt = 0;
  FOR(i, 1, n) {
    cnt += (a[i] % x != 0) + (a[i] / x);
    cnt--;
    if (cnt > k)
      RE 0;
  }
  RE 1;
}
signed main() {
  cin >> n >> k;
  ll m = 1e10;
  FOR(i, 1, n) {
    cin >> a[i];
    a[i] *= m;
  }
  ll l = 1, r = 1e19, mid, ans = -1;
  while (r >= l) {
    mid = (l + r) >> 1;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  ll t = ans / m;
  cout << t + (ans % m != 0);
  RE 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define maxn 200005

ll fastpower(ll b, ll p) {
  ll k = p % 2 ? b : 1;
  return p == 0 ? 1 : k * (fastpower(b * b, p / 2));
}

ll n, k;
ll a[maxn];

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

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--) {
    cin >> n >> k;
    ll l = 0, r = 1;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      r = max(r, a[i]);
    }
    ll ans = 1e18;
    while (r - l > 1) {
      ll mid = (l + r) / 2;
      if (check(mid)) {
        r = mid;
        ans = min(ans, mid);
      } else
        l = mid;
    }
    cout << r;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define maxn 200005

ll fastpower(ll b, ll p) {
  ll k = p % 2 ? b : 1;
  return p == 0 ? 1 : k * (fastpower(b * b, p / 2));
}

ll n, k;
ll a[maxn];

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

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--) {
    cin >> n >> k;
    ll l = 0, r = 1;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      r = max(r, a[i]);
    }
    ll ans = 1e18;
    while (l < r) {
      ll mid = (l + r) / 2;
      if (check(mid)) {
        r = mid - 1;
        ans = min(ans, mid);
      } else
        l = mid + 1;
    }
    cout << ans;
  }
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int maxn = 200005;
long long a[maxn], N, K;
bool ok(long long w) {
  long long sum = 0;
  for (int i = 1; i <= N; i++)
    sum += (a[i] - 1) / w;
  if (sum <= K)
    return 1;
  else {
    return 0;
  }
}
int main() {
  cin >> N >> K;
  long long l = 1, r = 0;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
    r = max(r, a[i]);
  }
  if (K == 0)
    cout << r << endl;
  else {
    while (l <= r) {
      long long mid = (l + r) / 2;
      if (ok(mid))
        r = mid - 1;
      else
        l = mid + 1;
    }
    cout << l << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int maxn = 200005;
long long a[maxn], N, K;
bool ok(long long w) {
  long long sum = 0;
  for (int i = 0; i < N; i++)
    sum += (a[i] - 1) / w;
  if (sum <= K)
    return 1;
  else {
    return 0;
  }
}
int main() {
  cin >> N >> K;
  long long l = 0, r = 0;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
    r = max(r, a[i]);
  }
  if (K == 0)
    cout << r << endl;
  else {
    while (l <= r) {
      long long mid = (l + r) / 2;
      if (ok(mid))
        r = mid - 1;
      else
        l = mid + 1;
    }
    cout << l << endl;
  }
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

typedef long long ll;

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;
}

ll ulim = 1e17;
ll n, k;
vector<ll> a;
bool cond(ll x) {
  ll tmp = 0ll;
  rep(i, n) { tmp += (a[i] + x - 1) / x - 1; }

  return tmp <= k;
}

// condを満たす最小のxを求める(lower_bound)
// 全てのxが条件を満たさない場合はnを返す(nはとりえない値)
ll lower_bound_cond() {
  ll lb = 0, ub = ulim;

  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;

    if (cond(mid))
      ub = mid; // Answer is in (lb, mid]
    else
      lb = mid; // Answer is in (mid, ub]
  }

  // now lb + 1 = ub
  return ub;
}

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

  cin >> n >> k;

  a.resize(n);
  rep(i, n) cin >> a[i];

  // ll mx = *max_element(a.begin(), a.end());
  cout << lower_bound_cond() << endl;
}


Error: 0

Fixed submission:

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

using namespace std;

typedef long long ll;

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;
}

ll ulim = 1e17;
ll n, k;
vector<ll> a;
bool cond(ll x) {
  ll tmp = 0;
  rep(i, n) { tmp += (a[i] + x - 1) / x - 1; }

  return tmp <= k;
}

// condを満たす最小のxを求める(lower_bound)
// 全てのxが条件を満たさない場合はnを返す(nはとりえない値)
ll lower_bound_cond() {
  ll lb = -1, ub = ulim;

  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;

    if (cond(mid))
      ub = mid; // Answer is in (lb, mid]
    else
      lb = mid; // Answer is in (mid, ub]
  }

  // now lb + 1 = ub
  return ub;
}

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

  cin >> n >> k;

  a.resize(n);
  rep(i, n) cin >> a[i];

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

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (auto &e : A)
    cin >> e;
  auto OK = [&](int x) -> bool {
    int cnt = 0;
    for (auto &a : A) {
      if (a == x)
        continue;
      cnt += (a / x);
    }
    return cnt <= K;
  };
  int l = 0, r = 1e9;
  while (r - l > 1) {
    int c = (l + r) / 2;
    if (OK(c))
      r = c;
    else
      l = c;
  }
  cout << r << '\\n';
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (auto &e : A)
    cin >> e;
  auto OK = [&](int x) -> bool {
    int cnt = 0;
    for (auto &a : A) {
      if (a == x)
        continue;
      cnt += (a / x);
    }
    return cnt <= K;
  };
  int l = -1, r = 1e9;
  while (r - l > 1) {
    int c = (l + r) / 2;
    if (OK(c))
      r = c;
    else
      l = c;
  }
  cout << r << '\\n';
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long

using namespace std;

const ll INF = 10010010010010;

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

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

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

    ll sum = 0;
    for (int ii = 0; ii < N; ++ii) {
      sum += (A[ii] + mid - 1) / mid - 1;
    }

    bool ok = (sum <= K) ? true : false;

    if (ok) {
      r = mid;
    } else {
      l = mid;
    }
  }

  cout << r << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long

using namespace std;

const ll INF = 10010010010010;

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

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

  ll l = -1;
  ll r = INF;
  while (l + 1 < r) {
    ll mid = (l + r) / 2;

    ll sum = 0;
    for (int ii = 0; ii < N; ++ii) {
      sum += (A[ii] + mid - 1) / mid - 1;
    }

    bool ok = (sum <= K) ? true : false;

    if (ok) {
      r = mid;
    } else {
      l = mid;
    }
  }

  cout << r << "\\n";

  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <cstdint>
#include <vector>
#define FOR(i, l, r) for (int i = (l); i < (r); ++i)
#define RFOR(i, l, r) for (int i = (l); i >= (int)(r); i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n - 1, 0)

#define int long long
using namespace std;
const int MX = 1e6;
const int inf = 1e13;
const int mod = 1e9 + 7;
#define ll long long
map<char, int> m;
signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end(), greater<int>());
  if (k == 0) {
    cout << a[0] << endl;
    return 0;
  }
  int l = 1;
  int r = a[0];
  int m = (l + r) / 2;
  sort(a.begin(), a.end());
  while (l <= r) {
    bool f = true;
    int c = k;
    // cout << m << endl;
    rep(i, n) {
      c -= (int)ceil((double)a[i] / (double)m) - 1;
      if (c < 0) {
        f = false;
        break;
      }
    }
    // cout << c << endl;
    if (f) {
      r = m - 1;
      m = (l + r) / 2;
    } else {
      l = m + 1;
      m = (l + r) / 2;
    }
    // cout << l<<" "<<r<<" "<<m <<" "<<f<< endl;
  }
  cout << l << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cstdint>
#include <vector>
#define FOR(i, l, r) for (int i = (l); i < (r); ++i)
#define RFOR(i, l, r) for (int i = (l); i >= (int)(r); i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n - 1, 0)

#define int long long
using namespace std;
const int MX = 1e6;
const int inf = 1e13;
const int mod = 1e9 + 7;
#define ll long long
map<char, int> m;
signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end(), greater<int>());
  if (k == 0) {
    cout << a[0] << endl;
    return 0;
  }
  int l = 1;
  int r = a[0];
  int m = (l + r) / 2;
  sort(a.begin(), a.end());
  while (l <= r) {
    bool f = true;
    int c = k;
    // cout << m << endl;
    rep(i, n) {
      int j = 1;
      while ((int)(ceil((double)a[i] / (double)(j))) > m) {
        // cout << a[i]<< " "<<(int)(ceil((double)a[i]/(double)(j)))<< " "<<m <<
        // endl;
        if (c <= 0) {
          f = false;
          break;
        }
        j++;
        c--;
      }
      if (!f)
        break;
    }
    // cout << c << endl;
    if (f) {
      r = m - 1;
      m = (l + r) / 2;
    } else {
      l = m + 1;
      m = (l + r) / 2;
    }
    // cout << l<<" "<<r<<" "<<m <<" "<<f<< endl;
  }
  cout << l << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 6010100;
long long n, k;
long long S[N];
bool pd(int K) {
  if (K <= 0)
    return false;
  long long A = 0;
  for (int i = 1; i <= n; i++) {
    A += S[i] % K ? S[i] / K : S[i] / K - 1;
  }
  return A <= k;
}
int main() {
  long long ans = 0;
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &S[i]);
    ans = max(S[i], ans);
  }
  int r = ans, l = 1;
  while (l <= r) {
    long long mid = l + r >> 1;
    if (pd(mid)) {
      ans = min(ans, mid);
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 2010100;
long long n, k;
long long S[N];
bool pd(int K) {
  long long A = 0;
  for (int i = 1; i <= n; i++) {
    A += S[i] % K ? S[i] / K : S[i] / K - 1;
  }
  return A <= k;
}
int main() {
  long long ans = 0;
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &S[i]);
    ans = max(S[i], ans);
  }
  int r = ans, l = 0;
  while (l <= r) {
    long long mid = l + r >> 1;
    if (pd(mid)) {
      ans = min(ans, mid);
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
const ll mod = 1000000007;
const ll INF = 1e+14;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define _GLIBCXX_DEBUG

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

  ll ng = 0;
  ll ok = 10000000001;
  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    ll count = 0;
    rep(i, n) {
      if (count > k)
        break;
      count += (a[i] + mid - 1) / mid - 1;
    }
    if (count <= k)
      ok = mid;
    else
      ng = mid;
  }

  cout << ok << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
const ll mod = 1000000007;
const ll INF = 1e+14;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define _GLIBCXX_DEBUG

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

  ll ng = -1;
  ll ok = 10000000001;
  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    ll count = 0;
    rep(i, n) {
      if (count > k)
        break;
      count += (a[i] + mid - 1) / mid - 1;
    }
    if (count <= k)
      ok = mid;
    else
      ng = mid;
  }

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

// Code by Mukul Totla
#include <bits/stdc++.h>
using namespace std;
#define ll long long

bool check(ll mid, ll k, ll *arr, ll n) {
  for (ll i = 0; i < n; i++) {
    if (arr[i] > mid) {
      ll val = ((arr[i] + mid - 1) / mid) - 1;
      if (k - val >= 0)
        k -= val;
      else
        return false;
    }
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, k, i, lo = 0, hi = 0;
  cin >> n >> k;
  ll arr[n];
  for (i = 0; i < n; i++) {
    cin >> arr[i];
    hi = max(arr[i], hi);
  }

  ll ans = hi;
  hi = 1e9 + 100;
  while (lo <= hi) {
    ll mid = (lo + hi) / 2;
    bool chk;
    if (mid > 0)
      chk = check(mid, k, arr, n);
    else
      chk = false;
    if (chk == true) {
      hi = mid - 1;
      ans = min(ans, mid);
    } else
      lo = mid + 1;
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

// Code by Mukul Totla
#include <bits/stdc++.h>
using namespace std;
#define ll long long

bool check(ll mid, ll k, ll *arr, ll n) {
  for (ll i = 0; i < n; i++) {
    if (arr[i] > mid) {
      ll val = ((arr[i] + mid - 1) / mid) - 1;
      if (k - val >= 0)
        k -= val;
      else
        return false;
    }
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, k, i, lo = 0, hi = 0;
  cin >> n >> k;
  ll arr[n];
  for (i = 0; i < n; i++) {
    cin >> arr[i];
    hi = max(arr[i], hi);
  }

  ll ans = hi;
  hi = 1e9 + 100;
  while (lo <= hi) {
    ll mid = (lo + hi) / 2;

    bool chk = check(mid, k, arr, n);

    if (chk == true) {
      hi = mid - 1;
      ans = min(ans, mid);
    } else
      lo = mid + 1;
  }
  cout << ans;
  return 0;
}