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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace __gnu_pbds;
using namespace std;
/*
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
*/
#define ull unsigned long long int
#define ll long long int
#define MAX 1000000007
// 1e9+7
#define pll pair<ll, ll>
#define pii pair<int, int>
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define FO                                                                     \\
  ifstream fin("a.in");                                                        \\
  ofstream fout("a.out");
#define TEST                                                                   \\
  int testcase;                                                                \\
  cin >> testcase;                                                             \\
  while (testcase--)
#define all(x) (x).begin(), (x).end()
#define mset0(x) memset((x), 0, sizeof((x)));
#define mset1(x) memset((x), -1, sizeof((x)));
#define endl "\\n"
#define plll pair<ll, pll>
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
FO ll n, k;
int arr[200001];
bool ok(int m) {
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    // if(arr[i]<=m) continue;
    if (arr[i] % m == 0)
      cnt += arr[i] / m - 1;
    else
      cnt += arr[i] / m;
  }

  return (cnt <= k);
}
int bsearch(int l, int r) {
  if (l == r)
    return (l);
  if (l + 1 == r) {
    if (ok(l))
      return (l);
    return (r);
  }
  int mid = r - (r - l) / 2;
  if (ok(mid))
    return (bsearch(l, mid));
  else
    return (bsearch(mid + 1, r));
}
void solve() {
  cin >> n >> k;
  int l = 1, r = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    r = max(r, arr[i]);
  }
  int ans = bsearch(l, r);
  cout << ans;
  return;
}
int main() {
  IO
  // TEST
  solve();
  return (0);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace __gnu_pbds;
using namespace std;
/*
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
*/
#define ull unsigned long long int
#define ll long long int
#define MAX 1000000007
// 1e9+7
#define pll pair<ll, ll>
#define pii pair<int, int>
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define FO                                                                     \\
  ifstream fin("a.in");                                                        \\
  ofstream fout("a.out");
#define TEST                                                                   \\
  int testcase;                                                                \\
  cin >> testcase;                                                             \\
  while (testcase--)
#define all(x) (x).begin(), (x).end()
#define mset0(x) memset((x), 0, sizeof((x)));
#define mset1(x) memset((x), -1, sizeof((x)));
#define endl "\\n"
#define plll pair<ll, pll>
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
FO ll n, k;
int arr[200001];
bool ok(int m) {
  priority_queue<int> pq;
  for (int i = 0; i < n; i++) {
    if (arr[i] > m)
      pq.push(arr[i]);
  }
  int kk = k;
  while (kk-- && !pq.empty()) {
    int a = pq.top();
    pq.pop();
    if (a <= m)
      return (true);
    pq.push(a - m);
  }
  return (pq.top() <= m || pq.empty());
}
int bsearch(int l, int r) {
  if (l == r)
    return (l);
  if (l + 1 == r) {
    if (ok(l))
      return (l);
    return (r);
  }
  int mid = r - (r - l) / 2;
  if (ok(mid))
    return (bsearch(l, mid));
  else
    return (bsearch(mid + 1, r));
}
void solve() {
  cin >> n >> k;
  int l = 1, r = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    r = max(r, arr[i]);
  }
  int ans = bsearch(l, r);
  cout << ans;
  return;
}
int main() {
  IO
  // TEST
  solve();
  return (0);
}