Xenia the mathematician has a sequence consisting of n (n is divisible by 3) positive integers, each of them is at most 7. She wants to split the sequence into groups of three so that for each group of three a, b, c the following conditions held:
- a < b < c;
- a divides b, b divides c.
Naturally, Xenia wants each element of the sequence to belong to exactly one group of three. Thus, if the required partition exists, then it has groups of three.
Help Xenia, find the required partition or else say that it doesn't exist.
The first line contains integer n (3 ≤ n ≤ 99999) — the number of elements in the sequence. The next line contains n positive integers, each of them is at most 7.
It is guaranteed that n is divisible by 3.
If the required partition exists, print groups of three. Print each group as values of the elements it contains. You should print values in increasing order. Separate the groups and integers in groups by whitespaces. If there are multiple solutions, you can print any of them.
If there is no solution, print -1.
6 1 1 1 2 2 2
-1
6 2 2 1 1 4 6
1 2 4 1 2 6
/** Author: ITAK Date: 2015-09-29 **/ #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <algorithm> #include <set> using namespace std; #define MM(a) memset(a,0,sizeof(a)) typedef long long LL; typedef unsigned long long ULL; const int maxn = 100000+5; const int mod = 1000000007; const double eps = 1e-7; int cmp(int a, int b) { return a > b; } int gcd(int a, int b) { if(b == 0) return a; else return gcd(b, a%b); } int arr[maxn]; int data[10]; int main() { int m; scanf("%d",&m); MM(data); for(int i=0; i<m; i++) { scanf("%d",&arr[i]); data[arr[i]]++; } m /= 3; if(data[1]!=m || data[5] | data[7] || data[2]<data[4] || data[6]<data[3] || data[2]+data[3]!=data[4]+data[6]) puts("-1"); else { for(int i=1; i<=data[4]; i++) puts("1 2 4"); for(int i=1; i<= data[2]-data[4]; i++) puts("1 2 6"); for(int i=1; i<=data[3]; i++) puts("1 3 6"); } return 0; }