题解:

简单dp

代码:

#include<bits/stdc++.h> 
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;
}

 

posted on 2018-04-26 20:56  宣毅鸣  阅读(92)  评论(0编辑  收藏  举报