Problem p00462 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int, int> P;
int store[100001];
int point[10001];
long long int ans[10001];
int abs(int x) {
  if (x > 0)
    return x;
  else
    return x * -1;
}

int main() {
  lli d;
  int n, m;
  while (cin >> d && d) {
    cin >> n >> m;
    store[0] = 0;
    for (int i = 1; i < n; ++i) {
      cin >> store[i];
    }
    for (int i = 0; i < m; ++i) {
      cin >> point[i];
    }
    for (int i = 0; i < m; ++i) {
      ans[i] = INF;
    }
    sort(store, store + n);
    for (int i = 0; i < m; ++i) {
      int up = n, down = 0;
      int count = 0;
      /*for(int j=0; j<n; ++j){
              cout << store[j] << "\\n";
      }*/
      // cout << "\\n";
      while (1) {
        int mid = (up + down) / 2.0;
        if (ans[i] > abs(point[i] - store[mid]))
          ans[i] = abs(point[i] - store[mid]);
        if (count == 5000) {
          break;
        }
        if (store[mid] > point[i]) {
          up = mid;
        } else {
          down = mid;
        }
        ++count;
      }
      if (d - point[i] < ans[i])
        ans[i] = d - point[i];
      // cout << ans[i] << "\\n";
    }
    long long int Ans = 0;
    for (int i = 0; i < m; ++i) {
      Ans += ans[i];
    }
    cout << Ans << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int, int> P;
int store[100001];
int point[10001];
long long int ans[10001];
int abs(int x) {
  if (x > 0)
    return x;
  else
    return x * -1;
}

int main() {
  lli d;
  int n, m;
  while (cin >> d && d) {
    cin >> n >> m;
    store[0] = 0;
    store[n] = 0;
    for (int i = 1; i < n; ++i) {
      cin >> store[i];
    }
    for (int i = 0; i < m; ++i) {
      cin >> point[i];
    }
    for (int i = 0; i < m; ++i) {
      ans[i] = -1;
    }
    sort(store, store + n);
    for (int i = 0; i < m; ++i) {
      double up = n - 1, down = 0;
      int count = 0;
      while (1) {
        int mid = (int)((up + down) / 2.0 + 0.5);
        if (count == 10000) {
          ans[i] = abs(point[i] - store[mid]);
          break;
        }
        if (store[mid] > point[i]) {
          up = mid;
        } else {
          down = mid;
        }
        ++count;
      }
      count = 0;
      up = n;
      down = 1;
      while (1) {
        int mid = (int)((up + down) / 2.0 + 0.5);
        if (count == 10000) {
          if (abs(d - point[i] - (d - store[mid])) < ans[i])
            ans[i] = abs(d - point[i] - (d - store[mid]));
          break;
        }
        if (d - store[mid] > d - point[i]) {
          down = mid;
        } else {
          up = mid;
        }
        ++count;
      }
      if (d - point[i] < ans[i])
        ans[i] = d - point[i];
      // cout << ans[i] << "\\n";
    }
    long long int Ans = 0;
    for (int i = 0; i < m; ++i) {
      Ans += ans[i];
    }
    cout << Ans << "\\n";
  }
}