Problem p02598 (C++) — buggy submission, verdict: Runtime Error

/// Nahid Hasan Raju
#include <bits/stdc++.h>
#define ll long long
#define MS(ARRAY, VALUE) memset(ARRAY, VALUE, sizeof(ARRAY))
#define Fin freopen("input.txt", "r", stdin)
using namespace std;

#define P1(XX) cerr << "db1: " << XX << endl
#define P2(XX, YY) cerr << "db2: " << XX << " " << YY << endl
#define P3(XX, YY, ZZ) cerr << "db3: " << XX << " " << YY << " " << ZZ << endl
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define Fout freopen("output.txt", "w", stdout)
#define rep(i, a, b) for (i = a; i <= b; i++)
#define EPS 0.00000001
#define INF INT_MAX
#define PI 2 * acos(0.0)
#define set(XX, POS) XX |= (1 << POS)
#define reset(XX, POS) XX &= (~(1 << POS))
#define check(XX, POS) (bool)(XX & (1 << POS))
#define toggle(XX, POS) (XX ^ (1 << POS))
#define SORT(v) sort(v.begin(), v.end())
#define REVERSE(V) reverse(v.begin(), v.end())
#define VALID(X, Y, R, C) X >= 0 && X < R &&Y >= 0 && Y < C
#define SIZE(ARRAY) sizeof(ARRAY) / sizeof(ARRAY[0])
#define RT                                                                     \\
  printf("Run Time : %0.3lf seconds\\n", clock() / (CLOCKS_PER_SEC * 1.0))
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    odrst;

#define MOD 1000000007ll
#define MX 200006 /// 200005

ll m, n;
ll a[MX];
bool f(ll p) {
  ll c = 0, u;
  for (int i = 0; i < n; i++) {
    if (a[i] <= p)
      break;
    u = (((a[i] + p - 1) / p) - 1);
    c += u;
    if (c > m)
      return 0;
  }
  // P1(c);
  if (c <= m)
    return 1;
  return 0;
}

int main() {
#ifndef ONLINE_JUDGE
  Fin;
#endif
  int test = 1, tc = 0, i, j;
  ll k, ans;
  ll c, y, x, mid;
  // scanf("%d", &test);
  while (test--) {
    ans = 0;
    cin >> n >> m;

    for (i = 0; i < n; i++)
      cin >> a[i];
    sort(a, a + n);
    reverse(a, a + n);

    x = 1;
    y = INT_MAX;
    while (x < y) {

      mid = (x + y) / 2;
      // P3(x,mid,y);
      if (f(mid)) {
        y = mid;
      } else {
        x = mid + 1;
      }

      //            if(y-x<=5){
      //                if(f(mid)==1){if(f(mid-1)==0)break;}
      //                else if(f(mid+1)==1){
      //                        //mid++;
      //                        break;
      //                }
      //            }
    }
    if (!f(mid))
      mid++;

    printf("%lld\\n", mid);
  }
  return 0;
}


Error: WA

Fixed submission:

/// Nahid Hasan Raju
#include <bits/stdc++.h>
#define ll long long
#define MS(ARRAY, VALUE) memset(ARRAY, VALUE, sizeof(ARRAY))
#define Fin freopen("input.txt", "r", stdin)
using namespace std;

#define P1(XX) cerr << "db1: " << XX << endl
#define P2(XX, YY) cerr << "db2: " << XX << " " << YY << endl
#define P3(XX, YY, ZZ) cerr << "db3: " << XX << " " << YY << " " << ZZ << endl
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define Fout freopen("output.txt", "w", stdout)
#define rep(i, a, b) for (i = a; i <= b; i++)
#define EPS 0.00000001
#define INF INT_MAX
#define PI 2 * acos(0.0)
#define set(XX, POS) XX |= (1 << POS)
#define reset(XX, POS) XX &= (~(1 << POS))
#define check(XX, POS) (bool)(XX & (1 << POS))
#define toggle(XX, POS) (XX ^ (1 << POS))
#define SORT(v) sort(v.begin(), v.end())
#define REVERSE(V) reverse(v.begin(), v.end())
#define VALID(X, Y, R, C) X >= 0 && X < R &&Y >= 0 && Y < C
#define SIZE(ARRAY) sizeof(ARRAY) / sizeof(ARRAY[0])
#define RT                                                                     \\
  printf("Run Time : %0.3lf seconds\\n", clock() / (CLOCKS_PER_SEC * 1.0))
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    odrst;

#define MOD 1000000007ll
#define MX 200006 /// 200005

ll m, n;
ll a[MX];
bool f(ll p) {
  ll c = 0, u;
  for (int i = 0; i < n; i++) {
    if (a[i] <= p)
      break;
    u = (((a[i] + p - 1) / p) - 1);
    // if(u>0)
    c += u;
    if (c > m)
      return 0;
  }
  P1(c);
  if (c <= m)
    return 1;
  return 0;
}

int main() {
#ifndef ONLINE_JUDGE
  Fin;
#endif
  int test = 1, tc = 0, i, j;
  ll k, ans;
  ll c, y, x, mid;
  // scanf("%d", &test);
  while (test--) {
    ans = 0;
    cin >> n >> m;

    for (i = 0; i < n; i++)
      cin >> a[i];
    sort(a, a + n);
    reverse(a, a + n);

    x = 0;
    y = INT_MAX;
    while (x < y) {

      mid = (x + y) / 2;
      P3(x, mid, y);
      if (f(mid)) {
        y = mid;
      } else {
        x = mid + 1;
      }

      if (y - x <= 5) {
        if (f(mid) == 1) {
          if (f(mid - 1) == 0)
            break;
        } else if (f(mid + 1) == 1) {
          // mid++;
          break;
        }
      }
    }
    if (!f(mid))
      mid++;

    printf("%lld\\n", mid);
  }
  return 0;
}