Problem p02598 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
using ll = long long;

template <typename TYPE> void print_vec(const vector<TYPE> &v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << endl;
}

template <typename TYPE> void print_vec2(const vector<vector<TYPE>> &v) {
  cout << endl;
  cout << "       ";
  for (int i = 0; i < v[0].size(); i++)
    cout << i << "   ";
  cout << endl;
  for (int i = 0; i < v.size(); i++) {
    cout << "i=" << i << ":   ";
    for (int j = 0; j < v[i].size(); j++) {
      if (v[i][j] == 0)
        cout << "\x1B[0m" << v[i][j] << "   ";
      else
        cout << "\x1B[31m" << v[i][j]
             << "   "; // https://stackoverrun.com/ja/q/12618775
    }
    cout << "\x1B[0m" << endl;
  }
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  sort(A.begin(), A.end());

  // 2分探索
  ll left = 0, right = 1e9 + 1;
  while (right - left > 1) {
    vector<ll> B = A;
    ll mid = (left + right) / 2;
    ll num_cut = 0;
    for (int i = 0; i < N; i++) {
      while (B[i] > mid) {
        B[i] = B[i] - mid; // B[i]をmidとA[i]-midに切り分ける
        num_cut++;
        if (num_cut > K)
          break;
      }
      if (num_cut > K)
        break;
    }

    if (num_cut > K)
      left = mid;
    else
      right = mid;
    // cout << " left: " << left << "  mid: " << mid << " right: " << right << "
    // num_cut: " << num_cut << endl; cout << " B: "; print_vec(B);
  }

  cout << right << endl;

  // sort(A.begin(), A.end());
  // for(int i=0; i<K; i++){
  // 	ll tmp = A.back();
  // 	A.pop_back();
  // 	if(tmp % 2 == 1){
  // 		A.push_back(tmp/2);
  // 		A.push_back(tmp/2+1);
  // 	}
  // 	else{
  // 		A.push_back(tmp/2);
  // 		A.push_back(tmp/2);
  // 	}
  // 	sort(A.begin(), A.end());

  // }

  // cout << A.back() << endl;

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;

template <typename TYPE> void print_vec(const vector<TYPE> &v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << endl;
}

template <typename TYPE> void print_vec2(const vector<vector<TYPE>> &v) {
  cout << endl;
  cout << "       ";
  for (int i = 0; i < v[0].size(); i++)
    cout << i << "   ";
  cout << endl;
  for (int i = 0; i < v.size(); i++) {
    cout << "i=" << i << ":   ";
    for (int j = 0; j < v[i].size(); j++) {
      if (v[i][j] == 0)
        cout << "\x1B[0m" << v[i][j] << "   ";
      else
        cout << "\x1B[31m" << v[i][j]
             << "   "; // https://stackoverrun.com/ja/q/12618775
    }
    cout << "\x1B[0m" << endl;
  }
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  sort(A.begin(), A.end());

  // 2分探索
  ll left = 0, right = 1e9 + 1;
  while (right - left > 1) {
    vector<ll> B = A;
    ll mid = (left + right) / 2;
    ll num_cut = 0;
    for (int i = 0; i < N; i++) {
      // while(B[i] > mid){
      // 	//B[i] = B[i] - mid;//B[i]をmidとA[i]-midに切り分ける
      // 	//num_cut++;
      // 	num_cut += B[i] / mid;
      // 	if(num_cut>K) break;
      // }
      // num_cut += (B[i]+mid-1) / mid;
      if (B[i] > mid)
        num_cut += B[i] / mid;
      // num_cut += B[i] / mid - 1;
      if (num_cut > K)
        break;
    }

    if (num_cut > K)
      left = mid;
    else
      right = mid;
    // cout << " left: " << left << "  mid: " << mid << " right: " << right << "
    // num_cut: " << num_cut << endl; cout << " B: "; print_vec(B);
  }

  cout << right << endl;

  // sort(A.begin(), A.end());
  // for(int i=0; i<K; i++){
  // 	ll tmp = A.back();
  // 	A.pop_back();
  // 	if(tmp % 2 == 1){
  // 		A.push_back(tmp/2);
  // 		A.push_back(tmp/2+1);
  // 	}
  // 	else{
  // 		A.push_back(tmp/2);
  // 		A.push_back(tmp/2);
  // 	}
  // 	sort(A.begin(), A.end());

  // }

  // cout << A.back() << endl;

  return 0;
}