Problem:
ジョイ四人組 (JOI04)
問題文
JOI 中学校には
4N
人の一年生が在籍しており，
4
つのクラスに分かれている．各クラスの情報は以下の通りである．
1 年 A 組：
N
人の生徒がいる．それぞれの生徒の身長は
A
1
, A
2
, …, A
N
である．
1 年 B 組：
N
人の生徒がいる．それぞれの生徒の身長は
B
1
, B
2
, …, B
N
である．
1 年 C 組：
N
人の生徒がいる．それぞれの生徒の身長は
C
1
, C
2
, …, C
N
である．
1 年 D 組：
N
人の生徒がいる．それぞれの生徒の身長は
D
1
, D
2
, …, D
N
である．
来月，JOI 中学校では体育祭が開催されることになった．体育祭には，リレー，騎馬戦，棒倒しなどの様々な種目があるが，各学年が踊るダンスは「体育祭の華」とも呼ばれる注目の種目である．
ここで一年生は，各クラスから代表を
1
人ずつ選び，
4
人でダンスをすることになった．ダンスの見栄えをできるだけ良くするため，身長の差ができるだけ小さくなるように
4
人組を選ぶことにした．
一年生の身長が与えられるとき，「
4
人の身長の最大値」と「
4
人の身長の最小値」の差として考えられる最小の値を求めるプログラムを作成せよ．
制約
1 ≦ N ≦ 75000
．
1 ≦ A
i
≦ 10
9
(
1 ≦ i ≦ N
)．
1 ≦ B
j
≦ 10
9
(
1 ≦ j ≦ N
)．
1 ≦ C
k
≦ 10
9
(
1 ≦ k ≦ N
)．
1 ≦ D
l
≦ 10
9
(
1 ≦ l ≦ N
)．
入力される値はすべて整数である．
入力
入力は以下の形式で与えられる．
N
A
1
A
2
…
A
N
B
1
B
2
…
B
N
C
1
C
2
…
C
N
D
1
D
2
…
D
N
出力
「
4
人の身長の最大値」と「
4
人の身長の最小値」の差として考えられる最小の値を
1
行で出力せよ．
入出力例
入力例 1
1
169
173
152
200
出力例 1
48
N = 1
なので，全員の生徒を選んで
4
人組を作らなければならない．このとき，
4
人の身長の最大値は
200
，最小値は
152
となり，その差は
200 - 152 = 48
である．よって，
48
を出力する．
入力例 2
7
7 9 9 4 6 3 5
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
出力例 2
2
入力例 3
7
1 1 1 1 2 1 1
1 2 1 1 1 1 1
1 1 1 1 2 1 1
1 1 1 1 1 1 2
出力例 3
0
入力例 4
5
287 690 413 420 138
813 873 223 415 907
261 330 361 747 787
958 672 544 126 345
出力例 4
70
入力例 5
10
1682 2008 1135 1576 2450 1362 1518 1925 2212 1275
1993 1945 1312 1401 2027 1705 1086 2333 1787 1654
2257 1548 1219 1031 2613 2171 1866 1532 2800 1497
1062 1175 1984 1870 2059 1639 2107 1335 1289 2494
出力例 5
79
情報オリンピック日本委員会
作 『第 22 回日本情報オリンピック JOI 2022/2023 二次予選競技課題』

Submitted solutions:

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include<utility>
#include<set>
#include<deque>
#include <unordered_map>
#include<string>
#include<map>
#include<cmath>

using namespace std;


const long long INF = 1LL << 60;
int main() {
	int N;
	cin >> N;
	vector<vector<long long>>A(4, vector<long long>(N, 0));
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < N; j++) {
			cin >> A[i][j];
		}
		A[i].push_back(INF);
		sort(A[i].begin(), A[i].end());
	}
	
	long long ans = INF;
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < N; j++) {
			long long a = A[i][j];

			long long ka = 0;
			for (int o = 0; o < 4; o++) {
				long long m = *lower_bound(A[o].begin(), A[o].end(), a);
				
				ka = max(ka, m - a);

			}
			ans = min(ans,ka);
		}


	}
	
	cout << ans << endl;
}
accuracy: 1
time_ms: 120
memory_kb: 6084
final_rank: 19

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

struct Student {
    char grp;
    int height;
};

bool comp(const Student& a, const Student& b) {
    if (a.height == b.height)
        return a.grp < b.grp;
    else
        return a.height < b.height;
}

int main()
{
    int N;
    std::cin >> N;
    std::vector<Student> ST(4 * N);

    for (int i = 0; i < N; i++) {
        ST[i].grp = 'A';
        std::cin >> ST[i].height;
    }
    for (int i = 0; i < N; i++) {
        ST[N + i].grp = 'B';
        std::cin >> ST[N + i].height;
    }
    for (int i = 0; i < N; i++) {
        ST[2 * N + i].grp = 'C';
        std::cin >> ST[2 * N + i].height;
    }
    for (int i = 0; i < N; i++) {
        ST[3 * N + i].grp = 'D';
        std::cin >> ST[3 * N + i].height;
    }

    std::sort(ST.begin(), ST.end(), comp);

    int ans = 1000000009;
    int cnt_A = 0, cnt_B = 0, cnt_C = 0, cnt_D = 0;
    int diff;
    int i = 0;
    for (int j = 0; j < 4 * N; j++) {
        if (ST[j].grp == 'A')
            ++cnt_A;
        else if (ST[j].grp == 'B')
            ++cnt_B;
        else if (ST[j].grp == 'C')
            ++cnt_C;
        else
            ++cnt_D;
        while (j - i >= 3 && cnt_A > 0 && cnt_B > 0 && cnt_C > 0 && cnt_D > 0) {
            diff = ST[j].height - ST[i].height;
            if (diff < ans)
                ans = diff;
            if (ST[i].grp == 'A')
                --cnt_A;
            else if (ST[i].grp == 'B')
                --cnt_B;
            else if (ST[i].grp == 'C')
                --cnt_C;
            else
                --cnt_D;
            ++i;
        }
    }

    std::cout << ans << std::endl;

    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 5364
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main(){
    int n;
    cin >> n;
    vector<int> a(n),b(n),c(n),d(n);
    rep(i,n){
        cin >> a[i];
    }
    rep(i,n){
        cin >> b[i];
    }
    rep(i,n){
        cin >> c[i];
    }
    rep(i,n){
        cin >> d[i];
    }
    sort(a.begin(),a.end());
    sort(b.begin(),b.end());
    sort(c.begin(),c.end());
    sort(d.begin(),d.end());

    int res=INT_MAX;
    int i=0,j=0,k=0,l=0;
    while(i<n && j<n && k<n && l<n){
        int curmax=max({a[i],b[j],c[k],d[l]});
        int curmin=min({a[i],b[j],c[k],d[l]});
        res = min(res,curmax-curmin);
        if(curmin==a[i]){
            i++;
        }
        else if(curmin==b[j]){
            j++;
        }
        else if(curmin==c[k]){
            k++;
        }
        else{
            l++;
        }
    }
    cout << res << endl;
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 4348
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long INF = 1LL << 60;
const int inf = 1<<30;
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
//a:97, A:65
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
const double PI = acos(-1);
typedef string::const_iterator State;//構文解析用

int main(){
    int n; cin >> n;
    vector<pair<int,int>> p;
    for(int i=0;i<n;i++){
        int a; cin >> a;
        p.push_back({a,1});
    }
    for(int i=0;i<n;i++){
        int a; cin >> a;
        p.push_back({a,2});
    }
    for(int i=0;i<n;i++){
        int a; cin >> a;
        p.push_back({a,3});
    }
    for(int i=0;i<n;i++){
        int a; cin >> a;
        p.push_back({a,4});
    }

    n*=4;
    sort(p.begin(),p.end());
    int a=0,b=0,c=0,d=0,ans=inf;
    while(a<n||b<n||c<n||d<n){ // 4*75000 = 3*10^5
        while(a<n&&p[a].second!=1) a++;
        while(b<n&&p[b].second!=2) b++;
        while(c<n&&p[c].second!=3) c++;
        while(d<n&&p[d].second!=4) d++;

        int mx=max(max(p[a].first,p[b].first),max(p[c].first,p[d].first));
        int mi=min(min(p[a].first,p[b].first),min(p[c].first,p[d].first));
        ans=min(ans,mx-mi);
        if(a<n&&mi==p[a].first) a++;
        else if(b<n&&mi==p[b].first) b++;
        else if(c<n&&mi==p[c].first) c++;
        else if(d<n) d++;
        else break;
        //cout << a << " " << b << " " << c << " " << d << endl;
    }

    cout << ans << endl;
}
accuracy: 1
time_ms: 80
memory_kb: 9316
final_rank: 20

code_snippet: #include <algorithm>
#include <iostream>
#include <vector>

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  int N;
  std::cin >> N;
  std::vector<int> A(N), B(N), C(N), D(N);
  for (auto &a : A) std::cin >> a;
  for (auto &b : B) std::cin >> b;
  for (auto &c : C) std::cin >> c;
  for (auto &d : D) std::cin >> d;
  std::sort(A.begin(), A.end());
  std::sort(B.begin(), B.end());
  std::sort(C.begin(), C.end());
  std::sort(D.begin(), D.end());
  int min_diff = 2e9;
  size_t i = 0, j = 0, k = 0, l = 0;
  while (i < A.size() && j < B.size() && k < C.size() && l < D.size()) {
    int min_height = std::min({A[i], B[j], C[k], D[l]});
    int max_height = std::max({A[i], B[j], C[k], D[l]});
    min_diff = std::min(min_diff, max_height - min_height);
    if (min_height == A[i]) i++;
    else if (min_height == B[j]) j++;
    else if (min_height == C[k]) k++;
    else l++;
  }
  std::cout << min_diff << std::endl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4392
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
  #include "settings/debug.cpp"
#else
  #define Debug(...) void(0)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), c(n), d(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) cin >> c[i];
  rep(i, n) cin >> d[i];
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  sort(d.begin(), d.end());
  int ans = 2e9;
  rep(i, n) {
    vector<int> w = { a[i] }, x, y, z;
    for (auto&& [arr, cand] : vector<pair<vector<int>&, vector<int>&>> { { b, x }, { c, y }, { d, z } }) {
      auto it = lower_bound(arr.begin(), arr.end(), a[i]);
      if (it != arr.end()) cand.push_back(*it);
      if (it != arr.begin()) cand.push_back(*prev(it));
    }
    Debug(w, x, y, z);
    for (auto&& wi : w)
      for (auto&& xi : x)
        for (auto&& yi : y)
          for (auto&& zi : z) {
            ans = min(ans, max({ wi, xi, yi, zi }) - min({ wi, xi, yi, zi }));
          }
  }
  cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 4480
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i,l,r) for(ll i=l;i<r;i++)
#define REP(i,n) FOR(i,0,n)
int main(){
    int n;cin>>n;
    vector<vector<ll>>a(4,vector<ll>(n));
    REP(i,4)REP(j,n)cin>>a[i][j];
    REP(i,4)sort(a[i].begin(),a[i].end());
    ll ans=1e18;
    REP(i,4)REP(j,n){
        ll s=0;
        REP(k,4){
            auto it=lower_bound(a[k].begin(),a[k].end(),a[i][j]);
            if(it==a[k].end())s=1e18;
            else s=max(s,*it);
        }
        ans=min(ans,s-a[i][j]);
    }
    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 6052
final_rank: 18

code_snippet: #include <algorithm>
#include <iostream>
#include <limits>
#include <vector>

std::vector<int> read_and_sort_vector(const int n) {
    std::vector<int> res(n);
    for (int i{0}; i < n; ++i) {
        std::cin >> res[i];
    }
    std::sort(res.begin(), res.end());

    return res;
}

int main(void) {
    int n{};
    std::cin >> n;
    const auto a{read_and_sort_vector(n)}, b{read_and_sort_vector(n)},
        c{read_and_sort_vector(n)}, d{read_and_sort_vector(n)};

    int ib{0}, ic{0}, id{0}, min_range_size{std::numeric_limits<int>::max()};
    for (int ia{0}; ia < n; ++ia) {
        while (ib + 1 < n && b[ib] < a[ia]) ++ib;
        while (ic + 1 < n && c[ic] < a[ia]) ++ic;
        while (id + 1 < n && d[id] < a[ia]) ++id;

        const std::vector<int> candidates_b = {b[std::max(ib - 1, 0)], b[ib]};
        const std::vector<int> candidates_c = {c[std::max(ic - 1, 0)], c[ic]};
        const std::vector<int> candidates_d = {d[std::max(id - 1, 0)], d[id]};

        for (const int vb : candidates_b) {
            for (const int vc : candidates_c) {
                for (const int vd : candidates_d) {
                    const std::vector<int> v{a[ia], vb, vc, vd};
                    const auto [min_it, max_it] =
                        std::minmax_element(v.begin(), v.end());

                    min_range_size =
                        std::min(min_range_size, *max_it - *min_it);
                }
            }
        }
    }

    std::cout << min_range_size << std::endl;

    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 4300
final_rank: 9

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int N;
    cin >> N;
    
    // 4つのクラスの身長を格納するベクター
    vector<long long> A(N), B(N), C(N), D(N);
    
    for(int i=0; i<N; i++) cin >> A[i];
    for(int i=0; i<N; i++) cin >> B[i];
    for(int i=0; i<N; i++) cin >> C[i];
    for(int i=0; i<N; i++) cin >> D[i];
    
    // 各クラスをソート
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    sort(C.begin(), C.end());
    sort(D.begin(), D.end());
    
    // 各クラスのポインタを初期化
    int i=0, j=0, k=0, l=0;
    long long min_diff = LLONG_MAX;
    
    while(i < N && j < N && k < N && l < N){
        // 現在の4つの値を取得
        long long current_A = A[i];
        long long current_B = B[j];
        long long current_C = C[k];
        long long current_D = D[l];
        
        // 現在の最大値と最小値を計算
        long long current_max = max({current_A, current_B, current_C, current_D});
        long long current_min = min({current_A, current_B, current_C, current_D});
        
        // 差を計算し、最小差を更新
        long long diff = current_max - current_min;
        if(diff < min_diff){
            min_diff = diff;
        }
        
        // 最小値を持つクラスのポインタを進める
        if(current_min == current_A){
            i++;
        }
        else if(current_min == current_B){
            j++;
        }
        else if(current_min == current_C){
            k++;
        }
        else{ // current_min == current_D
            l++;
        }
    }
    
    // 最終的な結果を出力
    cout << min_diff << endl;

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 5428
final_rank: 4

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using vc = vector<int>;
using vvc = vector<vector<int>>;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
const long long INF = 1LL << 60;

int N;
int A[75009], B[75009], C[75009], D[75009];

int solve(int LIMIT) {
	int pos1 = lower_bound(A + 1, A + N + 1, LIMIT) - A;
	int pos2 = lower_bound(B + 1, B + N + 1, LIMIT) - B;
	int pos3 = lower_bound(C + 1, C + N + 1, LIMIT) - C;
	int pos4 = lower_bound(D + 1, D + N + 1, LIMIT) - D;
	if (pos1 == N + 1 || pos2 == N + 1 || pos3 == N + 1 || pos4 == N + 1) return 2000000000;
	return max({ A[pos1], B[pos2], C[pos3], D[pos4] });
}

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

	cin >> N;
	for (int i = 1; i <= N; i++) cin >> A[i];
	for (int i = 1; i <= N; i++) cin >> B[i];
	for (int i = 1; i <= N; i++) cin >> C[i];
	for (int i = 1; i <= N; i++) cin >> D[i];

	// 驟榊�繧偵た繝ｼ繝�
	sort(A + 1, A + N + 1);
	sort(B + 1, B + N + 1);
	sort(C + 1, C + N + 1);
	sort(D + 1, D + N + 1);

	// 豎ｺ繧∵遠縺｡蜈ｨ謗｢邏｢
	int Answer = 2000000000;
	for (int i = 1; i <= N; i++) Answer = min(Answer, solve(A[i]) - A[i]);
	for (int i = 1; i <= N; i++) Answer = min(Answer, solve(B[i]) - B[i]);
	for (int i = 1; i <= N; i++) Answer = min(Answer, solve(C[i]) - C[i]);
	for (int i = 1; i <= N; i++) Answer = min(Answer, solve(D[i]) - D[i]);

	// 蜃ｺ蜉�
	cout << Answer << endl;
	return 0;

}
accuracy: 1
time_ms: 60
memory_kb: 4600
final_rank: 5

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using vc = vector<int>;
using vvc = vector<vector<int>>;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
const long long INF = 1LL << 60;

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

	unsigned int N, i[4], j, max, min = 0, Min = UINT_MAX;
	cin >> N;

	vector<unsigned int> S[4] = { vector<unsigned int>(N), vector<unsigned int>(N), vector<unsigned int>(N), vector<unsigned int>(N) };
	for (j = 0; j != 4; ++j)
	{
		for (i[j] = 0; i[j] != N; ++i[j]) cin >> S[j][i[j]];
		stable_sort(S[j].begin(), S[j].end());
	}

	for (i[0] = i[1] = i[2] = i[3] = 0; i[min] != N; ++i[min])
	{
		max = 0, min = 0;
		for (j = 1; j != 4; ++j)
		{
			if (S[j][i[j]] < S[min][i[min]]) min = j;
			if (S[j][i[j]] > S[max][i[max]]) max = j;
		}

		if (S[max][i[max]] - S[min][i[min]] < Min) Min = S[max][i[max]] - S[min][i[min]];
	}

	cout << Min << '\n';
	return 0;


    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4432
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

int main() {
    int N;
    cin >> N;

    vector<vector<int>> classes(4, vector<int>(N));

    // 入力を読み込む
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < N; j++) {
            cin >> classes[i][j];
        }
    }

    // 各クラスの身長をソート
    for (int i = 0; i < 4; i++) {
        sort(classes[i].begin(), classes[i].end());
    }

    int min_diff = INT_MAX;
    int a = 0, b = 0, c = 0, d = 0;

    while (a < N && b < N && c < N && d < N) {
        int min_height = min({classes[0][a], classes[1][b], classes[2][c], classes[3][d]});
        int max_height = max({classes[0][a], classes[1][b], classes[2][c], classes[3][d]});
        
        min_diff = min(min_diff, max_height - min_height);

        if (classes[0][a] == min_height) a++;
        else if (classes[1][b] == min_height) b++;
        else if (classes[2][c] == min_height) c++;
        else d++;
    }

    cout << min_diff << endl;

    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 4668
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <limits>

using namespace std;

int main() {
    int N;
    cin >> N;

    vector<int> A(N), B(N), C(N), D(N);
    for (int i = 0; i < N; ++i) cin >> A[i];
    for (int i = 0; i < N; ++i) cin >> B[i];
    for (int i = 0; i < N; ++i) cin >> C[i];
    for (int i = 0; i < N; ++i) cin >> D[i];

    // 各リストをソート
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    sort(C.begin(), C.end());
    sort(D.begin(), D.end());

    // 4-way merge setup
    vector<int> indices(4, 0);
    int min_diff = numeric_limits<int>::max();

    while (indices[0] < N && indices[1] < N && indices[2] < N && indices[3] < N) {
        // 現在の4クラスの生徒の身長を取得
        int currentA = A[indices[0]];
        int currentB = B[indices[1]];
        int currentC = C[indices[2]];
        int currentD = D[indices[3]];

        // 最小値と最大値を見つける
        int min_height = min({currentA, currentB, currentC, currentD});
        int max_height = max({currentA, currentB, currentC, currentD});

        // 差を更新
        min_diff = min(min_diff, max_height - min_height);

        // 最小値を持つインデックスを進める
        if (min_height == currentA) {
            indices[0]++;
        } else if (min_height == currentB) {
            indices[1]++;
        } else if (min_height == currentC) {
            indices[2]++;
        } else {
            indices[3]++;
        }
    }

    cout << min_diff << endl;
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 4300
final_rank: 9

code_snippet: #include <iostream>
#include <climits>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	unsigned int N, i[4], j, max, min = 0, Min = UINT_MAX;
	cin >> N;

	vector<unsigned int> S[4] = { vector<unsigned int>(N), vector<unsigned int>(N), vector<unsigned int>(N), vector<unsigned int>(N) };
	for (j = 0; j != 4; ++j)
	{
		for (i[j] = 0; i[j] != N; ++i[j]) cin >> S[j][i[j]];
		stable_sort(S[j].begin(), S[j].end());
	}

	for (i[0] = i[1] = i[2] = i[3] = 0; i[min] != N; ++i[min])
	{
		max = 0, min = 0;
		for (j = 1; j != 4; ++j)
		{
			if (S[j][i[j]] < S[min][i[min]]) min = j;
			if (S[j][i[j]] > S[max][i[max]]) max = j;
		}

		if (S[max][i[max]] - S[min][i[min]] < Min) Min = S[max][i[max]] - S[min][i[min]];
	}

	cout << Min << '\n';
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4392
final_rank: 1

code_snippet: #line 1 "/opt/library/template.hpp"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'001'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
#define inf infty<ll>

using pi = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

#define rep1(a) for (ll _ = 0; _ < (ll)(a); ++_)
#define rep2(i, a) for (ll i = 0; i < (ll)(a); ++i)
#define rep3(i, a, b) for (ll i = a; i < (ll)(b); ++i)
#define rep4(i, a, b, c) for (ll i = a; i < (ll)(b); i += (c))
#define rrep1(a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep2(i, a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep3(i, a, b) for (ll i = (b)-1; i >= (ll)(a); --i)
#define rrep4(i, a, b, c) for (ll i = (b)-1; i >= (ll)(a); i -= (c))
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)

#define all(x) (x).begin(),(x).end()
#define len(x) (ll)(x.size())
#define elif else if
#define bit(x, i) (((x)>>(i))&1)

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll
#define abs llabs

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
  sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

ll popcnt(ll x) { return __builtin_popcountll(x); }
ll popcnt(u64 x) { return __builtin_popcountll(x); }
ll popcnt_mod_2(ll x) { return __builtin_parityll(x); }
ll popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
ll topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
ll lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template<typename T> T SUM(const vector<T> &A) {
  T s = 0; for (auto &&a: A) s += a;
  return s;
}

template<typename T> inline bool chmax(T &a, T b) {return ((a<b)?(a=b,true):(false));}
template<typename T> inline bool chmin(T &a, T b) {return ((a>b)?(a=b,true):(false));}

inline void wt(const char c) { cout << c; }
inline void wt(const string s) { cout << s; }
inline void wt(const char *s) { cout << s; }

template <typename T>
void wt_integer(T x) {
  if (x < 0) { cout << (i64)(x); }
  else { cout << (u64)(x); }
}
template <typename T>
void wt_real(T x) {
  cout << fixed << setprecision(15) << (long double)(x);
}

inline void wt(int x) { wt_integer(x); }
inline void wt(ll x) { wt_integer(x); }
inline void wt(i128 x) { wt_integer(x); }
inline void wt(u32 x) { wt_integer(x); }
inline void wt(u64 x) { wt_integer(x); }
inline void wt(u128 x) { wt_integer(x); }
inline void wt(double x) { wt_real(x); }
inline void wt(long double x) { wt_real(x); }
inline void wt(f128 x) { wt_real(x); }

template <class T, class U>
void wt(const pair<T, U> val) {
  wt(val.first); wt(' '); wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) { wt(' '); }
    const auto x = std::get<N>(t);
    wt(x);
    wt_tuple<N + 1>(t);
  }
}
template <class... T>
void wt(tuple<T...> tpl) {
  wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}
