Assume we need to check if X is enough. Iterate a the number of whole cash paying for 1000. Clearly, we should pay b = (X - 1000 * a) / 2000 times for 2000. And We should do it for first a 1000 and first b 2000, each position we set values to 100/200. The remaining position are filled with their negative cost. Finally, add X - 1000 * a - 2000 * b to first position. If the minimal prefix is non-negative we know X is enough.

How does Div2C greedy approach work? We can create a bipartite graph with the left vertices as initial times and the right vertices as final times and an edge with the weight of the cost. It is a classic assignment problem. What condition in this question reduces the problem complexity making the greedy solution optimal?

Call q is a heap contain planes can depart at time t (k + 1 <= t <= n) then the plane depart at time t will be the plane with the largest c in curent heap. And go on... we will have an optimal solution. Sorry for my bad English.

Let me try it. Consider we have a set of planes yet to leave. Now, if we choose ith plane to stay from our current set, we incur a penality of Ci. And we want this penality to be minimum. Now think about it and you will understand.

Let's say at some point we have two planes x and y with cx = 2, cy = 5. Intially x is supposed to depart at 4 and y is supposed to depart at 3. Currently we are at 5th time unit. The total cost if we choose x first will be 2 * (5 — 4) + 5 * (6 — 3) equals 17. If we choose y, it will be 5 * (5 — 3) + 2 * (6 — 4) = 14. But the minimal penalty is given by cx at 5th time unit. That is not leading to the optimal solution. Correct me if I am wrong.

"We will show that following greedy is correct: let's for each moment of time use a plane, which can depart in this moment of time (and didn't depart earlier, of course) with minimal cost of delay." Does this not mean that the plane x gives us the minimal cost?

can someone explain the greedy proof in div2 C could not understand it from editorial like in this statement "Let x be plane with minimal cx, such ax ≠ bx. At any moment greedy algorithm takes avaliable plane with lowest cx, so ax < bx. " what is the proof for ax<bx.?

I think there is a better explanation. You can think about this, how to get the best answer? Supposing that we have an initial arrangement, how to optimize it. First of all, we will come up with an idea to swap some planes' departure time. If plane i departs at ti, plane j departs at tj, then the cost of them two is ci * (ti — i) + cj * (tj — j) = ci * ti + cj * tj — ci * i — cj * j, and ci * i — cj * j is a constant. So our goal is to minimize ci * ti + cj * tj, and it's obvious if ci > cj, ti > tj, we should swap ti and tj. So for every moment t, we should choose the highest cost from those who can take off at this time. And this is my code.http://codeforces.com/contest/853/submission/30136836

Sort the queries and marked squares from left to right(column),and use segment tree to maintain it.When adding a suqare(i,p_i),add 1 to p_i in the segment tree,and you can find the answer when asking a query.

Fix a window [l, r] of size K. Then all the jury must go to city 0 before time l and must return to their cities after time r.

So let's find L[i] = minimum cost of making all jury travel to the capital at time i or less and R[i] = minimum cost of making all jury return to their cities at time i or later. Since all times are ≤ 106, we can keep a list of flights for each time. First, go in increasing order, processing flights and keeping the minimum value for each jury as well as the total minimum cost. Then do the same, but in decreasing order of time. All this process is linear.

Once we have calculated L and R, we simply try out all possible windows [l, r] of size K. The cost for a fixed window is L[l - 1] + R[r + 1]. Checking all windows is also linear.

Yes, actually with a 2D Tree, you get O(Q * log2N). To reach O(Q * logN), you can use an approach similar to sweep line supported by a BIT. The process would be as follows.

Sort the queries by top row.

Process queries from top to bottom row by row. For each row, do the following in order: Query the BIT for every query in this row, add column of this row to BIT. To query the BIT, consider we're at row i, and we're processing query (i, j1) - > (i2, j2), then we'll query the BIT in the range (1, j1 - 1) (squares up and to the left of query rectangle) and in the range (j2 + 1, N) (squares up and to the right of query rectangle). Since we only add the column of a row once we've processed all queries of that row, and since we're processing rows from top to bottom, we know that all data in the BIT will be from rows above the current one.

Sort the queries by bottom row now and do the same as in previous step but this time from bottom to top.

854 B Please Explain this part. Otherwise, if k > x, assume that apartments with indices 2, 5, 8 and so on are occupied, so any room has at least one inhabited room adjacent to it. Therefore number of good apartments is equal to number of vacant apartments and is equal to n - k.

One neighbour can cover two adjacent apartments. If there are more than x occupied apartments, one can divide the apartments in groups of 3 and assign them to the middle of each group (hence the 2 in 123, 5 in 456, etc.)