Codeforces celebrates 10 years! We are pleased to announce the crowdfunding-campaign. Congratulate us by the link https://codeforces.com/10years. ×

### Ashishgup's blog

By Ashishgup, history, 18 months ago, ,

I hope you guys enjoyed the contest — I hope to host another one soon :D

With that said, here are the tutorials:

Author's Code: 42591712

Author's Code: 42591830

Author's Code: 42591846

Author's Code: 42591882

Author's Code: 42592019

Author's Code: 42592026

Tester(_kun_)'s Code: 42593206

For O(n3) or O(n2) solution, refer to this comment by evil666man

• +172

 » 18 months ago, # |   +13 The proof to the greedy approach in Problem C is interesting!
•  » » 18 months ago, # ^ |   0 I didn't understand the +0.5 part, can someone explain please ?
•  » » » 18 months ago, # ^ |   0 me too
•  » » » 18 months ago, # ^ |   0 Me too
•  » » » 18 months ago, # ^ | ← Rev. 2 →   +10 For my understanding:Let A be the sum of a[1] / 2 + a[2] / 2 + a[3] / 2 + ... + a[n] / 2,and B be the sum of b[1] / 2 + b[2] / 2 + b[3] / 2 + ... + b[n] / 2.The list of the 1st player becomes a' = {a[1] / 2, a[2] / 2, ..., a[n] / 2}.The list of the 2nd player becomes b' = {b[1] / 2, b[2] / 2, ..., b[n] / 2}.Now, the problem tells us that the 1st player will play to maximize the amount A - B. For this player the process of choosing a number a[i] is equal to adding the halved-value element a'[i] to A, and choosing a number b[i] is equal to subtracting the halved-value element b'[i] from B, so the 1st player has two choices: either to increase A or to decrease B, and with any of them the amount A - B will increase, so he should choose the element that affects more, so he should choose the maximum element between a[i] and b[i] (where a[i] and b[i] are the maximum elements in a and b, respectively).For the second player, he want to maximize the amount B - A, and in a same discussion as above we can see that he should choose the maximum element.Hope this will help.
•  » » » » 18 months ago, # ^ |   +3 @MahmoudAlio Thanks for your answer, unfortunately I still don't understand the /2 ? why don't you do the same reasoning without dividing by 2 on both sides ?
•  » » » » » 18 months ago, # ^ | ← Rev. 3 →   0 It's a way to show how choosing an element from the second list affects the answer of the first player.Because, without /2, when the turn is, for example, to the first player, adding an element from his list will increase A, so will increase the difference A - B, but removing an element from the 2nd player's list will not increase A neither decrease B, so the amount A - B will not change.But sometimes, we can see that removing elements from the other's list will help more than adding elements from my list. And to show this, the /2 was helpful.Anyway, I'm sorry that I couldn't help you, maybe someone else will help more.
•  » » » » » » 18 months ago, # ^ |   0 Can you tell me how I_love_Tanya_Romanova solve this problem. Here is the link 42562135
•  » » » » » » » 18 months ago, # ^ |   0 My solution is the same as one described in editorial, and the reasoning that I used during the contest is similar to what described in editorial — thinking like "both taking X away from your opponent and picking X from your list has same impact on your final score". I'm just simulating the game and keeping track of scores.
•  » » » » » » » 18 months ago, # ^ | ← Rev. 2 →   0 He stores the two lists in one vector, then sorts this vector (then reverses it, so sorts them in non-increasing order), and continues choosing the remaining biggest number.The editorial, instead, stores each list in an array, then sorts them both, and keeps one pointer on the remaining biggest number in the first list, and another pointer on the remaining biggest number in the second list, then it chooses the biggest between the two number that the two pointers points to.
•  » » 18 months ago, # ^ |   0  + 1
 » 18 months ago, # |   +67 Excellently organized contest with short and precise problem statements.Well done Ashishgup and Mahir83 !
 » 18 months ago, # | ← Rev. 4 →   +26 Problem E. We can delete all loops because if we stay on vertex we can take all loops.O(n) solution we know that number of vertexes which has odd degree can be only 0, 2, 4. Because if 3 vertex has odd degree sum of all degree not divide on 2. It means if number of vertexes which has odd degree 0 or 2 graph has Euler path, it means we can take all edges. If number of vertex which has odd degree 4 we have two case. 1 case number of components equal 1 and number of components equal 2. If number of components 2. Each component has Euler path we take max. If number of components equal 1, we can take all edges except 1 edge. Sorry for my english.
