#K11951. Closest Product Pair Selection

    ID: 23582 Type: Default 1000ms 256MiB

Closest Product Pair Selection

Closest Product Pair Selection

Mina has a list of integers representing the costs of various products in a store. She wants to buy exactly two products such that the sum of their costs is as close as possible to a given budget. In other words, given a list of integers \(C = [c_1, c_2, \dots, c_n]\) and a target integer \(B\), find two distinct elements \(c_i\) and \(c_j\) (with \(i < j\)) such that the absolute difference \(|c_i+c_j - B|\) is minimized.

If there exist multiple valid pairs, any one of them can be returned.

Note: The output should be the pair of product costs, not their indices.

inputFormat

The input is given via standard input (stdin) in the following format:

  • The first line contains an integer \(N\), representing the number of products.
  • The second line contains \(N\) space-separated integers, where each integer represents the cost of a product.
  • The third line contains an integer \(B\), the budget.

outputFormat

Output to standard output (stdout) a single line with two space-separated integers representing the pair of product costs whose sum is closest to \(B\). If there is a pair that sums exactly to \(B\), then output that pair.

## sample
6
10 22 28 29 30 40
54
22 30