2 条题解

  • 0
    @ 2025-10-8 16:55:48
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn = 1e5 + 5;
    ll n, m, f[maxn], w[maxn], v[maxn]; 
    //本题有个值得注意的地方就是,本题可能会炸int,所以得开ll
    int main(){
        std::ios::sync_with_stdio(false);
        cin >> n >> m;
        for(int i = 1; i <= n; ++i) cin >> w[i] >> v[i];
        for(int i = 1; i <= n; ++i){
            for(int j = m; j >= w[i]; --j){
                f[j] = max(f[j - w[i]] + v[i], f[j]);
            }
        }
        cout << f[m];
        return 0;
    }
    
    • 0
      @ 2025-10-8 16:55:42
      #include<bits/stdc++.h>
      using namespace std;
      typedef long long ll;
      const int maxn=1e5+5;
      ll n,m,f[maxn],w[maxn],v[maxn]; 
      //本题有个值得注意的地方就是,本题可能会炸int,所以得开ll
      int main(){
      	std::ios::sync_with_stdio(False);
      	cin>>n>>m;
      	for(int i=1;i<=n;++i)cin>>w[i]>>v[i];
      	for(int i=1;i<=n;++i){
      		for(int j=m;j>=w[i];--j){
      			f[j]=max(f[j-w[i]]+v[i],f[j]);
      		}
      	}
      	cout<<f[m];
      	return 0;
      }
      • 1

      信息

      ID
      1228
      时间
      2000ms
      内存
      1024MiB
      难度
      7
      标签
      递交数
      125
      已通过
      26
      上传者