Google Code Jam '21 Round 1A Problem B - Prime Time
View as PDFYou are playing a new solitaire game called Prime Time. You are given a deck of cards, and each card has a prime number written on it. Multiple cards may have the same number.
Your goal is to divide the cards into two groups in such a way that the sum of the numbers in the first group is equal to the product of the numbers in the second group. Each card must belong to exactly one of the two groups, and each group must contain at least one card. The sum or product of a group that consists of a single card is simply the number on that card.

For example, in the image above, the left group has cards whose sum is  and the right
group has cards whose product is 
. Therefore, this is a valid split into groups.
Your score is the sum of the numbers in the first group (which is equal to the product of the numbers in the second group), or 0 if you cannot split the cards this way at all. What is the maximum score you can achieve?
Input Specification
The first line of the input gives the number of test cases, . 
 test cases follow.
The first line of each test case contains a single integer 
, representing the number of
distinct prime numbers in your deck.
Each of the next 
 lines contains two values: 
 and 
, representing
that you have exactly 
 cards with the prime 
 written on them.
Note that the total number of cards in your deck is the sum of all s.
Output Specification
For each test case, output one line containing Case #x: y, where  is the
test case number (starting from 1) and 
 is the maximum score you can achieve.
Limits
Time limit: 45 seconds.
Memory limit: 1 GB.
.
. (Note that there are exactly 
 distinct primes between 
 and 
)
, for all 
.
Each  is prime.
, for all 
. (The primes are given in strictly increasing order)
, for all 
.
Test Set 1
.
Test Set 2
.
Test Set 3
.
Sample Input
4
5
2 2
3 1
5 2
7 1
11 1
1
17 2
2
2 2
3 1
1
2 7
Sample Output
Case #1: 25
Case #2: 17
Case #3: 0
Case #4: 8
In Sample Case #1, the optimal split is: . Another split is also possible:
, but it gives a lower score.
In Sample Case #2, note that cards with the same number can be placed in different groups.
Comments