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

#include <bits/stdc++.h>

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll l = 0, r = 1e9, m;
  while (r > 1 && l < r) {
    m = (l + r) / 2;
    ll cnt = 0;
    for (int i = 0; i < n; i++)
      cnt += (a[i] - 1) / m;
    if (k < cnt)
      l = m + 1;
    else
      r = m - 1;
  }
  r = max(1ll, r - 2);
  while (1) {
    ll cnt = 0;
    for (int i = 0; i < n; i++)
      cnt += (a[i] - 1) / r;
    if (cnt <= k) {
      cout << r << endl;
      return 0;
    } else
      r++;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll l = 0, r = 1e9, m;
  while (l < r) {
    m = (l + r) / 2;
    ll cnt = 0;
    for (int i = 0; i < n; i++)
      cnt += (a[i] - 1) / m;
    if (k < cnt)
      l = m + 1;
    else
      r = m - 1;
  }
  r = max(1ll, r - 2);
  while (1) {
    ll cnt = 0;
    for (int i = 0; i < n; i++)
      cnt += (a[i] - 1) / r;
    if (cnt <= k) {
      cout << r << endl;
      return 0;
    } else
      r++;
  }
}
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;
using ll = long long;

vector<ll> a(200000);

int main(void) {
  ll n, k;
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  ll l = 1, h = *max_element(a.begin(), a.begin() + n);
  while (l < h) {
    ll mid = (l + h) / 2, cnt = 0;
    rep(i, n) {
      cnt += a[i] / mid;
      if (a[i] % mid == 0)
        --cnt;
    }
    if (cnt <= k)
      h = mid;
    else
      l = mid + 1;
  }
  cout << h << endl;
  return 0;
}

Error: 0

Fixed submission:

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

vector<ll> a(200000);

int main(void) {
  ll n, k;
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  ll l = 0, h = *max_element(a.begin(), a.begin() + n);
  while (l < h) {
    ll mid = (l + h) / 2, cnt = 0;
    rep(i, n) {
      cnt += a[i] / mid;
      if (a[i] % mid == 0)
        --cnt;
    }
    if (cnt <= k)
      h = mid;
    else
      l = mid + 1;
  }
  cout << h << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define eb emplace_back
#define ii pair<int, int>
#define OK (cerr << "OK" << endl)
#define debug(x) cerr << #x " = " << (x) << endl
#define ff first
#define ss second
#define int long long
#define tt tuple<int, int, int>
#define all(x) x.begin(), x.end()
#define vvi vector<vector<int>>
#define vvii vector<vector<pair<int, int>>>
#define Mat(n, m, v) vector<vector<int>>(n, vector<int>(m, v))
#define endl '\\n'

constexpr int INF = 2e18;
constexpr int MOD = 1e9 + 7;
constexpr int MAXN = 2e5 + 3;

bool check(int g, vector<int> &arr, int k) {
  int c = 0;
  for (int x : arr) {
    c += (x - 1) / g;
  }

  return (c <= k);
}

// #define MULTIPLE_TEST_CASES
void solve() {
  int n;
  cin >> n;
  int k;
  cin >> k;

  vector<int> arr(n);
  for (int &x : arr)
    cin >> x;

  int l = 1, r = 2e9;
  int ans = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid, arr, k)) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << ans << endl;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t = 1;
#ifdef MULTIPLE_TEST_CASES
  cin >> t;
#endif
  while (t--)
    solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define eb emplace_back
#define ii pair<int, int>
#define OK (cerr << "OK" << endl)
#define debug(x) cerr << #x " = " << (x) << endl
#define ff first
#define ss second
#define int long long
#define tt tuple<int, int, int>
#define all(x) x.begin(), x.end()
#define vvi vector<vector<int>>
#define vvii vector<vector<pair<int, int>>>
#define Mat(n, m, v) vector<vector<int>>(n, vector<int>(m, v))
#define endl '\\n'

constexpr int INF = 2e18;
constexpr int MOD = 1e9 + 7;
constexpr int MAXN = 2e5 + 3;

