結果
| 問題 | No.3401 Large Knapsack Problem |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-12-08 01:16:20 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,489 bytes |
| 記録 | |
| コンパイル時間 | 3,395 ms |
| コンパイル使用メモリ | 278,784 KB |
| 実行使用メモリ | 19,012 KB |
| 最終ジャッジ日時 | 2025-12-08 01:16:30 |
| 合計ジャッジ時間 | 8,164 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | WA * 3 |
| other | WA * 42 |
ソースコード
#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 >> w[i] >> v[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;
}