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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define IO_OP                                                                  \
  std::ios::sync_with_stdio(0);                                                \
  std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << " is " << x << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7, N = 2e5 + 7;

int a[N];
int n, k;

bool ok(int m) {
  ll cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (a[i] + m - 1) / m - 1;
  return cnt <= k;
}

signed main() {
  IO_OP;

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


Fixed submission:
#include <bits/stdc++.h>
#define IO_OP                                                                  \
  std::ios::sync_with_stdio(0);                                                \
  std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << " is " << x << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7, N = 2e5 + 7;

int a[N];
int n, k;

bool ok(int m) {
  ll cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (a[i] + m - 1) / m - 1;
  return cnt <= k;
}

signed main() {
  IO_OP;

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

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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

int N;
ll K;
ll A[202020];

ll cut(ll v) {
  ll num = 0;
  int i;
  FOR(i, N) {
    num += (A[i] - 1) / v;
    if (num > K)
      return num;
  }
  return num;
}

void solve() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> N >> K;
  FOR(i, N) cin >> A[i];

  ll L = (1 << 30) - 1;

  for (i = 29; i >= 0; i--) {
    if (cut(L - (1 << i)) <= K)
      L -= 1 << i;
  }
  cout << L << endl;
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}


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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

int N;
ll K;
ll A[202020];

ll cut(ll v) {
  ll num = 0;
  int i;
  FOR(i, N) {
    num += (A[i] - 1) / v;
    if (num > K)
      return num;
  }
  return num;
}

void solve() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> N >> K;
  FOR(i, N) cin >> A[i];

  ll L = (1 << 30);

  for (i = 29; i >= 0; i--) {
    if (cut(L - (1 << i)) <= K)
      L -= 1 << i;
  }
  cout << L << endl;
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
double X[200100];
int N;
long long Count(double len) {
  long long Ans = 0;
  for (int i = 0; i < N; i++) {
    // Ans+=(long long)(X[i]/len);//解答引っ張ってきた人の条件。なぜか通る
    Ans += ceil(X[i] / len) - 1; // 多少遅くなるがこっちが正解
  }
  return Ans;
}

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

  // long double l=1,r=1e9;
  double l = 1, r = 1e9;

  // while(l<r-0.1)//解答を引っ張ってきた人の条件。なぜかOKだったが、lとｒの整数部が異なる状態でループを出ると問題になるはず
  // for(int i=0;i<35;i++)//OK
  // log2(10^9)=30.二分探索だとこのやり方安定する。ただし、浮動小数のせいか34以上でないと通らず。
  // while(!((ceil(r)-ceil(l)) ==
  // 0))//NG:左端が初期値0のままだと、右端のceilは0にならず、無限ループ
  // while(!((floor(r)-floor(l)) ==
  // 0))//OK:右端がぴったり整数にならなければ成立.(あまりよくない)
  while ((floor(r) - floor(l)) >
         0) // OK:右端がぴったり整数にならなければ成立.(あまりよくない)
  {
    // long double mid=(l+r)/2;
    double mid = (l + r) / 2;
    if (Count(mid) <= K)
      r = mid;
    else
      l = mid;
  }
  cout << (long long)(l + 0.999) << endl;
  return 0;
}

Fixed submission:
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
double X[200100];
int N;
long long Count(double len) {
  long long Ans = 0;
  for (int i = 0; i < N; i++) {
    Ans += (long long)(X[i] / len); // 解答引っ張ってきた人の条件。なぜか通る
    // Ans+= ceil(X[i]/len)-1;//多少遅くなるがこっちが正解
  }
  return Ans;
}

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

  // long double l=1,r=1e9;
  double l = 1, r = 1e9;

  // while(l<r-0.1)//解答を引っ張ってきた人の条件。なぜかOKだったが、lとｒの整数部が異なる状態でループを出ると問題になるはず
  // for(int i=0;i<35;i++)//OK
  // log2(10^9)=30.二分探索だとこのやり方安定する。ただし、浮動小数のせいか34以上でないと通らず。
  // while(!((ceil(r)-ceil(l)) ==
  // 0))//NG:左端が初期値0のままだと、右端のceilは0にならず、無限ループ
  // while(!((floor(r)-floor(l)) ==
  // 0))//OK:右端がぴったり整数にならなければ成立.(あまりよくない)
  while ((floor(r) - floor(l)) >
         0) // OK:右端がぴったり整数にならなければ成立.(あまりよくない)
  {
    // long double mid=(l+r)/2;
    double mid = (l + r) / 2;
    if (Count(mid) <= K)
      r = mid;
    else
      l = mid;
  }
  cout << (long long)(l + 0.999) << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define lli long long int
#define tc                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define pb push_back
#define vi vector<int>
#define vll vector<lli>
#define maxn 1771562
#define mod 1000000007
#define inf 10000000

int main() {
  fastio;
  int n, k;
  cin >> n >> k;
  lli a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  lli l = 0, h = 1e9, ans;
  while (l <= h) {
    lli mid = (l + h) / 2;
    lli ca = 0;
    for (int i = 0; i < n; i++) {
      ca += a[i] / mid - 1;
      if (a[i] % mid)
        ca++;
    }
    // cout<<mid<<" "<<ca<<"\n";
    if (ca <= k)
      ans = mid, h = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define lli long long int
#define tc                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define pb push_back
#define vi vector<int>
#define vll vector<lli>
#define maxn 1771562
#define mod 1000000007
#define inf 10000000

int main() {
  fastio;
  int n, k;
  cin >> n >> k;
  lli a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  lli l = 1, h = 1e9, ans;
  while (l <= h) {
    lli mid = (l + h) / 2;
    lli ca = 0;
    for (int i = 0; i < n; i++) {
      ca += a[i] / mid - 1;
      if (a[i] % mid)
        ca++;
    }
    // cout<<mid<<" "<<ca<<"\n";
    if (ca <= k)
      ans = mid, h = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans << "\n";
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define mid ((l + r) >> 1)
#define MAXN (int)(1e5 + 2333)
int a[MAXN];
inline int f(int x, int y) {
  if (x % y)
    return (int)(x / y) + 1;
  return x / y;
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int l = 1, r = 1e9 + 1, sum, ans = 1e9;
  while (l < r) {
    sum = 0;
    //		printf(";%d %d %d\n",l,mid,r);
    for (int i = 1; i <= n; i++)
      sum += (f(a[i], mid) - 1);
    //		printf("--- %d\n",sum);
    if (sum > k)
      l = mid + 1;
    else {
      ans = mid;
      r = mid;
    }
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define mid ((l + r) >> 1)
#define MAXN (int)(2e5 + 2333)
int a[MAXN];
inline int f(int x, int y) {
  if (x % y)
    return (int)(x / y) + 1;
  return x / y;
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int l = 1, r = 1e9 + 1, sum, ans = 1e9;
  while (l < r) {
    sum = 0;
    //		printf(";%d %d %d\n",l,mid,r);
    for (int i = 1; i <= n; i++)
      sum += (f(a[i], mid) - 1);
    //		printf("--- %d\n",sum);
    if (sum > k)
      l = mid + 1;
    else {
      ans = mid;
      r = mid;
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define rep(i, n)                                                              \
  ;                                                                            \
  for (long long i = 0; i < (n); i++)
using ll = long long;
using P = pair<long long, long long>;
template <class T> using vec = vector<T>;

ll n, k;
vec<ll> a;
bool cut(ll x) {
  ll p = 0;
  rep(i, n) p += (a[i] - 1) / x;
  return p <= k;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  a.resize(n);
  rep(i, n) cin >> a[i];
  sort(a.rbegin(), a.rend());
  ll left = 0, right = a[0], mid;
  while (left + 1 < right) {
    mid = (left + right) / 2;
    cut(mid) ? right = mid : left = mid;
  }
  cout << (cut(left) ? left : right) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define rep(i, n)                                                              \
  ;                                                                            \
  for (long long i = 0; i < (n); i++)
using ll = long long;
using P = pair<long long, long long>;
template <class T> using vec = vector<T>;

ll n, k;
vec<ll> a;
bool cut(ll x) {
  if (x == 0)
    return false;
  ll p = 0;
  rep(i, n) p += (a[i] - 1) / x;
  return p <= k;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  a.resize(n);
  rep(i, n) cin >> a[i];
  sort(a.rbegin(), a.rend());
  ll left = 0, right = a[0], mid;
  while (left + 1 < right) {
    mid = (left + right) / 2;
    cut(mid) ? right = mid : left = mid;
  }
  cout << (cut(left) ? left : right) << endl;
}
Problem p02598 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long double X[200100];
int N;
long long Count(long double len) {
  long long Ans = 0;
  for (int i = 0; i < N; i++) {
    // Ans+=(long long)(X[i]/len);//解答引っ張ってきた人の条件。なぜか通る
    Ans += ceil(X[i] / len) - 1; // 多少遅くなるがこっちが正解
  }
  return Ans;
}

int main() {
  long long K;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> X[i];
    // X[i]-=0.1;
  }

  long double l = 1, r = 1e9;
  // while(l<r-0.1)//解答を引っ張ってきた人の条件。なぜかOKだったが、lとｒの整数部が異なる状態でループを出ると問題になるはず
  // for(int i=0;i<35;i++)//OK
  // log2(10^9)=30.二分探索だとこのやり方安定する。ただし、浮動小数のせいか34以上でないと通らず。
  // while(!((ceil(r)-ceil(l)) ==
  // 0))//NG:左端が初期値0のままだと、右端のceilは0にならず、無限ループ
  while (!((floor(r) - floor(l)) ==
           0)) // OK:右端がぴったり整数にならなければ成立.(あまりよくない)
  {
    long double mid = (l + r) / 2;
    if (Count(mid) <= K)
      r = mid;
    else
      l = mid;
  }
  cout << (long long)(l + 0.999) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long double X[200100];
int N;
long long Count(long double len) {
  long long Ans = 0;
  for (int i = 0; i < N; i++) {
    Ans += (long long)(X[i] / len); // 解答引っ張ってきた人の条件。なぜか通る
    // Ans+= ceil(X[i]/len)-1;//多少遅くなるがこっちが正解
  }
  return Ans;
}

int main() {
  long long K;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> X[i];
    // X[i]-=0.1;
  }

  long double l = 1, r = 1e9;
  // while(l<r-0.1)//解答を引っ張ってきた人の条件。なぜかOKだったが、lとｒの整数部が異なる状態でループを出ると問題になるはず
  // for(int i=0;i<35;i++)//OK
  // log2(10^9)=30.二分探索だとこのやり方安定する。ただし、浮動小数のせいか34以上でないと通らず。
  // while(!((ceil(r)-ceil(l)) ==
  // 0))//NG:左端が初期値0のままだと、右端のceilは0にならず、無限ループ
  while (!((floor(r) - floor(l)) ==
           0)) // OK:右端がぴったり整数にならなければ成立.(あまりよくない)
  {
    long double mid = (l + r) / 2;
    if (Count(mid) <= K)
      r = mid;
    else
      l = mid;
  }
  cout << (long long)(l + 0.999) << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
using namespace std;
int a[200005] = {0};
int n, k;
int solve(int fun) {
  int num = 0;
  for (int i = 0; i < n; i++) {
    num += (a[i] - 1) / fun;
  }
  if (num <= k)
    return 1;
  return 0;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int l = 0, r = 1000000007;
  int ans = 0;
  while (l < r) {
    int m = (l + r) / 2;
    if (solve(m)) {
      r = m;
      ans = m;
    } else
      l = m + 1;
    // cout << l << " " << r << endl;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
using namespace std;
int a[200005] = {0};
int n, k;
int solve(int fun) {
  int num = 0;
  for (int i = 0; i < n; i++) {
    num += (a[i] - 1) / fun;
  }
  if (num <= k)
    return 1;
  return 0;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int l = 1, r = 1000000007;
  int ans = 0;
  while (l < r) {
    int m = (l + r) / 2;
    if (solve(m)) {
      r = m;
      ans = m;
    } else
      l = m + 1;
    // cout << l << " " << r << endl;
  }
  cout << ans << endl;
  return 0;
}
Problem p02598 (C++)

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

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  sort(A.begin(), A.end(), greater<ll>());
  ll l = 0, r = 1000000000;
  ll m = (l + r) / 2;
  while (r - l > 0) {
    ll Count = 0;
    rep(i, N) {
      if (A[i] > m) {
        Count += A[i] / m;
      }
    }
    if (Count <= K) {
      r = m;
      m = (l + r) / 2;
    } else {
      l = m + 1;
      m = (l + r) / 2;
    }
  }
  cout << m << endl;
}

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

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  sort(A.begin(), A.end(), greater<ll>());
  ll l = 1, r = 1000000000;
  ll m = (l + r) / 2;
  while (r - l > 0) {
    ll Count = 0;
    rep(i, N) {
      if (A[i] > m) {
        Count += A[i] / m;
      }
    }
    if (Count <= K) {
      r = m;
      m = (l + r) / 2;
    } else {
      l = m + 1;
      m = (l + r) / 2;
    }
  }
  cout << m << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define io std::ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int P = 1e9 + 7, INF = 0x3f3f3f3f;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int qpow(int a, int n) {
  int r = 1 % P;
  for (a %= P; n; a = a * a % P, n >>= 1)
    if (n & 1)
      r = r * a % P;
  return r;
}
const int maxn = 1e5 + 10;

int a[maxn];
int n, k;
int check(int x) {
  int kk = 0;
  for (int i = 1; i <= n; i++) {
    kk += (a[i] - 1) / x;
  }
  if (kk > k)
    return 0;
  else
    return 1;
}
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;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define io std::ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int P = 1e9 + 7, INF = 0x3f3f3f3f;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int qpow(int a, int n) {
  int r = 1 % P;
  for (a %= P; n; a = a * a % P, n >>= 1)
    if (n & 1)
      r = r * a % P;
  return r;
}
const int maxn = 2e5 + 50;

int a[maxn];
int n, k;
int check(int x) {
  int kk = 0;
  for (int i = 1; i <= n; i++) {
    kk += (a[i] - 1) / x;
  }
  if (kk > k)
    return 0;
  else
    return 1;
}
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;
    }
  }
  cout << ans << endl;
}
Problem p02598 (C++)

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

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

const int MAXN = 2e5 + 1;
ll N, K;
ll A[MAXN];

bool chk(ll l) {
  ll totalCut = 0;
  rep(i, N) { totalCut += (A[i] + l - 1) / l - 1; }
  return totalCut <= K;
}

int main() {
  cin >> N >> K;
  rep(i, N) cin >> A[i];
  ll ans = -1;
  ll lo = 0, hi = 1e9;
  while (lo <= hi) {
    ll mid = (lo + hi) / 2;
    if (chk(mid)) {
      ans = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }
  cout << ans << endl;
}

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

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

const int MAXN = 2e5 + 1;
ll N, K;
ll A[MAXN];

bool chk(ll l) {
  ll totalCut = 0;
  rep(i, N) { totalCut += (A[i] + l - 1) / l - 1; }
  return totalCut <= K;
}

int main() {
  cin >> N >> K;
  rep(i, N) cin >> A[i];
  ll ans = -1;
  ll lo = 1, hi = 1e9;
  while (lo <= hi) {
    ll mid = (lo + hi) / 2;
    if (chk(mid)) {
      ans = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }
  cout << ans << endl;
}
Problem p02598 (C++)

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

#define ll long long
#define VI vector<int>
#define VLL vector<ll>
#define PII pair<int, int>
#define mp make_pair
#define pb push_back
#define PI acos(-1)
#define ld long double
const int MxN = 200001;
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)
const int MOD = 1e9 + 7;

/* VARIABLES */
int n, k;
VI logs(MxN);
/* END OF VARIABLES */

ll fun(int v) {
  ll cnt = 0;
  FOR(i, 0, n) {
    if (logs[i] <= v)
      continue;
    int rem = (logs[i] + v - 1) / v;
    // cout << rem << " " ;
    cnt += rem - 1;
  }
  return cnt;
}

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

  cin >> n >> k;
  FOR(i, 0, n)
  cin >> logs[i];
  ll l = 0;
  ll r = 1e9;
  ll ans = 1e9;
  while (l <= r) {
    ll m = l + (r - l) / 2;
    // cout << m << " " << fun(m) << endl ;
    if (fun(m) > k)
      l = m + 1;
    else {
      ans = min(ans, m);
      r = m - 1;
    }
  }
  cout << ans;
  cout << endl;
  // cout << fun(5) ;

  return 0;
}


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

#define ll long long
#define VI vector<int>
#define VLL vector<ll>
#define PII pair<int, int>
#define mp make_pair
#define pb push_back
#define PI acos(-1)
#define ld long double
const int MxN = 200001;
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)
const int MOD = 1e9 + 7;

/* VARIABLES */
int n, k;
VI logs(MxN);
/* END OF VARIABLES */

ll fun(int v) {
  ll cnt = 0;
  FOR(i, 0, n) {
    if (logs[i] <= v)
      continue;
    int rem = (logs[i] + v - 1) / v;
    // cout << rem << " " ;
    cnt += rem - 1;
  }
  return cnt;
}

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

  cin >> n >> k;
  FOR(i, 0, n)
  cin >> logs[i];
  ll l = 1;
  ll r = 1e9;
  ll ans = 1e9;
  while (l <= r) {
    ll m = l + (r - l) / 2;
    // cout << m << " " << fun(m) << endl ;
    if (fun(m) > k)
      l = m + 1;
    else {
      ans = min(ans, m);
      r = m - 1;
    }
  }
  cout << ans;
  cout << endl;
  // cout << fun(5) ;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef bool boool;
typedef long long ll;
#define vl vector<ll>
#define vb vector<boool>
#define vs vector<string>
#define vp vector<pair<ll, ll>>
#define vvl vector<vector<ll>>
#define vvp vector<vector<pair<ll, ll>>>
#define mod 1000000007
#define all(x) x.begin(), x.end()
#define rep1(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rep2(i, s, e) for (long long i = (s); i < (long long)(e); i++)
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)

int main() {
  ll n, k;
  cin >> n >> k;
  vl vec(n);
  rep(i, n) cin >> vec[i];
  sort(all(vec));
  if (k == 0) {
    cout << vec.back() << endl;
    return 0;
  }
  ll ng = -1;
  ll ok = 1000000001;
  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    ll sum = 0;
    rep(i, n) { sum += (vec[i] + (mid - 1)) / mid - 1; }
    if (sum <= k)
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef bool boool;
typedef long long ll;
#define vl vector<ll>
#define vb vector<boool>
#define vs vector<string>
#define vp vector<pair<ll, ll>>
#define vvl vector<vector<ll>>
#define vvp vector<vector<pair<ll, ll>>>
#define mod 1000000007
#define all(x) x.begin(), x.end()
#define rep1(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rep2(i, s, e) for (long long i = (s); i < (long long)(e); i++)
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)

int main() {
  ll n, k;
  cin >> n >> k;
  vl vec(n);
  rep(i, n) cin >> vec[i];
  sort(all(vec));
  ll ng = 0;
  ll ok = 1000000001;
  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    ll sum = 0;
    rep(i, n) { sum += (vec[i] + (mid - 1)) / mid - 1; }
    if (sum <= 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;

int n, k;
vector<int> a(20000);

bool good(int x) {
  int cut = 0;
  for (int i = 0; i < n; i++) {
    cut += a[i] / x;
    if (a[i] % x == 0)
      cut--;
    if (cut > k)
      return false;
  }
  return true;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int upper = *max_element(a.begin(), a.end());
  int lower = 0;
  int mid = 1;
  while (upper - lower > 1) {
    mid = (upper + lower) / 2;
    if (good(mid)) {
      upper = mid;
    } else {
      lower = mid;
    }
  }
  cout << upper << '\n';
  return 0;
}

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

using namespace std;

int n, k;
vector<int> a(200000);

bool good(int x) {
  int cut = 0;
  for (int i = 0; i < n; i++) {
    cut += a[i] / x;
    if (a[i] % x == 0)
      cut--;
    if (cut > k)
      return false;
  }
  return true;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int upper = *max_element(a.begin(), a.end());
  int lower = 0;
  int mid = 1;
  while (upper - lower > 1) {
    mid = (upper + lower) / 2;
    if (good(mid)) {
      upper = mid;
    } else {
      lower = mid;
    }
  }
  cout << upper << '\n';
  return 0;
}
Problem p02598 (C++)

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

using namespace std;

using ll = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (auto &&e : a) {
    cin >> e;
  }
  int upper = *max_element(a.begin(), a.end()) + 1;
  int lower = -1;
  while (upper - lower > 1) {
    int mid = lower + (upper - lower) / 2;
    auto ok = [&](int x) {
      int cnt = 0;
      for (auto &&e : a) {
        cnt += (e - 1) / x;
      }
      return (cnt <= k);
    };
    if (ok(mid)) {
      upper = mid;
    } else {
      lower = mid;
    }
  }
  cout << upper << '\n';
  return 0;
}


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

using namespace std;

using ll = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (auto &&e : a) {
    cin >> e;
  }
  int upper = *max_element(a.begin(), a.end());
  int lower = 0;
  while (upper - lower > 1) {
    int mid = lower + (upper - lower) / 2;
    auto ok = [&](int x) {
      int cnt = 0;
      for (auto &&e : a) {
        cnt += (e - 1) / x;
      }
      return (cnt <= k);
    };
    if (ok(mid)) {
      upper = mid;
    } else {
      lower = mid;
    }
  }
  cout << upper << '\n';
  return 0;
}
Problem p02598 (C++)

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

#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
#define endl "\n"

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;

void solve() {
  // 解xに対して K回以内の切断で達成かのうか調べる O(Nlogmax(Ai))
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];

  auto isOk = [&](ll key) {
    ll need_cut = 0;
    for (const auto &a : A) {
      // debug
      ll cut = ((a + key - 1) / key) - 1;
      need_cut += cut;
    }
    bool res = need_cut <= K;
    return res;
  };

  // binary search
  ll l = -1;
  ll r = INFLL;

  while ((r - l) > 1) {
    ll mid = (l + r) / 2;
    if (isOk(mid))
      r = mid;
    else
      l = mid;
  }

  cout << r << endl;
}

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

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

#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
#define endl "\n"

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;

void solve() {
  // 解xに対して K回以内の切断で達成かのうか調べる O(Nlogmax(Ai))
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];

  auto isOk = [&](ll key) {
    ll need_cut = 0;
    for (const auto &a : A) {
      // debug
      ll cut = ((a + key - 1) / key) - 1;
      need_cut += cut;
    }
    bool res = need_cut <= K;
    return res;
  };

  // binary search
  ll l = 0;
  ll r = INFLL;

  while ((r - l) > 1) {
    ll mid = (l + r) / 2;
    if (isOk(mid))
      r = mid;
    else
      l = mid;
  }

  cout << r << endl;
}

int main() {
  solve();
  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++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(n) begin(n), end(n)
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;
typedef pair<int, int> pint;
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;

bool judge(Int cut, vector<Int> logs, Int k) {
  Int ans = 0;
  for (int i = 0; i < logs.size(); i++) {
    if (logs[i] % cut != 0)
      ans += (logs[i] / cut);
    else
      ans += (logs[i] / cut - 1);
    if (ans > k)
      return false;
  }
  if (ans > k)
    return false;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  Int n, k;
  cin >> n >> k;
  vector<Int> a(n, 0);
  rep(i, n) cin >> a[i];
  Int ok = 1000000100;
  Int ng = -1;
  while (abs(ok - ng) > 1) {
    Int mid = (ok + ng) / 2;
    if (judge(mid, a, k)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << endl;
  return 0;
}

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(n) begin(n), end(n)
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;
typedef pair<int, int> pint;
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;

bool judge(Int cut, vector<Int> logs, Int k) {
  Int ans = 0;
  if (cut == 0)
    return false;
  for (int i = 0; i < logs.size(); i++) {
    if (logs[i] % cut != 0)
      ans += (logs[i] / cut);
    else
      ans += (logs[i] / cut - 1);
    if (ans > k)
      return false;
  }
  if (ans > k)
    return false;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  Int n, k;
  cin >> n >> k;
  vector<Int> a(n, 0);
  rep(i, n) cin >> a[i];
  Int ok = 1000000100;
  Int ng = -1;
  while (abs(ok - ng) > 1) {
    Int mid = (ok + ng) / 2;
    if (judge(mid, a, k)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << endl;
  return 0;
}
Problem p02598 (C++)

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

template <typename T> void print(T t) { cout << t << endl; }
template <typename T, typename... Args> void print(T t, Args... args) {
  cout << t << " ";
  print(args...);
}

typedef complex<double> base;

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl '\n'
#define int long long
#define double long double

int n, k;
int arr[200005];
bool check(int req) {
  int moves = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] <= req)
      continue;
    int ans = 0;
    ans += arr[i] / req;
    if (arr[i] % req == 0)
      ans--;
    moves += ans;
    if (moves > k)
      return 0;
  }
  return 1;
}
int32_t main() {
  IOS cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int low = 0;
  int high = 2e9;
  while (low < high) {
    int mid = (low + high) / 2;
    if (!check(mid))
      low = mid + 1;
    else
      high = mid;
  }
  print(low);
}


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

template <typename T> void print(T t) { cout << t << endl; }
template <typename T, typename... Args> void print(T t, Args... args) {
  cout << t << " ";
  print(args...);
}

typedef complex<double> base;

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl '\n'
#define int long long
#define double long double

int n, k;
int arr[200005];
bool check(int req) {
  int moves = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] <= req)
      continue;
    int ans = 0;
    ans += arr[i] / req;
    if (arr[i] % req == 0)
      ans--;
    moves += ans;
    if (moves > k)
      return 0;
  }
  return 1;
}
int32_t main() {
  IOS cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int low = 1;
  int high = 2e9;
  while (low < high) {
    int mid = (low + high) / 2;
    if (!check(mid))
      low = mid + 1;
    else
      high = mid;
  }
  print(low);
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define vii vector<ll>
#define pb push_back
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
const int MOD = 1e9 + 7, N = 1e4 + 3;

int main() {
  fast;
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  ll low = 0, high = 1e9 + 5, ans;
  while (low <= high) {
    ll mid = (low + high) / 2, sum = 0;
    for (int i = 0; i < n; ++i) {
      ll g;
      if (a[i] < mid)
        continue;
      if (a[i] % mid == 0)
        g = a[i] / mid - 1;
      else
        g = a[i] / mid;
      sum += g;
    }
    if (sum > k)
      low = mid + 1;
    else if (sum <= k) {
      ans = mid;
      high = mid - 1;
    }
  }
  cout << ans;
  cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define vii vector<ll>
#define pb push_back
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
const int MOD = 1e9 + 7, N = 1e4 + 3;

int main() {
  fast;
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  ll low = 1, high = 1e9 + 5, ans;
  while (low <= high) {
    ll mid = (low + high) / 2, sum = 0;
    for (int i = 0; i < n; ++i) {
      ll g;
      if (a[i] < mid)
        continue;
      if (a[i] % mid == 0)
        g = a[i] / mid - 1;
      else
        g = a[i] / mid;
      sum += g;
    }
    if (sum > k)
      low = mid + 1;
    else if (sum <= k) {
      ans = mid;
      high = mid - 1;
    }
  }
  cout << ans;
  cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
  return 0;
}
Problem p02598 (C++)

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

using namespace std;
#define all(a) a.begin(), a.end()
using ll = long long;
const int INF = 1 << 30;
const ll INFll = 1LL << 62;
const int mod = int(1e9) + 7;
using P = pair<ll, ll>;
using ld = long double;

vector<ll> a;
ll n;
ll k;
bool ch(ll x) {
  ll now = 0;
  for (int i = 0; i < n; ++i) {
    now += (a[i] - 1) / x;
  }
  if (now <= k)
    return true;
  return false;
}

int main() {
  cin >> n >> k;
  a.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  ll ok = 1e9;
  ll ng = 0;
  ll cnt = 0;
  while (cnt < 1000) {
    ll m = (ok + ng) / 2;
    if (ch(m))
      ok = m;
    else
      ng = m;
    cnt++;
  }
  cout << ok << endl;
}


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

using namespace std;
#define all(a) a.begin(), a.end()
using ll = long long;
const int INF = 1 << 30;
const ll INFll = 1LL << 62;
const int mod = int(1e9) + 7;
using P = pair<ll, ll>;
using ld = long double;

vector<ll> a;
ll n;
ll k;
bool ch(ll x) {
  ll now = 0;
  if (x == 0)
    return false;
  for (int i = 0; i < n; ++i) {
    now += (a[i] - 1) / x;
  }
  if (now <= k)
    return true;
  return false;
}

int main() {
  cin >> n >> k;
  a.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  ll ok = 1e9;
  ll ng = 0;
  ll cnt = 0;
  while (cnt < 1000) {
    ll m = (ok + ng) / 2;
    if (ch(m))
      ok = m;
    else
      ng = m;
    cnt++;
  }
  cout << ok << endl;
}
Problem p02598 (C++)

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

// g++ -std=c++17 -Wshadow -Wall -g -fsanitize=address -fsanitize=undefined
// -D_GLIBCXX_DEBUG -o

int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll r = *max_element(a, a + n);
  ll l = 0;
  while (l < r) {
    ll mid = l + (r - l) / 2;
    ll cuts = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] % mid)
        cuts += (a[i] / mid);
      else
        cuts += ((a[i] - 1) / mid);
    }
    if (cuts > k)
      l = mid + 1;
    else {
      r = mid;
    }
    // cout<<l<<" "<<r<<"\n";
  }
  cout << l << "\n";
  return 0;
}


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

// g++ -std=c++17 -Wshadow -Wall -g -fsanitize=address -fsanitize=undefined
// -D_GLIBCXX_DEBUG -o

int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll r = *max_element(a, a + n);
  ll l = 1;
  while (l < r) {
    ll mid = l + (r - l) / 2;
    ll cuts = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] % mid)
        cuts += (a[i] / mid);
      else
        cuts += ((a[i] - 1) / mid);
    }
    if (cuts > k)
      l = mid + 1;
    else {
      r = mid;
    }
    // cout<<l<<" "<<r<<"\n";
  }
  cout << l << "\n";
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef unsigned long long ull;
typedef set<int>::iterator sit;
#define st first
#define sd second
#define mkp make_pair
#define pb push_back
void tempwj() {
  freopen("hash.in", "r", stdin);
  freopen("hash.out", "w", stdout);
}
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll qpow(ll a, ll b, ll mod) {
  a %= mod;
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
struct cmp {
  bool operator()(const pii &a, const pii &b) { return a.second < b.second; }
};
int lb(int x) { return x & -x; }
// friend bool operator < (Node a,Node b)   重载
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const int maxn = 2e6 + 10;
int a[maxn];
int n, m;
bool pd(int x) {
  ll s = 0;
  for (int i = 1; i <= n; i++) {
    s += (a[i] + x - 1) / x - 1;
  }
  return s <= m;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int l = 0, r = 1e9;
  while (l < r) {
    int mid = l + r >> 1;
    if (pd(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", r);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef unsigned long long ull;
typedef set<int>::iterator sit;
#define st first
#define sd second
#define mkp make_pair
#define pb push_back
void tempwj() {
  freopen("hash.in", "r", stdin);
  freopen("hash.out", "w", stdout);
}
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll qpow(ll a, ll b, ll mod) {
  a %= mod;
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
struct cmp {
  bool operator()(const pii &a, const pii &b) { return a.second < b.second; }
};
int lb(int x) { return x & -x; }
// friend bool operator < (Node a,Node b)   重载
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const int maxn = 2e6 + 10;
int a[maxn];
int n, m;
bool pd(int x) {
  ll s = 0;
  for (int i = 1; i <= n; i++) {
    s += (a[i] + x - 1) / x - 1;
  }
  return s <= m;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int l = 1, r = 1e9;
  while (l < r) {
    int mid = l + r >> 1;
    if (pd(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", r);
}
Problem p02598 (C++)

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

#define pb push_back
#define mp make_pair
#define all(n) (n).begin(), (n).end()
#define fi first
#define se second

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

const int MOD = 1e9 + 7;

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, k;
  cin >> n >> k;
  int a[200000], ans = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll lo = 0, hi = 1e9;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    int cuts = 0;
    for (int i = 0; i < n; i++) {
      cuts += (a[i] - 1) / mid;
      if (cuts > k)
        break;
    }
    if (cuts <= k)
      ans = mid, hi = mid - 1;
    else
      lo = mid + 1;
  }
  cout << ans << "\n";
}

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

#define pb push_back
#define mp make_pair
#define all(n) (n).begin(), (n).end()
#define fi first
#define se second

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

const int MOD = 1e9 + 7;

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, k;
  cin >> n >> k;
  int a[200000], ans = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll lo = 0, hi = 1e9;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    int cuts = 0;
    if (mid == 0) {
      cout << 1 << "\n";
      return 0;
    }
    for (int i = 0; i < n; i++) {
      cuts += (a[i] - 1) / mid;
      if (cuts > k)
        break;
    }
    if (cuts <= k)
      ans = mid, hi = mid - 1;
    else
      lo = mid + 1;
  }
  cout << ans << "\n";
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int N = 2e5 + 5, INF = 2e9;
using namespace std;
template <typename tp> inline void r(tp &n) {
  n = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c))
    n = n * 10 + c - 48, c = getchar();
}
int n, k;
int a[N];
bool OK(int cur) {
  int ret = 0;
  for (int i = 1; i <= n; ++i)
    ret += ((a[i] - 1) / cur);
  return ret <= k;
}
int main() {

  int L = 0, R = 0;
  r(n), r(k);
  for (int i = 1; i <= n; ++i)
    r(a[i]), R = max(R, a[i]);
  while (L <= R) {
    int mid = L + R >> 1;
    if (OK(mid))
      R = mid - 1;
    else
      L = mid + 1;
  }
  printf("%d\n", R + 1);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
const int N = 2e5 + 5, INF = 2e9;
using namespace std;
template <typename tp> inline void r(tp &n) {
  n = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c))
    n = n * 10 + c - 48, c = getchar();
}
int n, k;
int a[N];
bool OK(int cur) {
  int ret = 0;
  for (int i = 1; i <= n; ++i)
    ret += ((a[i] - 1) / cur);
  return ret <= k;
}
int main() {

  int L = 1, R = 0;
  r(n), r(k);
  for (int i = 1; i <= n; ++i)
    r(a[i]), R = max(R, a[i]);
  while (L <= R) {
    int mid = L + R >> 1;
    if (OK(mid))
      R = mid - 1;
    else
      L = mid + 1;
  }
  printf("%d\n", R + 1);
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mod 1000000007
#define ll long long int
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fr(j, i, n) for (int i = j; i < n; ++i)
#define frr(j, i, n) for (int i = j; i <= n; ++i)
using namespace std;
void solve() {
  ll n, k;
  cin >> n >> k;
  ll a[n + 1];
  frr(1, i, n) cin >> a[i];
  ll left = 0, right = 1e9;
  ll ans = 1e10;
  while (left <= right) {
    ll moves = 0;
    ll mid = (left + right) / 2ll;
    frr(1, i, n) {
      if (a[i] % mid == 0)
        moves += ((a[i] / mid) - 1ll);
      else
        moves += (a[i] / mid);
    }
    if (moves > k)
      left = mid + 1ll;
    else
      ans = min(mid, ans), right = mid - 1ll;
  }
  cout << ans << "\n";
}
int main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt","r",stdin);
  // 	freopen("output.txt","w",stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // t=1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007
#define ll long long int
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fr(j, i, n) for (int i = j; i < n; ++i)
#define frr(j, i, n) for (int i = j; i <= n; ++i)
using namespace std;
void solve() {
  ll n, k;
  cin >> n >> k;
  ll a[n + 1];
  frr(1, i, n) cin >> a[i];
  ll left = 1, right = 1e9;
  ll ans = 1e10;
  while (left <= right) {
    ll moves = 0;
    ll mid = (left + right) / 2ll;
    frr(1, i, n) {
      if (a[i] % mid == 0)
        moves += ((a[i] / mid) - 1ll);
      else
        moves += (a[i] / mid);
    }
    if (moves > k)
      left = mid + 1ll;
    else
      ans = min(mid, ans), right = mid - 1ll;
  }
  cout << ans << "\n";
}
int main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt","r",stdin);
  // 	freopen("output.txt","w",stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // 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 all(x) (x).begin(), (x).end()

using ll = long long;
using P = pair<int, int>;
using mp = map<int, int>;

const int MOD = 1e9 + 7;
const int INF = 1001001001;

int main(void) {

  int n, k;
  cin >> n >> k;

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

  int left = -1, right = amax + 1, mid = 0;
  while (left + 1 < right) {
    mid = (left + right) / 2;

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

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

  cout << right << "\n";

  return 0;
}


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

using namespace std;

#define all(x) (x).begin(), (x).end()

using ll = long long;
using P = pair<int, int>;
using mp = map<int, int>;

const int MOD = 1e9 + 7;
const int INF = 1001001001;

int main(void) {

  int n, k;
  cin >> n >> k;

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

  int left = 0, right = amax + 1, mid = 0;
  while (left + 1 < right) {
    mid = (left + right) / 2;

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

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

  cout << right << "\n";

  return 0;
}