update dp version, O(n^2) solution