1 solutions

  • 0
    @ 2025-3-3 16:24:09

    C :

    #include <stdio.h>
    #include <stdlib.h>
    
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    
    int main(int argc, char *argv[]) {
    	int n,m;
    	while(scanf("%d%d",&n,&m)!=EOF){
    		int w[5500],v[5500];
    		int dp[6500]={0};
    		int size=0;
    		while(n--){
    			int tw,tv,k,c=1;
    			scanf("%d%d%d",&tw,&tv,&k);
    			while(k-c>0){
    				k-=c;
    				w[size]=c*tw;
    				v[size++]=c*tv;
    				c*=2;
    			}
    			w[size]=k*tw;
    			v[size++]=k*tv;
    		}
    		
    		int i,j;
    		for(i=0;i<size;i++){
    			for(j=m;j>=w[i];j--){
    				dp[j]=dp[j]>(dp[j-w[i]]+v[i])?
    				dp[j]:(dp[j-w[i]]+v[i]);
    			}
    		}
    		printf("%d\n",dp[m]); 
    	}
    	return 0;
    }
    

    C++ :

    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int N = 505, M = 6005;
    int p[N],v[N],s[N],f[M]={0};
    
    int main(){
    	int n,m;
    	cin>>n>>m;
    	for (int i=1; i<=n; i++) cin>>p[i]>>v[i]>>s[i];
    	for (int i=1; i<=n; i++)
    		for (int j=m; j>=0; j--)
    			for (int k=0; k<=s[i]; k++)
    				if (j-k*p[i]>=0) f[j]=max(f[j],f[j-k*p[i]]+k*v[i]);
    	cout<<f[m]<<endl;
    	return 0;
    }
    
    • 1

    Information

    ID
    10019
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By