Problem Statement
The idols Ami and Mami like playing games. Today they bought a new game. At the beginning of the game a group of slimes appears on the screen. In each turn of the game the player can select any two of the slimes and merge them together. The game ends when there is only one slime left.
Each slime has a positive integer size. Whenever the player merges two slimes, the size of the merged slime is x+y, where x and y are the sizes of the two merged slimes. Additionally, the player is awarded x*y mascots for performing this merge.
Ami and Mami have just started a new game. You are given a vector a containing the initial sizes of all slimes. Ami and Mami really like mascots. Find and return the maximum total number of mascots they can obtain during the game.
Definition
Class:
CombiningSlimes
Method:
maxMascots
Parameters:
vector
Returns:
int
Method signature:
int maxMascots(vector a)
(be sure your method is public)
Limits
Time limit (s):
2.000
Memory limit (MB):
256
Stack limit (MB):
256
Constraints
a will contain between 2 and 100 elements, inclusive.
Each element of a will be between 1 and 100, inclusive.
Examples
0)
{3,4}
Returns: 12
There are two slimes, their sizes are 3 and 4. There is only one possible move: we merge them into a single slime of size 7. Doing so gives us 3*4 = 12 mascots.
1)
{2,2,2}
Returns: 12
In the first turn we will merge any two slimes. The size of the new slime will be 2+2 = 4, and we will gain 2*2 = 4 mascots. In the second turn we will merge the two remaining slimes. The size of the final slime will be 4+2 = 6. The second merge will give us 4*2 = 8 mascots. Hence, the total number of mascots we will obtain is 4 + 8 = 12.
2)
{1,2,3}
Returns: 11
One optimal solution looks as follows: First, merge slimes of sizes 1 and 3. This produces a slime of size 4, and we get 3 mascots. Then, merge slimes of sizes 4 and 2. This produces a slime of size 6, and we get 8 mascots.
3)
{3,1,2}
Returns: 11
The set of slimes is the same as in Example 2, therefore the correct answer is also the same. As we can merge any two slimes, their order in a does not matter.
4)
{7,6,5,3,4,6}
Returns: 395
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
My Solution
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <map>
using namespace std;
class CombiningSlimes
{
public:
int maxMascots(vector <int> a);
};
int CombiningSlimes::maxMascots(vector <int> a)
{
sort(a.begin(), a.end());
int ans = 0;
vector <int> temp;
while(a.size() > 1)
{
int size = a.size();
if (size % 2 != 0) a.resize(size+1, 0), size ++;
temp.clear();
for (int i = 0; i < size; i += 2)
{
temp.push_back(a[i] + a[i+1]);
ans += a[i] * a[i+1];
}
a = temp;
}
return ans;
}