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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
const ll MOD = 1000000007;
const long double PI = 3.14159265358979;
const ll MAX = 0;

int main() {
  ll N, K;
  cin >> N >> K;
  ll a[N];
  rep(i, N) { cin >> a[i]; }
  ll l = 1, r = 1000000000;
  while (r - l > 10000) {
    ll mid = (l + r) / 2;
    ll k = 0;
    rep(i, N) { k += (a[i] - 1) / mid; }
    if (k > K) {
      l = mid;
    } else {
      r = mid;
    }
  }
  ll ans = l;
  while (1) {
    ll k = 0;
    rep(i, N) { k += (a[i] - 1) / ans; }
    if (k > K) {
      ans++;
    } else {
      break;
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
const ll MOD = 1000000007;
const long double PI = 3.14159265358979;
const ll MAX = 0;

int main() {
  ll N, K;
  cin >> N >> K;
  ll a[N];
  rep(i, N) { cin >> a[i]; }
  ll l = 1, r = 1000000000;
  while (r - l > 10) {
    ll mid = (l + r) / 2;
    ll k = 0;
    rep(i, N) { k += (a[i] - 1) / mid; }
    if (k > K) {
      l = mid;
    } else {
      r = mid;
    }
  }
  ll ans = l;
  while (1) {
    ll k = 0;
    rep(i, N) { k += (a[i] - 1) / ans; }
    if (k > K) {
      ans++;
    } else {
      break;
    }
  }
  cout << ans;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int n, k;
int a[200200];

bool check(int now) {
  int cnt = 0;
  rep(i, n) { cnt += ((a[i] - 1) / now); }
  return cnt <= k;
}

int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  int lb = 0, ub = 1000000000;
  while (lb <= ub) {
    int mid = (lb + ub) >> 1;
    if (check(mid))
      ub = mid - 1;
    else
      lb = mid + 1;
  }
  cout << lb << endl;
  return 0;
}

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

int n, k;
int a[200200];

bool check(int now) {
  if (now == 0)
    return 0;
  int cnt = 0;
  rep(i, n) { cnt += ((a[i] - 1) / now); }
  return cnt <= k;
}

int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  int lb = 0, ub = 1000000000;
  while (lb <= ub) {
    int mid = (lb + ub) >> 1;
    if (check(mid))
      ub = mid - 1;
    else
      lb = mid + 1;
  }
  cout << lb << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
#define ff first
#define endl "\n"
#define ss second
#define all(v) v.begin(), v.end()
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int power(int x, unsigned int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res % p;
}
void solve() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int lo = 0, hi = 1e9;
  int ans = 0;
  while (lo <= hi) {
    int mid = lo + hi;
    mid /= 2;
    int re = 0;
    for (int i = 0; i < n; i++) {
      int f = a[i] + mid - 1;
      f /= mid;
      f--;
      re += f;
    }
    if (re <= k) {
      ans = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }
  cout << ans << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tc;
  tc = 1;
  // cin >> tc;
  while (tc--) {
    solve();
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
#define ff first
#define endl "\n"
#define ss second
#define all(v) v.begin(), v.end()
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int power(int x, unsigned int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res % p;
}
void solve() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int lo = 1, hi = 1e9;
  int ans = 0;
  while (lo <= hi) {
    int mid = lo + hi;
    mid /= 2;
    int re = 0;
    for (int i = 0; i < n; i++) {
      int f = a[i] + mid - 1;
      f /= mid;
      f--;
      re += f;
    }
    if (re <= k) {
      ans = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }
  cout << ans << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tc;
  tc = 1;
  // cin >> tc;
  while (tc--) {
    solve();
  }

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define nl '\n'
#define dbg(a) cout << #a << "=" << a << endl
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ll long long
#define vl vector<ll>
#define pll pair<ll, ll>
#define ii pair<ll, ll>
#define vi vector<ll>
#define MX 600000
#define MXX 1000000000
using namespace std;

vl v;

ll cut(ll x) {
  ll ct = 0;
  for (int i = 0; i < v.size(); i++) {
    ct += (v[i] + x - 1) / x;
    ct--;
    // dbg(ct);
  }
  // dbg(ct);
  return ct;
}

void solve() {
  ll n, k;
  cin >> n >> k;
  v.clear();
  v.resize(n);
  // dbg(n);
  for (auto &it : v)
    cin >> it;
  sort(v.begin(), v.end());
  ll l = 0, r = v.back();
  while (l < r) {
    ll m = (l + r) / 2;
    // dbg(m);
    // dbg(cut(m));
    if (cut(m) <= k) {
      r = m;
    } else
      l = m + 1;
  }
  cout << l << endl;
}

int main() {
  fast;
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define nl '\n'
#define dbg(a) cout << #a << "=" << a << endl
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ll long long
#define vl vector<ll>
#define pll pair<ll, ll>
#define ii pair<ll, ll>
#define vi vector<ll>
#define MX 600000
#define MXX 1000000000
using namespace std;

vl v;

ll cut(ll x) {
  ll ct = 0;
  for (int i = 0; i < v.size(); i++) {
    ct += (v[i] + x - 1) / x;
    ct--;
    // dbg(ct);
  }
  // dbg(ct);
  return ct;
}

void solve() {
  ll n, k;
  cin >> n >> k;
  v.clear();
  v.resize(n);
  // dbg(n);
  for (auto &it : v)
    cin >> it;
  sort(v.begin(), v.end());
  ll l = 1, r = v.back();
  while (l < r) {
    ll m = (l + r) / 2;
    // dbg(m);
    // dbg(cut(m));
    if (cut(m) <= k) {
      r = m;
    } else
      l = m + 1;
  }
  cout << l << endl;
}

int main() {
  fast;
  solve();
  return 0;
}
Problem p02598 (C++)

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

#define ll long long

bool kmax(vector<ll> &v, ll log, ll k) {
  ll sum = 0;
  for (int i = 0; i < v.size(); i++)
    sum += (v[i] - 1) / log;
  return sum <= k;
}
ll bs(vector<ll> &v, ll low, ll high, ll k) {
  if (low < high) {
    ll mid = (high + low) / 2;
    if (kmax(v, mid, k))
      return bs(v, low, mid, k);
    else
      return bs(v, mid + 1, high, k);
  }
  return low;
}
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n);
  ll max_val = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    if (max_val < v[i])
      max_val = v[i];
  }
  cout << bs(v, 0, max_val, k) << endl;
  return 0;
}

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

#define ll long long

bool kmax(vector<ll> &v, ll log, ll k) {
  ll sum = 0;
  for (int i = 0; i < v.size(); i++)
    sum += (v[i] - 1) / log;
  return sum <= k;
}
ll bs(vector<ll> &v, ll low, ll high, ll k) {
  if (low < high) {
    ll mid = (high + low) / 2;
    if (kmax(v, mid, k))
      return bs(v, low, mid, k);
    else
      return bs(v, mid + 1, high, k);
  }
  return low;
}
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n);
  ll max_val = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    if (max_val < v[i])
      max_val = v[i];
  }
  cout << bs(v, 1, max_val, k) << endl;
  return 0;
}
Problem p02598 (C++)

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

const int MAXN = 200005;

int n, k, a[MAXN];

bool can(int L) {
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    sum += (a[i] - 1) / L;
  }

  if (sum > k)
    return false;
  else
    return true;
}

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

  int L = 0, R = 1e9 + 50;
  while (L < R) {
    int mid = (L + R) / 2;
    if (can(mid))
      R = mid;
    else
      L = mid + 1;
  }
  printf("%d\n", L);

  return 0;
}


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

const int MAXN = 200005;

int n, k, a[MAXN];

bool can(int L) {
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    sum += (a[i] - 1) / L;
  }

  if (sum > k)
    return false;
  else
    return true;
}

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

  int L = 1, R = 1e9 + 50;
  while (L < R) {
    int mid = (L + R) / 2;
    if (can(mid))
      R = mid;
    else
      L = mid + 1;
  }
  printf("%d\n", L);

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;

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

  long long left = -1, right = 1000000000;
  while (right - left > 1) {
    long long mid = (left + right) / 2;
    long long count = 0;
    for (long long i = 0; i < N; i++) {
      count += A[i] / mid;
      if (A[i] % mid == 0)
        count--;
      if (count > K)
        break;
    }
    if (count <= K)
      right = mid;
    else
      left = mid;
  }

  cout << right << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

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

  long long left = -1, right = 1000000000;
  while (right - left > 1) {
    long long mid = (left + right) / 2;
    long long count = 0;
    for (long long i = 0; i < N; i++) {
      if (mid == 0) {
        count = 100000000000;
        break;
      }
      count += A[i] / mid;
      if (A[i] % mid == 0)
        count--;
      if (count > K)
        break;
    }
    if (count <= K)
      right = mid;
    else
      left = mid;
  }

  cout << right << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MSET(x, y) memset(x, y, sizeof(x))
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define SZ size()
#define M 100005
#define ll long long
#define ld long double
#define INF 1e18
#define endl "\n"
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
ll MOD = 1000000007;
string yes = "Yes\n";
string no = "No\n";

