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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define dup(x, y) (((x) + (y)-1) / (y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e15;
const int MOD = 1e9 + 7;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
typedef pair<ll, ll> pll;
long long n, k;
long long a[200005];

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

int main() {

  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  long long left = -1;
  long long right = INF;

  while (right - left > 1) {

    long long mid = (left + right) / 2;

    if (check(mid))
      right = mid;
    else
      left = mid;
    // cout << left << " " << right << endl;
  }
  cout << right << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define dup(x, y) (((x) + (y)-1) / (y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e15;
const int MOD = 1e9 + 7;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
typedef pair<ll, ll> pll;
long long n, k;
long long a[200005];

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

int main() {

  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  long long left = 0;
  long long right = INF;

  while (right - left > 1) {

    long long mid = (left + right) / 2;

    if (check(mid))
      right = mid;
    else
      left = mid;
    // cout << left << " " << right << endl;
  }
  cout << right << endl;
}
Problem p02598 (C++)

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

// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

#define ll long long int
#define ld long double
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define pi 3.14159265358979
#define pa pair<ll, ll>
// #define goodset tree<pa, null_type, less<pa>, rb_tree_tag,
// tree_order_statistics_node_update> #define FRE freopen("acm.in","r",stdin);
// freopen("acm.out","w",stdout);

const ll mod = 1e9 + 7;
const ll N = 1e7 + 5;
int main() {
  FAST

      ll tc,
      i, j, k;
  // cin>>tc;
  tc = 1;
  while (tc--) {
    ll n, k;
    cin >> n >> k;
    ll a[n];
    for (i = 0; i < n; i++)
      cin >> a[i];
    sort(a, a + n);

    ll lo = 0;
    ll hi = a[n - 1];
    ll ans = a[n - 1];
    while (lo <= hi) {
      ll mi = (lo + hi) / 2;
      ll cnt = 0;
      for (i = 0; i < n; i++) {
        ll val = a[i] / mi;
        if (a[i] % mi != 0)
          val++;
        if (val > 0)
          cnt += (val - 1);
      }
      if (cnt <= k) {
        ans = min(ans, mi);
        hi = mi - 1;
      } else
        lo = mi + 1;
    }
    cout << ans;
  }
  return 0;
}


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

// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

#define ll long long int
#define ld long double
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define pi 3.14159265358979
#define pa pair<ll, ll>
// #define goodset tree<pa, null_type, less<pa>, rb_tree_tag,
// tree_order_statistics_node_update> #define FRE freopen("acm.in","r",stdin);
// freopen("acm.out","w",stdout);

const ll mod = 1e9 + 7;
const ll N = 1e7 + 5;
int main() {
  FAST

      ll tc,
      i, j, k;
  // cin>>tc;
  tc = 1;
  while (tc--) {
    ll n, k;
    cin >> n >> k;
    ll a[n];
    for (i = 0; i < n; i++)
      cin >> a[i];
    sort(a, a + n);

    ll lo = 0;
    ll hi = a[n - 1];
    ll ans = a[n - 1];
    while (lo <= hi) {
      ll mi = (lo + hi) / 2;
      if (mi == 0) {
        ans = 1;
        break;
      }
      ll cnt = 0;
      for (i = 0; i < n; i++) {
        ll val = a[i] / mi;
        if (a[i] % mi != 0)
          val++;
        if (val > 0)
          cnt += (val - 1);
      }
      if (cnt <= k) {
        ans = min(ans, mi);
        hi = mi - 1;
      } else
        lo = mi + 1;
    }
    cout << ans;
  }
  return 0;
}
Problem p02598 (C++)

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

int n, k, a[200010];

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

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

int n, k, a[200010];

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

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

using namespace std;

using llong = long long int;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define stl_rep(itr, x) for (auto itr = x.begin(); itr != x.end(); ++itr)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()

const static int MOD = 1000000007;
const static int INF = 1000000000;
const static int dx[4] = {1, 0, -1, 0};
const static int dy[4] = {0, 1, 0, -1};

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<int> logs(n);
  rep(i, n) cin >> logs[i];

  int left = -1, right = INF, mid;
  while (right - left > 1) {
    mid = left + (right - left) / 2;
    int cut = 0;
    rep(i, n) {
      if (cut > k)
        break;
      cut += (logs[i] + mid - 1) / mid - 1;
    }
    if (cut <= k)
      right = mid;
    else
      left = mid;
  }

  cout << right << endl;

  return 0;
}

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

using namespace std;

using llong = long long int;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define stl_rep(itr, x) for (auto itr = x.begin(); itr != x.end(); ++itr)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()

const static int MOD = 1000000007;
const static int INF = 1000000000;
const static int dx[4] = {1, 0, -1, 0};
const static int dy[4] = {0, 1, 0, -1};

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<int> logs(n);
  rep(i, n) cin >> logs[i];

  int left = 0, right = INF, mid;
  while (right - left > 1) {
    mid = left + (right - left) / 2;
    int cut = 0;
    rep(i, n) {
      if (cut > k)
        break;
      cut += (logs[i] + mid - 1) / mid - 1;
    }
    if (cut <= k)
      right = mid;
    else
      left = mid;
  }

  cout << right << endl;

  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;
using vi = vector<int>;
using vll = vector<ll>;
#define MOD 1000000007

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

  ll ng = -1, ok = 1e10, mid;
  ll cnt;
  while (abs(ok - ng) > 1) {
    mid = (ok + ng) / 2;
    cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += (a[i] + mid - 1) / mid;
      cnt--;
    }
    if (cnt <= k)
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << 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;
using vi = vector<int>;
using vll = vector<ll>;
#define MOD 1000000007

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

  ll ng = 0, ok = 1e10, mid;
  ll cnt;
  while (abs(ok - ng) > 1) {
    mid = (ok + ng) / 2;
    cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += (a[i] + mid - 1) / mid;
      cnt--;
    }
    if (cnt <= k)
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define REP(i, start, end) for (int i = start, i##Len = (end); i < i##Len; ++i)
#define REPR(i, start, end) for (int i = start, i##Len = (end); i > i##Len; --i)
using ll = long long;
using namespace std;

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

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  REP(i, 0, n)
  cin >> a[i];

  auto solve = [&](ll x) {
    ll cnt = 0;
    for (auto &l : a)
      cnt += l / x;
    return cnt > k;
  };

  ll ok = 0, ng = 1e9 + 1;
  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    if (solve(mid))
      ok = mid;
    else
      ng = mid;
  }

  ll cnt = 0;
  for (auto &l : a) {
    cnt += (l % ok > 0 ? l / ok : l / ok - 1);
  }
  cout << (cnt > k ? ok + 1 : ok) << endl;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define REP(i, start, end) for (int i = start, i##Len = (end); i < i##Len; ++i)
#define REPR(i, start, end) for (int i = start, i##Len = (end); i > i##Len; --i)
using ll = long long;
using namespace std;

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

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  REP(i, 0, n)
  cin >> a[i];

  auto solve = [&](ll x) {
    ll cnt = 0;
    for (auto &l : a)
      cnt += l / x;
    return cnt > k;
  };

  ll ok = 0, ng = 1e9 + 1;
  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    if (solve(mid))
      ok = mid;
    else
      ng = mid;
  }

  ll cnt = 0;
  if (ok == 0) {
    cout << 1 << endl;
    return 0;
  }
  for (auto &l : a) {
    cnt += (l % ok > 0 ? l / ok : l / ok - 1);
  }
  cout << (cnt > k ? ok + 1 : ok) << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long

using namespace std;

const ll INF = 10010010010010;

int main() {
  ll N, K;
  cin >> N >> K;

  vector<ll> A(N);
  for (int ii = 0; ii < N; ++ii) {
    cin >> A[ii];
  }

  ll l = -1;
  ll r = INF;
  while (l + 1 < r) {
    ll mid = (l + r) / 2;

    ll sum = 0;
    for (int ii = 0; ii < N; ++ii) {
      sum += (A[ii] + mid - 1) / mid - 1;
    }

    bool ok = (sum <= K) ? true : false;

    if (ok) {
      r = mid;
    } else {
      l = mid;
    }
  }

  cout << r << "\n";

  return 0;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long

using namespace std;

const ll INF = 10010010010010;

int main() {
  ll N, K;
  cin >> N >> K;

  vector<ll> A(N);
  for (int ii = 0; ii < N; ++ii) {
    cin >> A[ii];
  }

  ll l = 0;
  ll r = INF;
  while (l + 1 < r) {
    ll mid = (l + r) / 2;

    ll sum = 0;
    for (int ii = 0; ii < N; ++ii) {
      sum += (A[ii] + mid - 1) / mid - 1;
    }

    bool ok = (sum <= K) ? true : false;

    if (ok) {
      r = mid;
    } else {
      l = mid;
    }
  }

  cout << r << "\n";

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
/*COMPETITIVE PROGRAMMING C++ TEMPLATE */

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << x << "\n"
#define MOD 998244353

const long double PI = 3.141592653589793236L;

typedef long long int ll;
typedef long double ld;

using namespace std;

bool check(ll ans, vector<ll> &a, ll k) {
  ll count = 0;
  int n = a.size();
  for (int i = 0; i < n; i++) {
    count += a[i] / ans - 1;
    if (a[i] % ans) {
      count++;
    }
  }

  if (count <= k) {
    return true;
  }

  return false;
}

void solve() {
  ll n, k;
  cin >> n >> k;

  vector<ll> a(n);

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

  ll lo = 0;
  ll high = 1e9;
  ll ans = high;

  while (lo <= high) {
    ll mid = lo + (high - lo) / 2;
    bool is_valid = check(mid, a, k);

    if (is_valid) {
      ans = mid;
      high = mid - 1;
    } else {
      lo = mid + 1;
    }
  }

  cout << ans << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int T = 1;
  // cin >> T;

  while (T--) {
    solve();
  }

  return 0;
}


Fixed submission:
/*COMPETITIVE PROGRAMMING C++ TEMPLATE */

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << x << "\n"
#define MOD 998244353

const long double PI = 3.141592653589793236L;

typedef long long int ll;
typedef long double ld;

using namespace std;

bool check(ll ans, vector<ll> &a, ll k) {

  if (ans == 0) {
    return false;
  }

  ll count = 0;
  int n = a.size();
  for (int i = 0; i < n; i++) {
    count += a[i] / ans - 1;
    if (a[i] % ans) {
      count++;
    }
  }

  if (count <= k) {
    return true;
  }

  return false;
}

void solve() {
  ll n, k;
  cin >> n >> k;

  vector<ll> a(n);

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

  ll lo = 0;
  ll high = 1e9;
  ll ans = high;

  while (lo <= high) {
    ll mid = lo + (high - lo) / 2;
    bool is_valid = check(mid, a, k);

    if (is_valid) {
      ans = mid;
      high = mid - 1;
    } else {
      lo = mid + 1;
    }
  }

  cout << ans << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int T = 1;
  // cin >> T;

  while (T--) {
    solve();
  }

  return 0;
}
Problem p02598 (C++)

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

using namespace std;

#define eb emplace_back
#define ii pair<int, int>
#define OK (cerr << "OK" << endl)
#define debug(x) cerr << #x " = " << (x) << endl
#define ff first
#define ss second
#define int long long
#define tt tuple<int, int, int>
#define all(x) x.begin(), x.end()
#define vvi vector<vector<int>>
#define vvii vector<vector<pair<int, int>>>
#define Mat(n, m, v) vector<vector<int>>(n, vector<int>(m, v))
#define endl '\n'

constexpr int INF = 2e18;
constexpr int MOD = 1e9 + 7;
constexpr int MAXN = 2e5 + 3;

bool check(int g, vector<int> &arr, int k) {
  int c = 0;
  for (int x : arr) {
    c += (x - 1) / g;
  }

  return (c <= k);
}

// #define MULTIPLE_TEST_CASES
void solve() {
  int n;
  cin >> n;
  int k;
  cin >> k;

  vector<int> arr(n);
  for (int &x : arr)
    cin >> x;

  int l = 0, r = 2e9;
  int ans = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid, arr, k)) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << ans << endl;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t = 1;
#ifdef MULTIPLE_TEST_CASES
  cin >> t;
#endif
  while (t--)
    solve();
}

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

using namespace std;

#define eb emplace_back
#define ii pair<int, int>
#define OK (cerr << "OK" << endl)
#define debug(x) cerr << #x " = " << (x) << endl
#define ff first
#define ss second
#define int long long
#define tt tuple<int, int, int>
#define all(x) x.begin(), x.end()
#define vvi vector<vector<int>>
#define vvii vector<vector<pair<int, int>>>
#define Mat(n, m, v) vector<vector<int>>(n, vector<int>(m, v))
#define endl '\n'

constexpr int INF = 2e18;
constexpr int MOD = 1e9 + 7;
constexpr int MAXN = 2e5 + 3;

bool check(int g, vector<int> &arr, int k) {
  int c = 0;
  for (int x : arr) {
    c += (x - 1) / g;
  }

  return (c <= k);
}

// #define MULTIPLE_TEST_CASES
void solve() {
  int n;
  cin >> n;
  int k;
  cin >> k;

  vector<int> arr(n);
  for (int &x : arr)
    cin >> x;

  int l = 1, r = 2e9;
  int ans = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid, arr, k)) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << ans << endl;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t = 1;
#ifdef MULTIPLE_TEST_CASES
  cin >> t;
#endif
  while (t--)
    solve();
}
Problem p02598 (C++)

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

typedef vector<int> vi;
typedef long long ll;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define pmod 1000000007
#define INF 1000000000000000000
#define pi atan(1) * 4
#define modi 1000000

void solve() {

  ll n, k;

  cin >> n >> k;

  ll a[n];

  ll sum = 0;

  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }

  ll mini = sum;

  ll ini = 0, fin = sum;

  bool can;

  while (ini <= fin) {

    ll med = (fin + ini) / 2;
    ll counter = 0;

    for (ll i = 0; i < n; i++) {

      counter += (((a[i] + med - 1) / med) - 1);
    }

    if (counter <= k) {
      mini = min(mini, med);
      fin = med - 1;
    } else {
      ini = med + 1;
    }
  }

  cout << mini << endl;
}

int main() {

  ifstream fin("text.in");
  ofstream fout("text.out");

  ll t = 1;

  while (t--)
    solve();

  return 0;
}

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

typedef vector<int> vi;
typedef long long ll;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define pmod 1000000007
#define INF 1000000000000000000
#define pi atan(1) * 4
#define modi 1000000

void solve() {

  ll n, k;

  cin >> n >> k;

  ll a[n];

  ll sum = 0;

  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }

  ll mini = sum;

  ll ini = 1, fin = sum;

  bool can;

  while (ini <= fin) {

    ll med = (fin + ini) / 2;
    ll counter = 0;

    for (ll i = 0; i < n; i++) {

      counter += (((a[i] + med - 1) / med) - 1);
    }

    if (counter <= k) {
      mini = min(mini, med);
      fin = med - 1;
    } else {
      ini = med + 1;
    }
  }

  cout << mini << endl;
}

int main() {

  ifstream fin("text.in");
  ofstream fout("text.out");

  ll t = 1;

  while (t--)
    solve();

  return 0;
}
Problem p02598 (C++)

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

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

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll ub = 2e9;
  ll lb = 0;
  rep(i, 100) {
    ll mid = (ub + lb) / 2;
    ll tans = 0;
    rep(j, n) {
      if (a[j] > mid) {
        tans += (a[j] + mid - 1) / mid - 1;
      }
    }
    if (tans > k)
      lb = mid;
    else
      ub = mid;
  }
  cout << ub << endl;

  return 0;
}


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

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

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll ub = 2e9;
  ll lb = 0;
  rep(i, 100) {
    ll mid = (ub + lb) / 2;
    if (mid == 0)
      break;
    ll tans = 0;
    rep(j, n) {
      if (a[j] > mid) {
        tans += (a[j] + mid - 1) / mid - 1;
      }
    }
    if (tans > k)
      lb = mid;
    else
      ub = mid;
  }
  cout << ub << endl;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define pb push_back
#define size(v) (int)v.size()
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
ll power_mod(ll a, ll x) {
  if (x == 0)
    return 1;
  ll y = power_mod(a, x / 2);
  ll ans = (y * y) % mod;
  if (x % 2)
    ans = (ans * a) % mod;
  return ans;
}
ll inv(ll a) { return power_mod(a, mod - 2); }
ll power(ll a, ll x) {
  if (x == 0)
    return 1;
  ll y = power(a, x / 2);
  ll ans = (y * y);
  if (x % 2)
    ans *= a;
  return ans;
}

int main() {
  fast;
  ll n, k;
  cin >> n >> k;

  vector<ll> a(n, 0);

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

  ll lo = 0, hi = 1000000000;
  ll ans = hi;

  while (lo <= hi) {
    ll mid = (lo + hi) / 2;
    if (mid == 0) {
      if (lo == hi) {
        break;
      }
      lo = 1;
    }
    ll cut_req = 0;
    for (auto i : a) {
      ll curr = i;
      if (curr < mid)
        continue;
      cut_req += ((curr - 1) / mid);
    }
    // printf("%0.9lf ",mid);
    // cout << cut_req << endl;
    if (cut_req > k) {
      lo = mid + 1;
    } else {
      ans = min(ans, mid);
      hi = mid - 1;
    }
  }
  ll res = ans;
  // printf("%0.9lf\n",ans);
  cout << res << endl;
}

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define pb push_back
#define size(v) (int)v.size()
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
ll power_mod(ll a, ll x) {
  if (x == 0)
    return 1;
  ll y = power_mod(a, x / 2);
  ll ans = (y * y) % mod;
  if (x % 2)
    ans = (ans * a) % mod;
  return ans;
}
ll inv(ll a) { return power_mod(a, mod - 2); }
ll power(ll a, ll x) {
  if (x == 0)
    return 1;
  ll y = power(a, x / 2);
  ll ans = (y * y);
  if (x % 2)
    ans *= a;
  return ans;
}

int main() {
  fast;
  ll n, k;
  cin >> n >> k;

  vector<ll> a(n, 0);

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

  ll lo = 0, hi = 1000000000;
  ll ans = hi;

  while (lo <= hi) {
    ll mid = (lo + hi) / 2;
    if (mid == 0) {
      if (lo == hi) {
        break;
      }
      mid = 1;
    }
    ll cut_req = 0;
    for (auto i : a) {
      ll curr = i;
      if (curr < mid)
        continue;
      cut_req += ((curr - 1) / mid);
    }
    // printf("%0.9lf ",mid);
    // cout << cut_req << endl;
    if (cut_req > k) {
      lo = mid + 1;
    } else {
      ans = min(ans, mid);
      hi = mid - 1;
    }
  }
  ll res = ans;
  // printf("%0.9lf\n",ans);
  cout << res << endl;
}
Problem p02598 (C++)

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;

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

  int left = -1;
  int right = INF;

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

    ll num = 0;
    for (int i = 0; i < n; ++i) {
      if (a[i] % mid == 0)
        num += a[i] / mid - 1;
      else
        num += a[i] / mid;
    }
    if (num <= k)
      right = mid;
    else
      left = mid;
  }
  cout << right << endl;
}

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;

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

  int left = 0;
  int right = INF;

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

    ll num = 0;
    for (int i = 0; i < n; ++i) {
      if (a[i] % mid == 0)
        num += a[i] / mid - 1;
      else
        num += a[i] / mid;
    }
    if (num <= k)
      right = mid;
    else
      left = mid;
  }
  cout << right << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