bool check(int g, vector<int> &arr, int k) {
  int c = 0;
  for (int x : arr) {
    c += (x - 1) / g;
  }

  return (c <= k);
}

// #define MULTIPLE_TEST_CASES
void solve() {
  int n;
  cin >> n;
  int k;
  cin >> k;

  vector<int> arr(n);
  for (int &x : arr)
    cin >> x;

  int l = 0, r = 2e9;
  int ans = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid, arr, k)) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << ans << endl;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t = 1;
#ifdef MULTIPLE_TEST_CASES
  cin >> t;
#endif
  while (t--)
    solve();
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n - 1); i >= 0; i--)
#define all(x) x.begin(), x.end()
#define br cout << "\\n";
using namespace std;
const long long INF = 1e10;
const long long MOD = 1e9 + 7;
using Graph = vector<vector<ll>>;
using pll = pair<ll, ll>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// 0 false, 1 true
// string number to int : -48
// a to A : -32
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp

ll n, k;
vector<ll> vec(n);

// return the number of ng
// left always ng. right always ok
long long nibun_search(ll val) {
  long long left = -1;
  long long right = val;
  while (right - left > 1) {
    long long mid = (left + right) / 2;
    if (mid == 0) {
      return 1;
    }
    ll count = 0;
    rep(i, n) {
      if (vec[i] % mid == 0) {
        count += ((vec[i] / mid) - 1);

      } else {
        count += (vec[i] / mid);
      }
    }
    if (count <= k) {
      right = mid;
    } else {
      left = mid;
    }
  }
  return right;
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  cin >> n >> k;
  vec.resize(n);

  for (long long i = 0; i < n; i++) {
    cin >> vec[i];
  }
  cout << nibun_search(1000000000) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n - 1); i >= 0; i--)
#define all(x) x.begin(), x.end()
#define br cout << "\\n";
using namespace std;
const long long INF = 1e10;
const long long MOD = 1e9 + 7;
using Graph = vector<vector<ll>>;
using pll = pair<ll, ll>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// 0 false, 1 true
// string number to int : -48
// a to A : -32
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp

ll n, k;
vector<ll> vec(n);

// return the number of ng
// left always ng. right always ok
long long nibun_search(ll val) {
  long long left = -1;
  long long right = val;
  while (right - left > 1) {
    long long mid = (left + right) / 2;
    if (mid == 0) {
      mid = 1;
    }
    ll count = 0;
    rep(i, n) {
      if (vec[i] % mid == 0) {
        count += ((vec[i] / mid) - 1);

      } else {
        count += (vec[i] / mid);
      }
    }
    if (count <= k) {
      right = mid;
    } else {
      left = mid;
    }
  }
  return right;
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  cin >> n >> k;
  vec.resize(n);

  for (long long i = 0; i < n; i++) {
    cin >> vec[i];
  }
  cout << nibun_search(1000000000) << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define loopi(x, y) for (int i = x; i < (y); i++)
#define loopj(x, y) for (int j = x; j < (y); j++)
#define rloopi(x, y) for (int i = x; i >= (y); i--)
#define rloopj(x, y) for (int j = x; j >= (y); j--)

#define nl cout << "\\n";
#define ll long long int
#define mod 1000000007

using namespace std;

int main() {

  ios ::sync_with_stdio(false);
  cin.tie(nullptr);

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

  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  ll q = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > q)
      q = a[i];
  }

  ll p = 1;
  while (p < q) {
    ll mid = (p + q) / 2;
    ll count = 0;
    for (int i = 0; i < n; i++) {
      count += (a[i] - 1) / mid;
    }

    if (count <= k) {
      q = mid;
    } else {
      p = mid + 1;
    }
  }

  cout << p;

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define loopi(x, y) for (int i = x; i < (y); i++)
#define loopj(x, y) for (int j = x; j < (y); j++)
#define rloopi(x, y) for (int i = x; i >= (y); i--)
#define rloopj(x, y) for (int j = x; j >= (y); j--)

