fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int A[5001];
  4. long long DP[5001][5001];
  5. int main()
  6. {
  7. ios::sync_with_stdio(0);
  8. cin.tie(0);
  9. int T,i,j;
  10. cin>>T;
  11. for(;T--;)
  12. {
  13. int N,K,i,l;
  14. cin>>N>>K;
  15. for(i=1;i<=N;i++)
  16. cin>>A[i];
  17. for(i=1;i<=K;i++)
  18. DP[0][i]=INT_MIN;
  19.  
  20. for(i=1;i<=N;i++)
  21. for(j=1;j<=K;j++)
  22. DP[i][j]=INT_MIN;
  23.  
  24. for(i=1;i<=N;i++)
  25. {
  26. int temp=0;
  27. for(l=i;l>=1;l--)
  28. {
  29. if(temp|A[l] >temp)
  30. {
  31. temp|=A[l];
  32. for(j=1;j<=min(K,i);j++)
  33. DP[i][j]=max(DP[l-1][j-1]+temp,DP[i][j]);
  34.  
  35. }
  36. }
  37. }
  38. cout<<DP[N][K]<<"\n";
  39. }
  40. return 0;
  41. }
  42.  
Runtime error #stdin #stdout 3.37s 198656KB
stdin
Standard input is empty
stdout
Standard output is empty