首页 > 代码库 > bzoj2748:[HAOI2012]音量调节

bzoj2748:[HAOI2012]音量调节

思路:刷水有益健康。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 55
#define maxc 10000
 
int n,a,r,ans;
int c[maxn],f[maxn][maxc];
 
int main(){
    scanf("%d%d%d",&n,&a,&r);
    for (int i=1;i<=n;i++) scanf("%d",&c[i]);
    f[0][a]=1;
    for (int i=1;i<=n;i++)
        for (int j=0;j<=r;j++){
            if (j-c[i]>=0) f[i][j]|=f[i-1][j-c[i]];
            if (j+c[i]<=r) f[i][j]|=f[i-1][j+c[i]];
        }
    ans=-1;
    for (int i=r;i>=0;i--) if (f[n][i]){ans=i;break;}
    printf("%d\n",ans);
    return 0;
}

 

bzoj2748:[HAOI2012]音量调节