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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <deque>
#include <functional>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <complex>
#include <numeric>
#include <utility>

#include <iomanip>
#include <iostream>
#include <sstream>

#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

// namespace
using namespace std;

// type alias
using ll = long long;
using ull = unsigned long long;
using comp = complex<double>;

// constant
static const ll MOD = 1000000007LL;
static const double PI = 3.14159265358979323846;

// conversion
inline ll toint(string s) {
  ll v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class t> inline string tostring(t x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// print
#define RET(x) return cout << x << endl, 0;

// for loop
#define REP(i, a, b) for ((i) = (ll)(a); (i) < (ll)(b); (i)++)
#define REPD(i, a, b) for (ll i = (ll)(a); (i) < (ll)(b); (i)++)
#define REPI(v, vs) for (auto &v : vs)

// debug
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << #x << " = " << (x) << " (l" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl

#define MAX_VALUE 9223372036854775807

int solve() {
  ll n, k;
  cin >> n >> k;

  vector<ll> as(n);
  REPD(i, 0, n) cin >> as[i];

  ll low = 1, high = 1000000000, mid = (low + high) / 2;
  while (high - low >= 2) {
    ll curr_k = 0;
    REPD(i, 0, n) { curr_k += (as[i] - 1) / mid; }

    if (curr_k <= k) {
      high = mid;
    } else {
      low = mid;
    }
    mid = (low + high) / 2;
  }

  {
    ll curr_k = 0;
    REPD(i, 0, n) { curr_k += (as[i] - 1) / low; }
    if (curr_k <= k) {
      RET(low);
    }
  }

  RET(high);

  return 0;
}

// main function
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  solve();

  return 0;
}


Error: 0

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <deque>
#include <functional>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <complex>
#include <numeric>
#include <utility>

#include <iomanip>
#include <iostream>
#include <sstream>

#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

// namespace
using namespace std;

// type alias
using ll = long long;
using ull = unsigned long long;
using comp = complex<double>;

// constant
static const ll MOD = 1000000007LL;
static const double PI = 3.14159265358979323846;

// conversion
inline ll toint(string s) {
  ll v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class t> inline string tostring(t x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// print
#define RET(x) return cout << x << endl, 0;

// for loop
#define REP(i, a, b) for ((i) = (ll)(a); (i) < (ll)(b); (i)++)
#define REPD(i, a, b) for (ll i = (ll)(a); (i) < (ll)(b); (i)++)
#define REPI(v, vs) for (auto &v : vs)

// debug
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << #x << " = " << (x) << " (l" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl

#define MAX_VALUE 9223372036854775807

int solve() {
  ll n, k;
  cin >> n >> k;

  vector<ll> as(n);
  REPD(i, 0, n) cin >> as[i];

  ll low = 0, high = 1000000000, mid = (low + high) / 2;
  while (high - low >= 2) {
    ll curr_k = 0;
    REPD(i, 0, n) { curr_k += (as[i] - 1) / mid; }

    if (curr_k <= k) {
      high = mid;
    } else {
      low = mid;
    }
    mid = (low + high) / 2;
  }

  {
    ll curr_k = 0;
    REPD(i, 0, n) { curr_k += (as[i] - 1) / low; }
    if (curr_k <= k) {
      RET(low);
    }
  }

  RET(high);

  return 0;
}

// main function
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  solve();

  return 0;
}