This page is still under construction.

Parts of this page are still being built. What you see may change.

Calculator

Time limit1sMemory limit512 MB

Summary
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 nn, 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 nn and plans to press the operation buttons in some order. Specifically, he plans to press button A a total of aa times, button B bb times, and button C cc times. He wants to know the smallest number that can result from performing these operations.

Write a program that, given the number nn and the numbers aa, bb, and cc 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: nn, aa, bb, and cc (1≤n≤10181 \le n \le 10^{18}, 0≤a,b,c≤600 \le a, b, c \le 60). 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.

Examples1

  1. Example 1

    Input
    72 2 1 1
    
    Expected output
    4