•  » » 18 months ago, # ^ |   0 "It means if number of vertex which has odd degree 0 or 2 graph has Euler path, it means we can take all edges." No, if the graph has edges in different connected components, there is no Euler path in this graph.
•  » » » 18 months ago, # ^ |   +8 I mean each component has Euler path. It means we can take all edges in component.
•  » » » » 18 months ago, # ^ |   0 Okay. What about this?"If number of vertex which has odd degree 4 ... If number of components equal 1, we can take all edges except 1 edge." By deleting this edge, we can make this graph disconnected.
•  » » » » » 18 months ago, # ^ | ← Rev. 2 →   +11 No. Because obviously that at leas two vertexes should has degree grater than 1. It means if we delete edge number of components didn't change or we just delete one vertex.
•  » » » » » » 18 months ago, # ^ |   0 If the graph contains loops, this statement is untruth.Example: test #117. Input6 1 10 1 1 2 2 2 10 3 2 20 3 3 1 4 4 10 4 Visualisation: ┌-┐ └-1---2 / \ | | \ / ┌-4---3 └-┘ If you delete any edge from this graph, it will not have Eulerian path.
•  » » » » » » » 18 months ago, # ^ | ← Rev. 3 →   +11 I told we delete all loops in begin and in the end add to answer. We can delete loops because if stay in some vertex we can visit all loops in this vertex.
•  » » » » » » » » 18 months ago, # ^ |   -11 So some deleted loop can become new connected component. Look at my example in previous message.
•  » » » » » » » » » 18 months ago, # ^ |   +6 We delete loops in start. We calc degree of vertex without loops.
•  » » 18 months ago, # ^ |   0 My AC solution 42591906
•  » » » 18 months ago, # ^ |   0 Submit again and see if it passes test #117 :P
•  » » » » 18 months ago, # ^ |   0 LoL. Another submit AC:D 42595244
•  » » » » » 18 months ago, # ^ |   0 Also wrong :P 5 1 1 2 2 10 3 3 10 4 2 10 4 3 10 4 
•  » » » » » » 18 months ago, # ^ |   0 Hmmm stupid bug( 42612109
•  » » » 18 months ago, # ^ |   0 I require some assistance in understanding your solution. Firstly, the number of vertices with odd degree is 0, 2 or 4. It cannot be 1 or 3 because then the sum of degrees of each vertex would not be even. Case 1 — The number of odd-degree vertices is 0 We can take the whole graphCase 2 — The number of odd-degree vertices is 2. You have written that we can take the whole graph, but I did not understand why. Please explain. Case 3 — The number of odd-degree vertices is 4.Case 3a — There is one component — Take the full graphCase 3b — There are two components — Take whichever component has a higher sum. I hope I summarised your solution correctly, but I didn't understand Case 2 and Case 3. Please help me understand your solution.
•  » » » » 18 months ago, # ^ | ← Rev. 2 →   +5 This isn't quite right. For case 3a, we take the full graph except for a single edge. It's well known that a graph has an Euler circuit if and only if there are no vertices with odd degree, and a graph has an Euler path if and only if there are exactly 0 or 2 vertices with odd degree. This means in these cases we can take every edge.When we have 4 vertices of odd degree and a single connected component, we can remove an edge to get back to the earlier case. Therefore we should remove the minimum weight edge that connects two different nodes.If there's two components, we can obviously only take one of them. This is a graph with two nodes of odd degree, which has an Euler path. Take all the edges in this component.
•  » » » » » 14 months ago, # ^ |   0 As you said before, "When we have 4 vertices of odd degree and a single connected component, we can remove an edge to get back to the earlier case. Therefore we should remove the minimum weight edge that connects two different nodes."I am doing this but this gives WA I think this is happening because if after deleting the "minimum weight edge" , the graph may divides into two divided components resulting in the different answer. Test case 117 is about this only. 6 1 10 1 1 2 2 2 10 3 2 20 3 3 1 4 4 10 4We should delete "3 1 4" but this will make graph disconnected and we will also have to remove the loop on 4 itself .
•  » » 18 months ago, # ^ |   0 This solution is not O(n). It is plain wrong if n > 4.
•  » » » 18 months ago, # ^ |   0 Sorry O(M).
•  » » » » 18 months ago, # ^ |   +1 You probably don't understand big-O notation. Solution works in O(f(n)) if there exist c > 0 and N such that for all n > N the number of operations is no more than c·f(n).But your solution can't work for n > 4.
•  » » » » » 18 months ago, # ^ |   +16 I think you might be talking about different things. In this problem, there are always at most four vertices, and n is the number of edges. The solution is correct (because the problem is only defined for small number of vertices) and O(n).
 » 18 months ago, # |   +76 F can be solved in O(n3), by counting strings based on the KMP state after reading whole string. As string is cyclic we want the KMP state to start and end with same state and somewhere in between the KMP state should be |S|. See my code for details
