Problem:
Problem H: ねこ鍋改造計画（仮）
あなたは、自宅にたくさんのねこを飼っている。そんなあなたの日課は、飼っているねこが土鍋の中
に入って昼寝している様子（通称：ねこ鍋）を撮影することだ。何匹かのねこが、鍋の中でいっしょ
に丸くなって眠る姿は、実に愛くるしい。
それぞれのねこには、「重さ」
m
i
と「Cute」
c
i
が定義されている。Cute とは、そのねこ1 匹だけを見たときの、愛らしさ・あどけなさ・しなやかさ・やわらかさ・ういういしさ、などを総合的に評価した数値であり、大きければ大きいほどそのねこは魅力的である。
また、ねこ鍋に対しては「UnCute」という数値が定義される。これは、ねこ鍋全体としてのアンバラ
ンスさをあらわす値であり、Cute とは逆に、小さければ小さいほど、そのねこ鍋はよいねこ鍋であ
る。ねこ鍋のUnCute は、中にいるねこのCute の最大値
C
max
と最小値
C
min
を用いて、
C
max
-
C
min
とあらわされる。つまり、中にいるねこがみな同じくらいのCute を持っているねこ鍋が、よいねこ鍋である。
ねこ鍋を持ち運ぶときのことを考えると、ねこ鍋の重さ（＝中にいるねこの重さの総和）は、限界値
W
以下でなければならない。この条件をみたすようにねこ鍋に入ってもらうねこを選んで、ねこ鍋のUnCute を小さくしたいのだが、いったいどこまで小さくできるのだろうか。
……という問題を解いてすっかり満足した気になっていたのは、過去の話だ。
この前、あなたが家の倉庫を整理していたら、倉庫の中から古ぼけた土鍋が見つかった。これであな
たの家にある土鍋は２つ。そう、これからは、デュアルねこ鍋の時代なのだ！　ただのねこ鍋なども
う古い！
デュアルねこ鍋では、土鍋
A
はオスのねこ専用、もう片方の土鍋B はメスのねこ専用である。また、
デュアルねこ鍋のUnCute を定義するにあたっては、Cute のアンバランスさだけではなく、２つの
ねこ鍋の重さのアンバランスさも考慮に入れる必要がある。具体的には、重いほうのねこ鍋の重さを
M
max
、軽いほうのねこ鍋の重さを
M
min
とおくと（２つのねこ鍋の重さが同じときは
M
max
=
M
min
）、デュアルねこ鍋のUnCute は、
max{
M
max
-
M
min
,
C
max
-
C
min
}
とあらわされる。なお、ここでの
C
max
と
C
min
は、ねこ鍋
A
の中またはねこ鍋
B
の中にいるねこのCute の最大値,最小値である。
ねこ鍋を持ち運ぶときのことを考えると、
M
max
は、限界値
W
以下でなければならない。この条件をみたすようにデュアルねこ鍋に入ってもらうねこを選んで、デュアルねこ鍋のUnCute を小さくしたいのだが、いったいどこまで小さくできるのだろうか。
Input
N
A
N
B
W
m
A
,1
c
A
,1
m
A
,2
c
A
,2
.
.
.
m
A
,
N
A
c
A
,
N
A
m
B
,1
c
B
,1
m
B
,2
c
B
,2
.
.
.
m
B
,
N
B
c
B
,
N
B
入力の１行目には、整数
N
A
（1 ≤
N
A
≤ 500）と整数
N
B
（1 ≤
N
B
≤ 500）と整数
W
（1 ≤
W
≤ 10,000）が、空白区切りで書かれている。これは、あなたが飼っているオスのねこが全部で
N
A
匹、メスのねこが全部で
N
B
匹いることをあらわす。
W
はねこ鍋の重さの限界値である。
続く
N
A
行には、整数
m
A
,
i
（1 ≤
m
A
,
i
≤ 10,000）と整数
c
A
,
i
（1 ≤
c
A
,
i
≤ 1,000,000,000）が、空白区切りで書かれている。１＋ i 行目に書かれた整数
m
A
,
i
と
c
A
,
i
は、i 番目のオスのねこの重さが
m
A
,
i
、Cute が
c
A
,
i
であることをあらわす。
続く
N
B
行には、整数
m
B
,
i
（1 ≤
m
B
,
i
≤ 10,000）と整数
c
B
,
i
（1 ≤
c
B
,
i
≤ 1,000,000,000）が、空白区切りで書かれている。１＋
N
A
＋ i 行目に書かれた整数
m
B
,
i
と
c
B
,
i
は、i 番目のメスのねこの重さが
m
B
,
i
、Cute が
c
B
,
i
であることをあらわす。
オスのねこにもメスのねこにも、重さが
W
以下であるようなねこが、それぞれ１匹以上は存在すると仮定してよい。
Output
M
max
が
W
を超えないという条件のもとで、デュアルねこ鍋のUnCute の最小値を出力せよ。ただし、どちらのねこ鍋にも１匹以上のねこが入っていなければならない。
Sample Input 1
4 3 12
3 6
2 4
7 9
10 1
6 5
8 4
15 19
Sample Output 1
2
Sample Input 2
1 3 10
1 15
6 8
5 9
8 7
Sample Output 2
6
Sample Input 3
8 6 65
30 98
27 51
4 74
65 87
49 19
27 48
43 7
35 28
43 69
8 47
64 75
18 23
54 29
40 43
Sample Output 3
8

Submitted solutions:

code_snippet: // AOJ #2337 Remodeling Plan for Neko-Nabe (tentative)
// 2025.3.13

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct BS {
    int sz, nw;
    vector<ll> d;
    BS(int s) : sz(s), nw((s+63)/64) { d.assign(nw,0ULL); }
    void setBit(int i){ d[i/64] |= (1ULL << (i%64)); }
    bool test(int i) const { return (d[i/64] >> (i%64)) & 1ULL; }
    void orWith(const BS &o) { for(int i=0; i<nw; i++) d[i] |= o.d[i]; }
    BS shl(int sh) const {
        BS r(sz);
        int ws = sh/64, bs = sh%64;
        for (int i = 0; i < nw; i++){
            if(i+ws < r.nw){
                ll cur = d[i];
                r.d[i+ws] |= (cur << bs);
                if(bs && i+ws+1 < r.nw)
                    r.d[i+ws+1] |= (cur >> (64-bs));
            }
        }
        int extra = nw*64 - sz;
        if(extra>0) r.d[nw-1] &= ~0ULL >> extra;
        return r;
    }
    void upd(int sh) {
        BS tmp = shl(sh);
        orWith(tmp);
        int extra = nw*64 - sz;
        if(extra>0) d[nw-1] &= ~0ULL >> extra;
    }
    vector<int> getSet(int l, int r) const {
        vector<int> v;
        r = min(r, sz-1);
        for(int i=l; i<=r; i++){
            if(test(i)) v.push_back(i);
        }
        return v;
    }
    bool any(int l, int r) const {
        r = min(r, sz-1);
        for(int i=l; i<=r; i++){
            if(test(i)) return true;
        }
        return false;
    }
};

int minDiff(const BS &A, const BS &B, int W) {
    vector<int> vA = A.getSet(1,W), vB = B.getSet(1,W);
    if(vA.empty() || vB.empty()) return INT_MAX;
    int i=0, j=0, md = INT_MAX;
    while(i < (int)vA.size() && j < (int)vB.size()){
        md = min(md, abs(vA[i]-vB[j]));
        if(vA[i] < vB[j]) i++; else j++;
    }
    return md;
}

struct Cat { int m, c; };

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

    int NA, NB, W;
    cin >> NA >> NB >> W;
    vector<Cat> A(NA), B(NB);
    for(int i=0;i<NA;i++) cin >> A[i].m >> A[i].c;
    for(int i=0;i<NB;i++) cin >> B[i].m >> B[i].c;
    sort(A.begin(), A.end(), [](const Cat &a, const Cat &b){ return a.c < b.c; });
    sort(B.begin(), B.end(), [](const Cat &a, const Cat &b){ return a.c < b.c; });

    vector<int> U;
    for(auto &cat: A) U.push_back(cat.c);
    for(auto &cat: B) U.push_back(cat.c);
    sort(U.begin(), U.end());
    U.erase(unique(U.begin(), U.end()), U.end());

    int ans = INT_MAX;
    for (int L : U) {
        BS dpA(W+1), dpB(W+1);
        dpA.setBit(0);
        dpB.setBit(0);
        int pa = 0, pb = 0;
        while(pa < (int)A.size() && A[pa].c < L) pa++;
        while(pb < (int)B.size() && B[pb].c < L) pb++;

        for (int R : U) {
            if(R < L) continue;
            int cd = R - L;
            if(cd > ans) break;
            while(pa < (int)A.size() && A[pa].c <= R){
                if(A[pa].m <= W) dpA.upd(A[pa].m);
                pa++;
            }
            while(pb < (int)B.size() && B[pb].c <= R){
                if(B[pb].m <= W) dpB.upd(B[pb].m);
                pb++;
            }

            if(!dpA.any(1,W) || !dpB.any(1,W)) continue;

            int wd = minDiff(dpA, dpB, W);
            int cand = max(cd, wd);
            ans = min(ans, cand);
            if(ans==0) { cout << 0 << endl; return 0; }
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 480
memory_kb: 3508
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=1<<30;

bitset<10005> dp1[505],dp2[505];

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N,M,W;cin>>N>>M>>W;
    vector<pair<int,int>> A(N),B(M);
    for(int i=0;i<N;i++) cin>>A[i].se>>A[i].fi;
    for(int i=0;i<M;i++) cin>>B[i].se>>B[i].fi;
    
    sort(all(A));
    sort(all(B));
    
    vector<int> cand;
    for(int i=0;i<N;i++) cand.push_back(A[i].fi);
    for(int i=0;i<M;i++) cand.push_back(B[i].fi);
    
    sort(all(cand));
    cand.erase(unique(all(cand)),cand.end());
    
    int ans=INF;
    
    for(int c:cand){
        for(int i=0;i<=N;i++) dp1[i].reset();
        for(int i=0;i<=M;i++) dp2[i].reset();
        
        dp1[0][0]=true;
        for(int i=0;i<N;i++){
            if(A[i].fi>=c){
                dp1[i+1]|=dp1[i];
                dp1[i+1]|=dp1[i]<<A[i].se;
            }else{
                dp1[i+1][0]=true;
            }
        }
        
        dp2[0][0]=true;
        for(int i=0;i<M;i++){
            if(B[i].fi>=c){
                dp2[i+1]|=dp2[i];
                dp2[i+1]|=dp2[i]<<B[i].se;
            }else{
                dp2[i+1][0]=true;
            }
        }
        
        int left=-1,right=INF;
        while(right-left>1){
            int mid=(left+right)/2;
            int a=lower_bound(all(A),mp(c+mid+1,-1))-A.begin();
            int b=lower_bound(all(B),mp(c+mid+1,-1))-B.begin();
            if(dp1[a].count()==1||dp2[b].count()==1){
                left=mid;
                continue;
            }
            vector<int> S,T;
            for(int i=1;i<=W;i++){
                if(dp1[a][i]) S.push_back(i);
                if(dp2[b][i]) T.push_back(i);
            }
            
            int mi=INF;
            
            int j=0;
            for(int x:S){
                while(j<si(T)&&x>T[j]) j++;
                
                if(j<si(T)){
                    chmin(mi,max(abs(x-T[j]),mid));
                }
                if(j){
                    chmin(mi,max(abs(x-T[j-1]),mid));
                }
            }
            
            chmin(ans,mi);
            
            if(mi==mid) right=mid;
            else left=mid;
        }
    }
    
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 1550
memory_kb: 4772
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, x)    for(int i = 0; i < (x); i++)
#define rng(i, l, r) for(int i = (l); i < (r); i++)
#define sz(v)        (int)(v).size()
#define all(v)       (v).begin(), (v).end()

constexpr int mod = 1000000007;
struct mint {
    ll v;
    mint(ll x = 0) {
        v = x % mod;
        if(v < 0) v += mod;
    }
    mint operator+(mint a) { return v + a.v; }
    mint operator-(mint a) { return v - a.v; }
    mint operator*(mint a) { return v * a.v; }
};

using fps = vector<mint>;

// f *= (1 + x^d)
void multiply(fps &f, int d) {
    for(int i = sz(f) - 1 - d; i >= 0; i--)
        f[i + d] = f[i + d] + f[i];
}

// f /= (1 + x^d)
void divide(fps &f, int d) {
    rep(i, sz(f) - d)
        f[i + d] = f[i + d] - f[i];
}

int main() {
    int na, nb, w;
    cin >> na >> nb >> w;

    using T = tuple<int, int, int>;
    vector<T> ctm;

    rep(i, na) {
        int m, c;
        cin >> m >> c;
        if(m > w) continue;
        ctm.push_back({c, 0, m});
    }
    rep(i, nb) {
        int m, c;
        cin >> m >> c;
        if(m > w) continue;
        ctm.push_back({c, 1, m});
    }

    sort(all(ctm));

    int term2_min = 1 << 30;
    rep(i, sz(ctm) - 1) {
        if(get<1>(ctm[i]) != get<1>(ctm[i + 1])) {
            term2_min = min(term2_min, get<0>(ctm[i + 1]) - get<0>(ctm[i]));
        }
    }
    if(term2_min >= w - 1) {
        cout << term2_min << endl;
        return 0;
    }

    auto f = [&](int x) -> bool {
        vector<fps> fs(2, fps(w + 1));
        fs[0][0] = fs[1][0] = 1;
        queue<T> que;
        for(auto [c, t, m] : ctm) {
            while(!que.empty() and c - get<0>(que.front()) > x) {
                auto [oc, ot, om] = que.front();
                que.pop();
                divide(fs[ot], om);
            }
            que.push(T{c, t, m});
            multiply(fs[t], m);
            pii bef = {-(1 << 30), 0};
            rng(i, 1, w + 1) {
                rep(j, 2) {
                    if(fs[j][i].v != 0) {
                        if(i - bef.first <= x and bef.second != j) return true;
                        bef = {i, j};
                    }
                }
            }
        }
        return false;
    };

    int ok = w, ng = -1, mid;
    while(mid = (ok + ng) >> 1, ok - ng > 1) (f(mid) ? ok : ng) = mid;

    cout << ok << endl;
}
accuracy: 1
time_ms: 220
memory_kb: 3576
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, x)    for(int i = 0; i < (x); i++)
#define rng(i, l, r) for(int i = (l); i < (r); i++)
#define sz(v)        (int)(v).size()
#define all(v)       (v).begin(), (v).end()

constexpr int mod = 1000000007;
struct mint {
    ll v;
    mint(ll x = 0) {
        v = x % mod;
        if(v < 0) v += mod;
    }
    mint operator+(mint a) { return v + a.v; }
    mint operator-(mint a) { return v - a.v; }
    mint operator*(mint a) { return v * a.v; }
};

using fps = vector<mint>;

// f *= (1 + x^d)
void multiply(fps &f, int d) {
    for(int i = sz(f) - 1 - d; i >= 0; i--)
        f[i + d] = f[i + d] + f[i];
}

// f /= (1 + x^d)
void divide(fps &f, int d) {
    rep(i, sz(f) - d)
        f[i + d] = f[i + d] - f[i];
}

int main() {
    int na, nb, w;
    cin >> na >> nb >> w;

    using T = tuple<int, int, int>;
    vector<T> ctm;

    rep(i, na) {
        int m, c;
        cin >> m >> c;
        if(m > w) continue;
        ctm.push_back({c, 0, m});
    }
    rep(i, nb) {
        int m, c;
        cin >> m >> c;
        if(m > w) continue;
        ctm.push_back({c, 1, m});
    }

    sort(all(ctm));

    int term2_min = 1 << 30;
    rep(i, sz(ctm) - 1) {
        if(get<1>(ctm[i]) != get<1>(ctm[i + 1])) {
            term2_min = min(term2_min, get<0>(ctm[i + 1]) - get<0>(ctm[i]));
        }
    }
    if(term2_min >= w - 1) {
        cout << term2_min << endl;
        return 0;
    }

    auto f = [&](int x) -> bool {
        fps fa(w + 1), fb(w + 1);
        fa[0] = fb[0] = 1;
        queue<T> que;
        for(auto [c, t, m] : ctm) {
            while(!que.empty() and c - get<0>(que.front()) > x) {
                auto [oc, ot, om] = que.front();
                que.pop();
                if(!ot)
                    divide(fa, om);
                else
                    divide(fb, om);
            }
            que.push(T{c, t, m});
            if(!t)
                multiply(fa, m);
            else
                multiply(fb, m);
            pii bef = {-(1 << 30), -1};
            rng(i, 1, w + 1) {
                if(fa[i].v != 0) {
                    if(i - bef.first <= x and bef.second == 1) return true;
                    bef = {i, 0};
                }
                if(fb[i].v != 0) {
                    if(i - bef.first <= x and !bef.second) return true;
                    bef = {i, 1};
                }
            }
        }
        return false;
    };

    int ok = w, ng = -1, mid;
    while(mid = (ok + ng) >> 1, ok - ng > 1) (f(mid) ? ok : ng) = mid;

    cout << ok << endl;
}
accuracy: 1
time_ms: 190
memory_kb: 3572
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, x)    for(int i = 0; i < (x); i++)
#define rng(i, l, r) for(int i = (l); i < (r); i++)
#define sz(v)        (int)(v).size()
#define all(v)       (v).begin(), (v).end()

constexpr int mod = 1000000007;
struct mint {
    ll v;
    mint(ll x = 0) {
        v = x % mod;
        if(v < 0) v += mod;
    }
    mint operator+(mint a) { return v + a.v; }
    mint operator-(mint a) { return v - a.v; }
    mint operator*(mint a) { return v * a.v; }
};

using fps = vector<mint>;

// f *= (1 + x^d)
void multiply(fps &f, int d) {
    for(int i = sz(f) - 1 - d; i >= 0; i--)
        f[i + d] = f[i + d] + f[i];
}

// f /= (1 + x^d)
void divide(fps &f, int d) {
    rep(i, sz(f) - d)
        f[i + d] = f[i + d] - f[i];
}

int main() {
    int na, nb, w;
    cin >> na >> nb >> w;

    using T = tuple<int, int, int>;
    vector<T> ctm;

    rep(i, na) {
        int m, c;
        cin >> m >> c;
        if(m > w) continue;
        ctm.push_back({c, 0, m});
    }
    rep(i, nb) {
        int m, c;
        cin >> m >> c;
        if(m > w) continue;
        ctm.push_back({c, 1, m});
    }

    sort(all(ctm));

    int term2_min = 1 << 30;
    rep(i, sz(ctm) - 1) {
        if(get<1>(ctm[i]) != get<1>(ctm[i + 1])) {
            term2_min = min(term2_min, get<0>(ctm[i + 1]) - get<0>(ctm[i]));
        }
    }
    if(term2_min >= w - 1) {
        cout << term2_min << endl;
        return 0;
    }

    auto f = [&](int x) -> bool {
        fps fa(w + 1), fb(w + 1);
        fa[0] = fb[0] = 1;
        queue<T> que;
        for(auto [c, t, m] : ctm) {
            while(!que.empty() and c - get<0>(que.front()) > x) {
                auto [oc, ot, om] = que.front();
                que.pop();
                if(!ot)
                    divide(fa, om);
                else
                    divide(fb, om);
            }
            que.push(T{c, t, m});
            if(!t)
                multiply(fa, m);
            else
                multiply(fb, m);
            vector<pii> cw;
            rng(i, 1, w + 1) {
                if(fa[i].v != 0) cw.emplace_back(i, 0);
                if(fb[i].v != 0) cw.emplace_back(i, 1);
            }
            rep(i, sz(cw) - 1) if(cw[i].second != cw[i + 1].second and cw[i + 1].first - cw[i].first <= x) return true;
        }
        return false;
    };

    int ok = w, ng = -1, mid;
    while(mid = (ok + ng) >> 1, ok - ng > 1) (f(mid) ? ok : ng) = mid;

    cout << ok << endl;
}
accuracy: 1
time_ms: 300
memory_kb: 3732
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, x)    for(int i = 0; i < (x); i++)
#define rng(i, l, r) for(int i = (l); i < (r); i++)
#define sz(v)        (int)(v).size()
#define all(v)       (v).begin(), (v).end()

constexpr int mod = 1000000007;
struct mint {
	ll v;
	mint(ll x = 0) {
		v = x % mod;
		if(v < 0) v += mod;
	}
	mint operator+(mint a) { return v + a.v; }
	mint operator-(mint a) { return v - a.v; }
	mint operator*(mint a) { return v * a.v; }
};

using fps = vector<mint>;

// f *= (1 + x^d)
void multiply(fps &f, int d) {
	for(int i = sz(f) - 1 - d; i >= 0; i--)
		f[i + d] = f[i + d] + f[i];
}

// f /= (1 + x^d)
void divide(fps &f, int d) {
	rep(i, sz(f) - d)
	    f[i + d] = f[i + d] - f[i];
}

int main() {
	int na, nb, w;
	cin >> na >> nb >> w;

	using T = tuple<int, int, int>;
	vector<T> ctm;

	rep(i, na) {
		int m, c;
		cin >> m >> c;
		if(m > w) continue;
		ctm.push_back({c, 0, m});
	}
	rep(i, nb) {
		int m, c;
		cin >> m >> c;
		if(m > w) continue;
		ctm.push_back({c, 1, m});
	}

	sort(all(ctm));

	int term2_min = 1 << 30;
	rep(i, sz(ctm) - 1) {
		if(get<1>(ctm[i]) != get<1>(ctm[i + 1])) {
			term2_min = min(term2_min, get<0>(ctm[i + 1]) - get<0>(ctm[i]));
		}
	}
	if(term2_min >= w - 1) {
		cout << term2_min << endl;
		return 0;
	}

	auto f = [&](int x) -> bool {
		fps fa(w + 1), fb(w + 1);
		fa[0] = fb[0] = 1;
		queue<T> que;
		for(auto [c, t, m] : ctm) {
			while(!que.empty() and c - get<0>(que.front()) > x) {
				auto [oc, ot, om] = que.front();
				que.pop();
				if(!ot)
					divide(fa, om);
				else
					divide(fb, om);
			}
			que.push(T{c, t, m});
			if(!t)
				multiply(fa, m);
			else
				multiply(fb, m);
			vector<int> tmp;
			rng(i, 1, w + 1) if(fa[i].v != 0) tmp.push_back(i);
			rng(i, 1, w + 1) if(fb[i].v != 0) if(lower_bound(all(tmp), i - x) != lower_bound(all(tmp), i + x + 1)) return true;
		}
		return false;
	};

	int ok = w, ng = -1, mid;
	while(mid = (ok + ng) >> 1, ok - ng > 1) (f(mid) ? ok : ng) = mid;

	cout << ok << endl;
}
accuracy: 1
time_ms: 290
memory_kb: 3712
final_rank: 5

code_snippet: // 14:18 start
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, x)    for(int i = 0; i < (x); i++)
#define rng(i, l, r) for(int i = (l); i < (r); i++)
#define sz(v)        (int)(v).size()
#define all(v)       (v).begin(), (v).end()

constexpr int mod = 1000000007;
struct mint {
	ll v;
	mint() : v(0) {}
	mint(ll x) {
		v = x % mod;
		if(v < 0) v += mod;
	}
	mint operator+(mint a) { return v + a.v; }
	mint operator-(mint a) { return v - a.v; }
	mint operator*(mint a) { return v * a.v; }
};

using fps = vector<mint>;

// f *= (1 + x^c)
void multiply(fps &f, int c) {
	for(int i = sz(f) - 1 - c; i >= 0; i--) {
		f[i + c] = f[i + c] + f[i];
	}
}

// f /= (1 + x^c)
void divide(fps &f, int c) {
	rep(i, sz(f) - c) {
		f[i + c] = f[i + c] - f[i];
	}
}

int main() {
	int na, nb, w;
	cin >> na >> nb >> w;
	vector<pii> mca, mcb;
	rep(i, na) {
		int m, c;
		cin >> m >> c;
		if(m > w) continue;
		mca.emplace_back(m, c);
	}
	rep(i, nb) {
		int m, c;
		cin >> m >> c;
		if(m > w) continue;
		mcb.emplace_back(m, c);
	}
	na = sz(mca), nb = sz(mcb);
	// cute type weight
	using T = tuple<int, int, int>;
	vector<T> ctm;
	rep(i, na) {
		auto [m, c] = mca[i];
		ctm.push_back({c, 0, m});
	}
	rep(i, nb) {
		auto [m, c] = mcb[i];
		ctm.push_back({c, 1, m});
	}
	sort(all(ctm));
	int res = 1 << 30;
	rep(i, sz(ctm) - 1) {
		if(get<1>(ctm[i]) != get<1>(ctm[i + 1])) {
			res = min(res, get<0>(ctm[i + 1]) - get<0>(ctm[i]));
		}
	}
	if(res > w) {
		cout << res << endl;
		return 0;
	}
	int ok = w, ng = -1, mid;
	auto f = [&](int x) -> bool {
		fps fa(w + 1), fb(w + 1);
		fa[0] = fb[0] = 1;
		queue<T> que;
		for(auto [c, t, m] : ctm) {
			while(!que.empty() and c - get<0>(que.front()) > x) {
				auto [oc, ot, om] = que.front();
				que.pop();
				if(ot == 0)
					divide(fa, om);
				else
					divide(fb, om);
			}
			que.push(T{c, t, m});
			if(t == 0)
				multiply(fa, m);
			else
				multiply(fb, m);
			vector<int> tmp;
			rng(i, 1, w + 1) {
				if(fa[i].v != 0) tmp.push_back(i);
			}
			rng(i, 1, w + 1) {
				if(fb[i].v != 0) {
					int l = i - x;
					int r = i + x + 1;
					if(lower_bound(all(tmp), l) != lower_bound(all(tmp), r)) return true;
				}
			}
		}
		return false;
	};
	while(mid = (ok + ng) >> 1, ok - ng > 1) (f(mid) ? ok : ng) = mid;
	cout << ok << endl;
}
accuracy: 1
time_ms: 300
memory_kb: 3552
final_rank: 4

code_snippet: // 14:18 start
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, x)    for(int i = 0; i < (x); i++)
#define rng(i, l, r) for(int i = (l); i < (r); i++)
#define sz(v)        (int)(v).size()
#define all(v)       (v).begin(), (v).end()

constexpr int mod = 1000000007;
struct mint {
	ll v;
	mint() : v(0) {}
	mint(ll x) {
		v = x % mod;
		if(v < 0) v += mod;
	}
	mint operator+(mint a) { return v + a.v; }
	mint operator-(mint a) { return v - a.v; }
	mint operator*(mint a) { return v * a.v; }
};

using fps = vector<mint>;

// f *= (1 + x^c)
void multiply(fps &f, int c) {
	for(int i = sz(f) - 1 - c; i >= 0; i--) {
		f[i + c] = f[i + c] + f[i];
	}
}

// f /= (1 + x^c)
void divide(fps &f, int c) {
	rep(i, sz(f) - c) {
		f[i + c] = f[i + c] - f[i];
	}
}

int main() {
	int na, nb, w;
	cin >> na >> nb >> w;
	vector<pii> mca, mcb;
	rep(i, na) {
		int m, c;
		cin >> m >> c;
		if(m > w) continue;
		mca.emplace_back(m, c);
	}
	rep(i, nb) {
		int m, c;
		cin >> m >> c;
		if(m > w) continue;
		mcb.emplace_back(m, c);
	}
	na = sz(mca), nb = sz(mcb);
	// cute type weight
	using T = tuple<int, int, int>;
	vector<T> ctm;
	rep(i, na) {
		auto [m, c] = mca[i];
		ctm.push_back({c, 0, m});
	}
	rep(i, nb) {
		auto [m, c] = mcb[i];
		ctm.push_back({c, 1, m});
	}
	sort(all(ctm));
	int res = 1 << 30;
	rep(i, sz(ctm) - 1) {
		if(get<1>(ctm[i]) != get<1>(ctm[i + 1])) {
			res = min(res, get<0>(ctm[i + 1]) - get<0>(ctm[i]));
		}
	}
	if(res > w) {
		cout << res << endl;
		return 0;
	}
	int ok = w, ng = 0, mid;
	auto f = [&](int x) -> bool {
		fps fa(w + 1), fb(w + 1);
		fa[0] = fb[0] = 1;
		queue<T> que;
		for(auto [c, t, m] : ctm) {
			while(!que.empty() and c - get<0>(que.front()) > x) {
				auto [oc, ot, om] = que.front();
				que.pop();
				if(ot == 0)
					divide(fa, om);
				else
					divide(fb, om);
			}
			que.push(T{c, t, m});
			if(t == 0)
				multiply(fa, m);
			else
				multiply(fb, m);
			vector<int> tmp;
			rng(i, 1, w + 1) {
				if(fa[i].v != 0) tmp.push_back(i);
			}
			rng(i, 1, w + 1) {
				if(fb[i].v != 0) {
					int l = i - x;
					int r = i + x + 1;
					if(lower_bound(all(tmp), l) != lower_bound(all(tmp), r)) return true;
				}
			}
		}
		return false;
	};
	while(mid = (ok + ng) >> 1, ok - ng > 1) (f(mid) ? ok : ng) = mid;
	cout << ok << endl;
}
accuracy: 0.35
time_ms: 190
memory_kb: 3588
final_rank: 14

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
//constexpr ll mod = 998244353;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


