Mateusz and Escape Room
Contest: Dasha Code Championship - SPb Finals Round (only for onsite-finalists)
Difficulty rating: 3500
Tags: dp

Mateusz likes to travel! However, on his $$$42$$$nd visit to Saint Computersburg there is not much left to sightsee. That's why he decided to go to an escape room with his friends!

The team has solved all riddles flawlessly. There is only one riddle remaining — a huge circular table! There are $$$n$$$ weighing scales lying on top of the table, distributed along the circle. Each scale is adjacent to exactly two other scales: for each $$$i \in \{1, 2, \dots, n-1\}$$$, the $$$i$$$-th and the $$$(i+1)$$$-th scales are adjacent to each other, as well as the first and the $$$n$$$-th scale.

The $$$i$$$-th scale initially contains $$$a_i$$$ heavy coins. Mateusz can perform moves — each move consists of fetching a single coin from one scale and putting it on any adjacent scale.

It turns out that the riddle will be solved when there is a specific amount of coins on each of the scales. Specifically, each scale has parameters $$$l_i$$$ and $$$r_i$$$. If each coin lies on a single scale and for each $$$i$$$, the $$$i$$$-th scale contains at least $$$l_i$$$ and at most $$$r_i$$$ coins, the riddle will be solved and Mateusz's team will win!

Mateusz is aiming for the best possible time. Therefore, he wants to solved the riddle as quickly as possible. What is the minimum possible number of moves required to fulfill all the conditions?

Input format:
The first line contains an integer $$$n$$$ ($$$3 \le n \le 35\,000$$$) — the number of weighing scales in the circle.

The following $$$n$$$ lines describe the scales. The $$$i$$$-th of these lines describes the $$$i$$$-th scale and consists of three integers $$$a_i, l_i, r_i$$$ ($$$0 \le a_i \le 35\,000$$$, $$$0 \le l_i \le r_i \le 35\,000$$$).

It's guaranteed that the riddle is solvable, that is, $$$\sum_{i=1}^n l_i \le \sum_{i=1}^n a_i \le \sum_{i=1}^n r_i$$$.

Output format:
Output one integer — the minimum number of operations required to solve the riddle.

Constraints:
Time limit: 7.0 s
Memory limit: 256.0 MB

Example input:
5
0 2 3
1 2 3
4 3 3
4 3 3
4 3 3

Example output:
4

Example input:
3
0 1 2
3 0 3
1 0 0

Example output:
1

Editorial:
Let's introduce the variables $$$x_1, x_2, \dots, x_n$$$ where $$$x_i$$$ is the number of coins passed from the $$$i$$$-th to the $$$(i+1)$$$-th scale (or, $$$x_i < 0$$$, it means that $$$-x_i$$$ coins are passed from the $$$(i+1)$$$-th to the $$$i$$$-th scale). We can now create the following conditions regarding the final number of stones on each scale:
$$$$$$ a_i - x_i + x_{i - 1} \in [l_i, r_i] \qquad \text{for all } i.$$$$$$
It turns out that for any sequence integers $$$x_1, \dots, x_n$$$ satisfying the inequalities before, we can create a sequence of $$$|x_1| + |x_2| + \dots + |x_n|$$$ moves satisfying all the conditions in the statement! In order to see this, consider a few cases:
• If $$$x_1, x_2, \dots, x_n > 0$$$, then we take any coin and make a full lap with it along the circle in the order of increasing $$$i$$$'s. We can now decrease each $$$x_i$$$ by one.
• If $$$x_1, x_2, \dots, x_n < 0$$$, we can do the similar thing, but we're decreasing $$$i$$$'s.
• In the remaining cases, we can pick a scale $$$i$$$ that won't receive coins anymore (that is, $$$x_i \geq 0$$$ and $$$x_{i-1} \leq 0$$$) and it still has some coins to distribute ($$$x_i > 0$$$ or $$$x_{i-1} < 0$$$). If $$$x_i > 0$$$, take a single coin, put it on the $$$(i+1)$$$-th scale, and decrease $$$x_i$$$ by one. If $$$x_{i-1} < 0$$$, take a coin, put it on the $$$(i-1)$$$-th scale, and increase $$$x_i$$$ by one.
By following these operations, we will create the final configuration in $$$|x_1| + \dots + |x_n|$$$ moves.
Therefore, we need to minimize this value. Let's try to guess $$$x_1$$$ and try to optimize $$$|x_1| + |x_2| + \dots + |x_n|$$$ for some fixed $$$x_1$$$. The simplest way is to write a dynamic programming: $$$dp(i, x_i) = $$$ the minimum value of $$$|x_1| + |x_2| + \dots + |x_i|$$$ given a value of $$$x_i$$$ and such that the final numbers of stones on the second, third, fourth, $$$\dots$$$, $$$i$$$-th scale are satisfied. To progress, we iterate over the possible values $$$x_{i+1}$$$ such that $$$a_i - x_i + x_{i - 1} \in [l_i, r_i]$$$ and compute the best value of $$$dp(i+1, x_{i+1})$$$. Notice that the initial state is $$$dp(1, x_1) = |x_1|$$$ and $$$dp(1, \star) = +\infty$$$ everywhere else. To compute the result, we must take the minimum value $$$dp(n, x_n)$$$ for $$$x_n$$$ satisfying $$$a_1 - x_1 + x_n \in [l_1, r_1]$$$.
How to improve this DP? First of all, we'll try to maintain the $$$dp(i, x_i)$$$ as a function on $$$x_i$$$. In order to compute $$$dp(i+1, x)$$$ from $$$dp(i, x)$$$, we'll need to:
• Shift the function (left or right).
• Given a function $$$f$$$ and a constant $$$t$$$, compute $$$g(x) := \min_{y \in [x, x+t]} f(x)$$$.
• Given a function $$$f$$$, compute $$$g(x) := f(x) + |x|$$$.
It turns out that after each of these operations, the function remains convex. We can therefore say that the function is linear on some segments with increasing slopes. Therefore, we can maintain a function as a set of segments, each segment described by its length and its slope.
How to describe the second operation? We can see that it's actually adding a segment with slope $$$0$$$ and length $$$t$$$ to the function. Meanwhile, the third operation is splitting the function into two parts: for negative and positive $$$x$$$. We need to decrease the slopes in the first part by $$$1$$$, and increase the slopes in the second part by $$$1$$$.
All these operations can be implemented on any balanced BST in $$$O(\log n)$$$ time. Therefore, the whole subproblem (for fixed $$$x_1$$$) can be solved in $$$O(n \log n)$$$ time.
How to solve the general problem? It turns out that...
Lemma. The function $$$F$$$ mapping $$$x_1$$$ into the optimal result for a fixed $$$x_1$$$ is convex.
Proof will be added if anyone requests it. The basic idea is to prove that: (1) the answer won't change if $$$x_i$$$ could be any arbitrary real values, (2) if $$$(a_1, \dots, a_n)$$$ and $$$(b_1, \dots, b_n)$$$ are good candidates for $$$(x_1, \dots, x_n)$$$, so is $$$\left(\frac{a_1+b_1}{2}, \dots, \frac{a_n+b_n}{2}\right)$$$. We can use that to show that $$$\frac{F(a_1) + F(b_1)}{2} \ge F\left(\frac{a_1+b_1}{2}\right)$$$.
Therefore, we can run a ternary search on $$$x_1$$$ to find the optimal result for any $$$x_1$$$. This ternary search takes $$$O(\log(\sum a_i))$$$ iterations, so the final time complexity is $$$O(n \log n \log(\sum a_i))$$$ with a rather large constant.