#P6767. [BalticOI 2012/2020] 玫瑰 (Day0)

    ID: 7590 远端评测题 500ms 256MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>枚举最大公约数 gcdBalticOI(波罗的海)

[BalticOI 2012/2020] 玫瑰 (Day0)

Background

For special reasons, the other 2424 testdata sets of this problem should be submitted here.

Valentine always wins.

Problem Description

Valentine wants to buy roses for his NN girlfriends. There are two shops in front of him. Each shop has an unlimited number of roses, but they sell roses only in bundles. In the first shop, each bundle contains AA roses and costs BB money. In the second shop, each bundle contains CC roses and costs DD money.

Find the minimum amount of money Valentine needs to spend to buy at least NN roses.

You can understand it like this: if M>NM > N but buying MM roses costs less than buying NN roses, then Valentine will buy MM roses and give the extra roses to other girls. Yes, Valentine is very “playboy” (huaxin).

Input Format

One line with five integers N,A,B,C,DN, A, B, C, D, with meanings as described in the statement.

Output Format

One line with one integer representing the minimum cost.

5 1 4 3 6
12
22 2 3 10 14
31

Hint

Sample Explanation

For sample 11, Valentine can choose to buy 22 bundles from the second shop.

For sample 22, Valentine can choose to buy 11 bundle from the first shop and 22 bundles from the second shop.

Constraints

This problem uses bundled tests.

  • Subtask 1 (20 pts): N,A,B,C,D1000N, A, B, C, D \le 1000.
  • Subtask 2 (80 pts): no special restrictions.

For 100%100\% of the data, 1N10151 \le N \le 10^{15}, 1A,B,C,D1051 \le A, B, C, D \le 10^5, and it is guaranteed that the answer does not exceed 101810^{18}.

Notes

Translated from BalticOI 2020 Day0 B Roses.

It is the same as BalticOI 2012 Day0 A.

Translated by ChatGPT 5