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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool dfs(int st, int par, vector<vector<int>> &adj, vector<int> &p,
         vector<int> &h, vector<int> &g, vector<int> &b) {
  bool ans = true;
  int flag = 0;
  int x, y;
  for (int j = 0; j < adj[st].size(); j++) {
    if (adj[st][j] != par) {
      // cout<<adj[st][j]<<" "<<par<<" "<<st<<endl;
      flag = 1;
      ans = ans & dfs(adj[st][j], st, adj, p, h, g, b);
      g[st] += g[adj[st][j]];
      b[st] += b[adj[st][j]];
      p[st] += p[adj[st][j]];
    }
  }
  if (flag == 0) {
    if (abs(h[st]) > p[st]) {
      // cout<<st<<endl;
      return false;
    } else {
      g[st] = (p[st] + h[st]) / 2;
      b[st] = (p[st] - h[st]) / 2;
      // cout<<st<<" "<<p[st]<<" "<<h[st]<<" "<<g[st]<<" "<<b[st]<<endl;
      if (g[st] + b[st] == p[st])
        return true;
      else
        return false;
    }
  } else {
    int x = (p[st] + h[st]) / 2;
    int y = (p[st] - h[st]) / 2;
    // cout<<st<<" "<<p[st]<<" "<<h[st]<<" "<<g[st]<<" "<<b[st]<<" "<<ans<<endl;
    if (x < 0 || y < 0 || (x + y) != p[st] || (x - y) != h[st] ||
        abs(h[st]) > p[st] || x < g[st])
      return ans & false;
    else {
      g[st] = x;
      b[st] = y;
      return ans & true;
    }
  }
}

int main() {
  int n, k;
  cin >> n >> k;
  int arr[n];
  int ans = 0;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    ans = max(ans, arr[i]);
    sum += (long long)(2 * arr[i] - 1);
  }
  int l = 0, h = ans;
  // cout<<ans<<endl;
  while (l + 1 < h) {
    int m = (l + h) / 2;
    if (m == 0)
      m = 1;
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (arr[i] % m == 0)
        cnt += (arr[i] / m) - 1;
      else
        cnt += arr[i] / m;
    }
    if (cnt <= k) {
      h = m;
    } else {
      l = m;
    }
    // cout<<ans<<" "<<l<<" "<<h<<endl;
  }
  cout << h << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool dfs(int st, int par, vector<vector<int>> &adj, vector<int> &p,
         vector<int> &h, vector<int> &g, vector<int> &b) {
  bool ans = true;
  int flag = 0;
  int x, y;
  for (int j = 0; j < adj[st].size(); j++) {
    if (adj[st][j] != par) {
      // cout<<adj[st][j]<<" "<<par<<" "<<st<<endl;
      flag = 1;
      ans = ans & dfs(adj[st][j], st, adj, p, h, g, b);
      g[st] += g[adj[st][j]];
      b[st] += b[adj[st][j]];
      p[st] += p[adj[st][j]];
    }
  }
  if (flag == 0) {
    if (abs(h[st]) > p[st]) {
      // cout<<st<<endl;
      return false;
    } else {
      g[st] = (p[st] + h[st]) / 2;
      b[st] = (p[st] - h[st]) / 2;
      // cout<<st<<" "<<p[st]<<" "<<h[st]<<" "<<g[st]<<" "<<b[st]<<endl;
      if (g[st] + b[st] == p[st])
        return true;
      else
        return false;
    }
  } else {
    int x = (p[st] + h[st]) / 2;
    int y = (p[st] - h[st]) / 2;
    // cout<<st<<" "<<p[st]<<" "<<h[st]<<" "<<g[st]<<" "<<b[st]<<" "<<ans<<endl;
    if (x < 0 || y < 0 || (x + y) != p[st] || (x - y) != h[st] ||
        abs(h[st]) > p[st] || x < g[st])
      return ans & false;
    else {
      g[st] = x;
      b[st] = y;
      return ans & true;
    }
  }
}

int main() {
  int n, k;
  cin >> n >> k;
  int arr[n];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    ans = max(ans, arr[i]);
  }
  int l = 0, h = ans;
  // cout<<ans<<endl;
  while (l <= h) {
    int m = (l + h) / 2;
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (arr[i] % m == 0)
        cnt += (arr[i] / m) - 1;
      else
        cnt += arr[i] / m;
    }
    if (cnt <= k) {
      h = m - 1;
      ans = min(ans, m);
    } else {
      l = m + 1;
    }
    // cout<<ans<<" "<<l<<" "<<h<<endl;
  }
  cout << ans << endl;
}