#這程式碼還能怎麼優化
1 messages · Page 1 of 1 (latest)
#include <bits/stdc++.h>
using namespace std;
int t,n,p[1001]={},w[1001]={},gi,g[101]={},dp[1001][1001]={},counter;
int main(){
ios::sync_with_stdio(0),cin.tie(0);
while(cin>>t){
while(t--){
counter=0;
cin>>n;
for(int i=1;i<=n;i++){
cin>>p[i]>>w[i];
}
cin>>gi;
for(int i=1;i<=gi;i++){
cin>>g[i];
}
for(int l=1;l<=gi;l++){
for(int i=1;i<=n;i++){
for(int j=1;j<=g[l];j++){
if(w[i]>j){
dp[i][j]=dp[i-1][j];
}
else{
dp[i][j]=max(dp[i-1][j],p[i]+dp[i-1][j-w[i]]);
}
}
}
counter+=dp[n][g[l]];
}
cout<<counter<<'\n';
}
}
}
sorry…還不太會用dc