Min Cost Climbing Stairs

Easy

You are climbing a staircase. The array cost has one entry per step: cost[i] is what you pay to leave step i.

After paying for a step, you climb either one or two steps. You can start on step 0 or on step 1, and starting is free.

The top of the staircase is just past the last step, at position cost.length. Return the minimum total cost to reach the top.

Examples

Example 1

Input:  cost = [10,15,20]
Output: 15
Example 1: cost = [10,15,20]pay 10step 0pay 15step 1pay 20step 2toppast the endpay 15, climb 2start

Start on step 1, pay 15, and climb two steps to the top. Starting on step 0 costs at least 10 + 15 = 25.

Example 2

Input:  cost = [1,100,1,1,1,100,1,1,100,1]
Output: 6
Example 2: hop over every 10010100112131410051617100819topPay 1 on each of the six steps you stand on: 6 in total.

Stand on steps 0, 2, 4, 6, 7 and 9, paying 1 each time, and skip every 100. Each move is one or two steps: 0 → 2 → 4 → 6 → 7 → 9 → top.

Example 3

Input:  cost = [7,3]
Output: 3

Start on step 1, pay 3, and climb one step to the top. The top is past the last step, so you always pay for the step you leave from.

Example 4

Input:  cost = [0,1,2,2]
Output: 2

Picking the cheaper next step every time doesn't work here. From step 0, step 1 looks cheaper than step 2, but going through step 1 costs 0 + 1 + 2 = 3. Climbing from step 0 straight to step 2 and then to the top costs 0 + 2 = 2.

Constraints

  • 2 <= cost.length <= 1000
  • 0 <= cost[i] <= 999