#define nl cout << "\\n";
#define ll long long int
#define mod 1000000007

using namespace std;

int main() {

  ios ::sync_with_stdio(false);
  cin.tie(nullptr);

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

  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  ll q = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > q)
      q = a[i];
  }

  ll p = 0;
  while (p < q) {
    ll mid = (p + q) / 2;
    ll count = 0;
    for (int i = 0; i < n; i++) {
      count += (a[i] - 1) / mid;
    }

    if (count <= k) {
      q = mid;
    } else {
      p = mid + 1;
    }
  }

  cout << p;

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

// #include <fsociety>
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// end of libraries ;
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
//  ll lmax(ll o , ll w) {if(o>w)return o; return w;}
//  ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define endl "\\n"
#define F first
#define S second
#define pb push_back
#define ll long long
#define all(c) (c).begin(), (c).end()
#define sz(c) (c).size()
#define fcin ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
using namespace std;
ll n, k, a[N], ans;
int main() {
  fcin;
  cin >> n >> k;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll l = 1, r = 10e8;
  while (l <= r) {
    ll mid = (l + r) / 2, c = 0;
    for (ll i = 0; i < n; i++) {
      if (a[i] < mid)
        continue;
      c += ((a[i] / mid + (a[i] % mid != 0)) - 1);
    }
    if (c > k) {
      l = mid + 1;
    } else {
      ans = mid;
      r = mid - 1;
    }
  }
  cout << ans << "\\n";
}

Error: 0

Fixed submission:

// #include <fsociety>
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// end of libraries ;
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
//  ll lmax(ll o , ll w) {if(o>w)return o; return w;}
//  ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define endl "\\n"
#define F first
#define S second
#define pb push_back
#define ll long long
#define all(c) (c).begin(), (c).end()
#define sz(c) (c).size()
#define fcin ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
using namespace std;
ll n, k, a[N], ans;
int main() {
  fcin;
  cin >> n >> k;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll l = 0, r = 10e10;
  while (l <= r) {
    ll mid = (l + r) / 2, c = 0;
    for (ll i = 0; i < n; i++) {
      if (a[i] < mid)
        continue;
      c += ((a[i] / mid + (a[i] % mid != 0)) - 1);
    }
    if (c > k) {
      l = mid + 1;
    } else {
      ans = mid;
      r = mid - 1;
    }
  }
  cout << ans << "\\n";
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, k;
long long a[200002];
priority_queue<pair<int, pair<int, int>>> pq;

bool ck(long long x) {
  int tmp = 0;
  for (int i = 0; i < n; i++) {
    tmp += ceil((double)a[i] / x);
    tmp--;
  }

  if (tmp <= k)
    return (true);
  return (false);
}

int main() {

  /*
  cin>>n>>k;
  for(int i=0;i<n;i++){
          cin>>a[i];
          pq.push(make_pair(a[i],make_pair(a[i],1)));
  }

  while(cnt<k){
          pair<int,pair<int,int> >p = pq.top();pq.pop();
          pair<int,pair<int,int> >np = pq.top();
          if(p.first == 1)break;

          cnt = cnt - (p.second.second-1);
          int tmp = (p.second.first/np.first)+1;
          while(k < cnt+(tmp-1))tmp=k-cnt+1;
          cnt = cnt+(tmp-1);

          p.second.second = tmp;
          p.first = ceil((double)p.second.first/p.second.second);
          pq.push(p);

  }

  cout<<pq.top().first<<endl;
  */

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

  long long left = 1, right = 1000000000;
  long long ans = right;
  while (left <= right) {
    long long mid = (left + right) / 2;
    if (ck(mid) == true) {
      ans = mid;
      right = mid - 1;
    } else {
      left = mid + 1;
    }
  }

  cout << ans << endl;
  return (0);
}

Error: TLE

Fixed submission:

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

int n, k;
int a;
int cnt = 0;
priority_queue<pair<int, pair<int, int>>> pq;

int main() {

  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a;
    pq.push(make_pair(a, make_pair(a, 1)));
  }

  while (cnt < k) {
    pair<int, pair<int, int>> p = pq.top();
    pq.pop();
    pair<int, pair<int, int>> np = pq.top();
    if (p.first == 1)
      break;

    cnt = cnt - (p.second.second - 1);
    int tmp = (p.second.first / np.first) + 1;
    while (k < cnt + (tmp - 1))
      tmp--;
    cnt = cnt + (tmp - 1);

    p.second.second = tmp;
    p.first = ceil((double)p.second.first / p.second.second);
    pq.push(p);
  }

  cout << pq.top().first << endl;

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

/*COMPETITIVE PROGRAMMING C++ TEMPLATE */

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << x << "\\n"
#define MOD 998244353

const long double PI = 3.141592653589793236L;

typedef long long int ll;
typedef long double ld;

using namespace std;

bool check(ll ans, vector<ll> &a, ll k) {

  if (ans == 0) {
    return false;
  }

  ll count = 0;
  int n = a.size();
  for (int i = 0; i < n; i++) {
    count += a[i] / ans - 1;
    if (a[i] % ans) {
      count++;
    }
  }

  if (count <= k) {
    return true;
  }

  return false;
}

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

  vector<ll> a(n);

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

  ll lo = 0;
  ll high = 1e9;
  ll ans = high;

  while (lo <= high) {
    ll mid = lo + (high - lo) / 2;
    bool is_valid = check(mid, a, k);

    if (is_valid) {
      ans = mid;
      high = mid - 1;
    } else {
      lo = mid + 1;
    }
  }

  cout << ans << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int T = 1;
  // cin >> T;

  while (T--) {
    solve();
  }

  return 0;
}


Error: 0

Fixed submission:

/*COMPETITIVE PROGRAMMING C++ TEMPLATE */

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << x << "\\n"
#define MOD 998244353

const long double PI = 3.141592653589793236L;

typedef long long int ll;
typedef long double ld;

using namespace std;

bool check(ll ans, vector<ll> &a, ll k) {
  ll count = 0;
  int n = a.size();
  for (int i = 0; i < n; i++) {
    count += a[i] / ans - 1;
    if (a[i] % ans) {
      count++;
    }
  }

  if (count <= k) {
    return true;
  }

  return false;
}

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

  vector<ll> a(n);

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

  ll lo = 0;
  ll high = 1e9;
  ll ans = high;

  while (lo <= high) {
    ll mid = lo + (high - lo) / 2;
    bool is_valid = check(mid, a, k);

    if (is_valid) {
      ans = mid;
      high = mid - 1;
    } else {
      lo = mid + 1;
    }
  }

  cout << ans << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int T = 1;
  // cin >> T;

  while (T--) {
    solve();
  }

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

#include <bits/stdc++.h>

typedef long long int ll;
typedef unsigned long long int ull;
#define rep(i, n) for (auto i = 0; i < (n); i++)
#define rep1(i, n) for (auto i = 1; i <= (n); i++)
#define all(x) (x).begin(), (x).end()

using namespace std;

struct init {
  init() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
} aaaaaaa;

int solve(int k, set<int, std::greater<int>> st, map<int, int> ma) {
  priority_queue<int> que;
  for (auto e : st)
    que.push(e);
  int e = que.top();
  while (!que.empty()) {
    e = que.top(), que.pop();
    if (ma[e] == 0)
      continue;
    if (k < ma[e])
      break;
    int half = e / 2 + e % 2;
    ma[half] += 2 * ma[e];
    k -= ma[e];
    ma[e] = 0;
    que.push(half);
  }

  return e;
}

