Let's do the math. With , a DP table of size would need entries. That's billion operations and hundreds of gigabytes of memory. No amount of improvement saves you. The problem isn't implementation; it's the approach.
You need to change what you're computing. Look at the values instead: and . Maximum total value is at most . That's a much smaller dimension.