Optimal walk
Easy Accuracy: 41.56% Submissions: 894 Points: 2

Given a point on x axis, (N,0), you are standing at origin and you can only move on x-axis. Lets say you are at (x,0), at each step you can either move one step forward ie (x+1,0) or one step backwards (x-1,0) or make a jump to double your current position (2*x,0). One step movement costs you A while a jump costs you B. If your initial position is (0,0) , find the minimum cost to reach (N,0) .


Example 1:

Input: N = 7, A = 1, B = 100
Output: 7
Explaination: Move forward to (N, 0) one 
unit at each step.


Example 2:

Input: N = 8, A = 2, B = 4
Output: 12
Explaination: The optimal movement is to 
1, 2, 3, 4, 8.


Your Task:
You do not need to read input or print anything. Your task is to complete the function optimalWalk() which takes N, A and B as input parameters and returns the minimum cost to go to (N, 0) from (0, 0).


Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)


Constraints:
1 ≤ N ≤ 105
1 ≤ A, B ≤ 109   

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.


to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Optimal walk

Output Window