 What's up guys like the go-to here today? We're gonna go over the problem for Rita princess for Rita And once upon a time there is a cute princess called for Rita living in a castle with her father On the way to the castle there are many monsters each of them have a gold coin Although there are monsters they will not hurt and said they will give you gold coins If and only if you didn't take any coins from the monsters directly before the current one So to marry princess for Rita you have to pass all the monsters and collect as many gold coins as possible Given the number of gold coins each monster has calculated the maximum number of coins you can collect on your way to the castle Okay So how do you do this problem? Okay, so let's look at the first test case. We have one two three four five and the answer is nine so That's I basically just labeled one two three four five here and our output should be nine, okay So how do we get nine? Okay? Well, we have to think Good thing about this problem is that if you think hard enough you'll be able to get it Okay, so let's actually start with the first monster. So the first monster is one and What is a monster of one? Well What if you're at the first monster, right? There's no monster before it, right? So if remember if you if you look at the problem statement, right? You can only pick the monsters if you didn't take any coins from the monster before it, right? So I only could pick the coins of the current monster if I didn't take any coins before it So in this case if I'm at one, there's no no, there's no monster before it, right? So I didn't take any coins before it so I could literally just pick one, right? Like I just pick one. So if I kill this monster, I could take his coin. So I'll just take one Okay, so let's say let's look at the number of coins we have so let's say this is our coins Now we just pick one. Okay Now let's look at the second monster well, if I pick If I'm at the second monster, right? I Only could pick this second monster If I didn't pick any monster before it, but I already did pick the monster one So I can't actually pick two. You cannot you cannot pick two. Okay, so In there's only two stages. You could do this, right? If I started picking at one Right Then I would have only picked one and I can't pick two Correct. I can't pick the second one, right? Because in order to pick two Right the second monster. I couldn't take any coins for the first monster Correct. So that's why you cannot pick any coins for the first monster. So the answer is only one but now at two Because I couldn't pick any coins with a fur from the one before it. I can't pick two, right? So I'm gonna have to skip this All right Now at three Did I pick any monster before it? No, I didn't pick any monster before it Right. I didn't pick any monster. I didn't pick two So then at this point I could actually add three. So I'm gonna add three to this Okay Now at four did I pick any monster before this? No, I Didn't pick any monster before this. Oh, no, I actually at four at four I picked the monster three so I can't pick four. So at four I cannot pick this. So this is bad Now at five did I pick any monster before this? Yes, I picked I didn't pick four I didn't pick four so I could pick the current monster five So I'm gonna add five and I'll get you nine. Okay, so that's basically answer how you get this Now here's the issue. So what if the monster you picked? So this is a this is what would happen if I started at one, right? But remember you could actually start from two So like remember if I started at Remember we chose here the answer to be nine because we started these the value at one, right? We couldn't pick the value before it. That's the reason why right? So you started at one, right? There's nothing before it. So we just started at one But technically you could just pick starting from two and the reason why is that in now instead of starting at one I'm not gonna pick the values at one. So I could just start from two. So and in this case my coins would be Two right and then then I go to the next one. Okay. Well, did I pick the coin before this I? Did I did so that's why I can't go to three. So for three. I'm gonna skip. So right here I'm gonna skip so I'm not gonna do three and at four. Did I pick the ones before it? No, so I could pick the coins at four. So I pick four and then five. Did I pick coins before it? Yes, so I can't pick five. So this would be six. So if we look at These values, right? We only could pick Choices the coins if you didn't pick the coins prior to it. Correct. So if you would think about this using Using your solutions, right, you should You might be thinking, okay, maybe what if I just skip every two like every single two values I skip and I just pick the values for that now. You could do that But you don't know which one is the best version of which or not, right? I can't just go one two three four one three five seven nine because sometimes that might not be the best possible outcome So what do we do? We store each pot best possible outcome? In an array called DP. So in this array DP the ith value is going to represent the most optimal coins taking so far and the The yeah, the ith I'd represent so far the most optimal coins you picked so far and I minus one is going to represent the optimal Coins you picked prior to it. So I'm gonna show you what I mean by that. So Here's what we're gonna do for each value here. We're gonna store Store it into an array called DP. So this is gonna be the optimal solution. Okay, so first of all Let me just quickly look at the code real quick because uh, Actually, don't okay. Yeah, I remember this. Okay, so In the first two one or two The DP is just gonna equal to one and two. Okay So the reason why it's equal to one and two is because those are the first two choices You could pick right if I pick one and I can't pick two Right and if I pick two and I can't pick Pick one right so at the second choice, it's just gonna be whatever this maximum You could pick from one or two and at the first choice. It's just gonna be just the first choice, right? okay Now from three now, I'm gonna loop is I'm gonna loop through from the second choice to the end of the array Basically, I'm gonna loop through from the second choice to the end of the array and what am I gonna do? Well From the second choice to the end of the array, I'm gonna store the most optimal solution at the current value So in this case, what is the most optimal solution? Well, if I pick three, right? I pick three. I only can add this three to the answer that is Most optimal solution if I pick three because if I pick three I only could pick three if I didn't choose a Choose the monster coins before it right if I didn't choose the monsters coins before it Right, so what is the most optimal solution starting for it from three if I didn't pick the choice coins before it? Well, it's the Value that I chose to to two choices ago, right if you think about it Right, like if if I had added something prior to here like if I added a To here right to here then um, I can't I couldn't pick three with two because this would have been chosen Right, if I picked three with two that means that uh, that means I've killed the monster prior to it The monster before it and I can't do that because it's not allowed Right if you look at the problem statement You only can pick monsters if and only if you didn't take monsters directly before the current one Right, so it's not allowed if I picked the ones before it So the the choice you could only pick is the ones is the choice you picked two I minus two so the trans before before the ones that before the other one so before two I picked one right so I picked this one so if I wanted to pick this one What would be the number of points I have simple it would be the ones two choices ago that I picked here I plus the one that I currently have just three so it's gonna be one plus three so that gives me four right so that's picking The values of two choices will go if I pick the current value But what if I didn't want to pick the current value? Right, what if I didn't want to pick the current value remember? I want to maximum value the most optimal solution for every coins for each of our array Well, you have to compare this new value with the choice you picked prior So the choice that you picked before if I instead of picking starting from one Let's say I want to start from two Well, if I'm starting from two and that means that I couldn't have picked one I couldn't pick one because one is before two right So if I have to compare the current value that I added of three plus one So I have to pick three plus one which gives me four and I have to compare this four with the Value of prior to it of two because if I pick four and if I choose four and two I need to get the maximum value in order to store in my current optimal solution of DP so in this case of which one is greater Four or two well four is greater. So I'm gonna maintain the state of four So four is gonna be my DP state here. Okay, then I'm gonna start going to the next value I'm gonna go to the next value. So what is the next value? The next index is four. Okay Well, if I'm picking four right, how can I pick the current solution of four? How can I pick this? How can I pick four? Well in order to pick four and that means I couldn't have picked the one before The current I picked three and the solution of three, right? I couldn't have picked this because this this this solution of three is This solution of three would only I wouldn't have only picked it if if Because in order to pick three that means I wouldn't have picked the one prior to it So in order to pick four right I have to choose the values two Twice before it right the ones before it. So in this case, I would have to pick two So which value would if I pick four which five would have to do I would have to do four plus two Okay, four plus two. So I'm gonna store the value of four plus the value of two. That's gonna give me six right That's gonna be six and I have to compare the six with the one that I stored prior to it In case if I didn't want to pick the value of four So in order to do that I have to compare six and four So which is the maximum of six and four and that's gonna give me six So the answer here would just be six. Okay. Now we're gonna be at five Okay Now we're at the solution of five and What is what do we do at this solution of five? Well in order in order to have done the In order to have picked five that means I couldn't have picked four So in order to pick couldn't have picked four it that means that I'm gonna take my five And I'm gonna add it to the optimal solution two times before it right twice before it So what is the optimal solution of five twice before it would be four here right four here So I'm gonna take this five and I'm gonna add it with the optimal solution twice before it of four So I'm gonna be five plus four is it gonna be nine then I have to take this nine I have to compare it with the one that had before of Six because in the case of if I didn't choose five Then I would have had this all solution of six So in this case, I'm gonna take nine compare it with six and which is greater nine So maximum of nine and six is gonna equal to nine and that's gonna be my answer Then at the end of the DPR we're gonna return this value nine So that's basically how this solution works. All right, and let's look at the code now Okay, so here I have a Read a number of test cases T Read in T and I read in the number of N. Okay. All right. This is a this is a slight case You have to watch out for if we have n is equal to zero then we have to print out zero Okay, and also another thing you have to realize is that a case? This is uppercase case and then I Okay, you have to print out the case number current case and then the current number you have because That's just one that will have what they wanted to output So like if you look at case one case two if two cases you have to print out case one Then your answer of nine and then the second case Which your input statement and the the solution for that case also so for that For the special case of any equal to zero you have to print out case and I okay So our my index of I is just representing looping through from the current test case Okay from one to T So I read in number test case and loop through from one T and then I print out zero, okay And then I do a continue to skip to the next test case. All right, so now at Now I have to create my two arrays. So here I have vector long long int which is going to represent the array and Then I have the vector int long long int Dp my DP array to store the current optimal solution So now here what I'm going to do is I'm going to loop through all the values of my array And then just read in all the values right read in all the values of my DP array of R of I Okay And what am I going to do? Okay, so this is going to be a little a little little tricky a Little difficult Okay, so it's actually not that difficult. Okay, so at the current solution of DP is zero I'm going to set it to the first value of my array So that's just like the same thing of what I explained prior hand, right for one If you pick the value of one you couldn't have picked the ones before it So the only solution you could pick if you pick the value one is just going to be DP of one So that's just going to be DP of one for that case Okay, and then the second solution second case DP of one At the At one that's just going to be the maximum of the first and second number So the maximum of the first second number one and two which is going to be two So that's why we do DP of zero and one. We're just going to give us two. Okay, so Yeah, that's what we store here maximum of the prior number and part of one. All right now in order to build a DP array, we're going to loop through from two to the We'll loop through to starting index of this part. This isn't this is index from index. Okay, so This this is actually the array This is the array and remember we're actually indexing my my array is here is indexed from Zero, but you can index it from one any in your solutions. It really doesn't matter. But yeah, I indexed it from zero so My starting index is going to start from two and I'm a loop through to the value of n. Okay now What do I do for every optimal solution? I'm going to take the previous choice that I had I minus one DP of the previous choice I had compare it and it's going to equal to the maximum of the previous choice I had and the previous choice that I had two choices ago plus the current value I have currently so remember That's going to compare the previous choice you have currently In order to pick the current value of my current value of three. That means two choices ago. I have to add The choices I had two choices ago of one plus three, right? I can't you can't pick that you can't add the previous choice here because they said that you only can pick coins You only can pick coins if you didn't pick the coins directly before the current one So that's why your answer would have to be two choices ago So it would be the DP of the two choices ago plus the current value at I and you have to find the maximum of your previous choice So yeah, that builds up your DP array And you have to print out your case and the current case number And then you have to print out DP of n-1, which is the last value of your DP array and that's your case space So yeah, that's basically they just had to do this problem. I hope you guys enjoy this video Remember my array LL long long is just representing the Long long int I actually use a little as long and because The test cases was pretty large. So yeah, I use long long in but you but I don't know if you could use long long But yeah That doesn't matter that much for our vector for our array. But yeah, I hope you guys enjoyed this video where you come subscribe I'll check you guys later. Peace