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

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

const int MAX = 200000;

int N, K;
int A[MAX];

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

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

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

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

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

const int MAX = 200000;

int N, K;
int A[MAX];

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

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

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

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

  cout << ans << endl;

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

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


Error: 0

Fixed submission:

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


Error: 0

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 = -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;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define db long double
#define f(i, l, u) for (ll i = l; i < u; i++)
ll k;
ll val;
ll bs(ll left, ll right, ll a[], ll n) {

  ll cost = 0;
  ll mid = (left + right) / 2;
  //  cout<<"left = "<<left<<" right= "<<right<<endl;
  //    cout<<"mid = "<<mid<<endl;
  f(i, 0, n) {
    cost += a[i] / mid;
    if (a[i] % mid == 0)
      cost--;
  }
  //  cout<<"cost= "<<cost<<endl<<endl;
  if (left == right) {
    //    cout<<"cost= "<<cost<<endl;
    return mid;

  }

  else if (cost <= k)
    return bs(left, mid, a, n);
  else
    return bs(mid + 1, right, a, n);
}

int main() {

  ll n;
  cin >> n;

  cin >> k;
  ll a[n];
  f(i, 0, n) { cin >> a[i]; }

  sort(a, a + n);
  ll val = bs(1, a[n - 1], a, n);
  cout << val << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define db long double
#define f(i, l, u) for (ll i = l; i < u; i++)
ll k;

ll bs(ll left, ll right, ll a[], ll n) {
  ll cost = 0;
  ll mid = (left + right) / 2;
  f(i, 0, n) {
    cost += a[i] / mid;
    if (a[i] % mid == 0)
      cost--;
  }
  if (left == right)
    return mid;
  else if (cost <= k)
    return bs(left, mid - 1, a, n);
  else
    return bs(mid + 1, right, a, n);
}

int main() {

  ll n;
  cin >> n;

  cin >> k;
  ll a[n];
  f(i, 0, n) { cin >> a[i]; }

  sort(a, a + n);
  ll val = bs(1, a[n - 1], a, n);
  cout << val << endl;

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

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

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


Error: 0

Fixed submission:

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

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

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

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

  return output <= k;
}

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

  ll mini = 1;
  ll maxi = 0;

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

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

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

  cout << maxi << endl;

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

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

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

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

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

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

Error: 0

Fixed submission:

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

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

  return output <= k;
}

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

  ll mini = 0;
  ll maxi = 0;

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

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

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

  cout << maxi << endl;

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

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

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

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

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

  // cout << logs.top() << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
const ll MOD = 1000000007;
const long double PI = 3.14159265358979;
const ll MAX = 0;

