A. Partition

Codeforces
IDCF946A
Time1000ms
Memory256MB
Difficulty
greedy
English · Original
Chinese · Translation
Formal · Original
You are given a sequence _a_ consisting of _n_ integers. You may partition this sequence into two sequences _b_ and _c_ in such a way that every element belongs exactly to one of these sequences. Let _B_ be the sum of elements belonging to _b_, and _C_ be the sum of elements belonging to _c_ (if some of these sequences is empty, then its sum is 0). What is the maximum possible value of _B_ - _C_? ## Input The first line contains one integer _n_ (1 ≤ _n_ ≤ 100) — the number of elements in _a_. The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ ( - 100 ≤ _a__i_ ≤ 100) — the elements of sequence _a_. ## Output Print the maximum possible value of _B_ - _C_, where _B_ is the sum of elements of sequence _b_, and _C_ is the sum of elements of sequence _c_. [samples] ## Note In the first example we may choose _b_ = {1, 0}, _c_ = { - 2}. Then _B_ = 1, _C_ =  - 2, _B_ - _C_ = 3. In the second example we choose _b_ = {16, 23, 16, 15, 42, 8}, _c_ = {} (an empty sequence). Then _B_ = 120, _C_ = 0, _B_ - _C_ = 120.
给定一个由 #cf_span[n] 个整数组成的序列 #cf_span[a]。你可以将该序列划分为两个序列 #cf_span[b] 和 #cf_span[c],使得每个元素恰好属于其中一个序列。 设 #cf_span[B] 为属于 #cf_span[b] 的元素之和,#cf_span[C] 为属于 #cf_span[c] 的元素之和(如果某个序列为空,则其和为 #cf_span[0])。求 #cf_span[B - C] 的最大可能值? 第一行包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 100]) —— 序列 #cf_span[a] 中的元素个数。 第二行包含 #cf_span[n] 个整数 #cf_span[a1], #cf_span[a2], ..., #cf_span[an] (#cf_span[ - 100 ≤ ai ≤ 100]) —— 序列 #cf_span[a] 的元素。 请输出 #cf_span[B - C] 的最大可能值,其中 #cf_span[B] 是序列 #cf_span[b] 的元素之和,#cf_span[C] 是序列 #cf_span[c] 的元素之和。 在第一个例子中,我们可以选择 #cf_span[b = {1, 0}],#cf_span[c = { - 2}]。此时 #cf_span[B = 1],#cf_span[C =  - 2],#cf_span[B - C = 3]。 在第二个例子中,我们选择 #cf_span[b = {16, 23, 16, 15, 42, 8}],#cf_span[c = {}](空序列)。此时 #cf_span[B = 120],#cf_span[C = 0],#cf_span[B - C = 120]。 ## Input 第一行包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 100]) —— 序列 #cf_span[a] 中的元素个数。第二行包含 #cf_span[n] 个整数 #cf_span[a1], #cf_span[a2], ..., #cf_span[an] (#cf_span[ - 100 ≤ ai ≤ 100]) —— 序列 #cf_span[a] 的元素。 ## Output 请输出 #cf_span[B - C] 的最大可能值,其中 #cf_span[B] 是序列 #cf_span[b] 的元素之和,#cf_span[C] 是序列 #cf_span[c] 的元素之和。 [samples] ## Note 在第一个例子中,我们可以选择 #cf_span[b = {1, 0}],#cf_span[c = { - 2}]。此时 #cf_span[B = 1],#cf_span[C =  - 2],#cf_span[B - C = 3]。在第二个例子中,我们选择 #cf_span[b = {16, 23, 16, 15, 42, 8}],#cf_span[c = {}](空序列)。此时 #cf_span[B = 120],#cf_span[C = 0],#cf_span[B - C = 120]。
**Definitions** Let $ n \in \mathbb{Z} $ be the number of elements. Let $ A = (a_1, a_2, \dots, a_n) $ be a sequence of integers, where $ a_i \in [-100, 100] $. Let $ B \subseteq A $ and $ C = A \setminus B $ be a partition of $ A $ into two disjoint subsequences (possibly empty). Let $ B = \sum_{a_i \in B} a_i $ and $ C = \sum_{a_i \in C} a_i $ be the sums of elements in $ B $ and $ C $, respectively. **Constraints** $ 1 \leq n \leq 100 $ **Objective** Maximize $ B - C $, where $ B + C = S = \sum_{i=1}^n a_i $. Since $ B - C = B - (S - B) = 2B - S $, the objective is equivalent to: $$ \max_{B \subseteq A} (2B - S) = 2 \cdot \max_{B \subseteq A} B - S $$ Thus, the problem reduces to: $$ \max_{B \subseteq A} (2B - S) = 2 \cdot \left( \max_{B \subseteq A} B \right) - S $$ Equivalently, maximize $ B - C $ by choosing $ B $ to be the subset of $ A $ with maximum possible sum. **Final Objective** $$ \max_{B \subseteq A} (B - (S - B)) = \max_{B \subseteq A} (2B - S) $$
Samples
Input #1
3
1 -2 0
Output #1
3
Input #2
6
16 23 16 15 42 8
Output #2
120
API Response (JSON)
{
  "problem": {
    "name": "A. Partition",
    "description": {
      "content": "You are given a sequence _a_ consisting of _n_ integers. You may partition this sequence into two sequences _b_ and _c_ in such a way that every element belongs exactly to one of these sequences. Let",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF946A"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a sequence _a_ consisting of _n_ integers. You may partition this sequence into two sequences _b_ and _c_ in such a way that every element belongs exactly to one of these sequences.\n\nLet...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "给定一个由 #cf_span[n] 个整数组成的序列 #cf_span[a]。你可以将该序列划分为两个序列 #cf_span[b] 和 #cf_span[c],使得每个元素恰好属于其中一个序列。\n\n设 #cf_span[B] 为属于 #cf_span[b] 的元素之和,#cf_span[C] 为属于 #cf_span[c] 的元素之和(如果某个序列为空,则其和为 #cf_span[0])。求 #c...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z} $ be the number of elements.  \nLet $ A = (a_1, a_2, \\dots, a_n) $ be a sequence of integers, where $ a_i \\in [-100, 100] $.  \n\nLet $ B \\subseteq A $ and $ C = ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments