fast dp solution