結果

問題 No.3401 Large Knapsack Problem
コンテスト
ユーザー umimel
提出日時 2025-12-08 00:44:15
言語 C++14
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 1,426 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,769 ms
コンパイル使用メモリ 171,116 KB
実行使用メモリ 35,160 KB
最終ジャッジ日時 2025-12-08 00:44:20
合計ジャッジ時間 5,361 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const ll MOD1000000007 = 1000000007;
const ll MOD998244353 = 998244353;
const ll MOD[3] = {999727999, 1070777777, 1000000007};
const ll LINF = 1LL << 60LL;
const int IINF = (1 << 30) - 1;


void solve(){
    int N, W; cin >> N >> W;
    vector<pair<int, int>> item(N);
    for(int i=0; i<N; i++) cin >> item[i].first >> item[i].second;
    sort(all(item), [](const pair<int, int>& a, const pair<int, int>& b) {
        return (ll)a.first*(ll)b.second<(ll)b.first*(ll)a.second;
    });

    int sum = 0;
    for(int i=0; i<N-1; i++) sum += item[i].second*(item[N-1].second-1);
    vector<int> dp(sum+1, -1);
    dp[0] = 0;
    for(int i=0; i<N-1; i++){
        for(int j=item[i].second; j<=sum; j++){
            if(dp[j-item[i].second]!=-1){
                dp[j] = max(dp[j], dp[j-item[i].second]+item[i].first);
            }
        }
    }
    ll ans = 0LL;
    for(int i=0; i<=min(sum, W); i++) if(dp[i]!=-1){
        ans = max(ans, (ll)dp[i]+(ll)item[N-1].first*((ll)(W-i)/(ll)(item[N-1].second)));
    }

    cout << ans << "\n";
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    int T=1;
    //cin >> T;
    while(T--) solve();
}
0