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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define zip(i, a, b) for (int i = a; i < b; i++)
#define rzip(i, a, b) for (int i = a; i >= b; i--)
#define ll unsigned int
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define um unordered_map
#define en '\\n'
#define us unordered_set
typedef pair<int, int> pii;
typedef pair<char, int> pci;
typedef pair<char, char> pcc;
typedef vector<pii> vii;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define INF (1e18 + 5)
#define inf (1e9 + 5)
#define mod 1000000007

void __print(int x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
// You should only debug a pair of simple data types. For example,
// the debug won't work if one of pair's elements is collection type
// (std::vector, std::map, std::set...).
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

// bool check_prime(int n)  return whether a number is prime or not O(sqrt(N));
// int BE(int x,int n,int m)  return x^n%m; O(logN);
// void sieve()  from number 1-1000001 saare prime store kar lega  in bool
// is_prime[1000001] array vector<int> z_array(string s)  return vector which is
// Z-array of string s; vector<int> lps(string s)      return vector which is
// lps array of string s;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
#endif
  //_______________________________-code
  //starts-_______________________________________________
  // its incorrect approach as try n=1,l=6,k=2;
  // corrct answer 2 2 2
  // my answer 3 2 1
  // int n, k;
  // cin >> n >> k;
  // int a[n];
  // cout << fixed << setprecision(10);
  // multiset<double>s;
  // for(int i=0;i<n;i++)
  // {
  // 	cin >> a[i];
  // 	s.insert(1.000000000 * a[i]);
  // }
  // //sort(a, a + n, greater<int>());
  // for(int i=0;i<k;i++)
  // {
  // 	double x = *s.rbegin();
  // 	double a, b;
  // 	a = x / 2;
  // 	b = x / 2;
  // 	auto itr = s.find(x);
  // 	s.erase(itr);
  // 	s.insert(a);
  // 	s.insert(b);
  // }
  // cout << (int)*s.rbegin() << en;

  // mm what would be the correct approach
  // we can do binary search for answer
  int n, k;
  cin >> n >> k;
  int a[n];
  zip(i, 0, n) { cin >> a[i]; }
  int l = 1, r = INF + 100;
  int print;
  while (l <= r) {
    int m = (l + r) / 2;

    // print = m;

    // assuming answer is m
    int ans = 0;
    zip(i, 0, n) {
      if (a[i] > m) {
        if (m > 0)
          ans += (a[i] - 1) / m;
      }
    }
    if (ans <= k) {
      r = m - 1;
      print = m;
    } else {
      l = m + 1;
    }
  }
  cout << print << en;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define zip(i, a, b) for (int i = a; i < b; i++)
#define rzip(i, a, b) for (int i = a; i >= b; i--)
#define ll unsigned int
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define um unordered_map
#define en '\\n'
#define us unordered_set
typedef pair<int, int> pii;
typedef pair<char, int> pci;
typedef pair<char, char> pcc;
typedef vector<pii> vii;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define INF (1e18 + 5)
#define inf (1e9 + 5)
#define mod 1000000007

void __print(int x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
// You should only debug a pair of simple data types. For example,
// the debug won't work if one of pair's elements is collection type
// (std::vector, std::map, std::set...).
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

// bool check_prime(int n)  return whether a number is prime or not O(sqrt(N));
// int BE(int x,int n,int m)  return x^n%m; O(logN);
// void sieve()  from number 1-1000001 saare prime store kar lega  in bool
// is_prime[1000001] array vector<int> z_array(string s)  return vector which is
// Z-array of string s; vector<int> lps(string s)      return vector which is
// lps array of string s;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
#endif
  //_______________________________-code
  //starts-_______________________________________________
  // its incorrect approach as try n=1,l=6,k=2;
  // corrct answer 2 2 2
  // my answer 3 2 1
  // int n, k;
  // cin >> n >> k;
  // int a[n];
  // cout << fixed << setprecision(10);
  // multiset<double>s;
  // for(int i=0;i<n;i++)
  // {
  // 	cin >> a[i];
  // 	s.insert(1.000000000 * a[i]);
  // }
  // //sort(a, a + n, greater<int>());
  // for(int i=0;i<k;i++)
  // {
  // 	double x = *s.rbegin();
  // 	double a, b;
  // 	a = x / 2;
  // 	b = x / 2;
  // 	auto itr = s.find(x);
  // 	s.erase(itr);
  // 	s.insert(a);
  // 	s.insert(b);
  // }
  // cout << (int)*s.rbegin() << en;

  // mm what would be the correct approach
  // we can do binary search for answer
  int n, k;
  cin >> n >> k;
  int a[n];
  zip(i, 0, n) { cin >> a[i]; }
  int l = 0, r = INF;
  int print;
  while (l <= r) {
    int m = (l + r) / 2;
    // print = m;

    // assuming answer is m
    int ans = 0;
    zip(i, 0, n) {
      if (a[i] > m) {
        ans += (a[i] - 1) / m;
      }
    }
    if (ans <= k) {
      r = m - 1;
      print = m;
    } else {
      l = m + 1;
    }
  }
  cout << print << en;

  return 0;
}