Skip to content

Instantly share code, notes, and snippets.

@maehrm
Created February 27, 2018 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maehrm/b9df3d74ba3cc432e78eba5672970048 to your computer and use it in GitHub Desktop.
Save maehrm/b9df3d74ba3cc432e78eba5672970048 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int dp[101][10001];
int value[101], weight[101];
int n, w;
cin >> n >> w;
for (int i = 0; i < n; i++)
cin >> value[i] >> weight[i];
for (int i = 0; i <= w; i++)
dp[0][i] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= w; j++) {
if (j >= weight[i]) {
// int t = j/weight[i]; // ついついこんな風に考えてしまいます…。
// dp[i+1][j] = max(dp[i][j-t*weight[i]]+t*value[i], dp[i][j]);
dp[i+1][j] = max(max(dp[i][j-weight[i]]+value[i], dp[i+1][j-weight[i]]+value[i]),
dp[i][j]);
}
else {
dp[i+1][j] = dp[i][j];
}
}
}
cout << dp[n][w] << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment