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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

/*bool compare_function(const pair<int, int> a, const pair<int, int> b)
{
    return a.first < b.first;
}*/
// use case
// sort(V.begin(), V.end(), compare_function);

/*struct compare
{
    bool operator ()(const pair<int, int> &a, const pair<int, int> &b) {
    if (a.second-a.first==b.second-b.first)
    {
        return a.first>b.first;
    }
    return a.second-a.first < b.second-b.first;}
};
priority_queue <pair<int,int>, vector <pair<int,int>>, compare> Q;*/

/*void print1(vector <int> X)
{
    for (int i=0; i<X.size(); i++)
    {
        cout<<X[i]<<" ";
    }
    cout<<endl;
    return;
}

void print2(vector <vector <int>> X)
{
    for (int i=0; i<X.size(); i++)
    {
    for (int j=0;j<X[i].size();j++) {
        cout<<X[i][j]<<" ";}
    cout<<endl;}
    return;
}*/

bool solve(ll c, vector<ll> &A, int n, ll k) {
  ll w = 0;
  for (int i = 0; i < n; i++) {
    w += A[i] / c;
    if (A[i] % c == 0) {
      w--;
    }
  }
  if (w > k) {
    return false;
  }
  return true;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> A(n);
  ll d = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    d = max(d, A[i]);
  }
  ll a = 1;
  ll b = d;
  while (b - a > 3) {
    ll c = (a + b) / 2;
    if (solve(c, A, n, k)) {
      b = c;
    } else {
      a = c;
    }
  }
  while (!solve(a, A, n, k)) {
    a++;
  }
  cout << a << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

/*bool compare_function(const pair<int, int> a, const pair<int, int> b)
{
    return a.first < b.first;
}*/
// use case
// sort(V.begin(), V.end(), compare_function);

/*struct compare
{
    bool operator ()(const pair<int, int> &a, const pair<int, int> &b) {
    if (a.second-a.first==b.second-b.first)
    {
        return a.first>b.first;
    }
    return a.second-a.first < b.second-b.first;}
};
priority_queue <pair<int,int>, vector <pair<int,int>>, compare> Q;*/

/*void print1(vector <int> X)
{
    for (int i=0; i<X.size(); i++)
    {
        cout<<X[i]<<" ";
    }
    cout<<endl;
    return;
}

void print2(vector <vector <int>> X)
{
    for (int i=0; i<X.size(); i++)
    {
    for (int j=0;j<X[i].size();j++) {
        cout<<X[i][j]<<" ";}
    cout<<endl;}
    return;
}*/

bool solve(ll c, vector<ll> &A, int n, int k) {
  int w = 0;
  for (int i = 0; i < n; i++) {
    if (A[i] % c == 0) {
      w += (A[i] / c) - 1;
    } else {
      w += A[i] / c;
    }
  }
  if (w > k) {
    return false;
  }
  return true;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<ll> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  ll a = 0;
  ll b = 1000000000;
  while (b - a > 3) {
    ll c = (a + b) / 2;
    if (solve(c, A, n, k)) {
      b = c;
    } else {
      a = c;
    }
  }
  while (!solve(a, A, n, k)) {
    a++;
  }
  cout << a << endl;
  return 0;
}