QOJ.ac
QOJ
ID | 提交记录ID | 题目 | Hacker | Owner | 结果 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|
#548 | #206576 | #7403. Subset Sum | 5sb | RedDiamond | Success! | 2024-02-29 18:13:26 | 2024-02-29 18:13:26 |
详细
Extra Test:
Time Limit Exceeded
input:
1 20000 1000001 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20...
output:
result:
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#206576 | #7403. Subset Sum | RedDiamond | TL | 3ms | 4172kb | C++23 | 906b | 2023-10-07 21:23:30 | 2024-02-29 18:13:46 |
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int) 2e4 + 7;
int n, a[N];
ll suf[N], c, sol = 0;
void solve(int pos, ll sum = 0) {
if (sum > c || sol == c) return;
sol = max(sol, sum);
if (pos == n + 1) return;
if (sum + suf[pos] <= sol) return;
solve(pos + 1, sum + a[pos]);
if (sum + suf[pos + 1] > sol) solve(pos + 1, sum);
}
signed main() {
#ifdef ONPC
freopen("input.txt", "r", stdin);
#else
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif // ONPC
int tt;
cin >> tt;
while (tt--) {
cin >> n >> c;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a + 1, a + n + 1);
reverse(a + 1, a + n + 1);
suf[n + 1] = 0;
for (int i = n; i >= 1; i--) {
suf[i] = suf[i + 1] + a[i];
}
sol = 0;
solve(1);
cout << sol << "\n";
}
return 0;
}