codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1499C.md (3091B)


      1 Source: https://codeforces.com/problemset/problem/1499/C
      2 
      3 C. Minimum Grid Path
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$.
     22 
     23 You can move only in two directions:
     24 
     25 * to the right, i. e. horizontally and in the direction that increase your $x$ coordinate,
     26 * or up, i. e. vertically and in the direction that increase your $y$ coordinate.
     27 
     28 In other words, your path will have the following structure:
     29 
     30 * initially, you choose to go to the right or up;
     31 * then you go some positive integer distance in the chosen direction (distances can be chosen independently);
     32 * after that you change your direction (from right to up, or from up to right) and repeat the process.
     33 
     34 You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes.
     35 
     36 As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate.
     37 
     38 Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment.
     39 
     40 Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path).
     41 
     42 Find the path of the minimum cost and print its cost.
     43 
     44 Input
     45 
     46 The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
     47 
     48 The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$).
     49 
     50 The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) — the costs of each segment.
     51 
     52 It's guaranteed that the total sum of $n$ doesn't exceed $10^5$.
     53 
     54 Output
     55 
     56 For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments.
     57 
     58 Example
     59 
     60 Input
     61 
     62 ```
     63 3
     64 2
     65 13 88
     66 3
     67 2 3 1
     68 5
     69 4 3 2 1 4
     70 ```
     71 
     72 Output
     73 
     74 ```
     75 202
     76 13
     77 19
     78 ```
     79 
     80 Note
     81 
     82 In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$.
     83 
     84 In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$.
     85 
     86 The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$.
     87 
     88 In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one — $1$, the third one — $4$, the fourth one — $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.