Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

// マクロ&定数&関数 ================================================
typedef unsigned int uint;
typedef long long ll;

typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<bool> vbool;
typedef vector<string> vstring;

typedef vector<pair<int, int>> vpint;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<double, double>> vpdouble;

typedef vector<vector<int>> vvint;
typedef vector<vector<ll>> vvll;
typedef vector<vpint> vvpint;
typedef vector<vpll> vvpll;
typedef vector<vector<double>> vvdouble;
typedef vector<vector<string>> vvstring;
typedef vector<vector<bool>> vvbool;

typedef vector<vector<vector<ll>>> vvvll;

const int INF = 1e9 + 1;
const ll LLINF = 1e17 + 1;
const int DX[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; // 4;４近傍
const int DY[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; // 8:８近傍 9:(0,0)を含む
const double PI = 3.14159265358979323846264338327950288;

// VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV

const ll MOD = 1000000007; // 10^9 + 7

// VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  } else
    return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  } else
    return false;
}

int main() {

  //////==================================
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(25);
  //////==================================

  /*
  ～思いついたことはとりあえず絶対メモする！！～



  */

  ll N, K;
  cin >> N >> K;

  vll A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  if (K == 0) {
    ll ans = 0;
    for (int i = 0; i < N; i++) {
      chmax(ans, A[i]);
    }
    cout << ans;
    return 0;
  }

  ll start = 0;
  ll last = LLINF;
  ll mid = (start + last) / 2;

  while (last - start > 1 && (start + last) / 2 > 0) {
    mid = (start + last) / 2;

    // mid以下にできるか？
    ll count = 0;

    for (int i = 0; i < N; i++) {
      count += (A[i] - 1) / mid;
    }

    if (count <= K) {
      last = mid;
    } else {
      start = mid;
    }
  }

  // mid以下にできるか？
  ll c = 0;
  if (mid > 0) {
    mid = start;
  }
  for (int i = 0; i < N; i++) {
    c += (A[i] - 1) / mid;
  }

  if (c <= K) {
    cout << mid << endl;
    ;
    return 0;
  } else {
    cout << mid + 1 << endl;
    return 0;
  }
}

Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

// マクロ&定数&関数 ================================================
typedef unsigned int uint;
typedef long long ll;

typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<bool> vbool;
typedef vector<string> vstring;

typedef vector<pair<int, int>> vpint;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<double, double>> vpdouble;

typedef vector<vector<int>> vvint;
typedef vector<vector<ll>> vvll;
typedef vector<vpint> vvpint;
typedef vector<vpll> vvpll;
typedef vector<vector<double>> vvdouble;
typedef vector<vector<string>> vvstring;
typedef vector<vector<bool>> vvbool;

typedef vector<vector<vector<ll>>> vvvll;

const int INF = 1e9 + 1;
const ll LLINF = 1e17 + 1;
const int DX[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; // 4;４近傍
const int DY[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; // 8:８近傍 9:(0,0)を含む
const double PI = 3.14159265358979323846264338327950288;

// VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV

const ll MOD = 1000000007; // 10^9 + 7

// VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  } else
    return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  } else
    return false;
}

int main() {

  //////==================================
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(25);
  //////==================================

  /*
  ～思いついたことはとりあえず絶対メモする！！～



  */

  ll N, K;
  cin >> N >> K;

  vll A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  if (K == 0) {
    ll ans = 0;
    for (int i = 0; i < N; i++) {
      chmax(ans, A[i]);
    }
    cout << ans;
    return 0;
  }

  ll start = 0;
  ll last = LLINF;
  ll mid = (start + last) / 2;

  while (last - start > 1 && (start + last) / 2 > 0) {
    mid = (start + last) / 2;

    // mid以下にできるか？
    ll count = 0;

    for (int i = 0; i < N; i++) {
      count += (A[i] - 1) / mid;
    }

    if (count <= K) {
      last = mid;
    } else {
      start = mid;
    }
  }

  // mid以下にできるか？
  ll c = 0;
  if (start > 0) {
    mid = start;
  }
  for (int i = 0; i < N; i++) {
    c += (A[i] - 1) / mid;
  }

  if (c <= K) {
    cout << mid << endl;
    ;
    return 0;
  } else {
    cout << mid + 1 << endl;
    return 0;
  }
}