prithivMLmods/Coder-Stat huggingface dataset:
Problem p03111 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;

int N, A, B, C, l[8] = {};
long long mpsum, mpmin = 9999999999;
void dfs(int depth, vector<int> v) {

  if (depth == N) {          // ここから計算部
    int num[4] = {};         // 使われた回数を確認
    int a = 0, b = 0, c = 0; // a,b,cのmp消費前の竹の長さ
    for (int i = 0; i < N; i++) {
      num[v[i]] += 1;
      if (v[i] == 1)
        a += l[i];
      if (v[i] == 2)
        b += l[i];
      if (v[i] == 3)
        c += l[i];
    }
    if (num[1] != 0 && num[2] != 0 && num[3] != 0) {
      mpsum = (abs(A - a) + abs(B - b) + abs(C - c) +
               (num[1] + num[2] + num[3] - 3) * 10);
      if (mpsum < mpmin)
        mpmin = mpsum;
    }
    // ここまで計算部
  } else {
    for (int i = 0; i < 4; i++) {
      v[depth] = i;
      dfs(depth + 1, v);
    }
  }
}

int main() {
  cin >> N >> A >> B >> C;
  for (int i = 0; i < N; i++)
    cin >> l[i];
  int depth = 0;
  vector<int> v;
  dfs(depth, v);
  cout << mpmin << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;

int N, A, B, C, l[8] = {};
long long mpsum, mpmin = 9999999999;
void dfs(int depth, vector<int> v) {

  if (depth == N) {          // ここから計算部
    int num[4] = {};         // 使われた回数を確認
    int a = 0, b = 0, c = 0; // a,b,cのmp消費前の竹の長さ
    for (int i = 0; i < N; i++) {
      num[v[i]] += 1;
      if (v[i] == 1)
        a += l[i];
      if (v[i] == 2)
        b += l[i];
      if (v[i] == 3)
        c += l[i];
    }
    if (num[1] != 0 && num[2] != 0 && num[3] != 0) {
      mpsum = (abs(A - a) + abs(B - b) + abs(C - c) +
               (num[1] + num[2] + num[3] - 3) * 10);
      if (mpsum < mpmin)
        mpmin = mpsum;
    }
    // ここまで計算部
  } else {
    for (int i = 0; i < 4; i++) {
      v[depth] = i;
      dfs(depth + 1, v);
    }
  }
}

int main() {
  cin >> N >> A >> B >> C;
  for (int i = 0; i < N; i++)
    cin >> l[i];
  vector<int> v(8, 0);
  dfs(0, v);
  cout << mpmin << endl;

  return 0;
}
Problem p03111 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int N, A, B, C;
vector<int> l;

int dfs(int depth, int La, int Lb, int Lc) {
  if (depth == N + 1) {
    if (La != 0 && Lb != 0 && Lc != 0) {
      return abs(A - La) + abs(Lb - B) + abs(Lc - C) - 30;
    }
  }

  int r1 = dfs(depth + 1, La + l[depth], Lb, Lc) + 10;
  int r2 = dfs(depth + 1, La, Lb + l[depth], Lc) + 10;
  int r3 = dfs(depth + 1, La, Lb, Lc + l[depth]) + 10;
  int r4 = dfs(depth + 1, La, Lb, Lc);

  return min(min(r1, r2), min(r3, r4));
}

int main() {
  cin >> N >> A >> B >> C;
  l.resize(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> l[i];
  } // 1-indexed

  cout << dfs(1, 0, 0, 0) << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int N, A, B, C;
vector<int> l;

int dfs(int depth, int La, int Lb, int Lc) {
  if (depth == N + 1) {
    if (La != 0 && Lb != 0 && Lc != 0) {
      return abs(A - La) + abs(Lb - B) + abs(Lc - C) - 30;
    } else {
      return (1 << 29);
    }
  }

  int r1 = dfs(depth + 1, La + l[depth], Lb, Lc) + 10;
  int r2 = dfs(depth + 1, La, Lb + l[depth], Lc) + 10;
  int r3 = dfs(depth + 1, La, Lb, Lc + l[depth]) + 10;
  int r4 = dfs(depth + 1, La, Lb, Lc);

  return min(min(r1, r2), min(r3, r4));
}

int main() {
  cin >> N >> A >> B >> C;
  l.resize(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> l[i];
  } // 1-indexed

  cout << dfs(1, 0, 0, 0) << endl;

  return 0;
}
Problem p03111 (C++)

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

using namespace std;
int N;
vector<int> L;
int A, B, C;

int ans = 1e9;

void dfs(int depth, int a, int b, int c, int mp) {
  if (depth == N) { // ベースケース
    if (a > 0 && b > 0 && c > 0) {
      mp += abs(a - A) + abs(b - B) + abs(c - C);
      ans = min(ans, mp - 30);
    } else {
      return;
    }
  }

  dfs(depth + 1, a + L[depth], b, c, mp + 10);
  dfs(depth + 1, a, b + L[depth], c, mp + 10);
  dfs(depth + 1, a, b, c + L[depth], mp + 10);
  dfs(depth + 1, a, b, c, mp);
}

int main() {

  cin >> N >> A >> B >> C;
  L.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> L[i];
  }

  dfs(0, 0, 0, 0, 0);

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
int N;
vector<int> L;
int A, B, C;

int ans = 1e9;

void dfs(int depth, int a, int b, int c, int mp) {
  if (depth == N) { // ベースケース
    if (a > 0 && b > 0 && c > 0) {
      mp += abs(a - A) + abs(b - B) + abs(c - C);
      ans = min(ans, mp - 30);
      return;
    } else {
      return;
    }
  }

  dfs(depth + 1, a + L[depth], b, c, mp + 10);
  dfs(depth + 1, a, b + L[depth], c, mp + 10);
  dfs(depth + 1, a, b, c + L[depth], mp + 10);
  dfs(depth + 1, a, b, c, mp);
}

int main() {

  cin >> N >> A >> B >> C;
  L.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> L[i];
  }

  dfs(0, 0, 0, 0, 0);

  cout << ans << endl;
  return 0;
}
Problem p03111 (Python)

Buggy submission (status: Time Limit Exceeded):
N, A, B, C = map(int, input().split())
L = [int(input()) for _ in range(N)]


def solve(X):
    if not ("1" in X and "2" in X and "3" in X):
        return 1e10

    MS = [[L[i] for i in range(N) if X[i] == j] for j in "123"]
    ret = 1e10
    ret = min(ret, solve2(MS[0], A) + solve2(MS[1], B) + solve2(MS[2], C))
    ret = min(ret, solve2(MS[0], A) + solve2(MS[2], B) + solve2(MS[1], C))
    ret = min(ret, solve2(MS[1], A) + solve2(MS[0], B) + solve2(MS[2], C))
    ret = min(ret, solve2(MS[1], A) + solve2(MS[2], B) + solve2(MS[0], C))
    ret = min(ret, solve2(MS[2], A) + solve2(MS[0], B) + solve2(MS[1], C))
    ret = min(ret, solve2(MS[2], A) + solve2(MS[1], B) + solve2(MS[0], C))
    return ret


def solve2(M, target):
    def rec(Y, target):
        # Y: [1, 0, 0]
        if len(Y) == len(M):
            if len(set(Y)) == 1 and Y[0] == 0:
                return 1e10
            sum_M = sum([M[i] for i in range(len(M)) if Y[i] == 1])
            return abs(target - sum_M) + 10 * (Y.count(1) - 1)
        return min(rec(Y + [0], target), rec(Y + [1], target))

    ret = rec([], target)
    return ret


ans = 1e10


def rec(X):
    global ans
    if len(X) == N:
        ans = min(ans, solve(X))
        return
    rec(X + "1")
    rec(X + "2")
    rec(X + "3")


rec("")

print(ans)


Fixed submission:
N, A, B, C = map(int, input().split())
L = [int(input()) for _ in range(N)]


def solve(X):
    if not ("1" in X and "2" in X and "3" in X):
        return 1e10

    MS = [[L[i] for i in range(N) if X[i] == j] for j in "123"]
    ret = 1e10
    A_ans = []
    B_ans = []
    C_ans = []
    for i in range(3):
        A_ans.append(solve2(MS[i], A))
        B_ans.append(solve2(MS[i], B))
        C_ans.append(solve2(MS[i], C))
    ret = min(ret, A_ans[0] + B_ans[1] + C_ans[2])
    ret = min(ret, A_ans[0] + B_ans[2] + C_ans[1])
    ret = min(ret, A_ans[1] + B_ans[0] + C_ans[2])
    ret = min(ret, A_ans[1] + B_ans[2] + C_ans[0])
    ret = min(ret, A_ans[2] + B_ans[0] + C_ans[1])
    ret = min(ret, A_ans[2] + B_ans[1] + C_ans[0])
    return ret


def solve2(M, target):
    def rec(Y, target):
        # Y: [1, 0, 0]
        if len(Y) == len(M):
            if len(set(Y)) == 1 and Y[0] == 0:
                return 1e10
            sum_M = sum([M[i] for i in range(len(M)) if Y[i] == 1])
            return abs(target - sum_M) + 10 * (Y.count(1) - 1)
        return min(rec(Y + [0], target), rec(Y + [1], target))

    ret = rec([], target)
    return ret


ans = 1e10


def rec(X):
    global ans
    if len(X) == N:
        ans = min(ans, solve(X))
        return
    rec(X + "1")
    rec(X + "2")
    rec(X + "3")


rec("")

print(ans)
Problem p03111 (Python)

Buggy submission (status: Runtime Error):
from math import inf
from collections import deque

N, A, B, C = map(int, input().split())
ls = [0]
for _ in range(N):
    ls.append(int(input()))
ls.sort()

q = deque()
q.append((0, -30, 0, 0, 0))
minmp = inf
while len(q):
    n, mp, a, b, c = q.pop()
    if n == N:
        if min(a, b, c) == 0:
            continue
        minmp = min(minmp, mp + abs(a - A) + abs(b - B) + abs(c - C))
    else:
        q.append((n + 1, mp, a, b, c))
        q.append((n + 1, mp + 10, a + ls[n + 1], b, c))
        q.append((n + 1, mp + 10, a, b + ls[n + 1], c))
        q.append((n + 1, mp + 10, a, b, c + ls[n + 1]))

print(minmp)


Fixed submission:
try:
    from math import inf
except:
    inf = float("inf")
from collections import deque

N, A, B, C = map(int, input().split())
ls = [0]
for _ in range(N):
    ls.append(int(input()))
ls.sort()

q = deque()
q.append((0, -30, 0, 0, 0))
minmp = inf
while len(q):
    n, mp, a, b, c = q.pop()
    if n == N:
        if min(a, b, c) == 0:
            continue
        minmp = min(minmp, mp + abs(a - A) + abs(b - B) + abs(c - C))
    else:
        q.append((n + 1, mp, a, b, c))
        q.append((n + 1, mp + 10, a + ls[n + 1], b, c))
        q.append((n + 1, mp + 10, a, b + ls[n + 1], c))
        q.append((n + 1, mp + 10, a, b, c + ls[n + 1]))

print(minmp)
Problem p03111 (Python)

Buggy submission (status: Runtime Error):
# 入力
N, A, B, C = map(int, input().split())
L = [int(input()) for i in range(N)]

INF = 10**9


def dfs(cursor, a, b, c):  # cursor:カーソル a,b,c:現在の竹の長さ
    if cursor == N:  # cursorが最後まで行ったら終了する。
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
    # abs(a - A) + abs(b - B) + abs(c - C) - 30 でなぜ30を減じているのかというと、
    # dfs(0,0,0)
    # dfs(0,0,0,0)で始まる以上、最初に選ばれるa,b,cを決定する際にもコストが10増加してしまうからである。
    # また、全探索を行う中で、a,b,cの初期値が0,0,0である以上、a,b,cのどれかが0のまま終了する場合が存在する。
    # この場合はa,b,cに竹が対応してないと言えるため、解にはならない、そこで三項演算子を利用して
    # その場合についてコストをINFとしている

    # 以下は4**Nで展開される再起処理となる。
    # カーソルの当たっている竹に対して、(A or B or Cに合成する) or (合成しない)の場合に分ける
    no_compound_pattern = dfs(cursor, a, b, c)
    compound_a_pattern = dfs(cursor, a + L[cursor], b, c)
    compound_b_pattern = dfs(cursor, a, b + L[cursor], c)
    compound_c_pattern = dfs(cursor, a, b, c + L[cursor])

    # 結果的に以下の値が返るのはそれぞれのパターンのコストが決定されてからなので
    # 以下のコードは最終的なコストの最小値
    return min(
        no_compound_pattern, compound_a_pattern, compound_b_pattern, compound_c_pattern
    )


print(dfs(0, 0, 0, 0))


Fixed submission:
# 入力
N, A, B, C = map(int, input().split())
L = [int(input()) for i in range(N)]

INF = 10**9


def dfs(cursor, a, b, c):  # cursor:カーソル a,b,c:現在の竹の長さ
    if cursor == N:  # cursorが最後まで行ったら終了する。
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
    # abs(a - A) + abs(b - B) + abs(c - C) - 30 でなぜ30を減じているのかというと、
    # dfs(0,0,0)
    # dfs(0,0,0,0)で始まる以上、最初に選ばれるa,b,cを決定する際にもコストが10増加してしまうからである。
    # また、全探索を行う中で、a,b,cの初期値が0,0,0である以上、a,b,cのどれかが0のまま終了する場合が存在する。
    # この場合はa,b,cに竹が対応してないと言えるため、解にはならない、そこで三項演算子を利用して
    # その場合についてコストをINFとしている

    # 以下は4**Nで展開される再起処理となる。
    # カーソルの当たっている竹に対して、(A or B or Cに合成する) or (合成しない)の場合に分ける
    no_compound_pattern = dfs(cursor + 1, a, b, c)
    compound_a_pattern = dfs(cursor + 1, a + L[cursor], b, c) + 10
    compound_b_pattern = dfs(cursor + 1, a, b + L[cursor], c) + 10
    compound_c_pattern = dfs(cursor + 1, a, b, c + L[cursor]) + 10

    # 結果的に以下の値が返るのはそれぞれのパターンのコストが決定されてからなので
    # 以下のコードは最終的なコストの最小値
    return min(
        no_compound_pattern, compound_a_pattern, compound_b_pattern, compound_c_pattern
    )


print(dfs(0, 0, 0, 0))
Problem p03111 (Python)

Buggy submission (status: Runtime Error):
import itertools

N, A, B, C = map(int, input().split())
L = [int(input()) for _ in range(N)]

ans = float("inf")
for Lp in itertools.permutations(L):
    Lac = tuple(itertools.accumulate((0, *Lp)))
    for a in range(1, N - 1):
        for b in range(a + 1, N):
            for c in range(b + 1, N + 1):
                A_MP = 10 * (a - 1) + abs(Lac[a] - Lac[0] - A)
                B_MP = 10 * (b - a - 1) + abs(Lac[b] - Lac[a] - B)
                C_MP = 10 * (c - b - 1) + abs(Lac[c] - Lac[b] - C)
                ans = min(ans, A_MP + B_MP + C_MP)
print(ans)


Fixed submission:
import itertools

N, A, B, C = map(int, input().split())
L = [int(input()) for _ in range(N)]

ans = float("inf")
for Lp in itertools.permutations(L):
    Lac = tuple(itertools.accumulate(([0] + list(Lp))))
    for a in range(1, N - 1):
        for b in range(a + 1, N):
            for c in range(b + 1, N + 1):
                A_MP = 10 * (a - 1) + abs(Lac[a] - Lac[0] - A)
                B_MP = 10 * (b - a - 1) + abs(Lac[b] - Lac[a] - B)
                C_MP = 10 * (c - b - 1) + abs(Lac[c] - Lac[b] - C)
                ans = min(ans, A_MP + B_MP + C_MP)
print(ans)
Problem p03111 (Python)

Buggy submission (status: Runtime Error):
import itertools

N, a, b, c = [int(_) for _ in input().split()]
L = [int(input()) for _ in range(N)]
ans = 10**10
for K in itertools.product(range(4), repeat=N):
    A = [[] for _ in range(N)]
    for i in range(N):
        A[K[i]] += [L[i]]
    if len(A[1]) and len(A[2]) and len(A[3]):
        cost = 10 * (N - len(A[0]) - 3)
        cost += abs(a - sum(A[1]))
        cost += abs(b - sum(A[2]))
        cost += abs(c - sum(A[3]))
        ans = min(cost, ans)
print(ans)


Fixed submission:
import itertools

N, a, b, c = [int(_) for _ in input().split()]
L = [int(input()) for _ in range(N)]
ans = 10**10
for K in itertools.product(range(4), repeat=N):
    A = [[] for _ in range(4)]
    for i in range(N):
        A[K[i]] += [L[i]]
    if len(A[1]) and len(A[2]) and len(A[3]):
        cost = 10 * (N - len(A[0]) - 3)
        cost += abs(a - sum(A[1]))
        cost += abs(b - sum(A[2]))
        cost += abs(c - sum(A[3]))
        ans = min(cost, ans)
print(ans)
Problem p03111 (C++)

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

const long long INF = 1LL << 60;

template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}

