Your Digits

Your Digits
Codeforces easy

A box contains digit cards: \(k_2\) cards with the digit \(2\), \(k_3\) cards with the digit \(3\), \(k_5\) cards with the digit \(5\), and \(k_6\) cards with the digit \(6\).

You can build only two numbers from the cards:

  • the number \(32\), which uses one card \(3\) and one card \(2\);
  • the number \(256\), which uses one card \(2\), one card \(5\), and one card \(6\).

You can build each number any number of times, but every card can be used at most once. Cards that you do not use are not counted.

Find the maximum possible sum of the numbers you build.

Constraints:

  • \(0 \le k_2, k_3, k_5, k_6 \le 5 \cdot 10^6\)

Input:

  • One line: four integers \(k_2\), \(k_3\), \(k_5\), \(k_6\).

Output:

  • One integer — the maximum possible sum.

Example:

Input:

5 1 3 4

Output:

800

(Build \(256\) three times and \(32\) once: \(256 + 256 + 256 + 32 = 800\). One card \(2\) and one card \(6\) are not used.)

Solution

Each number \(256\) adds more to the sum than a number \(32\), and both of them use a card \(2\). So it is better to build \(256\) as many times as possible, and then build \(32\) from the remaining cards.

The number \(256\) uses one card \(2\), one card \(5\), and one card \(6\), so we can build it at most \(k = \min(k_2, k_5, k_6)\) times. After that, \(k_3\) cards \(3\) and \(k_2 - k\) cards \(2\) remain, so we can build \(32\) exactly \(\min(k_3, k_2 - k)\) times.

#include <iostream>
using namespace std;

int main() {
    int k2, k3, k5, k6;
    cin >> k2 >> k3 >> k5 >> k6;

    int k = min(min(k2, k5), k6);

    int solution = k * 256 + min(k3, k2 - k) * 32;
    cout << solution << '\n';
}