CF1630D.Flipping Range
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an array a of n integers and a set B of m positive integers such that 1≤bi≤⌊2n⌋ for 1≤i≤m , where bi is the i -th element of B .
You can make the following operation on a :
- Select some x such that x appears in B .
- Select an interval from array a of size x and multiply by −1 every element in the interval. Formally, select l and r such that 1≤l≤r≤n and r−l+1=x , then assign ai:=−ai for every i such that l≤i≤r .
Consider the following example, let a=[0,6,−2,1,−4,5] and B={1,2} :
- [0,6,−2,−1,4,5] is obtained after choosing size 2 and l=4 , r=5 .
- [0,6,2,−1,4,5] is obtained after choosing size 1 and l=3 , r=3 .
Find the maximum i=1∑nai you can get after applying such operation any number of times (possibly zero).
输入格式
The input consists of multiple test cases. The first line contains a single integer t ( 1≤t≤105 ) — the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers n and m ( 2≤n≤106 , 1≤m≤⌊2n⌋ ) — the number of elements of a and B respectively.
The second line of each test case contains n integers a1,a2,…,an ( −109≤ai≤109 ).
The third line of each test case contains m distinct positive integers b1,b2,…,bm ( 1≤bi≤⌊2n⌋ ) — the elements in the set B .
It's guaranteed that the sum of n over all test cases does not exceed 106 .
输出格式
For each test case print a single integer — the maximum possible sum of all ai after applying such operation any number of times.
输入输出样例
输入#1
3 6 2 0 6 -2 1 -4 5 1 2 7 1 1 -1 1 -1 1 -1 1 2 5 1 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 1
输出#1
18 5 5000000000
说明/提示
In the first test, you can apply the operation x=1 , l=3 , r=3 , and the operation x=1 , l=5 , r=5 , then the array becomes [0,6,2,1,4,5] .
In the second test, you can apply the operation x=2 , l=2 , r=3 , and the array becomes [1,1,−1,−1,1,−1,1] , then apply the operation x=2 , l=3 , r=4 , and the array becomes [1,1,1,1,1,−1,1] . There is no way to achieve a sum bigger than 5 .