Largest Rectangle in Histogram

Hard

You are given an array of integers heights, where heights[i] is the height of a bar in a histogram. Every bar is 1 wide, and the bars stand side by side with no gaps.

Return the area of the largest rectangle that fits inside the histogram.

Examples

Example 1

Input:  heights = [2,1,5,6,2,3]
Output: 10
Example 1: the largest rectangle215623height 5width 2area 10

The bars of height 5 and 6 hold a rectangle that is 5 tall and 2 wide.

Example 2

Input:  heights = [2,4]
Output: 4
Example 2: two rectangles with area 4244 × 1 = 4242 × 2 = 4

The 4 bar on its own gives 4 × 1, and both bars at height 2 give 2 × 2. Both are 4.

Example 3

Input:  heights = [6,2,5,4,5,1,6]
Output: 12
Example 3: the largest rectangle6254516height 4width 3area 12

The three bars 5, 4, 5 hold a rectangle that is 4 tall and 3 wide.

Example 4

Input:  heights = [4,4,4,4]
Output: 16

Example 5

Input:  heights = [5,4,1,2]
Output: 8
Example 5: the short bar cuts it off5412height 4width 2area 8

The first two bars give 4 × 2. Any rectangle that crosses the 1 bar can be at most 1 tall.

Constraints

  • 1 <= heights.length <= 10⁵
  • 0 <= heights[i] <= 10⁴