// #define N 998244353
#define INF 1e16
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> Q;

const int inf = (int)1e9;

ll gcd(ll a, ll b) {
  if (b > a) {
    ll tmp = b;
    b = a;
    a = tmp;
  }
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}

ll n, k;
vector<ll> a(n);

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

int main(void) {
  n, k;
  cin >> n >> k;
  a.resize(n);
  ll ng = -1, ok = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ok = max(ok, a[i]);
  }
  while (ok - ng > 1) {
    ll mid = (ok + ng) / 2;
    if (check(mid))
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
// #define N 998244353
#define INF 1e16
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> Q;

const int inf = (int)1e9;

ll gcd(ll a, ll b) {
  if (b > a) {
    ll tmp = b;
    b = a;
    a = tmp;
  }
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}

ll n, k;
vector<ll> a(n);

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

int main(void) {
  n, k;
  cin >> n >> k;
  a.resize(n);
  ll ng = -1, ok = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ok = max(ok, a[i]);
  }
  while (ok - ng > 1) {
    ll mid = (ok + ng) / 2;
    if (check(mid))
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, k, i, mini, maxi = 1e9;
vector<int> A;

bool check(int tar) {
  int j, cur = 0;
  for (j = 0; j < n; j++) {
    cur += (A[j] - 1) / tar;
  }
  if (cur > k)
    return false;
  else
    return true;
}

int main() {
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  while (mini < maxi) {
    int mid = mini + maxi >> 1;
    if (mini == mid) {
      if (!check(mini))
        mini = maxi;
      else
        maxi = mini;
    } else {
      if (check(mid))
        maxi = mid;
      else
        mini = mid + 1;
    }
  }
  cout << mini;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, k, i, mini = 1, maxi = 1e9;
vector<int> A;

bool check(int tar) {
  int j, cur = 0;
  for (j = 0; j < n; j++) {
    cur += (A[j] - 1) / tar;
  }
  if (cur > k)
    return false;
  else
    return true;
}

int main() {
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  while (mini < maxi) {
    int mid = mini + maxi >> 1;
    if (mini == mid) {
      if (!check(mini))
        mini = maxi;
      else
        maxi = mini;
    } else {
      if (check(mid))
        maxi = mid;
      else
        mini = mid + 1;
    }
  }
  cout << mini;
  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>
#include <functional>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define mk make_pair

template <class key, class cmp = std::less<key>>
using ordered_set =
    tree<key, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 1e9 + 7;

/*
    if you have any question about any part of my code,
    please don't shy to ask anytime :)
*/

ll n, k;
ll x[200005];

bool get(ll mid) {
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += (x[i] - 1) / mid;
  }

  return cnt <= k;
}

void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }

  ll st = 0, en = 1e9;
  ll ans = 0;
  while (st <= en) {
    ll mid = (st + en) >> 1;
    if (get(mid)) {
      ans = mid;
      en = mid - 1;
    } else {
      st = mid + 1;
    }
  }

  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  // ll t;
  // cin >> t;
  // while (t--)
  // {
  //     solve();
  // }
  solve();
  return 0;
}

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

#define ll long long
#define mk make_pair

template <class key, class cmp = std::less<key>>
using ordered_set =
    tree<key, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 1e9 + 7;

/*
    if you have any question about any part of my code,
    please don't shy to ask anytime :)
*/

ll n, k;
ll x[200005];

bool get(ll mid) {
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += (x[i] - 1) / mid;
  }

  return cnt <= k;
}

void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }

  ll st = 1, en = 1e9;
  ll ans = 0;
  while (st <= en) {
    ll mid = (st + en) >> 1;
    if (get(mid)) {
      ans = mid;
      en = mid - 1;
    } else {
      st = mid + 1;
    }
  }

  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  // ll t;
  // cin >> t;
  // while (t--)
  // {
  //     solve();
  // }
  solve();
  return 0;
}
Problem p02598 (C++)

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (auto &e : A)
    cin >> e;
  auto OK = [&](int x) -> bool {
    int cnt = 0;
    for (auto &a : A) {
      if (a == x)
        continue;
      cnt += (a / x);
    }
    return cnt <= K;
  };
  int l = -1, r = 1e9;
  while (r - l > 1) {
    int c = (l + r) / 2;
    if (OK(c))
      r = c;
    else
      l = c;
  }
  cout << r << '\n';
  return 0;
}


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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (auto &e : A)
    cin >> e;
  auto OK = [&](int x) -> bool {
    int cnt = 0;
    for (auto &a : A) {
      if (a == x)
        continue;
      cnt += (a / x);
    }
    return cnt <= K;
  };
  int l = 0, r = 1e9;
  while (r - l > 1) {
    int c = (l + r) / 2;
    if (OK(c))
      r = c;
    else
      l = c;
  }
  cout << r << '\n';
  return 0;
}
Problem p02598 (Python)

Buggy submission (status: Runtime Error):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K, *A = map(int, read().split())

    ng = -1
    ok = 10**9
    while ok - ng > 1:
        mid = (ok + ng) // 2
        res = 0
        for a in A:
            res += (a + mid - 1) // mid - 1
        if res <= K:
            ok = mid
        else:
            ng = mid

    print(ok)
    return


if __name__ == "__main__":
    main()


Fixed submission:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K, *A = map(int, read().split())

    ng = 0
    ok = 10**9
    while ok - ng > 1:
        mid = (ok + ng) // 2
        res = 0
        for a in A:
            res += (a + mid - 1) // mid - 1
        if res <= K:
            ok = mid
        else:
            ng = mid

    print(ok)
    return


if __name__ == "__main__":
    main()
Problem p02598 (Python)

Buggy submission (status: Time Limit Exceeded):
def is_good(mid, key):
    return sum(count_cuts(a, mid) for a in A) <= key


def binary_search(key):
    bad, good = 0, 10**16
    while good - bad > 1:
        mid = (bad + good) // 2
        if is_good(mid, key):
            good = mid
        else:
            bad = mid
    return good


def count_cuts(a, unit):
    return (a + unit - 1) // unit - 1


N, K, *A = map(int, open(0).read().split())
print(binary_search(K))


Fixed submission:
def is_good(mid, key):
    return sum(count_cuts(a, mid) for a in A) <= key


def binary_search(key):
    bad, good = 0, 2 * 10**14 + 1
    while good - bad > 1:
        mid = (bad + good) // 2
        if is_good(mid, key):
            good = mid
        else:
            bad = mid
    return good


def count_cuts(a, unit):
    return (a + unit - 1) // unit - 1


N, K, *A = map(int, open(0).read().split())
print(binary_search(K))
Problem p02598 (Python)

Buggy submission (status: Time Limit Exceeded):
from sys import stdin
import numpy as np

n, k = map(int, stdin.readline().split())
a = np.array(stdin.readline().split(), dtype=np.int64)

ng = 0
ok = 10**9 + 1
while ok - ng > 1:
    mid = (ok + ng) >> 1
    if sum(0 - -b // mid - 1 for b in a) <= k:
        ok = mid
    else:
        ng = mid
print(ok)


Fixed submission:
from sys import stdin
import numpy as np

n, k = map(int, stdin.readline().split())
a = np.array(stdin.readline().split(), dtype=np.int64)

ng = 0
ok = 10**9 + 1
while ok - ng > 1:
    mid = (ok + ng) >> 1
    if np.sum(0 - -a // mid - 1) <= k:
        ok = mid
    else:
        ng = mid
print(ok)
Problem p02598 (C++)

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

bool able_check(long L, long *A, long N, long K);

int main(void) {
  long N, K;
  cin >> N >> K;
  long *A = (long *)malloc(sizeof(long) * N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  long min = 0;
  long max = 1000000010;
  while (max - min > 5) {
    long mid = (min + max) / 2;
    if (able_check(mid, A, N, K)) {
      max = mid;
    } else {
      min = mid;
    }
  }
  for (long i = min; i <= max; i++) {
    if (able_check(i, A, N, K)) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}

bool able_check(long L, long *A, long N, long K) {
  long retval = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] % L == 0) {
      retval += A[i] / L - 1;
    } else {
      retval += A[i] / L;
    }
  }
  return retval <= K;
}


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

bool able_check(long L, long *A, long N, long K);

int main(void) {
  long N, K;
  cin >> N >> K;
  long *A = (long *)malloc(sizeof(long) * N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  long min = 1;
  long max = 1000000010;
  while (max - min > 5) {
    long mid = (min + max) / 2;
    if (able_check(mid, A, N, K)) {
      max = mid;
    } else {
      min = mid;
    }
  }
  for (long i = min; i <= max; i++) {
    if (able_check(i, A, N, K)) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}

bool able_check(long L, long *A, long N, long K) {
  long retval = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] % L == 0) {
      retval += A[i] / L - 1;
    } else {
      retval += A[i] / L;
    }
  }
  return retval <= K;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 200003;
int n, k;
int a[N];
inline bool check(int x) {
  long long tot = 0;
  for (int i = 1; i <= n; i += 1)
    tot += a[i] / x + (a[i] % x != 0) - 1;
  return tot <= k;
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i += 1)
    cin >> a[i];
  int l = 0, r = 1000000001, ans = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 200003;
int n, k;
int a[N];
inline bool check(int x) {
  long long tot = 0;
  for (int i = 1; i <= n; i += 1)
    tot += a[i] / x + (a[i] % x != 0) - 1;
  return tot <= k;
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i += 1)
    cin >> a[i];
  int l = 1, r = 1000000001, ans = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans << endl;
}
Problem p02598 (C++)

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
#define fr(i, a, b) for (ll i = a; i < b; i++)

