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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T, class C> void chmax(T &a, C b) { a > b ?: a = b; }
template <class T, class C> void chmin(T &a, C b) { a < b ?: a = b; }
int main() {
  ll N;
  cin >> N;
  priority_queue<ll> R;
  priority_queue<ll, vector<ll>, greater<ll>> W;
  for (ll i = 0; i < N; i++) {
    char c;
    cin >> c;
    if (c == 'R')
      R.push(i);
    else if (c == 'W')
      W.push(i);
  }
  if (R.size() == 0 || W.size() == 0) {
    cout << "0" << endl;
  }
  ll cnt = 0;
  while (1) {
    ll r = R.top();
    ll w = W.top();
    if (r > w) {
      R.pop();
      W.pop();
      R.push(w);
      W.push(r);
      cnt++;
    } else {
      break;
    }
  }
  cout << cnt << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T, class C> void chmax(T &a, C b) { a > b ?: a = b; }
template <class T, class C> void chmin(T &a, C b) { a < b ?: a = b; }
int main() {
  ll N;
  cin >> N;
  priority_queue<ll> R;
  priority_queue<ll, vector<ll>, greater<ll>> W;
  for (ll i = 0; i < N; i++) {
    char c;
    cin >> c;
    if (c == 'R')
      R.push(i);
    else if (c == 'W')
      W.push(i);
  }
  if (R.size() == 0 || W.size() == 0) {
    cout << "0" << endl;
    return 0;
  }
  ll cnt = 0;
  while (1) {
    ll r = R.top();
    ll w = W.top();
    if (r > w) {
      R.pop();
      W.pop();
      R.push(w);
      W.push(r);
      cnt++;
    } else {
      break;
    }
  }
  cout << cnt << endl;
}
Problem p02597 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>

using namespace std;

int main(void) {
  int N;
  cin >> N;

  char c[N + 1];
  c[0] = 'R';
  for (int i = 1; i <= N; i++) {
    cin >> c[i];
  }

  int counter = 0;
  int numBeforeAllRed = 0;
  for (int i = N; i != 1; i--) {
    if (c[i] == 'R') {
      if (numBeforeAllRed > 0) {
        if (i > numBeforeAllRed) {
          counter++;
        } else {
          break;
        }
      } else {
        for (int j = 1; j < i; j++) {
          numBeforeAllRed = j;
          if (c[j] == 'W') {
            c[j] = 'R';
            c[i] = 'W';
            counter++;
            numBeforeAllRed = 0;
            break;
          }
        }
      }
    }
  }
  cout << counter << "\n";
}


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

using namespace std;

int main(void) {
  int N;
  cin >> N;

  char c[N + 1];
  c[0] = 'R';
  for (int i = 1; i <= N; i++) {
    cin >> c[i];
  }

  int counter = 0;
  int numBeforeAllRed = 0;
  for (int i = 1; i <= N; i++) {
    if (c[i] == 'W') {
      counter++;
    } else {
      break;
    }
  }
  if (counter >= N / 2) {
    numBeforeAllRed = 1;
  }
  counter = 0;
  for (int i = N; i != 1; i--) {
    if (c[i] == 'R') {
      if (numBeforeAllRed > 0) {
        if (i > numBeforeAllRed) {
          counter++;
        } else {
          break;
        }
      } else {
        for (int j = 1; j < i; j++) {
          numBeforeAllRed = j;
          if (c[j] == 'W') {
            c[j] = 'R';
            c[i] = 'W';
            counter++;
            numBeforeAllRed = 0;
            break;
          }
        }
      }
    }
  }
  cout << counter << "\n";
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mt make_tuple
#define MOD 1000000007
#define fo(i, a, b) for (i = a; i < b; i++)
#define foe(i, a, b) for (i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<long long int>
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<long long int, long long int>>
#define boost                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)
using namespace std;
const int inf = 1e9 + 5;
const ll inf64 = 1e18 + 5;

int main() {
  boost;
  int n, i;
  cin >> n;
  string s;
  cin >> s;

  int r = 0, w = 0;
  for (char c : s) {
    if (c == 'R')
      r++;
    else
      w++;
  }

  int totr = 0, totw = 0;
  fo(i, 0, r) {
    if (s[i] == 'R')
      totr++;
  }
  for (i = s.size() - 1; i >= s.size() - w; i--) {
    if (s[i] == 'W')
      totw++;
  }
  cout << min(r - totr, w - totw);
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mt make_tuple
#define MOD 1000000007
#define fo(i, a, b) for (i = a; i < b; i++)
#define foe(i, a, b) for (i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<long long int>
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<long long int, long long int>>
#define boost                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)
using namespace std;
const int inf = 1e9 + 5;
const ll inf64 = 1e18 + 5;

int main() {
  boost;
  int n, i;
  cin >> n;
  string s;
  cin >> s;

  int r = 0, w = 0;
  for (char c : s) {
    if (c == 'R')
      r++;
    else
      w++;
  }

  int totr = 0, totw = 0;
  fo(i, 0, r) {
    if (s[i] == 'R')
      totr++;
  }
  for (i = n - 1; i >= n - w; i--) {
    if (s[i] == 'W')
      totw++;
  }
  cout << min(r - totr, w - totw);
}
Problem p02598 (C++)

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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

bool possible(long long int mid, long long int arr[], long long int n,
              long long int k) {
  for (long long int i = 0; i < n; i++) {
    long long int p = arr[i];

    if (k >= (p - 1) / mid) {
      k -= (p - 1) / mid;
    } else {
      return false;
    }
  }
  return true;
}

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);
  long long int n;
  cin >> n;
  long long int k;
  cin >> k;
  long long int arr[n];
  for (long long int i = 0; i < n; i++)
    cin >> arr[i];
  long long int start = 0;
  long long int end = 1000000001;
  while (start < end) {
    long long int mid = start + (end - start) / 2;
    if (possible(mid, arr, n, k)) {
      end = mid;
    } else {
      start = mid + 1;
    }
  }

  cout << end << endl;

  return 0;
}


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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

bool possible(long long int mid, long long int arr[], long long int n,
              long long int k) {
  if (mid <= 0)
    return false;
  for (long long int i = 0; i < n; i++) {
    long long int p = arr[i];

    if (k >= (p - 1) / mid) {
      k -= (p - 1) / mid;
    } else {
      return false;
    }
  }
  return true;
}

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);
  long long int n;
  cin >> n;
  long long int k;
  cin >> k;
  long long int arr[n];
  for (long long int i = 0; i < n; i++)
    cin >> arr[i];
  long long int start = 0;
  long long int end = 1000000001;
  while (start < end) {
    long long int mid = start + (end - start) / 2;
    if (possible(mid, arr, n, k)) {
      end = mid;
    } else {
      start = mid + 1;
    }
  }

  cout << end << endl;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, k;
int arr[200010];
bool valid(int temp) {
  int count = 0;
  for (int i = 0; i < n; i++) {
    count += (arr[i] + temp - 1) / temp - 1;
  }
  if (count <= k)
    return true;
  else
    return false;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int l = -1, r = 1000000001;
  while (l + 1 < r) {
    int mid = (l + r) / 2;
    if (valid(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, k;
int arr[200010];
bool valid(int temp) {
  int count = 0;
  for (int i = 0; i < n; i++) {
    count += (arr[i] + temp - 1) / temp - 1;
  }
  if (count <= k)
    return true;
  else
    return false;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int l = 0, r = 1000000001;
  while (l + 1 < r) {
    int mid = (l + r) / 2;
    if (valid(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << "\n";
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define SZ(a) ((int)(a).size())
int main() {
#ifdef LOCAL
  freopen(".a.in", "r", stdin);
#endif
  ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int64_t> A(n);
  for (int i = 0; i < n; ++i)
    cin >> A[i];
  for (int i = 0; i < n; ++i)
    A[i] *= 2;
  auto ok = [&](int64_t x) {
    int64_t c = 0;
    for (int i = 0; i < n; ++i) {
      if (A[i] % x == 0)
        c += A[i] / x - 1;
      else
        c += A[i] / x;
    }
    return c <= k;
  };
  int64_t l = 0, r = 2e9;
  while (l < r) {
    int64_t mid = (l + r) >> 1;
    if (ok(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  if (l % 2 == 0)
    cout << l / 2;
  else
    cout << (l + 1) / 2;

  return 0;
}
// 2020.08.02 20:31:43


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define SZ(a) ((int)(a).size())
int main() {
#ifdef LOCAL
  freopen(".a.in", "r", stdin);
#endif
  ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int64_t> A(n);
  for (int i = 0; i < n; ++i)
    cin >> A[i];
  for (int i = 0; i < n; ++i)
    A[i] *= 2;
  auto ok = [&](int64_t x) {
    int64_t c = 0;
    for (int i = 0; i < n; ++i) {
      if (A[i] % x == 0)
        c += A[i] / x - 1;
      else
        c += A[i] / x;
    }
    return c <= k;
  };
  int64_t l = 1, r = 2e9;
  while (l < r) {
    int64_t mid = (l + r) >> 1;
    if (ok(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  if (l % 2 == 0)
    cout << l / 2;
  else
    cout << (l + 1) / 2;

  return 0;
}
// 2020.08.02 20:31:43
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define pb push_back
#define mp make_pair
#define clr(v, d) memset(v, d, sizeof(v))
#define PI acos(-1)
typedef long long ll;
typedef unsigned long long ull;
const double eps = (1e-9);
using namespace std;
int dcmp(long double a, long double b) {
  return fabsl(a - b) <= eps ? 0 : (a > b) ? 1 : -1;
}
int getBit(ll num, int idx) { return ((num >> idx) & 1ll) == 1; }
int setBit1(int num, int idx) { return num | (1 << idx); }
ll setBit0(ll num, int idx) { return num & ~(1ll << idx); }
ll flipBit(ll num, int idx) { return num ^ (1ll << idx); }
void GO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int countNumBit1(int mask) {
  int ret = 0;
  while (mask) {
    mask &= (mask - 1);
    ++ret;
  }
  return ret;
}
const int N = 1000 + 5;
vector<int> v[2];
int n, k, a[N];
bool check(int val) {
  ll tot = 0;
  for (int i = 0; i < n; i++) {
    int len = a[i];
    int F = (len / val + (len % val != 0)) - 1;
    if (tot + F > k)
      return 0;
    tot += F;
  }
  return (tot <= k);
}
int BS(int l, int r) {
  int res = 0;
  while (l <= r) {
    ll mid = (l + r) / 2;
    if (check(mid)) {
      r = mid - 1;
      res = mid;
    } else {
      l = mid + 1;
    }
  }
  return res;
}
int main() {
  GO();

  int T;
  T = 1;
  // cin>>T;
  while (T--) {

    cin >> n >> k;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    cout << BS(1, 1000000000) << "\n";
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define pb push_back
#define mp make_pair
#define clr(v, d) memset(v, d, sizeof(v))
#define PI acos(-1)
typedef long long ll;
typedef unsigned long long ull;
const double eps = (1e-9);
using namespace std;
int dcmp(long double a, long double b) {
  return fabsl(a - b) <= eps ? 0 : (a > b) ? 1 : -1;
}
int getBit(ll num, int idx) { return ((num >> idx) & 1ll) == 1; }
int setBit1(int num, int idx) { return num | (1 << idx); }
ll setBit0(ll num, int idx) { return num & ~(1ll << idx); }
ll flipBit(ll num, int idx) { return num ^ (1ll << idx); }
void GO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int countNumBit1(int mask) {
  int ret = 0;
  while (mask) {
    mask &= (mask - 1);
    ++ret;
  }
  return ret;
}
const int N = 200000 + 5;
int n, k, a[N];
bool check(int val) {
  ll tot = 0;
  for (int i = 0; i < n; i++) {
    int len = a[i];
    int F = (len / val + (len % val != 0)) - 1;
    if (tot + F > k)
      return 0;
    tot += F;
  }
  return (tot <= k);
}
int BS(int l, int r) {
  int res = 0;
  while (l <= r) {
    ll mid = (l + r) / 2;
    if (check(mid)) {
      r = mid - 1;
      res = mid;
    } else {
      l = mid + 1;
    }
  }
  return res;
}
int main() {
  GO();

  int T;
  T = 1;
  // cin>>T;
  while (T--) {

    cin >> n >> k;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    cout << BS(1, 1000000000) << "\n";
  }
}
Problem p02598 (C++)

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

using namespace std;

// #define mod 1000000007
// #define mod1 9982
#define int long long int
#define pii pair<int, int>
#define vpii vector<pii>
#define vi vector<int>
#define fi first
#define se second
#define psb push_back
#define ppb pop_back
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define all(x) x.begin(), x.end()
#define endl "\n"

const int maxn = 2e5 + 5;
int n, k;
int ar[maxn];

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

void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> ar[i];
  int l = 0, h = (1e9), ans = -1;
  while (l <= h) {
    int mid = (l + h) / 2;
    if (ok(mid)) {
      ans = mid;
      h = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans << endl;
}

int32_t main() {
  fio;
  int t;
  // cin>>t;
  t = 1;
  while (t--)
    solve();
}

/*
./a.out <input.txt > output.txt
*/


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

using namespace std;

// #define mod 1000000007
// #define mod1 9982
#define int long long int
#define pii pair<int, int>
#define vpii vector<pii>
#define vi vector<int>
#define fi first
#define se second
#define psb push_back
#define ppb pop_back
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define all(x) x.begin(), x.end()
#define endl "\n"

const int maxn = 2e5 + 5;
int n, k;
int ar[maxn];

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

void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> ar[i];
  int l = 1, h = (1e9), ans = -1;
  while (l <= h) {
    int mid = (l + h) / 2;
    if (ok(mid)) {
      ans = mid;
      h = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans << endl;
}

int32_t main() {
  fio;
  int t;
  // cin>>t;
  t = 1;
  while (t--)
    solve();
}

/*
./a.out <input.txt > output.txt
*/
Problem p02598 (C++)

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

#define pb push_back
#define sf(x) scanf("%d", &x)
#define sfl(x) scanf("%lld", &x)
#define pf(x) printf("%d\n", x)
#define pfl(x) printf("%lld\n", x)
#define endl '\n'
#define pii pair<int, int>
#define mapii map<int, int>
#define mapll map<ll, ll>
#define mapci map<char, int>
#define mapcl map<char, ll>
#define mapsi map<string, int>
#define mapsl map<string, ll>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vd vector<double, double>
#define all(c) c.begin(), c.end()
#define F first
#define S second
#define mp make_pair
#define ftc(x) cerr << #x << ": " << x << " " << endl;
#define PI acos(-1)
#define lcm(a, b) ((a * b) / __gcd(a, b))
#define optimize                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define sqr(a) ((a) * (a))

int main() {
  ll i, j, k, l, m, n, l1, l2, l3;

  cin >> n >> k;

  ll ar[n + 10], cnt;

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

  ll low = 0, high = 1e10, mid;

  while (low <= high) {
    mid = (low + high) / 2;
    cnt = 0;

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

    // cout << mid << " " << cnt << endl;

    if (cnt <= k) {
      // cout << mid << endl;
      l1 = mid;
      high = mid - 1;
    } else
      low = mid + 1;
  }

  cout << l1 << endl;
}


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

#define pb push_back
#define sf(x) scanf("%d", &x)
#define sfl(x) scanf("%lld", &x)
#define pf(x) printf("%d\n", x)
#define pfl(x) printf("%lld\n", x)
#define endl '\n'
#define pii pair<int, int>
#define mapii map<int, int>
#define mapll map<ll, ll>
#define mapci map<char, int>
#define mapcl map<char, ll>
#define mapsi map<string, int>
#define mapsl map<string, ll>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vd vector<double, double>
#define all(c) c.begin(), c.end()
#define F first
#define S second
#define mp make_pair
#define ftc(x) cerr << #x << ": " << x << " " << endl;
#define PI acos(-1)
#define lcm(a, b) ((a * b) / __gcd(a, b))
#define optimize                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define sqr(a) ((a) * (a))

int main() {
  ll i, j, k, l, m, n, l1, l2, l3;

  cin >> n >> k;

  ll ar[n + 10], cnt;

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

  ll low = 1, high = 1e10, mid;

  while (low <= high) {
    mid = (low + high) / 2;
    cnt = 0;

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

    // cout << mid << " " << cnt << endl;

    if (cnt <= k) {
      // cout << mid << endl;
      l1 = mid;
      high = mid - 1;
    } else
      low = mid + 1;
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 1000000010;
constexpr ll INF = 2000000000000000000;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<int, int> P;

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 mod(ll val) {
  ll res = val % MOD;
  if (res < 0) {
    res += MOD;
  }
  return res;
}

// N^P mod M（ただしM == -1の時はmodを取らない）
template <typename T> T RS(T N, T P, T M) {
  if (P == 0) {
    return 1;
  }
  if (P < 0) {
    return 0;
  }
  if (P % 2 == 0) {
    ll t = RS(N, P / 2, M);
    if (M == -1)
      return t * t;
    return t * t % M;
  }
  if (M == -1) {
    return N * RS(N, P - 1, M);
  }
  return N * RS(N, P - 1, M) % M;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  int ng = 0;
  int ok = 1000000000;
  int mid;
  for (int i = 0; i < 100; i++) {
    mid = (ng + ok) / 2;
    int cnt = 0;
    for (int j = 0; j < N; j++) {
      cnt += (vec.at(j) - 1) / mid;
    }
    if (cnt <= K) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 1000000010;
constexpr ll INF = 2000000000000000000;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<int, int> P;

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 mod(ll val) {
  ll res = val % MOD;
  if (res < 0) {
    res += MOD;
  }
  return res;
}

// N^P mod M（ただしM == -1の時はmodを取らない）
template <typename T> T RS(T N, T P, T M) {
  if (P == 0) {
    return 1;
  }
  if (P < 0) {
    return 0;
  }
  if (P % 2 == 0) {
    ll t = RS(N, P / 2, M);
    if (M == -1)
      return t * t;
    return t * t % M;
  }
  if (M == -1) {
    return N * RS(N, P - 1, M);
  }
  return N * RS(N, P - 1, M) % M;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  int ng = 0;
  int ok = 1000000000;
  int mid;
  while (ok - ng > 1) {
    mid = (ng + ok) / 2;
    int cnt = 0;
    for (int j = 0; j < N; j++) {
      cnt += (vec.at(j) - 1) / mid;
    }
    if (cnt <= K) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int n, a[maxn], k;
bool judge(int m) {
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    cnt += (a[i] - 1) / m;
  }
  return cnt <= k;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  int l = 0, r = 1e9 + 10;
  while (l <= r) {
    int mid = l + r >> 1;
    if (judge(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int n, a[maxn], k;
bool judge(int m) {
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    cnt += (a[i] - 1) / m;
  }
  return cnt <= k;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  int l = 1, r = 1e9 + 10;
  while (l <= r) {
    int mid = l + r >> 1;
    if (judge(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;

bool f(double mid, vector<int> a, int k) {

  ll t{};
  for (int i = 0; i < a.size(); i++) {
    t += ceil(a.at(i) / mid) - 1;
  }
  return t <= k;
}

int main() {

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (auto &i : a)
    cin >> i;
  int j = 100000;
  double ok = 1e9, ng = 0;
  while (--j) {
    auto mid = (ok + ng) / 2;
    (f(mid, a, k) ? ok : ng) = mid;
  }
  cout << (ll)ceil(ok) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;

bool f(double mid, vector<int> a, int k) {

  ll t{};
  for (int i = 0; i < a.size(); i++) {
    t += ceil(a.at(i) / mid) - 1;
  }
  return t <= k;
}

int main() {

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (auto &i : a)
    cin >> i;
  int j = 100;
  double ok = 1e9, ng = 0;
  while (--j) {
    auto mid = (ok + ng) / 2;
    (f(mid, a, k) ? ok : ng) = mid;
  }
  cout << (ll)ceil(ok) << endl;
}
Problem p02598 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef long double ld;
const int INF = 1e9;

int main() {
  ll n, k, t = 0;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  ll l = -1, r = 1e9, m;
  while (r - l > 1) {
    m = (l + r) / 2;
    t = 0;
    rep(i, n) t += (a[i] - 1) / m;
    if (t <= k)
      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 < (n); i++)
using namespace std;
typedef long long ll;
typedef long double ld;
const int INF = 1e9;

int main() {
  ll n, k, t = 0;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  ll l = 0, r = 1e9, m;
  while (r - l > 1) {
    m = (l + r) / 2;
    t = 0;
    rep(i, n) t += (a[i] - 1) / m;
    if (t <= k)
      r = m;
    else
      l = m;
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

vector<int> v;
int k;

bool can(int len) {
  int cnt = k;
  for (int i = v.size() - 1; i >= 0; i--) {
    if (v[i] > len)
      cnt -= (v[i] / len);
    if (cnt < 0)
      return false;
  }
  return true;
}
int main() {
  int n;
  cin >> n >> k;
  v.resize(n);
  rep(i, n) cin >> v[i];
  int l = 0, r = 1e9 + 1;
  for (int i = 0; i < 100; i++) {
    int mid = (l + r) / 2;
    if (can(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

vector<int> v;
int k;

bool can(int len) {
  if (len == 0)
    return false;
  int cnt = k;
  for (int i = v.size() - 1; i >= 0; i--) {
    if (v[i] > len)
      cnt -= (v[i] / len);
    if (cnt < 0)
      return false;
  }
  return true;
}
int main() {
  int n;
  cin >> n >> k;
  v.resize(n);
  rep(i, n) cin >> v[i];
  int l = 0, r = 1e9 + 1;
  for (int i = 0; i < 100; i++) {
    int mid = (l + r) / 2;
    if (can(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << endl;

  return 0;
}
Problem p02598 (C++)

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

                            Online C Compiler.
                Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, a[200005];
bool check(int l, int k) {
  int cnt = 0;
  for (int i = 0; i < n; i++) {

    cnt += (a[i] - 1) / l;
  }
  if (cnt <= k)
    return true;
  return false;
}
signed main() {
  int k;
  cin >> n >> k;
  int l = 0, r = 1e9, mid, ans = -1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  // int mx=p.top();
  while (l <= r) {
    mid = l + (r - l) / 2;
    if (check(mid, k)) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << ans;
  return 0;
}


Fixed submission:
/******************************************************************************

                            Online C Compiler.
                Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, a[200005];
bool check(int l, int k) {
  int cnt = 0;
  for (int i = 0; i < n; i++) {

    cnt += (a[i] - 1) / l;
  }
  if (cnt <= k)
    return true;
  return false;
}
signed main() {
  int k;
  cin >> n >> k;
  int l = 1, r = 1e9, mid, ans = -1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  // int mx=p.top();
  while (l <= r) {
    mid = l + (r - l) / 2;
    if (check(mid, k)) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << ans;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(v) (v).begin()(v).end()
ll n, k;
bool check(ll x, std::vector<ll> &v) {
  ll moves = 0;
  for (auto i : v) {
    moves += i / x - (i % x == 0);
  }

  return moves <= k;
}
void solve() {
  cin >> n >> k;
  std::vector<ll> v(n);
  ll ans = 0, mid, l = 0, r = 1e9;

  for (auto &i : v)
    cin >> i, ans = max(ans, i);
  r = ans + 1;
  while (l <= r) {
    mid = l + r >> 1;
    if (check(mid, v))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t = 1;
  // cin >> t;
  for (int i = 0; i < t; i++) {
    solve();
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(v) (v).begin()(v).end()
ll n, k;
bool check(ll x, std::vector<ll> &v) {
  ll moves = 0;
  for (auto i : v) {
    moves += i / x - (i % x == 0);
  }

  return moves <= k;
}
void solve() {
  cin >> n >> k;
  std::vector<ll> v(n);
  ll ans = 0, mid, l = 1, r = 1e9;

  for (auto &i : v)
    cin >> i, ans = max(ans, i);
  r = ans + 1;
  while (l <= r) {
    mid = l + r >> 1;
    if (check(mid, v))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t = 1;
  // cin >> t;
  for (int i = 0; i < t; i++) {
    solve();
  }
}
Problem p02598 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define req(i, n) for (int i = 1; i <= n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define ALL(a) a.begin(), a.end()
#define PI acos(-1)
using namespace std;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
typedef long long int ll;
typedef long double ld;
const int MOD = 1e9 + 7;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}, sum = 0, k;
vector<int> a;
bool check(int x) {
  int cnt = 0;
  for (int i : a) {
    int aa = i;
    req(j, 1e9) {
      if ((aa - 1) / j + 1 <= x)
        break;
      cnt++;
    }
  }
  if (cnt > k)
    return 1;
  else
    return 0;
}
int main() {
  int n;
  cin >> n >> k;
  a.resize(n);
  rep(i, n) cin >> a[i];
  int right = 1e9 + 7, left = 0;
  while (right - left > 1) {
    int mid = (right + left) / 2;
    if (check(mid))
      left = mid;
    else
      right = mid;
  }
  cout << right << endl;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define req(i, n) for (int i = 1; i <= n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define ALL(a) a.begin(), a.end()
#define PI acos(-1)
using namespace std;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
typedef long long int ll;
typedef long double ld;
const int MOD = 1e9 + 7;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}, sum = 0, k;
vector<int> a;
bool check(int x) {
  int cnt = 0;
  for (int i : a) {
    int kk = (i - 1) / x + 1;
    if (kk * x < i)
      kk++;
    cnt += kk - 1;
  }
  if (cnt > k)
    return 1;
  else
    return 0;
}
int main() {
  int n;
  cin >> n >> k;
  a.resize(n);
  rep(i, n) cin >> a[i];
  int right = 1e9 + 7, left = 0;
  while (right - left > 1) {
    int mid = (right + left) / 2;
    if (check(mid))
      left = mid;
    else
      right = mid;
  }
  cout << right << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long
#define endl "\n"
using namespace std;

int arr[200005];
int n, k;

bool check(int cuts) {
  int req = 0;
  for (int i = 0; i < n; i++) {
    req += ((arr[i] + cuts - 1) / cuts - 1);
  }
  return req <= k;
}

int32_t main() {

  fast cin >> n >> k;

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int left = 0, right = (int)1e10 + 5;

  int ans = 0;
  while (left <= right) {
    int mid = (left + right) / 2;
    if (check(mid)) {
      ans = mid;
      right = mid - 1;
    } else {
      left = mid + 1;
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long
#define endl "\n"
using namespace std;

int arr[200005];
int n, k;

bool check(int cuts) {
  int req = 0;
  for (int i = 0; i < n; i++) {
    req += ((arr[i] + cuts - 1) / cuts - 1);
  }
  return req <= k;
}

int32_t main() {

  fast cin >> n >> k;

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int left = 1, right = (int)1e10 + 5;

  int ans = 0;
  while (left <= right) {
    int mid = (left + right) / 2;
    if (check(mid)) {
      ans = mid;
      right = mid - 1;
    } else {
      left = mid + 1;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02598 (C++)

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

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


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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int l = 0, r = 1e9;
  while (r - l > 1) {
    int x = (l + r) / 2;
    auto f = [&](int x) {
      ll now = 0;
      rep(i, n) now += (a[i] - 1) / x;
      return now <= k;
    };
    if (f(x))
      r = x;
    else
      l = x;
  }
  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 pair<int, int> P;

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

ll a[210000];

bool check(ll N, ll K, ll mid) {
  ll res = 0;
  for (int i = 0; i < N; i++) {
    res += (a[i] - 1) / mid;
  }
  if (res > K)
    return true;
  else
    return false;
}

int main() {
  ll N, K;
  cin >> N >> K;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    ans = max(ans, a[i]);
  }
  if (K == 0) {
    cout << ans << endl;
    return 0;
  }
  ll right = INF;
  ll left = 0;
  for (int i = 0; i < 1000; i++) {
    ll mid = (right + left) / 2;
    if (check(N, K, mid)) {
      left = mid;
    } else {
      right = mid;
    }
  }
  cout << right << endl;
  return 0;
}

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

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

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

ll a[210000];

bool check(ll N, ll K, ll mid) {
  ll res = 0;
  for (int i = 0; i < N; i++) {
    res += (a[i] - 1) / mid;
  }
  if (res > K)
    return true;
  else
    return false;
}

int main() {
  ll N, K;
  cin >> N >> K;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    ans = max(ans, a[i]);
  }
  if (K == 0) {
    cout << ans << endl;
    return 0;
  }
  ll right = INF;
  ll left = 0;
  for (int i = 0; i < 1000; i++) {
    ll mid = (right + left) / 2;
    if (mid == 0)
      break;
    if (check(N, K, mid)) {
      left = mid;
    } else {
      right = mid;
    }
  }
  cout << right << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
#define e_b emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
using namespace std;
using ll = long long;
using ld = long double;
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int N, K;
vector<int> A;

bool isOK(int x) {
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    int a = A.at(i);
    cnt += (a + x - 1) / x - 1;
  }
  if (cnt <= K)
    return true;
  else
    return false;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> K;
  A.resize(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);

  int ng = -1;
  int ok = 1e9;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (isOK(mid))
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
#define e_b emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
using namespace std;
using ll = long long;
using ld = long double;
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int N, K;
vector<int> A;

bool isOK(int x) {
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    int a = A.at(i);
    cnt += (a + x - 1) / x - 1;
  }
  if (cnt <= K)
    return true;
  else
    return false;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> K;
  A.resize(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);

  int ng = 0;
  int ok = 1e9;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (isOK(mid))
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;

  return 0;
}
Problem p02598 (C++)

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define LL long long

int n;
LL k;
LL a[200000];

bool ok(LL st) {
  LL cnt = 0;
  REP(i, n) {
    LL cut = (a[i] - 1) / st;
    cnt += cut;
    // cerr<<st<<" "<<cnt<<endl;
    if (cnt > k)
      return false;
  }

  return true;
}

int main() {
  cin >> n >> k;
  REP(i, n) cin >> a[i];

  LL lb = 0, ub = 10e9;
  while (lb < ub) {
    LL mid = (lb + ub) / 2;
    if (ok(mid)) {
      ub = mid;
    } else {
      lb = mid + 1;
    }
  }
  cout << lb << endl;
  return 0;
}


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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define LL long long

int n;
LL k;
LL a[200000];

bool ok(LL st) {
  LL cnt = 0;
  REP(i, n) {
    LL cut = (a[i] - 1) / st;
    cnt += cut;
    // cerr<<st<<" "<<cnt<<endl;
    if (cnt > k)
      return false;
  }

  return true;
}

int main() {
  cin >> n >> k;
  REP(i, n) cin >> a[i];

  LL lb = 1, ub = 10e9;
  while (lb < ub) {
    LL mid = (lb + ub) / 2;
    if (ok(mid)) {
      ub = mid;
    } else {
      lb = mid + 1;
    }
  }
  cout << lb << endl;
  return 0;
}
Problem p02598 (C++)

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

using namespace std;

typedef long long int ll;

int n, k;
vector<int> a;

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

int main() {
  cin >> n >> k;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int low = 0;
  int high = 1e9;
  int ans = 1e9;
  while (low <= high) {
    int md = (low + high) / 2;
    if (ok(md)) {
      high = md - 1;
      ans = min(ans, md);
    } else {
      low = md + 1;
    }
  }
  cout << ans << "\n";
  return 0;
}


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

using namespace std;

typedef long long int ll;

int n, k;
vector<int> a;

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

int main() {
  cin >> n >> k;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int low = 1;
  int high = 1e9;
  int ans = 1e9;
  while (low <= high) {
    int md = (low + high) / 2;
    if (ok(md)) {
      high = md - 1;
      ans = min(ans, md);
    } else {
      low = md + 1;
    }
  }
  cout << ans << "\n";
  return 0;
}
Problem p02598 (C++)

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

typedef long long ll;
typedef long double ld;
typedef vector<vector<int>> Graph;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());
  ll left = 1, right = A[N - 1] + 1;
  ll centre;
  vector<ll> ans;
  while (right - left > 1) {
    centre = (left + right) / 2;
    ll cnt = 0;
    for (int i = 0; i < N; i++)
      cnt += ceil((double)A[i] / (double)centre) - 1;
    if (cnt > K)
      left = centre;
    else {
      ans.push_back(centre);
      right = centre;
    }
  }
  cout << ans.back() << endl;
  return 0;
}

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

typedef long long ll;
typedef long double ld;
typedef vector<vector<int>> Graph;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());
  ll left = 0, right = A[N - 1] + 1;
  ll centre;
  vector<ll> ans;
  while (right - left > 1) {
    centre = (left + right) / 2;
    ll cnt = 0;
    for (int i = 0; i < N; i++)
      cnt += ceil((double)A[i] / (double)centre) - 1;
    if (cnt > K)
      left = centre;
    else {
      ans.push_back(centre);
      right = centre;
    }
  }
  cout << ans.back() << endl;
  return 0;
}
Problem p02598 (C++)

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

#define int long long
#define double long double
#define PI 3.141592653589793
#define FAST                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

// Use for file I/O;
#define FIN                                                                    \
  string _fname = "paintbarn";                                                 \
  string _is = _fname + ".in", _os = _fname + ".out";                          \
  freopen(_is.c_str(), "r", stdin);                                            \
  freopen(_os.c_str(), "w", stdout);

using namespace std;

const int INF = 4e18 + 10;
const int INF2 = 2e18 + 10;
const int INF3 = 2e10;
const int MAXN = 305;
const int LOGN = 19;
const int MOD = 1e9 + 7;
const int EPS = 1e-9;
const int MULT = 2e5;
const int MCNST = MOD * MOD * 8;

int32_t main() {
  FAST;

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.rbegin(), a.rend());
  int ans = a[0];
  int lb = 0, ub = ans;
  while (lb <= ub) {
    int m = (lb + ub) / 2;
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += (a[i] + m - 1) / m - 1;
    }
    // cout << m << ' ' << cnt << endl;

    if (cnt <= k) {
      ans = m;
      ub = m - 1;
    } else {
      lb = m + 1;
    }
  }
  cout << ans;
}

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

#define int long long
#define double long double
#define PI 3.141592653589793
#define FAST                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

// Use for file I/O;
#define FIN                                                                    \
  string _fname = "paintbarn";                                                 \
  string _is = _fname + ".in", _os = _fname + ".out";                          \
  freopen(_is.c_str(), "r", stdin);                                            \
  freopen(_os.c_str(), "w", stdout);

using namespace std;

const int INF = 4e18 + 10;
const int INF2 = 2e18 + 10;
const int INF3 = 2e10;
const int MAXN = 305;
const int LOGN = 19;
const int MOD = 1e9 + 7;
const int EPS = 1e-9;
const int MULT = 2e5;
const int MCNST = MOD * MOD * 8;

int32_t main() {
  FAST;

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.rbegin(), a.rend());
  int ans = a[0];
  int lb = 1, ub = ans;
  while (lb <= ub) {
    int m = (lb + ub) / 2;
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += (a[i] + m - 1) / m - 1;
    }
    // cout << m << ' ' << cnt << endl;

    if (cnt <= k) {
      ans = m;
      ub = m - 1;
    } else {
      lb = m + 1;
    }
  }
  cout << ans;
}