所给数据在int范围内
单独使用其中一种枚举算法都会导致TLE
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
int main(){
int T;
scanf("%d", &T);
for(int kase = 1; kase <= T; kase++) {
int n, s1, v1, s2, v2;
scanf("%d%d%d%d%d", &n, &s1, &v1, &s2, &v2);
if(s1 > s2){ // s2 is bigger
swap(s1, s2);
swap(v1, v2);
}
LL ans = 0;
if(n / s2 >= 2<<16){ // both s1 and s2 are small
for(LL i = 0; i <= s1; i++){ //item 2 as many as possible
ans = max(ans, v2*i + (n-s2*i)/s1*v1);
}
for(LL i = 0; i <= s2; i++){ //item 1 as many as possible
ans = max(ans, v1*i + (n-s1*i)/s2*v2);
}
}else{ // s2 is large
for(LL i = 0; s2*i <= n; i++)
// search the number of item 2 and then item 1 as many as possible
ans = max(ans, v2*i + (n-s2*i)/s1*v1);
}
printf("Case #%d: %lld\n", kase, ans);
}
return 0;
}