CF1630D.Flipping Range

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an array aa of nn integers and a set BB of mm positive integers such that 1≤bi≤⌊n2⌋1 \leq b_i \leq \lfloor \frac{n}{2} \rfloor for 1≤i≤m1\le i\le m, where bib_i is the ii-th element of BB.

You can make the following operation on aa:

  1. Select some xx such that xx appears in BB.
  2. Select an interval from array aa of size xx and multiply by −1-1 every element in the interval. Formally, select ll and rr such that 1≤l≤r≤n1\leq l\leq r \leq n and r−l+1=xr-l+1=x, then assign ai:=−aia_i:=-a_i for every ii such that l≤i≤rl\leq i\leq r.

Consider the following example, let a=[0,6,−2,1,−4,5]a=[0,6,-2,1,-4,5] and B=1,2B={1,2}:

  1. [0,6,−2,−1,4,5][0,6,-2,-1,4,5] is obtained after choosing size 22 and l=4l=4, r=5r=5.
  2. [0,6,2,−1,4,5][0,6,2,-1,4,5] is obtained after choosing size 11 and l=3l=3, r=3r=3.

Find the maximum ∑i=1nai\sum\limits_{i=1}^n {a_i} you can get after applying such operation any number of times (possibly zero).

给你一个包含 nn 个整数的数组 aa,以及一个包含 mm 个正整数的集合 BB,满足对所有 1≤i≤m1 \le i \le m,有 1≤bi≤⌊n2⌋1 \leq b_i \leq \lfloor \frac{n}{2} \rfloor,其中 bib_i 表示集合 BB 中的第 ii 个元素。

你可以在数组 aa 上执行如下操作:

  1. 选择某个 xx,使得 xx 属于集合 BB;
  2. 在数组 aa 中选取一个长度为 xx 的连续子区间,并将该区间内每个元素乘以 −1-1。形式化地说,选择下标 ll 和 rr,满足 1≤l≤r≤n1 \leq l \leq r \leq n 且 r−l+1=xr - l + 1 = x,然后对所有满足 l≤i≤rl \leq i \leq r 的 ii,令 ai:=−aia_i := -a_i。

考虑如下示例:设 a=[0,6,−2,1,−4,5]a = [0,6,-2,1,-4,5],B={1,2}B = \{1,2\}:

  1. 若选择长度 x=2x = 2,并取 l=4l = 4、r=5r = 5,则得到 [0,6,−2,−1,4,5][0,6,-2,-1,4,5];
  2. 若选择长度 x=1x = 1,并取 l=3l = 3、r=3r = 3,则得到 [0,6,2,−1,4,5][0,6,2,-1,4,5]。

求在执行上述操作任意次数(包括零次)后,所能达到的最大值 ∑i=1nai\sum\limits_{i=1}^n {a_i}。

输入格式

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1051 \le t \le 10^5) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers nn and mm (2≤n≤1062\leq n \leq 10^6, 1≤m≤⌊n2⌋1 \leq m \leq \lfloor \frac{n}{2} \rfloor) — the number of elements of aa and BB respectively.

The second line of each test case contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (−109≤ai≤109-10^9\leq a_i \leq 10^9).

The third line of each test case contains mm distinct positive integers b1,b2,…,bmb_1,b_2,\ldots,b_m (1≤bi≤⌊n2⌋1 \leq b_i \leq \lfloor \frac{n}{2} \rfloor) — the elements in the set BB.

It's guaranteed that the sum of nn over all test cases does not exceed 10610^6.

输入包含多个测试用例。第一行包含一个整数 tt(1≤t≤1051 \le t \le 10^5),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(2≤n≤1062\leq n \leq 10^6,1≤m≤⌊n2⌋1 \leq m \leq \lfloor \frac{n}{2} \rfloor),分别表示数组 aa 和集合 BB 的元素个数。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(−109≤ai≤109-10^9\leq a_i \leq 10^9)。

每个测试用例的第三行包含 mm 个互不相同的正整数 b1,b2,…,bmb_1,b_2,\ldots,b_m(1≤bi≤⌊n2⌋1 \leq b_i \leq \lfloor \frac{n}{2} \rfloor),即集合 BB 中的元素。

保证所有测试用例的 nn 值之和不超过 10610^6。

输出格式

For each test case print a single integer — the maximum possible sum of all aia_i after applying such operation any number of times.

对于每个测试用例,输出一个整数——在任意次数执行该操作后,所有 aia_i 的和的最大可能值。

输入输出样例

  • 输入#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=1x=1, l=3l=3, r=3r=3, and the operation x=1x=1, l=5l=5, r=5r=5, then the array becomes [0,6,2,1,4,5][0, 6, 2, 1, 4, 5].

In the second test, you can apply the operation x=2x=2, l=2l=2, r=3r=3, and the array becomes [1,1,−1,−1,1,−1,1][1, 1, -1, -1, 1, -1, 1], then apply the operation x=2x=2, l=3l=3, r=4r=4, and the array becomes [1,1,1,1,1,−1,1][1, 1, 1, 1, 1, -1, 1]. There is no way to achieve a sum bigger than 55.

在第一个测试用例中,你可以执行操作 x=1x=1、l=3l=3、r=3r=3,以及操作 x=1x=1、l=5l=5、r=5r=5,此时数组变为 [0,6,2,1,4,5][0, 6, 2, 1, 4, 5]。

在第二个测试用例中,你可以先执行操作 x=2x=2、l=2l=2、r=3r=3,此时数组变为 [1,1,−1,−1,1,−1,1][1, 1, -1, -1, 1, -1, 1];再执行操作 x=2x=2、l=3l=3、r=4r=4,此时数组变为 [1,1,1,1,1,−1,1][1, 1, 1, 1, 1, -1, 1]。不存在使数组元素和大于 55 的方法。

输入解题思路,AI测评打分。不知道怎么写?

首页