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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;

#ifdef _DEBUG
#include "../../library/library/debug_template.cpp"
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif

struct init {
  init() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} init_;

int main() {

  int N, K;
  cin >> N >> K;

  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  auto judge = [&](auto idx) {
    lint sum = 0;
    for (int i = 0; i < N; i++)
      sum += (A[i] - 1) / idx;
    return sum <= K;
  };

  auto binary_search = [&](auto f) {
    auto ng = -1;
    auto ok = INF;

    while (abs(ok - ng) > 1) {
      auto mid = (ok + ng) / 2;

      if (f(mid))
        ok = mid;
      else
        ng = mid;
    }

    return ok;
  };

  cout << binary_search(judge) << "\n";

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;

#ifdef _DEBUG
#include "../../library/library/debug_template.cpp"
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif

struct init {
  init() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} init_;

int main() {

  int N, K;
  cin >> N >> K;

  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  auto judge = [&](auto idx) {
    lint sum = 0;
    for (int i = 0; i < N; i++)
      sum += (A[i] - 1) / idx;
    return sum <= K;
  };

  auto binary_search = [&](auto f) {
    auto ng = 0;
    auto ok = INF;

    while (abs(ok - ng) > 1) {
      auto mid = (ok + ng) / 2;

      if (f(mid))
        ok = mid;
      else
        ng = mid;
    }

    return ok;
  };

  cout << binary_search(judge) << "\n";

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
int n, k;
int a[maxn];
bool check(int x) {
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += a[i] / x;
    if (a[i] % x == 0)
      sum--;
  }
  if (sum > k)
    return 0;
  else
    return 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  cin >> n >> k;
  int maxv = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    maxv = max(maxv, a[i]);
  }
  int l = 0, r = maxv + 1, ans;
  while (l < r) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
int n, k;
int a[maxn];
bool check(int x) {
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += a[i] / x;
    if (a[i] % x == 0)
      sum--;
  }
  if (sum > k)
    return 0;
  else
    return 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  cin >> n >> k;
  int maxv = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    maxv = max(maxv, a[i]);
  }
  int l = 1, r = 1000000000, ans;
  while (l < r) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << '=' << h << ',';
  _dbg(sdbg + 1, a...);
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

typedef long long ll;
string s;
const int maxn = 2e5 + 100;
int a[maxn];
int main() {
  int n, k;
  cin >> n >> k;
  int maxx = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (a[i] > maxx)
      maxx = a[i];
  }
  int l = 0, r = maxx;
  while (l < r) {
    int mid = (l + r) >> 1;
    int res = k;
    for (int i = 0; i < n; ++i) {
      if (a[i] % mid == 0)
        res -= a[i] / mid - 1;
      else
        res -= a[i] / mid;
    }
    debug(l, r, mid, res);

    if (res < 0)
      l = mid + 1;
    else
      r = mid;
  }
  cout << l << endl;
  return 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << '=' << h << ',';
  _dbg(sdbg + 1, a...);
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

typedef long long ll;
string s;
const int maxn = 2e5 + 100;
int a[maxn];
int main() {
  int n, k;
  cin >> n >> k;
  int maxx = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (a[i] > maxx)
      maxx = a[i];
  }
  int l = 1, r = maxx;
  while (l < r) {
    int mid = (l + r) >> 1;
    int res = k;
    for (int i = 0; i < n; ++i) {
      if (a[i] % mid == 0)
        res -= a[i] / mid - 1;
      else
        res -= a[i] / mid;
    }
    debug(l, r, mid, res);

    if (res < 0)
      l = mid + 1;
    else
      r = mid;
  }
  cout << l << endl;
  return 0;
}
Problem p02598 (C++)

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

#define ll long long
// #define endl '\n'

void max_self(int &a, int b) { a = max(a, b); }
void min_self(int &a, int b) { a = min(a, b); }

int n, k;
vector<int> v;

bool good(int x) {
  int cnt = k;
  for (int i = 0; i < n; i++) {
    cnt -= (v[i] - 1) / x;
  }
  return cnt >= 0;
}

