#include <iostream>
#include <vector>
#define int long long int
using namespace std;

const int maxN = 1e5 + 326;
int pa[maxN], wei[maxN], has[maxN], need[maxN], N, isLeaf[maxN], stillneed[maxN];
vector<int> chi[maxN];

void dfs(int u = 0){
	for(int v : chi[u]){
		dfs(v);
		if(stillneed[v]){
			has[u] -= stillneed[v] * wei[v];
		}
	}
	//cout << "has[" << u << "] = " << has[u] << endl;
	if(has[u] >= need[u]){
		stillneed[u] = 0;
		if(pa[u] != u)
			has[pa[u]] += has[u];
	} else {
		stillneed[u] = need[u] - has[u];
	}
}

signed main(){
	cin >> N;
	fill(isLeaf, isLeaf + N, true);
	for(int i = 0; i < N; i++) cin >> has[i];
	for(int i = 0; i < N; i++) cin >> need[i];
	for(int i = 1; i < N; i++){
		cin >> pa[i] >> wei[i];
		pa[i]--;
		chi[pa[i]].push_back(i);
		isLeaf[pa[i]] = false;
	}
	dfs();
	if(has[0] >= need[0]){
		cout << "YES\n";
	} else {
		cout << "NO\n";
	}
}


#include <iostream>
#include <vector>
#define int long long int
using namespace std;

const int maxN = 1e5 + 326;
int pa[maxN], wei[maxN], has[maxN], need[maxN], N, isLeaf[maxN], stillneed[maxN];
vector<int> chi[maxN];

void dfs(int u = 0){
	for(int v : chi[u]){
		dfs(v);
		if(stillneed[v]){
			//cout << "YES: " << v << endl;
			has[u] -= stillneed[v] * wei[v];
		}
	}
	//cout << "has[" << u << "] = " << has[u] << endl;
	if(has[u] >= need[u]){
		stillneed[u] = 0;
		if(pa[u] != u)
			has[pa[u]] += has[u] - need[u];
	} else {
		stillneed[u] = need[u] - has[u];
		//cout << "sn[" << u << "] = " << stillneed[u] << endl;
		//cout << "wei[" << u << "] = " << wei[u] << endl;
	}
}

signed main(){
	cin >> N;
	fill(isLeaf, isLeaf + N, true);
	for(int i = 0; i < N; i++) cin >> has[i];
	for(int i = 0; i < N; i++) cin >> need[i];
	for(int i = 1; i < N; i++){
		cin >> pa[i] >> wei[i];
		pa[i]--;
		chi[pa[i]].push_back(i);
		isLeaf[pa[i]] = false;
	}
	dfs();
	if(has[0] >= need[0]){
		cout << "YES\n";
	} else {
		cout << "NO\n";
	}
}


#include <iostream>
#include <vector>
#define int long long int
using namespace std;

const int maxN = 1e5 + 326;
int pa[maxN], wei[maxN], has[maxN], need[maxN], N, isLeaf[maxN], stillneed[maxN];
bool isViable[maxN];
vector<int> chi[maxN];

void dfs(int u = 0){
	for(int v : chi[u]){
		dfs(v);
		if(!isViable[v]){
			isViable[u] = false;
			return;
		}
		if(stillneed[v]){
			//cout << "YES: " << v << endl;
			if(stillneed[v] >= ((int)1e17 + has[u] - need[u] + wei[v] - 1) / wei[v]){
				isViable[u] = false;
				return;
			}
			has[u] -= stillneed[v] * wei[v];
			has[v] += stillneed[v];
		}
	}
	//cout << "has[" << u << "] = " << has[u] << endl;
	if(has[u] >= need[u]){
		stillneed[u] = 0;
		if(pa[u] != u)
			has[pa[u]] += has[u] - need[u];
		has[u] = need[u];
	} else {
		stillneed[u] = need[u] - has[u];
	}
}

signed main(){
	cin >> N;
	fill(isLeaf, isLeaf + N, true);
	for(int i = 0; i < N; i++) cin >> has[i];
	for(int i = 0; i < N; i++) cin >> need[i];
	for(int i = 1; i < N; i++){
		cin >> pa[i] >> wei[i];
		pa[i]--;
		chi[pa[i]].push_back(i);
		isLeaf[pa[i]] = false;
	}
	dfs();
	cout << (has[0] >= need[0] ? "YES" : "NO") << endl;
}


#include <iostream>
#include <vector>
#define int __int128_t
using namespace std;

const int maxN = 1e5 + 326;
int pa[maxN], wei[maxN], has[maxN], need[maxN], N, isLeaf[maxN], stillneed[maxN];
vector<int> chi[maxN];

void dfs(int u = 0){
	for(int v : chi[u]){
		dfs(v);
		if(stillneed[v]){
			//cout << "YES: " << v << endl;
			has[u] -= stillneed[v] * wei[v];
			has[v] += stillneed[v];
		}
	}
	//cout << "has[" << u << "] = " << has[u] << endl;
	if(has[u] >= need[u]){
		stillneed[u] = 0;
		if(pa[u] != u)
			has[pa[u]] += has[u] - need[u];
		has[u] = need[u];
	} else {
		stillneed[u] = need[u] - has[u];
	}
}

signed main(){
	cin >> N;
	fill(isLeaf, isLeaf + N, true);
	for(int i = 0; i < N; i++) cin >> has[i];
	for(int i = 0; i < N; i++) cin >> need[i];
	for(int i = 1; i < N; i++){
		cin >> pa[i] >> wei[i];
		pa[i]--;
		chi[pa[i]].push_back(i);
		isLeaf[pa[i]] = false;
	}
	dfs();
	cout << (has[0] >= need[0] ? "YES" : "NO") << endl;
}


#include <iostream>
#include <vector>
#define int __int128
using namespace std;

const int maxN = 1e5 + 326;
int pa[maxN], wei[maxN], has[maxN], need[maxN], N, isLeaf[maxN], stillneed[maxN];
vector<int> chi[maxN];

void dfs(int u = 0){
	for(int v : chi[u]){
		dfs(v);
		if(stillneed[v]){
			//cout << "YES: " << v << endl;
			has[u] -= stillneed[v] * wei[v];
			has[v] += stillneed[v];
		}
	}
	//cout << "has[" << u << "] = " << has[u] << endl;
	if(has[u] >= need[u]){
		stillneed[u] = 0;
		if(pa[u] != u)
			has[pa[u]] += has[u] - need[u];
		has[u] = need[u];
	} else {
		stillneed[u] = need[u] - has[u];
	}
}

signed main(){
	cin >> N;
	fill(isLeaf, isLeaf + N, true);
	for(int i = 0; i < N; i++) cin >> has[i];
	for(int i = 0; i < N; i++) cin >> need[i];
	for(int i = 1; i < N; i++){
		cin >> pa[i] >> wei[i];
		pa[i]--;
		chi[pa[i]].push_back(i);
		isLeaf[pa[i]] = false;
	}
	dfs();
	cout << (has[0] >= need[0] ? "YES" : "NO") << endl;
}


#include <iostream>
#include <vector>
#define int long long int
using namespace std;

const int maxN = 1e5 + 326;
int pa[maxN], wei[maxN], has[maxN], need[maxN], N, isLeaf[maxN], stillneed[maxN];
vector<int> chi[maxN];

void dfs(int u = 0){
	int take = 0;
	for(int v : chi[u]){
		dfs(v);
		if(stillneed[v]){
			has[u] -= stillneed[v] * wei[v];
			take += stillneed[v] * wei[v];
		}
	}
	has[u] -= take;
	//cout << "has[" << u << "] = " << has[u] << endl;
	if(has[u] >= need[u]){
		stillneed[u] = 0;
		if(pa[u] != u)
			has[pa[u]] += has[u];
	} else {
		stillneed[u] = need[u] - has[u];
	}
}

signed main(){
	cin >> N;
	fill(isLeaf, isLeaf + N, true);
	for(int i = 0; i < N; i++) cin >> has[i];
	for(int i = 0; i < N; i++) cin >> need[i];
	for(int i = 1; i < N; i++){
		cin >> pa[i] >> wei[i];
		pa[i]--;
		chi[pa[i]].push_back(i);
		isLeaf[pa[i]] = false;
	}
	dfs();
	if(has[0] >= need[0]){
		cout << "YES\n";
	} else {
		cout << "NO\n";
	}
}


#include <iostream>
#include <vector>
#define int long long int
using namespace std;

const int maxN = 1e5 + 326;
int pa[maxN], wei[maxN], has[maxN], need[maxN], N, isLeaf[maxN], stillneed[maxN];
vector<int> chi[maxN];

void dfs(int u = 0){
	for(int v : chi[u]){
		dfs(v);
		if(stillneed[v]){
			//cout << "YES: " << v << endl;
			has[u] -= stillneed[v] * wei[v];
			has[v] += stillneed[v];
		}
	}
	//cout << "has[" << u << "] = " << has[u] << endl;
	if(has[u] >= need[u]){
		stillneed[u] = 0;
		if(pa[u] != u)
			has[pa[u]] += has[u] - need[u];
		has[u] = need[u];
	} else {
		stillneed[u] = need[u] - has[u];
	}
}

