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

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

ll A[200006];
int N, K;

bool f(ll x) {
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] <= x)
      continue;
    cnt += (int)ceil(1.0 * A[i] / x) - 1;
  }
  return cnt <= K;
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  ll ub = 1LL << 60;
  ll lb = 0;
  while (ub - lb > 1) {
    ll mid = (ub + lb) / 2;
    if (f(mid))
      ub = mid;
    else
      lb = mid;
  }
  cout << ub << endl;
}

Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

ll A[100006];
int N, K;

bool f(ll x) {
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] <= x)
      continue;
    cnt += (int)ceil(1.0 * A[i] / x) - 1;
  }
  return cnt <= K;
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  ll ub = 1LL << 60;
  ll lb = 0;
  while (ub - lb > 1) {
    ll mid = (ub + lb) / 2;
    if (f(mid))
      ub = mid;
    else
      lb = mid;
  }
  cout << ub << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

// https://atcoder.jp/contests/abc174/tasks/abc174_e
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define DIV(a, b) ((a - 1) / b + 1)

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  REP(i, N) { cin >> A[i]; }
  sort(ALL(A));
  // 二分探索
  int ng = 0, ok = 1e9;
  // 全ての丸太を長さtarget以下にする
  auto fn = [&](int target) {
    int cnt = 0;
    REP(i, N) {
      // 例えば長さ7を7以下にするなら切らなくていい。
      // 7未満にするなら1回切るように数える。
      cnt += DIV(A[i], target) - 1;
      if (cnt > K) {
        break;
      }
    }
    return cnt <= K;
  };
  while (ok - ng > 1) {
    int mid = (ng + ok) / 2;
    (fn(mid) ? ok : ng) = mid;
  }
  cout << ok << endl;

  return 0;
}


Error: TLE

Fixed submission:

