
Good day Codeforces ✨
We are excited to invite you all to Codeforces Round 1067 (Div. 2), starting at Nov/29/2025 17:35 (Moscow time).
This round features 6 problems to be scored in 2 hours, with a few problems including multiple parts. All tasks were authored and prepared by koderkushy and me. This round will be rated for all participants with rating below 2100. Take a look at all the problems, we hope you enjoy the challenge! 🚀
We extend many thanks to our team for helping make this round possible:
- abc864197532 for his excellent coordination.
- KAN for preliminary review.
- Testers lineup: Dominater069, dreamoon_love_AA, Fysty, amano_hina, _istil, __baozii__, IceKnight1093, CoderAnshu, Pols_Agyi_Pols, sv1shan, piyush_pransukhka, a.out, BiggestOtaku, Bucketsmith, -firefly-, satyam343, plAtEaUpUs, omsincoconut, snasibov05, homelanderrr, _helloLad, BladeRunner, deneribeiro10, Rafio, balakrishnan, UniversalAdmin
- Alexdat2000 for translation problem statements into Russian.
- MikeMirzayanov and KAN for lovely platforms Codeforces and Polygon.
- And of course, you, for taking part! 💛
Score Distribution: $$$500-1250-1500-2000-2750-(2500+1000)$$$ 🏆
UPD: Congrats to the winners!
Kudos to antontrygubO_o for almost getting to the intended solution of F2 in contest!
UPD 2: The editorial is out.








