QOJ.ac

QOJ

実行時間制限: 12 s メモリ制限: 256 MB 満点: 100

#10623. Wagons

統計

There are $n$ train cars standing on a one-dimensional track. Initially, no two cars are connected. In each move, you can connect two adjacent groups of connected cars, provided that the number of cars in these groups does not differ by more than $d$. The time taken for such a connection depends on the number of cars in the groups being connected. Your task is to connect all groups into a single train consisting of $n$ cars.

The cost of connecting a group of $w$ cars with a group of $v$ cars (provided $|w - v| \le d$) is given by the function $(aw + bv) \pmod{1001}$.

Input

The only line of input contains four positive integers $n$, $d$, $a$, and $b$ ($1 \le n \le 10^{16}$, $1 \le d, a, b \le 1000$).

Output

The only line of output should contain a single integer representing the minimum cost to connect all $n$ cars into one train.

Examples

Input 1

5 1 1 1

Output 1

12

Note

Explanation of the example: We connect the first car with the second (cost 2), the third with the fourth (cost 2), then with the fifth (cost 3), and finally we connect the two resulting groups of cars (cost 5).

Ocenianie

Subtask Constraints Points
1 $n \le 100\,000$ 21
2 $d \le 300$ 46
3 No additional constraints 33

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.