Problem Statement
Bear Limak has just started a new year at school. The school teaches N subjects. At the end of the year Limak will get a grade for each of the subjects. Each grade will be an integer between 0 (worst) and 10 (best), inclusive.
The grade for each subject depends on the number of days Limak will spend studying that subject. If he does not study a subject at all, his grade for that subject will be 0. Each subject has its own difficulty rating d[i]: each d[i] days spent studying the subject will increase Limak's grade by 1 (up to a maximum of 10, of course).
Note that these increases are discrete. E.g., if Limak spends d[i]-1 days studying subject i, his grade will still be 0.
Limak's parents are quite strict. Each of them has made a request:
- Mama bear told Limak that his mean grade must be at least needMean.
- Papa bear told Limak that his median grade must be at least needMedian.
Compute and return the smallest possible number of days of studying Limak needs in order to satisfy the requests of both parents.
Definition
- Class:
- MeanMedian
- Method:
- minEffort
- Parameters:
- int, int, int[]
- Returns:
- int
- Method signature:
- int minEffort(int needMean, int needMedian, int[] d)
- (be sure your method is public)
Notes
- The mean of N values is their sum divided by N. (The mean can be non-integer.)
- The median of N values is the middle element of a sorted list of all those values. (In this problem, N is always odd and thus the middle element always exists.)
Constraints
- needMean will be between 0 and 10, inclusive.
- needMedian will be between 0 and 10, inclusive.
- N (the number of elements in d) will be between 1 and 49, inclusive.
- N will be odd.
- Each element in d will be between 1 and 1000, inclusive.
Examples
2
4
{30, 25, 20}
Returns: 180
There are three subjects at school. Limak's grades should have mean at least 2 and median at least 4. The only optimal solution is to study subject 0 for 0 days, subject 1 for 4*25 = 100 days and subject 2 for 4*20 = 80 days. Afterwards, Limak's grades will be {0, 4, 4}. Their mean is 8/3 (which is more than we need) and their median is 4 (which is exactly what we need).
4
4
{30, 25, 20}
Returns: 260
Limak should get grades 0, 4, 8. The mean is exactly 4 and so is the median. The answer is 4*25+8*20=260.
10
3
{1, 4, 3, 2, 1}
Returns: 110
0
0
{1000}
Returns: 0
8
3
{4, 8, 12, 16, 18, 20, 22, 23, 24}
Returns: 1046
3
7
{20}
Returns: 140
9
2
{1}
Returns: 9
0
0
{2,3,4}
Returns: 0
1
0
{22,75,26}
Returns: 66
0
1
{45,72,81}
Returns: 117
9
10
{47,29,97}
Returns: 1439
10
9
{2,75,25}
Returns: 1020
10
10
{82,84,17}
Returns: 1830
1
1
{156,932,902,728,537}
Returns: 1733
1
2
{7,9,8,1,9,6,10}
Returns: 44
2
1
{8,8,6,10,3,3,9}
Returns: 50
2
2
{991,310,355,68,431,580,757,218,934}
Returns: 3308
1
3
{8,6,5,1,10,2,5,1,7,7,4,1,4,5,4}
Returns: 66
2
3
{5,5,1,5,1,2,5,1,2,3,1,2,4,4,2,1,2}
Returns: 46
3
3
{502,968,333,475,792,84,10,694,328,354,712,409,480,643,121,951,492,420,197,607,925}
Returns: 13173
4
3
{167,717,438,200,986,104,483,620,806,881,858,559,553,502,554,962,325,435,279,464,327,549,832,595,200}
Returns: 29868
5
3
{85,12,68,115,22,37,8,85,104,107,49,33,37,107,37,34,81,60,74,9,27,7,23,78,106,100,5,59,76,110,93}
Returns: 4290
6
3
{147,147,552,224,451,934,837,895,463,25,974,205,456,376,112,358,857,188,752,511,423,122,580,761,15,316,579,79,611,265,98,551,353,362,849,340,603}
Returns: 56272
7
3
{17,40,43,27,20,42,34,39,46,25,1,8,49,27,40,28,38,5,17,3,33,18,7,7,6,28,27,18,49,32,41,26,14,19,1,48,48,13,15,13,45}
Returns: 5156
8
3
{523,571,206,357,904,310,410,844,611,484,410,559,262,394,949,107,546,737,987,979,306,685,291,542,542,134,94,751,89,898,729,212,964,297,823,720,297,280,917,338,176,183,965,740,541}
Returns: 152570
9
3
{555,3,316,256,13,611,974,931,24,609,176,304,151,199,876,825,893,939,737,374,323,846,819,154,157,814,343,886,197,100,856,709,879,479,461,14,123,744,400,94,447,20,152,963,674,829,984}
Returns: 187213
0
4
{44,50,54,21,61,24,19,33,57}
Returns: 564
1
4
{60,20,27,39,31,62,33}
Returns: 444
2
4
{71,69,73,31,92,127,13,21,58,23,78}
Returns: 860
3
4
{20,51,3,49,35,2,39,17,39,8,49,42,53,37,34}
Returns: 662
4
4
{33,46,17,32,44,20,7,38,48,49,49,2,31,25,27,45,29,27,11,22,6,38,27,31,13,2,45,23,48,8,13,22,4,38,30,16,22,10,27,21,31,44,37,7,41}
Returns: 2308
5
4
{89,15,70,142,116,140,136,152,17,91,108,89,52,159,105,33,107,155,104,160,62,99,20,164,96,194,60,166,136,117,176,118,124,155,58,86,99,15,110}
Returns: 13135
6
4
{15,20,7,24,19,4,3,2,3,24,14,3,12,26,14,15,1,26,5,15,27,16,27,3,16,1,11,22,2,9,5,27,3,26,23}
Returns: 1470
7
4
{95,78,88,96,62,38,47,83,12,77,68,63,18,45,41,9,13,37,69,9,46,38,82,42,89,21,95,12,70,77,46,66,36,90,87}
Returns: 10235
8
4
{240,55,5,175,254,54,163,305,76,67,91,249,362,137,49,106,377,33,269,55,65,150,55,162,65,233,192,267,184,219,36,352,328,229,343,261,209,129,154,173,173,168,370}
Returns: 47728
9
4
{31,21,19,15,1,24,3,33,41,29,12,22,41,13,11,5,17,32,19,38,33,26,4,34,34,34,20}
Returns: 5034
10
4
{227,96,140,177,155,110,201,37,66,30,53,172,23,114,31,188,229,55,134,147,133,9,21,31,112,74,32,144,108,137,60,70,128,102,66,195,178}
Returns: 39850
6
2
{117,32,68}
Returns: 864
1
6
{4,1,8}
Returns: 30
5
4
{125,115,5,174,208,246,111,149,247,38,76}
Returns: 4075
9
9
{3,9,7,7,1,1,11,3,5,7,11,11,3,2,2,11,5,6,6,7,5}
Returns: 999
3
0
{117,102,61,42,15,35,42,4,84,18,2,7,75,33,100,42,74,70,26}
Returns: 642
10
8
{14,3,20,18,16,9,2,18,13,16,16,16,8,16,9,14,8,14,1,7,4,18,6}
Returns: 2660
7
0
{33,31,7,18,14,29,9,10,26,8,30,11,25,36,7,25,29,19,7,6,25,6,32,29,31,13,11,6,7,2,19,27,10,14,2,27,36,4,32,29,2}
Returns: 3589
8
6
{25,43,5,42,13}
Returns: 850
0
0
{133,739,319,533,388,299,558,720,122,861,766,922,456,601,377,144,882,966,46,104,9,149,696,327,585,555,413,859,963,139,591,970,860,834,911,274,876,145,677,214,545,743,609,879,937,940,782,553,531}
Returns: 0
0
10
{495,817,994,471,865,590,886,593,504,172,493,954,221,132,60,623,982,181,64,736,365,371,894,458,175,956,310,254,429,274,995,816,126,883,364,352,715,140,335,180,362,874,345,612,45,607,654,221,520}
Returns: 64070
10
0
{211,807,703,613,552,263,351,291,725,774,542,643,181,342,719,130,629,953,157,803,1,40,643,338,671,773,906,889,778,931,678,919,952,656,49,547,261,434,951,318,430,391,993,823,503,963,346,972,406}
Returns: 279510
10
10
{854,661,31,989,851,228,263,412,499,178,819,436,672,571,795,549,554,967,215,289,925,19,141,583,969,119,15,281,993,200,835,518,781,616,865,920,504,545,881,783,490,214,82,285,943,307,164,976,933}
Returns: 267200
10
1
{47,95,812,978,593,714,422,797,536,565,466,936,26,359,181,581,182,144,179,589,994,232,794,322,333,719,246,646,62,19,147,770,291,397,862,617,643,611,925,274,973,64,498,873,407,640,908,302,296}
Returns: 240670
1
10
{789,581,980,717,388,613,531,544,864,325,952,660,550,590,184,235,56,664,352,520,443,19,112,373,448,793,443,878,822,953,960,700,926,54,884,220,614,830,982,349,447,396,834,766,215,111,743,645,856}
Returns: 84860
1
2
{377,353,713,343,592,967,537,374,264,862,562,18,774,268,293,557,607,645,334,173,375,210,222,923,635,637,762,430,88,423,179,574,721,387,780,673,356,920,727,569,405,386,472,979,179,649,120,841,670}
Returns: 15132
8
7
{800,699,457,639,751,723,933,408,138,539,65,31,785,124,360,816,694,915,672,840,545,257,121,419,879,154,377,518,599,863,635,717,348,364,707,39,526,706,396,597,600,614,562,239,755,688,736,894,938}
Returns: 186760
8
8
{749,228,299,149,70,118,959,822,127,677,905,486,152,785,659,577,263,114,261,286,837,680,737,352,4,656,413,247,344,742,972,728,107,589,885,697,286,104,403,998,935,597,699,535,53,725,305,807,526}
Returns: 159010
5
4
{686,803,151,35,785,548,600,518,548,651,917,410,224,977,351,787,620,535,166,266,376,861,199,82,235,428,281,110,958,126,95,689,623,104,849,772,655,871,986,331,828,344,731,296,496,54,719,895,224}
Returns: 61695
5
3
{7,757,376,549,312,250,769,998,445,620,449,461,737,144,798,702,532,436,296,179,528,13,236,138,380,259,505,85,897,136,918,161,663,589,627,800,113,438,867,23,404,664,944,224,37,10,647,89,256}
Returns: 47830
4
8
{921,864,892,905,666,759,645,887,996,702,647,372,747,449,543,23,759,656,271,306,331,888,301,579,126,498,313,911,277,747,2,971,15,811,218,986,599,214,717,58,902,30,158,334,615,38,659,661,873}
Returns: 58520
10
10
{1000}
Returns: 10000
10
10
{1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000}
Returns: 490000
8
0
{20 }
Returns: 160
4
4
{1, 2, 3, 4, 5, 6, 7, 8, 9 }
Returns: 90
9
9
{44, 560, 595, 404, 786, 487, 923, 418, 992, 183, 163, 165, 962, 851, 293, 472, 352, 498, 757, 774, 697, 864, 734, 642, 591, 350, 753, 615, 653, 230, 703, 323, 397, 134, 512, 148, 759, 116, 821, 518, 471, 526, 868, 890, 679, 420, 530, 82, 774 }
Returns: 219308
1
5
{1, 2, 3, 4, 5, 6, 7, 8, 9 }
Returns: 75
8
10
{1, 1, 10 }
Returns: 60
10
10
{44, 560, 595, 404, 786, 487, 923, 418, 992, 183, 163, 165, 962, 851, 293, 472, 352, 498, 757, 774, 697, 864, 734, 642, 591, 350, 753, 615, 653, 230, 703, 323, 397, 134, 512, 148, 759, 116, 821, 518, 471, 526, 868, 890, 679, 420, 530, 82, 774 }
Returns: 264790
0
10
{10 }
Returns: 100
0
5
{4, 8, 12, 16, 18, 20, 22, 23, 24 }
Returns: 290
10
10
{1, 2, 3, 4, 5 }
Returns: 150
9
10
{1, 1, 1, 10, 10 }
Returns: 180
4
5
{1, 2, 3, 4, 5, 6, 7, 8, 9 }
Returns: 93
8
6
{411, 823, 125, 163, 181, 727, 22, 123, 241, 67, 113, 21, 221 }
Returns: 14414