Codeforces Round #588 (Div. 2) A. Dawid and Bags of Candies

链接:

https://codeforces.com/contest/1230/problem/A

题意:

Dawid has four bags of candies. The i-th of them contains ai candies. Also, Dawid has two friends. He wants to give each bag to one of his two friends. Is it possible to distribute the bags in such a way that each friend receives the same amount of candies in total?

Note, that you can't keep bags for yourself or throw them away, each bag should be given to one of the friends.

思路:

乱搞一下就行, 写了个背包

代码:

#include 
using namespace std;

int a[5], dp[1000];
int sum = 0;

int main()
{
    for (int i = 1;i <= 4;i++)
        cin >> a[i], sum += a[i];
    if (sum%2 == 1)
    {
        puts("NO");
        return 0;
    }
    for (int i = 1;i <= 4;i++)
    {
        for (int j = sum/2;j >= a[i];j--)
            dp[j] = max(dp[j], dp[j-a[i]]+a[i]);
    }
    if (dp[sum/2] == sum/2)
        puts("YES");
    else
        puts("NO");

    return 0;
}

你可能感兴趣的:(Codeforces Round #588 (Div. 2) A. Dawid and Bags of Candies)