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

/* [Template].cpp
Example code to use when starting new code. This particular problem was problem
977A_Wrong_Subtraction from CodeForces

Compile: g++  -o E.exe E.cpp
Execute: ./E
*/

/* Solution sketch
E: Use (integer) binary search on the answer. Suppose the answer is X
(0≤X≤10^9). Then go through all logs Ai and count how many splits Ki you need to
get the length of each of them under X, using the equation Ai/(Ki+1)=X. There's
some funky arithmetic with the rounding, which I'm unsure of, but it works.
Code.

Code: https://atcoder.jp/contests/abc174/submissions/15619338
*/

#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <queue>
#include <string>
using namespace std;

// Global variables for convenience
int n, k, a[200000];

int find_k(int x) {
  int result = 0;
  int k0;
  for (int i = 0; i < n; i++) {
    // Compute using integer arithmetic: ceil(Ai/X) - 1
    if (a[i] % x == 0) {
      k0 = a[i] / x - 1;
    } else {
      k0 = a[i] / x;
    }
    result += k0;
  }

  return result;
}

/* Integer binary search for this problem.
        Precondition: f(a) > target & f(b) <= target.

*/
int binary_search(int a, int b, int target) {
  int m = a + (b - a) / 2;

  if (m == 0)
    return 1;

  int fm = find_k(m);

  // Base case: end of search
  if (b == a || b == a + 1)
    return b;

  // Recursive cases
  if (fm > target)
    return binary_search(m, b, target);
  else
    return binary_search(a, m, target);
}

int main() {
  // Input
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  // Binary search
  int a = 0, b = 1000000001;

  cout << binary_search(a, b, k) << endl;

  return 0;
}


Error: 0

Fixed submission:

/* [Template].cpp
Example code to use when starting new code. This particular problem was problem
977A_Wrong_Subtraction from CodeForces

Compile: g++  -o E.exe E.cpp
Execute: ./E
*/

/* Solution sketch
E: Use (integer) binary search on the answer. Suppose the answer is X
(0≤X≤10^9). Then go through all logs Ai and count how many splits Ki you need to
get the length of each of them under X, using the equation Ai/(Ki+1)=X. There's
some funky arithmetic with the rounding, which I'm unsure of, but it works.
Code.

Code: https://atcoder.jp/contests/abc174/submissions/15619338
*/

#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <queue>
#include <string>
using namespace std;

// Global variables for convenience
int n, k, a[200000];

int find_k(int x) {
  int result = 0;
  int k0;
  for (int i = 0; i < n; i++) {
    // Compute using integer arithmetic: ceil(Ai/X) - 1
    if (a[i] % x == 0) {
      k0 = a[i] / x - 1;
    } else {
      k0 = a[i] / x;
    }
    result += k0;
  }

  return result;
}

/* Integer binary search for this problem.
        Precondition: f(a) > target & f(b) <= target.

*/
int binary_search(int a, int b, int target) {
  int m = a + (b - a) / 2, fm = find_k(m);

  // Base case: end of search
  if (b == a || b == a + 1)
    return b;

  // Recursive cases
  if (fm > target)
    return binary_search(m, b, target);
  else
    return binary_search(a, m, target);
}

int main() {
  // Input
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  // Binary search
  int a = 0, b = 1000000001;

  cout << binary_search(a, b, k) << endl;

  return 0;
}