You are given an integer array weights where each element represents the weight of a marble, and an integer k representing how many bags we must divide the marbles into.
The marbles must be divided using these rules:
Every bag must contain at least one marble.
Marbles placed in a bag must form a continuous segment in the original array.
(If marbles at positions i and j are in the same bag, then all marbles from i to j must also be in that bag.)
Cost of a bag that contains marbles from index i to j is calculated as:
The total score of a distribution is the sum of costs of all k bags.
Your task is to compute the difference between:
The maximum possible total score,
The minimum possible total score.
Return this difference.
Input: weights = [2, 4, 7, 1, 3], k = 3
Output: 9
Explanation:
Different valid partitions will produce different scores. The smallest score and largest score differ by 6.
Input: weights = [5, 5, 5], k = 1
Output: 0
Explanation:
There is only one bag, so max and min scores are the same.
Accepted:
Submission: