採藥 OpenJ_Bailian - 2726(0-1揹包問題)
阿新 • • 發佈:2019-01-02
#include <stdio.h>
#include <string.h>typedef struct Thing
{
int time, value;
}Ting;Ting thing[11];
int max(int a, int b)
{
return a > b ? a:b;
}int dp[1001];
int main()
{
int t, m;
scanf("%d %d", &t, &m);
for(int i = 1; i <= m; i++)
scanf("%d %d", &thing[i].time, &thing[i].value);
memset(dp, 0, 1001); //標頭檔案為string.h或者memory.h
for(int i = 1; i <= m; i++)
for(int j = t; j >= thing[i].time; j--)
dp[j] = max(dp[j-thing[i].time] + thing[i].value, dp[j]);
printf("%d\n", dp[t]);
return 0;
}