Split Array Largest Sum

Hard

Given an integer array nums and an integer k, split nums into k non-empty subarrays so that the largest sum of any subarray is as small as possible.

Return that minimized largest sum.

A subarray is a contiguous part of the array. Together, the k pieces cover all of nums in its original order: no reordering and no skipping.

Examples

Example 1

Input:  nums = [7,2,5,10,8], k = 2
Output: 18

There are four ways to cut the array in two. The best is [7,2,5] and [10,8], with sums 14 and 18, so the largest is 18. Every other cut does worse. For example, [7,2,5,10] and [8] has a largest sum of 24.

Example 2

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

[1,2,3] and [4,5] sum to 6 and 9.

Example 3

Input:  nums = [1,4,4], k = 3
Output: 4

Three pieces from three numbers means each number is its own piece, so the answer is the largest number.

Example 4

Input:  nums = [7,2,5,10,8], k = 1
Output: 32

One piece means no cuts: the whole array sums to 7 + 2 + 5 + 10 + 8 = 32.

Example 5

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

[2] [3] [1,2] [4] [3] has sums 2, 3, 3, 4 and 3. No split can beat 4, because the piece holding the 4 sums to at least 4.

Constraints

  • 1 <= nums.length <= 1000
  • 0 <= nums[i] <= 10^6
  • 1 <= k <= min(50, nums.length)