•  » » 18 months ago, # ^ |   +16 Interesting solution! I have added a link to your comment in the editorial :)
•  » » » 18 months ago, # ^ |   +61 It can also be reduced to O(n2) by doing forward and backward dp from the first position where KMP state becomes |S|. Code
•  » » » » 17 months ago, # ^ |   -10 How does the O(n^2) taking more execution time in compare to O(n^3)? evil666man Can you please clarify your approach or explain your solution a little bit more?
•  » » 17 months ago, # ^ |   0 Can u explain more clearly ? Please.
 » 18 months ago, # |   0 In Problem D, Why in the solution code of the author he say that when all of numbers are positive or all of them are negative then answer is ans-2*minValue ?????
•  » » 18 months ago, # ^ |   0 Because he adds it to the variable ans in the loop.For example, a = {3, 4, 5, 6, 7}, the answer of this case is:4 + 5 + 6 + 7 - 3  =  19.But in the loop he adds all the element to ans, so ans have 3 in it, so he should remove the number 3 two times from ans in that case:ans = 3 + 4 + 5 + 6 + 7 = 25, so the correct answer is, ans - 3 - 3.
•  » » 17 months ago, # ^ | ← Rev. 3 →   0 cuz you waste the least number (after taking the absolute value into account) so you dont use it at all and you delete it's value from some other number in the sequence ex: n=5sequence is : 1 2 3 4 5then we takenote:(the number after word eat denote the eat process number not any value of any slime) eat 1:1-2=-1then using -1 eat all but leave just one number (i.e the last number)continue eating eat 2:-1-3=-4eat 3:-4-4=-8eat 4: here we use the last one 5 to eat our result (-8)5-(-8)=13so you can see that we didn't add the least number which is 1 to our result and we subtract its value from the final result ->>(that happens when we say 1-2 to get our -ve slime)so we sum all the values we have then subtract minimum of it twice first time mean we don't use it second time mean first eat (1-2)
 » 18 months ago, # | ← Rev. 2 →   +30 I think The easiest way to solve problem B was to take 'n' in one set and the rest 1 to n-1 in another set. why does this work? As the sum of 1 to n-1 is n * (n-1)/2, so, the gcd of n and n*(n-1)/2 is always greater than 1. My Submission : http://codeforces.com/contest/1038/submission/42567986
•  » » 18 months ago, # ^ |   0 How did u reach the conclusion that gcd of n and n*(n-1)/2 is always >1
•  » » » 18 months ago, # ^ |   +2 If n odd it means n — 1 is even hence n * (n — 1) / 2 is divisible by n. If n even let say n = 2 * k it mean n * (n — 1) / 2 = k * (2k — 1) is divisible by k and 2 * k is divisible by k.
•  » » » » 18 months ago, # ^ |   0 Thanks!!!
•  » » » 18 months ago, # ^ |   -6 you can take n common from n*(n-1)/2 and n*1, as n is dividing both of the numbers.
•  » » » » 18 months ago, # ^ | ← Rev. 2 →   0 Definitely wrong!!!! check the above comment
•  » » » » » 18 months ago, # ^ |   0 Yes, Thanks!
•  » » » » 16 months ago, # ^ |   0 yes that seems like the obvious explanation. but it isn't true because if u take n common what is left in the first part is (n-1)/2 and it is not guaranteed that it is an integer as (n-1) could be odd or even.
 » 18 months ago, # |   0 I m not able to get AC even after referring to solution for problem C. Please help !!!I have attached link to my solution 42593499
•  » » 18 months ago, # ^ |   0 I'm not exactly sure why did you use sum of arrays a and b. You don't need it. Refer to my solution if you want, I think it's same as editorial says. I made 2 functions, one for player A, one for B. I hope it helps :D
•  » » » 18 months ago, # ^ |   0 Brankonymous: u have sequentially add the values selected by the players from their own to ans_a, ans_b respectively and i have subtracted the values from suma and sumb that have been removed by opponent.I didn't understand why there is comparison of a[f] and b[g] in your solution ? Frankly speaking I didn't even understand argument about "each player gets 0.5 of all numbers in his list" given in editorial which is used to explain O(nlogn) solution.
•  » » » » 18 months ago, # ^ | ← Rev. 3 →   0 Well I use f to have index array a, and g to have index of array b. Sort array and declare f ang to n-1 (because that index is biggest) And let's look at player A because he goes first. If he sees that b[g] is bigger than a[f] that means that optimal solution is to substract g by 1, so player B won't put b[g] in his list. Otherwise put a[f] in A's list. Because we used a[f] substract f by 1 so that we will se biggest number in a not used.Do the similar thing for player B.And do it until f and g is out of bounderies.
 » 18 months ago, # | ← Rev. 2 →   0 still, haven't understood the problem D... So, anybody can give the proof of correctness for D???? thanks in advance....
•  » » 18 months ago, # ^ | ← Rev. 2 →   +10 Let pos be the number of positive numbers in the array,and neg be the number of negative numbers in the array.If pos > 0 and neg > 0, so you can let each negative number eat the positive numbers to the left and right of it, so the result of each process of eating will be a negative number also (because negative - positive = negative), and keep one positive number alive, so the array will become like following: {some negative elements, one positive element, some negative elements}, so this remaining positive element can eat any negative element with still being positive (because positive - negative = positive), so the answer of this case is the sum of all absolute values of the initial values.But when all the elements are positive (neg = 0), we should create a negative element to return to the previous case. And to create a negative element in this case we should let an element eat any other neighbor element which is greater than the element which eats, for example, a[i] - a[i + 1] = negative element, after that the answer will be the sum of the absolute values of the values remaining after this process of eating (as the previous case exactly). But we shouldn't forget that we remove an element (i.e. a[i]) to create the negative element, so we should choose a[i] to be as small as possible.And when all the elements are negative (pos = 0), we should create a positive element to return to the first case. And choosing the element that has to eat other element is discussed as the previous case.
•  » » » 18 months ago, # ^ |   +2 Thank you..
•  » » » 18 months ago, # ^ |   0 how we can solve it using DP
•  » » » 17 months ago, # ^ |   +3 Many thanks mate ! You told the intuition behind the editorial. It really helped a lot.
•  » » » » 17 months ago, # ^ |   0 Happy to hear this :D. Wish you all the best!
 » 18 months ago, # |   +39 Thanks for allowing exponential solutions to pass for F! http://codeforces.com/contest/1038/submission/42588924Let m=s.size(). If m>=20, we just fix s as the prefix of t and brute force the remaining 2^(n-m) possibilities and carefully count the number of rotations that will work. This part is O(2^(n/2)*n)Else, we brute force the prefix of length (m-1) of t and there are 2^(m-1) possibilities. This is a standard trick to convert cyclic dp to array dp, which is easier to think of for stupid people like me.