//int dx[4] = { 1,0,-1,0 };
//int dy[4] = { 0,1,0,-1 };


//-----------------------------------------

int m[2][500];
int c[2][500];

int dp[2][10001];

void solve() {
	int n[2];
	cin >> n[0] >> n[1];
	int w; cin >> w;
	rep(id, 2) {
		rep(i, n[id]) {
			cin >> m[id][i] >> c[id][i];
		}
	}
	vector<P> vp;
	rep(id, 2) {
		rep(i, n[id]) {
			vp.push_back({ c[id][i],1000 * id + i });
		}
	}
	sort(all(vp));
	int ans = 2 * mod;
	rep(id, 2)rep(i, w + 1)dp[id][i] = -1;
	rep(id, 2)dp[id][0] = mod;
	//[l,r]
	int l = 0, r = -1;
	bool ad = true;
	while (true) {
		if (ad) {
			r++;
			if (r >= vp.size())break;
			int i = vp[r].second;
			int id = i / 1000; i %= 1000;
			int mm = m[id][i];
			per(j, w + 1) {
				if (j + mm <= w) {
					chmax(dp[id][j + mm], min(dp[id][j], r));
				}
			}
		}
		else {
			l++;
		}
		int cur[2] = { -mod,-mod };
		int mi = 2 * mod;
		rep1(j, w) {
			rep(i, 2) {
				if (dp[i][j] >= l) {
					chmin(mi, j - cur[i ^ 1]);
					cur[i] = j;
				}
			}
		}
		int mi2 = vp[r].first - vp[l].first;
		//cout << l << " " << r << " " << mi << " " << mi2 << "\n";
		chmin(ans, max(mi, mi2));
		if (mi >= mi2) {
			ad = true;
		}
		else {
			ad = false;
		}
	}
	cout << ans << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 11732
final_rank: 12

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


//int dx[4] = { 1,0,-1,0 };
//int dy[4] = { 0,1,0,-1 };


//-----------------------------------------

using bt = bitset<10001>;
int m[2][500];
int c[2][500];


int sear(bt& b) {
	rep(i, 10001)if (b[i])return i;
	return -1;
}
void solve() {
	int n[2];
	cin >> n[0] >> n[1];
	int w; cin >> w;
	rep(id, 2) {
		rep(i, n[id]) {
			cin >> m[id][i] >> c[id][i];
		}
	}
	vector<P> vp;
	rep(id, 2) {
		rep(i, n[id]) {
			vp.push_back({ c[id][i],1000 * id + i });
		}
	}
	int ans = 2 * mod;
	sort(all(vp));
	rep(l, vp.size()) {
		bt b[2];
		rep(id, 2) {
			b[id] = 0;
			b[id][0] = 1;
		}
		set<int> st[2];
		int cur = 2 * mod;
		Rep(r, l, vp.size()) {
			int i = vp[r].second;
			int id = i / 1000;
			i %= 1000;
			int w = m[id][i];
			bt ad = (b[id] << w) ^ ((b[id] << w) & b[id]);
			int c = ad.count();
			rep(_, c) {
				int loc = ad._Find_first();
				//int loc = sear(ad);
				ad[loc] = 0;
				st[id].insert(loc);
				auto itrnex = next_itr(st[id ^ 1], loc);
				if (itrnex != st[id ^ 1].end()) {
					chmin(cur, *itrnex - loc);
				}
				auto itrpre = prev_itr(st[id ^ 1], loc);
				if (itrpre != st[id ^ 1].end()) {
					chmin(cur, loc - *itrpre);
				}
			}
			b[id] |= (b[id] << w);
			chmin(ans, max(cur, vp[r].first - vp[l].first));
		}
	}
	cout << ans << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 1
time_ms: 4640
memory_kb: 12604
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second

using vint=vector<int>;
using pint=pair<int,int>;
using vpint=vector<pint>;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

template<class A,class B>
ostream& operator<<(ostream& ost,const pair<A,B>&p){
	ost<<"{"<<p.first<<","<<p.second<<"}";
	return ost;
}

template<class T>
ostream& operator<<(ostream& ost,const vector<T>&v){
	ost<<"{";
	for(int i=0;i<v.size();i++){
		if(i)ost<<",";
		ost<<v[i];
	}
	ost<<"}";
	return ost;
}

inline int topbit(unsigned long long x){
	return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
	return __builtin_popcountll(x);
}

inline int parity(unsigned long long x){
	return __builtin_parity(x);
}


template<uint32_t mod>
struct ModInt{
	uint32_t a;
	ModInt& s(uint32_t vv){
		a=vv<mod?vv:vv-mod;
		return *this;
	}

	ModInt(int64_t x=0){s(x%mod+mod);}

	ModInt& operator+=(const ModInt &x){return s(a+x.a);}
	ModInt& operator-=(const ModInt &x){return s(a+mod-x.a);}
	ModInt& operator*=(const ModInt &x){
		a=uint64_t(a)*x.a%mod;
		return *this;
	}
	ModInt& operator/=(const ModInt &x){
		*this*=x.inv();
		return *this;
	}

	ModInt operator+(const ModInt &x)const{return ModInt(*this)+=x;}
	ModInt operator-(const ModInt &x)const{return ModInt(*this)-=x;}
	ModInt operator*(const ModInt &x)const{return ModInt(*this)*=x;}
	ModInt operator/(const ModInt &x)const{return ModInt(*this)/=x;}
	bool operator==(const ModInt &x)const{return a==x.a;}
	bool operator!=(const ModInt &x)const{return a!=x.a;}
	bool operator<(const ModInt &x)const{return a<x.a;}

	ModInt operator-()const{return ModInt()-*this;}
	ModInt pow(int64_t n)const{
		ModInt res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}

	ModInt inv()const{return pow(mod-2);}
};

template<uint32_t mod>
istream& operator>>(istream& in,ModInt<mod>& a){
	return (in>>a.a);
}
template<uint32_t mod>
ostream& operator<<(ostream& out,const ModInt<mod>& a){
	return (out<<a.a);
}
using mint=ModInt<1000000007>;

int W;


// c m
vector<pint>ps[2];

mint dp[2][11111];

int acc[11111];

bool test[11111];

bool check(int lim){
	vector<int>cs;
	rep(i,2)for(auto p:ps[i])cs.pb(p.fi);

	sort(all(cs));
	cs.erase(unique(all(cs)),cs.end());

	int l[2]={},r[2]={};
	fill_n(*dp,2*11111,0);

	rep(i,2)dp[i][0]=1;

	for(auto x:cs){
		rep(k,2){
			while(r[k]<ps[k].size()&&ps[k][r[k]].fi-x<=lim){
				auto p=ps[k][r[k]];
				for(int j=W;j>=p.se;j--)dp[k][j]+=dp[k][j-p.se];
				r[k]++;
			}
			while(l[k]<ps[k].size()&&ps[k][l[k]].fi<x){
				auto p=ps[k][l[k]];
				for(int j=p.se;j<=W;j++)dp[k][j]-=dp[k][j-p.se];
				l[k]++;
			}
		}

		memset(acc,0,sizeof(acc));
		for(int j=1;j<=W;j++)acc[j]=acc[j-1]+(dp[1][j]!=0);
		for(int j=1;j<=W;j++){
			if(dp[0][j]==0)continue;
			if(acc[min(W,j+lim)]-acc[max(0ll,j-lim-1)])return true;
		}
	}
	return false;
}

signed main(){
	int n[2];
	cin>>n[0]>>n[1]>>W;
	rep(i,2){
		rep(j,n[i]){
			int m,c;
			cin>>m>>c;
			ps[i].eb(c,m);
		}
		sort(all(ps[i]));
	}
	

	int lb=-1,ub=1000000000;
	while(ub-lb>1){
		int mid=(ub+lb)/2;
		if(check(mid))ub=mid;
		else lb=mid;
	}
	cout<<ub<<endl;
	return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 3332
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second

using vint=vector<int>;
using pint=pair<int,int>;
using vpint=vector<pint>;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

template<class A,class B>
ostream& operator<<(ostream& ost,const pair<A,B>&p){
	ost<<"{"<<p.first<<","<<p.second<<"}";
	return ost;
}

template<class T>
ostream& operator<<(ostream& ost,const vector<T>&v){
	ost<<"{";
	for(int i=0;i<v.size();i++){
		if(i)ost<<",";
		ost<<v[i];
	}
	ost<<"}";
	return ost;
}

inline int topbit(unsigned long long x){
	return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
	return __builtin_popcountll(x);
}

inline int parity(unsigned long long x){
	return __builtin_parity(x);
}


template<uint32_t mod>
struct ModInt{
	uint32_t a;
	ModInt& s(uint32_t vv){
		a=vv<mod?vv:vv-mod;
		return *this;
	}

	ModInt(int64_t x=0){s(x%mod+mod);}

	ModInt& operator+=(const ModInt &x){return s(a+x.a);}
	ModInt& operator-=(const ModInt &x){return s(a+mod-x.a);}
	ModInt& operator*=(const ModInt &x){
		a=uint64_t(a)*x.a%mod;
		return *this;
	}
	ModInt& operator/=(const ModInt &x){
		*this*=x.inv();
		return *this;
	}

	ModInt operator+(const ModInt &x)const{return ModInt(*this)+=x;}
	ModInt operator-(const ModInt &x)const{return ModInt(*this)-=x;}
	ModInt operator*(const ModInt &x)const{return ModInt(*this)*=x;}
	ModInt operator/(const ModInt &x)const{return ModInt(*this)/=x;}
	bool operator==(const ModInt &x)const{return a==x.a;}
	bool operator!=(const ModInt &x)const{return a!=x.a;}
	bool operator<(const ModInt &x)const{return a<x.a;}

	ModInt operator-()const{return ModInt()-*this;}
	ModInt pow(int64_t n)const{
		ModInt res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}

	ModInt inv()const{return pow(mod-2);}
};

template<uint32_t mod>
istream& operator>>(istream& in,ModInt<mod>& a){
	return (in>>a.a);
}
template<uint32_t mod>
ostream& operator<<(ostream& out,const ModInt<mod>& a){
	return (out<<a.a);
}
using mint=ModInt<1000000007>;

int W;


// c m
vector<pint>ps[2];

mint dp[2][11111];

int acc[11111];

bool test[11111];

bool check(int lim){
	vector<int>cs;
	rep(i,2)for(auto p:ps[i])cs.pb(p.fi);

	sort(all(cs));
	cs.erase(unique(all(cs)),cs.end());

	int l[2]={},r[2]={};
	fill_n(*dp,2*11111,0);

	rep(i,2)dp[i][0]=1;

	for(auto x:cs){
		rep(k,2){
			while(r[k]<ps[k].size()&&ps[k][r[k]].fi-x<=lim){
				auto p=ps[k][r[k]];
				for(int j=W;j>=p.se;j--)dp[k][j]+=dp[k][j-p.se];
				r[k]++;
			}
			while(l[k]<ps[k].size()&&ps[k][l[k]].fi<x){
				auto p=ps[k][l[k]];
				for(int j=p.se;j<=W;j++)dp[k][j]-=dp[k][j-p.se];
				l[k]++;
			}
		}

		memset(test,0,sizeof(test));
		test[0]=1;
		for(int i=l[0];i<r[0];i++){
			pint p=ps[0][i];
			for(int j=W;j>=p.se;j--)test[j]|=test[j-p.se];
		}

		for(int i=0;i<=W;i++)if(test[i]&&dp[0][i]==0)assert(0);

		memset(test,0,sizeof(test));
		test[0]=1;
		for(int i=l[1];i<r[1];i++){
			pint p=ps[1][i];
			for(int j=W;j>=p.se;j--)test[j]|=test[j-p.se];
		}
		for(int i=0;i<=W;i++)if(test[i]&&dp[1][i]==0)assert(0);

		memset(acc,0,sizeof(acc));
		for(int j=1;j<=W;j++)acc[j]=acc[j-1]+(dp[1][j]!=0);
		for(int j=1;j<=W;j++){
			if(dp[0][j]==0)continue;
			if(acc[min(W,j+lim)]-acc[max(0ll,j-lim-1)])return true;
		}
	}
	return false;
}

signed main(){
	int n[2];
	cin>>n[0]>>n[1]>>W;
	rep(i,2){
		rep(j,n[i]){
			int m,c;
			cin>>m>>c;
			ps[i].eb(c,m);
		}
		sort(all(ps[i]));
	}
	

	int lb=-1,ub=1000000000;
	while(ub-lb>1){
		int mid=(ub+lb)/2;
		if(check(mid))ub=mid;
		else lb=mid;
	}
	cout<<ub<<endl;
	return 0;
}
accuracy: 1
time_ms: 510
memory_kb: 3356
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second

using vint=vector<int>;
using pint=pair<int,int>;
using vpint=vector<pint>;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

template<class A,class B>
ostream& operator<<(ostream& ost,const pair<A,B>&p){
	ost<<"{"<<p.first<<","<<p.second<<"}";
	return ost;
}

template<class T>
ostream& operator<<(ostream& ost,const vector<T>&v){
	ost<<"{";
	for(int i=0;i<v.size();i++){
		if(i)ost<<",";
		ost<<v[i];
	}
	ost<<"}";
	return ost;
}

inline int topbit(unsigned long long x){
	return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
	return __builtin_popcountll(x);
}

inline int parity(unsigned long long x){
	return __builtin_parity(x);
}


template<uint32_t mod>
struct ModInt{
	uint32_t a;
	ModInt& s(uint32_t vv){
		a=vv<mod?vv:vv-mod;
		return *this;
	}

	ModInt(int64_t x=0){s(x%mod+mod);}

	ModInt& operator+=(const ModInt &x){return s(a+x.a);}
	ModInt& operator-=(const ModInt &x){return s(a+mod-x.a);}
	ModInt& operator*=(const ModInt &x){
		a=uint64_t(a)*x.a%mod;
		return *this;
	}
	ModInt& operator/=(const ModInt &x){
		*this*=x.inv();
		return *this;
	}

	ModInt operator+(const ModInt &x)const{return ModInt(*this)+=x;}
	ModInt operator-(const ModInt &x)const{return ModInt(*this)-=x;}
	ModInt operator*(const ModInt &x)const{return ModInt(*this)*=x;}
	ModInt operator/(const ModInt &x)const{return ModInt(*this)/=x;}
	bool operator==(const ModInt &x)const{return a==x.a;}
	bool operator!=(const ModInt &x)const{return a!=x.a;}
	bool operator<(const ModInt &x)const{return a<x.a;}

	ModInt operator-()const{return ModInt()-*this;}
	ModInt pow(int64_t n)const{
		ModInt res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}

	ModInt inv()const{return pow(mod-2);}
};

template<uint32_t mod>
istream& operator>>(istream& in,ModInt<mod>& a){
	return (in>>a.a);
}
template<uint32_t mod>
ostream& operator<<(ostream& out,const ModInt<mod>& a){
	return (out<<a.a);
}
using mint=ModInt<1000000007>;

int W;


// c m
vector<pint>ps[2];

mint dp[2][11111];

int acc[11111];

bool test[11111];

bool check(int lim){
	vector<int>cs;
	rep(i,2)for(auto p:ps[i])cs.pb(p.fi);

	sort(all(cs));
	cs.erase(unique(all(cs)),cs.end());

	int l[2]={},r[2]={};
	fill_n(*dp,2*11111,0);

	rep(i,2)dp[i][0]=1;

	for(auto x:cs){
		rep(k,2){
			while(r[k]<ps[k].size()&&ps[k][r[k]].fi-x<=lim){
				auto p=ps[k][r[k]];
				for(int j=W;j>=p.se;j--)dp[k][j]+=dp[k][j-p.se];
				r[k]++;
			}
			if(l[k]<ps[k].size()&&ps[k][l[k]].fi<x){
				auto p=ps[k][l[k]];
				for(int j=p.se;j<=W;j++)dp[k][j]-=dp[k][j-p.se];
				l[k]++;
			}
		}

		memset(test,0,sizeof(test));
		test[0]=1;
		for(int i=l[0];i<r[0];i++){
			pint p=ps[0][i];
			for(int j=W;j>=p.se;j--)test[j]|=test[j-p.se];
		}

		for(int i=0;i<=W;i++)if(test[i]&&dp[0][i]==0)assert(0);

		memset(test,0,sizeof(test));
		test[0]=1;
		for(int i=l[1];i<r[1];i++){
			pint p=ps[1][i];
			for(int j=W;j>=p.se;j--)test[j]|=test[j-p.se];
		}
		for(int i=0;i<=W;i++)if(test[i]&&dp[1][i]==0)assert(0);

		memset(acc,0,sizeof(acc));
		for(int j=1;j<=W;j++)acc[j]=acc[j-1]+(dp[1][j]!=0);
		for(int j=1;j<=W;j++){
			if(dp[0][j]==0)continue;
			if(acc[min(W,j+lim)]-acc[max(0ll,j-lim-1)])return true;
		}
	}
	return false;
}

signed main(){
	int n[2];
	cin>>n[0]>>n[1]>>W;
	rep(i,2){
		rep(j,n[i]){
			int m,c;
			cin>>m>>c;
			ps[i].eb(c,m);
		}
		sort(all(ps[i]));
	}
	

	int lb=-1,ub=1000000000;
	while(ub-lb>1){
		int mid=(ub+lb)/2;
		if(check(mid))ub=mid;
		else lb=mid;
	}
	cout<<ub<<endl;
	return 0;
}
accuracy: 0.25
time_ms: 590
memory_kb: 3344
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second

using vint=vector<int>;
using pint=pair<int,int>;
using vpint=vector<pint>;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

template<class A,class B>
ostream& operator<<(ostream& ost,const pair<A,B>&p){
	ost<<"{"<<p.first<<","<<p.second<<"}";
	return ost;
}

template<class T>
ostream& operator<<(ostream& ost,const vector<T>&v){
	ost<<"{";
	for(int i=0;i<v.size();i++){
		if(i)ost<<",";
		ost<<v[i];
	}
	ost<<"}";
	return ost;
}

inline int topbit(unsigned long long x){
	return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
	return __builtin_popcountll(x);
}

inline int parity(unsigned long long x){
	return __builtin_parity(x);
}


template<uint32_t mod>
struct ModInt{
	uint32_t a;
	ModInt& s(uint32_t vv){
		a=vv<mod?vv:vv-mod;
		return *this;
	}

	ModInt(int64_t x=0){s(x%mod+mod);}

	ModInt& operator+=(const ModInt &x){return s(a+x.a);}
	ModInt& operator-=(const ModInt &x){return s(a+mod-x.a);}
	ModInt& operator*=(const ModInt &x){
		a=uint64_t(a)*x.a%mod;
		return *this;
	}
	ModInt& operator/=(const ModInt &x){
		*this*=x.inv();
		return *this;
	}

	ModInt operator+(const ModInt &x)const{return ModInt(*this)+=x;}
	ModInt operator-(const ModInt &x)const{return ModInt(*this)-=x;}
	ModInt operator*(const ModInt &x)const{return ModInt(*this)*=x;}
	ModInt operator/(const ModInt &x)const{return ModInt(*this)/=x;}
	bool operator==(const ModInt &x)const{return a==x.a;}
	bool operator!=(const ModInt &x)const{return a!=x.a;}
	bool operator<(const ModInt &x)const{return a<x.a;}

	ModInt operator-()const{return ModInt()-*this;}
	ModInt pow(int64_t n)const{
		ModInt res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}

	ModInt inv()const{return pow(mod-2);}
};

template<uint32_t mod>
istream& operator>>(istream& in,ModInt<mod>& a){
	return (in>>a.a);
}
template<uint32_t mod>
ostream& operator<<(ostream& out,const ModInt<mod>& a){
	return (out<<a.a);
}
using mint=ModInt<1000000007>;

int W;


// c m
vector<pint>ps[2];

mint dp[2][11111];

int acc[11111];

bool test[11111];

bool check(int lim){
	vector<int>cs;
	rep(i,2)for(auto p:ps[i])cs.pb(p.fi);

	sort(all(cs));
	cs.erase(unique(all(cs)),cs.end());

	int l[2]={},r[2]={};
	fill_n(*dp,2*11111,0);

	rep(i,2)dp[i][0]=1;

	for(auto x:cs){
		rep(k,2){
			while(r[k]<ps[k].size()&&ps[k][r[k]].fi-x<=lim){
				auto p=ps[k][r[k]];
				for(int j=W;j>=p.se;j--)dp[k][j]+=dp[k][j-p.se];
				r[k]++;
			}
			if(l[k]<ps[k].size()&&ps[k][l[k]].fi<x){
				auto p=ps[k][l[k]];
				for(int j=p.se;j<=W;j++)dp[k][j]-=dp[k][j-p.se];
				l[k]++;
			}
		}

		memset(test,0,sizeof(test));
		test[0]=1;
		for(int i=l[0];i<r[0];i++){
			pint p=ps[0][i];
			for(int j=W;j>=p.se;j--)test[j]|=test[j-p.se];
		}

		for(int i=0;i<=W;i++)if(test[i]&&dp[i]==0){
			assert(0);
		}

		memset(acc,0,sizeof(acc));
		for(int j=1;j<=W;j++)acc[j]=acc[j-1]+(dp[1][j]!=0);
		for(int j=1;j<=W;j++){
			if(dp[0][j]==0)continue;
			if(acc[min(W,j+lim)]-acc[max(0ll,j-lim-1)])return true;
		}
	}
	return false;
}

signed main(){
	int n[2];
	cin>>n[0]>>n[1]>>W;
	rep(i,2){
		rep(j,n[i]){
			int m,c;
			cin>>m>>c;
			ps[i].eb(c,m);
		}
		sort(all(ps[i]));
	}
	

	int lb=-1,ub=1000000000;
	while(ub-lb>1){
		int mid=(ub+lb)/2;
		if(check(mid))ub=mid;
		else lb=mid;
	}
	cout<<ub<<endl;
	return 0;
}
accuracy: 0.25
time_ms: 150
memory_kb: 3348
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second

using vint=vector<int>;
using pint=pair<int,int>;
using vpint=vector<pint>;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

template<class A,class B>
ostream& operator<<(ostream& ost,const pair<A,B>&p){
	ost<<"{"<<p.first<<","<<p.second<<"}";
	return ost;
}

template<class T>
ostream& operator<<(ostream& ost,const vector<T>&v){
	ost<<"{";
	for(int i=0;i<v.size();i++){
		if(i)ost<<",";
		ost<<v[i];
	}
	ost<<"}";
	return ost;
}

inline int topbit(unsigned long long x){
	return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
	return __builtin_popcountll(x);
}

inline int parity(unsigned long long x){
	return __builtin_parity(x);
}


template<uint32_t mod>
struct ModInt{
	uint32_t a;
	ModInt& s(uint32_t vv){
		a=vv<mod?vv:vv-mod;
		return *this;
	}

	ModInt(int64_t x=0){s(x%mod+mod);}

	ModInt& operator+=(const ModInt &x){return s(a+x.a);}
	ModInt& operator-=(const ModInt &x){return s(a+mod-x.a);}
	ModInt& operator*=(const ModInt &x){
		a=uint64_t(a)*x.a%mod;
		return *this;
	}
	ModInt& operator/=(const ModInt &x){
		*this*=x.inv();
		return *this;
	}

	ModInt operator+(const ModInt &x)const{return ModInt(*this)+=x;}
	ModInt operator-(const ModInt &x)const{return ModInt(*this)-=x;}
	ModInt operator*(const ModInt &x)const{return ModInt(*this)*=x;}
	ModInt operator/(const ModInt &x)const{return ModInt(*this)/=x;}
	bool operator==(const ModInt &x)const{return a==x.a;}
	bool operator!=(const ModInt &x)const{return a!=x.a;}
	bool operator<(const ModInt &x)const{return a<x.a;}

	ModInt operator-()const{return ModInt()-*this;}
	ModInt pow(int64_t n)const{
		ModInt res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}

	ModInt inv()const{return pow(mod-2);}
};

template<uint32_t mod>
istream& operator>>(istream& in,ModInt<mod>& a){
	return (in>>a.a);
}
template<uint32_t mod>
ostream& operator<<(ostream& out,const ModInt<mod>& a){
	return (out<<a.a);
}
using mint=ModInt<1000000007>;

int W;


// c m
vector<pint>ps[2];

mint dp[2][11111];

int acc[11111];

bool test[11111];

bool check(int lim){
	vector<int>cs;
	rep(i,2)for(auto p:ps[i])cs.pb(p.fi);

	sort(all(cs));
	cs.erase(unique(all(cs)),cs.end());

	int l[2]={},r[2]={};
	fill_n(*dp,2*11111,0);

	rep(i,2)dp[i][0]=1;

	for(auto x:cs){
		rep(k,2){
			while(r[k]<ps[k].size()&&ps[k][r[k]].fi-x<=lim){
				auto p=ps[k][r[k]];
				for(int j=W;j>=p.se;j--)dp[k][j]+=dp[k][j-p.se];
				r[k]++;
			}
			if(l[k]<ps[k].size()&&ps[k][l[k]].fi<x){
				auto p=ps[k][l[k]];
				for(int j=p.se;j<=W;j++)dp[k][j]-=dp[k][j-p.se];
				l[k]++;
			}
		}

		memset(test,0,sizeof(test));
		test[0]=1;
		for(int i=l[0];i<r[0];i++){
			pint p=ps[0][i];
			for(int j=W;j>=p.se;j--)test[j]|=test[j-p.se];
		}

		for(int i=0;i<=W;i++)if(test[i]&&dp[i]==0){
			cout<<l[0]<<" "<<r[0]<<endl;
			for(int i=0;i<=W;i++)cout<<test[i]<<" ";cout<<endl;
			return 0;
		}

		memset(acc,0,sizeof(acc));
		for(int j=1;j<=W;j++)acc[j]=acc[j-1]+(dp[1][j]!=0);
		for(int j=1;j<=W;j++){
			if(dp[0][j]==0)continue;
			if(acc[min(W,j+lim)]-acc[max(0ll,j-lim-1)])return true;
		}
	}
	return false;
}

signed main(){
	int n[2];
	cin>>n[0]>>n[1]>>W;
	rep(i,2){
		rep(j,n[i]){
			int m,c;
			cin>>m>>c;
			ps[i].eb(c,m);
		}
		sort(all(ps[i]));
	}
	

	int lb=-1,ub=1000000000;
	while(ub-lb>1){
		int mid=(ub+lb)/2;
		if(check(mid))ub=mid;
		else lb=mid;
	}
	cout<<ub<<endl;
	return 0;
}
accuracy: 0.25
time_ms: 150
memory_kb: 3328
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second

using vint=vector<int>;
using pint=pair<int,int>;
using vpint=vector<pint>;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

template<class A,class B>
ostream& operator<<(ostream& ost,const pair<A,B>&p){
	ost<<"{"<<p.first<<","<<p.second<<"}";
	return ost;
}

template<class T>
ostream& operator<<(ostream& ost,const vector<T>&v){
	ost<<"{";
	for(int i=0;i<v.size();i++){
		if(i)ost<<",";
		ost<<v[i];
	}
	ost<<"}";
	return ost;
}

inline int topbit(unsigned long long x){
	return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
	return __builtin_popcountll(x);
}

inline int parity(unsigned long long x){
	return __builtin_parity(x);
}


template<uint32_t mod>
struct ModInt{
	uint32_t a;
	ModInt& s(uint32_t vv){
		a=vv<mod?vv:vv-mod;
		return *this;
	}

	ModInt(int64_t x=0){s(x%mod+mod);}

	ModInt& operator+=(const ModInt &x){return s(a+x.a);}
	ModInt& operator-=(const ModInt &x){return s(a+mod-x.a);}
	ModInt& operator*=(const ModInt &x){
		a=uint64_t(a)*x.a%mod;
		return *this;
	}
	ModInt& operator/=(const ModInt &x){
		*this*=x.inv();
		return *this;
	}

	ModInt operator+(const ModInt &x)const{return ModInt(*this)+=x;}
	ModInt operator-(const ModInt &x)const{return ModInt(*this)-=x;}
	ModInt operator*(const ModInt &x)const{return ModInt(*this)*=x;}
	ModInt operator/(const ModInt &x)const{return ModInt(*this)/=x;}
	bool operator==(const ModInt &x)const{return a==x.a;}
	bool operator!=(const ModInt &x)const{return a!=x.a;}
	bool operator<(const ModInt &x)const{return a<x.a;}

	ModInt operator-()const{return ModInt()-*this;}
	ModInt pow(int64_t n)const{
		ModInt res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}

	ModInt inv()const{return pow(mod-2);}
};

template<uint32_t mod>
istream& operator>>(istream& in,ModInt<mod>& a){
	return (in>>a.a);
}
template<uint32_t mod>
ostream& operator<<(ostream& out,const ModInt<mod>& a){
	return (out<<a.a);
}
using mint=ModInt<1000000007>;

int W;


// c m
vector<pint>ps[2];

mint dp[2][11111];

int acc[11111];

mint test[11111];

bool check(int lim){
	vector<int>cs;
	rep(i,2)for(auto p:ps[i])cs.pb(p.fi);

	sort(all(cs));
	cs.erase(unique(all(cs)),cs.end());

	int l[2]={},r[2]={};
	fill_n(*dp,2*11111,0);

	rep(i,2)dp[i][0]=1;

	for(auto x:cs){
		rep(k,2){
			while(r[k]<ps[k].size()&&ps[k][r[k]].fi-x<=lim){
				auto p=ps[k][r[k]];
				for(int j=W;j>=p.se;j--)dp[k][j]+=dp[k][j-p.se];
				r[k]++;
			}
			if(l[k]<ps[k].size()&&ps[k][l[k]].fi<x){
				auto p=ps[k][l[k]];
				for(int j=p.se;j<=W;j++)dp[k][j]-=dp[k][j-p.se];
				l[k]++;
			}
		}

		fill_n(test,W+1,0);
		test[0]=1;
		for(int i=l[0];i<r[0];i++){
			pint p=ps[0][i];
			for(int j=W;j>=p.se;j--)test[j]+=test[j-p.se];
		}

		for(int i=0;i<=W;i++)if(test[i]!=dp[0][i]){
			cout<<l[0]<<" "<<r[0]<<endl;
			for(int i=0;i<=W;i++)cout<<test[i]<<" ";cout<<endl;
			return 0;
		}

		memset(acc,0,sizeof(acc));
		for(int j=1;j<=W;j++)acc[j]=acc[j-1]+(dp[1][j]!=0);
		for(int j=1;j<=W;j++){
			if(dp[0][j]==0)continue;
			if(acc[min(W,j+lim)]-acc[max(0ll,j-lim-1)])return true;
		}
	}
	return false;
}

signed main(){
	int n[2];
	cin>>n[0]>>n[1]>>W;
	rep(i,2){
		rep(j,n[i]){
			int m,c;
			cin>>m>>c;
			ps[i].eb(c,m);
		}
		sort(all(ps[i]));
	}
	

	int lb=-1,ub=1000000000;
	while(ub-lb>1){
		int mid=(ub+lb)/2;
		if(check(mid))ub=mid;
		else lb=mid;
	}
	cout<<ub<<endl;
	return 0;
}
accuracy: 0.25
time_ms: 190
memory_kb: 3360
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second

using vint=vector<int>;
using pint=pair<int,int>;
using vpint=vector<pint>;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

template<class A,class B>
ostream& operator<<(ostream& ost,const pair<A,B>&p){
	ost<<"{"<<p.first<<","<<p.second<<"}";
	return ost;
}

template<class T>
ostream& operator<<(ostream& ost,const vector<T>&v){
	ost<<"{";
	for(int i=0;i<v.size();i++){
		if(i)ost<<",";
		ost<<v[i];
	}
	ost<<"}";
	return ost;
}

inline int topbit(unsigned long long x){
	return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
	return __builtin_popcountll(x);
}

inline int parity(unsigned long long x){
	return __builtin_parity(x);
}


template<uint32_t mod>
struct ModInt{
	uint32_t a;
	ModInt& s(uint32_t vv){
		a=vv<mod?vv:vv-mod;
		return *this;
	}

	ModInt(int64_t x=0){s(x%mod+mod);}

	ModInt& operator+=(const ModInt &x){return s(a+x.a);}
	ModInt& operator-=(const ModInt &x){return s(a+mod-x.a);}
	ModInt& operator*=(const ModInt &x){
		a=uint64_t(a)*x.a%mod;
		return *this;
	}
	ModInt& operator/=(const ModInt &x){
		*this*=x.inv();
		return *this;
	}

	ModInt operator+(const ModInt &x)const{return ModInt(*this)+=x;}
	ModInt operator-(const ModInt &x)const{return ModInt(*this)-=x;}
	ModInt operator*(const ModInt &x)const{return ModInt(*this)*=x;}
	ModInt operator/(const ModInt &x)const{return ModInt(*this)/=x;}
	bool operator==(const ModInt &x)const{return a==x.a;}
	bool operator!=(const ModInt &x)const{return a!=x.a;}
	bool operator<(const ModInt &x)const{return a<x.a;}

	ModInt operator-()const{return ModInt()-*this;}
	ModInt pow(int64_t n)const{
		ModInt res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}

	ModInt inv()const{return pow(mod-2);}
};

template<uint32_t mod>
istream& operator>>(istream& in,ModInt<mod>& a){
	return (in>>a.a);
}
template<uint32_t mod>
ostream& operator<<(ostream& out,const ModInt<mod>& a){
	return (out<<a.a);
}
using mint1=ModInt<1000000007>;
using mint2=ModInt<998244353>;
int W;


// c m
vector<pint>ps[2];

mint1 dp1[2][11111];
mint2 dp2[2][11111];
int acc[11111];
bool check(int lim){
	vector<int>cs;
	rep(i,2)for(auto p:ps[i])cs.pb(p.fi);

	sort(all(cs));
	cs.erase(unique(all(cs)),cs.end());

	int l[2]={},r[2]={};
	fill_n(*dp1,2*11111,0);
	fill_n(*dp2,2*11111,0);

	rep(i,2)dp1[i][0]=1;
	rep(i,2)dp2[i][0]=1;

	for(auto x:cs){
		rep(k,2){
			while(r[k]<ps[k].size()&&ps[k][r[k]].fi-x<=lim){
				auto p=ps[k][r[k]];
				for(int j=W;j>=p.se;j--){
					dp1[k][j]+=dp1[k][j-p.se];
					dp2[k][j]+=dp2[k][j-p.se];
				}
				
				r[k]++;
			}
			if(l[k]<ps[k].size()&&ps[k][l[k]].fi<x){
				auto p=ps[k][l[k]];
				for(int j=p.se;j<=W;j++){
					dp1[k][j]-=dp1[k][j-p.se];
					dp2[k][j]-=dp2[k][j-p.se];
					
				}
				l[k]++;
			}
		}

		memset(acc,0,sizeof(acc));
		for(int j=1;j<=W;j++)acc[j]=acc[j-1]+(dp1[1][j]!=0||dp2[1][j]!=0);
		for(int j=1;j<=W;j++){
			if(dp1[0][j]==0&&dp2[0][j]==0)continue;
			if(acc[min(W,j+lim)]-acc[max(0ll,j-lim-1)])return true;
		}
	}
	return false;
}

signed main(){
	int n[2];
	cin>>n[0]>>n[1]>>W;
	rep(i,2){
		rep(j,n[i]){
			int m,c;
			cin>>m>>c;
			ps[i].eb(c,m);
		}
		sort(all(ps[i]));
	}
	

	int lb=-1,ub=1000000000;
	while(ub-lb>1){
		int mid=(ub+lb)/2;
		if(check(mid))ub=mid;
		else lb=mid;
	}
	cout<<ub<<endl;
	return 0;
}
accuracy: 0.25
time_ms: 210
memory_kb: 3408
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second

using vint=vector<int>;
using pint=pair<int,int>;
using vpint=vector<pint>;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

template<class A,class B>
ostream& operator<<(ostream& ost,const pair<A,B>&p){
	ost<<"{"<<p.first<<","<<p.second<<"}";
	return ost;
}

template<class T>
ostream& operator<<(ostream& ost,const vector<T>&v){
	ost<<"{";
	for(int i=0;i<v.size();i++){
		if(i)ost<<",";
		ost<<v[i];
	}
	ost<<"}";
	return ost;
}

inline int topbit(unsigned long long x){
	return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
	return __builtin_popcountll(x);
}

inline int parity(unsigned long long x){
	return __builtin_parity(x);
}


template<uint32_t mod>
struct ModInt{
	uint32_t a;
	ModInt& s(uint32_t vv){
		a=vv<mod?vv:vv-mod;
		return *this;
	}

	ModInt(int64_t x=0){s(x%mod+mod);}

	ModInt& operator+=(const ModInt &x){return s(a+x.a);}
	ModInt& operator-=(const ModInt &x){return s(a+mod-x.a);}
	ModInt& operator*=(const ModInt &x){
		a=uint64_t(a)*x.a%mod;
		return *this;
	}
	ModInt& operator/=(const ModInt &x){
		*this*=x.inv();
		return *this;
	}

	ModInt operator+(const ModInt &x)const{return ModInt(*this)+=x;}
	ModInt operator-(const ModInt &x)const{return ModInt(*this)-=x;}
	ModInt operator*(const ModInt &x)const{return ModInt(*this)*=x;}
	ModInt operator/(const ModInt &x)const{return ModInt(*this)/=x;}
	bool operator==(const ModInt &x)const{return a==x.a;}
	bool operator!=(const ModInt &x)const{return a!=x.a;}
	bool operator<(const ModInt &x)const{return a<x.a;}

	ModInt operator-()const{return ModInt()-*this;}
	ModInt pow(int64_t n)const{
		ModInt res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}

	ModInt inv()const{return pow(mod-2);}
};

template<uint32_t mod>
istream& operator>>(istream& in,ModInt<mod>& a){
	return (in>>a.a);
}
template<uint32_t mod>
ostream& operator<<(ostream& out,const ModInt<mod>& a){
	return (out<<a.a);
}
using mint=ModInt<1000000007>;

int W;


// c m
vector<pint>ps[2];

mint dp[2][11111];

int acc[11111];
bool check(int lim){
	vector<int>cs;
	rep(i,2)for(auto p:ps[i])cs.pb(p.fi);

	sort(all(cs));
	cs.erase(unique(all(cs)),cs.end());

	int l[2]={},r[2]={};
	fill_n(*dp,2*11111,0);

	rep(i,2)dp[i][0]=1;

	for(auto x:cs){
		rep(k,2){
			while(r[k]<ps[k].size()&&ps[k][r[k]].fi-x<=lim){
				auto p=ps[k][r[k]];
				for(int j=W;j>=p.se;j--)dp[k][j]+=dp[k][j-p.se];
				r[k]++;
			}
			if(l[k]<ps[k].size()&&ps[k][l[k]].fi<x){
				auto p=ps[k][l[k]];
				for(int j=p.se;j<=W;j++)dp[k][j]-=dp[k][j-p.se];
				l[k]++;
			}
		}

		memset(acc,0,sizeof(acc));
		for(int j=1;j<=W;j++)acc[j]=acc[j-1]+(dp[1][j]!=0);
		for(int j=1;j<=W;j++){
			if(dp[0][j]==0)continue;
			if(acc[min(W,j+lim)]-acc[max(0ll,j-lim-1)])return true;
		}
	}
	return false;
}

signed main(){
	int n[2];
	cin>>n[0]>>n[1]>>W;
	rep(i,2){
		rep(j,n[i]){
			int m,c;
			cin>>m>>c;
			ps[i].eb(c,m);
		}
		sort(all(ps[i]));
	}
	

	int lb=-1,ub=1000000000;
	while(ub-lb>1){
		int mid=(ub+lb)/2;
		if(check(mid))ub=mid;
		else lb=mid;
	}
	cout<<ub<<endl;
	return 0;
}
accuracy: 0.25
time_ms: 140
memory_kb: 3304
final_rank: 15

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,ssse3,sse4a")
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(const auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define sar(a,n) {cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl;}

using namespace std;

template<typename S,typename T>auto&operator<<(ostream&o,pair<S,T>p){return o<<"{"<<p.fi<<","<<p.se<<"}";}
template<typename T>auto&operator<<(ostream&o,set<T>s){for(auto&e:s)o<<e<<" ";return o;}
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,priority_queue<S,T,U>q){while(!q.empty())o<<q.top()<<" ",q.pop();return o;}
template<typename K,typename T>auto&operator<<(ostream&o,map<K,T>&m){for(auto&e:m)o<<e<<" ";return o;}
template<typename T>auto&operator<<(ostream&o,vector<T>v){for(auto&e:v)o<<e<<" ";return o;}
void ashow(){cout<<endl;}template<typename T,typename...A>void ashow(T t,A...a){cout<<t<<" ";ashow(a...);}
template<typename S,typename T,typename U>
struct TRI{S fi;T se;U th;TRI(){}TRI(S f,T s,U t):fi(f),se(s),th(t){}
bool operator<(const TRI&_)const{return(fi==_.fi)?((se==_.se)?(th<_.th):(se<_.se)):(fi<_.fi);}};
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,TRI<S,T,U>&t){return o<<"{"<<t.fi<<","<<t.se<<","<<t.th<<"}";}

typedef pair<int, int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int na, nb, w;
    cin >> na >> nb >> w;
    const int n = na + nb;
    vector<int> ma(n), ca(n);
    vector<pair<int, int> > vec(n);
    rep(i,na){
        cin >> ma[i] >> ca[i];
        vec[i] = {ca[i], i};
    }
    rep(i,nb){
        cin >> ma[na + i] >> ca[na + i];
        vec[na + i] = {ca[na + i], na + i};
    }
    sort(all(vec));
    int ans = INF;
    rep(i,n-1){
        bitset<10001> bt1, rbt1, bt2, rbt2, nbt, diff;
        bt1[0] = 1, rbt1[10000] = 1, bt2[0] = 1, rbt2[10000] = 1;
        const int st = ca[vec[i].second];
        for(int j = i; j < n; ++j){
            if(vec[j].second < na){
                const int c = ca[vec[j].second];
                const int m = ma[vec[j].second];
                nbt = (bt1 | (bt1 << m));
                if(bt2 != 1){
                    diff = (bt1 ^ nbt);
                    int mn = INF;
                    for(int k = diff._Find_first(); k <= w; k = diff._Find_next(k)){
                        const int hoge = bt2._Find_next(k-1);
                        if(hoge <= w) cmn(mn, hoge - k);
                        const int hoge2 = 10000 - rbt2._Find_next(9999 - k);
                        if(hoge2 != 0) cmn(mn, k - hoge2);
                    }
                    cmn(ans, max(c - st, mn));
                }
                bt1 = nbt, rbt1 = (rbt1 | (rbt1 >> m));
            }else{
                const int c = ca[vec[j].second];
                const int m = ma[vec[j].second];
                nbt = (bt2 | (bt2 << m));
                if(bt1 != 1){
                    diff = (bt2 ^ nbt);
                    int mn = INF;
                    for(int k = diff._Find_first(); k <= w; k = diff._Find_next(k)){
                        const int hoge = bt1._Find_next(k-1);
                        if(hoge <= w) cmn(mn, hoge - k);
                        const int hoge2 = 10000 - rbt1._Find_next(9999 - k);
                        if(hoge2 != 0) cmn(mn, k - hoge2);
                    }
                    cmn(ans, max(c - st, mn));
                }
                bt2 = nbt, rbt2 = (rbt2 | (rbt2 >> m));
            }
        }
    }
    cout << ans << "\n";
    return 0;
}
accuracy: 1
time_ms: 610
memory_kb: 3204
final_rank: 8

code_snippet: #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(const auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define sar(a,n) {cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl;}

using namespace std;

template<typename S,typename T>auto&operator<<(ostream&o,pair<S,T>p){return o<<"{"<<p.fi<<","<<p.se<<"}";}
template<typename T>auto&operator<<(ostream&o,set<T>s){for(auto&e:s)o<<e<<" ";return o;}
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,priority_queue<S,T,U>q){while(!q.empty())o<<q.top()<<" ",q.pop();return o;}
template<typename K,typename T>auto&operator<<(ostream&o,map<K,T>&m){for(auto&e:m)o<<e<<" ";return o;}
template<typename T>auto&operator<<(ostream&o,vector<T>v){for(auto&e:v)o<<e<<" ";return o;}
void ashow(){cout<<endl;}template<typename T,typename...A>void ashow(T t,A...a){cout<<t<<" ";ashow(a...);}
template<typename S,typename T,typename U>
struct TRI{S fi;T se;U th;TRI(){}TRI(S f,T s,U t):fi(f),se(s),th(t){}
bool operator<(const TRI&_)const{return(fi==_.fi)?((se==_.se)?(th<_.th):(se<_.se)):(fi<_.fi);}};
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,TRI<S,T,U>&t){return o<<"{"<<t.fi<<","<<t.se<<","<<t.th<<"}";}

typedef pair<int, int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int na, nb, w;
    cin >> na >> nb >> w;
    const int n = na + nb;
    vector<int> ma(n), ca(n);
    vector<pair<int, int> > vec(n);
    rep(i,na){
        cin >> ma[i] >> ca[i];
        vec[i] = {ca[i], i};
    }
    rep(i,nb){
        cin >> ma[na + i] >> ca[na + i];
        vec[na + i] = {ca[na + i], na + i};
    }
    sort(all(vec));
    int ans = INF;
    rep(i,n-1){
        bitset<10001> bt1, rbt1, bt2, rbt2, nbt, diff;
        bt1[0] = 1, rbt1[10000] = 1, bt2[0] = 1, rbt2[10000] = 1;
        const int st = ca[vec[i].second];
        for(int j = i; j < n; ++j){
            if(vec[j].second < na){
                const int c = ca[vec[j].second];
                const int m = ma[vec[j].second];
                nbt = (bt1 | (bt1 << m));
                if(bt2 != 1){
                    diff = (bt1 ^ nbt);
                    int mn = INF;
                    for(int k = diff._Find_first(); k <= w; k = diff._Find_next(k)){
                        const int hoge = bt2._Find_next(k-1);
                        if(hoge <= w) cmn(mn, hoge - k);
                        const int hoge2 = 10000 - rbt2._Find_next(9999 - k);
                        if(hoge2 != 0) cmn(mn, k - hoge2);
                    }
                    cmn(ans, max(c - st, mn));
                }
                bt1 = nbt, rbt1 = (rbt1 | (rbt1 >> m));
            }else{
                const int c = ca[vec[j].second];
                const int m = ma[vec[j].second];
                nbt = (bt2 | (bt2 << m));
                if(bt1 != 1){
                    diff = (bt2 ^ nbt);
                    int mn = INF;
                    for(int k = diff._Find_first(); k <= w; k = diff._Find_next(k)){
                        const int hoge = bt1._Find_next(k-1);
                        if(hoge <= w) cmn(mn, hoge - k);
                        const int hoge2 = 10000 - rbt1._Find_next(9999 - k);
                        if(hoge2 != 0) cmn(mn, k - hoge2);
                    }
                    cmn(ans, max(c - st, mn));
                }
                bt2 = nbt, rbt2 = (rbt2 | (rbt2 >> m));
            }
        }
    }
    cout << ans << "\n";
    return 0;
}
accuracy: 1
time_ms: 610
memory_kb: 3256
final_rank: 10