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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define int long long
#define mid ((l + r) >> 1)
#define MAXN (int)(2e5 + 2333)
int a[MAXN], asbsummadebysbzzx;
inline int f(int x, int y) {
  if (x % y)
    return (int)(x / y) + 1;
  return x / y;
}
signed main() {
  int n, k;
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    asbsummadebysbzzx += a[i];
  }
  if (!asbsummadebysbzzx) {
    cout << 0;
    return 0;
  }
  int l = 1, r = 1e9, ans = 1e9;
  long long sum;
  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 = min(ans, mid);
      r = mid;
    }
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: 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 = 0, r = 1e9, ans = 2e9;
  long long sum;
  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 = min(ans, mid);
      r = mid;
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#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;
}


Error: 0

Fixed submission:

#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;
}
Problem p02598 (C++) — buggy submission, verdict: 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) {
  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;
}

Error: 0

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) {
  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, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
using ll = int64_t;

bool ok(const vector<ll> &A, ll k, ll allowed) {
  ll cost = 0;
  for (ll i = 0; i < A.size(); i++) {
    // smallest K s.t. allowed*K >= A[i]
    // cost += K-1
    // 1..allowed -> 0
    // allowed+1..2*allowed -> 1
    cost += (A[i] - 1) / allowed;
  }
  return cost <= k;
}

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

  ll lo = 1;
  ll hi = 1e9;
  while (lo < hi) {
    ll mid = (lo + hi) / 2;
    // cerr << " mid=" << mid << " ok=" << ok(A, k, mid) << endl;
    if (ok(A, k, mid)) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  cout << lo << endl;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
using ll = int64_t;

bool ok(const vector<ll> &A, ll k, ll allowed) {
  ll cost = 0;
  for (ll i = 0; i < A.size(); i++) {
    // smallest K s.t. allowed*K >= A[i]
    // cost += K-1
    // 1..allowed -> 0
    // allowed+1..2*allowed -> 1
    cost += (A[i] - 1) / allowed;
  }
  return cost <= k;
}

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

  ll lo = 0;
  ll hi = 1e9;
  while (lo < hi) {
    ll mid = (lo + hi) / 2;
    // cerr << " mid=" << mid << " ok=" << ok(A, k, mid) << endl;
    if (ok(A, k, mid)) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  cout << lo << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
// *st.find_by_order(index), 0-based
// order_of_key(e)

ll eval(ll x, vector<ll> &A) {
  ll ret = 0;
  for (auto a : A)
    ret += (a + x - 1) / x - 1;
  return ret;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  ll low = 1, high = 1e9 + 7;
  while (low < high) {
    int mid = (low + high) / 2;
    if (eval(mid, A) <= K)
      high = mid;
    else
      low = mid + 1;
  }
  cout << min(*max_element(A.begin(), A.end()), high) << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
// *st.find_by_order(index), 0-based
// order_of_key(e)

ll eval(ll x, vector<ll> &A) {
  ll ret = 0;
  for (auto a : A)
    ret += (a + x - 1) / x - 1;
  return ret;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  ll low = 0, high = 1e9 + 7;
  while (low < high) {
    int mid = (low + high) / 2;
    if (eval(mid, A) <= K)
      high = mid;
    else
      low = mid + 1;
  }
  cout << min(*max_element(A.begin(), A.end()), high) << "\\n";
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

// Abhinav ---IIIT_A
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define f(i, x, n) for (int i = x; i < (int)n; ++i)
#define vpii vector<pair<int, int>>
#define vi vector<int>
#define mpii map<pair<int, int>, int>
#define mpivpii map<int, vector<pair<int, int>>>
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define mpi map<int, int>
#define vvi vector<vector<int>>
#define vvvi vector<vvi>
ld pie = 3.141592653589;
int mod = 1e9 + 7;

bool Compare(pair<int, int> a, pair<int, int> b) {
  int l1 = a.ss - a.ff + 1;
  int l2 = b.ss - b.ff + 1;
  if (l1 != l2)
    return l1 > l2;
  return a.ff > b.ff;
}
std::priority_queue<pii, std::vector<pii>, std::function<bool(pii, pii)>>
    pq(Compare);

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int ans = -1;
  int n, k;
  cin >> n >> k;
  int a[n];
  f(i, 0, n) cin >> a[i];
  int l = 1, h = 1e10;
  while (l <= h) {
    int mid = (l + h) / 2;
    int copy[n];
    f(i, 0, n) copy[i] = a[i];
    int cuts = 0;
    f(i, 0, n) {
      if (copy[i] > mid) {
        cuts += (((copy[i] + mid - 1) / mid) - 1);
      }
    }
    if (cuts <= k) {
      ans = mid;
      h = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans;
  return 0;
}
// check for overflows dummy !!!

Error: -11

Fixed submission:

// Abhinav ---IIIT_A
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define f(i, x, n) for (int i = x; i < (int)n; ++i)
#define vpii vector<pair<int, int>>
#define vi vector<int>
#define mpii map<pair<int, int>, int>
#define mpivpii map<int, vector<pair<int, int>>>
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define mpi map<int, int>
#define vvi vector<vector<int>>
#define vvvi vector<vvi>
ld pie = 3.141592653589;
int mod = 1e9 + 7;

bool Compare(pair<int, int> a, pair<int, int> b) {
  int l1 = a.ss - a.ff + 1;
  int l2 = b.ss - b.ff + 1;
  if (l1 != l2)
    return l1 > l2;
  return a.ff > b.ff;
}
std::priority_queue<pii, std::vector<pii>, std::function<bool(pii, pii)>>
    pq(Compare);

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int ans = -1;
  int n, k;
  cin >> n >> k;
  int a[n];
  f(i, 0, n) cin >> a[i];
  int l = 0, h = 1e10;
  while (l <= h) {
    int mid = (l + h) / 2;
    int copy[n];
    f(i, 0, n) copy[i] = a[i];
    int cuts = 0;
    f(i, 0, n) {
      if (copy[i] > mid) {
        cuts += (((copy[i] + mid - 1) / mid) - 1);
      }
    }
    if (cuts <= k) {
      ans = mid;
      h = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans;
  return 0;
}
// check for overflows dummy !!!
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define MAX 300005
const int maxn = 2e5 + 10;
int a[maxn];
int main() {
  LL n, k;
  cin >> n >> k;

  int cntR = 0;
  int ma = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
    ma = max(ma, a[i]);
  }
  LL l = 0, r = ma;
  LL d = k + n;
  while (l < r) {
    int p = 0;
    int m = (l + r + 1) / 2;
    if (m == 0) {
      break;
    } else
      for (int i = 0; i < n; ++i) {
        p += (a[i] + m - 1) / m;
      }
    if (p > d)
      l = m;
    else
      r = m - 1;
  }
  if (r == 0) {
    cout << 1;
    return 0;
  }
  int p = 0;
  for (int i = 0; i < n; ++i) {
    p += (a[i] + r - 1) / r;
  }
  if (p > d)
    cout << r + 1 << endl;
  else
    cout << r << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define MAX 300005
const int maxn = 2e5 + 10;
int a[maxn];
int main() {
  LL n, k;
  cin >> n >> k;

  int cntR = 0;
  int ma = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
    ma = max(ma, a[i]);
  }
  LL l = 0, r = ma;
  LL d = k + n;
  while (l < r) {
    int p = 0;
    int m = (l + r + 1) / 2;
    for (int i = 0; i < n; ++i) {
      p += (a[i] + m - 1) / m;
    }
    if (p > d)
      l = m;
    else
      r = m - 1;
  }

  int p = 0;
  for (int i = 0; i < n; ++i) {
    p += (a[i] + r - 1) / r;
  }
  if (p > d)
    cout << r + 1 << endl;
  else
    cout << r << 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++)
#define all(x) (x).begin(), (x).end()    // 昇順ソート
#define rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define INF 1 LL << 60
typedef long long int LL;
typedef long long int ll;
#define pll pair<ll, ll>
#define F first
#define S second
const int MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
// 任意のlogは　対数の底の変換を使う    log(N) / log(任意の底)

int main() {
  LL N;
  cin >> N;
  LL K;
  cin >> K;
  vector<LL> A(N);
  rep(i, N) cin >> A[i];

  LL lb = 0;
  LL ub = 1000000001;

  while (ub - lb > 1) {
    LL mid = (lb + ub) / 2;
    LL ct = 0;
    rep(i, N) { ct += ((A[i] + mid - 1) / mid) - 1; }

    if (ct <= K) {
      ub = mid;
    } else {
      lb = mid;
    }
  }

  cout << ub << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()    // 昇順ソート
#define rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define INF 1 LL << 60
typedef long long int LL;
typedef long long int ll;
#define pll pair<ll, ll>
#define F first
#define S second
const int MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
// 任意のlogは　対数の底の変換を使う    log(N) / log(任意の底)

int main() {
  LL N;
  cin >> N;
  LL K;
  cin >> K;
  vector<LL> A(N);
  rep(i, N) cin >> A[i];

  if (K == 0) {
    sort(rall(A));
    cout << A[0] << endl;
  } else {
    LL lb = -1;
    LL ub = 100000500;

    while (ub - lb > 1) {
      LL mid = (lb + ub) / 2;
      LL ct = 0;
      rep(i, N) { ct += A[i] / mid; }
      if (ct <= K) {
        ub = mid;
      } else {
        lb = mid;
      }
    }

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


#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

template <class T> using vec = vector<T>;
template <class T> using vec2 = vector<vec<T>>;
template <class T> using vec3 = vector<vec2<T>>;

constexpr int INF = numeric_limits<int>::max();
constexpr ll INFL = numeric_limits<ll>::max();
constexpr ll MOD = 1000000007; // 10^9+7

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

int N;
int K;
vec<ll> A;

ll solve() {
  ll ok = *max_element(A.begin(), A.end()) + 2;
  ll ng = 0;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;

    int cnt = 0;
    rep(i, N) { cnt += (A[i] + mid - 1) / mid - 1; }

    if (cnt <= K)
      ok = mid;
    else
      ng = mid;
  }
  return ok;
}

int main() {
  cin >> N >> K;
  A.resize(N);
  rep(i, N) cin >> A[i];
  cout << solve() << endl;
  return 0;
}


Error: 0

Fixed submission:


#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

template <class T> using vec = vector<T>;
template <class T> using vec2 = vector<vec<T>>;
template <class T> using vec3 = vector<vec2<T>>;

constexpr int INF = numeric_limits<int>::max();
constexpr ll INFL = numeric_limits<ll>::max();
constexpr ll MOD = 1000000007; // 10^9+7

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

int N;
int K;
vec<int> A;

int solve() {
  int ng = 1000000000 + 1;
  int ok = -1;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;

    int cnt = 0;
    rep(i, N) { cnt += (A[i] + mid - 1) / mid - 1; }

    if (cnt <= K) {
      ng = mid;
    } else {
      ok = mid;
    }
  }

  return ok;
}

int main() {
  cin >> N >> K;
  A.resize(N);
  rep(i, N) cin >> A[i];
  cout << solve() + 1 << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;

ll n, k;
ll mx = 0;
vector<ll> a(200001);

int search(int l, int r) {
  int x = (l + r) / 2;
  if (x == 0)
    return 1;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += a[i] / x - 1;
    if (a[i] % x != 0)
      ans++;
  }

  if (ans > k) {
    l = x;
  } else {
    r = x;
  }

  if (r - l <= 1)
    return r;
  else
    return search(l, r);
}

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

  int ans = search(0, mx);
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;

ll n, k;
ll mx = 0;
vector<ll> a(200001);

int search(int l, int r) {
  int x = (l + r) / 2;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += a[i] / x - 1;
    if (a[i] % x != 0)
      ans++;
  }

  if (ans > k) {
    l = x;
  } else {
    r = x;
  }

  if (r - l <= 1)
    return r;
  else
    return search(l, r);
}

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

  int ans = search(0, mx);
  if (ans == 0)
    ans = 1;
  cout << ans << endl;

  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

using TUP = tuple<double, int, int>;

int main() {
  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());

  // 最大長の最小値
  double lower = 1;
  double upper = a[0];

  while (ceil(lower) != ceil(upper)) {

    double mid = (lower + upper) / 2;
    // printf("lower %16.3f, upper %16.3f, mid %16.3f, diff %16.3f\\n",
    // 	lower, upper, mid, upper - lower);

    long long cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += (long long)ceil(1.0 * a[i] / mid) - 1;
    }
    if (cnt > k) {
      lower = mid;
    } else {
      upper = mid;
    }
    // printf("    cnt %lld\\n", cnt);
  }

  cout << (int)ceil(upper) << endl;
  return 0;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

using TUP = tuple<double, int, int>;

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

  priority_queue<TUP> que;
  int a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    que.emplace(a, a, 1);
  }

  double val;
  int x, y;
  for (int i = 0; i < k; i++) {
    tie(val, x, y) = que.top();
    // printf("i %d: %d %d\\n", i, x, y);
    que.pop();
    y++;
    que.emplace((double)x / y, x, y);
  }

  tie(val, x, y) = que.top();
  int ans = ceil(val);
  cout << ans << endl;

  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

bool check(const vector<int> &A, long long K, long long m) {
  long long count = 0;
  for (size_t i = 0; i < A.size(); i++) {
    // add smallest k s.t. A[i]/k <= m to count
    long long k = (A[i] - 1) / m;
    count += k;
  }
  return (count <= K);
}

int main(void) {
  long N, K;
  cin >> N >> K;
  vector<int> A(N);
  int l = 1, u = 0;
  for (long long i = 0; i < N; i++) {
    cin >> A[i];
    u = max(u, A[i]);
  }
  while (true) {
    if (l == u)
      break;
    long long m = (l + u) / 2;
    // cout << l << "," << u << "," << m << endl;
    if (check(A, K, m)) { // m can be achieved
      u = m;
    } else {
      l = m + 1;
    }
  }
  cout << l << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main(void) {
  long N, K;
  cin >> N >> K;
  vector<long long> A(N);
  set<pair<double, long long>> V; // A[i]/D[i]
  vector<long long> D(N, 1);      // (cuts+1)
  for (long long i = 0; i < N; i++) {
    cin >> A[i];
    V.insert(make_pair(A[i], i));
  }
  for (long long j = 0; j < K; j++) {
    pair<double, long long> max_elem = *(V.rbegin());
    long i = max_elem.second;
    // cout << "max: " << max_elem.first << "," << i << endl;
    D[i]++;
    V.erase(max_elem);
    V.insert(make_pair(((double)A[i]) / D[i], i));
  }
  pair<double, long long> max_elem = *(V.rbegin());
  cout << (long long)ceil(max_elem.first) << endl;
  return 0;
}