CF BUDDY
← Problems·

1917C · Watering an Array

1600 · brute force, greedy, implementation

Problem: You have an array of integers a1,a2,,ana_1, a_2, \ldots, a_n of length nn. On the ii-th of the next dd days you are going to do exactly one of the following two actions:

  • Add 11 to each of the first bib_i elements of the array aa (i.e., set aj:=aj+1a_j := a_j + 1 for each 1jbi1 \le j \le b_i).
  • Count the elements which are equal to their position (i.e., the aj=ja_j = j). Denote the number of such elements as cc. Then, you add cc to your score, and reset the entire array aa to a 00-array of length nn (i.e., set [a1,a2,,an]:=[0,0,,0][a_1, a_2, \ldots, a_n] := [0, 0, \ldots, 0]).

Your score is equal to 00 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.

What is the maximum score you can achieve at the end?

Since dd can be quite large, the sequence bb is given to you in the compressed format:

  • You are given a sequence of integers v1,v2,,vkv_1, v_2, \ldots, v_k. The sequence bb is a concatenation of infinitely many copies of vv: b=[v1,v2,,vk,v1,v2,,vk,]b = [v_1, v_2, \ldots, v_k, v_1, v_2, \ldots, v_k, \ldots].

Input Format: The first line contains a single integer tt (1t1031 \le t \le 10^3) — the number of test cases.

The first line of each test case contains three integers nn, kk and dd (1n20001 \le n \le 2000, 1k1051 \le k \le 10^5, kd109k \le d \le 10^9) — the length of the array aa, the length of the sequence vv and the number of days you are going to perform operations on.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (0ain0 \le a_i \le n) — the array aa.

The third line of each test case contains kk integers v1,v2,,vkv_1, v_2, \ldots, v_k (1vin1 \le v_i \le n) — the sequence vv.

It is guaranteed that the sum of nn over all test cases doesn't exceed 20002000 and the sum of kk over all test cases doesn't exceed 10510^5.

Output Format: For each test case, output one integer: the maximum score you can achieve at the end of the dd-th day.

Note: In the first test case, the sequence bb is equal to [1,3,2,3,1,3,2,3,][1, 3, 2, 3, 1, 3, 2, 3, \ldots] and one of the optimal solutions for this case is as follows:

  • Perform the operation of the second type on the 11-st day: your score increases by 33 and array aa becomes equal to [0,0,0][0, 0, 0].
  • Perform the operation of the first type on the 22-nd day: array aa becomes equal to [1,1,1][1, 1, 1].
  • Perform the operation of the first type on the 33-rd day: array aa becomes equal to [2,2,1][2, 2, 1].
  • Perform the operation of the second type on the 44-th day: your score increases by 11 and array aa becomes equal to [0,0,0][0, 0, 0].

It can be shown that it is impossible to score more than 44, so the answer is 44.

In the second test case, the sequence bb is equal to [6,6,6,6,][6, 6, 6, 6, \ldots]. One of the ways to score 33 is to perform operations of the first type on the 11-st and the 33-rd days and to perform an operation of the second type on the 22-nd day.

Sample Cases

Case 1

Input

5
3 4 4
1 2 3
1 3 2 3
6 2 3
6 1 2 4 1 5
6 6
5 1 1
0 5 0 5 0
5
1 1 1
1
1
3 4 6
1 2 3
1 3 2 3

Output

4
3
0
1
5

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback