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

#include <algorithm>
#include <bits/stdc++.h>
#include <chrono>
#include <map>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include <vector>

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repu(i, k, n) for (ll i = k; i <= n; i++)
#define repd(i, k, n) for (ll i = k; i >= n; i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define endl "\\n"
#define INF (ll) ll_MAX

typedef long long ll;
typedef long double ld;
typedef std::pair<ll, ll> pii;
typedef std::vector<ll> vi;
typedef std::vector<pii> vii;
typedef std::vector<ll> vll;
typedef std::vector<std::pair<pii, ll>> vpp;

const ll N = 3 * 1e5 + 5;
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
const long long inf = 1e18;

using namespace std;

// ll g[200000], ig[200000];

bool ok;

void dfs1(ll u, bool vis[], ll tot[], ll p[], ll h[], vll v[]) {
  if (vis[u] == 1)
    return;
  vis[u] = 1;

  ll sum = 0;

  sum += p[u];

  ll sorr = 0;

  for (ll i = 1; i < v[u].size(); i++) {
    // cout<<'a';
    dfs1(v[u][i], vis, tot, p, h, v);
    sum += tot[v[u][i]];
    sorr = sorr + tot[v[u][i]] - h[v[u][i]];
  }

  // cout<<sum;

  tot[u] = sum;

  // if(tot[u]-h[u]>sorr){
  //     cout<<tot[u]-h[u]<<' '<<sorr<<'\\n';
  //     ok = false;
  //     // cout<<"NOOOOO";
  // }

  return;
}

void dfs2(ll u, bool vis[], ll tot[], ll p[], ll h[], vll v[]) {
  if (vis[u] == 1)
    return;
  vis[u] = 1;

  // ll sum = 0;

  // sum += p[u];

  ll sorr = 0;

  for (ll i = 1; i < v[u].size(); i++) {
    // cout<<'a';
    dfs2(v[u][i], vis, tot, p, h, v);
    // sum+= tot[v[u][i]];
    sorr = sorr + tot[v[u][i]] - h[v[u][i]];
  }

  // cout<<sum;

  // tot[u] = sum;

  // cout<<sorr<<endl;

  if (tot[u] - h[u] > sorr + 2 * p[u]) {
    // cout<<u<<' '<<tot[u]-h[u]<<' '<<sorr<<'\\n';
    ok = false;
    // cout<<"NOOOOO";
  }

  if ((tot[u] - h[u]) % 2 == 1) {
    ok = false;
  }

  if (h[u] > (tot[u]) or h[u] < (-tot[u])) {
    ok = false;
  }

  return;
}

void solve() {

  ll n, k;
  cin >> n >> k;

  ll a[n];
  ll mx = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mx = max(mx, a[i]);
  }

  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    sum += a[i];
  }

  ll l = 1;
  ll h = mx;

  while (l < h) {
    ll mid = (l + h) / 2;

    ll sum2 = 0;

    for (ll i = 0; i < n; i++) {
      sum2 += (a[i] - 1) / mid;
    }

    if (sum2 > k) {
      l = mid + 1;
    } else if (sum2 == k) {
      h = mid;
    } else {
      h = mid;
    }
  }

  cout << h;

  // ll mn = ceil(sum/(n+k+(ld)0.0));

  // ll sum2 = 0;
  // for(ll i=0;i<n;i++){
  //     sum2+= a[i]/mn;
  // }
  // cout<<mn;
}

int main() {

  // #ifndef ONLINE_JUDGE
  //     // for getting input from input.txt
  //     freopen("input.txt", "r", stdin);
  //     // for writing output to output.txt
  //     freopen("output.txt", "w", stdout);
  // #endif

  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;
  ll ctr = 0;
  while (t--) {
    ctr++;
    // cout<<"Case #"<<ctr<<": ";
    solve();
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <chrono>
#include <map>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include <vector>

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repu(i, k, n) for (ll i = k; i <= n; i++)
#define repd(i, k, n) for (ll i = k; i >= n; i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define endl "\\n"
#define INF (ll) ll_MAX

typedef long long ll;
typedef long double ld;
typedef std::pair<ll, ll> pii;
typedef std::vector<ll> vi;
typedef std::vector<pii> vii;
typedef std::vector<ll> vll;
typedef std::vector<std::pair<pii, ll>> vpp;

const ll N = 3 * 1e5 + 5;
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
const long long inf = 1e18;

using namespace std;

// ll g[200000], ig[200000];

bool ok;

void dfs1(ll u, bool vis[], ll tot[], ll p[], ll h[], vll v[]) {
  if (vis[u] == 1)
    return;
  vis[u] = 1;

  ll sum = 0;

  sum += p[u];

  ll sorr = 0;

  for (ll i = 1; i < v[u].size(); i++) {
    // cout<<'a';
    dfs1(v[u][i], vis, tot, p, h, v);
    sum += tot[v[u][i]];
    sorr = sorr + tot[v[u][i]] - h[v[u][i]];
  }

  // cout<<sum;

  tot[u] = sum;

  // if(tot[u]-h[u]>sorr){
  //     cout<<tot[u]-h[u]<<' '<<sorr<<'\\n';
  //     ok = false;
  //     // cout<<"NOOOOO";
  // }

  return;
}

void dfs2(ll u, bool vis[], ll tot[], ll p[], ll h[], vll v[]) {
  if (vis[u] == 1)
    return;
  vis[u] = 1;

  // ll sum = 0;

  // sum += p[u];

  ll sorr = 0;

  for (ll i = 1; i < v[u].size(); i++) {
    // cout<<'a';
    dfs2(v[u][i], vis, tot, p, h, v);
    // sum+= tot[v[u][i]];
    sorr = sorr + tot[v[u][i]] - h[v[u][i]];
  }

  // cout<<sum;

  // tot[u] = sum;

  // cout<<sorr<<endl;

  if (tot[u] - h[u] > sorr + 2 * p[u]) {
    // cout<<u<<' '<<tot[u]-h[u]<<' '<<sorr<<'\\n';
    ok = false;
    // cout<<"NOOOOO";
  }

  if ((tot[u] - h[u]) % 2 == 1) {
    ok = false;
  }

  if (h[u] > (tot[u]) or h[u] < (-tot[u])) {
    ok = false;
  }

  return;
}

void solve() {

  ll n, k;
  cin >> n >> k;

  ll a[n];
  ll mx = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mx = max(mx, a[i]);
  }

  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    sum += a[i];
  }

  ll l = 0;
  ll h = mx;

  while (l < h) {
    ll mid = (l + h) / 2;

    ll sum2 = 0;

    for (ll i = 0; i < n; i++) {
      sum2 += a[i] / mid;
    }

    if (sum2 > k) {
      l = mid + 1;
    } else if (sum2 == k) {
      h = mid;
    } else {
      h = mid;
    }
  }

  cout << h;

  // ll mn = ceil(sum/(n+k+(ld)0.0));

  // ll sum2 = 0;
  // for(ll i=0;i<n;i++){
  //     sum2+= a[i]/mn;
  // }
  // cout<<mn;
}

int main() {

  // #ifndef ONLINE_JUDGE
  //     // for getting input from input.txt
  //     freopen("input.txt", "r", stdin);
  //     // for writing output to output.txt
  //     freopen("output.txt", "w", stdout);
  // #endif

  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;
  ll ctr = 0;
  while (t--) {
    ctr++;
    // cout<<"Case #"<<ctr<<": ";
    solve();
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}