結果
| 問題 | No.3401 Large Knapsack Problem |
| コンテスト | |
| ユーザー |
applejam
|
| 提出日時 | 2025-12-08 13:56:46 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 140 ms / 2,000 ms |
| コード長 | 1,647 bytes |
| 記録 | |
| コンパイル時間 | 5,645 ms |
| コンパイル使用メモリ | 335,500 KB |
| 実行使用メモリ | 167,524 KB |
| 最終ジャッジ日時 | 2025-12-08 13:56:57 |
| 合計ジャッジ時間 | 10,853 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 42 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using mint = modint998244353;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vmi = vector<mint>;
using vvmi = vector<vmi>;
using vvvmi = vector<vvmi>;
#define all(a) (a).begin(), (a).end()
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
void solve(){
}
using p = pair<ll, ll>;
int main(){
ll n, w; cin >> n >> w;
vector<p> vp(n);
rep(i, n){
ll a, b; cin >> a >> b;
vp[i] = {a, b};
}
sort(all(vp), [](const p a, const p b){
if(a.first*b.second != a.second*b.first){
return a.first*b.second > a.second*b.first;
}else{
return a.second < b.second;
}
});
ll nw = w;
ll ans = 0;
if(w > 1000000){
auto [a, b] = vp[0];
ll c = (nw-1000000)/b;
ans += a*c;
nw -= b*c;
}
vvll dp(n, vll(nw+1, -1));
for(int j = 0; j*vp[0].second <= nw; j++)dp[0][j*vp[0].second] = vp[0].first * j;
rep2(i, 1, n){
auto [a, b] = vp[i];
rep(j, nw+1){
if(dp[i-1][j] != -1)dp[i][j] = dp[i-1][j];
if(j - b >= 0){
if(dp[i][j-b] != -1)dp[i][j] = max(dp[i][j], dp[i][j-b] + a);
}
}
}
ll tmp = 0;
rep(i, nw+1)tmp = max(tmp, dp[n-1][i]);
cout << ans+tmp << endl;
return 0;
}
applejam