Calculator
Time limit1sMemory limit512 MB
Given n and counts of three halving-style operations (A: floor(n/2), B: floor((n+1)/2), C: floor((n-1)/2), C fixed at 0), find the minimum reachable value.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Math, Greedy, Bit manipulation
- Solved
- No attempts yet
Problem
For a computer science homework assignment, students were asked to build a special calculator that works as follows.
First the user enters a positive integer , which is displayed on the screen. The user can then press three buttons: A, B, and C.
Pressing button A divides the number on the screen by 2. If the number on the screen is odd, the remainder is discarded. For example, the result of this operation is 40 for the number 80 and 119 for the number 239.
Pressing button B adds 1 to the number on the screen and divides the result by 2. The remainder of the division is discarded. For example, the result of this operation is 40 for the number 80 and 120 for the number 239.
Pressing button C does the following. If the number on the screen is positive, 1 is subtracted from it and the result is divided by 2, with the remainder discarded. If the screen showed 0 before button C was pressed, the number stays unchanged. For example, the result of this operation is 39 for the number 80 and 119 for the number 239.
The user entered the number and plans to press the operation buttons in some order. Specifically, he plans to press button A a total of times, button B times, and button C times. He wants to know the smallest number that can result from performing these operations.
Write a program that, given the number and the numbers , , and indicating how many operations of each type were performed on the calculator, determines the smallest number that can result from the calculator's operation.
Input
The input file contains four integers: , , , and (, ). The numbers are given on one line, with adjacent numbers separated by a single space.
Output
Print a single number: the smallest number the user can obtain from the calculator's operation.
Hint
In the example, the user must act optimally as follows: press button B to get 36, then press button A to get 18, then press button C to get 8, then press button A a second time to get 4.