Your Digits
Your Digits
You have \(k_2\) digits \(2\), \(k_3\) digits \(3\), \(k_5\) digits \(5\), and \(k_6\) digits \(6\). You want to form numbers \(32\) and \(256\) using these digits (each digit at most once) to maximize their total sum. Find the maximum possible sum.
Solution
We can see that it’s better to make number \(256\) than \(32\). So we will make \(256\) for as long as we can, and then with remaining digits number \(32\). Number \(256\) takes digits \(2,5,6\) each ones, so we can make at most \(k=\)min(\(k_2, k_5, k_6\)) those numbers. With remaining digits, which are \(k_3\) digits \(3\) and \(k_2 - k\) digits \(2\) (we used \(k\) of them to make \(256\)), we create min(\(k_3, k_2-k\)) numbers \(32\).
#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';
}