Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define Fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define Freed freopen("0in.txt", "r", stdin);
#define Fout freopen("0out.txt", "w", stdout);
#define TC                                                                     \\
  int tt;                                                                      \\
  cin >> tt;                                                                   \\
  for (int t = 0; t < tt; t++)
#define ll long long
#define pb push_back
#define mp make_pair
#define Mod 1000000007
using namespace std;

void solve(int t) {
  int i, j, n, m, k, temp, c = 0;
  cin >> n >> k;
  int D[n + 5], cnt[n + 5];
  D[0] = 0;
  for (i = 1; i <= n; i++) {
    cin >> D[i];
  }
  sort(D, D + n + 1);
  int left = 0, right = 1000000005, mid;
  while (left <= right) {
    c++;
    if (c > 100)
      break;
    mid = (left + right) / 2;
    int ct = 0;
    for (i = 1; i <= n; i++) {
      ct += ceil((double)D[i] / (double)mid);
      ct--;
    }
    // cout <<left<<" "<<right<<" "<<mid<<" "<<ct<<endl;
    if (ct <= k)
      right = mid;
    else if (ct > k)
      left = mid;
    else
      break;
  }
  cout << right << endl;
}
int main() {
  //    Fast
  //    Freed
  //    Fout
  int tt = 1, t;
  //    cin >> tt;
  for (t = 1; t <= tt; t++)
    solve(t);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define Fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define Freed freopen("0in.txt", "r", stdin);
#define Fout freopen("0out.txt", "w", stdout);
#define TC                                                                     \\
  int tt;                                                                      \\
  cin >> tt;                                                                   \\
  for (int t = 0; t < tt; t++)
#define ll long long
#define pb push_back
#define mp make_pair
#define Mod 1000000007
using namespace std;

void solve(int t) {
  int i, j, n, m, k, temp;
  cin >> n >> k;
  int D[n + 5], cnt[n + 5];
  for (i = 0; i < n + 5; i++) {
    cnt[i] = 1;
  }
  D[0] = 0;
  for (i = 1; i <= n; i++) {
    cin >> D[i];
  }
  sort(D, D + n + 1);
  multiset<pair<int, int>> myset;
  multiset<pair<int, int>>::iterator it;
  for (i = 1; i <= n; i++) {
    myset.insert(mp(D[i], i));
  }
  while (k) {
    k--;
    it = myset.end();
    it--;
    int ind = it->second;
    double d, ct;
    d = (double)D[ind];
    ct = (double)cnt[ind];
    cnt[ind]++;
    temp = (int)ceil(1.0 * d / (ct + 1.00)); // cout <<it->first<<" "<<ind<<"
                                             // "<<d<<" "<<ct<<" "<<temp<<endl;
    myset.erase(it);
    myset.insert(mp(temp, ind));
  }
  it = myset.end();
  it--;
  cout << it->first << endl;
}
int main() {
  //    Fast
  //    Freed
  //    Fout
  int tt = 1, t;
  //  cin >> tt;
  for (t = 1; t <= tt; t++)
    solve(t);
  return 0;
}