signed main(){
	cin >> N;
	fill(isLeaf, isLeaf + N, true);
	for(int i = 0; i < N; i++) cin >> has[i];
	for(int i = 0; i < N; i++) cin >> need[i];
	for(int i = 1; i < N; i++){
		cin >> pa[i] >> wei[i];
		pa[i]--;
		chi[pa[i]].push_back(i);
		isLeaf[pa[i]] = false;
	}
	dfs();
	bool f = true;
	for(int i = 0; i < N; i++){
		f &= (has[i] >= need[i];
	}
	cout << (f ? "YES" : "NO") << endl;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	long long n;
	cin>>n;
	vector<long long>vi;
	long long ans = 0;
	for(int i = 0; i <n ; i++)
	{
		long long x ; cin>>x;vi.push_back(x);
	}
	sort(vi.begin() , vi.end());
	for(int i = 0; i < n ; i++){
		if(i > 2)
			ans+=vi[i]*2;
		else
			ans+=vi[i];
	}
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int a[ 2000000 ]; ll b;

int main()
{
    ios_base::sync_with_stdio(false), cin.tie( nullptr ), cout.tie( nullptr );

    const auto read_int = [] () { int x; cin >> x; return x; }; int n = read_int();

	for( int i = 0; i < n; i++ )
	    a[ i ] = read_int();

	sort( a, a + n ), reverse( a, a + n );

	for( int i = 0, u = 1; u <= n; i++, u <<= 2 )
	    for( int j = 0, v = n / u; j < v; j++ )
	        b += a[ j ];

	cout << b;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int a[ 2000000 ];

int main()
{
    const auto read_int = [] ()
    {
	    int c = getchar();

	    while ( !isdigit(c) )
	        c = getchar();

	    int x = c - '0', y = x << 1;

	    for( c = getchar(); isdigit(c); c = getchar() )
	        x = c - '0', x += y, x += y << 2, y = x << 1;

	    return x;
    };

    int n = read_int(); ll b = 0;

	for( int i = 0; i < n; i++ )
	    a[ i ] = read_int();

	sort( a, a + n ), reverse( a, a + n );

	for( int i = 0, u = 1; u <= n; i++, u <<= 2 )
	    for( int j = 0, v = n / u; j < v; j++ )
	        b += a[ j ];

	printf( "%I64i", b );
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()
{
    ios_base::sync_with_stdio(false), cin.tie( nullptr ), cout.tie( nullptr );

    const auto read_int = [] () { int x; cin >> x; return x; }; int n = read_int();

    vector< int > a; map< int, int > b; ll c = 0;

	for( int i = 0; i < n; i++ )
	    a.push_back( read_int() );

	sort( a.begin(), a.end() ), reverse( a.begin(), a.end() );

	for( int i = 0, u = 1; u <= n; i++, u <<= 2 )
	    for( int j = 0, v = n / u; j < v; j++ )
	        b[ a[ j ] ]++;

	for( auto p: b )
	    c += ll( p.first ) * ll( p.second );

	cout << c;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int a[ 2000000 ], n; ll b;

int main()
{
    ios_base::sync_with_stdio(false), cin.tie( nullptr ), cout.tie( nullptr ), cin >> n;

	for( int i = 0; i < n; i++ )
	    cin >> a[ i ];

	sort( a, a + n ), reverse( a, a + n );

	for( int i = 0, u = 1; u <= n; i++, u <<= 2 )
	    for( int j = 0, v = n / u; j < v; j++ )
	        b += a[ j ];

	cout << b;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()
{
    ios_base::sync_with_stdio(false), cin.tie( nullptr ), cout.tie( nullptr );

    const auto read_int = [] () { int x; cin >> x; return x; }; int n = read_int();

    vector< int > a; ll c = 0;

	for( int i = 0; i < n; i++ )
	    a.push_back( read_int() );

	sort( a.begin(), a.end() ), reverse( a.begin(), a.end() );

	for( int i = 0, u = 1; u <= n; i++, u <<= 2 )
	    for( int j = 0, v = n / u; j < v; j++ )
	        c += a[ j ];

	cout << c;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int a[ 2000000 ]; ll b;

int main()
{
    ios_base::sync_with_stdio(false), cin.tie( nullptr ), cout.tie( nullptr );

    const auto read_int = [] () { int x; cin >> x; return x; }; int n = read_int();

	for( int i = 0; i < n; i++ )
	    a[ i ] = read_int();

	sort( a, a + n, greater<int>() );

	for( int i = 0, u = 1; u <= n; i++, u <<= 2 )
	    for( int j = 0, v = n / u; j < v; j++ )
	        b += a[ j ];

	cout << b;
}


#include <bits/stdc++.h>

using namespace std;

int read_int()
{
    register int c = getchar();

    while ( !isdigit(c) )
	    c = getchar();

    register int x = c - '0', y = x << 1;

    for( c = getchar(); isdigit(c); c = getchar() )
	    x = c - '0', x += y, x += y << 2, y = x << 1;

	    return x;
}

int a[ 2000000 ];

int main()
{
    register int n = read_int(); register long long b = 0;

	for( register int i = 0; i < n; i++ )
	    a[ i ] = read_int();

	sort( a, a + n ), reverse( a, a + n );

	for( register int i = 0, u = 1; u <= n; i++, u <<= 2 )
	    for( register int j = 0, v = n / u; j < v; j++ )
	        b += a[ j ];

	printf( "%I64i", b );
}


/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
*/

#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define pi acos(-1.0)
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()

//#define inf 1<<30

// isupper() -> upper case check
// tolower() -> convert upper to lower case
// idx = str.find("bear", pos)) != string::npos =>substring find
// Tuple access -> get<0>(v[i])
// cmnt - ctrl []
// stoi("...") -> decimel

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using tpl = tuple<int, int, int>;
using pcc = pair<char, char>;

const int inf = 2e9;
const int mod = 1e9+7;

int dr[] = {-1, 0, 1,  0};// up, right, down, left
int dc[] = { 0, 1, 0, -1};

// King moves similar to 8 direction
//int dr[] = {-1, -1, 0, 1, 1,  1, 0, -1};
//int dc[] = { 0,  1, 1, 1, 0, -1,-1, -1};

// Knight moves
int kr[] = {-1,  1, -2, -2, -1, 1,  2,  2};
int kc[] = {-2, -2, -1,  1,  2, 2, -1,  1};
// diagonal moves
int dgr[] = {-1, -1,  1, 1};
int dgc[] = { 1, -1, -1, 1};

int main()
{
    int n;
    cin>>n;
    if(n/4)
        n = n*n;

    vector<int>v(n);
    for(int i=0; i<n; i++) cin>>v[i];

    if(n == 1){
        cout << v[0] <<endl;
    }else {
        sort(rall(v));
        ll ans = 0;
        for(int i=0; i<n/4; i++) ans += v[i];
        cout << ans+v[0] <<endl;
    }

    return 0;
}


/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
*/

#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define pi acos(-1.0)
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()

//#define inf 1<<30

// isupper() -> upper case check
// tolower() -> convert upper to lower case
// idx = str.find("bear", pos)) != string::npos =>substring find
// Tuple access -> get<0>(v[i])
// cmnt - ctrl []
// stoi("...") -> decimel

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using tpl = tuple<int, int, int>;
using pcc = pair<char, char>;

const int inf = 2e9;
const int mod = 1e9+7;

int dr[] = {-1, 0, 1,  0};// up, right, down, left
int dc[] = { 0, 1, 0, -1};

// King moves similar to 8 direction
//int dr[] = {-1, -1, 0, 1, 1,  1, 0, -1};
//int dc[] = { 0,  1, 1, 1, 0, -1,-1, -1};

// Knight moves
int kr[] = {-1,  1, -2, -2, -1, 1,  2,  2};
int kc[] = {-2, -2, -1,  1,  2, 2, -1,  1};
// diagonal moves
int dgr[] = {-1, -1,  1, 1};
int dgc[] = { 1, -1, -1, 1};

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

    vector<ll>v(n);
    for(int i=0; i<n; i++) scanf("%lld", &v[i]);

    if(n == 1){
        cout << v[0] <<endl;
    }else {
        sort(rall(v));
        ll ans = n;
        int k = 0;

        ans = 0;
        //printf("k %d\n", k);
        while(n != 1) {
            for(int j=0; j<n; j++){
                ans += v[j];
            }
            if(n == 4)
                ans += v[0];
            n/=4;
        }
        cout << ans <<endl;
    }

    return 0;
}


/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
*/

#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define pi acos(-1.0)
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()

//#define inf 1<<30

// isupper() -> upper case check
// tolower() -> convert upper to lower case
// idx = str.find("bear", pos)) != string::npos =>substring find
// Tuple access -> get<0>(v[i])
// cmnt - ctrl []
// stoi("...") -> decimel

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using tpl = tuple<int, int, int>;
using pcc = pair<char, char>;

const int inf = 2e9;
const int mod = 1e9+7;

int dr[] = {-1, 0, 1,  0};// up, right, down, left
int dc[] = { 0, 1, 0, -1};

// King moves similar to 8 direction
//int dr[] = {-1, -1, 0, 1, 1,  1, 0, -1};
//int dc[] = { 0,  1, 1, 1, 0, -1,-1, -1};

// Knight moves
int kr[] = {-1,  1, -2, -2, -1, 1,  2,  2};
int kc[] = {-2, -2, -1,  1,  2, 2, -1,  1};
// diagonal moves
int dgr[] = {-1, -1,  1, 1};
int dgc[] = { 1, -1, -1, 1};

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

    vector<ll>v(n);
    for(int i=0; i<n; i++) cin>>v[i];

    if(n == 1){
        cout << v[0] <<endl;
    }else {
        sort(rall(v));
        ll ans = n;
        int k = 0;
        while(ans != 1){
            k++; ans/=4;
        }
        ans = 0;
        //printf("k %d\n", k);
        for(int i=0; i<k; i++){
            for(int j=0; j<n; j++){
                ans += v[j];
            }
            if(n == 4)
                ans += v[0];
            n/=4;
        }
        cout << ans <<endl;
    }

    return 0;
}


/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
*/

#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define pi acos(-1.0)
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()

//#define inf 1<<30

// isupper() -> upper case check
// tolower() -> convert upper to lower case
// idx = str.find("bear", pos)) != string::npos =>substring find
// Tuple access -> get<0>(v[i])
// cmnt - ctrl []
// stoi("...") -> decimel

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using tpl = tuple<int, int, int>;
using pcc = pair<char, char>;

const int inf = 2e9;
const int mod = 1e9+7;

int dr[] = {-1, 0, 1,  0};// up, right, down, left
int dc[] = { 0, 1, 0, -1};

// King moves similar to 8 direction
//int dr[] = {-1, -1, 0, 1, 1,  1, 0, -1};
//int dc[] = { 0,  1, 1, 1, 0, -1,-1, -1};

// Knight moves
int kr[] = {-1,  1, -2, -2, -1, 1,  2,  2};
int kc[] = {-2, -2, -1,  1,  2, 2, -1,  1};
// diagonal moves
int dgr[] = {-1, -1,  1, 1};
int dgc[] = { 1, -1, -1, 1};

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

    vector<ll>v(n);
    for(int i=0; i<n; i++) cin>>v[i];

    if(n == 1){
        cout << v[0] <<endl;
    }else {
        sort(rall(v));
        ll ans = 0;
        for(int i=0; i<n; i++) ans += v[i];// cout << v[i]<<endl;
        for(int i=0; i<(n/4); i++) ans += v[i];
        while(n!=4) ans+=v[0], n/=4;

        cout << ans <<endl;
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")

#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define pi acos(-1.0)
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()

//#define inf 1<<30

// isupper() -> upper case check
// tolower() -> convert upper to lower case
// idx = str.find("bear", pos)) != string::npos =>substring find
// Tuple access -> get<0>(v[i])
// cmnt - ctrl []
// stoi("...") -> decimel

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using tpl = tuple<int, int, int>;
using pcc = pair<char, char>;

const int inf = 2e9;
const int mod = 1e9+7;

int dr[] = {-1, 0, 1,  0};// up, right, down, left
int dc[] = { 0, 1, 0, -1};

// King moves similar to 8 direction
//int dr[] = {-1, -1, 0, 1, 1,  1, 0, -1};
//int dc[] = { 0,  1, 1, 1, 0, -1,-1, -1};

// Knight moves
int kr[] = {-1,  1, -2, -2, -1, 1,  2,  2};
int kc[] = {-2, -2, -1,  1,  2, 2, -1,  1};
// diagonal moves
int dgr[] = {-1, -1,  1, 1};
int dgc[] = { 1, -1, -1, 1};

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

    vector<ll>v(n);
    for(int i=0; i<n; i++) cin>>v[i];

    if(n == 1){
        cout << v[0] <<endl;
    }else {
        sort(rall(v));
        ll ans = n;
        int k = 0;
        while(ans != 1){
            k++; ans/=4;
        }
        ans = 0;
        //printf("k %d\n", k);
        for(int i=0; i<k; i++){
            for(int j=0; j<n; j++){
                ans += v[j];
            }
            if(n == 4)
                ans += v[0];
            n/=4;
        }
        cout << ans <<endl;
    }

    return 0;
}


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

typedef long long ll;
typedef signed long long sll;

ll ans = 0;
void solve(vector <ll>& v, int n, ll max){
    if (n == 0) return;
    ans += v[n-1];
    ans += max;
    solve(v,n-1, v[n-1]);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    ll n;
    cin >> n;
    vector <ll> v(n,0);
    ll  s = 0;
    for (int i = 0; i < n; ++i) cin >> v[i], s+=v[i];
    sort(v.begin(), v.end());
    int a = n, b = n;
    int cnt = 1;
    while (n / 4 > 1){
        for (int i = 0; i < b/4; ++i) ans += v[b-i-1];
        n/=4;
        cnt++;
    }
    if(a==1) cout << s;
    else if(cnt==1) cout << ans + s + cnt*v[a-1];
    else cout << ans + s + (cnt-1)*v[a-1];

}


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

typedef long long ll;
typedef signed long long sll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    ll n;
    cin >> n;
    vector <ll> v(n,0);
    ll ans = 0, s = 0;
    for (int i = 0; i < n; ++i) cin >> v[i], s+=v[i];
    sort(v.begin(), v.end());
    for (int i = 0; i < n/4; ++i){
        ans += v[n-i-1];
    }
    int j = n-1;
    while (n/4 > 1){
        ans += v[j--];
        n/=4;
    }
     cout << s + ans;
}


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

typedef long long ll;
typedef signed long long sll;

ll ans = 0;
void solve(vector <ll>& v, int n, ll max){
    if (n == 0) return;
    ans += v[n-1];
    ans += max;
    solve(v,n-1, v[n-1]);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    ll n;
    cin >> n;
    vector <ll> v(n,0);
    ll  s = 0;
    for (int i = 0; i < n; ++i) cin >> v[i], s+=v[i];
    sort(v.begin(), v.end());
    int a = n;
    while (n / 4 > 1){
        for (int i = 0; i < a/4; ++i) ans += v[n-i-1];
        ans += v[a-1];
        n/=4;
    }
    if(n==1) cout << s;
    else cout << ans + s;

}


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

typedef long long ll;
typedef signed long long sll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    ll n;
    cin >> n;
    vector <ll> v(n,0);
    ll ans = 0, s = 0;
    for (int i = 0; i < n; ++i) cin >> v[i], s+=v[i];
    sort(v.begin(), v.end());
    int j = n/4;
    for (int i = 0; i < n/4; ++i){
        //ans += v[n-1];
        ans += v[n-i-1];
    }
    if (n > 1) cout << s;
    else cout << s + ans + v[n-1];
}


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

typedef long long ll;
typedef signed long long sll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    ll n;
    cin >> n;
    vector <ll> v(n,0);
    for (int i = 0; i < n; ++i) cin >> v[i];
    sort(v.rbegin(), v.rend());
    ll s = 0;
    for (int i = 1; i <= n; i*=4){
        s += accumulate(v.begin(), v.begin()+i, 0LL);
    }
    cout << s;

}


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

typedef long long ll;
typedef signed long long sll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    ll n;
    cin >> n;
    vector <ll> v(n,0);
    ll ans = 0, s = 0;
    for (int i = 0; i < n; ++i) cin >> v[i], s+=v[i];
    sort(v.begin(), v.end());
    for (int i = 0; i < n/4; ++i){
        ans += v[n-i-1];
    }
    if (n == 1) cout << s;
    else if (n == 4) cout << s + ans;
    else cout << s + ans + v[n-1];
}


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

typedef long long ll;
typedef signed long long sll;

ll ans = 0;
void solve(vector <ll>& v, int n, ll max){
    if (n == 0) return;
    ans += v[n-1];
    ans += max;
    solve(v,n-1, v[n-1]);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    ll n;
    cin >> n;
    vector <ll> v(n,0);
    ll  s = 0;
    for (int i = 0; i < n; ++i) cin >> v[i], s+=v[i];
    sort(v.begin(), v.end());
    int a = n;
    while (n / 4 > 1){
        for (int i = 0; i < a/4; ++i) ans += v[n-i-1];
        n/=4;
    }
    if(n==1) cout << s;
    else cout << ans + s + v[v.size()-1];

}


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

typedef long long ll;
typedef signed long long sll;

ll ans = 0;
void solve(vector <ll>& v, int n, ll max){
    if (n == 0) return;
    ans += v[n-1];
    ans += max;
    solve(v,n-1, v[n-1]);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    ll n;
    cin >> n;
    vector <ll> v(n,0);
    ll  s = 0;
    for (int i = 0; i < n; ++i) cin >> v[i], s+=v[i];
    sort(v.begin(), v.end());
    int a = n;
    int cnt = 1;
    while (n / 4 > 1){
        for (int i = 0; i < a/4; ++i) ans += v[n-i-1];
        n/=4;
        cnt++;
    }
    if(a==1) cout << s;
    else if(cnt==1) cout << ans + s + cnt*v[a-1];
    else cout << ans + s + (cnt-1)*v[a-1];

}


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

typedef long long ll;
typedef signed long long sll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    int n;
    cin >> n;
    vector <int> v(n,0);
    ll ans = 0, s = 0;
    for (int i = 0; i < n; ++i) cin >> v[i], s+=v[i];
    sort(v.begin(), v.end());
    for (int i = 0; i < n/4; ++i){
        ans += v[n-i-1];
    }
     cout << s + ans;
}


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

typedef long long ll;
typedef signed long long sll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    ll n;
    cin >> n;
    vector <ll> v(n,0);
    ll ans = 0, s = 0;
    for (int i = 0; i < n; ++i) cin >> v[i], s+=v[i];
    sort(v.begin(), v.end());
    int j = n/4;
    for (int i = 0; i < n/4; ++i){
        //ans += v[n-1];
        ans += v[n-i-1];
    }

     cout << s + ans + v[n-1];
}


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

typedef long long ll;
typedef signed long long sll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    ll n;
    cin >> n;
    vector <ll> v(n,0);
    ll ans = 0, s = 0;
    for (int i = 0; i < n; ++i) cin >> v[i], s+=v[i];
    sort(v.begin(), v.end());
    for (int i = 0; i < n/4; ++i){
        ans += v[n-i-1];
    }
    while (n/4 > 1) ans += v[n-1], n/=4;
    cout << ans + s;
}


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

typedef long long ll;
typedef signed long long sll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    ll n;
    cin >> n;
    vector <ll> v(n,0);
    ll ans = 0, s = 0;
    for (int i = 0; i < n; ++i) cin >> v[i], s+=v[i];
    sort(v.begin(), v.end());
    int j = n/4;
    for (int i = 0; i < n/4; ++i){
        //ans += v[n-1];
        ans += v[n-i-1];
    }
    if (n == 1) cout << s;
    else cout << s + ans + v[n-1];
}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
//#define int long long int
#define mod 1000000007
#define f0(n) for(int i=0;i<n;i++)
#define vi vector<int>
#define vs vector<string>
#define mp make_pair
#define pb push_back
#define rt return
#define sumof(arr) accumulate(arr,arr+n,0);
double pi = 3.141592653589793238462;
string numkey="0123456789";
string uni="abcdefghijklmnopqrstuvwxyz";
#define pii pair<int,int>
#define setf cout.setf(ios::fixed);
#define setp setprecision
#define mii map<int,int>
#define all(x) x.begin(),x.end()
#define S second
#define F first
struct Knight
{
  int power,coins,indx;

};
bool cmp(Knight &k1,Knight &k2)
{
  return k1.power<k2.power;
}
int main()
{
 #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("out1.txt","w",stdout);
  #endif

  long long  n;
  cin>>n;
  vector<long long > v;
  for(int i=0;i<n;i++)
  {
    int x;
    cin>>x;
    v.pb(x);

  }
  long long  sum=0LL;
  sum=sum*0LL;
  sort(all(v),greater<long long >());
  for(int i=1;i<=n;i*=4)
    sum+=accumulate(v.begin(),v.begin()+i,0LL);
  printf("%lld",sum);
}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
//#define int long long int
#define mod 1000000007
#define f0(n) for(int i=0;i<n;i++)
#define vi vector<int>
#define vs vector<string>
#define mp make_pair
#define pb push_back
#define rt return
#define sumof(arr) accumulate(arr,arr+n,0);
double pi = 3.141592653589793238462;
string numkey="0123456789";
string uni="abcdefghijklmnopqrstuvwxyz";
#define pii pair<int,int>
#define setf cout.setf(ios::fixed);
#define setp setprecision
#define mii map<int,int>
#define all(x) x.begin(),x.end()
#define S second
#define F first
struct Knight
{
  int power,coins,indx;

};
bool cmp(Knight &k1,Knight &k2)
{
  return k1.power<k2.power;
}
int main()
{
 #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("out1.txt","w",stdout);
  #endif

  int n;
  scanf("%d",&n);
  vector<long long int> v;
  for(int i=0;i<n;i++)
  {
    long long int x;
    //cin>>x;
    scanf("%d",&x);
    v.pb(x);

  }
  long long int sum=0LL;
  sort(all(v),greater<int>());
  for(int i=1;i<=n;i*=4)
    sum+=accumulate(v.begin(),v.begin()+i,0LL);
  printf("%lld",sum);
}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
//#define int long long int
#define mod 1000000007
#define f0(n) for(int i=0;i<n;i++)
#define vi vector<int>
#define vs vector<string>
#define mp make_pair
#define pb push_back
#define rt return
#define sumof(arr) accumulate(arr,arr+n,0);
double pi = 3.141592653589793238462;
string numkey="0123456789";
string uni="abcdefghijklmnopqrstuvwxyz";
#define pii pair<int,int>
#define setf cout.setf(ios::fixed);
#define setp setprecision
#define mii map<int,int>
#define all(x) x.begin(),x.end()
#define S second
#define F first
struct Knight
{
  int power,coins,indx;

};
bool cmp(Knight &k1,Knight &k2)
{
  return k1.power<k2.power;
}
int main()
{
 #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("out1.txt","w",stdout);
  #endif

  long long  n;
  cin>>n;
  vector<long long > v;
  for(int i=0;i<n;i++)
  {
    int x;
    cin>>x;
    v.pb(x);

  }
  long long  sum=0LL;
  sum=sum*0LL;
  sort(all(v),greater<long long >());
  for(int i=1;i<=n;i*=4)
    sum+=accumulate(v.begin(),v.begin()+i,0LL);
  cout<<sum;
}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
//#define int long long int
#define mod 1000000007
#define f0(n) for(int i=0;i<n;i++)
#define vi vector<int>
#define vs vector<string>
#define mp make_pair
#define pb push_back
#define rt return
#define sumof(arr) accumulate(arr,arr+n,0);
double pi = 3.141592653589793238462;
string numkey="0123456789";
string uni="abcdefghijklmnopqrstuvwxyz";
#define pii pair<int,int>
#define setf cout.setf(ios::fixed);
#define setp setprecision
#define mii map<int,int>
#define all(x) x.begin(),x.end()
#define S second
#define F first
struct Knight
{
  int power,coins,indx;

};
bool cmp(Knight &k1,Knight &k2)
{
  return k1.power<k2.power;
}
int main()
{
 #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("out1.txt","w",stdout);
  #endif

  long long  n;
  cin>>n;
  vector<long long > v;
  for(int i=0;i<n;i++)
  {
    int x;
    cin>>x;
    v.pb(x);

  }
  long long  sum=0LL;
  sum=sum*0LL;
  sort(all(v),greater<long long >());
  for(int i=1;i<=n;i*=4)
    sum+=accumulate(v.begin(),v.begin()+i,0LL);
  printf("%d",sum);
}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
//#define int long long int
#define mod 1000000007
#define f0(n) for(int i=0;i<n;i++)
#define vi vector<int>
#define vs vector<string>
#define mp make_pair
#define pb push_back
#define rt return
#define sumof(arr) accumulate(arr,arr+n,0);
double pi = 3.141592653589793238462;
string numkey="0123456789";
string uni="abcdefghijklmnopqrstuvwxyz";
#define pii pair<int,int>
#define setf cout.setf(ios::fixed);
#define setp setprecision
#define mii map<int,int>
#define all(x) x.begin(),x.end()
#define S second
#define F first
struct Knight
{
  int power,coins,indx;

};
bool cmp(Knight &k1,Knight &k2)
{
  return k1.power<k2.power;
}
int main()
{
 #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("out1.txt","w",stdout);
  #endif

  int n;
  scanf("%d",&n);
  vector<long long int> v;
  for(int i=0;i<n;i++)
  {
    long long int x;
    //cin>>x;
    scanf("%d",&x);
    v.pb(x);

  }
  long long int sum=0;
  sort(all(v),greater<int>());
  for(int i=1;i<=n;i*=4)
    sum+=accumulate(v.begin(),v.begin()+i,0LL);
  printf("%lld",sum);
}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long int
#define mod 1000000007
#define f0(n) for(int i=0;i<n;i++)
#define vi vector<int>
#define vs vector<string>
#define mp make_pair
#define pb push_back
#define rt return
#define sumof(arr) accumulate(arr,arr+n,0);
double pi = 3.141592653589793238462;
string numkey="0123456789";
string uni="abcdefghijklmnopqrstuvwxyz";
#define pii pair<int,int>
#define setf cout.setf(ios::fixed);
#define setp setprecision
#define mii map<int,int>
#define all(x) x.begin(),x.end()
#define S second
#define F first
signed main()
{
 #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("out1.txt","w",stdout);
  #endif
  int n;
  cin>>n;
  int arr[n+1]={0};
  for(int i=1;i<=n;i++)
    cin>>arr[i];
  sort(arr+1,arr+n+1,greater<int>());
  int ans=0;
  int sum=0;
  int t=1;
  for(int i=1;i<=n;i++)
  {
    sum+=arr[i];
    if(t==i)
    {
      t*=4;
      ans+=sum;
    }
  }

  cout<<ans;

}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long int
#define mod 1000000007
#define f0(n) for(int i=0;i<n;i++)
#define vi vector<int>
#define vs vector<string>
#define mp make_pair
#define pb push_back
#define rt return
#define sumof(arr) accumulate(arr,arr+n,0);
double pi = 3.141592653589793238462;
string numkey="0123456789";
string uni="abcdefghijklmnopqrstuvwxyz";
#define pii pair<int,int>
#define setf cout.setf(ios::fixed);
#define setp setprecision
#define mii map<int,int>
#define all(x) x.begin(),x.end()
#define S second
#define F first
struct Knight
{
  int power,coins,indx;

};
bool cmp(Knight &k1,Knight &k2)
{
  return k1.power<k2.power;
}
signed main()
{
 #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("out1.txt","w",stdout);
  #endif

  int p=1;
  vi v;
  for(int i=0;i<30;i++)
  {
    v.pb(p);
    p*=4;
  }
  vi v1;
  int c;
  cin>>c;
  int x=0;
  int ans=0;
  int n=lower_bound(all(v),c)-v.begin();
  for(int i=0;i<c;i++)
  {
    int x;
    cin>>x;
    ans+=x;
    v1.pb(x);
  }

  sort(all(v1));
  int pos=c-1;
  while(n>0)
  {
    ans+=v1[pos];
    pos--;
    n--;
  }
  cout<<ans;
}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
//#define int long long int
#define mod 1000000007
#define f0(n) for(int i=0;i<n;i++)
#define vi vector<int>
#define vs vector<string>
#define mp make_pair
#define pb push_back
#define rt return
#define sumof(arr) accumulate(arr,arr+n,0);
double pi = 3.141592653589793238462;
string numkey="0123456789";
string uni="abcdefghijklmnopqrstuvwxyz";
#define pii pair<int,int>
#define setf cout.setf(ios::fixed);
#define setp setprecision
#define mii map<int,int>
#define all(x) x.begin(),x.end()
#define S second
#define F first
struct Knight
{
  int power,coins,indx;

};
bool cmp(Knight &k1,Knight &k2)
{
  return k1.power<k2.power;
}
int main()
{
 #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("out1.txt","w",stdout);
  #endif

  int n;
  scanf("%d",&n);
  vector<long long int> v;
  for(int i=0;i<n;i++)
  {
    long long int x;
    //cin>>x;
    scanf("%d",&x);
    v.pb(x);

  }
  long long int sum=0;
  sort(all(v),greater<int>());
  for(int i=1;i<=n;i*=4)
    sum+=accumulate(v.begin(),v.begin()+i,0LL);
  cout<<sum;
}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long int
#define mod 1000000007
#define f0(n) for(int i=0;i<n;i++)
#define vi vector<int>
#define vs vector<string>
#define mp make_pair
#define pb push_back
#define rt return
#define sumof(arr) accumulate(arr,arr+n,0);
double pi = 3.141592653589793238462;
string numkey="0123456789";
string uni="abcdefghijklmnopqrstuvwxyz";
#define pii pair<int,int>
#define setf cout.setf(ios::fixed);
#define setp setprecision
#define mii map<int,int>
#define all(x) x.begin(),x.end()
#define S second
#define F first
signed main()
{
 #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("out1.txt","w",stdout);
  #endif
  int n;
  cin>>n;
  int arr[n+1]={0};
  for(int i=1;i<=n;i++)
   scanf("%d",&arr[i]);
  sort(arr+1,arr+n+1,greater<int>());
  int ans=0;
  int sum=0;
  int t=1;
  for(int i=1;i<=n;i++)
  {
    sum+=arr[i];
    if(t==i)
    {
      t*=4;
      ans+=sum;
    }
  }

  cout<<ans;

}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long int
#define mod 1000000007
#define f0(n) for(int i=0;i<n;i++)
#define vi vector<int>
#define vs vector<string>
#define mp make_pair
#define pb push_back
#define rt return
#define sumof(arr) accumulate(arr,arr+n,0);
double pi = 3.141592653589793238462;
string numkey="0123456789";
string uni="abcdefghijklmnopqrstuvwxyz";
#define pii pair<int,int>
#define setf cout.setf(ios::fixed);
#define setp setprecision
#define mii map<int,int>
#define all(x) x.begin(),x.end()
#define S second
#define F first
struct Knight
{
  int power,coins,indx;

};
bool cmp(Knight &k1,Knight &k2)
{
  return k1.power<k2.power;
}
signed main()
{
 #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("out1.txt","w",stdout);
  #endif

  int n;
  cin>>n;
  vi v;
  for(int i=0;i<n;i++)
  {
    int x;
    cin>>x;
    v.pb(x);

  }
  int sum=0;
  sort(all(v),greater<int>());
  for(int i=1;i<=n;i*=4)
    sum+=accumulate(v.begin(),v.begin()+i,0LL);
  cout<<sum;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
	int n;
	cin>>n;
	int arr[n];
	for(int i=0;i<n;i++)cin>>arr[i];
	sort(arr,arr+n);
	long long ar[n+1]={};
	for(int i=1;i<=n;i++)
		ar[i]= ar[i-1]+arr[i-1];
	long long sum=0;
	for(int i=1;i<=n;i*=4)
		sum+=(ar[n]-ar[n-i]);
	cout<<sum<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
	int n;
	scanf("%d",&n);
	int arr[n];
	for(int i=0;i<n;i++)scanf("%d",arr+i);
	sort(arr,arr+n);
	long long ar[n+1]={};
	for(int i=1;i<=n;i++)
		ar[i]= ar[i-1]+arr[i-1];
	long long sum=0;
	for(int i=1;i<=n;i*=4)
		sum+=(ar[n]-ar[n-i]);
	printf("%I64d\n",sum);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	int arr[n];
	for(int i=0;i<n;i++)cin>>arr[i];
	sort(arr,arr+n);
	long long ar[n+1]={};
	for(int i=1;i<=n;i++)
		ar[i]= ar[i-1]+arr[i-1];
	long long sum=0;
	for(int i=1;i<=n;i*=4)
		sum+=(ar[n]-ar[n-i]);
	cout<<sum<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin>>n;
	int arr[n];
	for(int i=0;i<n;i++)cin>>arr[i];
	sort(arr,arr+n);
	int ar[n+1]={};
	for(int i=1;i<=n;i++)
		ar[i]= ar[i-1]+arr[i-1];
	long long sum=0;
	for(int i=1;i<=n;i*=4)
		sum+=(ar[n]-ar[n-i]);
	cout<<sum<<endl;
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<string>
#include<string.h>
#include<fstream>
#include<stack>
#include<iomanip>
#include<bitset>
#include<cmath>
//#include"Wait.h"
using namespace std;
typedef long long LL;
int arr[2000003];
signed main()
{
	ifstream in("Text.txt");
	int n;
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	int ex = 0;
	int num = 1;
	while (n > num)
	{
		ex++;
		num *= 4;
	}
	for (int i = 0; i < n; i++)cin >> arr[i];
	sort(arr, arr + n);
	LL ans = 0;
	for (int i = n - 1; i >= 0; i--)
	{
		ans += (LL)arr[i] * max(1, (ex - (n - 1 - i) + 1));
	}
	cout << ans << endl;
	//system("pause");
	return 0;
}
/*  int N, Q;
	in >> N >> Q;
	//scanf("%d%d", &N, &Q);
	init(N);
	int tp;
	for (int i = 0; i < N; i++)
	{
		in >> tp;
		//scanf("%d", &tp);
		if (tp != -1)
		{
			merge(i, tp);
			nods[tp].nex.push_back(i);
		}
		else rt.push_back(i);
	}
	for (int i = 0; i < rt.size(); i++)
	{
		dfs(rt[i], 1);
	}
	int x, y;
	while (Q--) {
		in >> x >> y;
		//scanf("%d%d", &x, &y);
		if (find(x) != find(y))
			cout << "No\n";
		else if (nods[y].h <= nods[x].h)
		{
			if(nods[y].xx>=nods[x].h)
			cout << "Yes\n";
			else cout << "No\n";
		}
		else cout << "No\n";
	}*/
/*typedef long long LL;
typedef pair<int, int> P;
const int maxn = 0;
struct ball
{
	int val, belong;
	int no;
	friend bool operator<(const ball&a, const ball&b)
	{
		return a.val < b.val;
	}
}balls[300007];
int r[100003];
int g[100003];
int b[100003];
int rd() {
	LL ans = 0; char last = ' ', ch = getchar();
	while (!(ch >= '0' && ch <= '9'))last = ch, ch = getchar();
	while (ch >= '0' && ch <= '9')ans = ans * 10 + ch - '0', ch = getchar();
	if (last == '-')ans = -ans; return ans;
}
LL ans(LL x, LL y, LL z)
{
	return (x - y)*(x - y) + (x - z)*(x - z) + (y - z)*(y - z);
}*/
/*ball temp;
int nr, ng, nb;
int ri, gi, bi;
in >> t;
while (t--)
{
	in >> nr >> ng >> nb;
	memset(r, 0, sizeof r);
	memset(g, 0, sizeof g);
	memset(b, 0, sizeof b);
	int cnt = 0;
	for (int i = 0; i < nr; i++)
	{
		in >> ri;
		temp.val = ri, temp.belong = 0;
		balls[cnt++] = temp;
	}
	for (int i = 0; i < ng; i++)
	{
		in >> gi;
		temp.val = gi, temp.belong = 1;
		balls[cnt++] = temp;
	}
	for (int i = 0; i < nb; i++)
	{
		in >> bi;
		temp.val = bi, temp.belong = 2;
		balls[cnt++] = temp;
	}
	sort(balls, balls + cnt);
	int lastr = -1;
	int lastg = -1;
	int lastb = -1;
	int cntr=0, cntg=0, cntb=0;
	for (int i = 0; i < cnt; i++)
	{
		if (balls[i].belong == 0)
		{
			r[cntr] = i;
			balls[i].no = cntr;
			cntr++;
		}
		if (balls[i].belong == 1)
		{
			g[cntg] = i;
			balls[i].no = cntg;
			cntg++;
		}
		if (balls[i].belong == 2)
		{
			b[cntb] = i;
			balls[i].no = cntb;
			cntb++;
		}
	}
	int id1;
	int id2;
	LL anss = ((LL)(1))<<60;
	for (int i = 0; i < cnt; i++)
	{
		if (balls[i].belong == 0)
		{
			id1 = upper_bound(g, g + cntg, i) - g;
			id2 = upper_bound(b, b + cntb, i) - b;
			if(id1!=cntg&&id2!=cntb)
			anss = min(anss, ans(balls[i].val, balls[g[id1]].val, balls[b[id2]].val));
		}
		if(balls[i].belong == 1)
		{
			id1 = upper_bound(r, r + cntr, i) - r;
			id2 = upper_bound(b, b + cntb, i) - b;
			if (id1 != cntr && id2 != cntb)
			anss = min(anss, ans(balls[i].val, balls[r[id1]].val, balls[b[id2]].val));
		}
		if (balls[i].belong == 2)
		{
			id1 = upper_bound(g, g + cntg, i) - g;
			id2 = upper_bound(r, r + cntr, i) - r;
			if (id1 != cntg && id2 != cntr)
			anss = min(anss, ans(balls[i].val, balls[g[id1]].val, balls[r[id2]].val));
		}
	}
	cout << "ans:"<<anss << endl;
}*/


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<string>
#include<string.h>
#include<fstream>
#include<stack>
#include<iomanip>
#include<bitset>
#include<cmath>
//#include"Wait.h"
using namespace std;
typedef long long LL;
int arr[2000003];
LL QPow(LL a, LL b)
{
	LL ans = 1;
	while (b)
	{
		if (b & 1)ans *= a;
		a *= a;
		b >>= 1;
	}
	return ans;
}
signed main()
{
	ifstream in("Text.txt");
	int n;
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	int ex = 0;
	int num = 1;
	while (n > num)
	{
		ex++;
		num *= 4;
	}
	for (int i = 0; i < n; i++)cin >> arr[i];
	sort(arr, arr + n);
	LL ans = 0;
	bool flag = 0;
	int ii = 0;
	for (int i = n - 1; i >= 0; i--)
	{
		if (n - i > QPow(4, ii))
			ii++;
		else flag = 1;
		ans += (LL)arr[i] * max(1, ex-ii+1);
		//cout << ex + 1 << " ";
	}
	cout << ans << endl;//14440495117
	//system("pause");
	return 0;
}
/*  int N, Q;
	in >> N >> Q;
	//scanf("%d%d", &N, &Q);
	init(N);
	int tp;
	for (int i = 0; i < N; i++)
	{
		in >> tp;
		//scanf("%d", &tp);
		if (tp != -1)
		{
			merge(i, tp);
			nods[tp].nex.push_back(i);
		}
		else rt.push_back(i);
	}
	for (int i = 0; i < rt.size(); i++)
	{
		dfs(rt[i], 1);
	}
	int x, y;
	while (Q--) {
		in >> x >> y;
		//scanf("%d%d", &x, &y);
		if (find(x) != find(y))
			cout << "No\n";
		else if (nods[y].h <= nods[x].h)
		{
			if(nods[y].xx>=nods[x].h)
			cout << "Yes\n";
			else cout << "No\n";
		}
		else cout << "No\n";
	}*/
/*typedef long long LL;
typedef pair<int, int> P;
const int maxn = 0;
struct ball
{
	int val, belong;
	int no;
	friend bool operator<(const ball&a, const ball&b)
	{
		return a.val < b.val;
	}
}balls[300007];
int r[100003];
int g[100003];
int b[100003];
int rd() {
	LL ans = 0; char last = ' ', ch = getchar();
	while (!(ch >= '0' && ch <= '9'))last = ch, ch = getchar();
	while (ch >= '0' && ch <= '9')ans = ans * 10 + ch - '0', ch = getchar();
	if (last == '-')ans = -ans; return ans;
}
LL ans(LL x, LL y, LL z)
{
	return (x - y)*(x - y) + (x - z)*(x - z) + (y - z)*(y - z);
}*/
/*ball temp;
int nr, ng, nb;
int ri, gi, bi;
in >> t;
while (t--)
{
	in >> nr >> ng >> nb;
	memset(r, 0, sizeof r);
	memset(g, 0, sizeof g);
	memset(b, 0, sizeof b);
	int cnt = 0;
	for (int i = 0; i < nr; i++)
	{
		in >> ri;
		temp.val = ri, temp.belong = 0;
		balls[cnt++] = temp;
	}
	for (int i = 0; i < ng; i++)
	{
		in >> gi;
		temp.val = gi, temp.belong = 1;
		balls[cnt++] = temp;
	}
	for (int i = 0; i < nb; i++)
	{
		in >> bi;
		temp.val = bi, temp.belong = 2;
		balls[cnt++] = temp;
	}
	sort(balls, balls + cnt);
	int lastr = -1;
	int lastg = -1;
	int lastb = -1;
	int cntr=0, cntg=0, cntb=0;
	for (int i = 0; i < cnt; i++)
	{
		if (balls[i].belong == 0)
		{
			r[cntr] = i;
			balls[i].no = cntr;
			cntr++;
		}
		if (balls[i].belong == 1)
		{
			g[cntg] = i;
			balls[i].no = cntg;
			cntg++;
		}
		if (balls[i].belong == 2)
		{
			b[cntb] = i;
			balls[i].no = cntb;
			cntb++;
		}
	}
	int id1;
	int id2;
	LL anss = ((LL)(1))<<60;
	for (int i = 0; i < cnt; i++)
	{
		if (balls[i].belong == 0)
		{
			id1 = upper_bound(g, g + cntg, i) - g;
			id2 = upper_bound(b, b + cntb, i) - b;
			if(id1!=cntg&&id2!=cntb)
			anss = min(anss, ans(balls[i].val, balls[g[id1]].val, balls[b[id2]].val));
		}
		if(balls[i].belong == 1)
		{
			id1 = upper_bound(r, r + cntr, i) - r;
			id2 = upper_bound(b, b + cntb, i) - b;
			if (id1 != cntr && id2 != cntb)
			anss = min(anss, ans(balls[i].val, balls[r[id1]].val, balls[b[id2]].val));
		}
		if (balls[i].belong == 2)
		{
			id1 = upper_bound(g, g + cntg, i) - g;
			id2 = upper_bound(r, r + cntr, i) - r;
			if (id1 != cntg && id2 != cntr)
			anss = min(anss, ans(balls[i].val, balls[g[id1]].val, balls[r[id2]].val));
		}
	}
	cout << "ans:"<<anss << endl;
}*/


#include "iostream"
using std::cout;
using std::cin;
using std::cout;
using std::endl;
int compare(const void* x1, const void* x2)
{
	return (*(int*)x1 - *(int*)x2);
}
int func(int n)
{
	int it = 0;
	while (n != 1)
	{
		n = n / 4;
		it++;
	}
	return it;
}
int main()
{

	long long int btf = 0;
	int n, temp;
	cin >> n;
	int* mass = new int[n];
	for (int i = 0; i < n; i++)
	{
		cin >> mass[i];
	}
	qsort(mass, n, sizeof(int), compare);
	btf += mass[n - 1];
	int numb1 = 4;
	int numb2 = n-1;
	while (n != 1)
	{
		for (int i = numb2; i > numb2-numb1; i--)
		{
		btf += mass[i];
		}
		numb1 *= 4;
		n = n / 4;
	}
	cout << btf;
	return 0;
}


#include "iostream"
using std::cout;
using std::cin;
using std::cout;
using std::endl;
int compare(const void* x1, const void* x2)
{
	return (*(int*)x1 - *(int*)x2);
}

void func(int* mass, int n, long long int* btf)
{
	qsort(mass, n, sizeof(int), compare);
	*btf += mass[n - 1];
	if (n != 1)
	{
		int temp = n / 4;
		int* ms1 = new int[temp];
		int* ms2 = new int[temp];
		int* ms3 = new int[temp];
		int* ms4 = new int[temp];
		int nb = 0;
		for (int i = 0; nb < n; i++)
		{
			ms1[i] = mass[nb];
			nb += 4;
		}
		nb = 1;
		for (int i = 0; nb < n; i++)
		{
			ms2[i] = mass[nb];
			nb += 4;
		}
		nb = 2;
		for (int i = 0; nb < n; i++)
		{
			ms3[i] = mass[nb];
			nb += 4;
		}
		nb = 3;
		for (int i = 0; nb < n; i++)
		{
			ms4[i] = mass[nb];
			nb += 4;
		}
		n /= 4;
		func(ms1, n, btf);
		func(ms2, n, btf);
		func(ms3, n, btf);
		func(ms4, n, btf);
		delete[](ms1);
		delete[](ms2);
		delete[](ms3);
		delete[](ms4);
	}
}
int main()
{

	long long int btf = 0;
	int n;
	cin >> n;
	int* mass = new int[n];
	for (int i = 0; i < n; i++)
	{
		cin >> mass[i];
	}
	func(mass, n, &btf);
	cout << btf;
	return 0;
}


#include "stdafx.h"
#include <iostream>
#include <algorithm>

using namespace std;

long long int btf;
int n, temp;
int mass[2012345];

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

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

	sort(mass, mass + n);

	btf += mass[n - 1];
	int numb1 = 4;
	int numb2 = n - 1;
	while (n != 1)
	{
		for (int i = numb2; i > numb2 - numb1; i--)
		{
			btf += mass[i];
		}
		numb1 *= 4;
		n = n / 4;
	}

	cout << btf;
	return 0;
}


#include "iostream"
#include "ctime"
#include "algorithm"

using namespace std;
const int RUN = 32;

// this function sorts array from left index to
// to right index which is of size atmost RUN
void insertionSort(int arr[], int left, int right)
{
    for (int i = left + 1; i <= right; i++)
    {
      int temp = arr[i];
      int j = i - 1;
      while (arr[j] > temp && j >= left)
      {
        arr[j+1] = arr[j];
        j--;
      }
      arr[j+1] = temp;
    }
}

// merge function merges the sorted runs
void merge(int arr[], int l, int m, int r)
{
  // original array is broken in two parts
  // left and right array
  int len1 = m - l + 1, len2 = r - m;
  int* left = new int[len1];
  int*right = new int[len2];
  for (int i = 0; i < len1; i++)
    left[i] = arr[l + i];
  for (int i = 0; i < len2; i++)
    right[i] = arr[m + 1 + i];

  int i = 0;
  int j = 0;
  int k = l;

  // after comparing, we merge those two array
  // in larger sub array
  while (i < len1 && j < len2)
  {
    if (left[i] <= right[j])
    {
        arr[k] = left[i];
        i++;
    }
    else
    {
        arr[k] = right[j];
        j++;
    }
    k++;
  }

  // copy remaining elements of left, if any
  while (i < len1)
  {
    arr[k] = left[i];
    k++;
    i++;
  }

  // copy remaining element of right, if any
  while (j < len2)
  {
    arr[k] = right[j];
    k++;
    j++;
  }
}

// iterative Timsort function to sort the
// array[0...n-1] (similar to merge sort)
void timSort(int arr[], int n)
{
  // Sort individual subarrays of size RUN
  for (int i = 0; i < n; i+=RUN)
    insertionSort(arr, i, min((i+31), (n-1)));

  // start merging from size RUN (or 32). It will merge
  // to form size 64, then 128, 256 and so on ....
  for (int size = RUN; size < n; size = 2*size)
  {
    // pick starting point of left sub array. We
    // are going to merge arr[left..left+size-1]
    // and arr[left+size, left+2*size-1]
    // After every merge, we increase left by 2*size
    for (int left = 0; left < n; left += 2*size)
    {
        // find ending point of left sub array
        // mid+1 is starting point of right sub array
        int mid = left + size - 1;
        int right = min((left + 2*size - 1), (n-1));

        // merge sub array arr[left.....mid] &
        // arr[mid+1....right]
        merge(arr, left, mid, right);
    }
  }
}

