比赛 |
2025.5.4 |
评测结果 |
AAATAWWTWT |
题目名称 |
送礼物 |
最终得分 |
40 |
用户昵称 |
LikableP |
运行时间 |
18.593 s |
代码语言 |
C++ |
内存使用 |
1.49 MiB |
提交时间 |
2025-05-04 09:14:53 |
显示代码纯文本
#include <cstdio>
#include <algorithm>
#define max(x, y) x > y ? x : y
typedef long long ll;
//inline ll max(ll x, ll y) {
// return x > y ? x : y;
//}
ll w, n;
int a[50];
ll sum;
ll ans;
int f[67108865];
void dfs(int pos, ll now) {
if (pos == n + 1) {
ans = max(ans, now);
return ;
}
if (now + (ll)a[pos] <= w) dfs(pos + 1, now + a[pos]);
dfs(pos + 1, now);
}
void Work1() {
dfs(1, 0);
printf("%lld", ans);
}
void Work2() {
for (int i = 1; i <= n; ++i) {
for (int j = w; j >= 0; --j) {
if (j - a[i] >= 0) {
f[j] = max(f[j], f[j - a[i]] + a[i]);
}
}
}
printf("%d", f[w]);
}
void Work3() {
::std::sort(a + 1, a + n + 1, [](int x, int y){return x > y;});
for (int i = 1; i <= n; ++i) {
sum += a[i];
if (sum > w) {
sum -= a[i];
}
}
printf("%lld", sum);
}
int main() {
freopen("giftgiving.in", "r", stdin);
freopen("giftgiving.out", "w", stdout);
scanf("%lld %lld", &w, &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
if (n <= 42) {
Work1();
} else if (w <= 67108864){
Work2();
} else {
Work3();
}
return 0;
}