Dungeon Game

Hard

A knight enters a dungeon at the top-left room of an m x n grid and has to reach the bottom-right room. From any room, the knight can only move right or down.

Entering room (i, j) changes the knight's health by dungeon[i][j]. Negative numbers are monsters that take health away, positive numbers are potions that give health back, and 0 does nothing. The first and last rooms count too.

If the knight's health drops to 0 or below at any point, the knight dies. There is no upper limit on health.

Return the minimum starting health the knight needs to reach the bottom-right room alive.

Examples

Example 1

Input:  dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
Output: 7
Example 1: start with 7 health-2-33-5-1011030-5♥ 5♥ 2♥ 5♥ 6♥ 1startexitmonster: takes healthpotion: gives healththe path taken♥health after the room7 → 5 → 2 → 5 → 6 → 1Start with 6 instead andit hits 0 in the last room.

Go right, right, down, down. Starting with 7, the knight's health goes 7 → 5 → 2 → 5 → 6 → 1, and never reaches 0. Starting with 6, the knight would reach the last room and drop to 0, and no other path does better.

Example 2

Input:  dungeon = [[0]]
Output: 1

The only room does nothing, but the knight still needs at least 1 health to be alive.

Example 3

Input:  dungeon = [[-3]]
Output: 4

The single room takes 3 health. Starting with 4 leaves the knight with 1.

Example 4

Input:  dungeon = [[0,0],[-1,0],[3,-4]]
Output: 3
Example 4: two of the paths (♥ = health)00-103-4♥ 3♥ 2♥ 5♥ 100-103-4♥ 5♥ 5♥ 5♥ 1Down, down, rightRight, down, downneeds 3 to startneeds 5 to startthe potion covers the -4nothing covers the -4

Go down, down, right. Starting with 3, health goes 3 → 3 → 2 → 5 → 1. The path along the top avoids the -1 but never reaches the potion, so it needs 5 health to survive the -4 at the end. Losing a little health early can be worth it if it leads to a potion.

Example 5

Input:  dungeon = [[1,-3,3],[0,-2,0],[-3,-3,-3]]
Output: 3

Go right, right, down, down. Starting with 3, health goes 3 → 4 → 1 → 4 → 4 → 1.

Constraints

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