Language: C++

#include<iostream>                                                                                                                                                                                                                              using namespace std;                                                                                                    long calc(int* map, int x, int n);                                                                                      
int* half, *half2;
int sum = 0;
int main(){
   int n,q;
   cin >> n >> q;
   int *map = new int[n];
   half = new int[n];
   half2 = new int[n];
   sum = 0;
   int halfsum = 0;
   for(int i=0;i<n;i++){
      int a;
      cin >> a;
      map[i] = a;
      half2[i] = -sum;
      sum += a;
      if((i%2==1) ^ (n%2==1)) {
         halfsum+=a;
      }
      half[i] = halfsum;
   }
   for(int i=0;i<q;i++){
      int x;
      cin >> x;
      auto ans = calc(map, x, n);
      cout << ans << endl;
   }
}

int bs(int* map, int x, int i, int j){
   if(i + 1 == j)return j;
   int k = (i+j)/2;
   if(map[k] < x) return bs(map,x, k, j);
   else return bs(map,x, i,k);
}
long calc(int* map, int x, int n)
{
   int k = x <= map[0] ? 0
      : map[n-1] <= x ?  n-1
      : bs(map, x, 0, n-1);
   int low =k-1, high=k;
   int a = n-k;
   int t = k+a/2;

   //int sc_a = 0;
   int sc_t = sum + half2[k];
   //printf("start:%d, a:%d, low:%d, high:%d\n", sc_t,a, low, high);
   for(int i=0;i<a/2;i++){
      if(low < 0 || map[high] - x < x - map[low]){
         //sc_a += map[high];
         sc_t -= map[high];
         high++;
      }else{
         //sc_a += map[low];
         low--;
         t--;
      }
   }
   // 0~lowまでを分ける
   /*
   for(int i=0;i<=low;i+=2){
      sc_a += map[i];
   }
   */
   //;printf("second:%d\n", sc_t);
   if(low >= 0){
      sc_t += half[low];
   }                                                                                                                                                                                                                                               return sc_t;                                                                                                         }     

Compiler output:
a.cc:1:241: warning: extra tokens at end of #include directive
    1 | #include<iostream>                                                                                                                                                                                                                              using namespace std;                                                                                                    long calc(int* map, int x, int n);
      |                                                                                                                                                                                                                                                 ^~~~~
a.cc: In function 'int main()':
a.cc:6:4: error: 'cin' was not declared in this scope; did you mean 'std::cin'?
    6 |    cin >> n >> q;
      |    ^~~
      |    std::cin
In file included from a.cc:1:
/usr/include/c++/14/iostream:62:18: note: 'std::cin' declared here
   62 |   extern istream cin;           ///< Linked to standard input
      |                  ^~~
a.cc:26:18: error: 'calc' was not declared in this scope
   26 |       auto ans = calc(map, x, n);
      |                  ^~~~
a.cc:27:7: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
   27 |       cout << ans << endl;
      |       ^~~~
      |       std::cout
/usr/include/c++/14/iostream:63:18: note: 'std::cout' declared here
   63 |   extern ostream cout;          ///< Linked to standard output
      |                  ^~~~
a.cc:27:22: error: 'endl' was not declared in this scope; did you mean 'std::endl'?
   27 |       cout << ans << endl;
      |                      ^~~~
      |                      std::endl
In file included from /usr/include/c++/14/iostream:41:
/usr/include/c++/14/ostream:744:5: note: 'std::endl' declared here
  744 |     endl(basic_ostream<_CharT, _Traits>& __os)
      |     ^~~~