D. Barrels

Codeforces
IDCF10279D
Time2000ms
Memory256MB
Difficulty
English · Original
Formal · Original
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water *at most* $k$ times. The first line contains two integers $n$ and $k$ ($1 <= k < n <= 2 dot.op 10^5$) — the number of barrels and the number of pourings you can make. The second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($0 <= a_i <= 10^9$), where $a_i$ is the initial amount of water the $i$-th barrel has. Print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water *at most* $k$ times. In the first example, you can pour all water from the second barrel to the fourth one. Then amount of water in each barrel will be equal to $[ 5, 0, 5, 10 ]$, and difference between the maximum and the minimum will be equal to $10$. In the second example, all barrels are empty, so we can't have anything to pour. The difference between the maximum and the minimum will be equal to $0$. ## Input The first line contains two integers $n$ and $k$ ($1 <= k < n <= 2 dot.op 10^5$) — the number of barrels and the number of pourings you can make.The second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($0 <= a_i <= 10^9$), where $a_i$ is the initial amount of water the $i$-th barrel has. ## Output Print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water *at most* $k$ times. [samples] ## Note In the first example, you can pour all water from the second barrel to the fourth one. Then amount of water in each barrel will be equal to $[ 5, 0, 5, 10 ]$, and difference between the maximum and the minimum will be equal to $10$.In the second example, all barrels are empty, so we can't have anything to pour. The difference between the maximum and the minimum will be equal to $0$.
**Definitions** Let $ n, k \in \mathbb{Z} $ with $ 1 \leq k < n \leq 2 \cdot 10^5 $. Let $ A = (a_1, a_2, \dots, a_n) $ be a sequence of non-negative integers representing initial water amounts in $ n $ barrels. **Constraints** 1. $ 1 \leq k < n \leq 2 \cdot 10^5 $ 2. $ 0 \leq a_i \leq 10^9 $ for all $ i \in \{1, \dots, n\} $ 3. At most $ k $ pour operations are allowed; each operation transfers any amount from a non-empty barrel $ x $ to another barrel $ y \neq x $. **Objective** Maximize the difference $ \max_i b_i - \min_i b_i $, where $ B = (b_1, \dots, b_n) $ is the final water distribution after at most $ k $ pours, subject to conservation of total water: $$ \sum_{i=1}^n b_i = \sum_{i=1}^n a_i $$ and $ b_i \geq 0 $ for all $ i $.
API Response (JSON)
{
  "problem": {
    "name": "D. Barrels",
    "description": {
      "content": "You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10279D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water.\n\nYou can pour water from one barrel to another. In one act of...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, k \\in \\mathbb{Z} $ with $ 1 \\leq k < n \\leq 2 \\cdot 10^5 $.  \nLet $ A = (a_1, a_2, \\dots, a_n) $ be a sequence of non-negative integers representing initial water amounts in...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments