[深搜]A. 【例题1】拔河比赛

A . 【 例 题 1 】 拔 河 比 赛 A. 【例题1】拔河比赛 A.1


在这里插入图片描述
在这里插入图片描述


解析

模板题,选与不选


Code

#include <bits/stdc++.h>
#define ll long long
using namespace std;

int w[25];
int t, n, ans, sum;

void dfs (int x, int y, int z)
{
	if (y == n / 2)
	{
		ans = min (ans, abs (z * 2 - sum));
		return ;	
	}
	if (x > n) return ;
	dfs (x + 1, y + 1, z + w[x]);
	dfs (x + 1, y, z);
}

int main ()
{
	scanf ("%d", &t);
	while (t --)
	{
		sum = ans = 0;
		scanf ("%d", &n);
		for (int i = 1; i <= n; ++ i) scanf ("%d", &w[i]), sum += w[i];
		ans = sum;
		dfs (1, 0, 0);
		printf ("%d\n", ans);
	}	
	return 0;
} 
posted @ 2021-01-22 11:36  unknown_future  阅读(88)  评论(0编辑  收藏  举报