vector<ll> a;
ll k;

bool f(ll mid) {
  ll ans = 0;
  for (auto itr : a) {
    ans += (itr - 1) / mid;
  }
  if (ans > k)
    return true;
  else
    return false;
}

int32_t main() {
  fastio;
#ifndef ONLINE_JUDGE
  freopen("i.txt", "r", stdin);
  freopen("o.txt", "w", stdout);
#endif
  ll n;
  cin >> n >> k;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    a.PB(x);
  }
  sort(a.begin(), a.end());
  ll lo = 1, hi = 1e9, ans = -1, mid;
  while (lo <= hi) {
    mid = (lo + hi) / 2;
    if (f(mid))
      ans = mid, lo = mid + 1;
    else
      hi = mid - 1;
  }
  if (ans == -1) {
    ll a = 0;
    cout << 2 / a;
  }
  cout << ans + 1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define MSET(x, y) memset(x, y, sizeof(x))
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define SZ size()
#define M 100005
#define ll long long
#define ld long double
#define INF 1e18
#define endl "\n"
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
ll MOD = 1000000007;
string yes = "Yes\n";
string no = "No\n";

vector<ll> a;
ll k;

bool f(ll mid) {
  ll ans = 0;
  for (auto itr : a) {
    ans += (itr - 1) / mid;
  }
  if (ans > k)
    return true;
  else
    return false;
}

int32_t main() {
  fastio;
#ifndef ONLINE_JUDGE
  freopen("i.txt", "r", stdin);
  freopen("o.txt", "w", stdout);
#endif
  ll n;
  cin >> n >> k;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    a.PB(x);
  }
  sort(a.begin(), a.end());
  ll lo = 1, hi = 1e9, ans = -1, mid;
  while (lo <= hi) {
    mid = (lo + hi) / 2;
    if (f(mid))
      ans = mid, lo = mid + 1;
    else
      hi = mid - 1;
  }
  if (ans == -1) {
    cout << 1 << endl;
    return 0;
  }
  cout << ans + 1 << endl;
}
Problem p02598 (C++)

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

typedef long long ll;
typedef pair<int, int> P;

struct edge {
  int to;
  ll cost;
};

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};

ll GCD(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}

ll LCM(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return a / GCD(a, b) * b;
}

int main() {
  int n;
  ll k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll lo = -1, hi = 1000000005;
  while (hi - lo > 1) {
    ll mid = (lo + hi) / 2;
    ll tmp = 0;
    for (int i = 0; i < n; i++) {
      tmp += a[i] / mid - 1;
      if (a[i] % mid)
        tmp++;
    }
    if (tmp > k)
      lo = mid;
    else
      hi = mid;
  }
  cout << hi << endl;
  return 0;
}


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

typedef long long ll;
typedef pair<int, int> P;

struct edge {
  int to;
  ll cost;
};

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};

ll GCD(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}

ll LCM(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return a / GCD(a, b) * b;
}

int main() {
  int n;
  ll k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll lo = 0, hi = 1000000005;
  while (hi - lo > 1) {
    ll mid = (lo + hi) / 2;
    ll tmp = 0;
    for (int i = 0; i < n; i++) {
      tmp += a[i] / mid - 1;
      if (a[i] % mid)
        tmp++;
    }
    if (tmp > k)
      lo = mid;
    else
      hi = mid;
  }
  cout << hi << endl;
  return 0;
}
Problem p02598 (C++)

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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"

const int N = 1e5 + 5;

int n, k;
int a[N];

bool check(int x) {
  int reqd = 0;
  for (int i = 0; i + 1 <= n; i++) {
    int diff = a[i];
    reqd += (diff - 1) / x;
    if (reqd > k)
      return 0;
  }
  return 1;
}

