QOJ.ac
QOJ
ID | Submission ID | Problem | Hacker | Owner | Result | Submit time | Judge time |
---|---|---|---|---|---|---|---|
#548 | #206576 | #7403. Subset Sum | 5sb | RedDiamond | Success! | 2024-02-29 18:13:26 | 2024-02-29 18:13:26 |
Details
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 | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#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;
}