simple Dynamic programming algorithm implementation