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

#include <bits/stdc++.h>

#define ll long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<long long, long long>

#define fastio()                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl "\\n"

#define Debug(x) cout << (#x) << " >> " << (x) << endl

using namespace std;

ll fastpow(ll angka, ll pangkat, ll MOD) {
  ll res = 1;
  while (pangkat) {
    if (pangkat & 1)
      res = (res * angka) % MOD;
    angka = (angka * angka) % MOD;
    pangkat = pangkat >> 1;
  }
  return res;
}

ll fastpow(ll angka, ll pangkat) {
  ll res = 1;
  while (pangkat) {
    if (pangkat & 1)
      res = (res * angka);
    angka = (angka * angka);
    pangkat = pangkat >> 1;
  }
  return res;
}

ll gcd(ll angka1, ll angka2) {
  while (angka2) {
    swap(angka1, angka2);
    angka2 = angka2 % angka1;
  }
  return angka1;
}

ll lcm(ll angka1, ll angka2) { return (angka1 / gcd(angka1, angka2)) * angka2; }

int f(int len, int x) { return (len - 1) / x; }

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

  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];

  int l, r;
  l = 1;
  r = 1;
  for (int i = 0; i < n; i++)
    r = max(r, v[i]);

  int res = r;
  int m;
  while (l <= r) {
    //		cout<<l<<" "<<r<<endl;
    m = (l + r) / 2;

    ll ct = 0;
    for (int i = 0; i < n; i++) {
      ct += f(v[i], m);
    }

    if (ct <= k) {
      res = min(res, m);
      r = m - 1;
    } else
      l = m + 1;
  }
  cout << res << endl;
}

int main() {

  //	fastio();

  int t = 1;
  //	cin>>t;

  for (int tt = 0; tt < t; tt++) {
    //		cout<<"Case #"<<tt+1<<": ";solve();
    //		if(solve()) cout<<""<<endl;
    solve();
  }

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

#define ll long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<long long, long long>

#define fastio()                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl "\\n"

#define Debug(x) cout << (#x) << " >> " << (x) << endl

using namespace std;

ll fastpow(ll angka, ll pangkat, ll MOD) {
  ll res = 1;
  while (pangkat) {
    if (pangkat & 1)
      res = (res * angka) % MOD;
    angka = (angka * angka) % MOD;
    pangkat = pangkat >> 1;
  }
  return res;
}

ll fastpow(ll angka, ll pangkat) {
  ll res = 1;
  while (pangkat) {
    if (pangkat & 1)
      res = (res * angka);
    angka = (angka * angka);
    pangkat = pangkat >> 1;
  }
  return res;
}

ll gcd(ll angka1, ll angka2) {
  while (angka2) {
    swap(angka1, angka2);
    angka2 = angka2 % angka1;
  }
  return angka1;
}

ll lcm(ll angka1, ll angka2) { return (angka1 / gcd(angka1, angka2)) * angka2; }

int f(int len, int x) { return (len - 1) / x; }

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

  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];

  int l, r;
  l = 1;
  r = 1;
  for (int i = 0; i < r; i++)
    r = max(r, v[i]);

  int res = r;
  int m;
  while (l <= r) {
    m = (l + r) / 2;

    ll ct = 0;
    for (int i = 0; i < n; i++) {
      ct += f(v[i], m);
    }

    if (ct <= k) {
      res = min(res, m);
      r = m - 1;
    } else
      l = m + 1;
  }
  cout << res << endl;
}

int main() {

  fastio();

  int t = 1;
  //	cin>>t;

  for (int tt = 0; tt < t; tt++) {
    //		cout<<"Case #"<<tt+1<<": ";solve();
    //		if(solve()) cout<<""<<endl;
    solve();
  }

  return 0;
}