#C6733. Partition Sequence into Valid Groups

    ID: 50526 Type: Default 1000ms 256MiB

Partition Sequence into Valid Groups

Partition Sequence into Valid Groups

You are given an integer n and a sequence of n integers. The sequence consists only of the numbers 1, 2, 4, and 8. Your task is to partition the sequence into groups such that each group contains exactly one occurrence of each number in the set \(\{1,2,4,8\}\) and, when printed, the numbers in each group appear in the order 1 2 4 8.

If it is possible to partition the entire sequence into such groups (i.e. if n = 4k for some integer \(k\) and the counts of 1,2,4,8 are all equal), output each group on a separate line. Otherwise, output -1.

inputFormat

The input is read from standard input (stdin) and consists of:

  • An integer n representing the number of elements in the sequence.
  • n integers separated by spaces, each being one of the values {1, 2, 4, 8}.

Note: It is expected that n is a multiple of 4.

outputFormat

If a valid partition exists, print the groups on separate lines; each line should contain the numbers 1 2 4 8 (space-separated). Otherwise, print a single line with -1.

The output is written to standard output (stdout).

## sample
8
1 2 4 8 1 2 4 8
1 2 4 8

1 2 4 8

</p>