J. Restorer Distance

Codeforces
IDCF10259J
Time1000ms
Memory256MB
Difficulty
English · Original
Formal · Original
You have to restore the wall. The wall consists of $N$ pillars of bricks, the height of the $i$-th pillar is initially equal to $h_i$, the height is measured in number of bricks. After the restoration all the $N$ pillars should have equal heights. You are allowed the following operations: You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes $0$. What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height? The first line of input contains four integers $N$, $A$, $R$, $M$ ($1 <= N <= 10^5$, $0 <= A, R, M <= 10^4$) — the number of pillars and the costs of operations. The second line contains $N$ integers $h_i$ ($0 <= h_i <= 10^9$) — initial heights of pillars. Print one integer — the minimal cost of restoration. ## Input The first line of input contains four integers $N$, $A$, $R$, $M$ ($1 <= N <= 10^5$, $0 <= A, R, M <= 10^4$) — the number of pillars and the costs of operations.The second line contains $N$ integers $h_i$ ($0 <= h_i <= 10^9$) — initial heights of pillars. ## Output Print one integer — the minimal cost of restoration. [samples]
**Definitions** Let $ N \in \mathbb{Z}^+ $ be the number of pillars. Let $ A, R, M \in \mathbb{R}_{\geq 0} $ be the costs per brick for adding, removing, and moving a brick, respectively. Let $ H = (h_1, h_2, \dots, h_N) $ be the initial height vector of the pillars, where $ h_i \in \mathbb{Z}_{\geq 0} $. Let $ x \in \mathbb{Z}_{\geq 0} $ be the target uniform height of all pillars after restoration. **Constraints** 1. $ 1 \leq N \leq 10^5 $ 2. $ 0 \leq A, R, M \leq 10^4 $ 3. $ 0 \leq h_i \leq 10^9 $ for all $ i \in \{1, \dots, N\} $ **Objective** Minimize the total cost: $$ \min_{x \in \mathbb{Z}_{\geq 0}} \left( A \cdot \sum_{i=1}^N \max(0, x - h_i) + R \cdot \sum_{i=1}^N \max(0, h_i - x) + M \cdot \min\left( \sum_{i=1}^N \max(0, x - h_i), \sum_{i=1}^N \max(0, h_i - x) \right) \right) $$
API Response (JSON)
{
  "problem": {
    "name": "J. Restorer Distance",
    "description": {
      "content": "You have to restore the wall. The wall consists of $N$ pillars of bricks, the height of the $i$-th pillar is initially equal to $h_i$, the height is measured in number of bricks. After the restoration",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10259J"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You have to restore the wall. The wall consists of $N$ pillars of bricks, the height of the $i$-th pillar is initially equal to $h_i$, the height is measured in number of bricks. After the restoration...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ N \\in \\mathbb{Z}^+ $ be the number of pillars.  \nLet $ A, R, M \\in \\mathbb{R}_{\\geq 0} $ be the costs per brick for adding, removing, and moving a brick, respectively.  \nLet $ ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments