結果
| 問題 | No.3401 Large Knapsack Problem |
| コンテスト | |
| ユーザー |
Akidai
|
| 提出日時 | 2025-12-08 00:28:00 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 193 ms / 2,000 ms |
| コード長 | 2,230 bytes |
| 記録 | |
| コンパイル時間 | 4,293 ms |
| コンパイル使用メモリ | 257,964 KB |
| 実行使用メモリ | 175,244 KB |
| 最終ジャッジ日時 | 2025-12-08 00:28:13 |
| 合計ジャッジ時間 | 12,194 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 42 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = (1LL << 62) - (1LL << 31) - 1;
#define REP(i, init, n) for(int i = (int)(init); i < (int)(n); i++)
#define RREP(i, init, n) for(int i = (int)(init); i >= (int)(n); i--)
#define All(A) A.begin(), A.end()
#define rAll(A) A.rbegin(), A.rend()
#define vi vector<int>
#define vl vector<long>
#define vvi vector<vector<int>>
#define vvl vector<vector<long>>
#define pint pair<int, int>
#define plong pair<long, long>
int N, W;
vector<plong> P;
void solve() {
long ans = 0;
long tmp_max_val = 0, tmp_idx = -1, max_count = 0;
REP(i, 0, N) {
long count = (long)(W - 1000000) / P[i].second;
long tmp_val = count * P[i].first;
if(tmp_max_val < tmp_val) {
tmp_max_val = tmp_val;
tmp_idx = i;
max_count = count;
}
}
// cerr << tmp_max_val << " " << tmp_idx << " " << max_count << endl;
if(tmp_idx != -1) {
ans += P[tmp_idx].first * max_count;
W -= P[tmp_idx].second * max_count;
}
vvl dp(N + 1, vl(W + 1, -1));
dp[0][W] = 0;
REP(i, 0, N) {
long pv = P[i].first;
long pw = P[i].second;
RREP(w, W, 0) {
if(dp[i][w] != -1) {
dp[i + 1][w] = max(dp[i + 1][w], dp[i][w]);
}
if(w + pw <= W && dp[i][w + pw] != -1) {
dp[i + 1][w] = max(dp[i + 1][w], dp[i][w + pw] + pv);
}
if(w + pw <= W && dp[i + 1][w + pw] != -1) {
dp[i + 1][w] = max(dp[i + 1][w], dp[i + 1][w + pw] + pv);
}
}
}
/*
REP(i, 0, N + 1) {
REP(j, 0, W + 1) {
cerr << dp[i][j] << " ";
}
cerr << endl;
}
*/
long max_table = 0;
REP(i, 0, N + 1) {
REP(j, 0, W + 1) {
max_table = max(max_table, dp[i][j]);
}
}
ans += max_table;
cout << ans << endl;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> N >> W;
P.resize(N);
REP(i, 0, N) {
cin >> P[i].first >> P[i].second;
}
solve();
}
Akidai