prithivMLmods/Coder-Stat huggingface dataset:
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  long K;
  cin >> N >> K;
  vector<long> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  sort(A.begin(), A.end());
  long left = 1;
  long right = 1000000000;
  while (left < right) {
    long X = (right - left) / 2;
    long count = 0;
    for (int i = 0; i < N; i++) {
      count += A.at(i) / X;
      if (A.at(i) % X == 0)
        count--;
    }
    if (count > K)
      left = X + 1;
    else
      right = X;
  }
  cout << right << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  long K;
  cin >> N >> K;
  vector<long> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  sort(A.begin(), A.end());
  long left = 1;
  long right = 1000000000;
  while (left < right) {
    long X = (right + left) / 2;
    long count = 0;
    for (int i = 0; i < N; i++) {
      count += A.at(i) / X;
      if (A.at(i) % X == 0)
        count--;
    }
    if (count > K)
      left = X + 1;
    else
      right = X;
  }
  cout << right << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define db double
using namespace std;
const int M = 2e5 + 10;
int a[M], n, k;
bool check(int x) {
  int cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (a[i] - 1) / x;
  // printf("%d %d\n",x,cnt);
  return cnt <= k;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  sort(a, a + n);
  int l = 0, r = 1e9 + 10, ans = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define db double
using namespace std;
const int M = 2e5 + 10;
int a[M], n, k;
bool check(int x) {
  int cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (a[i] - 1) / x;
  // printf("%d %d\n",x,cnt);
  return cnt <= k;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  sort(a, a + n);
  int l = 1, r = 1e9 + 10, ans = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define P 1000000007
#define in(x, a, b) (a <= x && x < b)

using namespace std;
using ll = long long;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
const ll inf = 1000000001, INF = (ll)1e18 + 1;

int n, k;
vi a;

bool check(int val) {
  int total = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] <= val)
      continue;
    total += a[i] / val;
    if (a[i] % val == 0)
      total--;
  }

  return (total <= k);
}

void solve() {
  cin >> n >> k;
  a.resize(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  ll ans = INF;
  int l = 0, r = 1e9 + 10;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid)) {
      ans = min(ans, (ll)mid);
      r = mid - 1;
    } else
      l = mid + 1;
  }

  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define P 1000000007
#define in(x, a, b) (a <= x && x < b)

using namespace std;
using ll = long long;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
const ll inf = 1000000001, INF = (ll)1e18 + 1;

int n, k;
vi a;

bool check(int val) {
  int total = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] <= val)
      continue;
    total += a[i] / val;
    if (a[i] % val == 0)
      total--;
  }

  return (total <= k);
}

void solve() {
  cin >> n >> k;
  a.resize(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  ll ans = INF;
  int l = 1, r = 1e9 + 10;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid)) {
      ans = min(ans, (ll)mid);
      r = mid - 1;
    } else
      l = mid + 1;
  }

  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mt make_tuple
#define MOD 1000000007
#define fo(i, a, b) for (i = a; i < b; i++)
#define foe(i, a, b) for (i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<long long int>
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<long long int, long long int>>
#define boost                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)
using namespace std;
const int inf = 1e9 + 5;
const ll inf64 = 1e18 + 5;

bool chk(int arr[], int n, int mid, int k) {
  ll res = 0;
  for (int i = 0; i < n; i++)
    res += (arr[i] - 1) / mid;
  return (res <= k);
}
int main() {
  boost;
  int n, k, i;
  cin >> n >> k;
  int arr[n];
  fo(i, 0, n) cin >> arr[i];

  int f = 0, l = inf, mid, ans = inf;
  while (f <= l) {
    mid = (f + l) / 2;
    if (chk(arr, n, mid, k)) {
      ans = min(ans, mid);
      l = mid - 1;
    } else
      f = mid + 1;
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mt make_tuple
#define MOD 1000000007
#define fo(i, a, b) for (i = a; i < b; i++)
#define foe(i, a, b) for (i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<long long int>
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<long long int, long long int>>
#define boost                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)
using namespace std;
const int inf = 1e9 + 5;
const ll inf64 = 1e18 + 5;

bool chk(int arr[], int n, int mid, int k) {
  ll res = 0;
  for (int i = 0; i < n; i++)
    res += (arr[i] - 1) / mid;
  return (res <= k);
}
int main() {
  boost;
  int n, k, i;
  cin >> n >> k;
  int arr[n];
  fo(i, 0, n) cin >> arr[i];

  int f = 1, l = inf, mid, ans = inf;
  while (f <= l) {
    mid = (f + l) / 2;
    if (chk(arr, n, mid, k)) {
      ans = min(ans, mid);
      l = mid - 1;
    } else
      f = mid + 1;
  }
  cout << ans;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

using namespace std;
using ll = long long;

const int mx = 2e5 + 9;

int n;
ll k;
ll a[mx];

ll ceill(ll x, ll y) { return (x + y - 1) / y; }

bool ok(ll x) {
  ll ttl = 0;
  for (int i = 0; i < n; i++) {
    ttl += ceill(a[i], x);
  }

  return (ttl - n <= k);
}

int main() {
  scanf("%d %lld", &n, &k);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);

  ll ans = 0;
  ll lo = 0, hi = 2e9;
  while (lo <= hi) {
    ll mid = (lo + hi) / 2;
    if (ok(mid)) {
      ans = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }

  printf("%lld\n", ans);
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;
using ll = long long;

const int mx = 2e5 + 9;

int n;
ll k;
ll a[mx];

ll ceill(ll x, ll y) { return (x + y - 1) / y; }

bool ok(ll x) {
  ll ttl = 0;
  for (int i = 0; i < n; i++) {
    ttl += ceill(a[i], x);
  }

  return (ttl - n <= k);
}

int main() {
  scanf("%d %lld", &n, &k);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);

  ll ans = *max_element(a, a + n);
  ll lo = 1, hi = 2e9;
  while (lo <= hi) {
    ll mid = (lo + hi) / 2;
    if (ok(mid)) {
      ans = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }

  printf("%lld\n", ans);
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define PI acos(-1)

using namespace std;

constexpr double EPS = 1e-8;
constexpr int MAXN = 3e5 + 5;
constexpr int INF = 0x3f3f3f3f;

array<int, MAXN> a;

int n, k;

inline bool check(int mid) {
  int cnt(0);
  for (int i = 1; i <= n; i++) {
    if (a[i] % mid == 0)
      cnt += a[i] / mid - 1;
    else
      cnt += a[i] / mid;
  }
  return cnt <= k; //
}

signed main() {
  std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int l = 0, r = 2e9, ans = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      ans = mid, r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define PI acos(-1)

using namespace std;

constexpr double EPS = 1e-8;
constexpr int MAXN = 3e5 + 5;
constexpr int INF = 0x3f3f3f3f;

array<int, MAXN> a;

int n, k;

inline bool check(int mid) {
  int cnt(0);
  for (int i = 1; i <= n; i++) {
    if (a[i] % mid == 0)
      cnt += a[i] / mid - 1;
    else
      cnt += a[i] / mid;
  }
  return cnt <= k; //
}

signed main() {
  std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int l = 1, r = 2e9, ans = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      ans = mid, r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<ll, ll>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
ll bpow(ll b, ll p, ll m = 1000000007LL) {
  ll rt = 1;
  for (; p; p >>= 1, b = b * b % m)
    if (p & 1)
      rt = rt * b % m;
  return rt;
}
ll n, k;
ll a[200005];
bool check(ll mi) {
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > mi)
      cnt += a[i] / mi;
  }
  return cnt <= k;
}
int main() {
  IOS;
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll lo = 0, hi = 1000000000;
  while (lo <= hi) {
    ll mi = lo + hi >> 1;
    if (check(mi))
      hi = mi - 1;
    else
      lo = mi + 1;
  }
  cout << lo << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<ll, ll>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
ll bpow(ll b, ll p, ll m = 1000000007LL) {
  ll rt = 1;
  for (; p; p >>= 1, b = b * b % m)
    if (p & 1)
      rt = rt * b % m;
  return rt;
}
ll n, k;
ll a[200005];
bool check(ll mi) {
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > mi)
      cnt += a[i] / mi;
  }
  return cnt <= k;
}
int main() {
  IOS;
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll lo = 1, hi = 1000000000;
  while (lo <= hi) {
    ll mi = lo + hi >> 1;
    if (check(mi))
      hi = mi - 1;
    else
      lo = mi + 1;
  }
  cout << lo << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define trav(a, x) for (auto &a : x)
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;

int n, k;
vi a;

bool test(int m) {
  int cuts = 0;
  rep(i, 0, n) { cuts += (a[i] - 1) / m; }
  return cuts <= k;
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);

  cin >> n >> k;
  a = vi(n);
  rep(i, 0, n) { cin >> a[i]; }

  int lo = -1;
  int hi = 1e9 + 1;
  int mi;
  while (lo + 1 < hi) {
    mi = (lo + hi) / 2;
    if (test(mi)) {
      hi = mi;
    } else {
      lo = mi;
    }
  }
  cout << hi << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define trav(a, x) for (auto &a : x)
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;

int n, k;
vi a;

bool test(int m) {
  int cuts = 0;
  rep(i, 0, n) { cuts += (a[i] - 1) / m; }
  return cuts <= k;
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);

  cin >> n >> k;
  a = vi(n);
  rep(i, 0, n) { cin >> a[i]; }

  int lo = 0;
  int hi = 1e9 + 1;
  int mi;
  while (lo + 1 < hi) {
    mi = (lo + hi) / 2;
    if (test(mi)) {
      hi = mi;
    } else {
      lo = mi;
    }
  }
  cout << hi << endl;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int64_t divceil(int64_t numer, int64_t denom) {
  if (numer % denom == 0) {
    return numer / denom;
  } else {
    return 1L + numer / denom;
  }
}
int main() {
  int64_t N, K;
  cin >> N;
  cin >> K;
  int64_t max_a = -1;

  vector<int64_t> A(N);
  int64_t total_length = 0;
  for (uint64_t i = 0; i < N; i++) {
    cin >> A[i];
    total_length += A[i];
  }

  // 適当な初期値を与えて上界・下界とする
  double upper_bound = -1;
  double lower_bound = 10000000000;
  for (auto a : A) {
    int64_t split_num_heuristic = (K * a) / total_length;
    double val = 1.0 * a / (split_num_heuristic + 1);
    upper_bound = max(upper_bound, val);
    lower_bound = min(lower_bound, val);
  }

  int64_t result = upper_bound + 2;
  int64_t left = lower_bound - 1;
  int64_t right = upper_bound + 1;
  while (right >= left) {
    int64_t mid = (left + right) / 2;

    int64_t total_split = 0;
    for (uint64_t i = 0; i < N; i++) {
      int64_t split_num = divceil(A[i], mid) - 1L;
      total_split += split_num;
    }

    if (total_split <= K) {
      result = min(mid, result);
      right = mid - 1;
    } else {
      left = mid + 1;
    }
  }
  cout << result << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int64_t divceil(int64_t numer, int64_t denom) {
  if (numer % denom == 0) {
    return numer / denom;
  } else {
    return 1L + numer / denom;
  }
}
int main() {
  int64_t N, K;
  cin >> N;
  cin >> K;
  int64_t max_a = -1;

  vector<int64_t> A(N);
  int64_t total_length = 0;
  for (uint64_t i = 0; i < N; i++) {
    cin >> A[i];
    total_length += A[i];
  }

  // 適当な初期値を与えて上界・下界とする
  double upper_bound = -1;
  double lower_bound = 10000000000;
  for (auto a : A) {
    int64_t split_num_heuristic = (K * a) / total_length;
    double val = 1.0 * a / (split_num_heuristic + 1);
    upper_bound = max(upper_bound, val);
    lower_bound = min(lower_bound, val);
  }

  int64_t result = upper_bound + 2;
  int64_t left = max(lower_bound, 1.0);
  int64_t right = upper_bound + 1;
  while (right >= left) {
    int64_t mid = (left + right) / 2;

    int64_t total_split = 0;
    for (uint64_t i = 0; i < N; i++) {
      int64_t split_num = divceil(A[i], mid) - 1L;
      total_split += split_num;
    }

    if (total_split <= K) {
      result = min(mid, result);
      right = mid - 1;
    } else {
      left = mid + 1;
    }
  }
  cout << result << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vint = vector<int>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vchar = vector<char>;
using vvchar = vector<vchar>;
using vp = vector<P>;
using vpp = vector<pair<P, P>>;
using vvp = vector<vp>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
istream &operator>>(istream &is, P &a) { return is >> a.first >> a.second; }
ostream &operator<<(ostream &os, const P &a) {
  return os << "(" << a.first << "," << a.second << ")";
}

template <typename T>

void view(const std::vector<T> &v) {
#ifndef ONLINE_JUDGE
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
#endif
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
#pragma endregion

int main() {
  int n, k;
  cin >> n >> k;
  vint a(n);
  rep(i, n) cin >> a[i];

  auto pos = [&](int x) {
    int sum = 0;
    rep(i, n) {
      int cut = a[i] / x;
      if (a[i] % x)
        cut++;
      cut--;
      sum += cut;
    }
    return sum <= k;
  };

  int l = 0, r = max_element(a.begin(), a.end())[0];
  while (l < r) {
    int mid = (l + r) / 2;
    bool ok = pos(mid);
    if (ok) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }

  cout << r << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vint = vector<int>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vchar = vector<char>;
using vvchar = vector<vchar>;
using vp = vector<P>;
using vpp = vector<pair<P, P>>;
using vvp = vector<vp>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
istream &operator>>(istream &is, P &a) { return is >> a.first >> a.second; }
ostream &operator<<(ostream &os, const P &a) {
  return os << "(" << a.first << "," << a.second << ")";
}

template <typename T>

void view(const std::vector<T> &v) {
#ifndef ONLINE_JUDGE
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
#endif
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
#pragma endregion

int main() {
  int n, k;
  cin >> n >> k;
  vint a(n);
  rep(i, n) cin >> a[i];

  auto pos = [&](int x) {
    int sum = 0;
    rep(i, n) {
      int cut = a[i] / x;
      if (a[i] % x)
        cut++;
      cut--;
      sum += cut;
    }
    return sum <= k;
  };

  int l = 1, r = max_element(a.begin(), a.end())[0];
  while (l < r) {
    int mid = (l + r) / 2;
    bool ok = pos(mid);
    if (ok) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }

  cout << r << endl;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (register int i = a; i <= b; i++)
#define per(i, a, b) for (register int i = a; i >= b; i--)
typedef long long LL;
const LL mod = 1e9 + 7;
const int N = 3e5 + 5;

int n;
LL k, a[N];
LL l, r;

bool Check(LL s) {
  LL res = 0;
  rep(i, 1, n) res += (a[i] + s - 1) / s - 1;
  return res <= k;
}

int main() {
  cin >> n >> k;
  rep(i, 1, n) cin >> a[i];
  l = 0, r = 1e9;
  while (l < r) {
    LL mid = (r + l) / 2;
    if (Check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (register int i = a; i <= b; i++)
#define per(i, a, b) for (register int i = a; i >= b; i--)
typedef long long LL;
const LL mod = 1e9 + 7;
const int N = 3e5 + 5;

int n;
LL k, a[N];
LL l, r;

bool Check(LL s) {
  LL res = 0;
  rep(i, 1, n) res += (a[i] + s - 1) / s - 1;
  return res <= k;
}

int main() {
  cin >> n >> k;
  rep(i, 1, n) cin >> a[i];
  l = 1, r = 1e9;
  while (l < r) {
    LL mid = (r + l) / 2;
    if (Check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
long long a[100005];

int isOk(int n, long long have, long long len) {
  long long left = have;
  for (int i = 1; i <= n; i++) {
    long long L = 0, R = left;
    long long res = -1;
    while (L <= R) {
      long long M = (L + R) / 2;
      if ((M + 1) * len >= a[i]) {
        res = M;
        R = M - 1;
      } else
        L = M + 1;
    }
    if (res == -1)
      return 0;
    else
      left -= res;
  }
  return 1;
}

int main() {
  int n;
  long long k;
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  long long L = 1, R = 1000000000;
  long long res = -1;
  while (L <= R) {
    int M = (L + R) / 2;
    if (isOk(n, k, M)) {
      res = M;
      R = M - 1;
    } else
      L = M + 1;
  }
  printf("%lld\n", res);
  return 0;
}


Fixed submission:
#include <cstdio>
long long a[200005];

int isOk(int n, long long have, long long len) {
  long long left = have;
  for (int i = 1; i <= n; i++) {
    long long L = 0, R = left;
    long long res = -1;
    while (L <= R) {
      long long M = (L + R) / 2;
      if ((M + 1) * len >= a[i]) {
        res = M;
        R = M - 1;
      } else
        L = M + 1;
    }
    if (res == -1)
      return 0;
    else
      left -= res;
  }
  return 1;
}

int main() {
  int n;
  long long k;
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  long long L = 1, R = 1000000000;
  long long res = -1;
  while (L <= R) {
    int M = (L + R) / 2;
    if (isOk(n, k, M)) {
      res = M;
      R = M - 1;
    } else
      L = M + 1;
  }
  printf("%lld\n", res);
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long int ll;
typedef pair<int, int> P;

bool check(int mid, vector<int> &a, int k) {
  int n = a.size();
  priority_queue<double> que;
  // bool ret = false;
  rep(i, n) que.push(a[i]);
  rep(i, k) {
    double p = que.top();
    que.pop();
    que.push(mid);
    que.push(p - mid);
  }

  return (que.top() <= mid);
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  int ok = 1e9, ng = 0;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (check(mid, a, k))
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long int ll;
typedef pair<int, int> P;

bool check(int mid, vector<int> &a, int k) {
  int n = a.size();
  int cnt = 0;
  rep(i, n) cnt += (a[i] - 1) / mid;
  return (cnt <= k);
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  int ok = 1e9, ng = 0;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (check(mid, a, k))
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int mod = 1e9 + 7;
signed main() {
  int n, k, a[200000];
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int ub = 0, lb = mod;
  for (int cnt = 0; cnt < 100; cnt++) {
    int mid = (lb + ub) / 2;
    int ans = 0;
    for (int i = 0; i < n; i++)
      ans += (a[i] + mid - 1) / mid;
    (ans <= n + k ? lb : ub) = mid;
  }
  cout << lb << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int mod = 1e9 + 7;
signed main() {
  int n, k, a[200000];
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int ub = 0, lb = mod;
  while (lb - ub > 1) {
    int mid = (lb + ub) / 2;
    int ans = 0;
    for (int i = 0; i < n; i++)
      ans += (a[i] + mid - 1) / mid;
    (ans <= n + k ? lb : ub) = mid;
  }
  cout << lb << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
// ll mod = 998244353;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  ll max_num = 0;
  rep(i, n) {
    cin >> a[i];
    max_num = max(max_num, a[i]);
  }
  if (k == 0) {
    cout << max_num << endl;
    return 0;
  }
  ll l = -1;
  ll r = 1e9 + 1;

  while (r - l > 1LL) {
    ll m = (l + r) / 2LL;
    ll cnt = 0;
    rep(i, n) { cnt += (a[i] + m - 1LL) / m - 1LL; }
    // cout << l << " " << r << " " << cnt << endl;
    if (cnt > k) {
      l = m;
    } else {
      r = m;
    }
  }
  cout << r << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
// ll mod = 998244353;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  ll max_num = 0;
  rep(i, n) {
    cin >> a[i];
    max_num = max(max_num, a[i]);
  }
  if (k == 0) {
    cout << max_num << endl;
    return 0;
  }
  ll l = 0;
  ll r = 1e9;

  while (r - l > 1LL) {
    ll m = (l + r) / 2LL;
    ll cnt = 0;
    rep(i, n) { cnt += (a[i] + m - 1LL) / m - 1LL; }
    // cout << l << " " << r << " " << cnt << endl;
    if (cnt > k) {
      l = m;
    } else {
      r = m;
    }
  }
  cout << r << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll l = -1;
  ll r = 1e9 + 1;
  while (r - l > 1) {
    ll m = (l + r) / 2;
    ll num = 0;
    rep(i, n) { num += (a[i] - 1LL) / m; }
    if (num > k) {
      l = m;
    } else {
      r = m;
    }
  }
  cout << r << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll l = 0;
  ll r = 1e9;
  while (r - l > 1) {
    ll m = (l + r) / 2;
    ll num = 0;
    rep(i, n) { num += (a[i] - 1LL) / m; }
    if (num > k) {
      l = m;
    } else {
      r = m;
    }
  }
  cout << r << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
// Abhinav ---IIIT_A
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define f(i, x, n) for (int i = x; i < (int)n; ++i)
#define vpii vector<pair<int, int>>
#define vi vector<int>
#define mpii map<pair<int, int>, int>
#define mpivpii map<int, vector<pair<int, int>>>
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define mpi map<int, int>
#define vvi vector<vector<int>>
#define vvvi vector<vvi>
ld pie = 3.141592653589;
int mod = 1e9 + 7;

bool Compare(pair<int, int> a, pair<int, int> b) {
  int l1 = a.ss - a.ff + 1;
  int l2 = b.ss - b.ff + 1;
  if (l1 != l2)
    return l1 > l2;
  return a.ff > b.ff;
}
std::priority_queue<pii, std::vector<pii>, std::function<bool(pii, pii)>>
    pq(Compare);

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int ans = -1;
  int n, k;
  cin >> n >> k;
  int a[n];
  f(i, 0, n) cin >> a[i];
  int l = 0, h = 1e10;
  while (l <= h) {
    int mid = (l + h) / 2;
    int copy[n];
    f(i, 0, n) copy[i] = a[i];
    int cuts = 0;
    f(i, 0, n) {
      if (copy[i] > mid) {
        cuts += (((copy[i] + mid - 1) / mid) - 1);
      }
    }
    if (cuts <= k) {
      ans = mid;
      h = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans;
  return 0;
}
// check for overflows dummy !!!

Fixed submission:
// Abhinav ---IIIT_A
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define f(i, x, n) for (int i = x; i < (int)n; ++i)
#define vpii vector<pair<int, int>>
#define vi vector<int>
#define mpii map<pair<int, int>, int>
#define mpivpii map<int, vector<pair<int, int>>>
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define mpi map<int, int>
#define vvi vector<vector<int>>
#define vvvi vector<vvi>
ld pie = 3.141592653589;
int mod = 1e9 + 7;

bool Compare(pair<int, int> a, pair<int, int> b) {
  int l1 = a.ss - a.ff + 1;
  int l2 = b.ss - b.ff + 1;
  if (l1 != l2)
    return l1 > l2;
  return a.ff > b.ff;
}
std::priority_queue<pii, std::vector<pii>, std::function<bool(pii, pii)>>
    pq(Compare);

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int ans = -1;
  int n, k;
  cin >> n >> k;
  int a[n];
  f(i, 0, n) cin >> a[i];
  int l = 1, h = 1e10;
  while (l <= h) {
    int mid = (l + h) / 2;
    int copy[n];
    f(i, 0, n) copy[i] = a[i];
    int cuts = 0;
    f(i, 0, n) {
      if (copy[i] > mid) {
        cuts += (((copy[i] + mid - 1) / mid) - 1);
      }
    }
    if (cuts <= k) {
      ans = mid;
      h = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans;
  return 0;
}
// check for overflows dummy !!!
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
// *st.find_by_order(index), 0-based
// order_of_key(e)

ll eval(ll x, vector<ll> &A) {
  ll ret = 0;
  for (auto a : A)
    ret += (a + x - 1) / x - 1;
  return ret;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  ll low = 0, high = 1e9 + 7;
  while (low < high) {
    int mid = (low + high) / 2;
    if (eval(mid, A) <= K)
      high = mid;
    else
      low = mid + 1;
  }
  cout << min(*max_element(A.begin(), A.end()), high) << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
// *st.find_by_order(index), 0-based
// order_of_key(e)

ll eval(ll x, vector<ll> &A) {
  ll ret = 0;
  for (auto a : A)
    ret += (a + x - 1) / x - 1;
  return ret;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  ll low = 1, high = 1e9 + 7;
  while (low < high) {
    int mid = (low + high) / 2;
    if (eval(mid, A) <= K)
      high = mid;
    else
      low = mid + 1;
  }
  cout << min(*max_element(A.begin(), A.end()), high) << "\n";
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
using ll = int64_t;

bool ok(const vector<ll> &A, ll k, ll allowed) {
  ll cost = 0;
  for (ll i = 0; i < A.size(); i++) {
    // smallest K s.t. allowed*K >= A[i]
    // cost += K-1
    // 1..allowed -> 0
    // allowed+1..2*allowed -> 1
    cost += (A[i] - 1) / allowed;
  }
  return cost <= k;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> A(n, 0);
  for (ll i = 0; i < n; i++) {
    cin >> A[i];
  }

  ll lo = 0;
  ll hi = 1e9;
  while (lo < hi) {
    ll mid = (lo + hi) / 2;
    // cerr << " mid=" << mid << " ok=" << ok(A, k, mid) << endl;
    if (ok(A, k, mid)) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  cout << lo << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
using ll = int64_t;

bool ok(const vector<ll> &A, ll k, ll allowed) {
  ll cost = 0;
  for (ll i = 0; i < A.size(); i++) {
    // smallest K s.t. allowed*K >= A[i]
    // cost += K-1
    // 1..allowed -> 0
    // allowed+1..2*allowed -> 1
    cost += (A[i] - 1) / allowed;
  }
  return cost <= k;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> A(n, 0);
  for (ll i = 0; i < n; i++) {
    cin >> A[i];
  }

  ll lo = 1;
  ll hi = 1e9;
  while (lo < hi) {
    ll mid = (lo + hi) / 2;
    // cerr << " mid=" << mid << " ok=" << ok(A, k, mid) << endl;
    if (ok(A, k, mid)) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  cout << lo << endl;
}
Problem p02598 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define NMAX 200002
using namespace std;
vector<int> v;
int N, K, M = 0, m = 0, ans = 0;
bool able(int a) {
  int cou = 0;
  for (auto &&i : v) {
    int b = i;
    if (b <= a)
      break;
    while (true) {
      if (b > a) {
        b = b - a;
        cou++;
      } else {
        break;
      }
    }
  }
  // cout << a << ":" << cou << endl;
  if (cou > K)
    return false;
  else
    return true;
}
int main() {
  cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    v.push_back(a);
    M = max(M, a);
  }
  sort(v.begin(), v.end(), greater<int>());
  while (M - m > 1) {
    int k = (M + m) / 2;
    if (able(k))
      M = k;
    else
      m = k;
  }
  cout << M << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define NMAX 200002
using namespace std;
vector<int> v;
int N, K, M = 0, m = 0, ans = 0;
bool able(int a) {
  int cou = 0;
  for (auto &&i : v) {
    int b = i;
    if (b <= a)
      break;
    if (b > a) {
      cou = cou + b / a - (b % a == 0);
    }
  }
  // cout << a << ":" << cou << endl;
  if (cou > K)
    return false;
  else
    return true;
}
int main() {
  cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    v.push_back(a);
    M = max(M, a);
  }
  sort(v.begin(), v.end(), greater<int>());
  while (M - m > 1) {
    int k = (M + m) / 2;
    if (able(k))
      M = k;
    else
      m = k;
  }
  cout << M << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

bool f(vector<int> &a, int target, int k) {
  int t = 0, i = 0;
  while (t <= k && i != a.size()) {
    t += (a[i] - 1) / target;
    ++i;
  }
  if (t > k)
    return false;
  else
    return true;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  sort(a.rbegin(), a.rend());
  int maxi = a[0];
  int mini = 0;

  while (1) {
    int now = (maxi + mini) / 2;
    bool ret = f(a, now, k);
    if (ret)
      maxi = now;
    else
      mini = now;
    if (maxi == mini + 1)
      break;
  }
  cout << maxi << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

bool f(vector<int> &a, int target, int k) {
  int t = 0, i = 0;
  while (t <= k && i != a.size()) {
    t += (a[i] - 1) / target;
    ++i;
  }
  if (t > k)
    return false;
  else
    return true;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  sort(a.rbegin(), a.rend());
  int maxi = a[0];
  int mini = 0;

  while (1) {
    int now = (maxi + mini) / 2;
    if (now == 0)
      break;
    bool ret = f(a, now, k);
    if (ret)
      maxi = now;
    else
      mini = now;
    if (maxi == mini + 1)
      break;
  }
  cout << maxi << endl;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

using namespace std;

bool possible(long long longestLog, const vector<long long> &A, long long K) {
  long long N = A.size();
  long long numOfLongestLogs = 0;
  for (long long i = 0; i < N; ++i) {
    numOfLongestLogs += A[i] / longestLog;
    if (A[i] % longestLog == 0) {
      --numOfLongestLogs;
    }
  }
  return (numOfLongestLogs <= K);
}

void Main() {
  long long N, K;
  cin >> N >> K;
  vector<long long> A(N, 0LL);
  for (long long i = 0; i < N; ++i) {
    cin >> A[i];
  }
  sort(A.begin(), A.end(), greater<long long>());

  long long ub = A.front();
  long long lb = 0LL;
  while (ub - lb > 1LL) {
    long long mid = (ub + lb) / 2LL;
    if (possible(mid, A, K)) {
      ub = mid;
    } else {
      lb = mid;
    }
  }
  if (ub == lb) {
    cout << ub << endl;
  } else {
    if (possible(lb, A, K)) {
      cout << lb << endl;
    } else {
      cout << ub << endl;
    }
  }
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

bool possible(long long longestLog, const vector<long long> &A, long long K) {
  long long N = A.size();
  long long numOfLongestLogs = 0;
  for (long long i = 0; i < N; ++i) {
    numOfLongestLogs += A[i] / longestLog;
    if (A[i] % longestLog == 0) {
      --numOfLongestLogs;
    }
  }
  return (numOfLongestLogs <= K);
}

void Main() {
  long long N, K;
  cin >> N >> K;
  vector<long long> A(N, 0LL);
  for (long long i = 0; i < N; ++i) {
    cin >> A[i];
  }
  sort(A.begin(), A.end(), greater<long long>());

  long long ub = A.front();
  long long lb = 0LL;
  while (ub - lb > 1LL) {
    long long mid = (ub + lb) / 2LL;
    if (possible(mid, A, K)) {
      ub = mid;
    } else {
      lb = mid;
    }
  }
  cout << ub << endl;
  // if (ub == lb) {
  //     cout << ub << endl;
  // }
  // else {
  //     if (possible(lb, A, K)) {
  //         cout << lb << endl;
  //     }
  //     else {
  //         cout << ub << endl;
  //     }
  // }
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  int maxsize = 0;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    maxsize = max(A[i], maxsize);
  }

  int l = 0;
  int r = maxsize;
  for (int n = 0; n < 100; ++n) {
    int x = (l + r) / 2;
    int count = 0;
    for (int i = 0; i < N; ++i) {
      count += A[i] / x;
      if (0 != A[i] % x) {
        count++;
      }
    }
    if (count > N + K) {
      l = x;
    } else {
      r = x;
    }
  }
  cout << r << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  int maxsize = 0;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    maxsize = max(A[i], maxsize);
  }

  int l = 1;
  int r = maxsize;
  for (int n = 0; n < 100; ++n) {
    int x = (l + r) / 2;
    int count = 0;
    for (int i = 0; i < N; ++i) {
      count += A[i] / x;
      if (0 != A[i] % x) {
        count++;
      }
    }
    if (count > N + K) {
      l = x;
    } else {
      r = x;
    }
  }
  cout << r << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
// #define _CRT_SECURE_NO_WARNINGS
// #include <bits/stdc++.h>
#include "bits/stdc++.h"

#define rep(i, n) for (long long(i) = 0; (i) < (long long)(n); (i)++)
#define all(x) (x).begin(), (x).end()
#define int long long

#define PI (3.14159265358979323)
#define MOD (1000000007LL)
#define INF (1LL << 60LL)
#define MAX_N (10000001)
#define MAX (1000000000000000000LL)
using namespace std;

signed main() {
  int l = 0, r = 1000000000;
  int n;
  int k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  while (l != r) {
    int m = (l + r) / 2;
    int k2 = 0;
    rep(i, n) {
      int b = a[i];
      k2 += (b / m) - 1;
      if (b % m != 0)
        k2++;
    }
    if (k2 <= k) {
      r = m;
    } else {
      l = m + 1;
    }
  }
  cout << l << endl;
}

Fixed submission:
// #define _CRT_SECURE_NO_WARNINGS
// #include <bits/stdc++.h>
#include "bits/stdc++.h"

#define rep(i, n) for (long long(i) = 0; (i) < (long long)(n); (i)++)
#define all(x) (x).begin(), (x).end()
#define int long long

#define PI (3.14159265358979323)
#define MOD (1000000007LL)
#define INF (1LL << 60LL)
#define MAX_N (10000001)
#define MAX (1000000000000000000LL)
using namespace std;

signed main() {
  int l = 1, r = 1000000000;
  int n;
  int k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  while (l != r) {
    int m = (l + r) / 2;
    int k2 = 0;
    rep(i, n) {
      int b = a[i];
      k2 += (b / m) - 1;
      if (b % m != 0)
        k2++;
    }
    if (k2 <= k) {
      r = m;
    } else {
      l = m + 1;
    }
  }
  cout << l << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

long long int Exp(long long int exp_n, long long int exp_e, long long exp_mod);

int main() {
  long long int N, K, Max_A = 0, O;
  priority_queue<long long int> q;
  cin >> N >> K;
  long long int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    Max_A = max(Max_A, A[i]);
  }
  long long int X, top = Max_A, btm = 0;
  X = (top + btm) / 2;
  while (top != X) {
    O = 0;
    for (int i = 0; i < N; i++) {
      O = O + (A[i] - 1) / X;
    }
    if (O > K) {
      btm = X;
      X = (top + btm + 1) / 2;
    } else {
      top = X;
      X = (top + btm + 1) / 2;
    }
  }

  cout << X << endl;

  return 0;
}

long long int Exp(long long int exp_n, long long int exp_e,
                  long long int exp_mod) {
  long long int exp_o = 1;
  long long int exp_x = exp_n;
  long long int exp_tmp;

  while (0 < exp_e) {
    exp_tmp = exp_e / 2;
    if (exp_e - exp_tmp * 2 == 1) {
      exp_o = exp_o * exp_x;
      if (exp_o > exp_mod) {
        exp_o = exp_o % exp_mod;
      }
    }

    exp_x = exp_x * exp_x;
    if (exp_x > exp_mod) {
      exp_x = exp_x % exp_mod;
    }
    exp_e = exp_tmp;
  }
  return exp_o;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

long long int Exp(long long int exp_n, long long int exp_e, long long exp_mod);

int main() {
  long long int N, K, Max_A = 0, O;
  priority_queue<long long int> q;
  cin >> N >> K;
  long long int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    Max_A = max(Max_A, A[i]);
  }
  long long int X, top = Max_A, btm = 0;
  X = (top + btm + 1) / 2;
  while (top != X) {
    O = 0;
    for (int i = 0; i < N; i++) {
      O = O + (A[i] - 1) / X;
    }
    if (O > K) {
      btm = X;
      X = (top + btm + 1) / 2;
    } else {
      top = X;
      X = (top + btm + 1) / 2;
    }
  }

  cout << X << endl;

  return 0;
}

long long int Exp(long long int exp_n, long long int exp_e,
                  long long int exp_mod) {
  long long int exp_o = 1;
  long long int exp_x = exp_n;
  long long int exp_tmp;

  while (0 < exp_e) {
    exp_tmp = exp_e / 2;
    if (exp_e - exp_tmp * 2 == 1) {
      exp_o = exp_o * exp_x;
      if (exp_o > exp_mod) {
        exp_o = exp_o % exp_mod;
      }
    }

    exp_x = exp_x * exp_x;
    if (exp_x > exp_mod) {
      exp_x = exp_x % exp_mod;
    }
    exp_e = exp_tmp;
  }
  return exp_o;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#define dlog(str) cout << "====" << str << endl;
#else
#define dlog(str)
#endif
#define INF 999999999
#define MOD 1000000007
#define REP(i, n) for (int i = 0, i##_l = (n); i < i##_l; i++)
#define FOR(i, s, e) for (int i = s, i##_l = (e); i < i##_l; i++)
#define LLI long long int
#define _min(a, b) ((a < b) ? a : b)
#define _max(a, b) ((a < b) ? b : a)
#define chmax(a, b) a = _max(a, b)
#define chmin(a, b) a = _min(a, b)
#define bit(a, shift) ((a>>shift)&1))
#define pm(a) ((a) ? 1 : -1)
#define SORT(v) sort(v.begin(), v.end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
// int 2.14E±9    lli 9.2E±18    double 1.7E±380

int main() {
  cout << fixed << setprecision(10);
  int n, k;
  cin >> n >> k;

  vector<int> a(n);
  REP(i, n) { cin >> a[i]; }
  RSORT(a);

  LLI l = 0, r = 1e9;

  while (l < r) {
    LLI m = (l + r) / 2;
    int c = 0;
    REP(i, n) { c += (a[i] - 1) / m; }

    if (c <= k) {
      r = m;
    } else {
      l = m + 1;
    }
  }
  cout << l << endl;

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#define dlog(str) cout << "====" << str << endl;
#else
#define dlog(str)
#endif
#define INF 999999999
#define MOD 1000000007
#define REP(i, n) for (int i = 0, i##_l = (n); i < i##_l; i++)
#define FOR(i, s, e) for (int i = s, i##_l = (e); i < i##_l; i++)
#define LLI long long int
#define _min(a, b) ((a < b) ? a : b)
#define _max(a, b) ((a < b) ? b : a)
#define chmax(a, b) a = _max(a, b)
#define chmin(a, b) a = _min(a, b)
#define bit(a, shift) ((a>>shift)&1))
#define pm(a) ((a) ? 1 : -1)
#define SORT(v) sort(v.begin(), v.end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
// int 2.14E±9    lli 9.2E±18    double 1.7E±380

int main() {
  cout << fixed << setprecision(10);
  int n, k;
  cin >> n >> k;

  vector<int> a(n);
  REP(i, n) { cin >> a[i]; }
  RSORT(a);

  LLI l = 1, r = 1e9;

  while (l < r) {
    LLI m = (l + r) / 2;
    int c = 0;
    REP(i, n) { c += (a[i] - 1) / m; }

    if (c <= k) {
      r = m;
    } else {
      l = m + 1;
    }
  }
  cout << l << endl;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);

  int l = 0, r = 0;
  rep(i, 0, n) {
    cin >> a[i];
    r = max(r, a[i]);
  }

  // upper bound
  while (l != r) {
    int mid = (r + l) >> 1;
    bool ok = [&] {
      int res = 0;
      rep(i, 0, n) { res += (a[i] - 1) / mid; }
      return res <= k;
    }();
    if (ok)
      r = mid;
    else
      l = mid + 1;
  }
  cout << l;
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);

  int l = 1, r = 0;
  rep(i, 0, n) {
    cin >> a[i];
    r = max(r, a[i]);
  }

  // upper bound
  while (l != r) {
    int mid = (r + l) >> 1;
    bool ok = [&] {
      int res = 0;
      rep(i, 0, n) { res += (a[i] - 1) / mid; }
      return res <= k;
    }();
    if (ok)
      r = mid;
    else
      l = mid + 1;
  }
  cout << l;
  return 0;
}