#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
bool check(ll S, const vector<ll>& a, int N, int K, int M) {
int total = a.size();
int left = 0, right = total - 1;
int skips = 0;
int pairs = 0;
while (left < right && pairs < M && skips <= K) {
if (a[left] + a[right] == S) {
pairs++;
left++;
right--;
} else if (a[left] + a[right] < S) {
skips++;
left++;
} else {
skips++;
right--;
}
}
if (skips > K) return false;
if (pairs < M) {
if (left == right) {
skips++;
}
if (skips > K) return false;
return pairs == M;
}
skips += (right - left + 1);
return skips == K;
}
vector<ll> build_solution(ll S, const vector<ll>& a, int N, int K) {
int total = a.size();
int left = 0, right = total - 1;
vector<ll> taken;
while (left <= right && taken.size() < N) {
if (a[left] + a[right] == S) {
taken.push_back(a[left]);
taken.push_back(a[right]);
left++;
right--;
} else if (a[left] + a[right] < S) {
left++;
} else {
right--;
}
}
sort(taken.begin(), taken.end());
return taken;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N, K;
cin >> N >> K;
int total = N + K;
vector<ll> a(total);
for (int i = 0; i < total; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
int M = N / 2;
set<ll> candidates;
for (int i = 0; i <= K; i++) {
for (int j = total - 1; j >= total - 1 - K; j--) {
candidates.insert(a[i] + a[j]);
}
}
for (ll S : candidates) {
if (check(S, a, N, K, M)) {
vector<ll> sol = build_solution(S, a, N, K);
for (size_t i = 0; i < sol.size(); i++) {
cout << sol[i];
if (i < sol.size() - 1) cout << " ";
}
cout << endl;
return 0;
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |