Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <iostream>
#define ll long long int
#define lld long double
#define F first
#define S second
#define f(i, a, b) for (int i = a; i <= b; i++)
#define g(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define mh make_heap
#define ph push_heap
#define pq priority_queue
#define bits(x) __builtin_popcountll(x)
#define op(x) cout << "Case #" << x << ": "
#define op1(x) cout << "Scenario #" << x << ": "
#define endl "\\n"
using namespace std;
const ll mod = 1000000007;
const ll INF = 1e18;
const int N = 20;

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define policy tree<int, null_type,less<int>,
rb_tree_tag,tree_order_statistics_node_update>
*/

bool check(ll arr[], ll n, ll val, ll k) {

  ll sum = 0;

  for (int i = 0; i < n; i++) {
    if (arr[i] > val) {
      ll d = arr[i] / val;
      if (arr[i] % val == 0) {
        d--;
      }
      sum += d;
    }
  }

  if (sum <= k)
    return true;

  return false;
}

void solve(int t) {

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

  ll a[n];
  ll ma = -1;

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

  for (int i = 0; i < n; i++) {
    ma = max(ma, a[i]);
  }

  ll lo = 1;
  ll hi = ma;
  ll ans = ma;

  while (lo <= hi) {
    ll mid = (lo + hi) / 2;

    if (check(a, n, mid, k)) {
      hi = mid - 1;
      ans = mid;
    } else {
      lo = mid + 1;
    }
  }

  cout << ans << endl;
}

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

  int t = 1;
  // cin >> t;

  for (int i = 1; i <= t; i++) {
    solve(i);
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#define ll long long int
#define lld long double
#define F first
#define S second
#define f(i, a, b) for (int i = a; i <= b; i++)
#define g(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define mh make_heap
#define ph push_heap
#define pq priority_queue
#define bits(x) __builtin_popcountll(x)
#define op(x) cout << "Case #" << x << ": "
#define op1(x) cout << "Scenario #" << x << ": "
#define endl "\\n"
using namespace std;
const ll mod = 1000000007;
const ll INF = 1e18;
const int N = 20;

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define policy tree<int, null_type,less<int>,
rb_tree_tag,tree_order_statistics_node_update>
*/

bool check(ll arr[], ll n, ll val, ll k) {

  ll sum = 0;

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

      ll d = arr[i] / val;
      if (arr[i] % val == 0) {
        d--;
      }
      sum += d;
    }
  }

  if (sum <= k)
    return true;

  return false;
}

void solve(int t) {

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

  ll a[n];
  ll ma = -1;

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

  for (int i = 0; i < n; i++) {
    ma = max(ma, a[i]);
  }

  ll lo = 0;
  ll hi = ma;
  ll ans = ma;

  while (lo <= hi) {
    ll mid = (lo + hi) / 2;

    if (check(a, n, mid, k)) {
      hi = mid - 1;
      ans = mid;
    } else {
      lo = mid + 1;
    }
  }

  cout << ans << endl;
}

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

  int t = 1;
  // cin >> t;

  for (int i = 1; i <= t; i++) {
    solve(i);
  }
}