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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

#define REP(k, n) for (ll k = 0; k < (ll)(n); ++k)
#define REPREV(k, n) for (ll k = (ll)(n)-1; k >= 0; --k)
#define FOR(k, a, b) for (ll k = (ll)(a); k < (ll)(b); ++k)
#define FORREV(k, a, b) for (ll k = (ll)(b)-1; k >= (ll)(a); --k)
#define FOREACH(e, v) for (auto &e : v)

#define TGET(t, i) get<i>(t)
#define TSET(t, i, a) get<i>(t) = a
// MinHeap - priority_queue<ll, vll, greater<ll>> pq;

#define EVEN(n) ((n)&1) == 0
#define ODD(n) ((n)&1) == 1

#define SORT(v) sort(v.begin(), v.end())
#define SORTREV(v) sort(v.rbegin(), v.rend())
#define SHUFFLE(v) random_shuffle(v.begin(), v.end())
// srand(unsigned(time(0)));

#define REMOVEAT(v, i) v.erase(v.begin() + i)
#define REVERSE(v) reverse(v.begin(), v.end())

ostream &operator<<(ostream &str, const pll &p) {
  str << p.first << " " << p.second;
  return str;
}

const ll md = 1e9 + 7;

#define POS "POSSIBLE"
#define IMPOS "IMPOSSIBLE"

// #define INF LLONG_MAX
// #define MINF LLONG_MIN
#define INF 1e18
#define MINF -INF

void solve() {
  ll n, k;
  cin >> n >> k;
  vll a(n);
  REP(i, n) { cin >> a[i]; }
  ll res = 0;

  ll lo = 1;
  ll hi = *max_element(a.begin(), a.end());
  if (k == 0) {
    cout << hi << "\\n";
    return;
  }
  res = hi;
  while (lo <= hi) {
    ll mid = lo + (hi - lo) / 2;
    ll cuts = 0;
    REP(i, n) {
      if (a[i] <= mid)
        continue;
      cuts += a[i] / mid;
    }
    if (cuts > k) {
      lo = mid + 1;
    } else {
      res = mid;
      hi = mid - 1;
    }
  }

  // cout << fixed << setprecision(6);
  cout << res << "\\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt = 1;
  // cin >> tt;
  for (int t = 1; t <= tt; t++) {
    // cout << "Case #" << t << ": ";
    solve();
  }
  // cin.rdbuf()->in_avail()
  return 0;
}


Error: TLE

Fixed submission:

/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<ll, pair<ll, ll>> plll;
#define fastread() (ios_base::sync_with_stdio(false), cin.tie(NULL));
#define vll(v) v.begin(), v.end()
#define all(x) x.rbegin(), x.rend()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define F first
#define S second
#define in freopen("input.txt", "r", stdin)
#define out freopen("output.txt", "w", stdout)
#define minheap int, vector<int>, greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x) (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch)                                                            \\
  ((ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') ||          \\
   (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'))
#define bug cout << "BUG" << endl;
const int Max = 2e5 + 10;
const int Mod = 1e9 + 7;
const double PI = 3.141592653589793238463;
bool compare(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.first > b.first);
}
ll lcm(ll a, ll b) {
  if (a == 0 || b == 0)
    return 0;

  return a / __gcd(a, b) * b;
}

void input(ll ara[], ll n) {
  for (ll i = 0; i < n; i++)
    cin >> ara[i];
}

ll ara[Max + 10];

ll n, k;
bool ok(ll val) {
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    // if(ara[i])
    cnt += (ara[i] / val);
  }

  // cout<<ara[0]<<" "<<cnt<<" "<<val<<endl;

  // cout<<cnt

  return (cnt <= k);
}
int main() {

  fastread();

  ll i, j, m, p, a, sum = 0, t, b, c, d, cnt = 0, q, l, r;

  string str;

  bool flag = true;

  ll low = 1, high = 2e9, ans = 0;

  cin >> n >> k;

  for (i = 0; i < n; i++) {
    cin >> ara[i];
    ans = max(ara[i], ans);
  }

  high = ans;

  // cout<<ans<<endl;

  if (k == 0) {
    cout << ans << endl;

    return 0;
  }

  while (low <= high) {
    ll mid = (low + high) / 2;

    if (ok(mid)) {
      ans = mid;
      high = mid - 1;
    } else
      low = mid;
  }

  cout << ans << endl;
}