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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  ll N, K;
  cin >> N >> K;
  ll A[N];
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  ll l = 0, r = mod, m;
  int tmp;
  while (l < r) {
    m = (l + r) / 2;
    if (m == 0) {
      l = 1;
      break;
    }
    tmp = 0;
    for (int i = 0; i < N; ++i) {
      if (A[i] <= m)
        continue;
      if (A[i] % m == 0) {
        tmp += A[i] / m - 1;
      } else {
        tmp += A[i] / m;
      }
    }
    if (tmp <= K)
      r = m;
    else
      l = m + 1;
  }
  cout << l << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  ll N, K;
  cin >> N >> K;
  ll A[N];
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  ll l = 0, r = mod, m;
  int tmp;
  while (l < r) {
    m = (l + r) / 2;
    tmp = 0;
    for (int i = 0; i < N; ++i) {
      if (A[i] <= m)
        continue;
      tmp += A[i] / m;
    }
    if (tmp <= K)
      r = m;
    else
      l = m + 1;
  }
  cout << l << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, k, h, i, x, y;
  vector<int> v;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> h;
    v.push_back(h);
  }
  sort(v.rbegin(), v.rend());
  x = 1;
  y = v[0];
  while (y - x > 0) {
    h = 0;
    int m = (y + x) / 2;
    for (i = 0; i < n; i++)
      h += (v[i] + m - 1) / m - 1;
    if (h > k)
      x = m + 1;
    else
      y = m;
  }
  cout << x << '\\n';
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int a[200100];
int n, k;

int check(int x) {
  int cut = 0;
  for (int i = 1; i <= n; ++i) {
    cut += a[i] / x - 1;
    if (a[i] % x)
      ++cut;
    if (cut > k)
      return 0;
  }
  return 1;
}

int main() {
  scanf("%d%d", &n, &k);
  int l = 0, r = 0;
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    r = max(r, a[i]);
    ans = r;
  }

  while (l <= r) {
    int mid = l + r >> 1;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }

  printf("%d\\n", ans);
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

#define LL long long

