#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <sstream>
#include <set>
#include <iomanip>
#include <list>
#include <stack>
#include <queue>
#include <bitset>
#include <numeric>
#include <functional>

#include <cstdio>
#include <cmath>
#include <climits>
#include <cstring>
#include <cctype>
#include <cstdlib>

using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector <int> vi;
typedef pair< int ,int > pii;
#define rep(i, n)       for(int i = 0; i < n; i++)
#define reps(i, a, n)   for(int i = a; i < n; i++)
#define precout(a,b)    cout << fixed << setprecision((b)) << (a)
#define endl 			'\n'
#define pb              push_back
#define mp              make_pair
#define ff              first
#define ss              second
#define all(a)          a.begin(), a.end()
#define rall(a)			a.rbegin(), a.rend()
#define sz(x)			(int)x.size()
#define fastIO   		ios::sync_with_stdio(false); cin.tie(0);

const int maxx = 1e5 + 9;
vi graph[maxx];

int main(){
#ifndef ONLINE_JUDGE
    const clock_t begin_time = clock();
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif

    fastIO

    int n, d, h;
    cin >> n >> d >> h;
    if (d >  2 * h || d > n || (d == 1 && n < 2)) {
        cout << -1 << endl;
        return 0;
    }

    int id = 0;
    int l = 1, r = 1;
    int ih = 0;
    map<int, bool> vis;
    vis[1] = true;
    for (int i = 2; i <= n && id <= d;) {
        bool check = false;
        if (id + 1 <= d) {
            graph[l].pb(i);
            vis[i] = true;
            id++;
            l = i;

            i++;
            check = true;
        }
        if (id + 1 <= d) {
            graph[r].pb(i);
            vis[i] = true;
            id++;
            r = i;
            i++;
             cout << r << endl;

            check = true;
        }
        ih++;
        if (not check) break;
    }

    int c = -1;
    for(int i = 1; i <= n; i++){
        if (!vis[i]) {
            c = i;
            break;
        }
    }
    bool doo = false;
    if ( c != -1) doo = true;
    int pre = c - 1;
    int nxt = c;
    //cout << pre << " " << nxt << endl;
    while (ih <= h && doo) {
        graph[pre].pb(nxt);
        pre = nxt;
        nxt++;
        ih++;
    }

    // cout << pre << " " << nxt << endl;

    while (nxt <= n && doo) {
        graph[1].pb(nxt);
        nxt++;
    }

    for (int i = 1; i <= n; i++) {
        for (auto x : graph[i])
            cout << i << " " << x << endl;
    }
#ifndef ONLINE_JUDGE
   	cout << endl;
    cout << "Time : ";
    cout << float( clock () - begin_time ) / CLOCKS_PER_SEC << endl;
#endif

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <sstream>
#include <set>
#include <iomanip>
#include <list>
#include <stack>
#include <queue>
#include <bitset>
#include <numeric>
#include <functional>

#include <cstdio>
#include <cmath>
#include <climits>
#include <cstring>
#include <cctype>
#include <cstdlib>

using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector <int> vi;
typedef pair< int ,int > pii;
#define rep(i, n)       for(int i = 0; i < n; i++)
#define reps(i, a, n)   for(int i = a; i < n; i++)
#define precout(a,b)    cout << fixed << setprecision((b)) << (a)
#define endl 			'\n'
#define pb              push_back
#define mp              make_pair
#define ff              first
#define ss              second
#define all(a)          a.begin(), a.end()
#define rall(a)			a.rbegin(), a.rend()
#define sz(x)			(int)x.size()
#define fastIO   		ios::sync_with_stdio(false); cin.tie(0);

const int maxx = 1e5 + 9;
vi graph[maxx];

int main(){
#ifndef ONLINE_JUDGE
    const clock_t begin_time = clock();
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif

    fastIO

    int n, d, h;
    cin >> n >> d >> h;
    if (d > min(n, 2 * h)) {
        cout << -1 << endl;
        return 0;
    }

    int id = 0;
    int l = 1, r = 1;
    int ih = 0;
    map<int, bool> vis;
    vis[1] = true;
    for (int i = 2; i <= n && id <= d;) {
        bool check = false;
        if (id + 1 <= d) {
            graph[l].pb(i);
            vis[i] = true;
            id++;
            l = i;

          //  cout << l << endl;
            i++;
            check = true;
        }
        if (id + 1 <= d) {
            graph[r].pb(i);
            vis[i] = true;
            id++;
            r = i;
            i++;
           // cout << r << endl;

            check = true;
        }
        ih++;
        if (not check) break;
    }
    int c = 1;
    for(int i = 1; i <= n; i++){
        if (!vis[i]) {
            c = i;
            break;
        }
    }
    int pre = c - 1;
    int nxt = c;
   // cout << pre << " " << nxt << endl;
    while (ih <= h) {
        graph[pre].pb(nxt);
        pre = nxt;
        nxt++;
        ih++;
    }

   // cout << pre << " " << nxt << endl;

    while (nxt <= n) {
        graph[1].pb(nxt);
        nxt++;
    }

    for (int i = 1; i <= n; i++) {
        for (auto x : graph[i])
            cout << i << " " << x << endl;
    }
#ifndef ONLINE_JUDGE
   	cout << endl;
    cout << "Time : ";
    cout << float( clock () - begin_time ) / CLOCKS_PER_SEC << endl;
#endif

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <sstream>
#include <set>
#include <iomanip>
#include <list>
#include <stack>
#include <queue>
#include <bitset>
#include <numeric>
#include <functional>

#include <cstdio>
#include <cmath>
#include <climits>
#include <cstring>
#include <cctype>
#include <cstdlib>

using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector <int> vi;
typedef pair< int ,int > pii;
#define rep(i, n)       for(int i = 0; i < n; i++)
#define reps(i, a, n)   for(int i = a; i < n; i++)
#define precout(a,b)    cout << fixed << setprecision((b)) << (a)
#define endl 			'\n'
#define pb              push_back
#define mp              make_pair
#define ff              first
#define ss              second
#define all(a)          a.begin(), a.end()
#define rall(a)			a.rbegin(), a.rend()
#define sz(x)			(int)x.size()
#define fastIO   		ios::sync_with_stdio(false); cin.tie(0);

const int maxx = 1e5 + 9;
vi graph[maxx];

int main(){
#ifndef ONLINE_JUDGE
    const clock_t begin_time = clock();
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif

    fastIO

    int n, d, h;
    cin >> n >> d >> h;
    if (n == 2) {
        if ( d > 1 && h > 1) cout << -1 << endl;
        else cout << 1 << " " << 2 << endl;
    } else if (d >  2 * h || d > n || (d == 1 && n >= 3))
        cout << -1 << endl;
    else {

        if (d == h) {
            int i;
            for (i = 1; i <= h; i++)
                cout << i << " " << i + 1 << endl;

            while (i < n) {
                cout << 2 << " " << i + 1 << endl;
                i++;
            }
        } else {
            int i;
            for (i = 1; i <= h; i++) {
                cout << i << " " << i + 1 << endl;
            }

            int ih = d - h;
            int c;
            for (c = i + 1; c < ih; c++)
                cout << 1 << " " << c << endl;
            while (c < n) {
                cout << 1 << " " << c + 1 << endl;
                c++;
            }
        }

    }

#ifndef ONLINE_JUDGE
   	cout << endl;
    cout << "Time : ";
    cout << float( clock () - begin_time ) / CLOCKS_PER_SEC << endl;
#endif

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <sstream>
#include <set>
#include <iomanip>
#include <list>
#include <stack>
#include <queue>
#include <bitset>
#include <numeric>
#include <functional>

#include <cstdio>
#include <cmath>
#include <climits>
#include <cstring>
#include <cctype>
#include <cstdlib>

using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector <int> vi;
typedef pair< int ,int > pii;
#define rep(i, n)       for(int i = 0; i < n; i++)
#define reps(i, a, n)   for(int i = a; i < n; i++)
#define precout(a,b)    cout << fixed << setprecision((b)) << (a)
#define endl 			'\n'
#define pb              push_back
#define mp              make_pair
#define ff              first
#define ss              second
#define all(a)          a.begin(), a.end()
#define rall(a)			a.rbegin(), a.rend()
#define sz(x)			(int)x.size()
#define fastIO   		ios::sync_with_stdio(false); cin.tie(0);

const int maxx = 1e5 + 9;
vi graph[maxx];

int main(){
#ifndef ONLINE_JUDGE
    const clock_t begin_time = clock();
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif

    fastIO

    int n, d, h;
    cin >> n >> d >> h;

    if (d >  2 * h || d > n || (d == 1 && n < 2)) cout << -1 << endl;
    else {

        int i = 1;
        int th = h;
        int c = 1;
        while (th--) {
            cout << c << " " << i + 1 << endl;
            c = i;
            i++;

        }
        int td = d - h;
        if ( td > 0) {
            cout << i << " " << i + 1 << endl;
            i++;
        }
        while(i < n) {
            cout << 2 << " " << i + 1 << endl;
            i++;
        }

    }

#ifndef ONLINE_JUDGE
   	cout << endl;
    cout << "Time : ";
    cout << float( clock () - begin_time ) / CLOCKS_PER_SEC << endl;
#endif

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <sstream>
#include <set>
#include <iomanip>
#include <list>
#include <stack>
#include <queue>
#include <bitset>
#include <numeric>
#include <functional>

#include <cstdio>
#include <cmath>
#include <climits>
#include <cstring>
#include <cctype>
#include <cstdlib>

using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector <int> vi;
typedef pair< int ,int > pii;
#define rep(i, n)       for(int i = 0; i < n; i++)
#define reps(i, a, n)   for(int i = a; i < n; i++)
#define precout(a,b)    cout << fixed << setprecision((b)) << (a)
#define endl 			'\n'
#define pb              push_back
#define mp              make_pair
#define ff              first
#define ss              second
#define all(a)          a.begin(), a.end()
#define rall(a)			a.rbegin(), a.rend()
#define sz(x)			(int)x.size()
#define fastIO   		ios::sync_with_stdio(false); cin.tie(0);

int main(){
#ifndef ONLINE_JUDGE
    const clock_t begin_time = clock();
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif

   // fastIO

    int n, d, h;
    cin >> n >> d >> h;
    if (n == 2) {
        if ( d > 1 && h > 1) cout << -1 << endl;
        else cout << 1 << " " << 2 << endl;
    } else if (d >  2 * h || d > n || (d == 1 && n >= 3))
        cout << -1 << endl;
    else {

        if (d == h) {
            int i;
            for (i = 1; i <= h; i++)
                cout << i << " " << i + 1 << endl;

            while (i < n) {
                cout << 2 << " " << i + 1 << endl;
                i++;
            }
        } else {
            int i;
            for (i = 1; i <= h; i++) {
                cout << i << " " << i + 1 << endl;
            }

            int ih = d - h;
            int c;
            for (c = i + 1; c < ih; c++)
                cout << 1 << " " << c << endl;
            while (c < n) {
                cout << 1 << " " << c + 1 << endl;
                c++;
            }
        }

    }

#ifndef ONLINE_JUDGE
   	cout << endl;
    cout << "Time : ";
    cout << float( clock () - begin_time ) / CLOCKS_PER_SEC << endl;
#endif

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <sstream>
#include <set>
#include <iomanip>
#include <list>
#include <stack>
#include <queue>
#include <bitset>
#include <numeric>
#include <functional>

#include <cstdio>
#include <cmath>
#include <climits>
#include <cstring>
#include <cctype>
#include <cstdlib>

using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector <int> vi;
typedef pair< int ,int > pii;
#define rep(i, n)       for(int i = 0; i < n; i++)
#define reps(i, a, n)   for(int i = a; i < n; i++)
#define precout(a,b)    cout << fixed << setprecision((b)) << (a)
#define endl 			'\n'
#define pb              push_back
#define mp              make_pair
#define ff              first
#define ss              second
#define all(a)          a.begin(), a.end()
#define rall(a)			a.rbegin(), a.rend()
#define sz(x)			(int)x.size()
#define fastIO   		ios::sync_with_stdio(false); cin.tie(0);

const int maxx = 1e5 + 9;
vi graph[maxx];

int main(){
#ifndef ONLINE_JUDGE
    const clock_t begin_time = clock();
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif

    fastIO

    int n, d, h;
    cin >> n >> d >> h;
    if (d >  2 * h || d > n || (d == 1 && n < 2)) {
        cout << -1 << endl;
        return 0;
    }

    int id = 0;
    int l = 1, r = 1;
    int ih = 0;
    map<int, bool> vis;
    vis[1] = true;
    for (int i = 2; i <= n && id <= d;) {
        bool check = false;
        if (id + 1 <= d) {
            graph[l].pb(i);
            vis[i] = true;
            id++;
            l = i;

            i++;
            check = true;
        }
        if (id + 1 <= d) {
            graph[r].pb(i);
            vis[i] = true;
            id++;
            r = i;
            i++;
             //cout << r << endl;

            check = true;
        }
        ih++;
        if (not check) break;
    }

    int c = -1;
    for(int i = 1; i <= n; i++){
        if (!vis[i]) {
            c = i;
            break;
        }
    }
    bool doo = false;
    if ( c != -1) doo = true;
    int pre = c - 1;
    int nxt = c;
    //cout << pre << " " << nxt << endl;
    while (ih <= h && doo) {
        graph[pre].pb(nxt);
        pre = nxt;
        nxt++;
        ih++;
    }

    // cout << pre << " " << nxt << endl;

    while (nxt <= n && doo) {
        graph[1].pb(nxt);
        nxt++;
    }

    for (int i = 1; i <= n; i++) {
        for (auto x : graph[i])
            cout << i << " " << x << endl;
    }
#ifndef ONLINE_JUDGE
   	cout << endl;
    cout << "Time : ";
    cout << float( clock () - begin_time ) / CLOCKS_PER_SEC << endl;
#endif

    return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>

using namespace std;

#define int long long
#define cerr if (false) cerr

const int INF = 1e18 + 239;

namespace Podd {
	int k;
	int cs = 0;
	int add_sum = 0;
	multiset<int> in;

	void init(int k_) {
		k = k_;
		cs = 0;
		add_sum = 0;
		in.clear();
	}

	void add(int x) {
		add_sum += x;
	}

	void ers(int x) {
		cs -= (x);
		assert(in.count(x));
		in.erase(in.find(x));
	}

	void ins(int x) {
		cs += (x - add_sum);
		x -= add_sum;
		in.insert(x);
		while ((int)in.size() > k) {
			ers(*in.rbegin());
		}
	}

	int get() {
		if ((int)in.size() < k) {
			return INF;
		} else {
			return cs + add_sum * k;
		}
	}
}

int solve(int n, int k, vector<int> a, vector<int> p, int cost_5) {
	int can = INF;
	Podd::init(k);
	int pr = 0;
	for (int i = 0; i < n; i++) {
		Podd::add((a[i] - pr) / 5 * cost_5);
		Podd::ins(p[i]);
		int cand = Podd::get();
		can = min(can, cand);
		pr = a[i];
	}
	return can;
 }

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int n, k, b, c;
	cin >> n >> k >> b >> c;
	vector<int> a(n);
	for (auto &t : a) {
		cin >> t;
	}
	sort(a.begin(), a.end());
	int cost_1 = c;
	int cost_5 = min(b, 5 * c);
	int ans = INF;
	for (int i = 0; i < 5; i++) {
		vector<int> cand = a;
		vector<int> ps;
		for (auto &t : cand) {
			ps.push_back(0);
			while (((t % 5) + 5) % 5 != i) {
				t++;
				ps.back() += cost_1;
			}
		}
		int add = solve(n, k, cand, ps, cost_5);
		cerr << "add " << add << endl;
		ans = min(ans, add);
	}
	cout << ans << endl;
	/*Podd::init(2);
	Podd::ins(2);
	Podd::ins(2);
	cout << Podd::get() << endl;
	Podd::add(2);
	cout << Podd::get() << endl;
	Podd::ins(3);
	Podd::ins(3);
	cout << Podd::get() << endl;*/
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>

using namespace std;

#define int long long
#define cerr if (false) cerr

const int INF = 1e18 + 239;

namespace Podd {
	int k;
	vector<int> a;

	void init(int k_) {
		a.clear();
		k = k_;
	}

	int get() {
		if ((int)a.size() < k) {
			return INF;
		} else {
			int ans = 0;
			for (int i = 0; i < k; i++) {
				ans += a[i];
			}
			return ans;
		}
	}

	void add(int x) {
		for (auto &t : a) {
			t += x;
		}
	}

	void ins(int x) {
		a.push_back(x);
		sort(a.begin(), a.end());
	}
}

int solve(int n, int k, vector<int> a, vector<int> p, int cost_5) {
	int can = INF;
	Podd::init(k);
	int pr = 0;
	for (int i = 0; i < n; i++) {
		Podd::add((a[i] - pr) / 5 * cost_5);
		Podd::ins(p[i]);
		int cand = Podd::get();
		can = min(can, cand);
		pr = a[i];
	}
	return can;
 }

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int n, k, b, c;
	cin >> n >> k >> b >> c;
	vector<int> a(n);
	for (auto &t : a) {
		cin >> t;
	}
	sort(a.begin(), a.end());
	int cost_1 = c;
	int cost_5 = min(b, 5 * c);
	int ans = INF;
	for (int i = 0; i < 5; i++) {
		vector<int> cand = a;
		vector<int> ps;
		for (auto &t : cand) {
			ps.push_back(0);
			while (((t % 5) + 5) % 5 != i) {
				t++;
				ps.back() += cost_1;
			}
		}
		int add = solve(n, k, cand, ps, cost_5);
		cerr << "add " << add << endl;
		ans = min(ans, add);
	}
	cout << ans << endl;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>

using namespace std;

#define int long long
#define cerr if (false) cerr

const int INF = 1e18 + 239;

int solve(int n, int k, vector<int> a, vector<int> padd, int cost_5) {
	vector<int> p(n);
	p[0] = a[0];
	for (int i = 1; i < n; i++) {
		p[i] = p[i - 1] + a[i];
	}
	auto get_sum = [&](int l, int r) {
		int f = p[r];
		int s = 0;
		if (l > 0) s = p[l - 1];
		return f - s;
	};
	auto get_sum_1 = [&](int l, int r) {
		int f = padd[r];
		int s = 0;
		if (l > 0) s = padd[l - 1];
		return f - s;
	};
	int can = INF;
	for (int i = 0; i + k <= n; i++) {
		int l = i;
		int r = i + k - 1;
		auto s = get_sum(l, r);
		int add = a[r] * k - s;
		cerr << "in start " << i + 1 << " " << add << " padd " << get_sum_1(l, r) << endl;
		can = min(can, (add / 5) * cost_5 + get_sum_1(l, r));
 	}
 	return can;
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int n, k, b, c;
	cin >> n >> k >> b >> c;
	vector<int> a(n);
	for (auto &t : a) {
		cin >> t;
	}
	sort(a.begin(), a.end());
	int cost_1 = c;
	int cost_5 = min(b, 5 * c);
	int ans = INF;
	for (int i = 0; i < 5; i++) {
		vector<int> cand = a;
		vector<int> ps;
		for (auto &t : cand) {
			ps.push_back(0);
			while (((t % 5) + 5) % 5 != i) {
				t++;
				ps.back() += cost_1;
			}
		}
		vector<int> padd(n);
		padd[0] = ps[0];
		for (int i = 1; i < n; i++) {
			padd[i] = padd[i - 1] + ps[i];
		}
		cerr << i << endl;
		for (auto t : cand) {
			cerr << t << ' ';
		}
		cerr << endl;
		for (auto t : padd) {
			cerr << t << ' ';
		}
		cerr << endl;
		int add = solve(n, k, cand, padd, cost_5);
		cerr << "add " << add << endl;
		ans = min(ans, add);
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define oo 2000000000
#define mod 998244353
const int N = 200010;
int n , k , b , c , arr[N];

pair<long long , int> seg[4 * N];
long long lazy[4 * N];

long long val;
int l , r;

inline void fix(int s,int e,int idx){
    seg[idx].first += lazy[idx];
    if(s != e){
        lazy[(idx << 1)] += lazy[idx];
        lazy[(idx << 1) + 1] += lazy[idx];
    }
    lazy[idx] = 0;
}

void build(int s,int e,int idx){
    seg[idx] = make_pair(0 , s);
    lazy[idx] = 0;
    if(s == e) return;
    build(s,((s+e) >> 1),(idx << 1));
    build(((s+e) >> 1) + 1,e,(idx << 1) + 1);
}

pair<long long , int> update(int s,int e,int idx){
    fix(s,e,idx);
    if(s > r || e < l)
        return seg[idx];
    if(s >= l && e <= r){
        lazy[idx] += val;
        fix(s,e,idx);
        return seg[idx];
    }
    return seg[idx] = max(update(s,((s+e) >> 1),(idx << 1)) , update(((s+e) >> 1) + 1,e,(idx << 1) + 1));
}

pair< long long , int > getmax(int s,int e,int idx){
    if(s > r || e < l)
        return make_pair((long long)-1e18 , n);
    if(s >= l && e <= r)
        return seg[idx];
    return max(getmax(s,((s+e) >> 1),(idx << 1)) , getmax(((s+e) >> 1) + 1,e,(idx << 1) + 1));
}

int main(){
    scanf("%d%d%d%d",&n,&k,&b,&c);
    for(int i = 0 ;i < n;i++){
        scanf("%d",&arr[i]);
        arr[i] += (int)1e9;
    }
    sort(arr,arr+n);
    if(c * 5 < b) b = c * 5;
    long long ans = (long long)1e18;
    long long sum = 0;
    for(int m = 0 ;m < 5;m++){
        build(0 , n - 1, 1);
        sum = 0;
        int togo = arr[k - 1] , pre;
        pair< long long , int > cur;
        while(togo % 5 != m) togo++;
        for(int i = 0 ;i < k;i++){
            l = r = i;
            val = (long long)b * ((togo - arr[i]) / 5);
            val += (long long)c * ((togo - arr[i]) % 5);
            sum += val;
            update(0 ,n-1,1);
        }
        ans = min(ans , sum);
        for(int i = k;i<n;i++){
            pre = togo;
            togo = arr[i];
            while(togo % 5 != m) togo++;
            sum += (long long)b * (long long)(togo - pre) * (long long)k / 5;
            l = 0 , r = i - 1 , val = (long long)(togo - pre) * b / 5;
            update(0 , n - 1 , 1);
            l = r = i;
            val = (long long)b * ((togo - arr[i]) / 5);
            val += (long long)c * ((togo - arr[i]) % 5);
            sum += val;
            update(0,n-1,1);
            l = 0 , r = i;
            cur = getmax(0,n-1,1);
            sum -= cur.first;
            l = r = cur.second;
            val = (long long)-1e18;
            update(0,n-1,1);
            ans = min(ans , sum);
        }
    }
    cout << ans << endl;
    return 0;
}


#include<algorithm>
#include<set>
#include<iostream>

typedef long long ll;
using namespace std;

int main()
{
    ll n,k;
    ll b,c;
    ll t[200005],nt[200005],cost[200005],r[200005];

    cin >> n >> k >> b >> c;
    b = min(b, 5*c);

    for(int i=0;i<n;i++)
    {
        cin >> t[i];
    }

    sort(t, t+n);

    for(int i=0; i<n; i++)
    {
        r[i] = (t[i] % 5 + 5) % 5;
    }

    ll ans=9e18;

    for(int rot=0; rot<5; rot++)
    {
        for(int i=0; i<n; i++)
        {
            ll shift = (rot-r[i]+5) % 5;
            cost[i] = c*shift;
            nt[i] = t[i] + shift;
        }

        ll add=0,total=0;
        multiset<ll>s;

        for(int i=0; i<n; i++)
        {
            if(i>0 && nt[i] > nt[i-1])
            {
                ll value = ((nt[i]-nt[i-1])/5)*1LL*b;
                add += value;
                total += value*(ll)s.size();
            }

            s.insert(cost[i]-add);
            total += cost[i];

            while((int)s.size()>k)
            {
                ll z =*(--s.end());
                total-=(z+add);
                s.erase(--s.end());
            }
            if(s.size()==k)
            {
                ans = min(ans, total);
            }
        }
    }
    cout << ans << endl;
}


#pragma comment(linker, "/STACK:134217728")
#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
#include <functional>
#include <cmath>
#include <time.h>

using namespace std;

typedef long long LL;

int n, k, b, c;

int t[200022];
pair<int, int> a[5][200022];

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

	b = min(b, 5 * c);

	for (int i = 0; i < n; ++i)
		scanf("%d", &t[i]);

	for (int i = 0; i < n; ++i)
	{
		int x = t[i];
		int cost = 0;
		for (int j = 0; j < 5; ++j)
		{
			int r = x % 5;
			if (r < 0)
				r += 5;
			a[r][i] = make_pair(x, cost);
			x++;
			cost++;
		}
	}

	LL res = 1LL << 62;

	for (int r = 0; r < 5; ++r)
	{
		sort(a[r], a[r] + n);

		set<pair<LL, int> > S;

		LL sum = 0;

		for (int i = 0; i < k; ++i)
		{
			pair<LL, int> add = make_pair((LL)b * (a[r][k - 1].first - a[r][i].first) / 5 + (LL)c * a[r][i].second, (LL)i);
			sum += add.first;
			S.insert(add);
		}

		res = min(res, sum);

		LL d = 0;

		for (int i = k; i < n; ++i)
		{
			sum -= S.rbegin()->first + d;
			d += (LL)b * (a[r][i].first - a[r][i - 1].first) / 5;
			S.erase(*S.rbegin());
			pair<LL, int> add = make_pair(-d + (LL)c * a[r][i].second, i);
			S.insert(add);
			sum += (LL)b * (k - 1) * (a[r][i].first - a[r][i - 1].first) / 5;
			sum += (LL)c * a[r][i].second;
			res = min(res, sum);
		}
	}

	printf("%I64d\n", res);

	return 0;
}


#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <limits>
#include <sstream>
#include <vector>
#include <map>
#include<set>
#include <string>
#include<cstdlib>
#include <queue>

using namespace std;

#define int long long
#define fi(i, a, b) for(int i = (a); (i) < (b); (i)++)

const int N = 200004;
int a[N];
int e[N];

int indices[N];

pair<int,int> x[N];
vector<int> level[5];

int t[N * 5];
void build(int a[], int v, int tl, int tr) {
	if (tl == tr)
		t[v] = a[tl];
	else {
		int tm = (tl + tr) / 2;
		build(a, v * 2, tl, tm);
		build(a, v * 2 + 1, tm + 1, tr);
		t[v] = t[v * 2] + t[v * 2 + 1];
	}
}

int sum(int v, int tl, int tr, int l, int r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return t[v];
	int tm = (tl + tr) / 2;
	return sum(v * 2, tl, tm, l, min(r, tm))
		+ sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
}

void update(int v, int tl, int tr, int pos, int new_val) {
	if (tl == tr)
		t[v] = new_val;
	else {
		int tm = (tl + tr) / 2;
		if (pos <= tm)
			update(v * 2, tl, tm, pos, new_val);
		else
			update(v * 2 + 1, tm + 1, tr, pos, new_val);
		t[v] = t[v * 2] + t[v * 2 + 1];
	}
}

bool comp(pair<int,int>& a, pair<int,int>& b){ return b.first < a.first; }
void main()
{
#if defined(DEBUG) || defined(_DEBUG)
  ifstream cin("input.txt");
  ofstream cout("output.txt");
#endif
  int n,k,b,c;
  cin >>n>>k>>b>>c;
  //read
  fi(i, 0, n){ cin >> a[i]; }

  sort(a, a + n);

  //5 vector of levels(1mln)
  fi(i, 0, n){
	  fi(j, 0, 5){
		  level[(a[i] + j + 1000000000000ll) % 5ll].push_back(a[i] + j);
	  }
  }

  int ans = 1000000000000000000ll;

  //for i = 0: 4
  fi(lll, 0, 5){
	  vector<int>& l = level[lll];
	  //find max with it (from last level)
	  int m = *l.rbegin();
	  //calculate all prices.for each sorted fill indice in prices; create segment tree

	  fi(i, 0, n){
		  if (c * 5 <= b){
			  x[i] = make_pair((m - a[i])  * c , i);
		  }
		  else{
			  x[i] = make_pair((m - a[i]) / 5 * b + (m - a[i]) % 5 * c, i);
		  }

	  }
	  sort(x, x + n, comp);
	  fi(i, 0, n){
		  indices[x[i].second] = i;
		  e[i] = x[i].first;
	  }

	  build(e, 1, 0, n - 1);

	  //calc ans for level(with sum).
	  int curFirst = n-k;
	  int countInMas = n ;
	  for (int i = l.size() - 1; i >= 0; i--)
	  {

		  int s = sum(1, 0, n - 1, curFirst , n - 1);
		  if (c * 5 <= b){
			  s -= (m - l[i]) *c*k;

		  }
		  else{
			  s -= (m - l[i]) / 5 * b*k;

		  }
		  ans = min(ans, s);

		  //set all larger in tree to zero
		  if (i >= 1){
			  while (a[countInMas - 1] > l[i - 1]){
				  int index = indices[countInMas - 1];
				  update(1, 0, n - 1, index, 0);
				  if (index >= curFirst)
					  curFirst--;
				  countInMas--;
			  }
		  }

		  if (countInMas < k)
			  break;
	  }
  }

  cout << ans;
}


#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <limits>
#include <sstream>
#include <vector>
#include <map>
#include<set>
#include <string>
#include<cstdlib>
#include <queue>

using namespace std;

#define int long long
#define fi(i, a, b) for(int i = (a); (i) < (b); (i)++)

const int N = 200004;
int a[N];
int e[N];

int indices[N];

pair<int,int> x[N];
vector<int> level[5];

int t[N * 5];
void build(int a[], int v, int tl, int tr) {
	if (tl == tr)
		t[v] = a[tl];
	else {
		int tm = (tl + tr) / 2;
		build(a, v * 2, tl, tm);
		build(a, v * 2 + 1, tm + 1, tr);
		t[v] = t[v * 2] + t[v * 2 + 1];
	}
}

int sum(int v, int tl, int tr, int l, int r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return t[v];
	int tm = (tl + tr) / 2;
	return sum(v * 2, tl, tm, l, min(r, tm))
		+ sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
}

void update(int v, int tl, int tr, int pos, int new_val) {
	if (tl == tr)
		t[v] = new_val;
	else {
		int tm = (tl + tr) / 2;
		if (pos <= tm)
			update(v * 2, tl, tm, pos, new_val);
		else
			update(v * 2 + 1, tm + 1, tr, pos, new_val);
		t[v] = t[v * 2] + t[v * 2 + 1];
	}
}

bool comp(pair<int,int>& a, pair<int,int>& b){ return b.first < a.first; }
bool removed[N];
void main()
{
#if defined(DEBUG) || defined(_DEBUG)
  ifstream cin("input.txt");
  ofstream cout("output.txt");
#endif
  int n,k,b,c;
  cin >>n>>k>>b>>c;
  //read
  fi(i, 0, n){ cin >> a[i]; }

  sort(a, a + n);

  //5 vector of levels(1mln)
  fi(i, 0, n){
	  fi(j, 0, 5){
		  level[(a[i] + j + 1000000000000ll) % 5ll].push_back(a[i] + j);
	  }
  }

  int ans = 1000000000000000000ll;

  //for i = 0: 4
  fi(lll, 0, 5){
	  vector<int>& l = level[lll];
	  //find max with it (from last level)
	  int m = *l.rbegin();
	  //calculate all prices.for each sorted fill indice in prices; create segment tree

	  fi(i, 0, n){
		  removed[i] = false;
		  if (c * 5 <= b){
			  x[i] = make_pair((m - a[i])  * c , i);
		  }
		  else{
			  x[i] = make_pair((m - a[i]) / 5 * b + (m - a[i]) % 5 * c, i);
		  }

	  }
	  sort(x, x + n, comp);
	  fi(i, 0, n){
		  indices[x[i].second] = i;
		  e[i] = x[i].first;
	  }

	  build(e, 1, 0, n - 1);

	  //calc ans for level(with sum).
	  int curFirst = n-k;
	  int countInMas = n ;
	  for (int i = l.size() - 1; i >= 0; i--)
	  {

		  int s = sum(1, 0, n - 1, curFirst , n - 1);
		  if (c * 5 <= b){
			  s -= (m - l[i]) *c*k;

		  }
		  else{
			  s -= (m - l[i]) / 5 * b*k;

		  }
		  ans = min(ans, s);

		  //set all larger in tree to zero
		  if (i >= 1){
			  while (a[countInMas - 1] > l[i - 1]){
				  int index = indices[countInMas - 1];
				  update(1, 0, n - 1, index, 0);
				  removed[index] = true;
				  if (index >= curFirst){
					  curFirst--;
					  while (removed[curFirst])
						  curFirst--;
				  }
				  countInMas--;
			  }
		  }

		  if (countInMas < k)
			  break;
	  }
  }

  cout << ans;
}


#
# http://codeforces.com/problemset/problem/658/E
import sys
from itertools import imap

def read_line():
    return next(sys.stdin).strip()

def read_ints():
    return map(int, read_line().split())

def case1():
    y = K * A[K - 1] - sum(A[:K])
    yield y * C

    for i in xrange(K, N):
        a, b, c = A[i], A[i - 1], A[i - K]
        y += (K - 1) * (a - b) - (b - c)
        yield y * C

def case2():
    def round(x):
        return x + (r - x) % 5

    def score(x, y):
        return x * C + (y / 5) * B

    for r in xrange(5):
        s = sum(imap(round, A[:K]))
        x, y = s - sum(A[:K]), K * round(A[K - 1]) - s

        yield score(x, y)

        for i in xrange(K, N):
            a, b, c = round(A[i]), round(A[i - 1]), round(A[i - K])
            x += (a - A[i]) - (c - A[i - K])
            y += (K - 1) * (a - b) - (b - c)
            yield score(x, y)

N, K, B, C = read_ints()
A = sorted(read_ints())

case = case1 if B >= 5 * C else case2
print min(case())


#include <algorithm>
#include <cstdio>
#include <climits>
#include <queue>

typedef long long LL;

using namespace std;

LL N, K, B, C, result = LLONG_MAX;

void case1(LL *A) {
  LL sum = 0;
  for (auto i = 0; i < K; i++)
    sum += A[K - 1] - A[i];

  result = min(result, sum);

  for (auto i = K; i < N; i++) {
    sum += (K - 1) * A[i] - K * A[i - 1] + A[i - K];
    result = min(result, sum);
  }
}

void case2(LL *A, LL r) {
  priority_queue<LL> q;
  auto sum = 0;
  for (auto i = 0; i < N; i++) {
    auto x = A[i];
    auto y = x + ((r - x) % 5 + 5) % 5;
    auto v = (y - x) * C * 5 - y * B;
    q.push(v);
    sum += v;
    if (i >= K) {
      sum -= q.top();
      q.pop();
    }
    if (i >= K - 1)
      result = min(result, (y * K * B + sum) / 5);
  }
}

int main() {
  scanf("%lld%lld%lld%lld", &N, &K, &B, &C);
  LL A[N];
  for (auto i = 0; i < N; i++)
    scanf("%lld", A + i);
  sort(A, A + N);
  if (B >= 5 * C)
    case1(A);
  else {
    for (auto r = 0; r < 5; r++)
      case2(A, r);
  }

  printf("%lld\n", result);
}


#
# http://codeforces.com/problemset/problem/658/E
import sys
from itertools import imap

def read_line():
    return next(sys.stdin).strip()

def read_ints():
    return map(int, read_line().split())

def case1():
    y = K * A[K - 1] - sum(A[:K])
    yield y * C

    for i in xrange(K, N):
        a, b, c = A[i], A[i - 1], A[i - K]
        y += (K - 1) * (a - b) - (b - c)
        yield y * C

def case2():
    def round(x):
        return x + (r - x) % 5

    for r in xrange(5):
        s = sum(imap(round, A[:K]))
        x = s - sum(A[:K])
        y = K * round(A[K - 1]) - s

        yield x * C + (y / 5) * B

        for i in xrange(K, N):
            a, b, c = round(A[i]), round(A[i - 1]), round(A[i - K])
            x += (a - A[i]) - (c - A[i - K])
            y += (K - 1) * (a - b) - (b - c)
            yield x * C + (y / 5) * B

N, K, B, C = read_ints()
A = sorted(read_ints())

case = case1 if B >= 5 * C else case2
print min(case())
print min(case1())


#
# http://codeforces.com/problemset/problem/658/E
import sys
import heapq

def read_line():
    return next(sys.stdin).strip()

def read_ints():
    return map(int, read_line().split())

def case1():
    s = sum(A[K - 1] - v for v in A[:K])
    yield s * C

    for i in xrange(K, N):
        s += (K - 1) * A[i] - K * A[i - 1] + A[i - K]
        yield s * C

def case2():
    def round(x):
        return x + (r - x) % 5

    def val(v):
        return round(v) * B - (round(v) - v) * C * 5

    for r in xrange(5):
        heap = map(val, A[:K])
        heapq.heapify(heap)

        s = sum(heap)
        yield (round(A[K - 1]) * K * B - s) // 5

        for v in A[K:]:
            s += val(v) - heapq.heappushpop(heap, val(v))
            yield (round(v) * K * B - s) // 5

N, K, B, C = read_ints()
A = sorted(read_ints())
case = case1 if B >= 5 * C else case2
print min(case())


#
# http://codeforces.com/problemset/problem/658/E
import sys
from itertools import islice
from collections import deque
import heapq

def read_line():
    return next(sys.stdin).strip()

def read_ints():
    return map(int, read_line().split())

def case1():
    y = K * A[K - 1] - sum(A[:K])
    yield y * C

    for i in xrange(K, N):
        a, b, c = A[i], A[i - 1], A[i - K]
        y += (K - 1) * (a - b) - (b - c)
        yield y * C

def case2():
    for r in xrange(5):
        yield min(_case2(r))

def _case2(r):
    def round(x):
        return x + (r - x) % 5

    def gen(k):
        for v in buckets[k]:
            yield k * C + ((goal - v) / 5) * B

    buckets = [deque() for i in xrange(5)]
    for i, v in enumerate(A):
        goal = round(v)
        buckets[goal - v].appendleft(goal)
        if i >= K - 1:
            yield sum(islice(heapq.merge(*[gen(k) for k in xrange(5)]), K))

N, K, B, C = read_ints()
A = sorted(read_ints())

case = case1 if B >= 5 * C else case2
print min(case())


#
# http://codeforces.com/problemset/problem/658/E
import sys
from fractions import Fraction
import heapq

def read_line():
    return next(sys.stdin).strip()

def read_ints():
    return map(int, read_line().split())

def case1():
    s = sum(A[K - 1] - v for v in A[:K])
    yield s * C

    for i in xrange(K, N):
        s += (K - 1) * A[i] - K * A[i - 1] + A[i - K]
        yield s * C

def case2():
    def round(x):
        return x + (r - x) % 5

    def val(v):
        return Fraction(round(v) * B, 5) - (round(v) - v) * C

    for r in xrange(5):
        heap = map(val, A[:K])
        heapq.heapify(heap)

        s = sum(heap)
        yield Fraction(round(A[K - 1]) * K * B, 5) - s

        for v in A[K:]:
            s += val(v) - heapq.heappushpop(heap, val(v))
            yield Fraction(round(v) * K * B, 5) - s

N, K, B, C = read_ints()
A = sorted(read_ints())

case = case1 if B >= 5 * C else case2
print min(case())


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class ProE {
	public static void main(String[] args) {
		MyScanner cin = new MyScanner();

		int n = cin.nextInt(), k = cin.nextInt(), b = cin.nextInt(), c = cin.nextInt();
		int[] con = new int[n];
		for (int i = 0; i < con.length; i++) {
			con[i] = cin.nextInt();
		}
		Arrays.sort(con);
		int min = Integer.MAX_VALUE;
		for (int start = 0; start <= n - k; start++) {
			final int end = k - 1 + start;
			min = Integer.min(solve(con, start, end, b, c), min);
		}
		System.out.println(min);
	}

	private static int solve(int[] con, int start, int end, int b, int c) {
		int max = con[end];
		if (5 * c <= b) {
			int cCount = 0;
			for (int index = start; index < end; index++) {
				cCount += (max - con[index]);
			}
			return c * cCount;
		} else {
			int min = Integer.MAX_VALUE;
			for (int i = 0; i < 5; i++) {
				int cost = i * c;
				for (int index = start; index < end; index++) {
					int diff = max + i - con[index];
					cost += (diff / 5) * b + (diff % 5) * c;
				}
				if (cost < min)
					min = cost;
			}
			return min;
		}
	}

	private static class MyScanner {
		BufferedReader br;
		StringTokenizer st;

		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}
	}
}


#include <bits/stdc++.h>
#define ll long long
#define dd double
#define dll deque<long long>
#define vll vector<long long>
#define vc vector<char>
#define sll stack<long long>
#define sc stack<char>
#define mll map<long long,long long>
#define pll pair<long long,long long>
#define mcl map<char,long long>
#define pcl pair<char,long long>
using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n; cin>>n;
    string s; cin>>s;
    ll bl[n]={0},cnt1[n]={0},cnt0[n]={0};
    if(s[0]==1){cnt1[0]=1;}
    else{cnt0[0]=1;}
    for(ll i=1;i<n;i++){
        if(s[i]=='1'){cnt1[i]=1+cnt1[i-1]; cnt0[i]=cnt0[i-1];}
        else{cnt0[i]=1+cnt0[i-1]; cnt1[i]=cnt1[i-1];}
    }
    for(ll i=0;i<n;i++){
        bl[i] = cnt1[i]-cnt0[i];
    }
    unordered_map<ll,ll> mp;
    ll ans=0;
    mp[0]-1;
    for(ll i=0;i<n;i++){
      if(mp.find(bl[i])==mp.end()){mp[bl[i]]=i;}
      else{
          ans = max(ans,i-mp[bl[i]]);
      }
    }
    cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long
#define dd double
#define dll deque<long long>
#define vll vector<long long>
#define vc vector<char>
#define sll stack<long long>
#define sc stack<char>
#define mll map<long long,long long>
#define pll pair<long long,long long>
#define mcl map<char,long long>
#define pcl pair<char,long long>
using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n; cin>>n;
    string s; cin>>s;
    ll bl[n]={0},cnt1[n]={0},cnt0[n]={0};
    if(s[0]==1){cnt1[0]=1;}
    else{cnt0[0]=1;}
    for(ll i=1;i<n;i++){
        if(s[i]=='1'){cnt1[i]=1+cnt1[i-1]; cnt0[i]=cnt0[i-1];}
        else{cnt0[i]=1+cnt0[i-1]; cnt1[i]=cnt1[i-1];}
    }
    for(ll i=0;i<n;i++){
        bl[i] = cnt1[i]-cnt0[i];
    }
    unordered_map<ll,ll> mp;
    ll ans=0;
    mp[0]=-1;
    for(ll i=0;i<n;i++){
      if(mp.find(bl[i])==mp.end()){mp[bl[i]]=i;}
      else{
          ans = max(ans,i-mp[bl[i]]);
      }
    }
    cout<<ans;
}


#include <iostream>
using namespace std;
const int N=100005;
int pos[N],a[N],n,m=0;
int main(){
	char x;
	cin>>n;
	pos[-7]=0;
	for(int i=1;i<=n;i++){
		cin>>x;
		a[i]=(x-'0')?1:-1;
		a[i]+=a[i-1];
	}
	for(int i=1;i<=n;i++){
		if(pos[a[i]]||!a[i]){
			m=max(m,i-pos[a[i]]);
		}
		else pos[a[i]]=i;
	}
	cout<<m<<endl;
	return 0;
}


#include <iostream>
using namespace std;
int main(){
	int a[100005],n,m=0;
	char x;
	cin>>n;
	a[0]=0;
	for(int i=1;i<=n;i++){
		cin>>x;
		a[i]=(x-'0')?1:-1;
		a[i]+=a[i-1];
	}
	for(int i=0;i<n;i++){
		for(int j=n;j>i;j--){
			if((a[j]-a[i])==0){
				m=max(m,j-i);
			}
		}
	}
	cout<<m<<endl;
	return 0;
}


#include <iostream>
#include <string>
using namespace std;
int main(){
	int a[100005],n,m=0,pos[100005];
	char x;
	cin>>n;
	a[0]=0;
	for(i=0;i<100005;i++){
		pos[i]=0;
	}
	for(int i=1;i<=n;i++){
		cin>>x;
		a[i]=(x-'0')?1:-1;
		a[i]+=a[i-1];
	}
	for(int i=0;i<=n;i++){
		if(pos[a[i]]||!a[i]){
			m=max(m,i-pos[a[i]]);
		}
		else pos[i]=i;
	}
	cout<<m<<endl;
	return 0;
}


#include<iostream>
using namespace std;
const int N=100004;
int n,a[N],pos[N],ans;
int main()
{
	char x;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
    	cin>>x;
		a[i]=a[i-1]+((x-'0')?1:-1);
    }
    for(int i=1;i<=n;i++)
		if(pos[a[i]]||a[i]==0)
    		ans=max(ans,i-pos[a[i]]);
		else pos[a[i]]=i;
    cout<<ans<<endl;
	return 0;
}//Paul_Guderian


#include <iostream>
#include <string>
using namespace std;
int main(){
	int a[100005],n,m=0,pos[100005];
	char x;
	cin>>n;
	a[0]=0;
	for(int i=0;i<100005;i++){
		pos[i]=0;
	}
	for(int i=1;i<=n;i++){
		cin>>x;
		a[i]=(x-'0')?1:-1;
		a[i]+=a[i-1];
	}
	for(int i=0;i<=n;i++){
		if(pos[a[i]]||!a[i]){
			m=max(m,i-pos[a[i]]);
		}
		else pos[i]=i;
	}
	cout<<m<<endl;
	return 0;
}


#include <iostream>
#include <map>
using namespace std;
const int N=100005;
int a[N],n,m=0;
map <int,int> pos;
int main(){
	char x;
	cin>>n;
    for(int i=1;i<=n;i++){
    	cin>>x;
		a[i]=a[i-1]+((x-'0')?1:-1);
    }
	for(int i=1;i<=n;i++){
		if(pos[a[i]]||!a[i]){
			m=max(m,i-pos[a[i]]);
		}
		else pos[a[i]]=i;
	}
	cout<<m<<endl;
	return 0;
}


#include <iostream>
#include <string>
using namespace std;
int main(){
	int a[100005],n,m=0,pos[100005];
	char x;
	cin>>n;
	a[0]=0;
	memset(pos,0,sizeof(pos));
	for(int i=1;i<=n;i++){
		cin>>x;
		a[i]=(x-'0')?1:-1;
		a[i]+=a[i-1];
	}
	for(int i=0;i<=n;i++){
		if(pos[a[i]]||!a[i]){
			m=max(m,i-pos[a[i]]);
		}
		else pos[i]=i;
	}
	cout<<m<<endl;
	return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;
const int N=100004;
int n,a[N],pos[N],ans;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%1d",a+i);
    for(int i=1;i<=n;i++)a[i]=a[i-1]+(a[i]?1:-1);
    for(int i=1;i<=n;i++)
	if(pos[a[i]]||a[i]==0)
    ans=max(ans,i-pos[a[i]]);else pos[a[i]]=i;
    printf("%d\n",ans);return 0;
}


#include <iostream>
using namespace std;
const int N=100005;
int main(){
	int a[N],n,m=0,pos[N];
	char x;
	cin>>n;
	a[0]=0;
	for(int i=0;i<100005;i++){
		pos[i]=0;
	}
	for(int i=1;i<=n;i++){
		cin>>x;
		a[i]=(x-'0')?1:-1;
		a[i]+=a[i-1];
	}
	for(int i=1;i<=n;i++){
		if(pos[a[i]]||!a[i]){
			m=max(m,i-pos[a[i]]);
		}
		else pos[a[i]]=i;
	}
	cout<<m<<endl;
	return 0;
}


#include <iostream>
using namespace std;
int main(){
	int a[100005],n,m=0;
	char x;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>x;
		a[i]=x-'0';
		if(a[i]==0){
			a[i]=-1;
		}
		a[i]+=a[i-1];
	}
	for(int i=1;i<n;i++){
		for(int j=n;j>i;j--){
			if((a[j]-a[i])==0){
				m=max(m,j-i);
			}
		}
	}
	cout<<m<<endl;
	return 0;
}


#include <iostream>
#include <string>
using namespace std;
int main(){
	int a[100005],n,m=0,pos[100005];
	char x;
	cin>>n;
	a[0]=0;
	for(int i=0;i<100005;i++){
		pos[i]=0;
	}
	for(int i=1;i<=n;i++){
		cin>>x;
		a[i]=(x-'0')?1:-1;
		a[i]+=a[i-1];
	}
	for(int i=0;i<=n;i++){
		if(pos[a[i]]||!a[i]){
			m=max(m,i-pos[a[i]]);
		}
		else pos[a[i]]=i;
	}
	cout<<m<<endl;
	return 0;
}


#include <iostream>
using namespace std;
const int N=100005;
int pos[N];
int main(){
	int a[N],n,m=0;
	char x;
	cin>>n;
	a[0]=0;
	for(int i=0;i<100005;i++){
		pos[i]=0;
	}
	for(int i=1;i<=n;i++){
		cin>>x;
		a[i]=(x-'0')?1:-1;
		a[i]+=a[i-1];
	}
	for(int i=1;i<=n;i++){
		if(pos[a[i]]||!a[i]){
			m=max(m,i-pos[a[i]]);
		}
		else pos[a[i]]=i;
	}
	cout<<m<<endl;
	return 0;
}


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

#define pb push_back
#define ld double
#define ll long long
#define MOD 1000000007

ll power(ll x,ll y,ll p)
{
    ll res=1;

    x = x % p;

    while (y > 0) {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }

    return res;
}

#define ALL(x) x.begin(),x.end()

bool prime[1000005];

void seive(int n)
{
    memset(prime, true, sizeof(prime));

    for (int p=2; p*p<=n; p++)
    {
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }

}
map<ll,vector<ll>> adj;
map<ll,ll> visited;
ll cnt=0;

void dfs(ll curr,ll par,ll lvl)
{
    visited[curr]=1;

    cnt=max(cnt,lvl);

    for (auto x:adj[curr])
    {
        if (visited[x]!=1&&x!=par)
        {
            dfs(x,curr,lvl+1);
        }
    }
}

ll rect(ll x,ll y,ll zero[],ll one[])
{
    ll res=0;
    res=(zero[y]-zero[x-1])-(one[y]-one[x-1]);

    return res;
}

int main()
{
    ll n;
    cin>>n;
    string s;
    cin>>s;

    ll zero[n+1]={0};
    ll one[n+1]={0};

    ll mx=0;
    for (ll i=0;i<n;i++)
    {
        if (s[i]=='0')
        zero[i+1]=zero[i]+1,one[i+1]=one[i];
        else
        one[i+1]=one[i]+1,zero[i+1]=zero[i];

    }

    /*
    for (ll i=0;i<=n;i++)
    {
        cout<<zero[i]<<" "<<one[i]<<endl;
    }
    */
    ll ptr1=1,ptr2=n;
    ll now=(zero[ptr2]-zero[ptr1-1])-(one[ptr2]-one[ptr1-1]);
    while(ptr2>=ptr1)
    {
        now=rect(ptr1,ptr2,zero,one);

        if (now==0)
        break;
        else if (abs(rect(ptr1+1,ptr2,zero,one))<abs(rect(ptr1,ptr2-1,zero,one)))
        {
            ptr1++;
        }
        else
        ptr2--;

       // cout<<ptr1<<" "<<ptr2<<endl;
    }

    //cout<<zero[ptr2]-zero[ptr1-1]<<" "<<one[ptr2]-one[ptr1-1]<<endl;
   // cout<<ptr1<<" "<<ptr2<<" ->now:"<<now<<endl;
    if (now==0)
    cout<<ptr2-ptr1+1<<endl;
    else
    cout<<0<<endl;

    return 0;
}


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

#define pb push_back
#define ld double
#define ll long long
#define MOD 1000000007

ll power(ll x,ll y,ll p)
{
    ll res=1;

    x = x % p;

    while (y > 0) {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }

    return res;
}

#define ALL(x) x.begin(),x.end()

bool prime[1000005];

void seive(int n)
{
    memset(prime, true, sizeof(prime));

    for (int p=2; p*p<=n; p++)
    {
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }

}
map<ll,vector<ll>> adj;
map<ll,ll> visited;
ll cnt=0;

void dfs(ll curr,ll par,ll lvl)
{
    visited[curr]=1;

    cnt=max(cnt,lvl);

    for (auto x:adj[curr])
    {
        if (visited[x]!=1&&x!=par)
        {
            dfs(x,curr,lvl+1);
        }
    }
}

ll rect(ll x,ll y,ll zero[],ll one[])
{
    ll res=0;
    res=(zero[y]-zero[x-1])-(one[y]-one[x-1]);

    return res;
}

int main()
{
    ll n;
    cin>>n;
    string s;
    cin>>s;

    ll zero[n+1]={0};
    ll one[n+1]={0};

    ll mx=0;
    for (ll i=0;i<n;i++)
    {
        if (s[i]=='0')
        zero[i+1]=zero[i]+1,one[i+1]=one[i];
        else
        one[i+1]=one[i]+1,zero[i+1]=zero[i];

    }

    /*
    for (ll i=0;i<=n;i++)
    {
        cout<<zero[i]<<" "<<one[i]<<endl;
    }
    */
    ll ptr1=1,ptr2=n;
    ll now=(zero[ptr2]-zero[ptr1-1])-(one[ptr2]-one[ptr1-1]);
    while(ptr2>=ptr1)
    {
        now=rect(ptr1,ptr2,zero,one);

        if (now==0)
        break;
        else if (abs(rect(ptr1+1,ptr2,zero,one))<=abs(rect(ptr1,ptr2-1,zero,one)))
        {
            ptr1++;
        }
        else
        ptr2--;
    }

    //cout<<zero[ptr2]-zero[ptr1-1]<<" "<<one[ptr2]-one[ptr1-1]<<endl;
   // cout<<ptr1<<" "<<ptr2<<" ->now:"<<now<<endl;
    if (now==0)
    cout<<ptr2-ptr1+1<<endl;
    else
    cout<<0<<endl;

    return 0;
}


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

#define pb push_back
#define ld double
#define ll long long
#define MOD 1000000007

ll power(ll x,ll y,ll p)
{
    ll res=1;

    x = x % p;

    while (y > 0) {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }

    return res;
}

#define ALL(x) x.begin(),x.end()

bool prime[1000005];

void seive(int n)
{
    memset(prime, true, sizeof(prime));

    for (int p=2; p*p<=n; p++)
    {
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }

}
map<ll,vector<ll>> adj;
map<ll,ll> visited;
ll cnt=0;

void dfs(ll curr,ll par,ll lvl)
{
    visited[curr]=1;

    cnt=max(cnt,lvl);

    for (auto x:adj[curr])
    {
        if (visited[x]!=1&&x!=par)
        {
            dfs(x,curr,lvl+1);
        }
    }
}

ll rect(ll x,ll y,ll zero[],ll one[])
{
    ll res=0;
    res=(zero[y]-zero[x-1])-(one[y]-one[x-1]);

    return res;
}

map<pair<ll,ll>,ll> dp;

ll rec(ll ptr1,ll ptr2,string s,ll zero[],ll one[])
{
    ll res=0;
    ll x,y;
    x=ptr1;y=ptr2;

        while(ptr2>=ptr1)
        {
            ll now=rect(ptr1,ptr2,zero,one);

            if (now==0)
            {
            res=max((ll)0,ptr2-ptr1+1);
            break;
            }
            else if (abs(rect(ptr1+1,ptr2,zero,one))<abs(rect(ptr1,ptr2-1,zero,one)))
            {
                ptr1++;
            }
            else if (abs(rect(ptr1+1,ptr2,zero,one))>abs(rect(ptr1,ptr2-1,zero,one)))
            ptr2--;
            else
            {

                ll a=0,b=0;
                if (zero[ptr2]-zero[ptr1]>0&&one[ptr2]-one[ptr1]>0)
                a=rec(ptr1+1,ptr2,s,zero,one);

                if (zero[ptr2-1]-zero[ptr1-1]>0&&one[ptr2-1]-one[ptr1-1]>0)
                b=rec(ptr1,ptr2-1,s,zero,one);

                res=max(a,b);
                break;
            }

           // cout<<ptr1<<" "<<ptr2<<endl;
        }

    return res;

}

int main()
{
    ll n;
    cin>>n;
    string s;
    cin>>s;

    ll zero[n+1]={0};
    ll one[n+1]={0};

    ll mx=0;
    for (ll i=0;i<n;i++)
    {
        if (s[i]=='0')
        zero[i+1]=zero[i]+1,one[i+1]=one[i];
        else
        one[i+1]=one[i]+1,zero[i+1]=zero[i];

    }

    map<ll,vector<ll>> mp;
    mp[0].pb(0);
    for (ll i=1;i<=n;i++)
    {
        mp[one[i]-zero[i]].pb(i);
    }
    /*
    for (auto s:mp)
    {
        cout<<s.first<<endl;
        for (auto x:s.second)
        cout<<x<<" ";
        cout<<endl;
        cout<<"%%%%%"<<endl;
    }
    */

    for (auto s:mp)
    {
        ll w=mp[s.first].size();
            ll x=mp[s.first][0],y=mp[s.first][w-1];
            mx=max(mx,abs(y-x));

    }

    cout<<mx<<endl;

    return 0;
}


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

#define pb push_back
#define ld double
#define ll long long
#define MOD 1000000007

ll power(ll x,ll y,ll p)
{
    ll res=1;

    x = x % p;

    while (y > 0) {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }

    return res;
}

#define ALL(x) x.begin(),x.end()

bool prime[1000005];

void seive(int n)
{
    memset(prime, true, sizeof(prime));

    for (int p=2; p*p<=n; p++)
    {
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }

}
map<ll,vector<ll>> adj;
map<ll,ll> visited;
ll cnt=0;

void dfs(ll curr,ll par,ll lvl)
{
    visited[curr]=1;

    cnt=max(cnt,lvl);

    for (auto x:adj[curr])
    {
        if (visited[x]!=1&&x!=par)
        {
            dfs(x,curr,lvl+1);
        }
    }
}

ll rect(ll x,ll y,ll zero[],ll one[])
{
    ll res=0;
    res=(zero[y]-zero[x-1])-(one[y]-one[x-1]);

    return res;
}

map<pair<ll,ll>,ll> dp;

ll rec(ll ptr1,ll ptr2,string s,ll zero[],ll one[])
{
    ll res=0;
    ll x,y;
    x=ptr1;y=ptr2;

    if (ptr2>=ptr1&&dp[{x,y}]!=0)
    {
        return dp[{x,y}];
    }
    else
    {
        while(ptr2>=ptr1)
        {
            ll now=rect(ptr1,ptr2,zero,one);

            if (now==0)
            {
            res=max((ll)0,ptr2-ptr1+1);
            break;
            }
            else if (abs(rect(ptr1+1,ptr2,zero,one))<abs(rect(ptr1,ptr2-1,zero,one)))
            {
                ptr1++;
            }
            else if (abs(rect(ptr1+1,ptr2,zero,one))>abs(rect(ptr1,ptr2-1,zero,one)))
            ptr2--;
            else
            {
                ll a=0,b=0;
                if (zero[ptr2]-zero[ptr1]>0&&one[ptr2]-one[ptr1]>0)
                a=rec(ptr1+1,ptr2,s,zero,one);

                if (zero[ptr2-1]-zero[ptr1-1]>0&&one[ptr2-1]-one[ptr1-1]>0)
                b=rec(ptr1,ptr2-1,s,zero,one);

                res=max(a,b);
                break;
            }

           // cout<<ptr1<<" "<<ptr2<<endl;
        }

        return dp[{x,y}]=res;
    }

}

int main()
{
    ll n;
    cin>>n;
    string s;
    cin>>s;

    ll zero[n+1]={0};
    ll one[n+1]={0};

    ll mx=0;
    for (ll i=0;i<n;i++)
    {
        if (s[i]=='0')
        zero[i+1]=zero[i]+1,one[i+1]=one[i];
        else
        one[i+1]=one[i]+1,zero[i+1]=zero[i];

    }

    /*
    for (ll i=0;i<=n;i++)
    {
        cout<<zero[i]<<" "<<one[i]<<endl;
    }
    */
    ll ptr1=1,ptr2=n;

    ll now=rec(ptr1,ptr2,s,zero,one);

    cout<<now<<endl;

    return 0;
}


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

#define pb push_back
#define ld double
#define ll long long
#define MOD 1000000007

ll power(ll x,ll y,ll p)
{
    ll res=1;

    x = x % p;

    while (y > 0) {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }

    return res;
}

#define ALL(x) x.begin(),x.end()

bool prime[1000005];

void seive(int n)
{
    memset(prime, true, sizeof(prime));

    for (int p=2; p*p<=n; p++)
    {
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }

}
map<ll,vector<ll>> adj;
map<ll,ll> visited;
ll cnt=0;

void dfs(ll curr,ll par,ll lvl)
{
    visited[curr]=1;

    cnt=max(cnt,lvl);

    for (auto x:adj[curr])
    {
        if (visited[x]!=1&&x!=par)
        {
            dfs(x,curr,lvl+1);
        }
    }
}

ll rect(ll x,ll y,ll zero[],ll one[])
{
    ll res=0;
    res=(zero[y]-zero[x-1])-(one[y]-one[x-1]);

    return res;
}

ll rec(ll ptr1,ll ptr2,string s,ll zero[],ll one[])
{
    ll res=0;
    while(ptr2>=ptr1)
    {
        ll now=rect(ptr1,ptr2,zero,one);

        if (now==0)
        {
        res=max((ll)0,ptr2-ptr1+1);
        break;
        }
        else if (abs(rect(ptr1+1,ptr2,zero,one))<abs(rect(ptr1,ptr2-1,zero,one)))
        {
            ptr1++;
        }
        else if (abs(rect(ptr1+1,ptr2,zero,one))>abs(rect(ptr1,ptr2-1,zero,one)))
        ptr2--;
        else
        {
            res=max(rec(ptr1+1,ptr2,s,zero,one),rec(ptr1,ptr2-1,s,zero,one));
            break;
        }

       // cout<<ptr1<<" "<<ptr2<<endl;
    }

   return res;

}

int main()
{
    ll n;
    cin>>n;
    string s;
    cin>>s;

    ll zero[n+1]={0};
    ll one[n+1]={0};

    ll mx=0;
    for (ll i=0;i<n;i++)
    {
        if (s[i]=='0')
        zero[i+1]=zero[i]+1,one[i+1]=one[i];
        else
        one[i+1]=one[i]+1,zero[i+1]=zero[i];

    }

    /*
    for (ll i=0;i<=n;i++)
    {
        cout<<zero[i]<<" "<<one[i]<<endl;
    }
    */
    ll ptr1=1,ptr2=n;

    ll now=rec(ptr1,ptr2,s,zero,one);

    cout<<now<<endl;

    return 0;
}


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

#define pb push_back
#define ld double
#define ll long long
#define MOD 1000000007

ll power(ll x,ll y,ll p)
{
    ll res=1;

    x = x % p;

    while (y > 0) {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }

    return res;
}

#define ALL(x) x.begin(),x.end()

bool prime[1000005];

void seive(int n)
{
    memset(prime, true, sizeof(prime));

    for (int p=2; p*p<=n; p++)
    {
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }

}
map<ll,vector<ll>> adj;
map<ll,ll> visited;
ll cnt=0;

void dfs(ll curr,ll par,ll lvl)
{
    visited[curr]=1;

    cnt=max(cnt,lvl);

    for (auto x:adj[curr])
    {
        if (visited[x]!=1&&x!=par)
        {
            dfs(x,curr,lvl+1);
        }
    }
}

ll rect(ll x,ll y,ll zero[],ll one[])
{
    ll res=0;
    res=(zero[y]-zero[x-1])-(one[y]-one[x-1]);

    return res;
}

map<pair<ll,ll>,ll> dp;

ll rec(ll ptr1,ll ptr2,string s,ll zero[],ll one[])
{
    ll res=0;
    ll x,y;
    x=ptr1;y=ptr2;

        while(ptr2>=ptr1)
        {
            ll now=rect(ptr1,ptr2,zero,one);

            if (now==0)
            {
            res=max((ll)0,ptr2-ptr1+1);
            break;
            }
            else if (abs(rect(ptr1+1,ptr2,zero,one))<abs(rect(ptr1,ptr2-1,zero,one)))
            {
                ptr1++;
            }
            else if (abs(rect(ptr1+1,ptr2,zero,one))>abs(rect(ptr1,ptr2-1,zero,one)))
            ptr2--;
            else
            {
                ll a=0,b=0;
                if (zero[ptr2]-zero[ptr1]>0&&one[ptr2]-one[ptr1]>0)
                a=rec(ptr1+1,ptr2,s,zero,one);

                if (zero[ptr2-1]-zero[ptr1-1]>0&&one[ptr2-1]-one[ptr1-1]>0)
                b=rec(ptr1,ptr2-1,s,zero,one);

                res=max(a,b);
                break;
            }

           // cout<<ptr1<<" "<<ptr2<<endl;
        }

    return res;

}

int main()
{
    ll n;
    cin>>n;
    string s;
    cin>>s;

    ll zero[n+1]={0};
    ll one[n+1]={0};

    ll mx=0;
    for (ll i=0;i<n;i++)
    {
        if (s[i]=='0')
        zero[i+1]=zero[i]+1,one[i+1]=one[i];
        else
        one[i+1]=one[i]+1,zero[i+1]=zero[i];

    }

    /*
    for (ll i=0;i<=n;i++)
    {
        cout<<zero[i]<<" "<<one[i]<<endl;
    }
    */
    ll ptr1=1,ptr2=n;

    ll now=rec(ptr1,ptr2,s,zero,one);

    cout<<now<<endl;

    return 0;
}


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

#define pb push_back
#define ld double
#define ll long long
#define MOD 1000000007

ll power(ll x,ll y,ll p)
{
    ll res=1;

    x = x % p;

    while (y > 0) {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }

    return res;
}

#define ALL(x) x.begin(),x.end()

bool prime[1000005];

void seive(int n)
{
    memset(prime, true, sizeof(prime));

    for (int p=2; p*p<=n; p++)
    {
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }

}
map<ll,vector<ll>> adj;
map<ll,ll> visited;
ll cnt=0;

void dfs(ll curr,ll par,ll lvl)
{
    visited[curr]=1;

    cnt=max(cnt,lvl);

    for (auto x:adj[curr])
    {
        if (visited[x]!=1&&x!=par)
        {
            dfs(x,curr,lvl+1);
        }
    }
}

ll rect(ll x,ll y,ll zero[],ll one[])
{
    ll res=0;
    res=(zero[y]-zero[x-1])-(one[y]-one[x-1]);

    return res;
}

map<pair<ll,ll>,ll> dp;

ll rec(ll ptr1,ll ptr2,string s,ll zero[],ll one[])
{
    ll res=0;
    ll x,y;
    x=ptr1;y=ptr2;

        while(ptr2>=ptr1)
        {
            ll now=rect(ptr1,ptr2,zero,one);

            if (now==0)
            {
            res=max((ll)0,ptr2-ptr1+1);
            break;
            }
            else if (abs(rect(ptr1+1,ptr2,zero,one))<abs(rect(ptr1,ptr2-1,zero,one)))
            {
                ptr1++;
            }
            else if (abs(rect(ptr1+1,ptr2,zero,one))>abs(rect(ptr1,ptr2-1,zero,one)))
            ptr2--;
            else
            {

                ll a=0,b=0;
                if (zero[ptr2]-zero[ptr1]>0&&one[ptr2]-one[ptr1]>0)
                a=rec(ptr1+1,ptr2,s,zero,one);

                if (zero[ptr2-1]-zero[ptr1-1]>0&&one[ptr2-1]-one[ptr1-1]>0)
                b=rec(ptr1,ptr2-1,s,zero,one);

                res=max(a,b);
                break;
            }

           // cout<<ptr1<<" "<<ptr2<<endl;
        }

    return res;

}

int main()
{
    ll n;
    cin>>n;
    string s;
    cin>>s;

    ll zero[n+1]={0};
    ll one[n+1]={0};

    ll mx=0;
    for (ll i=0;i<n;i++)
    {
        if (s[i]=='0')
        zero[i+1]=zero[i]+1,one[i+1]=one[i];
        else
        one[i+1]=one[i]+1,zero[i+1]=zero[i];

    }

    map<ll,vector<ll>> mp;
    for (ll i=1;i<=n;i++)
    {
        mp[one[i]-zero[i]].pb(i);
    }
    for (auto s:mp)
    {
        ll w=mp[s.first].size();
        if (w>1)
        {
            ll x=mp[s.first][0],y=mp[s.first][w-1];
            mx=max(mx,abs(y-x));
        }
    }

    cout<<mx<<endl;

    return 0;
}


#include <iostream>
#include<algorithm>
#include<string>

using namespace std;

int main()
{
    int n;cin>>n;
    string d;cin>>d;
    char s[n];int i,j,max=0,o,z;int l=d.length(),t=0,h=0;
    for(i=0;i<l;i++)
        s[i]=d[i];
    for(i=0;i<l;i++)
    {
        for(j=i;j<l;j++)
        {
            o=count(s+i,s+j,'1');
            z=count(s+i,s+j,'0');
            if(o==z)
            {
                t=o+z;h=1;
                if(t>=max)
                    max=t;
            }
        }
    }
if(h==0)
cout<<'0';
else
 cout<<max;
    return 0;
}


#include <iostream>
#include<algorithm>
#include<string>

using namespace std;

int main()
{
    int n;cin>>n;
    string d;cin>>d;
    char s[n];int i,j,max=0,o=0,z=0;int l=d.length(),t,k;
    string p;
    for(i=0;i<l;i++)
    {
        for(j=i+1;j<l;j++)
        {  o=0;z=0;p=d.substr(i,j);
            for(k=0;k<(j-i);k++)
            {

                if(p[k]=='1')
                    o++;
                if(p[k]=='0')
                    z++;
            }
            if(o==z)
            {
                t=o+z;
                if(t>=max)
                    max=t;
            }
        }
    }
    cout<<max;
    return 0;
}


#include <iostream>
#include<algorithm>
#include<string>

using namespace std;

int main()
{
    int n;cin>>n;
    string d;cin>>d;
    char s[n];int i,j,max=0,o=0,z=0;int l=d.length(),t,k;
    string p;
    for(i=0;i<l;i++)
    {if((l-i)>max){
        for(j=i+1;j<l;j++)
        {
            o=0;z=0;p=d.substr(i,j+1);
        if(p.length()>max){
            for(k=0;k<(j-i+1);k++)
            {

                if(p[k]=='1')
                    o++;

                else
                    z++;
                    if(o>=1+l/2||z>=1+l/2)
                        break;
            }
            if(o==z)
            {
                t=o+z;
                if(t>=max){
                    max=t;
                    if(max==l)
                        break;}
            }
            if(max==l)
                break;
        }
        }
        if(max==l)
            break;
    }
    }
    cout<<max;
    return 0;
}


#include <iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{int n;cin>>n;
string s;
cin>>s;
int i;
int maxi=0,m=0;
vector<int>v(2*n+20,0);
for(i=1;i<=n;i++)
{
    if(s[i-1]=='0')
        m--;
    else
        m++;
        if(m==0)
            maxi=i;
    else{
            if(v[m+n]==0)
            v[m+n]=i;
    else{
        maxi=max(maxi,i-v[m+n]);

    }

    }
}
cout<<maxi;

    return 0;
}


#include <iostream>
#include<string>

using namespace std;

int main()
{int n;cin>>n;
    string s;cin>>s;int max=0;string p="";int count,q,i,l=s.length();int j=0;
    for(i=0;i<l;i++)
    {j=i+2;
        while(j<l)
        {count=0;
           p=s.substr(i,j) ;int len=p.length();
           for(q=0;q<len;q++)
           {
               if(p[q]=='1')
                count++;
                if(count>(len)/2)
                    break;
           }
           if(count==(len)/2&&count>max)
            max=len;
            j=j+2;}

        }

    cout<<max;
    return 0;
}


#include <iostream>
#include<string>

using namespace std;

int main()
{
    string s;cin>>s;int max=0;string p="";int count,q,i,l=s.length();int j=0;
    for(i=0;i<l;i++)
    {j=i+2;
        while(j<l)
        {count=0;
           p=s.substr(i,j) ;int len=p.length();
           for(q=0;q<j-i;q++)
           {
               if(s[q]=='1')
                count++;
                if(count>(len)/2)
                    break;
           }
           if(count==(len)/2&&count>max)
            max=len;
            j=j+2;}

        }

    cout<<max;
    return 0;
}


#include <iostream>
#include<string>

using namespace std;

int main()
{int n;cin>>n;
    string s;cin>>s;int max=0;string p="";int count,q,i,l=s.length();int j=0;
    for(i=0;i<l;i++)
    {j=i+2;
        while(j<l)
        {count=0;
           p=s.substr(i,j) ;int len=p.length();
           for(q=0;q<j-i;q++)
           {
               if(s[q]=='1')
                count++;
                if(count>(len)/2)
                    break;
           }
           if(count==(len)/2&&count>max)
            max=len;
            j=j+2;}

        }

    cout<<max;
    return 0;
}


#include <iostream>
#include<algorithm>
#include<string>

using namespace std;

int main()
{
    int n;cin>>n;
    string d;cin>>d;
    char s[n];int i,j,max=0,o=0,z=0;int l=d.length(),t,k;
    string p;
    for(i=0;i<l;i++)
    {
        for(j=i+1;j<l;j++)
        {  o=0;z=0;p=d.substr(i,j+1);
            for(k=0;k<(j-i+1);k++)
            {

                if(p[k]=='1')
                    o++;

                if(p[k]=='0')
                    z++;
                    if(o>=1+l/2||z>=1+l/2)
                        break;
            }
            if(o==z)
            {
                t=o+z;
                if(t>=max){
                    max=t;
                    if(max==l)
                        break;}
            }
            if(max==l)
                break;
        }
        if(max==l)
            break;
    }
    cout<<max;
    return 0;
}


#include <iostream>
#include<algorithm>
#include<string>

using namespace std;

int main()
{
    int n;cin>>n;
    string d;cin>>d;
    char s[n];int i,j,max=0,o=0,z=0;int l=d.length(),t,k;
    string p;
    for(i=0;i<l;i++)
    {
        for(j=i+1;j<l;j++)
        {  o=0;z=0;p=d.substr(i,j+1);
            for(k=0;k<(j-i+1);k++)
            {

                if(p[k]=='1')
                    o++;
                if(p[k]=='0')
                    z++;
            }
            if(o==z)
            {
                t=o+z;
                if(t>=max)
                    max=t;
            }
        }
    }
    cout<<max;
    return 0;
}


#include <iostream>
#include<algorithm>
#include<string>

using namespace std;

int main()
{
    int n;cin>>n;
    string d;cin>>d;
    int i,j,max=0,o=0,z=0;int l=d.length(),t,k;
    string p;
    for(i=0;i<l;i++)
        if(d[i]=='0')
        o++;

    if(o!=l){
    for(i=0;i<l;i++)
    {if((l-i)>max){
        for(j=i+1;j<l;j++)
        {
            o=0;z=0;p=d.substr(i,j+1);
        if(p.length()>max){
            for(k=0;k<(j-i+1);k++)
            {

                if(p[k]=='1')
                    o++;

                else
                    z++;
                    if(o>=1+l/2||z>=1+l/2)
                        break;
            }
            if(o==z)
            {
                t=o+z;
                if(t>=max){
                    max=t;
                    if(max==l)
                        break;}
            }
            if(max==l)
                break;
        }
        }
        if(max==l)
            break;
    }
    }}
    cout<<max;
    return 0;
}


#include <iostream>
#include<algorithm>
#include<string>

using namespace std;

int main()
{
    int n;cin>>n;
    string d;cin>>d;
    char s[n];int i,j,max=0,o,z;int l=d.length(),t;
    for(i=0;i<l;i++)
        s[i]=d[i];
    for(i=0;i<l;i++)
    {
        for(j=i+1;j<l;j++)
        {
            o=count(s+i,s+j,'1');
            z=count(s+i,s+j,'0');
            if(o==z)
            {
                t=o+z;
                if(t>=max)
                    max=t;
            }
        }
    }
    cout<<max;
    return 0;
}


#include <iostream>
#include<algorithm>
#include<string>

using namespace std;

int main()
{
    int n;cin>>n;
    string d;cin>>d;
    ;int i,j,max=0,o=0,z=0;int l=d.length(),t,k;
    string p;
    for(i=0;i<l;i++)
        if(d[i]=='0')
        z++;
    else
        o++;

    if(o!=l||z!=l){
    for(i=0;i<l;i++)
    {if((l-i)>max){
        for(j=i+1;j<l;j++)
        {
            o=0;z=0;p=d.substr(i,j+1);
        if(p.length()>max){
            for(k=0;k<(j-i+1);k++)
            {

                if(p[k]=='1')
                    o++;

                else
                    z++;
                    if(o>=1+l/2||z>=1+l/2)
                        break;
            }
            if(o==z)
            {
                t=o+z;
                if(t>=max){
                    max=t;
                    if(max==l)
                        break;}
            }
            if(max==l)
                break;
        }
        }
        if(max==l)
            break;
    }
    }}
    cout<<max;
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;

    int32_t main(){
     int n; cin>>n;
     string s; cin>>s;
     map <int,int> f;
     int ans = 0, curr=0;
     f[0] = -1;
     set <int> Set;
     for(int i=0 ; s[i] ; i++){
        if(s[i]=='1') curr++;
        else  curr--;
        if(Set.find(curr)!=Set.end()) ans = max(ans,i-f[curr]);
        else {f[curr] = i; Set.insert(curr);}
     }

       cout<<ans;
  }


#include<bits/stdc++.h>
#define int long long
using namespace std;

  bool check(string s, int mid){
    map <char,int> m;
    for(int i=0 ; i<mid ; i++) m[s[i]]++;
    if(m['0']==m['1']) return true;
    for(int i=mid ; s[i] ; i++){
       m[s[i]]++;
       m[s[i-mid]]--;
       if(m['0']==m['1']) return true;
    }
       return false;
  }
   int32_t main(){
     int n; cin>>n;
     string s; cin>>s;
     int ans = 0;
     for(int l=2, r=n, mid = (l+r)/2 ; l<=r ; mid = (l+r)/2){
        if(check(s,mid)) {ans = mid; l = mid+1;}
        else r = mid-1;
     }
       cout<<ans;
  }


#include<bits/stdc++.h>
#define int long long
using namespace std;

    int32_t main(){
     int n; cin>>n;
     string s; cin>>s;
     map <char,int> m;
     map <int,int> f;
     int ans = 0, curr=0;
     for(int i=0 ; s[i] ; i++){
        if(s[i]=='1') curr++;
        else  curr--;
        if(f[curr]) ans = max(ans,i-f[curr]);
        else f[curr] = i;
     }
       cout<<ans;
  }


#include<bits/stdc++.h>
#define int long long
using namespace std;

    int32_t main(){
     int n; cin>>n;
     string s; cin>>s;
     map <char,int> m;
     map <int,int> f;
     int ans = 0;
     for(int i=0 ; s[i] ; i++){
        if(s[i]=='1') {m['1']++; f[m['1']] = i;}
        else{
          m['0']--;
          ans = max(ans,i-f[m[0]]);
        }
     }
       cout<<ans;
  }


#include<bits/stdc++.h>
#define int long long
using namespace std;

    int32_t main(){
     int n; cin>>n;
     string s; cin>>s;
     map <char,int> m;
     map <int,int> f;
     int ans = 0, curr=0;
     f[0] = -1;
     for(int i=0 ; s[i] ; i++){
        if(s[i]=='1') curr++;
        else  curr--;
        if(f[curr]) ans = max(ans,i-f[curr]);
        else f[curr] = i;
     }
       cout<<ans;
  }


#include<bits/stdc++.h>
#define int long long
using namespace std;

    int32_t main(){
     int n; cin>>n;
     string s; cin>>s;
     map <int,int> f;
     int ans = 0, curr=0;
     f[0] = -1;
     set <int> Set;
     Set.insert(0);
     for(int i=0 ; s[i] ; i++){
        if(s[i]=='1') curr++;
        else  curr--;
        if(Set.find(curr)!=Set.end()) ans = max(ans,i-f[curr]);
        else {f[curr] = i; Set.insert(curr);}
     }

       cout<<ans;
  }


#include<bits/stdc++.h>
#define int long long
using namespace std;

  bool check(string s, int mid){
    map <char,int> m;
    for(int i=0 ; i<mid ; i++) m[s[i]]++;
    if(m['0']==m['1']) return true;
    for(int i=mid ; s[i] ; i++){
       m[s[i]]++;
       m[s[i-mid]]--;
       if(m['0']==m['1']) return true;
    }
       return false;
  }
   int32_t main(){
     int n; cin>>n;
     string s; cin>>s;
     int ans = 0;
     for(int l=1, r=n, mid = (l+r)/2 ; l<=r ; mid = (l+r)/2){
        if(check(s,mid)) {ans = mid; l = mid+1;}
        else r = mid-1;
     }
       cout<<ans;
  }


#include "iostream"
#define ll int
using namespace std;

ll p[100001][2];

int main(){
    ll i,n,j,k,lo,hi,mid;
    scanf("%d",&n);
    string s;
    cin>>s;
    p[0][0] = p[0][1] = 0;
    lo = 0;
    hi = n/2;
    for(i=1;i<=n;i++){
        p[i][s[i-1]-'0']++;
        p[i][0] += p[i-1][0];
        p[i][1] += p[i-1][1];
    }
    ll maxi = 0;
    while(lo <= hi){
        mid = (lo+hi)/2;
        mid *= 2;
        for(i=mid;i<=n;i++)
            if(p[i][1]-p[i-mid][1] == p[i][0]-p[i-mid][0])
                break;
        if(lo == hi){
            if(i != n+1)
                maxi = max(maxi,mid);
            break;
        }
        mid /= 2;
        if(i != n+1){
            lo = mid+1;
            maxi = max(maxi,2*mid);
        }
        else
            hi = mid;
    }
    cout<<maxi;
    return 0;
}


#include "iostream"
#define ll int
using namespace std;

ll p[100001][2];

int main(){
    ll i,n,j,k,lo,hi,mid;
    scanf("%d",&n);
    string s;
    cin>>s;
    p[0][0] = p[0][1] = 0;
    lo = 0;
    hi = n;
    for(i=1;i<=n;i++){
        p[i][s[i-1]-'0']++;
        p[i][0] += p[i-1][0];
        p[i][1] += p[i-1][1];
    }
    ll maxi = 0;
    while(lo <= hi){
        mid = (lo+hi)/2;
        k = mid;
        for(i=1;i<=n-k+1;i++){
            j = i+k-1;
            if(p[j][0]-p[i-1][0] == p[j][1]-p[i-1][1])
                break;
        }
        if(lo == hi){
            if(i != n-k+2)
                maxi = max(maxi,mid);
            break;
        }
        if(i != n-k+2){
            lo = mid+1;
            maxi = max(maxi,mid);
        }
        else
            hi = mid;
    }
    cout<<maxi;
    return 0;
}


#include "iostream"
#define ll int
using namespace std;

ll p[100001][2];

int main(){
    ll i,n,j,k,lo,hi,mid;
    scanf("%d",&n);
    string s;
    cin>>s;
    p[0][0] = p[0][1] = 0;
    lo = 0;
    hi = n/2;
    for(i=1;i<=n;i++){
        p[i][s[i-1]-'0']++;
        p[i][0] += p[i-1][0];
        p[i][1] += p[i-1][1];
    }
    ll maxi = 0;
    while(lo <= hi){
        mid = (lo+hi)/2;
        mid *= 2;
        for(i=mid;i<=n;i++)
            if(p[i][1]-p[i-mid][1] == p[i][0]-p[i-mid][0])
                break;
        if(lo == hi or hi-lo == 1){
            if(i != n+1)
                maxi = max(maxi,mid);
            break;
        }
        mid /= 2;
        if(i != n+1){
            lo = mid;
            maxi = max(maxi,2*mid);
        }
        else
            hi = mid;
        //cout<<mid<<endl;
    }
    /*for(i=2;i<=n;i+=2){
        for(j=i;j<=n;j++)
            if(p[j][1]-p[j-i][1] == p[j][0]-p[j-i][0])
                break;
        if(j!=n+1)
            maxi = i;
    }*/
    cout<<maxi;
    return 0;
}


#include "iostream"
#define ll int
using namespace std;

ll p[100001][2];

int main(){
    ll i,n,j,k,lo,hi,mid;
    scanf("%d",&n);
    string s;
    cin>>s;
    p[0][0] = p[0][1] = 0;
    lo = 0;
    hi = n/2;
    for(i=1;i<=n;i++){
        p[i][s[i-1]-'0']++;
        p[i][0] += p[i-1][0];
        p[i][1] += p[i-1][1];
    }
    ll maxi = 0;
    while(lo <= hi){
        mid = (lo+hi)/2;
        mid *= 2;
        for(i=mid;i<=n;i++)
            if(p[i][1]-p[i-mid][1] == p[i][0]-p[i-mid][0])
                break;
        if(lo == hi or hi-lo == 1){
            if(i != n+1)
                maxi = max(maxi,mid);
            break;
        }
        mid /= 2;
        if(i != n+1){
            lo = mid;
            maxi = max(maxi,2*mid);
        }
        else
            hi = mid-1;
        //cout<<mid<<endl;
    }
    /*for(i=2;i<=n;i+=2){
        for(j=i;j<=n;j++)
            if(p[j][1]-p[j-i][1] == p[j][0]-p[j-i][0])
                break;
        if(j!=n+1)
            maxi = i;
    }*/
    cout<<maxi;
    return 0;
}


#include "iostream"
#define ll int
using namespace std;

ll p[100001];

int main(){
    ll i,n,j,k,mx,mn,maxi;
    scanf("%d",&n);
    string s;
    cin>>s;
    p[0] = 1;
    if(s[0] == '0')
        p[0] = -1;
    mn = mx = (s[0]-'0');
    for(i=1;i<n;i++){
        p[i] = 1;
        if(s[i] == '0')
            p[i] = -1;
        p[i] += p[i-1];
        mx = max(mx,p[i]);
        mn = min(mn,p[i]);
    }
    ll hash[mx-mn+1];
    for(i=0;i<mx-mn+1;i++)
        hash[i] = -1;
    maxi = 0;
    //cout<<mn<<" "<<mx<<endl;
    //for(i=0;i<n;i++)
        //cout<<i<<" "<<p[i]<<endl;
    for(i=0;i<n;i++){
        if(p[i] == 0)
            maxi = i+1;
        //cout<<i<<" "<<p[i]-mn<<endl;
        if(hash[p[i]-mn] == -1)
            hash[p[i]-mn] = i;
        else
            maxi = max(maxi,i-hash[p[i]-mn]);
    }
    cout<<maxi;
    return 0;
}


#include "iostream"
#define ll int
using namespace std;

ll p[100001][2];

int main(){
    ll i,n,j,k,lo,hi,mid;
    scanf("%d",&n);
    string s;
    cin>>s;
    p[0][0] = p[0][1] = 0;
    lo = 1;
    hi = n/2;
    for(i=1;i<=n;i++){
        p[i][s[i-1]-'0']++;
        p[i][0] += p[i-1][0];
        p[i][1] += p[i-1][1];
    }
    ll maxi = 0;
    while(lo <= hi){
        mid = (lo+hi)/2;
        mid *= 2;
        for(i=mid;i<=n;i++)
            if(p[i][1]-p[i-mid][1] == p[i][0]-p[i-mid][0])
                break;
        if(lo == hi or hi-lo == 1){
            if(i != n+1)
                maxi = max(maxi,mid);
            break;
        }
        mid /= 2;
        if(i != n+1){
            lo = mid;
            maxi = max(maxi,2*mid);
        }
        else
            hi = mid-1;
        //cout<<mid<<endl;
    }
    /*for(i=2;i<=n;i+=2){
        for(j=i;j<=n;j++)
            if(p[j][1]-p[j-i][1] == p[j][0]-p[j-i][0])
                break;
        if(j!=n+1)
            maxi = i;
    }*/
    cout<<maxi;
    return 0;
}


#include "iostream"
#define ll int
using namespace std;

ll p[100001][2];

int main(){
    ll i,n,j,k,lo,hi,mid;
    scanf("%d",&n);
    string s;
    cin>>s;
    p[0][0] = p[0][1] = 0;
    lo = 1;
    hi = n/2;
    for(i=1;i<=n;i++){
        p[i][s[i-1]-'0']++;
        p[i][0] += p[i-1][0];
        p[i][1] += p[i-1][1];
    }
    ll maxi = 0;
    while(lo <= hi){
        mid = (lo+hi)/2;
        mid *= 2;
        for(i=mid;i<=n;i++)
            if(p[i][1]-p[i-mid][1] == p[i][0]-p[i-mid][0])
                break;
        if(lo == hi or abs(lo-hi) == 1){
            if(i != n+1)
                maxi = max(maxi,mid);
            break;
        }
        mid /= 2;
        if(i != n+1){
            lo = mid;
            maxi = max(maxi,2*mid);
        }
        else
            hi = mid;
        //cout<<mid<<endl;
    }
    /*for(i=2;i<=n;i+=2){
        for(j=i;j<=n;j++)
            if(p[j][1]-p[j-i][1] == p[j][0]-p[j-i][0])
                break;
        if(j!=n+1)
            maxi = i;
    }*/
    cout<<maxi;
    return 0;
}


#include "iostream"
#define ll int
using namespace std;

ll p[100001][2];

int main(){
    ll i,n,j,k,lo,hi,mid;
    scanf("%d",&n);
    string s;
    cin>>s;
    p[0][0] = p[0][1] = 0;
    lo = 1;
    hi = n/2;
    for(i=1;i<=n;i++){
        p[i][s[i-1]-'0']++;
        p[i][0] += p[i-1][0];
        p[i][1] += p[i-1][1];
    }
    ll maxi = 0;
    while(lo <= hi){
        mid = (lo+hi)/2;
        mid *= 2;
        for(i=mid;i<=n;i++)
            if(p[i][1]-p[i-mid][1] == p[i][0]-p[i-mid][0])
                break;
        /*if(lo == hi){
            if(i != n+1)
                maxi = max(maxi,mid);
            break;
        }*/
        mid /= 2;
        if(i != n+1){
            lo = mid+1;
            maxi = max(maxi,2*mid);
        }
        else
            hi = mid-1;
    }
    cout<<maxi;
    return 0;
}


#include "iostream"
#define ll int
using namespace std;

ll p[100001][2];

int main(){
    ll i,n,j,k,lo,hi,mid;
    scanf("%d",&n);
    string s;
    cin>>s;
    p[0][0] = p[0][1] = 0;
    lo = 0;
    hi = n/2;
    for(i=1;i<=n;i++){
        p[i][s[i-1]-'0']++;
        p[i][0] += p[i-1][0];
        p[i][1] += p[i-1][1];
    }
    ll maxi = 0;
    while(lo <= hi){
        mid = (lo+hi)/2;
        mid *= 2;
        for(i=mid;i<=n;i++)
            if(p[i][1]-p[i-mid][1] == p[i][0]-p[i-mid][0])
                break;
        if(lo == hi){
            if(i != n+1)
                maxi = max(maxi,mid);
            break;
        }
        mid /= 2;
        if(i != n+1){
            lo = mid+1;
            maxi = max(maxi,2*mid);
        }
        else
            hi = mid;
        //cout<<mid<<endl;
    }
    /*for(i=2;i<=n;i+=2){
        for(j=i;j<=n;j++)
            if(p[j][1]-p[j-i][1] == p[j][0]-p[j-i][0])
                break;
        if(j!=n+1)
            maxi = i;
    }*/
    cout<<maxi;
    return 0;
}


#include "iostream"
#define ll int
using namespace std;

ll p[100001][2];

int main(){
    ll i,n,j,k,lo,hi,mid;
    scanf("%d",&n);
    string s;
    cin>>s;
    p[0][0] = p[0][1] = 0;
    lo = 0;
    hi = n/2;
    for(i=1;i<=n;i++){
        p[i][s[i-1]-'0']++;
        p[i][0] += p[i-1][0];
        p[i][1] += p[i-1][1];
    }
    ll maxi = 0;
    while(lo <= hi){
        mid = (lo+hi)/2;
        mid *= 2;
        for(i=mid;i<=n;i++)
            if(p[i][1]-p[i-mid][1] == p[i][0]-p[i-mid][0])
                break;
        if(lo == hi){
            if(i != n+1)
                maxi = max(maxi,mid);
            break;
        }
        mid /= 2;
        if(i != n+1){
            lo = mid;
            maxi = max(maxi,2*mid);
        }
        else
            hi = mid-1;
        //cout<<mid<<endl;
    }
    /*for(i=2;i<=n;i+=2){
        for(j=i;j<=n;j++)
            if(p[j][1]-p[j-i][1] == p[j][0]-p[j-i][0])
                break;
        if(j!=n+1)
            maxi = i;
    }*/
    cout<<maxi;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007;
const int N = 2e5 + 5;
int n, ans, o, z;
vector<int> d[N];
char s[N];

int main() {
    scanf("%d\n%s",&n,s);
    for(int i=0;i<n;++i){
        if(s[i]=='1')o++;
        else o--;
        d[o+100000].push_back(i);
    }
    for(int i=0;i<N;++i){
        if(d[i].size()>0){
            sort(d[i].begin(),d[i].end());
            ans=max(ans,d[i][d[i].size()-1]-d[i][0]);
        }
    }
    printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long int n,l,k,z,c,sl,sz,sk;
string p,q,s;
bool ggg, gg;
long long int a[100];
int main()
{
    cin>>n>>s;
    for(int i=0;i<n;i++){
        if((s[i]=='1'&&s[i+1]=='0')||(s[i]=='0'&&s[i+1]=='1'))
        c++;
    }
    cout<<c;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
const int N = 1e5+5;
int n, o, z, c, ans;
char s[N];
vector<pair<int,int> > v;

int main() {
	scanf("%d%s",&n,s);
	for(int i=0;i<n;i++){
		if(s[i]=='0')z++;
		else o++;
		if(i%2)v.push_back({o,z});
	}
	for(int i=2;i<=n;i+=2){
		int one = v[c].first;
		int zero = v[c].second;
		//cout<<one << " " << zero<<endl;
		if(one==zero)ans=i;
		for(int j=1;j<n-1;j++){
			if(s[j-1]=='0')zero--;
			else one--;
			if(s[j+i-1]=='0')zero++;
			else one++;
			//cout<<one<<" "<<j<<" "<< zero<<endl;
			if(one==zero)ans=i;
		}
		c++;
	}
	printf("%d",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long int n,l,k,z,c,sl,sz,sk;
string p,q,s;
bool ggg, gg;
long long int a[100];
int main()
{
    /*cin>>n>>k>>z;
    if(n%2==0)
    c=n/2;
    else
    c=(n/2)+1;
    for(int i=0;i<n;i++){
       cin>>a[i];
       if(i+1<=c)
       l+=a[i];
    }
    cout<<l+k*z;*/
    cin>>n>>s;
    for(int i=0;i<n;i++){
        if((s[i]=='1'&&s[i+1]=='0')||(s[i]=='0'&&s[i+1]=='1'))
        c++;
        else
        c=0;
        if(c>k)
        k=c;
    }
    cout<<k;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
const ll N = 1e6 + 5,MOD = 1000000007;
int n, fr[27], ans, one, zero, o, z, idx1, idx2, c;
string bs;
char s[N];
vector<string> strs;
map<string,int> mp;

int main() {
    scanf("%d%s",&n,s);
    for(int i=0;i<n;++i){
        if(s[i]=='0')o--;
        else o++;
    }
    idx1=-1;
    idx2=n;
    if(o>0){
        for(int i=0;i<n;++i){
            if(s[i]!='1')break;
            idx1=i;
            o--;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
        for(int i=n-1;i>0;--i){
            if(s[i]!='1')break;
            idx2=i;
            o--;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
    }else{
        for(int i=0;i<n;++i){
            if(s[i]!='0')break;
            idx1=i;
            o++;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
        for(int i=n-1;i>0;--i){
            if(s[i]!='0')break;
            idx2=i;
            o++;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
    }
    for(int i=idx1+1;i<idx2;++i){
        o=0;
        int j, r;
        for( j=i,r=i+1;j>idx1&&r<idx2;++r,--j){
            if(s[j]=='1')o++;
            else o--;
            if(o==0)ans=max(ans,r-j+1);
            if(s[r]=='0')o--;
            else o++;
            if(o==0)ans=max(ans,r-j+1);
        }
        o=0;
        for( j=i,r=i+1;j>idx1&&r<idx2;++r,--j){
            if(s[r]=='0')o--;
            else o++;
            if(o==0)ans=max(ans,r-j+1);
            if(s[j]=='1')o++;
            else o--;
            if(o==0)ans=max(ans,r-j+1);
        }

    }
    printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
const ll N = 1e6 + 5,MOD = 1000000007;
int n, fr[27], ans, one, zero, o, z, idx1, idx2, c;
string bs;
char s[N];
vector<string> strs;
map<string,int> mp;

int main() {
    scanf("%d%s",&n,s);
    for(int i=0;i<n;++i){
        if(s[i]=='0')o--;
        else o++;
    }
    if(o>0){
        for(int i=0;i<n;++i){
            if(s[i]!='1')break;
            idx1=i;
            o--;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
        for(int i=n-1;i>0;--i){
            if(s[i]!='1')break;
            idx2=i;
            o--;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
    }else{
        for(int i=0;i<n;++i){
            if(s[i]!='0')break;
            idx1=i;
            o++;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
        for(int i=n-1;i>0;--i){
            if(s[i]!='0')break;
            idx2=i;
            o++;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
    }
    for(int i=0;i<=n;++i){
        o=0;
        int j, r;
        for( j=i,r=i+1;j>=0&&r<n;++r,--j){
            if(s[j]=='1')o++;
            else o--;
            if(o==0)ans=max(ans,r-j+1);
            if(s[r]=='0')o--;
            else o++;
            if(o==0)ans=max(ans,r-j+1);
        }
        o=0;
        for( j=i,r=i+1;j>=0&&r<n;++r,--j){
            if(s[r]=='0')o--;
            else o++;
            if(o==0)ans=max(ans,r-j+1);
            if(s[j]=='1')o++;
            else o--;
            if(o==0)ans=max(ans,r-j+1);
        }

    }
    printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long int n,l,k,z,c,sl,sz,sk;
string p,q,s;
bool ggg, gg;
long long int a[100];
int main()
{
    /*cin>>n>>k>>z;
    if(n%2==0)
    c=n/2;
    else
    c=(n/2)+1;
    for(int i=0;i<n;i++){
       cin>>a[i];
       if(i+1<=c)
       l+=a[i];
    }
    cout<<l+k*z;*/
    cin>>n>>s;
    for(int i=0;i<n;i++){
        if((s[i]=='1'&&s[i+1]=='0')||(s[i]=='0'&&s[i+1]=='1'))
        c++;
        else
        c=0;
        if(c>k)
        k=c;
    }
    if(k!=n-1)
    cout<<k;
    else
    cout<<k+1;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
const ll N = 1e6 + 5,MOD = 1000000007;
int n, fr[27], ans, one, zero, o, z;
string bs;
char s[N];
vector<string> strs;
map<string,int> mp;

int main() {
    scanf("%d%s",&n,s);
    for(int i=0;i<=n;++i){
        o=0;
        int j, r;
        for( j=i,r=i+1;j>=0&&r<n;++r,--j){
            if(s[j]=='1')o++;
            else o--;
            if(o==0)ans=max(ans,r-j+1);
            if(s[r]=='0')o--;
            else o++;
            if(o==0)ans=max(ans,r-j+1);
        }
        for( j=i,r=i+1;j>=0&&r<n;++r,--j){
            if(s[r]=='0')o--;
            else o++;
            if(o==0)ans=max(ans,r-j+1);
            if(s[j]=='1')o++;
            else o--;
            if(o==0)ans=max(ans,r-j+1);
        }

    }
    printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007;
const int N = 2e5 + 5;
int n, ans, o, z;
vector<int> d[N];
char s[N];

int main() {
    scanf("%d\n%s",&n,s);
    for(int i=0;i<n;++i){
        if(s[i]=='1')o++;
        else o--;
        int x=abs(o);
        d[x].push_back(i);

    }
    for(int i=0;i<1e5+5;++i){
        if(d[i].size()>0){
            sort(d[i].begin(),d[i].end());
            ans=max(ans,d[i][d[i].size()-1]-d[i][0]);
        }
    }
    printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
const ll N = 1e6 + 5,MOD = 1000000007;
int n, fr[27], ans, one, zero, o, z, idx1, idx2;
string bs;
char s[N];
vector<string> strs;
map<string,int> mp;

int main() {
    scanf("%d%s",&n,s);
    for(int i=0;i<n;++i){
        if(s[i]=='0')o--;
        else o++;
    }
    if(o>0){
        for(int i=0;i<n;++i){
            if(s[i]!='1')break;
            idx1=i;
            o--;
            if(o==0)return printf("%d",n-i-1),0;
        }
        for(int i=n-1;i>0;--i){
            if(s[i]!='1')break;
            idx2=i;
            o--;
            if(o==0)return printf("%d",i-idx1-1),0;
        }
    }else{
        for(int i=0;i<n;++i){
            if(s[i]!='0')break;
            idx1=i;
            o++;
            if(o==0)return printf("%d",n-i-1),0;
        }
        for(int i=n-1;i>0;--i){
            if(s[i]!='0')break;
            idx2=i;
            o++;
            if(o==0)return printf("%d",i-idx1-1),0;
        }
    }
    for(int i=0;i<=n;++i){
        o=0;
        int j, r;
        for( j=i,r=i+1;j>=0&&r<n;++r,--j){
            if(s[j]=='1')o++;
            else o--;
            if(o==0)ans=max(ans,r-j+1);
            if(s[r]=='0')o--;
            else o++;
            if(o==0)ans=max(ans,r-j+1);
        }
        o=0;
        for( j=i,r=i+1;j>=0&&r<n;++r,--j){
            if(s[r]=='0')o--;
            else o++;
            if(o==0)ans=max(ans,r-j+1);
            if(s[j]=='1')o++;
            else o--;
            if(o==0)ans=max(ans,r-j+1);
        }

    }
    printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
const ll N = 1e6 + 5,MOD = 1000000007;
int n, fr[27], ans, one, zero, o, z, idx1, idx2, c;
string bs;
char s[N];
vector<string> strs;
map<string,int> mp;

int main() {
    scanf("%d%s",&n,s);
    for(int i=0;i<n;++i){
        if(s[i]=='0')o--;
        else o++;
    }
    idx1=-1;
    idx2=n;
    if(o>0){
        for(int i=0;i<n;++i){
            if(s[i]!='1')break;
            idx1=i;
            o--;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
        for(int i=n-1;i>0;--i){
            if(s[i]!='1')break;
            idx2=i;
            o--;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
    }else{
        for(int i=0;i<n;++i){
            if(s[i]!='0')break;
            idx1=i;
            o++;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
        for(int i=n-1;i>0;--i){
            if(s[i]!='0')break;
            idx2=i;
            o++;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
    }
    for(int i=idx1+1;i<idx2;++i){
        o=0;
        int j, r;
        for( j=i,r=i+1;j>idx1&&r<idx2;++r,--j){
            if(s[j]=='1')o++;
            else o--;
            if(o==0)ans=max(ans,r-j+1);
            if(s[r]=='0')o--;
            else o++;
            if(o==0)ans=max(ans,r-j+1);
        }
        one=0;
        for( j=i,r=i+1;j>idx1&&r<idx2;++r,--j){
            if(s[r]=='0')one--;
            else one++;
            if(one==0)ans=max(ans,r-j+1);
            if(s[j]=='1')one++;
            else one--;
            if(one==0)ans=max(ans,r-j+1);
        }

    }
    printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007;
const int N = 2e5 + 5;
int n, ans, o, z;
vector<int> d[N];
char s[N];

int main() {
    scanf("%d\n%s",&n,s);
    d[100000].push_back(-1);
    for(int i=0;i<n;++i){
        if(s[i]=='1')o++;
        else z++;
        d[o-z+100000].push_back(i);
    }
    for(int i=0;i<N;++i){
        if(d[i].size()>0){
            sort(d[i].begin(),d[i].end());
            ans=max(ans,d[i][d[i].size()-1]-d[i][0]);
        }
    }
    printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
const ll N = 1e6 + 5,MOD = 1000000007;
int n, fr[27], ans, one, zero, o, z, idx1, idx2, c;
string bs;
char s[N];
vector<string> strs;
map<string,int> mp;

int main() {
    scanf("%d%s",&n,s);
    for(int i=0;i<n;++i){
        if(s[i]=='0')o--;
        else o++;
    }
    idx1=-1;
    idx2=n;
    if(o>0){
        for(int i=0;i<n;++i){
            if(s[i]!='1')break;
            idx1=i;
            o--;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
        for(int i=n-1;i>0;--i){
            if(s[i]!='1')break;
            idx2=i;
            o--;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
    }else{
        for(int i=0;i<n;++i){
            if(s[i]!='0')break;
            idx1=i;
            o++;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
        for(int i=n-1;i>0;--i){
            if(s[i]!='0')break;
            idx2=i;
            o++;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
    }
    for(int i=idx1+1;i<idx2;++i){
        o=0;
        int j, r;
        for( j=i,r=i+1;j>idx1&&r<idx2;++r,--j){
            if(s[j]=='1')o++;
            else o--;
            if(o==0)ans=max(ans,r-j+1);
            if(s[r]=='0')o--;
            else o++;
            if(o==0)ans=max(ans,r-j+1);
        }
        one=o;
        for( j=i,r=i+1;j>idx1&&r<idx2;++r,--j){
            if(s[r]=='0')one--;
            else one++;
            if(one==0)ans=max(ans,r-j+1);
            if(s[j]=='1')one++;
            else one--;
            if(one==0)ans=max(ans,r-j+1);
        }

    }
    printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
const ll N = 1e6 + 5,MOD = 1000000007;
int n, fr[27], ans, one, zero, o, z, idx1, idx2, c;
string bs;
char s[N];
vector<string> strs;
map<string,int> mp;

int main() {
    scanf("%d%s",&n,s);
    for(int i=0;i<n;++i){
        if(s[i]=='0')o--;
        else o++;
    }
    idx1=-1;
    idx2=n;
    if(o>0){
        for(int i=0;i<n;++i){
            if(s[i]!='1')break;
            idx1=i;
            o--;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
        for(int i=n-1;i>0;--i){
            if(s[i]!='1')break;
            idx2=i;
            o--;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
    }else{
        for(int i=0;i<n;++i){
            if(s[i]!='0')break;
            idx1=i;
            o++;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
        for(int i=n-1;i>0;--i){
            if(s[i]!='0')break;
            idx2=i;
            o++;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
    }
    one=o;
    for(int i=idx1+1;i<idx2;++i){
        int j, r;
        for( j=i,r=i+1;j>idx1&&r<idx2;++r,--j){
            if(s[j]=='1')o++;
            else o--;
            if(o==0)ans=max(ans,r-j+1);
            if(s[r]=='0')o--;
            else o++;
            if(o==0)ans=max(ans,r-j+1);
        }
        for( j=i,r=i+1;j>idx1&&r<idx2;++r,--j){
            if(s[r]=='0')one--;
            else one++;
            if(one==0)ans=max(ans,r-j+1);
            if(s[j]=='1')one++;
            else one--;
            if(one==0)ans=max(ans,r-j+1);
        }

    }
    printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
const ll N = 1e6 + 5,MOD = 1000000007;
int n, fr[27], ans, one, zero, o, z, idx1, idx2, c;
string bs;
char s[N];
vector<string> strs;
map<string,int> mp;

int main() {
    scanf("%d%s",&n,s);
    for(int i=0;i<n;++i){
        if(s[i]=='0')o--;
        else o++;
    }
    if(o>0){
        for(int i=0;i<n;++i){
            if(s[i]!='1')break;
            idx1=i;
            o--;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
        for(int i=n-1;i>0;--i){
            if(s[i]!='1')break;
            idx2=i;
            o--;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
    }else{
        for(int i=0;i<n;++i){
            if(s[i]!='0')break;
            idx1=i;
            o++;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
        for(int i=n-1;i>0;--i){
            if(s[i]!='0')break;
            idx2=i;
            o++;
            c++;
            if(o==0)return printf("%d",n-c),0;
        }
    }
    for(int i=idx1;i<=idx2;++i){
        o=0;
        int j, r;
        for( j=i,r=i+1;j>=idx1&&r<=idx2;++r,--j){
            if(s[j]=='1')o++;
            else o--;
            if(o==0)ans=max(ans,r-j+1);
            if(s[r]=='0')o--;
            else o++;
            if(o==0)ans=max(ans,r-j+1);
        }
        o=0;
        for( j=i,r=i+1;j>=idx1&&r<idx2;++r,--j){
            if(s[r]=='0')o--;
            else o++;
            if(o==0)ans=max(ans,r-j+1);
            if(s[j]=='1')o++;
            else o--;
            if(o==0)ans=max(ans,r-j+1);
        }

    }
    printf("%d",ans);
    return 0;
}