The template below is what you'll adapt for every digit DP problem. The structure stays constant. Only the state and transitions change.
You'll always have a recursive function that processes one digit at a time, from most significant to least. The tight flag tracks whether you're still bounded by the input number's digits. When tight is true, your choices are limited. When it's false, you can pick any digit from to .