•  » » 18 months ago, # ^ | ← Rev. 2 →   -24 That's a nice approach as well. We were wondering if there was an exponential approach to solving this problem, while setting it up, but failed to came up with one! Something new learnt :)
 » 18 months ago, # |   0 Alternative solution for E (harder to implement but was easier to come up with for me): While there are 3 or more edges between two colors, we can remove two most valuable edges and note that if any of these vertices is visited than we need to add the value of all corresponding removed edges. Now there are no more than 2 (edges per color-pair) * 6 (color-pairs) = 12 edges. The graph became small enough to bruteforce all possible edge-paths: After making 11 steps there will be only 1 edge left so we won't have a choice at that point. First 11 times there will be no more than 3 (number of other colors) edges to choose from. This gives an upper bound of 3^11 = 177147 node traversals. We will need to try starting at all four colors so the final number is 4 * 3^11 = 708588. In practise it works surprisingly fast: 31ms. If you for some reason would like to look at my solution: https://codeforces.com/contest/1038/submission/42595983
 » 18 months ago, # | ← Rev. 4 →   +18 My alternative bruteforce Solution for E, without any nasty graph theory and bitmasks https://codeforces.com/contest/1038/submission/42586454the Idea here of this solution is that we can divide the blocks into 6 type of block (block with color (1,2), (1,3), (1,4), (2,3), (2,4) (3,4) ) (since (1,2) is equal to (2,1))for blocks that has the same color (1,1) (2,2) (3,3) (4,4). we could count it greedily since if we could choose one blocks of a type, we could choose all blocks of that type. Lastly we could brute force all of the possible quantity for block with type (1,2), (1,3) (1,4), (2,3), (2,4), (3,4) and check whether it possible to create a valid sequence of blocks. then, for each case find the maximum.At the worst case, the blocks will be divided equally (16-17 blocks for each type) The overall time complexity is O(6 * (n/6)^6) ~= 128600823.04526755, which is fit the time limitCorrect me if i wrong and sorry for my bad england.
•  » » 18 months ago, # ^ |   +8 Good to see that someone else went through the same hell-ish implementation :P
 » 18 months ago, # |   0 Can Anyone help me with the dp approach that author was tring to convey( about the 4*n states)..It shall be really helpful!
 » 18 months ago, # |   +3 In problem D, Can anyone prove how "any combination of + and − is obtainable"?And what train of thought leads to this conclusion?
•  » » 18 months ago, # ^ | ← Rev. 2 →   0 In this comment, I tried to explain how I was led to the solution.
 » 18 months ago, # |   0 In problem D I'm getting WA in test case 35.It's very big test case.So,Can anyone give small counter test case?My submission
•  » » 18 months ago, # ^ |   +3 3-1 -2 -3
•  » » » 18 months ago, # ^ |   0 Thanks
•  » » 18 months ago, # ^ | ← Rev. 2 →   0 Any test with all elements being negative (except when n = 1) will work.
•  » » » 18 months ago, # ^ |   +3 Thanks
 » 18 months ago, # |   0 Can anyone give a proof of why the greedy solution is correct for Problem C. I read the editorial and comments but I'm still confused why it works. Thanks in advance!!
 » 18 months ago, # |   -8 i solved problem D by greedy approach as mentioned above.. please someOne explain DP Solution for problem D Slime?
