cswork / OAOpen practice workspace

IBMOA #40

Get Optimal String Length

Estimated MediumJudge-ready

Topics (AI-labelled)Bits & MathGreedy

Using at most countA A's and countB B's, with at most maxA consecutive A's and at most maxB consecutive B's, find the maximum length of a string you can construct. You need not use all the characters; a maximum run length of 0 for a letter means that letter cannot be used.

Input

One line containing countA countB maxA maxB, all four integers in 0..10^6.

Output

Output one integer, the answer.

Example 1

Input:

5 1 2 1

Output:

5

Example 2

Input:

3 4 0 2

Output:

2

Example 3

Input:

0 0 0 0

Output:

0

Example notes

The examples give 5, 2 and 0 respectively. In the first example, AABAA can be constructed.

Editorial

Loading solution…