alexjercan/bugnet huggingface dataset:
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int charToInt(char c) {
  char zero_num = '0';
  return (int)c - (int)zero_num;
}

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

  vec_int A(N);
  rep(i, N) cin >> A.at(i);
  // これは二分探索だ
  int large_num = pow(10, 9) * 2;
  int small_num = 1;

  while (large_num > small_num + 1) {
    int medium_num = (small_num + large_num) / 2;

    int count = 0;
    for (int i = 0; i < N; i++) {
      // A.at(i)をmedium_numよりも短くするのに必要な回数
      count += A.at(i) / medium_num;
      if (A.at(i) % medium_num == 0) {
        count = count - 1;
      }
    }
    if (count <= K) {
      // 丸太をもっと短くできる
      large_num = medium_num;
    } else {
      // 丸太はもっと長い
      small_num = medium_num;
    }
  }

  int count = 0;
  for (int i = 0; i < N; i++) {
    count += A.at(i) / small_num;
    if (A.at(i) % small_num == 0) {
      count = count - 1;
    }
  }
  if (count <= K) {
    cout << small_num << endl;
  } else {

    cout << large_num << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int charToInt(char c) {
  char zero_num = '0';
  return (int)c - (int)zero_num;
}

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

  vec_int A(N);
  rep(i, N) cin >> A.at(i);

  priority_queue<tuple<double, double, int>> pq;
  rep(i, N) { pq.push(make_tuple((double)A.at(i), (double)A.at(i), 1)); }

  rep(i, K) {
    tuple<double, double, int> temp = pq.top();
    pq.pop();
    double divided, original;
    int kaisuu;
    tie(divided, original, kaisuu) = temp;
    divided = original / ((double)kaisuu + 1.);
    pq.push(make_tuple(divided, original, kaisuu + 1));
  }
  tuple<double, double, int> temp = pq.top();
  pq.pop();
  double divided, original;
  int kaisuu;
  tie(divided, original, kaisuu) = temp;
  cout << (int)ceil(divided) << endl;

  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
#define sz 200009
ll n, m, i, j, k, l, t, arr[sz], brr[sz], crr[sz], ans, hi, lo, mi;
char c[sz];
int main() {
  scanf("%lld %lld", &n, &k);
  for (i = 1; i <= n; i++)
    scanf("%lld", &arr[i]), hi = max(hi, arr[i]);
  ans = hi;
  lo = 1;
  while (hi >= lo) {
    mi = (hi + lo) / 2;
    j = 0;
    for (i = 1; i <= n; i++) {
      j += arr[i] / mi;
      if (arr[i] % mi)
        j++;
      j--;
    }
    if (j <= k) {
      ans = min(ans, mi);
      hi = mi - 1;
    } else
      lo = mi + 1;
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
#define sz 200009
ll n, m, i, j, k, l, t, arr[sz], brr[sz], crr[sz], ans, hi, lo, mi;
char c[sz];
int main() {
  scanf("%lld %lld", &n, &k);
  for (i = 1; i <= n; i++)
    scanf("%lld", &arr[i]), hi = max(hi, arr[i]);
  ans = hi;
  while (hi >= lo) {
    mi = (hi + lo) / 2;
    j = 0;
    for (i = 1; i <= n; i++) {
      j += arr[i] / mi;
      if (arr[i] % mi)
        j++;
      j--;
    }
    if (j <= k) {
      ans = min(ans, mi);
      hi = mi - 1;
    } else
      lo = mi + 1;
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded


#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll N, K;
  cin >> N >> K;

  vll v(N);
  REP(i, N) cin >> v[i];

  ll ng = 0, ok = INF;
  while (ok - ng > 1) {
    ll mid = (ng + ok) / 2;
    ll cnt = 0;
    REP(i, N) {
      ll c = v[i];
      if (mid >= c)
        continue;
      cnt += c / mid;
    }

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

  print(ok);
}


Error: TLE

Fixed submission:


#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll N, K;
  cin >> N >> K;

  vll v(N);
  REP(i, N) cin >> v[i];

  ll ng = 0, ok = INF;
  while (ok - ng > 1) {
    ll mid = (ng + ok) / 2;
    ll cnt = 0;
    REP(i, N) {
      ll c = v[i];
      while (c > mid) {
        c -= mid;
        cnt++;
      }
    }

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

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

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
// typedef pair<int,int> pii;
#define int long long

int a[200010];

int n, k;
int cal(int x) {
  if (x == 0) {
    return -1;
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    if ((a[i] - 1) / x > k) {
      return 1e9;
    }
    ans = ans + ((a[i] - 1) / x);
    if (ans > k) {
      return 1e9;
    }
  }
  if (ans < 0) {
    return 1e9;
  }
  return ans;
}

signed main() {
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", &a[i]);
  }
  int l = 1, r = 1e9 + 10, mid;
  while (l <= r) {
    mid = (l + r) / 2;
    int f = cal(mid);
    if (f > k)
      l = mid + 1;
    else
      r = mid - 1;
  }
  printf("%lld\\n", l);
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
// typedef pair<int,int> pii;
#define int long long

int a[500010];

int n, k;
int cal(int x) {
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (ans - k > -(a[i] / x)) {
      return 1e9;
    }
    if (a[i] / x > k) {
      return 1e9;
    }
    ans = ans + (a[i] / x);
    if (ans > k) {
      return 1e9;
    }
  }
  return ans;
}

signed main() {
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", &a[i]);
  }
  int l = 0, r = 1e9 + 10, mid;
  while (l <= r) {
    mid = (l + r) / 2;
    int f = cal(mid);
    if (f > k)
      l = mid + 1;
    else
      r = mid - 1;
  }
  printf("%lld\\n", l);
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

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

int main() {

  long long int N, K, tmp, high, mid, low, res = 0;
  priority_queue<long long int> A;

  cin >> N >> K;

  rep(i, N) {
    cin >> tmp;
    A.push(tmp);
  }

  high = A.top() + 1;
  low = 0;

  while (high > low) {
    mid = (high + low) / 2;
    if (mid == 0) {
      break;
    }
    priority_queue<long long int> tmp_que = A;
    long long int count = 0;
    rep(i, N) {
      long long int tmp = tmp_que.top();
      if (tmp <= mid) {
        break;
      }
      if (tmp % mid == 0) {
        count += (tmp / mid - 1);
      } else {
        count += tmp / mid;
      }
      tmp_que.pop();
    }
    if (count <= K) {
      res = mid;
      high = mid;
    } else {
      low = mid + 1;
    }
  }

  cout << res;

  return 0;
}

Error: TLE

Fixed submission:

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

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

int main() {

  long long int N, K, tmp, high, mid, low, res = 0;
  priority_queue<long long int> A;

  cin >> N >> K;

  rep(i, N) {
    cin >> tmp;
    A.push(tmp);
  }

  high = A.top() + 1;
  low = 0;

  while (high > low) {
    mid = (high + low) / 2 + 1;
    priority_queue<long long int> tmp_que = A;
    long long int count = 0;
    rep(i, N) {
      long long int tmp = tmp_que.top();
      if (tmp <= mid) {
        break;
      }
      if (tmp % mid == 0) {
        count += (tmp / mid - 1);
      } else {
        count += tmp / mid;
      }
      tmp_que.pop();
    }
    if (count <= K) {
      res = mid;
      high = mid;
    } else {
      low = mid + 1;
    }
  }

  cout << res;

  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define in insert
#define er erase
#define fi first
#define se second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define le(x) (int)x.length()
#define rsz resize

using namespace std;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef pair<int, int> pii;
typedef queue<int> qi;
typedef double db;
typedef long long ll;
typedef long double ld;

const long double PI = 3.141592653589793238462643383279502884;
const long long PRIME1 = 1e9 + 787;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll minll(ll a, ll b) { return a <= b ? a : b; }
ll maxll(ll a, ll b) { return a >= b ? a : b; }

int main() {
  fast;
  ll n, k;
  cin >> n >> k;
  vi a(n);
  int mx = -1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mx = max(a[i], mx);
  }
  int l = 1, r = mx;
  while (l < r) {
    int d = (l + r) / 2;
    ll count = 0;
    for (int i = 0; i < n; i++) {
      count += (a[i] - 1) / d;
    }
    if (count > k) {
      l = d + 1;
    } else {
      r = d;
    }
  }
  cout << l;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define in insert
#define er erase
#define fi first
#define se second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define le(x) (int)x.length()
#define rsz resize

using namespace std;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef pair<int, int> pii;
typedef queue<int> qi;
typedef double db;
typedef long long ll;
typedef long double ld;

const long double PI = 3.141592653589793238462643383279502884;
const long long PRIME1 = 1e9 + 787;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll minll(ll a, ll b) { return a <= b ? a : b; }
ll maxll(ll a, ll b) { return a >= b ? a : b; }

int main() {
  fast;
  int n, k;
  vi a(n);
  int mx = -1;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mx = max(a[i], mx);
  }
  int l = 1, r = mx;
  while (l < r) {
    int d = (l + r) / 2;
    int count = 0;
    for (int i = 0; i < n; i++) {
      count += (a[i] - 1) / d;
    }
    if (count > k) {
      l = d + 1;
    } else {
      r = d;
    }
  }
  cout << l;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

// BISMILLAHIR RAHMANIR RAHIM
#include <bits/stdc++.h>
#define mem(a, b) (memset(a, b, sizeof(a)))
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define PI acos(-1)
#define INF 2147483647
#define MOD 1000000007
#define MAX 200005
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<int, ii> pii;
typedef vector<ii> vii;
typedef priority_queue<int, vector<int>, greater<int>> PQ;

int Set(int mask, int pos) { return mask = mask | (1 << pos); }
bool check(int mask, int pos) { return (bool)(mask & (1 << pos)); }

int arr[MAX];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, k;
  cin >> n >> k;

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

  ll low = 1, high = 1e16, mid, ans;

  while (low <= high) {
    mid = (low + high + 1) >> 1;
    ll cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += (ll)(arr[i] / mid) - 1;
      if (arr[i] % mid != 0)
        cnt++;
    }
    if (cnt <= k) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

// BISMILLAHIR RAHMANIR RAHIM
#include <bits/stdc++.h>
#define mem(a, b) (memset(a, b, sizeof(a)))
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define PI acos(-1)
#define INF 2147483647
#define MOD 1000000007
#define MAX 200005
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<int, ii> pii;
typedef vector<ii> vii;
typedef priority_queue<int, vector<int>, greater<int>> PQ;

int Set(int mask, int pos) { return mask = mask | (1 << pos); }
bool check(int mask, int pos) { return (bool)(mask & (1 << pos)); }

int arr[MAX];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, k;
  cin >> n >> k;

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

  ll low = 0, high = 1e16, mid, ans;

  while (low <= high) {
    mid = (low + high + 1) >> 1;
    ll cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += (ll)(arr[i] / mid) - 1;
      if (arr[i] % mid != 0)
        cnt++;
    }
    if (cnt <= k) {
      ans = mid;
      high = mid - 1;
    } else {
      low = 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;
ll n, m, r, a[222222], l = 1e9, ans;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cin >> n >> m;
  l = 1;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    r = max(a[i], r);
  }
  ans = r;
  while (l <= r) {
    ll mid = l + r >> 1, cnt = 0;
    for (int i = 1; i <= n; i++)
      cnt += (a[i] - 1) / mid;
    if (cnt > m)
      l = mid + 1;
    else {
      ans = mid;
      r = mid - 1;
    }
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
ll n, m, r, a[222222], l, ans;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    r = max(a[i], r);
  }
  ans = r;
  while (l <= r) {
    ll mid = l + r >> 1, cnt = 0;
    for (int i = 1; i <= n; i++)
      cnt += a[i] / mid;
    if (cnt > m)
      l = mid + 1;
    else {
      ans = mid;
      r = mid - 1;
    }
  }
  cout << ans;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

// catgo
#include <iostream>
#define ll long long int
using namespace std;
ll n, k, a[5000000];
bool check(int m) {
  ll sl = 0;
  for (ll i = 1; i <= n; ++i) {
    sl = sl + a[i] / m;
    if (a[i] % m == 0)
      sl--;
  }
  return (sl <= k);
}
int main() {
  // freopen("b.inp","r",stdin);
  cin >> n >> k;
  for (ll i = 1; i <= n; ++i)
    cin >> a[i];
  ll d = 1, c = a[1], g, m;
  for (ll i = 2; i <= n; ++i)
    c = max(c, a[i]);
  while (d <= c) {
    g = (d + c) / 2;
    if (check(g)) {
      m = g;
      c = g - 1;
    } else
      d = g + 1;
    // cout<<m<<" "<<g<<endl;;
  }
  cout << m;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long a[100005], n, k;
bool check(int m) {
  int sl = 0;
  for (int i = 1; i <= n; i++) {
    sl = sl + (a[i] / m);
    if (a[i] % m == 0)
      sl--;
  }
  return (sl <= k);
}
int main() {
  long dau = 1, cuoi, giua, M;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    cuoi = max(cuoi, a[i]);
  }
  while (dau <= cuoi) {
    giua = (dau + cuoi) / 2;
    if (check(giua) == true) {
      M = giua;
      cuoi = giua - 1;
    } else
      dau = giua + 1;
  }
  cout << M;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

ll BinarySearch(vector<ll> &A, ll K) {
  ll N = A.size();
  ll low = 1, high = (*max_element(A.begin(), A.end()));
  while (high - low > 1) {
    ll mid = (low + high) / 2;
    ll cuts = 0;
    loop(i, 0, N) {
      cuts += (A[i] / mid);
      if (A[i] % mid == 0) {
        cuts--;
      }
    }
    if (cuts <= K) {
      high = mid;
    } else
      low = mid + 1;
  }
  ll cuts = 0;
  loop(i, 0, N) {
    cuts += (A[i] / low);
    if (A[i] % low == 0) {
      cuts--;
    }
  }
  if (cuts <= K) {
    return low;
  }
  return high;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  loop(i, 0, N) { cin >> A[i]; }
  cout << BinarySearch(A, K) << "\\n";
  return 0;
}

Error: 0

Fixed submission:

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

int BinarySearch(vector<ll> &A, ll K) {
  ll N = A.size();
  ll low = 0, high = *max_element(A.begin(), A.end());
  while (high - low > 1) {
    ll mid = (low + high) / 2;
    ll cuts = 0;
    loop(i, 0, N) {
      cuts += (A[i] / mid);
      if (A[i] % mid == 0) {
        cuts--;
      }
    }
    if (cuts <= K) {
      high = mid;
    } else
      low = mid + 1;
  }
  ll cuts = 0;
  loop(i, 0, N) {
    cuts += (A[i] / low);
    if (A[i] % low == 0) {
      cuts--;
    }
  }
  if (cuts <= K) {
    return low;
  }
  return high;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  loop(i, 0, N) { cin >> A[i]; }
  cout << BinarySearch(A, K) << "\\n";
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) (x).size()

ll mod = 1000000007;

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

ll inv(ll x) {
  ll res = 1;
  ll k = mod - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % mod;
    y = (y * y) % mod;
    k /= 2;
  }
  return res;
}

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

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

  sort(all(a));
  int l = 0, r = a[n - 1];

  while (l < r) {
    int m = (l + r) / 2;
    if (m == 0)
      m = 1;
    int cnt = 0;
    rep(i, n) {
      int c = a[i] / m;
      if (a[i] % m > 0)
        c++;
      cnt += (c - 1);
    }
    if (cnt > k) {
      if (r - l == 1)
        l = r;
      else
        l = m;
    } else {
      if (m == 1 && l == 0)
        r = l = 1;
      else
        r = m;
    }
  }

  cout << l << endl;
}

Error: 0

Fixed submission:

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) (x).size()

ll mod = 1000000007;

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

ll inv(ll x) {
  ll res = 1;
  ll k = mod - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % mod;
    y = (y * y) % mod;
    k /= 2;
  }
  return res;
}

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

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

  sort(all(a));
  int l = 0, r = a[n - 1];

  while (l < r) {
    int m = (l + r) / 2;
    int cnt = 0;
    rep(i, n) {
      int c = a[i] / m;
      if (a[i] % m > 0)
        c++;
      cnt += (c - 1);
    }
    if (cnt > k) {
      if (r - l == 1)
        l = r;
      else
        l = m;
    } else {
      r = m;
    }
  }

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

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

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


Error: -11

Fixed submission:

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

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