1poj2209(贪心)

来源:互联网 发布:win10关闭软件快捷键 编辑:程序博客网 时间:2024/05/29 06:51

http://poj.org/problem?id=2209

The King
Time Limit: 2000MSMemory Limit: 65536KTotal Submissions: 5568Accepted: 3111

Description

Once upon a time in a countryfar away lived a king and he had a big kingdom. He was a veryclever king but he had one weakness -- he could count only up tothree.
Nevertheless, he did not consider this to be a really greatdrawback, since he had a lot of wizards who could count up to onehundred (and some of them, people said, even up to one thousand),so it was all right. But one day the grief came to the kingdom asthe outnumbering barbarians started to approach from all sides. Andthe king then had to make the most important decision in his life.He had to choose which of his sons to make generals that he wouldsend to the borders of the country to lead the army.
However, the king knew that though some of his sons were clever,just like he was, some of them were quite stupid and could onlylower army spirits with their wrong decisions. More precisely, heknew about each of his sons his mental potential -- an integernumber ranging from minus three to three (remember, that the kingcould count only up to three). He also knew that the chance of hisarmy defeating barbarians was proportional to the sum of somepowers of mental potentials of those of his sons that he would makegenerals (the power exponent was a positive integer number, thesame for all his sons and not exceeding three either). Thus he hadto choose such a combination of his sons to lead
the army, that this sum would be maximal possible.
However, the king himself could not make all apropriatecalculations since, for example, the second power of the number notexceeding three (which is its square) could be greater than three,and therefore he asked you, his most intellegent wizard, to solvethis problem.

Input

The first line of the input filecontains the number of the sons of the king (integer number less orequal to one hundred). The second line contains the positiveinteger number not exceeding three, the exponent in the formulaused to calculate the chance of defeating barbarians. The thirdline contains the list of mental potentials of king’s sons -- allinteger numbers, not greater than three by their absolutevalue.

Output

Output the only number -- themaximal possible chance of defeating barbarians measured as the sumdescribed.

Sample Input

332 -1 1

Sample Output

9

Hint

In the example above the kingshould choose his first and third sons to be the generals. In thiscase the chance to defeat barbarians, which is the sum of cubes ofmental potentials of these sons, is eight plus one, that isnine.

Source

Northeastern Europe 2002, Northern Subregion
题意:求n个数的k次方的和的最大值。。。
#include<stdio.h>
#include<math.h>
int a[101];
int main()
{
 int n;
 scanf("%d",&n);
 int i,e;
 scanf("%d",&e);
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
 int sum=0;
 for(i=0;i<n;i++)
 {
  if(a[i]>=0)
   sum+=pow((double)a[i],(double)e);
  elseif(a[i]<0&&e%2==0)
   sum+=pow((double)a[i],(double)e);
 }
 printf("%d\n",sum);
 return 0;
}
原创粉丝点击