君はまるで砂漠に咲く、一輪の花。

僕はその花に引き寄せられる蝶。

Codeforces Round #199 (Div.2)

はい。
http://codeforces.com/contest/342

A. Xenia and Divisors

ざっくりと大意

・n(3で割り切れる)個の数が与えられるので、a<b<cでb/a,c/bな関係の数を3つで1組にする。1回使った数は使えない。
・n個ある中の数は最大でも7である。
・n/3以上の組合せを作成出来ていればそれらを出力、出来ていなければ-1を出力。

方針のようなもの

・最大で7なのが救いで、1,3,6と1,2,6と1,2,4の3パターンしかない。

n=int(raw_input())
l=[int(x) for x in raw_input().split()]
chk=0
one=l.count(1)
two=l.count(2)
tre=l.count(3)
fou=l.count(4)
six=l.count(6)
ans=[(1,3,6),(1,2,6),(1,2,4)]
cnt=[0,0,0]
if one>0 and tre>0 and six>0:
    chk=min(one,tre,six)
    cnt[0]=chk
    one-=chk
    tre-=chk
    six-=chk
if one>0 and two>0 and six>0:
    chk=min(one,two,six)
    cnt[1]=chk
    one-=chk
    two-=chk
    six-=chk
if one>0 and two>0 and fou>0:
    chk=min(one,two,fou)
    cnt[2]=chk
    one-=chk
    two-=chk
    fou-=chk

if sum(cnt)<(n/3):
    print -1
else:
    for i in range(3):
        for j in range(cnt[i]):
            print ans[i][0],ans[i][1],ans[i][2]

もうちょっと短く書ける気もするけど考えたことのまま処理させようとしたらこうなった。。