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

Codeforces
IDCF741B
Time1000ms
Memory256MB
Difficulty
dfs and similardpdsu
English · Original
Chinese · Translation
Formal · Original
_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 _w__i_ and some beauty _b__i_. 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 _a_1, _a_2, ..., _a__k_ such that _a__i_ and _a__i_ + 1 are friends for each 1 ≤ _i_ < _k_, and _a_1 = _x_ and _a__k_ = _y_. <center>![image](https://espresso.codeforces.com/06c6e83602b56f97aa5d67634ad76cab1cb35a69.png)</center>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 _n_, _m_ 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 _w_1, _w_2, ..., _w__n_ (1 ≤ _w__i_ ≤ 1000) — the weights of the Hoses. The third line contains _n_ integers _b_1, _b_2, ..., _b__n_ (1 ≤ _b__i_ ≤ 106) — the beauties of the Hoses. The next _m_ lines contain pairs of friends, the _i_\-th of them contains two integers _x__i_ and _y__i_ (1 ≤ _x__i_, _y__i_ ≤ _n_, _x__i_ ≠ _y__i_), meaning that Hoses _x__i_ and _y__i_ are friends. Note that friendship is bidirectional. All pairs (_x__i_, _y__i_) 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_. [samples] ## 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.
[{"iden":"statement","content":"_Just to remind, girls in Arpa's land are really nice._\n\nMehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight $w_i$ and some beauty $b_i$. 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 $a_1, a_2, ..., a_k$ such that $a_i$ and $a_{i + 1}$ are friends for each $1 ≤ i < k$, and $a_1 = x$ and $a_k = y$.\n\nArpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most $w$ weight on it. \n\nMehrdad 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$.\n\nThe first line contains integers $n$, $m$ 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.\n\nThe second line contains $n$ integers $w_1, w_2, ..., w_n$ ($1 ≤ w_i ≤ 1000$) — the weights of the Hoses.\n\nThe third line contains $n$ integers $b_1, b_2, ..., b_n$ ($1 ≤ b_i ≤ 10^6$) — the beauties of the Hoses.\n\nThe next $m$ lines contain pairs of friends, the $i$-th of them contains two integers $x_i$ and $y_i$ ($1 ≤ x_i, y_i ≤ n$, $x_i ≠ y_i$), meaning that Hoses $x_i$ and $y_i$ are friends. Note that friendship is bidirectional. All pairs $(x_i, y_i)$ are distinct.\n\nPrint the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed $w$.\n\nIn 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$.\n\nIn 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$.\n"},{"iden":"input","content":"The first line contains integers $n$, $m$ 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 $w_1, w_2, ..., w_n$ ($1 ≤ w_i ≤ 1000$) — the weights of the Hoses.The third line contains $n$ integers $b_1, b_2, ..., b_n$ ($1 ≤ b_i ≤ 10^6$) — the beauties of the Hoses.The next $m$ lines contain pairs of friends, the $i$-th of them contains two integers $x_i$ and $y_i$ ($1 ≤ x_i, y_i ≤ n$, $x_i ≠ y_i$), meaning that Hoses $x_i$ and $y_i$ are friends. Note that friendship is bidirectional. All pairs $(x_i, y_i)$ are distinct."},{"iden":"output","content":"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$."},{"iden":"examples","content":"Input\n3 1 5\n3 2 5\n2 4 2\n1 2\nOutput\n6\n\nInput\n4 2 11\n2 4 6 6\n6 4 2 1\n1 2\n2 3\nOutput\n7\n"},{"iden":"note","content":"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$.\nIn 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$."}]
**Definitions** Let $ n, m, w \in \mathbb{Z}^+ $ denote the number of Hoses, number of friendship pairs, and maximum allowed weight, respectively. Let $ W = (w_1, \dots, w_n) \in \mathbb{Z}^n $ be the weight vector, and $ B = (b_1, \dots, b_n) \in \mathbb{Z}^n $ the beauty vector. Let $ G = (V, E) $ be an undirected graph with $ V = \{1, \dots, n\} $ and $ E $ given by the $ m $ friendship pairs. Let $ \mathcal{C} = \{C_1, \dots, C_k\} $ be the set of connected components (friendship groups) of $ G $. **Constraints** 1. $ 1 \leq n \leq 1000 $, $ 1 \leq m \leq 1000 $, $ 1 \leq w \leq 1000 $ 2. $ 1 \leq w_i \leq 1000 $, $ 1 \leq b_i \leq 10^6 $ for all $ i \in \{1, \dots, n\} $ 3. For each group $ C_j \in \mathcal{C} $, Mehrdad may select: - **Option 1:** Exactly one Hos $ i \in C_j $, or - **Option 2:** All Hoses in $ C_j $, - **Option 3:** No Hos from $ C_j $. **Objective** Maximize total beauty $ \sum_{i \in S} b_i $ subject to total weight $ \sum_{i \in S} w_i \leq w $, where $ S \subseteq \{1, \dots, n\} $ satisfies the group selection constraint above. Formally: $$ \max_{S \subseteq \{1,\dots,n\}} \sum_{i \in S} b_i \quad \text{subject to} \quad \sum_{i \in S} w_i \leq w \quad \text{and} \quad \forall C_j \in \mathcal{C}, \quad |S \cap C_j| \in \{0, 1\} \cup \{C_j\} $$
Samples
Input #1
3 1 5
3 2 5
2 4 2
1 2
Output #1
6
Input #2
4 2 11
2 4 6 6
6 4 2 1
1 2
2 3
Output #2
7
API Response (JSON)
{
  "problem": {
    "name": "B. Arpa's weak amphitheater and Mehrdad's valuable Hoses",
    "description": {
      "content": "_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 _w__i_ and some beauty _b__i_. Also each Hos may",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF741B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "_Just to remind, girls in Arpa's land are really nice._\n\nMehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight _w__i_ and some beauty _b__i_. Also each Hos may...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "[{\"iden\":\"statement\",\"content\":\"_Just to remind, girls in Arpa's land are really nice._\\n\\nMehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight $w_i$ and some...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, m, w \\in \\mathbb{Z}^+ $ denote the number of Hoses, number of friendship pairs, and maximum allowed weight, respectively.  \nLet $ W = (w_1, \\dots, w_n) \\in \\mathbb{Z}^n $ be...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments