Luogu3092:[USACO13NOV]No Change

时间:2024-12-07 20:06:44

题面

传送门

Sol

状压一下\(k\),\(f[S]\)表示用过的硬币集合为\(S\)能买到的物品个数

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(100005); IL ll Input(){
RG ll x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
} int k, coin[_], n, cost[_], sum[_], f[65536], ans = -1, mi[20]; int main(RG int argc, RG char* argv[]){
k = Input(); n = Input();
for(RG int i = 1; i <= k; ++i) coin[i] = Input();
for(RG int i = 1; i <= n; ++i){
cost[i] = Input();
sum[i] = sum[i - 1] + cost[i];
}
mi[1] = 1;
for(RG int i = 2; i <= k; ++i) mi[i] = mi[i - 1] << 1;
RG int S = 1 << k;
for(RG int i = 0; i < S; ++i)
for(RG int j = 1; j <= k; ++j){
if(i & mi[j]) continue;
RG int pos = lower_bound(sum + 1, sum + n + 1, coin[j] + sum[f[i]]) - sum;
if(pos > n || sum[pos] > coin[j] + sum[f[i]]) --pos;
f[i | mi[j]] = max(f[i | mi[j]], pos);
}
for(RG int i = 0; i < S; ++i)
if(f[i] == n){
RG int cnt = 0;
for(RG int j = 1; j <= k; ++j)
if(~i & mi[j]) cnt += coin[j];
ans = max(ans, cnt);
}
printf("%d\n", ans);
return 0;
}