Rain and Umbrellas
Contest: Codeforces Round 486 (Div. 3)
Difficulty rating: 2100
Tags: dp

Polycarp lives on a coordinate line at the point $$$x = 0$$$. He goes to his friend that lives at the point $$$x = a$$$. Polycarp can move only from left to right, he can pass one unit of length each second.

Now it's raining, so some segments of his way are in the rain. Formally, it's raining on $$$n$$$ non-intersecting segments, the $$$i$$$-th segment which is in the rain is represented as $$$[l_i, r_i]$$$ ($$$0 \le l_i < r_i \le a$$$).

There are $$$m$$$ umbrellas lying on the line, the $$$i$$$-th umbrella is located at point $$$x_i$$$ ($$$0 \le x_i \le a$$$) and has weight $$$p_i$$$. When Polycarp begins his journey, he doesn't have any umbrellas.

During his journey from $$$x = 0$$$ to $$$x = a$$$ Polycarp can pick up and throw away umbrellas. Polycarp picks up and throws down any umbrella instantly. He can carry any number of umbrellas at any moment of time. Because Polycarp doesn't want to get wet, he must carry at least one umbrella while he moves from $$$x$$$ to $$$x + 1$$$ if a segment $$$[x, x + 1]$$$ is in the rain (i.e. if there exists some $$$i$$$ such that $$$l_i \le x$$$ and $$$x + 1 \le r_i$$$).

The condition above is the only requirement. For example, it is possible to go without any umbrellas to a point where some rain segment starts, pick up an umbrella at this point and move along with an umbrella. Polycarp can swap umbrellas while he is in the rain.

Each unit of length passed increases Polycarp's fatigue by the sum of the weights of umbrellas he carries while moving.

Can Polycarp make his way from point $$$x = 0$$$ to point $$$x = a$$$? If yes, find the minimum total fatigue after reaching $$$x = a$$$, if Polycarp picks up and throws away umbrellas optimally.

Input format:
The first line contains three integers $$$a$$$, $$$n$$$ and $$$m$$$ ($$$1 \le a, m \le 2000, 1 \le n \le \lceil\frac{a}{2}\rceil$$$) — the point at which Polycarp's friend lives, the number of the segments in the rain and the number of umbrellas.

Each of the next $$$n$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$0 \le l_i < r_i \le a$$$) — the borders of the $$$i$$$-th segment under rain. It is guaranteed that there is no pair of intersecting segments. In other words, for each pair of segments $$$i$$$ and $$$j$$$ either $$$r_i < l_j$$$ or $$$r_j < l_i$$$.

Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$p_i$$$ ($$$0 \le x_i \le a$$$, $$$1 \le p_i \le 10^5$$$) — the location and the weight of the $$$i$$$-th umbrella.

Output format:
Print "-1" (without quotes) if Polycarp can't make his way from point $$$x = 0$$$ to point $$$x = a$$$. Otherwise print one integer — the minimum total fatigue after reaching $$$x = a$$$, if Polycarp picks up and throws away umbrellas optimally.

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

Note:
In the first example the only possible strategy is to take the fourth umbrella at the point $$$x = 1$$$, keep it till the point $$$x = 7$$$ (the total fatigue at $$$x = 7$$$ will be equal to $$$12$$$), throw it away, move on from $$$x = 7$$$ to $$$x = 8$$$ without an umbrella, take the third umbrella at $$$x = 8$$$ and keep it till the end (the total fatigue at $$$x = 10$$$ will be equal to $$$14$$$).

In the second example the only possible strategy is to take the first umbrella, move with it till the point $$$x = 9$$$, throw it away and proceed without an umbrella till the end.

Example input:
10 2 4
3 7
8 10
0 10
3 4
8 1
1 2

Example output:
14

Example input:
10 1 1
0 9
0 5

Example output:
45

Editorial:
Any experienced contestant can easily guess that the problem can be solved with dynamic programming. Coordinates are not really large so you can precalculate the array $$$rain_0, rain_1, \dots, rain_{a -1 }$$$, where $$$rain_i$$$ is a boolean value — $$$True$$$ if there exists some segment of rain to cover the segment between positions $$$i$$$ and $$$i + 1$$$ and $$$False$$$ otherwise. This can be done in $$$O(n \cdot a)$$$ with the most straightforward algorithm. You can also precalculate another array $$$umb_0, umb_1, \dots, umb_a$$$, where $$$umb_i$$$ is the index of the umbrella of minimal weight at position $$$i$$$ or $$$-1$$$ if there is no such umbrella.
Now let $$$dp[i][j]$$$ be the minimal total fatigue you can take if you are holding umbrella number $$$j$$$ on the end of the walk up to position $$$i$$$. If $$$j = 0$$$ then you hold no umbrella. Initially all the values are $$$\infty$$$ and $$$dp[0][0]$$$ is $$$0$$$. You can either hold your umbrella, drop it or pick up the best one lying there (and drop the current one if any) when going from some position $$$i$$$ to $$$i + 1$$$. So here are the transitions for these cases:
• $$$dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + weight_j)$$$ if $$$j > 0$$$;
• $$$dp[i + 1][0] = min(dp[i + 1][0], dp[i][j])$$$ if $$$rain[i] = False$$$;
• $$$dp[i + 1][umb_i] = min(dp[i + 1][umb_i], dp[i][j] + weight[umb_i])$$$ if $$$umb_i \neq -1$$$.
The answer is equal to $$$\min\limits_{i = 0}^{m} dp[a][i]$$$. If it is $$$\infty$$$ then there is no answer.
So you have $$$a \cdot m$$$ states and all the transitions are $$$O(1)$$$.
Overall complexity: $$$O(a \cdot (n + m))$$$.
There is also a solution in $$$O(a \log^2 a)$$$ with Convex Hull Trick using Li Chao tree. You can probably even achieve $$$(n + m) \log^2 (n + m)$$$ with some coordinate compression. Obviously this wasn't required for the problem as the constraints are small enough.