Skip to content

Instantly share code, notes, and snippets.

@pinglunliao
Created November 10, 2019 03:43
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 pinglunliao/6f60f6624f094113f91bf61433c57072 to your computer and use it in GitHub Desktop.
Save pinglunliao/6f60f6624f094113f91bf61433c57072 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int n, m;
cin >> n >> m;
int v[n], w[m];
for(int i = 0; i < n; i++)
{
cin >> w[i] >> v[i];
}
int joy[n+1][m+1];
memset(joy, 0, sizeof(joy));
for (int i = 0; i < n; ++i) // 窮舉每部影片
{
for (int j = 0; j <= m; ++j)// 窮舉每種長度
{
if (j - w[i] < 0)
// 可觀長度不夠,故只能不看。
joy[i+1][j] = joy[i][j];
else
// 可觀長度足夠,可以看或不看。
joy[i+1][j] =
max(
joy[i][j],
joy[i][j - w[i]] + v[i]
);
}
}
cout << joy[n][m] << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment