#P6746. 『MdOI R3』Operations

『MdOI R3』Operations

Description

Given non-negative integers a,ba, b, there are two operations:

  1. Choose any positive integer xx, subtract xx from both numbers. The cost of performing this operation once is cc.
  2. Choose any positive integer xx, multiply one of the two numbers by xx, and divide the other by xx and then take the floor. The cost of performing this operation once is dd.

Here, taking the floor means changing a number to the greatest integer that is not greater than it. For example, the floor of 3.53.5 is 33, and the floor of 0.07-0.07 is 1-1.

The chosen xx can be any positive integer. During the operations, aa and bb may become negative.

You may perform operations on these two numbers any number of times. Find the minimum total cost to turn both aa and bb into 00.

Input Format

One line with four integers a,b,c,da, b, c, d, separated by spaces, representing the initial values of a,ba, b and the costs of the two operations.

Output Format

One line with one integer, the minimum cost.

9 36 1 3
4

Hint

[Sample Explanation]

First use operation 22 once, choose x=2x = 2, multiply aa by 22, and divide bb by 22, obtaining a=18,b=18a = 18, b = 18.
Then use operation 11 once, choose x=18x = 18, subtract 1818 from both numbers, obtaining a=0,b=0a = 0, b = 0.
It can be proven that there is no solution with a smaller total cost than the one above.

For more samples, please get them here.

[Constraints]

This problem uses bundled testdata, in other words, you can get the score of a subtask only if you pass all test points in that subtask.

Subtask ID a=0a=0 b=0b=0 a=ba=b c=1,d105c=1,d\geq 10^5 c105,d=1c \geq 10^5,d=1 Score
11 \surd \surd 10
22
33 \surd \surd
44
55 \surd \surd
66
77 40

The special properties are as shown above, where \surd means this special property is guaranteed, and a blank cell means it is not guaranteed.

For all data, 0a,b,c,d1090\leq a,b,c,d \leq 10^9.

Input Format

One line with four integers a,b,c,da,b,c,d.

Output Format

One line with one integer.

Hint

Translated by ChatGPT 5