Language: C++

#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#else
#define TEST(n) ((void)0)
#endif

using namespace std;

vector<int> A, PS, PS_odd, PS_even;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt", "r", stdin));
	TEST(freopen("output.txt", "w", stdout));
	TEST(freopen("debug.txt", "w", stderr));
	int N, Q, X;
	cin >> N >> Q;
	A.resize(N + 1);
	PS.resize(N + 1);
	PS_odd.resize(N + 1);
	PS_even.resize(N + 1);
	for (int i = 1; i <= N; i++) {
		cin >> A[i];
		PS[i] = PS[i - 1] + A[i];
		PS_odd[i] = PS_odd[i - 1] + ((i & 1) ? A[i] : 0);
		PS_even[i] = PS_even[i - 1] + ((i & 1) ? 0 : A[i]);
	}
	while (Q--) {
		int s = 0, e = 1e9, t1, t2;
		long long ans = 0;
		cin >> X;
		while (s <= e) {
			int m = (s + e) >> 1;
			t1 = max(lower_bound(A.begin(), A.end(), X - m) - A.begin(), 1);
			t2 = upper_bound(A.begin(), A.end(), X + m) - A.begin();
			if (t2 - t1 >= N + 1 - t2) {
				e = m - 1;
			}
			else {
				s = m + 1;
			}
		}
		t1 = max(lower_bound(A.begin(), A.end(), X - s) - A.begin(), 1);
		t2 = upper_bound(A.begin(), A.end(), X + s) - A.begin();
		ans = PS[N] - PS[t2 - 1];
		if (t2 - t1 > N + 1 - t2) {
			ans += A[t2 - 1];
			if (t1 & 1) ans += t1 ? PS_odd[t1 - 1] : 0;
			else ans += t1 ? PS_even[t1 - 1] : 0;
		}
		else {
			if (t1 & 1) ans += t1 ? PS_even[t1 - 1]:0;
			else ans += t1 ? PS_odd[t1 - 1]:0;
		}
		cout << ans << '\n';
	}
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:38:33: error: no matching function for call to 'max(__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type, int)'
   38 |                         t1 = max(lower_bound(A.begin(), A.end(), X - m) - A.begin(), 1);
      |                              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:38:33: note:   deduced conflicting types for parameter 'const _Tp' ('long int' and 'int')
   38 |                         t1 = max(lower_bound(A.begin(), A.end(), X - m) - A.begin(), 1);
      |                              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:38:33: note:   mismatched types 'std::initializer_list<_Tp>' and 'long int'
   38 |                         t1 = max(lower_bound(A.begin(), A.end(), X - m) - A.begin(), 1);
      |                              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:47:25: error: no matching function for call to 'max(__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type, int)'
   47 |                 t1 = max(lower_bound(A.begin(), A.end(), X - s) - A.begin(), 1);
      |                      ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:47:25: note:   deduced conflicting types for parameter 'const _Tp' ('long int' and 'int')
   47 |                 t1 = max(lower_bound(A.begin(), A.end(), X - s) - A.begin(), 1);
      |                      ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:47:25: note:   mismatched types 'std::initializer_list<_Tp>' and 'long int'
   47 |                 t1 = max(lower_bound(A.begin(), A.end(), X - s) - A.begin(), 1);
      |                      ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~