fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. long long int sum[55];///stores cummulative sum of array
  4.  
  5. bool isTrue(long long int a,int n,int k)
  6. {
  7. bool isPart[n+1][k+1];
  8. memset(isPart,false,sizeof(isPart));
  9. isPart[0][0]=true;
  10.  
  11. for(int i=1;i<=k;i++)
  12. for(int j=1;j<=n;j++)
  13. for(int x=0;x<j;x++)
  14. isPart[j][i]=isPart[j][i] | (isPart[x][i-1]&&(((sum[j]-sum[x])&a)==a));
  15.  
  16. return isPart[n][k];///isPart[n][k] is true of its possible to make a for size n and k parts
  17. }
  18. int main()
  19. {
  20. long long int n;
  21. cin>>n;
  22.  
  23. long long int arr[n+1];
  24. for(int i=1;i<=n;i++)
  25. cin>>arr[i];
  26.  
  27. ///query
  28. int q;
  29. cin>>q;
  30. while(q--){
  31.  
  32. ///input k
  33. int k;
  34. cin>>k;
  35. long long int toppings[n+1];
  36. for(int i=1;i<=n;i++){
  37. cin>>toppings[i];
  38. }
  39.  
  40. vector<long long int>v;
  41. for(int i=1;i<=n;i++){
  42. if(toppings[i]*arr[i] > 0){
  43. v.push_back(toppings[i]*arr[i]);
  44. }
  45. }
  46. if(k>v.size()) {
  47. cout<<"0"<<endl;
  48. continue;
  49. }
  50. int n2=v.size();
  51.  
  52. long long int arr2[n2+1];
  53. for(int i=0;i<v.size();i++){
  54. arr2[i+1]=v[i];
  55. }
  56. sum[0]=arr2[0];
  57. for(int i=1;i<=n2;i++)
  58. sum[i]=sum[i-1]+arr2[i];
  59. long long int ans=0;///maximum possible answers till now
  60.  
  61. long long int a=1;///intialization
  62.  
  63. for(int i=61;i>=0;i--)///started from 60 so that numbers tested from highest to lowest because me need maximum answer
  64.  
  65. if(isTrue(ans|a<<i,n2,k)) /*istrue(x,n,k) function will tell if is it po
  66.   possible to divide array of "n" size with "k" parts
  67.   such that bitwise and becomes "x" */
  68. ans=ans|(a<<i);
  69.  
  70. cout<<ans<<endl;
  71. }
  72.  
  73. return 0;
  74. }
Runtime error #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty