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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define DEBUG(x) cout << #x << "=" << x << endl
#define DEBUG2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
typedef long long ll;
// #define LOCAL
const int MAXN = 2e5 + 10;
const ll MOD = 17;
ll _gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return _gcd(b, a % b);
}
ll gcd(ll a, ll b) {
  a = abs(a), b = abs(b);
  if (a < b)
    swap(a, b);
  return _gcd(a, b);
}
ll qpow(ll a, ll n) {
  ll rt = 1;
  while (n) {
    if (n & 1)
      rt = (rt * a) % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return rt;
}
ll factor[MAXN];
void cal_factor() {
  factor[0] = 1;
  for (int u = 1; u < MAXN; u++) {
    factor[u] = (factor[u - 1] * u) % MOD;
  }
}
ll C(ll n, ll k) {
  return factor[n] * qpow(factor[n - k], MOD - 2) % MOD *
         qpow(factor[k], MOD - 2) % MOD;
}
int updiv(int a, int b) {
  if (a % b == 0)
    return a / b;
  return a / b + 1;
}
int N, K;
int A[MAXN];
bool check(int x) {
  int cnt = 0;
  for (int u = 0; u < N; u++) {
    int t = A[u];
    if (t > x)
      cnt += t % x == 0 ? t / x - 1 : t / x;
  }
  return cnt <= K;
}
void solve() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> K;
  for (int u = 0; u < N; u++)
    cin >> A[u];
  int l = 1, r = 1e9, lst = -1;
  while (l <= r) {
    int mid = l + (r - l) / 2;
    if (check(mid)) {
      r = mid - 1;
      lst = mid;
    } else {
      l = mid + 1;
    }
  }
  assert(lst != -1);
  cout << lst;
}
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif
  solve();
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define DEBUG(x) cout << #x << "=" << x << endl
#define DEBUG2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
typedef long long ll;
// #define LOCAL
const int MAXN = 2e5 + 10;
const ll MOD = 17;
ll _gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return _gcd(b, a % b);
}
ll gcd(ll a, ll b) {
  a = abs(a), b = abs(b);
  if (a < b)
    swap(a, b);
  return _gcd(a, b);
}
ll qpow(ll a, ll n) {
  ll rt = 1;
  while (n) {
    if (n & 1)
      rt = (rt * a) % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return rt;
}
ll factor[MAXN];
void cal_factor() {
  factor[0] = 1;
  for (int u = 1; u < MAXN; u++) {
    factor[u] = (factor[u - 1] * u) % MOD;
  }
}
ll C(ll n, ll k) {
  return factor[n] * qpow(factor[n - k], MOD - 2) % MOD *
         qpow(factor[k], MOD - 2) % MOD;
}
int updiv(int a, int b) {
  if (a % b == 0)
    return a / b;
  return a / b + 1;
}
int N, K;
int A[MAXN];
bool check(int x) {
  int cnt = 0;
  for (int u = 0; u < N; u++) {
    int t = A[u];
    if (t > x)
      cnt += t % x == 0 ? t / x - 1 : t / x;
  }
  return cnt <= K;
}
void solve() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> K;
  for (int u = 0; u < N; u++)
    cin >> A[u];
  int l = 0, r = 1e9, lst = -1;
  while (l <= r) {
    int mid = l + (r - l) / 2;
    if (check(mid)) {
      r = mid - 1;
      lst = mid;
    } else {
      l = mid + 1;
    }
  }
  if (lst == -1)
    lst = 0;
  cout << lst;
}
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif
  solve();
}