•  » » 18 months ago, # ^ |   +11 I agree with you!
•  » » 18 months ago, # ^ |   +1 Done
 » 18 months ago, # | ← Rev. 3 →   +27 F can also be solved in O(n2) using inclusion-exclusion principle. Implementation: https://codeforces.com/contest/1038/submission/42615092Explanation:Let n be the length of target string and m the length of pattern. Let Ai be a set of cyclical strings containing pattern at position i. The answer to the problem is the size of . We can write it using inclusion-exclusion principle as: Now consider dynamic programming f(k) as contribution (in the sum above) of subsets J of {0, ..., k} containing 0 and k, but forgetting about bits on positions  > k (so f(k) is contribution divided by 2irrelevant bits). Having values of f(k) computed we can get the answer in the following way: (for each k we need to multiply f(k) by count of "non-wrapping shifts" of positions set (all of them contain 0 and k) and fill in the forgotten bits). How to compute f(k)? Let valid(i) be the set of positions j such that we can place pattern at position i and j simultaneously. f(k) is non-zero only for since counted sets always contain 0 and k. For such k we consider all possible previous positions of pattern and fill not covered bits. Whole sum is negated since we increase power of -1 in inclusion-exclusion equation. Notice that valid(i) is just cyclical shift of positions in valid(0), it's introduced for notation. valid(0) can be computed naively in O(nm) or O(n + m) using KMP.
•  » » 17 months ago, # ^ |   0 Hello, did you make a mistake by accident? Your f(k) is either 0 or negative, so your answer must be a non-positive number.
•  » » » 17 months ago, # ^ |   0 I forgot to mention that f(0) = 1 (base case), I'll edit the answer. Thanks for noticing.
 » 18 months ago, # |   0 Can someone tell me how this work to solve the problem E?
•  » » 18 months ago, # ^ |   +8 That solution actually fails on test 117. However, to explain it, the main idea is that every graph has an even number of odd degree vertices.If a connected graph has at most 2 odd degree vertices, then there is an eulerian path (a path that visits all edges exactly once). Therefore, every connected component in the graph with at most 2 odd degree vertices has a path that visits all edges and the answer is just the sum of all edges' values. However, when there are 4 odd degree vertices (all of them), one (wrong) approach is to just remove the edge with minimum value. This fails because you may remove a loop, which won't change the parity of the vertices or you may remove a bridge which will make the graph disconnected (that's case 117).
•  » » » 18 months ago, # ^ |   0 Thanks a lot to explain this.
 » 18 months ago, # |   0 How can we solve problem D using DP ?
 » 18 months ago, # |   0 In problem C: what's the induction proof to show DP == Greedy ?? Some hints plz
 » 18 months ago, # |   0 i can't understand the solution of problem D any help ?
 » 17 months ago, # |   0 Can someone explain this solution to problem F? 42584804
 » 17 months ago, # | ← Rev. 6 →   0 UPD. I wrote something stupid and found editorial could do nearly the same thing.Whatever, for problem C, follow the proving idea in editorial, we can have a shorter solution. my submission (This code is different from the proof in editorial, but I think they are similar if you wrote a version based on proving idea in editorial.)
 » 17 months ago, # | ← Rev. 2 →   0 How does bool checker() in author's solution on problem E work ?
 » 17 months ago, # |   0 just wondering if there exists a solution for problem E that involves minimum cost flows, thanks and good round btw :)
 » 17 months ago, # |   0 Details for pF please
 » 17 months ago, # | ← Rev. 2 →   +8 There is an interesting O(2n / 2n2) solution for the Problem F. In the case , enumerate the rest part and count different number of string; In the case , enumerate the first L-1 chars of the string and do a O(n2) dp. The total time complexity is O(2n / 2n2) and I got AC. My code : http://codeforces.com/contest/1038/submission/42792131
 » 17 months ago, # |   +5 " The key observation to solving the problem is that any combination of + and − is obtainable, except where all signs are + or all are − " How to prove this ?
 » 17 months ago, # |   +1 Another solution for the Problem E. Just brute force and greedy.For every node (1 2 3 4) , we fix its matching priority. For example, node 1, match 2 first, then match 4, and then 3.Why? If we choose three edges in our answer, for example, 1-2 , 1-3 , 1-2 . This means we can at least go back to node 1 twice. So why not choose the matching nodes in a fixed order ? (1-3, 1-2, 1-2) This means we must select node 3 before select node 2 when we are at node 1. This can be done for every nodes. And brute force matching priority only cost O((3!)^4) , the specific complexity depends on your implementation.check my code for more details. 42952879Sorry for my extremely poor English.
 » 16 months ago, # |   0 Ashishgup , How can we solve problem D with DP ,could you please explain in brief ?
 » 15 months ago, # |   0 1038B O(1) solution (its not about output data), but for n>2 answer is Yes, and you need to put (n+1)/2 to first set, and all other numbers to second set, and No otherwise
 » 14 months ago, # | ← Rev. 2 →   0 For D problem The Slimes one for the test case 41 1 -1 -1,According to editorial the answer should be 4 but there is no sequence in which the slimes can eat to get the answer 4 or maybe I am missing something,According to me the answer for this test case should be 2
