Skip to content

Commit

Permalink
Add solution
Browse files Browse the repository at this point in the history
  • Loading branch information
RikuPutkinen committed Sep 19, 2023
1 parent 19c8fd3 commit 5d536bc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dynamic-programming/book-shop/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Book Shop

Problem page: <https://cses.fi/problemset/task/1158/>
28 changes: 28 additions & 0 deletions dynamic-programming/book-shop/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main(){
int n{}, x{};
cin >> n >> x;
int h[n], s[n];

for (auto &x : h) cin >> x;
for (auto &x : s) cin >> x;

vector<vector<int>> memo(n+1, vector<int>(x+1, 0));

for (int i = 1; i <= n; i++){
for (int j = 1; j <= x; j++){
memo[i][j] = memo[i-1][j];
if(j - h[i-1] >= 0){
memo[i][j] = max(memo[i][j], memo[i-1][j-h[i-1]] + s[i-1]);
}
}
}
cout << memo[n][x] << "\n";

return 0;
}

0 comments on commit 5d536bc

Please sign in to comment.