stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



using i64 = std::int_fast64_t;



#define REP(i, stop) FOR(i, 0, stop)

#define FOR(i, start, stop) for (int i = start, i##_len = stop; i < i##_len; ++i)

#define RREP(i, n) for (int i = n; i-- > 0;)

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



template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& a) { return is >> a.first >> a.second; }

template<typename T> istream& operator>>(istream& is, vector<T>& a) { for (T& x : a) is >> x; return is; }



struct InitCpp { InitCpp() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } initCpp;



signed main() {

  i64 N, M;

  cin >> N >> M;

  vector<i64> A(N);

  cin >> A;

  sort(ALL(A));

  vector<i64> S(N + 1);

  REP(i, N) S[i + 1] = S[i] + A[i];

  i64 h_ng = 0;

  i64 h_ok = A[N - 1] * 2 + 1;

  i64 h = h_ok;

  i64 ans = 0;

  while (h_ok > h_ng + 1) {

    h = (h_ok + h_ng) / 2;

    i64 s = 0;

    i64 q = 0;

    i64 r = 0;

    i64 ge = 0;

    i64 gt = 0;

    RREP(i, N) {

      while (ge < N && !(A[i] + A[ge] >= h)) ge++;

      while (gt < N && !(A[i] + A[gt] > h)) gt++;

      q += N - gt;

      r += gt - ge;

      s += S[N] - S[gt] + A[i] * (N - gt);

    }

    if (q <= M) {

      h_ok = h;

      if (M <= q + r) {

        cout << (s + (M - q) * h) << endl;

        exit(0);

      }

    } else {

      h_ng = h;

    }

  }

}


// Faster C++ solution for the same problem (measured speedup: 1.1010682464146866x):
#include <cstdio>

#include <algorithm>

#include <vector>

using namespace std;



using i64 = long long;



#define REP(i, n) for (int i = 0, i##_len = n; i < i##_len; ++i)

#define RREP(i, n) for (int i = n; i-- > 0;)



const int is = 1 << 17;

char ib[is], *ip = ib + is, it;

inline char getc() {

	if (ip == ib + is) { fread_unlocked(ib, 1, is, stdin); ip = ib; }

	return *ip++;

}

inline i64 read() {

	i64 r = 0;

	if (ip + 16 > ib + is) while ((it = getc()) & 16) r = r * 10 + it - '0';

	else while ((it = *ip++) & 16) r = r * 10 + it - '0';

	return r;

}



const int os = 1 << 17;

char ob[os], *op = ob, ot;

inline void putc(const char c) {

	*op++ = c;

	if (op == ob + os) { fwrite_unlocked(ob, 1, os, stdout); op = ob; }

}

// x shoud be greater than or equal to zero

inline void write(i64 x) {

	char d[20];

	int i = 0;

	while (true) {

		d[i] = x % 10 + 48;

		x /= 10;

		++i;

		if (x <= 0) break;

	}

	while (i--) {

		putc(d[i]);

	}

}



