Language: C++

#include<algorithm>
#include<cmath>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<random>
#include<set>
#include<sstream>
#include<sys/time.h>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include"rprint2.hpp"
#else
#define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
#endif
template <class S, class T>
istream& operator >> (istream& in, pair<S, T>& p){ return in >> p.first >> p.second; }
template <class T>
istream& operator >> (istream& in, vector<T>& v){ for(auto& e : v){ in >> e; } return in; }

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    vector<int> as(n); cin >> as;
    vector<ll> accs(n + 1), accs2(n + 1);
    for(int i = n - 1; i >= 0; i--){
        accs[i] = accs[i + 1] + as[i];
    }
    for(int i = 0; i < n; i++){
        accs2[i] = (i >= 2 ? accs2[i - 2] : 0) + as[i];
    }
    printde(as, accs, accs2);
    for(int i = 0; i < q; i++){
        int x; cin >> x;
        int tin = 1, tout = n - 1;
        ll ans = 0;
        auto proc = [&](int& tin, int& tout){ 
            int num = (tin + tout) / 2;
            int diff = as[n - num - 1] - x;
            int diff2 = as[n - num] - x;
            auto idx = lower_bound(as.begin(), as.end(), x - diff) - as.begin();
            auto idx2 = lower_bound(as.begin(), as.end(), x - diff2) - as.begin();
            // ---- idx2 ---- idx ---
            int numa = n - num - 1 - idx + 1;
            int numb = n - num - 1 - idx2 + 1;
            if(num < numa){
                tin = num;
            }else if(num > numb + 1){
                tout = num;
            }else{
                int idx = n - num - num - 1;
                ll cand = accs[n - num] + (idx >= 0 ? accs2[idx] : 0);
                printde(num, cand, accs[n - num], (idx >= 0 ? accs2[idx] : 0), ans);
                ans = max(ans, cand);
                tin = max(1, num - 10);
                tout = min(n - 1, num + 10);
            }
        };
        while(tin + 20 < tout){
            proc(tin, tout);
        }
        for(int i = tin; i <= tout; i++){
            int tin = i, tout = i;
            proc(tin, tout);
        }
        if(ans == 0){
            exit(1);
        }
        cout << ans << '\n';
    }
}
#include<algorithm>
#include<cmath>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<random>
#include<set>
#include<sstream>
#include<sys/time.h>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include"rprint2.hpp"
#else
#define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
#endif
template <class S, class T>
istream& operator >> (istream& in, pair<S, T>& p){ return in >> p.first >> p.second; }
template <class T>
istream& operator >> (istream& in, vector<T>& v){ for(auto& e : v){ in >> e; } return in; }

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    vector<int> as(n); cin >> as;
    vector<ll> accs(n + 1), accs2(n + 1);
    for(int i = n - 1; i >= 0; i--){
        accs[i] = accs[i + 1] + as[i];
    }
    for(int i = 0; i < n; i++){
        accs2[i] = (i >= 2 ? accs2[i - 2] : 0) + as[i];
    }
    printde(as, accs, accs2);
    for(int i = 0; i < q; i++){
        int x; cin >> x;
        int tin = 1, tout = n - 1;
        ll ans = 0;
        auto proc = [&](int& tin, int& tout){ 
            int num = (tin + tout) / 2;
            int diff = as[n - num - 1] - x;
            int diff2 = as[n - num] - x;
            auto idx = lower_bound(as.begin(), as.end(), x - diff) - as.begin();
            auto idx2 = lower_bound(as.begin(), as.end(), x - diff2) - as.begin();
            // ---- idx2 ---- idx ---
            int numa = n - num - 1 - idx + 1;
            int numb = n - num - 1 - idx2 + 1;
            if(num < numa){
                tin = num;
            }else if(num > numb + 1){
                tout = num;
            }else{
                int idx = n - num - num - 1;
                ll cand = accs[n - num] + (idx >= 0 ? accs2[idx] : 0);
                printde(num, cand, accs[n - num], (idx >= 0 ? accs2[idx] : 0), ans);
                ans = max(ans, cand);
                tin = max(1, num - 10);
                tout = min(n - 1, num + 10);
            }
        };
        while(tin + 20 < tout){
            proc(tin, tout);
        }
        for(int i = tin; i <= tout; i++){
            int tin = i, tout = i;
            proc(tin, tout);
        }
        if(ans == 0){
            exit(1);
        }
        cout << ans << '\n';
    }
}


Compiler output:
a.cc:100:29: error: 'M' conflicts with a previous declaration
  100 | enum : int { M = (int)1e9 + 7 };
      |                             ^
a.cc:17:14: note: previous declaration '<unnamed enum> M'
   17 | enum : int { M = (int)1e9 + 7 };
      |              ^
a.cc:101:31: error: 'MLL' conflicts with a previous declaration
  101 | enum : ll { MLL = (ll)1e18L + 9 };
      |                               ^
a.cc:18:13: note: previous declaration '<unnamed enum> MLL'
   18 | enum : ll { MLL = (ll)1e18L + 9 };
      |             ^~~
a.cc:107:6: error: redefinition of 'template<std::ostream& out, class ... T> void prints(T&& ...)'
  107 | FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
      |      ^~~~~~
a.cc:106:68: note: in definition of macro 'FUNC'
  106 | #define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
      |                                                                    ^~~~
a.cc:24:6: note: 'template<std::ostream& out, class ... T> void prints(T&& ...)' previously declared here
   24 | FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
      |      ^~~~~~
