3 solutions

  • 0
    @ 2026-1-10 14:43:46
    #include<bits/stdc++.h>
    using namespace std;
    const int N=510;
    int f[N*N],v[N],w[N];
    int main()
    {
    	int n,V=N*N-10;
    	int k;
    	cin>>n>>k;
    	for(int i=1;i<=n;i++)
    	{
    		cin>>v[i]>>w[i];
    	}
    	memset(f,0x3f,sizeof f);
    	f[0]=0;
    	for(int i=1;i<=n;i++)
    	{
    		for(int j=V;j>=v[i];j--)
    		{
    			f[j]=min(f[j],f[j-v[i]]+w[i]);
    		}
    	}
    	int ans=0;
    	for(int i=1;i<=V;i++)
    	{
    		if(f[i]<=k)
    		{
    			ans=i;
    		}
    	}
    	cout<<ans;
    	return 0;
    } 
    
    
    • 0
      @ 2026-1-4 9:43:34

      100pts

      #include<bits/stdc++.h>
      using namespace std;
      const int N=510;
      int f[N*N],v[N],w[N];
      int main()
      {
      	int n,V=N*N-10;
      	int k;
      	cin>>n>>k;
      	for(int i=1;i<=n;i++)
      	{
      		cin>>v[i]>>w[i];
      	}
      	memset(f,0x3f,sizeof f);
      	f[0]=0;
      	for(int i=1;i<=n;i++)
      	{
      		for(int j=V;j>=v[i];j--)
      		{
      			f[j]=min(f[j],f[j-v[i]]+w[i]);
      		}
      	}
      	int ans=0;
      	for(int i=1;i<=V;i++)
      	{
      		if(f[i]<=k)
      		{
      			ans=i;
      		}
      	}
      	cout<<ans;
      	return 0;
      } 
      
      • 0
        @ 2026-1-4 9:31:57

        60pts

        #include<bits/stdc++.h>
        using namespace std;
        const int N=510;
        int f[N],v[N],w[N];
        int main()
        {
        	int n,V;
        	cin>>n>>V;
        	for(int i=1;i<=n;i++)
        	{
        		cin>>w[i]>>v[i];
        	}
        	for(int i=1;i<=n;i++)
        	{
        		for(int j=V;j>=v[i];j--)
        		{
        			f[j]=max(f[j],f[j-v[i]]+w[i]);
        		}
        	}
        	cout<<f[V];
        	return 0;
        } 
        
        • 1

        Information

        ID
        3026
        Time
        1000ms
        Memory
        256MiB
        Difficulty
        10
        Tags
        # Submissions
        2
        Accepted
        1
        Uploaded By