CF1499C.Minimum Grid Path

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Let's say you are standing on the XYXY -plane at point (0,0)(0, 0) and you want to reach point (n,n)(n, n) .

You can move only in two directions:

  • to the right, i. e. horizontally and in the direction that increase your xx coordinate,
  • or up, i. e. vertically and in the direction that increase your yy coordinate.

In other words, your path will have the following structure:

  • initially, you choose to go to the right or up;
  • then you go some positive integer distance in the chosen direction (distances can be chosen independently);
  • after that you change your direction (from right to up, or from up to right) and repeat the process.

You don't like to change your direction too much, so you will make no more than n1n - 1 direction changes.

As a result, your path will be a polygonal chain from (0,0)(0, 0) to (n,n)(n, n) , consisting of at most nn line segments where each segment has positive integer length and vertical and horizontal segments alternate.

Not all paths are equal. You have nn integers c1,c2,,cnc_1, c_2, \dots, c_n where cic_i is the cost of the ii -th segment.

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 kk segments ( knk \le n ), then the cost of the path is equal to i=1kcilengthi\sum\limits_{i=1}^{k}{c_i \cdot length_i} (segments are numbered from 11 to kk in the order they are in the path).

Find the path of the minimum cost and print its cost.

输入格式

The first line contains the single integer tt ( 1t10001 \le t \le 1000 ) — the number of test cases.

The first line of each test case contains the single integer nn ( 2n1052 \le n \le 10^5 ).

The second line of each test case contains nn integers c1,c2,,cnc_1, c_2, \dots, c_n ( 1ci1091 \le c_i \le 10^9 ) — the costs of each segment.

It's guaranteed that the total sum of nn doesn't exceed 10510^5 .

输出格式

For each test case, print the minimum possible cost of the path from (0,0)(0, 0) to (n,n)(n, n) consisting of at most nn alternating segments.

输入输出样例

  • 输入#1

    3
    2
    13 88
    3
    2 3 1
    5
    4 3 2 1 4

    输出#1

    202
    13
    19

说明/提示

In the first test case, to reach (2,2)(2, 2) you need to make at least one turn, so your path will consist of exactly 22 segments: one horizontal of length 22 and one vertical of length 22 . The cost of the path will be equal to 2c1+2c2=26+176=2022 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202 .

In the second test case, one of the optimal paths consists of 33 segments: the first segment of length 11 , the second segment of length 33 and the third segment of length 22 .

The cost of the path is 12+33+21=131 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13 .

In the third test case, one of the optimal paths consists of 44 segments: the first segment of length 11 , the second one — 11 , the third one — 44 , the fourth one — 44 . The cost of the path is 14+13+42+41=191 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19 .

首页