【nyoj 1291 ABS 】

描述

Mr.Ha is a famous scientist .He has just not got a kind of magic medicine called Entropy Cracker.The medicine was preserved in V bottles,and the i-th (1≤i≤N) bottle contains V liters of medicine.To make the magic available, he needs to put the medicine from all the bottles together.

Due to mixing medicine is a dangerous action, Mr.Ha takes a huge container with infinite volume and decides only to pour a whole bottle of medicine into the container each time.After adding a p liter`s bottle of medicine into the container with q liters of medicinein it,the resulted volume of medicine in the container will be |p-q| liters.

Initially the container is empty ,and Mr.Ha can put the bottles of medicine into the container by arbitrary order.Finally if there are R liters of medicine in the container ,Mr.Ha will be able to use the magic to increase the time for R seconds every day so that he can achieve more work! Help Mr.Ha to make an arrangement so that the resulted R is maximum.

输入
The first line contains an integer T,indicating the number of test cases.
For each test case ,the first line contains an integer N 1≤N≤200,indicating the number of botters, and the second line contains V (|vi|<=500),indicating the volume of medicine in each bottle Attention the volume may be negative as a result of magic
The sum of N in all test cases will not exceed 2000.
输出
For each test case , output the case number in the format of the format of the sample ,and an
Integer ,the maximum seconds Mr.Ha will able to increase
样例输入
3
4
1 2 2 9
1
-1
10
1 3 0 0 0 1 2 7 3 7
样例输出
8
1
6

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int pa[210];
int main()
{
    int T,N,i,j,a,ans,sum;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&N);
        i=0;ans=0;sum=0;
        while(N--)
        {
            scanf("%d",&a);
            if(a<0)
                ans-=a;
            else
                pa[++i]=a;
        }
        sort(pa+1,pa+1+i);
        ans+=pa[i];
        i--;
        for(j=1;j<=i;)
        {
           sum=abs(pa[i]-sum);
           i--;
           if(j>i)
            break;
           sum=abs(pa[j]-sum);
           j++;
        }
        ans-=sum;
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值