using std::cout;
using std::cin;
using std::cout;
using std::endl;
int compare(const void* x1, const void* x2)
{
	return (*(int*)x1 - *(int*)x2);
}

int main()
{

	long long int btf = 0;
	int n, temp;
	cin >> n;
	//n = 1048576;
	srand(time(0));
	int* mass = new int[n];
	for (int i = 0; i < n; i++)
	{
		cin >> mass[i];
		//mass[i] = 2000000000 + rand() % 100;
	}
	unsigned int startime = clock();
	//timSort(mass, n);
	sort(mass, mass + n);
	unsigned int endtime = clock();
	//cout << endtime - startime << endl;
	btf += mass[n - 1];
	int numb1 = 4;
	int numb2 = n-1;
	startime = clock();
	while (n != 1)
	{
		for (int i = numb2; i > numb2-numb1; i--)
		{
		btf += mass[i];
		}
		numb1 *= 4;
		n = n / 4;
	}
	endtime = clock();
	//cout << endtime - startime << endl;
	cout << btf;
	return 0;
}
//unsigned int startime = clock();
//qsort(mass, n, sizeof(int), compare);
//unsigned int endtime = clock();
//cout << endtime - startime << endl;


#include "iostream"
#include "ctime"
#include "algorithm"

using namespace std;
const int RUN = 32;

// this function sorts array from left index to
// to right index which is of size atmost RUN
void insertionSort(int arr[], int left, int right)
{
    for (int i = left + 1; i <= right; i++)
    {
      int temp = arr[i];
      int j = i - 1;
      while (arr[j] > temp && j >= left)
      {
        arr[j+1] = arr[j];
        j--;
      }
      arr[j+1] = temp;
    }
}

// merge function merges the sorted runs
void merge(int arr[], int l, int m, int r)
{
  // original array is broken in two parts
  // left and right array
  int len1 = m - l + 1, len2 = r - m;
  int* left = new int[len1];
  int*right = new int[len2];
  for (int i = 0; i < len1; i++)
    left[i] = arr[l + i];
  for (int i = 0; i < len2; i++)
    right[i] = arr[m + 1 + i];

  int i = 0;
  int j = 0;
  int k = l;

  // after comparing, we merge those two array
  // in larger sub array
  while (i < len1 && j < len2)
  {
    if (left[i] <= right[j])
    {
        arr[k] = left[i];
        i++;
    }
    else
    {
        arr[k] = right[j];
        j++;
    }
    k++;
  }

  // copy remaining elements of left, if any
  while (i < len1)
  {
    arr[k] = left[i];
    k++;
    i++;
  }

  // copy remaining element of right, if any
  while (j < len2)
  {
    arr[k] = right[j];
    k++;
    j++;
  }
}

// iterative Timsort function to sort the
// array[0...n-1] (similar to merge sort)
void timSort(int arr[], int n)
{
  // Sort individual subarrays of size RUN
  for (int i = 0; i < n; i+=RUN)
    insertionSort(arr, i, min((i+31), (n-1)));

  // start merging from size RUN (or 32). It will merge
  // to form size 64, then 128, 256 and so on ....
  for (int size = RUN; size < n; size = 2*size)
  {
    // pick starting point of left sub array. We
    // are going to merge arr[left..left+size-1]
    // and arr[left+size, left+2*size-1]
    // After every merge, we increase left by 2*size
    for (int left = 0; left < n; left += 2*size)
    {
        // find ending point of left sub array
        // mid+1 is starting point of right sub array
        int mid = left + size - 1;
        int right = min((left + 2*size - 1), (n-1));

        // merge sub array arr[left.....mid] &
        // arr[mid+1....right]
        merge(arr, left, mid, right);
    }
  }
}

using std::cout;
using std::cin;
using std::cout;
using std::endl;
int compare(const void* x1, const void* x2)
{
	return (*(int*)x1 - *(int*)x2);
}

int main()
{

	long long int btf = 0;
	int n, temp;
	cin >> n;
	//n = 1048576;
	srand(time(0));
	int* mass = new int[n];
	for (int i = 0; i < n; i++)
	{
			cin >> mass[i];
		//mass[i] = 2000000000 + rand() % 100;
	}
	unsigned int startime = clock();
	timSort(mass, n);
	unsigned int endtime = clock();
	//cout << endtime - startime << endl;
	btf += mass[n - 1];
	int numb1 = 4;
	int numb2 = n-1;
	startime = clock();
	while (n != 1)
	{
		for (int i = numb2; i > numb2-numb1; i--)
		{
		btf += mass[i];
		}
		numb1 *= 4;
		n = n / 4;
	}
	endtime = clock();
	//cout << endtime - startime << endl;
	cout << btf;
	return 0;
}
//unsigned int startime = clock();
//qsort(mass, n, sizeof(int), compare);
//unsigned int endtime = clock();
//cout << endtime - startime << endl;


#include "iostream"
using std::cout;
using std::cin;
using std::cout;
using std::endl;
void sort(int* mass, int n)
{
	int temp;
	for (int i = 0; i < n-1; i++)
	{
		for (int j = i+1; j < n; j++)
		{
			if (mass[i] > mass[j])
			{
				temp = mass[i];
				mass[i] = mass[j];
				mass[j] = temp;
			}
		}
	}
}
void func(int* mass,int n, long long int* btf)
{
	sort(mass, n);
	*btf += mass[n - 1];
	if (n != 1)
	{
		int temp = n / 4;
		int* ms1 = new int[temp];
		int* ms2 = new int[temp];
		int* ms3 = new int[temp];
		int* ms4 = new int[temp];
		int nb = 0;
		for (int i = 0; nb < n; i++)
		{
			ms1[i] = mass[nb];
			nb += 4;
		}
		nb = 1;
		for (int i = 0; nb < n; i++)
		{
			ms2[i] = mass[nb];
			nb += 4;
		}
		nb = 2;
		for (int i = 0; nb < n; i++)
		{
			ms3[i] = mass[nb];
			nb += 4;
		}
		nb = 3;
		for (int i = 0; nb < n; i++)
		{
			ms4[i] = mass[nb];
			nb += 4;
		}
		n /= 4;
		func(ms1, n, btf);
		func(ms2, n, btf);
		func(ms3, n, btf);
		func(ms4, n, btf);
		delete[](ms1);
		delete[](ms2);
		delete[](ms3);
		delete[](ms4);
	}
}
int main()
{
	long long int btf = 0;
	int n;
	cin >> n;
	int* mass = new int[n];
	for (int i = 0; i < n; i++)
	{
		cin >> mass[i];
	}
	func(mass, n, &btf);
	cout << btf;
	return 0;
}


#include "iostream"
using std::cout;
using std::cin;
using std::cout;
using std::endl;
int getMax(int arr[], int n)
{
	int max = arr[0];
	for (int i = 1; i < n; i++)
		if (arr[i] > max)
			max = arr[i];
	return max;
}
void countSort(int arr[], int n, int exp)
{
	int i, count[10] = { 0 };
	int *output = new int[n];
	for (i = 0; i < n; i++)
		count[(arr[i] / exp) % 10]++;
	for (i = 1; i < 10; i++)
		count[i] += count[i - 1];
	for (i = n - 1; i >= 0; i--)
	{
		output[count[(arr[i] / exp) % 10] - 1] = arr[i];
		count[(arr[i] / exp) % 10]--;
	}
	for (i = 0; i < n; i++)
		arr[i] = output[i];
}

void radixsort(int arr[], int n)
{
	int exp, m;
	m = getMax(arr, n);

	for (exp = 1; m / exp > 0; exp *= 10)
		countSort(arr, n, exp);
}

int compare(const void* x1, const void* x2)
{
	return (*(int*)x1 - *(int*)x2);
}
int main()
{

	long long int btf = 0;
	int n, temp;
	cin >> n;
	int* mass = new int[n];
	for (int i = 0; i < n; i++)
	{
			cin >> mass[i];
	}
	radixsort(mass, n);
	btf += mass[n - 1];
	int numb1 = 4;
	int numb2 = n-1;
	while (n != 1)
	{
		for (int i = numb2; i > numb2-numb1; i--)
		{
		btf += mass[i];
		}
		numb1 *= 4;
		n = n / 4;
	}
	cout << btf;
	return 0;
}


#include "iostream"
using std::cout;
using std::cin;
using std::cout;
using std::endl;
void sort(int* mass, int n)
{
	int temp;
	for (int i = 0; i < n-1; i++)
	{
		for (int j = i+1; j < n; j++)
		{
			if (mass[i] > mass[j])
			{
				temp = mass[i];
				mass[i] = mass[j];
				mass[j] = mass[i];
			}
		}
	}
}
void func(int* mass,int n, int* btf)
{
	sort(mass, n);
	*btf += mass[n - 1];
	if (n != 1)
	{
		int temp = n / 4;
		int* ms1 = new int[temp];
		int* ms2 = new int[temp];
		int* ms3 = new int[temp];
		int* ms4 = new int[temp];
		int nb = 0;
		for (int i = 0; nb < n; i++)
		{
			ms1[i] = mass[nb];
			nb += 4;
		}
		nb = 1;
		for (int i = 0; nb < n; i++)
		{
			ms2[i] = mass[nb];
			nb += 4;
		}
		nb = 2;
		for (int i = 0; nb < n; i++)
		{
			ms3[i] = mass[nb];
			nb += 4;
		}
		nb = 3;
		for (int i = 0; nb < n; i++)
		{
			ms4[i] = mass[nb];
			nb += 4;
		}
		n /= 4;
		func(ms1, n, btf);
		func(ms2, n, btf);
		func(ms3, n, btf);
		func(ms4, n, btf);
		delete[](ms1);
		delete[](ms2);
		delete[](ms3);
		delete[](ms4);
	}
}
int main()
{
	int btf = 0;
	int n;
	cin >> n;
	int* mass = new int[n];
	for (int i = 0; i < n; i++)
	{
		cin >> mass[i];
	}
	func(mass, n, &btf);
	cout << btf;
	return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

long long int btf;
int n, temp;
int mass[2012345];

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

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

	sort(mass, mass + n);

	btf += mass[n - 1];
	int numb1 = 4;
	int numb2 = n - 1;
	while (n != 1)
	{
		for (int i = numb2; i > numb2 - numb1; i--)
		{
			btf += mass[i];
		}
		numb1 *= 4;
		n = n / 4;
	}

	cout << btf;
	return 0;
}


#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <iterator>
#include <fstream>
#include <math.h>

#include <vector>
#include <string>
#include <stack>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <list>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

#define FOR(i,n,m) for(int i=n; i<m; i++)
#define ROF(i,n,m) for(int i=n; i>m; i--)

const double PI = 3.141592653589793238463;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;

/*------------------------------------------------------------------------------------------------------------*/

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);

	int n;
	ll ans = 0;
	cin >> n;
	vector<int> a(n);
	vector<ll> pref(n + 1);
	FOR(i, 0, n)
		cin >> a[i];
	sort(a.rbegin(), a.rend());
	pref[0] = a[0];
	FOR(i, 1, n)
		pref[i] = pref[i - 1] + a[i];
	while (n) {
		ans += pref[n - 1];
		n /= 4;
	}
	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#define  N 2000005
#define EPS 10e-9
typedef long long ll;

inline int scan() {
	int a = 0;
	char c = getchar();
	while ((c >= '0') && (c <= '9')) {
		a = (a << 3) + (a << 1) + c - '0';
		c = getchar();
	}
	return a;
}
ll mas[N] = { 0 };

