AlgoLounge

Put Marbles in Bags

hard

You have a row of marbles, where weights[i] is the weight of marble i, and k bags. Put all the marbles into the bags so that:

  • No bag is empty.
  • Each bag holds a contiguous stretch of the row: if marbles i and j share a bag, every marble between them is in that bag too.

A bag holding marbles i through j costs weights[i] + weights[j] — its first marble plus its last (a bag with a single marble counts that marble twice). The score of a distribution is the total cost of all k bags.

Return the difference between the maximum and the minimum score over all possible distributions.

The answer can be larger than 2^31 − 1, so use a 64-bit integer (long / long long) in Java, C++ and C.

Examples

Example 1

Input:  weights = [1,4,2,5,2], k = 3
Output: 3

The highest score comes from [1,4,2] [5] [2]: (1+2) + (5+5) + (2+2) = 17. The lowest comes from [1] [4] [2,5,2]: (1+1) + (4+4) + (2+2) = 14. 17 − 14 = 3.

Example 2

Input:  weights = [10,1,1,10], k = 2
Output: 9

[10] [1,1,10] scores (10+10) + (1+10) = 31, while [10,1] [1,10] scores (10+1) + (1+10) = 22.

Example 3

Input:  weights = [1,3], k = 2
Output: 0

There's only one way to split two marbles into two bags, so the maximum and minimum are equal.

Example 4

Input:  weights = [7], k = 1
Output: 0

Example 5

Input:  weights = [1,2,3,4,5], k = 3
Output: 8

Constraints

  • 1 <= k <= weights.length <= 10^5
  • 1 <= weights[i] <= 10^9
