Advertisement
Underdogs

cf 383 div2 D

Dec 12th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef pair<int,int> ii;
  6. #define pii 3.141592654
  7. #define INF 1000000007
  8. #define N 1005
  9. #define M 10
  10. #define F first
  11. #define S second
  12. #define pb push_back
  13. #define mp make_pair
  14. #define si(x) scanf("%d",&x)
  15. #define sif(x) scanf("%lf",&x)
  16. #define sil(x) scanf("%I64d",&x)
  17. #define pi(x) printf("%d\n",x)
  18. #define pis(x) printf("%d ",x)
  19. #define cpi(x) printf("Case %d: ",x)
  20. #define pil(x) printf("%I64d\n",x)
  21. #define rep(i,n,m) for(int i = n; i< m; i++)
  22. #define reps(i,n,m) for(int i = n; i >= m; i--)
  23. #define lft(x) x*2
  24. #define rgt(x) x*2+1
  25.  
  26. int n,m,x,y,cmp,mark[N];
  27. vector<int>ad[N];
  28. int weight[N],w,allw[N],allb[N],nxt[N];
  29. pair<int,int>pa[N];
  30. ll beauty[N],dp[N][N];
  31.  
  32. void go(int u)
  33. {
  34.     mark[u] = cmp;
  35.     allw[cmp] += weight[u];
  36.     allb[cmp] += beauty[u];
  37.  
  38.  
  39.     rep(i,0,ad[u].size()){
  40.         int v = ad[u][i];
  41.         if(!mark[v]){
  42.             go(v);
  43.         }
  44.     }
  45. }
  46.  
  47. ll go1(int pos,int tot)
  48. {
  49.     if(tot > w) return -1e12;
  50.     if(pos > n) return 0;
  51.  
  52.     ll &ret = dp[pos][tot];
  53.     if(ret != -1) return ret;
  54.  
  55.     ret = 0;
  56.  
  57.     int p = nxt[pos];
  58.     ll a = 0;
  59.     rep(i,pos,p){
  60.         //two options
  61.         //ami ai component er sb nite pari or akta niye next component a jete pari
  62.         a = max(a,beauty[pa[i].S] + go1(nxt[i],tot+weight[pa[i].S]));
  63.     }
  64.     ll b = go1(nxt[pos],tot);
  65.     ll c = allb[pa[pos].F] + go1(nxt[pos],tot+allw[pa[pos].F]);
  66.  
  67.     return ret = max(a,max(b,c));
  68. }
  69.  
  70. int main()
  71. {
  72.     memset(dp,-1,sizeof(dp));
  73.     si(n);si(m);si(w);
  74.     rep(i,1,n+1) si(weight[i]);
  75.     rep(i,1,n+1) sil(beauty[i]);
  76.  
  77.     rep(i,0,m){
  78.         si(x);si(y);
  79.         ad[x].pb(y);
  80.         ad[y].pb(x);
  81.     }
  82.     cmp = 0;
  83.     rep(i,1,n+1){
  84.         if(!mark[i]){
  85.             cmp++;
  86.             go(i);
  87.         }
  88.     }
  89.  
  90.  
  91.     rep(i,1,n+1){
  92.         pa[i].F = mark[i];
  93.         pa[i].S = i;
  94.     }
  95.  
  96.     sort(pa+1,pa+n+1);
  97.     int u = n + 1,y = pa[n].F;
  98.     //this nxt array for finding efficiently next component's start index to escape time limit
  99.     reps(i,n,1){
  100.         if(y == pa[i].F){
  101.             nxt[i] = u;
  102.         }
  103.         else{
  104.             u = i + 1;
  105.             y = pa[i].F;
  106.             nxt[i] = u;
  107.         }
  108.     }
  109.  
  110.     ll ans = go1(1,0);
  111.  
  112.     pil(ans);
  113.  
  114.  
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement