AtCoder ABC100D Patisserie ABC
Problem Statement
Takahashi became a pastry chef and opened a shop La Confiserie d’ABC to celebrate AtCoder Beginner Contest 100.
The shop sells NN kinds of cakes.
Each kind of cake has three parameters “beauty”, “tastiness” and “popularity”. The -th kind of cake has the beauty of , the tastiness of and the popularity of .
These values may be zero or negative.
Ringo has decided to have MM pieces of cakes here. He will choose the set of cakes as follows:
- Do not have two or more pieces of the same kind of cake.
- Under the condition above, choose the set of cakes to maximize (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity).
Find the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses.
Constraints
- is an integer between and (inclusive).
- is an integer between and (inclusive).
- , , () are integers between and (inclusive).
Input
Input is given from Standard Input in the following format:
Output
Print the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses.
Sample Input 1
1 | 5 3 |
Sample Output 1
1 | 56 |
Consider having the -nd, -th and -th kinds of cakes. The total beauty, tastiness and popularity will be as follows:
- Beauty:
- Tastiness:
- Popularity:
The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is . This is the maximum value.
Sample Input 2
1 | 5 3 |
Sample Output 2
1 | 54 |
Consider having the -st, -rd and -th kinds of cakes. The total beauty, tastiness and popularity will be as follows:
- Beauty:
- Tastiness:
- Popularity:
The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is . This is the maximum value.
Sample Input 3
1 | 10 5 |
Sample Output 3
1 | 638 |
If we have the -rd, -th, -th, -th and -th kinds of cakes, the total beauty, tastiness and popularity will be , and , respectively.
The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is . This is the maximum value.
Sample Input 4
1 | 3 2 |
Sample Output 4
1 | 30000000000 |
The values of the beauty, tastiness and popularity of the cakes and the value to be printed may not fit into 32-bit integers.
Idea
设所有蛋糕的下标集合为 ,被挑选的蛋糕下标集合为 ,则 。最后的价值为 ,枚举 、、 三项的符号,共 种情况。
用 个二进制位表示情况, 表示正号, 表示负号。对于一种特定的情况,我们可以知道 个蛋糕中每个蛋糕对最后价值的贡献:比如三项的符号分别为负、负、正,第 个蛋糕的贡献为 ;当 ,其Beauty属性的贡献实际是负的,其他属性类似。枚举所有情况,对于每一种情况,取贡献最大的 个蛋糕,然后更新最大值即可。
这是一种类似反证法的思想:假设最终的情况,当另一种情况比当前情况的价值更大时,就说明当前假设失败。
Code
1 |
|