bzoj2748

题解:

简单dp

代码:

#include 
using namespace std;
const int N=1005;
int n,x,m,a[N],f[N][N];
int main()
{
    scanf("%d%d%d",&n,&x,&m);
    for (int i=1;i<=n;i++)scanf("%d",&a[i]);
    f[0][x]=1;
    for (int i=1;i<=n;i++)
     for (int j=0;j<=m;j++)
      {
           if (j+a[i]<=m&&f[i-1][j+a[i]])f[i][j]=1;
        if (j-a[i]>=0&&f[i-1][j-a[i]])f[i][j]=1;
       } 
    for (int i=m;i>=0;i--)
     if (f[n][i]){printf("%d",i);return 0;}
    printf("-1");
    return 0;
}

 

转载于:https://www.cnblogs.com/xuanyiming/p/8954983.html

你可能感兴趣的:(数据结构与算法)