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

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define pr pair<ll, ll>
#define vct vector<ll>
#define vpt vector<pr>
#define st set<ll>
#define pq priority_queue<ll>
#define mat vector<vector<ll>>
#define F(i, n) for (i = 0; i < (1LL * n); i++)
#define Fr(i, n) for (i = n - 1; i >= 0; i--)

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
#define umap unordered_map<long long, long long, custom_hash>
void prv(vct v) {
  for (ll i = 0; i < v.size(); i++)
    cout << v[i] << " ";
  cout << "\\n";
}
ll poww(ll x, ll y, ll m) {
  ll res = 1;
  while (y) {
    if (y % 2 == 0) {
      x = (x * x) % m;
      y = y / 2;
    } else {
      res = (res * x) % m;
      y--;
    }
  }
  return res;
}
void deb(string s, ll n) { cout << s << " : " << n << "\\n"; }
void prn(ll n) { cout << n << "\\n"; }
void prs(string n) { cout << n << "\\n"; }

mat mt;
void setmtsize(ll m, ll n, ll ini = 0) { mt.resize(m, vector<ll>(n, ini)); }
void prmt(mat m) {
  for (ll i = 0; i < (1LL * m.size()); i++)
    prv(m[i]);
}
vct divsors(ll n) {
  vct ans; // ans.push_back(1);
  for (ll i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      ans.push_back(i);
      if (i != (n / i))
        ans.push_back(n / i);
    }
  }
  sort(ans.begin(), ans.end());
  return ans;
}
vct sieve(ll n) {
  vct ans(n + 1, -1);
  for (ll i = 2; i <= n; i++) {
    if (ans[i] == -1) {
      for (ll j = i; j <= n; j += i)
        if (ans[j] == -1)
          ans[j] = i;
    }
  }
  return ans;
}
ll n, k, m, l, r, x, y, z, ans = 0;
ll t, t1, t2;
// ll q;
ll i, j;
ll a, b, c;
ll mod = 1000000007;
ll cas = 0;

ll chk(ll md, vct v, ll cuts) {
  ll an = -1;
  ll mxn = -1;
  for (ll i = 0; i < v.size(); i++) {
    ll cr = v[i] - 1;
    ll r1 = cr / md;
    ll rm = cr % md;
    if (r1 > 0) {
      if (cuts < r1)
        return -1;
      cuts -= r1;
      an = md;
    } else {
      an = max(an, rm);
    }
  }
  return an;
}

void solve() {
  cin >> n >> k;
  vct v(n);
  r = -1;
  F(i, n) {
    cin >> v[i];
    r = max(r, v[i]);
  }
  if (k == 0) {
    prn(r);
    return;
  }
  l = 1;
  ans = -1;
  ll prmx = LLONG_MAX;
  while (l <= r) {
    ll md = l + (r - l) / 2;
    ll an = chk(md, v, k);
    if (an == -1) {
      l = md + 1;
      continue;
    } else {
      ans = md;
      r = md - 1;
    }
  }
  prn(ans);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  ll t = 1;
  // cin >> t;
  cas = 1;
  // stress();
  while (t--) {
    solve();
    cas++;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define pr pair<ll, ll>
#define vct vector<ll>
#define vpt vector<pr>
#define st set<ll>
#define pq priority_queue<ll>
#define mat vector<vector<ll>>
#define F(i, n) for (i = 0; i < (1LL * n); i++)
#define Fr(i, n) for (i = n - 1; i >= 0; i--)

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
#define umap unordered_map<long long, long long, custom_hash>
void prv(vct v) {
  for (ll i = 0; i < v.size(); i++)
    cout << v[i] << " ";
  cout << "\\n";
}
ll poww(ll x, ll y, ll m) {
  ll res = 1;
  while (y) {
    if (y % 2 == 0) {
      x = (x * x) % m;
      y = y / 2;
    } else {
      res = (res * x) % m;
      y--;
    }
  }
  return res;
}
void deb(string s, ll n) { cout << s << " : " << n << "\\n"; }
void prn(ll n) { cout << n << "\\n"; }
void prs(string n) { cout << n << "\\n"; }

mat mt;
void setmtsize(ll m, ll n, ll ini = 0) { mt.resize(m, vector<ll>(n, ini)); }
void prmt(mat m) {
  for (ll i = 0; i < (1LL * m.size()); i++)
    prv(m[i]);
}
vct divsors(ll n) {
  vct ans; // ans.push_back(1);
  for (ll i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      ans.push_back(i);
      if (i != (n / i))
        ans.push_back(n / i);
    }
  }
  sort(ans.begin(), ans.end());
  return ans;
}
vct sieve(ll n) {
  vct ans(n + 1, -1);
  for (ll i = 2; i <= n; i++) {
    if (ans[i] == -1) {
      for (ll j = i; j <= n; j += i)
        if (ans[j] == -1)
          ans[j] = i;
    }
  }
  return ans;
}
ll n, k, m, l, r, x, y, z, ans = 0;
ll t, t1, t2;
// ll q;
ll i, j;
ll a, b, c;
ll mod = 1000000007;
ll cas = 0;

ll chk(ll md, vct v, ll cuts) {
  ll an = -1;
  ll mxn = -1;
  for (ll i = 0; i < v.size(); i++) {
    ll cr = v[i];
    ll r1 = cr / md;
    ll rm = cr % md;
    if (r1 > 0) {
      if (cuts < r1)
        return -1;
      cuts -= r1;
      an = md;
    } else {
      an = max(an, rm);
    }
  }
  return an;
}

void solve() {
  cin >> n >> k;
  vct v(n);
  r = -1;
  F(i, n) {
    cin >> v[i];
    r = max(r, v[i]);
  }
  if (k == 0) {
    prn(r);
    return;
  }
  l = 0;
  ans = 0;
  ll prmx = LLONG_MAX;
  while (l <= r) {
    ll md = l + (r - l) / 2;
    ll an = chk(md, v, k);
    if (an == -1) {
      l = md + 1;
      continue;
    }
    if (an < md) {
      r = md - 1;
    } else {
      an = chk(md - 1, v, k);
      if (an == -1) {
        prn(md);
        return;
      } else
        r = md - 1;
    }
  }
  // if(chk(l,v,k)==l)prn(l);
  // else{
  //     if(chk(l,v,k)==(l-1))prn(l-1);
  //     else prn(prmx);
  // }
  prn(-1);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  ll t = 1;
  // cin >> t;
  cas = 1;
  // stress();
  while (t--) {
    solve();
    cas++;
  }
  return 0;
}