Money

Money

Time Limit: 1s Memory Limit: 128MB

Submissions: 41 Solved: 12
Description

Kile and Pogi have found ​N banknotes on the street. After making sure that the original  owner is nowhere to be found, they decided to split the banknotes amongst themselves. In the end they want to “win” the same amount of money sothey split the banknotes in such a way. Of course, the sum of the banknotes nobody ends up having is ​the least possible ​one. Since they couldn’t just leave the remaining banknotes on the street, they decided to go to a nearby casino and put everything on ​red​, hoping that they would end up getting twice the money they bet. The roulette decided on the (lucky,for this time) number 13 and our heroes decided to split the money they won. The payout is such that Kile and Pogi will always be able to split the money they won into two equal parts. Because of the immense adrenaline rush, the boys have lost their mathematical abilities. Help them figure out how much money each of them is taking home. 

Input

 The first line of input contains the integer ​N

​ (1 ≤ ​N ≤ 500) that denotes the number of banknotes on the street. Each of the following ​N lines contains a single positive integer ​ci​ that denotes the value of the ​ith banknote in kunas (kn). The total sum of money will not exceed 100 000 kn.  

Output

You must output the amount of money each of them took home. 

Sample Input
Input
1
4
2
3
1
6
Input
2
5
2
3
5
8
1
3
Sample Output
Output
16
Output
18


题意:反正我是看了好长时间没看懂,意思是:从n个数字里面取任意个数字,不能重复得到两个m,且保证m最大。

虽然当时感觉是dp,但是还是没写出来。看了题解之后发现自己真的好傻

题解:查出所有能够通过n个数字中任意个来组成的数字,维护一个最大值m,为偶数,并且,m/2 和 m 都在之前查出的数组中


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn=1e5+10;
bool dp[maxn]; //所有能够组成的数字
int n[maxn];

int main()
{
    int t,sum=0;
    scanf("%d",&t);
    for(int i=0;i<t;i++)
        scanf("%d",&n[i]),sum+=n[i];
    memset(dp,0,sizeof(dp));

    dp[0]=1;

    for(int i=0;i<t;i++){
        for(int j=sum;j>=n[i];j--){
            if(dp[j-n[i]])
                dp[j]=1;
        }
    }
    for(int j=sum;j>=0;j--){
        if( !(j&1) && dp[j] && dp[j>>1]){
            printf("%d",sum-(j>>1));
            break;
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值