1917C.md (3276B)
1 Source: https://codeforces.com/problemset/problem/1917/C 2 3 C. Watering an Array 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 have an array of integers $a_1, a_2, \ldots, a_n$ of length $n$. On the $i$-th of the next $d$ days you are going to do exactly one of the following two actions: 22 23 * Add $1$ to each of the first $b_i$ elements of the array $a$ (i.e., set $a_j := a_j + 1$ for each $1 \le j \le b_i$). 24 * Count the elements which are equal to their position (i.e., the $a_j = j$). Denote the number of such elements as $c$. Then, you add $c$ to your score, and reset the entire array $a$ to a $0$-array of length $n$ (i.e., set $[a_1, a_2, \ldots, a_n] := [0, 0, \ldots, 0]$). 25 26 Your score is equal to $0$ in the beginning. Note that on each day you should perform exactly one of the actions above: you cannot skip a day or perform both actions on the same day. 27 28 What is the maximum score you can achieve at the end? 29 30 Since $d$ can be quite large, the sequence $b$ is given to you in the compressed format: 31 32 * You are given a sequence of integers $v_1, v_2, \ldots, v_k$. The sequence $b$ is a concatenation of infinitely many copies of $v$: $b = [v_1, v_2, \ldots, v_k, v_1, v_2, \ldots, v_k, \ldots]$. 33 34 Input 35 36 The first line contains a single integer $t$ ($1 \le t \le 10^3$) — the number of test cases. 37 38 The first line of each test case contains three integers $n$, $k$ and $d$ ($1 \le n \le 2000$, $1 \le k \le 10^5$, $k \le d \le 10^9$) — the length of the array $a$, the length of the sequence $v$ and the number of days you are going to perform operations on. 39 40 The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le n$) — the array $a$. 41 42 The third line of each test case contains $k$ integers $v_1, v_2, \ldots, v_k$ ($1 \le v_i \le n$) — the sequence $v$. 43 44 It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2000$ and the sum of $k$ over all test cases doesn't exceed $10^5$. 45 46 Output 47 48 For each test case, output one integer: the maximum score you can achieve at the end of the $d$-th day. 49 50 Example 51 52 Input 53 54 ``` 55 5 56 57 3 4 4 58 59 1 2 3 60 61 1 3 2 3 62 63 6 2 3 64 65 6 1 2 4 1 5 66 67 6 6 68 69 5 1 1 70 71 0 5 0 5 0 72 73 5 74 75 1 1 1 76 77 1 78 79 1 80 81 3 4 6 82 83 1 2 3 84 85 1 3 2 3 86 ``` 87 88 Output 89 90 ``` 91 4 92 3 93 0 94 1 95 5 96 ``` 97 98 Note 99 100 In the first test case, the sequence $b$ is equal to $[1, 3, 2, 3, 1, 3, 2, 3, \ldots]$ and one of the optimal solutions for this case is as follows: 101 102 * Perform the operation of the second type on the $1$-st day: your score increases by $3$ and array $a$ becomes equal to $[0, 0, 0]$. 103 * Perform the operation of the first type on the $2$-nd day: array $a$ becomes equal to $[1, 1, 1]$. 104 * Perform the operation of the first type on the $3$-rd day: array $a$ becomes equal to $[2, 2, 1]$. 105 * Perform the operation of the second type on the $4$-th day: your score increases by $1$ and array $a$ becomes equal to $[0, 0, 0]$. 106 107 It can be shown that it is impossible to score more than $4$, so the answer is $4$. 108 109 In the second test case, the sequence $b$ is equal to $[6, 6, 6, 6, \ldots]$. One of the ways to score $3$ is to perform operations of the first type on the $1$-st and the $3$-rd days and to perform an operation of the second type on the $2$-nd day.