int main() {
	ll answer = 0;
	int n = scan();
	for (int i = 0; i < n; i++) mas[i] = scan();
	std::sort(mas, mas + n);
	int count = log(n)/log(4)+1;
	int some = 1;
	for(int i = n-1;i >= 0;i--){
		if (i == (n - 1 - some)) {
			some *= 4;
			count--;
		}
		answer += (mas[i] * count);
	}
	std::cout << answer;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#define  N 2000005
#define EPS 10e-9
typedef long long ll;

inline int scan() {
	int a = 0;
	char c = getchar();
	while ((c >= '0') && (c <= '9')) {
		a = (a << 3) + (a << 1) + c - '0';
		c = getchar();
	}
	return a;
}
ll mas[N] = { 0 };

int main() {
	ll answer = 0;
	int n = scan();
	for (int i = 0; i < n; i++) mas[i] = scan();
	std::sort(mas, mas + n);
	ll count = log(n)/log(4)+1;
	int some = 1;
	for(int i = n-1;i >= 0;i--){
		if (i == (n - 1 - some)) {
			some *= 4;
			count--;
		}
		answer += (mas[i] * count);
	}
	std::cout << answer;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#define  N 100005
#define EPS 10e-9
typedef long long ll;
ll mas[N] = { 0 };
int count[N] = { 0 };

inline int scan() {
	int a = 0;
	char c = getchar();
	while ((c >= '0') && (c <= '9')) {
		a = (a << 3) + (a << 1) + c - '0';
		c = getchar();
	}
	return a;
}
/*
void Merge(int* mass, int a, int b)
{
	int middle, start, final;
	int* mas = new int[N];
	middle = (a + b) / 2; //вычисление среднего элемента
	start = a; //начало левой части
	final = middle + 1; //начало правой части
	for (int i = a; i <= b; i++) //выполнять от начала до конца
		if ((start <= middle) && ((final > b) || (mass[start] < mass[final])))
		{
			mas[i] = mass[start];
			start++;
		}
		else
		{
			mas[i] = mass[final];
			final++;
		}
	//возвращение результата в список
	for (int i = a; i <= b; i++) mass[i] = mas[i];
	delete[]mas;
};
//рекурсивная процедура сортировки
void MergeSort(int* mass, int l, int r)
{
	if (l < r)
	{
		MergeSort(mass, l, (l + r) / 2); //сортировка левой части
		MergeSort(mass, (l + r) / 2 + 1, r); //сортировка правой части
		Merge(mass, l, r); //слияние двух частей
	}
};*/
int main() {
	ll answer = 0;
	int n = scan();
	int n1 = n;
	for (int i = 0; i < n; i++) mas[i] = scan();
	std::sort(mas, mas + n);
	int count = 1;
	int some = n - n / 4;
	int i = 0;
	while(i != n1){
		if (i == some) {
			count++; n = n / 4;
			some += (n - n / 4);
		}
		answer += (mas[i] * count);
		i++;
	}
	std::cout << answer;
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int l;
	cin>>l;
	int n=0, ll=l;
	while (ll / 4 != 0) {
		n++;
		ll/=4;
	}
	int a[200001];
	long long s[200001];
	long long result=0;
	for (int i=0; i<l; i++) {
		cin>>a[i];
		s[i]=0;
		//result+=a[i];
	}
	sort(a, a+l);
	s[l-1]=a[l-1];
	int t=1;
	result+=s[l-1];
	t*=4;
	for (int i=l-2; i>=0; i--) {
		s[i]=s[i+1]+a[i];
		if (i==l-t) {
			result+=s[i];
			t*=4;
		}
	}
	//for (int i=0; i<l; i++)
	//	cout<<a[i]<<" ";
	//int t=1, count=0;
	//for (int i=0; i<n; i++) {
		//for (int j=0; j<t; j++) {
		//	result+=a[l-1-j];
			//count++;
		//}
	//	result+=s[l-t];
	//	t*=4;
	//}
	cout<<result<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int l;
	cin>>l;
	int n=0, ll=l;
	while (ll / 4 != 0) {
		n++;
		ll/=4;
	}
	int a[200000];
	long long s[200000];
	long long result=0;
	for (int i=0; i<l; i++) {
		cin>>a[i];
		s[i]=0;
		result+=a[i];
	}
	sort(a, a+l);
	s[l-1]=a[l-1];
	for (int i=l-2; i>=0; i--) {
		s[i]=s[i+1]+a[i];
	}
	//for (int i=0; i<l; i++)
	//	cout<<a[i]<<" ";
	int t=1, count=0;
	for (int i=0; i<n; i++) {
		//for (int j=0; j<t; j++) {
		//	result+=a[l-1-j];
			//count++;
		//}
		result+=s[l-t];
		t*=4;
	}
	cout<<result<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int l;
	cin>>l;
	int n=0, ll=l;
	while (ll / 4 != 0) {
		n++;
		ll/=4;
	}
	int a[2000000];
	long long s[2000000];
	long long result=0;
	for (int i=0; i<l; i++) {
		cin>>a[i];
		s[i]=0;
	}
	sort(a, a+l);
	s[l-1]=a[l-1];
	int t=1;
	result+=s[l-1];
	t*=4;
	for (int i=l-2; i>=0; i--) {
		s[i]=s[i+1]+a[i];
		if (i==l-t) {
			result+=s[i];
			t*=4;
		}
	}

	cout<<result<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int l;
	cin>>l;
	int n=0, ll=l;
	while (ll / 4 != 0) {
		n++;
		ll/=4;
	}
	int a[200000];
	long long result=0;
	for (int i=0; i<l; i++) {
		cin>>a[i];
		result+=a[i];
	}
	sort(a, a+l);
	//for (int i=0; i<l; i++)
	//	cout<<a[i]<<" ";
	int t=1, count=0;
	for (int i=0; i<n; i++) {
		for (int j=0; j<t; j++) {
			result+=a[l-1-j];
			//count++;
		}
		t*=4;
	}
	cout<<result<<endl;
	return 0;
}


// CFR186_div2.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>

#include "vector"
#include "string"
#include "set"
#include "sstream"
#include "algorithm"
#include "cmath"
#include "cassert"
#include "iostream"

using namespace std;

#define int64 long long
#define F(vec, index) for (int index = 0; index  < vec.size(); ++index)
#define F_S(vec, index, start) for (int index = start; index  < vec.size(); ++index)

vector<int64> data1;
	vector<int64> data2;
	vector<int64> data3;
	vector<int64> data4;
	vector<int64> dataSave;

int64 rec(vector<int64>& data, int start, int end)
{
	int64 max = data[start];
	if (end - start == 1)
	{
		return  max;
	}
	//sort(data.rbegin(), data.rend());

	data1.clear();
	data2.clear();
	data3.clear();
	data4.clear();

	for (int i = start; i < end; i += 4)
	{
		data1.push_back(data[i]);
		data2.push_back(data[i + 1]);
		data3.push_back(data[i + 2]);
		data4.push_back(data[i + 3]);

	}

	for (int i = 0; i < data1.size(); i++)
	{
		data[start + i ] = data1[i];
	}
	for (int i = 0 ; i < data1.size(); i++)
	{
		data[start + i + data1.size()] = data2[i];
	}

	for (int i = 0 ; i < data1.size(); i++)
	{
		data[start + i + data1.size() * 2] = data3[i];
	}

	for (int i = 0 ; i < data1.size(); i++)
	{
		data[start + i + data1.size() * 3] = data4[i];
	}

	int64 max1 = rec(data, start                   ,  start + data1.size());
	int64 max2 = rec(data, start + data1.size()    ,  start + 2 * data1.size());
	int64 max3 = rec(data, start + 2 * data1.size(),  start + 3 * data1.size());
	int64 max4 = rec(data, start + 3 * data1.size(),  start + 4 * data1.size());
	return max1 + max2 + max3 + max4 + max;

}

int _tmain(int argc, _TCHAR* argv[])
{

	int n;
	cin >> n;

	vector<int64> data(n);

	for (int i  = 0; i < n; ++i)
	{
		cin >> data[i];
	}
	sort(data.rbegin(), data.rend());

	int64 result = rec(data, 0, data.size());

	cout << result << endl;

	return 0;

	/*
	int n;
	cin >> n;

	if (n >= 0)
	{
		cout << n << endl;
		return 0;
	}

	stringstream stream;
	stringstream stream1;
	stringstream stream2;

	stream << n;

	string str = stream.str();

	if (str.length() <= 2)
	{
		cout << 0 << endl;
		return 0;
	}

	for (int i = 0; i < str.length() - 2; ++i)
	{
		stream1 << str[i];
	}
	stream1 << str[str.length() - 1];

	for (int i = 0; i < str.length() - 1; ++i)
	{
		stream2 << str[i];
	}

	int n1, n2;
	stream1 >> n1;
	stream2 >> n2;

	if (n1 > n2)
	{
		cout << n1 << endl;
	}
	else
	{
		cout << n2 << endl;
	}*/

	/*string str;
	cin >> str;

	vector<int> data(str.length() - 1, 0);

	F(data, i)
	{
		if (str[i] == str[i + 1])
		{
			data[i] = 1;
		}
	}

	vector<int> dataInteg(str.length(), 0);

	for (int i = 1; i < dataInteg.size(); ++i)
	{

		dataInteg[i] = data[i - 1] + dataInteg[i - 1];
	}

	int m;
	cin >> m;

	for (int i = 0; i < m;++i)
	{
		int l, r;
		cin >> l >> r;
		l--;
		r--;
		r = min(r, (int)dataInteg.size() - 1);

		cout <<dataInteg[r] - dataInteg[l] << endl;

	}*/

}


// CFR186_div2.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>

#include "vector"
#include "string"
#include "set"
#include "sstream"
#include "algorithm"
#include "cmath"
#include "cassert"
#include "iostream"

using namespace std;

#define int64 long long
#define F(vec, index) for (int index = 0; index  < vec.size(); ++index)
#define F_S(vec, index, start) for (int index = start; index  < vec.size(); ++index)

int rec(vector<int>& data)
{
	if (data.size() == 1)
	{
		return  data[0];
	}
	sort(data.rbegin(), data.rend());

	int max = data[0];

	vector<int> data1;
	vector<int> data2;
	vector<int> data3;
	vector<int> data4;

	for (int i = 0; i < data.size(); i += 4)
	{
		data1.push_back(data[i]);
		data2.push_back(data[i + 1]);
		data3.push_back(data[i + 2]);
		data4.push_back(data[i + 3]);

	}

	int max1 = rec(data1);
	int max2 = rec(data2);
	int max3 = rec(data3);
	int max4 = rec(data4);
	return max1 + max2 + max3 + max4 + max;

}

int _tmain(int argc, _TCHAR* argv[])
{

	int n;
	cin >> n;

	vector<int> data(n);

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

	int result = rec(data);
	cout << result << endl;

	return 0;

	/*
	int n;
	cin >> n;

	if (n >= 0)
	{
		cout << n << endl;
		return 0;
	}

	stringstream stream;
	stringstream stream1;
	stringstream stream2;

	stream << n;

	string str = stream.str();

	if (str.length() <= 2)
	{
		cout << 0 << endl;
		return 0;
	}

	for (int i = 0; i < str.length() - 2; ++i)
	{
		stream1 << str[i];
	}
	stream1 << str[str.length() - 1];

	for (int i = 0; i < str.length() - 1; ++i)
	{
		stream2 << str[i];
	}

	int n1, n2;
	stream1 >> n1;
	stream2 >> n2;

	if (n1 > n2)
	{
		cout << n1 << endl;
	}
	else
	{
		cout << n2 << endl;
	}*/

	/*string str;
	cin >> str;

	vector<int> data(str.length() - 1, 0);

	F(data, i)
	{
		if (str[i] == str[i + 1])
		{
			data[i] = 1;
		}
	}

	vector<int> dataInteg(str.length(), 0);

	for (int i = 1; i < dataInteg.size(); ++i)
	{

		dataInteg[i] = data[i - 1] + dataInteg[i - 1];
	}

	int m;
	cin >> m;

	for (int i = 0; i < m;++i)
	{
		int l, r;
		cin >> l >> r;
		l--;
		r--;
		r = min(r, (int)dataInteg.size() - 1);

		cout <<dataInteg[r] - dataInteg[l] << endl;

	}*/

}


// CFR186_div2.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>

#include "vector"
#include "string"
#include "set"
#include "sstream"
#include "algorithm"
#include "cmath"
#include "cassert"
#include "iostream"

using namespace std;

#define int64 long long
#define F(vec, index) for (int index = 0; index  < vec.size(); ++index)
#define F_S(vec, index, start) for (int index = start; index  < vec.size(); ++index)

vector<int64> data1;
	vector<int64> data2;
	vector<int64> data3;
	vector<int64> data4;
	vector<int64> dataSave;

int64 rec(vector<int64>& data, int start, int end)
{
	if (end - start == 1)
	{
		return  data[start];
	}
	//sort(data.rbegin(), data.rend());

	int64 max = data[start];

	data1.clear();
	data2.clear();
	data3.clear();
	data4.clear();

	for (int i = start; i < end; i += 4)
	{
		data1.push_back(data[i]);
		data2.push_back(data[i + 1]);
		data3.push_back(data[i + 2]);
		data4.push_back(data[i + 3]);

	}

	for (int i = 0; i < data1.size(); i++)
	{
		data[start + i ] = data1[i];
	}
	for (int i = 0 ; i < data1.size(); i++)
	{
		data[start + i + data1.size()] = data2[i];
	}

	for (int i = 0 ; i < data1.size(); i++)
	{
		data[start + i + data1.size() * 2] = data3[i];
	}

	for (int i = 0 ; i < data1.size(); i++)
	{
		data[start + i + data1.size() * 3] = data4[i];
	}

	int64 max1 = rec(data, start                   ,  start + data1.size());
	int64 max2 = rec(data, start + data1.size()    ,  start + 2 * data1.size());
	int64 max3 = rec(data, start + 2 * data1.size(),  start + 3 * data1.size());
	int64 max4 = rec(data, start + 3 * data1.size(),  start + 4 * data1.size());
	return max1 + max2 + max3 + max4 + max;

}

int _tmain(int argc, _TCHAR* argv[])
{

	int n;
	cin >> n;

	vector<int64> data(n);

	for (int i  = 0; i < n; ++i)
	{
		cin >> data[i];
	}
	sort(data.rbegin(), data.rend());

	int64 result = rec(data, 0, data.size());

	cout << result << endl;

	return 0;

	/*
	int n;
	cin >> n;

	if (n >= 0)
	{
		cout << n << endl;
		return 0;
	}

	stringstream stream;
	stringstream stream1;
	stringstream stream2;

	stream << n;

	string str = stream.str();

	if (str.length() <= 2)
	{
		cout << 0 << endl;
		return 0;
	}

	for (int i = 0; i < str.length() - 2; ++i)
	{
		stream1 << str[i];
	}
	stream1 << str[str.length() - 1];

	for (int i = 0; i < str.length() - 1; ++i)
	{
		stream2 << str[i];
	}

	int n1, n2;
	stream1 >> n1;
	stream2 >> n2;

	if (n1 > n2)
	{
		cout << n1 << endl;
	}
	else
	{
		cout << n2 << endl;
	}*/

	/*string str;
	cin >> str;

	vector<int> data(str.length() - 1, 0);

	F(data, i)
	{
		if (str[i] == str[i + 1])
		{
			data[i] = 1;
		}
	}

	vector<int> dataInteg(str.length(), 0);

	for (int i = 1; i < dataInteg.size(); ++i)
	{

		dataInteg[i] = data[i - 1] + dataInteg[i - 1];
	}

	int m;
	cin >> m;

	for (int i = 0; i < m;++i)
	{
		int l, r;
		cin >> l >> r;
		l--;
		r--;
		r = min(r, (int)dataInteg.size() - 1);

		cout <<dataInteg[r] - dataInteg[l] << endl;

	}*/

}


// CFR186_div2.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>

#include "vector"
#include "string"
#include "set"
#include "sstream"
#include "algorithm"
#include "cmath"
#include "cassert"
#include "iostream"

using namespace std;

#define int64 long long
#define F(vec, index) for (int index = 0; index  < vec.size(); ++index)
#define F_S(vec, index, start) for (int index = start; index  < vec.size(); ++index)

int64 rec(vector<int64>& data)
{
	if (data.size() == 1)
	{
		return  data[0];
	}
	//sort(data.rbegin(), data.rend());

	int64 max = data[0];

	vector<int64> data1;
	vector<int64> data2;
	vector<int64> data3;
	vector<int64> data4;

	for (int i = 0; i < data.size(); i += 4)
	{
		data1.push_back(data[i]);
		data2.push_back(data[i + 1]);
		data3.push_back(data[i + 2]);
		data4.push_back(data[i + 3]);

	}

	int64 max1 = rec(data1);
	int64 max2 = rec(data2);
	int64 max3 = rec(data3);
	int64 max4 = rec(data4);
	return max1 + max2 + max3 + max4 + max;

}

int _tmain(int argc, _TCHAR* argv[])
{

	int n;
	cin >> n;

	vector<int64> data(n);

	for (int i  = 0; i < n; ++i)
	{
		cin >> data[i];
	}
	sort(data.rbegin(), data.rend());

	int64 result = rec(data);

	cout << result << endl;

	return 0;

	/*
	int n;
	cin >> n;

	if (n >= 0)
	{
		cout << n << endl;
		return 0;
	}

	stringstream stream;
	stringstream stream1;
	stringstream stream2;

	stream << n;

	string str = stream.str();

	if (str.length() <= 2)
	{
		cout << 0 << endl;
		return 0;
	}

	for (int i = 0; i < str.length() - 2; ++i)
	{
		stream1 << str[i];
	}
	stream1 << str[str.length() - 1];

	for (int i = 0; i < str.length() - 1; ++i)
	{
		stream2 << str[i];
	}

	int n1, n2;
	stream1 >> n1;
	stream2 >> n2;

	if (n1 > n2)
	{
		cout << n1 << endl;
	}
	else
	{
		cout << n2 << endl;
	}*/

	/*string str;
	cin >> str;

	vector<int> data(str.length() - 1, 0);

	F(data, i)
	{
		if (str[i] == str[i + 1])
		{
			data[i] = 1;
		}
	}

	vector<int> dataInteg(str.length(), 0);

	for (int i = 1; i < dataInteg.size(); ++i)
	{

		dataInteg[i] = data[i - 1] + dataInteg[i - 1];
	}

	int m;
	cin >> m;

	for (int i = 0; i < m;++i)
	{
		int l, r;
		cin >> l >> r;
		l--;
		r--;
		r = min(r, (int)dataInteg.size() - 1);

		cout <<dataInteg[r] - dataInteg[l] << endl;

	}*/

}


// CFR186_div2.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>

#include "vector"
#include "string"
#include "set"
#include "sstream"
#include "algorithm"
#include "cmath"
#include "cassert"
#include "iostream"

using namespace std;

#define int64 long long
#define F(vec, index) for (int index = 0; index  < vec.size(); ++index)
#define F_S(vec, index, start) for (int index = start; index  < vec.size(); ++index)

vector<int64> data1;
	vector<int64> data2;
	vector<int64> data3;
	vector<int64> data4;

int64 rec(vector<int64>& data, int start, int end)
{
	if (end - start == 1)
	{
		return  data[start];
	}
	//sort(data.rbegin(), data.rend());

	int64 max = data[0];

	data1.clear();
	data2.clear();
	data3.clear();
	data4.clear();

	for (int i = start; i < end; i += 4)
	{
		data1.push_back(data[i]);
		data2.push_back(data[i + 1]);
		data3.push_back(data[i + 2]);
		data4.push_back(data[i + 3]);

	}

	for (int i = 0; i < data1.size(); i++)
	{
		data[start + i] = data1[i];
	}
	for (int i = 0 + data1.size() ; i < 2 * data1.size(); i++)
	{
		data[start + i] = data2[i];
	}

	for (int i = 0 + 2 * data1.size(); i < 3 * data1.size(); i++)
	{
		data[start + i] = data3[i];
	}

	for (int i = 0 + 3 * data1.size(); i < 4 * data1.size(); i++)
	{
		data[start + i] = data4[i];
	}

	int64 max1 = rec(data, start, start + data1.size());
	int64 max2 = rec(data,  start + data1.size(),  start + 2 * data1.size());
	int64 max3 = rec(data, start + 2 * data1.size(),  start + 3 * data1.size());
	int64 max4 = rec(data, start + 3 * data1.size(),  start + 4 * data1.size());
	return max1 + max2 + max3 + max4 + max;

}

int _tmain(int argc, _TCHAR* argv[])
{

	int n;
	cin >> n;

	vector<int64> data(n);

	for (int i  = 0; i < n; ++i)
	{
		cin >> data[i];
	}
	sort(data.rbegin(), data.rend());

	int64 result = rec(data, 0, data.size());

	cout << result << endl;

	return 0;

	/*
	int n;
	cin >> n;

	if (n >= 0)
	{
		cout << n << endl;
		return 0;
	}

	stringstream stream;
	stringstream stream1;
	stringstream stream2;

	stream << n;

	string str = stream.str();

	if (str.length() <= 2)
	{
		cout << 0 << endl;
		return 0;
	}

	for (int i = 0; i < str.length() - 2; ++i)
	{
		stream1 << str[i];
	}
	stream1 << str[str.length() - 1];

	for (int i = 0; i < str.length() - 1; ++i)
	{
		stream2 << str[i];
	}

	int n1, n2;
	stream1 >> n1;
	stream2 >> n2;

	if (n1 > n2)
	{
		cout << n1 << endl;
	}
	else
	{
		cout << n2 << endl;
	}*/

	/*string str;
	cin >> str;

	vector<int> data(str.length() - 1, 0);

	F(data, i)
	{
		if (str[i] == str[i + 1])
		{
			data[i] = 1;
		}
	}

	vector<int> dataInteg(str.length(), 0);

	for (int i = 1; i < dataInteg.size(); ++i)
	{

		dataInteg[i] = data[i - 1] + dataInteg[i - 1];
	}

	int m;
	cin >> m;

	for (int i = 0; i < m;++i)
	{
		int l, r;
		cin >> l >> r;
		l--;
		r--;
		r = min(r, (int)dataInteg.size() - 1);

		cout <<dataInteg[r] - dataInteg[l] << endl;

	}*/

}


// CFR186_div2.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>

#include "vector"
#include "string"
#include "set"
#include "sstream"
#include "algorithm"
#include "cmath"
#include "cassert"
#include "iostream"

using namespace std;

#define int64 long long
#define F(vec, index) for (int index = 0; index  < vec.size(); ++index)
#define F_S(vec, index, start) for (int index = start; index  < vec.size(); ++index)

vector<int64> data1;
	vector<int64> data2;
	vector<int64> data3;
	vector<int64> data4;
	vector<int64> dataSave;

int64 rec(vector<int64>& data, int64 start, int64 end)
{
	int64 max = data[start];
	if (end - start == 1)
	{
		return  max;
	}
	//sort(data.rbegin(), data.rend());

	data1.clear();
	data2.clear();
	data3.clear();
	data4.clear();

	for (int i = start; i < end; i += 4)
	{
		data1.push_back(data[i]);
		data2.push_back(data[i + 1]);
		data3.push_back(data[i + 2]);
		data4.push_back(data[i + 3]);

	}

	for (int i = 0; i < data1.size(); i++)
	{
		data[start + i ] = data1[i];
	}
	for (int i = 0 ; i < data1.size(); i++)
	{
		data[start + i + data1.size()] = data2[i];
	}

	for (int i = 0 ; i < data1.size(); i++)
	{
		data[start + i + data1.size() * 2] = data3[i];
	}

	for (int i = 0 ; i < data1.size(); i++)
	{
		data[start + i + data1.size() * 3] = data4[i];
	}

	int64 max1 = rec(data, start                   ,  start + data1.size());
	int64 max2 = rec(data, start + data1.size()    ,  start + 2 * data1.size());
	int64 max3 = rec(data, start + 2 * data1.size(),  start + 3 * data1.size());
	int64 max4 = rec(data, start + 3 * data1.size(),  start + 4 * data1.size());
	return max1 + max2 + max3 + max4 + max;

}

int _tmain(int argc, _TCHAR* argv[])
{

	int64 n;
	cin >> n;

	vector<int64> data(n);

	for (int i  = 0; i < n; ++i)
	{
		cin >> data[i];
	}
	sort(data.rbegin(), data.rend());

	int64 result = rec(data, 0, data.size());

	cout << result << endl;

	return 0;

	/*
	int n;
	cin >> n;

	if (n >= 0)
	{
		cout << n << endl;
		return 0;
	}

	stringstream stream;
	stringstream stream1;
	stringstream stream2;

	stream << n;

	string str = stream.str();

	if (str.length() <= 2)
	{
		cout << 0 << endl;
		return 0;
	}

	for (int i = 0; i < str.length() - 2; ++i)
	{
		stream1 << str[i];
	}
	stream1 << str[str.length() - 1];

	for (int i = 0; i < str.length() - 1; ++i)
	{
		stream2 << str[i];
	}

	int n1, n2;
	stream1 >> n1;
	stream2 >> n2;

	if (n1 > n2)
	{
		cout << n1 << endl;
	}
	else
	{
		cout << n2 << endl;
	}*/

	/*string str;
	cin >> str;

	vector<int> data(str.length() - 1, 0);

	F(data, i)
	{
		if (str[i] == str[i + 1])
		{
			data[i] = 1;
		}
	}

	vector<int> dataInteg(str.length(), 0);

	for (int i = 1; i < dataInteg.size(); ++i)
	{

		dataInteg[i] = data[i - 1] + dataInteg[i - 1];
	}

	int m;
	cin >> m;

	for (int i = 0; i < m;++i)
	{
		int l, r;
		cin >> l >> r;
		l--;
		r--;
		r = min(r, (int)dataInteg.size() - 1);

		cout <<dataInteg[r] - dataInteg[l] << endl;

	}*/

}


// CFR186_div2.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>

#include "vector"
#include "string"
#include "set"
#include "sstream"
#include "algorithm"
#include "cmath"
#include "cassert"
#include "iostream"

using namespace std;

#define int64 long long
#define F(vec, index) for (int index = 0; index  < vec.size(); ++index)
#define F_S(vec, index, start) for (int index = start; index  < vec.size(); ++index)

vector<int64> data1;
	vector<int64> data2;
	vector<int64> data3;
	vector<int64> data4;
	vector<int64> dataSave;

int64 rec(vector<int64>& data, int64 start, int64 end)
{
	int64 max = data[start];
	if (end - start == 1)
	{
		return  max;
	}

	data1.clear();
	data2.clear();
	data3.clear();
	data4.clear();

	for (int i = start; i < end; i += 4)
	{
		data1.push_back(data[i]);
		data2.push_back(data[i + 1]);
		data3.push_back(data[i + 2]);
		data4.push_back(data[i + 3]);

	}

	for (int i = 0; i < data1.size(); i++)
	{
		data[start + i ] = data1[i];
	}
	for (int i = 0 ; i < data2.size(); i++)
	{
		data[start + i + data1.size()] = data2[i];
	}

	for (int i = 0 ; i < data3.size(); i++)
	{
		data[start + i + data1.size() * 2] = data3[i];
	}

	for (int i = 0 ; i < data4.size(); i++)
	{
		data[start + i + data1.size() * 3] = data4[i];
	}
	int64 size = data1.size();

	int64 max1 = rec(data, start                   ,  start + size);
	int64 max2 = rec(data, start + size    ,  start + 2 * size);
	int64 max3 = rec(data, start + 2 * size,  start + 3 * size);
	int64 max4 = rec(data, start + 3 * size,  start + 4 * size);
	return max1 + max2 + max3 + max4 + max;

}

int _tmain(int argc, _TCHAR* argv[])
{

	int64 n;
	cin >> n;

	vector<int64> data(n);

	for (int i  = 0; i < n; ++i)
	{
		cin >> data[i];
	}
	sort(data.rbegin(), data.rend());

	int64 result = rec(data, 0, data.size());

	cout << result << endl;

	return 0;

	/*
	int n;
	cin >> n;

	if (n >= 0)
	{
		cout << n << endl;
		return 0;
	}

	stringstream stream;
	stringstream stream1;
	stringstream stream2;

	stream << n;

	string str = stream.str();

	if (str.length() <= 2)
	{
		cout << 0 << endl;
		return 0;
	}

	for (int i = 0; i < str.length() - 2; ++i)
	{
		stream1 << str[i];
	}
	stream1 << str[str.length() - 1];

	for (int i = 0; i < str.length() - 1; ++i)
	{
		stream2 << str[i];
	}

	int n1, n2;
	stream1 >> n1;
	stream2 >> n2;

	if (n1 > n2)
	{
		cout << n1 << endl;
	}
	else
	{
		cout << n2 << endl;
	}*/

	/*string str;
	cin >> str;

	vector<int> data(str.length() - 1, 0);

	F(data, i)
	{
		if (str[i] == str[i + 1])
		{
			data[i] = 1;
		}
	}

	vector<int> dataInteg(str.length(), 0);

	for (int i = 1; i < dataInteg.size(); ++i)
	{

		dataInteg[i] = data[i - 1] + dataInteg[i - 1];
	}

	int m;
	cin >> m;

	for (int i = 0; i < m;++i)
	{
		int l, r;
		cin >> l >> r;
		l--;
		r--;
		r = min(r, (int)dataInteg.size() - 1);

		cout <<dataInteg[r] - dataInteg[l] << endl;

	}*/

}


// CFR186_div2.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>

#include "vector"
#include "string"
#include "set"
#include "sstream"
#include "algorithm"
#include "cmath"
#include "cassert"
#include "iostream"

using namespace std;

#define int64 long long
#define F(vec, index) for (int index = 0; index  < vec.size(); ++index)
#define F_S(vec, index, start) for (int index = start; index  < vec.size(); ++index)

int64 rec(vector<int64>& data)
{
	if (data.size() == 1)
	{
		return  data[0];
	}
	//sort(data.rbegin(), data.rend());

	int64 max = data[0];

	vector<int64> data1;
	vector<int64> data2;
	vector<int64> data3;
	vector<int64> data4;

	for (int i = 0; i < data.size(); i += 4)
	{
		data1.push_back(data[i]);
		data2.push_back(data[i + 1]);
		data3.push_back(data[i + 2]);
		data4.push_back(data[i + 3]);

	}

	int64 max1 = rec(data1);
	int64 max2 = rec(data2);
	int64 max3 = rec(data3);
	int64 max4 = rec(data4);
	return max1 + max2 + max3 + max4 + max;

}

int _tmain(int argc, _TCHAR* argv[])
{

	int n;
	cin >> n;

	vector<int64> data(n);

	for (int i  = 0; i < n; ++i)
	{
		cin >> data[i];
	}
	sort(data.rbegin(), data.rend());

	int result = rec(data);
	cout << result << endl;

	return 0;

	/*
	int n;
	cin >> n;

	if (n >= 0)
	{
		cout << n << endl;
		return 0;
	}

	stringstream stream;
	stringstream stream1;
	stringstream stream2;

	stream << n;

	string str = stream.str();

	if (str.length() <= 2)
	{
		cout << 0 << endl;
		return 0;
	}

	for (int i = 0; i < str.length() - 2; ++i)
	{
		stream1 << str[i];
	}
	stream1 << str[str.length() - 1];

	for (int i = 0; i < str.length() - 1; ++i)
	{
		stream2 << str[i];
	}

	int n1, n2;
	stream1 >> n1;
	stream2 >> n2;

	if (n1 > n2)
	{
		cout << n1 << endl;
	}
	else
	{
		cout << n2 << endl;
	}*/

	/*string str;
	cin >> str;

	vector<int> data(str.length() - 1, 0);

	F(data, i)
	{
		if (str[i] == str[i + 1])
		{
			data[i] = 1;
		}
	}

	vector<int> dataInteg(str.length(), 0);

	for (int i = 1; i < dataInteg.size(); ++i)
	{

		dataInteg[i] = data[i - 1] + dataInteg[i - 1];
	}

	int m;
	cin >> m;

	for (int i = 0; i < m;++i)
	{
		int l, r;
		cin >> l >> r;
		l--;
		r--;
		r = min(r, (int)dataInteg.size() - 1);

		cout <<dataInteg[r] - dataInteg[l] << endl;

	}*/

}


// CFR186_div2.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>

#include "vector"
#include "string"
#include "set"
#include "sstream"
#include "algorithm"
#include "cmath"
#include "cassert"
#include "iostream"

using namespace std;

#define int64 long long
#define F(vec, index) for (int index = 0; index  < vec.size(); ++index)
#define F_S(vec, index, start) for (int index = start; index  < vec.size(); ++index)

vector<int64> data1;
	vector<int64> data2;
	vector<int64> data3;
	vector<int64> data4;

int64 rec(vector<int64>& data, int start, int end)
{
	if (start - end == 1)
	{
		return  data[start];
	}
	//sort(data.rbegin(), data.rend());

	int64 max = data[0];

	data1.clear();
	data2.clear();
	data3.clear();
	data4.clear();

	for (int i = start; i < end; i += 4)
	{
		data1.push_back(data[i]);
		data2.push_back(data[i + 1]);
		data3.push_back(data[i + 2]);
		data4.push_back(data[i + 3]);

	}

	for (int i = 0; i < data1.size(); i++)
	{
		data[start + i] = data1[i];
	}
	for (int i = 0 + data1.size() ; i < 2 * data1.size(); i++)
	{
		data[start + i] = data2[i];
	}

	for (int i = 0 + 2 * data1.size(); i < 3 * data1.size(); i++)
	{
		data[start + i] = data3[i];
	}

	for (int i = 0 + 3 * data1.size(); i < 4 * data1.size(); i++)
	{
		data[start + i] = data4[i];
	}

	int64 max1 = rec(data, start, start + data1.size());
	int64 max2 = rec(data,  start + data1.size(),  start + 2 * data1.size());
	int64 max3 = rec(data, start + 2 * data1.size(),  start + 3 * data1.size());
	int64 max4 = rec(data, start + 3 * data1.size(),  start + 4 * data1.size());
	return max1 + max2 + max3 + max4 + max;

}

int _tmain(int argc, _TCHAR* argv[])
{

	int n;
	cin >> n;

	vector<int64> data(n);

	for (int i  = 0; i < n; ++i)
	{
		cin >> data[i];
	}
	sort(data.rbegin(), data.rend());

	int64 result = rec(data, 0, data.size());

	cout << result << endl;

	return 0;

	/*
	int n;
	cin >> n;

	if (n >= 0)
	{
		cout << n << endl;
		return 0;
	}

	stringstream stream;
	stringstream stream1;
	stringstream stream2;

	stream << n;

	string str = stream.str();

	if (str.length() <= 2)
	{
		cout << 0 << endl;
		return 0;
	}

	for (int i = 0; i < str.length() - 2; ++i)
	{
		stream1 << str[i];
	}
	stream1 << str[str.length() - 1];

	for (int i = 0; i < str.length() - 1; ++i)
	{
		stream2 << str[i];
	}

	int n1, n2;
	stream1 >> n1;
	stream2 >> n2;

	if (n1 > n2)
	{
		cout << n1 << endl;
	}
	else
	{
		cout << n2 << endl;
	}*/

	/*string str;
	cin >> str;

	vector<int> data(str.length() - 1, 0);

	F(data, i)
	{
		if (str[i] == str[i + 1])
		{
			data[i] = 1;
		}
	}

	vector<int> dataInteg(str.length(), 0);

	for (int i = 1; i < dataInteg.size(); ++i)
	{

		dataInteg[i] = data[i - 1] + dataInteg[i - 1];
	}

	int m;
	cin >> m;

	for (int i = 0; i < m;++i)
	{
		int l, r;
		cin >> l >> r;
		l--;
		r--;
		r = min(r, (int)dataInteg.size() - 1);

		cout <<dataInteg[r] - dataInteg[l] << endl;

	}*/

}


// CFR186_div2.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>

#include "vector"
#include "string"
#include "set"
#include "sstream"
#include "algorithm"
#include "cmath"
#include "cassert"
#include "iostream"

using namespace std;

#define int64 long long
#define F(vec, index) for (int index = 0; index  < vec.size(); ++index)
#define F_S(vec, index, start) for (int index = start; index  < vec.size(); ++index)

int rec(vector<int>& data)
{
	if (data.size() == 1)
	{
		return  data[0];
	}
	//sort(data.rbegin(), data.rend());

	int max = data[0];

	vector<int> data1;
	vector<int> data2;
	vector<int> data3;
	vector<int> data4;

	for (int i = 0; i < data.size(); i += 4)
	{
		data1.push_back(data[i]);
		data2.push_back(data[i + 1]);
		data3.push_back(data[i + 2]);
		data4.push_back(data[i + 3]);

	}

	int max1 = rec(data1);
	int max2 = rec(data2);
	int max3 = rec(data3);
	int max4 = rec(data4);
	return max1 + max2 + max3 + max4 + max;

}

int _tmain(int argc, _TCHAR* argv[])
{

	int n;
	cin >> n;

	vector<int> data(n);

	for (int i  = 0; i < n; ++i)
	{
		cin >> data[i];
	}
	sort(data.rbegin(), data.rend());

	int result = rec(data);
	cout << result << endl;

	return 0;

	/*
	int n;
	cin >> n;

	if (n >= 0)
	{
		cout << n << endl;
		return 0;
	}

	stringstream stream;
	stringstream stream1;
	stringstream stream2;

	stream << n;

	string str = stream.str();

	if (str.length() <= 2)
	{
		cout << 0 << endl;
		return 0;
	}

	for (int i = 0; i < str.length() - 2; ++i)
	{
		stream1 << str[i];
	}
	stream1 << str[str.length() - 1];

	for (int i = 0; i < str.length() - 1; ++i)
	{
		stream2 << str[i];
	}

	int n1, n2;
	stream1 >> n1;
	stream2 >> n2;

	if (n1 > n2)
	{
		cout << n1 << endl;
	}
	else
	{
		cout << n2 << endl;
	}*/

	/*string str;
	cin >> str;

	vector<int> data(str.length() - 1, 0);

	F(data, i)
	{
		if (str[i] == str[i + 1])
		{
			data[i] = 1;
		}
	}

	vector<int> dataInteg(str.length(), 0);

	for (int i = 1; i < dataInteg.size(); ++i)
	{

		dataInteg[i] = data[i - 1] + dataInteg[i - 1];
	}

	int m;
	cin >> m;

	for (int i = 0; i < m;++i)
	{
		int l, r;
		cin >> l >> r;
		l--;
		r--;
		r = min(r, (int)dataInteg.size() - 1);

		cout <<dataInteg[r] - dataInteg[l] << endl;

	}*/

}


// CFR186_div2.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>

#include "vector"
#include "string"
#include "set"
#include "sstream"
#include "algorithm"
#include "cmath"
#include "cassert"
#include "iostream"

using namespace std;

#define int64 long long
#define F(vec, index) for (int index = 0; index  < vec.size(); ++index)
#define F_S(vec, index, start) for (int index = start; index  < vec.size(); ++index)

vector<int64> data1;
	vector<int64> data2;
	vector<int64> data3;
	vector<int64> data4;
	vector<int64> dataSave;

int64 rec(vector<int64>& data, int64 start, int64 end)
{
	int64 max = data[start];
	if (end - start == 1)
	{
		return  max;
	}

	data1.clear();
	data2.clear();
	data3.clear();
	data4.clear();

	for (int i = start; i < end; i += 4)
	{
		data1.push_back(data[i]);
		data2.push_back(data[i + 1]);
		data3.push_back(data[i + 2]);
		data4.push_back(data[i + 3]);

	}

	for (int i = 0; i < data1.size(); i++)
	{
		data[start + i ] = data1[i];
	}
	for (int i = 0 ; i < data1.size(); i++)
	{
		data[start + i + data1.size()] = data2[i];
	}

	for (int i = 0 ; i < data1.size(); i++)
	{
		data[start + i + data1.size() * 2] = data3[i];
	}

	for (int i = 0 ; i < data1.size(); i++)
	{
		data[start + i + data1.size() * 3] = data4[i];
	}

	int64 max1 = rec(data, start                   ,  start + data1.size());
	int64 max2 = rec(data, start + data1.size()    ,  start + 2 * data1.size());
	int64 max3 = rec(data, start + 2 * data1.size(),  start + 3 * data1.size());
	int64 max4 = rec(data, start + 3 * data1.size(),  start + 4 * data1.size());
	return max1 + max2 + max3 + max4 + max;

}

int _tmain(int argc, _TCHAR* argv[])
{

	int64 n;
	cin >> n;

	vector<int64> data(n);

	for (int i  = 0; i < n; ++i)
	{
		cin >> data[i];
	}
	sort(data.rbegin(), data.rend());

	int64 result = rec(data, 0, data.size());

	cout << result << endl;

	return 0;

	/*
	int n;
	cin >> n;

	if (n >= 0)
	{
		cout << n << endl;
		return 0;
	}

	stringstream stream;
	stringstream stream1;
	stringstream stream2;

	stream << n;

	string str = stream.str();

	if (str.length() <= 2)
	{
		cout << 0 << endl;
		return 0;
	}

	for (int i = 0; i < str.length() - 2; ++i)
	{
		stream1 << str[i];
	}
	stream1 << str[str.length() - 1];

	for (int i = 0; i < str.length() - 1; ++i)
	{
		stream2 << str[i];
	}

	int n1, n2;
	stream1 >> n1;
	stream2 >> n2;

	if (n1 > n2)
	{
		cout << n1 << endl;
	}
	else
	{
		cout << n2 << endl;
	}*/

	/*string str;
	cin >> str;

	vector<int> data(str.length() - 1, 0);

	F(data, i)
	{
		if (str[i] == str[i + 1])
		{
			data[i] = 1;
		}
	}

	vector<int> dataInteg(str.length(), 0);

	for (int i = 1; i < dataInteg.size(); ++i)
	{

		dataInteg[i] = data[i - 1] + dataInteg[i - 1];
	}

	int m;
	cin >> m;

	for (int i = 0; i < m;++i)
	{
		int l, r;
		cin >> l >> r;
		l--;
		r--;
		r = min(r, (int)dataInteg.size() - 1);

		cout <<dataInteg[r] - dataInteg[l] << endl;

	}*/

}


// CFR186_div2.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>

#include "vector"
#include "string"
#include "set"
#include "sstream"
#include "algorithm"
#include "cmath"
#include "cassert"
#include "iostream"

using namespace std;

#define int64 long long
#define F(vec, index) for (int index = 0; index  < vec.size(); ++index)
#define F_S(vec, index, start) for (int index = start; index  < vec.size(); ++index)

vector<int64> data1;
	vector<int64> data2;
	vector<int64> data3;
	vector<int64> data4;
	vector<int64> dataSave;

int64 rec(vector<int64>& data, int start, int end)
{
	if (end - start == 1)
	{
		return  data[start];
	}
	//sort(data.rbegin(), data.rend());

	int64 max = data[0];

	data1.clear();
	data2.clear();
	data3.clear();
	data4.clear();

	for (int i = start; i < end; i += 4)
	{
		data1.push_back(data[i]);
		data2.push_back(data[i + 1]);
		data3.push_back(data[i + 2]);
		data4.push_back(data[i + 3]);

	}

	for (int i = 0; i < data1.size(); i++)
	{
		data[start + i ] = data1[i];
	}
	for (int i = 0 ; i < data1.size(); i++)
	{
		data[start + i + data1.size()] = data2[i];
	}

	for (int i = 0 ; i < data1.size(); i++)
	{
		data[start + i + data1.size() * 2] = data3[i];
	}

	for (int i = 0 ; i < data1.size(); i++)
	{
		data[start + i + data1.size() * 3] = data4[i];
	}

	int64 max1 = rec(data, start, start + data1.size());
	int64 max2 = rec(data,  start + data1.size(),  start + 2 * data1.size());
	int64 max3 = rec(data, start + 2 * data1.size(),  start + 3 * data1.size());
	int64 max4 = rec(data, start + 3 * data1.size(),  start + 4 * data1.size());
	return max1 + max2 + max3 + max4 + max;

}

int _tmain(int argc, _TCHAR* argv[])
{

	int n;
	cin >> n;

	vector<int64> data(n);

	for (int i  = 0; i < n; ++i)
	{
		cin >> data[i];
	}
	sort(data.rbegin(), data.rend());

	int64 result = rec(data, 0, data.size());

	cout << result << endl;

	return 0;

	/*
	int n;
	cin >> n;

	if (n >= 0)
	{
		cout << n << endl;
		return 0;
	}

	stringstream stream;
	stringstream stream1;
	stringstream stream2;

	stream << n;

	string str = stream.str();

	if (str.length() <= 2)
	{
		cout << 0 << endl;
		return 0;
	}

	for (int i = 0; i < str.length() - 2; ++i)
	{
		stream1 << str[i];
	}
	stream1 << str[str.length() - 1];

	for (int i = 0; i < str.length() - 1; ++i)
	{
		stream2 << str[i];
	}

	int n1, n2;
	stream1 >> n1;
	stream2 >> n2;

	if (n1 > n2)
	{
		cout << n1 << endl;
	}
	else
	{
		cout << n2 << endl;
	}*/

	/*string str;
	cin >> str;

	vector<int> data(str.length() - 1, 0);

	F(data, i)
	{
		if (str[i] == str[i + 1])
		{
			data[i] = 1;
		}
	}

	vector<int> dataInteg(str.length(), 0);

	for (int i = 1; i < dataInteg.size(); ++i)
	{

		dataInteg[i] = data[i - 1] + dataInteg[i - 1];
	}

	int m;
	cin >> m;

	for (int i = 0; i < m;++i)
	{
		int l, r;
		cin >> l >> r;
		l--;
		r--;
		r = min(r, (int)dataInteg.size() - 1);

		cout <<dataInteg[r] - dataInteg[l] << endl;

	}*/

}