codeforces-scrape

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

1006B.md (2981B)


      1 Source: https://codeforces.com/problemset/problem/1006/B
      2 
      3 B. Polycarp's Practice
      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 Polycarp is practicing his problem solving skill. He has a list of $n$ problems with difficulties $a_1, a_2, \dots, a_n$, respectively. His plan is to practice for exactly $k$ days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all $n$ problems in exactly $k$ days.
     22 
     23 Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in $k$ days he will solve all the $n$ problems.
     24 
     25 The profit of the $j$-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the $j$-th day (i.e. if he solves problems with indices from $l$ to $r$ during a day, then the profit of the day is $\max\limits_{l \le i \le r}a_i$). The total profit of his practice is the sum of the profits over all $k$ days of his practice.
     26 
     27 You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all $n$ problems between $k$ days satisfying the conditions above in such a way, that the total profit is maximum.
     28 
     29 For example, if $n = 8, k = 3$ and $a = [5, 4, 2, 6, 5, 1, 9, 2]$, one of the possible distributions with maximum total profit is: $[5, 4, 2], [6, 5], [1, 9, 2]$. Here the total profit equals $5 + 6 + 9 = 20$.
     30 
     31 Input
     32 
     33 The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the number of problems and the number of days, respectively.
     34 
     35 The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2000$) — difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
     36 
     37 Output
     38 
     39 In the first line of the output print the maximum possible total profit.
     40 
     41 In the second line print exactly $k$ positive integers $t_1, t_2, \dots, t_k$ ($t_1 + t_2 + \dots + t_k$ must equal $n$), where $t_j$ means the number of problems Polycarp will solve during the $j$-th day in order to achieve the maximum possible total profit of his practice.
     42 
     43 If there are many possible answers, you may print any of them.
     44 
     45 Examples
     46 
     47 Input
     48 
     49 ```
     50 8 3  
     51 5 4 2 6 5 1 9 2
     52 ```
     53 
     54 Output
     55 
     56 ```
     57 20  
     58 3 2 3
     59 ```
     60 
     61 Input
     62 
     63 ```
     64 5 1  
     65 1 1 1 1 1
     66 ```
     67 
     68 Output
     69 
     70 ```
     71 1  
     72 5
     73 ```
     74 
     75 Input
     76 
     77 ```
     78 4 2  
     79 1 2000 2000 2
     80 ```
     81 
     82 Output
     83 
     84 ```
     85 4000  
     86 2 2
     87 ```
     88 
     89 Note
     90 
     91 The first example is described in the problem statement.
     92 
     93 In the second example there is only one possible distribution.
     94 
     95 In the third example the best answer is to distribute problems in the following way: $[1, 2000], [2000, 2]$. The total profit of this distribution is $2000 + 2000 = 4000$.