Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <string>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;

const ll MOD = 1000000007ll;
const int MAX = 5100000;

using namespace std;

int inputValue() {
  int a;
  cin >> a;
  return a;
};

void inputArray(int *p, int a) {
  for (int i = 0; i < a; i++) {
    cin >> p[i];
  }
};

void inputVector(vector<int> *p, int a) {
  for (int i = 0; i < a; i++) {
    int input;
    cin >> input;
    p->push_back(input);
  }
}

ll fact[MAX], fact_inv[MAX];

ll power(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

// nCr
ll comb(ll n, ll r) {
  ll t = 1000000;
  fact[0] = 1;
  for (int i = 0; i < t; i++) {
    fact[i + 1] = fact[i] * (i + 1) % MOD;
  }
  fact_inv[t] = power(fact[t], MOD - 2);
  for (int i = 0; i < t; i++) {
    fact_inv[i] = fact_inv[i + 1] * (i + 1) % MOD;
  }
  return (fact[n] * fact_inv[r]) % MOD * fact_inv[n - r] % MOD;
}

int i, j, k, tmp;
ll ans = 0;
ll A[200000];

bool solve(int N, ll K, ll ans) {
  ll tmp = 0;
  for (i = 0; i < N; i++) {
    if (A[i] <= ans) {
      continue;
    }
    tmp += (A[i] + ans - 1) / ans - 1;
    if (tmp > K) {
      return false;
    }
  }
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  ll K;
  cin >> K;
  ll max = 0;
  for (i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] > max) {
      max = A[i];
    }
  }
  if (K == 0) {
    cout << max << endl;
    return 0;
  }

  ll left = -1, right = max;
  while (right - left > 1) {
    ll mid = left + (right - left) / 2;
    if (solve(N, K, mid)) {
      right = mid;
    } else {
      left = mid;
    }
  }
  cout << right << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <string>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;

const ll MOD = 1000000007ll;
const int MAX = 5100000;

using namespace std;

int inputValue() {
  int a;
  cin >> a;
  return a;
};

void inputArray(int *p, int a) {
  for (int i = 0; i < a; i++) {
    cin >> p[i];
  }
};

void inputVector(vector<int> *p, int a) {
  for (int i = 0; i < a; i++) {
    int input;
    cin >> input;
    p->push_back(input);
  }
}

ll fact[MAX], fact_inv[MAX];

ll power(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

// nCr
ll comb(ll n, ll r) {
  ll t = 1000000;
  fact[0] = 1;
  for (int i = 0; i < t; i++) {
    fact[i + 1] = fact[i] * (i + 1) % MOD;
  }
  fact_inv[t] = power(fact[t], MOD - 2);
  for (int i = 0; i < t; i++) {
    fact_inv[i] = fact_inv[i + 1] * (i + 1) % MOD;
  }
  return (fact[n] * fact_inv[r]) % MOD * fact_inv[n - r] % MOD;
}

int i, j, k, tmp;
ll ans = 0;
ll A[200000];

bool solve(int N, ll K, ll ans) {
  ll tmp = 0;
  for (i = 0; i < N; i++) {
    if (A[i] <= ans) {
      continue;
    }
    tmp += (A[i] + ans - 1) / ans - 1;
    if (tmp > K) {
      return false;
    }
  }
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  ll K;
  cin >> K;
  ll max = 0;
  for (i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] > max) {
      max = A[i];
    }
  }
  if (K == 0) {
    cout << max << endl;
    return 0;
  }

  ll left = 0, right = max;
  while (right - left > 1) {
    ll mid = left + (right - left) / 2;
    if (solve(N, K, mid)) {
      right = mid;
    } else {
      left = mid;
    }
  }
  cout << right << endl;

  return 0;
}