TypeScript
Loading editor…
List of test cases (31)
Case 1
weights = [1,3,5,1]
k = 2
Case 2
weights = [1,3]
k = 2
Case 3
weights = [7]
k = 1
Case 4
weights = [1,2,3,4,5]
k = 1
Case 5
weights = [1,2,3,4,5]
k = 5
Case 6
weights = [1,2,3,4,5]
k = 2
Case 7
weights = [1,2,3,4,5]
k = 3
Case 8
weights = [5,5,5,5]
k = 2
Case 9
weights = [1,100,1]
k = 2
Case 10
weights = [1,4,2,5,2]
k = 3
Case 11
weights = [1000000000,1,1000000000,1,1000000000]
k = 2
Case 12
weights = [1,1,1000000000,1000000000,1,1]
k = 3
Case 13
weights = [10,1,1,10]
k = 2
Case 14
weights = [3,1,4,1,5,9,2,6]
k = 4
Case 15
weights = [2,7]
k = 1
Case 16
weights = [10,10,17,8,15,15,17,15]
k = 6
Case 17
weights = [8,3,18,15,9,7,16,20]
k = 1
Case 18
weights = [4,6,17,5,4,5]
k = 6
Case 19
weights = [12,19,12,20,8,8,13]
k = 4
Case 20
weights = [2,5,1,11,9,11,9,1,6]
k = 7
Case 21
weights = [9,18,1]
k = 1
Case 22
weights = [19,18,14,14]
k = 2
Case 23
weights = [11,8,8,10,18,12,7,12]
k = 1
Case 24
weights = [3,9,10,15,6,5]
k = 2
Case 25
weights = [9,14,19]
k = 3
Case 26
weights = [940764427,683787746,496426598,62493828,874186967,538600573,490648382,689492941,967958521,212381895,151238889,707468584,479656986,936887366,382620553,435683634,284900348,498316603,614674674,349238465,137223403,225252590,289033370,253158299,776010137,281083613,560724618,859430052,328302614,372670131,322898353,149436218,441805277,675769525,727799899,698421832,544455752,969560171,398768719,171430908,269980394,470034046,395648257,981452348,89687067,287679607,499565852,166228279,956578772,142550422,646922840,255218858,970150463,117264578,540719760,325407788,3074441,954719961,853688893,389281897,351093786,348339778,867129498,571442741,202179338,102766813,680733267,626624466,866398961,850712505,764050595,277577982,17961682,930243061,855051837,719396698,510874236,947940826,41576555,827586134,231292453,826610066,349687464,112620908,624242785,187060188,262378405,49489969,725114637,24312342,590955043,863786091,571721198,365682933,626916834,803072599,90548004,316782732,917841644,624413321,830811228,448448987,903315453,892666930,689462219,871359745,469095598,284805881,879477814,715446082,857994388,408727572,800234996,270957021,281292090,888350871,416821870,809814792,385638235,309352879,396576410,434300288,932045608,620381087,916759735,83019314,20741928,851798735,25356257,765594320,876199225,481982198,992929657,553040563,467598947,918989804,780239273,14518701,266179719,395037008,891619070,781769797,186828630,603769715,463727332,370784628,486155036,967817822,566877631,492065468,136566687,195200958,194300288,648070160,741008165,327185808,968355142,903277539,523946960,921677244,768088140,597023833,314092977,550163838,966402266,255436413,568528869,300838823,267342785,77618667,706723370,539466173,625158224,976485353,40481237,329865752,392011875,248778074,557441267,550605174,464602697,944414761,365337696,964781670,767223621,29002266,192940379,191178342,865515378,109888229,403327194,592296050,481686087,654960715,852863305,56274556,664819100,984088266,570616162,468449161]
k = 100
Case 27
weights = [940764427,683787746,496426598,62493828,874186967,538600573,490648382,689492941,967958521,212381895,151238889,707468584,479656986,936887366,382620553,435683634,284900348,498316603,614674674,349238465,137223403,225252590,289033370,253158299,776010137,281083613,560724618,859430052,328302614,372670131,322898353,149436218,441805277,675769525,727799899,698421832,544455752,969560171,398768719,171430908,269980394,470034046,395648257,981452348,89687067,287679607,499565852,166228279,956578772,142550422,646922840,255218858,970150463,117264578,540719760,325407788,3074441,954719961,853688893,389281897,351093786,348339778,867129498,571442741,202179338,102766813,680733267,626624466,866398961,850712505,764050595,277577982,17961682,930243061,855051837,719396698,510874236,947940826,41576555,827586134,231292453,826610066,349687464,112620908,624242785,187060188,262378405,49489969,725114637,24312342,590955043,863786091,571721198,365682933,626916834,803072599,90548004,316782732,917841644,624413321,830811228,448448987,903315453,892666930,689462219,871359745,469095598,284805881,879477814,715446082,857994388,408727572,800234996,270957021,281292090,888350871,416821870,809814792,385638235,309352879,396576410,434300288,932045608,620381087,916759735,83019314,20741928,851798735,25356257,765594320,876199225,481982198,992929657,553040563,467598947,918989804,780239273,14518701,266179719,395037008,891619070,781769797,186828630,603769715,463727332,370784628,486155036,967817822,566877631,492065468,136566687,195200958,194300288,648070160,741008165,327185808,968355142,903277539,523946960,921677244,768088140,597023833,314092977,550163838,966402266,255436413,568528869,300838823,267342785,77618667,706723370,539466173,625158224,976485353,40481237,329865752,392011875,248778074,557441267,550605174,464602697,944414761,365337696,964781670,767223621,29002266,192940379,191178342,865515378,109888229,403327194,592296050,481686087,654960715,852863305,56274556,664819100,984088266,570616162,468449161]
k = 2
Case 28
weights = [940764427,683787746,496426598,62493828,874186967,538600573,490648382,689492941,967958521,212381895,151238889,707468584,479656986,936887366,382620553,435683634,284900348,498316603,614674674,349238465,137223403,225252590,289033370,253158299,776010137,281083613,560724618,859430052,328302614,372670131,322898353,149436218,441805277,675769525,727799899,698421832,544455752,969560171,398768719,171430908,269980394,470034046,395648257,981452348,89687067,287679607,499565852,166228279,956578772,142550422,646922840,255218858,970150463,117264578,540719760,325407788,3074441,954719961,853688893,389281897,351093786,348339778,867129498,571442741,202179338,102766813,680733267,626624466,866398961,850712505,764050595,277577982,17961682,930243061,855051837,719396698,510874236,947940826,41576555,827586134,231292453,826610066,349687464,112620908,624242785,187060188,262378405,49489969,725114637,24312342,590955043,863786091,571721198,365682933,626916834,803072599,90548004,316782732,917841644,624413321,830811228,448448987,903315453,892666930,689462219,871359745,469095598,284805881,879477814,715446082,857994388,408727572,800234996,270957021,281292090,888350871,416821870,809814792,385638235,309352879,396576410,434300288,932045608,620381087,916759735,83019314,20741928,851798735,25356257,765594320,876199225,481982198,992929657,553040563,467598947,918989804,780239273,14518701,266179719,395037008,891619070,781769797,186828630,603769715,463727332,370784628,486155036,967817822,566877631,492065468,136566687,195200958,194300288,648070160,741008165,327185808,968355142,903277539,523946960,921677244,768088140,597023833,314092977,550163838,966402266,255436413,568528869,300838823,267342785,77618667,706723370,539466173,625158224,976485353,40481237,329865752,392011875,248778074,557441267,550605174,464602697,944414761,365337696,964781670,767223621,29002266,192940379,191178342,865515378,109888229,403327194,592296050,481686087,654960715,852863305,56274556,664819100,984088266,570616162,468449161]
k = 200
Case 29
weights = [940764427,683787746,496426598,62493828,874186967,538600573,490648382,689492941,967958521,212381895,151238889,707468584,479656986,936887366,382620553,435683634,284900348,498316603,614674674,349238465,137223403,225252590,289033370,253158299,776010137,281083613,560724618,859430052,328302614,372670131,322898353,149436218,441805277,675769525,727799899,698421832,544455752,969560171,398768719,171430908,269980394,470034046,395648257,981452348,89687067,287679607,499565852,166228279,956578772,142550422,646922840,255218858,970150463,117264578,540719760,325407788,3074441,954719961,853688893,389281897,351093786,348339778,867129498,571442741,202179338,102766813,680733267,626624466,866398961,850712505,764050595,277577982,17961682,930243061,855051837,719396698,510874236,947940826,41576555,827586134,231292453,826610066,349687464,112620908,624242785,187060188,262378405,49489969,725114637,24312342,590955043,863786091,571721198,365682933,626916834,803072599,90548004,316782732,917841644,624413321,830811228,448448987,903315453,892666930,689462219,871359745,469095598,284805881,879477814,715446082,857994388,408727572,800234996,270957021,281292090,888350871,416821870,809814792,385638235,309352879,396576410,434300288,932045608,620381087,916759735,83019314,20741928,851798735,25356257,765594320,876199225,481982198,992929657,553040563,467598947,918989804,780239273,14518701,266179719,395037008,891619070,781769797,186828630,603769715,463727332,370784628,486155036,967817822,566877631,492065468,136566687,195200958,194300288,648070160,741008165,327185808,968355142,903277539,523946960,921677244,768088140,597023833,314092977,550163838,966402266,255436413,568528869,300838823,267342785,77618667,706723370,539466173,625158224,976485353,40481237,329865752,392011875,248778074,557441267,550605174,464602697,944414761,365337696,964781670,767223621,29002266,192940379,191178342,865515378,109888229,403327194,592296050,481686087,654960715,852863305,56274556,664819100,984088266,570616162,468449161]
k = 199
Case 30
weights = [36,9,14,4,15,37,2,42,15,47,4,48,14,11,34,28,34,48,25,14,4,38,31,30,48,33,31,22,50,43,28,23,2,14,15,41,3,41,2,26,10,36,22,28,16,47,25,46,8,12,5,16,29,31,3,48,9,24,42,46,16,27,8,16,27,4,13,12,35,29,22,6,14,3,27,28,2,23,31,3,5,14,4,11,29,16,33,46,43,44,30,28,39,24,28,30,30,5,33,29,24,2,5,46,31,11,19,36,13,31,18,44,3,34,5,46,33,28,46,16,48,41,7,9,37,30,33,3,16,23,27,26,27,37,41,21,38,21,44,47,43,32,9,19,15,4,47,37,50,7,2,2,34,43,39,42,9,2,11,19,47,33,15,23,39,1,7,41,24,20,45,49,21,2,23,24,29,19,17,27,13,44,13,25,49,7,45,44,29,28,8,26,47,17,8,9,50,35,34,41,14,11,28,1,19,21,10,28,33,11,27,19,39,47,42,17,12,2,19,25,25,27,7,3,40,2,48,15,45,49,33,48,45,38,22,33,5,30,44,47,2,3,12,8,34,26,5,2,50,38,28,48,6,6,38,21,40,41,45,6,29,47,32,19,49,19,35,8,30,18,47,49,16,6,21,6,31,17,41,19,42,26,20,11,19,9,38,46,35,40,42,21,43,27,50,46,15,47,3,7,21,7,20,24,45,29,22,37,14,5,24,20,36,36,37,38,27,25,41,28,40,39,7,13,29,44,49,6,45,40,34,41,34,28,42,20,45,29,49,38,14,14,6,43,40,42,10,9,27,30,18,44,32,32,5,13,30,9,17,39,38,39,42,40,24,39,42,40,28,20,9,39,7,50,8,49,34,34,18,39,32,3,20,2,2,5,31,15,41,11,35,15,27,40,3,47,29,30,18,41,9,13,39,17,38,48,38,42,32,12,10,42,49,10,4,34,5,19,40,49,19,29,4,35,26,29,13,12,19,44,42,5,21,39,38,47,25,20,46,24,11,24,14,36,27,23,42,21,45,35,15,2,20,28,38,26,44,35,16,10,13,16,42,50,31,30,13,18,25,24,10,30,35,36,45,32,43,2,7,12,22,42,40,44,40,38,50,20,31,46,2,17,22,40,24,17,14,33,13,12]
k = 250
Case 31
weights = [1000000000,1,1,1000000000,1,1000000000,1000000000,1000000000,1000000000,1,1000000000,1000000000,1,1000000000,1,1,1000000000,1,1,1,1,1,1,1000000000,1,1,1000000000,1,1000000000,1000000000,1000000000,1000000000,1,1000000000,1000000000,1000000000,1,1000000000,1,1,1,1,1000000000,1000000000,1,1000000000,1,1000000000,1,1000000000,1000000000,1,1000000000,1,1000000000,1,1,1000000000,1,1,1000000000,1,1,1000000000,1,1000000000,1000000000,1,1000000000,1,1000000000,1000000000,1000000000,1,1,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1,1,1,1000000000,1,1,1,1000000000,1,1000000000,1,1,1000000000,1000000000,1000000000,1,1000000000,1,1000000000,1,1000000000,1,1,1,1,1,1000000000,1,1,1000000000,1,1,1,1,1,1,1,1000000000,1,1000000000,1000000000,1000000000,1000000000,1,1000000000,1,1,1,1,1000000000,1000000000,1,1,1000000000,1,1000000000,1,1,1,1,1000000000,1000000000,1000000000,1,1,1000000000,1000000000,1000000000,1,1,1000000000,1,1000000000,1000000000,1000000000,1000000000,1,1,1,1,1,1,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1,1,1,1000000000,1,1,1,1,1000000000,1,1,1,1,1000000000,1,1000000000,1000000000,1,1000000000,1000000000,1000000000,1,1000000000,1,1000000000,1000000000,1,1,1,1,1,1000000000,1000000000,1,1000000000,1,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1,1000000000,1,1000000000,1000000000,1,1000000000,1,1,1,1000000000,1,1000000000,1000000000,1,1,1000000000,1,1,1000000000,1000000000,1,1000000000,1000000000,1000000000,1000000000,1,1000000000,1000000000,1000000000,1000000000,1000000000,1,1,1000000000,1,1000000000,1,1000000000,1000000000,1000000000,1,1000000000,1,1,1,1000000000,1,1,1000000000,1,1000000000,1,1000000000,1000000000,1000000000,1,1,1000000000,1,1000000000,1,1,1000000000,1,1000000000,1,1,1000000000,1000000000,1,1,1,1000000000,1000000000,1,1000000000,1000000000,1,1,1000000000,1,1,1000000000,1000000000,1000000000,1000000000]
k = 150