Thursday, 27 January 2022

Flipping Range Solution|Codeforces round #768 (div 2)

                                     Flipping Range

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array a of n integers and a set B of m positive integers such that 1bin2 for 1im, where bi is the i-th element of B.

You can make the following operation on a:

  1. Select some x such that x appears in B.
  2. 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 1lrn and rl+1=x, then assign ai:=ai for every i such that lir.

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

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

Find the maximum i=1nai you can get after applying such operation any number of times (possibly zero).

Input

The input consists of multiple test cases. The first line contains a single integer t (1t105) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers n and m (2n1061mn2) — the number of elements of a and B respectively.

The second line of each test case contains n integers a1,a2,,an (109ai109).

The third line of each test case contains m distinct positive integers b1,b2,,bm (1bin2) — the elements in the set B.

It's guaranteed that the sum of n over all test cases does not exceed 106.

Output

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

Note

In the first test, you can apply the operation x=1l=3r=3, and the operation x=1l=5r=5, then the array becomes [0,6,2,1,4,5].

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

Solution:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. #define OJ \
  5. freopen("input.txt", "r", stdin); \
  6. freopen("output.txt", "w", stdout);
  7. ll bp(ll a,ll b,ll mod=LONG_LONG_MAX){
  8. a%=mod;
  9. ll res=1;
  10. while(b>0){
  11. if(b&1) res=res*a%mod;
  12. a=a*a%mod;
  13. b/=2;
  14. }
  15. return res;
  16. }
  17.  
  18. int c(int x,int n){
  19. return x^(n-1);
  20. }
  21.  
  22. void solve(){
  23. int n,k;
  24. cin>>n>>k;
  25. vector<int> a(n/2),b(n/2);
  26. if(k==0){
  27. for(int i=0;i<n/2;i++){
  28. a[i]=i,b[i]=c(i,n);
  29. }
  30. }
  31. else if(k>0 && k<n-1){
  32. // k & n-1 0 & c(k)
  33. for(int i=0;i<n/2;i++){
  34. if(i==0) a[i]=0,b[i]=c(k,n);
  35. else if(i==k || i==c(k,n)) a[i]=k,b[i]=n-1;
  36. else a[i]=i,b[i]=c(i,n);
  37. }
  38. }
  39. else if(n==4){
  40. cout<<-1<<"\n";
  41. return;
  42. }
  43. else {
  44. a[0]=n-1,b[0]=n-2;
  45. a[1]=n-3,b[1]=1;
  46. a[2]=0,b[2]=2;
  47. for(int i=3;i<n/2;i++){
  48. a[i]=i,b[i]=c(i,n);
  49. }
  50. }
  51. for(int i=0;i<n/2;i++) cout<<a[i]<<" "<<b[i]<<"\n";
  52. }
  53.  
  54. int main(){
  55. cin.tie(0);ios_base::sync_with_stdio(0);
  56. int ttt=1;
  57. cin>>ttt;
  58. while(ttt--){
  59. solve();
  60. }
  61. }
  62.