#P2773. Maximum Super Elegant String

    ID: 16035 Type: Default 1000ms 256MiB

Maximum Super Elegant String

Maximum Super Elegant String

Caima considers the letters O and X to be the most elegant. A string composed only of O and X is called an elegant string. Among these, a string is defined as a (\textbf{super elegant unbeatable string}) if it satisfies the following conditions:

  1. Every contiguous substring composed solely of X has length at most (\texttt{maxX}).
  2. Every contiguous substring composed solely of O has length at most (\texttt{maxO}).
  3. The total number of O's in the string is at most (\texttt{countO}).
  4. The total number of X's in the string is at most (\texttt{countX}).

You are given four integers: (\texttt{maxX}), (\texttt{maxO}), (\texttt{countO}), and (\texttt{countX}) (in that order). Your task is to determine the maximum length of a super elegant unbeatable string that can be formed under these constraints.

An optimal strategy is to interleave the letters in such a way that no block of consecutive identical characters exceeds the allowed maximum. In fact, note that the maximum number of X's that can be used is (\min(\texttt{countX},, \texttt{maxX} \times (\texttt{countO}+1))) and similarly, the maximum number of O's that can be used is (\min(\texttt{countO},, \texttt{maxO} \times (\texttt{countX}+1))). Thus, the answer is given by:

[ \text{answer} = \min(\texttt{countX},, \texttt{maxX} \times (\texttt{countO}+1)) + \min(\texttt{countO},, \texttt{maxO} \times (\texttt{countX}+1)). ]

Assume that the input values are non‐negative integers and it is possible that one (or both) of (\texttt{countO}) and (\texttt{countX}) is zero.

inputFormat

The input consists of a single line containing four space-separated integers:

(\texttt{maxX}) (\texttt{maxO}) (\texttt{countO}) (\texttt{countX})

Where:

  • (\texttt{maxX}) is the maximum allowed length for any contiguous block of X's.
  • (\texttt{maxO}) is the maximum allowed length for any contiguous block of O's.
  • (\texttt{countO}) is the maximum total number of O's that can be used.
  • (\texttt{countX}) is the maximum total number of X's that can be used.

outputFormat

Output a single integer representing the maximum possible length of a super elegant unbeatable string that can be formed.

sample

1 1 5 5
10