Problem p03078 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
void ts() {
  cout << "***我只想过个水题抽奖****" << endl;
  cout << "***I JUST WANT PASS A WATER-PROBLEM FOR LUCK DRAW***" << endl;
}
#define ll long long
int x, y, z, m;
ll a[1005], b[1005], c[1005];
vector<ll> v;
void solve1() {
  scanf("%d%d%d%d", &x, &y, &z, &m);
  for (int i = 1; i <= x; i++)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= y; i++)
    scanf("%lld", &b[i]);
  for (int i = 1; i <= z; i++)
    scanf("%lld", &c[i]);
  sort(a + 1, a + 1 + x);
  sort(b + 1, b + 1 + y);
  sort(c + 1, c + 1 + z);
  reverse(a + 1, a + 1 + x);
  reverse(b + 1, b + 1 + y);
  reverse(c + 1, c + 1 + z);
  for (int i = 1; i <= min(200, x); i++)
    for (int j = 1; j <= min(200, y); j++)
      for (int k = 1; k <= min(200, z); k++)
        v.push_back(a[i] + b[j] + c[k]);
  for (int i = 201; i <= x; i++)
    for (int j = 1; j <= min(50, y); j++)
      for (int k = 1; k <= min(50, z); k++)
        v.push_back(a[i] + b[j] + c[k]);
  for (int i = 1; i <= min(50, x); i++)
    for (int j = 201; j <= y; j++)
      for (int k = 1; k <= min(50, z); k++)
        v.push_back(a[i] + b[j] + c[k]);
  for (int i = 1; i <= min(50, x); i++)
    for (int j = 1; j <= min(50, y); j++)
      for (int k = 201; k <= z; k++)
        v.push_back(a[i] + b[j] + c[k]);
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  for (int i = 0; i < m; i++)
    printf("%lld\n", v[i]);
}
bool judge(ll p) {
  int tot = 0;
  for (int i = 1; i <= x; i++)
    for (int j = 1; j <= y; j++) {
      if (a[i] + b[j] + c[1] < p)
        break;
      for (int k = 1; k <= z; k++) {
        if (a[i] + b[j] + c[k] < p)
          break;
        tot++;
        if (tot >= m)
          return true;
      }
    }
  return false;
}
void solve2() {
  scanf("%d%d%d%d", &x, &y, &z, &m);
  for (int i = 1; i <= x; i++)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= y; i++)
    scanf("%lld", &b[i]);
  for (int i = 1; i <= z; i++)
    scanf("%lld", &c[i]);
  sort(a + 1, a + 1 + x);
  sort(b + 1, b + 1 + y);
  sort(c + 1, c + 1 + z);
  reverse(a + 1, a + 1 + x);
  reverse(b + 1, b + 1 + y);
  reverse(c + 1, c + 1 + z);
  ll l = 0, r = 3e10, ans;
  while (r > 0) {
    ll m = l + r >> 1;
    if (judge(m))
      ans = m, l = m + 1;
    else
      r = m - 1;
  }
  for (int i = 1; i <= x; i++)
    for (int j = 1; j <= y; j++) {
      if (a[i] + b[j] + c[1] < ans)
        break;
      for (int k = 1; k <= z; k++) {
        if (a[i] + b[j] + c[k] < ans)
          break;
        v.push_back(a[i] + b[j] + c[k]);
      }
    }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  for (int i = 0; i < m; i++)
    printf("%lld\n", v[i]);
}
int main() {
  // solve1();
  solve2();
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
void ts() {
  cout << "***我只想过个水题抽奖****" << endl;
  cout << "***I JUST WANT PASS A WATER-PROBLEM FOR LUCK DRAW***" << endl;
}
#define ll long long
int x, y, z, m;
ll a[1005], b[1005], c[1005];
vector<ll> v;
void solve1() {
  scanf("%d%d%d%d", &x, &y, &z, &m);
  for (int i = 1; i <= x; i++)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= y; i++)
    scanf("%lld", &b[i]);
  for (int i = 1; i <= z; i++)
    scanf("%lld", &c[i]);
  sort(a + 1, a + 1 + x);
  sort(b + 1, b + 1 + y);
  sort(c + 1, c + 1 + z);
  reverse(a + 1, a + 1 + x);
  reverse(b + 1, b + 1 + y);
  reverse(c + 1, c + 1 + z);
  for (int i = 1; i <= min(200, x); i++)
    for (int j = 1; j <= min(200, y); j++)
      for (int k = 1; k <= min(200, z); k++)
        v.push_back(a[i] + b[j] + c[k]);
  for (int i = 201; i <= x; i++)
    for (int j = 1; j <= min(50, y); j++)
      for (int k = 1; k <= min(50, z); k++)
        v.push_back(a[i] + b[j] + c[k]);
  for (int i = 1; i <= min(50, x); i++)
    for (int j = 201; j <= y; j++)
      for (int k = 1; k <= min(50, z); k++)
        v.push_back(a[i] + b[j] + c[k]);
  for (int i = 1; i <= min(50, x); i++)
    for (int j = 1; j <= min(50, y); j++)
      for (int k = 201; k <= z; k++)
        v.push_back(a[i] + b[j] + c[k]);
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  for (int i = 0; i < m; i++)
    printf("%lld\n", v[i]);
}
bool judge(ll p) {
  int tot = 0;
  for (int i = 1; i <= x; i++)
    for (int j = 1; j <= y; j++) {
      if (a[i] + b[j] + c[1] < p)
        break;
      for (int k = 1; k <= z; k++) {
        if (a[i] + b[j] + c[k] < p)
          break;
        tot++;
        if (tot >= m)
          return true;
      }
    }
  return false;
}
void solve2() {
  scanf("%d%d%d%d", &x, &y, &z, &m);
  for (int i = 1; i <= x; i++)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= y; i++)
    scanf("%lld", &b[i]);
  for (int i = 1; i <= z; i++)
    scanf("%lld", &c[i]);
  sort(a + 1, a + 1 + x);
  sort(b + 1, b + 1 + y);
  sort(c + 1, c + 1 + z);
  reverse(a + 1, a + 1 + x);
  reverse(b + 1, b + 1 + y);
  reverse(c + 1, c + 1 + z);
  ll l = 0, r = 3e10, ans;
  while (l <= r) {
    ll m = l + r >> 1;
    if (judge(m))
      ans = m, l = m + 1;
    else
      r = m - 1;
  }
  for (int i = 1; i <= x; i++)
    for (int j = 1; j <= y; j++) {
      if (a[i] + b[j] + c[1] < ans)
        break;
      for (int k = 1; k <= z; k++) {
        if (a[i] + b[j] + c[k] < ans)
          break;
        v.push_back(a[i] + b[j] + c[k]);
      }
    }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  for (int i = 0; i < m; i++)
    printf("%lld\n", v[i]);
}
int main() {
  // solve1();
  solve2();
}