Problem p02598 (C++)

Buggy submission (status: Runtime Error):
// 問題の URL を書いておく
//

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;

// #define ENABLE_PRINT

#if defined(ENABLE_PRINT)

#define Print(v)                                                               \
  do {                                                                         \
    cout << #v << ": " << v << endl;                                           \
  } while (0)

#define PrintVec(v)                                                            \
  do {                                                                         \
    for (int __i = 0; __i < v.size(); ++__i) {                                 \
      cout << #v << "[" << __i << "]: " << v[__i] << endl;                     \
    }                                                                          \
  } while (0)

#else

#define Print(v) ((void)0)
#define PrintVec(v) ((void)0)

#endif

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using ll = int64_t;

bool isValid(int x, const vector<int> &a, int k) {
  ll count = 0;
  for (auto av : a) {
    auto m = av % x;
    count += (m == 0 ? av / x - 1 : av / x);
    if (count > k) {
      return false;
    }
  }
  return true;
}

int main(int, const char **) {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  int maxLen = 0;
  rep(i, n) {
    cin >> a[i];
    maxLen = max(maxLen, a[i]);
  }

  ll l = 0, h = maxLen;
  while (h != l) {
    auto t = (l + h) / 2;
    if (isValid(t, a, k)) {
      h = t;
    } else {
      if (l == t) {
        l = h;
        break;
      }
      l = t;
    }
  }
  cout << l << endl;
  return 0;
}


Fixed submission:
// 問題の URL を書いておく
//

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;

// #define ENABLE_PRINT

#if defined(ENABLE_PRINT)

#define Print(v)                                                               \
  do {                                                                         \
    cout << #v << ": " << v << endl;                                           \
  } while (0)

#define PrintVec(v)                                                            \
  do {                                                                         \
    for (int __i = 0; __i < v.size(); ++__i) {                                 \
      cout << #v << "[" << __i << "]: " << v[__i] << endl;                     \
    }                                                                          \
  } while (0)

#else

#define Print(v) ((void)0)
#define PrintVec(v) ((void)0)

#endif

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using ll = int64_t;

bool isValid(int x, const vector<int> &a, int k) {
  ll count = 0;
  if (x == 0) {
    return false;
  }
  for (auto av : a) {
    auto m = av % x;
    count += (m == 0 ? av / x - 1 : av / x);
    if (count > k) {
      return false;
    }
  }
  return true;
}

int main(int, const char **) {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  int maxLen = 0;
  rep(i, n) {
    cin >> a[i];
    maxLen = max(maxLen, a[i]);
  }

  ll l = 0, h = maxLen;
  while (h != l) {
    auto t = (l + h) / 2;
    if (isValid(t, a, k)) {
      h = t;
    } else {
      if (l == t) {
        l = h;
        break;
      }
      l = t;
    }
  }
  cout << l << endl;
  return 0;
}