template <class T>
void wt(const vector<T> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}

void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  wt(head);
  if (sizeof...(Tail)) wt(' ');
  print(forward<Tail>(tail)...);
}

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
void onez(bool t = 1) { print(t ? 1 : 0); }
#define endl '\n'
#define dump(x) {cerr << #x " = " << x << '\n';}
#line 2 "main.cpp"

int solve();
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  while (!solve()) {break;}
  return 0;
}

int solve() {
  ll M = 4;
  ll N;
  cin >> N;
  vv(ll, A, M, N);
  rep(i, M) {
    rep(j, N) cin >> A[i][j];
    sort(all(A[i]));
  }
  ll ans = inf;
  rep(i, M) {
    rep(j, N) {
      vi t(4);
      rep(k, M) {
        t[k] = A[k][min((ll)LB(A[k], A[i][j]), N-1)];
      }
      chmin(ans, MAX(t)-MIN(t));
    }
  }
  print(ans);
  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 5992
final_rank: 13

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 0748.cc: ジョイ四人組 (JOI04)
 */

#include<cstdio>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 75000;
const int MAX_N4 = MAX_N * 4;
const int INF = 1 << 30;

/* typedef */

/* global variables */

int as[MAX_N], bs[MAX_N], cs[MAX_N], ds[MAX_N];
int xs[MAX_N4];

/* subroutines */

void readv(int n, int as[], int &m) {
  for (int i = 0; i < n; i++)
    scanf("%d", as + i), xs[m++] = as[i];
  sort(as, as + n);
}

/* main */

int main() {
  int n;
  scanf("%d", &n);

  int m = 0;
  readv(n, as, m);
  readv(n, bs, m);
  readv(n, cs, m);
  readv(n, ds, m);

  sort(xs, xs + m);
  m = unique(xs, xs + m) - xs;

  int mind = INF;
  for (int i = 0, ai = 0, bi = 0, ci = 0, di = 0; i < m; i++) {
    while (ai < n && as[ai] < xs[i]) ai++;
    if (ai >= n) break;
    while (bi < n && bs[bi] < xs[i]) bi++;
    if (bi >= n) break;
    while (ci < n && cs[ci] < xs[i]) ci++;
    if (ci >= n) break;
    while (di < n && ds[di] < xs[i]) di++;
    if (di >= n) break;

    int maxv = max(max(as[ai], bs[bi]), max(cs[ci], ds[di]));
    mind = min(mind, maxv - xs[i]);
  }

  printf("%d\n", mind);
  
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 5284
final_rank: 7

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

constexpr int INF=(1<<30)-1;

int func(int key,vector<int> &A,vector<int> &B,vector<int> &C,vector<int> &D){
    int N=A.size();
    int idx1=lower_bound(A.begin(),A.end(),key)-A.begin();
    int idx2=lower_bound(B.begin(),B.end(),key)-B.begin();
    int idx3=lower_bound(C.begin(),C.end(),key)-C.begin();
    int idx4=lower_bound(D.begin(),D.end(),key)-D.begin();
    if(idx1==N or idx2==N or idx3==N or idx4==N) return INF*2;
    return max({A[idx1],B[idx2],C[idx3],D[idx4]});
}

int main(){

    int N; cin >> N;
    vector<int> A(N),B(N),C(N),D(N);
    for(int i=0;i<N;i++) cin >> A[i];
    for(int i=0;i<N;i++) cin >> B[i];
    for(int i=0;i<N;i++) cin >> C[i];
    for(int i=0;i<N;i++) cin >> D[i];

    sort(A.begin(),A.end());
    sort(B.begin(),B.end());
    sort(C.begin(),C.end());
    sort(D.begin(),D.end());

    int ans=INF*2;
    
    for(int i=0;i<N;i++){
        int minval=A[i];
        int maxval=func(minval,A,B,C,D);
        ans=min(ans,maxval-minval);
    }
    for(int i=0;i<N;i++){
        int minval=B[i];
        int maxval=func(minval,A,B,C,D);
        ans=min(ans,maxval-minval);
    }
    for(int i=0;i<N;i++){
        int minval=C[i];
        int maxval=func(minval,A,B,C,D);
        ans=min(ans,maxval-minval);
    }
    for(int i=0;i<N;i++){
        int minval=D[i];
        int maxval=func(minval,A,B,C,D);
        ans=min(ans,maxval-minval);
    }

    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 4340
final_rank: 17

code_snippet: #include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<tuple>
#include<math.h>
#include<numeric>
#include<set>
#include <climits>
#include<map>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;
typedef long double ld;
//int a, b, c;
void swap(int* p, int* q) {
    int t = *p;
    *p = *q;
    *q = t;
}
ld you(ld x1, ld y1, ld x2, ld y2) {
    return (ld)sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int wa(int n) {
    int sum = 0;
    while (n > 0) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
    //自作関数 wa で各桁の和を求める。
}
ll sinsu(string s, ll k) { // K 進法表記の S を、10 進法表記で表す関数
    ll ans = 0;
    for (char x : s) {
        ans *= k;
        ans += x - '0';
    }
    return ans;
}
vector<ll> enumdiv(ll n) {
    vector<ll> S;
    for (int i = 1; 1LL * i * i <= n; i++) {
        if (n % i == 0) {
            S.push_back(i);
            if (i * i != n) {
                S.push_back(n / i);
            }
        }
    }
    sort(S.begin(), S.end());
    return S;
    //約数をlong long 型のvectorでsortして返す　
    //auto A = などで受け取って A.size() で約数の数を出す。
}
bool IsPrime(int num)
{
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false; // 偶数はあらかじめ除く
    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            // 素数ではない
            return false;
        }
    }
    // 素数である
    return true;
}
int MM(int k) {
    int sum = 1;
    for (int i = 1; i <= k; ++i)
    {
        sum *= i;
    }
    return sum;
}//階上計算
vector<pair<long long, long long> > prime_factorize(long long N) {
    vector<pair<long long, long long> > res;
    for (long long a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        long long ex = 0; // 指数

        // 割れる限り割り続ける
        while (N % a == 0) {
            ++ex;
            N /= a;
        }

        // その結果を push
        res.push_back({ a, ex });
    }

    // 最後に残った数について
    if (N != 1) res.push_back({ N, 1 });
    return res;
}
using Graph = vector<vector<int>>;
/*#define ALL(box)  (box).begin(),(box).end()
#define ALL(box2)  (box2).begin(),(box2).end()
#define ALL(boxa)  (boxa).begin(),(boxa).end()
#define ALL(boxb)  (boxb).begin(),(boxb).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)*/
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(box)  (box).begin(),(box).end()
//sort(box.begin(), box.end());
//sort(box.rbegin(), box.rend());
//vector<vector<int>> box( a , vector<int>( a ));
//printf("%.7Lf", n);
//reverse(t.begin(), t.end());
//auto It = upper_bound(ALL(box), n); 
//cout << box.end() - It ; 末尾までの距離
//cout << It - a.begin() ; 先頭までの距離
//auto It =box.upper_bound( k);set,multiset

ll sum = 0;
ll num = 0;
int pum = 0;
ll mum = 0;
int min1 = 2000000001;
int max1 = -2;
ll min2 = 9223372036854775807;
ll max2 = -10000000000000000;
ll mod1 = 1000000007;
ll MOD = 998244353;
//---------------------------------------------------------
int kyori(int a, int b, int c, int d) {
    int p = max({ b,c,d });
    return abs(p - a);
}
int main() {
   
    int a;
    cin >> a;
    vector<int> A(a), B(a), C(a), D(a);
    rep(i, a) {
        cin >> A[i];
    }
    rep(i, a) {
        cin >> B[i];
    }
    rep(i, a) {
        cin >> C[i];
    }
    rep(i, a) {
        cin >> D[i];
    }
    A.push_back(2000000000);
    B.push_back(2000000000);
    C.push_back(2000000000);
    D.push_back(2000000000);
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    sort(C.begin(), C.end());
    sort(D.begin(), D.end());

    //sort(ALL(B));
    rep(i, a) {
        int k = A[i];
        auto It = lower_bound(B.begin(),B.end(), k);
        auto It2 = lower_bound(C.begin(), C.end(), k);
        auto It3 = lower_bound(D.begin(), D.end(), k);
        min1 = min({ kyori(k,*It,*It2,*It3),min1 });
        //cout << min1 << " ";
    }
    rep(i, a) {
        int k = B[i];
        auto It = lower_bound(A.begin(), A.end(), k);
        auto It2 = lower_bound(C.begin(), C.end(), k);
        auto It3 = lower_bound(D.begin(), D.end(), k);
        min1 = min({ kyori(k,*It,*It2,*It3),min1 });
        //cout << min1 << " ";
    }
    rep(i, a) {
        int k = C[i];
        auto It = lower_bound(B.begin(), B.end(), k);
        auto It2 = lower_bound(A.begin(), A.end(), k);
        auto It3 = lower_bound(D.begin(), D.end(), k);
        min1 = min({ kyori(k,*It,*It2,*It3),min1 });
        //cout << min1 << " ";
    }
    rep(i, a) {
        int k = D[i];
        auto It = lower_bound(B.begin(), B.end(), k);
        auto It2 = lower_bound(C.begin(), C.end(), k);
        auto It3 = lower_bound(A.begin(), A.end(), k);
        min1 = min({ kyori(k,*It,*It2,*It3),min1 });
        //cout << min1 << " ";
    }
    cout << min1 << endl;
}
accuracy: 1
time_ms: 100
memory_kb: 4656
final_rank: 14

code_snippet: #include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<tuple>
#include<math.h>
#include<numeric>
#include<set>
#include <climits>
#include<map>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;
typedef long double ld;
//int a, b, c;
void swap(int* p, int* q) {
    int t = *p;
    *p = *q;
    *q = t;
}
ld you(ld x1, ld y1, ld x2, ld y2) {
    return (ld)sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int wa(int n) {
    int sum = 0;
    while (n > 0) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
    //自作関数 wa で各桁の和を求める。
}
ll sinsu(string s, ll k) { // K 進法表記の S を、10 進法表記で表す関数
    ll ans = 0;
    for (char x : s) {
        ans *= k;
        ans += x - '0';
    }
    return ans;
}
vector<ll> enumdiv(ll n) {
    vector<ll> S;
    for (int i = 1; 1LL * i * i <= n; i++) {
        if (n % i == 0) {
            S.push_back(i);
            if (i * i != n) {
                S.push_back(n / i);
            }
        }
    }
    sort(S.begin(), S.end());
    return S;
    //約数をlong long 型のvectorでsortして返す　
    //auto A = などで受け取って A.size() で約数の数を出す。
}
bool IsPrime(int num)
{
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false; // 偶数はあらかじめ除く
    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            // 素数ではない
            return false;
        }
    }
    // 素数である
    return true;
}
int MM(int k) {
    int sum = 1;
    for (int i = 1; i <= k; ++i)
    {
        sum *= i;
    }
    return sum;
}//階上計算
vector<pair<long long, long long> > prime_factorize(long long N) {
    vector<pair<long long, long long> > res;
    for (long long a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        long long ex = 0; // 指数

        // 割れる限り割り続ける
        while (N % a == 0) {
            ++ex;
            N /= a;
        }

        // その結果を push
        res.push_back({ a, ex });
    }

    // 最後に残った数について
    if (N != 1) res.push_back({ N, 1 });
    return res;
}
using Graph = vector<vector<int>>;
/*#define ALL(box)  (box).begin(),(box).end()
#define ALL(box2)  (box2).begin(),(box2).end()
#define ALL(boxa)  (boxa).begin(),(boxa).end()
#define ALL(boxb)  (boxb).begin(),(boxb).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)*/
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
//sort(box.begin(), box.end());
//sort(box.rbegin(), box.rend());
//vector<vector<int>> box( a , vector<int>( a ));
//printf("%.7Lf", n);
//reverse(t.begin(), t.end());
//auto It = upper_bound(ALL(box), n); 
//cout << box.end() - It ; 末尾までの距離
//cout << It - a.begin() ; 先頭までの距離
//auto It =box.upper_bound( k);set,multiset

ll sum = 0;
ll num = 0;
int pum = 0;
ll mum = 0;
int min1 = 2000000001;
int max1 = -2;
ll min2 = 9223372036854775807;
ll max2 = -10000000000000000;
ll mod1 = 1000000007;
ll MOD = 998244353;
//---------------------------------------------------------
int kyori(int a, int b, int c, int d) {
    int p = max({ b,c,d });
    return abs(p - a);
}
int main() {
    int a;
    cin >> a;
    vector<int> A(a), B(a), C(a), D(a);
    rep(i, a) {
        cin >> A[i];
    }
    rep(i, a) {
        cin >> B[i];
    }
    rep(i, a) {
        cin >> C[i];
    }
    rep(i, a) {
        cin >> D[i];
    }
    A.push_back(2000000000);
    B.push_back(2000000000);
    C.push_back(2000000000);
    D.push_back(2000000000);
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    sort(C.begin(), C.end());
    sort(D.begin(), D.end());
    rep(i, a) {
        int k = A[i];
        auto It = lower_bound(B.begin(),B.end(), k);
        auto It2 = lower_bound(C.begin(), C.end(), k);
        auto It3 = lower_bound(D.begin(), D.end(), k);
        min1 = min({ kyori(k,*It,*It2,*It3),min1 });
        //cout << min1 << " ";
    }
    rep(i, a) {
        int k = B[i];
        auto It = lower_bound(A.begin(), A.end(), k);
        auto It2 = lower_bound(C.begin(), C.end(), k);
        auto It3 = lower_bound(D.begin(), D.end(), k);
        min1 = min({ kyori(k,*It,*It2,*It3),min1 });
        //cout << min1 << " ";
    }
    rep(i, a) {
        int k = C[i];
        auto It = lower_bound(B.begin(), B.end(), k);
        auto It2 = lower_bound(A.begin(), A.end(), k);
        auto It3 = lower_bound(D.begin(), D.end(), k);
        min1 = min({ kyori(k,*It,*It2,*It3),min1 });
        //cout << min1 << " ";
    }
    rep(i, a) {
        int k = D[i];
        auto It = lower_bound(B.begin(), B.end(), k);
        auto It2 = lower_bound(C.begin(), C.end(), k);
        auto It3 = lower_bound(A.begin(), A.end(), k);
        min1 = min({ kyori(k,*It,*It2,*It3),min1 });
        //cout << min1 << " ";
    }
    cout << min1 << endl;
}
accuracy: 1
time_ms: 100
memory_kb: 4680
final_rank: 15

code_snippet: #define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int n;
  cin>>n;
  vector<vector<int>> G(4,vector<int>(n));
  rep(i,4) rep(j,n) cin>>G[i][j];
  rep(i,4) sort(ALL(G[i]));
  
  int mi=1e9+7;
  rep(i,4){
    rep(j,n){
      int ma=0;
      rep(k,4){
        if(k==i) continue;
        auto t=lower_bound(ALL(G[k]),G[i][j])-G[k].begin();
        if(t==n) ma=1e9+7;
        ma=max(ma,G[k][t]-G[i][j]);
      }
      mi=min(mi,ma);
    }
  }
  cout<<mi<<endl;
  
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 4660
final_rank: 6