Cycling the segments with gears

A route has N segments; segment i has a slope and a length. Your bike has gears 1 to G. Riding a segment in gear g costs |slope − g| × length. Between segments you may change gear from g to h for s × |g − h|. The starting gear is free. Find the minimum total cost.

Reported in: Infosys (SP / DSE · 22 Nov 2026) (memory-based)

Input

First line: N G s. Next N lines: slope and length.

Output

The minimum total cost.

Constraints

1 ≤ N, G ≤ 2000; 0 ≤ slope, length, s ≤ 10^4.

Example 1

Input:
3 5 2
1 10
5 6
2 3

Output:
14

Example 2

Input:
2 3 100
1 5
3 5

Output:
10

Example 3

Input:
1 4 1
9 2

Output:
10

Hint: dp[g] is the cheapest cost so far finishing a segment in gear g. The cheapest way to start the next segment in gear h is the minimum of dp[g] + s·|g − h| over all g; one sweep left to right and one right to left give all of these in O(G).

All coding practice