BOJ 12865 평범한 배낭

1 개요[ | ]

BOJ 12865 평범한 배낭


2 C++[ | ]

#include <iostream>
using namespace std;

int N, K, temp;
int W[101], V[101];
int A[102][100001];

void solve() {
	for(int i=1; i<=N+1; i++) {
		for(int j=1; j<=K; j++) {
			temp = A[i-1][j];
			if(j>=W[i-1] && A[i-1][j-W[i-1]]+V[i-1] > temp) { 
				temp = A[i-1][j-W[i-1]]+V[i-1];
			}
			A[i][j] = temp;
		}
	}
	cout << A[N+1][K];
}

int main() {
    cin >> N >> K;
    for(int i=1; i<=N; i++) {
        cin >> W[i] >> V[i];
    }
    solve();
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}