Algorithmic problem from top coder - level easy