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

#include <bits/stdc++.h>
#define ll long long int
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define vi vector<int>
#define vvi vector<vector<int>>
#define mii map<int, int>
#define pb push_back
#define pii pair<int, int>
#define mkp make_pair
#define scan(a, n)                                                             \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> a[i]
#define print(a, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  cout << a[i] << ' '
#define mem(a, v) memset(a, v, sizeof(a))
#define loop(i, a, n) for (int i = a; i < n; i++)
#define FastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define PRECISION                                                              \\
  std::cout.unsetf(std::ios::floatfield);                                      \\
  std::cout.precision(15)
double PI = acos(-1.0);
using namespace std;
int mod = 1000000007;
int inf = INT_MAX;
int minf = INT_MIN;

/*int main()
{
    int x;
    cin>>x;
    if(x>=30)
        cout<<"Yes";
    else
        cout<<"No";
}*/

/*int main()
{
    ll n,d;
    cin>>n>>d;
    int cnt=0;
    while(n--)
    {
        ll x,y;
        cin>>x>>y;
        ll temp=x*x+y*y;
        ll ans=ceil(sqrt(temp));
        //cout<<ans<<endl;
        if(ans<=d)
            cnt++;
    }
    cout<<cnt;
}*/

/*int main()
{
    int k;
    cin>>k;
    if(k%2==0 || k%5==0)
    {
        cout<<"-1";
        return 0;
    }
    int first=7,cnt=1;
    while(k>first)
    {
        first=first*10+7;
        cnt++;
    }
    while(first>0)
    {
        first=first%k;
        if(first>0)
        {
            first=first*10+7;
            cnt++;
        }
    }
    cout<<cnt;
}*/

/*int main()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    int white=0,red=0,posw=-1,posr=n;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='W')
        {
            white++;
            if(posw==-1)
                posw=i;
        }
        else
        {
            red++;
            posr=i;
        }
    }
    if(white==0 || red==0 || posw>posr)
    {
        cout<<"0";
        return 0;
    }
    queue<int> q;
    int cnt=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='R')
        {
            if(i>=red && q.size())
            {
                s[q.front()]='R';
                s[i]='W';
                q.pop();
                cnt++;
            }
        }
        else
            q.push(i);
    }
    cout<<cnt;
}*/
ll n, k;
vector<ll> a;
bool check(ll mid) {
  ll steps = 0;
  for (ll x : a)
    steps += (x - 1) / mid;
  return steps <= k;
}

int main() {
  cin >> n >> k;
  a.resize(n);
  loop(i, 0, n) { cin >> a[i]; }
  ll lo = 1, hi = 1e9, ans;
  while (lo <= hi) {
    ll mid = (lo + hi) / 2;
    if (check(mid)) {
      hi = mid - 1;
      ans = mid;
    } else
      lo = mid + 1;
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define vi vector<int>
#define vvi vector<vector<int>>
#define mii map<int, int>
#define pb push_back
#define pii pair<int, int>
#define mkp make_pair
#define scan(a, n)                                                             \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> a[i]
#define print(a, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  cout << a[i] << ' '
#define mem(a, v) memset(a, v, sizeof(a))
#define loop(i, a, n) for (int i = a; i < n; i++)
#define FastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define PRECISION                                                              \\
  std::cout.unsetf(std::ios::floatfield);                                      \\
  std::cout.precision(15)
double PI = acos(-1.0);
using namespace std;
int mod = 1000000007;
int inf = INT_MAX;
int minf = INT_MIN;

/*int main()
{
    int x;
    cin>>x;
    if(x>=30)
        cout<<"Yes";
    else
        cout<<"No";
}*/

/*int main()
{
    ll n,d;
    cin>>n>>d;
    int cnt=0;
    while(n--)
    {
        ll x,y;
        cin>>x>>y;
        ll temp=x*x+y*y;
        ll ans=ceil(sqrt(temp));
        //cout<<ans<<endl;
        if(ans<=d)
            cnt++;
    }
    cout<<cnt;
}*/

/*int main()
{
    int k;
    cin>>k;
    if(k%2==0 || k%5==0)
    {
        cout<<"-1";
        return 0;
    }
    int first=7,cnt=1;
    while(k>first)
    {
        first=first*10+7;
        cnt++;
    }
    while(first>0)
    {
        first=first%k;
        if(first>0)
        {
            first=first*10+7;
            cnt++;
        }
    }
    cout<<cnt;
}*/

/*int main()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    int white=0,red=0,posw=-1,posr=n;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='W')
        {
            white++;
            if(posw==-1)
                posw=i;
        }
        else
        {
            red++;
            posr=i;
        }
    }
    if(white==0 || red==0 || posw>posr)
    {
        cout<<"0";
        return 0;
    }
    queue<int> q;
    int cnt=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='R')
        {
            if(i>=red && q.size())
            {
                s[q.front()]='R';
                s[i]='W';
                q.pop();
                cnt++;
            }
        }
        else
            q.push(i);
    }
    cout<<cnt;
}*/
ll n, k, a[200005];
bool check(ll mid) {
  ll steps = 0;
  for (ll x : a)
    steps += (x - 1) / mid;
  return steps <= k;
}

int main() {
  cin >> n >> k;
  ll large = 0;
  loop(i, 0, n) { cin >> a[i]; }
  ll lo = 0, hi = 1e9, ans;
  while (lo <= hi) {
    ll mid = (lo + hi) / 2;
    if (check(mid)) {
      hi = mid - 1;
      ans = mid;
    } else
      lo = mid + 1;
  }
  cout << ans;
}