[ create a new paste ] login | about

Link: http://codepad.org/clATGtJk    [ raw code | output | fork ]

C, pasted on Apr 10:
#include <stdio.h>
long long int weight[1002];
long long int cost[1002];
long long int cap;
long long int dp[1002][102];
long long int fun(long long int i,long long int w)
{
    long long int a,b;
    if(dp[i][w]==-1){
        if(weight[i]>cap){
            dp[i][w]=fun(i-1,w);
        }
        if(weight[i]>w){
            dp[i][w]=fun(i-1,w);
        }
        else{
            a=fun(i-1,w);
            b=fun(i-1,w-weight[i])+cost[i];
            if(a>b){
                dp[i][w]=a;
            }
            else{
                dp[i][w]=b;
            }
        }
        return dp[i][w];
    }
    else{
        return dp[i][w];
    }
    if(i==0 || w==0){
        dp[i][w]=0;
        return dp[i][w];
    }
}
int main()
{
    long long int c,d,a,b,e,G,N,T,k,ans;
    for(c=0;c<1003;c=c+1){
        for(d=0;d<103;d=d+1){
            dp[c][d]=-1;
        }
    }
    a=1;
    scanf("%lld",&T);
    while(a<=T){
        for(c=0;c<1003;c=c+1){
        for(d=0;d<103;d=d+1){
            dp[c][d]=-1;
        }
    }
        for(c=0;c<1003;c=c+1){
            cost[c]=0;
            weight[c]=0;
        }
        ans=0;
        scanf("%lld",&N);
        for(c=0;c<N;c=c+1){
            scanf("%lld %lld",&cost[c],&weight[c]);
        }
        scanf("%lld",&G);
        for(c=0;c<G;c=c+1){
            scanf("%lld",&cap);
            k=fun(N,cap);
            ans=ans+k;
        }
        printf("%lld\n",ans);
        a=a+1;
    }
    return 0;
}


Output:
1
Timeout


Create a new paste based on this one


Comments: