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

using namespace std;

 

int h[100000], mCost[100000];

int Nstones, k;



int main() {



	scanf("%d %d", &Nstones, &k);

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

		scanf("%d", &h[i]);

	}

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

        	mCost[i] = 10000000000;

        	for (int j = i - 1; j >= max(i-k, 0); j--){

            		mCost[i] = min(mCost[j] + abs(h[j] - h[i]), mCost[i]);

        	}

	}

	printf("%d", mCost[Nstones - 1]);

}

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

#include <algorithm>

#include <vector>

using namespace std;

 

int n,dp[100005],a[100005],k; 

int main(){

	scanf("%d%d",&n,&k);

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

		scanf("%d",&a[i]);

	}

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

		dp[i]=dp[i-1]+abs(a[i]-a[i-1]);

		for (int j=max(1,i-k);j<i;j++){

			dp[i]=min(dp[i],dp[j]+abs(a[i]-a[j])); 

		}

	}

	printf("%d\n",dp[n]);

	return 0;

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

#define ll long long

#define endl "\n"

using namespace std;

inline int D()

{

    int t;

    scanf("%d",&t);

    return t;

}

inline ll llD()

{

    ll t;

    scanf("%lld",&t);

    return t;

}



const int Max=1e6+9;

const int OO=-1e6;

int st[4]= {1,0,0,-1};

int nt[4]= {0,1,-1,0};

int n,m;

bool valid(int x,int y)

{

    return x>-1 && y>-1 && x<n && y<m;

}

ll mem[Max];

void init()

{

    memset(mem,-1,sizeof mem);

}

//////////////////////////////////////////////////////////



ll a[Max];



ll solve (int i )

{

    if(i==n-1)return 0;

    if(i>=n)

        return Max;



    ll &ret=mem[i];

    if(~ret)return ret;

    ret=1e9;

    for(int j=0;j<=m;j++)

    ret=min(ret,(solve(i+j)+abs(a[i]-a[i+j])));



     return mem[i]=ret;

}



void Fast()

{

    std::ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    cerr.tie(0);

}

int main()

{

   init();

   n =D();

m =D();

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

   {

       a[i]=llD();

   }

   printf("%lld",solve(0));



}


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

#include <set>

using namespace std;

#define mp             make_pair

#define pb             push_back

#define fi             first

#define se             second

#define sz(x)          (int)((x).size())

#define fill(x, y)     memset(x, y, sizeof(y))

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

typedef long long      ll;

typedef pair<int, int> ii;

typedef pair<ii, int>  iii;

typedef vector<int>    vi;

typedef vector<ii>     vii;

typedef vector<iii>    viii;



const int INF = 1e9 + 5;



int main() {

  ios::sync_with_stdio(false);

  cin.tie(0);

  int N, K;

  cin >> N >> K;

  vi h(N+1);

  h[0] = INF;

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

    cin >> h[i];

  }

  vi dp(N+1, INF);

  dp[0] = 0;

  dp[1] = 0;

  for (int i = 2; i <= N; i++) {

    for (int j = 1; j <= min(i-1,K); j++) {

      dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));

    }

  }

  cout << dp[N] << "\n";

  return 0;

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

using namespace std;



int main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    long long n, k;

    cin >> n >> k;

    long long a[n + 1], dp[n + 1];

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

        cin >> a[i];

        dp[i] = INT_MAX;

    }

    if(n == 1) {

        cout << 0 << endl;

    }

    else {

        dp[1] = 0;

        dp[2] = abs(a[1] - a[2]);

        for(long long i = 3; i <= min(n, k); i++) {

            for(long long j = 1; j < i; j++) {

                dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j]));

            }

        }

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

            for(long long j = i - k; j < i; j++) {

                dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j]));

            }

        }

        cout << dp[n] << endl;

    }

    return 0;

}


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

#include<algorithm>

using namespace std;



#define ll long long 



#define file_io 0



ll i,t,j;

ll n,k,h[100005],dp[100005];



int main(){

	if (file_io){

		freopen(".in","r",stdin);

		freopen(".out","w",stdout);

	}

	

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

	

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

		scanf("%lld",&h[i]);

	}

	

	dp[1]=0;

	for (i=2; i<=n; ++i){

		dp[i]=2e9;

		for (t=max(1ll,i-k); t<i; ++t){

			dp[i]=min(dp[t]+abs(h[i]-h[t]),dp[i]);

		}

	}

	

	printf("%lld\n",dp[n]);

	

	return 0;

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

using namespace std;

typedef long long int ll;

int main()

{

    ll i,j,n,k,m;

    cin>>n>>m;

    for(i=m-n+1;i<=m;i++)

    {

        cout<<i<<" ";

    }

    for(i=m+1;i<=m+n-1;i++)

    {

        cout<<i<<" ";

    }



    cout<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 5.224597221857012x):
///d. one clue

#include <stdio.h>

#include <math.h>



int main()

{

    int k,x;

    scanf("%d %d",&k, &x);



    for(int i=x-k+1;i<=x+k-1;i++){

        printf("%d ",i);

    }

    return 0;

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



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

#define for1(i, n) for (int i = 1; i <= int(n); ++i)

#define fore(i, l, r) for (int i = int(l); i <= int(r); ++i)

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

#define pb push_back

#define eb emplace_back

#define ms(a, x) memset(a, x, sizeof(a))

#define F first

#define S second

#define endl '\n'

#define de(x) cout<<#x<<" = "<<(x)<<endl

#define de2(x,y) cout<<#x<<" = "<<(x) <<' '<< #y<<" = "<<y<<endl;



using namespace std;



typedef long long ll;

typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;

mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());



int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

	int n,k;

	cin>>n>>k;

	vector<int> a(n),dp(n,INF);

	for(auto& it:a) cin>>it;

	dp[0]=0;

	forn(i,n){

		for(int j=i-1;j>=i-k&&j>=0;j--){

			dp[i]=min(dp[i],dp[j]+abs(a[i]-a[j]));

		}

	}

	cout<<dp[n-1];

    return 0;

}




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

#include <algorithm>

#include <vector>

using namespace std;

 

int n,dp[100005],a[100005],k; 

int main(){

	scanf("%d%d",&n,&k);

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

		scanf("%d",&a[i]);

	}

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

		dp[i]=dp[i-1]+abs(a[i]-a[i-1]);

		for (int j=max(1,i-k);j<i;j++){

			dp[i]=min(dp[i],dp[j]+abs(a[i]-a[j])); 

		}

	}

	printf("%d\n",dp[n]);

	return 0;

}
// Slower C++ solution:


#include<bits/stdc++.h>

#define lld         long long int

#define llf         long double

#define pb          push_back

#define mp          make_pair

#define IN          insert

#define loopN(n)    for(lld i=0;i<n;i++)

#define loop(p,n)   for(lld i=p;i<=n;i++)

#define rloop(p,n)  for(lld i=n;i>=p;i--)

#define loop2(p,n)  for(lld j=p;j<=n;j++)

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

#define F1          first

#define F2          second

#define INF         2e18

#define FILE        freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)

#define PI          2*acos(0.0)

#define ISO         ios::sync_with_stdio(false);cin.tie(0)

#define mem(a,b)    memset(a,b,sizeof(a))

#define case(z)     cout<<"Case "<<z<<": ";

using namespace std;

const lld mod=1e9+7;



lld n,k;

lld a[100005];

lld dp[100005];



lld fun(lld p)

{



    if(p>=n-1)

    {

        return 0;

    }

    if(n-1-p<=k)

    {

        return abs(a[p]-a[n-1]);

    }

    if(dp[p]!=-1)

    {

        return dp[p];

    }

    lld ans=INF;

    loop(1,k)

    {

        ans=min(abs(a[p]-a[p+i])+fun(p+i),ans);

    }

    return dp[p]=ans;





}



int main()

{

    ISO;



    //lld t;cin>>t;while(t--)

    mem(dp,-1);

    cin>>n>>k;

    loopN(n)cin>>a[i];

    cout<<fun(0)<<endl;



    return 0;

}




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

using namespace std;



#define ff              first

#define ss              second

#define ll             long long

#define pb              push_back

#define mp              make_pair

#define pii             pair<ll,ll>

#define vi              vector<ll>

#define mii             map<ll,ll>

#define pqb             priority_queue<ll>

#define pqs             priority_queue<ll,vi,greater<ll> >

#define setbits(x)      __builtin_popcountll(x)

#define zrobits(x)      __builtin_ctzll(x)

#define mod             1000000007

#define inf             1e18

#define ps(x,y)         fixed<<setprecision(y)<<x

#define mk(arr,n,type)  type *arr=new type[n];

#define w(x)            ll x; cin>>x; while(x--)

#define FIO             ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)





ll solve(ll n,vector<ll>v,ll k){

    ll op1,op2;

    vector<ll>dp(n+1);

dp[1]=0;

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

{

    dp[i]=INT_MAX;

    for(int j=i-1;j>=1&&(i-j)<=k;j--)

    {

        dp[i]=min(dp[i],abs(v[i]-v[j])+dp[j]);

    }

}



return dp[n];



}



int main()

{

    FIO;

    ll a,b,c,d,e,f=0,sum=0,ans,ck=0,m,n,k,p,q,r,t,add=0,x,y,z;

    string s;



    cin>>n>>k;

    vector<ll>v(n+1);

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

    ans=solve(n,v,k);

    cout<<ans<<endl;







    return 0;

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

#include <vector>

#include <map>

#include <list>

#include <set>

#include <stack>

#include <queue>

#include <string>

#include <utility>

#include <algorithm>

#include <cstdio>

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

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



using namespace std;

typedef pair<int, int> P;

int lis[100000];

int tim[100000];

int main() {

    int n,r,l;

    scanf("%d %d %d",&n,&r,&l);



    int top = 0;

    int id  = 1;

    int nowtime = 0;

    int timemax = 0;

    int timemaxid = 0;

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

       

        int d,t,x;

        scanf("%d %d %d",&d,&t,&x);

        tim[id] += t - nowtime;

        if(tim[id] > timemax) {

            timemax = tim[id];

            timemaxid = id;

        }

        nowtime = t;

        lis[d] += x;

        if(id == d) {

            top += x;

        }

        if(x < 0 && d == id) {

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

                if(top < lis[j] || (top == lis[j] && j < id)) {

                    top = lis[j];

                    id = j;

                }

            }

            

        }

        if(x > 0 && d != id) {

            if(top < lis[d] || (top == lis[d] && d < id)) {

                top = lis[d];

                id = d;

            }

        }



    }

    tim[id] += l - nowtime;

    if(tim[id] > timemax) {

        timemax = tim[id];

        timemaxid = id;

    }

    printf("%d\n",timemaxid);

}

// Faster C++ solution for the same problem (measured speedup: 5.195058735594709x):


#include <cstdio>





using namespace std;

int lis[100000];

int tim[100000];

int main() {

    int n,r,l;

    scanf("%d %d %d",&n,&r,&l);



    int top = 0;

    int id  = 1;

    int nowtime = 0;

    int timemax = 0;

    int timemaxid = 0;

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

       

        int d,t,x;

        scanf("%d %d %d",&d,&t,&x);

        tim[id] += t - nowtime;

        if(tim[id] > timemax) {

            timemax = tim[id];

            timemaxid = id;

        }

        nowtime = t;

        lis[d] += x;

        if(id == d) {

            top += x;

        }

        if(x < 0 && d == id) {

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

                if(top < lis[j] || (top == lis[j] && j < id)) {

                    top = lis[j];

                    id = j;

                }

            }

            

        }

        if(x > 0 && d != id) {

            if(top < lis[d] || (top == lis[d] && d < id)) {

                top = lis[d];

                id = d;

            }

        }



    }

    tim[id] += l - nowtime;

    if(tim[id] > timemax) {

        timemax = tim[id];

        timemaxid = id;

    }

    printf("%d\n",timemaxid);

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

#include <fstream>

#define inf INFINITY

#define file freopen( "input.txt" , "r" , stdin) ; freopen( "output.txt" , "w" , stdout) ; 

#define Gamed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

#define _3102_ main()

#define endl '\n'

typedef long long ll ;

typedef unsigned long long ull ; 

typedef long double ld ; 

using namespace std ;

const int N = 1e5+100  ; 

const ll mod = 1e9 + 7  ;







int _3102_ {

    Gamed

    ll n , k , arr[N] , dp[N] ; 

    cin >> n >> k ; 

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

        cin >> arr[i] ; 

    }

    dp[0] = 0 ;  

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

        ll ans = inf , cnt = k; 

        for ( int j = i - 1 ; j >= 0 && cnt > 0 ; j -- , cnt --){

            ans = min (ans, dp[j] + abs(arr[i]-arr[j])) ;

        }

        dp[i] = ans ; 

    }

    cout << dp[--n] << endl ; 

    return 0;

}

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

#include <algorithm>

#include <vector>

using namespace std;

 

int n,dp[100005],a[100005],k; 

int main(){

	scanf("%d%d",&n,&k);

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

		scanf("%d",&a[i]);

	}

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

		dp[i]=dp[i-1]+abs(a[i]-a[i-1]);

		for (int j=max(1,i-k);j<i;j++){

			dp[i]=min(dp[i],dp[j]+abs(a[i]-a[j])); 

		}

	}

	printf("%d\n",dp[n]);

	return 0;

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

#include <algorithm>

#include <bits/stdc++.h>

using namespace std;



int main() {

  int s; cin >> s;

  vector<int> flag(1000000, 0);

  flag[s] = 1;

  bool f = false;

  int cnt = 1;

  while(!f) {

    cnt++;

    if (s%2 == 0) {

      if (flag[s/2] == 1) {

        f = true;

      } else {

        flag[s/2] = 1;

        s = s/2;

      }

    } else {

      if (flag[s*3+1] == 1) {

        f = true;

      } else {

        flag[s*3+1] = 1;

        s = s*3+1;

      }

    }

  }

  cout << cnt << endl;

  return 0;

}

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

#include <algorithm>

#include <bits/stdc++.h>

using namespace std;



int main() {

  int s; cin >> s;

  set<int> st;

  int x = s;

  while (st.count(x) == 0) {

    st.insert(x);

    if (x%2 == 0) x /= 2;

    else x = x*3+1;

  }

  cout << st.size()+1 << endl;

  return 0;

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

#include<string>

using namespace std;

int main() {

	int g = 0;

	string s, r;

		cin >> s;

		for (int i = 0; i < s.size(); i++) {

			if (s[i] == '0' || s[i] == '1') {

				r += (s[i]);

				g++;

			}

			else if (s[i] == 'B') {

				if (!r.empty()) {

					r.erase(g - 1, 1);

					g--;

				}

			}



				

		}

		if (!r.empty())

		cout << r << endl;



	return 0;

}

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

#include<string.h>



int main(){

	char str[15];

	scanf("%s",&str);

	char temp[15];

	int index=0,len=strlen(str);

	for(int i=0;str[i] != '\0';i++){

		if(str[i]=='0'){

			temp[index] = '0';

			//printf("index[%d] : %s\n",index,temp);

			index++;

		} 

		else if(str[i]=='1'){	

			temp[index] = '1';

			//printf("index[%d] : %s\n",index,temp);

			index++;

		} 				//01B0 temp =     --0

		else if(str[i]=='B'){

			//printf("index[%d] : %s\n",index,temp);

			if(index != 0) index--;

		}

	}

	//printf("%d\n",index);

	temp[index]='\0';

	printf("%s\n",&temp);

	

	

	

	return 0;

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



using namespace std;





int main() {



   int n,m;

   cin>>n>>m;

   const int N = n*2 -1;

   int arr[N];

   arr[0]=m;

   int j=1;



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

       arr[j++]=i;

   }



    for(int i=m-1;i>m-n;i--){

        arr[j++]=i;

    }





    sort(arr,arr+j);



    for(int i=0;i<j;i++)cout<<arr[i]<<" ";



    cout<<endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.230284733824182x):
///d. one clue

#include <stdio.h>

#include <math.h>



int main()

{

    int k,x;

    scanf("%d %d",&k, &x);



    for(int i=x-k+1;i<=x+k-1;i++){

        printf("%d ",i);

    }

    return 0;

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

#include <cmath>

#include <cstring>

#include <queue>

#include <iostream>

#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 1009;

const double PI = acos(-1.0);



LL mod = 1e9+7;

int main()

{

    int a;

    cin >> a;

    LL sum = 1;

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

        sum = (sum*i)%mod;

    cout << sum << endl;

}


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

#include <algorithm>

using namespace std;



int main()

{

	int n,i;

	long long sum=1;

	scanf("%d",&n);

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

	{

		sum*=i;

		sum%=(1000000000+7);

	}

	printf("%lld\n",sum);

	return 0;

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

using namespace std;

const int nax = 1e5+103;

int number, k;

int dyn[nax];

int a[nax];

int op1(int i){

	if (i==number-1) return 0;

	if(dyn[i]!=-1) return dyn[i];

	int res = abs(a[i]-a[i+1])+op1(i+1);

	for(int j=2; j<=k; j++){

		if(i+j<number) res = min(res, abs(a[i]-a[i+j])+op1(i+j));

	}

	return dyn[i]=res;

}

int main(){

	cin>>number>>k;

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

	memset(dyn, -1, sizeof dyn);

	cout<<op1(0)<<endl;

	return 0;

}

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

#include <fstream>

#define inf INFINITY

#define file freopen( "input.txt" , "r" , stdin) ; freopen( "output.txt" , "w" , stdout) ; 

#define Gamed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

#define _3102_ main()

#define endl '\n'

typedef long long ll ;

typedef unsigned long long ull ; 

typedef long double ld ; 

using namespace std ;

const int N = 1e5+100  ; 

const ll mod = 1e9 + 7  ;







int _3102_ {

    Gamed

    ll n , k , arr[N] , dp[N] ; 

    cin >> n >> k ; 

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

        cin >> arr[i] ; 

    }

    dp[0] = 0 ;  

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

        ll ans = inf , cnt = k; 

        for ( int j = i - 1 ; j >= 0 && cnt > 0 ; j -- , cnt --){

            ans = min (ans, dp[j] + abs(arr[i]-arr[j])) ;

        }

        dp[i] = ans ; 

    }

    cout << dp[--n] << endl ; 

    return 0;

}
// Slower C++ solution:
//Zory-2018

#include<cmath>

#include<cstdio>

#include<cstring>

#include<cstdlib>

#include<map>

#include<set>

#include<queue>

#include<deque>

#include<bitset>

#include<vector>

#include<algorithm>

#include<iostream>

#include<stack>

using namespace std;



namespace mine

{

	const int MAX_N=310000;

	typedef long long ll;

	int mymax(int x,int y) {return x>y?x:y;}



	int a[MAX_N];

	int f[MAX_N],g[MAX_N];//最大和次大值的位置

	void update(int x,int pos)

	{

		if(pos<0 or pos==f[x] or pos==g[x]) return;

		if(a[pos]>=a[f[x]]) g[x]=f[x],f[x]=pos;

		else if(a[pos]>=a[g[x]]) g[x]=pos;

	}

	int bin[30];

	void main()

	{

		bin[0]=1;for(int i=1;i<30;i++) bin[i]=bin[i-1]<<1;



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

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



		int ans=0;

		for(int k=0;k<bin[n];k++)

		{

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

			{

				if(k&bin[i]) continue;

				update(k+bin[i],f[k]);

				update(k+bin[i],g[k]);

			}

			ans=mymax(ans,a[f[k]]+a[g[k]]);

			if(k>0) printf("%d\n",ans);

		}

	}

};



int main()

{

	mine::main();

}


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

#include<cstring>

#include<cstdlib>

#include<algorithm>

#include<cmath>

#include<queue>

#include<vector>

#include<ctime>

#include<map>

#include<bitset>

#include<set>

#define LL long long

#define mp(x,y) make_pair(x,y)

#define pll pair<long long,long long>

#define pii pair<int,int>

using namespace std;

inline int read()

{

	int f=1,x=0;char ch=getchar();

	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}

	return x*f;

}

int stack[20];

inline void write(LL x)

{

	if(x<0){putchar('-');x=-x;}

    if(!x){putchar('0');return;}

    int top=0;

    while(x)stack[++top]=x%10,x/=10;

    while(top)putchar(stack[top--]+'0');

}

inline void pr1(int x){write(x);putchar(' ');}

inline void pr2(LL x){write(x);putchar('\n');}



const int MAX_N=20;

int bin[MAX_N];

#define pr pair<int,int>

#define FR first

#define SE second

#define MP make_pair

int a[1<<MAX_N];pr b[1<<MAX_N];

int gmax(int x,int y){return a[x]>a[y]?x:y;}

pr merge(pr x,pr y)

{

	if(a[x.FR]>a[y.FR]) return MP(x.FR,gmax(x.SE,y.FR));

	else return MP(y.FR,gmax(x.FR,y.SE));

}

void fwt(int n)

{

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

		for(int j=bin[n]-1;j>=0;j--) if(j&bin[i])

				b[j]=merge(b[j],b[j^bin[i]]);

}

int main()

{

	bin[0]=1;for(int i=1;i<MAX_N;i++) bin[i]=bin[i-1]<<1;

	

	int n;scanf("%d",&n);a[bin[n]]=0;

	for(int i=0;i<bin[n];i++) scanf("%d",&a[i]),b[i]=MP(i,bin[n]);

	fwt(n);

	int lst=0;

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

	{

		lst=max(a[b[i].FR]+a[b[i].SE],lst);

		printf("%d\n",lst);

	}

}

/*

4

75 26 45 72 81 47 97 97 2 2 25 82 84 17 56 32

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

#define MAX 100005



using namespace std;



int h[MAX];

int minimum[MAX];



int n, k;



int main(){

    cin>>n>>k;

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

        cin>>h[i];

    memset(minimum, -1, sizeof(minimum));

    minimum[0]=0;

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

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

                //cout<<"v";

                if(minimum[i+j]<0) minimum[i+j] = minimum[i]+abs(h[i]-h[i+j]);

                minimum[i+j] = min(minimum[i+j], minimum[i]+abs(h[i]-h[i+j]));

        }

    }

    cout<<minimum[n-1]<<"\n";

    return 0;

}

/*

4

10 30 40 20

0  20 30 30



4

10 50 10 10

*/




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

using namespace std;

int h[100001];

int dp[100001];

int main(int argc, char const *argv[]) {

    int n, K;

    scanf("%d %d", &n, &K);

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

        scanf("%d", &h[i]);

    dp[0] = 0;

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

        dp[i] = dp[i-1] + abs(h[i] - h[i-1]);

        for(int k=2;k<=K;++k)

            if(i >= k)

                dp[i] = min(dp[i], dp[i-k] + abs(h[i] - h[i-k]));

    }

    printf("%d\n", dp[n-1]);

    return 0;

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

#include <algorithm>



using namespace std;



int N, A, B, used[100], rings[100], G[100][199];



int rec(int pos)

{

	int ret = used[pos] = 1;



	for(int i = 0; i < rings[pos]; i++)

	{

		if(!used[G[pos][i]])

		{

			ret = max(ret, rec(G[pos][i]) + 1);

		}

	}



	used[pos] = 0;



	return ret;

}



int main()

{

	int N, A, B;



	while(true)

	{

		scanf("%d", &N);

 

		if(N == 0) { break; }



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

		{

			used[i] = rings[i] = 0;

		}



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

		{

			scanf("%d", &A); A--;

			scanf("%d", &B); B--;



			G[A][rings[A]] = B; rings[A]++;

			G[B][rings[B]] = A; rings[B]++;

		}



		int nodes = 0;



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

		{

			if(rings[i])

			{

				nodes++;

			}

		}



		int ret = 0;



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

		{

			ret = max(ret, rec(i));



 			if(ret == nodes) break;

		}

 

		printf("%d\n", ret);

	}



	return 0;

}

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

#include <algorithm>

#pragma warning(disable : 4996)

using namespace std;

int N, A, B, v[100], r[100], G[100][199];

int rec(int pos) {

	int ret = 0; v[pos] = 1;

	for (int i = 0; i < r[pos]; i++) {

		if (!v[G[pos][i]]) ret = max(ret, rec(G[pos][i]));

	}

	v[pos] = 0;

	return ret + 1;

}

int main() {

	while (scanf("%d", &N), N) {

		for (int i = 0; i < 100; i++) r[i] = 0;

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

			scanf("%d%d", &A, &B), A--, B--;

			G[A][r[A]++] = B, G[B][r[B]++] = A;

		}

		int ret = 0;

		for (int i = 0; i < 100; i++) ret = max(ret, rec(i));

		printf("%d\n", ret);

	}

	return 0;

}