Trapping Rain Water

Hard

You are given n non-negative integers in height, an elevation map where every bar is 1 wide. Work out how many units of water it traps after it rains.

Examples

Example 1

Input:  height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Example 1: 6 units of water010210132121barwater

Example 2

Input:  height = [4,2,0,3,2,5]
Output: 9
Example 2420325heightwater024120The level is 4,the shorter wall.total 9barwater

Example 3

Input:  height = [3,0,2,0,4]
Output: 7
Example 330204heightwater03130The level is 3,the left wall.total 7barwater

Water over index 1 rises to 3, the height of the left wall, so it holds 3 units. Index 2 holds 1 and index 3 holds 3.

Example 4

Input:  height = [1,2,3,4,5]
Output: 0
Example 4: the water runs off12345heightwater00000Nothing on the leftis taller, so thewater runs off: 0barwater

No bar has a taller bar on both sides, so all the water runs off.

Example 5

Input:  height = [5,0,0,0,1]
Output: 3
Example 5: the shorter wall decides500013 units of water,1 deep and 3 widebarwater

The right wall is only 1 tall, so the water is 1 deep over each of the three 0s.

Constraints

  • n == height.length
  • 1 <= n <= 2 × 10⁴
  • 0 <= height[i] <= 10⁵
TypeScript
Loading editor…
Test cases33
Case 1
height = [0,1,0,2,1,0,1,3,2,1,2,1]
Case 2
height = [4,2,0,3,2,5]
Case 3
height = [3,0,2,0,4]
Case 4
height = [1,2,3,4,5]
Case 5
height = [5,0,0,0,1]
Case 6
height = [0]
Case 7
height = [7]
Case 8
height = [2,1]
Case 9
height = [0,0,0]
Case 10
height = [2,1,2]
Case 11
height = [5,4,1,2]
Case 12
height = [5,4,3,2,1]
Case 13
height = [1,0,1,0,1,0,1]
Case 14
height = [100000,0,100000]
Case 15
height = [0,3,0,0,2,0,4]
Case 16
height = [4,2,3]
Case 17
height = [6,4,2,0,3,2,0,3,1,4,5,3,2,7,5,3,0,1,2,1,3,4,6,8,1,3]
Case 18
height = [2,0,2,0,9,0,1]
Case 19
height = [8,6,6,4,2,2,0,8]
Case 20
height = [5,9,3,5,1,5,4,9,0,7]
Case 21
height = [0,8,1,8,1,6,0,2,8,8,0,5]
Case 22
height = [7,10,2,2,6,2,10,5,4,2,8,2,8,9,6]
Case 23
height = [1,8,0,0,6,4,9,10,8,9,10,6,4,4,9,10,0,2,5,4]
Case 24
height = [41897,76181,41496,94788,32392,75381,82007,72266,13801,11861,28258,55612,57436,78157,27138,41658,12602,98810,70082,36521,88024,69221,14008,70745,66795,53524,71444,19695,89182,44705]
Case 25
height = [23861,34190,71409,83728,4183,27779,3066,22172,34343,45444,18054,55611,5258,57514,4565,60746,52927,73523,95560,6643,74704,50383,53000,10664,72194,72439,74374,97424,767,91742,98761,41666,70628,94726,67599,55810,12534,38782,69976,19756,92461,79369,96489,86984,57770,91064,74100,12574,97908,93745]
Case 26
height = [8815,81054,27069,4105,3283,34704,76367,5193,84797,65155,54765,99988,29342,33473,34762,61080,7465,20401,26257,80487,49786,78444,88621,10264,37325,71517,66304,68002,64802,95510,43312,63018,22996,21848,89622,52287,38449,93058,45867,98865,88852,99647,85176,71435,31833,90410,55947,94769,91923,32703,33117,65113,23551,73093,64313,88117,57325,93347,5638,87303,2964,50832,72249,75673,49810,77545,91879,56417,72112,44552,60310,67434,20003,32221,68574,37592,58367,83870,923,76859,74021,73304,14533,24132,95876,64279,91752,94814,96409,75675,45920,29819,86592,10898,29024,90392,29520,26009,55590,34020]
Case 27
height = [51022,93567,70445,88058,89114,41966,82169,90881,459,94145,86257,9087,95993,36204,55379,69849,90939,510,59728,24968,38645,62232,49243,72149,28829,81743,34627,21339,91499,68640,32516,49877,33091,12270,80099,84818,60503,5764,40165,31944,33026,72737,6512,45690,23838,98462,25985,2899,44214,69837,90999,33133,35845,38911,93134,18975,7931,78724,57052,66883,68607,73777,50014,39199,71980,61013,59067,526,13570,84787,35159,49121,26090,39471,41876,97344,92564,38047,4459,19150,63032,17700,87945,75859,3719,48997,59653,37189,55341,89481,93339,94010,84980,99766,87515,77454,82024,51420,48329,39114,85341,91082,80741,28746,22318,65083,88260,25580,59530,52483,25275,24533,74099,95870,90120,88553,5127,18194,28749,64034,42569,70168,27863,75468,86152,39730,39486,59659,33910,56251,65387,86807,97948,23323,3352,57920,97513,62365,52912,98376,86353,76474,73895,40695,71522,64632,8804,67086,17388,14625,37898,62553,5222,89299,29958,97654,6286,3171,74369,40513,95704,49598,72311,52361,57950,54167,87984,95600,59044,30786,36313,39003,84846,35106,48515,34273,53784,31926,8060,23040,68255,89403,20215,37523,96947,27839,49172,94568,71687,3760,62251,48477,92086,39675,28635,74841,33842,35289,24754,41155]
Case 28
height = [0,1,2,3,2,2,3,3,0,0,2,1,2,1,2,0,2,1,3,1,1,2,3,0,0,1,1,3,0,1,1,3,1,3,1,3,1,2,0,3,1,2,2,2,1,3,1,2,0,0,1,1,2,3,0,3,2,1,3,2,2,0,1,1,2,3,1,2,2,2,3,2,0,3,3,2,1,3,3,2,3,0,0,2,0,1,0,1,1,2,2,2,2,1,0,2,0,0,2,1,0,3,0,1,3,2,3,2,1,2,1,3,1,0,2,0,0,1,0,0,3,0,3,1,3,1,0,0,3,1,2,1,2,1,3,0,0,3,1,2,3,0,2,3,3,2,3,2,0,2,2,1,2,1,0,0,1,0,3,3,1,2,0,0,3,3,0,0,1,1,0,0,3,1,2,0,0,2,1,0,2,0,1,1,3,2,2,3,2,0,2,3,2,0,2,2,2,0,0,1,3,3,3,0,0,2,2,3,1,1,1,2,3,0,1,3,0,2,2,0,0,2,3,2,3,3,2,2,0,3,2,1,3,0,0,2,2,0,1,2,3,2,0,1,0,0,2,0,2,1,3,2,0,2,1,1,3,0,1,2,2,2,2,0,0,0,3,2,2,3,1,3,2,0,1,2,2,0,2,1,0,2,0,1,1,2,3,1,1,1,3,3,1,3,1,3,1,0,0,1]
Case 29
height = [300,299,298,297,296,295,294,293,292,291,290,289,288,287,286,285,284,283,282,281,280,279,278,277,276,275,274,273,272,271,270,269,268,267,266,265,264,263,262,261,260,259,258,257,256,255,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,127,126,125,124,123,122,121,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81,80,79,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300]
Case 30
height = [100000,36,28,26,49,26,18,19,0,17,6,30,23,38,39,10,0,26,30,13,29,23,30,7,34,4,46,25,14,12,8,37,18,12,12,41,35,29,22,29,32,25,18,4,25,21,42,42,44,15,33,41,12,6,47,8,0,30,33,39,6,8,30,18,30,7,30,45,4,0,36,29,15,33,35,4,43,50,1,43,19,1,43,27,47,27,50,1,45,37,0,35,8,13,12,30,9,36,4,15,1,35,19,24,31,20,0,11,42,28,42,18,14,16,13,38,16,46,42,13,27,5,38,30,35,5,23,26,4,9,39,20,8,16,1,7,41,28,42,48,25,16,2,17,1,28,15,38,22,25,32,9,23,46,9,41,24,32,45,4,3,44,34,43,12,24,22,36,45,43,41,42,32,31,50,47,40,36,10,37,16,10,29,21,46,19,1,40,29,10,35,26,25,9,20,10,32,18,48,22,23,39,34,34,38,35,3,22,29,43,49,38,47,7,44,20,25,23,25,26,16,10,42,3,18,19,32,4,38,27,37,20,38,27,19,27,25,25,36,19,18,5,44,1,41,7,16,28,21,38,13,45,15,0,21,21,15,29,17,2,19,13,23,3,34,36,39,1,30,2,4,9,40,41,1,31,39,26,32,23,38,30,33,37,8,5,48,31,40,15,16,9,26,27,46,27,5,14,17,25,10,20,31,32,17,31,1,47,15,19,12,42,17,37,29,28,43,15,4,49,40,45,46,5,38,49,43,8,35,50,43,43,23,28,14,8,16,45,23,23,32,5,40,29,7,39,23,0,3,10,38,4,7,27,6,8,3,38,32,17,38,15,8,40,6,13,43,9,21,21,24,12,27,17,17,40,4,23,50,33,42,8,11,0,27,41,3,43,44,29,49,32,8,12,50,48,29,21,45,14,18,99999]
Case 31
height = [549,331,970,765,207,236,924,624,352,165,123,203,67,534,75,295,725,997,374,385,987,225,667,923,300,599,957,112,893,7,383,269,647,116,54,907,172,4,656,453,698,759,307,703,287,181,32,416,341,546,54,905,996,516,204,600,244,574,150,796,315,376,203,989,10,185,642,371,702,254,4,874,895,326,94,754,549,845,378,423,924,426,393,703,383,453,468,518,614,802,569,200,139,296,252,16,243,484,47,883,50,977,82,75,876,89,836,544,281,421,967,828,162,436,813,47,703,650,589,123,301,787,321,275,820,932,680,279,243,58,199,312,319,283,512,544,921,423,356,162,700,103,230,454,447,545,104,213,970,972,843,25,342,655,862,644,308,411,299,974,406,507,101,274,67,245,987,104,295,940,670,289,323,657,441,842,537,30,415,479,846,615,319,642,255,514,303,446,922,924,808,845,95,510,819,123,834,524,582,525,416,727,759,17,214,579,910,255,344,596,832,196,430,717,150,932,382,333,688,209,700,585,822,360,645,695,211,889,249,963,587,548,49,112,560,34,608,294,750,343,209,17,738,771,491,714,634,360,865,553,71,339,41,592,967,692,990,14,786,760,79,382,198,862,68,941,973,40,924,259,372,676,490,906,861,636,364,830,421,912,953,277,210,800,522,103,39,875,526,583,948,211,138,791,939,737,585,126,481,100,821,12,472,80,232,203,242,639,38,879,894,369,501,700,607,405,151,613,114,910,932,458,499,968,370,854,875,327,278,946,388,621,194,28,661,378,370,647,866,858,167,6,809,109,370,177,574,803,935,829,986,977,750,523,438,142,743,225,172,684,373,746,727,220,616,341,694,489,758,435,476,763,210,7,87,204,451,72,993,35,531,271,173,360,697,682,92,697,708,840,430,138,180,859,169,300,616,190,453,807,718,192,612,240,966,676,614,199,109,177,539,772,584,194,225,70,714,603,737,775,48,606,323,212,788,937,853,538,404,630,813,963,544,535,952,68,426,726,825,208,309,474,209,815,489,608,223,158,878,491,534,881,596,75,760,803,965,572,15,587,940,634,48,467,768,885,755,694,610,185,2,650,517,3,36,143,49,978,291,665,194,892,206,725,256,710,565,123,45,832,789,37,754,966,452,99,518,761,684,284,624,353,894,864,81,405,586,746,181,220,920,627,554,346,980,545,533,524,355,412,138,356,728,359,829,284,4,636,345,724,395,354,465,264,692,683,105,377,889,864,441,66,354,79,216,62,679,432,823,300,474,426,84,60,481,532,755,540,540,333,900,73,537,442,162,738,21,971,59,536,199,165,216,883,684,998,253,577,99,869,528,448,892,96,282,685,656,802,212,272,178,942,616,627,994,30,722,718,424,706,64,453,728,355,460,524,786,854,587,735,59,735,964,214,311,594,755,345,80,620,536,843,778,392,990,479,526,346,971,659,942,644,132,760,562,805,664,639,862,454,274,769,756,35,122,792,945,889,856,616,213,909,315,74,289,351,618,233,210,100,419,414,905,889,943,698,685,884,275,130,159,485,583,299,28,379,959,837,163,298,269,702,634,570,357,609,97,471,362,619,473,646,527,821,5,57,447,73,409,406,249,518,604,255,432,148,374,633,31,379,42,36,366,956,288,895,382,799,870,589,714,650,606,712,801,879,96,898,19,397,357,844,682,24,12,68,864,43,521,70,357,312,728,247,414,5,699,963,444,270,384,897,830,178,570,477,951,965,209,718,668,633,887,223,697,70,739,678,883,332,247,515,821,47,234,748,65,56,128,691,505,814,982,189,537,236,604,989,796,421,290,171,685,625,652,801,260,648,115,384,795,433,857,3,880,296,531,324,275,387,100,966,848,847,471,849,798,493,621,370,798,499,342,68,157,351,717,552,696,484,391,925,951,243,393,806,880,736,436,921,700,170,304,308,621,69,426,451,584,87,169,504,145,513,519,50,33,135,433,440,355,999,671,556,879,122,502,857,68,14,859,639,125,181,725,853,348,375,651,807,571,150,135,75,684,97,498,805,143,127,230,72,831,528,261,737,256,301,161,28,600,988,203,725,682,24,559,554,257,144,298,630,261,279,893,795,590,602,683,707,675,391,995,835,20,663,725,982,891,531,452,611,880,965,447,649,512,470,461,801,418,935,571,738,747,725,155,957,559,569,98,372,148,926,290,940,618,390,642,641,834,313,144,223,511,398,50,4,337,591,134,310,389,761,318,469,631,128,379,460,889,481,204,520,715,163,537,541,295,790,600,552,709,554,619,626,874,795,559,701,544,177,631,900,318,583,424,492,793,798,164,381,127,814,378,74,245,256,977,491,378,14,814,652,343,847,414,749,669,588,457,985,106,412,952,469,572,517,480,166,504,97,485,601,335,192,895,64,547,250,739,209,226,70,668,266,133,828,133,153,424,34,946,33,244,215,124,21,201,955,765,211,617,916,127,403,914,592,896,624,821,909,310,106,785,265,153,310,895,249,992,403,943,555,966,367,802,447,39,755,625,599,766,383,970,104,464,149,214,676,992,79,354,969,200,654,596,549,515,857,906,215,2,173,476,200,803,666,418,572,862,946,880,901,919,991,494,474,787,837,757,443,849,959,189,904,553,385,578,66,25,540,809,714,646,573,706,378,961,567,758,547,547,566,667,289,401,279,465,206,709,744,124,392,656,697,186,548,444,814,59,185,348,520,416,219,510,741,738,758,417,879,59,973,162,723,291,835,930,843,200,658,138,967,927,982,842,486,767,130,764,509,505,454,577,238,671,326,344,922,833,713,353,453,117,553,57,111,43,864,821,679,199,395,155,908,833,888,666,985,915,214,985,862,896,952,282,685,57,112,19,55,33,768,745,223,649,347,498,433,242,885,85,540,385,630,868,748,269,492,414,814,109,14,320,653,628,497,24,470,725,736,434,581,830,943,626,957,960,213,213,675,669,966,286,311,885,665,968,54,970,222,647,853,343,280,964,846,139,742,129,812,747,7,673,703,787,481,337,493,836,628,472,966,746,583,933,433,792,794,393,949,407,825,222,284,242,289,76,121,260,726,950,877,448,58,105,51,719,55,279,702,396,368,279,675,353,898,783,333,477,325,431,837,707,621,385,71,195,588,443,912,770,715,135,732,66,380,450,521,980,708,76,977,581,111,85,925,826,667,713,165,549,218,250,204,765,724,68,497,221,184,25,421,464,141,313,885,179,373,724,201,760,45,661,364,598,674,918,860,58,101,928,900,882,688,84,128,327,66,111,670,889,83,385,850,282,314,848,514,659,116,296,600,0,176,993,307,807,749,167,249,288,348,658,142,415,463,759,798,57,409,740,587,13,380,506,853,813,940,710,645,268,411,73,962,540,525,767,269,913,280,392,317,780,591,346,461,170,659,175,877,987,10,427,26,737,175,890,905,475,893,303,464,67,775,848,388,612,6,830,774,579,779,193,772,170,490,187,554,804,264,299,388,822,791,126,978,938,24,495,252,208,401,938,240,714,243,791,675,888,473,475,688,287,317,423,639,862,511,212,454,388,233,901,191,618,930,955,899,662,980,391,832,601,695,553,485,745,88,332,276,94,27,543,203,4,310,886,85,457,806,748,671,516,349,158,229,455,762,600,952,736,600,691,504,439,108,391,871,162,895,244,873,882,155,733,506,375,385,44,583,884,159,428,124,949,674,301,216,815,12,696,300,886,911,334,406,630,83,881,584,483,953,175,987,240,416,564,775,963,599,44,522,536,141,633,7,254,536,656,418,803,792,239,84,445,676,673,415,669,844,521,552,811,372,555,720,801,211,368,222,754,651,882,256,694,642,375,68,104,174,579,62,810,548,96,976,137,201,197,951,237,194,192,28,948,29,316,202,479,775,610,509,952,859,696,744,945,195,607,185,310,794,740,759,831,444,500,808,279,813,675,915,714,148,712,78,611,363,520,441,864,694,281,743,951,781,429,181,900,293,634,54,501,536,934,391,739,420,703,516,128,168,99,259,443,657,715,632,688,406,77,53,325,355,407,20,889,28,497,115,885,492,757,999,463,711,360,80,958,24,510,415,697,117,243,359,251,715,25,885,863,407,704,153,276,616,7,780,111,332,723,71,151,195,715,379,55,949,795,785,338,771,446,901,59,337,330,773,174,86,493,215,363,243,543,88,262,820,255,520,852,259,345,721,737,401,921,885,249,593,214,203,872,132,102,745,547,530,449,148,944,629,24,971,781,785,198,306,124,203,307,886,744,434,193,95,383,668,11,266,189,650,576,202,57,704,354,139,324,768,758,306,850,201,945,429,971,884,929,145,128,607,954,592,533,50,188,765,907,906,416,270,569,995,198,650,445,796,561,737,525,890,310,295,945,110,980,251,173,273,399,580,311,417,138,877,502,837,241,291,751,591,630,161,27,75,14,749,545,525,266,294,769,334,534,170,490,508,777,740,357,884,250,582,371,936,619,30,97,736,458,850,957,130,811,830,619,733,585,970,164,626,689,483,590,431,4,859,846,347,105,939,877,906,563,99,848,355,734,386,675,881,118,369,438,338,731,94,240,190,851,999,215,660,811,37,823,611,60,775,870,306,539,140,729,765,768,385,754,928,456,905,911,254,241,44,747,983,505,341,380,154,873,243,371,757,417,812,631,170,264,24,712,269,373,105,551,313,106,232,720,253,15,918,397,440,443,411,210,725,61,91,18,580,990,347,668,227,82,271,221,781,9,588,388,409,944,914,867,546,940,843,489,332,895,459,622,755,687,380,210,720,51,513,887,731,903,358,337,641,11,463,158,489,645,67,435,398,73,747,841,43,138,295,79,42,453,768,953,317,497,6,905,516,246,171,518,662,739,160,863,992,241,92,48,185,155,556,939,644,452,100,414,418,307,625,524,755,850,605,493,586,504,405,904,235,1,717,767,680,279,617,806,572,277,972,817,708,324,236,683,831,545,789,164,680,386,979,784,341,668,862,797,897,391,787,13,93,836,557,949,487,577,213,202,153,767,121,364,673,550,101,86,77,212,885,662,707,546,237,782,344,346,169,471,311,877,860,546,606,231,682,528,178,319,971,80,238,509,34,286,341,661,756,899,98,802,33,101,122,998,384,733,548,192,44,967,515,867,6,725,243,14,408,523,146,72,589,139,578,148,681,237,315,33,229,492,889,473,260,868,653,264,454,637,167,753,498,755,184,573,679,474,700,526,221,360,289,233,403,281,879,817,377,66,143,158,163,775,702,892,660,487,339,161,149,713,644,220,418,143,667,125,888,359,267,356,976,603,755,218,611,616,14,447,223,690,611,210,341,724,423,881,832,699,424,581,321,254,639,210,92,422,653,335,37,373,363,607,627,390,442,497,24,501,497,68,715,80,182,700,237,482,887,785,351,12,226,630,700,198,316,91,608,515,405,185,748,632,861,398,782,662,164,771,323,304,692,767,784,498,619,81,801,329,786,566,241,903,864,117,342,882,42,429,279,477,398,942,555,914,242,322,827,636,911,324,827,308,424,286,231,256,4,786,287,778,863,199,554,384,934,87,836,587,785,279,166,137,617,230,626,560,367,724,642,671,654,146,133,299,846,479,353,136,785,31,633,155,525,789,512,338,231,759,337,215,591,698,106,466,674,806,343,68,573,385,568,193,175,656,527,472,963,831,11,618,565,928,153,803,518,131,84,353,806,457,381]
Case 32
height = [11175,83945,55669,22042,89434,99987,86548,87677,78533,85477,81193,16865,4110,83728,6202,63719,98522,41208,25191,58706,88358,52634,96785,49053,61640,95664,42357,97174,18559,75596,51276,25127,29264,1322,2036,93601,18135,58073,90045,48809,33758,70694,24677,21937,80059,44555,92264,78440,68336,5783,20462,58179,23169,37137,29672,44428,83864,72988,37948,28093,3404,25137,62868,43489,64137,23163,94693,8889,1982,39377,65320,49464,27444,53609,96715,66633,66277,90761,1471,7529,51692,82414,71858,9344,9007,95592,82186,86359,96054,98292,6282,70460,65300,50934,31521,81783,17446,36967,30980,46272,44819,14609,79446,91125,85613,8485,86801,49954,81437,69870,91839,50534,64685,75560,26370,23574,58402,38960,97159,42990,86147,89388,24313,80726,42870,24433,85079,78798,65103,71489,46774,83706,22632,17492,82214,48287,85186,60324,66236,73017,23151,55231,86695,43171,11369,45761,3294,16332,31695,20236,57332,33078,93324,91274,33178,93681,31469,18017,8758,49086,84807,20503,91696,33401,80204,2027,58905,22564,48672,72237,41708,4820,89762,89833,81021,30432,98294,52831,45108,1724,53221,23749,11644,17332,56847,66532,87295,11342,50212,3791,34199,6346,64123,72194,58630,42991,78379,98610,75301,93410,5058,63500,13952,3598,56433,5713,10828,69280,59912,28756,67064,78931,11755,12846,66241,27994,35521,32203,99812,48558,51861,93130,9368,98397,50861,47602,7363,1534,71204,75147,82733,75729,33866,8084,81974,72039,190,3231,29830,51736,13932,53042,23862,70502,88324,92200,82747,5623,18990,13302,70785,30816,90733,43034,53582,88792,94597,94924,23010,76855,21088,7425,79684,60240,45720,75948,6310,90507,78517,87281,6622,57371,72306,23622,28677,93614,43461,82175,47981,41347,40731,70194,11151,20851,29488,39821,4149,86597,2059,45248,48529,80758,45727,11795,95681,39315,95636,22757,6539,62985,13900,35761,93164,35794,41797,31444,65942,76285,46855,96036,30052,13843,24360,76303,79012,57362,56059,81862,28447,43266,12023,10396,57637,81375,10852,14714,60146,73922,6856,1785,64656,73146,92757,1660,7377,72963,31505,28012,22532,96845,1064,91179,17986,86865,30156,76985,98518,57823,16007,95697,40220,25813,37786,18488,13338,49783,37363,21116,19537,38601,42822,22904,30720,77375,38338,11931,10319,38512,7475,5331,89689,32836,55257,67069,85815,91249,4273,81748,78988,75628,56997,37352,80884,5620,20344,5254,49646,31597,29322,93243,68018,99413,88394,71972,91716,90605,15458,8769,39116,36233,35856,52539,62904,20971,74051,80561,83517,79973,47723,58604,97346,89521,19781,58178,6383,42809,81822,11608,18761,88087,29803,62930,805,93508,35611,19481,62321,11265,3966,739,40081,87846,22192,33440,1011,38082,59067,22091,31953,30192,79634,76337,71016,59567,8021,6456,69090,66224,85935,85526,94615,78609,81236,52934,67692,74479,82563,42276,84732,61712,5310,99644,89566,36042,65376,75075,6539,65379,6774,79564,97804,63176,63994,79060,19171,94820,68386,89961,84645,52183,96997,17911,64641,47079,46470,90231,84379,12443,44947,33856,56554,17423,37760,8221,18298,36364,77651,37237,68127,18095,42161,2960,95619,40691,93146,5678,55545,45140,85451,311,28305,36538,60867,78120,3588,54611,13112,69718,24756,28151,29771,58935,82889,988,34794,3599,32758,48820,21781,74704,23793,69992,65911,35758,55713,86581,90468,48722,46497,28920,16020,89610,71054,38000,4933,55471,93888,67478,87460,46330,16794,78934,50399,33750,72956,80098,1682,34507,95394,55011,72190,97165,32846,79025,45108,90065,74997,35699,73249,31910,80802,8387,52127,9404,29450,32875,56377,47772,97701,19550,47109,41392,15958,2491,2428,62181,7486,67337,47420,56161,98277,59192,22722,59724,39471,20427,67073,49242,4194,73226,48730,80202,25367,85936,99528,57947,87860,49362,70854,2221,26087,1498,15935,91485,4489,11632,25213,16462,28105,90506,29743,10818,32795,40560,18844,75882,86115,64247,37092,2668,188,59257,9131,63768,96643,37305,91081,21861,80217,95851,19498,28296,93613,84314,63337,38032,92213,82268,17178,33924,63261,65485,19762,88274,75025,81390,88677,31225,78136,77784,66480,82768,85408,4385,87286,8737,27235,73061,58204,16026,3949,26446,10956,43725,94901,67413,54200,4538,63033,4770,8531,40086,61481,54337,96808,28254,27220,37254,85840,9319,60208,40320,54832,30543,49972,64251,30244,46594,62129,28012]
Case 33
height = [51,47,38,14,0,32,71,20,98,48,65,34,45,14,73,11,21,9,76,92,66,42,97,58,96,80,73,60,99,61,9,19,80,93,71,0,50,35,64,9,71,79,21,79,50,14,7,47,84,74,97,58,68,33,24,29,46,41,68,24,28,37,56,83,88,73,38,48,41,38,1,76,79,21,71,44,88,3,88,0,83,83,74,41,80,72,60,29,34,23,32,75,60,30,10,63,75,2,62,94,70,17,76,42,66,87,63,85,68,91,55,31,52,30,84,40,13,71,50,56,91,39,74,64,72,16,38,77,76,21,26,29,88,8,48,22,78,43,21,35,97,25,23,77,97,99,94,1,27,65,42,17,81,21,63,39,68,57,16,56,56,49,29,61,45,37,69,95,54,10,67,24,5,68,96,42,29,95,8,4,8,28,27,95,83,15,65,49,36,29,82,90,71,90,89,1,38,79,74,28,37,28,61,38,57,62,78,12,56,71,99,92,13,55,21,44,86,55,14,49,3,71,79,73,47,65,93,10,23,47,77,75,16,97,49,68,91,6,62,82,18,46,19,24,1,12,39,50,1,30,18,40,82,6,41,26,24,67,44,96,89,89,9,3,41,89,78,68,30,76,91,22,17,86,58,66,58,53,37,93,69,36,78,67,64,58,33,6,90,47,60,76,17,37,95,71,38,6,41,99,23,2,56,18,6,42,26,66,76,75,57,40,38,73,40,65,87,2,98,47,9,12,66,1,74,52,88,52,87,44,10,11,71,94,7,62,95,0,49,47,48,74,4,5,24,20,76,68,47,29,55,43,33,11,86,99,35,32,60,65,97,51,56,50,48,64,46,43,7,0,93,76,44,72,51,83,23,65,45,82,88,58,53,75,5,63,65,55,50,93,45,89,12,67,92,58,27,26,92,52,86,75,21,26,75,8,76,93,65,68,72,77,33,62,31,0,68,97,71,96,41,61,24,99,16,44,84,90,7,41,5,98,42,69,92,0,3,26,25,47,83,80,92,34,72,31,31,46,65,67,16,29,28,43,0,12,55,97,74,31,28,81,60,90,23,13,38,76,3,92,71,44,52,39,47,46,10,0,15,13,18,64,19,43,74,38,59,37,6,42,56,94,87,4,13,24,21,4,55,81,24,60,58,78,6,3,80,68,38,34,1,81,47,10,81,87,59,2,73,34,59,61,96,13,38,66,28,26,88,17,33,92,57,28,95,39,7,52,24,9,46,90,8,42,88,15,20,91,22,51,67,40,7,82,19,84,29,5,67,67,85,37,93,89,56,14,4,94,26,77,47,74,51,71,53,15,80,50,96,37,41,77,40,52,72,67,3,1,86,1,91,68,32,7,41,85,87,47,59,24,44,6,23,12,45,99,8,37,94,81,79,1,71,48,15,45,64,98,73,32,13,21,79,4,21,24,56,20,27,32,86,44,3,34,61,63,26,92,58,30,94,71,51,0,26,3,54,87,97,42,73,96,66,10,86,21,68,67,52,20,89,86,68,91,73,48,32,19,69,27,17,60,64,1,69,98,41,38,26,23,69,14,50,88,53,44,80,96,4,17,24,66,98,89,55,77,85,83,5,40,86,69,79,88,82,81,50,3,99,91,90,62,17,98,57,65,28,50,42,37,55,59,14,29,0,42,34,54,64,76,24,6,23,4,67,99,95,54,50,90,5,81,67,72,79,96,48,91,88,76,0,48,94,41,86,8,0,2,50,66,4,45,3,36,16,33,21,47,55,38,14,29,5,72,2,97,2,28,62,40,0,3,86,11,34,67,26,76,14,93,50,81,41,39,88,64,18,28,90,97,88,55,1,24,57,84,47,49,82,10,6,10,49,73,42,57,10,93,40,76,48,34,97,8,71,68,70,64,26,95,73,69,95,9,29,62,10,64,86,89,88,35,38,81,12,0,93,61,19,66,33,32,22,67,16,10,13,24,2,34,30,25,73,2,79,38,34,85,60,57,88,89,65,48,45,47,2,39,34,63,52,29,56,57,9,80,33,82,58,93,57,70,15,62,41,36,99,86,36,7,17,99,85,81,5,79,16,35,32,59,3,43,63,7,12,92,42,53,98,78,37,22,84,74,36,64,26,31,81,24,8,66,37,18,61,4,46,74,27,83,87,12,68,42,83,17,22,30,98,80,78,42,69,35,99,94,19,40,37,95,61,29,63,26,91,54,81,9,86,68,47,8,96,71,77,36,90,31,87,84,72,27,39,42,41,18,72,21,56,29,66,81,88,2,89,62,27,37,7,30,62,52,15,82,88,41,13,96,66,12,7,78,96,55,76,35,66,21,52,30,5,63,7,57,13,55,71,52,98,89,63,11,40,1,52,48,24,19,23,20,35,44,67,53,29,14,18,89,1,84,72,27,50,51,73,39,68,88,42,80,30,85,75,69,31,14,65,89,44,50,38,23,7,62,88,58,36,37,18,27,34,9,65,11,16,62,3,35,15,7,21,30,89,18,42,59,97,78,69,24,56,56,71,91,50,51,77,17,76,12,83,71,29,13,2,27,36,43,61,34,32,10,7,12,30,17,2,58,89,20,56,62,47,59,6,46,80,30,79,9,64,51,46,92,14,6,4,6,7,69,91,24,14,66,39,90,33,8,48,48,55,11,73,25,19,4,59,61,73,43,56,87,66,13,11,84,60,62,92,74,69,5,85,16,28,27,13,34,71,51,38,60,55,96,71,72,78,78,9,47,31,62,25,60,60,16,16,33,94,92,30,55,98,9,26,85,2,44,25,3,63,46,28,28,73,61,17,76,52,44,53,18,46,12,72,71,2,97,71,80,97,77,94,40,59,32,30,76,70,74,4,32,53,33,93,86,43,71,42,95,92,84,20,32,89,29,24,73,53,40,1,84,13,97,27,99,58,71,60,57,53,64,67,28,9,74,93,29,72,19,83,96,53,80,75,81,89,8,52,64,69,14,11,52,63,77,82,93,81,32,9,69,49,52,33,66,95,37,52,2,35,29,11,99,24,72,93,77,66,82,16,99,29,14,47,55,48,4,80,75,89,22,74,86,14,69,21,28,10,51,30,64,76,97,51,64,40,69,19,1,92,76,44,38,77,34,68,32,13,4,80,91,42,78,79,78,12,47,62,71,42,14,14,81,72,79,78,95,37,82,86,77,75,32,24,61,95,23,97,66,58,1,10,7,39,22,50,59,25,16,57,62,83,62,4,29,59,15,24,53,60,13,32,55,57,97,17,63,90,5,50,93,79,21,80,51,59,76,3,22,18,21,74,90,80,50,31,94,36,32,87,39,17,90,52,96,11,72,15,66,52,79,72,76,38,0,43,94,95,9,24,3,36,84,95,26,75,79,10,91,75,92,3,96,6,21,76,28,45,15,8,30,8,38,66,53,68,51,17,16,76,7,21,0,2,99,37,87,44,28,5,61,75,66,56,76,93,64,43,72,67,12,52,43,96,18,82,12,17,10,98,53,94,73,67,65,99,26,8,75,81,96,57,2,2,72,81,41,23,30,82,17,62,2,65,61,42,47,6,72,34,18,69,60,48,9,57,51,40,1,5,46,17,53,36,93,49,67,25,3,12,59,8,4,96,45,22,95,76,11,4,44,25,29,50,59,69,94,3,14,49,8,50,37,99,59,86,61,66,42,26,7,24,41,96,77,43,61,21,69,34,71,62,11,79,88,36,68,41,70,48,27,38,65,1,77,38,24,67,57,36,0,7,64,58,76,38,69,36,97,97,55,89,18,85,20,59,36,84,55,45,50,27,58,47,62,80,70,67,68,4,2,18,4,12,79,58,3,52,86,91,78,98,3,92,8,58,19,78,86,76,93,31,71,43,1,9,84,34,38,41,11,95,20,2,35,80,9,7,99,92,2,11,49,83,69,92,83,26,93,5,46,73,42,91,66,15,66,54,1,58,79,14,83,98,70,82,4,88,82,9,66,47,40,0,32,39,77,60,94,82,92,38,53,41,14,9,47,24,36,47,40,37,51,24,36,64,89,41,44,43,38,34,52,1,82,75,97,66,78,3,28,94,85,4,59,60,36,30,84,66,20,65,51,95,64,64,27,85,0,91,79,44,6,26,94,77,92,42,31,39,56,33,10,48,40,23,97,20,3,99,20,1,28,75,24,28,92,10,13,71,3,47,73,21,37,58,68,18,62,17,20,35,35,93,16,73,46,68,4,79,35,19,4,85,41,2,9,5,77,60,12,10,23,17,66,8,5,37,35,47,93,27,82,17,8,48,87,32,37,26,94,27,21,6,22,86,97,39,17,73,65,55,99,66,43,34,67,80,92,73,12,63,40,7,22,21,28,2,68,41,76,77,50,29,49,58,54,45,9,79,32,12,72,78,43,55,29,64,83,65,22,71,63,13,60,55,51,2,75,0,55,99,48,14,49,43,65,7,59,6,16,10,7,76,42,80,52,65,91,23,84,33,34,40,76,97,24,88,48,16,73,48,93,50,15,92,61,11,3,87,75,94,14,4,34,93,14,42,70,32,80,33,77,98,32,12,77,65,17,89,26,60,21,54,21,86,78,74,82,59,12,30,87,94,47,89,80,91,9,26,30,86,84,61,72,92,37,75,63,37,64,33,72,52,82,5,63,82,63,1,71,86,14,86,45,16,31,82,81,52,62,47,76,41,36,62,46,74,29,45,92,35,59,62,48,8,28,27,37,61,77,8,59,38,98,19,48,26,58,50,30,94,27,33,58,0,62,57,39,98,45,56,29,32,80,83,91,16,52,69,72,19,95,30,81,30,17,46,75,30,92,80,38,97,82,97,83,66,10,13,7,8,49,57,0,61,8,96,32,97,64,46,93,40,27,89,92,21,98,21,77,5,72,17,26,29,7,30,69,83,71,42,82,84,53,86,15,15,14,55,88,44,61,78,62,2,84,72,52,81,7,40,81,17,53,44,65,80,59,80,87,40,42,50,51,50,59,65,71,10,29,67,71,68,94,92,5,96,71,43,36,96,11,70,54,97,81,27,85,36,73,31,52,51,47,46,89,82,1,12,70,86,91,6,54,31,62,36,78,92,37,50,24,35,54,3,4,74,29,70,64,79,40,43,78,66,22,51,1,88,2,15,81,61,57,99,55,7,51,72,24,84,19,92,41,8,2,15,62,99,3,52,36,67,90,84,5,48,31,70,80,25,47,93,53,4,74,34,90,63,84,47,44,40,74,90,5,3,51,30,99,71,26,48,46,75,28,22,13,49,38,53,18,51,53,53,93,86,82,34,33,71,13,61,33,28,77,33,32,18,40,7,8,57,81,20,54,55,3,58,82,40,28,9,47,65,92,60,76,18,5,65,6,27,77,62,83,1,58,38,78,78,64,32,22,9,93,50,26,5,56,26,58,21,81,75,75,1,66,30,57,8,30,85,78,0,21,12,31,65,61,91,95,58,20,59,36,12,84,27,73,63,16,52,63,41,87,73,91,2,71,23,92,23,53,34,19,20,48,64,52,62,31,53,27,95,83,44,38,38,73,72,74,99,69,89,53,22,40,3,44,79,99,81,94,55,32,7,32,74,45,79,21,73,37,89,29,53,37,41,3,70,31,33,89,2,68,87,43,55,22,95,68,70,70,18,16,19,74,98,96,39,45,23,90,96,42,79,77,78,77,50,73,49,60,96,64,56,87,54,94,2,64,62,41,90,50,51,92,48,6,23,4,60,91,66,56,77,86,72,28,77,45,34,21,5,75,1,45,34,24,90,90,64,49,35,21,30,7,79,11,55,25,96,77,30,26,20,70,0,57,66,90,84,98,86,50,2,6,96,41,82,24,30,27,26,12,76,84,8,32,84,31,72,60,10,54,60,3,57,83,55,7,31,92,6,41,9,97,75,8,68,99,1,94,31,67,53,11,99,16,6,75,35,75,1,8,83,53,31,26,32,34,85,63,84,0,13,35,34,8,90,46,33,11,87,99,54,62,90,85,25,37,24,98,47,91,78,12,76,23,58,83,32,15,72,81,83,60,12,97,56,55,70,12,58,52,9,49,69,16,45,51,34,27,72,93,42,23,5,77,9,93,38,15,7,93,26,8,80,96,44,68,8,6,88,33,1,83,91,24,1,22,54,25,99,13,22,38,19,91,72,77,53,51,48,3,6,84,0,0,14,82,87,27,62,26,36,90,5,82,99,11,31,31,50,78,86,83,64,86,23,17,46,84,25,99,91,88,80,40,52,49,61,89,49,46,13,89,23,46,55,31,6,82,25,85,16,64,34,47,88,43,68,30,70,9,69,27,93,2,20,23,5,56,17,97,51,44,95,49,90,39,20,62,48,97,28,92,82,85,56,25,11,81,33,82,16,6,46,47,71,93,35,42,79,20,49,70,28,53,92,15,8,1,0,23,69,55,66,59,59,80,28,27,43,29,93,67,55,81,45,44,35,29,97,16,38,48,72,3,34,2,6,38,0,31,70,58,32,67,25,83,0,52,85,98,57,40,99,32,30,23,18,15,37,26,26,66,70,74,45,6,57,40,15,32,30,25,39,11,39,69,79,13,41,61,9,52,90,39,70,97,66,34,14,47,29,36,87,78,10,42,61,43,27,20,20,70,85,17,24,44,13,29,10,20,82,12,88,91,13,31,57,38,31,96,54,75,67,27,62,35,69,60,69,65,85,84,26,38,41,25,70,26,60,52,60,96,40,42,41,19,28,86,36,50,78,44,26,53,31,98,27,66,34,73,11,35,45,16,98,18,20,26,29,87,9,69,87,38,47,3,38,55,78,77,29,64,36,18,21,7,69,93,90,19,26,74,8,22,40,88,59,21,38,40,18,47,39,37,86,62,70,86,89,97,21,3,88,3,34,70,2,87,43,2,86,13,29,63,75,48,31,37,3,57,12,68,10,36,94,78,77,77,49,60,60,88,10,44,0,12,25,80,53,80,83,72,54,10,99,73,70,83,18,31,32,81,91,29,19,25,45,98,15,68,76,31,27,82,5,32,94,18,37,55,57,61,77,80,96,90,12,55,19,78,55,68,87,17,48,92,4,73,13,80,9,27,75,95,67,56,59,45,15,56,91,21,92,1,39,65,34,29,89,16,31,40,6,11,34,52,13,43,55,68,39,15,3,29,86,62,65,24,66,56,19,55,28,66,24,11,39,5,53,51,76,32,87,30,3,45,34,4,67,77,86,73,92,82,75,44,0,18,33,94,53,19,70,5,5,47,33,67,13,86,33,19,42,89,53,58,48,56,78,9,52,2,28,83,26,25,58,49,2,81,27,19,17,52,91,44,72,99,20,90,54,97,97,59,65,66,3,48,18,27,92,64,95,18,82,91,20,0,31,10,82,1,39,58,59,77,40,89,28,83,14,59,54,75,24,5,97,25,63,5,34,19,46,72,11,33,63,16,68,89,99,81,95,29,30,19,40,98,66,86,76,75,67,22,46,33,40,40,37,31,32,18,52,8,87,82,12,51,41,27,50,96,83,11,77,17,55,87,9,31,98,44,86,95,59,55,38,14,90,55,79,30,67,4,71,60,12,56,75,28,8,25,22,94,45,76,42,42,14,0,8,16,36,41,60,89,19,42,89,50,39,20,0,56,58,92,59,45,24,5,78,35,86,91,19,90,88,83,89,7,4,43,37,82,52,37,49,62,60,57,23,37,54,6,4,70,50,19,7,38,42,81,31,93,82,33,95,94,76,20,23,47,94,65,11,22,14,54,66,90,84,61,35,8,68,7,59,4,48,34,34,34,95,91,79,49,22,34,29,32,21,26,74,81,48,12,3,27,42,9,96,33,49,50,29,62,21,70,46,22,47,34,59,10,43,75,28,51,38,39,12,18,22,52,44,15,82,43,93,34,84,70,66,15,81,31,21,66,66,36,98,16,76,57,66,69,32,55,84,64,53,89,39,47,70,33,51,87,76,82,41,53,99,93,92,22,67,74,62,96,37,6,54,17,97,72,58,44,67,67,99,41,37,66,12,68,74,6,39,30,73,10,92,33,81,68,51,46,30,38,42,40,84,96,16,33,50,99,44,15,12,31,19,54,13,35,58,80,6,70,76,93,31,68,53,5,8,56,3,61,42,28,77,34,6,26,44,48,96,75,17,33,45,33,98,63,70,72,20,82,82,91,98,29,91,36,17,87,46,24,27,67,85,74,62,0,75,97,15,56,84,19,24,60,88,11,79,77,17,74,75,55,25,95,84,83,25,16,71,43,68,86,79,59,60,23,41,50,30,92,64,82,69,15,63,62,91,24,29,56,33,47,66,70,18,37,28,24,77,99,25,22,25,66,57,72,78,59,88,24,87,51,39,55,13,6,55,21,67,54,36,14,93,73,3,37,13,11,51,26,11,31,77,21,33,77,99,78,19,73,25,68,69,90,25,39,69,92,39,91,68,77,82,61,74,50,78,17,27,16,15,29,24,66,85,16,85,40,81,39,72,63,84,0,0,66,47,93,54,55,83,53,32,75,30,20,13,73,83,28,52,13,2,75,55,29,74,72,56,89,18,97,54,26,52,74,48,51,97,21,93,93,31,69,32,15,89,17,39,83,94,1,16,41,57,17,53,86,11,30,30,24,18,5,74,8,61,89,97,82,57,2,9,27,89,7,24,65,76,14,85,21,20,19,19,5,34,5,85,25,95,17,93,20,1,23,91,34,70,99,79,24,51,8,43,40,41,85,22,72,56,54,81,33,26,14,80,87,95,95,53,29,38,52,83,45,12,85,8,0,90,96,73,82,88,42,67,76,37,82,23,55,16,62,49,46,17,77,89,24,12,12,53,67,7,28,90,2,10,5,51,94,74,23,23,36,53,84,8,77,48,14,37,20,82,44,65,95,83,84,97,39,66,16,76,88,24,61,10,40,86,89,26,84,11,18,75,73,44,69,79,30,4,71,33,67,43,7,33,38,3,37,29,30,84,23,11,5,60,99,24,10,28,44,68,76,57,75,71,31,15,26,17,49,48,17,23,67,27,71,5,6,6,96,16,23,38,30,27,63,60,60,7,44,96,6,25,65,96,50,45,57,7,85,22,56,85,41,29,84,25,62]