Online Judge problem solution