This looks like a greedy algorithm, but in my intro algorithms class we wrote a dynamic programming algorithm and showed that it does much better. Therefore you had better totally rewrite this.