Failed to solve a 500 point TopCoder problem -_-