This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
void solve(vector<int>& ar, int  D){
    if((int)ar.size() == 1){
        cout << 0 << ' ';
        return;
    }
    assert(is_sorted(ar.begin(), ar.end()));
    int N = (int)ar.size();
    vector<long long> pref(N), suff(N);
    for(int i = 1; i < N; ++i){
        pref[i] = pref[i - 1] + max(D - (ar[i] - ar[i - 1]), 0);
    }
    for(int i = N - 2; i >= 0; --i){
        suff[i] = suff[i + 1] + max(D - (ar[i] - ar[i - 1]), 0);
    }
    long long ans = min(suff[0], pref[N - 1]);
    for(int i = 0; i + 1 < N; ++i){
        ans = min(ans, max(pref[i], suff[i + 1]) * 2 + max(D - ar[i + 1] + ar[i], 0));
    }
    if(ans & 1) cout << (ans >> 1) << ".5 ";
    else cout << (ans >> 1) << ' ';
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#ifdef LOCAL
    freopen("task.inp", "r", stdin);
    freopen("task.out", "w", stdout);
#endif // LOCAL
    int N, M, D;
    cin >> N >> M >> D;
    vector<int> ar;
    for(int i = 1; i <= N; ++i){
        int x; cin >> x;
        ar.push_back(x);
    }
    sort(ar.begin(), ar.end());
    for(int i = 1; i <= M; ++i){
        int x; cin >> x;
        ar.insert(lower_bound(ar.begin(), ar.end(), x), x);
        solve(ar, D);
    }
    return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |