Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define PI 3.1415926535897932384626433832795

template <typename X> bool max_u(X &m, X v) {
  if (m < v) {
    m = v;
    return true;
  }
  return false;
}

template <typename X> bool min_u(X &m, X v) {
  if (m > v) {
    m = v;
    return true;
  }
  return false;
}

template <typename X> X ceil(X v, X d) { return (v + d - 1) / d; }

struct l_t {
  int id;
  ll a;
  ll l;
  ll d;
};

struct solve {
  solve() {}
  ll operator()(int n, ll k, vll &a) {
    ll a_sum = 0, a_max = 0;
    for (auto &m : a) {
      a_sum += m;
      max_u(a_max, m);
    }
    ll a_min = a_sum / (n + k);

    ll s = a_min;
    ll e = a_max;
    ll ans = e;
    while (s <= e) {
      ll c = (s + e) / 2;
      ll cut_sum = 0;
      for (auto m : a) {
        ll cut = (m - 1) / c;
        cut_sum += cut;
      }
      // cout << "s=" << s << " e=" << e << " c=" << c << " cut_sum=" << cut_sum
      // << "\n";

      if (cut_sum <= k) {
        e = c - 1;
        min_u(ans, c);
      } else {
        s = c + 1;
      }
    }
    return ans;
  }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  for (;;) {
    int n;
    ll k;
    cin >> n >> k;
    if (cin.fail())
      break;
    vll a(n);
    for (auto &m : a)
      cin >> m;

    cout << solve()(n, k, a) << "\n";
    cout.flush();
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define PI 3.1415926535897932384626433832795

template <typename X> bool max_u(X &m, X v) {
  if (m < v) {
    m = v;
    return true;
  }
  return false;
}

template <typename X> bool min_u(X &m, X v) {
  if (m > v) {
    m = v;
    return true;
  }
  return false;
}

template <typename X> X ceil(X v, X d) { return (v + d - 1) / d; }

struct l_t {
  int id;
  ll a;
  ll l;
  ll d;
};

struct solve {
  solve() {}
  ll operator()(int n, ll k, vll &a) {
    ll a_sum = 0, a_max = 0;
    for (auto &m : a) {
      a_sum += m;
      max_u(a_max, m);
    }
    ll a_min = a_sum / (n + k);

    ll s = max(1LL, a_min);
    ll e = a_max;
    ll ans = e;
    while (s <= e) {
      ll c = (s + e) / 2;
      ll cut_sum = 0;
      for (auto m : a) {
        ll cut = (m - 1) / c;
        cut_sum += cut;
      }
      // cout << "s=" << s << " e=" << e << " c=" << c << " cut_sum=" << cut_sum
      // << "\n";

      if (cut_sum <= k) {
        e = c - 1;
        min_u(ans, c);
      } else {
        s = c + 1;
      }
    }
    return ans;
  }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  for (;;) {
    int n;
    ll k;
    cin >> n >> k;
    if (cin.fail())
      break;
    vll a(n);
    for (auto &m : a)
      cin >> m;

    cout << solve()(n, k, a) << "\n";
    cout.flush();
  }
  return 0;
}