•  » » 11 months ago, # ^ | ← Rev. 2 →   0 The following transformation occurs:1 1 -1 -11 2 -11 -34Hope you understood. ronaldo123
 » 11 months ago, # |   0 I didn't understand the dp solution for problem D.
•  » » 11 months ago, # ^ |   0 plus and minus signs has to be placed before each element of the array in such a way that there is atleast one +ve sign and atleast one -ve sign. So while assigning the signs, keep track of whether +ve sign has been assigned or not. It is possible to keep track of this information by introducing a state p to the dp, p = 0 means + has not yet been assigned and p = 1 means + has been assigned. Introducing another variable to the dp state can track whether -ve sign has been assigned or not in a similar fashion.So there can be 4n states. You're welcome to check my code
•  » » » 11 months ago, # ^ |   0 Sorry but what is your dp state denoting
•  » » » » 11 months ago, # ^ | ← Rev. 2 →   0 dp[i][p][q] stores the answer if we were to consider only the slimes from i,i+1...n. Here, p=0 means that atleast one +ve sign has already been used before some slime 1,2...i-1. p=1 means that no +ve sign has been used over the segment 1,2...i-1. Same can be said for q which is used for -ve sign.
•  » » » » » 11 months ago, # ^ |   0 Thanks I understood. I implemented an iterative solution here https://codeforces.com/contest/1038/submission/51602779
•  » » » » » » 5 months ago, # ^ |   0 Could you please explain your solution...
 » 10 months ago, # |   0 My solution for problem C(Gambling)https://pastebin.com/Z3FLxML1
 » 10 months ago, # |   0 My solution to problem C(Gambling) My Code
 » 7 months ago, # |   0 Wow. Ques 2D is great. I couldn't think of anything else than matrix chain multiplication.
 » 3 months ago, # |   0 So,for problem D I have the proving process, and the Solution ignore it,the code ID:66302024 Welcome to Hack~
 » 3 months ago, # |   0 Could not understand B
 » 2 months ago, # | ← Rev. 3 →   +1 Most easy approach vll =vectorint main() { FAST; ll n; cin>>n; vll a(n); for(ll i=0;i>a[i]; sort(a.rbegin(),a.rend()); vll vis(n,0); for(ll i=1;i0) { a[n-1]-=a[i]; vis[i]=1; } } `for(ll i=1;i