CF:Card(贪心)
2017-02-25 17:54
246 查看
H. Card
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
After Hasan ruined the Worm puzzle, Rula brought a new game to the office, the Card game.
The game has a group of cards, each card has an integer value Vi written
on it.
![](http://codeforces.com/predownloaded/e0/c7/e0c794553a43cf5b8b1d241ab0f397328667cc66.png)
Initially, N cards are placed side by side in a straight line (refer to the picture above). In each turn you will pick two adjacent
card sets (a single card is also considered a card set) and merge the cards of the two sets together to form one new set.
After each turn, you will add the maximum card value in each card set to your score. The game is over when no more merges could be performed.
What is the maximum score you can get at the end of the game, given that your initial score is 0?
Input
The first line of input contains a single integer N (2 ≤ N ≤ 105),
the number of cards in the game.
The second line contains N space-separated integers, each integer is Vi (1 ≤ Vi ≤ 5000),
which represents the number written on the ith card.
Output
On a single line, print the maximum score you can get.
Examples
input
output
input
output
Note
One of the possible solutions for the first sample is as follows:
Initially, each set will contain a single card: {1} {2} {3} {2} {1}.
Merge {1} with {2}, the resulting sets are
4000
: {1 2} {3} {2} {1}, which will add (2 + 3 + 2 + 1 = 8) to your score.
Merge {2} with {1}, the resulting sets are: {1 2} {3} {2 1}, which will add (2 + 3 + 2 = 7) to your score.
Merge {1 2} with {3}, the resulting sets are: {1 2 3} {2 1}, which will add (3 + 2 = 5) to your score.
Merge {1 2 3} with {2 1}, the resulting sets are: {1 2 3 2 1}, which will add 3 to your score.
Final score is 8 + 7 + 5 + 3 = 23.
思路:要获得最大值,肯定每次操作都将最小的值覆盖掉。
# include <iostream>
# include <cstdio>
# include <algorithm>
using namespace std;
int main()
{
int a[100001], n;
long long sum1, sum2;
while(~scanf("%d",&n))
{
sum1 = sum2 = 0;
for(int i=0; i<n; ++i)
{
scanf("%d",&a[i]);
sum1 += a[i];
}
sum1 *= (n-1);
sort(a, a+n);
for(int i=0; i<n-1; ++i)
{
sum2 += a[i];
sum1 -= sum2;
}
printf("%I64d\n",sum1);
}
return 0;
}
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
After Hasan ruined the Worm puzzle, Rula brought a new game to the office, the Card game.
The game has a group of cards, each card has an integer value Vi written
on it.
![](http://codeforces.com/predownloaded/e0/c7/e0c794553a43cf5b8b1d241ab0f397328667cc66.png)
Initially, N cards are placed side by side in a straight line (refer to the picture above). In each turn you will pick two adjacent
card sets (a single card is also considered a card set) and merge the cards of the two sets together to form one new set.
After each turn, you will add the maximum card value in each card set to your score. The game is over when no more merges could be performed.
What is the maximum score you can get at the end of the game, given that your initial score is 0?
Input
The first line of input contains a single integer N (2 ≤ N ≤ 105),
the number of cards in the game.
The second line contains N space-separated integers, each integer is Vi (1 ≤ Vi ≤ 5000),
which represents the number written on the ith card.
Output
On a single line, print the maximum score you can get.
Examples
input
5 1 2 3 2 1
output
23
input
6 7 9 7 5 4 3
output
108
Note
One of the possible solutions for the first sample is as follows:
Initially, each set will contain a single card: {1} {2} {3} {2} {1}.
Merge {1} with {2}, the resulting sets are
4000
: {1 2} {3} {2} {1}, which will add (2 + 3 + 2 + 1 = 8) to your score.
Merge {2} with {1}, the resulting sets are: {1 2} {3} {2 1}, which will add (2 + 3 + 2 = 7) to your score.
Merge {1 2} with {3}, the resulting sets are: {1 2 3} {2 1}, which will add (3 + 2 = 5) to your score.
Merge {1 2 3} with {2 1}, the resulting sets are: {1 2 3 2 1}, which will add 3 to your score.
Final score is 8 + 7 + 5 + 3 = 23.
思路:要获得最大值,肯定每次操作都将最小的值覆盖掉。
# include <iostream>
# include <cstdio>
# include <algorithm>
using namespace std;
int main()
{
int a[100001], n;
long long sum1, sum2;
while(~scanf("%d",&n))
{
sum1 = sum2 = 0;
for(int i=0; i<n; ++i)
{
scanf("%d",&a[i]);
sum1 += a[i];
}
sum1 *= (n-1);
sort(a, a+n);
for(int i=0; i<n-1; ++i)
{
sum2 += a[i];
sum1 -= sum2;
}
printf("%I64d\n",sum1);
}
return 0;
}
相关文章推荐
- CF-18D - Seller Bob(贪心+简单大数)
- CF 332 C 贪心
- CF 305A(Strange Addition-贪心+分类讨论)
- cf 342B - Xenia and Spies(贪心)
- 【CF 550E】 Brackets in Implications(贪心)
- CF - 794C. Naming Company - 贪心+排序+模拟+字符串处理
- CF - 359 - A. Table(贪心)
- CF - 389 - B. Fox and Cross(贪心)
- 【CF】--- Game of Credit Cards (双端队列&&贪心)
- CF 217A A. Ice Skating(贪心)
- CF-(贪心)
- CF 286C(Main Sequence-贪心括号匹配)
- CF 305A(Strange Addition-贪心+分类讨论)
- CF 196B(Psychos in a Line-单调队列贪心)
- CF 789A Anastasia and pebbles 贪心
- CF 329A(Purification-贪心-非DLX)
- 51nod1621-贪心&枚举&好题&cf-花钱买车牌
- 贪心 CF 333B Chips
- CF - 801B. Valued Keys - 构造+贪心
- [LeetCode]Wildcard Matching 通配符匹配(贪心)