#K42752. Finding a Pair with a Given Sum

    ID: 27157 Type: Default 1000ms 256MiB

Finding a Pair with a Given Sum

Finding a Pair with a Given Sum

You are given a target integer \(T\) and a list of \(n\) integers. Your task is to find a pair of distinct integers from the list whose sum is exactly \(T\). If there are multiple such pairs, you must select the one with the smallest first element; if there is still a tie, choose the one with the smallest second element.

If no such pair exists, output \(-1\).

Input/Output: You will be provided input through standard input (stdin) and you must print your result to standard output (stdout).

Note: Two integers in a pair must be different (i.e., even if a number occurs twice in the list, you can only use it if the two occurrences are considered distinct, but for this problem, treat identical numbers as not distinct if they are equal). The input format is as follows:

  • The first number is the target sum \(T\).
  • The second number is \(n\), the count of integers in the list.
  • The next \(n\) numbers are the elements of the list.

For example, for an input of 9 5 1 4 5 7 3, the output should be 4 5 because \(4+5=9\) and it is the lexicographically smallest valid pair.

inputFormat

The input is read from stdin and has the following format:

T n
a1 a2 ... an

Where:

  • \(T\) is the target sum (an integer).
  • \(n\) is the number of integers in the list.
  • \(a1, a2, \ldots, an\) are the integers in the list.

outputFormat

If a valid pair is found, output the two integers separated by a space. If no valid pair exists, print -1.

## sample
9 5
1 4 5 7 3
4 5

</p>