// https://atcoder.jp/contests/abc174/tasks/abc174_e
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define DIV(a, b) ((a - 1) / b + 1)

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  priority_queue<pair<double, int>> q;
  vector<int> A(N), divCount(N);
  REP(i, N) {
    double t;
    cin >> t;
    A[i] = t;
    q.push({t, i});
  }
  // 各丸太に対して、何回切れるか計算
  REP(i, K) {
    auto t = q.top();
    q.pop();
    double half = A[t.second] / (divCount[t.second] + 2);
    divCount[t.second]++;
    q.push({half, t.second});
  }
  double maxv = 0;
  REP(i, N) { maxv = max(maxv, (double)A[i] / (divCount[i] + 1)); }
  cout << (int)ceil(maxv) << endl;

  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: 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<double, int>;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) cin >> A.at(i);

  int l = 0, r = 1e9;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    auto f = [&](int x) {
      ll now = 0;
      rep(i, N) { now += (A.at(i) - 1) / x; }
      return now <= K;
    };
    if (f(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << endl;
  return 0;
}

Error: TLE

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<double, int>;

int main() {
  int N, K;
  cin >> N >> K;

  priority_queue<P> q;
  rep(i, N) {
    double A;
    cin >> A;
    q.push(make_pair(A, 0));
  }

  rep(i, K) {
    P p = q.top();
    q.pop();
    double len = p.first;
    int num = p.second;

    double nlen = len * (num + 1) / (num + 2);
    q.push(make_pair(nlen, num + 1));
  }
  int ans = ceil(q.top().first);

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

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

vector<ll> a;
ll n, k;

bool ok(ll x) {
  ll res = 0;
  for (ll i = 0; i < n; i++)
    if (a[i] > x) {
      res += a[i] / x;
      if (a[i] % x == 0)
        res--;
    }
  return res <= k;
}

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

  cin >> n >> k;
  a.resize(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll x = 0;
  for (ll b = 2000000000; b; b /= 2)
    while (!ok(x + b))
      x += b;
  cout << x + 1 << '\\n';
}


Error: TLE

Fixed submission:

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

vector<ll> a;
ll n, k;

bool ok(ll x) {
  ll res = 0;
  for (ll i = 0; i < n; i++)
    if (a[i] > x) {
      res += a[i] / x;
      if (a[i] % x == 0)
        res--;
    }
  return res <= k;
}

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

  cin >> n >> k;
  a.resize(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll x = 0;
  for (ll b = 300000; b; b /= 2)
    while (!ok(x + b))
      x += b;
  cout << x + 1 << '\\n';
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

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

struct edge {
  int to;
  ll cost;
};

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};

ll GCD(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}

ll LCM(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return a / GCD(a, b) * b;
}

int main() {
  int n;
  ll k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll lo = 0, hi = 1000000005;
  while (hi - lo > 1) {
    ll mid = (lo + hi) / 2;
    ll tmp = 0;
    for (int i = 0; i < n; i++) {
      tmp += a[i] / mid - 1;
      if (a[i] % mid)
        tmp++;
    }
    if (tmp > k)
      lo = mid;
    else
      hi = mid;
  }
  cout << hi << endl;
  return 0;
}


Error: 0

Fixed submission:

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

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

struct edge {
  int to;
  ll cost;
};

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};

ll GCD(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}

ll LCM(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return a / GCD(a, b) * b;
}

int main() {
  int n;
  ll k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll lo = -1, hi = 1000000005;
  while (hi - lo > 1) {
    ll mid = (lo + hi) / 2;
    ll tmp = 0;
    for (int i = 0; i < n; i++) {
      tmp += a[i] / mid - 1;
      if (a[i] % mid)
        tmp++;
    }
    if (tmp > k)
      lo = mid;
    else
      hi = mid;
  }
  cout << hi << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;

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

  long long left = -1, right = 1000000000;
  while (right - left > 1) {
    long long mid = (left + right) / 2;
    long long count = 0;
    for (long long i = 0; i < N; i++) {
      if (mid == 0) {
        count = 100000000000;
        break;
      }
      count += A[i] / mid;
      if (A[i] % mid == 0)
        count--;
      if (count > K)
        break;
    }
    if (count <= K)
      right = mid;
    else
      left = mid;
  }

  cout << right << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

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

  long long left = -1, right = 1000000000;
  while (right - left > 1) {
    long long mid = (left + right) / 2;
    long long count = 0;
    for (long long i = 0; i < N; i++) {
      count += A[i] / mid;
      if (A[i] % mid == 0)
        count--;
      if (count > K)
        break;
    }
    if (count <= K)
      right = mid;
    else
      left = mid;
  }

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

#include <bits/stdc++.h>
#define ll long long
using namespace std;
template <class T> inline void read(T &res) {
  char c;
  T flag = 1;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-')
      flag = -1;
  res = c - '0';
  while ((c = getchar()) >= '0' && c <= '9')
    res = res * 10 + c - '0';
  res *= flag;
}
void gcd(ll a, ll b, ll &d, ll &x, ll &y) {
  if (!b) {
    d = a;
    x = 1;
    y = 0;
  } else {
    gcd(b, a % b, d, y, x);
    y -= x * (a / b);
  }
}
ll in(ll a, ll mod) {
  ll d, x, y;
  gcd(a, mod, d, x, y);
  return d == 1 ? (x + mod) % mod : -1;
}
int n;
ll k, a[200005];
ll l = 1, r = 1e9 + 7, mid;
int ok(ll x) {
  ll c = 0;
  for (int i = 0; i < n; i++) {
    c += a[i] / x - 1;
    if (a[i] % x != 0)
      c++;
  }
  if (k >= c)
    return 1;
  else
    return 0;
}
int main() {
  read(n);
  read(k);
  for (int i = 0; i < n; i++) {
    read(a[i]);
    r = max(r, a[i]);
  }
  while (l <= r) {
    mid = (l + r) / 2;
    if (ok(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}

Error: -8

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
template <class T> inline void read(T &res) {
  char c;
  T flag = 1;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-')
      flag = -1;
  res = c - '0';
  while ((c = getchar()) >= '0' && c <= '9')
    res = res * 10 + c - '0';
  res *= flag;
}
void gcd(ll a, ll b, ll &d, ll &x, ll &y) {
  if (!b) {
    d = a;
    x = 1;
    y = 0;
  } else {
    gcd(b, a % b, d, y, x);
    y -= x * (a / b);
  }
}
ll in(ll a, ll mod) {
  ll d, x, y;
  gcd(a, mod, d, x, y);
  return d == 1 ? (x + mod) % mod : -1;
}
ll n;
ll k, a[200005];
ll l = 0, r = 1e9 + 7, mid;
int ok(ll x) {
  ll c = 0;
  for (int i = 0; i < n; i++) {
    c += a[i] / x - 1;
    if (a[i] % x != 0)
      c++;
  }
  if (k >= c)
    return 1;
  else
    return 0;
}
int main() {
  read(n);
  read(k);
  for (int i = 1; i < n; i++) {
    read(a[i]);
    r = max(r, a[i]);
  }

  while (l <= r) {
    mid = (l + r) / 2;
    if (ok(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const int mod = 1e9 + 7;
// Not for Rating """"""""""I'm in Love with Experience :)""""""""""""""""""
ll check(ll a[], ll n, ll k, ll mid) {
  ll i, j;
  ll res = 0;
  for (i = 0; i < n; i++) {
    res += (((a[i] + mid - 1) / mid) - 1);
  }
  if (res <= k) {
    return 1;
  }
  return 0;
}
void solve() {
  ll n, k;
  ll i, j;
  cin >> n >> k;
  ll a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll maxi = *max_element(a, a + n);

  ll l = 1, r = maxi;
  ll ans = maxi;
  while (l <= r) {
    ll mid = l + (r - l) / 2;

    if (check(a, n, k, mid)) {
      ans = min(ans, mid);
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << ans << "\\n";
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int T = 1;
  while (T--) {
    solve();
  }
  return 0;
} /*Solved By:- RITIK AGARWAL*/

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const int mod = 1e9 + 7;
// Not for Rating """"""""""I'm in Love with Experience :)""""""""""""""""""
bool logs(ll *A, ll N, ll K, ll mid) {
  ll a = 0;
  for (ll i = 0; i < N; ++i) {
    if (A[i] <= mid)
      continue;
    a += A[i] / mid;
  }
  return a <= K;
}
void solve() {
  ll N;
  cin >> N;
  ll K;
  cin >> K;
  ll A[N], l = 0, h = 1e9 + 7;
  for (ll i = 0; i < N; ++i) {
    cin >> A[i];
    // h+=A[i];
  }

  ll ans = 0;
  while (l <= h) {
    ll mid = l + (h - l) / 2;
    if (logs(A, N, K, mid)) {
      ans = mid;
      h = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int T = 1;
  while (T--) {
    solve();
  }
  return 0;
} /*Solved By:- RITIK AGARWAL*/
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  int hmax = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > hmax)
      hmax = a[i];
  }
  int l = 1, h = hmax;
  while (l != h) {
    int m = l + (h - l) / 2;

    ll cnt = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] % m == 0) {
        cnt += (a[i] / m - 1);
      } else {
        cnt += a[i] / m;
      }
    }
    if (cnt <= k) {
      h = m;
    } else {
      l = m + 1;
    }
  }
  cout << l;
  return 0;
}

Error: WA

Fixed submission:

#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  int hmax = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > hmax)
      hmax = a[i];
  }
  int l = 0, h = hmax;
  while (l != h && h != 0) {
    int m = l + (h - l) / 2;

    ll cnt = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] % m == 0) {
        cnt += (a[i] / m - 1);
      } else {
        cnt += a[i] / m;
      }
    }
    if (cnt <= k) {
      h = m;
    } else {
      l = m + 1;
    }
  }
  cout << l;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define endl '\\n'
#define ll long long
#define double long double
using namespace std;

const ll inf = 10000000000000000;
ll n, k, a[200005], mux, l, r, m;
bool check(ll mun, ll kol) {
  ll ans = 0;
  if (mun > 0)
    for (ll i = 0; i < n; ++i)
      ans += ((a[i] / mun) + (a[i] % mun != 0) - 1);
  else
    for (ll i = 0; i < n; ++i)
      ans += (a[i] * 3);
  return (ans <= kol);
}

int main() {
  // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  cin >> n >> k;
  for (ll i = 0; i < n; ++i) {
    cin >> a[i];
    mux = max(mux, a[i]);
  }
  r = mux + 1;
  l = 0;
  while (r - l > 1) {
    m = l + (r - l) / 2;
    // cout << "LOL\\n";
    if (check(m, k))
      r = m;
    else
      l = m;
  }
  cout << r;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define endl '\\n'
#define ll long long
#define double long double
using namespace std;

const ll inf = 10000000000000000;
ll n, k, a[200005], mux, l, r, m;
bool check(ll mun, ll kol) {
  ll ans = 0;
  for (ll i = 0; i < n; ++i)
    ans += ((a[i] / mun) + (a[i] % mun != 0) - 1);
  return (ans <= kol);
}

int main() {
  // freopen("input.txt", "r", stdin);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (ll i = 0; i < n; ++i) {
    cin >> a[i];
    mux = max(mux, a[i]);
  }
  r = mux + 1;
  l = -1;
  while (r - l > 1) {
    m = l + (r - l) / 2;
    if (check(m, k))
      r = m;
    else
      l = m;
  }
  cout << r;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, k;
  cin >> n >> k;
  int a[n], ng = 0, ok = 1e9 + 1, mid, tk;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  while (ok - ng > 1) {
    mid = ng + (ok - ng) / 2;
    tk = 0;
    for (int i = 0; i < n; i++)
      tk += max(0, a[i] / mid - (a[i] % mid == 0 ? 1 : 0));
    (tk <= k ? ok : ng) = mid;
  }
  cout << ok << endl;
}


Error: 0

Fixed submission:

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

int main() {
  int n, k;
  cin >> n >> k;
  int a[n], ng = -1, ok = 1e9 + 1, mid, tk;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  while (ok - ng > 1) {
    mid = ng + (ok - ng) / 2;
    tk = 0;
    for (int i = 0; i < n; i++)
      tk += max(0, a[i] / mid - (a[i] % mid == 0 ? 1 : 0));
    (tk <= k ? ok : ng) = mid;
  }
  cout << ok << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

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

  ll l = 0, r = INF;

  vector<ll> a(n);

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

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

    ll count = 0;

    for (ll i = 0; i < n; i++) {
      if (a[i] % mid == 0) {
        count += a[i] / mid - 1;
      } else {
        count += a[i] / mid;
      }
    }

    if (count <= k) {
      r = mid;
    } else {
      l = mid;
    }
  }

  cout << r << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

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

  ll l = -1, r = INF;

  vector<ll> a(n);

  ll ans = 0;

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

    ans = max(ans, a[i]);
  }

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

    ll count = 0;

    for (ll i = 0; i < n; i++) {
      if (a[i] % mid == 0) {
        count += a[i] / mid - 1;
      } else {
        count += a[i] / mid;
      }
    }

    if (count <= k) {
      r = mid;
    } else {
      l = mid;
    }
  }

  cout << r << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef vector<int> vi;

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

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

  sort(a.begin(), a.end(), greater<double>());

  int l = a[0] / (k + 1);
  int r = a[0];
  bool f = false;
  while (l < r) {
    int m = (l + r) / 2;

    ll re = k;
    for (int i = 0; i < n; i++) {
      if (a[i] > m) {
        re -= ceil(a[i] / m) - 1;
        if (re < 0)
          break;
      }
    }

    if (re >= 0) {
      r = m;
      f = true;
    } else {
      l = m + 1;
      f = false;
    }
  }

  if (f) {
    cout << setprecision(20) << r << endl;
    return 0;
  } else {
    cout << setprecision(20) << l << endl;
    return 0;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef vector<int> vi;

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

  priority_queue<pair<double, int>> a;
  for (int i = 0; i < n; i++) {
    double ai;
    cin >> ai;
    a.push(make_pair(ai, 1));
  }

  while (k > 0) {
    auto max1 = a.top();
    a.pop();
    auto max2 = a.top();

    max1.first *= max1.second;

    int j = ceil(max1.first / max2.first);

    int dif = j - max1.second;
    if (k <= dif) {
      max1.first /= max1.second + dif;
      a.push(max1);
      break;
    }

    k -= dif;
    max1.first /= j;
    max1.second = j;

    a.push(max1);
  }

  cout << setprecision(20) << ceil(a.top().first) << endl;

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

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using P = pair<int, int>;
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <set>

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  int l = 0;
  int 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;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using P = pair<double, int>;
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>

int main() {
  ll n, k;
  cin >> n >> k;
  vector<double> a(n);
  priority_queue<P> q;
  rep(i, n) {
    double buf;
    cin >> buf;
    a[i] = buf;
    P p = {buf, i};
    q.push(p);
  }
  vector<int> b(n);
  while (k) {
    P tp = q.top();
    q.pop();
    double x = tp.first;
    // cout << x << endl;
    int y = tp.second;
    // cout << y << endl;
    b[y]++;
    x = a[y] / (b[y] + 1);
    P p = {x, y};
    q.push(p);
    --k;
  }
  P ans = q.top();
  cout << ll(ceil(ans.first)) << endl;
  // sort(a.rbegin(), a.rend());

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

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

bool ispossible(long long n, long long k, long long *arr, long long piecelen) {
  long long curr = 0;
  for (long long i = 0; i < n; i++) {
    long long numpiece = arr[i] / piecelen;
    if (numpiece * piecelen < arr[i])
      numpiece++;
    curr += (numpiece - 1);
  }
  if (curr <= k)
    return true;
  else
    return false;
}

int main() {
  long long n, k;
  cin >> n >> k;
  long long arr[n];
  for (long long i = 0; i < n; i++)
    cin >> arr[i];
  long long low = 1, mid, high = 1000000000000, ans = 1000000000000;
  while (low <= high) {
    mid = (low + high) / 2;
    if (ispossible(n, k, arr, mid)) {
      ans = mid;
      high = mid - 1;
    } else
      low = mid + 1;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

bool ispossible(long long n, long long k, long long *arr, long long piecelen) {
  long long curr = 0;
  for (long long i = 0; i < n; i++) {
    long long numpiece = arr[i] / piecelen;
    long long maxlen = numpiece * piecelen + ((numpiece - 1) / 2);
    if (maxlen < arr[i])
      numpiece++;
    curr += (numpiece - 1);
  }
  if (curr <= k)
    return true;
  else
    return false;
}

int main() {
  long long n, k;
  cin >> n >> k;
  long long arr[n];
  for (long long i = 0; i < n; i++)
    cin >> arr[i];
  long long low = 0, mid, high = 1000000000000, ans = 1000000000000;
  while (low <= high) {
    mid = (low + high) / 2;
    if (ispossible(n, k, arr, mid)) {
      ans = mid;
      high = mid - 1;
    } else
      low = mid + 1;
  }
  cout << ans << endl;
}