int main() {
  ios::sync_with_stdio(false);
#ifdef CODE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll n, k;
  cin >> n >> k;
  vl arr(n);
  fr(i, 0, n) { cin >> arr[i]; }
  ll low = 0, high = *max_element(arr.begin(), arr.end());
  ll ans = high;

  auto func = [&](ll x) {
    ll total = 0;
    fr(i, 0, n) {
      int temp = arr[i] / x;
      if (x * temp == arr[i])
        temp--;
      total += temp;
    }
    return (total <= k);
  };
  while (low <= high) {
    ll mid = low + (high - low) / 2;
    if (func(mid)) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  cout << ans;

  return 0;
}

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
#define fr(i, a, b) for (ll i = a; i < b; i++)

int main() {
  ios::sync_with_stdio(false);
#ifdef CODE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll n, k;
  cin >> n >> k;
  vl arr(n);
  fr(i, 0, n) { cin >> arr[i]; }
  ll low = 1, high = 1e+10;
  ll ans = high;

  auto func = [&](ll x) {
    ll total = 0;
    fr(i, 0, n) {
      int temp = arr[i] / x;
      if (x * temp == arr[i])
        temp--;
      total += temp;
    }
    return (total <= k);
  };
  while (low <= high) {
    ll mid = low + (high - low) / 2;
    if (func(mid)) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  cout << ans;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

const long long INF = 1e18;
// const ll mod = 1000000007;

ll N, K;
ll A[201000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  ll ok = 1e9;
  ll ng = 0;
  for (int _ = 0; _ <= 100; _++) {
    ll mid = (ok + ng) / 2;
    ll timer = 0;
    for (int i = 0; i < N; i++) {
      timer += (A[i] - 1) / mid;
    }
    if (timer <= K)
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

const long long INF = 1e18;
// const ll mod = 1000000007;

ll N, K;
ll A[201000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  ll ok = 1e9;
  ll ng = 0;
  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    ll timer = 0;
    for (int i = 0; i < N; i++) {
      timer += (A[i] - 1) / mid;
    }
    if (timer <= K)
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define endl '\n'
bool check(int x, vector<int> &v, int k) {
  int cuts = 0;
  for (int i = 0; i < v.size(); i++) {
    cuts += v[i] / x;
    if (v[i] % x == 0)
      cuts--;
  }
  if (cuts <= k)
    return true;
  else
    return false;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  int low = 0;
  int high = 1000000000;
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  int ans;
  while (low <= high) {
    int mid = (low + high) / 2;
    if (check(mid, v, k)) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define endl '\n'
bool check(int x, vector<int> &v, int k) {
  int cuts = 0;
  for (int i = 0; i < v.size(); i++) {
    cuts += v[i] / x;
    if (v[i] % x == 0)
      cuts--;
  }
  if (cuts <= k)
    return true;
  else
    return false;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  int low = 1;
  int high = 1000000000;
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  int ans;
  while (low <= high) {
    int mid = (low + high) / 2;
    if (check(mid, v, k)) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  cout << ans;
}
Problem p02598 (C++)

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

#define PI acos(-1)
#define pb push_back
#define mp make_pair
#define int long long int
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define MAXN 500001
#define MAXL 20
#define INF 200001
#define mod 1000000007

bool possible(int x, vector<int> v, int k) {
  int ans = 0;
  for (int i = 0; i < v.size(); i++) {
    if (v[i] > x) {
      int inc = v[i] / x;
      ans += inc;
    }
  }
  return ans <= k;
}
int bb(vector<int> v, int k) {
  int l = 0, r = 1e9, m;
  while (l < r) {
    m = (l + r) >> 1;
    if (possible(m, v, k))
      r = m;
    else
      l = m + 1;
  }
  return l;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  cout << bb(v, k) << endl;
  return 0;
}

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

#define PI acos(-1)
#define pb push_back
#define mp make_pair
#define int long long int
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define MAXN 500001
#define MAXL 20
#define INF 200001
#define mod 1000000007

bool possible(int x, vector<int> v, int k) {
  int ans = 0;
  for (int i = 0; i < v.size(); i++) {
    if (v[i] > x) {
      int inc = v[i] / x;
      ans += inc;
    }
  }
  return ans <= k;
}
int bb(vector<int> v, int k) {
  int l = 1, r = 1e9, m;
  while (l < r) {
    m = (l + r) >> 1;
    if (possible(m, v, k))
      r = m;
    else
      l = m + 1;
  }
  return l;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  cout << bb(v, k) << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(), (v).end()
#define lp(i, n) for (int i = 0; i < (n); ++i)
#define pr1(x) cout << "[" << #x << ": " << x << "] \n";
#define pr2(x, y)                                                              \
  cout << "[" << #x << ": " << x << ", " << #y << ": " << y << "] \n";
#define mod 1000000007
#define int long long
#define sz(v) (int)(v).size()

int n, k;
vector<int> v;

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

void solve() {
  cin >> n >> k;
  v.resize(n);
  lp(i, n) cin >> v[i];

  int lo = 0, hi = 1e12, md, ans = 0;
  while (hi >= lo) {
    md = (lo + hi) / 2;
    if (can(md)) {
      ans = md;
      hi = md - 1;
    } else
      lo = md + 1;
  }
  cout << ans << "\n";
}

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(), (v).end()
#define lp(i, n) for (int i = 0; i < (n); ++i)
#define pr1(x) cout << "[" << #x << ": " << x << "] \n";
#define pr2(x, y)                                                              \
  cout << "[" << #x << ": " << x << ", " << #y << ": " << y << "] \n";
#define mod 1000000007
#define int long long
#define sz(v) (int)(v).size()

int n, k;
vector<int> v;

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

void solve() {
  cin >> n >> k;
  v.resize(n);
  lp(i, n) cin >> v[i];

  int lo = 1, hi = 1e12, md, ans = 0;
  while (hi >= lo) {
    md = (lo + hi) / 2;
    if (can(md)) {
      ans = md;
      hi = md - 1;
    } else
      lo = md + 1;
  }
  cout << ans << "\n";
}

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool solve(vector<int> &a, int k, int l) {
  ll r = 0;
  for (int i : a)
    r += (i - 1) / l;
  return r <= k;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int x = 0, y = 1 << 30;
  while (x < y) {
    int sr = (x + y) / 2;
    if (solve(a, k, sr))
      y = sr;
    else
      x = sr + 1;
  }
  cout << x << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool solve(vector<int> &a, int k, int l) {
  ll r = 0;
  for (int i : a)
    r += (i - 1) / l;
  return r <= k;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int x = 1, y = 1 << 30;
  while (x < y) {
    int sr = (x + y) / 2;
    if (solve(a, k, sr))
      y = sr;
    else
      x = sr + 1;
  }
  cout << x << endl;
}