Problem p03158 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define MOD 998244353
template <typename ty1, typename ty2> inline int add(ty1 x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x += y;
  return x < MOD ? x : x - MOD;
}
template <typename ty1, typename ty2> inline void addto(ty1 &x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x += y;
  if (x >= MOD)
    x -= MOD;
}
template <typename ty1, typename ty2> inline int sub(ty1 x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x -= y;
  return x < 0 ? x + MOD : x;
}
template <typename ty1, typename ty2> inline void subto(ty1 &x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x -= y;
  if (x < 0)
    x += MOD;
}
template <typename ty1, typename ty2> inline int mul(ty1 x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  return 1ll * x * y % MOD;
}
template <typename ty1, typename ty2> void multo(ty1 &x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x = 1ll * x * y % MOD;
}

long long int gcd(long long int a, long long int b) {
  if (a > b) {
    swap(a, b);
  }
  while (a) {
    swap(a, b);
    a %= b;
  }
  return b;
}
long long int lcm(long long int a, long long int b) {
  return a / gcd(a, b) * b;
}
long long int ppow(long long int i, long long int j) {
  long long int res = 1LL;
  while (j) {
    if ((j & 1LL)) {
      res *= i;
      if (res >= MOD) {
        res %= MOD;
      }
    }
    j >>= 1;
    i *= i;
    if (i >= MOD) {
      i %= MOD;
    }
  }
  return res;
}
class Combination {

public:
  vector<long long int> k;
  vector<long long int> r;
  void resize(int N) {
    k.resize(N + 2);
    r.resize(N + 2);
    k[0] = 1;
    for (int i = 1; i < N + 2; i++) {
      k[i] = k[i - 1];
      k[i] *= i;
      if (k[i] >= MOD)
        k[i] %= MOD;
    }
    long long int al = k[k.size() - 1];
    long long int iv = ppow(k[k.size() - 1], MOD - 2);
    r[k.size() - 1] = iv;
    for (int i = (int)(r.size()) - 2; i >= 0; i--) {
      r[i] = r[i + 1] * (i + 1);
      if (r[i] >= MOD) {
        r[i] %= MOD;
      }
    }
  }
  long long int C(int a, int b) {
    if (a < b)
      return 0;
    long long int up = k[a];
    long long int dw = r[b] * r[a - b];
    dw %= MOD;
    up *= dw;
    up %= MOD;
    return up;
  }
  long long int H(int a, int b) { return C(a + b - 1, b); }
  long long int catalan_number(int n) {
    return (C(2 * n, n) + MOD - C(2 * n, n - 1)) % MOD;
  }
};
Combination C;

#define MAX 100002

