Road Optimization
Contest: Codeforces Round 765 (Div. 2)
Difficulty rating: 1700
Tags: dp

The Government of Mars is not only interested in optimizing space flights, but also wants to improve the road system of the planet.

One of the most important highways of Mars connects Olymp City and Kstolop, the capital of Cydonia. In this problem, we only consider the way from Kstolop to Olymp City, but not the reverse path (i. e. the path from Olymp City to Kstolop).

The road from Kstolop to Olymp City is $$$\ell$$$ kilometers long. Each point of the road has a coordinate $$$x$$$ ($$$0 \le x \le \ell$$$), which is equal to the distance from Kstolop in kilometers. So, Kstolop is located in the point with coordinate $$$0$$$, and Olymp City is located in the point with coordinate $$$\ell$$$.

There are $$$n$$$ signs along the road, $$$i$$$-th of which sets a speed limit $$$a_i$$$. This limit means that the next kilometer must be passed in $$$a_i$$$ minutes and is active until you encounter the next along the road. There is a road sign at the start of the road (i. e. in the point with coordinate $$$0$$$), which sets the initial speed limit.

If you know the location of all the signs, it's not hard to calculate how much time it takes to drive from Kstolop to Olymp City. Consider an example:

Here, you need to drive the first three kilometers in five minutes each, then one kilometer in eight minutes, then four kilometers in three minutes each, and finally the last two kilometers must be passed in six minutes each. Total time is $$$3\cdot 5 + 1\cdot 8 + 4\cdot 3 + 2\cdot 6 = 47$$$ minutes.

To optimize the road traffic, the Government of Mars decided to remove no more than $$$k$$$ road signs. It cannot remove the sign at the start of the road, otherwise, there will be no limit at the start. By removing these signs, the Government also wants to make the time needed to drive from Kstolop to Olymp City as small as possible.

The largest industrial enterprises are located in Cydonia, so it's the priority task to optimize the road traffic from Olymp City. So, the Government of Mars wants you to remove the signs in the way described above.

Input format:
The first line contains three integers $$$n$$$, $$$\ell$$$, $$$k$$$ ($$$1 \le n \le 500$$$, $$$1 \le \ell \le 10^5$$$, $$$0 \le k \le n-1$$$), the amount of signs on the road, the distance between the cities and the maximal number of signs you may remove.

The second line contains $$$n$$$ integers $$$d_i$$$ ($$$d_1 = 0$$$, $$$d_i < d_{i+1}$$$, $$$0 \le d_i \le \ell - 1$$$) — coordinates of all signs.

The third line contains $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 10^4$$$) — speed limits.

Output format:
Print a single integer — minimal possible time to drive from Kstolop to Olymp City in minutes, if you remove no more than $$$k$$$ road signs.

Constraints:
Time limit: 3.0 s
Memory limit: 128.0 MB

Note:
In the first example, you cannot remove the signs. So the answer is $$$47$$$, as it's said in the statements above.

In the second example, you may remove the second and the fourth sign. In this case, you need to drive four kilometers in $$$4\cdot5 = 20$$$ minutes, and then six kilometers in $$$6\cdot3 = 18$$$, so the total time is $$$4\cdot5 + 6\cdot3 = 38$$$ minutes.

Example input:
4 10 0
0 3 4 8
5 8 3 6

Example output:
47

Example input:
4 10 2
0 3 4 8
5 8 3 6

Example output:
38

Editorial:
First you need to understand that this problem must be solved with dynamic programming. Let $$$dp_{i,j}$$$ is the minimum time to drive between the two cities, if we consider first $$$i$$$ signs and have already removed $$$j$$$ signs. We also assume that the $$$i$$$th sign is taken.
Then, the initial state is: $$$dp_{0,0} = 0$$$, $$$dp_{i,0} = dp_{i-1,0} + b_{i-1} \cdot (a_i - a_{i-1})$$$. So, we don't need to drive to the first sign (as it takes $$$0$$$ seconds), and if we don't remove any signs, it's easy to calculate the time.
Initially, fill $$$dp_{i,j} = \infty$$$ for all $$$i = 1 \ldots n, j = 1 \ldots k$$$.
Then, the answer is $$$min(dp_{n,j})$$$ over all $$$j = 0 \ldots k$$$.
Consider which transitions can we make. Calculate our DP from bottom to top, so we go from smaller states to larger ones. Consider all the $$$i = 0 \ldots n-1$$$ and all the $$$j = 0 \ldots k$$$ in the loop order, i. e:
If we see $$$dp_{i, j} = \infty$$$, then there's no answer and we'll just skip this state. For example, it may mean that $$$i < j$$$.
Now, iterate over the positions of the next sign we'll put. Call this position $$$pos$$$. The transitions are as follows: $$$dp_{pos, j + pos - i - 1} = \min(dp_{pos,j + pos - i - 1}, dp_{i, j} + b_i * (a_{pos} - a_i))$$$.
Why such formula? After removing all the signs between $$$[i+1, pos-1]$$$ we will stay on the sign $$$pos$$$, remove $$$pos - i - 1$$$ signs, and the time to go from $$$i$$$ to $$$pos$$$ will depend on the sign $$$i$$$ and the distance between $$$i$$$ and $$$pos$$$.
So, we get the solution in $$$O(n^3)$$$.
There also exists a solution in $$$O(n^2\cdot \log n)$$$, which uses Convex Hull Trick. We don't describe it here, as it's not required to solve the problem.