题目链接: 点击打开链接
#include <cstdio>
#include <memory.h>
#include <algorithm>
using namespace std;
const int maxc = 100000 + 10;
const int maxn = 20;
int V, n, cost[maxn], amount[maxn], dp[maxc];
void ZeroOnePack(int cost, int value)
{
for (int i = V; i >= cost; --i)
dp[i] = max(dp[i], dp[i - cost] + value);
}
void ComPack(int cost, int value)
{
for (int i = cost; i <= V; ++i)
dp[i] = max(dp[i], dp[i - cost] + value);
}
void MulPack(int cost, int value, int amount)
{
if (cost * amount > V) {
ComPack(cost, value);
return;
}
for (int i = 1; i <= amount; i <<= 1) {
ZeroOnePack(cost * i, value * i);
amount -= i;
}
ZeroOnePack(cost * amount, value * amount);
}
void solve()
{
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; ++i)
MulPack(cost[i], cost[i], amount[i]);
}
int main()
{
while (scanf("%d", &V) == 1)
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d %d", &amount[i], &cost[i]);
solve();
printf("%d\n", dp[V]);
}
return 0;
}