这道题居然是dp
虽然最后听到别人说是dp,,但是自己还是不会,,,而且最后跟着敲得时候居然要longlong。
这道题属于01背包,, dp[i]表示c为i时最多的h,
然后根据公式:
因为当c固定时,这个式子只需要看前面 h*h-h*c=h*(h-c)
h大于c时,肯定h越大越好
h小于c时,表达式的值必定小于零,肯定是不考虑的
这个地方的通过使 c固定的方式来处理这个公式方式应该是这种题的套路吧。。做题太少了。。。不知道。。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int INF=10000000;
const int maxn=50050;
ll h[maxn],c[maxn];
ll dp[maxn];
ll cal(ll c,ll h){
return h*h-h*c-c*c;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
memset(c,0,sizeof(c));
memset(h,0,sizeof(h));
scanf("%d",&n);
int tot=0;
for(int i=1;i<=n;++i)
scanf("%d%d",&h[i],&c[i]),tot+=c[i];
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;++i){
for(int j=tot;j>=c[i];--j){
dp[j]=max(dp[j],dp[j-c[i]]+h[i]);
}
}
ll ans=0;
for(int i=1;i<=tot;++i){
ans=max(ans,cal(i,dp[i]));
}
cout<<ans<<'\n';
}
}