Given the count of digits 1, 2, 3, 4. Using these digits you are allowed to only form numbers 234 and 12. The task is to find the maximum possible sum that can be obtained after forming the numbers.

**Note**: The aim is only to maximize the sum, even if some of the digits left unused.

**Examples:**

Input :c1 = 5, c2 = 2, c3 = 3, c4 = 4Output :468Explanation :We can form two 234sInput :c1 = 5, c2 = 3, c3 = 1, c4 = 5Output :258Explanation :We can form one 234 and two 12s

**Approach** : An efficient approach is to first try to make 234’s. The possible number of 234s are minimum of c2, c3, c4. After this, with remaining 1’s and 2’s try to form 12s.

Below is the implementation of the above approach :

## C++

`// CPP program to maximum possible sum ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the maximum possible sum ` `int` `Maxsum(` `int` `c1, ` `int` `c2, ` `int` `c3, ` `int` `c4) ` `{ ` ` ` `// To store required sum ` ` ` `int` `sum = 0; ` ` ` ` ` `// Number of 234's can be formed ` ` ` `int` `two34 = min(c2, min(c3, c4)); ` ` ` ` ` `// Sum obtained with 234s ` ` ` `sum = two34 * 234; ` ` ` ` ` `// Remaining 2's ` ` ` `c2 -= two34; ` ` ` ` ` `// Sum obtained with 12s ` ` ` `sum += min(c2, c1) * 12; ` ` ` ` ` `// Return the requied sum ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `c1 = 5, c2 = 2, c3 = 3, c4 = 4; ` ` ` ` ` `cout << Maxsum(c1, c2, c3, c4); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to maximum possible sum ` `class` `GFG ` `{ ` ` ` `// Function to find the maximum possible sum ` `static` `int` `Maxsum(` `int` `c1, ` `int` `c2, ` `int` `c3, ` `int` `c4) ` `{ ` ` ` `// To store required sum ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// Number of 234's can be formed ` ` ` `int` `two34 = Math.min(c2,Math.min(c3, c4)); ` ` ` ` ` `// Sum obtained with 234s ` ` ` `sum = two34 * ` `234` `; ` ` ` ` ` `// Remaining 2's ` ` ` `c2 -= two34; ` ` ` ` ` `// Sum obtained with 12s ` ` ` `sum +=Math.min(c2, c1) * ` `12` `; ` ` ` ` ` `// Return the requied sum ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `c1 = ` `5` `, c2 = ` `2` `, c3 = ` `3` `, c4 = ` `4` `; ` ` ` ` ` `System.out.println(Maxsum(c1, c2, c3, c4)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to maximum possible sum ` ` ` `# Function to find the maximum ` `# possible sum ` `def` `Maxsum(c1, c2, c3, c4): ` ` ` ` ` `# To store required sum ` ` ` `sum` `=` `0` ` ` ` ` `# Number of 234's can be formed ` ` ` `two34 ` `=` `min` `(c2, ` `min` `(c3, c4)) ` ` ` ` ` `# Sum obtained with 234s ` ` ` `sum` `=` `two34 ` `*` `234` ` ` ` ` `# Remaining 2's ` ` ` `c2 ` `-` `=` `two34 ` ` ` `sum` `+` `=` `min` `(c2, c1) ` `*` `12` ` ` ` ` `# Return the requied sum ` ` ` `return` `sum` ` ` `# Driver Code ` `c1 ` `=` `5` `; c2 ` `=` `2` `; c3 ` `=` `3` `; c4 ` `=` `4` `print` `(Maxsum(c1, c2, c3, c4)) ` ` ` `# This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to maximum possible sum ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the maximum possible sum ` `static` `int` `Maxsum(` `int` `c1, ` `int` `c2, ` `int` `c3, ` `int` `c4) ` `{ ` ` ` `// To store required sum ` ` ` `int` `sum = 0; ` ` ` ` ` `// Number of 234's can be formed ` ` ` `int` `two34 = Math.Min(c2, Math.Min(c3, c4)); ` ` ` ` ` `// Sum obtained with 234s ` ` ` `sum = two34 * 234; ` ` ` ` ` `// Remaining 2's ` ` ` `c2 -= two34; ` ` ` ` ` `// Sum obtained with 12s ` ` ` `sum +=Math.Min(c2, c1) * 12; ` ` ` ` ` `// Return the requied sum ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `c1 = 5, c2 = 2, c3 = 3, c4 = 4; ` ` ` ` ` `Console.WriteLine(Maxsum(c1, c2, c3, c4)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to maximum possible sum ` ` ` `// Function to find the maximum possible sum ` `function` `Maxsum(` `$c1` `, ` `$c2` `, ` `$c3` `, ` `$c4` `) ` `{ ` ` ` `// To store required sum ` ` ` `$sum` `= 0; ` ` ` ` ` `// Number of 234's can be formed ` ` ` `$two34` `= min(` `$c2` `, min(` `$c3` `, ` `$c4` `)); ` ` ` ` ` `// Sum obtained with 234s ` ` ` `$sum` `= ` `$two34` `* 234; ` ` ` ` ` `// Remaining 2's ` ` ` `$c2` `-= ` `$two34` `; ` ` ` ` ` `// Sum obtained with 12s ` ` ` `$sum` `+= min(` `$c2` `, ` `$c1` `) * 12; ` ` ` ` ` `// Return the requied sum ` ` ` `return` `$sum` `; ` `} ` ` ` `// Driver code ` `$c1` `= 5; ` `$c2` `= 2; ` `$c3` `= 3; ` `$c4` `= 4; ` ` ` `echo` `Maxsum(` `$c1` `, ` `$c2` `, ` `$c3` `, ` `$c4` `); ` ` ` `// This code is contributed by Ryuga ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

468

## Recommended Posts:

- Count positive integers with 0 as a digit and maximum 'd' digits
- Find the number in a range having maximum product of the digits
- Find maximum product of digits among numbers less than or equal to N
- Find the total count of numbers up to N digits in a given base B
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find the count of maximum contiguous Even numbers
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Find the row whose product has maximum count of prime factors
- Find maximum number that can be formed using digits of a given number
- Find smallest possible Number from a given large Number with same count of digits
- Find count of digits in a number that divide the number
- Find the average of k digits from the beginning and l digits from the end of the given number
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Maximum possible time that can be formed from four digits
- Divide a number into two parts such that sum of digits is maximum
- Count numbers with same first and last digits
- Count digits in a factorial | Set 1

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.