C. Vasya And The Mushrooms

Codeforces
IDCF1016C
Time2000ms
Memory256MB
Difficulty
dpimplementation
English · Original
Chinese · Translation
Formal · Original
Vasya's house is situated in a forest, and there is a mushroom glade near it. The glade consists of two rows, each of which can be divided into _n_ consecutive cells. For each cell Vasya knows how fast the mushrooms grow in this cell (more formally, how many grams of mushrooms grow in this cell each minute). Vasya spends exactly one minute to move to some adjacent cell. Vasya cannot leave the glade. Two cells are considered adjacent if they share a common side. When Vasya enters some cell, he instantly collects all the mushrooms growing there. Vasya begins his journey in the left upper cell. Every minute Vasya must move to some adjacent cell, he cannot wait for the mushrooms to grow. He wants to visit all the cells **exactly once** and maximize the total weight of the collected mushrooms. Initially, all mushrooms have a weight of 0. Note that Vasya doesn't need to return to the starting cell. Help Vasya! Calculate the maximum total weight of mushrooms he can collect. ## Input The first line contains the number _n_ (1 ≤ _n_ ≤ 3·105) — the length of the glade. The second line contains _n_ numbers _a_1, _a_2, ..., _a__n_ (1 ≤ _a__i_ ≤ 106) — the growth rate of mushrooms in the first row of the glade. The third line contains _n_ numbers _b_1, _b_2, ..., _b__n_ (1 ≤ _b__i_ ≤ 106) is the growth rate of mushrooms in the second row of the glade. ## Output Output one number — the maximum total weight of mushrooms that Vasya can collect by choosing the optimal route. Pay attention that Vasya must visit every cell of the glade **exactly once**. [samples] ## Note In the first test case, the optimal route is as follows: <center>![image](https://espresso.codeforces.com/eed94d501269b39cd634215da228ac5a494084af.png)</center>Thus, the collected weight of mushrooms will be 0·1 + 1·2 + 2·3 + 3·4 + 4·5 + 5·6 = 70.In the second test case, the optimal route is as follows: <center>![image](https://espresso.codeforces.com/af365baf4d3bd2d827210a17247d6f0eb1eef1d9.png)</center>Thus, the collected weight of mushrooms will be 0·1 + 1·10 + 2·100 + 3·1000 + 4·10000 + 5·100000 = 543210.
Vasya 的房子位于一片森林中,附近有一个蘑菇林地。该林地由两行组成,每行可以划分为 #cf_span[n] 个连续的单元格。对于每个单元格,Vasya 知道其中蘑菇的生长速度(即每分钟长出多少克蘑菇)。Vasya 移动到相邻单元格恰好需要一分钟。Vasya 不能离开林地。如果两个单元格共享一条公共边,则它们是相邻的。当 Vasya 进入某个单元格时,他会立即收集该单元格中所有生长的蘑菇。 Vasya 从左上角的单元格开始旅程。每分钟,Vasya 必须移动到某个相邻的单元格,他不能等待蘑菇生长。他希望恰好访问每个单元格一次,并最大化收集的蘑菇总重量。初始时,所有蘑菇的重量均为 #cf_span[0]。注意,Vasya 不需要返回起点。 帮助 Vasya!计算他通过选择最优路径所能收集的蘑菇最大总重量。 第一行包含数字 #cf_span[n (1 ≤ n ≤ 3·105)] —— 林地的长度。 第二行包含 #cf_span[n] 个数字 #cf_span[a1, a2, ..., an (1 ≤ ai ≤ 106)] —— 林地第一行中蘑菇的生长速率。 第三行包含 #cf_span[n] 个数字 #cf_span[b1, b2, ..., bn (1 ≤ bi ≤ 106)] —— 林地第二行中蘑菇的生长速率。 输出一个数字 —— Vasya 通过选择最优路径所能收集的蘑菇最大总重量。请注意,Vasya 必须恰好访问林地的每个单元格一次。 ## Input 第一行包含数字 #cf_span[n (1 ≤ n ≤ 3·105)] —— 林地的长度。第二行包含 #cf_span[n] 个数字 #cf_span[a1, a2, ..., an (1 ≤ ai ≤ 106)] —— 林地第一行中蘑菇的生长速率。第三行包含 #cf_span[n] 个数字 #cf_span[b1, b2, ..., bn (1 ≤ bi ≤ 106)] —— 林地第二行中蘑菇的生长速率。 ## Output 输出一个数字 —— Vasya 通过选择最优路径所能收集的蘑菇最大总重量。请注意,Vasya 必须恰好访问林地的每个单元格一次。 [samples] ## Note 在第一个测试用例中,最优路径如下: 因此,收集的蘑菇重量为 #cf_span[0·1 + 1·2 + 2·3 + 3·4 + 4·5 + 5·6 = 70]。 在第二个测试用例中,最优路径如下: 因此,收集的蘑菇重量为 #cf_span[0·1 + 1·10 + 2·100 + 3·1000 + 4·10000 + 5·100000 = 543210]。
**Definitions** Let $ n \in \mathbb{Z}^+ $ be the length of each row of the glade. Let $ A = (a_1, a_2, \dots, a_n) \in \mathbb{R}^n $ be the mushroom growth rates in the first row. Let $ B = (b_1, b_2, \dots, b_n) \in \mathbb{R}^n $ be the mushroom growth rates in the second row. The glade is a $ 2 \times n $ grid, where cell $ (1, i) $ has growth rate $ a_i $ and cell $ (2, i) $ has growth rate $ b_i $, for $ i \in \{1, \dots, n\} $. Vasya starts at cell $ (1, 1) $. Each minute, he moves to an adjacent cell (up/down/left/right), without staying in place. He must visit each of the $ 2n $ cells exactly once. Mushroom weight in cell $ (r, i) $ at time $ t $ is $ w_{r,i}(t) = g_{r,i} \cdot t $, where $ g_{r,i} $ is the growth rate and $ t $ is the minute when the cell is visited (starting at $ t = 0 $ for the first move). The total collected weight is the sum of $ g_{r,i} \cdot t_{r,i} $, where $ t_{r,i} $ is the time step (0-indexed) at which cell $ (r, i) $ is visited. **Constraints** 1. $ 1 \leq n \leq 3 \cdot 10^5 $ 2. $ 1 \leq a_i, b_i \leq 10^6 $ for all $ i \in \{1, \dots, n\} $ 3. The path is a Hamiltonian path on the $ 2 \times n $ grid, starting at $ (1,1) $, with moves only between adjacent cells. **Objective** Maximize the total collected mushroom weight: $$ \max \sum_{(r,i) \in \text{grid}} g_{r,i} \cdot t_{r,i} $$ where $ t_{r,i} \in \{0, 1, \dots, 2n-1\} $ are distinct and correspond to a valid Hamiltonian path starting at $ (1,1) $.
Samples
Input #1
3
1 2 3
6 5 4
Output #1
70
Input #2
3
1 1000 10000
10 100 100000
Output #2
543210
API Response (JSON)
{
  "problem": {
    "name": "C. Vasya And The Mushrooms",
    "description": {
      "content": "Vasya's house is situated in a forest, and there is a mushroom glade near it. The glade consists of two rows, each of which can be divided into _n_ consecutive cells. For each cell Vasya knows how fas",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF1016C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Vasya's house is situated in a forest, and there is a mushroom glade near it. The glade consists of two rows, each of which can be divided into _n_ consecutive cells. For each cell Vasya knows how fas...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Vasya 的房子位于一片森林中,附近有一个蘑菇林地。该林地由两行组成,每行可以划分为 #cf_span[n] 个连续的单元格。对于每个单元格,Vasya 知道其中蘑菇的生长速度(即每分钟长出多少克蘑菇)。Vasya 移动到相邻单元格恰好需要一分钟。Vasya 不能离开林地。如果两个单元格共享一条公共边,则它们是相邻的。当 Vasya 进入某个单元格时,他会立即收集该单元格中所有生长的蘑菇。\n\nV...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z}^+ $ be the length of each row of the glade.  \nLet $ A = (a_1, a_2, \\dots, a_n) \\in \\mathbb{R}^n $ be the mushroom growth rates in the first row.  \nLet $ B = (b...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments