Language: C++

// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 07/22/20 22:03:56
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  int n,q; cin>>n>>q;
  vector<int> A(n); for (auto& a:A) cin>>a;
  vector<ll> cr(n), c2l(n);
  for (int i = 0; i < n; i++) {
    cr[i] = c2l[i] = A[i];
    if (i > 1)
      c2l[i] += c2l[i-2];
  }
  for (int i = n-2; i >= 0; i--)
    cr[i] += cr[i+1];

  while (q--) {
    int x; cin>>x;
    auto dist = [x](int a) {
      return abs(x-a);
    };
    auto find_nearest = [x]() {
      int l = 0, r = n;
      while (l+1<r) {
        auto ml = (l*2+r)/2, mr = (l+r*2)/2;
        if (dist(A[ml]) <= dist(A[mr]))
          r = mr;
        else
          l = ml;
      }
      return (l+r)/2;
    };
    auto c = find_nearest();
    if (c == n) {
      cout << c2l[n-1] << endl;
    } else {
      int t = c+(n-c)/2;
      int l = max(c+1, n/2), r = t;
//      cerr<<c<<" "<<l<<" "<<r<<endl;
      while (l+1<r) {
        auto cr = (l+r)/2;
        int cl = c-(t-cr)*2;
        if (cr-cl < n-cr)
          cl--;
        if (cl > 0 and dist(A[cl-1]) <= dist(cr-1)) {
          r = cr;
        } else if (dist(A[cl] > dist(cr))) {
          l = cr;
        } else {
          break;
        }
      }
      int cl = c-(t-l)*2;
      if (l-cl < n-l)
        cl--;
//      cerr<<cl<<" "<<l<<endl;
      ll ans = cr[l] + (cl > 0 ? c2l[cl-1] : 0);
      cout << ans << endl;
    }
  }

  return 0;
}


Compiler output:
a.cc: In lambda function:
a.cc:32:22: error: 'n' is not captured
   32 |       int l = 0, r = n;
      |                      ^
a.cc:31:27: note: the lambda has no capture-default
   31 |     auto find_nearest = [x]() {
      |                           ^
a.cc:15:7: note: 'int n' declared here
   15 |   int n,q; cin>>n>>q;
      |       ^
a.cc:35:13: error: 'dist' is not captured
   35 |         if (dist(A[ml]) <= dist(A[mr]))
      |             ^~~~
a.cc:31:27: note: the lambda has no capture-default
   31 |     auto find_nearest = [x]() {
      |                           ^
a.cc:28:10: note: 'main()::<lambda(int)> dist' declared here
   28 |     auto dist = [x](int a) {
      |          ^~~~
a.cc:35:18: error: 'A' is not captured
   35 |         if (dist(A[ml]) <= dist(A[mr]))
      |                  ^
a.cc:31:27: note: the lambda has no capture-default
   31 |     auto find_nearest = [x]() {
      |                           ^
a.cc:16:15: note: 'std::vector<int> A' declared here
   16 |   vector<int> A(n); for (auto& a:A) cin>>a;
      |               ^
a.cc:35:28: error: 'dist' is not captured
   35 |         if (dist(A[ml]) <= dist(A[mr]))
      |                            ^~~~
a.cc:31:27: note: the lambda has no capture-default
   31 |     auto find_nearest = [x]() {
      |                           ^
a.cc:28:10: note: 'main()::<lambda(int)> dist' declared here
   28 |     auto dist = [x](int a) {
      |          ^~~~
a.cc:35:33: error: 'A' is not captured
   35 |         if (dist(A[ml]) <= dist(A[mr]))
      |                                 ^
a.cc:31:27: note: the lambda has no capture-default
   31 |     auto find_nearest = [x]() {
      |                           ^
a.cc:16:15: note: 'std::vector<int> A' declared here
   16 |   vector<int> A(n); for (auto& a:A) cin>>a;
      |               ^