LL n, k, a[200100], ki, ka, mid, ans = 0;

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  ki = 1;
  ka = 1e9;
  while (ki <= ka) {
    mid = (ki + ka) / 2;
    LL x = 0;
    for (int i = 1; i <= n; i++) {
      x += (a[i] - 1) / mid;
    }
    if (x <= k) {
      ka = mid - 1;
      ans = mid;
    } else {
      ki = mid + 1;
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  ll n, k, a[100050];
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll l = 1, r = 1e9, mid, ans = 0;
  while (l <= r) {
    mid = (l + r) / 2;
    ll jum = 0;
    for (int i = 1; i <= n; i++) {
      if (mid != 0)
        jum += (a[i] - 1) / mid;
    }
    if (jum <= k) {
      r = mid - 1;
      ans = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << ans << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 200003;
int n, k;
int a[N];
inline bool check(int x) {
  long long tot = 0;
  for (int i = 1; i <= n; i += 1)
    tot += a[i] / x + (a[i] % x != 0) - 1;
  return tot <= k;
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i += 1)
    cin >> a[i];
  int l = 1, r = 1000000001, ans = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 200003;
int n, k;
int a[N];
inline bool check(int x) {
  long long tot = 0;
  for (int i = 1; i <= n; i += 1)
    tot += a[i] / x + (a[i] % x != 0) - 1;
  return tot <= k;
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i += 1)
    cin >> a[i];
  int l = 0, r = 1000000001, ans = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define bug(x) cerr << #x << " = " << x << '\\n'
#define ll long long
#define maxn 200005

int n, k;
int a[maxn];

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

int main() {
  // freopen(".inp","r",stdin);

  cin >> n >> k;

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

  int lo = 0, hi = (int)1e9 + 7;

  while (hi - lo > 1) {
    int mid = (hi + lo) / 2;
    if (f(mid))
      hi = mid;
    else
      lo = mid;
  }
  cout << hi;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define bug(x) cerr << #x << " = " << x << '\\n'
#define ll long long
#define maxn 200005

int n, k;
int a[maxn];

map<int, int> s;

bool f(int x) {
  s.clear();
  for (int i = 1; i <= n; ++i)
    s[a[i]]++;
  for (int i = 1; i <= k; ++i) {
    auto u = s.end();
    --u;
    int v = u->first;
    if (v > x) {
      s[v]--;
      s[x]++;
      s[v - x]++;
    }
  }
  auto u = s.end();
  --u;
  int v = u->first;
  return v <= x;
}

int main() {
  freopen(".inp", "r", stdin);

  cin >> n >> k;

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

  int lo = 0, hi = *max_element(a + 1, a + n + 1) + 1;

  while (hi >= lo) {
    int mid = (hi + lo) / 2;
    if (f(mid))
      hi = mid;
    else
      lo = mid;
  }

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

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

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

int N, K;
int A[200010];

signed main() {
  cin >> N >> K;
  rep(i, N) cin >> A[i];

  sort(A, A + N);

  if (K == 0) {
    cout << A[N - 1] << endl;
    return 0;
  }

  int l = 1, r = 1001001001001001001;
  rep(i, 70) {
    int mid = (l + r) / 2;
    int cnt = 0;
    rep(j, N) cnt += (A[j] + mid - 1) / mid - 1;
    if (cnt <= K)
      r = mid;
    else
      l = mid;
  }

  cout << r << endl;

  return 0;
}


Error: 0

Fixed submission:

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

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

int N, K;
int A[200010];

signed main() {
  cin >> N >> K;
  rep(i, N) cin >> A[i];

  sort(A, A + N);

  if (K == 0) {
    cout << A[N - 1] << endl;
    return 0;
  }

  int l = 0, r = 1001001001001001001;
  rep(i, 70) {
    int mid = (l + r) / 2;
    int cnt = 0;
    rep(j, N) cnt += (A[j] + mid - 1) / mid - 1;
    if (cnt <= K)
      r = mid;
    else
      l = mid;
  }

  cout << r << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  ll maxi = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  ll l = 0, r = (ll)1e9 + 10;
  ll mid = (l + r) / 2;
  ll cnt = 0;
  while (r - l > 1) {
    mid = l + (r - l) / 2;
    cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += (a[i] + mid - 1) / mid - 1;
    }
    if (cnt <= k) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  ll maxi = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  ll l = -1, r = (ll)1e9 + 10;
  ll mid = (l + r) / 2;
  ll cnt = 0;
  while (r - l > 1) {
    mid = l + (r - l) / 2;
    cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += (a[i] + mid - 1) / mid - 1;
    }
    if (cnt <= k) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

ll query(ll x, vector<ll> a) {
  int n = a.size();
  ll res = 0;
  rep(i, n) { res += (a[i] - 1) / x; }
  return res;
}

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  sort(a.begin(), a.end());

  ll mini = 1, maxi = a[n - 1], mid;
  if (query(1, a) <= k) {
    cout << 1 << endl;
    return 0;
  }
  while (mini < maxi) {
    // cout << "mini: " << mini << ", maxi: " << maxi << endl;
    mid = (mini + maxi) / 2;
    // cout << "query(" << mid << ", a): " << query(mid, a) << endl;
    if (query(mid, a) > k) {
      mini = mid + 1;
    } else {
      maxi = mid;
    }
  }

  cout << maxi << endl;

  return 0;
}
/*
 */


Error: 0

Fixed submission:

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

ll query(ll x, vector<ll> a) {
  int n = a.size();
  ll res = 0;
  rep(i, n) {
    if (a[i] > x) {
      res += a[i] / x;
    }
  }
  return res;
}

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  sort(a.begin(), a.end());

  ll mini = 1, maxi = a[n - 1], mid;
  while (mini < maxi) {
    // cout << "mini: " << mini << ", maxi: " << maxi << endl;
    mid = (mini + maxi) / 2;
    // cout << "query(" << mid << ", a): " << query(mid, a) << endl;
    if (query(mid, a) > k) {
      mini = mid + 1;
    } else if (query(mid - 1, a) <= k) {
      maxi = mid - 1;
    } else {
      break;
    }
  }

  cout << mini << endl;

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

#include <bits/stdc++.h>

using namespace std;

long long v[1000010];

int main() {

  int n, k;
  cin >> n >> k;

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

  auto ver = [&](long long dk) {
    long long cur = 0;
    for (int i = 0; i < n; ++i) {
      if (v[i] > dk)
        cur += v[i] / dk;
    }
    return cur <= k;
  };

  long long a = 0, b = 1e10;

  while (b - a > 1) {
    long long mid = (a + b) / 2;
    if (ver(mid))
      b = mid;
    else
      a = mid;
  }

  cout << b << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

long long v[200010];
int main() {

  int n, k;
  cin >> n >> k;

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

  auto ver = [&](long long dk) {
    long long cur = 0;
    for (int i = 0; i < n; ++i) {
      if (v[i] > dk)
        cur += v[i] / dk;
    }
    return cur <= k;
  };

  long long a = -1, b = 1e10;

  while (b - a > 1) {
    long long mid = (a + b) / 2;
    if (ver(mid))
      b = mid;
    else
      a = mid;
  }

  cout << b << "\\n";
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
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;
}
//(a+b-1)/b
// priority_queue<ll, vector<ll>, greater<ll>> q;
using pl = pair<ll, ll>;

signed main() {
  ll n, k;
  cin >> n >> k;
  ll m = 0;
  vl a(n);
  rep(i, 0, n) cin >> a[i], chmax(m, a[i]);
  ll left = 0;
  ll right = m;
  while (right - left > 1) {
    ll mid = (right + left) / 2;
    ll cnt = 0;
    rep(i, 0, n) { cnt += (a[i] + mid - 1) / mid - 1; }
    if (cnt <= k)
      right = mid;
    else
      left = mid;
  }
  cout << right << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
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;
}
//(a+b-1)/b
using pb = pair<double, ll>;
ll k;
ll f(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll t = f(a, b / 2);
    return t * t % k;
  }
  return a * f(a, b - 1) % k;
}

signed main() {
  ll n, k;
  cin >> n >> k;
  // vector<double> a(n);
  priority_queue<pb> q;
  rep(i, 0, n) {
    double a;
    cin >> a;
    q.push(make_pair(a, 1));
  }
  ll cnt = 1;
  rep(i, 0, k) {
    pb p = q.top();
    q.pop();
    double j;
    ll m;
    tie(j, m) = p;
    j *= m;
    j /= (m + 1);
    m++;
    q.push(make_pair(j, m));
  }
  pb p = q.top();
  double j = p.first;
  ll m = j;
  if (j - m != 0)
    m++;
  cout << m << endl;

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

#include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std; // std::の省略のため
using ll = long long;

ll MAX = LLONG_MAX;

int main() {

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

  ll a[n];

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

  ll sum = 0;
  ll ng = 0, ok = 100000000000;
  ll mid, ans, tmp;

  while (ok - ng > 1) {
    sum = 0;
    mid = (ok + ng) / 2;
    // cout << ok << " " << ng << " " << mid << endl;

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

    // cout << sum << endl;

    if (sum <= k)
      ok = mid;
    else
      ng = mid;
  }

  ans = ok;

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std; // std::の省略のため
using ll = long long;

ll MAX = LLONG_MAX;

int main() {

  int n, k;
  cin >> n >> k;

  ll a[n];

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

  ll sum = 0;
  ll ng = -1, ok = 100000000000;
  ll mid, ans, tmp;

  while (ok - ng > 1) {
    sum = 0;
    mid = (ok + ng) / 2;
    // cout << ok << " " << ng << " " << mid << endl;

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

    if (sum <= k)
      ok = mid;
    else
      ng = mid;
  }

  ans = ok;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

int main() {
  ll n, k;
  cin >> n >> k;
  ll ch[200010];
  ll ma = 0;
  for (ll i = 0; i < n; i++) {
    cin >> ch[i];
    ma = max(ch[i], ma);
  }
  ll left = 0; // 「index = 0」が条件を満たすこともあるので、初期値は -1
  ll right = 1000000001; // 「index =
                         // a.size()-1」が条件を満たさないこともあるので、初期値は
                         // a.size()

  /* どんな二分探索でもここの書き方を変えずにできる！ */
  while (right - left > 1) {
    ll mid = left + (right - left) / 2;
    ll cnt = 0;
    for (ll i = 0; i < n; i++) {
      if (ch[i] % mid == 0) {
        cnt += ch[i] / mid - 1;
      } else {
        cnt += (ch[i]) / (mid);
      }
    }
    if (cnt <= k)
      right = mid;
    else
      left = mid;
  }
  /* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている
   */
  cout << right;
  // your code goes here
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

int main() {
  ll n, k;
  cin >> n >> k;
  ll ch[200010];
  ll ma = 0;
  for (ll i = 0; i < n; i++) {
    cin >> ch[i];
    ma = max(ch[i], ma);
  }
  ll left = -1; // 「index = 0」が条件を満たすこともあるので、初期値は -1
  ll right = 1000000001; // 「index =
                         // a.size()-1」が条件を満たさないこともあるので、初期値は
                         // a.size()

  /* どんな二分探索でもここの書き方を変えずにできる！ */
  while (right - left > 1) {
    ll mid = left + (right - left) / 2;
    ll cnt = 0;
    for (ll i = 0; i < n; i++) {
      cnt += (ch[i]) / (mid);
    }
    if (cnt <= k)
      right = mid;
    else
      left = mid;
  }
  /* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている
   */
  cout << min(right, ma);
  // your code goes here
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

// #define mod 1000000007
// #define mod1 9982
#define int long long int
#define pii pair<int, int>
#define vpii vector<pii>
#define vi vector<int>
#define fi first
#define se second
#define psb push_back
#define ppb pop_back
#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define all(x) x.begin(), x.end()
#define endl "\\n"

const int maxn = 2e5 + 5;
int n, k;
int ar[maxn];

bool ok(int h) {
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += ((ar[i] + h - 1) / h) - 1;
  }
  return cnt <= k;
}

void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> ar[i];
  int l = 1, h = (1e9), ans = -1;
  while (l <= h) {
    int mid = (l + h) / 2;
    if (ok(mid)) {
      ans = mid;
      h = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans << endl;
}

int32_t main() {
  fio;
  int t;
  // cin>>t;
  t = 1;
  while (t--)
    solve();
}

/*
./a.out <input.txt > output.txt
*/


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

// #define mod 1000000007
// #define mod1 9982
#define int long long int
#define pii pair<int, int>
#define vpii vector<pii>
#define vi vector<int>
#define fi first
#define se second
#define psb push_back
#define ppb pop_back
#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define all(x) x.begin(), x.end()
#define endl "\\n"

const int maxn = 2e5 + 5;
int n, k;
int ar[maxn];

bool ok(int h) {
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += ((ar[i] + h - 1) / h) - 1;
  }
  return cnt <= k;
}

void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> ar[i];
  int l = 0, h = (1e9), ans = -1;
  while (l <= h) {
    int mid = (l + h) / 2;
    if (ok(mid)) {
      ans = mid;
      h = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans << endl;
}

int32_t main() {
  fio;
  int t;
  // cin>>t;
  t = 1;
  while (t--)
    solve();
}

/*
./a.out <input.txt > output.txt
*/
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool f(double mid, vector<int> a, int k) {

  ll t{};
  for (int i = 0; i < a.size(); i++) {
    t += ceil(a.at(i) / mid) - 1;
  }
  return t <= k;
}

int main() {

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (auto &i : a)
    cin >> i;
  int j = 100;
  double ok = 1e9, ng = 0;
  while (--j) {
    auto mid = (ok + ng) / 2;
    (f(mid, a, k) ? ok : ng) = mid;
  }
  cout << (ll)ceil(ok) << endl;
}


Error: TLE

Fixed submission:

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

bool f(double mid, vector<int> a, int k) {

  ll t{};
  for (int i = 0; i < a.size(); i++) {
    t += ceil(a.at(i) / mid) - 1;
  }
  return t <= k;
}

int main() {

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (auto &i : a)
    cin >> i;
  int j = 100000;
  double ok = 1e9, ng = 0;
  while (--j) {
    auto mid = (ok + ng) / 2;
    (f(mid, a, k) ? ok : ng) = mid;
  }
  cout << (ll)ceil(ok) << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

/* 大文字を小文字に変換 */
char tolower(char c) { return (c + 0x20); }
/* 小文字を大文字に変換 */
char toupr(char c) { return (c - 0x20); }

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

const int ma = 1000000000;

int N, K;

int binary_tree(vector<int> A, int left, int right) {
  if (left + 1 == right)
    return right;
  int mid = (left + right) / 2;

  int cnt = 0;
  rep(i, N) {
    if (A[i] % mid == 0) {
      cnt += A[i] / mid - 1;
    } else {
      cnt += A[i] / mid;
    }
  }
  if (cnt > K)
    return binary_tree(A, mid, right);
  // else if(cnt == K) return mid;
  else
    return binary_tree(A, left, mid);
}

int main() {
  cin >> N >> K;
  vector<int> A;

  rep(i, N) {
    int Ai;
    cin >> Ai;
    A.push_back(Ai);
  }
  sort(A.begin(), A.end());
  cout << binary_tree(A, 0, ma) << endl;
}

Error: 0

Fixed submission:

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

/* 大文字を小文字に変換 */
char tolower(char c) { return (c + 0x20); }
/* 小文字を大文字に変換 */
char toupr(char c) { return (c - 0x20); }

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

const int ma = 1000000000;

int N, K;

int binary_tree(vector<int> A, int left, int right) {
  if (left == right)
    return left;
  int mid = (left + right) / 2;

  int cnt = 0;
  rep(i, N) { cnt += A[i] / mid; }
  if (cnt > K)
    return binary_tree(A, mid + 1, right);
  // else if(cnt == K) return mid;
  else
    return binary_tree(A, left, mid);
}

int main() {
  cin >> N >> K;
  vector<int> A;

  rep(i, N) {
    int Ai;
    cin >> Ai;
    A.push_back(Ai);
  }
  sort(A.begin(), A.end());
  if (K == 0)
    cout << A[N - 1] << endl;
  else
    cout << binary_tree(A, 0, ma) << endl;
}