Skip to content

Commit

Permalink
12920. 평범한 배낭 2 - 변형 냅색
Browse files Browse the repository at this point in the history
  • Loading branch information
mwy3055 committed Jul 30, 2023
1 parent e9ddec1 commit a0def07
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions baekjoon/cpp/c++/12920.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <bits/stdc++.h>

struct thing
{
int weight, satis;
thing(int w, int s) : weight(w), satis(s) {}
};

int n, m;
std::vector<thing> things;

int dp[1501][10001];

void getinput()
{
std::cin >> n >> m;
for (int i = 0; i < n; i++)
{
int v, c, k;
std::cin >> v >> c >> k;

int bitmask = 1;
while (k > 0)
{
int count = std::min(k, bitmask);
things.emplace_back(v * count, c * count);
k -= count;
bitmask <<= 1;
}
}
}

int solve()
{
dp[0][things[0].weight] = things[0].satis;
for (int i = 1; i < things.size(); i++)
{
for (int j = 0; j <= m; j++)
{
// i번째 물건을 넣거나 넣지 않아서 무게가 j가 된 경우
if (j >= things[i].weight)
dp[i][j] = std::max(dp[i - 1][j], dp[i - 1][j - things[i].weight] + things[i].satis);
else
dp[i][j] = dp[i - 1][j];
}
}

int ans = 0;
for (int j = 0; j <= m; j++)
{
ans = std::max(ans, dp[things.size() - 1][j]);
}
return ans;
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);

getinput();
std::cout << solve() << '\n';
}

0 comments on commit a0def07

Please sign in to comment.