✅ What I did today
- 백준
- Prototyping :: P008 : Day 1
⚔️ 백준
평범한 배낭
2단 배열 풀이
#include<vector>
#include<iostream>
using namespace std;
#define fastIO cin.tie(0)->sync_with_stdio(0)
int main()
{
fastIO;
int N, W, V, K;
cin >> N >> K;
vector<vector<int>> dp(N+1, vector<int>(K+1, 0));
for(int i=1; i<N+1; i++)
{
cin >> W >> V;
for (int j = 1; j <= K; j++) {
int temp = 0;
if (j - W >= 0) temp = dp[i-1][j - W] + V;
dp[i][j] = max(dp[i-1][j], temp);
}
}
cout << dp[N][K];
}
1단 배열 풀이
int N, W, V, K;
cin >> N >> K;
vector<int> dp(K+1);
for(int i=1; i<N+1; i++)
{
cin >> W >> V;
for (int j = K; 1 <= j; j--) {
int temp = 0;
if (j - W >= 0) temp = dp[j - W] + V;
dp[j] = max(dp[j], temp);
}
}
cout << dp[K];
💡 Prototyping : P008