void solve() {
  cin >> n >> k;
  v.resize(n);
  for (int &i : v)
    cin >> i;
  int l = -1;  // bad
  int r = 1e9; // good

  while (r > l + 1) {
    int mid = (l + r) / 2;
    if (good(mid)) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1; // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}


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

#define ll long long
// #define endl '\n'

void max_self(int &a, int b) { a = max(a, b); }
void min_self(int &a, int b) { a = min(a, b); }

int n, k;
vector<int> v;

bool good(int x) {
  int cnt = k;
  for (int i = 0; i < n; i++) {
    cnt -= (v[i] - 1) / x;
  }
  return cnt >= 0;
}

void solve() {
  cin >> n >> k;
  v.resize(n);
  for (int &i : v)
    cin >> i;
  int l = 0;   // bad
  int r = 1e9; // good

  while (r > l + 1) {
    int mid = (l + r) / 2;
    if (good(mid)) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1; // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
Problem p02598 (C++)

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

int main() {
  // your code goes
  long long int n, k;
  cin >> n >> k;
  long long int a[n];
  for (long int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long int l = 0, r = 1e9;
  long long int ans = 1e9;
  while (l <= r) {
    long long int mid = (l + r) / 2;
    long long int ss = 0;
    for (long int i = 0; i < n; i++) {
      ss += a[i] / mid;
      if (a[i] % mid == 0)
        ss--;
    }
    if (ss > k) {
      l = mid + 1;
    } else {
      ans = min(ans, mid);
      r = mid - 1;
    }
  }
  cout << ans << endl;
  return 0;
}


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

int main() {
  // your code goes
  long long int n, k;
  cin >> n >> k;
  long long int a[n];
  for (long int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long int l = 1, r = 1e9;
  long long int ans = 1e9;
  while (l <= r) {
    long long int mid = (l + r) / 2;
    long long int ss = 0;
    for (long int i = 0; i < n; i++) {
      ss += a[i] / mid;
      if (a[i] % mid == 0)
        ss--;
    }
    if (ss > k) {
      l = mid + 1;
    } else {
      ans = min(ans, mid);
      r = mid - 1;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
// Bismillahir Rahmanir Rahim .Ya Allah...
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> PII;
const int mod = 1e9 + 7, INF = 1e9;
const int mx = 2e5 + 5;
vector<ll> v(mx);
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> v[i];
  ll lb = 0, rb = 1e9 + 7;
  while (lb < rb) {
    ll mid = (lb + rb) / 2;
    ll ans = 0;
    for (int i = 0; i < n; i++)
      ans = ans + (v[i] + mid - 1) / mid - 1;
    if (ans <= k)
      rb = mid;
    else
      lb = mid + 1;
  }
  cout << rb << endl;
  return 0;
}


Fixed submission:
// Bismillahir Rahmanir Rahim .Ya Allah...
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> PII;
const int mod = 1e9 + 7, INF = 1e9;
const int mx = 2e5 + 5;
vector<ll> v(mx);
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> v[i];
  ll lb = 1, rb = 1e9 + 7;
  while (lb < rb) {
    ll mid = (lb + rb) / 2;
    ll ans = 0;
    for (int i = 0; i < n; i++)
      ans = ans + (v[i] + mid - 1) / mid - 1;
    if (ans <= k)
      rb = mid;
    else
      lb = mid + 1;
  }
  cout << rb << endl;
  return 0;
}
Problem p02598 (C++)

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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;
typedef tuple<long long, long long, long long> tllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const llint INF = 1<<21;
// static const llint MOD = 1e9 + 7;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
bool compTuple(const tllint &arg1, const tllint &arg2) {
  return get<2>(arg1) > get<2>(arg2);
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

int main(void) {
  int n, k;
  cin >> n >> k;

  vector<int> a(n);
  int max = 0;
  for (int in = 0; in < n; in++) {
    cin >> a[in];
    if (max < a[in])
      max = a[in];
  }

  int l = 0, r = max, mid, ans = max, ktmp, times;
  while (l < r) {
    mid = l + (r - l) / 2;

    ktmp = 0;
    for (int in = 0; in < n; in++) {
      times = a[in] / mid;
      if (a[in] % mid != 0)
        times++;
      times--;
      ktmp += times;
    }
    if (ktmp <= k) {
      ans = min(ans, mid);
      r = mid;
    } else
      l = mid + 1;
  }

  cout << ans << endl;
  return 0;
}


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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;
typedef tuple<long long, long long, long long> tllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const llint INF = 1<<21;
// static const llint MOD = 1e9 + 7;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
bool compTuple(const tllint &arg1, const tllint &arg2) {
  return get<2>(arg1) > get<2>(arg2);
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

int main(void) {
  int n, k;
  cin >> n >> k;

  vector<int> a(n);
  int max = 0;
  for (int in = 0; in < n; in++) {
    cin >> a[in];
    if (max < a[in])
      max = a[in];
  }

  int l = 1, r = max, mid, ans = max, ktmp, times;
  while (l < r) {
    mid = l + (r - l) / 2;

    ktmp = 0;
    for (int in = 0; in < n; in++) {
      times = a[in] / mid;
      if (a[in] % mid != 0)
        times++;
      times--;
      ktmp += times;
    }
    if (ktmp <= k) {
      ans = min(ans, mid);
      r = mid;
    } else
      l = mid + 1;
  }

  cout << ans << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
// #include<i_am_noob_orz>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ull unsigned long long
#define pii pair<int, int>
#define X first
#define Y second
#define mod ((ll)1e9 + 7)
#define pb push_back
#define mp make_pair
#define abs(x) ((x) > 0 ? (x) : (-(x)))
#define F(n) Fi(i, n)
#define Fi(i, n) Fl(i, 0, n)
#define Fl(i, l, n) for (int i = l; i < n; i++)
#define memres(a) memset(a, 0, sizeof(a))
#define all(a) a.begin(), a.end()
#define sz(a) ((int)a.size())
#define ceiling(a, b) (((a) + (b)-1) / (b))
#define endl '\n'
#define bit_count(x) __builtin_popcount((x))
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// #define LOCAL
#ifdef LOCAL
#define debug(a) cerr << #a << " " << a << endl;
#else
#define debug(a)
#endif
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int a[200010];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  F(n) cin >> a[i];
  int l = 0, r = 1e9;
  while (l < r) {
    int mi = ((l + r) / 2);
    int s = 0;
    // cout<<l<<" "<<r<<endl;
    // cout<<mi<<endl;
    F(n) if (a[i] > mi) s += (ceiling(a[i], mi) - 1);
    // cout<<s<<" "<<m<<endl;
    if (s <= m)
      r = mi;
    else
      l = mi + 1;
  }
  cout << l << endl;
  return 0;
}


Fixed submission:
// #include<i_am_noob_orz>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ull unsigned long long
#define pii pair<int, int>
#define X first
#define Y second
#define mod ((ll)1e9 + 7)
#define pb push_back
#define mp make_pair
#define abs(x) ((x) > 0 ? (x) : (-(x)))
#define F(n) Fi(i, n)
#define Fi(i, n) Fl(i, 0, n)
#define Fl(i, l, n) for (int i = l; i < n; i++)
#define memres(a) memset(a, 0, sizeof(a))
#define all(a) a.begin(), a.end()
#define sz(a) ((int)a.size())
#define ceiling(a, b) (((a) + (b)-1) / (b))
#define endl '\n'
#define bit_count(x) __builtin_popcount((x))
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// #define LOCAL
#ifdef LOCAL
#define debug(a) cerr << #a << " " << a << endl;
#else
#define debug(a)
#endif
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int a[200010];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  F(n) cin >> a[i];
  int l = 1, r = 1e9;
  while (l < r) {
    int mi = ((l + r) / 2);
    int s = 0;
    // cout<<l<<" "<<r<<endl;
    // cout<<mi<<endl;
    F(n) if (a[i] > mi) s += (ceiling(a[i], mi) - 1);
    // cout<<s<<" "<<m<<endl;
    if (s <= m)
      r = mi;
    else
      l = mi + 1;
  }
  cout << l << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define M 1000000000
#define f(i, a, b) for (long long i = a; i < b; i++)
#define tc                                                                     \
  long long t;                                                                 \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define mp make_pair
#define gcd __gcd
#define F first
#define S second
#define input(a, n)                                                            \
  for (ll i = 0; i < n; i++)                                                   \
    cin >> a[i];
#define output(a, n)                                                           \
  for (ll i = 0; i < n; i++)                                                   \
    cout << a[i] << " ";
#define up upper_bound
#define lo lower_bound
#define endl "\n"
#define mem(a) memset(a, -1, sizeof(a))
#define ll long long
#define umap unordered_map

ll a[100005];
ll n, k;
bool solve(ll x) {
  ll sum = 0;
  f(i, 0, n) { sum += (a[i] - 1) / x; }
  return sum <= k;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> n >> k;

  input(a, n);

  ll l = 1, r = 1e9 + 10, mid;

  while (l < r) {
    mid = (l + r) / 2;
    if (solve(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << r;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define M 1000000000
#define f(i, a, b) for (long long i = a; i < b; i++)
#define tc                                                                     \
  long long t;                                                                 \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define mp make_pair
#define gcd __gcd
#define F first
#define S second
#define input(a, n)                                                            \
  for (ll i = 0; i < n; i++)                                                   \
    cin >> a[i];
#define output(a, n)                                                           \
  for (ll i = 0; i < n; i++)                                                   \
    cout << a[i] << " ";
#define up upper_bound
#define lo lower_bound
#define endl "\n"
#define mem(a) memset(a, -1, sizeof(a))
#define ll long long
#define umap unordered_map

ll a[200010];
ll n, k;
bool solve(ll x) {
  ll sum = 0;
  f(i, 0, n) { sum += (a[i] - 1) / x; }
  return sum <= k;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> n >> k;

  input(a, n);

  ll l = 1, r = 1e9 + 10, mid;

  while (l < r) {
    mid = (l + r) / 2;
    if (solve(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << r;

  return 0;
}
Problem p02598 (C++)

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

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll n, k, v[200001], minim = 1e10;
bool ok(ll val) {
  ll x = val;
  ll nrop = 0;
  for (int i = 1; i <= n; i++) {
    ll bc = v[i] / x;
    if (v[i] % x)
      bc++;
    nrop += bc - 1;
  }
  return nrop <= k;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  ll st = 0;
  ll dr = 1e9;
  while (st <= dr) {
    ll mij = (st + dr) / 2;
    if (ok(mij)) {
      minim = min(mij, minim);
      dr = mij - 1;
    } else
      st = mij + 1;
  }
  cout << minim;
  return 0;
}


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

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll n, k, v[200001], minim = 1e10;
bool ok(ll val) {
  ll x = val;
  ll nrop = 0;
  for (int i = 1; i <= n; i++) {
    ll bc = v[i] / x;
    if (v[i] % x)
      bc++;
    nrop += bc - 1;
  }
  return nrop <= k;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  ll st = 1;
  ll dr = 1e9;
  while (st <= dr) {
    ll mij = (st + dr) / 2;
    if (ok(mij)) {
      minim = min(mij, minim);
      dr = mij - 1;
    } else
      st = mij + 1;
  }
  cout << minim;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define sz(x) int(x.size())
#define bitsz(x) int(__builtin_popcount(x))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb(x) push_back(x)
#define INF 1e9
#define LINF 1e18
#define mod 1000000007
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const int di[4] = {1, 0, -1, 0};
const int dj[4] = {0, 1, 0, -1};

int main() {
  int n;
  cin >> n;
  int k;
  cin >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int lb = -1, ub = INF;
  auto C = [&](int x) {
    int num = 0;
    rep(i, n) {
      int tmp = a[i] / x;
      if (a[i] % x == 0)
        tmp--;
      num += tmp;
    }
    if (num <= k)
      return true;
    else
      return false;
  };
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    if (C(mid))
      ub = mid;
    else
      lb = mid;
  }
  cout << ub << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define sz(x) int(x.size())
#define bitsz(x) int(__builtin_popcount(x))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb(x) push_back(x)
#define INF 1e9
#define LINF 1e18
#define mod 1000000007
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const int di[4] = {1, 0, -1, 0};
const int dj[4] = {0, 1, 0, -1};

int main() {
  int n;
  cin >> n;
  int k;
  cin >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int lb = 0, ub = INF;
  auto C = [&](int x) {
    int num = 0;
    rep(i, n) {
      int tmp = a[i] / x;
      if (a[i] % x == 0)
        tmp--;
      num += tmp;
    }
    if (num <= k)
      return true;
    else
      return false;
  };
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    if (C(mid))
      ub = mid;
    else
      lb = mid;
  }
  cout << ub << endl;
}
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 l = 0;
  // int r = -1;
  int r = 1000000000;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    // r = max(a[i],r);
  }

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

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  int l = 1;
  // int r = -1;
  int r = 1000000000;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    // r = max(a[i],r);
  }

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

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

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define rep(i, cc, n) for (int i = cc; i < n; ++i)
#define lrep(i, cc, n) for (long long i = cc; i < n; ++i)
#define sqrep(i, cc, n) for (long long i = cc; i * i <= n; ++i)
#define rrep(i, cc, n) for (long i = cc; i > n; --i)
#define pii pair<int, int>
#define pll pair<long long, long long>

using ll = long long;

const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
const double PI = 3.1415926535;
const ll inf = 1001001001;
const ll e9 = 1000000000;
const ll mod = 1000000007;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, 0, n) cin >> a[i];
  sort(a.begin(), a.end());
  // 長さを置く
  ll right = a[n - 1] + 1;
  ll left = -1;
  ll p = 0;
  if (k == 0) {
    cout << a[n - 1] << endl;
    return 0;
  }
  while (right - left > 1) {
    ll templength = (right + left) / 2;
    ll cnt = 0;
    rep(i, 0, n) {
      cnt += (a[i] + templength - 1) / templength - 1;
      if (cnt > k)
        break;
    }
    if (cnt > k) {
      // 切る回数がkより大きい＝templengthが小さすぎ
      left = templength;
    } else {
      // 切る回数がkより小さい＝templengthが大きすぎ
      right = templength;
    }
  }
  cout << right << endl;
  return 0;
  // cout << left << " " << right << endl;
}

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

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define rep(i, cc, n) for (int i = cc; i < n; ++i)
#define lrep(i, cc, n) for (long long i = cc; i < n; ++i)
#define sqrep(i, cc, n) for (long long i = cc; i * i <= n; ++i)
#define rrep(i, cc, n) for (long i = cc; i > n; --i)
#define pii pair<int, int>
#define pll pair<long long, long long>

using ll = long long;

const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
const double PI = 3.1415926535;
const ll inf = 1001001001;
const ll e9 = 1000000000;
const ll mod = 1000000007;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, 0, n) cin >> a[i];
  sort(a.begin(), a.end());
  // 長さを置く
  ll right = e9 + 1;
  ll left = 0;
  ll p = 0;
  if (k == 0) {
    cout << a[n - 1] << endl;
    return 0;
  }
  while (right - left > 1) {
    ll templength = (right + left) / 2;
    ll cnt = 0;
    rep(i, 0, n) {
      cnt += (a[i] + templength - 1) / templength - 1;
      if (cnt > k)
        break;
    }
    if (cnt > k) {
      // 切る回数がkより大きい＝templengthが小さすぎ
      left = templength;
    } else {
      // 切る回数がkより小さい＝templengthが大きすぎ
      right = templength;
    }
  }
  cout << right << endl;
  return 0;
  // cout << left << " " << right << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define endl '\n'
typedef pair<int, int> P;
typedef long long ll;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  ll r = 1e9;
  ll l = 0;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }

  rep(tmp, 200) {
    ll mid = (l + r) / 2;
    int cnt = 0;
    for (auto j : a) {
      cnt += (j + mid - 1) / mid - 1;
    }
    if (cnt <= k) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define endl '\n'
typedef pair<int, int> P;
typedef long long ll;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  ll r = 1e9;
  ll l = 0;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }

  rep(tmp, 200) {
    ll mid = (l + r) / 2;
    if (mid == 0) {
      cout << 1 << endl;
      return 0;
    }
    int cnt = 0;
    for (auto j : a) {
      cnt += (j + mid - 1) / mid - 1;
    }
    if (cnt <= k) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << endl;
}
Problem p02598 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define fo(i, n) for (ll i = 0; i < n; i++)
#define fo1(i, n) for (ll i = 1; i <= n; i++)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define loopr(i, a, b) for (ll i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define vll vector<ll>
#define vvl vector<vll>
#define vpll vector<pll>
#define pll pair<ll, ll>
#define F first
#define S second
#define MOD 1000000007
ll max(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll fexp(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % MOD;
    b /= 2;
    a = a * a % MOD;
  }
  return ans;
}
ll inverse(ll a, ll p) { return fexp(a, p - 2); }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll T = 1;
  // cin >> T;
  ll t = 0;
  while (t++ < T) {
    ll n, k;
    cin >> n >> k;
    vll a(n);
    fo(i, n) cin >> a[i];
    ll lo = 1, hi = *max_element(all(a)), ans;
    ll m;
    while (lo <= hi) {
      m = (lo + hi) / 2;
      ll req = 0;
      fo(i, n) {
        if (a[i] % m == 0)
          req += a[i] / m - 1;
        else
          req += a[i] / m;
      }
      if (req <= k) {
        hi = m;
      } else
        lo = m + 1;
    }
    cout << lo << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define fo(i, n) for (ll i = 0; i < n; i++)
#define fo1(i, n) for (ll i = 1; i <= n; i++)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define loopr(i, a, b) for (ll i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define vll vector<ll>
#define vvl vector<vll>
#define vpll vector<pll>
#define pll pair<ll, ll>
#define F first
#define S second
#define MOD 1000000007
ll max(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll fexp(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % MOD;
    b /= 2;
    a = a * a % MOD;
  }
  return ans;
}
ll inverse(ll a, ll p) { return fexp(a, p - 2); }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll T = 1;
  // cin >> T;
  ll t = 0;
  while (t++ < T) {
    ll n, k;
    cin >> n >> k;
    vll a(n);
    fo(i, n) cin >> a[i];
    ll lo = 1, hi = *max_element(all(a)), ans;
    ll m;
    while (lo < hi) {
      m = (lo + hi) / 2;
      ll req = 0;
      fo(i, n) {
        if (a[i] % m == 0)
          req += a[i] / m - 1;
        else
          req += a[i] / m;
      }
      if (req <= k) {
        hi = m;
      } else
        lo = m + 1;
    }
    cout << lo << endl;
  }
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, a, b) for (int i = a; i >= b; --i)
#define mem0(x) memset(x, 0, sizeof(x))
#define meminf(x) memset(x, 0x3f, sizeof(x))
#define VI vector<int>
#define VL vector<ll>
using namespace std;

double a[200005];
int n, k;

int check(double x) {
  ll cnt = 0;
  rep(i, 1, n) {
    cnt += (ll)(a[i] / x);
    if (fabs(a[i] - (ll)(a[i] / x) * x) <= 0.1) {
      --cnt;
    }
  }
  return cnt > k;
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> k;

  double l = 1, r = 0;
  rep(i, 1, n) {
    cin >> a[i];
    r = max(r, a[i]);
  }
  while (l <= r) {
    double mid = (l + r) / 2;
    if (check(mid)) {
      l = mid + 0.01;
    } else {
      r = mid;
    }
  }
  cout << (ll)ceil(l);
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, a, b) for (int i = a; i >= b; --i)
#define mem0(x) memset(x, 0, sizeof(x))
#define meminf(x) memset(x, 0x3f, sizeof(x))
#define VI vector<int>
#define VL vector<ll>
using namespace std;

double a[200005];
int n, k;

int check(double x) {
  ll cnt = 0;
  rep(i, 1, n) {
    cnt += (ll)(a[i] / x);
    if (fabs(a[i] - (ll)(a[i] / x) * x) <= 0.1) {
      --cnt;
    }
  }
  return cnt > k;
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> k;

  double l = 1, r = 0;
  rep(i, 1, n) {
    cin >> a[i];
    r = max(r, a[i]);
  }
  int cnt = 0;
  while (cnt++ < 100) {
    double mid = (l + r) / 2;
    if (check(mid)) {
      l = mid + 0.01;
    } else {
      r = mid;
    }
  }
  cout << (ll)ceil(l);
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define uu first
#define vv second
#define pii pair<int, int>
#define pdp pair<double, pii>
#define pll pair<ll, ll>
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
const int INF = 1e9;
const ll MOD = 1e9 + 7;

const int N = 1e6 + 6;

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

  int lo = 0, hi = INF, ret = -1;

  while (lo <= hi) {
    int mid = (lo + hi) / 2;

    int cnt = 0;

    for (auto x : vec) {
      cnt += (x - 1) / mid + 1 - 1;
    }

    if (cnt > k) {
      lo = mid + 1;
    } else {
      ret = mid;
      hi = mid - 1;
    }
  }
  cout << ret << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define uu first
#define vv second
#define pii pair<int, int>
#define pdp pair<double, pii>
#define pll pair<ll, ll>
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
const int INF = 1e9;
const ll MOD = 1e9 + 7;

const int N = 1e6 + 6;

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

  int lo = 1, hi = INF, ret = -1;

  while (lo <= hi) {
    int mid = (lo + hi) / 2;

    int cnt = 0;

    for (auto x : vec) {
      cnt += (x - 1) / mid + 1 - 1;
    }

    if (cnt > k) {
      lo = mid + 1;
    } else {
      ret = mid;
      hi = mid - 1;
    }
  }
  cout << ret << '\n';
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define INF 0x3f3f3f3f
#define MOD 1000000007

using namespace std;
typedef long long ll;

mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

template <typename T> void read(T &x) {
  x = 0;
  char c = getchar();
  T sig = 1;
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      sig = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 3) + (x << 1) + c - '0';
  x *= sig;
}

class Solution {
public:
  void solve() {
    int n, k;
    read(n), read(k);
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
      read(a[i]);
    ll l = 0, r = *max_element(a.begin(), a.end());
    while (l <= r) {
      ll mid = l + (r - l) / 2;
      ll req = 0;
      for (int i : a)
        req += (i - 1) / mid;
      if (req > k)
        l = mid + 1;
      else
        r = mid - 1;
    }
    printf("%lld", l);
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  Solution solution = Solution();
  solution.solve();
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define INF 0x3f3f3f3f
#define MOD 1000000007

using namespace std;
typedef long long ll;

mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

template <typename T> void read(T &x) {
  x = 0;
  char c = getchar();
  T sig = 1;
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      sig = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 3) + (x << 1) + c - '0';
  x *= sig;
}

class Solution {
public:
  void solve() {
    int n, k;
    read(n), read(k);
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
      read(a[i]);
    ll l = 1, r = *max_element(a.begin(), a.end());
    while (l <= r) {
      ll mid = l + (r - l) / 2;
      ll req = 0;
      for (int i : a)
        req += (i - 1) / mid;
      if (req > k)
        l = mid + 1;
      else
        r = mid - 1;
    }
    printf("%lld", l);
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  Solution solution = Solution();
  solution.solve();
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
/**
 * @author: Mackenzie
 **/

#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define F first
#define S second
#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define pb push_back
#define fastIO ios::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAX = 2e5 + 5;
int A[MAX];
int n, k;

int check(int x) {
  int cnt = 0;
  forn(i, n) {
    if (A[i] > x)
      cnt += A[i] / x;
  }
  return cnt;
}
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#else
  fastIO;
#endif
  cin >> n >> k;
  forn(i, n) cin >> A[i];
  sort(A, A + n);
  int lo = 0, hi = 1e9;
  forn(it, 70) {
    int m = (lo + hi) / 2;
    // cout << m << endl;
    if (check(m) <= k)
      hi = m;
    else
      lo = m;
  }
  cout << hi << endl;
  return 0;
}


Fixed submission:
/**
 * @author: Mackenzie
 **/

#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define F first
#define S second
#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define pb push_back
#define fastIO ios::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAX = 2e5 + 5;
int A[MAX];
int n, k;

int check(int x) {
  int cnt = 0;
  forn(i, n) {
    if (A[i] > x)
      cnt += A[i] / x;
  }
  return cnt;
}
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#else
  fastIO;
#endif
  cin >> n >> k;
  forn(i, n) cin >> A[i];
  sort(A, A + n);
  int lo = 1, hi = 1e9;
  forn(it, 70) {
    int m = (lo + hi) / 2;
    // cout << m << endl;
    if (check(m) <= k)
      hi = m;
    else
      lo = m;
  }
  cout << hi << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, x, n) for (int i = x; i <= n; i++)
#define per(i, n, x) for (int i = n; i >= x; i--)
#define sz(a) int(a.size())
#define rson mid + 1, r, p << 1 | 1
#define pii pair<int, int>
#define lson l, mid, p << 1
#define ll long long
#define pb push_back
#define mp make_pair
#define se second
#define fi first
using namespace std;
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
const int inf = 1e9;
int n, k;
int a[N];
bool ck(int x) {
  int cnt = 0;
  rep(i, 1, n) {
    int y = a[i];
    cnt += (y - 1) / x;
    if (cnt > k)
      return false;
  }
  return cnt <= k;
}
int main() {
  ios::sync_with_stdio(false);
  // freopen("in","r",stdin);
  cin >> n >> k;
  rep(i, 1, n) { cin >> a[i]; }
  int l = 0, r = 1e9;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (ck(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", l);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, x, n) for (int i = x; i <= n; i++)
#define per(i, n, x) for (int i = n; i >= x; i--)
#define sz(a) int(a.size())
#define rson mid + 1, r, p << 1 | 1
#define pii pair<int, int>
#define lson l, mid, p << 1
#define ll long long
#define pb push_back
#define mp make_pair
#define se second
#define fi first
using namespace std;
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
const int inf = 1e9;
int n, k;
int a[N];
bool ck(int x) {
  int cnt = 0;
  rep(i, 1, n) {
    int y = a[i];
    cnt += (y - 1) / x;
    if (cnt > k)
      return false;
  }
  return cnt <= k;
}
int main() {
  ios::sync_with_stdio(false);
  // freopen("in","r",stdin);
  cin >> n >> k;
  rep(i, 1, n) { cin >> a[i]; }
  int l = 1, r = 1e9;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (ck(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", l);
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout << x << endl;
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define lp(i, n) repi(i, 0, n)
#define repn(i, n) for (int i = n; i >= 0; i--)
#define int long long
#define endl "\n"

typedef pair<int, int> PII;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  int a[n];
  rep(i, n) cin >> a[i];
  sort(a, a + n);
  rep(i, n) {
    // cout<<a[i]<<" ";
  }
  // cout<<endl;
  int l = 0, r = 1000000000;
  int mid = (l + r) / 2;
  while (l != r) {
    int cnt = 0;
    rep(i, n) {
      // cout<<(a[i]+mid-1)/mid<<" ";
      cnt += (a[i] + mid - 1) / mid;
      cnt--;
    }
    // cout<<endl;
    // cout<<mid<<" "<<cnt<<endl;
    if (cnt > k)
      l = mid + 1;
    else
      r = mid;
    mid = (r + l) / 2;
  }
  cout << min(mid, a[n - 1]) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout << x << endl;
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define lp(i, n) repi(i, 0, n)
#define repn(i, n) for (int i = n; i >= 0; i--)
#define int long long
#define endl "\n"

typedef pair<int, int> PII;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  int a[n];
  rep(i, n) cin >> a[i];
  sort(a, a + n);
  rep(i, n) {
    // cout<<a[i]<<" ";
  }
  // cout<<endl;
  int l = 1, r = 1000000000;
  int mid = (l + r) / 2;
  while (l != r) {
    int cnt = 0;
    rep(i, n) {
      // cout<<(a[i]+mid-1)/mid<<" ";
      cnt += (a[i] + mid - 1) / mid;
      cnt--;
    }
    // cout<<endl;
    // cout<<mid<<" "<<cnt<<endl;
    if (cnt > k)
      l = mid + 1;
    else
      r = mid;
    mid = (r + l) / 2;
  }
  cout << min(mid, a[n - 1]) << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

bool is_possible(int mid, vector<int> &v, int K) {
  int ret = 0;
  for (auto i : v) {
    ret += (i % mid == 0 ? i / mid - 1 : i / mid);
  }
  return (ret <= K);
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> v(N);
  for (auto &i : v)
    cin >> i;

  int left = 0, right = 1e9 + 2, ret = -1;
  while (left <= right) {
    int mid = (left + right) / 2;
    if (is_possible(mid, v, K)) {
      ret = mid;
      right = mid - 1;
    } else
      left = mid + 1;
  }
  cout << ret << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

bool is_possible(int mid, vector<int> &v, int K) {
  int ret = 0;
  for (auto i : v) {
    ret += (i % mid == 0 ? i / mid - 1 : i / mid);
  }
  return (ret <= K);
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> v(N);
  for (auto &i : v)
    cin >> i;

  int left = 1, right = 1e9 + 2, ret = -1;
  while (left <= right) {
    int mid = (left + right) / 2;
    if (is_possible(mid, v, K)) {
      ret = mid;
      right = mid - 1;
    } else
      left = mid + 1;
  }
  cout << ret << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
//-----------------------
// author : xyqkoala
// time : 2020-08-03
//-----------------------

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

typedef long long LL;
const int MOD = 1e9 + 7;

void solve();

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}

LL check(vector<int> &nums, int m, int k) {
  LL res = 0;
  for (auto item : nums) {
    int cnt = item / m;
    if (item % m)
      cnt++;
    res += 1ll * (cnt - 1);
    if (res > k)
      return false;
  }
  return res <= k;
}

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> nums(n);
  for (int i = 0; i < n; i++) {
    cin >> nums[i];
  }
  int l = 0, r = MOD;
  while (l < r) {
    int m = (r - l) / 2 + l;
    if (check(nums, m, k))
      r = m;
    else
      l = m + 1;
  }
  cout << l << endl;
}


Fixed submission:
//-----------------------
// author : xyqkoala
// time : 2020-08-03
//-----------------------

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

typedef long long LL;
const int MOD = 1e9 + 7;

void solve();

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}

LL check(vector<int> &nums, int m, int k) {
  LL res = 0;
  for (auto item : nums) {
    int cnt = item / m;
    if (item % m)
      cnt++;
    res += 1ll * (cnt - 1);
    if (res > k)
      return false;
  }
  return res <= k;
}

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> nums(n);
  for (int i = 0; i < n; i++) {
    cin >> nums[i];
  }
  int l = 1, r = MOD;
  while (l < r) {
    int m = (r - l) / 2 + l;
    if (check(nums, m, k))
      r = m;
    else
      l = m + 1;
  }
  cout << l << endl;
}