TypeScript
Loading editor…
Test cases35
Case 1
heights = [2,1,5,6,2,3]
Case 2
heights = [2,4]
Case 3
heights = [6,2,5,4,5,1,6]
Case 4
heights = [4,4,4,4]
Case 5
heights = [5,4,1,2]
Case 6
heights = [0]
Case 7
heights = [1]
Case 8
heights = [10000]
Case 9
heights = [0,9]
Case 10
heights = [9,0]
Case 11
heights = [0,0,0]
Case 12
heights = [1,2,3,4,5]
Case 13
heights = [5,4,3,2,1]
Case 14
heights = [7,1,7,2,2,4]
Case 15
heights = [1,3,7]
Case 16
heights = [2,1,2]
Case 17
heights = [3,6,5,7,4,8,1,0]
Case 18
heights = [4,2,0,3,2,5]
Case 19
heights = [1,1,1,10,1,1,1]
Case 20
heights = [2,3,3,3,2,1,6,6]
Case 21
heights = [5,0,0,10,8,2,9,5]
Case 22
heights = [2,7,8,8,7,7,4,10,7,6]
Case 23
heights = [7,10,8,9,4,6,3,8,0,7,4,1]
Case 24
heights = [8,3,10,8,2,5,6,6,10,9,6,10,2,4,10]
Case 25
heights = [3,3,10,8,4,1,2,2,7,8,1,4,6,1,0,4,4,0,8,8]
Case 26
heights = [5541,2991,4380,3102,910,397,7614,1939,8574,5600,8381,900,5719,8476,5084,1932,5365,3791,9753,3520,2300,9909,2457,7836,3526,4558,8742,7009,69,4142]
Case 27
heights = [8946,9270,6342,2098,4571,2816,7764,1946,5227,3882,1604,6456,2896,1374,1710,5473,2664,946,2537,851,4337,9509,2982,4449,889,7289,6841,4262,631,7702,4594,6690,3066,7763,9683,9254,6844,4099,7497,8487,9822,7780,6779,7675,2989,1491,7706,662,9486,8382]
Case 28
heights = [1344,4380,5129,167,8311,6386,7352,549,6825,2090,3939,3944,3110,1783,517,8764,3251,2532,3260,5420,7358,1803,5249,4256,3618,7444,1300,6044,9784,1642,4591,6233,3028,8932,3839,3381,9710,47,5199,4373,9407,1575,8842,9619,8518,860,1089,4796,8857,7784,177,6631,5051,5167,5864,3533,7736,6273,3987,4271,4381,2313,179,1841,5147,2428,1723,6793,6618,994,5289,3797,7998,3889,9201,7610,4659,3379,5581,6612,7168,196,5227,3542,7694,7261,1783,8503,3380,429,7479,3469,5576,9998,4669,5304,9419,1108,7878,5300]
Case 29
heights = [9260,854,4058,1972,1194,1576,5273,4701,9263,3469,690,240,5537,5975,855,7741,59,1795,4663,8919,2092,4584,3780,1924,8808,317,706,3442,6553,2559,8109,7432,6030,5874,1591,4412,3256,4181,7276,6536,6784,9862,8519,6358,2499,7966,792,6284,5382,2517,3972,8834,4793,1279,4274,8194,7150,6173,124,5363,3461,8043,8666,5267,9922,3721,5676,3543,5811,3673,6711,4916,5900,5134,8224,7255,3033,5932,1187,9239,7373,3272,9909,217,1676,436,2071,1495,607,3166,4172,8381,9666,9292,8956,864,1817,7855,3437,4992,1695,1385,1494,7748,9829,3781,8617,367,107,481,9774,8092,320,6841,4044,1049,4561,6607,5750,9752,8608,5646,1582,6113,9198,2679,5265,8638,1490,2494,3680,4257,7703,21,1920,1331,5720,2586,1857,5920,7336,1819,3269,1509,5469,3027,9324,6638,7972,9223,8751,6794,1084,4405,6641,4281,6315,1603,8640,3114,8012,64,5964,7800,1980,9114,7571,6275,5944,1006,6891,5808,8995,9999,3732,6838,8095,9251,4451,5351,9268,5485,1989,6833,7925,7636,6368,8869,995,8420,9306,205,9030,1070,5185,1242,771,7147,6706,9897]
Case 30
heights = [2,0,2,1,3,2,2,1,3,3,3,0,0,3,1,3,3,1,1,1,3,3,1,0,0,3,1,0,2,1,1,2,3,3,2,3,3,2,0,1,2,3,2,2,2,1,0,0,3,1,3,0,0,1,3,2,0,1,0,0,3,3,3,1,1,1,2,3,1,1,2,1,2,2,2,2,0,1,0,3,1,3,2,2,1,2,2,0,1,2,2,2,3,3,2,1,2,3,1,0,0,3,0,3,3,2,3,2,3,3,1,3,2,2,3,1,1,1,2,0,2,0,1,0,0,0,1,2,2,0,3,0,2,1,1,2,3,3,0,2,0,2,2,1,0,3,3,3,1,0,2,2,1,1,2,3,3,0,0,3,2,2,0,1,0,1,0,3,0,3,2,1,2,0,0,3,0,2,3,0,1,3,3,1,1,0,0,0,2,2,0,0,1,1,1,3,0,3,2,1,0,3,3,0,3,2,3,3,3,2,2,1,1,0,3,3,3,2,2,0,2,3,0,3,1,3,0,2,3,3,2,0,2,1,0,3,3,0,3,0,0,3,2,0,1,2,2,2,3,2,0,2,1,0,0,3,1,1,1,1,2,3,1,2,0,1,0,2,2,3,1,3,0,3,2,2,0,3,0,3,2,1,1,1,2,3,0,1,1,0,2,2,0,1,1,3,3,0,0,2]
Case 31
heights = [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,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,362,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,378,379,380,381,382,383,384,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400]
Case 32
heights = [10000,9999,9998,9997,9996,9995,9994,9993,9992,9991,9990,9989,9988,9987,9986,9985,9984,9983,9982,9981,9980,9979,9978,9977,9976,9975,9974,9973,9972,9971,9970,9969,9968,9967,9966,9965,9964,9963,9962,9961,9960,9959,9958,9957,9956,9955,9954,9953,9952,9951,9950,9949,9948,9947,9946,9945,9944,9943,9942,9941,9940,9939,9938,9937,9936,9935,9934,9933,9932,9931,9930,9929,9928,9927,9926,9925,9924,9923,9922,9921,9920,9919,9918,9917,9916,9915,9914,9913,9912,9911,9910,9909,9908,9907,9906,9905,9904,9903,9902,9901,9900,9899,9898,9897,9896,9895,9894,9893,9892,9891,9890,9889,9888,9887,9886,9885,9884,9883,9882,9881,9880,9879,9878,9877,9876,9875,9874,9873,9872,9871,9870,9869,9868,9867,9866,9865,9864,9863,9862,9861,9860,9859,9858,9857,9856,9855,9854,9853,9852,9851,9850,9849,9848,9847,9846,9845,9844,9843,9842,9841,9840,9839,9838,9837,9836,9835,9834,9833,9832,9831,9830,9829,9828,9827,9826,9825,9824,9823,9822,9821,9820,9819,9818,9817,9816,9815,9814,9813,9812,9811,9810,9809,9808,9807,9806,9805,9804,9803,9802,9801,9800,9799,9798,9797,9796,9795,9794,9793,9792,9791,9790,9789,9788,9787,9786,9785,9784,9783,9782,9781,9780,9779,9778,9777,9776,9775,9774,9773,9772,9771,9770,9769,9768,9767,9766,9765,9764,9763,9762,9761,9760,9759,9758,9757,9756,9755,9754,9753,9752,9751,9750,9749,9748,9747,9746,9745,9744,9743,9742,9741,9740,9739,9738,9737,9736,9735,9734,9733,9732,9731,9730,9729,9728,9727,9726,9725,9724,9723,9722,9721,9720,9719,9718,9717,9716,9715,9714,9713,9712,9711,9710,9709,9708,9707,9706,9705,9704,9703,9702,9701,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
Case 33
heights = [89,10,25,77,21,49,25,73,17,29,3,37,62,89,40,62,48,54,81,26,72,38,17,29,25,51,85,78,77,91,48,68,66,99,49,99,13,58,44,59,62,96,0,25,4,72,20,93,9,66,45,54,40,70,25,44,10,54,66,95,84,53,13,24,11,55,32,78,4,86,20,58,49,64,97,5,44,97,48,26,38,21,92,76,89,69,29,36,9,20,50,90,84,84,72,19,7,10,97,10,25,29,49,87,46,21,62,57,60,36,26,10,44,13,3,95,46,43,51,27,82,24,34,36,8,27,31,2,48,60,22,52,87,6,79,68,85,30,29,57,36,73,97,60,63,30,36,87,33,99,53,61,0,91,37,81,65,1,50,31,3,67,71,1,38,88,67,4,66,66,52,34,44,55,41,82,50,23,14,87,0,8,75,74,49,46,32,46,20,39,6,28,41,81,63,9,68,43,59,12,82,52,95,25,91,35,37,10,83,69,84,61,45,48,59,42,9,92,35,95,67,43,37,87,9,14,82,17,47,37,67,31,4,70,34,33,32,81,70,59,46,39,31,59,17,83,52,13,27,41,81,82,73,14,77,43,0,96,25,24,21,61,9,92,17,70,48,33,16,9,73,30,42,68,33,91,37,61,14,98,1,86,28,26,60,54,88,77,43,22,1,12,36,41,1,66,63,10,12,46,73,98,53,56,56,98,14,84,37,29,6,23,28,62,34,2,81,96,77,33,8,1,21,56,26,43,63,5,46,77,70,67,61,51,51,6,78,19,16,42,41,59,50,69,4,52,72,8,35,28,86,55,61,98,97,3,74,63,40,43,32,53,64,51,74,2,48,64,73,95,38,98,30,87,46,25,92,76,88,52,15,48,68,43,29,24,72,96,75,71,66,65,58,9,95,64,92,75,72,97,70,41,69,0,30,52,4,73,23,53,87,26,81,0,87,11,76,51,44,78,72,84,39,13,58,94,85,22,76,36,51,85,91,53,2,7,16,60,35,97,18,79,43,98,94,14,4,81,92,22,40,71,75,34,64,23,13,54,26,94,82,82,5,33,22,95,52,82,13,60,93,75,23,51,98,99,77,70,82,19,39,91,87,67,16,11,87,92,8,77,99,41,42,8,74,1,80,5,88,5,25,90,81,31,18,3,56,27,4,44,35,24,7,24,42,57,30,58,43,14,27,79,89,79,6,92,35,6,95,25,70,27,3,79,96,0,11,41,74,13,13,55,95,98,61,12,37,99,59,91,13,35,51,98,55,6,67,88,94,96,53,13,91,24,63,75,76,20,87,50,11,15,0,93,43,69,71,37,69,32,97,7,80,92,85,43,3,40,30,57,55,61,38,98,40,7,11,23,44,64,32,84,29,83,32,34,10,55,73,44,47,8,65,75,39,59,41,27,12,25,10,20,14,19,92,60,1,33,34,52,62,87,91,29,14,61,48,24,3,88,89,37,6,45,15,81,84,90,44,65,67,47,70,52,71,82,93,40,7,39,95,38,87,87,39,89,74,30,30,58,51,66,18,60,4,7,44,79,51,72,91,95,33,99,48,51,49,32,81,57,35,28,53,22,78,14,3,13,92,47,20,89,47,9,38,24,19,65,78,78,97,82,21,74,64,67,21,63,46,51,72,45,15,54,12,87,40,21,49,82,83,87,18,73,37,28,19,62,82,56,67,86,35,7,93,65,20,8,25,39,90,6,23,76,2,90,93,88,7,8,59,38,54,43,19,35,1,71,64,76,90,84,85,94,2,83,62,6,42,12,79,50,3,86,34,42,47,88,90,68,21,92,34,16,24,21,23,73,52,16,62,49,54,33,8,25,92,39,96,57,1,38,16,53,4,99,3,50,80,42,90,74,9,36,49,86,37,62,16,30,47,27,45,15,65,80,12,29,79,92,88,71,22,27,82,98,50,69,96,31,92,90,41,65,44,24,66,17,3,60,31,1,34,70,20,66,71,13,72,74,93,76,53,81,77,63,62,50,58,88,20,99,96,56,14,48,6,22,50,85,50,90,4,27,0,17,2,60,21,65,97,35,48,20,80,4,34,45,14,44,96,66,6,68,10,39,38,16,6,73,27,11,48,84,93,53,4,45,27,91,45,81,96,3,82,27,66,77,16,43,81,10,76,43,42,24,71,93,84,39,1,45,64,58,18,13,5,71,35,55,65,60,45,4,62,63,0,30,50,72,68,9,51,87,72,50,36,76,7,85,30,76,62,86,8,55,5,13,94,10,26,15,96,0,72,12,42,75,17,21,3,74,88,63,9,97,90,93,10,26,91,12,13,35,35,20,3,5,25,42,40,49,58,11,82,87,4,76,37,0,92,20,96,32,99,67,12,41,2,66,76,60,75,53,23,38,68,40,10,60,73,81,18,38,53,14,99,67,37,87,32,20,17,78,26,24,22,91,88,0,75,44,46,32,67,83,49,44,86,17,2,33,3,63,44,53,4,27,29,59,78,89,90,40,44,91,8,9,20,61,7,72,36,98,66,32,55,70,47,61,65,29,16,58,70,41,93,43,28,12,26,11,42,83,41,16,89,62,33,73,2,67,91,86,48,8,10,3,60,52,43,81,82,31,42,97,30,57,87,63,40,45,79,83,10,32,89,20,76,89,9,55,35,67,47,43,83,87,30,74,39,37,77,78,20,3,9,42,16,67,87,53,68,44,36,64,55,15,54,8,97,63,36,84,65,1,5,42,41,86,51,12,2,52,0,1,41,98,97,73,82,4,51,69,51,21,68,87,8,87,18,49,94,42,79,30,98,63,84,88,46,68,69,43,32,3,59,59,29,20,7,24,12,57,97,15,53,79,29,16,67,62,15,21,95,69,5,22,56,22,93,6,70,98,85,11,65,9,73,20,81,97,9,77,58,42,73,55,11,21,80,73,98,56,54,28,0,75,88,84,28,86,79,4,14,88,91,5,97,25,60,74,78,17,90,22,37,19,37,84,97,70,72,95,21,15,5,52,33,7,98,56,13,43,98,30,34,91,78,83,51,91,14,39,69,30,4,94,32,60,41,61,50,83,92,90,31,32,92,88,40,60,16,58,16,28,44,87,4,64,96,33,57,74,80,22,92,90,76,89,70,36,20,4,11,77,64,27,90,47,96,21,78,59,65,53,6,54,42,42,26,52,1,48,41,46,17,61,56,60,48,4,21,37,74,83,85,41,93,21,38,90,27,65,47,75,15,2,77,85,0,8,22,3,94,90,38,9,2,97,93,61,92,63,71,68,35,24,42,96,73,74,56,46,14,30,7,88,79,6,32,89,34,94,74,10,4,9,75,85,83,52,88,41,65,9,81,28,77,96,65,51,57,24,72,72,74,6,32,46,62,93,65,66,67,14,80,2,83,43,96,16,53,51,10,14,60,89,13,66,40,49,45,6,18,53,55,12,74,3,22,40,52,22,52,98,7,11,59,10,40,44,75,31,94,6,10,93,20,93,1,41,51,18,13,39,40,85,69,29,98,32,90,23,69,7,31,79,29,93,16,32,59,64,40,14,20,82,74,58,63,4,81,3,78,2,80,94,11,18,27,9,47,62,19,26,36,17,17,99,18,96,4,39,64,57,35,96,34,56,17,70,25,87,31,47,54,55,42,67,92,29,44,76,37,77,58,76,52,60,80,22,1,85,46,81,61,29,78,98,3,3,3,85,43,74,59,34,69,5,72,59,66,22,60,99,24,48,3,68,32,6,6,59,81,67,6,41,95,55,92,53,87,99,65,73,66,0,14,92,0,6,18,32,48,98,97,56,49,20,94,33,80,78,84,35,37,10,12,33,68,1,52,74,56,46,21,64,10,31,31,38,24,45,57,53,79,29,81,70,56,8,6,40,79,46,86,96,92,49,38,71,29,41,97,15,87,15,92,30,85,13,7,12,34,40,48,45,20,40,45,24,46,56,17,7,79,43,44,45,47,56,69,50,68,28,76,14,55,97,64,71,1,44,17,89,45,23,70,60,26,32,15,55,61,11,47,15,65,92,34,66,75,68,16,36,81,69,25,3,24,96,41,92,10,11,55,53,83,26,50,42,94,91,88,87,77,83,12,75,22,97,58,5,14,10,7,15,18,17,1,86,43,16,21,36,43,41,12,73,31,68,17,73,11,0,53,6,24,73,14,85,99,55,16,90,36,55,29,94,58,15,99,32,28,32,32,22,26,83,8,24,12,75,7,46,29,9,98,43,8,36,33,28,89,64,47,80,87,4,86,23,90,62,27,42,32,20,92,22,87,20,97,48,28,27,29,56,84,35,30,44,77,58,60,9,54,57,41,14,65,16,2,19,55,26,2,28,64,85,69,14,5,45,92,76,94,5,35,74,50,26,9,44,36,75,24,25,31,44,8,14,50,47,6,84,87,58,74,70,15,39,92,86,1,80,61,42,55,64,69,95,82,51,89,29,23,41,15,0,99,38,5,59,95,77,69,83,87,0,16,34,56,6,2,25,61,52,55,90,26,2,57,16,51,43,4,23,29,9,34,49,78,46,70,78,33,29,28,50,0,25,85,3,60,52,85,81,58,91,70,22,86,68,61,73,83,45,53,81,20,95,92,20,13,2,15,3,64,73,9,1,14,76,91,54,59,6,28,82,94,52,73,57,36,58,62,34,97,72,15,23,2,92,49,56,39,94,40,96,65,54,25,85,93,51,60,48,42,13,18,32,46,76,5,61,63,91,65,5,5,46,47,48,2,50,98,26,96,46,30,16,6,40,44,91,98,18,35,24,57,82,1,1,9,84,31,50,88,56,38,7,55,93,87,8,69,25,31,41,91,90,60,49,47,32,63,92,47,30,92,31,26,0,3,81,27,83,74,93,96,84,20,44,87,85,44,20,51,65,21,91,39,67,96,64,37,16,70,1,76,12,99,14,7,84,88,85,84,10,22,56,93,36,97,89,44,47,72,50,57,47,71,67,7,65,83,34,64,90,75,97,14,13,13,18,31,90,6,80,91,28,56,57,48,40,3,70,64,61,29,75,23,78,14,57,8,38,40,18,44,58,15,29,26,19,56,25,76,87,95,2,83,60,94,32,42,19,43,97,50,59,23,16,90,1,38,58,49,87,35,9,46,11,99,33,9,99,45,73,6,99,14,82,86,25,88,72,92,55,41,92,25,28,93,41,41,14,92,71,55,15,46,18,29,55,24,29,49,57,6,59,22,49,85,9,48,49,44,48,72,87,32,55,14,81,89,94,9,9,95,94,80,15,50,67,51,17,77,85,43,55,43,78,60,90,56,94,51,30,35,49,63,98,11,36,57,59,17,61,78,42,13,59,64,10,22,52,95,12,25,38,23,90,99,92,37,11,52,63,69,98,37,91,70,39,99,22,23,26,49,87,34,45,64,67,29,21,22,35,47,18,0,34,86,69,21,54,10,17,32,84,58,2,66,84,48,83,24,44,15,11,68,97,74,29,13,38,22,68,5,0,17,88,40,44,94,2,50,61,8,17,92,59,39,40,3,9,45,90,82,83,14,73,60,70,75,15,91,87,22,57,13,82,21,53,42,66,32,22,15,24,81,47,74,68,94,67,69,72,46,18,32,22,61,53,55,94,74,94,84,0,93,49,86,93,85,39,98,61,88,75,52,12,78,69,18,44,65,48,41,62,72,12,7,61,55,15,48,63]
Case 34
heights = [5233,8574,7797,9145,5812,7536,9701,6439,7225,8732,6996,6993,5082,8754,6465,7137,8920,6338,5325,6357,9234,5567,9511,5902,9063,6077,6884,5040,8841,5496,6393,6947,5286,9114,6734,9143,6911,9060,7951,5857,9825,6187,5973,6209,5099,8703,8937,7255,5910,9574,9278,6334,5642,9287,9624,5759,7294,5776,6759,9014,6488,8655,5957,8668,5965,7413,5262,6887,9087,5454,8329,5175,7674,8197,6995,7962,9540,7618,7592,7368,6290,8533,7704,7508,6727,9348,8524,7773,8943,6164,6240,7109,7828,9485,5172,9042,5036,5297,9843,6824,6689,8886,9948,6243,6471,7907,7451,9004,8301,9312,9159,7760,7923,5424,5045,8961,9627,8783,8582,7264,7594,6275,8457,8216,6898,8179,6764,8198,8018,6861,8353,7839,7214,8192,9849,5158,5975,9687,5743,8619,7292,6218,5625,7943,8248,7531,9109,7939,8647,9425,7720,7092,5717,5899,6510,6155,7897,8110,9550,8942,6762,6934,7666,6246,5896,6741,7376,7129,8251,7744,5974,5163,5366,9635,6059,6624,5542,6284,9550,7446,6648,8527,6572,7189,8807,8583,8314,9656,6686,9617,7712,8645,9411,6933,6686,9542,9092,6405,8040,7941,9827,7679,6339,6378,5333,8901,7196,5003,5449,8020,5824,5429,5253,5309,8441,9363,7309,8736,7094,5598,5845,7396,8937,6252,7553,5926,5894,7587,5468,8539,6568,8218,5349,5416,9117,7863,9821,7246,9535,8756,9946,9607,6400,5553,6024,6023,6458,5480,7620,5845,9924,6731,7877,7739,7465,7808,8877,7075,5060,9347,9739,9454,8005,5981,6126,8611,6626,8263,5511,7547,5836,6298,6176,5403,8301,7345,5277,8140,6803,8127,9019,8681,6798,5826,9173,9082,5086,9227,6696,9430,5902,6758,9100,8647,6854,5893,8316,5465,9101,5854,8541,9984,7993,7158,5341,8201,5364,8492,6080,7904,7699,5527,7060,7678,5584,9270,6689,6398,9333,9045,8620,6351,8990,8847,7633,9365,8568,8506,9050,5424,8695,8792,7122,8878,6758,9682,8239,6702,9834,7088,7339,6607,7177,9061,9263,5230,9391,9283,7438,6052,5610,9116,8652,6909,7352,7634,7521,5228,9695,7019,9259,7032,7152,6951,7874,8356,7731,5868,7766,5717,8545,7044,5796,9212,7906,9098,8961,5920,9016,9300,8701,9043,6417,9156,9702,8788,9914,8575,5726,5062,7507,8713,7441,8332,5985,9077,6550,5941,8398,8227,9909,7814,9052,8130,9566,7068,6699,7546,8584,9405,8817,7324,8062,5500,9796,5231,8872,6555,8190,6034,9930,5606,8253,9866,8215,9306,9041,7749,5802,8621,8467,8484,6881,6867,6075,5075,9864,6132,8960,8135,5107,7414,8972,6630,7426,6080,6320,8462,9349,9349,7531,7656,6322,6755,8430,9123,6519,6828,7352,5036,8796,8814,7122,7698,9705,6960,9969,6997,7049,9233,5609,9531,5644,8049,6136,9126,5560,7804,5039,9946,5177,9388,5963,5045,8980,7116,6680,9215,8658,9245,5108,8032,9483,5235,8419,7096,9473,9618,9847,9678,7886,7914,8301,9444,5610,5184,9859,8077,8620,5337,9344,5389,5853,7581,5346,6386,6912,6468,9700,8463,6720,5985,5645,6654,6483,8548,8351,9772,7099,5926,6056,7435,5895,5623,5695,7288,8086,5537,6047,7207,9541,7623,6403,7448,6284,6248,9293,5376,6317,9714,8355,7159,9777,9502,7324,9090,5853,8359,6853,6214,8731,8703,7796,8204,5263,8324,6641,5094,6195,7975,8012,7724,7442,9030,8210,7528,7422,5935,8121,9475,5477,5410,7293,9402,7104,7978,7811,7766,6633,7257,7774,7481,6996,8041,6566,8598,7213,7852,9903,9452,8174,7011,6292,5472,7201,7440,6539,6466,9829,5385,9189,8624,5584,9389,6293,8280,8263,5580,6582,5906,5256,7781,9824,9312,8802,8735,6441,8622,8206,7348,5371,9434,8224,5436,8514,7635,5534,7317,6168,6660,5221,6609,9924,7963,9272,9935,6536,9665,8551,6600,6280,8872,8413,7391,7433,8108,8625,9820,5465,6981,8033,8151,9529,8874,5402,8162,7125,7150,5017,6591,6770,8292,8630,5015,5178,7670,8686,5075,7649,5593,9042,9384,7435,5784,6488,9462,6604,7607,6949,6838,5714,6590,8517,9677,9483,9300,9159,8406,5303,7459,7704,8785,9989,9705,9399,9548,8379,9734,5498,6954,8686,9761,8046,9786,6405,8164,6853,8733,7209,7057,8435,9164,6002,7355,8508,6272,5007,9991,5778,5837,8810,6230,9178,9175,9503,6276,5792,5388,7047,6421,8952,9772,7903,8520,7683,9529,9961,6039,6088,6564,7122,5644,7711,9584,9622,7193,6842,9308,5182,6096,6998,6454,9662,9052,9482,5841,6766,6154,7499,5776,5453,9958,7713,9514,7640,6228,7223,8933,8976,8303,6312,8504,6151,7046,8949,8947,5582,5309,8811,8090,9254,5769,6892,6897,6526,5525,6275,9419,8871,6371,5447,8704,9215,8841,5853,8572,9024,8077,5781,9231,5454,9744,9038,6003,7094,5837,5294,7873,9248,7236,5155,5134,9087,5732,5242,5847,5260,5612,6176,5411,6480,5814,5551,6195,5763,8405,5990,8312,5080,5514,8459,7128,5054,7867,7620,6045,7724,8420,8261,9738,8559,9402,7419,5485,9056,8367,9679,8585,8898,5395,6880,5995,5226,5869,7688,8817,7232,7328,8045,9784,9386,7843,9433,6054,8018,6682,9199,7807,7363,5408,9792,5303,6578,9202,9533,6985,9584,6587,7202,7936,6098,6290,8565,7471,9092,7589,5380,8554,8177,6630]
Case 35
heights = [70,10,48,66,39,8,76,24,64,70,24,0,89,44,85,43,28,69,37,67,62,69,18,84,18,21,55,23,4,42,54,50,32,51,88,42,99,59,79,90,17,84,83,20,77,91,23,39,19,45,95,65,48,98,83,37,71,67,90,76,91,67,5,23,94,67,94,12,87,59,80,95,53,34,54,80,81,6,68,49,22,95,71,72,54,75,21,92,47,75,19,37,51,66,44,64,82,59,18,43,16,30,33,73,44,58,66,56,25,30,89,40,26,15,33,65,57,68,64,19,0,52,63,51,58,33,96,89,60,95,45,28,77,91,50,38,34,80,49,64,27,95,80,40,4,95,70,8,36,97,37,31,8,88,58,29,31,16,86,38,86,48,27,92,20,61,58,27,8,56,0,99,23,24,46,4,52,62,85,57,6,30,65,43,39,44,77,72,53,73,11,99,6,70,95,51,61,97,47,34,90,52,71,86,63,83,48,50,68,70,73,77,6,2,7,92,97,34,43,50,14,74,10,64,17,75,9,12,68,70,10,16,21,1,6,46,81,95,79,83,10,34,51,39,37,4,47,66,24,7,49,13,2,32,60,41,65,81,86,5,16,35,62,40,78,2,19,60,13,38,46,43,57,1,95,59,34,79,60,15,69,41,79,1,63,34,0,78,0,9,88,60,23,40,12,15,28,65,70,34,18,50,83,39,62,57,39,99,96,79,37,66,78,27,29,1,24,51,13,85,17,8,93,67,83,45,33,81,4,80,83,32,1,43,75,29,63,94,50,57,16,16,68,56,6,72,23,31,55,85,78,30,64,51,28,41,17,56,44,76,63,26,26,4,97,15,24,67,87,54,28,12,69,32,30,63,43,78,2,31,68,62,87,75,94,86,46,9,2,3,30,70,92,6,55,22,15,16,49,93,71,97,45,70,80,42,30,9,40,26,78,43,18,38,34,20,52,47,12,4,78,7,1,67,58,9,81,33,40,77,54,6,35,39,4,55,97,78,76,71,26,31,89,67,58,54,92,5,94,19,15,98,14,68,53,8,46,3,88,58,53,37,99,88,54,70,27,18,92,45,78,11,71,88,84,79,62,71,88,80,18,19,83,13,34,23,17,49,50,40,20,2,60,14,7,46,6,9,38,14,53,36,24,13,26,43,51,53,41,39,77,16,72,66,51,51,97,84,72,66,29,11,38,67,62,72,16,93,76,48,25,47,24,59,76,10,87,49,74,69,90,94,27,8,32,38,31,71,2,44,89,24,97,95,77,89,45,45,94,50,82,55,74,44,80,50,18,25,4,51,7,8,51,61,84,55,89,31,17,36,33,67,29,56,56,35,66,91,29,5,3,88,55,84,96,64,35,24,89,36,17,56,10,44,29,26,92,45,60,90,2,19,72,1,43,50,17,72,12,71,76,68,6,40,84,41,57,45,35,67,88,13,7,30,71,15,17,11,16,51,18,91,89,12,11,15,35,10,70,35,63,85,50,4,96,88,70,59,7,64,44,5,9,70,92,69,96,90,27,79,50,40,45,24,53,94,15,5,78,19,87,69,0,11,82,11,27,28,90,57,24,70,31,7,63,6,84,84,55,73,84,81,45,33,75,3,17,38,22,63,3,49,51,19,57,63,58,80,9,81,94,70,90,26,2,8,62,93,22,57,33,7,76,59,5,34,27,82,93,38,78,98,6,98,40,35,66,82,98,18,79,81,53,37,59,93,2,5,47,49,25,81,35,28,52,25,29,53,98,23,64,1,17,26,5,8,53,15,37,71,56,90,20,87,19,77,88,58,70,10,53,25,33,81,90,62,4,32,16,6,10,81,30,44,15,18,57,56,15,77,58,90,98,83,61,45,46,41,19,7,41,13,29,8,14,60,76,43,48,81,48,80,53,34,16,89,77,12,7,3,64,75,78,92,26,4,29,34,62,90,50,56,82,16,53,94,54,36,53,23,34,35,85,79,11,9,74,96,54,94,51,91,8,74,15,85,28,55,78,79,29,34,87,16,36,82,96,28,46,54,52,58,51,84,31,43,95,87,47,10,38,82,13,42,18,47,63,18,2,17,73,37,91,61,26,56,10,17,87,41,40,82,51,59,33,55,93,6,25,55,45,90,32,73,57,51,54,76,96,83,52,85,3,49,98,62,53,66,89,27,81,2,32,12,96,25,19,48,70,32,16,49,87,0,71,42,39,91,88,73,10,72,95,16,15,21,70,11,7,9,22,81,85,56,80,0,76,13,84,85,58,82,39,6,3,65,66,0,22,85,65,53,95,16,83,27,28,81,7,19,83,2,13,88,23,65,24,11,43,53,92,63,11,84,42,48,99,0,98,86,9,53,14,4,88,31,66,87,16,92,93,49,30,4,45,15,65,21,65,92,94,16,25,25,15,30,94,53,89,61,71,50,41,92,83,17,52,99,92,29,66,38,25,51,22,97,51,58,41,39,44,57,54,78,65,33,86,12,46,30,85,6,93,0,2,51,65,99,32,73,76,17,60,69,59,20,52,4,86,70,87,88,39,87,85,50,88,17,88,94,97,82,68,9,52,85,75,9,84,3,3,63,38,89,66,60,19,17,35,19,59,28,2,51,69,8,86,43,46,76,22,64,32,2,87,8,97,27,8,98,93,79,17,52,46,37,90,30,94,12,94,95,25,6,94,48,85,97,73,55,42,68,2,3,66,34,0,2,91,16,55,29,12,26,59,42,41,14,20,40,15,29,86,70,66,50,7,82,18,92,56,36,65,93,32,74,73,94,33,49,49,35,69,32,92,62,76,66,2,13,14,64,74,13,57,91,14,17,72,62,96,61,61,13,32,68,27,15,87,8,17,74,4,88,24,84,47,51,18,76,74,59,0,87,2,87,67,18,98,20,56,32,94,94,63,38,9,3,18,54,7,47,88,53,21,93,67,2,52,47,96,61,65,94,20,2,28,55,23,96,16,79,97,23,28,7,45,54,99,96,25,34,0,30,49,53,27,51,60,63,63,60,16,63,54,12,17,86,3,61,55,60,89,37,79,81,30,83,86,50,27,76,95,14,74,6,4,10,67,48,47,8,24,39,64,21,94,2,74,94,25,1,76,83,4,53,18,41,92,92,20,33,25,59,20,73,69,48,22,17,59,28,82,93,30,70,27,60,10,36,82,11,80,70,0,6,14,13,44,9,84,92,19,99,31,43,82,34,18,29,75,73,62,72,46,41,89,97,1,55,86,41,88,23,54,26,78,87,1,5,17,96,60,2,67,22,20,25,32,43,57,11,2,62,34,82,51,24,18,1,54,12,13,60,63,85,21,73,62,30,20,29,53,19,80,34,16,50,52,0,96,24,46,58,36,93,10,2,25,41,49,62,24,24,45,94,36,4,83,1,45,14,43,98,90,51,33,81,96,91,94,49,79,28,9,64,38,89,83,12,75,62,64,34,82,24,67,85,79,76,43,33,42,34,69,47,43,33,71,12,75,95,52,54,94,68,43,87,30,74,89,30,80,12,85,36,40,47,15,27,22,40,91,10,22,99,61,88,92,18,78,57,69,49,59,14,54,76,16,15,80,47,32,96,62,39,74,27,42,69,17,25,33,25,56,40,65,78,65,42,82,63,68,91,49,45,82,1,86,42,97,98,21,88,72,2,28,54,50,91,53,98,30,86,18,43,34,45,59,63,5,15,26,76,5,75,72,34,65,1,43,10,51,72,37,91,30,38,57,97,30,71,3,55,65,69,63,77,16,74,14,85,10,34,43,18,16,62,30,80,93,23,34,84,28,50,39,79,62,50,93,75,1,41,45,27,2,87,42,7,50,46,59,93,71,68,47,5,15,93,94,84,54,46,87,31,76,32,78,33,93,97,27,19,93,38,72,6,93,0,65,66,92,42,69,4,51,6,12,40,35,89,69,54,38,26,71,62,53,31,84,99,14,76,66,22,33,48,33,11,86,22,56,41,35,41,13,99,22,3,21,24,49,9,73,64,12,90,15,13,66,47,96,14,91,64,8,34,45,98,43,46,30,38,33,10,94,35,38,54,23,13,80,64,66,67,50,99,71,87,6,22,0,4,63,34,95,96,78,27,72,97,57,61,52,69,95,10,0,38,56,35,83,89,83,7,85,55,19,49,41,30,66,92,22,91,76,69,8,87,19,57,63,58,47,83,56,89,16,67,85,25,79,59,85,53,92,48,58,70,43,86,28,86,40,44,65,71,22,34,51,58,31,82,61,96,32,98,62,93,99,95,80,14,72,75,64,21,37,90,45,20,78,94,79,90,23,50,56,57,99,89,82,83,44,95,50,77,51,47,45,50,82,66,66,78,31,17,32,46,11,66,24,43,82,73,19,34,55,63,19,14,71,49,26,53,36,92,35,85,53,91,82,44,25,18,22,60,45,22,10,84,95,20,68,7,99,29,10,16,18,7,40,40,58,43,69,43,37,53,72,26,24,77,63,75,71,16,57,52,35,94,4,37,30,57,79,42,59,70,92,79,24,42,58,15,86,29,55,91,6,39,48,44,20,29,26,44,19,8,95,35,9,56,30,56,59,27,87,2,77,35,60,98,70,68,43,37,60,54,29,27,19,8,79,11,45,26,76,12,96,88,42,2,29,90,25,92,50,71,86,1,69,20,69,30,45,18,93,5,3,81,37,94,88,53,10,80,70,80,46,63,17,18,7,5,91,16,89,63,26,64,90,55,22,54,39,46,91,72,82,40,37,21,6,35,82,10,68,0,2,70,25,7,68,15,37,89,31,34,78,82,73,93,2,83,59,4,18,76,26,81,24,97,99,87,23,78,77,76,27,98,95,42,64,15,91,46,73,8,10,51,54,91,26,47,33,86,19,93,31,26,7,51,4,74,40,2,89,99,70,58,13,0,8,62,93,89,35,29,0,5,73,3,59,54,45,96,21,2,61,6,88,72,70,11,7,61,98,97,80,84,35,13,19,28,19,71,49,18,34,47,72,24,8,89,29,95,47,94,22,43,19,67,90,80,16,76,25,53,75,38,49,34,10,89,58,21,27,7,99,25,64,28,38,40,82,80,47,52,15,22,67,43,18,5,10,56,4,93,62,12,98,64,56,48,61,73,69,3,15,29,5,85,42,14,47,58,22,75,82,23,28,25,83,63,70,85,37,58,26,26,3,86,15,8,39,30,96,2,11,24,16,55,37,45,22,74,67,5,65,57,28,99,43,24,30,42,57,24,8,45,95,61,62,59,31,11,18,93,9,2,38,22,58,64,84,4,72,2,60,16,9,77,47,55,95,34,43,37,42,15,35,8,1,42,52,8,57,21,57,78,54,2,59,16,3,58,64,77,1,91,75,56,42,91,14,91,17,63,25,3,50,43,4,37,6,45,35,48,39,44,27,48,92,77,96,32,89,18,11,40,31,92,3,91,10,97,71,2,13,84,36,31,82,29,44,71,89,10,59,2,79,47,96,58,28,13,5,97,2,18,4,26,29,72,19,68,16,70,82,1,73,57,58,39,20,68,51,23,27,44,25,34,26,29,84,57,44,43,20,37,78,8,60,69,32,65,31,52,18,85,38,19,48,20,19,31,57,18,47,24,24,28,49,39,60,6,54,45,88,96,74,99,38,19,25,62,9,6,18,35,93,75,29,95,42,16,67,15,58,53,24,56,9,15,76,39,62,65,63,0,61,41,48,73,90,37,63,26,95,57,12,45,50,56,42,42,46,17,88,74,21,63,53,35,93,15,16,61,21,81,7,82,38,63,30,1,45,71,77,61,84,47,2,9,15,80,5,49,39,91,49,98,1,11,58,82,63,33,81,74,13,72,50,0,63,26,54,48,94,80,37,70,90,0,0,40,80,52,97,16,88,23,57,11,30,2,71,59,89,85,38,33,16,76,93,7,58,86,4,39,39,35,36,46,41,89,59,76,31,54,63,22,9,81,31,29,9,5,61,41,2,30,98,51,99,17,32,88,29,63,32,75,79,99,71,55,0,99,10,55,57,35,39,35,55,29,94,29,70,2,69,22,10,4,76,70,78,31,78,67,27,55,31,60,49,9,30,5,63,12,69,55,90,60,52,29,46,95,67,93,45,36,76,45,65,71,48,80,86,28,5,26,76,10,20,37,94,26,83,49,24,11,59,20,15,67,94,33,47,52,34,3,61,67,68,12,98,80,82,36,93,18,34,1,79,96,55,84,51,85,77,75,53,8,11,8,44,99,18,6,54,26,88,58,31,82,7,84,76,93,91,65,98,7,52,23,9,94,70,52,67,44,95,6,39,34,99,14,90,47,49,27,0,59,31,40,88,19,91,49,87,85,90,11,30,31,84,92,2,41,60,94,49,15,59,79,28,57,62,45,63,2,81,55,35,79,19,0,24,14,28,25,50,10,49,56,26,10,72,55,46,71,9,46,50,0,60,30,51,54,31,96,81,71,18,11,12,52,90,51,97,8,61,67,99,2,38,56,64,12,63,49,66,78,70,62,51,38,43,42,13,7,75,9,10,24,57,13,35,10,40,42,28,11,38,47,5,28,44,4,12,67,7,93,66,62,93,60,98,47,39,29,93,17,0,46,54,65,60,81,90,26,8,66,32,0,14,3,30,74,90,98,65,76,86,12,99,30,1,15,67,3,98,16,54,28,20,0,51,47,6,28,59,32,90,32,52,63,10,7,43,49,30,69,6,16,61,81,65,72,83,80,64,50,97,10,92,67,30,37,3,46,74,57,72,64,47,52,69,18,67,71,27,99,79,71,8,26,16,93,6,36,40,45,43,68,55,63,73,59,19,82,39,79,66,24,40,99,40,38,56,38,57,5,74,9,19,87,79,39,97,64,82,81,58,79,28,20,12,53,63,74,94,20,62,60,96,44,42,77,69,7,18,33,84,1,24,46,36,39,83,67,10,77,97,84,78,58,77,20,8,54,87,3,1,20,71,26,12,53,92,72,59,52,83,92,21,86,0,1,49,16,58,60,76,47,54,70,69,39,72,96,73,96,80,13,89,70,70,84,35,72,11,34,79,98,60,22,11,56,75,48,5,88,21,97,2,41,54,18,39,14,50,90,7,82,52,1,4,55,96,30,24,48,3,37,79,98,54,53,86,17,74,55,41,46,52,81,47,82,71,24,46,59,10,19,54,19,47,30,56,61,24,97,9,64,82,61,89,64,86,7,27,40,14,69,98,88,9,79,26,40,34,55,69,79,53,19,98,43,55,94,90,78,72,91,17,7,41,98,78,24,39,75,43,31,68,0,60,77,57,43,87,15,6,82,23,30,85,26,3,86,16,97,6,66,43,52,38,42,22,52,68,42,73,10,68,29,38,91,6,82,68,13,61,36,96,50,56,89,90,21,64,0,52,36,58,51,40,7,39,17,66,8,6,92,74,45,23,93,20,91,13,10,20,87,29,83,94,5,99,85,36,53,27,21,35,78,45,13,20,20,20,93,8,55,22,3,33,9,94,33,57,10,39,74,81,9,91,20,73,14,18,84,12,18,33,59,31,57,63,34,54,73,64,69,10,44,84,0,9,8,88,14,53,60,99,75,32,99,48,34,29,57,95,55,49,98,21,97,23,2,38,15,93,54,77,60,30,22,32,9,60,22,70,66,54,58,92,57,78,3,9,62,78,24,69,3,58,93,34,62,87,75,95,34,48,75,13,91,27,93,60,86,75,37,93,63,32,24,2,32,50,79,38,25,55,91,62,40,79,74,23,3,22,45,80,62,60,10,46,89,63,89,78,93,3,67,88,49,89,94,38,80,15,18,9,63,80,60,84,79,18,74,53,0,0,45,67,43,70,69,44,41,6,72,89,36,68,92,3,89,13,15,44,74,13,99,9,58,24,93,24,68,73,54,88,14,86,13,53,61,17,85,79,20,25,10,1,85,91,9,14,46,21,14,56,68,98,81,50,97,60,37,49,10,98,72,24,58,62,1,46,84,1,33,94,40,48,11,56,65,29,96,23,11,1,23,91,85,40,58,1,84,78,41,24,26,18,77,27,13,94,94,48,73,98,24,54,0,55,81,38,24,9,38,68,43,11,5,87,45,47,69,59,49,29,96,88,64,81,76,22,67,25,63,76,23,90,55,54,41,21,89,61,35,8,55,94,72,69,70,97,85,25,33,18,83,33,86,95,3,24,67,39,80,42,16,39,82,59,97,17,79,54,6,75,99,82,51,1,8,85,43,36,52,33,36,99,73,71,79,47,94,55,6,85,61,62,24,66,66,59,42,47,17,94,4,81,21,38,77,38,90,55,70,56,50,54,92,92,8,9,9,60,77,82,7,0,50,0,47,31,84,50,21,39,39,49,59,58,50,25,83,42,72,31,93,24,12,33,69,10,40,82,48,48,95,38,29,88,18,23,29,2,65,60,67,95,55,19,95,70,20,54,11,65,97,79,8,73,97,80,71,58,71,38,78,64,63,0,88,34,1,10,15,95,25,7,99,50,42,0,15,39,93,1,51,65,12,89,80,4,15,16,85,50,34,2,30,42,9,5,53,80,64,36,41,4,18,85,88,61,43,55,60,0,62,53,93,61,42,62,37,95,3,26,30,28,85,47,56,81,6,62,65,62,56,33,59,91,24,90,43,40,88,2,42,31,25,14,67,85,60,18,64,0,25,28,4,13,52,41,42,33,70,2,60,32,85,15,67,37,35,8,44,72,59,14,8,3,11,27,96,18,41,22,34,51,82,21,83,57,21,20,70,83,3,77,68,56,33,83,45,8,79,33,86,9,99,79,58,68,83,91,53,23,9,89,69,16,27,15,44,0,31,54,99,11,10,83,10,0,61,55,97,73,57,75,95,51,17,38,31,82,99,18,97,38,55,95,78,54,74,15,30,73,79,68,92,36,40,6,74,21,10,2,46,68,9,2,23,97,10,82,31,99,78,20,37,2,89,80,95,73,57,82,9,96,17,33,52,52,10,30,56,99,81,35,52,3,30,20,66,23,66,94,58,61,63,86,15,59,43,18,38,59,27,85,81,80,29,50,63,51,1,76,84,47,65,27,83,76,90,94,65,72,9,25,29,62,32,5,58,64,37,50,25]