alexjercan/bugnet huggingface dataset:
Problem p00462 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <iostream>
using namespace std;
bool index(int s, int k) {
  if (s > k)
    return true;
  else
    return false;
}
int main() {
  int d, n, m;
  while (cin >> d, d) {
    cin >> n >> m;
    int s[n + 1], k, ans = 0;
    for (int i = 1; i < n; i++)
      cin >> s[i];
    s[0] = 0;
    s[n] = d;
    sort(s, s + n + 1);
    for (int i = 0; i < m; i++) {
      cin >> k;
      int max = n;
      int mins = 0;
      while (max != mins + 1) {
        int a = (max + mins) / 2;
        if (index(s[a], k))
          max = a;
        else
          mins = a;
      }
      ans += min(abs(k - s[max]), abs(k - s[mins]));
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
using namespace std;
bool index(int s, int k) {
  if (k < s)
    return true;
  return false;
}
int main() {
  int d, n, m;
  while (cin >> d, d) {
    cin >> n >> m;
    int s[d], k, ans = 0;
    s[0] = 0;
    for (int i = 1; i < n; i++)
      cin >> s[i];
    sort(s, s + n);
    for (int i = 0; i < m; i++) {
      cin >> k;
      int max = n - 1;
      int mins = 0;
      if (s[max] == k)
        continue;
      while (max - mins > 1) {
        int a = (max + mins) / 2;
        if (index(s[a], k))
          max = a;
        else
          mins = a;
      }
      if (abs(k - s[max]) < abs(k - s[mins])) {
        if (max == n - 1)
          ans += min(abs(k - s[max]), abs(k - d));
        else
          ans += abs(k - s[max]);
      } else if (abs(k - s[max]) >= abs(k - s[mins]))
        ans += abs(k - s[mins]);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00462 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)

int main() {
  int d, n, m;
  while (std::cin >> d, d) {
    std::cin >> n >> m;

    int D[100001];
    D[0] = 0;
    D[n] = d;

    FOR(i, 1, n) { std::cin >> D[i]; }

    std::sort(D, D + n + 1);

    int res = 0;

    REP(i, m) {
      int k;
      std::cin >> k;

      int pos = std::lower_bound(D, D + n + 1, k) - D, dist = D[pos] - k;

      if (pos - 1 >= 0) {
        dist = std::min(dist, k - D[pos - 1]);
      }

      res += dist;
    }

    std::cout << res << std::endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)

const int INF = 1 << 24;

int d, n, m;
std::vector<int> D[10010];

int main() {
  while (std::cin >> d, d) {
    REP(i, 10010) { D[i].clear(); }

    std::cin >> n >> m;

    D[0].push_back(0);
    D[d / 10000].push_back(d);
    FOR(i, 1, n) {
      int r;
      std::cin >> r;
      D[r / 10000].push_back(r);
    }

    int res = 0;
    REP(i, m) {
      int k;
      std::cin >> k;

      int diff = INF;

      for (int j = -1; j <= 1; j++) {
        int index = k / 10000 + j;
        if (0 <= index && index <= d / 10000)
          ;
        else {
          continue;
        }

        for (int l = 0; l < D[index].size(); l++) {
          diff = std::min(diff, std::abs(D[index][l] - k));
        }
      }

      res += diff;
    }

    std::cout << res << std::endl;
  }
}
Problem p00462 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

const static int INFTY = (1 << 21);
const static int MAXM = 10000;

int d, n, m;
int order[MAXM];

void solve() {
  vector<int> shops;
  shops.push_back(0);
  shops.push_back(d);
  int temp;
  int res = 0;
  for (int i = 0; i < n - 1; i++) {
    scanf("%d", &temp);
    shops.push_back(temp);
    shops.push_back(temp + d);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d", &order[i]);
  }
  sort(shops.begin(), shops.end());
  for (int i = 0; i < m; i++) {
    int mid, right = shops.size() - 1, left = 0, minv = INFTY;
    while (left <= right) {
      mid = (left + right) / 2;
      if (shops[mid] < order[i]) {
        minv = min(minv, order[i] - shops[mid]);
        left = mid + 1;
      } else {
        minv = min(minv, shops[mid] - order[i]);
        right = mid - 1;
      }
    }
    res += minv;
  }
  printf("%d\\n", res);
}

int main() {
  while (true) {
    scanf("%d", &d);
    if (d == 0)
      break;
    scanf("%d%d", &n, &m);
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#define MAXN 100000
#define MAXM 10000
using namespace std;

int d, n, m;
int sell[MAXN];
int order[MAXM];

void solve() {
  int res = 0;
  // sort(sell,sell+n);
  for (int i = 0; i < m; i++) {
    int minDis = 2000000000;
    for (int j = 0; j < n; j++) {
      int temp;
      if (sell[j] <= order[i]) {
        temp = min(order[i] - sell[j], sell[j] + d - order[i]);
      } else {
        temp = min(sell[j] - order[i], d - sell[j] + order[i]);
      }
      minDis = min(minDis, temp);
    }
    res += minDis;
  }
  cout << res << endl;
}

void input() {
  while (cin >> d) {
    if (d == 0)
      break;
    cin >> n >> m;
    sell[0] = 0;
    for (int i = 1; i < n; i++) {
      cin >> sell[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> order[i];
    }
    solve();
  }
}

int main() { input(); }
Problem p00462 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

int main() {
  while (true) {
    int d, n, m;
    vector<int> s;
    scanf("%d", &d);
    if (d == 0)
      break;
    scanf("%d%d", &n, &m);
    s.resize(n + 1);
    for (int i = 0; i < n - 1; ++i)
      scanf("%d", &s[i]);
    s[n - 1] = 0;
    s[n] = d;
    sort(s.begin(), s.end());
    s.push_back(s[n - 1] - d);
    sort(s.begin(), s.end());
    int sum = 0;
    for (int i = 0; i < m; ++i) {
      int t;
      scanf("%d", &t);
      int r = lower_bound(s.begin(), s.end(), t) - s.begin();
      int l = r - 1;
      sum += min(abs(s[l] - t), abs(s[r] - t));
    }
    printf("%d\\n", sum);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

int main() {
  while (true) {
    int d, n, m;
    vector<int> s;
    scanf("%d", &d);
    if (d == 0)
      break;
    scanf("%d%d", &n, &m);
    s.resize(n);
    for (int i = 0; i < n; ++i)
      scanf("%d", &s[i]);
    sort(s.begin(), s.end());
    s.push_back(s[0] + d);
    s.push_back(s[n - 1] - d);
    sort(s.begin(), s.end());
    int sum = 0;
    for (int i = 0; i < m; ++i) {
      int t;
      scanf("%d", &t);
      int r = lower_bound(s.begin(), s.end(), t) - s.begin();
      int l = r - 1;
      sum += min(abs(s[l] - t), abs(s[r] - t));
    }
    printf("%d\\n", sum);
  }
  return 0;
}
Problem p00462 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 1e5;

int main() {
  int d, n, m;

  while (cin >> d, d) {
    vector<int> cu, sh;
    int c, s;
    sh.push_back(0);

    cin >> n >> m;
    for (int i = 1; i < n; i++) {
      cin >> s;
      sh.push_back(s);
    }
    sh.push_back(d);
    for (int i = 0; i < m; i++) {
      cin >> c;
      cu.push_back(c);
    }

    int sum = 0;
    sort(sh.begin(), sh.end());
    sort(cu.begin(), cu.end());
    int pos = 1;
    for (int i = 0; i < m;) {
      if (sh[pos] >= cu[i]) {
        sum += min(sh[pos] - cu[i], cu[i] - sh[pos - 1]);
        i++;
      } else
        ++pos;
    }
    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 1e5;

int main() {
  int d, n, m;

  while (cin >> d, d) {
    vector<int> cu;
    int sh[MAX + 1];
    int c;
    sh[0] = 0;

    cin >> n >> m;
    sh[n] = d;
    for (int i = 1; i < n; i++) {
      cin >> sh[i];
    }
    int sum = 0;
    for (int i = 0; i < m; i++) {
      cin >> c;
      cu.clear();
      for (int j = 0; j <= n; j++) {
        cu.push_back(abs(c - sh[j]));
      }
      sort(cu.begin(), cu.end());
      sum += cu[0];
    }

    cout << sum << endl;
  }

  return 0;
}
Problem p00462 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  long long d;
  while (cin >> d, d) {
    int n, m;
    cin >> n;
    cin >> m;
    vector<int> store(n + 1), dist(m);
    store[0] = 0;
    for (int i = 1; i < n; i++)
      cin >> store[i];
    store[n] = d;
    for (int i = 0; i < m; i++)
      cin >> dist[i];
    sort(store.begin(), store.end());
    long long sum = 0;
    for (int i = 0; i < m; i++) {
      // cout << "a" << endl;
      int left = 0, right = n;
      while (1) {
        // cout << "b" << endl;
        int next = (left + right) / 2;
        if (next == left || next == right)
          break;
        if (dist[i] < store[next])
          right = next;
        else
          left = next;
      }
      sum += min(abs(store[left] - dist[i]), abs(store[right] - dist[i]));
      // cout << sum << endl;
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int d;
  while (cin >> d, d) {
    int n, m;
    cin >> n;
    cin >> m;
    vector<int> store(n + 1), dist(m);
    store[0] = 0;
    for (int i = 1; i < n; i++)
      cin >> store[i];
    store[n] = d;
    for (int i = 0; i < m; i++)
      cin >> dist[i];
    sort(store.begin(), store.end());
    int sum = 0;
    for (int i = 0; i < m; i++) {
      int left = 0, right = n;
      while (1) {
        int next = (left + right) / 2;
        if (next == left || next == right)
          break;
        if (dist[i] < store[next])
          right = next;
        if (store[next] < dist[i])
          left = next;
      }
      sum += (abs(store[left] - dist[i]) < abs(store[right] - dist[i]))
                 ? abs(store[left] - dist[i])
                 : abs(store[right] - dist[i]);
      // cout << sum << endl;
    }
    cout << sum << endl;
  }
}
Problem p00462 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int to_int(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string to_str(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef long long ll;

int main() {
  // Flength:環状線の円周 store:店の数 order:注文の数 ans:answer
  ll Flength, store, order, ans;
  vector<ll> locate;

  while (cin >> Flength) {
    if (Flength == 0)
      break;

    cin >> store >> order;

    ans = 0;
    locate.clear();

    ll wk;
    for (int i = 0; i < store - 1; i++) {
      cin >> wk;
      locate.push_back(wk);
    }
    locate.push_back(0);
    locate.push_back(Flength);

    sort(locate.begin(), locate.end());

    for (int i = 0; i < order; i++) {
      cin >> wk;
      for (int i = 0; i < locate.size(); i++) {
        if (locate[i] > wk) {
          ans += min(locate[i] - wk, wk - locate[i - 1]);
          break;
        }
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int to_int(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string to_str(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef long long ll;

int main() {
  // Flength:環状線の全長 store:店の数 order:注文の数 ans:answer
  int Flength, store, order, ans;
  // dist[0]store1~store2間の距離 dist[2]store2~store3間の距離 ....
  int locate[100002];
  // 注文の場所
  int place[10001];

  while (cin >> Flength) {
    if (Flength == 0)
      break;

    cin >> store >> order;

    memset(locate, 0, sizeof(locate));
    ans = 0;
    locate[0] = 1;

    int cnt = 0, wk;
    for (int i = 0; i < store - 1; i++) {
      cin >> wk;
      locate[wk] = 1;
    }

    int j = wk, k = wk;
    cnt = 0;
    // Flengthと0を通ったらtrue;
    bool right = false, left = false;

    for (int i = 0; i < order; i++) {
      cin >> wk;

      cnt = 0;
      j = wk;
      k = wk;
      right = false;
      left = false;
      while (1) {
        if (locate[j] || locate[k]) {
          ans += cnt;
          break;
        }

        j++;
        k--;
        cnt++;

        if (j == Flength) {
          j = 0;
        }
        if (k == 0) {
          k = Flength;
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00462 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

vector<int> D, K;
int d, m, n, t;

int dist(int a, int b) {
  return min(abs(((a + d) - b) % d), abs(((b + d) - a) % d));
}

int main(void) {
  while (true) {
    D.clear();
    K.clear();
    cin >> d;
    if (d == 0)
      exit(0);
    cin >> n;
    cin >> m;
    D.push_back(0);
    for (int i = 0; i < n - 1; i++) {
      cin >> t;
      D.push_back(t);
    }
    for (int i = 0; i < m; i++) {
      cin >> t;
      K.push_back(t);
    }
    sort(D.begin(), D.end());
    sort(K.begin(), K.end());
    auto ptr = D.begin();
    int sum = 0;
    int mindist = dist(*(D.begin()), *(K.begin()));
    auto minitr = D.begin();
    for (auto itr = D.begin(); itr < D.end(); itr++) {
      if (mindist > dist(*itr, *(K.begin()))) {
        minitr = itr;
        mindist = dist(*itr, *(K.begin()));
      }
    }
    ptr = minitr;
    for (auto itr = K.begin(); itr < K.end(); itr++) {
      if (ptr + 1 != D.end()) {
        while (dist(*(ptr + 1), *itr) < dist(*ptr, *itr)) {
          *ptr++;
        }
      } else {
        if (dist(*(D.begin()), *itr) < dist(*ptr, *itr)) {
          ptr = D.begin();
        }
      }
      t = dist(*ptr, *itr);
      sum = sum + t;
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstdint>
#include <iostream>

using namespace std;

#define MAX_N 100002
#define MAX_M 10001

int64_t D, N, M, d[MAX_N], k[MAX_M];

int64_t _abs(int64_t a) {
  if (a < 0)
    return a * -1;
  else
    return a;
}

int64_t _min(int64_t a, int64_t b) {
  if (a > b)
    return b;
  else
    return a;
}

int64_t gd(int64_t a, int64_t b) {
  return _min(_abs(a - b), _abs(_abs(a - b) - D));
}

int main(void) {
  while ((cin >> D), D != 0) {
    cin >> N;
    cin >> M;
    for (int i = 2; i <= N; i++) {
      cin >> d[i];
    }
    for (int i = 1; i <= M; i++) {
      cin >> k[i];
    }
    int64_t res = 0;
    for (int i = 1; i <= M; i++) {
      int64_t sts = D;
      for (int ii = 1; ii <= N; ii++) {
        sts = _min(sts, gd(k[i], d[ii]));
      }
      res += sts;
    }
    cout << res << endl;
  }
  return 0;
}
Problem p00462 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  while (true) {

    vector<int> order;
    vector<int> store;
    int r = 0;
    int d, n, m;
    cin >> d;
    if (d == 0)
      break;
    cin >> n >> m;
    store.push_back(0);
    for (int i = 1; i < n; i++) {
      cin >> r;
      store.push_back(r);
    }
    store.push_back(d);
    for (int i = 0; i < m; i++) {
      cin >> r;
      order.push_back(r);
    }
    sort(store.begin(), store.end());
    sort(order.begin(), order.end());
    int now = 0, sum = 0;
    for (int i = 0; i < m; i++) {
      int p = order[i];
      while (!(p >= store[now] && p <= store[now + 1]))
        now++;
      sum += min(p - store[now], store[now + 1] - p);
    }

    cout << sum << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> order;
vector<int> store;
int main() {
  while (true) {
    int r = 0;
    int d, n, m;
    cin >> d >> n >> m;
    if (d == 0 && n == 0 && m == 0)
      break;
    store.push_back(0);
    for (int i = 1; i < n; i++) {
      cin >> r;
      store.push_back(r);
    }
    store.push_back(d);
    for (int i = 0; i < m; i++) {
      cin >> r;
      order.push_back(r);
    }
    sort(store.begin(), store.end());
    sort(order.begin(), order.end());
    int now = 0, sum = 0;
    for (int i = 0; i < m; i++) {
      int p = order[i];
      while (!(p >= store[now] && p <= store[now + 1]))
        now++;
      sum += min(p - store[now], store[now + 1] - p);
    }

    cout << sum << endl;
  }
  return 0;
}
Problem p00462 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int main() {

  int n, d, m;
  vector<int> pizza;

  while (cin >> d, d) {
    cin >> n >> m;
    pizza.resize(n + 1);
    pizza[0] = 0, pizza[n] = d;
    for (int i = 1; i < n; i++)
      cin >> pizza[i];
    sort(pizza.begin(), pizza.end());

    int sum = 0;
    for (int i = 0; i < m; i++) {
      int q;
      cin >> q;
      vector<int>::iterator it = lower_bound(pizza.begin(), pizza.end(), q);
      sum += min(abs(*it - q), abs(*(it - 1) - q));
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int n, d, m, k[100001];

int main() {

  while (cin >> d >> n >> m, d) {
    k[0] = 0;
    for (int i = 1; i < n; i++)
      cin >> k[i];

    int sum = 0;
    for (int i = 0; i < m; i++) {
      int q;
      cin >> q;
      int ret = INF;
      for (int j = 0; j < n; j++) {
        ret = min(ret, min(abs(q - k[j]), k[j] + (d - q)));
      }
      sum += ret;
      // cout << sum << endl;
    }
    cout << sum << endl;
  }
}
Problem p00462 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
int main() {

  int d, n, m;
  while (cin >> d, d) {
    cin >> n >> m;
    int D[100001], M[100001];
    ;
    D[0] = 0, D[n] = d;
    for (int i = 1; i < n; i++)
      cin >> D[i];
    for (int i = 0; i < m; i++)
      cin >> M[i];
    sort(M, M + m);
    sort(D, D + n + 1);

    ll ans = 0;
    int pos = 1;
    for (int i = 0; i < m; i++) {
      while (D[pos] <= M[i])
        pos++;
      ans += min(abs(D[pos] - M[i]), abs(D[pos - 1] - M[i]));
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
int main() {

  int d, n, m;
  while (cin >> d, d) {
    cin >> n >> m;
    int D[10001], M[10001];
    ;
    D[0] = 0, D[n] = d;
    for (int i = 1; i < n; i++)
      cin >> D[i];
    for (int i = 0; i < m; i++)
      cin >> M[i];
    sort(M, M + m);
    sort(D, D + n + 1);

    ll ans = 0;
    int pos = 1;
    for (int i = 0; i < m; i++) {
      while (D[pos] <= M[i])
        pos++;
      ans += min(abs(D[pos] - M[i]), abs(D[pos - 1] - M[i]));
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00462 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

static const int tx[] = {0, 1, 0, -1};
static const int ty[] = {-1, 0, 1, 0};

static const int MAX_BUCKET = 1000;
vector<int> bucket[MAX_BUCKET];

int compute_dist(int dist, int lhs, int rhs) {
  return min(dist - abs(lhs - rhs), abs(lhs - rhs));
}

int main() {
  int total_dist, store_num, order_num;
  while (~scanf("%d", &total_dist)) {
    if (total_dist == 0)
      break;
    scanf("%d", &store_num);
    scanf("%d", &order_num);

    vector<int> store;
    store.push_back(0);
    for (int i = 0; i < store_num - 1; i++) {
      int num;
      scanf("%d", &num);
      store.push_back(num);
    }
    sort(store.begin(), store.end());

    int cost = 0;
    for (int i = 0; i < order_num; i++) {
      int num;
      scanf("%d", &num);

      int idx = lower_bound(store.begin(), store.end(), num) - store.begin();
      int min_dist = compute_dist(total_dist, store[idx], num);

      for (int j = idx - 1; j >= 0; j--) {
        min_dist = min(min_dist, compute_dist(total_dist, store[j], num));
      }
      cost += min_dist;
    }
    printf("%d\\n", cost);
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

static const int tx[] = {0, 1, 0, -1};
static const int ty[] = {-1, 0, 1, 0};

static const int MAX_BUCKET = 1000;
vector<int> bucket[MAX_BUCKET];

int compute_dist(int dist, int lhs, int rhs) {
  return min(dist - abs(lhs - rhs), abs(lhs - rhs));
}

int main() {
  int total_dist, store_num, order_num;
  while (~scanf("%d", &total_dist)) {
    if (total_dist == 0)
      break;
    scanf("%d", &store_num);
    scanf("%d", &order_num);

    vector<int> store;
    store.push_back(0);
    for (int i = 0; i < store_num - 1; i++) {
      int num;
      scanf("%d", &num);
      store.push_back(num);
    }
    sort(store.begin(), store.end());

    int cost = 0;
    for (int i = 0; i < order_num; i++) {
      int num;
      scanf("%d", &num);

      int min_dist = INF;
      for (int j = 0; j < store.size(); j++) {
        min_dist = min(min_dist, compute_dist(total_dist, store[j], num));
      }
      cost += min_dist;
    }
    printf("%d\\n", cost);
  }
}