Source Code
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
void Fast(){
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
}
void File() {
#ifndef ONLINE_JUDGE
    freopen("Input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
template<class T> using ordered_set = tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update>;
typedef long long ll;
#define watch(x) cout << (#x) << " = " << x << '\n'
#define endl '\n'
#define all(a)  a.begin(), a.end()
#define fix(n) cout << fixed << setprecision(n)
#define skip continue
const long double pi = acos(-1);
const int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
const int dy[] = { 0, 1, 0, -1, 1, -1, 1, -1 };

int main(){
    Fast();
    int t;cin >> t;
    while(t--){
        int n , k;cin >> n >> k;
        vector<int>v(n+1) , left(n+1) , right(n+3);
        for(int i = 1 ; i <= n;i++){
            cin >> v[i];
            left[i] = left[i-1] + v[i];
        }
        for(int i = n ; i >= 1;i--){
            right[i] += v[i] + right[i+1];
        }
        int ans1 = left[k];
        int idx = 1;
        int sum = 0;
        for(int i = n ; i >= 1;i--){
            sum += v[i];
            int xx = (idx ^ k);
            if(xx + idx <= n){
                ans1 = max(ans1 , sum + left[xx]);
            }
            idx++;
        }
        int ans2 = right[n - k + 1];
        int idx2 = 1;
        int sum2 = 0;
        for(int i = 1; i <= n;i++){
            sum2 += v[i];
            int xx = (idx2 ^ k);
            if(xx + idx2 <= n){
                ans2 = max(ans2 , sum2 + right[n - xx + 1]);
            }
            idx2++;
        }
        cout << max(ans1 , ans2) << endl;
    }
}
Copy
Midterms Muhamed_Morsi
GNU G++17
16 ms
3.2 MB
Accepted