TypeScript
Loading editor…
Test cases36
Case 1
cost = [10,15,20]
Case 2
cost = [1,100,1,1,1,100,1,1,100,1]
Case 3
cost = [7,3]
Case 4
cost = [1,2,3]
Case 5
cost = [0,1,2,2]
Case 6
cost = [0,0]
Case 7
cost = [999,999]
Case 8
cost = [0,999]
Case 9
cost = [999,0]
Case 10
cost = [5,5,5]
Case 11
cost = [1,1,1,1]
Case 12
cost = [0,0,0,0,0]
Case 13
cost = [1,100,1,100,1,100,1,100,1,100]
Case 14
cost = [100,1,100,1,100,1,100,1,100,1]
Case 15
cost = [20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]
Case 16
cost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
Case 17
cost = [0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999]
Case 18
cost = [2,1,1,2]
Case 19
cost = [3,0,0,3,0,0,3]
Case 20
cost = [5,1,1,5,1,1,5,1]
Case 21
cost = [178,859,741,46,382,11]
Case 22
cost = [14,34,412,143,44,458,410,228,4,508]
Case 23
cost = [1,1,47,0,30,0,5,41,3,0]
Case 24
cost = [903,27,4,0,2]
Case 25
cost = [1,825,19,0,0,913,37]
Case 26
cost = [321,3]
Case 27
cost = [5,24,5,12,0]
Case 28
cost = [0,20,11,656,3,0,43,1,3,5,1,595,4]
Case 29
cost = [415,716,34,680,899,480,572,707,238,185,233,337,734,410,836,73,456,82,33,102,568,410,809,696,771,687,227,714,248,642,329,103,80,389,611,918,701,195,433,841,596,123,830,218,801,191,980,862,698,944,820,758,706,622,74,545,511,583,921,237,188,442,857,901,60,191,389,512,21,239,35,548,374,77,362,323,497,704,835,600,433,965,431,670,135,797,410,258,918,537,95,238,714,831,563,729,484,227,256,32,57,319,538,779,918,708,825,129,536,684,290,354,317,245,80,395,981,523,468,496,928,761,111,402,629,513,319,898,565,469,652,72,23,1,502,789,927,764,927,795,396,779,253,379,935,750,697,870,58,760,923,826,424,826,405,141,16,626,576,449,229,200,200,427,165,552,813,111,261,132,843,878,842,446,443,686,896,772,822,163,126,960,597,245,529,619,997,198,395,978,611,401,659,858,409,974,286,123,542,919,664,765,903,852,298,118,59,498,691,836,424,741,288,229,864,13,202,664,163,821,875,38,539,21,91,370,472,898,742,647,233,30,436,755,548,462,988,419,328,482,604,531,959,515,193,332,773,502,655,125,653,615,723,988,585,990,646,736,436,729,184,422,546,697,786,143,577,177,158,352,608,177,807,932,345,57,96,219,571,813,535,441,382,721,569,84,110,168,823,575,253,892,946,335,288,708,361,624,504,521,9,36,134,123,680,141,932,921,400,865,583,138,559,786,819,404,962,656,305,499,175,143,926,277,346,424,458,746,276,934,767,247,712,932,395,852,827,438,936,318,499,934,885,845,13,203,840,596,1,597,582,822,92,973,502,857,182,726,158,73,936,160,417,904,600,816,104,496,615,18,980,133,364,266,971,680,348,971,521,47,895,810,279,615,338,991,445,164,97,878,21,645,917,46,817,748,505,374,220,843,767,326,667,447,35,509,111,595,602,401,329,381,445,145,967,182,609,66,357,323,342,496,509,469,868,208,149,833,579,810,785,655,757,73,319,549,362,101,542,850,168,815,99,79,919,64,617,82,143,325,932,773,549,67,917,430,498,354,246,165,608,260,916,235,475,116,60,820,844,763,443,1,378,372,858,688,155,156,985,849,658,785,269,682,219,379,577,704,644,81,240,403,445,309,489,28,611,109,223,448,138,843,368,204,772,569,732,380,772,852,41,486,126,978,60,329,794,511,68,447,338,893,649,973,90,208,800,164,721,996,170,901,911,80,874,17,842,406,477,847,816,449,802,722,822,750,66,481,40,72,964,559,498,336,948,458,158,540,128,88,502,212,518,157,462,912,614,666,497,391,964,508,519,602,912,899,748,356,612,192,288,375,923,939,521,726,164,657,323,543,201,454,150,767,172,799,560,542,495,124,519,722,760,83,81,518,916,381,361,88,31,704,934,614,376,795,56,77,339,672,678,68,660,202,866,342,985,992,305,241,898,342,600,291,906,194,929,355,306,273,309,175,619,199,131,641,72,672,289,563,502,788,411,849,994,900,886,961,969,855,720,448,144,966,300,494,225,927,58,965,944,229,485,594,353,846,954,17,767,6,270,62,625,622,929,550,395,608,890,754,729,868,437,957,306,103,181,291,824,682,737,503,894,807,531,599,6,462,127,690,16,342,246,759,871,613,472,576,566,946,455,270,96,456,566,260,299,415,735,668,767,603,119,316,99,410,989,844,798,737,188,635,951,169,686,205,867,488,649,767,362,177,126,419,577,761,681,765,706,764,833,850,209,266,783,84,161,937,142,639,401,59,149,637,113,770,894,800,449,219,26,369,625,145,325,48,156,657,738,496,181,626,302,538,198,333,541,414,330,211,201,514,238,613,756,630,844,600,88,607,893,477,128,304,11,926,349,950,685,863,117,917,871,988,614,795,743,68,275,284,205,752,91,425,584,285,331,806,246,664,401,445,941,613,794,714,968,807,888,296,544,586,876,622,206,917,9,464,478,228,118,293,394,517,4,373,910,279,812,191,873,495,427,254,520,109,706,204,235,52,640,710,161,399,122,766,540,203,579,634,127,682,105,613,581,997,452,457,462,817]
Case 30
cost = [946,459,107,69,800,368,425,938,949,699,136,315,752,855,943,165,955,881,388,589,277,286,683,887,279,308,132,611,402,321,627,785,62,911,858,252,679,711,52,296,444,242,368,914,92,475,121,323,895,131,717,234,895,525,422,456,223,532,390,32,961,985,901,524,989,439,326,215,11,231,40,153,2,946,671,921,638,485,247,323,369,641,242,341,840,44,257,543,862,227,951,651,759,875,235,397,309,872,241,898,299,552,117,452,899,407,204,915,401,440,864,867,817,980,620,196,305,62,48,671,749,661,856,828,707,282,668,841,281,161,985,851,342,597,700,509,523,938,138,731,522,976,694,715,642,805,268,371,152,199,212,171,67,369,491,45,45,124,340,466,532,423,601,900,819,965,522,223,165,829,948,305,432,279,641,226,609,384,46,263,139,405,59,369,332,680,411,656,353,407,648,822,778,297,210,795,599,346,377,511,694,753,487,30,636,298,77,11,676,952,641,616,151,346,908,362,615,425,823,497,325,875,645,799,712,556,227,811,652,396,581,128,467,840,373,988,297,851,310,105,751,178,992,837,508,749,507,348,493,819,855,717,993,378,261,88,859,646,549,973,479,695,222,918,550,836,307,951,545,190,367,975,536,866,688,959,987,2,790,795,812,833,687,587,520,386,680,316,249,925,511,732,369,894,155,517,751,280,837,421,790,224,632,349,303,686,188,396,408,814,874,513,595,868,419,553,279,896,195,897,99,204,689,921,404,95,817,327,460,725,476,643,859,544,538,237,33,552,554,418,388,759,638,599,597,124,481,196,30,855,608,899,549,33,483,248,56,446,377,45,822,736,423,470,294,174,35,782,327,935,815,976,960,974,808,675,472,477,714,452,416,769,150,409,321,104,861,685,606,568,963,47,652,663,686,883,854,157,899,189,210,276,461,757,157,472,292,872,343,797,634,646,673,699,184,835,199,891,15,224,17,334,768,430,712,250,446,733,225,123,240,78,380,794,534,310,341,977,506,579,509,262,331,820,287,229,677,509,597,685,742,411,843,876,118,549,89,983,922,169,719,349,329,268,942,434,82,832,77,987,362,461,88,567,784,568,751,499,603,451,142,704,184,382,588,595,245,68,99,418,244,894,794,514,281,646,378,81,249,376,333,457,184,224,730,918,456,111,60,645,449,127,337,995,387,769,628,717,386,7,587,227,344,259,668,135,755,377,682,581,885,176,561,299,861,392,551,492,620,907,851,554,39,331,421,547,565,19,124,559,872,836,959,300,375,253,106,335,544,703,821,439,921,673,678,700,228,147,990,742,109,532,64,687,912,27,873,969,772,967,857,162,703,143,49,677,110,193,648,722,232,489,883,371,43,353,461,759,229,751,970,160,320,739,309,765,388,458,233,838,820,556,173,569,498,269,260,397,481,136,610,566,442,817,887,547,883,258,205,186,647,881,562,527,190,896,970,333,963,729,981,120,792,960,53,333,677,894,225,475,449,713,129,203,78,178,920,238,708,77,723,589,573,978,20,597,288,738,0,389,803,752,650,227,350,23,210,163,812,492,414,622,676,160,27,663,594,704,520,69,743,722,191,758,193,183,64,736,817,118,764,551,172,675,733,962,72,200,258,491,809,609,52,125,829,279,601,325,957,771,21,106,131,131,804,691,98,554,888,638,958,675,979,960,391,119,789,501,128,628,824,999,511,690,682,665,337,336,301,915,850,45,717,295,745,645,357,552,313,574,790,95,34,316,865,389,304,984,394,476,278,452,409,681,726,161,230,93,205,456,773,660,821,655,498,319,105,967,659,851,266,441,978,611,201,743,490,120,825,636,170,840,726,815,791,15,377,609,883,595,127,941,858,917,483,396,998,979,439,570,102,453,446,43,440,581,533,769,929,302,419,137,451,804,862,296,695,662,313,608,322,779,298,359,793,846,109,99,160,445,224,49,307,860,819,455,384,971,87,936,437,945,912,569,877,223,332,611,306,801,534,296,263,999,583,201,499,225,200,546,841,842,870,795,205,677,435,189,4,436,653,771,402,184,790,824,954,189,625,426]
Case 31
cost = [585,37,347,374,294,54,472,694,657,539,159,782,753,300,123,327,43,290,745,158,835,256,571,995,582,967,130,60,217,916,117,270,944,267,529,228,426,671,201,404,926,733,699,863,977,499,168,841,356,232,602,371,824,964,842,288,109,397,867,217,547,180,21,992,968,873,901,414,524,61,723,318,647,397,34,139,661,450,591,255,887,37,649,595,702,860,252,314,533,552,725,221,194,648,522,665,501,817,865,219,891,539,336,874,975,961,299,481,329,331,954,692,545,804,24,756,683,821,559,507,531,793,897,484,34,865,790,114,301,461,907,795,99,676,4,251,241,911,750,976,805,563,590,993,199,189,683,126,390,962,793,58,725,38,667,214,38,923,735,162,894,327,448,830,648,654,225,117,576,839,451,190,21,179,475,535,790,216,927,384,552,435,920,693,294,257,360,269,199,976,49,195,784,369,3,849,316,296,396,417,255,894,568,848,818,203,725,512,485,60,133,580,189,656,640,374,379,244,744,730,674,69,254,478,404,340,580,499,878,475,727,253,56,750,190,130,638,62,268,766,414,41,717,277,641,530,693,775,532,728,315,565,459,731,287,327,968,783,790,142,940,289,725,913,197,20,742,27,257,404,654,360,49,100,147,528,149,200,221,803,238,567,959,875,930,439,353,427,880,143,881,879,970,171,525,375,803,948,577,166,801,14,397,358,741,337,315,127,380,372,40,771,360,340,588,639,11,178,858,85,877,995,367,412,614,986,345,215,126,439,830,764,229,942,205,371,502,244,931,593,21,647,245,862,922,359,292,832,920,632,319,812,174,441,111,860,274,741,460,111,523,607,49,467,206,946,190,450,406,749,302,631,758,683,874,168,113,655,991,540,785,554,586,399,256,642,692,302,490,757,114,559,405,840,300,695,402,786,188,570,328,622,426,768,461,220,936,138,298,990,473,236,830,285,65,638,726,142,640,776,811,860,796,509,813,342,32,865,621,41,79,515,829,231,306,467,968,830,893,244,482,787,923,853,848,971,906,261,520,981,949,583,440,486,906,210,252,630,953,248,864,531,133,532,69,531,913,863,153,568,682,856,634,329,987,756,247,868,723,298,134,218,939,334,851,746,495,655,409,390,119,415,216,175,34,40,658,766,677,518,304,41,714,983,937,364,764,612,839,495,418,330,819,703,394,399,475,157,808,604,604,113,406,1,15,406,101,857,568,663,986,196,834,303,985,356,735,361,890,267,37,308,118,920,516,133,734,933,0,882,196,429,709,109,590,83,649,874,773,343,755,893,743,426,144,656,137,368,597,38,629,741,220,267,138,180,107,187,265,17,594,14,61,200,870,209,337,471,571,300,447,607,315,198,73,136,939,590,893,797,50,681,140,304,195,718,808,689,4,763,365,718,700,724,892,39,219,231,288,237,89,989,490,193,479,364,939,414,770,118,240,985,84,861,809,718,127,873,450,735,481,279,520,245,286,680,471,548,557,417,7,195,273,57,238,233,366,577,270,890,157,146,504,22,347,136,337,20,319,857,483,427,344,973,102,543,916,477,768,399,726,657,140,489,900,626,465,155,205,581,453,743,184,206,290,236,829,473,996,224,980,293,305,15,204,515,539,567,153,551,307,418,85,330,672,8,484,642,728,159,598,97,840,66,384,275,610,248,789,822,217,931,666,100,284,753,709,167,626,149,115,817,563,731,315,774,474,831,232,219,939,830,434,725,50,395,263,257,991,391,430,274,78,790,825,747,466,379,399,421,50,858,690,47,107,584,36,285,353,880,406,339,720,996,207,293,838,40,231,568,618,634,877,543,854,417,885,118,301,505,749,642,818,170,657,547,847,650,757,141,847,575,439,223,509,374,186,671,719,829,14,254,624,177,726,483,317,408,200,520,903,914,54,22,56,786,470,161,554,660,141,177,730,883,883,23,209,368,142,33,999,769,215,831,882,226,493,120,795,526,154,571,574,936,686,481,680,611,237,10,732,340,170,555,187,459,792,455,59,275,729,422,464,404,366,429,433,157,413,789,693,242,286,503,602,639,342,731,560,701]
Case 32
cost = [999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999]
Case 33
cost = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Case 34
cost = [0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999,0,999]
Case 35
cost = [999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999,0,0,999]
Case 36
cost = [3,2,0,1,2,3,3,0,0,2,1,1,3,2,1,0,2,3,1,2,0,0,0,2,2,1,1,0,3,3,2,2,2,1,0,2,0,3,1,3,3,0,0,2,0,2,3,2,1,3,2,1,3,3,0,1,2,2,2,2,0,0,0,3,3,0,0,3,2,0,2,0,0,0,0,1,2,2,3,0,2,1,0,3,3,3,3,0,3,1,2,0,3,0,0,2,2,2,3,0,1,3,2,3,1,0,2,3,1,1,2,0,2,0,2,3,3,0,2,3,0,3,2,3,3,0,3,0,2,1,1,2,1,0,1,1,3,0,2,2,1,1,0,3,3,1,1,0,1,3,2,2,2,3,3,0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