Code: Select all
#include<cstdio>
#include<iostream>
#include<deque>
#include<cctype>
#include<cstdlib>
#include<vector>
#include<map>
#include<algorithm>
#include<cstring>
#include<queue>
#include<string>
#include<set>
#include<sstream>
#include<cmath>
#define inf 2000000000
using namespace std;
struct cosa
{
int p,w;
};
struct cell
{
int x,y;
};
struct orden
{
bool operator()(cell a,cell b)
{
if(a.x<b.x||(a.x==b.x&&a.y<b.y))
return true;
return false;
}
};
set<cell,orden> S;
int T[1001][31];
cosa A[1001];
int memo(int x,int y)
{
cell aux;
aux.x=x;
aux.y=y;
if(S.find(aux)==S.end())
{
if(x==0||y==0)
T[x][y]=0;
else
{
if(A[x].w>y)
T[x][y]=memo(x-1,y);
else
T[x][y]=max(memo(x-1,y),memo(x-1,y-A[x].w)+A[x].p);
}
S.insert(aux);
}
return T[x][y];
}
int main()
{
int Test,test,n,m,i,j,k,total,x;
scanf("%d",&Test);
for(test=0;test<Test;++test)
{
scanf("%d",&n);
for(i=1;i<=n;++i)
scanf("%d%d",&A[i].p,&A[i].w);
scanf("%d",&m);
for(i=0,total=0;i<m;++i)
{
scanf("%d",&x);
S.clear();
total+=memo(n,x);
}
printf("%d\n",total);
}
}
I discovered that this line:
Code: Select all
if(S.find(aux)==S.end())