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

/*in the name of Allah the most beneficent the most merciful.*/
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define MAX 1e18
#define MIN -1e18
#define MOD 1000000007
// #define mod2 20071027
// #define MOD 998244353
#define base 139
// #define mod 1000074259
#define base2 31
#define memz(a) memset(a, 0, sizeof(a))
#define memn(a) memset(a, -1, sizeof(a))
#define in1(a) scanf("%lld", &a)
#define in2(a, b) scanf("%lld%lld", &a, &b)
#define TC(c) printf("Case #%lld: ", ++c)
#define out(x) cout << #x << " -> " << x << endl;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define FILE                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("out.txt", "w", stdout);
using namespace std;
const int B = 16, N = 300010, M = 5000010;
ll n, k, a[N];
bool fun(ll val) {
  ll K = k;
  for (int i = 0; i < n; i++) {
    ll d = a[i] / val;
    if (a[i] % val)
      d++;
    d--;
    if (d > K)
      return 0;
    K -= d;
  }
  return 1;
}
int solve() {
  in2(n, k);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    in1(a[i]);
    ans = max(ans, a[i]);
  }
  ll high = MAX, low = 1, mid;
  while (high >= low) {
    mid = (high + low) / 2;
    if (fun(mid)) {
      ans = mid;
      high = mid - 1;
    } else
      low = mid + 1;
  }
  printf("%lld\\n", ans);
  return 0;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  return solve();
}
/*

*/

Error: WA

Fixed submission:

/*in the name of Allah the most beneficent the most merciful.*/
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define MAX 1e18
#define MIN -1e18
#define MOD 1000000007
// #define mod2 20071027
// #define MOD 998244353
#define base 139
// #define mod 1000074259
#define base2 31
#define memz(a) memset(a, 0, sizeof(a))
#define memn(a) memset(a, -1, sizeof(a))
#define in1(a) scanf("%lld", &a)
#define in2(a, b) scanf("%lld%lld", &a, &b)
#define TC(c) printf("Case #%lld: ", ++c)
#define out(x) cout << #x << " -> " << x << endl;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define FILE                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("out.txt", "w", stdout);
using namespace std;
const int B = 16, N = 300010, M = 5000010;
ll n, k, a[N];
bool fun(ll val) {
  ll K = k;
  for (int i = 0; i < n; i++) {
    ll d = a[i] / val;
    if (a[i] % val)
      d++;
    d--;
    if (d > K)
      return 0;
    K -= d;
  }
  return 1;
}
int solve() {
  in2(n, k);
  for (int i = 0; i < n; i++)
    in1(a[i]);
  ll high = MAX, low = 0, mid, ans = -1;
  while (high >= low) {
    mid = (high + low) / 2;
    if (fun(mid)) {
      ans = mid;
      high = mid - 1;
    } else
      low = mid + 1;
  }
  printf("%lld\\n", ans);
  return 0;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  return solve();
}
/*

*/