 What's up guys, my name is Michael and welcome to my YouTube channel. Today we are going to go over another problem of code forces around 680C7. B, minimum product. So you're given four integers A, B, X, and Y. And initially A is greater than an X and B is greater than or equal to Y. And you could do the following no more than n times. You could pick, choose either A or B or decrease by one. Uh, however as a result of this operation, A cannot be less than X, okay, and B cannot be less than Y. So now our task is to find a minimum possible product of A and B, A times B, okay. And you could do the number operations no more than n times. So let me just explain this real quick again. So we are given a number, integer A and B. So we have A and B and A is greater than or equal to X and B is greater than or equal to Y. Okay, so these are numbers. So all of these are integers, right? These are all integers. Um, so, yeah I don't know why I just do that. Okay, these are all integers, right? So what can we do? Basically we want to find the minimum product of A times B. So we want to minimize minimum of A times B. Right, we want to find the minimum product of A times B. Okay, so what can we do? We can make a number of moves, right? So we could choose either A or B and decrease it by one. But our operation cannot be, our value of A cannot be less than X and our value of B cannot be less than Y. Okay, so if we could decrease A by one, so we could do like a, we could decrease A by one, so A decreases by one, but A cannot be less than X. Right, this condition can, A cannot be less than X. Okay, so A has to stay at this condition, right? And it's the same thing as B. B, we could decrease B by one, this number of times, right? We could decrease B by one, this number of times, but B cannot be less than Y. Okay, and our job is to find the minimum of A times B. And the number of operations we could do subtracting A and subtracting B is N. Okay, so we could, we can do no more than N, right? So we have the maximum number of operations we could do of subtracting one from A or subtracting one from B is N. Okay, so you might be wondering, how do you do this problem? So the first thing you have to realize is that if you're going to decrease number by one, let's say I keep decreasing, I keep decreasing A by one. Okay, so if I keep decreasing A by one, that means the number of operations that B could do, originally, remember our total number of operations that we could do is N, right? Number of operations we could do is N. If I decrease A by one, so let's say I decrease A by one, so A minus one, right? The number of operations B can do now decreases by one, right? N minus one now decreases by one, right? Because we can't have more than N. Do you guys understand what I'm saying? Like, if I decrease A by one, the total number of operations we could do, because we could do at most N now decreases by one for this one, for B, right? For B, it would decrease by one, right? And if I decrease A again by one, so if I decrease A minus one, minus one again, then the number of operations that I could decrease B by decreases by one again. Okay, do you guys understand what I'm saying? So, if based on this, the number of operations you could do for A, if I'm doing a certain number of operations on A, the number of operations B I could do is going to be N minus the number of operations I did on A. Do you guys understand what I'm going through, right? Do you get what I'm saying? Because we only could keep our total value of N. N is our total number of operations we could do. Do you guys know what I mean? So, now we have to think clearly again, okay? We want to minimize, let's go back to the problem. Now, we want to minimize A times B, okay? We want to minimize A times B. So, what are the two scenarios I could do to minimize A times B? I could keep decreasing A down. I'm going to keep decreasing A down, down, down, until I reach the bounds of X, okay? And once I've hit X, right? That is going to be the number of times I'm decreasing A by, okay? And then I'm going to subtract that number of times from N, okay? I'm going to subtract that number of times from N. So, here, if I decrease A twice, right? I'm going to subtract N that number of times, right? And that number of times I have to subtract A by. That number of times, right? And that number of times I have to subtract from N, that number of times is going to be the number of times I'm going to decrease B by, okay? So, if I'm, and that's just one case I could do, right? Then I could find the product of A and B and see what that gives me. But I also have another case I could do. Instead of decreasing A now, let's say I decrease B. So, if I keep decreasing B down, down, down, down, down, until I reach this bounds of Y, until I reach this, right? What can I do at this point? What can I do? How many times can I decrease A now? Simple. It would be N minus the number of times that I decrease by B, right? Because I only could decrease N times. The number of operations I could take to decrease by one, right? So, if I'm going to do this, our end result is going to be the minimum of the number of times I'm decreasing A by, right? The absolute minimum number of times I decrease A by, right? And then I multiply by whatever value that B is. Then I compare that end result. So, I'm going to have a decrease A, so I decrease A. Certain number of times, right? I decrease A. Certain number of times, right? And then I multiply it by B. That's going to give me a result, right? Now, another case I could do is decrease B. Certain number of times, and I multiply by A. And I'm going to get my other result. So, the minimum multiplication of A times B is going to be the minimum of both of these cases. Decrease A certain number of times multiplied by B, gives me a result. Take that result, find the minimum, and then decrease B some number of times times A, get the result, right? And then get the minimum of both of these results. Both of these results, okay? So now, let's go back to the problem, okay? So now, because I think you guys understand the gist of this problem, let's clear this now. So we have A is greater than or equal to greater than or equal to X, and B is maximum greater than or equal to Y. And we could only do an operation decrease A by one, or decrease B by one, and number of times, okay? So, I'm going to go explain my code on what I did at this point. So, let's go back here. If I'm going to decrease A certain number of times, right? Decrease A, that's going to be the number of times I'm going to do. I'm going to decrease A by one, decrease A by one, right? My bounds, it takes, the bounds it could take. I could only end at X, right? I only could end at X. So the maximum number of times I could do is going to be A minus X. Do you get what I'm saying? Because I'm decreasing A by one every time, every time, every time, decreasing A by one. The number of operations it would take, the maximum number of operations to take to get to A to X is going to be A minus X, right? So the number of operations is going to take, the maximum it would take would be A minus X, okay? Do you guys understand what I'm saying? Because the bounds it takes is going to be X, I have to decrease one into A by X, okay? So that's what I did first. I did A bound is A minus X, because that's going to be the number of moves it would take, okay? Now, what am I going to do now? This is the number of times I'm decreasing A by, right? I'm going to decrease A by, this is the number of times I'm decreasing A by, decrease, decrease, decrease. In the end, I'm going to get X, right? Because I'm decreasing A this number of times I'm going to get X, right? But I have to compare this with N, right? Because if X, let's say X was super duper large, right? Super duper large, and it exceeds N. I can't, I can't, I can't subtract more than N, right? I can't, I can't do A minus X more than N. I owe the maximum number of times I could do is N, right? So I have to subtract the number of operations A minus B, A minus X. This is the number of times I'm subtracting one from A to get to X. I need to compare this with N, right? The minimum number of times, and the number of times I could do is going to be the minimum of A minus X and N, right? Do you guys understand what I mean? Because the maximum, it has to be the minimum, A minus X and N. Because I can, the maximum times number of operations I could do is N, so therefore, the minimum number of operations would be the minimum. The number of operations I could do is going to be the minimum of A minus X and N, okay? Because I can't get, I can't get more than N, right? So that would, otherwise I go, I go past the bounds that it would take, the number of operations of N that it would take for me. So I have to do this. So that's what I do here. I take the minimum of A minus X and N, okay? To make sure that I don't get past N. To make sure I don't get past the number of operations of N. And now what am I going to do here? I'm going to subtract A from this, right? My new A is going to equal to this, right? Because that's going to be the number of times that A gets decreased by, right? And then if we're decreasing one by every time, this is going to be our end result of A, right? Because we're subtracting by one every time, by one every time, right? And this would be the results of our A in the end, this equation. It's going to be the result of A to the end, okay? So now once we have our A, after getting this value of A, which I did here, we need to do B now, right? So remember back when I said the number of operations it would take, right? Is the minimum of A minus X, right? A minus X, right? What is the number of times B could be? Right? If I decrease A by one time, I'm decreasing the total, the B, my total, my B value I could decrease, decreases by one. Remember in the beginning, right? Because our total value that we have is N. If I decrease A by one, my operations I did was I did one operation. So the number of times B could do has decreased by one, right? Because that's the total value of N, I can only do N operations. So if I decrease A by one, then number of times B could do decreases by one. So based on this, based on this understanding of A minus B and N minus one, right, we could formulate an idea to get the number of times B could be decreased by. Assuming we're decreasing A this number of times, the maximum number of times we decrease A. And that would be N minus this minimum number of operations of A minus X. Okay? This is going to be the number of times you're going to decrease B by. Okay? Because of the boundaries, because we could only decrease by N number of times, right? So B could only decrease by this. N minus minimum of A minus X and N. Okay? So because of that, that's the number of times we could decrease by. Right? For B. But, there is a but. Remember, we have a boundary of Y. We can't get more than Y. We can't. If we get past Y, it wouldn't work. Right? The number of times we decrease it wouldn't work. We have to have a boundary. So what do we do? We have to subtract the bounds of B and Y and compare it to this number of operations that we're doing. Okay? So what do I do here? At this point, I subtracted N minus minimum bounds of B minus Y and B bound. So the number of times B could do is going to be this. Right? B minus this. Whoops, I got to go back. It's going to be B is going to equal to B minus the minimum of this and B minus Y. Right? Because if I go past B minus Y if I go past B minus Y I would get, I would end my bounds that I'm at. The bounds, I only could stay I could only decrease certain number of times of Y. So it has to be the minimum. Right? Minimum of B minus Y that's the boundaries it would take number of operations it would take. So that's why I do here is minimum of N minus minimum of A minus X of N and B minus Y. That's what our B is going to equal to. Once we have this we just get our results of A times B. Okay? Once we have this we just get our result of A times B. Okay? Now this is the first scenario. This scenario is the scenario of decreasing A the maximum number of times. Now we have to look at what happens if we decrease B. Okay? Because decreasing A the maximum number of times and then multiplying by B and then decreasing B the maximum number of times multiplied by A and finding the minimum of both of those results would get us our answer. Remember our two scenarios. So now let's go back and decrease B. So let's think about how do we decrease B now? Well it's the same thing pretty much. It's basically the same thing. So remember A could only greater than or equal to X and B is only greater than or equal to Y and our total number of operations we could do is N of decreasing by one. So if I'm going to decrease B it's basically the same thing. We're going to subtract B minus Y and this is going to be the number of operations it take and we have to compare this minimum value with N. And the reason why we're doing this is that we don't get passed by N because if Y is super-duper large and we get passed by N we could the subtraction would be too much, right? We would get, but it's not allowed the maximum number of times you could subtract is, subtract by one is going to be N. So that's why we do this, okay? Min of B minus Y and N. And that's what our B is going to equal to, right? This is the number of times you're subtracting by one and we just subtract B minus this number of times, okay? This number of times. Now to do A it's going to be the same thing. Now we just do N minus this min of B minus Y and N N minus this, right? This is the number of times you would take and then we just take the minimum of the bounds of A minus X. And why do we do A minus X? Because the maximum number of times you could decrease by one is going to be A minus X. We can't go lower than this, right? If we get lower than this if we get more than this then A would become less than X, right? So that's why we have to do this. So then A is going to equal to A minus this, okay? And yeah, that's pretty much it. Then you multiply A and B and then compare that to the first result that we got. So that's what I did here. So yeah, here's the code that I did so I subtract A minus X that's the number of times A could be and I subtract B minus Y number of times B could be and I had a copy of A and a copy of B but that's just like what I did for the different results. I decrease A the maximum number of times possible so that's going to be A minus X compare the minimum of that with N decrease A by that number of times then here we decrease B that's going to be minimum of N minus this number of times so that we decrease by A because our total value that we could decrease is N so we have to do N minus that and then we compare this value with the number of times we could decrease by B which is B minus Y because we can't get lower than Y, right? So that's why we could do a minimum of that with the B minus Y then we just multiply them together okay? A times B and then we do the same thing for B we decrease B the maximum number of times we could decrease B right? for the second case we do B minus Y and compare it with N, minimum of that and then we do A decrease A the number of times is going to be N minus minimum of B B minus Y and then compare that with the number of times we would do it the minimum which is A minus X and then you have your value of the second case of decreasing B the maximum number of times okay? and then after that we just do the minimum of both of these but the both of our results multiply the product of both of our results so A times B and the other scenario of A times B and then that will get your answer that would basically get your answer hope you guys enjoyed this video Ray Comps subscribe it's pretty difficult a little hard to understand but it is what it is though Ray Comps subscribe, I'll check you guys later