Language: C++

#include<algorithm>
using namespace std;
#define SIZE 100002
#define QUERY 10001
int shop[SIZE];

int Bsearch(int n,int target){
  int l=0,r=n-1;
  int mid;
  while(l < r){
    mid = (r+l)/2;
    if ( shop[mid]> target){
      r=mid;
    }else if ( shop[mid]<target){
      l=mid;
    }else return 0;
    if ( l+1== r )break;
  }
  int ret1 = max(shop[mid],target)-min(shop[mid],target);
  int ret2 = max(shop[(mid+1)%n],target)-min(shop[(mid+1)%n],target);
  int ret3 = max(shop[(mid+n-1)%n],target)-min(shop[(mid+n-1)%n],target);  
  return min(ret1,ret2);
  //cout << shop[mid] << " " << target << endl;
  return min(ret1,min(ret2,ret3));
}

int solve(int n,map<int,int> &M){
  map<int,int>::iterator itr = M.begin();
  int ret =0;
  while(itr != M.end()){
    ret+= Bsearch(n,(*itr).first) * (*itr).second;
    itr++;
  }
  return ret;
}

main(){
  int d,n,m;
  while(cin>>d>>n>>m && d){
    shop[0]=0;
    for(int i=1;i<n;i++){
      int tmp;
      cin>>tmp;
      shop[i]=tmp;
    }
    shop[n++]=d;
    sort(shop,shop+n);
    map<int,int> M;
    for(int i=0;i<m;i++){
      int tmp;
      cin>>tmp;
      M[tmp]++;
    }
    cout <<solve(n,M)<<endl;
    
  }
  return false;
}

Compiler output:
a.cc:27:17: error: 'map' has not been declared
   27 | int solve(int n,map<int,int> &M){
      |                 ^~~
a.cc:27:20: error: expected ',' or '...' before '<' token
   27 | int solve(int n,map<int,int> &M){
      |                    ^
a.cc: In function 'int solve(int, int)':
a.cc:28:3: error: 'map' was not declared in this scope
   28 |   map<int,int>::iterator itr = M.begin();
      |   ^~~
a.cc:2:1: note: 'std::map' is defined in header '<map>'; this is probably fixable by adding '#include <map>'
    1 | #include<algorithm>
  +++ |+#include <map>
    2 | using namespace std;
a.cc:28:7: error: expected primary-expression before 'int'
   28 |   map<int,int>::iterator itr = M.begin();
      |       ^~~
a.cc:30:9: error: 'itr' was not declared in this scope
   30 |   while(itr != M.end()){
      |         ^~~
a.cc:30:16: error: 'M' was not declared in this scope
   30 |   while(itr != M.end()){
      |                ^
a.cc: At global scope:
a.cc:37:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   37 | main(){
      | ^~~~
a.cc: In function 'int main()':
a.cc:39:9: error: 'cin' was not declared in this scope
   39 |   while(cin>>d>>n>>m && d){
      |         ^~~
a.cc:2:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
    1 | #include<algorithm>
  +++ |+#include <iostream>
    2 | using namespace std;
a.cc:48:5: error: 'map' was not declared in this scope
   48 |     map<int,int> M;
      |     ^~~
a.cc:48:5: note: 'std::map' is defined in header '<map>'; this is probably fixable by adding '#include <map>'
a.cc:48:9: error: expected primary-expression before 'int'
   48 |     map<int,int> M;
      |         ^~~
a.cc:52:7: error: 'M' was not declared in this scope
   52 |       M[tmp]++;
      |       ^
a.cc:54:5: error: 'cout' was not declared in this scope
   54 |     cout <<solve(n,M)<<endl;
      |     ^~~~
a.cc:54:5: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:54:20: error: 'M' was not declared in this scope
   54 |     cout <<solve(n,M)<<endl;
      |                    ^
a.cc:54:24: error: 'endl' was not declared in this scope
   54 |     cout <<solve(n,M)<<endl;
      |                        ^~~~
a.cc:2:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
    1 | #include<algorithm>
  +++ |+#include <ostream>
    2 | using namespace std;