//    01001100 01001111 01010100 01000001    \\
                //                                           \\
                //                ╦  ╔═╗╔╦╗╔═╗               \\
                //                ║  ║ ║ ║ ╠═╣               \\
                //                ╩═╝╚═╝ ╩ ╩ ╩               \\
                //                                           \\
                //    01001100 01001111 01010100 01000001    \\
#include <bits/stdc++.h>
using namespace std;
#define N 400001
#define M 19
#define nl '\n'
#define ff first
#define ss second
#define add insert
#define ll long long
#define ld long double
#define terminator main
#define pll pair<ll,ll>
#define append push_back
#define pii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define L0TA ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
int n, m;
vector<int> a, b;
int dp[N];
bool check(int x){
    int o, p, q, r;
    for (int i = 1; i <= n; i++){
        dp[i] = dp[i - 1], o = b[dp[i - 1]], q = a[i - 1];
        if (o >= q - x) dp[i] = max(dp[i], int (upper_bound(all(b), q) - b.begin()));
        if (o >= q) dp[i] = max(dp[i], int (upper_bound(all(b), q + x) - b.begin()));
        if (i > 1){
            o = b[dp[i - 2]], p = a[i - 2];
            if(o >= q - x) 
                dp[i] = max(dp[i], int (upper_bound(all(b), p + x) - b.begin()));
        }
    }
    return dp[n] == m;
}
void solve(){
    int o;
    cin >> n >> m;
    for (int i = 0; i < n; i++){
        cin >> o;
        a.append(o); 
    }
    for (int i = 0; i < m; i++){
        cin >> o;
        b.append(o); 
    }
    if (n == 1 && b[0] < a[0] && a[0] < b[m - 1]){
        cout << "-1";
        return;
    }
    o = 0;
    b.append(INT_MAX);
    for (int i = 536870912; i > 0; i /= 2)
        if (!check(o + i)) o += i;
    cout << o + 1;
}
int terminator(){
    L0TA;
    solve();
    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... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |