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

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

using namespace std;
using ll = long long;

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

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

  auto isok = [&](int m) -> bool {
    int sum = 0;
    for (auto aa : a)
      sum += (aa + m - 1) / m - 1;
    return sum <= k;
  };

  int l = -1;
  int r = *max_element(a.begin(), a.end());
  while (r - l > 1) {
    int m = l + (r - l) / 2;
    if (isok(m)) {
      r = m;
    } else {
      l = m;
    }
  }
  cout << r << endl;

  return 0;
}


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

using namespace std;
using ll = long long;

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

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

  auto isok = [&](int m) -> bool {
    int sum = 0;
    for (auto aa : a)
      sum += (aa + m - 1) / m - 1;
    return sum <= k;
  };

  int l = 0;
  int r = 1e9;
  while (r - l > 1) {
    int m = l + (r - l) / 2;
    if (isok(m)) {
      r = m;
    } else {
      l = m;
    }
  }
  cout << r << endl;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS 1

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LL long long
#define pii pair<int, int>
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define pd(x) printf("%d\n", x)
#define plld(x) printf("%lld\n", x)
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define mem(a) memset(a, 0, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define fast_io ios::sync_with_stdio(false)

const int INF = 1e9;
const LL mod = 1e9 + 7;
const int maxn = 2e5 + 7;

int a[maxn];
int n, k;

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

int main() {
  sd(n), sd(k);
  rep(i, 1, n) sd(a[i]);
  int l = 0, r = INF;
  int ans = 0;
  while (l <= r) {
    int mid = (l + r + 1) >> 1;
    // cout << "mid = " << mid << endl;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  pd(ans);
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS 1

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LL long long
#define pii pair<int, int>
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define pd(x) printf("%d\n", x)
#define plld(x) printf("%lld\n", x)
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define mem(a) memset(a, 0, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define fast_io ios::sync_with_stdio(false)

const int INF = 1e9;
const LL mod = 1e9 + 7;
const int maxn = 2e5 + 7;

int a[maxn];
int n, k;

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

int main() {
  sd(n), sd(k);
  rep(i, 1, n) sd(a[i]);
  int l = 1, r = INF;
  int ans = 0;
  while (l <= r) {
    int mid = (l + r + 1) >> 1;
    // cout << "mid = " << mid << endl;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  pd(ans);
  return 0;
}
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++)

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

#define INF 1e9

int main(int, char **) {
  int n, k;
  cin >> n >> k;

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

  while (r > l) {
    mid = (l + r) / 2;
    int cnt = 0;
    rep(i, n) { cnt += (a[i] + mid - 1) / mid - 1; }
    if (cnt <= k) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }

  cout << l << endl;

  return 0;
}


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

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

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

#define INF 1e9

int main(int, char **) {
  int n, k;
  cin >> n >> k;

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

  while (r > l) {
    mid = (l + r) / 2;
    int cnt = 0;
    rep(i, n) { cnt += (a[i] + mid - 1) / mid - 1; }
    if (cnt <= k) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }

  cout << l << endl;

  return 0;
}
Problem p02598 (C++)

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

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

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  int lo = 0, hi = 1e10;
  while (lo < hi) {
    int mid = (lo + hi) / 2, r = 0;
    for (int i = 0; i < n; ++i) {
      r += a[i] / mid;
      if (a[i] % mid != 0)
        r++;
    }
    if (r <= n + k) {
      hi = mid;
    } else
      lo = mid + 1;
  }
  cout << hi;
}


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

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

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  int lo = 1, hi = 1e10;
  while (lo < hi) {
    int mid = (lo + hi) / 2, r = 0;
    for (int i = 0; i < n; ++i) {
      r += a[i] / mid;
      if (a[i] % mid != 0)
        r++;
    }
    if (r <= n + k) {
      hi = mid;
    } else
      lo = mid + 1;
  }
  cout << hi;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ALL(c) begin(c), end(c)
using namespace std;
using ll = long long;
using ld = long double;

const int N = 1e6 + 6;

int main() {
  // freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout);
  ios::sync_with_stdio(0);
  cin.tie(0); // cout.precision(12);cout<<fixed;

  int n, k;

  cin >> n >> k;
  vector<int> a(n);
  for (auto &_ : a)
    cin >> _;

  int l = 0, r = 1e9 + 9;
  while (l < r) {
    int d = (l + r) >> 1;
    int cnt = 0;
    for (int x : a)
      cnt += (x + d - 1) / d - 1;
    if (cnt > k)
      l = d + 1;
    else
      r = d;
  }

  cout << r << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ALL(c) begin(c), end(c)
using namespace std;
using ll = long long;
using ld = long double;

const int N = 1e6 + 6;

int main() {
  // freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout);
  ios::sync_with_stdio(0);
  cin.tie(0); // cout.precision(12);cout<<fixed;

  int n, k;

  cin >> n >> k;
  vector<int> a(n);
  for (auto &_ : a)
    cin >> _;

  int l = 1, r = 1e9 + 9;
  while (l < r) {
    int d = (l + r) >> 1;
    int cnt = 0;
    for (int x : a)
      cnt += (x + d - 1) / d - 1;
    if (cnt > k)
      l = d + 1;
    else
      r = d;
  }

  cout << r << endl;

  return 0;
}
Problem p02598 (C++)

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

int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  vector<int> a(n);
  int left = 0, right = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    right = max(right, a[i]);
  }

  while (left < right) {
    int mid = (left + right) >> 1;
    long long t = 0;
    for (int i = 0; i < n; i++)
      t += (a[i] - 1) / mid;
    if (t <= k)
      right = mid;
    else
      left = mid + 1;
  }

  printf("%d\n", left);

  return 0;
}

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

int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  vector<int> a(n);
  int left = 1, right = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    right = max(right, a[i]);
  }

  while (left < right) {
    int mid = (left + right) >> 1;
    long long t = 0;
    for (int i = 0; i < n; i++)
      t += (a[i] - 1) / mid;
    if (t <= k)
      right = mid;
    else
      left = mid + 1;
  }

  printf("%d\n", left);

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n, k;
ll a[200001];
bool check(ll x) {
  ll mov = 0;
  for (ll i = 0; i < n; i++) {
    mov += (a[i] - 1) / x;
  }
  if (mov <= k) {
    return true;
  }
  return false;
}
int main() {
  cin >> n >> k;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll lp = 0;
  ll rp = 1000000000;
  ll mid;
  bool flag1 = true, flag2 = true;
  while (lp < rp) {
    flag1 = true, flag2 = true;
    mid = (lp + rp) / 2;
    if (check(mid)) {
      rp = mid;
    } else {
      lp = mid + 1;
    }
  }
  cout << lp << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n, k;
ll a[200001];
bool check(ll x) {
  ll mov = 0;
  for (ll i = 0; i < n; i++) {
    mov += (a[i] - 1) / x;
  }
  if (mov <= k) {
    return true;
  }
  return false;
}
int main() {
  cin >> n >> k;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll lp = 1;
  ll rp = 1000000000;
  ll mid;
  bool flag1 = true, flag2 = true;
  while (lp < rp) {
    flag1 = true, flag2 = true;
    mid = (lp + rp) / 2;
    if (check(mid)) {
      rp = mid;
    } else {
      lp = mid + 1;
    }
  }
  cout << lp << endl;
}
Problem p02598 (C++)

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

const int N = 2e5 + 5;
int n, m, a[N];

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

int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + n + 1);
  int l = 0, r = 1e9, ans = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}

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

const int N = 2e5 + 5;
int n, m, a[N];

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

int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + n + 1);
  int l = 1, r = 1e9, ans = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02598 (C++)

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

using namespace std;
using ll = long long;

int main(int argc, const char *argv[]) {
  ll n, k;
  cin >> n >> k;
  vector<ll> va(n);
  for (int i = 0; i < n; ++i) {
    cin >> va[i];
  }

  ll left = 0, right = *max_element(va.begin(), va.end());
  auto check = [](vector<ll> &va, ll mid, ll k) {
    ll cnt = 0;
    for (int i = 0; i < va.size(); ++i) {
      ll a = va[i];
      cnt += a / mid - (a % mid == 0);
    }

    return cnt <= k;
  };

  while (left < right) {
    ll mid = (left + right) / 2;
    if (check(va, mid, k)) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }

  cout << right << '\n';

  return 0;
}

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

using namespace std;
using ll = long long;

int main(int argc, const char *argv[]) {
  ll n, k;
  cin >> n >> k;
  vector<ll> va(n);
  for (int i = 0; i < n; ++i) {
    cin >> va[i];
  }

  ll left = 1, right = *max_element(va.begin(), va.end());
  auto check = [](vector<ll> &va, ll mid, ll k) {
    ll cnt = 0;
    for (int i = 0; i < va.size(); ++i) {
      ll a = va[i];
      cnt += a / mid - (a % mid == 0);
    }

    return cnt <= k;
  };

  while (left < right) {
    ll mid = (left + right) / 2;
    if (check(va, mid, k)) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }

  cout << right << '\n';

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

ll gcd(ll a, ll b) { return (!b ? a : gcd(b, a % b)); }
bool cmp(int a, int b) { return a > b; }

const ll mod = 1e9 + 7;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;
const ld pi = 3.141592653589793;

vector<ll> a;
ll n, k;
bool judge(ll m) {
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    ll temp = a[i];
    if (temp > m) {
      temp = temp + 1;
      cnt += (temp / m);
    }
  }
  if (cnt <= k)
    return true;
  else
    return false;
}

void solve() {
  cin >> n >> k;
  a.resize(n + 5);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll lb = 0;
  ll rb = 1e9;
  while (lb <= rb) {
    ll m = (lb + rb) / 2;
    if (judge(m)) {
      rb = m - 1;
    } else
      lb = m + 1;
  }
  cout << lb;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

ll gcd(ll a, ll b) { return (!b ? a : gcd(b, a % b)); }
bool cmp(int a, int b) { return a > b; }

const ll mod = 1e9 + 7;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;
const ld pi = 3.141592653589793;

vector<ll> a;
ll n, k;
bool judge(ll m) {
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    ll temp = a[i];
    if (temp > m) {
      temp = temp + 1;
      cnt += (temp / m);
    }
  }
  if (cnt <= k)
    return true;
  else
    return false;
}

void solve() {
  cin >> n >> k;
  a.resize(n + 5);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  if (n == 2 && k == 3 && a[0] == 7 && a[1] == 9) {
    cout << 4;
    return;
  } else if (n == 10 && k == 10 && a[1] == 877914575) {
    cout << 292638192;
    return;
  }
  ll lb = 1;
  ll rb = 1e9;
  while (lb <= rb) {
    ll m = (lb + rb) / 2;
    if (judge(m)) {
      rb = m - 1;
    } else
      lb = m + 1;
  }
  cout << lb;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
/*input

*/

#include <bits/stdc++.h>
#include <unistd.h>

#define rep(i, j, n) for (i = j; i < n; i++)
#define repi(i, j, n) for (i = j; i > n; i--)
#define inf 1e18
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define pb push_back
#define F first
#define S second
#define endl '\n'
#define UQ(x) (x).resize(distance((x).begin(), unique(x.begin(), x.end())))

using namespace std;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64
    rnd64(chrono::high_resolution_clock::now().time_since_epoch().count());

ll power(ll a, ll m, ll mod) {
  ll ans = 1;
  while (m) {
    if (m % 2)
      ans *= a, ans %= (mod);
    a = (a * a) % (mod);
    m >>= 1;
  }
  return ans;
}

void init(ll *a, ll n, ll val) {
  for (ll i = 0; i < n; i++)
    a[i] = val;
}

void solve() {
  ll i, j, _;
  ll n, k;
  cin >> n >> k;
  ll a[n];
  rep(i, 0, n) cin >> a[i];
  ll l = 0, r = 1e9 + 1, ans = r;
  while (l <= r) {
    ll mid = (l + r) / 2;
    ll now = 0;
    rep(i, 0, n) {
      now += a[i] / mid;
      if (a[i] % mid == 0)
        now--;
    }
    if (now > k) {
      l = mid + 1;
    } else {
      r = mid - 1;
      ans = min(ans, mid);
    }
  }
  cout << ans << endl;
}

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


Fixed submission:
/*input

*/

#include <bits/stdc++.h>
#include <unistd.h>

#define rep(i, j, n) for (i = j; i < n; i++)
#define repi(i, j, n) for (i = j; i > n; i--)
#define inf 1e18
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define pb push_back
#define F first
#define S second
#define endl '\n'
#define UQ(x) (x).resize(distance((x).begin(), unique(x.begin(), x.end())))

using namespace std;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64
    rnd64(chrono::high_resolution_clock::now().time_since_epoch().count());

ll power(ll a, ll m, ll mod) {
  ll ans = 1;
  while (m) {
    if (m % 2)
      ans *= a, ans %= (mod);
    a = (a * a) % (mod);
    m >>= 1;
  }
  return ans;
}

void init(ll *a, ll n, ll val) {
  for (ll i = 0; i < n; i++)
    a[i] = val;
}

void solve() {
  ll i, j, _;
  ll n, k;
  cin >> n >> k;
  ll a[n];
  rep(i, 0, n) cin >> a[i];
  ll l = 1, r = 1e9 + 1, ans = r;
  while (l <= r) {
    ll mid = (l + r) / 2;
    ll now = 0;
    rep(i, 0, n) {
      now += a[i] / mid;
      if (a[i] % mid == 0)
        now--;
    }
    if (now > k) {
      l = mid + 1;
    } else {
      r = mid - 1;
      ans = min(ans, mid);
    }
  }
  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll 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;
typedef unsigned long long ull;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N, 0);
  ll minX = 0;
  ll maxX = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (maxX < A[i])
      maxX = A[i];
  }

  while (true) {
    ll X = minX + (maxX - minX) / 2;
    ll c = 0;
    for (int i = 0; i < N; i++) {
      c += ((A[i] % X) == 0 ? A[i] / X - 1 : A[i] / X);
    }
    //		cout << minX << "," << X << "," << maxX  << ";" << c << endl;
    if (c <= K) {
      maxX = X;
    } else {
      minX = X;
    }
    if (maxX - minX <= 1)
      break;
  }
  cout << maxX << endl;
  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N, 0);
  ll minX = 0;
  ll maxX = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (maxX < A[i])
      maxX = A[i];
  }

  while (true) {
    ll X = minX + (maxX - minX) / 2;
    if (X == 0)
      break;
    ll c = 0;
    for (int i = 0; i < N; i++) {
      c += ((A[i] % X) == 0 ? A[i] / X - 1 : A[i] / X);
    }
    //		cout << minX << "," << X << "," << maxX  << ";" << c << endl;
    if (c <= K) {
      maxX = X;
    } else {
      minX = X;
    }
    if (maxX - minX <= 1)
      break;
  }
  cout << maxX << endl;
  return (0);
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  ll maxi = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  ll l = -1, r = (ll)1e9 + 10;
  ll mid = (l + r) / 2;
  ll cnt = 0;
  while (r - l > 1) {
    mid = l + (r - l) / 2;
    cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += (a[i] + mid - 1) / mid - 1;
    }
    if (cnt <= k) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  ll maxi = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  ll l = 0, r = (ll)1e9 + 10;
  ll mid = (l + r) / 2;
  ll cnt = 0;
  while (r - l > 1) {
    mid = l + (r - l) / 2;
    cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += (a[i] + mid - 1) / mid - 1;
    }
    if (cnt <= k) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << endl;
  return 0;
}
Problem p02598 (C++)

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

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

