codeforces-scrape

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

2075B.md (1802B)


      1 Source: https://codeforces.com/problemset/problem/2075/B
      2 
      3 B. Array Recoloring
      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 integer array $a$ of size $n$. Initially, all elements of the array are colored red.
     22 
     23 You have to choose exactly $k$ elements of the array and paint them blue. Then, while there is at least one red element, you have to select any red element with a blue neighbor and make it blue.
     24 
     25 The cost of painting the array is defined as the sum of the first $k$ chosen elements and the last painted element.
     26 
     27 Your task is to calculate the maximum possible cost of painting for the given array.
     28 
     29 Input
     30 
     31 The first line contains a single integer $t$ ($1 \le t \le 10^3$) — the number of test cases.
     32 
     33 The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 5000$; $1 \le k \lt n$).
     34 
     35 The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
     36 
     37 Additional constraint on the input: the sum of $n$ over all test cases doesn't exceed $5000$.
     38 
     39 Output
     40 
     41 For each test case, print a single integer — the maximum possible cost of painting for the given array.
     42 
     43 Example
     44 
     45 Input
     46 
     47 ```
     48 3
     49 
     50 3 1
     51 
     52 1 2 3
     53 
     54 5 2
     55 
     56 4 2 3 1 3
     57 
     58 4 3
     59 
     60 2 2 2 2
     61 ```
     62 
     63 Output
     64 
     65 ```
     66 5
     67 10
     68 8
     69 ```
     70 
     71 Note
     72 
     73 In the first example, you can initially color the $2$-nd element, and then color the elements in the order $1, 3$. Then the cost of painting is equal to $2+3=5$.
     74 
     75 In the second example, you can initially color the elements $1$ and $5$, and then color the elements in the order $2, 4, 3$. Then the cost of painting is equal to $4+3+3=10$.
     76 
     77 In the third example, you can initially color the elements $2, 3, 4$, and then color the $1$-st element. Then the cost of painting is equal to $2+2+2+2=8$.