Under construction

Still being built. Things may change

Headshot

Interview

Time limit2sMemory limit1024 MB

Summary
Given n shots, magazine size m, full reload time a, and per-round load time b, find the minimum total time to fire exactly n shots.
Level

Medium4 of 10

Topics
Greedy, Math
Solved
No attempts yet

Statement

Agent Johnny English has come across his next target, and he has to kill him. To make sure his clumsiness does not get in the way, he decides to fire exactly nn shots from his pistol. The pistol has a magazine that holds mm rounds. It starts empty.

Johnny can fully reload the pistol in aa seconds, or load one round into the magazine in bb seconds. Each shot takes exactly one second. Find the minimum time Johnny needs to fire exactly nn shots. He cannot fire an empty pistol, and he cannot load a round into a full magazine.

Input

The first line contains four integers nn, mm, aa, and bb (1≤n,m,a,b≤1041 \le n, m, a, b \le 10^4). They are the number of shots to fire, the magazine size, the time for a full reload, and the time to load one round.

Output

Print one integer, the minimum time Johnny needs to fire exactly nn shots.

Hint

In the test from the statement, Johnny fully reloads once, fires twice, loads one more round, and fires it.

Examples1

  1. Example 1

    Input
    3 2 1 1
    
    Expected output
    5