Language: C++

#include<iostream>
#include<cmath>
#define D(x) cout << #x" = " << x << endl;
#define DD(x, y) cout << "("#x", "#y") = (" << x << ", " << y << ")" << endl;

using namespace std;
using P = pair<int, int>;
using ll = long long;
const int XMAX = 1000000000;

int n, q;
int a[100000];
ll tailsum[100000];
ll dubsum[100000];
// left can be equal to x
// right greater than x
P adj(int x) {
    int right = distance(a, upper_bound(a, a + n, x));
    return make_pair(right - 1, right);
}

P neighbor(int x, int k) {
    int mx = XMAX;
    int mn = 0;
    int mid;
    while (mx > mn) {
        mid = (mn + mx + 1) / 2;
        int right = adj(x + mid).first;
        int left = adj(x - mid - 1).second;
        int cnt = right - left + 1;
        // cout << mid << " = " << cnt << " - l " << left << " r " << right << endl;
        if (cnt == k) {
            return make_pair(left, right);
        } else if (cnt == k + 1){
            if (a[right] == x + mid && a[left] == x - mid) {
                return make_pair(left, right - 1);
            }
        }

        if (cnt > k) {
            mx = mid - 1;
        } else {
            mn = mid;
        }
    }
    assert(false);
    return make_pair(0, 0);
}

int main() {
    cin >> n >> q;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    tailsum[n - 1] = a[n - 1];
    dubsum[0] = a[0];
    dubsum[1] = a[1];
    for (int i = n - 2; i >= 0; i--) {
        tailsum[i] = tailsum[i + 1] + a[i];
    }
    for (int i = 2; i < n; ++i) {
        dubsum[i] = dubsum[i - 2] + a[i];
    }
    for (int i = 0; i < q; ++i) {
        int xi;
        cin >> xi;
        // for (int i = 1; i <= n; ++i) {
        //     cout << "take " << i << " from " << xi << endl;
        //     P res = neighbor(xi, i);
        //     cout << "  " << res.first << "(" << a[res.first] << ") -> " << res.second << "(" << a[res.second] << ")" << endl;
        // }
        // continue;

        int kmin = 0;
        int kmax = n / 2 + 2;
        int kmid;
        int kfound = 0;
        bool kmidodd = false;
        P resfound;

        if (neighbor(xi, 1).first == n - 1) {
            resfound = make_pair(n, n);
        } else {
        while(true) {
            kmid = (kmin + kmax + 1) / 2;
            // cout << kmax << ":" << kmin << endl;
            // cout << "take " << kmid << " from " << xi << endl;
            if (kmid == 0) {
                kfound = 0;
                resfound = make_pair(n, n);
            }
            P res = neighbor(xi, kmid);
            int rest = n - res.second - 1;
            // cout << res.first << ", " <<  res.second << endl;

            // cout << "rest = " << rest << endl;
            if (rest == kmid) {
                kfound = kmid;
                resfound = res;
                break;
            }
            if (rest == kmid + 1) {
                kfound = kmid + 1;
                resfound = res;
                kmidodd = true;
                break;
            }
            if (rest > kmid) {
                kmin = kmid;
            } else {
                kmax = kmid - 1;
            }
        }
        }
        // cout << xi << endl;
        // cout << kfound << endl;
        // cout << resfound.first << " -> " << resfound.second << endl;
        ll ans = 0;
        if (kfound != 0) {
            ans += tailsum[n - kfound];
        }
        int tmp = resfound.first - 1;
        if (kmidodd) {tmp--;}
        if (tmp >= 0) {
            ans += dubsum[tmp];
        }

        cout << ans << endl;
    }
}


Compiler output:
a.cc: In function 'P adj(int)':
a.cc:18:29: error: 'upper_bound' was not declared in this scope
   18 |     int right = distance(a, upper_bound(a, a + n, x));
      |                             ^~~~~~~~~~~
a.cc: In function 'P neighbor(int, int)':
a.cc:46:5: error: 'assert' was not declared in this scope
   46 |     assert(false);
      |     ^~~~~~
a.cc:3:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
    2 | #include<cmath>
  +++ |+#include <cassert>
    3 | #define D(x) cout << #x" = " << x << endl;