#K2556. Sequence Reordering with Specific Differences

    ID: 24762 Type: Default 1000ms 256MiB

Sequence Reordering with Specific Differences

Sequence Reordering with Specific Differences

In this problem, you are given a sequence of n distinct integers and two integers (x) and (y). Your task is to determine if it is possible to reorder the elements of the sequence such that the absolute difference between every pair of consecutive elements is either (x) or (y).

More formally, given a sequence (a_1,a_2,\dots,a_n), you need to find a permutation (p) of ([1,2,\dots,n]) such that for every (1 \le i < n), [ |a_{p(i)} - a_{p(i+1)}| = x \quad \text{or} \quad |a_{p(i)} - a_{p(i+1)}| = y. ]
If such a reordering exists output YES, otherwise output NO.

Note: The sequence may contain duplicate elements. In cases where the sequence contains duplicates, a valid reordering that satisfies the given condition might not exist unless the differences specified allow it (e.g., when one of (x) or (y) is 0). In our test cases, you can assume that (x) and (y) are positive integers.

inputFormat

The input is read from standard input and consists of three lines.

The first line contains a single integer (n) (the number of elements in the sequence).

The second line contains (n) space-separated integers representing the sequence.

The third line contains two space-separated integers (x) and (y).

outputFormat

Output a single line to standard output: YES if there exists a reordering of the sequence such that the absolute difference between every two consecutive elements is either (x) or (y); otherwise, output NO.## sample

4
10 7 3 14
4 3
YES