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

// is there any case of -1 or any special corner cases ?
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int, int>
#define all(v) v.begin(), v.end()
#define zeros(arr) memset(arr, 0, sizeof(arr));
#define precise(num)                                                           \\
  cout << fixed;                                                               \\
  cout << std::setprecision(num)
#define nl cout << "\\n";
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
const int maxx = 3 * 1e5, limit = 9 * 1e18, MOD = 1e9 + 7;
#define boost                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n';

bool check(int n, int a[], int k, int tot) {
  int res = 0;

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

    int val = num / k;
    if (num % k)
      val++;

    val--;
    res += val;
  }
  if (res <= tot)
    return true;
  return false;
}

int32_t main() {
  boost;
  // int k;cin>>k;

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

  while (r - l > 1) {
    int m = (l + r) / 2;

    if (check(n, a, m, k)) {
      // cout<<"hi\\n";
      r = m;
    } else {
      l = m;
    }

    // cout<<l<<" "<<r<<endl;
  }
  cout << r << endl;
}

Error: TLE

Fixed submission:

// is there any case of -1 or any special corner cases ?
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int, int>
#define all(v) v.begin(), v.end()
#define zeros(arr) memset(arr, 0, sizeof(arr));
#define precise(num)                                                           \\
  cout << fixed;                                                               \\
  cout << std::setprecision(num)
#define nl cout << "\\n";
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
const int maxx = 3 * 1e5, limit = 9 * 1e18, MOD = 1e9 + 7;
#define boost                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n';

bool check(int n, int a[], int k, int tot) {

  priority_queue<int> q;
  for (int i = 0; i < n; i++) {
    q.push(a[i]);
  }

  while (tot--) {

    int num = q.top();
    q.pop();

    q.push(num - k);
    q.push(k);
  }

  int minn = -1e9;
  while (q.size()) {
    minn = max(minn, q.top());
    q.pop();
  }
  if (minn <= k)
    return true;

  return false;
}

int32_t main() {
  boost;
  // int k;cin>>k;

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

  while (r - l > 1) {
    int m = (l + r) / 2;

    if (check(n, a, m, k)) {
      // cout<<"hi\\n";
      r = m;
    } else {
      l = m;
    }

    // cout<<l<<" "<<r<<endl;
  }
  cout << r << endl;
}