結果
| 問題 | No.3670 Fast Knapsack |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-09-05 00:12:20 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,196 bytes |
| 記録 | |
| コンパイル時間 | 2,335 ms |
| コンパイル使用メモリ | 351,560 KB |
| 実行使用メモリ | 9,768 KB |
| 最終ジャッジ日時 | 2026-09-05 00:12:40 |
| 合計ジャッジ時間 | 11,371 ms |
|
ジャッジサーバーID (参考情報) |
judge2_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 19 TLE * 2 -- * 4 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
// Sに依存する解法なので怪しいか...?
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while(T--) {
int N, S;
cin >> N >> S;
vector<int> a(N);
ll tot = 0, g = 0;
for(auto &&v : a){
cin >> v;
tot += v;
g = gcd(g, v);
}
if(tot <= S){
cout << tot << '\n';
continue;
}
if(g >= 2){
S /= g;
for(auto &&v : a) v /= g;
}
vector<int> small, large = {0};
for(auto v : a) (v > S / 2 ? large : small).push_back(v);
int W = S / 64 + 1;
vector<ull> dp(W, 0);
dp[0] = 1;
int Mx = 0;
auto shiftOR = [&](int shift) {
int WShift = shift / 64, BShift = shift & 63;
int nxtMx = min(S, Mx + shift);
for(int i = nxtMx / 64; i >= WShift; --i) {
ull moved = dp[i - WShift] << BShift;
if(BShift && i > WShift) {
moved |= dp[i - WShift - 1] >> (64 - BShift);
}
dp[i] |= moved;
}
Mx = nxtMx;
};
sort(small.begin(), small.end());
sort(large.rbegin(), large.rend());
for(int i = 0; i < small.size();) {
int p = i;
while(i < small.size() && small[i] == small[p]) i++;
int cnt = i - p;
for(int take = 1; cnt > 0; take *= 2) {
int D = min(take, cnt);
long long shift = (ll)(small[p]) * D;
cnt -= D;
if(shift <= S) shiftOR((int)shift);
}
}
int ans = 0;
for(int i = 0, li = 0, cur = 0; i < W; i++) {
for(int j = 0; j < 64; j++, cur++){
while(li < large.size() && cur + large[li] > S) li++;
if(li < large.size() && (dp[i] >> j & 1)) {
ans = max(ans, cur + large[li]);
}
}
}
cout << g * ans << '\n';
}
}