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

/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <cstdint>   // int64_t, int*_t
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <limits>   //setprecision
#include <string>   // string, to_string, stoi
#include <tuple>    // tuple, make_tuple
#include <utility>  // pair, make_pair
#include <vector>   // vector
// #include <cstdio> // printf
#include <map>   // map
#include <queue> // queue, priority_queue
#include <set>   // set
// #include <unordered_set> //unordered_set
#include <bitset> // bitset
#include <cmath>  //abs,,,
#include <deque>  // deque
#include <math.h> //pow,,,
#include <stack>  // stack
#define endl "\\n";
using namespace std;

const long long INF = 100100100100100;
const int MOD = 1000000007;
const int inf = 1001001001;
typedef pair<int, int> P;

// Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M) {
  if (M == 0)
    return 1;
  long long res = mod_pow((N * N) % MOD, M / 2);
  // 最下位ビット(*N)が1の時は単独でNをかける
  if (M & 1)
    res = (res * N) % MOD;

  return res %= MOD;
}

long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

bool ok(long long index, vector<long long> &retu, int border) {
  int cutCount = 0;
  for (int i = 0; i < retu.size(); i++) {
    cutCount += (retu.at(i) - 1) / index;
  }
  if (cutCount <= border)
    return true;
  else
    return false;
}
long long binary_search(long long size, vector<long long> &retu, int border) {
  // left,right,midはいずれもindex
  long long left = 0, right = size;
  while (right - left > 1) {
    long long mid = left + (right - left) / 2;
    bool hantei = ok(mid, retu, border);
    if (hantei)
      right = mid;
    else
      left = mid;
    // cout << left << " " << right << endl;
  }
  if (left == -1)
    return 0;
  return right;
}

// leftは条件を満たさない最大の値、rightは条件を満たす最小の値になっている

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int N;
  cin >> N;
  int K;
  cin >> K;
  vector<long long> retu(N);
  long long maxi = 0;
  for (int i = 0; i < N; i++)
    cin >> retu.at(i), maxi = max(maxi, retu.at(i));
  long long ans = binary_search(1000000000, retu, K);
  cout << ans << endl;
}

Error: 0

Fixed submission:

/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <cstdint>   // int64_t, int*_t
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <limits>   //setprecision
#include <string>   // string, to_string, stoi
#include <tuple>    // tuple, make_tuple
#include <utility>  // pair, make_pair
#include <vector>   // vector
// #include <cstdio> // printf
#include <map>   // map
#include <queue> // queue, priority_queue
#include <set>   // set
// #include <unordered_set> //unordered_set
#include <bitset> // bitset
#include <cmath>  //abs,,,
#include <deque>  // deque
#include <math.h> //pow,,,
#include <stack>  // stack
#define endl "\\n";
using namespace std;

const long long INF = 100100100100100;
const int MOD = 1000000007;
const int inf = 1001001001;
typedef pair<int, int> P;

// Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M) {
  if (M == 0)
    return 1;
  long long res = mod_pow((N * N) % MOD, M / 2);
  // 最下位ビット(*N)が1の時は単独でNをかける
  if (M & 1)
    res = (res * N) % MOD;

  return res %= MOD;
}

long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

bool ok(long long index, vector<long long> &retu, int border) {
  int cutCount = 0;
  for (int i = 0; i < retu.size(); i++) {
    cutCount += (retu.at(i) - 1) / index;
  }
  if (cutCount <= border)
    return true;
  else
    return false;
}
long long binary_search(long long size, vector<long long> &retu, int border) {
  // left,right,midはいずれもindex
  long long left = -1, right = size;
  while (right - left > 1) {
    long long mid = (left + right) / 2;
    bool hantei = ok(mid, retu, border);
    if (hantei)
      right = mid;
    else
      left = mid;
    // cout << left << " " << right << endl;
  }
  if (left == -1)
    return 0;
  return right;
}

// leftは条件を満たさない最大の値、rightは条件を満たす最小の値になっている

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int N;
  cin >> N;
  int K;
  cin >> K;
  vector<long long> retu(N);
  long long maxi = 0;
  for (int i = 0; i < N; i++)
    cin >> retu.at(i), maxi = max(maxi, retu.at(i));
  long long ans = binary_search(1000000000, retu, K);
  cout << ans << endl;
}