CF940B.Our Tanya is Crying Out Loud

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Right now she actually isn't. But she will be, if you don't solve this problem.

You are given integers nn , kk , AA and BB . There is a number xx , which is initially equal to nn . You are allowed to perform two types of operations:

  1. Subtract 1 from xx . This operation costs you AA coins.
  2. Divide xx by kk . Can be performed only if xx is divisible by kk . This operation costs you BB coins.

What is the minimum amount of coins you have to pay to make xx equal to 11 ?

输入格式

The first line contains a single integer nn ( 1<=n<=21091<=n<=2·10^{9} ).

The second line contains a single integer kk ( 1<=k<=21091<=k<=2·10^{9} ).

The third line contains a single integer AA ( 1<=A<=21091<=A<=2·10^{9} ).

The fourth line contains a single integer BB ( 1<=B<=21091<=B<=2·10^{9} ).

输出格式

Output a single integer — the minimum amount of coins you have to pay to make xx equal to 11 .

输入输出样例

  • 输入#1

    9
    2
    3
    1
    

    输出#1

    6
    
  • 输入#2

    5
    5
    2
    20
    

    输出#2

    8
    
  • 输入#3

    19
    3
    4
    2
    

    输出#3

    12
    

说明/提示

In the first testcase, the optimal strategy is as follows:

  • Subtract 1 from xx ( 989→8 ) paying 3 coins.
  • Divide xx by 2 ( 848→4 ) paying 1 coin.
  • Divide xx by 2 ( 424→2 ) paying 1 coin.
  • Divide xx by 2 ( 212→1 ) paying 1 coin.

The total cost is 66 coins.

In the second test case the optimal strategy is to subtract 1 from xx 44 times paying 88 coins in total.

首页