Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- using namespace std;
- typedef long long ll;
- const int maxn = 1e5 + 10;
- const int INF = 2e9 + 10;
- const ll inf = 1e18 + 10;
- ll dp[101][100001];
- int main() {
- int N, W;
- cin >> N;
- cin >> W;
- vector<ll> v(101);
- vector<ll> w(101);
- for (int i = 0; i < N; i++) {
- cin >> w[i];
- cin >> v[i];
- }
- for(int i = 0; i < N; ++i) {
- for(int j = 0; j <= W; ++j) {
- dp[i][j] = -inf;
- }
- }
- // dp[i][j] - maximum value till i with remaining j weight
- ll ret = -inf;
- for(int at = 0; at <= N; ++at) {
- for(int remaining_weight = 0; remaining_weight <= W; ++remaining_weight) {
- if(at == 0) {
- dp[at][remaining_weight] = 0;
- continue;
- }
- if(remaining_weight == 0) {
- dp[at][remaining_weight] = 0;
- continue;
- }
- if(w[at - 1] <= remaining_weight) {
- dp[at][remaining_weight] = max(dp[at][remaining_weight], dp[at - 1][remaining_weight - w[at - 1]] + v[at - 1]);
- }
- dp[at][remaining_weight] = max(dp[at][remaining_weight], dp[at - 1][remaining_weight]);
- ret = max(ret, dp[at][remaining_weight]);
- }
- }
- cout << ret << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment