一定要讀題啊!!
這題說的是——每次一定換,沒說前後不能到一個音量。
[cpp]
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<functional>
#include<algorithm>
#include<stack>
using namespace std;
#define MAXN (50 + 10)
#define MAXMAXLEVEL (1000+1)
int n,start,maxlevel,a[MAXN];
bool f[MAXN][MAXMAXLEVEL];
int main()
{
// freopen("changingsounds.in","r",stdin);
// freopen("changingsounds.out","w",stdout);
memset(f,0,sizeof(f));
scanf("%d%d%d",&n,&start,&maxlevel);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
f[0][start]=1;
for (int i=1;i<=n;i++)
for (int j=0;j<=maxlevel;j++)
{
f[i][j]=j-a[i]>=0? f[i-1][j-a[i]] : 0;
f[i][j]=j+a[i]<=maxlevel? f[i-1][j+a[i]]||f[i][j] : f[i][j];
}
int ans=maxlevel;
while (ans>=0&&!f[n][ans]) ans--;
cout<<ans<<endl;
// while (1);
return 0;
}