Comments
+17

I've solved it with a greedy approach, if two persons are assigned to the same shop, the assigned friends must be the top 2 friends with maximum values for that shop, and the remaining friends are free to choose their shops.

D can be solved using the pigeon hole principle as there exist significantly many repetitions in the extreme case where $$$n=10^5$$$ and $$$a_i$$$ takes at most $$$2\times10^4$$$ values, we can sort and eliminate adjacent identical elements using $$$(-1, 1)$$$ which significantly reduces the absolute sum of $$$b_i$$$'s, remaining cases can be handled similar to the editorial.

I had a completely different approach, by making observations from the brute force solution.

Even doesn't work due to parity. For the odd case, it turns out that the direct observation that can be made is by thinking it backward since the operations are invertible, considering an initial array of $$$k$$$ zeroes, iteratively picking the maximum sized island of zeroes, and making the operation such that it fills the highest power of $$$2$$$ number of ones at the end in this island while performing this operation at most one new island of zeroes will be created due to size of the flip, we can just push them into the priority queue.


Example

I overkilled it with digit dp.

For problem C2 "Method 2 — Greedy 1" can be implemented without a lazy segment tree using BIT:
Maintain a set of positive potion positions, and a priority queue of {negative potion value, index}. Check if the negative potion can be consumed using BIT. Greedily start decrementing the potion value of positions in descending order of positions that are less than the index while exhausting the priority queue & remove the position from the set if potion value reaches zero.

UPD: It turns out that my previous submission (without BIT) gives TLE for this hack case since my worst-case time complexity was $$$\mathcal{O}(n^2\log{}n)$$$, after optimizing using BIT it reduces to $$$\mathcal{O}(n\log{}n)$$$.
Cheese submission: 117672658, Optimized submission: 117983932

On AmShZCodeforces Round #722, 5 years ago
+19

A non recommended approach for Div 2 problem $$$D$$$
Write bruteforce solution and generate first few terms of the sequence, here they are $$$1, 3, 6, 13, 25, 52, 102, 206, 411, 823,...$$$
As this can't be found on OEIS, make an observation that $$$dp(i) = 2 \times dp(i - 1) + c_i$$$
Search for sequence $$$c$$$ : $$$1, 0, 1, -1, 2, -2, 2, -1, 1,...$$$
which is A051950, so $$$c_i = \tau(i) - \tau(i - 1)$$$, where $$$\tau(i)$$$ is the number of divisors of $$$i$$$.