a.cc:23:68: note: in definition of macro 'FUNC'
   23 | #define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
      |                                                                    ^~~~
a.cc:107:19: error: redefinition of 'template<std::ostream& out, class ... T> void printw(T&& ...)'
  107 | FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
      |                   ^~~~~~
a.cc:106:68: note: in definition of macro 'FUNC'
  106 | #define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
      |                                                                    ^~~~
a.cc:24:19: note: 'template<std::ostream& out, class ... T> void printw(T&& ...)' previously declared here
   24 | FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
      |                   ^~~~~~
a.cc:23:68: note: in definition of macro 'FUNC'
   23 | #define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
      |                                                                    ^~~~
a.cc:107:32: error: redefinition of 'template<std::ostream& out, class ... T> void printwe(T&& ...)'
  107 | FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
      |                                ^~~~~~~
a.cc:106:68: note: in definition of macro 'FUNC'
  106 | #define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
      |                                                                    ^~~~
a.cc:24:32: note: 'template<std::ostream& out, class ... T> void printwe(T&& ...)' previously declared here
   24 | FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
      |                                ^~~~~~~
a.cc:23:68: note: in definition of macro 'FUNC'
   23 | #define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
      |                                                                    ^~~~
a.cc:107:46: error: redefinition of 'template<std::ostream& out, class ... T> void printb(T&& ...)'
  107 | FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
      |                                              ^~~~~~
a.cc:106:68: note: in definition of macro 'FUNC'
  106 | #define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
      |                                                                    ^~~~
a.cc:24:46: note: 'template<std::ostream& out, class ... T> void printb(T&& ...)' previously declared here
   24 | FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
      |                                              ^~~~~~
a.cc:23:68: note: in definition of macro 'FUNC'
   23 | #define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
      |                                                                    ^~~~
a.cc:107:59: error: redefinition of 'template<std::ostream& out, class ... T> void printbe(T&& ...)'
  107 | FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
      |                                                           ^~~~~~~
a.cc:106:68: note: in definition of macro 'FUNC'
  106 | #define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
      |                                                                    ^~~~
a.cc:24:59: note: 'template<std::ostream& out, class ... T> void printbe(T&& ...)' previously declared here
   24 | FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
      |                                                           ^~~~~~~
a.cc:23:68: note: in definition of macro 'FUNC'
   23 | #define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
      |                                                                    ^~~~
a.cc:107:73: error: redefinition of 'template<std::ostream& out, class ... T> void printd(T&& ...)'
  107 | FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
      |                                                                         ^~~~~~
a.cc:106:68: note: in definition of macro 'FUNC'
  106 | #define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
      |                                                                    ^~~~
a.cc:24:73: note: 'template<std::ostream& out, class ... T> void printd(T&& ...)' previously declared here
   24 | FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
      |                                                                         ^~~~~~
a.cc:23:68: note: in definition of macro 'FUNC'
   23 | #define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
      |                                                                    ^~~~
a.cc:107:86: error: redefinition of 'template<std::ostream& out, class ... T> void printde(T&& ...)'
  107 | FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
      |                                                                                      ^~~~~~~
a.cc:106:68: note: in definition of macro 'FUNC'
  106 | #define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
      |                                                                    ^~~~
a.cc:24:86: note: 'template<std::ostream& out, class ... T> void printde(T&& ...)' previously declared here
   24 | FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
      |                                                                                      ^~~~~~~
a.cc:23:68: note: in definition of macro 'FUNC'
   23 | #define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
      |                                                                    ^~~~
a.cc:107:100: error: redefinition of 'template<std::ostream& out, class ... T> void printdu(T&& ...)'
  107 | FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
      |                                                                                                    ^~~~~~~
a.cc:106:68: note: in definition of macro 'FUNC'
  106 | #define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
      |                                                                    ^~~~
a.cc:24:100: note: 'template<std::ostream& out, class ... T> void printdu(T&& ...)' previously declared here
   24 | FUNC(prints) FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde) FUNC(printdu);
      |                                                                                                    ^~~~~~~
a.cc:23:68: note: in definition of macro 'FUNC'
   23 | #define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
      |                                                                    ^~~~
a.cc:110:10: error: redefinition of 'template<class S, class T> std::istream& operator>>(std::istream&, std::pair<_T1, _T2>&)'
  110 | istream& operator >> (istream& in, pair<S, T>& p){ return in >> p.first >> p.second; }
      |          ^~~~~~~~
a.cc:27:10: note: 'template<class S, class T> std::istream& operator>>(std::istream&, std::pair<_T1, _T2>&)' previously declared here
   27 | istream& operator >> (istream& in, pair<S, T>& p){ return in >> p.first >> p.second; }
      |          ^~~~~~~~
a.cc:112:10: error: redefinition of 'template<class T> std::istream& operator>>(std::istream&, std::vector<_Tp>&)'
  112 | istream& operator >> (istream& in, vector<T>& v){ for(auto& e : v){ in >> e; } return in; }
      |          ^~~~~~~~
a.cc:29:10: note: 'template<class T> std::istream& operator>>(std::istream&, std::vector<_Tp>&)' previously declared here
   29 | istream& operator >> (istream& in, vector<T>& v){ for(auto& e : v){ in >> e; } return in; }
      |          ^~~~~~~~
a.cc:114:5: error: redefinition of 'int main()'
  114 | int main(){
      |     ^~~~
a.cc:31:5: note: 'int main()' previously defined here
   31 | int main(){
      |     ^~~~