1389B.md (2331B)
1 Source: https://codeforces.com/problemset/problem/1389/B 2 3 B. Array Walk 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 You are given an array $a_1, a_2, \dots, a_n$, consisting of $n$ positive integers. 22 23 Initially you are standing at index $1$ and have a score equal to $a_1$. You can perform two kinds of moves: 24 25 1. move right — go from your current index $x$ to $x+1$ and add $a_{x+1}$ to your score. This move can only be performed if $x \lt n$. 26 2. move left — go from your current index $x$ to $x-1$ and add $a_{x-1}$ to your score. This move can only be performed if $x \gt 1$. Also, you can't perform two or more moves to the left in a row. 27 28 You want to perform exactly $k$ moves. Also, there should be no more than $z$ moves to the left among them. 29 30 What is the maximum score you can achieve? 31 32 Input 33 34 The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases. 35 36 The first line of each testcase contains three integers $n, k$ and $z$ ($2 \le n \le 10^5$, $1 \le k \le n - 1$, $0 \le z \le min(5, k)$) — the number of elements in the array, the total number of moves you should perform and the maximum number of moves to the left you can perform. 37 38 The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^4$) — the given array. 39 40 The sum of $n$ over all testcases does not exceed $3 \cdot 10^5$. 41 42 Output 43 44 Print $t$ integers — for each testcase output the maximum score you can achieve if you make exactly $k$ moves in total, no more than $z$ of them are to the left and there are no two or more moves to the left in a row. 45 46 Example 47 48 Input 49 50 ``` 51 4 52 5 4 0 53 1 5 4 3 2 54 5 4 1 55 1 5 4 3 2 56 5 4 4 57 10 20 30 40 50 58 10 7 3 59 4 6 8 2 9 9 7 4 10 9 60 ``` 61 62 Output 63 64 ``` 65 15 66 19 67 150 68 56 69 ``` 70 71 Note 72 73 In the first testcase you are not allowed to move left at all. So you make four moves to the right and obtain the score $a_1 + a_2 + a_3 + a_4 + a_5$. 74 75 In the second example you can move one time to the left. So we can follow these moves: right, right, left, right. The score will be $a_1 + a_2 + a_3 + a_2 + a_3$. 76 77 In the third example you can move four times to the left but it's not optimal anyway, you can just move four times to the right and obtain the score $a_1 + a_2 + a_3 + a_4 + a_5$.