装载问题

Problem Description

在一批共n个集装箱要装上艘载重量为c的轮船,其中集装箱i的重量为wi。找出一种最优装载方案,将轮船尽可能装满,即在装载体积不受限制的情况下,将尽可能重的集装箱装上轮船。

Input

输入有多组数据,每组数据的第一行有两个正整数n和c。n是集装箱数,c是轮船的载重量。接下来的1行中有n个正整数,表示集装箱的重量。

Output

对于每组数据输出最大装载重量。

Sample Input

5 10
7 2 6 5 4

Sample Output

10


<pre name="code" class="cpp">#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int c , n , ans;
int a[1000];
bool f[10000];

int main()
{
    while (cin >> n >> c)
    {
        for(int i = 1 ; i <= n ; i++)    scanf("%d" , &a[i]);
        ans = 0;
        memset(f,0,sizeof(f));
        f[0] = true;
        for(int i = 1 ; i <= n ; i++)
            for(int j = c ; j >= a[i] ; j--)
                if (f[j - a[i]] == true)
                {
                    f[j] = true;
                    if (j > ans) ans = j;
                }
        cout << ans << endl;
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值