結果

問題 No.527 ナップサック容量問題
ユーザー HIcoder
提出日時 2023-07-23 11:47:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 1,314 bytes
コンパイル時間 933 ms
コンパイル使用メモリ 111,456 KB
最終ジャッジ日時 2025-02-15 18:26:02
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<set> 
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<queue>
#include<cmath>
#include <unordered_set>
#include<cassert>
using namespace std;
typedef long long ll;
const ll INF=1LL<<60;
typedef pair<int,int> P;
typedef pair<int,P> PP;
const ll MOD=1e9+7;

int main(){
    int N;
    cin>>N;
    vector<P> vw(N);

    ll sumv=0;
    for(int i=0;i<N;i++){
        cin>>vw[i].first>>vw[i].second;
        sumv+=vw[i].first;
    }
    ll V;
    cin>>V;

    const int MAXW=1e5 + 10;
    /*
    dp[i][j]=i番目までの品を用いて 容量jで作れる価値のmax
    */
    //0にすることがポイント
    vector<vector<ll>> dp(N+1,vector<ll>(MAXW+1,0));
    
    for(int i=0;i<N;i++){
        auto [v,w]=vw[i];
        for(int j=0;j<=MAXW;j++){
            
            dp[i+1][j]=max(dp[i][j],dp[i+1][j]);

            if(j+w<=MAXW){
                dp[i+1][j+w]=max(dp[i+1][j+w],dp[i][j]+v);
            }
        }
    }   

    ll minw=INF;
    ll maxw=0;

    for(ll j=1;j<=MAXW;j++){
        if(dp[N][j]==V){
            minw=min(j,minw);
            maxw=max(j,maxw);
        }
    }

    cout<<minw<<endl;


    if(maxw==MAXW){
        cout<<"inf"<<endl;
    }else{
        cout<<maxw<<endl;
    }


    



}
0