比赛 SBOI2022暑假快乐赛① 评测结果 AAAAAAAAE
题目名称 送礼物 最终得分 88
用户昵称 yrtiop 运行时间 2.028 s
代码语言 C++ 内存使用 29.35 MiB
提交时间 2022-06-25 11:03:44
显示代码纯文本
#include <bits/stdc++.h>
#define int unsigned int
using namespace std;
const int maxn = 50;
const int maxm = 1 << 23;
int a[maxn],w,n,ans;
int s[maxm],cnt;
void dfs(int x,int t,int cur) {
    s[++ cnt] = cur;
    if(x > t)return ;
    if(w - cur >= a[x])dfs(x + 1 , t , cur + a[x]);
    dfs(x + 1 , t , cur);
    return ;
}
int Find(int x) {
    int l = 1,r = cnt,mid;
    while(l <= r) {
        mid = l + r >> 1;
        if(s[mid] <= x)l = mid + 1;
        else r = mid - 1;
    }
    return s[r];
}
void DFS(int x,int t,int cur) {
    ans = max(ans , cur + Find(w - cur));
    if(x > t)return ;
    if(w - cur >= a[x])DFS(x + 1 , t , cur + a[x]);
    DFS(x + 1 , t , cur);
    return ;
}
signed main() {
    freopen("giftgiving.in","r",stdin);
    freopen("giftgiving.out","w",stdout);
    scanf("%u%d",&w,&n);
    for(int i = 1;i <= n;++ i)scanf("%u",&a[i]);
    dfs(1 , n / 2 , 0);
    sort(s + 1 , s + 1 + cnt);
    cnt = unique(s + 1 , s + 1 + cnt) - s - 1;
    ans = s[cnt];
    DFS(n / 2 + 1 , n , 0);
    printf("%u\n",ans);
    fclose(stdin);
    fclose(stdout);
    return 0;
}