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

#include <bits/stdc++.h>
#define fastIO                                                                 \\
  std::ios_base::sync_with_stdio(false);                                       \\
  std::cin.tie(0);                                                             \\
  std::cout.tie(0)
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define Local
#ifdef Local
#define dbg(args...)                                                           \\
  do {                                                                         \\
    cout << #args << " -> ";                                                   \\
    err(args);                                                                 \\
  } while (0)
void err() { std::cout << std::endl; }
template <typename T, typename... Args> void err(T a, Args... args) {
  std::cout << a << ' ';
  err(args...);
}
template <template <typename...> class T, typename t, typename... A>
void err(const T<t> &arg, const A &...args) {
  for (auto &v : arg)
    std::cout << v << ' ';
  err(args...);
}
#else
#define dbg(...)
#endif

using namespace std;
typedef vector<int> VI;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const ll mod = 1000000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

const int maxn = 2e5 + 10;

int n;
ll k;
int a[maxn];

bool check(int x) {
  ll res = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] <= x)
      continue;
    /*
    ll cnt = 1;
    double y = a[i];
    while((int)y > x)
    {
            y = y / 2.0;
            res += cnt;
            cnt *= 2ll;
    }
    */
    res += a[i] / x;
  }
  // cout << x << " " << res << endl;
  return res <= k;
}

int main() {
  fastIO;
  cin >> n >> k;

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

  int l = 0, r = 1e9 + 1;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (mid != 0 && check(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fastIO                                                                 \\
  std::ios_base::sync_with_stdio(false);                                       \\
  std::cin.tie(0);                                                             \\
  std::cout.tie(0)
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define Local
#ifdef Local
#define dbg(args...)                                                           \\
  do {                                                                         \\
    cout << #args << " -> ";                                                   \\
    err(args);                                                                 \\
  } while (0)
void err() { std::cout << std::endl; }
template <typename T, typename... Args> void err(T a, Args... args) {
  std::cout << a << ' ';
  err(args...);
}
template <template <typename...> class T, typename t, typename... A>
void err(const T<t> &arg, const A &...args) {
  for (auto &v : arg)
    std::cout << v << ' ';
  err(args...);
}
#else
#define dbg(...)
#endif

using namespace std;
typedef vector<int> VI;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const ll mod = 1000000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

const int maxn = 2e5 + 10;

int n;
ll k;
int a[maxn];

bool check(int x) {
  ll res = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] <= x)
      continue;
    /*
    ll cnt = 1;
    double y = a[i];
    while((int)y > x)
    {
            y = y / 2.0;
            res += cnt;
            cnt *= 2ll;
    }
    */
    res += a[i] / x;
  }
  // cout << x << " " << res << endl;
  return res <= k;
}

int main() {
  fastIO;
  cin >> n >> k;

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

  int l = -1, r = 1e9 + 1;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << endl;
  return 0;
}