Balance problems [DP]

Description–

In order to test a small C small X, will be out of competition for the small matter of an X-related physical problems: now given the quality of the n weight, asked the little X can say how many kinds of quality items, but there are always busybodies want to destroy, and thus, n has reached 500, far beyond the small X can afford, perseverance, he decided to seek your help.


Input–

A first input line of a positive integer N
of the following N lines each a positive integer not exceeding 200, in turn represents the mass of each weight.

Output–

Maximum capacity of total output, said a number of different quality items.


Sample Input–

3
1
3
9

Sample Output–

13


Notes -

Note: The balance has two sides, both sides can be put.


Code -

#include<iostream>
#include<cstdio>
using namespace std;
bool f[505][40005];
int n,a,ans;
int main()
{
	scanf("%d",&n);
	f[0][20000]=1; //初始化:0个砝码可以称0kg的物品
	for (int i=1;i<=n;++i)
	{
	    scanf("%d",&a);
	    for (int j=0;j<=40000;++j)
	      if (f[i-1][j])
	        {
			    f[i][j]=1;
	        	if (j+a<40000) f[i][j+a]=1;
	    		if (j-a>=0) f[i][j-a]=1;
	        }
	}
	for (int i=20001;i<=40000;++i)
	  if (f[n][i])
	    ans++;
	printf("%d",ans);
	
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_43654542/article/details/90725541