TypeScript
Loading editor…
Test cases29
Case 1
nums = [7,2,5,10,8]
k = 2
Case 2
nums = [1,2,3,4,5]
k = 2
Case 3
nums = [1,4,4]
k = 3
Case 4
nums = [7,2,5,10,8]
k = 1
Case 5
nums = [7,2,5,10,8]
k = 5
Case 6
nums = [7,2,5,10,8]
k = 3
Case 7
nums = [1]
k = 1
Case 8
nums = [0]
k = 1
Case 9
nums = [0,0,0]
k = 2
Case 10
nums = [5,5,5]
k = 1
Case 11
nums = [5,5,5]
k = 3
Case 12
nums = [2,3,1,2,4,3]
k = 5
Case 13
nums = [10,5,13,4,8,4,5,11,14,9,16,10,20,8]
k = 8
Case 14
nums = [1,1,1,1,100]
k = 2
Case 15
nums = [100,1,1,1,1]
k = 2
Case 16
nums = [0,0,7,0,0]
k = 3
Case 17
nums = [1,2,3,4,5,6,7,8,9,10]
k = 3
Case 18
nums = [1000000,1000000,1000000]
k = 2
Case 19
nums = [2,16,14,15]
k = 2
Case 20
nums = [1,2,1,2,1,2,1,2]
k = 4
Case 21
nums = [14,6,20,14,3,3,4,11,20,10,10,13]
k = 4
Case 22
nums = [37,54,48,76,24,40,74,50,12,85,28,73,35,84,12,19,17,67,15,28,55,13,57,77,69,95,15,8,34,42]
k = 7
Case 23
nums = [999,123,369,511,154,57,731,46,370,851,181,284,696,349,197,922,363,521,348,683,877,913,227,773,704,279,724,259,833,63,29,957,354,238,303,86,31,687,628,227,724,884,828,340,156,68,760,982,549,132,951,449,68,628,37,239,780,445,877,993]
k = 10
Case 24
nums = [878168,310346,119503,544892,133405,504118,676491,779379,569522,304487,471120,519003,220471,18336,216258,188190,393621,997864,718873,882792,401494,552697,81336,61810,865736,460689,197874,454172,852500,398966,282288,216852,894978,429392,581163,18571,645280,178343,605073,615391,329317,726986,855095,57254,904166,447598,937376,116116,489187,156109,235774,826736,688874,559628,52466,66194,976458,990702,938679,114809,236281,996978,222463,915491,780874,28122,959444,314176,35056,154553,411911,428963,586314,992692,498537,179451,392539,186849,638612,98389,509366,22282,125031,424014,317558,127978,525831,372453,85620,275512,307282,511098,971625,416540,944149,881699,572433,645778,116092,305704,826855,322048,401212,19187,336989,726204,913948,436166,550076,786610,875696,516780,984379,447633,651253,126822,741284,890841,892834,964093,916181,654150,463496,774289,367399,771572,624373,708191,776533,672520,637174,998791,57136,338003,842389,709535,174875,391278,287951,53691,191985,24842,869302,828376,264060,909852,393974,935945,508768,577440]
k = 12
Case 25
nums = [20,26,18,0,15,33,3,10,9,49,20,31,10,35,33,26,0,1,40,38,44,28,4,25,43,19,22,29,47,47,45,39,30,19,48,28,15,1,12,2,13,4,3,38,42,26,29,13,48,14,32,49,37,27,6,44,14,8,1,40,9,33,2,37,26,19,12,36,19,46,8,0,3,10,42,37,25,29,39,42,14,15,44,5,29,28,22,4,1,44,15,50,0,46,50,36,5,10,42,9,23,49,10,3,8,48,8,31,40,14,18,36,4,45,40,43,45,38,48,40,14,37,9,46,33,23,22,29,5,11,21,4,15,22,47,8,26,35,35,29,23,47,24,28,46,39,2,31,4,46,10,4,8,40,41,24,15,46,15,7,14,36,11,48,22,13,7,9,22,11,12,18,9,25,38,15,21,38,12,9,46,28,40,44,27,47,36,45,47,21,32,49,38,21,48,17,49,47,31,28]
k = 50
Case 26
nums = [960063,965626,990768,999300,938994,919308,901498,933797,948090,926309,970996,919612,933776,952057,910347,902881,925838,910156,911958,972002,901548,917363,924760,926419,926280,900129,927518,903499,920186,985036,991364,918355,926506,902491,966769,918972,958708,953421,900895,927943,956976,924275,947669,949880,946218,932825,935941,949909,958506,973037,974175,924194,936277,997389,962678,934726,930688,993393,906778,937267,982718,912781,901331,955151,948251,982740,994493,939409,953406,915901,919131,942678,974949,959496,908745,942555,931954,910927,901193,998822,990022,977340,938476,952968,902415,995771,930615,966274,964597,999042,928678,962209,910175,997657,929486,975817,994084,930411,917631,971529,907030,908919,978781,924377,936937,979142,932055,950879,989011,976067,992879,963969,929546,978428,913170,990441,973261,989177,912687,901184,913505,949951,904035,960119,918638,988179,943561,971108,950116,903637,981370,922861,924396,945682,900498,993204,914259,971997,931216,906929,944179,972962,969884,908701,940483,924889,938234,981678,963438,934010,959574,950825,931294,937296,956124,908742,958846,942454,944210,923792,905733,918681,973690,935965,934752,973654,918241,914138,981267,959090,991333,915741,958943,964423,979926,906132,963469,956198,909786,933075,961426,947226,901474,918457,907675,918717,994537,945124,907284,956314,973246,949979,969361,911459,977394,949501,937047,946782,951935,916031,909222,988267,959325,967809,992883,950375,915946,972734,946040,997142,940016,916849,954022,921873,957740,920400,972610,984302,950498,901886,971260,926598,927486,988957,985402,912502,999308,957281,991892,960023,958689,938272,968042,994970,979319,915402,986615,918906,970079,940569,913208,952561,983201,928913,993562,979376,937571,926953,939217,996443,902004,929483,909789,968318,936581,921721,937183,935178,925250,938382,989827,902280,980035,936653,968705,981087,963482,956558,958087,905344,913455,967090,965463,915762,915362,914890,985635,990197,950122,977935,943732,957998,999036,991441,969690,981906,926145,950881,928284,921531,923176,979286,905307,986338,953051,958841,951549,945053,970704,949697,997927,974123,934490,957688,967237,933204,988164,964254,916250,988951,946459,926397,969590,985487,950212,943238,989774,961481,954515,999264,948782,998336,906513,909283,902622,914962,909023,965149,924823,971699,917419,967604,999909,915149,984527,912573,982934,988871,947138,932037,973314,991285,945887,998819,905226,971827,983898,920493,981425,959417,964496,940229,965777,942367,914686,917744,984367,902307,981424,911677,945592,907005,959302,916996,915159,911851,987709,970927,904882,990680,965040,930297,940328,989693,975866,916845,908886,931464,921320,959202,920421,971552,909325,979462,918450,937216,977573,917314,929497,971308,999799,925836,906158,965737,924381,993660,918031,955678,910185,983694,925906,989791,909311,947034,926892,981318,988030,975744,982648,951245,983471,908344,985540,901910,967215,975666,934630,908401,954687,923242,949250,963752,923090,945546,984534,974766,911044,998766,906189,915228,947844,917158,946094,916819,978711,995928,966804,951503,916605,935574,986955,956696,954941,982756,919186,974641,984292,987009,997844,931487,953057,977161,905206,958184,939994,969054,946095,930250,958930,906990,941746,993815,983291,908355,989007,911407,956941,961839,902360,969193,975027,986099,944815,933262,965116,947262,975979,948066,937316,988519,956936,948871,979217,923979,906512,910062,976881,924002,967377,930727,989620,911130,942833,999086,949256,944568,918218,944555,966164,997287,915111,937318,961040,915593,911609,986713,902484,913105,978663,930762,915862,912090,961677,958115,908755,918788,976101,998150,990457,954936,986776,979805,974774,965001,908529,905792,914827,923955,937735,968857,995188,933760,953806,951956,998504,987194,952376,996573,923110,933358,986131,974250,968144,991040,934597,945928,971577,986967,995949,933406,989256,952972,941260,999697,916985,947550,927771,908034,903456,905000,925099,914941,960581,917675,943833,949952,992230,975076,937073,969391,981647,923648,954812,934184,912930,913779,932404,993746,980662,915306,952142,941831,990474,990051,988954,987207,914194,953626,986013,905205,995368,910599,982181,998042,948357,955705,975619,952217,918957,980219,917696,968202,978871,918333,978358,931459,996190,912942,968991,977796,960017,926511,963763,912360,964982,906446,937602,977162,974864,976895,929029,997634,924351,936325,946683,978910,947686,937167,991984,990944,926039,977408,907287,927991,910538,948510,903383,976363,921742,990782,954994,903853,993471,942555,973721,996407,921342,989165,958518,958751,961691,952607,963573,909139,900586,953643,934992,923275,956404,970485,982061,950927,946121,964466,935679,995231,948599,902726,965059,922191,941211,980252,946073,936766,905406,974929,989838,935276,901910,902175,992988,984214,909299,988192,998075,934449,985466,987478,908377,940868,952279,986926,927940,991050,949665,972906,963156,998994,985954,989556,911974,914286,918330,929178,923099,930488,914783,934329,991874,994275,992409,925449,967281,908074,930046,966344,950305,933442,982234,941831,904105,994788,908579,910551,992361,955660,923086,944039,942959,947612,919882,907199,915018,924109,952808,997457,936864,932718,939816,942767,928855,955936,991744,971804,958263,942636,967395,907904,919784,992826,999600,997316,977343,925868,905694,975498,963466,910885,989641,990013,947589,999340,900799,949403,911202,912696,930112,997420,900047,918003,921034,935314,981844,907995,947310,905824,934536,932650,935320,999244,989661,997392,977395,951037,954077,962556,947590,954343,937380,916838,930563,933700,948322,963185,933827,972447,915891,937428,962848,956542,921207,989840,961748,922029,939848,929481,962956,980245,930131,931313,990965,998350,915022,907096,978982,981006,915864,953557,913715,978692,908840,991298,932548,931777,942491,989326,998377,946854,906453,912316,932410,999576,939523,952312,993015,937605,998142,964316,954422,942937,911546,997086,914189,963395,969611,949063,946835,997561,947367,918028,988744,941492,968784,900584,953317,975655,954859,925809,963240,950801,943117,959032,935290,992874,925734,952641,945422,963696,956800,922733,919988,979226,924345,904192,987132,926969,903190,962044,901304,978593,979835,998779,984590,916276,904453,972509,931184,988194,976436,974372,996304,997633,925681,992773,952440,996890,944835,968026,971907,980983,943170,920244,956254,941848,934438,975732,997255,983412,945648,923389,941720,933194,963797,916308,943781,935146,931685,942450,932806,980569,965780,904012,927320,919481,975653,938179,950095,945733,901333,901223,960642,904963,939374,957212,967566,990543,971334,950190,930363,974970,958572,913444,924079,920922,951464,989524,969852,931326,991782,936973,915959,905413,906472,906548,950423,950365,949552,921956,992849,926033,992722,997324,999758,932555,992871,962527,933697,926741,923586,936815,948318,991046,989880,957854,977529,980318,973535,955178,984352,969206,920039,940090,923753,926914,932570,953798,906753,977605,930587,999429,954011,997720,972383,904922,910037,904518,960567,929899,921977,963454]
k = 50
Case 27
nums = [960063,965626,990768,999300,938994,919308,901498,933797,948090,926309,970996,919612,933776,952057,910347,902881,925838,910156,911958,972002,901548,917363,924760,926419,926280,900129,927518,903499,920186,985036,991364,918355,926506,902491,966769,918972,958708,953421,900895,927943,956976,924275,947669,949880,946218,932825,935941,949909,958506,973037,974175,924194,936277,997389,962678,934726,930688,993393,906778,937267,982718,912781,901331,955151,948251,982740,994493,939409,953406,915901,919131,942678,974949,959496,908745,942555,931954,910927,901193,998822,990022,977340,938476,952968,902415,995771,930615,966274,964597,999042,928678,962209,910175,997657,929486,975817,994084,930411,917631,971529,907030,908919,978781,924377,936937,979142,932055,950879,989011,976067,992879,963969,929546,978428,913170,990441,973261,989177,912687,901184,913505,949951,904035,960119,918638,988179,943561,971108,950116,903637,981370,922861,924396,945682,900498,993204,914259,971997,931216,906929,944179,972962,969884,908701,940483,924889,938234,981678,963438,934010,959574,950825,931294,937296,956124,908742,958846,942454,944210,923792,905733,918681,973690,935965,934752,973654,918241,914138,981267,959090,991333,915741,958943,964423,979926,906132,963469,956198,909786,933075,961426,947226,901474,918457,907675,918717,994537,945124,907284,956314,973246,949979,969361,911459,977394,949501,937047,946782,951935,916031,909222,988267,959325,967809,992883,950375,915946,972734,946040,997142,940016,916849,954022,921873,957740,920400,972610,984302,950498,901886,971260,926598,927486,988957,985402,912502,999308,957281,991892,960023,958689,938272,968042,994970,979319,915402,986615,918906,970079,940569,913208,952561,983201,928913,993562,979376,937571,926953,939217,996443,902004,929483,909789,968318,936581,921721,937183,935178,925250,938382,989827,902280,980035,936653,968705,981087,963482,956558,958087,905344,913455,967090,965463,915762,915362,914890,985635,990197,950122,977935,943732,957998,999036,991441,969690,981906,926145,950881,928284,921531,923176,979286,905307,986338,953051,958841,951549,945053,970704,949697,997927,974123,934490,957688,967237,933204,988164,964254,916250,988951,946459,926397,969590,985487,950212,943238,989774,961481,954515,999264,948782,998336,906513,909283,902622,914962,909023,965149,924823,971699,917419,967604,999909,915149,984527,912573,982934,988871,947138,932037,973314,991285,945887,998819,905226,971827,983898,920493,981425,959417,964496,940229,965777,942367,914686,917744,984367,902307,981424,911677,945592,907005,959302,916996,915159,911851,987709,970927,904882,990680,965040,930297,940328,989693,975866,916845,908886,931464,921320,959202,920421,971552,909325,979462,918450,937216,977573,917314,929497,971308,999799,925836,906158,965737,924381,993660,918031,955678,910185,983694,925906,989791,909311,947034,926892,981318,988030,975744,982648,951245,983471,908344,985540,901910,967215,975666,934630,908401,954687,923242,949250,963752,923090,945546,984534,974766,911044,998766,906189,915228,947844,917158,946094,916819,978711,995928,966804,951503,916605,935574,986955,956696,954941,982756,919186,974641,984292,987009,997844,931487,953057,977161,905206,958184,939994,969054,946095,930250,958930,906990,941746,993815,983291,908355,989007,911407,956941,961839,902360,969193,975027,986099,944815,933262,965116,947262,975979,948066,937316,988519,956936,948871,979217,923979,906512,910062,976881,924002,967377,930727,989620,911130,942833,999086,949256,944568,918218,944555,966164,997287,915111,937318,961040,915593,911609,986713,902484,913105,978663,930762,915862,912090,961677,958115,908755,918788,976101,998150,990457,954936,986776,979805,974774,965001,908529,905792,914827,923955,937735,968857,995188,933760,953806,951956,998504,987194,952376,996573,923110,933358,986131,974250,968144,991040,934597,945928,971577,986967,995949,933406,989256,952972,941260,999697,916985,947550,927771,908034,903456,905000,925099,914941,960581,917675,943833,949952,992230,975076,937073,969391,981647,923648,954812,934184,912930,913779,932404,993746,980662,915306,952142,941831,990474,990051,988954,987207,914194,953626,986013,905205,995368,910599,982181,998042,948357,955705,975619,952217,918957,980219,917696,968202,978871,918333,978358,931459,996190,912942,968991,977796,960017,926511,963763,912360,964982,906446,937602,977162,974864,976895,929029,997634,924351,936325,946683,978910,947686,937167,991984,990944,926039,977408,907287,927991,910538,948510,903383,976363,921742,990782,954994,903853,993471,942555,973721,996407,921342,989165,958518,958751,961691,952607,963573,909139,900586,953643,934992,923275,956404,970485,982061,950927,946121,964466,935679,995231,948599,902726,965059,922191,941211,980252,946073,936766,905406,974929,989838,935276,901910,902175,992988,984214,909299,988192,998075,934449,985466,987478,908377,940868,952279,986926,927940,991050,949665,972906,963156,998994,985954,989556,911974,914286,918330,929178,923099,930488,914783,934329,991874,994275,992409,925449,967281,908074,930046,966344,950305,933442,982234,941831,904105,994788,908579,910551,992361,955660,923086,944039,942959,947612,919882,907199,915018,924109,952808,997457,936864,932718,939816,942767,928855,955936,991744,971804,958263,942636,967395,907904,919784,992826,999600,997316,977343,925868,905694,975498,963466,910885,989641,990013,947589,999340,900799,949403,911202,912696,930112,997420,900047,918003,921034,935314,981844,907995,947310,905824,934536,932650,935320,999244,989661,997392,977395,951037,954077,962556,947590,954343,937380,916838,930563,933700,948322,963185,933827,972447,915891,937428,962848,956542,921207,989840,961748,922029,939848,929481,962956,980245,930131,931313,990965,998350,915022,907096,978982,981006,915864,953557,913715,978692,908840,991298,932548,931777,942491,989326,998377,946854,906453,912316,932410,999576,939523,952312,993015,937605,998142,964316,954422,942937,911546,997086,914189,963395,969611,949063,946835,997561,947367,918028,988744,941492,968784,900584,953317,975655,954859,925809,963240,950801,943117,959032,935290,992874,925734,952641,945422,963696,956800,922733,919988,979226,924345,904192,987132,926969,903190,962044,901304,978593,979835,998779,984590,916276,904453,972509,931184,988194,976436,974372,996304,997633,925681,992773,952440,996890,944835,968026,971907,980983,943170,920244,956254,941848,934438,975732,997255,983412,945648,923389,941720,933194,963797,916308,943781,935146,931685,942450,932806,980569,965780,904012,927320,919481,975653,938179,950095,945733,901333,901223,960642,904963,939374,957212,967566,990543,971334,950190,930363,974970,958572,913444,924079,920922,951464,989524,969852,931326,991782,936973,915959,905413,906472,906548,950423,950365,949552,921956,992849,926033,992722,997324,999758,932555,992871,962527,933697,926741,923586,936815,948318,991046,989880,957854,977529,980318,973535,955178,984352,969206,920039,940090,923753,926914,932570,953798,906753,977605,930587,999429,954011,997720,972383,904922,910037,904518,960567,929899,921977,963454]
k = 1
Case 28
nums = [378759,322439,914824,427567,88171,479534,144950,779253,517904,229790,220141,663092,437320,226086,935728,788355,200622,586722,565794,236530,994679,794386,111739,55811,332721,720091,498227,408527,318414,597260,835086,193301,112631,91834,829583,432015,587410,225489,997555,989911,411725,372657,290911,262927,261800,69089,672257,281878,229313,664870,749176,960538,393386,380488,109434,341632,586730,643912,466898,381095,393306,937070,138304,609324,890825,821834,96182,437014,643539,665949,103286,543725,794520,489692,990500,785204,677470,672033,262929,979460,555678,618574,653416,178049,103272,442673,787358,322239,260227,407338,737732,517168,828706,330968,247752,107193,71980,816866,183740,44801,851861,254559,831327,911476,466596,357528,898283,313839,793805,879186,95496,656804,17435,584820,796353,118425,69650,846766,214380,116263,520722,826840,930635,849081,144476,630367,872807,609928,188380,855539,868435,994281,655295,910555,534804,71719,617314,414686,752392,235553,7305,603667,795391,744106,934528,553367,654324,816394,547404,831393,646794,361272,871675,294309,715861,680704,390994,797468,163331,464162,210206,615333,233383,203294,962770,672266,735871,830878,145581,533969,688990,562096,163460,945102,837034,488958,686023,423237,300534,333138,468277,655807,513355,173734,565919,304264,745013,871852,39494,411372,589849,975464,131857,871117,921286,51681,306372,932646,17487,981319,749448,453805,130241,221334,730996,290375,212365,802580,972723,885362,77761,515381,459019,522913,933862,172296,452661,744933,39904,644850,362153,545132,125600,148950,555903,595740,713518,3680,480728,898350,725644,376803,933245,345095,932713,32149,275464,910647,522164,185717,368551,88212,220388,24540,924871,223371,36191,79293,278813,185007,723246,76299,900648,303238,294557,864418,353735,726997,835607,204753,176065,553836,785240,79429,854427,813532,236268,114095,6623,766595,447857,561067,781827,430217,329490,21159,874865,880477,700871,381609,686631,11925,206600,382547,724667,731407,363574,335657,637886,426861,347318,430575,42766,281299,296489,228726,464933,503335,406166,792816,473431,957230,106763,71457,834077,627270,97054,638724,655912,160942,658100,111067,652665,961980,618355,3654,154121,209037,724875,96961,377656,93040,39358,237235,574821,950456,655317,30243,630207,130823,716264,445562,683752,781734,946084,49326,923181,616928,181256,235661,544041,897334,160453,511753,22771,909065,81728,489082,813141,735739,952523,895425,635656,551531,156382,640911,430982,767496,272877,40459,32682,357310,164157,172897,910716,745164,141002,438370,996516,487142,933060,754386,858736,816496,895651,433659,720610,148940,283062,668209,34121,405769,866982,937570,979905,590660,82183,550270,400644,231907,657839,937529,980959,640700,184,359682,308861,454168,176756,967292,408541,739025,707562,581882,757441,919203,706770,365699,667008,999915,210866,974,618425,779152,775988,290753,618736,224823,995526,152010,609752,897561,49973,919758,514947,424953,182440,397217,591083,51018,896640,41658,929301,298672,26591,956867,980345,132253,828295,612055,903632,219882,471211,671581,317606,755645,289687,170864,949804,330889,831935,698602,562653,741848,789573,358580,78174,540896,456828,57171,723285,801860,161820,187864,521165,441224,972085,781829,575696,317574,78332,459128,735353,939184,107620,838467,743374,269024,433644,351097,335047,699839,161503,333401,680280,660178,151250,343280,537126,278437,282667,399643,82817,307281,597186,920921,404600,406458,178068,607986,267547,714977,798036,143445,155763,517953,122431,112038,768417,465658,969805,80118,308918,13421,497929,706603,496603,890316,735409,31938,495402,496854,367612,921754,13379,975559,742784,187712,799972,95144,70754,964007,425095,479180,98457,995133,686127,206361,811825,57970,529973,225355,413276,636789,25410,157701,539324,633784,385340,654947,417106,162949,917873,834564,974678,461097,66430,839431,902002,379696,31592,714217,616919,762287,814791,729164,708725,145340,364251,540060,265776,335620,958324,632639,254134,362666,589659,30542,107856,654825,174574,167588,733896,171822,224543,85090,865121,585191,116584,233358,245142,305051,86077,260187,767962,433425,528539,87456,660825,191094,585089,558057,30806,423798,437156,611726,873087,43771,544117,710260,331966,762699,527553,708223,134596,650427,967885,814699,425274,104972,20362,853084,702547,487331,722748,108754,474264,439976,72914,607987,859415,848148,60181,864366,727709,814360,524009,550681,100914,30526,904603,552396,648896,81113,223616,577389,397957,92733,193900,622729,795925,859739,415242,310479,103188,562941,33965,457542,443253,96069,99978,590057,920953,395155,858219,593390,533298,243519,77338,900990,520413,509163,134012,737985,840691,805941,982701,717236,669003,269662,344221,835865,994048,971743,179227,430966,773331,464253,181027,132425,54685,877460,554820,219128,141074,888442,82293,766272,895025,825206,141315,156972,962534,929572,232521,19078,37042,473558,856840,380753,610393,835815,542411,209739,68811,602211,145125,815136,199422,158047,615444,61573,652822,37263,567282,357594,301626,933739,393043,860033,601586,468538,419015,25879,357718,32721,561372,371304,997148,867143,527973,744668,247521,390378,352200,416359,369467,347600,197216,587947,431002,746639,149567,579062,805452,740446,124599,904055,949466,798978,469764,886722,589802,717554,986629,159721,480386,236592,150288,173207,242343,939579,639327,185346,518805,726004,960248,829323,516006,669496,69311,36619,846404,544544,743257,380153,433227,725459,197858,421828,255542,35807,366748,686937,858317,992272,136945,456191,663447,265219,160949,256734,733948,790869,606277,421034,477283,774361,123802,45295,461829,739033,376902,146935,141846,551314,948668,18495,981387,171436,637656,877754,865983,510251,107379,895780,692436,214154,600992,1699,171808,928867,574207,579177,855846,984896,437326,271047,564784,286432,896112,920686,892216,41084,5551,524641,274167,723716,819970,708553,552616,901793,285680,59199,249188,919429,634289,374560,851419,724208,239054,844957,587256,505145,503835,221022,527379,761281,504871,784049,451289,141079,123357,978459,513518,165938,240215,765089,705204,437289,859914,71573,508405,814492,960554,354956,565316,194544,84703,56569,20273,725274,311750,384874,273274,678206,249931,663281,875941,879532,586979,46318,452475,767311,141419,904923,482860,489560,580697,521200,353156,657027,741693,923423,681863,790498,800473,690743,220155,395620,266233,764201,965671,623991,1438,941218,603533,143788,400224,714474,180630,677763,872936,87976,618840,808374,274132,88478,892977,347440,86479,246005,523586,965929,205324,51937,485352,801025,376905,349652,260047,120511,651765,85084,806253,679793,524144,619930,235535,515926,717185,599922,13350,688972,168097,863636,3476,488349,697627,715990,245274,846607,282329,105222,843483,285103,801970,935157,63029,76241,570471,179281,386408,462833,890555,640403,209418,434227,518208,131250,573618,37515,856162,613944,886268,281036,598203,503940,874445,375153]
k = 17
Case 29
nums = [1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000]
k = 50