Problem p03078 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

typedef long long ll;
int X, Y, Z, K;
std::vector<ll> A, B, C;

bool judge(ll P) {
  ll rtn = 0;
  for (int i = 0; i < X; i++) {
    for (int j = 0; j < Y; j++) {
      for (int k = 0; k < Z; k++) {
        if (A[i] + B[j] + C[k] < P)
          break;
        rtn++;
        if (rtn > K)
          return true;
      }
    }
  }
  return false;
}

ll binary_search(ll P1, ll P2) {
  if (P1 == P2)
    return P1;
  ll center = std::floor((double)(P1 + P2) / 2);
  if (judge(center))
    return binary_search(P1, center + 1);
  else
    return binary_search(center, P2);
}

void sol_4() {
  ll boarder =
      binary_search(A[0] + B[0] + C[0], A[X - 1] + B[Y - 1] + C[Z - 1]);

  std::vector<ll> ABCsum;
  for (int i = 0; i < X; i++) {
    for (int j = 0; j < Y; j++) {
      for (int k = 0; k < Z; k++) {
        if (A[i] + B[j] + C[k] < boarder)
          break;
        ABCsum.push_back(A[i] + B[j] + C[k]);
      }
    }
  }

  std::sort(ABCsum.begin(), ABCsum.end(), std::greater<ll>());
  for (int i = 0; i < K; i++)
    std::cout << ABCsum.at(i) << std::endl;
}

int main(void) {
  std::cin >> X >> Y >> Z >> K;
  for (int i = 0; i < X; i++) {
    ll t_;
    std::cin >> t_;
    A.push_back(t_);
  }
  for (int i = 0; i < Y; i++) {
    ll t_;
    std::cin >> t_;
    B.push_back(t_);
  }
  for (int i = 0; i < Z; i++) {
    ll t_;
    std::cin >> t_;
    C.push_back(t_);
  }

  std::sort(A.begin(), A.end(), std::greater<ll>());
  std::sort(B.begin(), B.end(), std::greater<ll>());
  std::sort(C.begin(), C.end(), std::greater<ll>());
  sol_4();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

typedef long long ll;
int X, Y, Z, K;
std::vector<ll> A, B, C;

bool judge(ll P) {
  ll rtn = 0;
  for (int i = 0; i < X; i++) {
    for (int j = 0; j < Y; j++) {
      for (int k = 0; k < Z; k++) {
        if (A[i] + B[j] + C[k] < P)
          break;
        rtn++;
        if (rtn > K)
          return true;
      }
    }
  }
  return false;
}

ll binary_search(ll P1, ll P2) {
  if (P1 == P2)
    return P1;
  ll center = std::floor((double)(P1 + P2) / 2);
  if (judge(center))
    return binary_search(P1, center + 1);
  else
    return binary_search(center, P2);
}

void sol_4() {
  ll boarder =
      binary_search(A[0] + B[0] + C[0], A[X - 1] + B[Y - 1] + C[Z - 1]);

  std::vector<ll> ABCsum;
  for (int i = 0; i < X; i++) {
    for (int j = 0; j < Y; j++) {
      for (int k = 0; k < Z; k++) {
        if (A[i] + B[j] + C[k] < boarder - 1)
          break;
        ABCsum.push_back(A[i] + B[j] + C[k]);
      }
    }
  }

  std::sort(ABCsum.begin(), ABCsum.end(), std::greater<ll>());
  for (int i = 0; i < K; i++)
    std::cout << ABCsum.at(i) << std::endl;
}

int main(void) {
  std::cin >> X >> Y >> Z >> K;
  for (int i = 0; i < X; i++) {
    ll t_;
    std::cin >> t_;
    A.push_back(t_);
  }
  for (int i = 0; i < Y; i++) {
    ll t_;
    std::cin >> t_;
    B.push_back(t_);
  }
  for (int i = 0; i < Z; i++) {
    ll t_;
    std::cin >> t_;
    C.push_back(t_);
  }

  std::sort(A.begin(), A.end(), std::greater<ll>());
  std::sort(B.begin(), B.end(), std::greater<ll>());
  std::sort(C.begin(), C.end(), std::greater<ll>());
  sol_4();

  return 0;
}