Source Code
#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t;
    cin >> t;
    while(t--){
        int n , k;
        cin >> n >> k;
        int arr[n];
        for(int i = 0; i < n; i++)
            cin >> arr[i];
        long long pre[n + 1] = {0};
        for(int i = 0; i < n; i++)
            pre[i + 1] = pre[i] + arr[i];
        long long sum = 0;
        for(int i = 0; i <= n; i++){
            int y = k ^ i;
            if(i + y <= n)
            sum = max(sum , pre[i] + pre[n] - pre[n - y]);
        }
        cout << sum << "\n";
    }
    return 0;
}
Copy
Midterms Mohamedmaher
GNU G++17
21 ms
3.4 MB
Accepted