結果

問題 No.3401 Large Knapsack Problem
コンテスト
ユーザー GOTKAKO
提出日時 2025-12-08 00:04:23
言語 C++17
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 600 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,677 ms
コンパイル使用メモリ 195,864 KB
実行使用メモリ 11,148 KB
最終ジャッジ日時 2025-12-08 00:04:28
合計ジャッジ時間 4,218 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    vector<long long> dp(1001001);
    int N,W; cin >> N >> W;
    int bv = 0,bw = 1;
    while(N--){
        int v,w; cin >> v >> w;
        if(bv*w < v*bw) bv = v,bw = w;
        for(int i=w; i<1001001; i++) dp.at(i) = max(dp.at(i),dp.at(i-w)+v);
    }
    if(W < 1001001) cout << dp.at(W) << endl;
    else{
        long long over = (W-1001001)/bw+1;
        long long answer = over*bv;
        W -= over*bw;
        answer += dp.at(W);
        cout << answer << endl;
    }
}
0