D. Arpa's weak amphitheater and Mehrdad's valuable Hoses

 

Just to remind, girls in Arpa's land are really nice.

Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≤ i < k, and a1 = x and ak = y.

Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it.

Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w.

Input

The first line contains integers nm and w (1  ≤  n  ≤  1000, , 1 ≤ w ≤ 1000) — the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited.

The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 1000) — the weights of the Hoses.

The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 106) — the beauties of the Hoses.

The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≤ xi, yi ≤ nxi ≠ yi), meaning that Hoses xiand yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct.

Output

Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w.

Examples
input
  1. 3 1 5
    3 2 5
    2 4 2
    1 2
output
  1. 6
input
  1. 4 2 11
    2 4 6 6
    6 4 2 1
    1 2
    2 3
output
  1. 7
Note

In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6.

In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<vector>
  5. #include<algorithm>
  6. #define pb push_back
  7. using namespace std;
  8. typedef long long LL;
  9. vector<int>G[];
  10. vector<int>block[];
  11. int w[],b[],mark[],dp[],sumw[],sumb[],n,m,W;
  12. int max(int a,int b,int c){return max(a,max(b,c));}
  13. void dfs(int u,int id)
  14. {
  15. mark[u]=id;
  16. block[id].pb(u);
  17. sumw[id]+=w[u];
  18. sumb[id]+=b[u];
  19. for(int i=;i<G[u].size();i++)
  20. if(!mark[G[u][i]])
  21. dfs(G[u][i],id);
  22. }
  23. void work(int cur)
  24. {
  25. for(int j=W;j>=;j--)
  26. {
  27. for(int i=;i<block[cur].size();i++)
  28. if(j>=w[block[cur][i]])
  29. dp[j]=max(dp[j],dp[j-w[block[cur][i]]]+b[block[cur][i]]);
  30. if(j>=sumw[cur])
  31. dp[j]=max(dp[j],dp[j-sumw[cur]]+sumb[cur]);
  32. }
  33. }
  34. int main()
  35. {
  36. scanf("%d%d%d",&n,&m,&W);
  37. for(int i=;i<=n;i++)scanf("%d",&w[i]);
  38. for(int i=;i<=n;i++)scanf("%d",&b[i]);
  39. for(int i=,u,v;i<m;i++)
  40. {
  41. scanf("%d%d",&u,&v);
  42. G[u].pb(v);
  43. G[v].pb(u);
  44. }
  45. int id=;
  46. for(int i=;i<=n;i++)
  47. if(!mark[i])dfs(i,++id);
  48. for(int i=;i<=id;i++)work(i);
  49. printf("%d\n",dp[W]);
  50. return ;
  51. }

codeforces 742D (分组背包)的更多相关文章

  1. #分组背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable

    2018-03-11 http://codeforces.com/contest/946/problem/D D. Timetable time limit per test 2 seconds me ...

  2. Codeforces 946D Timetable(预处理+分组背包)

    题目链接:http://codeforces.com/problemset/problem/946/D 题目大意:有n个字符串,代表n天的课表,1表示这个时间要上课,0表示不要上课,一天在学校时间为第 ...

  3. Codeforces Round #383 (Div. 2) D 分组背包

    给出一群女孩的重量和颜值 和她们的朋友关系 现在有一个舞台 ab是朋友 bc是朋友 ac就是朋友 给出最大承重 可以邀请这些女孩来玩 对于每一个朋友团体 全邀请or邀请一个or不邀请 问能邀请的女孩的 ...

  4. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(分组背包+dsu)

    D. Arpa's weak amphitheater and Mehrdad's valuable Hoses Problem Description: Mehrdad wants to invit ...

  5. Codeforces Round #383 (Div. 2) A,B,C,D 循环节,标记,暴力,并查集+分组背包

    A. Arpa’s hard exam and Mehrdad’s naive cheat time limit per test 1 second memory limit per test 256 ...

  6. Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)

    <题目链接> 题目大意: 就是有n个人,每个人都有一个体积和一个价值.这些人之间有有些人之间是朋友,所有具有朋友关系的人构成一组.现在要在这些组中至多选一个人或者这一组的人都选,在总容量为 ...

  7. Codeforces 946 D.Timetable-数据处理+动态规划(分组背包) 处理炸裂

    花了两个晚上来搞这道题. 第一个晚上想思路和写代码,第二个晚上调试. 然而还是菜,一直调不对,我的队友是Debug小能手呀(真的是无敌,哈哈,两个人一会就改好了) D. Timetable   tim ...

  8. 2018.12.14 codeforces 922E. Birds(分组背包)

    传送门 蒟蒻净做些水题还请大佬见谅 没错这又是个一眼的分组背包. 题意简述:有n棵树,每只树上有aia_iai​只鸟,第iii棵树买一只鸟要花cic_ici​的钱,每买一只鸟可以奖励bbb块钱,从一棵 ...

  9. Codeforces 946D - Timetable (预处理+分组背包)

    题目链接:Timetable 题意:Ivan是一个学生,在一个Berland周内要上n天课,每天最多会有m节,他能逃课的最大数量是k.求他在学校的时间最小是多少? 题解:先把每天逃课x节在学校呆的最小 ...

随机推荐

  1. Android基础

    今天学习Android基本环境及基础知识,正确来说是重新温习Android知识,因为初次接触Android开发已经是两年前的事,如今又回到Android开发依然那么熟悉,依然可以让人很兴奋,Andro ...

  2. 洛谷P3392 涂国旗

    P3392 涂国旗 107通过 507提交 题目提供者kkksc03 标签 难度普及- 提交  讨论  题解 最新讨论 直接读字符会wa WA?-- 为什么不对... 跪求找错 快点给钱 这不就是荷兰 ...

  3. 可变参数宏__VA_ARGS__和...

    __VA_ARGS__ 是一个可变参数的宏(gcc支持).实现思想就是宏定义中参数列表的最后一个参数为省略号(也就是三个点).这样预定义宏_ _VA_ARGS_ _就可以被用在替换部分中,替换省略号所 ...

  4. 我刚知道的WAP app中meta的属性

    之前我一直做的都是WEB前端开发,来北京以后面试了一个移动前端开发,WAP前端开发. 其实在原来公司的时候也做过这方面的开发,可面试的时候面试官问我,要想强制让文档与设备的宽度保持1:1,mate标签 ...

  5. JS数组转成json字符串的注意事项

    在js中常常会将一个数组转成json字符串发送给后端. 这时候在定义数组数据结构的时候需要格外注意,意味json中是有集合和对象的区别的. 集合的定义是[];对象的的定义是{}. 这时候,在创建数组时 ...

  6. MVC中Control和View之间数据传递的方式

    1:ViewBag和ViewData 具体区别不做讨论,本处只演示ViewData的具体示例: Controler代码:ViewData["Employee"] = emp; Vi ...

  7. nginx转发post消息成301了

    刚才开始是:第一次遇到这个问题是因为不支持sslv3 后来测试的时候,是因为没注意https的导致http 301跳转https post请求nginx转发成301了,到时post数据丢失

  8. High Memory in the Linux Kernel

    This is enabled via the PAE (Physical Address Extension) extension of the PentiumPro processors. PAE ...

  9. Idea安装及简单配置

    1. 安装JDK   设置环境变量   JAVA_HOME    C:\Program Files\Java\jdk1.8.0_45   CLASSPATH    .;%JAVA_HOME%\lib; ...

  10. RabbitMQ的work queue(2)

    课堂上work queue没能很好的理解,看了大神的博客,顿觉醍醐灌顶,豁然开朗. work queue有两种模式: 平均分配:(默认)//channel.basicQos(1);即把 同一时刻服务器 ...