template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}

int main() {
  int N, A, B, C;
  cin >> N >> A >> B >> C;
  vector<int> l(N);
  for (int i = 0; i < N; i++)
    cin >> l.at(i);

  long long ans = INF;

  for (int i = 0; i < pow(4, N); i++) {
    long long at = 0, bt = 0, ct = 0;
    long long al = 0, bl = 0, cl = 0;

    for (int j = 0, p = 1; j < N; j++) {
      int Z = (i / p) % 4;
      if (Z == 1) {
        at++;
        al += l.at(j);
      } else if (Z == 2) {
        bt++;
        bl += l.at(j);
      } else if (Z == 3) {
        ct++;
        cl += l.at(j);
      }
      p *= j;
    }

    if (at > 0 && bt > 0 && ct > 0) {
      long long MP =
          (at + bt + ct - 3) * 10 + abs(A - al) + abs(B - bl) + abs(C - cl);
      chmin(ans, MP);
    }
  }

  cout << ans << endl;
}

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

const long long INF = 1LL << 60;

template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}

template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}

int main() {
  int N, A, B, C;
  cin >> N >> A >> B >> C;
  vector<int> l(N);
  for (int i = 0; i < N; i++)
    cin >> l.at(i);

  long long ans = INF;

  for (int i = 0; i < pow(4, N); i++) {
    long long at = 0, bt = 0, ct = 0;
    long long al = 0, bl = 0, cl = 0;

    for (int j = 0, p = 1; j < N; j++) {
      int Z = (i / p) % 4;
      if (Z == 1) {
        at++;
        al += l.at(j);
      } else if (Z == 2) {
        bt++;
        bl += l.at(j);
      } else if (Z == 3) {
        ct++;
        cl += l.at(j);
      }
      p *= 4;
    }

    if (at > 0 && bt > 0 && ct > 0) {
      long long MP =
          (at + bt + ct - 3) * 10 + abs(A - al) + abs(B - bl) + abs(C - cl);
      chmin(ans, MP);
    }
  }

  cout << ans << endl;
}
Problem p03112 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

ll calc(vector<ll> a, vector<ll> b, ll x) {
  //  vector<ll>a,b;
  ll ans = 1e18;
  ll d;
  //  for ( int i = 0; i < 2; i++ ) {
  //    if ( i == 0 ) a = s,b = t;
  //    else a = t,b = s;
  for (int j = 0; j < 2; j++) {
    int apos = lower_bound(a.begin(), a.end(), x) - a.begin();
    ll ax = a[apos - j];
    for (int k = 0; k < 2; k++) {
      int bpos = lower_bound(b.begin(), b.end(), ax) - b.begin();
      ll bx = b[bpos - k];
      d = abs(x - ax) + abs(ax - bx);
      ans = min(ans, d);
    }
  }
  //  }
  return ans;
}

int main() {
  int A, B, Q;
  cin >> A >> B >> Q;
  vector<ll> s(A + 2), t(B + 2);

  ll x[100010];
  s[0] = t[0] = -1e18;
  for (int i = 1; i <= A; i++)
    cin >> s[i];
  for (int i = 1; i <= B; i++)
    cin >> t[i];
  for (int i = 0; i < Q; i++)
    cin >> x[i];
  s[A + 1] = 1e18;
  t[B + 1] = 1e18;
  for (int i = 0; i < Q; i++) {
    cout << min(calc(s, t, x[i]), calc(t, s, x[i])) << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

ll calc(vector<ll> &a, vector<ll> &b, ll x) {
  //  vector<ll>a,b;
  ll ans = 1e18;
  ll d;
  //  for ( int i = 0; i < 2; i++ ) {
  //    if ( i == 0 ) a = s,b = t;
  //    else a = t,b = s;
  for (int j = 0; j < 2; j++) {
    int apos = lower_bound(a.begin(), a.end(), x) - a.begin();
    ll ax = a[apos - j];
    for (int k = 0; k < 2; k++) {
      int bpos = lower_bound(b.begin(), b.end(), ax) - b.begin();
      ll bx = b[bpos - k];
      d = abs(x - ax) + abs(ax - bx);
      ans = min(ans, d);
    }
  }
  //  }
  return ans;
}

int main() {
  int A, B, Q;
  cin >> A >> B >> Q;
  vector<ll> s(A + 2), t(B + 2);

  ll x[100010];
  s[0] = t[0] = -1e18;
  for (int i = 1; i <= A; i++)
    cin >> s[i];
  for (int i = 1; i <= B; i++)
    cin >> t[i];
  for (int i = 0; i < Q; i++)
    cin >> x[i];
  s[A + 1] = 1e18;
  t[B + 1] = 1e18;
  for (int i = 0; i < Q; i++) {
    cout << min(calc(s, t, x[i]), calc(t, s, x[i])) << endl;
  }
  return 0;
}
Problem p03112 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ld long double
#define int long long
using namespace std;
const int N = 2e5 + 50;
const int mod = 1e9 + 7;
const int inf = 1e17;
int dx[] = {0, 1, -1, 0, -1, 1, 1, -1};
int dy[] = {1, 0, 0, -1, -1, 1, -1, 1};
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int a, b, q;
  cin >> a >> b >> q;
  vector<int> tem(a, 0), shr(b, 0);
  for (int i = 0; i < a; i++) {
    cin >> tem[i];
  }
  for (int i = 0; i < b; i++) {
    cin >> shr[i];
  }
  while (q--) {
    int s;
    cin >> s;
    int ans = 1e17;
    int in1 = lower_bound(tem.begin(), tem.end(), s) - tem.begin();
    int in2 = in1 - 1;
    int in3 = lower_bound(shr.begin(), shr.end(), s) - shr.begin();
    int in4 = in3 - 1;
    if (in1 < a && in3 < b) {
      ans = min(min(shr[in1], tem[in3]) - s + abs(shr[in1] - tem[in3]), ans);
    }
    if (in2 >= 0 && in3 < b) {
      int d1 = abs(shr[in3] - s);
      int d2 = abs(s - tem[in2]);
      int temp1 = 2LL * d1 + d2;
      int temp2 = 2LL * d2 + d1;
      ans = min(ans, min(temp1, temp2));
    }
    if (in2 >= 0 && in4 >= 0) {
      ans = min(s - max(shr[in4], tem[in2]) + abs(shr[in4] - tem[in2]), ans);
    }
    if (in4 >= 0 && in1 < a) {
      int d1 = abs(s - shr[in4]);
      int d2 = abs(tem[in1] - s);
      int temp1 = 2LL * d1 + d2;
      int temp2 = 2LL * d2 + d1;
      ans = min(ans, min(temp1, temp2));
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define ld long double
#define int long long
using namespace std;
const int N = 2e5 + 50;
const int mod = 1e9 + 7;
const int inf = 1e17;
int dx[] = {0, 1, -1, 0, -1, 1, 1, -1};
int dy[] = {1, 0, 0, -1, -1, 1, -1, 1};
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int a, b, q;
  cin >> a >> b >> q;
  vector<int> tem(a, 0), shr(b, 0);
  for (int i = 0; i < a; i++) {
    cin >> tem[i];
  }
  for (int i = 0; i < b; i++) {
    cin >> shr[i];
  }
  while (q--) {
    int s;
    cin >> s;
    int ans = 1e17;
    int in1 = lower_bound(tem.begin(), tem.end(), s) - tem.begin();
    int in2 = in1 - 1;
    int in3 = lower_bound(shr.begin(), shr.end(), s) - shr.begin();
    int in4 = in3 - 1;
    if (in1 < a && in3 < b) {
      ans = min(min(shr[in3], tem[in1]) - s + abs(shr[in3] - tem[in1]), ans);
    }
    if (in2 >= 0 && in3 < b) {
      int d1 = abs(shr[in3] - s);
      int d2 = abs(s - tem[in2]);
      int temp1 = 2LL * d1 + d2;
      int temp2 = 2LL * d2 + d1;
      ans = min(ans, min(temp1, temp2));
    }
    if (in2 >= 0 && in4 >= 0) {
      ans = min(s - max(shr[in4], tem[in2]) + abs(shr[in4] - tem[in2]), ans);
    }
    if (in4 >= 0 && in1 < a) {
      int d1 = abs(s - shr[in4]);
      int d2 = abs(tem[in1] - s);
      int temp1 = 2LL * d1 + d2;
      int temp2 = 2LL * d2 + d1;
      ans = min(ans, min(temp1, temp2));
    }
    cout << ans << endl;
  }
}
Problem p03112 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define INF 2e10
typedef long long llong;
using namespace std;

int main() {
  int a, b, q;
  cin >> a >> b >> q;
  llong s[10002], t[10002];
  for (int i = 1; i <= a; ++i)
    cin >> s[i];
  for (int i = 1; i <= b; ++i)
    cin >> t[i];
  s[0] = -INF, s[a + 1] = INF;
  t[0] = -INF, t[b + 1] = INF;

  REP(i, q) {
    llong x;
    cin >> x;

    auto itr_s = lower_bound(s, s + a + 1, x);
    auto itr_t = lower_bound(t, t + b + 1, x);

    llong ans = INF;
    ans = min(ans, max(*itr_s, *itr_t) - x); // 最適の神社と寺がともに東にある時.
    ans = min(ans, x - min(*(itr_s - 1),
                           *(itr_t - 1))); // 最適の神社と寺がともに西にある時.
    ans = min(ans, *(itr_t) - *(itr_s - 1) +
                       min(x - *(itr_s - 1), *(itr_t)-x)); // 西に神社、東に寺.
    ans = min(ans, *(itr_s) - *(itr_t - 1) +
                       min(x - *(itr_t - 1), *(itr_s)-x)); // 西に寺、東に神社.

    cout << ans << endl;
  }
} // 解説参考.

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define INF 2e10
typedef long long llong;
using namespace std;

int main() {
  int a, b, q;
  cin >> a >> b >> q;
  llong s[100002], t[100002];
  for (int i = 1; i <= a; ++i)
    cin >> s[i];
  for (int i = 1; i <= b; ++i)
    cin >> t[i];
  s[0] = -INF, s[a + 1] = INF;
  t[0] = -INF, t[b + 1] = INF;

  REP(i, q) {
    llong x;
    cin >> x;

    auto itr_s = lower_bound(s, s + a + 1, x);
    auto itr_t = lower_bound(t, t + b + 1, x);

    llong ans = INF;
    ans = min(ans, max(*itr_s, *itr_t) - x); // 最適の神社と寺がともに東にある時.
    ans = min(ans, x - min(*(itr_s - 1),
                           *(itr_t - 1))); // 最適の神社と寺がともに西にある時.
    ans = min(ans, *(itr_t) - *(itr_s - 1) +
                       min(x - *(itr_s - 1), *(itr_t)-x)); // 西に神社、東に寺.
    ans = min(ans, *(itr_s) - *(itr_t - 1) +
                       min(x - *(itr_t - 1), *(itr_s)-x)); // 西に寺、東に神社.

    cout << ans << endl;
  }
} // 解説参考.
Problem p03112 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ll long long
#define eps 1e-8
#define inf 1e17
using namespace std;

int main() {
  speed;
  ll a, b, q, x, v;
  cin >> a >> b >> q;
  set<ll> s, t;
  for (int i = 0; i < a; ++i) {
    cin >> v;
    s.insert(v);
  }
  for (int i = 0; i < b; ++i) {
    cin >> v;
    t.insert(v);
  }
  for (int i = 0; i < q; ++i) {
    cin >> x;
    auto q = s.lower_bound(x), w = t.lower_bound(x), pq = q, pw = w;
    --pq, --pw;
    ll ans = inf;
    if (q != s.end() && pw != t.end())
      ans = min({
          ans,
          min(*q - x, x - *pw) + *q - *pw,
      });
    if (pq != s.end() && w != t.end())
      ans = min({ans, min(*w - x, x - *pq) + *w - *pq});
    if (q != s.end() && w != t.end())
      ans = min(ans, max(*w - x, *q - x));
    if (pq != s.end() && pw != t.end())
      ans = min(ans, max(x - *pw, x - *pq));
    cout << ans << '\n';
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ll long long
#define eps 1e-8
#define inf 1e17
using namespace std;

int main() {
  speed;
  ll a, b, q, x, v;
  cin >> a >> b >> q;
  set<ll> s, t;
  for (int i = 0; i < a; ++i) {
    cin >> v;
    s.insert(v);
  }
  for (int i = 0; i < b; ++i) {
    cin >> v;
    t.insert(v);
  }
  for (int i = 0; i < q; ++i) {
    cin >> x;
    auto q = s.lower_bound(x), w = t.lower_bound(x), pq = q, pw = w;
    if (q == s.begin())
      pq = s.end();
    else
      --pq;
    if (w == t.begin())
      pw = t.end();
    else
      --pw;
    ll ans = inf;
    if (q != s.end() && pw != t.end())
      ans = min({
          ans,
          min(*q - x, x - *pw) + *q - *pw,
      });
    if (pq != s.end() && w != t.end())
      ans = min({ans, min(*w - x, x - *pq) + *w - *pq});
    if (q != s.end() && w != t.end())
      ans = min(ans, max(*w - x, *q - x));
    if (pq != s.end() && pw != t.end())
      ans = min(ans, max(x - *pw, x - *pq));
    cout << ans << '\n';
  }
}
Problem p03112 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, q, x;
  cin >> a >> b >> q;
  vector<long long> s(a), t(a);
  for (int i = 0; i < a; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < b; i++) {
    cin >> t[i];
  }
  for (int i = 0; i < q; i++) {
    cin >> x;
    long long r = a - 1, l = 0, ar, al, br, bl, ans = 1e18;
    while (abs(r - l) > 1) { // A
      long long mid = (r + l) / 2;
      if (s[mid] >= x) {
        r = mid;
      } else {
        l = mid;
      }
    }
    ar = r;
    al = r - 1;
    if (s[0] > x) {
      ar = 0;
      al = -1;
    }
    if (s[a - 1] < x) {
      ar = -1;
      al = a - 1;
    }

    r = b - 1;
    l = 0;
    while (abs(r - l) > 1) { // B
      long long mid = (r + l) / 2;
      if (t[mid] >= x) {
        r = mid;
      } else {
        l = mid;
      }
    }
    br = r;
    bl = r - 1;
    if (t[0] > x) {
      br = 0;
      bl = -1;
    }
    if (t[b - 1] < x) {
      br = -1;
      bl = b - 1;
    }
    if (al >= 0 and bl >= 0)
      ans = min(ans, max(abs(x - s[al]), abs(x - t[bl])));
    if (ar >= 0 and br >= 0)
      ans = min(ans, max(abs(s[ar] - x), abs(t[br] - x)));
    if (al >= 0 and br >= 0)
      ans = min(ans, 2 * abs(x - s[al]) + abs(t[br] - x));
    if (ar >= 0 and bl >= 0)
      ans = min(ans, 2 * abs(x - s[ar]) + abs(t[bl] - x));
    if (ar >= 0 and bl >= 0)
      ans = min(ans, 2 * abs(x - t[bl]) + abs(s[ar] - x));
    if (al >= 0 and br >= 0)
      ans = min(ans, 2 * abs(x - t[br]) + abs(s[al] - x));
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, q, x;
  cin >> a >> b >> q;
  vector<long long> s(a), t(b);
  for (int i = 0; i < a; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < b; i++) {
    cin >> t[i];
  }
  for (int i = 0; i < q; i++) {
    cin >> x;
    long long r = a - 1, l = 0, ar, al, br, bl, ans = 1e18;
    while (abs(r - l) > 1) { // A
      long long mid = (r + l) / 2;
      if (s[mid] >= x) {
        r = mid;
      } else {
        l = mid;
      }
    }
    ar = r;
    al = r - 1;
    if (s[0] > x) {
      ar = 0;
      al = -1;
    }
    if (s[a - 1] < x) {
      ar = -1;
      al = a - 1;
    }

    r = b - 1;
    l = 0;
    while (abs(r - l) > 1) { // B
      long long mid = (r + l) / 2;
      if (t[mid] >= x) {
        r = mid;
      } else {
        l = mid;
      }
    }
    br = r;
    bl = r - 1;
    if (t[0] > x) {
      br = 0;
      bl = -1;
    }
    if (t[b - 1] < x) {
      br = -1;
      bl = b - 1;
    }
    if (al >= 0 and bl >= 0)
      ans = min(ans, max(abs(x - s[al]), abs(x - t[bl])));
    if (ar >= 0 and br >= 0)
      ans = min(ans, max(abs(s[ar] - x), abs(t[br] - x)));
    if (al >= 0 and br >= 0)
      ans = min(ans, 2 * abs(x - s[al]) + abs(t[br] - x));
    if (ar >= 0 and bl >= 0)
      ans = min(ans, 2 * abs(x - s[ar]) + abs(t[bl] - x));
    if (ar >= 0 and bl >= 0)
      ans = min(ans, 2 * abs(x - t[bl]) + abs(s[ar] - x));
    if (al >= 0 and br >= 0)
      ans = min(ans, 2 * abs(x - t[br]) + abs(s[al] - x));
    cout << ans << endl;
  }
  return 0;
}
Problem p03112 (C++)

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

long long s[100000];
long long t[100000];
long long x[100000];

int binary_search(long long a[], int src, int dst, long long key) {
  int ok = 0;
  int ng = dst;
  while (ng - ok > 1) {
    int middle = (ok + ng) / 2;
    if (a[middle] >= a[key]) {
      ok = middle;
    } else {
      ng = middle;
    }
  }
  return ok;
}

int main() {

  int A, B, Q;
  std::cin >> A >> B >> Q;
  for (int i = 0; i < A; ++i) {
    std::cin >> s[i];
  }
  for (int i = 0; i < B; ++i) {
    std::cin >> t[i];
  }

  for (int i = 0; i < Q; ++i) {
    long long ans = 999999999999999999LL;
    std::cin >> x[i];
    int u = binary_search(s, 0, A, x[i]);
    int v = binary_search(t, 0, B, x[i]);
    for (int j = std::max(0, u - 10); j < std::min(A, u + 10); ++j) {
      for (int k = std::max(0, v - 10); k < std::min(B, v + 10); ++k) {
        ans = std::min(ans, std::abs(x[i] - s[j]) + std::abs(s[j] - t[k]));
        ans = std::min(ans, std::abs(x[i] - t[k]) + std::abs(t[k] - s[j]));
      }
    }
    std::cout << ans << std::endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

long long s[100000];
long long t[100000];
long long x[100000];

int binary_search(long long a[], int src, int dst, long long key) {
  int ok = 0;
  int ng = dst;
  while (ng - ok > 1) {
    int middle = (ok + ng) / 2;
    if (a[middle] <= key) {
      ok = middle;
    } else {
      ng = middle;
    }
  }
  return ok;
}

int main() {

  int A, B, Q;
  std::cin >> A >> B >> Q;
  for (int i = 0; i < A; ++i) {
    std::cin >> s[i];
  }
  for (int i = 0; i < B; ++i) {
    std::cin >> t[i];
  }

  for (int i = 0; i < Q; ++i) {
    long long ans = 999999999999999999LL;
    std::cin >> x[i];
    int u = binary_search(s, 0, A, x[i]);
    int v = binary_search(t, 0, B, x[i]);
    for (int j = std::max(0, u - 10); j < std::min(A, u + 10); ++j) {
      for (int k = std::max(0, v - 10); k < std::min(B, v + 10); ++k) {
        ans = std::min(ans, std::abs(x[i] - s[j]) + std::abs(s[j] - t[k]));
        ans = std::min(ans, std::abs(x[i] - t[k]) + std::abs(t[k] - s[j]));
      }
    }
    std::cout << ans << std::endl;
  }

  return 0;
}
Problem p03112 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define RANGE(i, n) for (ll i = 0; i < n; i++)
typedef long long ll;

using namespace std;

vector<ll> neighbor(vector<ll> arr, ll pos) {
  vector<ll> neighbors;
  if (pos <= arr[0]) {
    neighbors.push_back(arr[0]);
    return neighbors;
  } else if (pos >= arr.back()) {
    neighbors.push_back(arr.back());
    return neighbors;
  } else {
    ll left_id = 0;
    ll right_id = arr.size() - 1;
    while (right_id - left_id > 1) {
      ll mid_id = (left_id + right_id) / 2;
      if (arr[mid_id] <= pos)
        left_id = mid_id;
      else
        right_id = mid_id;
    }
    neighbors.push_back(arr[left_id]);
    neighbors.push_back(arr[right_id]);
    return neighbors;
  }
}

signed main() {
  ll A, B, Q;
  cin >> A >> B >> Q;
  vector<ll> s(A);
  vector<ll> t(B);
  RANGE(i, A) cin >> s[i];
  RANGE(i, B) cin >> t[i];
  RANGE(i, Q) {
    ll x;
    cin >> x;
    vector<ll> yashiro_cand = neighbor(s, x);
    vector<ll> tera_cand = neighbor(t, x);

    ll min_route = LLONG_MAX;
    for (ll yashiro : yashiro_cand) {
      for (ll tera : tera_cand) {
        ll route = min(abs(x - yashiro) + abs(yashiro - tera),
                       abs(x - tera) + abs(tera - yashiro));
        min_route = min(min_route, route);
      }
    }
    cout << min_route << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define RANGE(i, n) for (ll i = 0; i < n; i++)
typedef long long ll;

using namespace std;

vector<ll> neighbor(vector<ll> &arr, ll pos) {
  vector<ll> neighbors;
  if (pos <= arr[0]) {
    neighbors.push_back(arr[0]);
    return neighbors;
  } else if (pos >= arr.back()) {
    neighbors.push_back(arr.back());
    return neighbors;
  } else {
    ll left_id = 0;
    ll right_id = arr.size() - 1;
    while (right_id - left_id > 1) {
      ll mid_id = (left_id + right_id) / 2;
      if (arr[mid_id] <= pos)
        left_id = mid_id;
      else
        right_id = mid_id;
    }
    neighbors.push_back(arr[left_id]);
    neighbors.push_back(arr[right_id]);
    return neighbors;
  }
}

signed main() {
  ll A, B, Q;
  cin >> A >> B >> Q;
  vector<ll> s(A);
  vector<ll> t(B);
  RANGE(i, A) cin >> s[i];
  RANGE(i, B) cin >> t[i];
  RANGE(i, Q) {
    ll x;
    cin >> x;
    vector<ll> yashiro_cand = neighbor(s, x);
    vector<ll> tera_cand = neighbor(t, x);

    ll min_route = LLONG_MAX;
    for (ll yashiro : yashiro_cand) {
      for (ll tera : tera_cand) {
        ll route = min(abs(x - yashiro) + abs(yashiro - tera),
                       abs(x - tera) + abs(tera - yashiro));
        min_route = min(min_route, route);
      }
    }
    cout << min_route << endl;
  }
}
Problem p03112 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void chmin(long long &a, long long b) {
  if (a > b)
    a = b;
}
const long long INF = 1LL << 59;

// 前方の index
int former(const vector<long long> &v, long long x) {
  return (int)(upper_bound(v.begin(), v.end(), x) - v.begin()) - 1;
}

// 後方の index
int latter(const vector<long long> &v, long long x) {
  return (int)(lower_bound(v.begin(), v.end(), x) - v.begin());
}

int main() {
  int A, B, Q;
  cin >> A >> B >> Q;
  vector<long long> s(A), t(B);
  for (int i = 0; i < A; ++i)
    cin >> s[i];
  for (int i = 0; i < B; ++i)
    cin >> t[i];
  s.push_back(INF);
  s.push_back(-INF);
  t.push_back(INF);
  t.push_back(-INF);
  sort(s.begin(), s.end());
  sort(t.begin(), t.end());

  for (int _ = 0; _ < Q; ++_) {
    long long x;
    cin >> x;
    long long res = INF;

    // 最初に s (前方に行くか後方に行くか場合分け)
    for (int fl = 0; fl < 2; ++fl) {
      int sid = (fl == 0 ? former(s, x) : latter(s, x));
      // t の前か後ろか
      for (int fl2 = 0; fl2 < 2; ++fl2) {
        int tid = (fl2 == 0 ? former(t, s[sid]) : latter(s, s[sid]));
        chmin(res, abs(x - s[sid]) + abs(s[sid] - t[tid]));
      }
    }

    // 最初に t (前方に行くか後方に行くか場合分け)
    for (int fl = 0; fl < 2; ++fl) {
      int tid = (fl == 0 ? former(t, x) : latter(t, x));
      // t の前か後ろか
      for (int fl2 = 0; fl2 < 2; ++fl2) {
        int sid = (fl2 == 0 ? former(s, t[tid]) : latter(s, t[tid]));
        chmin(res, abs(x - t[tid]) + abs(t[tid] - s[sid]));
      }
    }

    cout << res << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void chmin(long long &a, long long b) {
  if (a > b)
    a = b;
}
const long long INF = 1LL << 59;

// 前方の index
int former(const vector<long long> &v, long long x) {
  return (int)(upper_bound(v.begin(), v.end(), x) - v.begin()) - 1;
}

// 後方の index
int latter(const vector<long long> &v, long long x) {
  return (int)(lower_bound(v.begin(), v.end(), x) - v.begin());
}

int main() {
  int A, B, Q;
  cin >> A >> B >> Q;
  vector<long long> s(A), t(B);
  for (int i = 0; i < A; ++i)
    cin >> s[i];
  for (int i = 0; i < B; ++i)
    cin >> t[i];
  s.push_back(INF);
  s.push_back(-INF);
  t.push_back(INF);
  t.push_back(-INF);
  sort(s.begin(), s.end());
  sort(t.begin(), t.end());

  for (int _ = 0; _ < Q; ++_) {
    long long x;
    cin >> x;
    long long res = INF;

    // 最初に s (前方に行くか後方に行くか場合分け)
    for (int fl = 0; fl < 2; ++fl) {
      int sid = (fl == 0 ? former(s, x) : latter(s, x));
      // t の前か後ろか
      for (int fl2 = 0; fl2 < 2; ++fl2) {
        int tid = (fl2 == 0 ? former(t, s[sid]) : latter(t, s[sid]));
        chmin(res, abs(x - s[sid]) + abs(s[sid] - t[tid]));
      }
    }

    // 最初に t (前方に行くか後方に行くか場合分け)
    for (int fl = 0; fl < 2; ++fl) {
      int tid = (fl == 0 ? former(t, x) : latter(t, x));
      // t の前か後ろか
      for (int fl2 = 0; fl2 < 2; ++fl2) {
        int sid = (fl2 == 0 ? former(s, t[tid]) : latter(s, t[tid]));
        chmin(res, abs(x - t[tid]) + abs(t[tid] - s[sid]));
      }
    }

    cout << res << endl;
  }
}
Problem p03112 (C++)

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

using namespace std;

#define INF 1e14 + 1

typedef long long ll;

template <class T> int former(const vector<T> &v, T x) {
  return upper_bound(v.begin(), v.end(), x) - v.begin() - 1;
}

template <class T> int latter(const vector<T> &v, T x) {
  return lower_bound(v.begin(), v.end(), x) - v.begin();
}

int main() {
  ll A, B, Q;
  cin >> A >> B >> Q;
  vector<ll> s(A), t(B), xx(Q);

  for (int i = 0; i < A; i++)
    cin >> s[i];
  for (int i = 0; i < B; i++)
    cin >> t[i];
  for (int i = 0; i < Q; i++)
    cin >> xx[i];

  s.push_back(INF);
  s.push_back(-INF);
  t.push_back(INF);
  t.push_back(-INF);

  sort(t.begin(), t.end());
  sort(s.begin(), s.end());

  for (int i = 0; i < Q; i++) {
    long long x = xx[i];
    long long res = INF;

    for (int i = 0; i < 2; i++) {
      long long first = (i ? s[former(s, x)] : s[latter(s, x)]);
      for (int j = 0; j < 2; j++) {
        long long second = (j ? t[former(t, first)] : t[latter(t, first)]);
        res = min(res, abs(x - first) + abs(first - second));
      }
    }
    for (int i = 0; i < 2; i++) {
      long long first = (i ? t[former(s, x)] : t[latter(s, x)]);
      for (int j = 0; j < 2; j++) {
        long long second = (j ? s[former(s, first)] : s[latter(s, first)]);
        res = min(res, abs(x - first) + abs(first - second));
      }
    }

    cout << res << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define INF 1e14 + 1

typedef long long ll;

template <class T> int former(const vector<T> &v, T x) {
  return upper_bound(v.begin(), v.end(), x) - v.begin() - 1;
}

template <class T> int latter(const vector<T> &v, T x) {
  return lower_bound(v.begin(), v.end(), x) - v.begin();
}

int main() {
  ll A, B, Q;
  cin >> A >> B >> Q;
  vector<ll> s(A), t(B), xx(Q);

  for (int i = 0; i < A; i++)
    cin >> s[i];
  for (int i = 0; i < B; i++)
    cin >> t[i];
  for (int i = 0; i < Q; i++)
    cin >> xx[i];

  s.push_back(INF);
  s.push_back(-INF);
  t.push_back(INF);
  t.push_back(-INF);

  sort(t.begin(), t.end());
  sort(s.begin(), s.end());

  for (int i = 0; i < Q; i++) {
    long long x = xx[i];
    long long res = INF;

    for (int i = 0; i < 2; i++) {
      long long first = (i ? s[former(s, x)] : s[latter(s, x)]);
      for (int j = 0; j < 2; j++) {
        long long second = (j ? t[former(t, first)] : t[latter(t, first)]);
        res = min(res, abs(x - first) + abs(first - second));
      }
    }
    for (int i = 0; i < 2; i++) {
      long long first = (i ? t[former(t, x)] : t[latter(t, x)]);
      for (int j = 0; j < 2; j++) {
        long long second = (j ? s[former(s, first)] : s[latter(s, first)]);
        res = min(res, abs(x - first) + abs(first - second));
      }
    }

    cout << res << endl;
  }
}
Problem p03112 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits.h>
#include <vector>

using namespace std;

int A, B, Q;
vector<long long> s, t, x;

int main() {

  cin >> A >> B >> Q;

  s.resize(A + 2);
  t.resize(B + 2);
  x.resize(Q);

  s[0] = LLONG_MIN / 4;
  s[A + 1] = LLONG_MAX / 4;
  t[0] = LLONG_MIN / 4;
  s[B + 1] = LLONG_MAX / 4;

  for (int i = 1; i <= A; i++)
    cin >> s[i];
  for (int i = 1; i <= B; i++)
    cin >> t[i];
  for (int i = 0; i < Q; i++)
    cin >> x[i];

  long long s_n[2], t_n[2];
  long long ans, d1, d2;
  for (int i = 0; i < Q; i++) {
    auto it = lower_bound(s.begin(), s.end(), x[i]);
    s_n[1] = it - s.begin();
    s_n[0] = s_n[1] - 1;

    it = lower_bound(t.begin(), t.end(), x[i]);
    t_n[1] = it - t.begin();
    t_n[0] = t_n[1] - 1;

    ans = LLONG_MAX / 2;

    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < 2; k++) {
        d1 = abs(x[i] - s[s_n[j]]) + abs(s[s_n[j]] - t[t_n[k]]);
        d2 = abs(x[i] - t[t_n[j]]) + abs(t[t_n[j]] - s[s_n[k]]);

        ans = min({ans, d1, d2});
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits.h>
#include <vector>

using namespace std;

int A, B, Q;
vector<long long> s, t, x;

int main() {

  cin >> A >> B >> Q;

  s.resize(A + 2);
  t.resize(B + 2);
  x.resize(Q);

  s[0] = LLONG_MIN / 4;
  s[A + 1] = LLONG_MAX / 4;
  t[0] = LLONG_MIN / 4;
  t[B + 1] = LLONG_MAX / 4;

  for (int i = 1; i <= A; i++)
    cin >> s[i];
  for (int i = 1; i <= B; i++)
    cin >> t[i];
  for (int i = 0; i < Q; i++)
    cin >> x[i];

  long long s_n[2], t_n[2];
  long long ans, d1, d2;
  for (int i = 0; i < Q; i++) {
    auto it = lower_bound(s.begin(), s.end(), x[i]);
    s_n[1] = it - s.begin();
    s_n[0] = s_n[1] - 1;

    it = lower_bound(t.begin(), t.end(), x[i]);
    t_n[1] = it - t.begin();
    t_n[0] = t_n[1] - 1;

    ans = LLONG_MAX / 2;

    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < 2; k++) {
        d1 = abs(x[i] - s[s_n[j]]) + abs(s[s_n[j]] - t[t_n[k]]);
        d2 = abs(x[i] - t[t_n[j]]) + abs(t[t_n[j]] - s[s_n[k]]);

        ans = min({ans, d1, d2});
      }
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p03112 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
const int maxn = 100000 + 5;
#define INF 1e14
int a, b, q;
long long s[maxn], t[maxn];
vector<long long> ss, tt;

int main() {
  while (scanf("%d%d%d", &a, &b, &q) == 3) {
    ss.clear();
    tt.clear();
    for (int i = 0; i < a; i++)
      scanf("%lld", &s[i]);
    for (int i = 0; i < b; i++)
      scanf("%lld", &t[i]);
    long long pos;
    while (q--) {
      scanf("%lld", &pos);
      int right_s = lower_bound(s, s + a, pos) - s;
      if (right_s < a)
        ss.push_back(s[right_s]);
      int left_s = right_s - 1;
      if (left_s >= 0)
        ss.push_back(s[left_s]);

      int right_t = lower_bound(t, t + b, pos) - t;
      if (right_t < b)
        tt.push_back(t[right_t]);
      int left_t = right_t - 1;
      if (left_t >= 0)
        tt.push_back(t[left_t]);

      long long ans = INF;
      for (unsigned i = 0; i < ss.size(); i++) {
        for (unsigned j = 0; j < tt.size(); j++) {
          long long t1 = abs(pos - ss[i]) + abs(ss[i] - tt[j]),
                    t2 = abs(pos - tt[j]) + abs(tt[j] - ss[i]);
          ans = min(ans, min(t1, t2));
        }
      }
      printf("%lld\n", ans);
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
const int maxn = 100000 + 5;
#define INF 1e14
int a, b, q;
long long s[maxn], t[maxn];
vector<long long> ss, tt;

int main() {
  while (scanf("%d%d%d", &a, &b, &q) == 3) {
    ss.clear();
    tt.clear();
    for (int i = 0; i < a; i++)
      scanf("%lld", &s[i]);
    for (int i = 0; i < b; i++)
      scanf("%lld", &t[i]);
    long long pos;
    while (q--) {
      scanf("%lld", &pos);
      ss.clear();
      tt.clear();
      int right_s = lower_bound(s, s + a, pos) - s;
      if (right_s < a)
        ss.push_back(s[right_s]);
      int left_s = right_s - 1;
      if (left_s >= 0)
        ss.push_back(s[left_s]);

      int right_t = lower_bound(t, t + b, pos) - t;
      if (right_t < b)
        tt.push_back(t[right_t]);
      int left_t = right_t - 1;
      if (left_t >= 0)
        tt.push_back(t[left_t]);

      long long ans = INF;
      for (unsigned i = 0; i < ss.size(); i++) {
        for (unsigned j = 0; j < tt.size(); j++) {
          long long t1 = abs(pos - ss[i]) + abs(ss[i] - tt[j]),
                    t2 = abs(pos - tt[j]) + abs(tt[j] - ss[i]);
          ans = min(ans, min(t1, t2));
        }
      }
      printf("%lld\n", ans);
    }
  }

  return 0;
}
Problem p03112 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using P = pair<int, int>;
const ll INF = 1LL << 58;
int main() {
  int a, b, q;
  cin >> a >> b >> q;
  vector<ll> s(a + 2), t(b + 2);
  rep(i, a) cin >> s[i + 1];
  rep(i, b) cin >> t[i + 1];
  s[0] = t[0] = -INF;
  s[a + 1] = t[a + 1] = INF;
  sort(s.begin(), s.end());
  sort(t.begin(), t.end());

  vector<ll> p(q);
  rep(i, q) {
    ll x;
    cin >> x;
    ll ans = INF;
    ll al = lower_bound(s.begin(), s.end(), x) - s.begin();
    ll bl = lower_bound(t.begin(), t.end(), x) - t.begin();
    ll af = upper_bound(s.begin(), s.end(), x) - s.begin() - 1;
    ll bf = upper_bound(t.begin(), t.end(), x) - t.begin() - 1;
    ans = min(ans, abs(s[af] - x) + abs(s[af] - t[bf]));
    ans = min(ans, abs(s[af] - x) + abs(s[af] - t[bl]));
    ans = min(ans, abs(s[al] - x) + abs(s[al] - t[bl]));
    ans = min(ans, abs(s[al] - x) + abs(s[al] - t[bf]));
    ans = min(ans, abs(t[bf] - x) + abs(s[af] - t[bf]));
    ans = min(ans, abs(t[bf] - x) + abs(s[al] - t[bf]));
    ans = min(ans, abs(t[bl] - x) + abs(s[af] - t[bl]));
    ans = min(ans, abs(t[bl] - x) + abs(s[al] - t[bl]));

    p[i] = ans;
  }
  rep(i, q) cout << p[i] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using P = pair<int, int>;
const ll INF = 1LL << 58;
int main() {
  int a, b, q;
  cin >> a >> b >> q;
  vector<ll> s(a + 2), t(b + 2);
  rep(i, a) cin >> s[i + 1];
  rep(i, b) cin >> t[i + 1];
  s[0] = t[0] = -INF;
  s[a + 1] = t[b + 1] = INF;
  sort(s.begin(), s.end());
  sort(t.begin(), t.end());

  vector<ll> p(q);
  rep(i, q) {
    ll x;
    cin >> x;
    ll ans = INF;
    ll al = lower_bound(s.begin(), s.end(), x) - s.begin();
    ll bl = lower_bound(t.begin(), t.end(), x) - t.begin();
    ll af = upper_bound(s.begin(), s.end(), x) - s.begin() - 1;
    ll bf = upper_bound(t.begin(), t.end(), x) - t.begin() - 1;
    ans = min(ans, abs(s[af] - x) + abs(s[af] - t[bf]));
    ans = min(ans, abs(s[af] - x) + abs(s[af] - t[bl]));
    ans = min(ans, abs(s[al] - x) + abs(s[al] - t[bl]));
    ans = min(ans, abs(s[al] - x) + abs(s[al] - t[bf]));
    ans = min(ans, abs(t[bf] - x) + abs(s[af] - t[bf]));
    ans = min(ans, abs(t[bf] - x) + abs(s[al] - t[bf]));
    ans = min(ans, abs(t[bl] - x) + abs(s[af] - t[bl]));
    ans = min(ans, abs(t[bl] - x) + abs(s[al] - t[bl]));

    p[i] = ans;
  }
  rep(i, q) cout << p[i] << endl;

  return 0;
}