alexjercan/bugnet huggingface dataset:
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#define max(i, j) (i > j ? i : j)

int main(void) {
  int n, m, l, ans, cnt;
  char s[1000001], o[3] = "IO";
  for (;;) {
    scanf("%d", &n);
    if (n == 0)
      break;
    scanf("%d%s", &m, s);
    l = 2 * n + 1;
    ans = 0;
    for (int i = 0; i < m; i++) {
      int j = 0;
      cnt = 0;
      while (i + j < m && s[i + j] == o[j % 2]) {
        cnt++;
        j++;
      }
      while (cnt >= l) {
        ans++;
        cnt -= 2;
      }
      // printf("%d %d\\n",i,j);
      i += max(j - 1, 0);
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
  int n, m, l, i, j, ans;
  char s[1000001], o[3] = "IO";
  for (;;) {
    scanf("%d", &n);
    if (n == 0)
      break;
    scanf("%d%s", &m, s);
    l = 2 * n + 1;
    ans = 0;
    for (i = 0; i < m - l; i++) {
      for (j = 0; j < l; j++)
        if (s[i + j] != o[j % 2])
          break;
      if (j == l)
        ans++;
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef vector<char> VC;

int main(void) {
  int n, m;
  while (cin >> n && n) {
    const int size = 2 * n + 1;
    VC ioi(size, 'I');
    for (int i = 1; i < size; i += 2)
      ioi[i] = 'O';

    cin >> m;
    VC s(m);
    for (int i = 0; i < m; i++)
      cin >> s[i];

    int answer = 0;
    int cnt;
    for (int i = 0; i < m - 1; i++) {
      if (s[i] == 'I') {
        cnt = 1;
        for (int j = i + 1; j < m; j += 2) {
          if (s[j] == 'O' && s[j + 1] == 'I') {
            cnt += 2;
            if (cnt == size) {
              cnt -= 2;
              answer++;
            }
          } else {
            i = j - 1;
            break;
          }
        }
      }
    }

    cout << answer << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

int main(void) {
  int n, m;
  while (cin >> n && n) {
    string ioistring = "IOI";
    for (int i = 1; i < n; i++)
      ioistring += "OI";

    string s;
    cin >> m;
    for (int i = 0; i < m; i++) {
      char c;
      cin >> c;
      s += c;
    }

    int cnt = 0;
    for (int i = 0; i < m - ioistring.size(); i++) {
      if (s.substr(i, ioistring.size()) == ioistring) {
        i++;
        cnt++;
      }
    }

    cout << cnt << endl;
  }
  return 0;
}
Problem p00462 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
long long a[100000], b, n, m, d;
long long dist(long long p1, long long p2) {
  return min((p2 - p1 + d) % d, (p1 - p2 + d) % d);
}
int main() {
  while (true) {
    cin >> d;
    if (d == 0)
      break;
    cin >> n >> m;
    for (int i = 1; i < n; i++)
      cin >> a[i];
    sort(a, a + n);
    long long sum = 0;
    for (int i = 0; i < m; i++) {
      cin >> b;
      long long pos1 = lower_bound(a, a + n, b) - a;
      pos1 %= n;
      sum += min(dist(a[pos1], b), dist(a[(pos1 + n - 1) % n], b));
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
long long a[100000], b, n, m, d;
long long dist(long long p1, long long p2) {
  return min((p2 - p1 + d) % d, (p1 - p2 + d) % d);
}
int main() {
  while (true) {
    cin >> d;
    if (d == 0)
      break;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    sort(a, a + n);
    long long sum = 0;
    for (int i = 0; i < m; i++) {
      cin >> b;
      long long pos1 = lower_bound(a, a + n, b) - a;
      pos1 %= n;
      sum += min(dist(a[pos1], b), dist(a[(pos1 + n - 1) % d], b));
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00462 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
using namespace std;
#define INF (1 << 29)
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n, m, d, k;
  int dist[100001];
  while (cin >> d && d) {
    int ans = 0;
    cin >> n >> m;
    rep(i, n - 1) { cin >> dist[i + 1]; }
    sort(dist + 1, dist + n);
    dist[0] = 0;
    dist[n] = d;
    while (m--) {
      cin >> k;
      int i = upper_bound(dist, dist + n + 1, k) - dist;
      ans += min(dist[i] - k, k - dist[i - 1]);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
using namespace std;
#define INF (1 << 29)
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n, m, d, k;
  int dist[100000];
  while (cin >> d && d) {
    int ans = 0;
    cin >> n >> m;
    rep(i, n - 1) { cin >> dist[i + 1]; }
    sort(dist + 1, dist + n);
    dist[0] = 0;
    dist[n] = d;
    while (m--) {
      cin >> k;
      int i = upper_bound(dist, dist + n + 1, k) - dist;
      ans += min(dist[i] - k, k - dist[i - 1]);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00462 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define Rep(i, N) for (int i = 0; i < N; i++)
#define INF 1 << 28
typedef pair<int, int> Pi;
#define pb push_back
#define fi first
#define se second

int main() {
  int D, N, M;
  int A, B;
  while (cin >> D, D) {
    cin >> N >> M;
    vector<int> d;
    d.pb(-114514);
    d.pb(0);
    Rep(i, N - 1) {
      cin >> A;
      d.pb(A);
    }
    d.pb(D);

    sort(d.begin(), d.end());
    int sum = 0;
    while (M--) {
      cin >> A;
      int pl = lower_bound(d.begin(), d.end(), A) - d.begin();
      sum += min(d[pl] - A, A - d[pl - 1]);
    }
    cout << sum << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define Rep(i, N) for (int i = 0; i < N; i++)
#define INF 1 << 28
typedef pair<int, int> Pi;
#define pb push_back
#define fi first
#define se second

int main() {
  int D, N, M;
  while (cin >> D, D) {
    cin >> N >> M;
    int A;
    int mas[1000005];
    int d[100005];

    fill_n(mas, 1000005, INF);

    d[0] = 0;
    for (int i = 1; i < N; i++) {
      cin >> d[i];
    }
    d[N] = D;

    sort(d, d + N + 1);
    Rep(i, N) {
      int m = (d[i] + d[i + 1]) / 2;
      for (int j = d[i], cnt = 0; j <= m; j++, cnt++) {
        mas[j % D] = min(mas[j % D], cnt);
      }
      for (int j = d[i + 1], cnt = 0; j >= m; j--, cnt++) {
        mas[j % D] = min(mas[j % D], cnt);
      }
    }

    int sum = 0;
    while (M--) {
      cin >> A;
      sum += mas[A];
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00462 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int f(int d, vector<int> &s, int q) {
  if (s.back() <= q) {
    return min(q - s.back(), d - q);
  }

  int x = 0;
  int y = s.size() - 1;
  while (x <= y) {
    int h = (x + y) / 2;
    if (s[h] == q)
      return 0;
    if (s[h] < q) {
      x = h + 1;
    } else {
      y = h - 1;
    }
  }
  int mind = d;
  for (int i = max(x - 2, 0); i <= min(x + 2, d - 1); i++) {
    int dd = abs(q - s[i]);
    if (dd > d - dd)
      dd = d - dd;
    if (dd < mind)
      mind = dd;
  }
  return mind;
}

int main() {
  int d, n, m, q;
  istream &sin = cin;
  ostream &sout = cout;
  //	fstream sin("input.txt",fstream::in);
  //	fstream sout("output.txt",fstream::out);

  while (true) {
    sin >> d >> n >> m;
    if (d == 0)
      break;

    vector<int> s(n);
    for (int i = 1; i < n; i++) {
      sin >> s[i];
    }
    sort(s.begin(), s.end());
    int ans = 0;
    for (int i = 0; i < m; i++) {
      sin >> q;
      int mind = f(d, s, q);
      /*		int mind=d;
                      for(int j=0;j<n;j++){
                              int dd = abs(s[j]-q);
                              if(dd*2>d) dd=d-dd;
                              if(dd<mind){
                                      mind=dd;
                              }
                      }
                      sout << mind << endl;
      */
      ans += mind;
    }
    sout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int d, n, m, q;
  istream &sin = cin;
  ostream &sout = cout;
  //	fstream sin("input.txt",fstream::in);
  //	fstream sout("output.txt",fstream::out);

  while (true) {
    sin >> d >> n >> m;
    if (d == 0)
      break;

    vector<int> s(n);
    for (int i = 1; i < n; i++) {
      sin >> s[i];
    }
    sort(s.begin(), s.end());
    int ans = 0;
    for (int i = 0; i < m; i++) {
      sin >> q;
      int mind = d;
      for (int j = 0; j < n; j++) {
        int dd = abs(s[j] - q);
        if (dd * 2 > d)
          dd = d - dd;
        if (dd < mind) {
          mind = dd;
        }
      }
      sout << mind << endl;
      ans += mind;
    }
    sout << ans << endl;
  }
  return 0;
}
Problem p00462 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int d;
  while (cin >> d && d) {
    int n, m;
    cin >> n >> m;
    vector<int> S;
    S.push_back(0);
    S.push_back(-d);
    S.push_back(d);
    int di;
    for (int i = 2; i <= n; ++i) {
      cin >> di;
      S.push_back(di);
      S.push_back(di - d);
      S.push_back(di + d);
    }
    sort(S.begin(), S.end());
    long long sum = 0;
    int ki;
    for (int i = 1; i <= m; ++i) {
      cin >> ki;
      auto it = lower_bound(S.begin(), S.end(), ki);
      int add = abs(ki - *it);
      if (it != S.begin()) {
        --it;
        add = min(add, abs(ki - *it));
      }
      sum += add;
    }
    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int d;
  while (cin >> d && d) {
    int n, m;
    cin >> n >> m;
    set<int> S;
    S.insert(0);
    S.insert(d);
    int di;
    for (int i = 2; i <= n; ++i) {
      cin >> di;
      S.insert(di);
      S.insert(di + d);
    }
    long long sum = 0;
    int ki;
    for (int i = 1; i <= m; ++i) {
      cin >> ki;
      auto it = lower_bound(S.begin(), S.end(), ki);
      long long add = abs(ki - (long long)*it);
      if (it != S.begin()) {
        --it;
        add = min(add, abs(ki - (long long)*it));
      }
      sum += add;
    }
    cout << sum << endl;
  }

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

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

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

int main() {
  //	cut here before submit
  //	freopen ("testcase.pizza", "r", stdin );
  int d;
  while (scanf("%d", &d), d) {
    int n, m;
    scanf("%d", &n);
    scanf("%d", &m);
    vector<int> shop(n + 1, 0);
    shop[0] = 0;
    shop[n] = d;
    for (int i = 1; i < n; i++) {
      scanf("%d", &shop[i]);
    } // end for
    sort(ALL(shop));
    vector<int> order(m, 0);
    rep(j, m) { scanf("%d", &order[j]); } // end rep
    ll res = 0LL;
    rep(j, m) {
      int k = lower_bound(ALL(shop), order[j]) - shop.begin();
      ll curr = (ll)min(shop[k] - order[j], order[j] - shop[k - 1]);
      res += curr;
    } // end rep
    cout << res << endl;
  } // end loop

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdlib> // require abs
#include <cstring>
#include <fstream> // require freopen
#include <iostream>

using namespace std;
const int MAX_N = 100001; // 100000
const int MAX_M = 10000;  // 10000
int dd[MAX_N];
int vp[MAX_M];
const int MAX_D = 2000000000;

int main() {
  //	cut here before submit
  //	freopen ("testcase.pizza", "r", stdin );
  int d, n, m;

  while (cin >> d >> n >> m && d) {
    memset(dd, 0, sizeof(dd));
    int i, j;

    for (i = 1; i <= n - 1; ++i) {
      cin >> dd[i];
    } // end for

    memset(vp, 0, sizeof(vp));

    for (j = 0; j < m; ++j) {
      cin >> vp[j];
    } // end for

    int res = 0;
    for (j = 0; j < m; ++j) {
      int sum = MAX_D;
      for (i = 0; i < n; ++i) {
        sum = min(sum, abs(dd[i] - vp[j]));
        sum = min(sum, abs(d + dd[i] - vp[j]));
      } // end for
      res += sum;
    } // end for
    cout << res << endl;
  } // end loop

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, m, d, t;
vector<int> s;
int len(int key, vector<int> shops) {
  int s = 0, e = shops.size();
  int c = (s + e) / 2;
  while ((e - s) > 1) {
    if (shops[c] == key)
      return 0;
    if (shops[c] > key)
      e = c;
    else
      s = c;
    c = (s + e) / 2;
  }
  return min(abs(key - shops[s]), abs(shops[e] - key));
}

int main(void) {
  int sum;
  cin >> d;
  while (d != 0) {
    s.clear();
    s.push_back(0);
    sum = 0;
    cin >> n;
    cin >> m;
    for (int i = 0; i < n - 1; i++) {
      cin >> t;
      s.push_back(t);
    }
    sort(s.begin(), s.end());
    s.push_back(d);
    for (int i = 0; i < m; i++) {
      cin >> t;
      sum += len(t, s);
    }
    cout << sum << endl;
    cin >> d;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, m, d, t;
vector<int> s;
vector<int> rev_s;
int len(int key, vector<int> shops) {
  int s = 0, e = shops.size();
  int c = (s + e) / 2;
  while ((e - s) > 1) {
    if (shops[c] == key)
      return 0;
    if (shops[c] > key)
      e = c;
    else
      s = c;
    c = (s + e) / 2;
  }
  return min(abs(key - shops[s]), abs(shops[e] - key));
}

int main(void) {
  int sum;
  cin >> d;
  while (d != 0) {
    s.clear();
    rev_s.clear();
    s.push_back(0);
    rev_s.push_back(0);
    sum = 0;
    cin >> n;
    cin >> m;
    for (int i = 0; i < n - 1; i++) {
      cin >> t;
      s.push_back(t);
    }
    sort(s.begin(), s.end());
    for (int i = 0; i < s.size(); i++)
      rev_s.push_back(d - s[i]);

    for (int i = 0; i < m; i++) {
      cin >> t;
      sum += min(len(t, s), len(d - t, rev_s));
    }
    cout << sum << endl;
    cin >> d;
  }
  return 0;
}
Problem p00462 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm> // std::min_element, std::max_element
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;

int main() {

  int c = 0; // count how many input sets
  int l;     // entire length
  int n;     // number of branches
  int m;     // number of orders
  vector<int> v;
  int sum;

  for (;; c++) {

    sum = 0;
    cin >> l;
    if (l == 0)
      break;
    cin >> n;
    cin >> m;

    vector<int> d; // from 4th to n-1 line on txt
    d.push_back(0);
    int k[m]; // coustomer k1' house at k[0] meater     n+3 to m line
    int minRoute[m];

    for (int i = 1; i < n; i++) {
      int in;
      cin >> in;
      d.push_back(in);
    }

    for (int i = 0; i < m; i++)
      cin >> k[i];

    std::sort(d.begin(), d.end());

    for (int j = 0; j < m; j++) {

      for (int i = 0; i < n; i++) {

        if (k[j] < d[i]) {
          minRoute[j] = min(d[i] - k[j], k[j] - d[i - 1]);
          break;
        } else if (d[n - 1] <= k[j]) {
          minRoute[j] = min(k[j] - d[n - 1], l - k[j]);
          break;
        }
      }

      sum += minRoute[j];
    }
    v.push_back(sum);
  }

  for (int i = 0; i < c; i++)
    cout << v[i] << endl;

  return 0;
}

Error: TLE

Fixed submission:



#include <algorithm> // std::min_element, std::max_element
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;

int main() {

  //    int c = 0; //count how many input sets
  int l; // entire length
  int n; // number of branches
  int m; // number of orders
  //    vector<int> v;

  for (;;) {

    cin >> l;

    if (l == 0)
      break;

    cin >> n;
    cin >> m;

    int d[n]; // from 4th to n-1 line on txt
    int k[m]; // coustomer k1' house at k[1] meater     n+3 to m line

    d[0] = 0;

    for (int i = 1; i < n; i++)
      cin >> d[i];

    for (int i = 0; i < m; i++)
      cin >> k[i];

    int abso[n];
    int minRoute[m];
    int sum = 0;

    // define abs and minRoute
    for (int j = 0; j < m; j++) {

      if (k[j] > l / 2) {
        d[0] = l;
      } else {
        d[0] = 0;
      }

      // define abso[0]~abso[n-1]
      for (int i = 0; i < n; i++) {
        abso[i] = abs(k[j] - d[i]);
      }

      int value = *min_element(abso, (abso + n));
      minRoute[j] = value;
      sum += minRoute[j];
    }
    //        v.push_back(sum);
    cout << endl;
    cout << sum;
  }

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

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

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

int main() {
  while (true) {
    cin >> d;
    if (!d)
      break;
    cin >> n >> m;
    for (int i = 1; i < n; i++) {
      cin >> shop[i];
    }
    shop[0] = 0;
    shop[n] = d;
    for (int i = 0; i < m; i++) {
      cin >> k[i];
    }

    sort(shop, shop + n);
    int sum = 0;
    for (int i = 0; i < m; i++) {
      int *s2 = upper_bound(shop, shop + n + 1, k[i]);
      int *s1 = s2 - 1;
      sum += min(k[i] - *s1, *s2 - k[i]);
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int d, n, m, shop[100000], k[10000];

int main() {
  while (true) {
    cin >> d;
    if (!d)
      break;
    cin >> n >> m;
    for (int i = 1; i < n; i++) {
      cin >> shop[i];
    }
    shop[0] = 0;
    shop[n] = d;
    for (int i = 0; i < m; i++) {
      cin >> k[i];
    }

    sort(shop, shop + n);
    int sum = 0;
    for (int i = 0; i < m; i++) {
      int *s2 = upper_bound(shop, shop + n + 1, k[i]);
      int *s1 = s2 - 1;
      sum += min(k[i] - *s1, *s2 - k[i]);
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00462 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

#define dis(a, b) min(abs(b - a), d - abs(b - a))

int main() {
  int d;
  while (cin >> d, d) {
    int n, m;
    cin >> n >> m;
    vector<int> v(n);
    REP(i, n - 1)
    cin >> v[i];
    v[n - 1] = 0;
    sort(ALL(v));
    int res = 0;
    while (m--) {
      int a;
      cin >> a;
      int hoge = INF;
      vector<int>::iterator it = lower_bound(ALL(v), a);
      if (it != v.end())
        hoge = min(hoge, dis(*it, a));
      else
        hoge = min(hoge, dis(v[0], a));
      if (v.size() > 1) {
        it--;
        hoge = min(hoge, dis(*it, a));
      }

      //      cout << hoge << endl;
      res += hoge;
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

#define dis(a, b) min(abs(b - a), d - abs(b - a))

int main() {
  int d;
  while (cin >> d, d) {
    int n, m;
    cin >> n >> m;
    int mise[n];
    REP(i, n - 1)
    cin >> mise[i];
    mise[n - 1] = 0;
    int res = 0;
    while (m--) {
      int a;
      cin >> a;
      int hoge = INF;
      REP(i, n) { hoge = min(hoge, dis(mise[i], a)); }
      //      cout << hoge << endl;
      res += hoge;
    }
    cout << res << endl;
  }
}
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;
}