Problem p02598 (C++)

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

#define ll long long
#define db double
#define el "\n"
#define ld long double
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n; i >= 0; i--)
#define rep_a(i, a, n) for (int i = a; i < n; i++)
#define tr(ds, it) for (auto it = ds.begin(); it != ds.end(); it++)
#define rtr(ds, it) for (auto it = ds.rbegin(); it != ds.rend(); it--)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef priority_queue<int> pq;
typedef vector<long long> vi;
typedef pair<long long, long long> ii;
#define o_set                                                                  \
  tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update>
#define graph vector<vi>
#define Mod 1000000007
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define T                                                                      \
  int t;                                                                       \
  cin >> t;                                                                    \
  rep(i, t)

int binarysearch(int arr[], int n, int mid) {

  int ans = 0;

  rep(i, n) {

    ans += arr[i] / mid;
    if (arr[i] % mid == 0)
      ans--;
  }

  return ans;
}

void solve() {

  int n, k;
  cin >> n >> k;
  int arr[n];
  int front = 0, end = 0;
  rep(i, n) {

    cin >> arr[i];
    end = max(end, arr[i]);
  }

  while (front < end) {

    int mid = front + (end - front) / 2;

    int temp = binarysearch(arr, n, mid);

    if (temp <= k)
      end = mid;
    else
      front = mid + 1;
  }

  cout << end << "\n";
}

int main() {
  fastio;
  // T
  solve();
  // code
  return 0;
}


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

#define ll long long
#define db double
#define el "\n"
#define ld long double
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n; i >= 0; i--)
#define rep_a(i, a, n) for (int i = a; i < n; i++)
#define tr(ds, it) for (auto it = ds.begin(); it != ds.end(); it++)
#define rtr(ds, it) for (auto it = ds.rbegin(); it != ds.rend(); it--)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef priority_queue<int> pq;
typedef vector<long long> vi;
typedef pair<long long, long long> ii;
#define o_set                                                                  \
  tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update>
#define graph vector<vi>
#define Mod 1000000007
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define T                                                                      \
  int t;                                                                       \
  cin >> t;                                                                    \
  rep(i, t)

int binarysearch(int arr[], int n, int mid) {

  int ans = 0;

  rep(i, n) {

    ans += arr[i] / mid;
    if (arr[i] % mid == 0)
      ans--;
  }

  return ans;
}

void solve() {

  int n, k;
  cin >> n >> k;
  int arr[n];
  int front = 0, end = 0;
  rep(i, n) {

    cin >> arr[i];
    end = max(end, arr[i]);
  }

  while (front < end) {

    int mid = front + (end - front) / 2;

    if (mid == 0)
      break;

    int temp = binarysearch(arr, n, mid);

    if (temp <= k)
      end = mid;
    else
      front = mid + 1;
  }

  cout << end << "\n";
}

int main() {
  fastio;
  // T
  solve();
  // code
  return 0;
}