01背包
如果你能发现C不变,H越大,公式值越大,这题就可以用01背包写了
C代表容量~dp[i]代表到使用前i种所能达到最大的H值
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <climits>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <sstream>
#define INF 0x3f3f3f3f
#define LL long long
#define fora(i,a,n) for(int i=a;i<=n;i++)
#define fors(i,n,a) for(int i=n;i>=a;i--)
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define MAXN 100024
const double eps = 1e-8;
using namespace std;
LL dp[50024];
LL h[524], c[524];
int t, n;
int main() {
#ifdef DID
freopen("in.txt", "r", stdin);
//freopen("out.txt","w",stdout);
#endif
sci(t);
while (t--) {
sci(n);
LL Min=0;
fora(i, 1, n) {
sci(h[i]), sci(c[i]);
Min += c[i];
}
fora(i, 0, Min) dp[i] = -INF;
dp[0] = 0;
fora(i, 1, n)
fors(j, Min,c[i]) {
dp[j] = max(dp[j], dp[j - c[i]] + h[i]);
}
LL Max = -1;
fora(i, 1, Min) {
if (dp[i] >= 0)
Max = max(Max, dp[i] * dp[i] - dp[i] * i - i*i);
}
if (Max < 0) printf("0\n");
else printf("%lld\n", Max);
}
return 0;
}