Last active
July 26, 2016 04:47
-
-
Save nathanPro/fbe3ecdee36e73805a05c7ac8bf25589 to your computer and use it in GitHub Desktop.
Knapsack se aproveitando de C++11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
typedef int64_t ll; | |
const int N = (2e3) + 7; | |
int n, k, w[N], v[N]; | |
int _S[N][N]; | |
int& S(int i, int w) { return (w >= 0) ? _S[i][w] : _S[N-1][N-1]; } | |
int s(int i, int w) { return max({S(i+1,w), v[i] + S(i+1,w-::w[i])}); } | |
int main(){ | |
scanf(" %d%d", &k, &n); | |
S(N-1,N-1) = INT_MIN; | |
for(int i=0;i<n;i++) scanf(" %d%d", w+i, v+i); | |
for(int i=n-1;i+1;i--) | |
for(int w=0;w<=k;w++) | |
S(i,w) = s(i,w); | |
printf("%d\n", S(0,k)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment