 What's up guys, my name is Michael and welcome to my YouTube channel today. We're gonna go over at code or beginner contest 171 d Replacing so basically you're given a sequence a of n positive integers a 1 a 2 up to a n and then basically what they want to do Is you're gonna replace every element whose value is bi with ci and then print out the sum of a after the ith operation So as we could see here this problem isn't very difficult when you think about it But I'll explain what I mean by it. So let's say we have one two three four The sum of one plus two plus three plus four is let's just see one plus two plus It's ten right so now if you were to replace one all the occurrences of one with two You're going to get two plus two plus three plus four And that is two plus two plus three plus four is 11. So then they print out 11 Now afterwards We're gonna read in three and four now We're gonna replace all occurrences of three with four and what does that give you that gives you? 12 after summing it then after that we're gonna replace all occurrences of two with four And what does that give you that gives you four four four and that's just 16 so I'm just gonna go over the the actually the The the optimal solution on this problem because uh It's actually not that difficult. So here I'm gonna show you what The editorials did so here So what you want to do Essentially is you want to keep track of the number of occurrences of every single element and you're going to sum up all the numbers First right so here what I did was I read in Read in and the number of elements and then I had of the Array of numbers. I'm reading. I go through And of the number of elements I'm reading I'm gonna read in every single element and I'm gonna keep track of the number of occurrences using a map So I'm gonna map every single number with the number of occurrences by doing occur at Whatever number I'm reading and then plus plus and then I'm gonna keep track a variable of sum that sums up all the numbers So some plus equal to a of I so this is just like the beginning sum so now Now what I'm gonna do is I'm gonna read in the number of test cases because that's what they require us to do So I do while q minus minus c and q and I'm gonna read in b and c and b is the number That you want to replace it with c so if you do a bunch of Do a bunch of trials you realize that the sum is actually you know The sum is actually not that There's a pattern you realize okay What you realize that you're actually just increasing the The difference between c and b and then multiplying by the number of occurrences of b, right? So like if I'm doing Let's say go. Let's go back to the problem. Whoops. Sorry guys. Let's say I go back to the problem of Let's go let's go back to this problem So let's say I'm replacing all the occurrences of one with two and one and that's basically just adding the initial sum By two minus one and then multiplying the number of occurrences of one Right because the difference of the sum is now changed based on the difference of what your Number you're replacing it subtracting by the initial the initial value, right? So that's going to be the number that's going to be the increase from the initial sum Right, so then you don't have to recalculate some over and over again You just have to calculate the sum of the beginning and then find how much difference it is that it's increasing so basically all you have to do is just Calculate the initial sum like what I did before and then you Every time you read in the numbers that you want to replace it with like the initial number and the number you want to replace it with You're going to subtract C minus B right, and I'll give you the The values that you're going to increase it by and then you just multiply the number of occurrences of the initial number So in this case it would be one two minus one, which is one right and multiply the number of occurrences of one So there's only since there's only one one it's going to increase their initial sum Which was ten by one so ten is going to be plus one and I'll get you eleven If you look at the second test case three four Oh, yeah, you also have to update the number of occurrences So then after that the number of occurrences of one is going to drop down to zero and then the initial number One of the number of occurrences of two is going to increase by one, right? So then now now when you update it you just have you just keep track of that new number of occurrences Right because like we're after we've replaced one with two the number of occurrences of one Doesn't exist anymore and then to the number of occurrences of two gets increased Right, so that's that's what we do. So I'll go back to the code of what I did before so my submissions so in here basically what I did was after I've Calculated the difference that I'm increasing it by and multiplying by the number of occurrences. I'm replacing it by I Just print out my sum and then I add the number of the number of occurrences of the new one plus equal the occurrences of B Right because I'm replacing so remember let's go back to the right case since I'm replacing one with two right The number of occurrences of two now gets increased by how many ones there are In the array so that's why I do do this occurrence of C plus equal the currents of B So this would add up The number of times two One showed up so by two right? So then the number of occurrences of two is now increased by the number of occurrences of one So that's what it does then I set the number of times One goes back to zero, right? So that's what this is the initial value that goes back to zero So yeah, that's basically how you do this problem What I'll explain it again you want to keep track of the The difference of what's the know the times it's increasing it by right? So so if I'm replacing one with two, I'm gonna see how much my initial sum is gonna increase by Right, and then I'm gonna take that and multiply by how many the number of times one occurred inside the array So that's what this does And then I'm gonna print out the sum but while you're doing so you also have to update The number of times two goes right so because of two occurs one time You have to increase it so now number of times two now gets increased by one and that's what this does. So yeah I hope that I hope I explained that correctly, but yeah, that's how you do this problem. Very calm subscribe I'll check you guys later. Peace