Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

const int MOD = 1000000007;
#define p(var) std::cout << var << std::endl
#define pp(var) std::cout << #var " = " << var << std::endl
#define PI (acos(-1))
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repeq(i, n) for (int i = 1; i <= (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
using ll = long long;
template <typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }

/*-----8<-----8<-----*/

// Standard partition process of QuickSort().
// It considers the last element as pivot
// and moves all smaller element to left of
// it and greater elements to right
template <typename T> int partition(vector<T> &arr, int l, int r) {
  T x = arr[r];
  int i = l;
  for (int j = l; j <= r - 1; j++) {
    if (arr[j] >=
        x) { // if (arr[j] <= x) { // if (arr[j] >= x) { //降順にしたい時
      swap(arr[i], arr[j]);
      i++;
    }
  }
  swap(arr[i], arr[r]);
  return i;
}

// This function returns k'th smallest
// element in arr[l..r] using QuickSort
// based method.  ASSUMPTION: ALL ELEMENTS
// IN ARR[] ARE DISTINCT
template <typename T> T qselect(vector<T> &arr, int l, int r, int k) {
  // If k is smaller than number of
  // elements in array
  if (k > 0 && k <= r - l + 1) {

    // Partition the array around last
    // element and get position of pivot
    // element in sorted array
    int index = partition(arr, l, r);

    // If position is same as k
    if (index - l == k - 1)
      return arr[index];

    // If position is more, recur
    // for left subarray
    if (index - l > k - 1)
      return qselect(arr, l, index - 1, k);

    // Else recur for right subarray
    return qselect(arr, index + 1, r, k - index + l - 1);
  }

  // If k is more than number of
  // elements in array
  return INT_MAX;
}

// Driver program to test above methods
int main() {
  int X, Y, Z, K;
  cin >> X >> Y >> Z >> K;
  vector<ll> a(X, 0), b(Y, 0), c(Z, 0);
  rep(i, X) cin >> a[i];
  rep(i, Y) cin >> b[i];
  rep(i, Z) cin >> c[i];

  sort(all(a), greater<ll>());
  sort(all(b), greater<ll>());
  sort(all(c), greater<ll>());

  vector<ll> v;
  rep(i, X) {
    rep(j, Y) {
      rep(k, Z) {
        if ((i + 1) * (j + 1) * (k + 1) > K)
          break;
        ll tmp = a[i] + b[j] + c[k];
        // pp(tmp);
        v.pb(tmp);
      }
    }
  }

  ll border = qselect(v, 0, v.size() - 1, K);

  vector<ll> vv;
  int count = 0;
  int i = 0;
  while (count < K) {
    if (border <= v[i]) {
      count++;
      vv.pb(v[i]);
    }
    i++;
  }

  sort(all(vv), greater<ll>());
  rep(i, K) p(vv[i]);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int MOD = 1000000007;
#define p(var) std::cout << var << std::endl
#define pp(var) std::cout << #var " = " << var << std::endl
#define PI (acos(-1))
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repeq(i, n) for (int i = 1; i <= (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
using ll = long long;
template <typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }

/*-----8<-----8<-----*/

// Standard partition process of QuickSort().
// It considers the last element as pivot
// and moves all smaller element to left of
// it and greater elements to right
template <typename T> int partition(vector<T> &arr, int l, int r) {
  T x = arr[r];
  int i = l;
  for (int j = l; j <= r - 1; j++) {
    if (arr[j] > x) { // if (arr[j] < x) { // if (arr[j] > x) { //降順にしたい時
      swap(arr[i], arr[j]);
      i++;
    }
  }
  swap(arr[i], arr[r]);
  return i;
}

// This function returns k'th smallest
// element in arr[l..r] using QuickSort
// based method.  ASSUMPTION: ALL ELEMENTS
// IN ARR[] ARE DISTINCT
template <typename T> T qselect(vector<T> &arr, int l, int r, int k) {
  // If k is smaller than number of
  // elements in array
  if (k > 0 && k <= r - l + 1) {

    // Partition the array around last
    // element and get position of pivot
    // element in sorted array
    int index = partition(arr, l, r);

    // If position is same as k
    if (index - l == k - 1)
      return arr[index];

    // If position is more, recur
    // for left subarray
    if (index - l > k - 1)
      return qselect(arr, l, index - 1, k);

    // Else recur for right subarray
    return qselect(arr, index + 1, r, k - index + l - 1);
  }

  // If k is more than number of
  // elements in array
  return INT_MAX;
}

// Driver program to test above methods
int main() {
  int X, Y, Z, K;
  cin >> X >> Y >> Z >> K;
  vector<ll> a(X, 0), b(Y, 0), c(Z, 0);
  rep(i, X) cin >> a[i];
  rep(i, Y) cin >> b[i];
  rep(i, Z) cin >> c[i];

  sort(all(a), greater<ll>());
  sort(all(b), greater<ll>());
  sort(all(c), greater<ll>());

  vector<ll> v;
  rep(i, X) {
    rep(j, Y) {
      rep(k, Z) {
        if ((i + 1) * (j + 1) * (k + 1) > K)
          break;
        ll tmp = a[i] + b[j] + c[k];
        // pp(tmp);
        v.pb(tmp);
      }
    }
  }

  ll border = qselect(v, 0, v.size() - 1, K);

  vector<ll> vv;
  int count = 0;
  int i = 0;
  while (count < K) {
    if (border <= v[i]) {
      count++;
      vv.pb(v[i]);
    }
    i++;
  }

  sort(all(vv), greater<ll>());
  rep(i, K) p(vv[i]);

  return 0;
}