ball
:gif (failed loading) T-T , excited to solve again !
As a commenter, I commented very fast!
As a watcher , I always watch you comment fast (~ ̄▽ ̄)~
As a singer, I released a new song very fast.
[Verse 1]
Yo, I pull up to the contest like “Who broke the queue?”
Codeforces laggin’ so hard even Mike’s like “...bro what’d you do?”
Tourist speed-typing like he’s powered by jet fuel,
I submit one solution—WA, I am a fool.
Meanwhile Farmer John in the back with a tractor,
Debuggin’ his fields like a Boolean factor.
Bessie’s in the barn yellin’, “GREEDY OR DP?”
Elsie writes a proof that would terrify CP.
[Chorus]
We in the RATED zone, yeah we hate cheaters and we known,
Mike Mirzayanov built the site with a throne.
Tourist on top, but the cows go moo,
'Cause Bessie hit red before I ever do.
HEY!
[Verse 2]
Farmer John droppin’ problems like hay bales,
I read the statement once—my brain fails.
Bessie solves it blindfolded in a bathtub,
Elsie flexes proofs like “get on my math club.”
Mike sees the chaos, presses F5,
Server explodes, but at least we alive.
Tourist teleports in like “yo who summoned me?”
I ask for help; he gives me another TLE.
[Bridge]
Uh, cows in div1, humans in div4,
I’m sittin’ there cryin’ like “no more… NO MORE!”
But Bessie and Elsie droppin’ editorial bars—
They got more stars than the top Codeforces stars.
[Chorus]
We in the RATED zone, and we codeforcers to the bone,
Tourist types so fast that his keyboard groans.
Lil Mike Mirz laughs while he patches the site,
Farmer John’s cows win the contest tonight.
YEAH!
[Outro]
So if you see me in the standings, scroll waaaay down low,
I’m the dude in last place sendin’ WA after WA, yo.
But at least the cows respect me (…I think?),
Now excuse me while I lose rating in sync—
Codeforces gang, we don't shower, we stink!
this is fire
Messi Gif is amazing, buddy.
I think so.
I was one of the testers for this contest, so I’ll leave my mark here.
Messi is peek
I'm a Ronaldo fan, may I participate in this contest
I'm also a Ronaldo fan, you are invited to participate!
I'm not a soccer fan, may I participate in this contest?
No. Because you called it in the wrong way. Its football.
messi, my beloved
I was one of the tester who did not test ^_^
That one guy(I'm not assuming your gender) who takes the payment and dips:
I need to careful about penalty Otherwise Messi can goal.
In my opinion, Ronaldo is better.
is this a sixseven contest or a messi contest
I think it's a Messi contest!
but you can participate even if you are not a messi fan!
Good luck to everyone for this contest!
koderkushy Orz

Searching for Ronaldo in comments great at least one identified the presence of suiiiiii!!!!
greatest of all time
As a tester I hope you enjoy the problemset.
As a tester, I enjoyed testing the problems.
Vai tell me this contest won't be a mathforces contest :'(
Ronaldo fans accepted?
_helloLad orz ^_^
Suiiiiii
If we avoid penalty, we can goal.
It would be amazing if all the problems were related to football or Messi.
What does the score distribution mean? I'm new here and don't know much of the thing. How will these score impact in the contest rating? Would anyone mind explaining please?
please check if this helps — Everything about Codeforces scoring system
hope I don't get negative delta like every div 2 i pass xD , Btw cool to post that gif of messi it really motivated me to pass the round !
As a tester, I’m just as clueless about the final problemset as the participants ^_^
A-500 , B-1250 , speedforces
Hope to solve at least 3 problems, thus increase my rating.
can you please also mention, if for some questions we have
pretest = full testLooking at the score distribution, I smell a hard B.
As a participant, Lionel Messi encourages me to solve at least three problems.
As a contestant I registered FIRST :D
As a tester, I'm commenting late as I'M A FUCKING CORPORATE SLAVE ;_;
As a Ronaldo fan I will participate in this contest. I hate pretests!!
Oh no, Indian round. Get ready for Math and DP.
Ronaldo is the goat today this round will be El Clasico.
Little unfair to keep a messi contest on a night when barca plays init ?
messssssssiiiiiiiiiiiii yooooooooooo
As an Argentine fan, I will always miss Messi. And also Ronaldo .
codeforces round $$$1067$$$ $$$6 7$$$
Will the first one be named goat?
__baozii__ strikes back!!
Lmao
Eibar man pessi stan
I practically never solved a dp problem on my own before but today I passed C. Im happy :)
We all know you are a cheater (✿◠‿◠)
why are you sad -/withered rose/-
cause i'm noob
I would love to read the editorial for F1 , it was painful :]
Idk bout others,but b was significantly harder than c, I solved c in 20 mins of seeing the question while b took forever. Whole time i was trying to split even Freq numbers manually in such a way that they get split into two odd parts.
Yeah What was your approach after that? I realised that if there are no odd occurences than we can only split the even occurences among two if the number of even occurences have the same parity as n. If not than there would be a even occurence we can't split and so my code became like this:
if you found an odd occurence add 1 to ans if you found an even occurence add 2 to ans
if there are no odd occurence than check whether parity of number of even occurences is same as n. If it is than subtract 2 from ans.
Finally output ans.
This was my solution for b but I could not solve C can you tell me how you solved it? I was able to deduce that if k is even than we simply calculate the maximum subarray sum using kadane's but when k is odd than we can change 1 element of a[i] to a[i] + b[i]. But how do you go about doing this optimally in o(n) or in o(nlogn). I was stuck here...
Assume two variables, storing max subarray sum ending at the current pointer, one of without selecting some b element, and of with some b element selected and apply something very similar to the standard algorithm for max subarray sum.
Am I wrong, but I think D can be solved in O(n)?
convert both strings into the zero string, reverse the order of instructions to get the 2nd string.
holy moly i complicated it way too much then. just made the ith index same and kept shortening the string until i am left with a string of length 4 i need to make equal to the last 4 in t and then ran bfs from it to all
Did the same lol. I pre calculated the required operations for all s, t pairs of length 4. https://mirror.codeforces.com/contest/2158/submission/351260008
Of course it can be solved in O(n). But $$$n\leq 100$$$ makes it easier to implement.
or maybe because of the checker? is there any way to check if the output correct or not faster than O(n^2) ?
This makes more sense
It can be done with hashing + lazy segment trees. On a node of one segment tree responsible for [l,r] we keep a polynom hash of s[l...r], on the second segment tree we keep a polynom hash of reverse of s[l...r]. When inverting hash changes from s[l]+s[l+1]*p+s[l+2]*p^2+... to 1+p+p^2+... -(s[l]+s[l+1]*p+s[l+2]*p^2+...).
you are my hero,
but how you are going to handle the update
imagine you inverted the range (l,r) , and now you have a range to check (l2,r2)
where l2 <= l <= r2 <= r, how you are going to check, the update function in segment tree needed to be Polynomial queries.
We maintain a boolean flag whether an inversion operation should be done on a node or not. Since doing two inversions is the same as not doing any operation at all, while propagating we do child_flag = child_flag XOR node_flag.
I also solved D in O(n). You can always use 3 bits to flip the first one in at most 2 operations. This way you can adjust all bits, but the last 2. For the last two I did a BFS over the last 4 bits of the string. n being bigger or equal than 4 seemed to hint that this was the wanted approach. (I have no proof though, that the last 4 bits are enough or that it does not use too many operations for the last 2 bits)
could you elaborate on the bfs part on the last 4 bits, i could figure out the next 2 bit logic during round, but couldnt figure out what to do with the last two bits
Sure, you can look at my submission here:
351233260
You must imagine this BFS a bit more abstractly. Every node is 4 bits. So $$$0000$$$ is a node, and $$$0101$$$ is also a node. And we connect two nodes with an edge, if there is an operation that transforms the string into the other. So $$$0101$$$ and $$$0010$$$ would have a common edge, because we can flip the last 3 bits by the rules. $$$0000$$$ and $$$0100$$$ have no common edge, because we cannot apply this palindrome rule directly.
Yup, there's a few solutions. My solution first brute-forces the $$$n = 4$$$ case. After that, I go from left to right in both strings (to the point $$$i = n-4$$$) and if they differ, fix the difference by doing small local changes — either a length $$$2$$$ change if $$$s_i = s_{i+1}$$$, a length $$$3$$$ change if $$$s_i = s_{i+2} \neq s_{i+1}$$$ or a length $$$2$$$ change on $$$(i+1, i+2)$$$ followed by a length $$$3$$$ on $$$(i, i+1, i+2)$$$ if $$$s_i \neq s_{i+1} = s_{i+2}$$$. After that, the strings are identical besides the last $$$4$$$ positions, the solution for which I get from my initial brute-force search and shift the indices.
The reason it works is that I make at most $$$2$$$ operations for each index except the last $$$4$$$, and I verified that my brute-force approach takes at most $$$8$$$ operations on the $$$n = 4$$$ case, so I get a total of at most $$$2(n-4) + 8$$$ operations as desired.
One could also just split the strings up into blocks of $$$4$$$ (where the last two may overlap) and apply the brute-force solution to each block.
Please Someone explain me how to solve B?????
it was ultimately how to divide elements having even frequency ... because however you divide odd frequency it can only contribute +1 to the answer
Numbers having ODD freq. will always contribute at max +1 to the final score, whereas EVEN frequency either contribute +2 or +0 only. Also, we can split any odd number = A + B, where |A-B| = 1, eg. 7 = 4 + 3. Also, these "1" are always even in numbers (as, sum of freq = 2*n, i.e. EVEN) so they can be distributed easily, to either set 'P' or set 'Q'. Now, for even to contribute +2, either even/2 = odd, or leftover evens, i.e. those evens where, even/2 = even number, can contribute +2 only if either even no. of such even numbers are present or either atleast we have 2 or more +1 deviation that we got from odd frequency. else one +2 won't be considered into our answer; Though a bit of casework but this is what i wrote in my code, if anyone have a simpler solution do share :)
Thanks for this, very insightful! Now i know how close i was to solving it myself,
Final answer:
ans = odd + ((n - even) % 2 == 0 || odd > 0 ? even : even - 1) * 2;I dislike problem D very much, uninspired and annoying
Problems C and E are cool though
How to solve E?
there are two kinds of holes:
Isn't type 2 a general case of type 1?
yes, but it helped to think of them separately when i was solving the problem
is similar to this same idea https://mirror.codeforces.com/problemset/problem/1593/G but the use here is brackets, to make "string flipping segments"
Because of the error of D checker, my wrong code past the PP,then I go to solving the E ,it taked me 1h, when I see the D error ,it only have 3min. I have no time to change it. It was so upset.I vote down...
。
what was the checker not checking?
fuckass palindromes
lol
shit,I'm terrible.I can only finish the first two questions.
Good round but $$$D$$$ is too hard for me :(
God damn that was brutal, I need to practice more.
Problem D is so bad lol
whyyyy the fuck i cant submit 1 second late
cooked by
Bagain... I kind of sawCimmediately ... for meB >> CSimilar for me, I solve C in less than 15 minutes but for b it took 1+ hour
same like C is way easier, I got the idea instantly after reading C, while B cooked me so hard
Since I'm a math person the solution to B popped out at me rly easily, while C was more of an implementation problem so it took longer for me.
Why there are annoying implementations on D nowadays??
what? u don't like writing
?
There is a much simpler $$$O(n)$$$ solution which only uses
vector<pair<int, int>>(submission).oh yeah the going to 0 from both sides solution, didnt think of that during contest
The implementation wasn't that bad frankly, especially since it's just duplicate code in a lot of places
The reversal solution starting from t is genius! Couldn't think about it :(.
shit B
Any ways for us to sumbit new solutions after the contest has finished?
You should be able to submit after the system tests for that contests are over.
OK thank you bro
How did so many people get C. It looked like there were lots of cases or am I just stupid:/
If k is even, just output maximum subarray sum Else, You can just get the Kadane prefixes and Kadane suffixes and try all indices, get the max
Damn I had a feeling that the answer won't change for even K but I couldn't prove it and was thinking about edge cases. Maybe if I had not wasted so much time on implementing B i could have got it.
For odd k, I did a dp where dp[i][0] is the maximal subarray ending at i without adding an element from b and dp[i][1] is the maximal subarray ending at i with adding an element from b. Then your answer is just the maximum over all dp[i][1].
if k is even then alice cannot increase the maximum sum since bob can always undo the operations. similarly the sum cannot be decreased. so the problem is just to find maximum non empty subarray sum which is standard kadanes algorithm. for odd k, bob will always undo alices move until the last move which alice will play. now the question reduced to finding the maximum subarray sum such that alice can perform one operation. just a slight variation of kadanes algorithm, which could be done with DP.
But how can we claim that undoing alice move is the best move for bob?
Cause on Alice turn he would make sure that Bob's best chance is reduced, cause by saying Alice trying to maximize, we also mean that Alice makes sure that Bob score is also reduced, cause reduced BOB score = increased ALICE score! Though it's not a formal proof, but if anyone have it please share
didn't prove deeply... but then alice can keep undoing what bob is doing LMAO !!!!
Alice is the one to start so, he is never the one undoing Bob's move. Instead Bob is the one who is obliqued to undo Alice move.
I was saying if somehow Bob had better move, then Alice can keep undoing it later after Alice made the first move ... sorry for confusing statement by me
If k is even, bob and Alice will keep going back and forth and the array will just stay the same, so we just have to find the maximum subarray sum in the origianl list. For the k is odd case, my solution was ngl really ugly, but basically I created an array of the cumulative minimum prefix sums from 1 to n, then an array of the cumulative maximum prefix sums from n to 1, so for each element b_i I found maxes[n-i-1]-mins[i]+b_i, and kept updating a final max variable so that is was the maximum value of maxes[n-i-1]-mins[i]+b_i over all i.
My code:
https://mirror.codeforces.com/contest/2158/submission/351223312
Because the optimal move is same for Alice and Bob.
Consider any maximum subarray, obviously Alice will add max $$$b_i$$$ in that subarray, and so now for Bob also it is optimal to decrement the same $$$b_i$$$ and not anything else.
Because
One way you can show this is by showing that when k is even Alice can always maintain the sum for the maximum subarray sum (MSS). Hint: Consider the maximal b[i] in the range of the maximal subarray sum.
This would show that the answer is >= MSS, but you also have to show that it is <= to the MSS which is quite simple to do as well. Let me know if you want the details.
"Power outage won the battle but not the war." Atleast i solved A and i will accept my negative delta.
DSU rollback came to my mind while thinking about problem E, but I wasn't confident enough to think it is actually true because DSU rollback doesn't appear frequently.
: — )
dear god i really hope the next div has DFS or even DSU
Why can’t Problem B be solved using a frequency array?
It is solved using a frequency array. Don't know if there is another way to solve it.
here is my code ~~~~~
include <bits/stdc++.h>
using namespace std;
void solve() { int n; cin >> n;
vector<int> a(2 * n); for(int i = 0; i < 2 * n; i++) { cin >> a[i]; } vector<int> freq(2 * n + 1, 0); for(int x : a) freq[x]++; int even = 0, odd = 0; for(int i = 1; i <= 2 * n; i++) { if(freq[i] == 0) continue; if(freq[i] % 2 == 0) even++; else odd++; } cout << odd + even * 2 << "\n";}
int main() { ios::sync_with_stdio(false); cin.tie(nullptr);
}
~~~~~
great contest
I am cricket lover
Terrible perf these past two contests :(
same :(
I claim D is possible by "pushing" to the last 4, making everything but last 4 match. and then using at most 8 operations (which we can brute force) to get it to the position.
However, the last 4 part is annoying impl and heavy casework and I didn't have enough time rip
You can use BFS to check the operations for the last 4.
I did this, bfs all possible masks for n=4,n=5,n=6,n=7, you can get any sum , s >= 4, with these 4, and i cut the string into parts of (4,5,6,7) and each part i can solve easily because i stored already the soultion for (4,5,6,7) .
For
s = 1010101, t = 0101010, pushing till the last 4 positions yieldss = 0101011. How to make the last 4 bits1011into a1010?1011 -> 1000 -> 1111 -> 0000 -> 0110 -> 1010
Thanks, I was only checking for sub-strings of length 2 and 3 :<
For my implementation, pushing till the last 4 makes: $$$0100101$$$
Anyway, here is the output from my code for the last 4 bits you mentioned:
Great problems!
Great contest!
Why it is showing unrated in my profile?
from my pov B is harder than C =))
Let's call a string of length $$$n$$$ with all zeros $$$Z$$$. We convert $$$S$$$ to $$$Z$$$ and note down the operations. Then we convert $$$T$$$ to $$$Z$$$ and note down the operations in reverse order. Easy to see the combination of these two would convert $$$S$$$ to $$$T$$$.
How to convert any string $$$k$$$ to $$$Z$$$?
If there are no $$$1s$$$ in $$$k$$$, we are done. Otherwise, let the first index at which $$$1$$$ is present in $$$k$$$ be $$$i$$$. We try to make $$$k[i] = 0$$$ while keeping $$$(k[0] = 0, ...., k[i-1] = 0)$$$.
If there exists any index $$$j$$$ such that $$$k[j] = 1$$$ and $$$j \gt i$$$, we pick the palindrome $$$s[i...j]$$$. This makes $$$k[i] = 0$$$ and we repeat this process for the further indices.
Otherwise, $$$s[i...n] = 10...0$$$ and we now perform $$$2$$$ operations to make it all $$$0s$$$. If there are $$$2$$$ zeros left after $$$i$$$, we perform the operations $$$(i+1, i+2)$$$ and $$$(i, i+2)$$$. Otherwise, there must be $$$2$$$ zeros present before $$$i$$$ (as $$$n \gt = 4$$$). In this case, we perform the operations $$$(i-2, i-1)$$$ and $$$(i-2, i)$$$. You can verify that this converts $$$k$$$ to $$$Z$$$.
very clear explanation, thank you!
To solve C, I would use a segment tree to avoid any overthinking if possible. Same goes for yesterday's E.
https://mirror.codeforces.com/contest/2158/submission/351205345
Can someone explain B?
Don’t have to think much, for odd values just do ans++. For even values, just split them into two close odd numbers. If they can be added to c1 and c2, then do ans += 2 also if(c1>=c2) c1+=max odd and c2+=min odd else c1+=min odd and c2+=max odd
my thinking was i can split odd how every i like so just splitting even like that i can end up to size n by adding odd parts
What is c1 and c2?
jus the count of values of both split. you can see my code: 351201805 I used cnt1 and cnt2 in code as the size of bith split
what does "they can be added to c1 and c2" mean? What if they cant be added? Do you still increase c1 and c2?
When I finally got Candidate Master omg >///<
Congrats random person on the Internet , feeling happy for you ^_^
Problem F is very similar to this problem: https://mirror.codeforces.com/contest/1981/problem/D
For D, I precalculated $$$ops[16][16]$$$ which gives a sequence of operation for a all 4 length strings represented as masks. Then we can directly reduce the string $$$s$$$ to $$$t$$$ till $$$n - 4$$$ positions and then use these operations for the remaining 4 digits. 351229248
Is there a way to hack own solutions after the contest? I think editorial solution for E is wrong, even though it passes tests, as it doesn't do proper DSU rollbacks.
on grid like
if we reduce the middle 5 by 1, and then reduce all other 5 by 2, they will still have min neighbor = 1, even though the picture is now
If someone is interested, I figured out this does not break the solution because:
1) if we had to decrease 5->3, we shall create a new node anyway, so last one is not used 2) if we don't decrease the value, the 5->4 decrease makes the adjacent components non-final anyway.
I figured some people might be interested, it is possible to solve problem D in linear time.
The idea is to iterate through s and force the characters to match up one at a time. Then, when we get to the last 4 characters, we can brute-force using BFS on just the suffix of the last 4 characters to force s and t to match up in constant time.
The reason this is linear is because we only have to do a constant number of flips (at most 2) for each index before we get to the ending, and those flips consist of 2-3 indices. Then for the ending, it's clear that it is O(1) because we have just a constant number of characters to worry about.
351238391
Nice round.ABCD are natural and E is absolutely a beautiful problem
In Problem F2, I wrote a randomization program and found 100 numbers with pairwise distinct gcd values.
randomization program
The probability of this randomization program succeeding was low, but I was lucky enough to stumble upon one such set of numbers.
After obtaining these 100 numbers, the construction method for this problem is the same as in CF1981D.
The submission
Good contest, love from China.
I had fun participating :) looking forward to the next one hehe
the question C's solution is wrong because for this test case: 2158C - Annoying Game
alice will not add -10 but subtract it so with your solution the final array is 2 -7 6 ans: 6 but the answer should be 2 3 3 ans: 8 i think you should update it and if can update the solution of this question @kingmessi
Read the problem statement. The constraints say that $$$b_i \ge 0$$$.
oh yeah i did not see that at all
Enjoyed the round — though seeing Messi on the cover made me feel like I needed GOAT-level skills just to pass pretests.
Yeah that's why u decided to cheat
I think renaming "Tc" to "tcasess" was a very important change when fixing their C :)
so this was a
div2contest.. should winner list includediv1people ??generally there is a list which contains only
div2winners..ankara messi....vamos argentina
how to think for problem like d
Thank you for giving me such a great rating, Now waiting for contest 1069 to finally match my rating
I received a warning for 351215259 for the problem 2158B saying my code matches with SupremoCoder/351200134 My code is not the same — I used a map-based approach, while the referenced submission used a frequency vector. I did not share my solution with anyone or collaborate. I can explain my entire thought process and show intermediate versions of my code if needed. This similarity might have been caused by the problem's deterministic logic. Please review my case.
This is a proper misuse of contest rules
enjoyed the contest