signed main() {

  i64 N = read();

  i64 M = read();

  vector<i64> A(N);

  REP(i, N) A[i] = read();

  sort(A.begin(), A.end());

  vector<i64> S(N + 1);

  REP(i, N) S[i + 1] = S[i] + A[i];

  i64 h_ng = 0;

  i64 h_ok = A[N - 1] * 2 + 1;

  i64 h = h_ok;

  i64 ans = 0;

  while (h_ok > h_ng + 1) {

    h = h_ng + h_ok >> 1;

    i64 s = 0;

    i64 q = 0;

    i64 gt = 0;

    RREP(i, N) {

      while (gt < N && !(A[i] + A[gt] > h)) gt++;

      q += N - gt;

      s += S[N] - S[gt] + A[i] * (N - gt);

    }

    if (q <= M) {

      h_ok = h;

      ans = s + (M - q) * h;

    } else {

      h_ng = h;

    }

  }

  write(ans);

  fwrite_unlocked(ob, 1, op - ob, stdout);

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;

const int N=2e5+5,mod=1e9+7;

#define ll long long

ll a[N],b[N];

ll cum[N];

ll ans=0,R;

ll n,k;

bool valid(int r)

{

    ans=0;

    ll t=k;

    for(int i=0;i<n;i++)

    {

        ll idx=lower_bound(b,b+n,r-a[i])-b;

        if(idx==n) continue;

        idx=n-1-idx;

        if(idx<i) continue;

        ll sz=idx-i+1;

        t-=sz*2-1;

        ll sum=cum[idx];

        if(i) sum-=cum[i-1];

        ans+=sum*2+a[i]*(sz-1)*2;

    }

    R=t;

    return t>=0;

}

ll bs()

{

    ll s=0,e=2e5+5,mid;

    while(s<e)

    {

        mid=(s+e)/2;

        if(valid(mid)) e=mid;

        else s=mid+1;

    }

    return s;

}

int main()

{

    ios_base::sync_with_stdio(0);

    cin.tie(0);cout.tie(0);

    cin>>n>>k;

    for(int i=0;i<n;i++)

        cin>>a[i],b[i]=a[i];

    sort(a,a+n);

    sort(b,b+n);

    reverse(a,a+n);

    cum[0]=a[0];

    for(int i=1;i<n;i++)

        cum[i]=cum[i-1]+a[i];



    ll B=bs();

    valid(B);

    cout<<ans+R*(B-1)<<endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.420035282050902x):
#include <cstdio>

#include <cctype>

#include <algorithm>

#define int long long



int n, m;

int a[100010], fs[100010];

int l = 0, r = 200010, ans, cnt, res;



int read() {

    int ret, f = 1;

    char ch;

    while(!isdigit(ch = getchar())) (ch == '-') && (f = -1);

    for(ret = ch - '0'; isdigit(ch = getchar()); ret *= 10, ret += ch - '0');

    return ret * f;

}



void print(int x) {

    if(x < 0) putchar('-'), x = -x;

    if(x > 9) print(x / 10);

    putchar(x % 10 + '0');

}



signed main() {

    n = read(), m = read();

    for(int i = 1; i <= n; ++i) a[i] = read();

    std::sort(a + 1, a + n + 1);

    for(int i = 1; i <= n; ++i) fs[i] = fs[i - 1] + a[i];

    while(l <= r) {

        int mid = (l + r) >> 1;

        cnt = res = 0;

        for(int i = 1; i <= n; ++i) {

            int pos = std::lower_bound(a + 1, a + n + 1, mid - a[i]) - a;

            res += fs[n] - fs[pos - 1] + a[i] * (n - pos + 1); 

            cnt += n - pos + 1;

        }

        if(cnt >= m) ans = mid, l = mid + 1;

        else r = mid - 1;

    }

    print(res - ans * (cnt - m));

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;

const int N=2e5+5,mod=1e9+7;

#define ll long long

ll a[N],b[N];

ll cum[N];

ll ans=0,R;

ll n,k;

bool valid(int r)

{

    ans=0;

    ll t=k;

    for(int i=0;i<n;i++)

    {

        ll idx=lower_bound(b,b+n,r-a[i])-b;

        if(idx==n) continue;

        idx=n-1-idx;

        if(idx<i) continue;

        ll sz=idx-i+1;

        t-=sz*2-1;

        ll sum=cum[idx];

        if(i) sum-=cum[i-1];

        ans+=sum*2+a[i]*(sz-1)*2;

    }

    R=t;

    return t>=0;

}

ll bs()

{

    ll s=0,e=2e5+5,mid;

    while(s<e)

    {

        mid=(s+e)/2;

        if(valid(mid)) e=mid;

        else s=mid+1;

    }

    return s;

}

int main()

{

    ios_base::sync_with_stdio(0);

    cin.tie(0);cout.tie(0);

    cin>>n>>k;

    for(int i=0;i<n;i++)

        cin>>a[i],b[i]=a[i];

    sort(a,a+n);

    sort(b,b+n);

    reverse(a,a+n);

    cum[0]=a[0];

    for(int i=1;i<n;i++)

        cum[i]=cum[i-1]+a[i];



    ll B=bs();

    valid(B);

    cout<<ans+R*(B-1)<<endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.234735424574246x):
#include<stdio.h>

#include<stdlib.h>

#include<algorithm>

using namespace std;

long long ha[100005],total[100005];

int inc(const void *a, const void *b)

{

    if(a<b)return -1;

    else return 1;

}

int main()

{

	long long m,n,l=0,r=200005,fenjie,sum,num,mid,kk,temp;

	//fenjie:二分答案的结果 sum:握手好感总和 num:握手次数 kk: 临界点 temp:区别于fenjie的临时值 

    scanf("%lld%lld",&n,&m);

    ha[0]=9999999;

    for(long long i=1;i<=n;i++)scanf("%lld",&ha[i]);

    sort(ha+1,ha+n+1);

    total[0]=0;

    for(long long i=1;i<=n;i++)total[i]=total[i-1]+ha[i];

    mid=(l+r)/2;

    while(l<=r)

	{

        sum=0;num=0;

        for(long long i=1;i<=n;i++)

		{

            kk=lower_bound(&ha[1],&ha[n+1],mid-ha[i])-&ha[0];

            num+=n-kk+1;

        }

		temp=mid;

        if(num>=m)

		{

            fenjie=mid;

            l=mid+1;

        }

        else

		{

			//fenjie=mid;

			r=mid-1;

		}

        mid=(l+r)/2;

    }

    for(long long i=1;i<=n;i++)

	{

        kk=lower_bound(&ha[1],&ha[n+1],temp-ha[i])-&ha[0];

        sum+=total[n]-total[kk-1];

        sum+=ha[i]*(n-kk+1);

    }

    if(m==num)printf("%lld\n",sum);

    else if(m>num)printf("%lld\n",sum+(m-num)*fenjie);

    else printf("%lld\n",sum-(num-m)*fenjie);

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;

const int N=2e5+5,mod=1e9+7;

#define ll long long

ll a[N],b[N];

ll cum[N];

ll ans=0,R;

ll n,k;

bool valid(int r)

{

    ans=0;

    ll t=k;

    for(int i=0;i<n;i++)

    {

        ll idx=lower_bound(b,b+n,r-a[i])-b;

        if(idx==n) continue;

        idx=n-1-idx;

        if(idx<i) continue;

        ll sz=idx-i+1;

        t-=sz*2-1;

        ll sum=cum[idx];

        if(i) sum-=cum[i-1];

        ans+=sum*2+a[i]*(sz-1)*2;

    }

    R=t;

    return t>=0;

}

ll bs()

{

    ll s=0,e=2e5+5,mid;

    while(s<e)

    {

        mid=(s+e)/2;

        if(valid(mid)) e=mid;

        else s=mid+1;

    }

    return s;

}

int main()

{

    ios_base::sync_with_stdio(0);

    cin.tie(0);cout.tie(0);

    cin>>n>>k;

    for(int i=0;i<n;i++)

        cin>>a[i],b[i]=a[i];

    sort(a,a+n);

    sort(b,b+n);

    reverse(a,a+n);

    cum[0]=a[0];

    for(int i=1;i<n;i++)

        cum[i]=cum[i-1]+a[i];



    ll B=bs();

    valid(B);

    cout<<ans+R*(B-1)<<endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.253187158039871x):
#include<cstdio>

#include<algorithm>

int n,f[100005];

long long m;

long long calc(int k)

{

	int l,r;

	long long cnt=0;

	r=n+1;

	for (l=1;l<=n;l++)

	{

		while (f[l]+f[r-1]>=k&&r>1) --r;

		cnt=cnt+(n-r+1);

	}

	return cnt;

}

int bs(int l,int r)

{

	if (l==r) return l;

	int mid=(l+r+1)/2;

	if (calc(mid)>=m) return bs(mid,r);

	else return bs(l,mid-1);

}

long long solve(int k,long long p)

{

	int l,r;

	long long ans=0,cnt=0;

	r=n+1;

	for (l=1;l<=n;l++)

	{

		while (f[l]+f[r-1]>=k&&r>1) --r;

		ans=ans+1ll*(n-r+1)*f[l];

		cnt=cnt+(n-r+1);

	}

	ans=ans*2;

	if (cnt>p) ans=ans-k*(cnt-p);

	return ans;

}

int main()

{

	scanf("%d%lld",&n,&m);

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

	std::sort(f+1,f+n+1);

	int k=bs(1,200000);

	printf("%lld\n",solve(k,m));

	return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <algorithm>

#include <functional>

#include <set>



int n;

int S[1<<18];



int main(){

  std::multiset<int, std::greater<int> > S, T;

  scanf("%d",&n);

  for(int i=0;i<(1<<n);i++){

    int x;

    scanf("%d",&x);

    S.insert(x);

  }

  T.insert(*(S.begin()));

  S.erase(S.begin());



  for(int i=0;i<n;i++){

    int tmp[1<<18];

    int t=0;

    auto itr = S.begin();

    for(int x: T){

      while(itr != S.end() && *itr == x) ++itr;

      if(itr == S.end()){

        puts("No");

        return 0;

      }

      tmp[t++] = *itr;

      itr = S.erase(itr);

    }

    if(i != n-1) T.insert(tmp, tmp+t);

  }



  puts("Yes");





  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.029399266498961x):
#include <cstdio>

#include <algorithm>

#include <functional>



int n;

int S[1<<18];

int Q[19];





int main(){

  scanf("%d",&n);

  for(int i=0;i<(1<<n);i++) scanf("%d",S+i);

  std::sort(S, S+(1<<n), std::greater<int>());

  int k = 0;

  for(int i=0;i<(1<<n);){

    int j = i;

    for(i++; S[i] == S[j]; i++) ;

    S[k++] = i-j;

  }

  Q[n] = 1;

  for(int i=0;i<k;i++){

    int l;

    int a=0;

    for(l=n;l>=0 && a < S[i];l--) a += Q[l];

    if(a<S[i]){

      puts("No");

      return 0;

    }

    l++;

    for(int t=0;t<l;t++) Q[t] += Q[l]-a+S[i];

    Q[l++] = a-S[i];

    for(; l<n; l++){

      for(int t=0;t<l;t++) Q[t] += Q[l];

      Q[l] = 0;

    }

  }

  puts("Yes");

  return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <queue>

#include <algorithm>



int n;

int S[1<<18];

std::priority_queue<int> Q;

std::vector<int> buf;



int main(){

  scanf("%d",&n);

  for(int i=0;i<(1<<n);i++) scanf("%d",S+i);

  std::sort(S, S+(1<<n), std::greater<int>());

  int k = 0;

  for(int i=0;i<(1<<n);){

    int j = i;

    for(i++; S[i] == S[j]; i++) ;

    S[k++] = i-j;

  }

  Q.push((1<<(n+1))-1);

  for(int i=0;i<k;i++){

    if(Q.size() < S[i]){

      puts("No");

      return 0;

    }

    for(int j=0;j<S[i];j++){

      int t = Q.top();

      Q.pop();

      for(t>>=1;t;t>>=1){

        buf.push_back(t);

      }

    }

    for(int x: buf) Q.push(x);

    buf.clear();

  }

  puts("Yes");

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.902579394750596x):
#include <cstdio>

#include <algorithm>

#include <functional>



int n;

int S[1<<18];

int Q[19];





int main(){

  scanf("%d",&n);

  for(int i=0;i<(1<<n);i++) scanf("%d",S+i);

  std::sort(S, S+(1<<n), std::greater<int>());

  int k = 0;

  for(int i=0;i<(1<<n);){

    int j = i;

    for(i++; S[i] == S[j]; i++) ;

    S[k++] = i-j;

  }

  Q[n] = 1;

  for(int i=0;i<k;i++){

    int l;

    int a=0;

    for(l=n;l>=0 && a < S[i];l--) a += Q[l];

    if(a<S[i]){

      puts("No");

      return 0;

    }

    l++;

    for(int t=0;t<l;t++) Q[t] += Q[l]-a+S[i];

    Q[l++] = a-S[i];

    for(; l<n; l++){

      for(int t=0;t<l;t++) Q[t] += Q[l];

      Q[l] = 0;

    }

  }

  puts("Yes");

  return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <algorithm>

#include <functional>

#include <set>



int n;

int S[1<<18];



int main(){

  std::multiset<int, std::greater<int> > S, T;

  scanf("%d",&n);

  for(int i=0;i<(1<<n);i++){

    int x;

    scanf("%d",&x);

    S.insert(x);

  }

  T.insert(*(S.begin()));

  S.erase(S.begin());



  for(int i=0;i<n;i++){

    int tmp[1<<18];

    int t=0;

    auto itr = S.begin();

    for(int x: T){

      while(itr != S.end() && *itr == x) ++itr;

      if(itr == S.end()){

        puts("No");

        return 0;

      }

      tmp[t++] = *itr;

      itr = S.erase(itr);

    }

    if(i != n-1) T.insert(tmp, tmp+t);

  }



  puts("Yes");





  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.036334198715715x):
#include <cstdio>

#include <algorithm>

#include <functional>



int n;

int S[1<<18];

int Q[19];





int main(){

  scanf("%d",&n);

  for(int i=0;i<(1<<n);i++) scanf("%d",S+i);

  std::sort(S, S+(1<<n), std::greater<int>());

  int k = 0;

  for(int i=0;i<(1<<n);){

    int j = i;

    for(i++; S[i] == S[j]; i++) ;

    S[k++] = i-j;

  }

  Q[n] = 1;

  for(int i=0;i<k;i++){

//for(int t=0;t<=n;t++) printf("%d ", Q[t]);

//puts("");

    int l;

    int a=0;

    for(l=n;l>=0 && a < S[i];l--) a += Q[l];

    if(a<S[i]){

      puts("No");

      return 0;

    }

    l++;

    for(int t=0;t<l;t++) Q[t] += S[i];

    int b = a-Q[l];

    Q[l] = a-S[i]+b;

    for(int t=l+1;t<=n;t++){

      b -= Q[t];

      Q[t] = b;

    }

  }

  puts("Yes");

  return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <queue>

#include <algorithm>



int n;

int S[1<<18];

std::priority_queue<int> Q;

std::vector<int> buf;



int main(){

  scanf("%d",&n);

  for(int i=0;i<(1<<n);i++) scanf("%d",S+i);

  std::sort(S, S+(1<<n), std::greater<int>());

  int k = 0;

  for(int i=0;i<(1<<n);){

    int j = i;

    for(i++; S[i] == S[j]; i++) ;

    S[k++] = i-j;

  }

  Q.push((1<<(n+1))-1);

  for(int i=0;i<k;i++){

    if(Q.size() < S[i]){

      puts("No");

      return 0;

    }

    for(int j=0;j<S[i];j++){

      int t = Q.top();

      Q.pop();

      for(t>>=1;t;t>>=1){

        buf.push_back(t);

      }

    }

    for(int x: buf) Q.push(x);

    buf.clear();

  }

  puts("Yes");

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.90933945772995x):
#include <cstdio>

#include <algorithm>

#include <functional>



int n;

int S[1<<18];

int Q[19];





int main(){

  scanf("%d",&n);

  for(int i=0;i<(1<<n);i++) scanf("%d",S+i);

  std::sort(S, S+(1<<n), std::greater<int>());

  int k = 0;

  for(int i=0;i<(1<<n);){

    int j = i;

    for(i++; S[i] == S[j]; i++) ;

    S[k++] = i-j;

  }

  Q[n] = 1;

  for(int i=0;i<k;i++){

//for(int t=0;t<=n;t++) printf("%d ", Q[t]);

//puts("");

    int l;

    int a=0;

    for(l=n;l>=0 && a < S[i];l--) a += Q[l];

    if(a<S[i]){

      puts("No");

      return 0;

    }

    l++;

    for(int t=0;t<l;t++) Q[t] += S[i];

    int b = a-Q[l];

    Q[l] = a-S[i]+b;

    for(int t=l+1;t<=n;t++){

      b -= Q[t];

      Q[t] = b;

    }

  }

  puts("Yes");

  return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <algorithm>

#include <functional>

#include <set>



int n;

int S[1<<18];



int main(){

  std::multiset<int, std::greater<int> > S, T;

  scanf("%d",&n);

  for(int i=0;i<(1<<n);i++){

    int x;

    scanf("%d",&x);

    S.insert(x);

  }

  T.insert(*(S.begin()));

  S.erase(S.begin());



  for(int i=0;i<n;i++){

    int tmp[1<<18];

    int t=0;

    auto itr = S.begin();

    for(int x: T){

      while(itr != S.end() && *itr == x) ++itr;

      if(itr == S.end()){

        puts("No");

        return 0;

      }

      tmp[t++] = *itr;

      itr = S.erase(itr);

    }

    if(i != n-1) T.insert(tmp, tmp+t);

  }



  puts("Yes");





  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.039218187887105x):
#include <cstdio>

#include <algorithm>

#include <functional>



int n;

int S[1<<18];

int Q[19];





int main(){

  scanf("%d",&n);

  for(int i=0;i<(1<<n);i++) scanf("%d",S+i);

  std::sort(S, S+(1<<n), std::greater<int>());

  int k = 0;

  for(int i=0;i<(1<<n);){

    int j = i;

    for(i++; S[i] == S[j]; i++) ;

    S[k++] = i-j;

  }

  Q[n] = 1;

  for(int i=0;i<k;i++){

    int l;

    int a=0;

    for(l=n;l>=0 && a < S[i];l--) a += Q[l];

    if(a<S[i]){

      puts("No");

      return 0;

    }

    l++;

    for(int t=0;t<l;t++) Q[t] += S[i];

    int b = a-Q[l];

    Q[l] = a-S[i]+b;

    for(int t=l+1;b;t++){

      b -= Q[t];

      Q[t] = b;

    }

  }

  puts("Yes");

  return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <queue>

#include <algorithm>



int n;

int S[1<<18];

std::priority_queue<int> Q;

std::vector<int> buf;



int main(){

  scanf("%d",&n);

  for(int i=0;i<(1<<n);i++) scanf("%d",S+i);

  std::sort(S, S+(1<<n), std::greater<int>());

  int k = 0;

  for(int i=0;i<(1<<n);){

    int j = i;

    for(i++; S[i] == S[j]; i++) ;

    S[k++] = i-j;

  }

  Q.push((1<<(n+1))-1);

  for(int i=0;i<k;i++){

    if(Q.size() < S[i]){

      puts("No");

      return 0;

    }

    for(int j=0;j<S[i];j++){

      int t = Q.top();

      Q.pop();

      for(t>>=1;t;t>>=1){

        buf.push_back(t);

      }

    }

    for(int x: buf) Q.push(x);

    buf.clear();

  }

  puts("Yes");

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.9121507250676855x):
#include <cstdio>

#include <algorithm>

#include <functional>



int n;

int S[1<<18];

int Q[19];





int main(){

  scanf("%d",&n);

  for(int i=0;i<(1<<n);i++) scanf("%d",S+i);

  std::sort(S, S+(1<<n), std::greater<int>());

  int k = 0;

  for(int i=0;i<(1<<n);){

    int j = i;

    for(i++; S[i] == S[j]; i++) ;

    S[k++] = i-j;

  }

  Q[n] = 1;

  for(int i=0;i<k;i++){

    int l;

    int a=0;

    for(l=n;l>=0 && a < S[i];l--) a += Q[l];

    if(a<S[i]){

      puts("No");

      return 0;

    }

    l++;

    for(int t=0;t<l;t++) Q[t] += S[i];

    int b = a-Q[l];

    Q[l] = a-S[i]+b;

    for(int t=l+1;b;t++){

      b -= Q[t];

      Q[t] = b;

    }

  }

  puts("Yes");

  return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <algorithm>

#include <functional>

#include <set>



int n;

int S[1<<18];



int main(){

  std::multiset<int, std::greater<int> > S, T;

  scanf("%d",&n);

  for(int i=0;i<(1<<n);i++){

    int x;

    scanf("%d",&x);

    S.insert(x);

  }

  T.insert(*(S.begin()));

  S.erase(S.begin());



  for(int i=0;i<n;i++){

    int tmp[1<<18];

    int t=0;

    auto itr = S.begin();

    for(int x: T){

      while(itr != S.end() && *itr == x) ++itr;

      if(itr == S.end()){

        puts("No");

        return 0;

      }

      tmp[t++] = *itr;

      itr = S.erase(itr);

    }

    if(i != n-1) T.insert(tmp, tmp+t);

  }



  puts("Yes");





  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.037566587967302x):
#include <cstdio>

#include <algorithm>

#include <functional>



int n;

int S[1<<18];

int Q[19];





int main(){

  scanf("%d",&n);

  for(int i=0;i<(1<<n);i++) scanf("%d",S+i);

  std::sort(S, S+(1<<n), std::greater<int>());

  int k = 0;

  for(int i=0;i<(1<<n);){

    int j = i;

    for(i++; S[i] == S[j]; i++) ;

    S[k++] = i-j;

  }

  Q[n] = 1;

  for(int i=0;i<k;i++){

    int l;

    int a=0;

    for(l=n;l>=0 && a < S[i];l--) a += Q[l];

    if(a<S[i]){

      puts("No");

      return 0;

    }

    l++;

    for(int t=0;t<l;t++) Q[t] += S[i];

    int b = a-Q[l];

    Q[l] = a-S[i]+b;

    for(n=l+1;b;n++){

      b -= Q[n];

      Q[n] = b;

    }

    n--;

  }

  puts("Yes");

  return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <queue>

#include <algorithm>



int n;

int S[1<<18];

std::priority_queue<int> Q;

std::vector<int> buf;



int main(){

  scanf("%d",&n);

  for(int i=0;i<(1<<n);i++) scanf("%d",S+i);

  std::sort(S, S+(1<<n), std::greater<int>());

  int k = 0;

  for(int i=0;i<(1<<n);){

    int j = i;

    for(i++; S[i] == S[j]; i++) ;

    S[k++] = i-j;

  }

  Q.push((1<<(n+1))-1);

  for(int i=0;i<k;i++){

    if(Q.size() < S[i]){

      puts("No");

      return 0;

    }

    for(int j=0;j<S[i];j++){

      int t = Q.top();

      Q.pop();

      for(t>>=1;t;t>>=1){

        buf.push_back(t);

      }

    }

    for(int x: buf) Q.push(x);

    buf.clear();

  }

  puts("Yes");

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.910540771411961x):
#include <cstdio>

#include <algorithm>

#include <functional>



int n;

int S[1<<18];

int Q[19];





int main(){

  scanf("%d",&n);

  for(int i=0;i<(1<<n);i++) scanf("%d",S+i);

  std::sort(S, S+(1<<n), std::greater<int>());

  int k = 0;

  for(int i=0;i<(1<<n);){

    int j = i;

    for(i++; S[i] == S[j]; i++) ;

    S[k++] = i-j;

  }

  Q[n] = 1;

  for(int i=0;i<k;i++){

    int l;

    int a=0;

    for(l=n;l>=0 && a < S[i];l--) a += Q[l];

    if(a<S[i]){

      puts("No");

      return 0;

    }

    l++;

    for(int t=0;t<l;t++) Q[t] += S[i];

    int b = a-Q[l];

    Q[l] = a-S[i]+b;

    for(n=l+1;b;n++){

      b -= Q[n];

      Q[n] = b;

    }

    n--;

  }

  puts("Yes");

  return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <algorithm>

#include <functional>

#include <set>



int n;

int S[1<<18];



int main(){

  std::multiset<int, std::greater<int> > S, T;

  scanf("%d",&n);

  for(int i=0;i<(1<<n);i++){

    int x;

    scanf("%d",&x);

    S.insert(x);

  }

  T.insert(*(S.begin()));

  S.erase(S.begin());



  for(int i=0;i<n;i++){

    int tmp[1<<18];

    int t=0;

    auto itr = S.begin();

    for(int x: T){

      while(itr != S.end() && *itr == x) ++itr;

      if(itr == S.end()){

        puts("No");

        return 0;

      }

      tmp[t++] = *itr;

      itr = S.erase(itr);

    }

    if(i != n-1) T.insert(tmp, tmp+t);

  }



  puts("Yes");





  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 8.336694048147885x):
#include <unistd.h>

#include <algorithm>

#include <functional>



char ibuf[4000000];

char *ibufe = ibuf-1;



void readall(){

  int k, t = 0;

  while((k=read(STDIN_FILENO, ibuf+t, sizeof(ibuf)-t))>0) t += k;

}



int read_uint(){

  int x=0;

  while(*(++ibufe) <'0');

  do {

    x *= 10;

    x += *ibufe-'0';

  } while(*(++ibufe) >='0');



  return x;

}



int n;

int S[1<<18];

int Q[19];



int main(){

  readall();

  n = read_uint();

  for(int i=0;i<(1<<n);i++) S[i] = read_uint();

  std::sort(S, S+(1<<n), std::greater<int>());

  int k = 0;

  for(int i=0;i<(1<<n);){

    int j = i;

    for(i++; S[i] == S[j]; i++) ;

    S[k++] = i-j;

  }

  Q[n] = 1;

  for(int i=0;i<k;i++){

    int l;

    int a=0;

    for(l=n;l>=0 && a < S[i];l--) a += Q[l];

    if(a<S[i]){

      write(STDOUT_FILENO, "No\n", 3);

      return 0;

    }

    l++;

    for(int t=0;t<l;t++) Q[t] += S[i];

    int b = a-Q[l];

    Q[l] = a-S[i]+b;

    for(n=l+1;b;n++){

      b -= Q[n];

      Q[n] = b;

    }

    n--;

  }

  write(STDOUT_FILENO, "Yes\n", 4);

  return 0;

}