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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define rep2(i, a) for (auto i : a)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x, std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
ll power(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x *= x;
      p /= 2;
    } else {
      a *= x;
      p--;
    }
  }
  return a;
}
ll mpower(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x = x * x % mod;
      p /= 2;
    } else {
      a = a * x % mod;
      p--;
    }
  }
  return a;
}
ll co(ll n, ll k) {
  ll a = 1;
  rep(i, 1ll, k) {
    a *= n - i + 1;
    a /= i;
  }
  return a;
}
ll mc(ll n, ll m) {
  ll k = 1, l = 1;
  rep(i, n - m + 1, n + 1) k = k * i % mod;
  rep(i, 1, m + 1) l = l * i % mod;
  l = mpower(l, mod - 2);
  return k * l % mod;
}
vector<int> a;
int n;
bool isOK(int m, int key) {
  int sum = 0;
  rep(i, 0, n) { sum += (a[i] - 1) / m; }
  return sum <= key;
}
int my_binary_search(int key) {
  int left = 0;
  int right = 0;
  rep(i, 0, n) right = max(right, a[i]);

  /* どんな二分探索でもここの書き方を変えずにできる！ */
  while (right - left > 1) {
    int mid = left + (right - left) / 2;

    if (isOK(mid, key))
      right = mid;
    else
      left = mid;
  }

  /* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている
   */
  return right;
}
int main(void) {
  int k;
  cin >> n >> k;
  a.resize(n);
  rep(i, 0, n) cin >> a[i];
  printf("%d\\n", my_binary_search(k));
  return 0;
}


Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define rep2(i, a) for (auto i : a)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x, std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
ll power(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x *= x;
      p /= 2;
    } else {
      a *= x;
      p--;
    }
  }
  return a;
}
ll mpower(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x = x * x % mod;
      p /= 2;
    } else {
      a = a * x % mod;
      p--;
    }
  }
  return a;
}
ll co(ll n, ll k) {
  ll a = 1;
  rep(i, 1, k) {
    a *= n - i + 1;
    a /= i;
  }
  return a;
}
ll mc(ll n, ll m) {
  ll k = 1, l = 1;
  rep(i, n - m + 1, n + 1) k = k * i % mod;
  rep(i, 1, m + 1) l = l * i % mod;
  l = mpower(l, mod - 2);
  return k * l % mod;
}

int main() {
  int n, k, m = INF;
  ll sum = 0;
  cin >> n >> k;
  priority_queue<pair<double, int>> a;
  vector<int> l(n);
  rep(i, 0, n) {
    cin >> l[i];
    sum += l[i];
  }
  int ave = sum / (n + k);
  rep(i, 0, n) {
    int z = max(l[i] / ave / 2, 1);

    a.push(make_pair(l[i] / z, z));
  }
  rep(i, 0, k) {
    auto it = a.top();
    a.pop();
    it.first = it.first * it.second / (it.second + 1);
    it.second++;
    a.push(it);
  }
  printf("%d\\n", (int)ceil(a.top().first));
  return 0;
}