int binsearch(int lo, int hi) {
  while (lo < hi) {
    int mid = (lo + hi) / 2;
    if (mid == 0) {
      return 0;
    }
    if (check(mid))
      hi = mid;
    else
      lo = mid + 1;
  }
  return lo;
}

int main() {
  IOS;
  int t = 1;
  int tc = 0;
  while (t--) {
    tc++;
    cin >> n >> k;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    int ans = binsearch(1, 1e9);
    cout << ans;
  }
  return 0;
}

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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"

const int N = 200001;

int n, k;
int a[N];

bool check(int x) {
  int reqd = 0;
  for (int i = 0; i + 1 <= n; i++) {
    int diff = a[i];
    reqd += (diff - 1) / x;
    if (reqd > k)
      return 0;
  }
  return 1;
}

int binsearch(int lo, int hi) {
  while (lo < hi) {
    int mid = (lo + hi) / 2;
    if (mid == 0) {
      return 0;
    }
    if (check(mid))
      hi = mid;
    else
      lo = mid + 1;
  }
  return lo;
}

int main() {
  IOS;
  int t = 1;
  int tc = 0;
  while (t--) {
    tc++;
    cin >> n >> k;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    int ans = binsearch(1, 1e9);
    cout << ans;
  }
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for (ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
/*-------------------------------------------*/

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

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

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

  cin >> n >> k;
  rep(i, n) cin >> a[i];

  ll ng = 0, ok = 1e9 + 1;
  while (ng + 1 < ok)
    (f(ng + ok >> 1) ? ok : ng) = ng + ok >> 1;

  cout << ok << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for (ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
/*-------------------------------------------*/

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

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

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

  cin >> n >> k;
  rep(i, n) cin >> a[i];

  ll ng = 0, ok = 1e9 + 1;
  while (ng + 1 < ok)
    (f(ng + ok >> 1) ? ok : ng) = ng + ok >> 1;

  cout << ok << endl;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
#define sz 200009
ll n, m, i, j, k, l, t, arr[sz], brr[sz], crr[sz], ans, hi, lo, mi;
char c[sz];
int main() {
  scanf("%lld %lld", &n, &k);
  for (i = 1; i <= n; i++)
    scanf("%lld", &arr[i]), hi = max(hi, arr[i]);
  ans = hi;
  while (hi >= lo) {
    mi = (hi + lo) / 2;
    j = 0;
    for (i = 1; i <= n; i++) {
      j += arr[i] / mi;
      if (arr[i] % mi)
        j++;
      j--;
    }
    if (j <= k) {
      ans = min(ans, mi);
      hi = mi - 1;
    } else
      lo = mi + 1;
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
#define sz 200009
ll n, m, i, j, k, l, t, arr[sz], brr[sz], crr[sz], ans, hi, lo, mi;
char c[sz];
int main() {
  scanf("%lld %lld", &n, &k);
  for (i = 1; i <= n; i++)
    scanf("%lld", &arr[i]), hi = max(hi, arr[i]);
  ans = hi;
  lo = 1;
  while (hi >= lo) {
    mi = (hi + lo) / 2;
    j = 0;
    for (i = 1; i <= n; i++) {
      j += arr[i] / mi;
      if (arr[i] % mi)
        j++;
      j--;
    }
    if (j <= k) {
      ans = min(ans, mi);
      hi = mi - 1;
    } else
      lo = mi + 1;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Time Limit Exceeded):

#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll N, K;
  cin >> N >> K;

  vll v(N);
  REP(i, N) cin >> v[i];

  ll ng = 0, ok = INF;
  while (ok - ng > 1) {
    ll mid = (ng + ok) / 2;
    ll cnt = 0;
    REP(i, N) {
      ll c = v[i];
      while (c > mid) {
        c -= mid;
        cnt++;
      }
    }

    if (cnt <= K)
      ok = mid;
    else
      ng = mid;
  }

  print(ok);
}


Fixed submission:

#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll N, K;
  cin >> N >> K;

  vll v(N);
  REP(i, N) cin >> v[i];

  ll ng = 0, ok = INF;
  while (ok - ng > 1) {
    ll mid = (ng + ok) / 2;
    ll cnt = 0;
    REP(i, N) {
      ll c = v[i];
      if (mid >= c)
        continue;
      cnt += c / mid;
    }

    if (cnt <= K)
      ok = mid;
    else
      ng = mid;
  }

  print(ok);
}
Problem p02598 (C++)

Buggy submission (status: Time Limit Exceeded):
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

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

int main() {

  long long int N, K, tmp, high, mid, low, res = 0;
  priority_queue<long long int> A;

  cin >> N >> K;

  rep(i, N) {
    cin >> tmp;
    A.push(tmp);
  }

  high = A.top() + 1;
  low = 0;

  while (high > low) {
    mid = (high + low) / 2 + 1;
    priority_queue<long long int> tmp_que = A;
    long long int count = 0;
    rep(i, N) {
      long long int tmp = tmp_que.top();
      if (tmp <= mid) {
        break;
      }
      if (tmp % mid == 0) {
        count += (tmp / mid - 1);
      } else {
        count += tmp / mid;
      }
      tmp_que.pop();
    }
    if (count <= K) {
      res = mid;
      high = mid;
    } else {
      low = mid + 1;
    }
  }

  cout << res;

  return 0;
}

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

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

int main() {

  long long int N, K, tmp, high, mid, low, res = 0;
  priority_queue<long long int> A;

  cin >> N >> K;

  rep(i, N) {
    cin >> tmp;
    A.push(tmp);
  }

  high = A.top() + 1;
  low = 0;

  while (high > low) {
    mid = (high + low) / 2;
    if (mid == 0) {
      break;
    }
    priority_queue<long long int> tmp_que = A;
    long long int count = 0;
    rep(i, N) {
      long long int tmp = tmp_que.top();
      if (tmp <= mid) {
        break;
      }
      if (tmp % mid == 0) {
        count += (tmp / mid - 1);
      } else {
        count += tmp / mid;
      }
      tmp_que.pop();
    }
    if (count <= K) {
      res = mid;
      high = mid;
    } else {
      low = mid + 1;
    }
  }

  cout << res;

  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;
    }
  }
  bool test = possible(lb, A, K);
  if (test) {
    cout << "";
  }
  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;
    }
  }
  bool test = possible(lb, A, K);
  if (test) {
    test != test;
  }
  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;
typedef long long ll;
ll n, k, a[200500];

bool check(ll x) {
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += a[i] / x - (a[i] % x == 0);
  }
  return cnt <= k;
}

