Problem p03158 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define ll long long
#define mod 1000000007
#define maxn 410
inline ll read() {
  ll x = 0;
  char c = getchar(), f = 1;
  for (; c < '0' || '9' < c; c = getchar())
    if (c == '-')
      f = -1;
  for (; '0' <= c && c <= '9'; c = getchar())
    x = x * 10 + c - '0';
  return x * f;
}
inline void write(ll x) {
  static char buff[20], len;
  len = 0;
  if (x < 0)
    putchar('-'), x = -x;
  for (; x; x /= 10)
    buff[len++] = x % 10;
  if (!len)
    putchar('0');
  else
    while (len)
      putchar(buff[--len] + '0');
}
inline void writeln(ll x) {
  write(x);
  putchar('\n');
}
inline void writesp(ll x) {
  write(x);
  putchar(' ');
}
int a[maxn];
ll sum[maxn], sum0[maxn], sum1[maxn];
int n, q;
int find(int x) {
  int l = 1, r = n + 1;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (x > a[mid])
      l = mid + 1;
    else
      r = mid;
  }
  return l;
}
ll work(int x) {
  //	printf("%d ***************\n",x);
  if (x >= a[n]) {
    if (n & 1)
      return sum1[n];
    else
      return sum0[n];
  }
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r + 1) >> 1, pos = find(x * 2 - a[mid - 1]);
    if (mid - pos > n - mid + 1)
      r = mid - 1;
    else
      l = mid;
  }
  int pos = l - (n - l + 1);
  //	writeln(l);
  return (sum[n] - sum[l - 1]) + ((pos & 1) ? sum0[pos - 1] : sum1[pos - 1]);
}
int main() {
  n = read();
  q = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  std::sort(a + 1, a + n + 1);
  sum[0] = sum0[0] = sum1[0] = 0;
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + a[i];
    sum0[i] = sum0[i - 1] + ((i & 1) ? 0 : a[i]);
    sum1[i] = sum1[i - 1] + ((i & 1) ? a[i] : 0);
  }
  for (int i = 1; i <= q; i++) {
    int x = read();
    writeln(work(x));
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define ll long long
#define mod 1000000007
#define maxn 100010
inline ll read() {
  ll x = 0;
  char c = getchar(), f = 1;
  for (; c < '0' || '9' < c; c = getchar())
    if (c == '-')
      f = -1;
  for (; '0' <= c && c <= '9'; c = getchar())
    x = x * 10 + c - '0';
  return x * f;
}
inline void write(ll x) {
  static char buff[20], len;
  len = 0;
  if (x < 0)
    putchar('-'), x = -x;
  for (; x; x /= 10)
    buff[len++] = x % 10;
  if (!len)
    putchar('0');
  else
    while (len)
      putchar(buff[--len] + '0');
}
inline void writeln(ll x) {
  write(x);
  putchar('\n');
}
inline void writesp(ll x) {
  write(x);
  putchar(' ');
}
int a[maxn];
ll sum[maxn], sum0[maxn], sum1[maxn];
int n, q;
int find(int x) {
  int l = 1, r = n + 1;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (x > a[mid])
      l = mid + 1;
    else
      r = mid;
  }
  return l;
}
ll work(int x) {
  //	printf("%d ***************\n",x);
  if (x >= a[n]) {
    if (n & 1)
      return sum1[n];
    else
      return sum0[n];
  }
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r + 1) >> 1, pos = find(x * 2 - a[mid - 1]);
    if (mid - pos > n - mid + 1)
      r = mid - 1;
    else
      l = mid;
  }
  int pos = l - (n - l + 1);
  //	writeln(l);
  return (sum[n] - sum[l - 1]) + ((pos & 1) ? sum0[pos - 1] : sum1[pos - 1]);
}
int main() {
  n = read();
  q = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  std::sort(a + 1, a + n + 1);
  sum[0] = sum0[0] = sum1[0] = 0;
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + a[i];
    sum0[i] = sum0[i - 1] + ((i & 1) ? 0 : a[i]);
    sum1[i] = sum1[i - 1] + ((i & 1) ? a[i] : 0);
  }
  for (int i = 1; i <= q; i++) {
    int x = read();
    writeln(work(x));
  }
}