Dynamic Programming and array problems