For each valid transition from to , add to . This sums all ways to reach the new profile. Precompute transitions: for each , run the recursive placement function to find all reachable values.
Store these in . Then the DP loop becomes: for each , for each , for each in , update. This separation makes code cleaner and faster. You compute transitions once and reuse them for all columns.