210
#include<stdio.h> #include<string.h> #define max 10 int val[max]; int cost[max]; int uni[max]; int hash[max]; int ans[max]; int find(int n){ if(n==uni[n]) return n; else return uni[n]=find(uni[n]); } int fmax(int a,int b){ if(a>b)return a; return b; } int main(void){ int i,a,b; int Vmax,conflic,count; memset(hash,0,sizeof(hash)); memset(ans,0,sizeof(ans)); scanf("%d%d%d",&count,&Vmax,&conflic); for(i=0;i<count;i++) uni[i]=i; for(i=0;i<count;i++){ scanf("%d%d",&val[i],&cost[i]); } for(i=0;i<conflic;i++){ scanf("%d%d",&a,&b); a--;b--; int x,y; x=find(a); y=find(b); if(x!=y){ uni[x]=y; } } for(i=0;i<count;i++){ if(find(i)==i)hash[i]=1; } for(i=0;i<count;i++){ if(hash[i]==0)continue; for(a=Vmax;a>=1;a--) for(b=0;b<count;b++) if(uni[b]==i&&a>=cost[b]) ans[a]=fmax(ans[a],ans[a-cost[b]]+val[b]); } printf("%d\n",ans[Vmax]); }