int n;
int q;
vector<int> v;
long long int im[MAX][2];
inline int rng(int x, int mint) {
  int lef = lower_bound(v.begin(), v.end(), (long long int)x - (v[mint] - x)) -
            v.begin();
  int rig = mint;
  return rig - lef + 1;
}
long long int sum(int lef, int rig) {
  if (lef > rig)
    return 0;
  long long int ret = im[rig][0] + im[rig][1];
  if (lef) {
    ret -= im[lef - 1][0] + im[lef - 1][1];
  }
  return ret;
}
long long int skip(int lef) {
  if (lef < 0)
    return 0;
  return im[lef][lef & 1];
}
int main() {
  cin >> n >> q;
  long long int overall = 0;
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    v.push_back(a);
    overall += a;
    im[i][i & 1] += a;
    if (i) {
      im[i][0] += im[i - 1][0];
      im[i][1] += im[i - 1][1];
    }
  }
  for (int i = 0; i < q; i++) {
    int x;
    scanf("%d", &x);
    if (v.back() <= x) {
      printf("%lld\n", skip(n - 1));
    } else {
      int id = lower_bound(v.begin(), v.end(), x) - v.begin();
      int mint = id;
      int maxt = v.size() - 1;
      while (mint + 1 < maxt) {
        int mid = (mint + maxt) / 2;
        if (rng(x, mid) <= v.size() - mid - 1) {
          mint = mid;
        } else {
          maxt = mid;
        }
      }
      int lef =
          lower_bound(v.begin(), v.end(), (long long int)x - (v[mint] - x)) -
          v.begin();
      int rig = mint;
      if (rng(x, mint) <= v.size() - mint - 1) {

      } else {
        rig = id - 1;
        mint = id - 1;
        lef = id - (v.size() - id);
        lef = max(0, lef);
      }
      long long int ao = sum(lef, rig);
      // cerr<<"sum "<<sum(lef,rig)<<endl;
      int ao_turn = rig - lef + 1;
      int tk_turn = v.size() - mint - 1;
      // cerr<<lef<<" "<<rig<<" "<<ao_turn<<" "<<tk_turn<<endl;
      if (ao_turn == tk_turn) {
        ao += skip(lef - 2);
      } else {
        assert(ao_turn + 1 == tk_turn);
        ao += skip(lef - 1);
        // cerr<<"skip"<<" "<<skip(lef-1)<<endl;
      }
      printf("%lld\n", overall - ao);
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define MOD 998244353
template <typename ty1, typename ty2> inline int add(ty1 x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x += y;
  return x < MOD ? x : x - MOD;
}
template <typename ty1, typename ty2> inline void addto(ty1 &x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x += y;
  if (x >= MOD)
    x -= MOD;
}
template <typename ty1, typename ty2> inline int sub(ty1 x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x -= y;
  return x < 0 ? x + MOD : x;
}
template <typename ty1, typename ty2> inline void subto(ty1 &x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x -= y;
  if (x < 0)
    x += MOD;
}
template <typename ty1, typename ty2> inline int mul(ty1 x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  return 1ll * x * y % MOD;
}
template <typename ty1, typename ty2> void multo(ty1 &x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x = 1ll * x * y % MOD;
}

long long int gcd(long long int a, long long int b) {
  if (a > b) {
    swap(a, b);
  }
  while (a) {
    swap(a, b);
    a %= b;
  }
  return b;
}
long long int lcm(long long int a, long long int b) {
  return a / gcd(a, b) * b;
}
long long int ppow(long long int i, long long int j) {
  long long int res = 1LL;
  while (j) {
    if ((j & 1LL)) {
      res *= i;
      if (res >= MOD) {
        res %= MOD;
      }
    }
    j >>= 1;
    i *= i;
    if (i >= MOD) {
      i %= MOD;
    }
  }
  return res;
}
class Combination {

public:
  vector<long long int> k;
  vector<long long int> r;
  void resize(int N) {
    k.resize(N + 2);
    r.resize(N + 2);
    k[0] = 1;
    for (int i = 1; i < N + 2; i++) {
      k[i] = k[i - 1];
      k[i] *= i;
      if (k[i] >= MOD)
        k[i] %= MOD;
    }
    long long int al = k[k.size() - 1];
    long long int iv = ppow(k[k.size() - 1], MOD - 2);
    r[k.size() - 1] = iv;
    for (int i = (int)(r.size()) - 2; i >= 0; i--) {
      r[i] = r[i + 1] * (i + 1);
      if (r[i] >= MOD) {
        r[i] %= MOD;
      }
    }
  }
  long long int C(int a, int b) {
    if (a < b)
      return 0;
    long long int up = k[a];
    long long int dw = r[b] * r[a - b];
    dw %= MOD;
    up *= dw;
    up %= MOD;
    return up;
  }
  long long int H(int a, int b) { return C(a + b - 1, b); }
  long long int catalan_number(int n) {
    return (C(2 * n, n) + MOD - C(2 * n, n - 1)) % MOD;
  }
};
Combination C;

#define MAX 100002

int n;
int q;
vector<int> v;
long long int im[MAX][2];
inline int rng(int x, int mint) {
  int lef = lower_bound(v.begin(), v.end(), (long long int)x - (v[mint] - x)) -
            v.begin();
  int rig = mint;
  return rig - lef + 1;
}
long long int sum(int lef, int rig) {
  if (lef > rig)
    return 0;
  long long int ret = im[rig][0] + im[rig][1];
  if (lef) {
    ret -= im[lef - 1][0] + im[lef - 1][1];
  }
  return ret;
}
long long int skip(int lef) {
  if (lef < 0)
    return 0;
  return im[lef][lef & 1];
}
int main() {
  cin >> n >> q;
  long long int overall = 0;
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    v.push_back(a);
    overall += a;
    im[i][i & 1] += a;
    if (i) {
      im[i][0] += im[i - 1][0];
      im[i][1] += im[i - 1][1];
    }
  }
  for (int i = 0; i < q; i++) {
    int x;
    scanf("%d", &x);
    if (v.back() <= x) {
      printf("%lld\n", skip(n - 1));
    } else {
      int id = lower_bound(v.begin(), v.end(), x) - v.begin();
      int mint = id;
      int maxt = v.size() - 1;
      while (mint + 1 < maxt) {
        int mid = (mint + maxt) / 2;
        if (rng(x, mid) <= v.size() - mid - 1) {
          mint = mid;
        } else {
          maxt = mid;
        }
      }
      int lef =
          lower_bound(v.begin(), v.end(), (long long int)x - (v[mint] - x)) -
          v.begin();
      int rig = mint;
      if (rng(x, mint) <= v.size() - mint - 1) {

      } else {
        rig = id - 1;
        mint = id - 1;
        lef = id - (v.size() - id);
        lef = max(0, lef);
      }
      long long int ao = sum(lef, rig);
      // cerr<<"sum "<<sum(lef,rig)<<endl;
      int ao_turn = rig - lef + 1;
      int tk_turn = v.size() - mint - 1;
      if (ao_turn + 1 < tk_turn) {
        lef -= (tk_turn - ao_turn - 1);
        ao = sum(lef, rig);
        ao_turn = rig - lef + 1;
      }
      cerr << lef << " " << rig << " " << ao_turn << " " << tk_turn << endl;
      if (ao_turn == tk_turn) {
        ao += skip(lef - 2);
      } else {
        assert(ao_turn + 1 == tk_turn);
        ao += skip(lef - 1);
        // cerr<<"skip"<<" "<<skip(lef-1)<<endl;
      }
      printf("%lld\n", overall - ao);
    }
  }
  return 0;
}