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

With that said, here are the tutorials:

Tutorial is loading...

Author's Code: 42591712

Tutorial is loading...

Author's Code: 42591830

Tutorial is loading...

Author's Code: 42591846

Tutorial is loading...

Author's Code: 42591882

Tutorial is loading...

Author's Code: 42592019

Tutorial is loading...

Author's Code: 42592026

Tester(_kun_)'s Code: 42593206

For *O*(*n*^{3}) or *O*(*n*^{2}) solution, refer to this comment by evil666man

The proof to the greedy approach in Problem C is interesting!

I didn't understand the +0.5 part, can someone explain please ?

me too

Me too

For my understanding:

Let

Abe the sum ofa[1] / 2 +a[2] / 2 +a[3] / 2 + ... +a[n] / 2,and

Bbe the sum ofb[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 numbera[i] is equal to adding the halved-value elementa'[i] toA, and choosing a numberb[i] is equal to subtracting the halved-value elementb'[i] fromB, so the 1st player has two choices: either to increaseAor to decreaseB, and with any of them the amountA-Bwill increase, so he should choose the element that affects more, so he should choose the maximum element betweena[i] andb[i] (wherea[i] andb[i] are the maximum elements inaandb, 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.

@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 ?

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 differenceA-B, but removing an element from the 2nd player's list will not increaseAneither decreaseB, so the amountA-Bwill 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.

Can you tell me how I_love_Tanya_Romanova solve this problem. Here is the link 42562135

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.

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.

+ 1

Excellently organized contest with short and precise problem statements.

Well done Ashishgup and Mahir83 !

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.

"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.

I mean each component has Euler path. It means we can take all edges in component.

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.

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.

If the graph contains loops, this statement is untruth.

Example: test #117.

InputVisualisation:

If you delete any edge from this graph, it will not have Eulerian path.

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.

So some deleted loop can become new connected component. Look at my example in previous message.

We delete loops in start. We calc degree of vertex without loops.

My AC solution 42591906

Submit again and see if it passes test #117 :P

LoL. Another submit AC:D 42595244

Also wrong :P

Hmmm stupid bug( 42612109

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 graph

Case 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 graph

Case 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.

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.

This solution is not

O(n). It is plain wrong ifn> 4.Sorry O(M).

You probably don't understand big-O notation. Solution works in

O(f(n)) if there existc> 0 andNsuch that for alln>Nthe number of operations is no more thanc·f(n).But your solution can't work for

n> 4.I think you might be talking about different things. In this problem, there are always at most four vertices, and

nis the number of edges. The solution is correct (because the problem is only defined for small number of vertices) andO(n).Fcan be solved inO(n^{3}), 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 detailsInteresting solution! I have added a link to your comment in the editorial :)

It can also be reduced to

O(n^{2}) by doing forward and backward dp from the first position where KMP state becomes |S|. CodeHow 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?

Can u explain more clearly ? Please.

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 ?????

Because he adds it to the variable

ansin 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, soanshave 3 in it, so he should remove the number 3 two times fromansin that case:ans= 3 + 4 + 5 + 6 + 7 = 25, so the correct answer is,ans- 3 - 3.cuz you waste the least number (after taking the absolute value into account) so you don`t use it at all and you delete it's value from some other number in the sequence

ex: n=5

sequence is : 1 2 3 4 5

then we take

note:

(the number after word eat denote the eat process number not any value of any slime)

eat 1:

1-2=-1

then using -1 eat all but leave just one number (i.e the last number)

continue eating

eat 2:

-1-3=-4

eat 3:

-4-4=-8

eat 4: here we use the last one 5 to eat our result (-8)

5-(-8)=13

so you can see that we didn't add the least number which is 1 to our result

and we subtract it`s 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)

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

How did u reach the conclusion that gcd of n and n*(n-1)/2 is always >1

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.

Thanks!!!

you can take n common from n*(n-1)/2 and n*1, as n is dividing both of the numbers.

Definitely wrong!!!! check the above comment

Yes, Thanks!

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

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 :DBrankonymous: 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.

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.

still, haven't understood the problem D... So, anybody can give the proof of correctness for D???? thanks in advance....

Let

posbe the number of positive numbers in the array,and

negbe the number of negative numbers in the array.If

pos> 0 andneg> 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 (becausenegative-positive=negative), and keep one positive number alive, so the array will become like following: {somenegativeelements,onepositiveelement,somenegativeelements}, so this remaining positive element can eat any negative element with still being positive (becausepositive-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 who eat, for example,a[i] -a[i+ 1] =negativeelement, 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 choosea[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.Thank you..

how we can solve it using DP

Many thanks mate ! You told the intuition behind the editorial. It really helped a lot.

Happy to hear this :D. Wish you all the best!

Thanks for allowing exponential solutions to pass for F! http://codeforces.com/contest/1038/submission/42588924

Let 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.

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 :)

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

My alternative bruteforce Solution for E, without any nasty graph theory and bitmasks https://codeforces.com/contest/1038/submission/42586454

the 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 limit

Correct me if i wrong and sorry for my bad england.

Good to see that someone else went through the same hell-ish implementation :P

Can Anyone help me with the dp approach that author was tring to convey( about the 4*n states)..It shall be really helpful!

In problem D, Can anyone prove how "any combination of + and − is obtainable"?

And what train of thought leads to this conclusion?

In this comment, I tried to explain how I was led to the solution.

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

3

-1 -2 -3

Thanks

Any test with all elements being negative (except when n = 1) will work.

Thanks

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!!

i solved problem D by greedy approach as mentioned above.. please someOne explain

DP Solutionfor problem DSlime?Please put this blog into Contest materials.

I agree with you!

Done

F can also be solved in

O(n^{2}) using inclusion-exclusion principle. Implementation: https://codeforces.com/contest/1038/submission/42615092Explanation:

Let

nbe the length of target string andmthe length of pattern. LetA_{i}be a set of cyclical strings containing pattern at positioni. 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 subsetsJof {0, ...,k} containing 0 andk, but forgetting about bits on positions >k(sof(k) is contribution divided by 2^{irrelevant bits}). Having values off(k) computed we can get the answer in the following way:(for each

kwe need to multiplyf(k) by count of "non-wrapping shifts" of positions set (all of them contain 0 andk) and fill in the forgotten bits). How to computef(k)? Letvalid(i) be the set of positionsjsuch that we can place pattern at positioniandjsimultaneously.f(k) is non-zero only for since counted sets always contain 0 andk. For suchkwe 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 thatvalid(i) is just cyclical shift of positions invalid(0), it's introduced for notation.valid(0) can be computed naively inO(nm) orO(n+m) using KMP.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.

I forgot to mention that

f(0) = 1 (base case), I'll edit the answer. Thanks for noticing.Can someone tell me how this work to solve the problem E?

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).

Thanks a lot to explain this.

How can we solve problem D using DP ?

In problem C: what's the induction proof to show DP == Greedy ?? Some hints plz

i can't understand the solution of problem D

any help ?

Can someone explain this solution to problem F? 42584804

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.)

How does

`bool checker()`

in author's solution on problem E work ?just wondering if there exists a solution for problem E that involves minimum cost flows, thanks and good round btw :)

Details for pF please

There is an interesting

O(2^{n / 2}n^{2}) 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 aO(n^{2}) dp. The total time complexity isO(2^{n / 2}n^{2}) and I got AC. My code : http://codeforces.com/contest/1038/submission/42792131" 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 ?

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. 42952879

Sorry for my extremely poor English.