You have an array containing N elements, and each element has a score. In each step, you can pop an element from the leftmost or the rightmost position of the array, and gain the score of the element you choose. What is the maximum aggregated score you can obtain after K steps?
You have another array M with K weights. Each time you make the i-th pop, multiply the score of the chosen element by the weight in M[i]. What is the maximum score you can get?
This problem is about optimal picking from either end of an array. In the first part, the usual solution is to enumerate how many elements are taken from the left versus the right, then compute the best total score for exactly K picks. In the second part, the per-step weights turn it into a dynamic programming problem with order dependence, where the state tracks how many items have been taken from each side after each step. The key idea is to exploit the limited boundary movement and replace brute force with structured state transitions.