int main() {
  ll N, K;
  cin >> N >> K;
  ll a[N];
  rep(i, N) { cin >> a[i]; }
  ll l = 1, r = 1000000000;
  while (r - l > 10) {
    ll mid = (l + r) / 2;
    ll k = 0;
    rep(i, N) { k += (a[i] - 1) / mid; }
    if (k > K) {
      l = mid;
    } else {
      r = mid;
    }
  }
  ll ans = l;
  while (1) {
    ll k = 0;
    rep(i, N) { k += (a[i] - 1) / ans; }
    if (k > K) {
      ans++;
    } else {
      break;
    }
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
const ll MOD = 1000000007;
const long double PI = 3.14159265358979;
const ll MAX = 0;

int main() {
  ll N, K;
  cin >> N >> K;
  ll a[N];
  rep(i, N) { cin >> a[i]; }
  ll l = 1, r = 1000000000;
  while (r - l > 10000) {
    ll mid = (l + r) / 2;
    ll k = 0;
    rep(i, N) { k += (a[i] - 1) / mid; }
    if (k > K) {
      l = mid;
    } else {
      r = mid;
    }
  }
  ll ans = l;
  while (1) {
    ll k = 0;
    rep(i, N) { k += (a[i] - 1) / ans; }
    if (k > K) {
      ans++;
    } else {
      break;
    }
  }
  cout << ans;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define rep(i, l, r) for (ll i = (l); i < (r); ++i)
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define pii pair<ll, ll>
#define pll pair<ll, ll>
#define pli pair<ll, ll>
#define pdd pair<ld, ld>
#define pb push_back
#define reset(x, v) memset(x, v, sizeof(x))
#define mod 1000000007
using namespace std;
bool poss(int a[], int n, int mid, int k) {
  if (mid == 0)
    return true;
  int c = 0;
  rep(i, 0, n) {
    int z = a[i] / mid + (a[i] % mid == 0 ? -1 : 0);
    c += z;
  }
  return c <= k;
}
int main() {
  FASTIO
  int n, k;
  cin >> n >> k;
  int a[n];
  rep(i, 0, n) cin >> a[i];
  int lo = 0, hi = *max_element(a, a + n), ans = *max_element(a, a + n);
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    if (poss(a, n, mid, k)) {
      hi = mid - 1;
      ans = mid;
    } else {
      lo = mid + 1;
    }
  }
  cout << max(ans, 1);
  return 0;
}

Error: WA

Fixed submission:

#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define rep(i, l, r) for (ll i = (l); i < (r); ++i)
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define pii pair<ll, ll>
#define pll pair<ll, ll>
#define pli pair<ll, ll>
#define pdd pair<ld, ld>
#define pb push_back
#define reset(x, v) memset(x, v, sizeof(x))
#define mod 1000000007
using namespace std;
bool poss(int a[], int n, int mid, int k) {
  int c = 0;
  rep(i, 0, n) {
    int z = a[i] / mid + (a[i] % mid == 0 ? -1 : 0);
    c += z;
  }
  return c <= k;
}
int main() {
  FASTIO
  int n, k;
  cin >> n >> k;
  int a[n];
  rep(i, 0, n) cin >> a[i];
  int lo = 0, hi = *max_element(a, a + n), ans = *max_element(a, a + n);
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    if (poss(a, n, mid, k)) {
      hi = mid - 1;
      ans = mid;
    } else {
      lo = mid + 1;
    }
  }
  cout << ans;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

using namespace std;

bool possible(long long longestLog, const vector<long long> &A, long long K) {
  long long N = A.size();
  long long numOfLongestLogs = 0;
  for (long long i = 0; i < N; ++i) {
    numOfLongestLogs += A[i] / longestLog;
    if (A[i] % longestLog == 0) {
      --numOfLongestLogs;
    }
  }
  return (numOfLongestLogs <= K);
}

void Main() {
  long long N, K;
  cin >> N >> K;
  vector<long long> A(N, 0LL);
  for (long long i = 0; i < N; ++i) {
    cin >> A[i];
  }
  sort(A.begin(), A.end(), greater<long long>());

  long long ub = A.front();
  long long lb = 0LL;
  while (ub - lb > 1LL) {
    long long mid = (ub + lb) / 2LL;
    if (possible(mid, A, K)) {
      ub = mid;
    } else {
      lb = mid;
    }
  }
  bool test = possible(lb, A, K);
  if (test) {
    test != test;
  }
  cout << ub << endl;
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

bool possible(long long longestLog, const vector<long long> &A, long long K) {
  long long N = A.size();
  long long numOfLongestLogs = 0;
  for (long long i = 0; i < N; ++i) {
    numOfLongestLogs += A[i] / longestLog;
    if (A[i] % longestLog == 0) {
      --numOfLongestLogs;
    }
  }
  return (numOfLongestLogs <= K);
}

void Main() {
  long long N, K;
  cin >> N >> K;
  vector<long long> A(N, 0LL);
  for (long long i = 0; i < N; ++i) {
    cin >> A[i];
  }
  sort(A.begin(), A.end(), greater<long long>());

  long long ub = A.front();
  long long lb = 0LL;
  while (ub - lb > 1LL) {
    long long mid = (ub + lb) / 2LL;
    if (possible(mid, A, K)) {
      ub = mid;
    } else {
      lb = mid;
    }
  }
  bool test = possible(lb, A, K);
  if (test) {
    cout << "";
  }
  cout << ub << endl;
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\\|/__\\|/__\\|/__\\|/__\\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 200002;

int n;
ll k;

ll a[N_MAX];

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll l = 1, r = 1000000000;
  while (l < r) {
    ll mid = ((l + r) >> 1);
    if (f(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << "\\n";
  return 0;
}


Error: 0

Fixed submission:

/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\\|/__\\|/__\\|/__\\|/__\\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 200002;

int n;
ll k;

ll a[N_MAX];

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll l = 0, r = 1000000000;
  while (l < r) {
    ll mid = ((l + r) >> 1);
    if (f(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << "\\n";
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#define dlog(str) cout << "====" << str << endl;
#else
#define dlog(str)
#endif
#define INF 999999999
#define MOD 1000000007
#define REP(i, n) for (int i = 0, i##_l = (n); i < i##_l; i++)
#define FOR(i, s, e) for (int i = s, i##_l = (e); i < i##_l; i++)
#define LLI long long int
#define _min(a, b) ((a < b) ? a : b)
#define _max(a, b) ((a < b) ? b : a)
#define chmax(a, b) a = _max(a, b)
#define chmin(a, b) a = _min(a, b)
#define bit(a, shift) ((a>>shift)&1))
#define pm(a) ((a) ? 1 : -1)
#define SORT(v) sort(v.begin(), v.end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
// int 2.14E±9    lli 9.2E±18    double 1.7E±380

int main() {
  cout << fixed << setprecision(10);
  int n, k;
  cin >> n >> k;

  vector<int> a(n);
  REP(i, n) { cin >> a[i]; }
  RSORT(a);

  LLI l = 1, r = 1e9;

  while (l < r) {
    LLI m = (l + r) / 2;
    int c = 0;
    REP(i, n) { c += (a[i] - 1) / m; }

    if (c <= k) {
      r = m;
    } else {
      l = m + 1;
    }
  }
  cout << l << endl;

  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#define dlog(str) cout << "====" << str << endl;
#else
#define dlog(str)
#endif
#define INF 999999999
#define MOD 1000000007
#define REP(i, n) for (int i = 0, i##_l = (n); i < i##_l; i++)
#define FOR(i, s, e) for (int i = s, i##_l = (e); i < i##_l; i++)
#define LLI long long int
#define _min(a, b) ((a < b) ? a : b)
#define _max(a, b) ((a < b) ? b : a)
#define chmax(a, b) a = _max(a, b)
#define chmin(a, b) a = _min(a, b)
#define bit(a, shift) ((a>>shift)&1))
#define pm(a) ((a) ? 1 : -1)
#define SORT(v) sort(v.begin(), v.end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
// int 2.14E±9    lli 9.2E±18    double 1.7E±380

int main() {
  cout << fixed << setprecision(10);
  int n, k;
  cin >> n >> k;

  vector<int> a(n);
  REP(i, n) { cin >> a[i]; }
  RSORT(a);

  LLI l = 0, r = 1e9;

  while (l < r) {
    LLI m = (l + r) / 2;
    int c = 0;
    REP(i, n) { c += (a[i] - 1) / m; }

    if (c <= k) {
      r = m;
    } else {
      l = m + 1;
    }
  }
  cout << l << endl;

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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)
#define rrep(i, j, n) for (int i = (int)n - 1; j <= i; --i)

#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\\n'
template <typename T> ostream &operator<<(ostream &os, vector<T> &a) {
  for (int i = 0; i < (int)a.size(); ++i)
    os << a[i] << " \\n"[i == (int)a.size() - 1];
  return os;
}

constexpr ll MOD = 1000000007;
constexpr int INF = 0x3f3f3f3f;
constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;

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

  auto c = [&](double x) { // 最大値をx以下にできるか。
    ll res = 0;
    rep(i, 0, n) {
      if (x >= a[i])
        continue;
      // double j = 1.0;
      // while (x < a[i] / (j + 1.0)) j += 1.0;
      ll j = ceil(a[i] / x);
      res += j - 1;
    }
    return res <= k;
  };
  double l = 0.0, r = 1e9 + 5;
  for (int i = 0; i < 100; ++i) {
    double mid = (r + l) / 2.0;
    if (c(mid))
      r = mid;
    else
      l = mid;
  }
  ll ans = ceil(r);
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)
#define rrep(i, j, n) for (int i = (int)n - 1; j <= i; --i)

#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\\n'
template <typename T> ostream &operator<<(ostream &os, vector<T> &a) {
  for (int i = 0; i < (int)a.size(); ++i)
    os << a[i] << " \\n"[i == (int)a.size() - 1];
  return os;
}

constexpr ll MOD = 1000000007;
constexpr int INF = 0x3f3f3f3f;
constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;

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

  auto c = [&](double x) { // 最大値をx以下にできるか。
    ll res = 0;
    rep(i, 0, n) {
      if (x >= a[i])
        continue;
      double j = 1.0;
      while (x < a[i] / (j + 1.0))
        j += 1.0;
      res += (ll)j;
    }
    return res <= k;
  };
  double ng = 0.0, ok = 1e18;
  for (int i = 0; i < 1000; ++i) {
    double mid = (ok + ng) / 2.0;
    if (c(mid))
      ok = mid;
    else
      ng = mid;
  }
  ll ans = ceil(ok);
  cout << ans << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);

  int l = 1, r = 0;
  rep(i, 0, n) {
    cin >> a[i];
    r = max(r, a[i]);
  }

  // upper bound
  while (l != r) {
    int mid = (r + l) >> 1;
    bool ok = [&] {
      int res = 0;
      rep(i, 0, n) { res += (a[i] - 1) / mid; }
      return res <= k;
    }();
    if (ok)
      r = mid;
    else
      l = mid + 1;
  }
  cout << l;
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);

  int l = 0, r = 0;
  rep(i, 0, n) {
    cin >> a[i];
    r = max(r, a[i]);
  }

  // upper bound
  while (l != r) {
    int mid = (r + l) >> 1;
    bool ok = [&] {
      int res = 0;
      rep(i, 0, n) { res += (a[i] - 1) / mid; }
      return res <= k;
    }();
    if (ok)
      r = mid;
    else
      l = mid + 1;
  }
  cout << l;
  return 0;
}