Output
For each case, you should output one integer denoting the maximum comfort.
Sample Input
2
3
10 1
5 1
2 10
2
1 10
2 10
Sample Output
191
0
Hint
For the first case, Edward should select the first and second courses.
For the second case, Edward should select no courses.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
struct node
{
int h,c;
}p[501];
//用SH表示对h求和,用SC表示对c求和,dp[j]数组 j表示SC dp 的值表示对应SC下的SH
//转化为01背包问题
//c取不同的值SH不同
int main()
{
int i,j,T,n;
long long int sum,dp[50005];
scanf("%d",&T);
while(T--)
{
memset(dp,0,sizeof(dp));
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d%d",&p[i].h,&p[i].c);
}
for(i=1;i<=n;i++)
{
for(j=50000;j;j--)
{
if(p[i].c>j) break;
dp[j]=max(dp[j],dp[j-p[i].c]+p[i].h);//算包不包含第i个c时的SH的最大值
}
}
sum=0;
for(i=1;i<=50000;i++) //i表示SC
{
sum=max(sum,dp[i]*dp[i]-i*dp[i]-(long long )i*i); //根据公式
}
printf("%lld\n",sum);
}
return 0;
}