Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define pi 3.14159265358979
#define mod 1000000007
#define ll long long int
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repn(i, a, b) for (ll i = a; i > b; i--)
#define mod1 1000003
#define mod2 998244353
#define inf 2e18
#define hi "hello"
#define nl "\n"
#define ws " "
#define sz size()
#define len length()
#define ff first
#define ss second
#define pb push_back
#define MP make_pair
#define B begin()
#define RB rbegin()
#define E end()
#define UB upper_bound
#define LB lower_bound
#define present(k, m) (m.find(k) != m.end()) ? 1 : 0
#define all(v) v.begin(), v.end()
#define reset(a, val) memset(a, val, sizeof(a))
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpll vector<pll>
#define vs vector<string>
#define vc vector<char>
#define sqr(a) ((a) * (a))
#define print1(a)                                                              \
  for (auto i : a)                                                             \
    cout << i << " ";                                                          \
  cout << "\n"
#define print2(a)                                                              \
  for (auto i : a)                                                             \
    cout << i;                                                                 \
  cout << "\n"
#define print3(a)                                                              \
  for (auto i : a)                                                             \
    cout << i << nl;
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

// ostringstream str1;
// str1 << n;
// string tmp = str1.str();
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}

bool compare(const pair<string, int> &a, const pair<string, int> &b) {
  return a.second < b.second;
}

double logn(double n, double r) { return (n > r - 1) ? 1 + logn(n / r, r) : 0; }

// vll pwx(32);
// void cal(ll x){ pwx[0] = 1; rep(i, 1, 32)pwx[i] = x*pwx[i-1]; }

/* ################################################################ */

#define space 200005

bool check(vector<ll> &a, ll x, ll k) {
  ll req = 0;

  for (ll i = 0; i < a.size(); i++) {
    if (a[i] > x)
      req += a[i] / x;
  }

  if (req > k)
    return false;
  return true;
}

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

  vector<ll> a(n);

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

  ll ans = 0;

  ll low = 0, high = 1e18, mid;

  while (low <= high) {
    mid = (low + high) / 2;

    // cout << low << ws << mid << ws << high << nl;;

    if (check(a, mid, k)) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }

  cout << ans << nl;
}

int main() {
  // boost;
  // cal(2);
  int t = 1, k = 1;
  // cin>>t;
  while (t--) {
    // cout<<"Case #"<<k<<": ";
    solve();
    k++;
  }
}

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

#define pi 3.14159265358979
#define mod 1000000007
#define ll long long int
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repn(i, a, b) for (ll i = a; i > b; i--)
#define mod1 1000003
#define mod2 998244353
#define inf 2e18
#define hi "hello"
#define nl "\n"
#define ws " "
#define sz size()
#define len length()
#define ff first
#define ss second
#define pb push_back
#define MP make_pair
#define B begin()
#define RB rbegin()
#define E end()
#define UB upper_bound
#define LB lower_bound
#define present(k, m) (m.find(k) != m.end()) ? 1 : 0
#define all(v) v.begin(), v.end()
#define reset(a, val) memset(a, val, sizeof(a))
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpll vector<pll>
#define vs vector<string>
#define vc vector<char>
#define sqr(a) ((a) * (a))
#define print1(a)                                                              \
  for (auto i : a)                                                             \
    cout << i << " ";                                                          \
  cout << "\n"
#define print2(a)                                                              \
  for (auto i : a)                                                             \
    cout << i;                                                                 \
  cout << "\n"
#define print3(a)                                                              \
  for (auto i : a)                                                             \
    cout << i << nl;
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

// ostringstream str1;
// str1 << n;
// string tmp = str1.str();
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}

bool compare(const pair<string, int> &a, const pair<string, int> &b) {
  return a.second < b.second;
}

double logn(double n, double r) { return (n > r - 1) ? 1 + logn(n / r, r) : 0; }

// vll pwx(32);
// void cal(ll x){ pwx[0] = 1; rep(i, 1, 32)pwx[i] = x*pwx[i-1]; }

/* ################################################################ */

#define space 200005

bool check(vector<ll> &a, ll x, ll k) {
  ll req = 0;

  for (ll i = 0; i < a.size(); i++) {
    if (a[i] > x)
      req += a[i] / x;
  }

  if (req > k)
    return false;
  return true;
}

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

  vector<ll> a(n);

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

  ll ans = 0;

  ll low = 1, high = 1e18, mid;

  while (low <= high) {
    mid = (low + high) / 2;

    // cout << low << ws << mid << ws << high << nl;;

    if (check(a, mid, k)) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }

  cout << ans << nl;
}

int main() {
  // boost;
  // cal(2);
  int t = 1, k = 1;
  // cin>>t;
  while (t--) {
    // cout<<"Case #"<<k<<": ";
    solve();
    k++;
  }
}