HardListsNot started

Shelf pair target

A packing desk needs two different shelf positions whose integer weights sum to a target. Print the lexicographically smallest pair of values in ascending order; if no pair exists, print NONE. Equal values are allowed only when that weight occurs in at least two positions. Do not use a single shelf twice.

Input

First line target; second line space-separated integer weights, with 2 to 100 weights.

Output

Two ascending integers separated by one space, or NONE.

Example 1

Input

10
4 6 3 7 5 5

Output

3 7

Both 4+6 and 3+7 reach 10, but ascending pair (3,7) is lexicographically smaller; the duplicate fives are also valid distinct positions.

Constraints

  • There are 2 to 100 shelf positions.
  • Weights and target are integers from -10000 through 10000.

Hints

Hint 1 of 2

Compare positions i < j so a shelf is never reused.

Hint 2 of 2

Normalize each pair to ascending values before choosing the smallest tuple.

Solution

Show a reference solution and explanation
 Python · reference solution
target = int(input())
weights = list(map(int, input().split()))
pairs = []
for i in range(len(weights)):
    for j in range(i + 1, len(weights)):
        if weights[i] + weights[j] == target:
            pairs.append(tuple(sorted((weights[i], weights[j]))))
print(*min(pairs) if pairs else ['NONE'])

Why it works

Checking distinct index pairs handles duplicate weights correctly without reusing one position. Sorting each valid value pair defines its presentation; taking the minimum tuple applies the requested lexicographic tie-break. The O(N²) approach is appropriate for at most 100 shelves.

Lesson for this exercise: Lists in Python

Your program
target = int(input())
weights = list(map(int, input().split()))
pairs = []
# Compare distinct positions and save valid ordered value pairs.
print(*min(pairs) if pairs else ['NONE'])

Tests: 4 cases including the examples. Passing every test marks the exercise solved in this browser.

Ready for a challenge?

How this page was checked. Every program on it was run with CPython 3.11 at build time; runs in your browser on CPython 3.14 (Pyodide) by the publishing checks, and the output shown is what it printed. The Run buttons execute the same code on your device; nothing you type is sent anywhere.