bool check(int m, set<int, std::greater<int>> &st, map<int, int> &ma, int k) {
  int kk = 0;
  for (auto e : st)
    kk += ((e - 1) / m) * ma[e];
  return kk <= k;
}

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

  map<int, int> ma;
  set<int, std::greater<int>> st;
  rep(i, n) {
    ma[a[i]]++;
    st.insert(a[i]);
  }

  int l = 1;
  int r = solve(k, st, ma);

  while (l != r) {
    int m = (l + r) / 2;
    if (check(m, st, ma, k))
      r = m;
    else
      l = m;
    if (l + 1 == r)
      break;
  }

  cout << ((check(l, st, ma, k)) ? l : r) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

typedef long long int ll;
typedef unsigned long long int ull;
#define rep(i, n) for (auto i = 0; i < (n); i++)
#define rep1(i, n) for (auto i = 1; i <= (n); i++)
#define all(x) (x).begin(), (x).end()

using namespace std;

struct init {
  init() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
} aaaaaaa;

int solve(int n, int k, set<int, std::greater<int>> st, map<int, int> ma) {
  int e = *st.begin();
  while (!st.empty()) {
    e = *st.begin();
    if (ma[e] == 0)
      continue;
    if (k < ma[e])
      break;
    int half = e / 2 + e % 2;
    ma[half] += 2 * ma[e];
    k -= ma[e];
    ma[e] = 0;
    st.erase(e);
    st.insert(half);
  }

  return e;
}

bool check(int m, set<int, std::greater<int>> &st, map<int, int> &ma, int k) {
  int kk = 0;
  for (auto e : st)
    kk += ((e - 1) / m) * ma[e];
  return kk <= k;
}

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

  map<int, int> ma;
  set<int, std::greater<int>> st;
  rep(i, n) {
    ma[a[i]]++;
    st.insert(a[i]);
  }

  int l = 1;
  int r = solve(n, k, st, ma);

  while (l != r) {
    int m = (l + r) / 2;
    if (check(m, st, ma, k))
      r = m;
    else
      l = m;
    if (l + 1 == r)
      break;
  }

  cout << ((check(l, st, ma, k)) ? l : r) << endl;

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

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  auto f = [&](int64_t x) {
    int64_t res = 0;
    if (x == 0)
      return false;
    for (int i = 0; i < N; i++) {
      res += (A[i] - 1) / x;
    }
    return res <= K;
  };
  int64_t lo = 0, hi = *max_element(A.begin(), A.end()) + 1;
  while (lo < hi) {
    int64_t mi = (lo + hi) / 2;
    if (f(mi)) {
      hi = mi;
    } else {
      lo = mi + 1;
    }
  }
  cout << hi << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  int64_t K;
  cin >> N >> K;
  vector<int64_t> A(N);
  int64_t lo = 0, hi = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    hi = max(hi, A[i]);
  }
  auto f = [&](int64_t x) {
    int64_t res = 0;
    for (int i = 0; i < N; i++) {
      res += (A[i] - 1) / x;
    }
    return res <= K;
  };
  while (lo < hi) {
    int64_t mi = (lo + hi) / 2;
    if (f(mi)) {
      hi = mi;
    } else {
      lo = mi + 1;
    }
  }
  cout << hi << '\\n';
  return 0;
}
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);
  int maxsize = 0;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    maxsize = max(A[i], maxsize);
  }

  int l = 1;
  int r = maxsize;
  for (int n = 0; n < 100; ++n) {
    int x = (l + r) / 2;
    int count = 0;
    for (int i = 0; i < N; ++i) {
      count += A[i] / x;
      if (0 != A[i] % x) {
        count++;
      }
    }
    if (count > N + K) {
      l = x;
    } else {
      r = x;
    }
  }
  cout << r << endl;
  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);
  int maxsize = 0;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    maxsize = max(A[i], maxsize);
  }

  int l = 0;
  int r = maxsize;
  for (int n = 0; n < 100; ++n) {
    int x = (l + r) / 2;
    int count = 0;
    for (int i = 0; i < N; ++i) {
      count += A[i] / x;
      if (0 != A[i] % x) {
        count++;
      }
    }
    if (count > N + K) {
      l = x;
    } else {
      r = x;
    }
  }
  cout << r << endl;
  return 0;
}