Dasha Code Championship - SPb Finals Round (only for onsite-finalists)
G
Mateusz and Escape Room
Codeforces
3500
原题
← F2→
翻译中…

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

翻译中…

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

翻译中…

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

翻译中…

样例

Input #1
5
0 2 3
1 2 3
4 3 3
4 3 3
4 3 3
Output #1
4
InputCopy OutputCopy
Input #2
3
0 1 2
3 0 3
1 0 0
Output #2
1
InputCopy OutputCopy
Input #3
4
1 0 2
3 3 3
4 0 4
5 3 5
Output #3
0
InputCopy OutputCopy
题目信息
题号
1210G
时间限制
7 s
内存限制
256 MB
标签
动态规划

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。