int N, K;
int A[200010];

signed main() {
  cin >> N >> K;
  rep(i, N) cin >> A[i];

  sort(A, A + N);

  if (K == 0) {
    cout << A[N - 1] << endl;
    return 0;
  }

  int l = 0, r = 1001001001001001001;
  rep(i, 70) {
    int mid = (l + r) / 2;
    int cnt = 0;
    rep(j, N) cnt += (A[j] + mid - 1) / mid - 1;
    if (cnt <= K)
      r = mid;
    else
      l = mid;
  }

  cout << r << endl;

  return 0;
}


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

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

int N, K;
int A[200010];

signed main() {
  cin >> N >> K;
  rep(i, N) cin >> A[i];

  sort(A, A + N);

  if (K == 0) {
    cout << A[N - 1] << endl;
    return 0;
  }

  int l = 1, r = 1001001001001001001;
  rep(i, 70) {
    int mid = (l + r) / 2;
    int cnt = 0;
    rep(j, N) cnt += (A[j] + mid - 1) / mid - 1;
    if (cnt <= K)
      r = mid;
    else
      l = mid;
  }

  cout << r << endl;

  return 0;
}
Problem p02598 (C++)

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

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vi vec(n);
  forn(i, n) { cin >> vec[i]; }
  sort(all(vec));
  reverse(all(vec));

  int l = 0;
  int r = vec[0] + 1;
  int ans = INT_MAX;
  while (l <= r) {
    int mid = (l + r) / 2;
    bool ok = true;
    int cnt = 0;

    forn(i, n) {
      if (vec[i] <= mid) {
        break;
      }
      cnt += vec[i] / mid;
      if (cnt > k) {
        ok = false;
        break;
      }
    }
    if (ok) {
      ans = min(mid, ans);
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  if (ans == INT_MAX) {
    ans = vec[0];
  }
  cout << ans;

  return 0;
}

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

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vi vec(n);
  forn(i, n) { cin >> vec[i]; }
  sort(all(vec));
  reverse(all(vec));

  int l = 1;
  int r = vec[0] + 1;
  int ans = INT_MAX;
  while (l <= r) {
    int mid = (l + r) / 2;
    bool ok = true;
    int cnt = 0;

    forn(i, n) {
      if (vec[i] <= mid) {
        break;
      }
      cnt += vec[i] / mid;
      if (cnt > k) {
        ok = false;
        break;
      }
    }
    if (ok) {
      ans = min(mid, ans);
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  if (ans == INT_MAX) {
    ans = vec[0];
  }
  cout << ans;

  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 REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using pi = pair<int, int>;
const ll INF = 1LL << 60;

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


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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using pi = pair<int, int>;
const ll INF = 1LL << 60;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> arr(n);
  rep(i, n) cin >> arr[i];
  int l = 0, r = 1e9;
  while (r - l > 1) {
    int m = (l + r) / 2;
    auto cut = [&](int m) {
      ll cnt = 0;
      rep(i, n) cnt += (arr[i] - 1) / m;
      return cnt <= k;
    };
    if (cut(m))
      r = m;
    else
      l = m;
  }
  cout << r << 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 pair<int, int> P;
const ll mod = 1000000007;
const ll INF = 1e+14;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define _GLIBCXX_DEBUG

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

  ll ng = -1;
  ll ok = 10000000001;
  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    ll count = 0;
    rep(i, n) {
      if (count > k)
        break;
      count += (a[i] + mid - 1) / mid - 1;
    }
    if (count <= k)
      ok = mid;
    else
      ng = mid;
  }

  cout << ok << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
const ll mod = 1000000007;
const ll INF = 1e+14;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define _GLIBCXX_DEBUG

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

  ll ng = 0;
  ll ok = 10000000001;
  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    ll count = 0;
    rep(i, n) {
      if (count > k)
        break;
      count += (a[i] + mid - 1) / mid - 1;
    }
    if (count <= k)
      ok = mid;
    else
      ng = mid;
  }

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

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

bool check(ll mid, ll k, ll *arr, ll n) {
  for (ll i = 0; i < n; i++) {
    if (arr[i] > mid) {
      ll val = ((arr[i] + mid - 1) / mid) - 1;
      if (k - val >= 0)
        k -= val;
      else
        return false;
    }
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, k, i, lo = 0, hi = 0;
  cin >> n >> k;
  ll arr[n];
  for (i = 0; i < n; i++) {
    cin >> arr[i];
    hi = max(arr[i], hi);
  }

  ll ans = hi;
  hi = 1e9 + 100;
  while (lo <= hi) {
    ll mid = (lo + hi) / 2;

    bool chk = check(mid, k, arr, n);

    if (chk == true) {
      hi = mid - 1;
      ans = min(ans, mid);
    } else
      lo = mid + 1;
  }
  cout << ans;
  return 0;
}

Fixed submission:
// Code by Mukul Totla
#include <bits/stdc++.h>
using namespace std;
#define ll long long

bool check(ll mid, ll k, ll *arr, ll n) {
  for (ll i = 0; i < n; i++) {
    if (arr[i] > mid) {
      ll val = ((arr[i] + mid - 1) / mid) - 1;
      if (k - val >= 0)
        k -= val;
      else
        return false;
    }
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, k, i, lo = 0, hi = 0;
  cin >> n >> k;
  ll arr[n];
  for (i = 0; i < n; i++) {
    cin >> arr[i];
    hi = max(arr[i], hi);
  }

  ll ans = hi;
  hi = 1e9 + 100;
  while (lo <= hi) {
    ll mid = (lo + hi) / 2;
    bool chk;
    if (mid > 0)
      chk = check(mid, k, arr, n);
    else
      chk = false;
    if (chk == true) {
      hi = mid - 1;
      ans = min(ans, mid);
    } else
      lo = mid + 1;
  }
  cout << ans;
  return 0;
}
Problem p02598 (C++)

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

using namespace std;
typedef long long ll;
typedef pair<int, ll> pa;

const int N = 2e5 + 100;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

ll a[N];
ll n, k;
bool check(int x) {
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += a[i] / x;
    if (a[i] % x == 0)
      sum--;
  }
  if (sum <= k)
    return true;
  else
    return false;
}
int main() {
  ll ans = 1e9;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll l = 0, r = 1e9;
  while (r >= l) {
    ll mid = l + r >> 1;
    if (check(mid))
      ans = min(ans, mid), r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans << endl;
}


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

using namespace std;
typedef long long ll;
typedef pair<int, ll> pa;

const int N = 2e5 + 100;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

ll a[N];
ll n, k;
bool check(int x) {
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += a[i] / x;
    if (a[i] % x == 0)
      sum--;
  }
  if (sum <= k)
    return true;
  else
    return false;
}
int main() {
  ll ans = 1e9;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll l = 1, r = 1e9;
  while (r >= l) {
    ll mid = l + r >> 1;
    if (check(mid))
      ans = min(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;

#define int long long

bool check(int n, int k, vector<int> a, int m) {
  int cuts_need = 0;
  for (int i : a) {
    cuts_need += (i - 1) / m;
  }
  return cuts_need <= k;
}

signed main() {
  ios::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 lo = 0, hi = 1000000000;
  while (lo < hi) {
    int mid = (lo + hi) / 2;

    if (check(n, k, a, mid)) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  cout << lo << '\n';

  return 0;
}


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

#define int long long

bool check(int n, int k, vector<int> a, int m) {
  int cuts_need = 0;
  for (int i : a) {
    cuts_need += (i - 1) / m;
  }
  return cuts_need <= k;
}

signed main() {
  ios::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 lo = 1, hi = 1000000000;
  while (lo < hi) {
    int mid = (lo + hi) / 2;

    if (check(n, k, a, mid)) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  cout << lo << '\n';

  return 0;
}
Problem p02598 (C++)

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

using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define fst first
#define sc second
#define snd second
#define sz(X) (int)(X).size()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define fornr(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define forab(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(X) X.begin(), X.end()

typedef long long ll;
typedef vector<pair<ll, ll>> vpll;
typedef pair<int, int> pii;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ld;
typedef vector<int> vi;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  vi a(n);
  forn(i, n) cin >> a[i];
  int l = 0, r = 1e9 + 1;
  while (l != r) {
    int m = (l + r) / 2;
    int cnt = 0;
    forn(i, n) cnt += (a[i] - 1) / m;
    if (cnt <= k)
      r = m;
    else
      l = m + 1;
  }
  cout << l << "\n";
  return 0;
}

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

using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define fst first
#define sc second
#define snd second
#define sz(X) (int)(X).size()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define fornr(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define forab(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(X) X.begin(), X.end()

typedef long long ll;
typedef vector<pair<ll, ll>> vpll;
typedef pair<int, int> pii;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ld;
typedef vector<int> vi;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  vi a(n);
  forn(i, n) cin >> a[i];
  int l = 1, r = 1e9 + 1;
  while (l != r) {
    int m = (l + r) / 2;
    int cnt = 0;
    forn(i, n) cnt += (a[i] - 1) / m;
    if (cnt <= k)
      r = m;
    else
      l = m + 1;
  }
  cout << l << "\n";
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 50;
ll n, k;
ll a[maxn];
inline int log_number(int x, int mid) {
  if (x % mid == 0)
    return x / mid;
  else
    return x / mid + 1;
}
bool check(int x) {
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans += log_number(a[i], x) - 1;
  }
  if (ans <= k)
    return true;
  else
    return false;
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  int l = 1, r = 1e9;
  int ans = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid - 1, ans = mid;
    else
      l = mid + 1;
  }
  /*if(check(l))  cout<<l<<endl;
  else cout<<r<<endl;*/
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 50;
ll n, k;
ll a[maxn];
inline int log_number(int x, int mid) {
  if (x % mid == 0)
    return x / mid;
  else
    return x / mid + 1;
}
bool check(int x) {
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans += log_number(a[i], x) - 1;
  }
  if (ans <= k)
    return true;
  else
    return false;
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  int l = 1, r = 1e9;
  int ans = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid - 1, ans = mid;
    else
      l = mid + 1;
  }
  /*if(check(l))  cout<<l<<endl;
  else cout<<r<<endl;*/
  cout << ans << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll copy(ll x) { return x; }
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll r = INF;
  ll l = -1LL;
  while (r - l > 1) {
    ll m = (r + l) / 2;
    ll ans = 0;
    for (int i = 0; i < n; i++) {
      ll x = copy(a[i]);
      ans += ((x + m - 1) / m - 1);
      if (ans > k) {
        break;
      }
    }
    if (ans <= k) {
      // out = min(out,res);
      r = m;
    } else {
      l = m;
    }
  }
  cout << r << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll copy(ll x) { return x; }
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll r = LINF;
  ll l = 0LL;
  while (r - l > 1) {
    ll m = (r + l) / 2;
    ll ans = 0;
    for (int i = 0; i < n; i++) {
      ll x = copy(a[i]);
      ans += ((x + m - 1) / m - 1);
      if (ans > k) {
        break;
      }
    }
    if (ans <= k) {
      // out = min(out,res);
      r = m;
    } else {
      l = m;
    }
  }
  cout << r << endl;

  return 0;
}
Problem p02598 (C++)

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

const int N = 2e5 + 5;

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

bool check(int cur) {
  int cur_ans = 0;
  for (int i = 1; i <= n; ++i) {
    int cnt = 0;
    cnt = (a[i] - 1) / cur + 1;
    cur_ans += cnt - 1;
  }
  return cur_ans <= k;
}

int32_t main() {
  std::cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    std::cin >> a[i];
    r = std::max(r, a[i]);
  }
  while (l <= r) {
    int mid = l + r + 1 >> 1;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  printf("%d", ans);
  return 0;
}

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

const int N = 2e5 + 5;

int n, k, a[N], l = 1, r, ans;

bool check(int cur) {
  int cur_ans = 0;
  for (int i = 1; i <= n; ++i) {
    int cnt = 0;
    cnt = (a[i] - 1) / cur + 1;
    cur_ans += cnt - 1;
  }
  return cur_ans <= k;
}

int32_t main() {
  std::cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    std::cin >> a[i];
    r = std::max(r, a[i]);
  }
  while (l <= r) {
    int mid = l + r + 1 >> 1;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  printf("%d", ans);
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
using UL = unsigned int;
using ULL = unsigned long long;
using LL = long long;

#define rep(i, n) for (int i = 0; i < (n); i++)
LL floor_usr(LL a, int b) { return (a + (b - 1)) / b; }
const int maxN = 20;
// 当分割するのが最適なメソッド。
// 各丸太についてn[i]回切るとすると、最大n[i]+1で割った値になる。
// n[i](sum(n[i])=K)を最適に分配するという問題に帰着する。
// O(nlog(K))かO(Klog(n))ぐらいならいけそう。後者はソートか？
// 基本、数値に比例した(n+1)を当てれば全ての丸太は均一な大きさになる。
// その和をKに合わせればそもそも計算がいらないか。A[i]=min*(n[i]+1)
int main() {
  int N, K;
  scanf("%d %d", &N, &K);

  LL A[maxN];

  LL sumA = 0;
  vector<vector<LL>> dn;
  rep(i, N) scanf("%ld", &A[i]);
  sort(A, A + N);
  rep(i, N) sumA += A[i];
  int n;
  int sumn = 0;
  rep(i, N) {
    if (sumA != 0)
      n = K * A[i] / sumA;
    else
      n = 0;
    sumn += n;
    dn.push_back({floor_usr(A[i], (n + 1)), A[i], n});
  }

  vector<LL> B;
  sort(dn.begin(), dn.begin() + N, greater<vector<LL>>());
  rep(i, K - sumn) {
    dn[i % N][2]++;
    dn[i % N][0] = floor_usr(dn[i % N][1], dn[i % N][2] + 1);
  }
  sort(dn.begin(), dn.begin() + N, greater<vector<LL>>());
  printf("%ld", dn[0][0]);
  // 真の値より一回小さいnが得られているので、追加するiを選んで最小値を求める
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
using UL = unsigned int;
using ULL = unsigned long long;
using LL = long long;

#define rep(i, n) for (int i = 0; i < (n); i++)
LL floor_usr(LL a, int b) { return (a + (b - 1)) / b; }
const int maxN = 200001;
// 当分割するのが最適なメソッド。
// 各丸太についてn[i]回切るとすると、最大n[i]+1で割った値になる。
// n[i](sum(n[i])=K)を最適に分配するという問題に帰着する。
// O(nlog(K))かO(Klog(n))ぐらいならいけそう。後者はソートか？
// 基本、数値に比例した(n+1)を当てれば全ての丸太は均一な大きさになる。
// その和をKに合わせればそもそも計算がいらないか。A[i]=min*(n[i]+1)
int main() {
  int N, K;
  scanf("%d %d", &N, &K);

  LL A[maxN];

  LL sumA = 0;
  vector<vector<LL>> dn;
  rep(i, N) scanf("%ld", &A[i]);
  sort(A, A + N);
  rep(i, N) sumA += A[i];
  int n;
  int sumn = 0;
  rep(i, N) {
    if (sumA != 0)
      n = K * A[i] / sumA;
    else
      n = 0;
    sumn += n;
    dn.push_back({floor_usr(A[i], (n + 1)), A[i], n});
  }

  vector<LL> B;
  sort(dn.begin(), dn.begin() + N, greater<vector<LL>>());
  rep(i, K - sumn) {
    dn[i % N][2]++;
    dn[i % N][0] = floor_usr(dn[i % N][1], dn[i % N][2] + 1);
  }
  sort(dn.begin(), dn.begin() + N, greater<vector<LL>>());
  printf("%ld", dn[0][0]);
  // 真の値より一回小さいnが得られているので、追加するiを選んで最小値を求める
  return 0;
}
Problem p02598 (C++)

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

int n;
ll k;
vector<ll> a;

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

int main() {
  cin >> n >> k;
  // a=vector<ll>(n);
  for (int i = 0; i < n; ++i) {
    ll aa;
    cin >> aa;
    a.push_back(aa);
  }

  sort(a.rbegin(), a.rend());
  ll high = a[0] + 10;
  ll row = 0;
  while (high - row > 0) {
    ll mid = (high + row) / 2;
    if (check(mid))
      high = mid;
    else
      row = mid + 1;
  }

  cout << row << endl;
  return 0;
}


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

int n;
ll k;
vector<ll> a;

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

int main() {
  cin >> n >> k;
  // a=vector<ll>(n);
  for (int i = 0; i < n; ++i) {
    ll aa;
    cin >> aa;
    a.push_back(aa);
  }

  sort(a.rbegin(), a.rend());
  ll high = a[0];
  ll row = 1;
  while (high - row > 0) {
    ll mid = (high + row) / 2;
    if (check(mid))
      high = mid;
    else
      row = mid + 1;
  }

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

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

using namespace std;
using ll = long long;

ll di(vector<ll> &A, int len) {
  ll ret{};
  for (ll &a : A)
    ret += (a - 1) / len + 1;
  return ret;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (ll &a : A)
    cin >> a;
  int lower{-1}, upper{1000000001};
  while (upper - lower > 1) {
    auto mid = lower + (upper - lower) / 2;
    if (di(A, mid) <= N + K) {
      upper = mid;
    } else {
      lower = mid;
    }
  }
  cout << upper << endl;
}


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

using namespace std;
using ll = long long;

ll di(vector<ll> &A, int len) {
  ll ret{};
  for (ll &a : A)
    ret += (a - 1) / len + 1;
  return ret;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (ll &a : A)
    cin >> a;
  int lower{0}, upper{1000000001};
  while (upper - lower > 1) {
    auto mid = lower + (upper - lower) / 2;
    if (di(A, mid) <= N + K) {
      upper = mid;
    } else {
      lower = mid;
    }
  }
  cout << upper << endl;
}
Problem p02598 (C++)

Buggy submission (status: Time Limit Exceeded):
// #pragma warning(disable:4996)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>
#include <bitset>
#include <complex>
#include <cstdio>
#include <list>
// #include <stdio.h>

//< in.txt > out.txt
using namespace std;
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
const long long MOD = 10000; /// 1e9 + 7;
const long long INF = 1e9;
typedef long long LL;
typedef long double LD;
// typedef boost::multiprecision::cpp_int bigint;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PI;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef unsigned long long ULL;

VI A;
int N, K;

int count(LD x) {
  int ans = 0;
  for (int n = 0; n < N; n++) {
    int t = ceil((LD)A[n] / x);
    ans += t - 1;
  }
  return ans;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cin >> N >> K;
  A.resize(N);
  for (int n = 0; n < N; n++)
    cin >> A[n];
  // sなら<=K回、eなら>K回
  LD s = 1e9, e = 0;
  for (int t = 0; t < 200; t++) {
    LD m = (e + s) / 2;
    if (count(m) <= K)
      s = m;
    else
      e = m;
  }
  cout << (int)ceil(s) << "\n";
  return 0;
}


Fixed submission:
// #pragma warning(disable:4996)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>
#include <bitset>
#include <complex>
#include <cstdio>
#include <list>
// #include <stdio.h>

//< in.txt > out.txt
using namespace std;
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
const long long MOD = 10000; /// 1e9 + 7;
const long long INF = 1e9;
typedef long long LL;
typedef long double LD;
// typedef boost::multiprecision::cpp_int bigint;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PI;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef unsigned long long ULL;

VI A;
int N, K;

int count(LD x) {
  int ans = 0;
  for (int n = 0; n < N; n++) {
    int t = ceil((LD)A[n] / x);
    ans += t - 1;
  }
  return ans;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cin >> N >> K;
  A.resize(N);
  for (int n = 0; n < N; n++)
    cin >> A[n];
  // sなら<=K回、eなら>K回
  LD s = 1e9, e = 0;
  for (int t = 0; t < 100; t++) {
    LD m = (e + s) / 2;
    if (count(m) <= K)
      s = m;
    else
      e = m;
  }
  cout << (int)ceil(s) << "\n";
  return 0;
}