-
Notifications
You must be signed in to change notification settings - Fork 0
/
10487.cpp
50 lines (39 loc) · 1.12 KB
/
10487.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ll n;
ll cs = 0;
while(cin >> n && n){
cout << "Case " << ++cs << ":\n";
ll arr[n];
for(ll i = 0; i < n; i++){
cin >> arr[i];
}
sort(arr, arr+n);
ll m; cin >> m;
while(m--){
ll temp; cin >> temp;
ll res = 0;
ll bs = LLONG_MAX;
// ll lb = upper_bound(arr, arr+n, temp) - arr;
// ll pt = min(n-1, lb);
for(ll i = 0; i < n; i++){
for(ll j = i+1; j < n; j++){
ll ts = abs(arr[i]+arr[j]-temp);
// cout << arr[i] << " " << arr[j] << "\n";
if(ts < bs){
bs = ts;
res = arr[i]+arr[j];
}
}
}
cout << "Closest sum to " << temp << " is " << res << ".\n";
}
}
return 0;
}