結果

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

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

using namespace std;

/*
ライブラリのincludeはここ。
#include
"/home/lolotte/projects/C++_Library/library/xxx.cpp"の様にフルパスで書く。
sourceでvenvに入ってoj-bundle a.cpp -I C++_Library/xxx >
bundled.cppで展開される。
*/

// clang-format off
// --- 型エイリアス (入力の手間を減らす) ---
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vs = vector<string>;
using P = pair<ll, ll>;

// --- 便利マクロ・定数 ---
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define el '\n'
#define BIT(i, j) (((i) >> (j)) & 1) //bit全探索

// 昇順(小さい順に取り出す):ダイクストラ法など
using min_pq = priority_queue<ll, vector<ll>, greater<ll>>;
// 降順(大きい順に取り出す):デフォルト
using max_pq = priority_queue<ll>;

const ll INF = 1LL << 61;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

inline void Yes(bool b = true) {cout << (b ? "Yes" : "No") << "\n";}
inline void No() { cout << "No" << "\n"; }
template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }
template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }

// clang-format on

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N, W;
    cin >> N >> W;

    vector<ll> w(N), v(N);
    int best_item_idx = 0;
    double max_efficiency = -1.0;

    for (int i = 0; i < N; i++) {
        cin >> v[i] >> w[i];

        double eff = (double)v[i] / w[i];
        if (eff > max_efficiency) {
            max_efficiency = eff;
            best_item_idx = i;
        }
    }

    ll w_best = w[best_item_idx];
    ll v_best = v[best_item_idx];

    ll limit = 2000000;

    vector<ll> dp(limit + 1, -1);
    dp[0] = 0;

    for (int j = 0; j <= limit; j++) {
        if (dp[j] == -1) continue;

        for (int i = 0; i < N; i++) {
            if (j + w[i] <= limit) {
                dp[j + w[i]] = max(dp[j + w[i]], dp[j] + v[i]);
            }
        }
    }

    ll ans = 0;

    for (ll x = 0; x <= limit; x++) {
        if (dp[x] == -1) continue;
        if (x > W) break;

        ll remaining_capacity = W - x;
        ll num_best = remaining_capacity / w_best;
        ll current_value = dp[x] + num_best * v_best;

        ans = max(ans, current_value);
    }

    cout << ans << endl;
}
0