Mårten and Simon enjoy the board game Seven Wonders and have just finished a match. It is time to tally the scores.
One way to score in Seven Wonders is Science. During the game, each player collects Science tokens of three types: Cog, Tablet, and Compass. A player with a Cogs, b Tablets, and c Compasses scores a2+b2+c2+7×min(a,b,c) points.
Wildcard Science tokens complicate the count. Each Wildcard Science token a player holds may be counted as any one of the three ordinary types. For instance, a player with 2 Cogs, 1 Tablet, 2 Compasses, and 1 Wildcard Science token can end up with (3,1,2), (2,2,2), or (2,1,3) Cogs, Tablets, and Compasses. The possible scores are 32+12+22+7×1=21, 22+22+22+7×2=26, and 22+12+32+7×1=21, so the maximum score for this player is 26.
Given the number of tokens each player holds, compute the maximum score each player can reach by assigning the Wildcard Science tokens optimally.