 What's up guys, my name is Michael and welcome to my YouTube channel today We're gonna go over an another problem at code beginner contest 171 be mixed juice So pretty much you're just given the prices Okay, how about I just rephrase this problem because it's not that Not that difficult you want to sum up the case All K smallest Prices of the fruits. That's basically the problem You give an array some of all the case case smallest items of the fruit Basically you have like n fruits and you have the prices for all them and you want to choose K fruits of each single Chosen kind of the fruit and you want to find the minimum possible total sum of these fruits so basically All you want to do is find the K K smallest fruits and then sum up all those ones pretty much from First smallest to K smallest so what do I mean by K smallest like the first smallest second smallest There are just most four smallest up to the K smallest and then sum of them up if you just this problem basically a variant of the K smallest K smallest Value in array or K for largest If you want to know that I have like you check out one of my videos on K smallest some on the code question It's basically the same thing. Only difference is that we're not just returning the K smallest We were summing up them. Okay, so I'll just explain to you guys how my code what I did for the code and then yeah So what did I do? Pretty much you have to use a heap You could also just sort it because apparently on add code of contests. They don't really care about the It's not like an interview where they care about the space complexity Well, some contests do some don't though, but yeah, so what I do I read in and I read in K So n is the size of the array that you have K is just a the kth value that you want right I created my my Array of n plus one the reason why I do n plus one because I want to index it from one I don't want to index it from whatever value it is. I just did this because I want to do okay You could you could index it from zero. It really doesn't matter Yeah, as long as you read in the values in your right, but Now what I do here? So I created this this This priority queue, which is a max heap this priority queue uses a max heap Yeah, so if you don't put in like the greater Comparator here. It's it would be a min heap, but this is a max heap like priority queues By default their max heap. All right, so what I do now I read in every single value of my Every single value of my array right that I've read in you're supposed to have that remember We have to read in all the values of our array. So that's for i equals zero I was less than equal to n i plus plus and then read in all the each single value of your array So then I check if my size of my heap is less than k then I just push push the Whatever value it is let my current value into the priority queue Okay So that's gonna put the Every k values like all k values into the size of my priority queue. All right so now I check if my if these if it's smaller then If the current value that I read in is now smaller than the topmost value of my priority queue right here I'm going to eject it pop it and then I'm gonna put add in the next smaller number. Okay, or next Hold up If it's smaller pop it. Yeah, then I add the next smaller one number. Yeah, so this would get me all the k smallest Values of in my priority queue right all the k smallest number values in my priority queue After that all I have to do is create a variable for my sum to the sum that I'm going to return And then I just while my priority queue is not empty I'm just going to add every single value of the top of the priority queue and I just eject each value Right of the top of the priority queue and then yeah after that I just return now not return. I just print out my total sum That I added it for every single value in my priority queue and then that that's pretty much it That's basically basically how you do this problem. This problem is basically the same thing of the kth I have like a whole array Playlists of like keep questions. I did on the code You guys to check that out first and then come back to this video because they wouldn't this video wouldn't make any much Sense if you don't understand what I did But basically I went over in that other video basically by pen and paper Explaining how to do kth largest kth smallest Those types of things, but yeah, that's pretty much the gist of this question. So yeah, I'll be guys enjoyed this video Ray com subscribe. I'll check you out