use multiset to store largest and smallest elements This problem has subtasks. Given an…
- User
use multiset to store largest and smallest elements
This problem has subtasks.
Given an array 𝐵 B containing 𝑀 M (where 𝑀 M is odd) integers, you have to perform the following operation on 𝐵 B until it consists of only a single element.
Choose distinct indices 𝑖 , i, 𝑗 , j, 𝑘 k of the array 𝐵 B. Remove 𝐵 𝑖 , 𝐵 𝑗 , 𝐵 𝑘 B i ,B j ,B k from the array. Append ( 𝐵 𝑖 + 𝐵 𝑗 − 𝐵 𝑘 ) (B i +B j −B k ) to 𝐵 B. Note that the operation reduces the length of 𝐵 B by 2 2. For example, if 𝐵
[ 2 , 5 , 3 , 4 , 2 ] B=[2,5,3,4,2], you can choose indices ( 3 , 1 , 4 ) (3,1,4), delete the values 𝐵 3 , 𝐵 1 , 𝐵 4 B 3 ,B 1 ,B 4 from 𝐵 B, and append 𝐵 3 + 𝐵 1 − 𝐵 4
3 + 2 − 4
1 B 3 +B 1 −B 4 =3+2−4=1 to it, obtaining 𝐵
[ 5 , 2 , 1 ] B=[5,2,1].
Your aim is to maximize the value of the final element remaining in the array. Let this maximum value be 𝑓 ( 𝐵 ) f(B).
You are given an array 𝐴 A of length 𝑁 N. Answer 𝑄 Q queries on it.
In the 𝑖 i-th query, you are given an integer 𝐾 𝑖 K i , which is guaranteed to be odd. Compute 𝑓 ( [ 𝐴 1 , 𝐴 2 , … , 𝐴 𝐾 𝑖 ] ) f([A 1 ,A 2 ,…,A K i
]). That is, compute the answer when considering only the first 𝐾 𝑖 K i elements of 𝐴 A.
Each query is independent, so the array remains unchanged across queries.
Input Format The first line of input will contain a single integer 𝑇 T, denoting the number of test cases. Each test case consists of three lines of input. The first line of each test case contains two space-separated integers 𝑁 N and 𝑄 Q — the length of the array and the number of queries. The second line contains 𝑁 N space-separated integers 𝐴 1 , 𝐴 2 , … , 𝐴 𝑁 A 1 ,A 2 ,…,A N . The third line contains 𝑄 Q space-separated integers 𝐾 1 , 𝐾 2 , … , 𝐾 𝑄 K 1 ,K 2 ,…,K Q . Output Format For each test case, output 𝑄 Q space-separated numbers: the answers for each query in order.
Constraints 1 ≤ 𝑇 ≤ 1 0 5 1≤T≤10 5
1 ≤ 𝑁 ≤ 2 ⋅ 1 0 5 1≤N≤2⋅10 5
1 ≤ 𝑄 ≤ ⌈ 𝑁 2 ⌉ 1≤Q≤⌈ 2 N ⌉ − 1 0 9 ≤ 𝐴 𝑖 ≤ 1 0 9 −10 9 ≤A i ≤10 9 for each 1 ≤ 𝑖 ≤ 𝑁 1≤i≤N 1 ≤ 𝐾 𝑖 ≤ 𝑁 1≤K i ≤N for each 1 ≤ 𝑖 ≤ 𝑄 1≤i≤Q 𝐾 𝑖 K i is odd The sum of 𝑁 N over all test cases won't exceed 2 ⋅ 1 0 5 2⋅10 5
Subtasks Subtask 1 (30 points): 1 ≤ 𝑄 ≤ 5 1≤Q≤5. Subtask 2 (70 points): No further constraints. Sample 1: Input Output 1 5 3 3 -1 2 4 5 3 1 5 6 3 11 Explanation: Query 1 1: The array to consider is [ 3 , − 1 , 2 ] [3,−1,2]. It's optimal to choose the triple of indices ( 1 , 3 , 2 ) (1,3,2), which turns the array into [ 6 ] [6] (since 𝐴 1 + 𝐴 3 − 𝐴 2
3 + 2 − ( − 1 )
6 A 1 +A 3 −A 2 =3+2−(−1)=6). This is optimal.
Query 2 2: The array to consider is [ 3 ] [3]. No operations can be performed, and the answer is just 3 3.
- Assistant