(题面经本蒟蒻美化)
kk因为没有买到自己心爱的歌手的演唱门票而伤心欲绝。他有n个志同道合朋友( 1<= n <= 21),他和朋友谈心就能获得快乐。虽然谈的开心,但聊天也会使还未完全从悲伤中缓过来的虚弱的kk感到疲劳,体力流失。现在kk有100点体力,当体力为0时,他会累得晕过去而忘记之前交谈而获得的短暂的快乐。
现在有多组数据,对每组数据都第一行都为n,表明有n个朋友。接下来两行分别为与每位朋友聊天消耗的体力和获得的快乐。当n = 0时停止输入;
输入样例:
3
1 21 79
20 30 25
4
100 100 100 100
1 2 3 4
0
样例输出
50
0
简单的01背包
#include"iostream"
#include"cstdio"
#include"cstring"
using namespace std;
int a[30],b[30],f[101],n,m,c[1000];
void input()
{
for(int i = 0;i < n;i++)
cin >> a[i];
for(int i = 0;i < n;i++)
cin >> b[i];
}
int solve()
{
memset(f,0,sizeof(f));
for(int i = 0;i < n;i++)
for(int j = 99;j >= a[i];j--)
f[j] = max(f[j],f[j - a[i]] + b[i]);
c[m++] = f[99];
}
void output()
{
for(int i = 0;i < m;i++)
cout << c[i] <<endl;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
input();
solve();
}
output();
return 0;
}