void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll l = -1, r = 1e9;
  while (r - l > 1) {
    ll mid = l + (r - l) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r;
}

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, k, a[200500];

bool check(ll x) {
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += a[i] / x - (a[i] % x == 0);
  }
  return cnt <= k;
}

void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll l = 0, r = 1e9;
  while (r - l > 1) {
    ll mid = l + (r - l) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r;
}

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
using namespace std;

#include <climits>
#include <iomanip>
#include <math.h>
#include <queue>
#include <vector>
using Graph = vector<vector<int>>;
const double PI = 3.14159265358979323846;

int needed_kaisu(long long a, long long N, long long *A) {
  int count = 0;
  for (long long i = 0; i < N; i++) {
    count += (A[i] + a - 1) / a - 1;
  }
  return count;
}

int main() {

  long long N, K, A[200100], ans, mem[200100];
  priority_queue<pair<long long, long long>> queue;

  long long max = 0;

  cin >> N >> K;
  for (long long i = 0; i < N; i++) {
    cin >> A[i];
    if (max < A[i]) {
      max = A[i];
    }
  }

  long long lb = 0;
  long long ub = max;

  if (K == 0) {
    cout << max << endl;
    return 0;
  }

  while (1) {
    long long tmp = (lb + ub) / 2;
    if (needed_kaisu(tmp, N, A) <= K) {
      ub = tmp;
    }
    if (needed_kaisu(tmp, N, A) >= K + 1) {
      lb = tmp;
    }
    if (ub <= lb + 1) {
      break;
    }
  }

  cout << ub << endl;

  cin >> N;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
using namespace std;

#include <climits>
#include <iomanip>
#include <math.h>
#include <queue>
#include <vector>
using Graph = vector<vector<int>>;
const double PI = 3.14159265358979323846;

int needed_kaisu(long long a, long long N, long long *A) {
  int count = 0;
  for (long long i = 0; i < N; i++) {
    count += (A[i] + a - 1) / a - 1;
  }
  return count;
}

int main() {

  long long N, K, A[200100], ans, mem[200100];
  priority_queue<pair<long long, long long>> queue;

  long long max = 0;

  cin >> N >> K;
  for (long long i = 0; i < N; i++) {
    cin >> A[i];
    if (max < A[i]) {
      max = A[i];
    }
  }

  long long lb = 0;
  long long ub = max;

  if (K == 0) {
    cout << max << endl;
    return 0;
  }

  while (1) {
    long long tmp = (lb + ub + 1) / 2;
    if (needed_kaisu(tmp, N, A) <= K) {
      ub = tmp;
    }
    if (needed_kaisu(tmp, N, A) >= K + 1) {
      lb = tmp;
    }
    if (ub <= lb + 1) {
      break;
    }
  }

  cout << ub << endl;

  cin >> N;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

vector<ll> a(200000);

int main(void) {
  ll n, k;
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  ll l = 0, h = *max_element(a.begin(), a.begin() + n);
  while (l < h) {
    ll mid = (l + h) / 2, cnt = 0;
    rep(i, n) {
      cnt += a[i] / mid;
      if (a[i] % mid == 0)
        --cnt;
    }
    if (cnt <= k)
      h = mid;
    else
      l = mid + 1;
  }
  cout << h << endl;
  return 0;
}

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

vector<ll> a(200000);

int main(void) {
  ll n, k;
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  ll l = 1, h = *max_element(a.begin(), a.begin() + n);
  while (l < h) {
    ll mid = (l + h) / 2, cnt = 0;
    rep(i, n) {
      cnt += a[i] / mid;
      if (a[i] % mid == 0)
        --cnt;
    }
    if (cnt <= k)
      h = mid;
    else
      l = mid + 1;
  }
  cout << h << endl;
  return 0;
}
Problem p02598 (C++)

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

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;

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

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

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, (n)-1, 0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-10;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> A(n);
  rep(i, n) cin >> A[i];
  auto bsearch = [&]() {
    auto test = [&](ll t) {
      ll need = 0;
      rep(i, n) need += (A[i] + t - 1) / t - 1;
      return need <= k;
    };
    ll ok = *max_element(all(A)), ng = -1;
    while (ok - ng > 1) {
      ll mid = (ok + ng) / 2;
      (test(mid) ? ok : ng) = mid;
    }
    return ok;
  };
  cout << bsearch() << endk;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, (n)-1, 0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-10;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> A(n);
  rep(i, n) cin >> A[i];
  auto bsearch = [&]() {
    auto test = [&](ll t) {
      ll need = 0;
      rep(i, n) need += (A[i] + t - 1) / t - 1;
      return need <= k;
    };
    ll ok = *max_element(all(A)), ng = 0;
    while (ok - ng > 1) {
      ll mid = (ok + ng) / 2;
      (test(mid) ? ok : ng) = mid;
    }
    return ok;
  };
  cout << bsearch() << endk;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, k;
double mid, Left, Right, len[200005];
const double eps = 1e-8;
bool Judge(double lim) {
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    cnt += ceil(len[i] / lim) - 1;
  return cnt <= k;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lf", &len[i]), Right = max(Right, len[i]);
  while (abs(Left - Right) > eps) {
    mid = (Left + Right) / 2.0;
    if (Judge(mid))
      Right = mid;
    else
      Left = mid;
  }
  printf("%.0lf\n", ceil(Right));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, k;
double mid, Left, Right, len[200005];
const double eps = 1e-6;
bool Judge(double lim) {
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    cnt += ceil(len[i] / lim) - 1;
  return cnt <= k;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lf", &len[i]), Right = max(Right, len[i]);
  while (abs(Left - Right) > eps) {
    mid = (Left + Right) / 2.0;
    if (Judge(mid))
      Right = mid;
    else
      Left = mid;
  }
  printf("%.0lf\n", ceil(Right));
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, k, mid, Left, Right, len[200005];
bool Judge(int lim) {
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (len[i] % lim == 0)
      cnt += len[i] / lim - 1;
    else
      cnt += len[i] / lim;
  }
  return cnt > k;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &len[i]), Right = max(Right, len[i]);
  while (Left <= Right) {
    mid = (Left + Right) / 2;
    if (Judge(mid))
      Left = mid + 1;
    else
      Right = mid - 1;
  }
  printf("%d\n", Left);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, k, mid, Left, Right, len[200005];
bool Judge(int lim) {
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (len[i] % lim == 0)
      cnt += len[i] / lim - 1;
    else
      cnt += len[i] / lim;
  }
  return cnt > k;
}
int main() {
  scanf("%d%d", &n, &k), Left = 1;
  for (int i = 1; i <= n; i++)
    scanf("%d", &len[i]), Right = max(Right, len[i]);
  while (Left <= Right) {
    mid = (Left + Right) / 2;
    if (Judge(mid))
      Left = mid + 1;
    else
      Right = mid - 1;
  }
  printf("%d\n", Left);
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
long long int lis[200003];
int main() {
  long long int N, K;
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> lis[i];
  long long int l = 0, r = 2100000000, mid;
  for (int i = 0; i < 100; i++) {
    mid = (l + r) / 2;
    long long int cou = 0;
    for (int i = 0; i < N; i++) {
      cou += (lis[i] - 1) / mid;
    }
    if (cou <= K)
      r = mid;
    else
      l = mid;
    if (mid == 0) {
      cout << 1 << endl;
      return 0;
    }
  }
  cout << r << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
long long int lis[200003];
int main() {
  long long int N, K;
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> lis[i];
  long long int l = 0, r = 2100000000, mid;
  for (int i = 0; i < 100; i++) {
    mid = (l + r) / 2;
    if (mid == 0) {
      cout << 1 << endl;
      return 0;
    }
    long long int cou = 0;
    for (int i = 0; i < N; i++) {
      cou += (lis[i] - 1) / mid;
    }
    if (cou <= K)
      r = mid;
    else
      l = mid;
    if (mid == 0) {
      cout << 1 << endl;
      return 0;
    }
  }
  cout << r << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int cut(int k, int *a, int n, int u) {
  int res = 0;
  for (int i = 0; i < n; ++i) {
    res += (a[i] + k - 1) / k - 1;
  }
  // cout<<k<<" "<<res<<endl;
  return (res <= u) ? 1 : 0;
}

signed main() {
  int n, k, a[200200] = {0};
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  int l = -1, r = 1000000001, m;
  while (l < r - 1) {
    m = l + (r - l) / 2;
    if (cut(m, a, n, k)) {
      r = m;
    } else {
      l = m;
    }
  }

  cout << r << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int cut(int k, int *a, int n, int u) {
  int res = 0;
  if (!k)
    return 0;
  for (int i = 0; i < n; ++i) {
    res += (a[i] + k - 1) / k - 1;
  }
  // cout<<k<<" "<<res<<endl;
  return (res <= u) ? 1 : 0;
}

signed main() {
  int n, k, a[200200] = {0};
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  int l = -1, r = 1000000001, m;
  while (l < r - 1) {
    m = l + (r - l) / 2;
    if (cut(m, a, n, k)) {
      r = m;
    } else {
      l = m;
    }
  }

  cout << r << endl;

  return 0;
}
Problem p02598 (C++)

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

ll n, k;

bool isOK(vector<ll> &a, ll mid) {
  ll l = 0;
  rep(i, 0, n) {
    if (a[i] % mid == 0) {
      l += a[i] / mid - 1;
    } else {
      l += a[i] / mid;
    }
  }
  if (l <= k)
    return true;
  else
    return false;
}

ll binary_search(vector<ll> &a, ll m) {
  ll left = -1;
  ll right = m + 1;

  while (right - left > 1) {
    ll mid = (right + left) / 2;
    if (isOK(a, mid))
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main() {
  cin >> n >> k;
  vector<ll> a(n);
  ll m = 0;
  rep(i, 0, n) {
    cin >> a[i];
    m = max(m, a[i]);
  }
  sort(a.begin(), a.end());
  int ans = binary_search(a, m);
  cout << ans << endl;
  return 0;
}


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

ll n, k;

bool isOK(vector<ll> &a, ll mid) {
  ll l = 0;
  rep(i, 0, n) {
    if (a[i] % mid == 0) {
      l += a[i] / mid - 1;
    } else {
      l += a[i] / mid;
    }
  }
  if (l <= k)
    return true;
  else
    return false;
}

ll binary_search(vector<ll> &a, ll m) {
  ll left = 0;
  ll right = m;

  while (right - left > 1) {
    ll mid = (right + left) / 2;
    if (isOK(a, mid))
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main() {
  cin >> n >> k;
  vector<ll> a(n);
  ll m = 0;
  rep(i, 0, n) {
    cin >> a[i];
    m = max(m, a[i]);
  }
  sort(a.begin(), a.end());
  int ans = binary_search(a, m);
  cout << ans << endl;
  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 vector<ll> vl;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;
typedef vector<vpll> vvpll;
typedef vector<vpii> vvpii;

#define pb push_back
#define eb emplace_back
#define pf push_front
#define all(c) (c).begin(), (c).end()
#define ff first
#define ss second
#define mp(x, y) make_pair((x), (y))
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

int f(int len, int goal) { return len / goal - (len % goal == 0); }

void solve() {

  int n, k;
  cin >> n >> k;
  int a[n];
  for (int &u : a) {
    cin >> u;
  }
  int lo = 0, hi = 1e9;
  int ans;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    int cnt = 0;
    for (int u : a) {
      cnt += f(u, mid);
    }
    if (cnt <= k) {
      ans = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }
  cout << ans << '\n';
}

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

  boost;

  int t = 1;
  // cin >> t;

  for (int i = 1; i <= t; i++) {
    // cout << "Case #" << i << ": ";

    solve();
  }
  return 0;
}

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

typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;
typedef vector<vpll> vvpll;
typedef vector<vpii> vvpii;

#define pb push_back
#define eb emplace_back
#define pf push_front
#define all(c) (c).begin(), (c).end()
#define ff first
#define ss second
#define mp(x, y) make_pair((x), (y))
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

int f(int len, int goal) { return len / goal - (len % goal == 0); }

void solve() {

  int n, k;
  cin >> n >> k;
  int a[n];
  for (int &u : a) {
    cin >> u;
  }
  int lo = 1, hi = 1e9;
  int ans;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    int cnt = 0;
    for (int u : a) {
      cnt += f(u, mid);
    }
    if (cnt <= k) {
      ans = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }
  cout << ans << '\n';
}

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

  boost;

  int t = 1;
  // cin >> t;

  for (int i = 1; i <= t; i++) {
    // cout << "Case #" << i << ": ";

    solve();
  }
  return 0;
}
Problem p02598 (C++)

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

using namespace std;
using ll = long long;
using ld = long double;

void solve() {
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);

  ll ma = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ma = max(ma, a[i]);
  }

  ll left = -1;
  ll right = ma + 1;

  while (right - left > 1) {
    const ll mid = (left + right) / 2;

    ll sum = 0;
    for (int i = 0; i < n; i++) {
      sum += (a[i] + mid - 1) / mid - 1;
    }

    if (sum <= k)
      right = mid;
    else
      left = mid;
  }

  cout << right << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);

  solve();
  return 0;
}


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

using namespace std;
using ll = long long;
using ld = long double;

void solve() {
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);

  ll ma = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ma = max(ma, a[i]);
  }

  ll left = 0;
  ll right = ma + 1;

  while (right - left > 1) {
    const ll mid = (left + right) / 2;

    ll sum = 0;
    for (int i = 0; i < n; i++) {
      sum += (a[i] + mid - 1) / mid - 1;
    }

    if (sum <= k)
      right = mid;
    else
      left = mid;
  }

  cout << right << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);

  solve();
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int &x : a) {
    cin >> x;
  }
  int l = 0, r = *max_element(a.begin(), a.end());
  while (l < r) {
    int64_t m = (l + r) >> 1;
    int64_t tmp = 0;
    for (int x : a) {
      tmp += (x + m - 1) / m - 1;
    }
    if (tmp > k) {
      l = m + 1;
    } else {
      r = m;
    }
  }
  cout << r << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int &x : a) {
    cin >> x;
  }
  int l = 1, r = *max_element(a.begin(), a.end());
  while (l < r) {
    int64_t m = (l + r) >> 1;
    int64_t tmp = 0;
    for (int x : a) {
      tmp += (x + m - 1) / m - 1;
    }
    if (tmp > k) {
      l = m + 1;
    } else {
      r = m;
    }
  }
  cout << r << endl;
  return 0;
}