Can you please explain the ternary search idea ? At first glance on this problem I thought a ternary search idea but while implementing it, I found some difficulties. Then I solved it using binary search. Thanks in advance.

Well, let store all x in x[i], and while adding i element we will calculate prefix sum for first i elemets (sum[i] = sum[i - 1] + x[i]).

Then, it is obvious that we should always use x[1] and x[cnt] element (where cnt — how many elements we have at the moment). So, we take x[cnt] and then search best answer choosing what prefix should we use from [1;cnt - 1].

I can't understand problem C, In example 1 it is said that contest should start at 3 hours at time zone 1 that would be 1 hour in time zone 2 and 2 hour in time zone 3. But As stated by problem time at ith time zone should be i if time at 1 is 1 hour. So the time at second time zone should be 4 hours and third 5 hours.

hmm, you should imagine that like in real life, except it only has n hours ( not 24 hours) and doesn't have 0 hours. Here is an example how that goes in time zone 1 if we have n=4: TZ1: 1 2 3 4 | 1 2 3 4 | 1 2.. and so on. Meanwhile, in TZ2: 2 3 4 | 1 2 3 4 | 1.. and so on . Hope you can understand, my English and explanations is not good at all =))) .

I'm not sure what the editorial is saying either, but let me explain my solution.

Let the two sides of the cutlet be A and B. Consider the case where we have two availability intervals [li, ri] and [lj, rj] with i < j. Let's say we enter interval i with side A, flip to B during interval i, stay at side B until interval j, and flip to A during interval j. If we only consider these two flips, then the amount of time we had side B would be in the interval [lj - ri, rj - li]. If we have two more intervals x,y where i < j < x < y, we can perform the same flip as above between i and j with x and y as well (ie. A -> flip at x -> B -> flip at y -> A). In fact there are no conflicts regardless of how we flip things at i, j, x, and y as long as they are of the form I described earlier.

Let's build a graph as follows. Each interval becomes a vertex. Let i < j, we connect interval i to j with a directed edge of length 1 and value [lj - ri, rj - li]. Connect the source to all intervals with value [li, ri], and connect all intervals to the sink with value [2n - ri, 2n - li]. These edges represent the time the cutlet spends at side B. We also have to add the side A edges, which are just duplicates of these with value [0,0]. Value here means the time the cutlet spends on side B.

Now, taking an edge from i to j means we do the flips A -> flip at i -> B -> flip at j -> A. This gives some time in the value of the edge. The value of a path whose edges have values [l1, r1], [l2, r2], ... is simply [l1 + l2 + ..., r1 + r2 + ...].

IF we add the restriction that we can only have The solution to the problem is now finding the shortest path from the source to the sink whose value contains n, taking alternating side A and side B edges (ie. we can have the cutlet on side B for time n).

To deal with the case where we might have to flip multiple times in one interval, first we note that we want to flip at most twice in a given interval. So we add side B edges from i to i with value [0, ri - li]. This doesn't cause a problem because we must take alternating side A and B edges. However, this only considers cases where we do A -> flip at i -> B -> flip at i -> A, but this is sufficient because the B -> flip at i -> A -> flip at i -> B case cancels out with this one.

To find the shortest alternating path, you can use Dijkstra with an extra flag that says whether to take a side A edge or a side B edge. Also store the value for each visit. Whenever we reach the sink, check to see if the value contains n, if it does, then we found the answer, otherwise, skip this path and keep looking.

For problem E Lemma 3: It should be f(i) is non-increasing for increasing i, for M(i) to have a maxima. If f(i) = M(i + 1) — M(i) is decreasing or non-increasing it implies M(i) has a minima. In the proof statement -f(i + 1) - f(i) ≤ 0, this means that f(i) does not decrease when i increases is simply wrong. It means f(i) does not increase.