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

#include "algorithm"
#include "bitset"
#include "chrono"
#include "cstring"
#include "ctime"
#include "iomanip"
#include "iostream"
#include "map"
#include "math.h"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "vector"

#include "ext/pb_ds/assoc_container.hpp" // Common file
#include "ext/pb_ds/detail/standard_policies.hpp"
#include "ext/pb_ds/tree_policy.hpp" // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

#define f first
#define lgn 25
#define endl '\\n'
#define sc second
#define pb push_back
#define N (int)5e5 + 5
#define PI acos(-1.0)
#define int long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define eb emplace_back
#define mii map<int, int>
#define vpii vector<pii>
#define pii pair<int, int>
#define pq priority_queue
#define BLOCK (int)sqrt(N)
#define test(x) while (x--)
#define all(x) begin(x), end(x)
#define allr(x) x.rbegin(), x.rend()
#define fo(i, a, n) for (int i = a; i < n; i++)
#define rfo(i, n, a) for (int i = n; i >= a; i--)
#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define time()                                                                 \\
  cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\\n"
#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    OS;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;

int n, m, k, q;
string s;
vi adj[N];
int vis[N], par[N], a[N];

bool check(int mid) {
  int cn = 0;
  fo(i, 0, n) {
    if (a[i] < mid)
      continue;
    cn += ((a[i] + mid - 1) / mid) - 1;
  }

  return (cn <= k);
}

void go() {
  cin >> n >> k;
  int mx = 0;
  fo(i, 0, n) cin >> a[i], mx = max(mx, a[i]);

  int l = 1, r = 1e11;
  int ans = mx;

  while (l <= r) {
    int mid = l + (r - l) / 2;

    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }

  cout << ans << endl;
}

int32_t main() {
  FAST;
  int t = 1;
  // cin>>t;
  test(t) go();
}


Error: 0

Fixed submission:

#include "algorithm"
#include "bitset"
#include "chrono"
#include "cstring"
#include "ctime"
#include "iomanip"
#include "iostream"
#include "map"
#include "math.h"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "vector"

#include "ext/pb_ds/assoc_container.hpp" // Common file
#include "ext/pb_ds/detail/standard_policies.hpp"
#include "ext/pb_ds/tree_policy.hpp" // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

#define f first
#define lgn 25
#define endl '\\n'
#define sc second
#define pb push_back
#define N (int)5e5 + 5
#define PI acos(-1.0)
#define int long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define eb emplace_back
#define mii map<int, int>
#define vpii vector<pii>
#define pii pair<int, int>
#define pq priority_queue
#define BLOCK (int)sqrt(N)
#define test(x) while (x--)
#define all(x) begin(x), end(x)
#define allr(x) x.rbegin(), x.rend()
#define fo(i, a, n) for (int i = a; i < n; i++)
#define rfo(i, n, a) for (int i = n; i >= a; i--)
#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define time()                                                                 \\
  cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\\n"
#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    OS;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;

int n, m, k, q;
string s;
vi adj[N];
int vis[N], par[N], a[N];

bool check(int mid) {
  int cn = 0;
  fo(i, 0, n) {
    if (a[i] < mid)
      continue;
    cn += ((a[i] + mid - 1) / mid) - 1;
  }

  return (cn <= k);
}

void go() {
  cin >> n >> k;
  int mx = 0;
  fo(i, 0, n) cin >> a[i], mx = max(mx, a[i]);

  int l = 0, r = 1e11;
  int ans = mx;

  while (l <= r) {
    int mid = l + (r - l) / 2;

    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }

  cout << ans << endl;
}

int32_t main() {
  FAST;
  int t = 1;
  // cin>>t;
  test(t) go();
}