First soution to topcoder: 11888 - not optimal