alexjercan/bugnet huggingface dataset:
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL 9223372036854775
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define MOD9 998244353
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

using II = pair<int, int>;

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
int extgcd(int a, int b, int &x, int &y) {
  int g = a;
  x = 1;
  y = 0;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

bool check(const vector<int> &A, int mid, int k) {
  if (mid == 0) {
    return false;
  }
  for (int a : A) {
    if (a <= mid) {
      continue;
    }
    int tmp = a / mid;
    if (a % mid == 0) {
      tmp--;
    }
    k -= tmp;
  }
  return 0 <= k;
}

signed main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  REP(n, N) { cin >> A[n]; }

  int l = 0, r = INF_LL;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (check(A, mid, K)) {
      r = mid;
    } else {
      l = mid;
    }
  }

  cout << r << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL 9223372036854775
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define MOD9 998244353
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

using II = pair<int, int>;

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
int extgcd(int a, int b, int &x, int &y) {
  int g = a;
  x = 1;
  y = 0;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

bool check(const vector<int> &A, int mid, int k) {
  for (int a : A) {
    if (a <= mid) {
      continue;
    }
    int tmp = a / mid;
    if (a % mid == 0) {
      tmp--;
    }
    k -= tmp;
  }
  return 0 <= k;
}

signed main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  REP(n, N) { cin >> A[n]; }

  int l = 0, r = INF_LL;
  REP(i, 100) {
    int mid = (l + r) / 2;
    if (check(A, mid, K)) {
      r = mid;
    } else {
      l = mid;
    }
  }

  cout << r << endl;

  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e9 + 7;
int mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int l = 0, r = INF;
  while (l + 1 < r) {
    int mid = (l + r) / 2;
    int cnt = 0;
    bool ok = true;
    for (int i = 0; i < N; i++) {
      cnt += max(0LL, (A[i] + mid - 1) / mid - 1);
      if (A[i] % mid > 0) {
        ok = false;
      }
    }
    if (cnt <= K) {
      r = mid;
    } else {
      l = mid;
    }
  }
  bool ok = true;
  if (l != 0) {
    for (int i = 0; i < N; i++) {
      if (A[i] % l) {
        ok = false;
      }
    }
  }
  if (!ok || l == 0) {
    cout << l + 1 << endl;
  } else {
    cout << l << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e9 + 7;
int mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int l = 0, r = INF;
  while (l + 1 < r) {
    int mid = (l + r) / 2;
    int cnt = 0;
    bool ok = true;
    for (int i = 0; i < N; i++) {
      cnt += max(0LL, (A[i] + mid - 1) / mid - 1);
      if (A[i] % mid > 0) {
        ok = false;
      }
    }
    if (cnt <= K) {
      r = mid;
    } else {
      l = mid;
    }
  }
  bool ok = true;
  for (int i = 0; i < N; i++) {
    if (A[i] % l) {
      ok = false;
    }
  }
  if (!ok) {
    cout << l + 1 << endl;
  } else {
    cout << l << endl;
  }
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

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

  auto candivide = [&](int nagasa) {
    int kai = 0;
    if (nagasa == 1)
      return true;
    for (int i = 0; i < n; i++) {
      if (a[i] <= nagasa - 1)
        continue;
      kai += (a[i] + nagasa - 2) / (nagasa - 1) - 1;
    }
    // cout << "(" << nagasa << "," << kai << ")";
    if (k >= kai)
      return true;
    else
      return false;
  };

  long long mn = 1;
  long long mx = 1000000001;
  rep(i, 1000) {
    long long mid = (mn + mx) / 2;
    if (candivide(mid))
      mx = mid;
    else
      mn = mid;
    // cout << "(" << mn << "," << mx << ")";
  }
  cout << mn << endl;

  return 0;
}


Error: 0

Fixed submission:

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

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

  auto candivide = [&](int nagasa) {
    int kai = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] <= nagasa - 1)
        continue;
      kai += (a[i] + nagasa - 2) / (nagasa - 1) - 1;
    }
    // cout << "(" << nagasa << "," << kai << ")";
    if (k >= kai)
      return true;
    else
      return false;
  };

  long long mn = 0;
  long long mx = 1000000001;
  rep(i, 1000) {
    long long mid = (mn + mx) / 2;
    if (candivide(mid))
      mx = mid;
    else
      mn = mid;
    // cout << "(" << mn << "," << mx << ")";
  }
  cout << mn << endl;

  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long
#define endl "\\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

struct fast_io {
  fast_io() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
  };
} fio;

int N, K;
vector<int> A;

bool check(int n) {
  int con = 0;

  for (int i = 0; i < N; i++) {
    if (A[i] < n)
      continue;

    con += (A[i]) / n;
  }

  return con <= K;
}

bool check2(int n) {
  int con = 0;
  int z = 0;

  for (int i = 0; i < N; i++) {
    if (A[i] < n)
      continue;

    con += (A[i]) / n;

    if (A[i] % n == 0)
      con--;
  }

  // if(z == N) return true;

  // return false;

  return con > K;
}

signed main() {
  cout << fixed << setprecision(10);

  cin >> N >> K;

  A.resize(N);

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

  int l = 1, h = 1e10, m = 0;

  while (l + 1 < h) {
    m = (l + h) / 2;

    if (!check(m)) {
      l = m;
    } else {
      h = m;
    }
  }

  // cout<<l<<endl;
  if (check2(l))
    l++;

  cout << l << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long
#define endl "\\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

struct fast_io {
  fast_io() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
  };
} fio;

int N, K;
vector<int> A;

bool check(int n) {
  int con = 0;

  for (int i = 0; i < N; i++) {
    if (A[i] < n)
      continue;

    con += (A[i]) / n;
  }

  return con <= K;
}

bool check2(int n) {
  int con = 0;
  int z = 0;

  for (int i = 0; i < N; i++) {
    if (A[i] < n)
      continue;

    con += (A[i]) / n;

    if (A[i] % n == 0)
      con--;
  }

  // if(z == N) return true;

  // return false;

  return con > K;
}

signed main() {
  cout << fixed << setprecision(10);

  cin >> N >> K;

  A.resize(N);

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

  int l = 0, h = 1e10, m = 0;

  while (l + 1 < h) {
    m = (l + h) / 2;

    if (!check(m)) {
      l = m;
    } else {
      h = m;
    }
  }

  // cout<<l<<endl;
  if (check2(l))
    l++;

  cout << l << endl;

  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<int> all;
int a, b;

bool can(int index) {
  int total = 0;
  for (int i = 0; i < a; i++) {
    total += (all[i] - 1) / index;
  }

  return total <= b;
}

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

  // int a,b;
  cin >> a >> b;

  for (int i = 0; i < a; i++) {
    int first;
    cin >> first;
    all.push_back(first);
  }

  int lo = 1, hi = 1e9;

  int index = -1;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    if (can(mid)) {
      hi = mid - 1;
      index = mid;
    } else
      lo = mid + 1;
  }
  cout << index << endl;

  // cout<<index<<endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<int> all;
int a, b;

bool can(int index) {
  int total = 0;
  for (int i = 0; i < a; i++) {
    total += (all[i] - 1) / index;
  }

  return total <= b;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // int a,b;
  cin >> a >> b;

  for (int i = 0; i < a; i++) {
    int first;
    cin >> first;
    all.push_back(first);
  }

  int lo = 0, hi = 1e9;

  int index = -1;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    if (can(mid)) {
      hi = mid - 1;
      index = mid;
    } else
      lo = mid + 1;
  }
  cout << index << endl;

  // cout<<index<<endl;

  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

int n, k, a[200005];

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

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  int lo = 0, hi = 1000000000, ans = hi;
  while (lo <= hi) {
    int mid = (hi + lo) / 2;
    // printf("%d %d\\n", mid, pos(mid));
    if (pos(mid)) {
      ans = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }
  printf("%d", ans);
}


Error: WA

Fixed submission:

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

int n, k, a[200005];

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

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  int lo = 0, hi = 1000000000, ans = hi;
  while (lo <= hi) {
    int mid = (hi + lo) / 2;
    // printf("%d %d\\n", mid, pos(mid));
    if (pos(mid)) {
      ans = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }
  printf("%d", ans);
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 0x6fffffff
#define INFL 0x6fffffffffffffffLL

int n, m, k;
vector<int> a(200001);

// 二分探索法
// k回まで切って丸太がindex以下になるればtrue;
bool isOK(int index) {
  int i, c = 0;

  for (i = 0; i < n; i++) {
    c += (a[i] + index - 1) / index - 1;
  }
  if (c <= k)
    return true;
  else
    return false;
}
// 汎用的な二分探索のテンプレ
int binary_search() {
  int left = 0; // 「index = 0」が条件を満たすこともあるので、初期値は -1
  int right =
      m + 1; // 「index = a.size()-1」が条件を満たさないこともあるので、初期値は
             // a.size()

  /* どんな二分探索でもここの書き方を変えずにできる！ */
  while (right - left > 1) {
    int mid = left + (right - left) / 2;

    if (isOK(mid))
      right = mid;
    else
      left = mid;
  }

  /* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている
   */
  return right;
}

int main() {
  int b, c, h, i, j, l, x, y;
  int ans = 0;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    m = max(m, a[i]);
  }

  if (m != 0)
    ans = binary_search();

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 0x6fffffff
#define INFL 0x6fffffffffffffffLL

int n, m, k;
vector<int> a(200001);

// 二分探索法
// k回まで切って丸太がindex以下になるればtrue;
bool isOK(int index) {
  int i, c = 0;

  for (i = 0; i < n; i++) {
    c += (a[i] + index - 1) / index - 1;
  }
  if (c <= k)
    return true;
  else
    return false;
}
// 汎用的な二分探索のテンプレ
int binary_search() {
  int left = -1; // 「index = 0」が条件を満たすこともあるので、初期値は -1
  int right =
      m + 1; // 「index = a.size()-1」が条件を満たさないこともあるので、初期値は
             // a.size()

  /* どんな二分探索でもここの書き方を変えずにできる！ */
  while (right - left > 1) {
    int mid = left + (right - left) / 2;

    if (isOK(mid))
      right = mid;
    else
      left = mid;
  }

  /* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている
   */
  return right;
}

int main() {
  int b, c, h, i, j, l, x, y;
  int ans = 0;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    m = max(m, a[i]);
  }

  ans = binary_search();

  cout << ans << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define ALL(x) (x).begin(), (x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

typedef pair<int, int> PI;
typedef pair<int, pair<int, int>> PII;
static const int INF = 1010000000000000017LL;
static const double eps = 1e-12;
static const double pi = 3.14159265358979323846;
static const int dx[4] = {1, -1, 0, 0};
static const int dy[4] = {0, 0, 1, -1};
static const int ddx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
static const int ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};

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

int N, K;
int A[200005];
int MAX_A;

bool check(int m) {
  int cnt = 0;
  for (int i = 0; i < N; ++i) {
    if (A[i] > m)
      cnt += A[i] / m;
  }
  if (cnt <= K)
    return true;
  else
    return false;
}

signed main() {
  cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    chmax(MAX_A, A[i]);
  }
  int l = 1, r = MAX_A;
  for (int i = 0; i < 100; ++i) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  if (check(l))
    cout << l << endl;
  else
    cout << r << endl;
  // cout<<(int)(ceil((double)(l+r)/2.0))<<endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define ALL(x) (x).begin(), (x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

typedef pair<int, int> PI;
typedef pair<int, pair<int, int>> PII;
static const int INF = 1010000000000000017LL;
static const double eps = 1e-12;
static const double pi = 3.14159265358979323846;
static const int dx[4] = {1, -1, 0, 0};
static const int dy[4] = {0, 0, 1, -1};
static const int ddx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
static const int ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};

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

int N, K;
int A[200005];
int MAX_A;

bool check(int m) {
  int cnt = 0;
  for (int i = 0; i < N; ++i)
    cnt += A[i] / m;
  if (cnt <= K)
    return true;
  else
    return false;
}

signed main() {
  cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    chmax(MAX_A, A[i]);
  }
  int l = 0, r = MAX_A;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  if (check(l))
    cout << l << endl;
  else
    cout << r << endl;
  // cout<<(int)(ceil((double)(l+r)/2.0))<<endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define ll long long

int cnt(int a[], int n, int len) {
  int ans = 0;
  for (int i = 0; i < n; i++) {
    // int low=1,high=1000;
    // for(int j=0;j<50;j++){
    // 	int mid = (low+high)/2;
    // 	float ha=a[i];
    // 	// cout<<ha/float(mid)<<" "<<mid<<" "<<high<<" "<<low<<endl;
    // 	if(ha/float(mid+1)<=len){
    // 		high=mid;
    // 	}
    // 	else
    // 	{
    // 		low=mid+1;
    // 	}
    // }
    int tempVal = (((a[i] - 1) / len));
    // cout<<tempVal<<endl;
    ans += tempVal;
  }
  return ans;
}

int main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int low = 1, high = 1e9 + 5;
  int ans = 0;
  for (int i = 0; i < 50; i++) {
    int mid = (high + low) / 2;
    if (mid == 0)
      continue;
    // cout<<mid<<endl;
    int value = cnt(a, n, mid);
    if (value <= k) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  // while(1){
  // 	if(cnt(a,n,ans-1)==k){
  // 		ans--;
  // 	}
  // 	else
  // 	break;
  // }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define ll long long

int cnt(int a[], int n, int len) {
  int ans = 0;
  for (int i = 0; i < n; i++) {
    // int low=1,high=1000;
    // for(int j=0;j<50;j++){
    // 	int mid = (low+high)/2;
    // 	float ha=a[i];
    // 	// cout<<ha/float(mid)<<" "<<mid<<" "<<high<<" "<<low<<endl;
    // 	if(ha/float(mid+1)<=len){
    // 		high=mid;
    // 	}
    // 	else
    // 	{
    // 		low=mid+1;
    // 	}
    // }
    int tempVal = (((a[i] - 1) / len));
    // cout<<tempVal<<endl;
    ans += tempVal;
  }
  return ans;
}

int main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int low = 1, high = 1e9 + 5;
  int ans = 0;
  for (int i = 0; i < 50; i++) {
    int mid = (high + low) / 2;
    int value = cnt(a, n, mid);
    // cout<<value<<" "<<mid<<endl;
    if (value <= k) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  while (1) {
    if (cnt(a, n, ans - 1) == k) {
      ans--;
    } else
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define lint long long
#define inf 100000000000000000
#define mod 1000000007
#define MAX 200005
#define pb push_back
#define vit vector<int>::iterator

int A[MAX], n, k;
int possible(int mid) {
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += (A[i] - 1) / mid;
  }
  if (sum <= k)
    return 1;
  return 0;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int mx = 0, hi, lo, mid;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    mx = max(mx, A[i]);
  }
  lo = 1;
  hi = mx;
  while (hi > lo) {
    mid = (hi + lo) / 2;
    if (possible(mid))
      hi = mid;
    else
      lo = mid + 1;
  }
  cout << lo;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define lint long long
#define inf 100000000000000000
#define mod 1000000007
#define MAX 200005
#define pb push_back
#define vit vector<int>::iterator

int A[MAX], n, k;
int possible(int mid) {
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (!(A[i] % mid))
      sum += A[i] / mid + 1;
    else
      sum += A[i] / mid;
  }
  if (sum <= k)
    return 1;
  return 0;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int mx = 0, hi, lo, mid;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    mx = max(mx, A[i]);
  }
  lo = 0;
  hi = mx;
  while (hi > lo) {
    mid = (hi + lo) / 2;
    if (possible(mid))
      hi = mid;
    else
      lo = mid + 1;
  }
  cout << lo;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
using namespace std;
const int MAX = 2e5 + 20;
typedef long long ll;
int pic[MAX], n, k;
bool check(int goal) {
  int tem = k, i;
  for (i = 1; i <= n; i++) {
    if (pic[i] <= goal)
      continue;
    else {
      int ans = 0, logg = pic[i];
      ans = logg / goal;
      if (logg % goal == 0)
        ans--;
      tem -= ans;
      if (tem < 0)
        return false;
    }
  }
  return true;
}
int main() {
  cin >> n >> k;
  int l = 1, r = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &pic[i]);
    r = max(pic[i], r);
  }
  while (l + 2 < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  for (int i = l; i <= r; i++)
    if (check(i)) {
      cout << i << endl;
      break;
    }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
using namespace std;
const int MAX = 2e5 + 20;
typedef long long ll;
int pic[MAX], n, k;
bool check(int goal) {
  int tem = k, i;
  for (i = 1; i <= n; i++) {
    if (pic[i] <= goal)
      continue;
    else {
      int ans = 0, logg = pic[i];
      /*while (logg > goal)
          logg >>= 1, ans++;*/
      ans = logg / goal;
      if (logg % goal == 0)
        ans--;
      tem -= ans;
      if (tem < 0)
        return false;
    }
  }
  return true;
}
int main() {
  cin >> n >> k;
  int l = 0, r = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &pic[i]);
    r = max(pic[i], r);
  }
  check(292638192);
  while (l + 2 < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  for (int i = l; i <= r; i++)
    if (check(i)) {
      cout << i << endl;
      break;
    }
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e15;

#include <math.h>
#define PI 3.14159265358979323846264338327950L
const int mxN = 1e9 + 7;

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

  int l = 1, r = 1e9;
  int ans = 0;
  while (l <= r) {
    int m = l + (r - l) / 2;
    long long sum = 0;
    for (int i = 0; i < n; i++) {
      sum += (a[i] - 1) / m;
    }
    if (sum > k) {
      l = m + 1;
    } else {
      ans = m;
      r = m - 1;
    }
  }
  cout << ans;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e15;

#include <math.h>
#define PI 3.14159265358979323846264338327950L
const int mxN = 1e9 + 7;

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

  int l = 0, r = 1e9;
  int ans = 0;
  while (l <= r) {
    int m = l + (r - l) / 2;
    long long sum = 0;
    for (int i = 0; i < n; i++) {
      sum += (a[i] - 1) / m;
    }
    if (sum > k) {
      l = m + 1;
    } else {
      ans = m;
      r = m - 1;
    }
  }
  cout << ans;

  return 0;
}