PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#TP0000032. Tổng gần nhất (Closest Sums)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng gần nhất (Closest Sums)

Closest Sums

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

You are given nn integers and mm queries. For each query value qq, choose two elements at distinct positions whose sum is closest to qq, minimizing ∣ai+aj−q∣|a_i+a_j-q|. Values may repeat, but the same position cannot be used twice.

For each query, the input guarantees that no two distinct sum values are tied for the minimum distance. Answer every query using the original array.

Input

  • The first line contains nn, the number of integers.
  • Each of the next nn lines contains exactly one integer aia_i.
  • The following line contains mm, the number of queries.
  • Each of the final mm lines contains exactly one query value qjq_j.

Output

Print Case 1: on the first line. For each query qq in input order, print Closest sum to q is s., where ss is the sum of two elements at distinct positions closest to qq. Preserve capitalization, spaces, and the final period.

Subtasks

  • Subtask 1 (20 points): 2≤n≤82\le n\le 8; 1≤m≤31\le m\le 3; −106≤ai≤106-10^6\le a_i\le10^6 for each array value; −106≤qj≤106-10^6\le q_j\le10^6 for every query; no distinct achievable sums tie in distance for any query.
  • Subtask 2 (30 points): 2≤n≤1202\le n\le 120; 1≤m≤101\le m\le 10; −106≤ai≤106-10^6\le a_i\le10^6 for each array value; −106≤qj≤106-10^6\le q_j\le10^6 for every query; no distinct achievable sums tie in distance for any query.
  • Subtask 3 (50 points): 2≤n≤10002\le n\le 1000; 1≤m≤241\le m\le 24; −106≤ai≤106-10^6\le a_i\le10^6 for each array value; −106≤qj≤106-10^6\le q_j\le10^6 for every query; no distinct achievable sums tie in distance for any query.

Examples

Input

5
3
12
17
33
34
3
1
51
30

Output

Case 1:
Closest sum to 1 is 15.
Closest sum to 51 is 51.
Closest sum to 30 is 29.

Explanation

The array is 3,12,17,33,343,12,17,33,34. For query 11, the smallest achievable sum is 3+12=153+12=15, which is closest. For 5151, choose 17+34=5117+34=51 for zero error. For 3030, choose 12+17=2912+17=29 with error 11; every other sum is farther away. The heading is Case 1: for this PHOJ input.