2 solutions

  • 1
    @ 2025-3-8 19:55:53
    #include <bits/stdc++.h>
    using namespace std;
    const int N=1010;
    int f[N][N];
    int w[N],g[N]; 
    int main(){
    int n,m;
    while(cin>>m>>n)
    {
    	for(int i=1;i<=n;i++)
    	{
    		cin>>w[i]>>g[i];
    	}
    	for(int i=1;i<=n;i++)
    	{
    		for(int j=1;j<=m;j++)
    		{
    			f[i][j]=f[i-1][j];
    			if(j>=w[i])f[i][j]=max(f[i-1][j],g[i]+f[i-1][j-w[i]]);
    		}
    	}
    	cout<<f[n][m];
    }
    	return 0;
    }
    
    
    
    • -1
      @ 2024-7-16 11:24:56
      #include<bits/stdc++.h>
      using namespace std;
      const int N=101,M=101;
      int f[N][M],v[N],w[N];
      int main()
      {
      	int n,V;
      	cin>>n>>V;
      	for(int i=1;i<=V;i++)
      	{
      		cin>>v[i]>>w[i];
      	}
      	for(int i=1;i<=V;i++)
      	{
      		for(int j=1;j<=n;j++)
      		{
      			f[i][j]=f[i-1][j-1];
      			if(j>v[i+1])
      			{
      				f[i][j]=max(f[i][j],f[i-1][j-1]+w[i]);
      			}
      		}
      	}
      	cout<<f[n][V];
      	return 0;
      }
      
      • 1

      Information

      ID
      986
      Time
      1000ms
      Memory
      256MiB
      Difficulty
      5
      Tags
      (None)
      # Submissions
      49
      Accepted
      15
      Uploaded By