Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef tree<int,null_type,less<int
// >,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, set<T> &v);
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v);
#ifdef APURBA
#define debug(a) cout << #a << " --> " << (a) << "\n";
#define debug2(a) cout << #a << " --> " << (a) << " ";
#define HERE cout << "here - " << __LINE__ << "\n";
#else
#define debug(a)
#define debug2(a)
#define HERE
#endif
const ll mod = 1e9 + 7;
const int N = 5e5 + 5;
typedef pair<int, int> pii;
ll n, k;
vector<ll> a;

bool check(ll val) {
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    ll now = a[i];
    if (now <= val)
      continue;
    ll tmp = now / val;
    if (now % val) {
      tmp++;
    }
    cnt += tmp - 1;
  }
  return cnt <= k;
}

void TEST_CASES(int cas) {
  scanf("%lld %lld", &n, &k);
  a.resize(n);
  for (ll &i : a) {
    scanf("%lld", &i);
  }
  ll ans = -1;
  ll low = 0, high = *max_element(a.begin(), a.end());
  while (low <= high) {
    ll mid = (low + high) / 2;
    if (check(mid)) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  printf("%lld\n", ans);
}

/*

*/

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int t = 1, cas = 0;
  // scanf("%d",&t);
  while (t--) {
    TEST_CASES(++cas);
  }
  return 0;
}

template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << "{" << p.first << ", " << p.second << "} ";
  return os;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << "[ ";
  for (int i = 0; i < v.size(); i++) {
    os << v[i] << " ";
  }
  os << " ]";
  return os;
}

template <class T> ostream &operator<<(ostream &os, set<T> &v) {
  os << "[ ";
  for (T i : v) {
    os << i << " ";
  }
  os << " ]";
  return os;
}
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v) {
  for (auto i : v) {
    os << "Key : " << i.first << " , Value : " << i.second << endl;
  }
  return os;
}


Fixed submission:
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef tree<int,null_type,less<int
// >,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, set<T> &v);
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v);
#ifdef APURBA
#define debug(a) cout << #a << " --> " << (a) << "\n";
#define debug2(a) cout << #a << " --> " << (a) << " ";
#define HERE cout << "here - " << __LINE__ << "\n";
#else
#define debug(a)
#define debug2(a)
#define HERE
#endif
const ll mod = 1e9 + 7;
const int N = 5e5 + 5;
typedef pair<int, int> pii;
ll n, k;
vector<ll> a;

bool check(ll val) {
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    ll now = a[i];
    if (now <= val)
      continue;
    ll tmp = now / val;
    if (now % val) {
      tmp++;
    }
    cnt += tmp - 1;
  }
  return cnt <= k;
}

void TEST_CASES(int cas) {
  scanf("%lld %lld", &n, &k);
  a.resize(n);
  for (ll &i : a) {
    scanf("%lld", &i);
  }
  ll ans = -1;
  ll low = 1, high = *max_element(a.begin(), a.end());
  while (low <= high) {
    ll mid = (low + high) / 2;
    if (check(mid)) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  printf("%lld\n", ans);
}

/*

*/

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int t = 1, cas = 0;
  // scanf("%d",&t);
  while (t--) {
    TEST_CASES(++cas);
  }
  return 0;
}

template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << "{" << p.first << ", " << p.second << "} ";
  return os;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << "[ ";
  for (int i = 0; i < v.size(); i++) {
    os << v[i] << " ";
  }
  os << " ]";
  return os;
}

template <class T> ostream &operator<<(ostream &os, set<T> &v) {
  os << "[ ";
  for (T i : v) {
    os << i << " ";
  }
  os << " ]";
  return os;
}
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v) {
  for (auto i : v) {
    os << "Key : " << i.first << " , Value : " << i.second << endl;
  }
  return os;
}