#include<bits/stdc++.h>
using namespace std;
int main(){
    int t,n,m,a,av=0,sup=0;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>n>>m;
        av=0;
        sup=0;
        for(int j=0;j<n;j++){
            cin>>a;
            av=av+a;
            if(a>sup){
                sup=a;
            }
        }
        av=av/n;
        if(n==1){
            cout<<min(m,a);
            cout<<'\n';
        }
        else{
            if(m/n<=av){
                cout<<m;
                cout<<'\n';
            }
            else{
                if(av==0){
                    cout<<sup;
                    cout<<'\n';
                }
                else{
                    cout<<av;
                    cout<<'\n';
                }
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int t,n,m,a,av=0,sup=0;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>n>>m;
        av=0;
        for(int j=0;j<n;j++){
            cin>>a;
            av=av+a;
            if(a>sup){
                sup=a;
            }
        }
        av=av/n;
        if(n==1){
            cout<<min(m,a);
            cout<<'\n';
        }
        else{
            if(m/n<=av){
                cout<<m;
                cout<<'\n';
            }
            else{
                if(av==0){
                    cout<<sup;
                    cout<<'\n';
                }
                else{
                    cout<<av;
                    cout<<'\n';
                }
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int t,n,m,a,av=0;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>n>>m;
        av=0;
        for(int j=0;j<n;j++){
            cin>>a;
            av=av+a;
        }
        av=av/n;
        if(m/n<=av){
            cout<<m;
            cout<<'\n';
        }
        else{
            cout<<av;
            cout<<'\n';
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int t,n,m,a,av=0;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>n>>m;
        av=0;
        for(int j=0;j<n;j++){
            cin>>a;
            av=av+a;
        }
        av=av/n;
        if(n==1){
            cout<<min(m,a);
            cout<<'\n';
        }
        else{
            if(m/n<=av){
                cout<<m;
                cout<<'\n';
            }
            else{
                cout<<av;
                cout<<'\n';
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int t,n,m,a,av=0,x;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>n>>m;
        for(int j=0;j<n;j++){
            cin>>a;
            av=av+a;
        }
        av=av/n;
        x=(av*n-m)/(n-1);
        cout<<m;
        cout<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int t,n,m,a,av=0;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>n>>m;
        for(int j=0;j<n;j++){
            cin>>a;
            av=av+a;
        }
        av=av/n;
        if((m/n)<=av){
            cout<<m;
            cout<<'\n';
        }
        else{
            cout<<av<<'\n';
        }

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
	ll test;
	cin>>test;
	while(test--)
	{
		ll n,m;
		cin>>n>>m;
		ll s=0;
		for(ll i=1;i<=n;i++)
		{
			ll x;
			cin>>x;
			s=s+x;
		}
		cout<<min(s,m)<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int n,m;
        cin>>n>>m;
        int sum=0;
        int x;
        for(int j=0;j<n;j++)
        {
            cin>>x;
            sum=sum+x;
        }
        int result;

        if(sum<m)
        {
            result=sum;
        }
        else
        {
            result=m;
        }
        cout<<result<<"\n";
        return 0;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n,m;
cin>>n>>m;
int arr[n];
int sum=0;
int avg;
for(int j=0;j<n;j++)
{
cin>>arr[j];
sum=sum+arr[j];
}
avg=sum/n;
int x=m/n;
int result;
if(x<=avg)
{
sum=m;
}
else
{
continue;
}
cout<<sum;
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n,m;
cin>>n>>m;
int arr[n];
int sum=0;
float avg;
for(int j=0;j<n;j++)
{
cin>>arr[j];
sum=sum+arr[j];
}
avg=sum/n;
float x=m/n;
int result;
if(x<=avg)
{
sum=m;
}
else
{
continue;
}
cout<<sum;
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n,m;
cin>>n>>m;
int arr[n];
int sum=0;
float avg;
for(int j=0;j<n;j++)
{
cin>>arr[j];
sum=sum+arr[j];
}
avg=sum/n;
float x=m/n;
int result;
if(x<=avg)
{
sum=m;
}
else
{
continue;
}
cout<<sum<<"\n";
}
return 0;
}


#include<iostream>
#include<algorithm>
#define lint long long int

using namespace std;

int main(void)
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		int a[n],ans,sum=0;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
			if(i!=0)
				sum+=a[i];
		}
		ans = a[0];
		sort(a,a+n);
		int id=n-1;
		if(a[0]>m)
		{
			if(sum >= (m - ans))
			{
				cout<<m<<endl;
				continue;
			}
			else
			{
				cout<< ans - sum<<endl;
				continue;
			}
		}
		while(id>0)
		{
			if(ans + a[id] <= m)
				ans += a[id];
			id-=1;
			//cout<<ans<<" \t"<<id<<endl;
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#define lint long long int

using namespace std;

int main(void)
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		int a[n],ans;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
		}
		ans = a[0];
		sort(a,a+n);
		int id=n-1;
		while(id>0)
		{
			if(ans + a[id] <= m)
				ans += a[id];
			id-=1;
			//cout<<ans<<" \t"<<id<<endl;
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define lint long long int

using namespace std;

int main(void)
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		int a[n],ans;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
		}
		ans = a[0];
		sort(a,a+n);
		int id=n;
		while(id>0)
		{
			id--;
			if(ans + a[id] <= m)
				ans += a[id];
			else
				continue;
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define lint long long int

using namespace std;

int main(void)
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		int a[n],ans;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
		}
		ans = a[0];
		sort(a,a+n);
		int id=n;
		while(id>=0)
		{
			id--;
			if(ans + a[id] <= m)
				ans += a[id];
			else
				continue;
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#define lint long long int

using namespace std;

int main(void)
{
	lint t;
	cin>>t;
	while(t--)
	{
		lint n,m;
		cin>>n>>m;
		lint a[n],ans,sum=0;
		for(lint i=0;i<n;i++)
		{
			cin>>a[i];
			if(i!=0)
				sum+=a[i];
		}
		ans = a[0];
		sort(a,a+n);
		lint id=n-1;
		if(a[0]>m)
		{
			if(sum >= (m - ans))
			{
				cout<<m<<endl;
				continue;
			}
			else
			{
				cout<< ans - sum<<endl;
				continue;
			}
		}
		if(sum >= (m - ans))
			cout<<m<<endl;
		else
			cout<<ans + sum<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#define lint long long int

using namespace std;

int main(void)
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		int a[n],ans,sum=0;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
			if(i!=0)
				sum+=a[i];
		}
		ans = a[0];
		sort(a,a+n);
		int id=n-1;
		if(a[0]>m)
		{
			if(sum >= (m - a[0]))
			{
				cout<<m<<endl;
				continue;
			}
			else
			{
				cout<< ans - sum<<endl;
				continue;
			}
		}
		while(id>0)
		{
			if(ans + a[id] <= m)
				ans += a[id];
			id-=1;
			//cout<<ans<<" \t"<<id<<endl;
		}
		cout<<ans<<endl;
	}
	return 0;
}


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

int main() {
	int t; cin >> t;
	while(t--){
		int n,m; cin >> n >> m;
		int total=0;
		for(int i=0;i<n;i++){
			int x; cin >> x;
			total+=x;
		}
		if(total>=m) cout << m << endl;
		else cout << total << endl;

	}
	// your code goes here
	return 0;
}


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

int main() {
	int t; cin >> t;
	while(t--){
		int n,m;
		cin >> n >> m;
		double s=0;
		for(int i=0;i<n;i++){
			double foo; cin >> foo;
			s+=foo;
		}
		int ans = (s/(double)n)*n;
		if(ans >= m) cout << m << endl;
		else cout << ans << endl;
	}
	// your code goes here
	return 0;
}


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

int main() {
	int t; cin >> t;
	while(t--){
		int n,m; cin >> n >> m;
		int total;
		for(int i=0;i<n;i++){
			int x; cin >> x;
			total+=x;
		}
		if(total>=m) cout << m << endl;
		else cout << total << endl;

	}
	// your code goes here
	return 0;
}


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

int main() {
	int t; cin >> t;
	while(t--){
		int n,m; cin >> n >> m;
		int a,mn=INT_MAX;
//		double total = 0;
		for(int i=0;i<n;i++){
			int x; cin >> x;
			if(i==0) a = x;
			mn = (mn,x);
		}
	//	total/=n;
		for(int i=a;i>=0 && a<m;i++){
			a++;
		}
		cout << a << endl;
	}
	// your code goes here
	return 0;
}


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

int main() {
	int t; cin >> t;
	while(t--){
		int n,m;
		cin >> n >> m;
		int s=0;
		for(int i=0;i<n;i++){
			double foo; cin >> foo;
			s+=foo;
		}
		int ans = (s/n)*n;
		if(ans >= m) cout << m << endl;
		else cout << ans << endl;
	}
	// your code goes here
	return 0;
}


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

int main() {
	int t; cin >> t;
	while(t--){
		int n,m;
		cin >> n >> m;
		double s=0;
		for(int i=0;i<n;i++){
			double foo; cin >> foo;
			s+=foo;
		}
		int ans = (s/n)*n;
		if(ans >= m) cout << m << endl;
		else cout << ans << endl;
	}
	// your code goes here
	return 0;
}


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

int main() {
	int t; cin >> t;
	while(t--){
		int n,m; cin >> n >> m;
		int a,mn=INT_MAX;
		int total = 0;
		for(int i=0;i<n;i++){
			int x; cin >> x;
			if(i==0) a = x;
			else total+=x;
			mn = (mn,x);
		}
		for(;total>=0&& a<m;total--){
			a++;
		}
		cout << a << endl;
	}
	// your code goes here
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll>v;
int main()
{
    ll n,m,t;
    cin>>t;
    while(t--){
            ll sum=0;
        cin>>n>>m;
    ll a[n+5];
        for(ll i=0;i<n;i++){
            cin>>a[i];
            sum+=a[i];
        }
        ll ans=sum/n;
        while(m){
            if(m>=ans && ans){
                break;
            }
            m--;
        }
        if(sum<=m){
            cout<<sum<<endl;
        }else
        cout<<m<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll>v;
int main()
{
    ll n,m,t;
    cin>>t;
    while(t--){
            ll sum=0;
        cin>>n>>m;
    ll a[n+5];
        for(ll i=0;i<n;i++){
            cin>>a[i];
            sum+=a[i];
        }
        ll ans=sum/n;
        while(1){
            if(m>=ans && !ans){
                break;
            }
            m--;
        }
        cout<<m<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll>v;
int main()
{
    ll n,m,t;
    cin>>t;
    while(t--){
            ll sum=0;
        cin>>n>>m;
    ll a[n+5];
        for(ll i=0;i<n;i++){
            cin>>a[i];
            sum+=a[i];
        }

        if(sum<=m){
            cout<<sum<<endl;
        }else
        cout<<m<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll>v;
int main()
{
    ll n,m,t;
    cin>>t;
    while(t--){
            ll sum=0;
        cin>>n>>m;
    ll a[n+5];
        for(ll i=0;i<n;i++){
            cin>>a[i];
            sum+=a[i];
        }
        ll ans=sum/n;
        while(m){
            if(m>=ans && !ans){
                break;
            }
            m--;
        }
        cout<<m<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll>v;
int main()
{
    ll n,m,t;
    cin>>t;
    while(t--){
            ll sum=0;
        cin>>n>>m;
    ll a[n+5];
        for(ll i=0;i<n;i++){
            cin>>a[i];
            sum+=a[i];
        }
        ll ans=sum/n;
        while(m){
            if(m>=ans && ans){
                break;
            }
            m--;
        }
        cout<<m<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll>v;
int main()
{
    ll n,m,t;
    cin>>t;
    while(t--){
            ll sum=0;
        cin>>n>>m;
    ll a[n+5];
        for(ll i=0;i<n;i++){
            cin>>a[i];
            sum+=a[i];
        }
        ll ans=sum/n;
        while(1){
            if(m>=ans){
                break;
            }
            m--;
        }
        cout<<m<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll>v;
int main()
{
    ll n,m,t;
    cin>>t;
    while(t--){
            ll sum=0;
        cin>>n>>m;
    ll a[n+5];
        for(ll i=0;i<n;i++){
            cin>>a[i];
            sum+=a[i];
        }
        ll ans=sum/n;
        while(m{
            if(m>=ans && !ans){
                break;
            }
            m--;
        }
        cout<<m<<endl;
    }

}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<ll,ll>
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define M 500005
#define mod 1000000007
#define inf LLONG_MAX
#define endl "\n"
#define MAX 100000
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    int n,m,s=0;
	cin>>t;
	while(t--){
		cin>>n>>m;
		int a[n];
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		for(int i=0;i<n;i++){
			s+=a[i];
		}
	}
	cout<<min(s,m)<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<ll,ll>
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define M 500005
#define mod 1000000007
#define inf LLONG_MAX
#define endl "\n"
#define MAX 100000
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll t;
    cin>>t;
    ll n,m,s=0;
	while(t--){
		cin>>n>>m;
		ll in;
		for(ll i=1;i<=n;i++){
			cin>>in;
			s+=in;
		}
		cout<<min(s,m)<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<ll,ll>
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define M 500005
#define mod 1000000007
#define inf LLONG_MAX
#define endl "\n"
#define MAX 100000
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    int n,m,s=0;
	while(t--){
		cin>>n>>m;
		int a[n];
		for(int i=1;i<=n;i++){
			cin>>a[i];
			s+=a[i];
		}
		cout<<min(s,m)<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<ll,ll>
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define M 500005
#define mod 1000000007
#define inf LLONG_MAX
#define endl "\n"
#define MAX 100000
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    int n,m,s=0;
	while(t--){
		cin>>n>>m;
		int a[n];
		for(int i=0;i<n;i++){
			cin>>a[i];
			s+=a[i];
		}
		cout<<min(s,m)<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<ll,ll>
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define M 500005
#define mod 1000000007
#define inf LLONG_MAX
#define endl "\n"
#define MAX 100000
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    int n,m,s=0;
	cin>>t;
	while(t--){
		cin>>n>>m;
		int a[n];
		for(int i=0;i<n;i++){
			cin>>a[i];
			s+=a[i];
		}
	}
	cout<<min(s,m)<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<ll,ll>
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define M 500005
#define mod 1000000007
#define inf LLONG_MAX
#define endl "\n"
#define MAX 100000
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll t;
    cin>>t;
	while(t--){
		ll n,m,s=0;
		cin>>n>>m;
		ll in;
		for(ll i=1;i<=n;i++){
			cin>>in;
			s+=in;
		}
		cout<<min(s,m)<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<ll,ll>
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define M 500005
#define mod 1000000007
#define inf LLONG_MAX
#define endl "\n"
#define MAX 100000
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    int n,m,s=0;
	while(t--){
		cin>>n>>m;
		int a[n];
		for(int i=0;i<n;i++){
			cin>>a[i];
			s+=a[i];
		}
	}
	cout<<min(s,m)<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<ll,ll>
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define M 500005
#define mod 1000000007
#define inf LLONG_MAX
#define endl "\n"
#define MAX 100000
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    int n,m,s=0;
	while(t--){
		cin>>n>>m;
		int a[n];
		for(int i=1;i<n;i++){
			cin>>a[i];
			s+=a[i];
		}
		cout<<min(s,m)<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<ll,ll>
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define M 500005
#define mod 1000000007
#define inf LLONG_MAX
#define endl "\n"
#define MAX 100000
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    int n,m,a[n].s=0;
	cin>>t;
	while(t--){
		cin>>n>>m;
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		for(int i=0;i<n;i++){
			s+=a[i];
		}
	}
	cout<<min(s,m)<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long int n,m,sum=0;
		cin>>n>>m;
		long long int a[n];
		cin>>a[0];
		sum+=a[0];
		int flag=0;
		for (long long int i = 1; i < n; ++i)
		{
			cin>>a[i];
			if (sum+a[i]>m)
			{
				cout<<m<<endl;
				flag=1;
				goto x;
			}
			else
			{
				sum+=a[i];
			}
		}
		if (flag!=1)
		{
			cout<<sum<<endl;
		}
		x:
		continue;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long int t;
	cin>>t;
	int count=1;
	for (int j = 0; j < t; ++j)
	{
		long long int n,m,sum=0;
		cin>>n>>m;
		long long int a[n];
		cin>>a[0];
		sum+=a[0];
		int flag=0;
		for (long long int i = 1; i < n; ++i)
		{
			cin>>a[i];
			if (sum+a[i]>=m)
			{
				sum=m;
			}
			else
			{
				sum+=a[i];
			}
		}
			cout<<sum<<endl;

	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long int n,m,sum=0;
		cin>>n>>m;
		long long int a[n];
		cin>>a[0];
		sum+=a[0];
		int flag=0;
		for (long long int i = 1; i < n; ++i)
		{
			cin>>a[i];
			if (sum+a[i]>=m)
			{
				cout<<m<<endl;
				sum=m;
				flag=1;
				break;
			}
			else
			{
				sum+=a[i];
			}
		}
		if (flag!=1)
		{
			cout<<sum<<endl;
		}

	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long int n,m,sum=0;
		cin>>n>>m;
		long long int a[n];
		cin>>a[0];
		sum+=a[0];
		int flag=0;
		for (int i = 1; i < n; ++i)
		{
			cin>>a[i];
			if (sum+a[i]>m)
			{
				cout<<m<<endl;
				flag=1;
				break;
			}
			else
			{
				sum+=a[i];
			}
		}
		if (flag!=1)
		{
			cout<<sum<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long int n,m,sum=0;
		cin>>n>>m;
		long long int a[n];
		cin>>a[0];
		sum+=a[0];
		int flag=0;
		for (int i = 1; i < n; ++i)
		{
			cin>>a[i];
			if (sum+a[i]>m)
			{
				cout<<m<<endl;
				flag=1;
				goto x;
			}
			else
			{
				sum+=a[i];
			}
		}
		if (flag!=1)
		{
			cout<<sum<<endl;
		}
		x:
		continue;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long int t;
	cin>>t;
	int count=1;
	for (int j = 0; j < t; ++j)
	{
		long long int n,m,sum=0;
		cin>>n>>m;
		long long int a[n];
		cin>>a[0];
		sum+=a[0];
		int flag=0;
		cout<<"t is "<<count++<<endl;
		for (long long int i = 1; i < n; ++i)
		{
			cin>>a[i];
			if (sum+a[i]>=m)
			{
				sum=m;
			}
			else
			{
				sum+=a[i];
			}
		}
			cout<<sum<<endl;

	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long int n,m,sum=0;
		cin>>n>>m;
		long long int a[n];
		cin>>a[0];
		sum+=a[0];
		cout<<"sum 0"<<sum<<endl;
		int flag=0;
		for (long long int i = 1; i < n; ++i)
		{
			cin>>a[i];
			if (sum+a[i]>=m)
			{
				cout<<m<<endl;
				sum=m;
				flag=1;
				break;
			}
			else
			{
				sum+=a[i];
			}
		}
		if (flag!=1)
		{
			cout<<sum<<endl;
		}

	}

	return 0;
}


#include <bits/stdc++.h>
#define LEFT(n) (2 * n + 1)
#define RIGHT(n) (2 * n + 2)
#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
typedef int lli;
const int MM = 1200000;
lli m,n,k,p1,p2,p3,p,f;
long long a[MM],ia[MM];
lli x[MM];
string s,s1,s2;
/* Print pair */
template <typename T,typename S>
ostream & operator << (ostream &os , const pair<T,S> &v) {
    os << "(" ;
    os << v.first << "," << v.second << ")" ;
    return os ;
}
/* Print vector */
template <typename T>
ostream & operator << (ostream &os , const vector<T> &v) {
    os << "[" ;
    int sz = v.size() ;
    for(int i = 0 ; i < sz ; ++i) {
        os << v[i] ;
        if(i!=sz-1)os << "," ;
    }
    os << "]\n" ;
    return os ;
}
/* Print set */
template <typename T>
ostream & operator << (ostream &os , const set<T> &v) {
    T last = *v.rbegin() ;
    os << "[" ;
    for(auto it : v) {
        os << it  ;
        if(it != last) os << "," ;
    }
    os << "]\n" ;
    return os ;
}
/* Print Map */
struct node{
    int ht;
    vector <int> arr;
    long long ans;
    long long ians;

    void reset(){
        ans = 0;
        ians = 0;
        ht = 0;
        arr.clear();
    }
};
node tree[2*MM];
long long inv(vector <int> fir, vector <int> &sec){
    long long ans = 0;
    int pt1 = 0, pt2 = 0;
    for(pt1 = 0; pt1 < sec.size(); pt1++){
        while(pt2 < fir.size() && fir[pt2] <= sec[pt1]){
            pt2++;
        }
        ans += (long long)fir.size() - pt2;
    }
    return ans;
}
void merge(vector <int> &fir, vector <int> &sec, vector <int> &res){
    int pt1 = 0;
    int pt2 = 0;
    while(pt1 < fir.size() && pt2 < sec.size()){
        if(fir[pt1] < sec[pt2])
            res.push_back(fir[pt1]), pt1++;
        else
            res.push_back(sec[pt2]), pt2++;
    }
    while(pt1 < fir.size()){
        res.push_back(fir[pt1]), pt1++;
    }
    while(pt2 < sec.size()){
        res.push_back(sec[pt2]), pt2++;
    }
    // cout << fir << "** " << sec << " -- " << res << endl;
}

node merge(node lef, node rig){
    node tem;
    tem.reset();
    merge(lef.arr, rig.arr, tem.arr);
    tem.ans = inv(lef.arr, rig.arr);
    tem.ians = inv(rig.arr, lef.arr);
    tem.ht = lef.ht + 1;
    a[tem.ht] += tem.ans;
    ia[tem.ht] += tem.ians;
    return tem;
}
void build(int nod, int st, int en){
    if(st == en){
        tree[nod].reset();
        tree[nod].arr.push_back(x[st]);
        return;
    }
    int mid = (st + en)/2;
    build(LEFT(nod), st, mid);
    build(RIGHT(nod), mid + 1, en);
    tree[nod] = merge(tree[LEFT(nod)], tree[RIGHT(nod)]);
}
void upd(int ht){

    for(int i=ht; i>=0; i--)
        swap(a[i], ia[i]);
    // for(int i=0; i<=m; i++)
    //     cout << a[i] << ' ' << ia[i] << endl;
    long long ans = 0;
    for(int i=0; i<=m; i++)
        ans += a[i];
    printf("%lld\n", ans);
    // cout << ans << endl;
}

int main(){ // remove during scanf

    scanf("%d", &n);
    m = n;
    n = 1 << n;
    for(int i=0; i<n; i++)
        scanf("%d", x+i);
    build(0, 0, n-1);

    int q;
    cin >> q;
    while(q--){
        int p;
        scanf("%d", &p);
        upd(p);
    }
    return 0;
}


#include<bits/stdc++.h>

#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;
const int N = 2000002;
int a[N], t[200], n, st;
ll d[200];
void precalc(int h, int l, int r)
{
    if (l == r) return;
    int mid = (r + l) >> 1;
    precalc(h + 1, l, mid);
    precalc(h + 1, mid + 1, r);
    int d = mid - l + 1;
    int L = l;
    int R = mid + 1;
    while(L <= mid && R <= r){
        while(R <= r && a[L] <= a[R]) R++;
        if (R <= r) t[h] += (r - R + 1);
        L++;
    }
    sort(a + l, a + r);
}
main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
   // freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < (1 << n); i++){
        cin >> a[i];
    }
    precalc(0, 0, (1 << n) - 1);
    int q;
    cin >> q;
    for (int i = 0; i < n; i++){
        ll x = (1 << i);
        ll y = (1 << (n - i - 1));
        d[i] = y * y  * x;
    }
    while(q--){
        int x;
        cin >> x;
        x = n - x;
        st ^= (1 << x);
        int f = 0;
        ll ans = 0;
        for (int i = 0; i < n; i++){
            if (st & (1 << i)) f++;
            if (f & 1) ans += (d[i] - t[i]); else ans += t[i];
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>

#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;
const int N = 2000002;
int a[N], t[200], n, st;
ll d[200];
void precalc(int h, int l, int r)
{
    if (l == r) return;
    int mid = (r + l) >> 1;
    precalc(h + 1, l, mid);
    precalc(h + 1, mid + 1, r);
    int dd = mid - l + 1;
    int L = l;
    int R = mid + 1;
    while(L <= mid && R <= r){
        while(L <= mid && a[L] <= a[R]) L++;
        if (L <= mid) t[h] += (mid - L + 1);
        R++;
    }
    L = l;
    int k = 0;
    for (int i = mid + 1; i <= r; i++)
    if (a[i] == a[i - 1] && i != mid + 1) d[h] += (dd - k); else{
        while(L <= mid && a[L] < a[i]) L++;
        k = 0;
        while(L <= mid && a[L] == a[i]){
            L++;
            k++;
        }
        d[h] += (dd - k);
    }
    sort(a + l, a + r + 1);
}
main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
   // freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < (1 << n); i++){
        cin >> a[i];
    }
    precalc(0, 0, (1 << n) - 1);
    int q;
    cin >> q;
    while(q--){
        int x;
        cin >> x;
        x = n - x;
        st ^= (1 << x);
        int f = 0;
        ll ans = 0;
        for (int i = 0; i < n; i++){
            if (st & (1 << i)) f++;
            if (f & 1) ans += (d[i] - t[i]); else ans += t[i];
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>

#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;
const int N = 2000002;
int a[N], n, st;
ll d[200];
ll t[200];
void precalc(int h, int l, int r)
{
    if (l == r) return;
    int mid = (r + l) >> 1;
    precalc(h + 1, l, mid);
    precalc(h + 1, mid + 1, r);
    int dd = mid - l + 1;
    int L = l;
    int R = mid + 1;
    while(L <= mid && R <= r){
        while(L <= mid && a[L] <= a[R]) L++;
        if (L <= mid) t[h] += (mid - L + 1);
        R++;
    }
    L = l;
    int k = 0;
    for (int i = mid + 1; i <= r; i++)
    if (a[i] == a[i - 1] && i != mid + 1) d[h] += (dd - k); else{
        while(L <= mid && a[L] < a[i]) L++;
        k = 0;
        while(L <= mid && a[L] == a[i]){
            L++;
            k++;
        }
        d[h] += (dd - k);
    }
    sort(a + l, a + r + 1);
}
main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
   // freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < (1 << n); i++){
        cin >> a[i];
    }
    precalc(0, 0, (1 << n) - 1);
    int q;
    cin >> q;
    while(q--){
        int x;
        cin >> x;
        x = n - x;
        st ^= (1 << x);
        int f = 0;
        ll ans = 0;
        for (int i = 0; i < n; i++){
            if (st & (1 << i)) f++;
            if (f & 1) ans += (d[i] - t[i]); else ans += t[i];
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>

#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;
const int N = 2000002;
int a[N], t[200], n, st;
ll d[200];
void precalc(int h, int l, int r)
{
    if (l == r) return;
    int mid = (r + l) >> 1;
    precalc(h + 1, l, mid);
    precalc(h + 1, mid + 1, r);
    int dd = mid - l + 1;
    int L = l;
    int R = mid + 1;
    while(L <= mid && R <= r){
        while(R <= r && a[L] <= a[R]) R++;
        if (R <= r) t[h] += (r - R + 1);
        L++;
    }
    L = l;
    int k = 0;
    for (int i = mid + 1; i <= r; i++)
    if (a[i] == a[i - 1] && i != mid + 1) d[h] += (dd - k); else{
        while(L <= mid && a[L] < a[i]) L++;
        k = 0;
        while(L <= mid && a[L] == a[i]){
            L++;
            k++;
        }
        d[h] += (dd - k);
    }
    sort(a + l, a + r + 1);
}
main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
   // freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < (1 << n); i++){
        cin >> a[i];
    }
    sort(a, a + 3);
    precalc(0, 0, (1 << n) - 1);
    int q;
    cin >> q;
    while(q--){
        int x;
        cin >> x;
        x = n - x;
        st ^= (1 << x);
        int f = 0;
        ll ans = 0;
        for (int i = 0; i < n; i++){
            if (st & (1 << i)) f++;
            if (f & 1) ans += (d[i] - t[i]); else ans += t[i];
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>

#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;
const int N = 2000002;
int a[N], t[200], n, st;
ll d[200];
void precalc(int h, int l, int r)
{
    if (l == r) return;
    int mid = (r + l) >> 1;
    precalc(h + 1, l, mid);
    precalc(h + 1, mid + 1, r);
    int dd = mid - l + 1;
    int L = l;
    int R = mid + 1;
    while(L <= mid && R <= r){
        while(R <= r && a[L] <= a[R]) R++;
        if (R <= r) t[h] += (r - R + 1);
        L++;
    }
    L = l;
    int k = 0;
    for (int i = mid + 1; i <= r; i++)
    if (a[i] == a[i - 1] && i != mid + 1) d[h] += (dd - k); else{
        while(L <= mid && a[L] < a[i]) L++;
        k = 0;
        while(L <= mid && a[L] == a[i]){
            L++;
            k++;
        }
        d[h] += (dd - k);
    }
    sort(a + l, a + r + 1);
}
main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
   // freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < (1 << n); i++){
        cin >> a[i];
    }
    precalc(0, 0, (1 << n) - 1);
    int q;
    cin >> q;
    while(q--){
        int x;
        cin >> x;
        x = n - x;
        st ^= (1 << x);
        int f = 0;
        ll ans = 0;
        for (int i = 0; i < n; i++){
            if (st & (1 << i)) f++;
            if (f & 1) ans += (d[i] - t[i]); else ans += t[i];
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>

#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;
const int N = 2000002;
int a[N], t[200], n, st;
ll d[200];
void precalc(int h, int l, int r)
{
    if (l == r) return;
    int mid = (r + l) >> 1;
    precalc(h + 1, l, mid);
    precalc(h + 1, mid + 1, r);
    int d = mid - l + 1;
    int L = l;
    int R = mid + 1;
    while(L <= mid && R <= r){
        while(R <= r && a[L] <= a[R]) R++;
        if (R <= r) t[h] += (r - R + 1);
        L++;
    }
    sort(a + l, a + r);
}
main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < (1 << n); i++){
        cin >> a[i];
    }
    precalc(0, 0, (1 << n) - 1);
    int q;
    cin >> q;
    for (int i = 0; i < n; i++){
        ll x = (1 << i);
        ll y = (1 << (n - i - 1));
        d[i] = y * y  * x;
    }
    while(q--){
        int x;
        cin >> x;
        x = n - x;
        st ^= (1 << x);
        int f = 0;
        ll ans = 0;
        for (int i = 0; i < n; i++){
            if (st & (1 << i)) f++;
            if (f & 1) ans += (d[i] - t[i]); else ans += t[i];
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define reset(X) memset(X, 0, sizeof(X))
#define int long long
typedef long long ll;
#define pb push_back
#define endl '\n'
#define all(X) X.begin(),X.end()

const int INF=1e18;
const int mod=1e9+7;
const int MAXN=21;
int arr[MAXN][1<<MAXN];
int ans[2][MAXN];
vector<int> build(int l, int r,int height)
{
    if(l!=r)
    {
        int mid=(l+r)/2;
        vector<int> left=build(l,mid,height+1);
        vector<int> right=build(mid+1,r,height+1);
        left.pb(INF);
        right.pb(INF);

        vector<int> output;
        int i=0,j=0;
        while(left[i]!=INF || right[j]!=INF)
        {
            if(left[i]<=right[j])
            {

                    output.pb(left[i]);
                    i++;
            }
            else if(left[i]>right[j])
            {
                ans[1][height]+=left.size()-i-1;
                output.pb(right[j]) ;
                j++;
            }
        }
        i=0,j=0;
         while(left[i]!=INF || right[j]!=INF)
        {
            if(left[i]>=right[j])
            {
                j++;

            }
            else if(left[i]<right[j])
            {
                ans[0][height]+=right.size()-j-1;
                i++;

            }
        }
        return output;

    }
    else
    {
        ans[0][height]=0;
        ans[1][height]=0;
        return {arr[height][l]};

    }

}

signed main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    if (fopen("INPUT.txt", "r")) {
        freopen ("INPUT.txt" , "r" , stdin);
        // freopen ("OUTPUT.txt" , "w" , stdout);
    }
    #endif
//---------------------------------------------------------------------------------------------------
    int n;
    cin>>n;
    for(int i=0;i<(1<<n);i++)
    {
        cin>>arr[n][i];
    }
    build(0, (1<<n)-1,0);
    int q;
    cin>>q;
    for(int i=0;i<q;i++)
    {
        int m;
        cin>>m;
        int cnt=0;
        for(int j=0;j<n-m;j++)
        {
            cnt+=ans[1][j];
        }
        for(int j=n-m;j<n;j++)
        {
            swap(ans[0][j] , ans[1][j]);
            cnt+=ans[1][j];
        }
        cout<<cnt<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define reset(X) memset(X, 0, sizeof(X))
#define int long long
typedef long long ll;
#define pb push_back
#define endl '\n'
#define all(X) X.begin(),X.end()

const int INF=1e18;
const int mod=1e9+7;
const int MAXN=21;
int arr[MAXN][2<<MAXN];
int ans[2][MAXN];
vector<int> build(int l, int r,int height)
{
    if(l!=r)
    {
        int mid=(l+r)/2;
        vector<int> left=build(l,mid,height+1);
        vector<int> right=build(mid+1,r,height+1);
        left.pb(INF);
        right.pb(INF);

        vector<int> output;
        int i=0,j=0;
        while(left[i]!=INF || right[j]!=INF)
        {
            if(left[i]<=right[j])
            {

                    output.pb(left[i]);
                    i++;
            }
            else if(left[i]>right[j])
            {
                ans[1][height]+=left.size()-i-1;
                output.pb(right[j]) ;
                j++;
            }
        }
        i=0,j=0;
         while(left[i]!=INF || right[j]!=INF)
        {
            if(left[i]>=right[j])
            {
                j++;

            }
            else if(left[i]<right[j])
            {
                ans[0][height]+=right.size()-j-1;
                i++;

            }
        }
        return output;

    }
    else
    {
        ans[0][height]=0;
        ans[1][height]=0;
        return {arr[height][l]};

    }

}

signed main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    if (fopen("INPUT.txt", "r")) {
        freopen ("INPUT.txt" , "r" , stdin);
        // freopen ("OUTPUT.txt" , "w" , stdout);
    }
    #endif
//---------------------------------------------------------------------------------------------------
    int n;
    cin>>n;
    for(int i=0;i<(1<<n);i++)
    {
        cin>>arr[n][i];
    }
    build(0, (1<<n)-1,0);
    int q;
    cin>>q;
    for(int i=0;i<q;i++)
    {
        int m;
        cin>>m;
        int cnt=0;
        for(int j=0;j<n-m;j++)
        {
            cnt+=ans[1][j];
        }
        for(int j=n-m;j<n;j++)
        {
            swap(ans[0][j] , ans[1][j]);
            cnt+=ans[1][j];
        }
        cout<<cnt<<endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

int a[1<<20];

long long mergeSortAndCountInversions(int* arr, int size)
{
    int m;
	if (size <= 1)
		return 0;
	m = size / 2;
	long long invCountA = 0, invCountB = 0, invCountC = 0;
	invCountA = mergeSortAndCountInversions(arr, m);
	invCountB = mergeSortAndCountInversions(arr + m, size - m);

	int* arrPartA = new int[m];
	int* arrPartB = new int[size - m];

	memcpy(arrPartA, arr, sizeof(int) * m);
	memcpy(arrPartB, arr + m, sizeof(int) * (size - m));

	int i = 0, j = 0, k = 0;

	while (k < size)
	{
		if (arrPartA[i] < arrPartB[j])
		{
			arr[k] = arrPartA[i];
			i++;

			invCountC += j;
		}
		else
		{
			arr[k] = arrPartB[j];
			j++;

			invCountC += 1;
		}

		k++;

		if (i >= m || j >= (size - m))
			break;
	}

	invCountC -= j;

	while (i < m)
	{
		arr[k] = arrPartA[i];
		k++;
		i++;

		invCountC += j;
	}
	while (j < (size - m))
	{
		arr[k] = arrPartB[j];
		k++;
		j++;
	}

	delete []arrPartA;
	delete []arrPartB;

	return (invCountA + invCountB + invCountC);
}

long long invequal(int *arr, int size){
    unordered_map<int,int> m;
    for(int i=0;i<size;i++){
        m[arr[i]]++;
    }
    long long ans=0;
    for(auto it:m){
        int v=it.second;
        ans+=(1LL*v*(v-1))/2;
    }
    return ans;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    for(int i=0;i<(1<<n);i++){
        cin >> a[i];
    }
    vector <long long> inv(n+1,0),eq(n+1,0);
    for(int i=0;i<=n;i++){
        int p=(1<<i);
        for(int j=0;j<(1<<n);j+=p){
            int* temp = new int[p];
            memcpy(temp,a+j,sizeof(int)*p);
            inv[i]+=mergeSortAndCountInversions(temp,p);
            eq[i]+=invequal(temp,p);
            delete []temp;
        }
        inv[i]-=eq[i];
    }
    int q;
    cin >> q;
    while(q--){
        int m;
        cin >> m;
        long long oi = inv[m];
        for(int i=0;i<=m;i++){
            int p = (1<<i), no = (1<<n)/p;
            inv[i] = 1LL * no * ((1LL*p*(p-1))/2) - eq[i] - inv[i];
        }
        for(int i=m+1;i<=n;i++){
            long long temp = inv[i];
            inv[i]=inv[i]-oi+inv[i-1];
            oi=temp;
        }
        /*for(int i=0;i<(1<<n);i+=p){
            reverse(a+i,a+i+p);
        }
        for(int i=0;i<(1<<n);i++){
            cout << a[i] << " ";
        }
        cout << endl;*/
        cout << inv[n] << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int a[1<<20];

long long mergeSortAndCountInversions(int* arr, int size)
{
    int m;
	if (size <= 1)
		return 0;
	m = size / 2;
	long long invCountA = 0, invCountB = 0, invCountC = 0;
	invCountA = mergeSortAndCountInversions(arr, m);
	invCountB = mergeSortAndCountInversions(arr + m, size - m);

	int* arrPartA = new int[m];
	int* arrPartB = new int[size - m];

	memcpy(arrPartA, arr, sizeof(int) * m);
	memcpy(arrPartB, arr + m, sizeof(int) * (size - m));

	int i = 0, j = 0, k = 0;

	while (k < size)
	{
		if (arrPartA[i] < arrPartB[j])
		{
			arr[k] = arrPartA[i];
			i++;

			invCountC += j;
		}
		else
		{
			arr[k] = arrPartB[j];
			j++;

			invCountC += 1;
		}

		k++;

		if (i >= m || j >= (size - m))
			break;
	}

	invCountC -= j;

	while (i < m)
	{
		arr[k] = arrPartA[i];
		k++;
		i++;

		invCountC += j;
	}
	while (j < (size - m))
	{
		arr[k] = arrPartB[j];
		k++;
		j++;
	}

	delete []arrPartA;
	delete []arrPartB;

	return (invCountA + invCountB + invCountC);
}

long long invequal(int *arr, int size){
    unordered_map<int,int> m;
    for(int i=0;i<size;i++){
        m[arr[i]]++;
    }
    long long ans=0;
    for(auto it:m){
        int v=it.second;
        ans+=(1LL*v*(v-1))/2;
    }
    return ans;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    for(int i=0;i<(1<<n);i++){
        cin >> a[i];
    }
    vector <long long> inv(n+1,0),eq(n+1,0);
    for(int i=0;i<=n;i++){
        int p=(1<<i);
        for(int j=0;j<(1<<n);j+=p){
            int* temp = new int[p];
            memcpy(temp,a+j,sizeof(int)*p);
            inv[i]+=mergeSortAndCountInversions(temp,p);
            eq[i]+=invequal(temp,p);
            delete []temp;
        }
    }
    int q;
    cin >> q;
    while(q--){
        int m;
        cin >> m;
        int p = (1<<m), no = (1<<n)/p;
        long long oi = inv[m];
        inv[m] = 1LL * no * (1LL*p*(p-1))/2 - eq[m] - inv[m];
        for(int i=m+1;i<=n;i++){
            long long temp = inv[i];
            inv[i]=inv[i]-oi+inv[i-1];
            oi=temp;
        }
        cout << inv[n] << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int a[1<<20];

long long mergeSortAndCountInversions(int* arr, int size)
{
    int m;
	if (size <= 1)
		return 0;
	m = size / 2;
	long long invCountA = 0, invCountB = 0, invCountC = 0;
	invCountA = mergeSortAndCountInversions(arr, m);
	invCountB = mergeSortAndCountInversions(arr + m, size - m);

	int* arrPartA = new int[m];
	int* arrPartB = new int[size - m];

	memcpy(arrPartA, arr, sizeof(int) * m);
	memcpy(arrPartB, arr + m, sizeof(int) * (size - m));

	int i = 0, j = 0, k = 0;

	while (k < size)
	{
		if (arrPartA[i] < arrPartB[j])
		{
			arr[k] = arrPartA[i];
			i++;

			invCountC += j;
		}
		else
		{
			arr[k] = arrPartB[j];
			j++;

			invCountC += 1;
		}

		k++;

		if (i >= m || j >= (size - m))
			break;
	}

	invCountC -= j;

	while (i < m)
	{
		arr[k] = arrPartA[i];
		k++;
		i++;

		invCountC += j;
	}
	while (j < (size - m))
	{
		arr[k] = arrPartB[j];
		k++;
		j++;
	}

	delete []arrPartA;
	delete []arrPartB;

	return (invCountA + invCountB + invCountC);
}

long long invequal(int *arr, int size){
    unordered_map<int,int> m;
    for(int i=0;i<size;i++){
        m[arr[i]]++;
    }
    long long ans=0;
    for(auto it:m){
        int v=it.second;
        ans+=(1LL*v*(v-1))/2;
    }
    return ans;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    for(int i=0;i<(1<<n);i++){
        cin >> a[i];
    }
    vector <long long> inv(n+1,0),eq(n+1,0);
    for(int i=0;i<=n;i++){
        int p=(1<<i);
        for(int j=0;j<(1<<n);j+=p){
            int* temp = new int[p];
            memcpy(temp,a+j,sizeof(int)*p);
            inv[i]+=mergeSortAndCountInversions(temp,p);
            eq[i]+=invequal(temp,p);
            delete []temp;
        }
        inv[i]-=eq[i];
    }
    int q;
    cin >> q;
    while(q--){
        int m;
        cin >> m;
        int p = (1<<m), no = (1<<n)/p;
        long long oi = inv[m];
        inv[m] = 1LL * no * (1LL*p*(p-1))/2 - eq[m] - inv[m];
        for(int i=m+1;i<=n;i++){
            long long temp = inv[i];
            inv[i]=inv[i]-oi+inv[i-1];
            oi=temp;
        }
        cout << inv[n] << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
int a[1<<20];
vector <long long> inv(21,0),eq(21,0);

char s[21];

void print(long long t) {
	int pos = 0;
	if (t == 0) {
		s[pos] = (char)(t + '0');
		pos++;
	}
	while (t != 0) {
		s[pos] = (char)(t % 10 + '0');
		pos++;
		t /= 10;
	}
	for (int i = pos - 1; i >= 0; i--) {
		_putchar_nolock(s[i]);
	}
	_putchar_nolock('\n');
}

void scan(int &t) {
	t = 0;
	char k;
	while (true) {
		k = _getchar_nolock();
		if (k == ' ' || k == '\n') {
			break;
		}
		t *= 10;
		t += k - '0';
	}
}

long long mergeSortAndCountInversions(int* arr, int size, int lgsize)
{
    int m;
	if (size <= 1)
		return 0;
	m = size / 2;
	long long invCountA = 0, invCountB = 0, invCountC = 0;
    long long eqCountC = 0;
	invCountA = mergeSortAndCountInversions(arr, m, lgsize-1);
	invCountB = mergeSortAndCountInversions(arr + m, size - m, lgsize-1);

	int* arrPartA = new int[m];
	int* arrPartB = new int[size - m];

	memcpy(arrPartA, arr, sizeof(int) * m);
	memcpy(arrPartB, arr + m, sizeof(int) * (size - m));

	int i = 0, j = 0, k = 0;

	while (k < size)
	{
		if (arrPartA[i] < arrPartB[j])
		{
			arr[k] = arrPartA[i];
			i++;

			invCountC += j;
		}
		else
		{
			arr[k] = arrPartB[j];
			j++;
		}

		k++;

		if (i >= m || j >= (size - m))
			break;
	}

	while (i < m)
	{
		arr[k] = arrPartA[i];
		k++;
		i++;

		invCountC += j;
	}
	while (j < (size - m))
	{
		arr[k] = arrPartB[j];
		k++;
		j++;
	}
    /* optimise this */
    int la=1;
    for(int i=1;i<size;i++){
        if(arr[i]!=arr[i-1]){
            eqCountC+=(1LL*la*(la-1))/2;
            la=1;
        }
        else{
            la++;
        }
    }
    eqCountC+=(1LL*la*(la-1))/2;
	delete []arrPartA;
	delete []arrPartB;
    inv[lgsize]+=(invCountA + invCountB + invCountC);
    eq[lgsize]+=eqCountC;
    return (invCountA + invCountB + invCountC);
}

int main(){
    ios_base::sync_with_stdio(false);
    scanf("%d\n",&n);
    for(int i=0;i<(1<<n);i++){
        scan(a[i]);
    }
    mergeSortAndCountInversions(a, 1<<n, n);
    for(int i=0;i<=n;i++){
        inv[i] -= eq[i];
    }
    //cout << "done" << endl;
    int q;
    scanf("%d\n",&q);
    while(q--){
        int m;
        scan(m);
        long long oi = inv[m];
        for(int i=0;i<=m;i++){
            int p = (1<<i), no = (1<<n)/p;
            inv[i] = 1LL * no * ((1LL*p*(p-1))/2) - eq[i] - inv[i];
        }
        for(int i=m+1;i<=n;i++){
            long long temp = inv[i];
            inv[i]=inv[i]-oi+inv[i-1];
            oi=temp;
        }
        /*for(int i=0;i<(1<<n);i+=p){
            reverse(a+i,a+i+p);
        }
        for(int i=0;i<(1<<n);i++){
            cout << a[i] << " ";
        }
        cout << endl;*/
        print(inv[n]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
int a[1<<20];
vector <long long> inv(21,0),eq(21,0);

long long mergeSortAndCountInversions(int* arr, int size, int lgsize)
{
    int m;
	if (size <= 1)
		return 0;
	m = size / 2;
	long long invCountA = 0, invCountB = 0, invCountC = 0;
    long long eqCountC = 0;
	invCountA = mergeSortAndCountInversions(arr, m, lgsize-1);
	invCountB = mergeSortAndCountInversions(arr + m, size - m, lgsize-1);

	int* arrPartA = new int[m];
	int* arrPartB = new int[size - m];

	memcpy(arrPartA, arr, sizeof(int) * m);
	memcpy(arrPartB, arr + m, sizeof(int) * (size - m));

	int i = 0, j = 0, k = 0;

	while (k < size)
	{
		if (arrPartA[i] < arrPartB[j])
		{
			arr[k] = arrPartA[i];
			i++;

			invCountC += j;
		}
		else
		{
			arr[k] = arrPartB[j];
			j++;
		}

		k++;

		if (i >= m || j >= (size - m))
			break;
	}

	while (i < m)
	{
		arr[k] = arrPartA[i];
		k++;
		i++;

		invCountC += j;
	}
	while (j < (size - m))
	{
		arr[k] = arrPartB[j];
		k++;
		j++;
	}
    /* optimise this */
    int la=1;
    for(int i=1;i<size;i++){
        if(arr[i]!=arr[i-1]){
            eqCountC+=(1LL*la*(la-1))/2;
            la=1;
        }
        else{
            la++;
        }
    }
    eqCountC+=(1LL*la*(la-1))/2;
	delete []arrPartA;
	delete []arrPartB;
    inv[lgsize]+=(invCountA + invCountB + invCountC);
    eq[lgsize]+=eqCountC;
    return (invCountA + invCountB + invCountC);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    for(int i=0;i<(1<<n);i++){
        cin >> a[i];
    }
    mergeSortAndCountInversions(a, 1<<n, n);
    for(int i=0;i<=n;i++){
        inv[i] -= eq[i];
    }
    //cout << "done" << endl;
    int q;
    cin >> q;
    while(q--){
        int m;
        cin >> m;
        long long oi = inv[m];
        for(int i=0;i<=m;i++){
            int p = (1<<i), no = (1<<n)/p;
            inv[i] = 1LL * no * ((1LL*p*(p-1))/2) - eq[i] - inv[i];
        }
        for(int i=m+1;i<=n;i++){
            long long temp = inv[i];
            inv[i]=inv[i]-oi+inv[i-1];
            oi=temp;
        }
        /*for(int i=0;i<(1<<n);i+=p){
            reverse(a+i,a+i+p);
        }
        for(int i=0;i<(1<<n);i++){
            cout << a[i] << " ";
        }
        cout << endl;*/
        cout << inv[n] << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
int a[1<<20];
vector <long long> inv(21,0),eq(21,0);

char s[21];

void print(long long t) {
	int pos = 0;
	if (t == 0) {
		s[pos] = (char)(t + '0');
		pos++;
	}
	while (t != 0) {
		s[pos] = (char)(t % 10 + '0');
		pos++;
		t /= 10;
	}
	for (int i = pos - 1; i >= 0; i--) {
		_putchar_nolock(s[i]);
	}
	_putchar_nolock('\n');
}

void scan(int &t) {
	t = 0;
	char k;
	while (true) {
		k = _getchar_nolock();
		if (k == ' ' || k == '\n') {
			break;
		}
		t *= 10;
		t += k - '0';
	}
}

long long mergeSortAndCountInversions(int* arr, int size, int lgsize)
{
    int m;
	if (size <= 1)
		return 0;
	m = size / 2;
	long long invCountA = 0, invCountB = 0, invCountC = 0;
    long long eqCountC = 0;
	invCountA = mergeSortAndCountInversions(arr, m, lgsize-1);
	invCountB = mergeSortAndCountInversions(arr + m, size - m, lgsize-1);

	int* arrPartA = new int[m];
	int* arrPartB = new int[size - m];

	memcpy(arrPartA, arr, sizeof(int) * m);
	memcpy(arrPartB, arr + m, sizeof(int) * (size - m));

	int i = 0, j = 0, k = 0;

	while (k < size)
	{
		if (arrPartA[i] < arrPartB[j])
		{
			arr[k] = arrPartA[i];
			i++;

			invCountC += j;
		}
		else
		{
			arr[k] = arrPartB[j];
			j++;
		}

		k++;

		if (i >= m || j >= (size - m))
			break;
	}

	while (i < m)
	{
		arr[k] = arrPartA[i];
		k++;
		i++;

		invCountC += j;
	}
	while (j < (size - m))
	{
		arr[k] = arrPartB[j];
		k++;
		j++;
	}
    /* optimise this */
    int la=1;
    for(int i=1;i<size;i++){
        if(arr[i]!=arr[i-1]){
            eqCountC+=(1LL*la*(la-1))/2;
            la=1;
        }
        else{
            la++;
        }
    }
    eqCountC+=(1LL*la*(la-1))/2;
	delete []arrPartA;
	delete []arrPartB;
    inv[lgsize]+=(invCountA + invCountB + invCountC);
    eq[lgsize]+=eqCountC;
    return (invCountA + invCountB + invCountC);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    for(int i=0;i<(1<<n);i++){
        cin >> a[i];
    }
    mergeSortAndCountInversions(a, 1<<n, n);
    for(int i=0;i<=n;i++){
        inv[i] -= eq[i];
    }
    //cout << "done" << endl;
    int q;
    cin >> q;
    while(q--){
        int m;
        scan(m);
        long long oi = inv[m];
        for(int i=0;i<=m;i++){
            int p = (1<<i), no = (1<<n)/p;
            inv[i] = 1LL * no * ((1LL*p*(p-1))/2) - eq[i] - inv[i];
        }
        for(int i=m+1;i<=n;i++){
            long long temp = inv[i];
            inv[i]=inv[i]-oi+inv[i-1];
            oi=temp;
        }
        /*for(int i=0;i<(1<<n);i+=p){
            reverse(a+i,a+i+p);
        }
        for(int i=0;i<(1<<n);i++){
            cout << a[i] << " ";
        }
        cout << endl;*/
        print(inv[n]);
    }
    return 0;
}


#include <iostream>
#include <map>

const int MAX_LEN = 1 << 21;
const int MAX_BIT = 21;

using namespace std;

int arr [MAX_LEN], invc [MAX_BIT][2], flipped [MAX_BIT];

class fenwick {
  int *val, size;

public:
  fenwick (int _size) {
    size = _size;
    val = new int [size + 1];
    for (int i = 0; i <= size; i++) {
      val[i] = 0;
    }
  }

  void add (int index, int value) {
    for (int i = index; i <= size; i += i & -i) {
      val[i] += value;
    }
  }

  int get (int index) {
    int ans = 0;
    for (int i = index; i > 0; i -= i & -i) {
      ans += val[i];
    }
    return ans;
  }
};

int main () {
  ios::sync_with_stdio(false);

  int length;
  cin >> length;

  map<int, int> compr;
  for (int i = 0; i < 1 << length; i++) {
    cin >> arr[i];

    if (compr.find(arr[i]) == compr.end()) {
      compr[arr[i]] = 0;
    }
  }

  int cur_comp = 1;
  for (auto i = compr.begin(); i != compr.end(); i++) {
    i->second = cur_comp;
    cur_comp++;
  }

  for (int i = 0; i < 1 << length; i++) {
    arr[i] = compr[arr[i]];
  }

  fenwick counter (cur_comp);
  for (int i = 1; i <= length; i++) {
    int l = 0, r = 0;
    while (r < 1 << length) {
      counter.add(arr[r], 1);
      r++;

      invc[i][0] += counter.get(cur_comp) - counter.get(arr[r - 1]);
      invc[i][1] += counter.get(arr[r - 1] - 1);

      if (r % (1 << i) == 0) {
        while (l < r) {
          counter.add(arr[l], -1);
          l++;
        }
      }
    }
  }

  for (int i = length; i > 1; i--) {
    invc[i][0] -= invc[i - 1][0];
    invc[i][1] -= invc[i - 1][1];
  }

  int cur_ans = 0;
  for (int i = 1; i <= length; i++) {
    cur_ans += invc[i][0];
  }

  int queryc;
  cin >> queryc;
  for (int i = 0; i < queryc; i++) {
    int query;
    cin >> query;

    for (int j = query; j > 0; j--) {
      flipped[j] = !flipped[j];
      cur_ans -= invc[j][!flipped[j]];
      cur_ans += invc[j][flipped[j]];
    }

    cout << cur_ans << '\n';
  }
}


#include <iostream>
#include <map>

typedef long long llong;

const int MAX_LEN = 1 << 21;
const int MAX_BIT = 21;

using namespace std;

llong arr [MAX_LEN], invc [MAX_BIT][2], flipped [MAX_BIT];

class fenwick {
  llong *val, size;

public:
  fenwick (llong _size) {
    size = _size;
    val = new llong [size + 1];
    for (llong i = 0; i <= size; i++) {
      val[i] = 0;
    }
  }

  void add (llong index, llong value) {
    for (llong i = index; i <= size; i += i & -i) {
      val[i] += value;
    }
  }

  llong get (llong index) {
    llong ans = 0;
    for (llong i = index; i > 0; i -= i & -i) {
      ans += val[i];
    }
    return ans;
  }
};

int main () {
  ios::sync_with_stdio(false);

  llong length;
  cin >> length;

  map<llong, llong> compr;
  for (llong i = 0; i < 1 << length; i++) {
    cin >> arr[i];

    if (compr.find(arr[i]) == compr.end()) {
      compr[arr[i]] = 0;
    }
  }

  llong cur_comp = 1;
  for (auto i = compr.begin(); i != compr.end(); i++) {
    i->second = cur_comp;
    cur_comp++;
  }

  for (llong i = 0; i < 1 << length; i++) {
    arr[i] = compr[arr[i]];
  }

  fenwick counter (cur_comp);
  for (llong i = 1; i <= length; i++) {
    llong l = 0, r = 0;
    while (r < 1 << length) {
      counter.add(arr[r], 1);
      r++;

      invc[i][0] += counter.get(cur_comp) - counter.get(arr[r - 1]);
      invc[i][1] += counter.get(arr[r - 1] - 1);

      if (r % (1 << i) == 0) {
        while (l < r) {
          counter.add(arr[l], -1);
          l++;
        }
      }
    }
  }

  for (llong i = length; i > 1; i--) {
    invc[i][0] -= invc[i - 1][0];
    invc[i][1] -= invc[i - 1][1];
  }

  llong cur_ans = 0;
  for (llong i = 1; i <= length; i++) {
    cur_ans += invc[i][0];
  }

  llong queryc;
  cin >> queryc;
  for (llong i = 0; i < queryc; i++) {
    llong query;
    cin >> query;

    for (llong j = query; j > 0; j--) {
      flipped[j] = !flipped[j];
      cur_ans -= invc[j][!flipped[j]];
      cur_ans += invc[j][flipped[j]];
    }

    cout << cur_ans << '\n';
  }
}


#include <iostream>
#include <map>
#include <vector>
#include <cassert>

typedef long long llong;

const int MAX_LEN = 1 << 21;
const int MAX_BIT = 21;

using namespace std;

llong arr [MAX_LEN][2], invc [MAX_BIT][2], flipped [MAX_BIT];

void merge_sort (llong left, llong right, llong depth, llong mode) {
  if (right - left > 1) {
    merge_sort(left, (left + right) / 2, depth - 1, mode);
    merge_sort((left + right) / 2, right, depth - 1, mode);
  } else {
    assert(depth == 0);
    return;
  }

  vector<llong> temp;
  llong aptr = left, bptr = (left + right) / 2;
  while (aptr < (left + right) / 2 && bptr < right) {
    llong ltop = arr[aptr][mode], rtop = arr[bptr][mode];

    if (ltop < rtop) {
      temp.push_back(ltop);
      aptr++;

      if (mode == 1) {
        invc[depth][mode] += right - bptr;
      }
    } else if (ltop == rtop) {
      if (mode == 0) {
        temp.push_back(ltop);
        aptr++;
      } else {
        temp.push_back(rtop);
        bptr++;
      }
    } else {
      temp.push_back(rtop);
      bptr++;

      if (mode == 0) {
        invc[depth][mode] += (left + right) / 2 - aptr;
      }
    }
  }

  while (aptr < (left + right) / 2) {
    temp.push_back(arr[aptr][mode]);
    aptr++;
  }

  while (bptr < right) {
    temp.push_back(arr[bptr][mode]);
    bptr++;
  }

  for (llong i = 0; i < (llong) temp.size(); i++) {
    arr[i + left][mode] = temp[i];
  }
}

int main () {
  ios::sync_with_stdio(false);

  llong length;
  cin >> length;

  for (llong i = 0; i < 1 << length; i++) {
    cin >> arr[i][0];
    arr[i][1] = arr[i][0];
  }

  merge_sort(0, 1 << length, length, 0);
  merge_sort(0, 1 << length, length, 1);

  /*
  for (llong i = length; i > 1; i--) {
    invc[i][0] -= invc[i - 1][0];
    invc[i][1] -= invc[i - 1][1];
  }
  */

  llong cur_ans = 0;
  for (llong i = 1; i <= length; i++) {
    cur_ans += invc[i][0];
  }

  llong queryc;
  cin >> queryc;
  for (llong i = 0; i < queryc; i++) {
    llong query;
    cin >> query;

    for (llong j = query; j > 0; j--) {
      flipped[j] = !flipped[j];
      cur_ans -= invc[j][!flipped[j]];
      cur_ans += invc[j][flipped[j]];
    }

    cout << cur_ans << '\n';
  }
}


#include <iostream>
#include <map>

typedef long long llong;

const int MAX_LEN = 1 << 21;
const int MAX_BIT = 21;

using namespace std;

int arr [MAX_LEN], invc [MAX_BIT][2], flipped [MAX_BIT];

class fenwick {
  int *val, size;

public:
  fenwick (int _size) {
    size = _size;
    val = new int [size + 1];
    for (int i = 0; i <= size; i++) {
      val[i] = 0;
    }
  }

  void add (int index, int value) {
    for (int i = index; i <= size; i += i & -i) {
      val[i] += value;
    }
  }

  int get (int index) {
    int ans = 0;
    for (int i = index; i > 0; i -= i & -i) {
      ans += val[i];
    }
    return ans;
  }
};

int main () {
  ios::sync_with_stdio(false);

  int length;
  cin >> length;

  map<int, int> compr;
  for (int i = 0; i < 1 << length; i++) {
    cin >> arr[i];

    if (compr.find(arr[i]) == compr.end()) {
      compr[arr[i]] = 0;
    }
  }

  int cur_comp = 1;
  for (auto i = compr.begin(); i != compr.end(); i++) {
    i->second = cur_comp;
    cur_comp++;
  }

  for (int i = 0; i < 1 << length; i++) {
    arr[i] = compr[arr[i]];
  }

  fenwick counter (cur_comp);
  for (int i = 1; i <= length; i++) {
    int l = 0, r = 0;
    while (r < 1 << length) {
      counter.add(arr[r], 1);
      r++;

      invc[i][0] += counter.get(cur_comp) - counter.get(arr[r - 1]);
      invc[i][1] += counter.get(arr[r - 1] - 1);

      if (r % (1 << i) == 0) {
        while (l < r) {
          counter.add(arr[l], -1);
          l++;
        }
      }
    }
  }

  for (int i = length; i > 1; i--) {
    invc[i][0] -= invc[i - 1][0];
    invc[i][1] -= invc[i - 1][1];
  }

  llong cur_ans = 0;
  for (int i = 1; i <= length; i++) {
    cur_ans += invc[i][0];
  }

  int queryc;
  cin >> queryc;
  for (int i = 0; i < queryc; i++) {
    int query;
    cin >> query;

    for (int j = query; j > 0; j--) {
      flipped[j] = !flipped[j];
      cur_ans -= invc[j][!flipped[j]];
      cur_ans += invc[j][flipped[j]];
    }

    cout << cur_ans << '\n';
  }
}


#include <iostream>
#include <map>

typedef long long llong;

const int MAX_LEN = 1 << 21;
const int MAX_BIT = 21;

using namespace std;

int arr [MAX_LEN], invc [MAX_BIT][2], flipped [MAX_BIT];

class fenwick {
  int *val, size;

public:
  fenwick (int _size) {
    size = _size;
    val = new int [size + 1];
    for (int i = 0; i <= size; i++) {
      val[i] = 0;
    }
  }

  void add (int index, int value) {
    for (int i = index; i <= size; i += i & -i) {
      val[i] += value;
    }
  }

  int get (int index) {
    int ans = 0;
    for (int i = index; i > 0; i -= i & -i) {
      ans += val[i];
    }
    return ans;
  }
};

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

  int length;
  cin >> length;

  map<int, int> compr;
  for (int i = 0; i < 1 << length; i++) {
    cin >> arr[i];

    if (compr.find(arr[i]) == compr.end()) {
      compr[arr[i]] = 0;
    }
  }

  int cur_comp = 1;
  for (auto i = compr.begin(); i != compr.end(); i++) {
    i->second = cur_comp;
    cur_comp++;
  }

  for (int i = 0; i < 1 << length; i++) {
    arr[i] = compr[arr[i]];
  }

  fenwick counter (cur_comp);
  for (int i = 1; i <= length; i++) {
    int l = 0, r = 0;
    while (r < 1 << length) {
      counter.add(arr[r], 1);
      r++;

      invc[i][0] += counter.get(cur_comp) - counter.get(arr[r - 1]);
      invc[i][1] += counter.get(arr[r - 1] - 1);

      if (r % (1 << i) == 0) {
        while (l < r) {
          counter.add(arr[l], -1);
          l++;
        }
      }
    }
  }

  for (int i = length; i > 1; i--) {
    invc[i][0] -= invc[i - 1][0];
    invc[i][1] -= invc[i - 1][1];
  }

  llong cur_ans = 0;
  for (int i = 1; i <= length; i++) {
    cur_ans += invc[i][0];
  }

  int queryc;
  cin >> queryc;
  for (int i = 0; i < queryc; i++) {
    int query;
    cin >> query;

    for (int j = query; j > 0; j--) {
      flipped[j] = !flipped[j];
      cur_ans -= invc[j][!flipped[j]];
      cur_ans += invc[j][flipped[j]];
    }

    cout << cur_ans << '\n';
  }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 998244353
ll power(ll a,ll b, ll m=MOD)
{
    ll res=1;
    while(b>0)
    {
        if(b&1)
            res=(res*a)%m;
        a=(a*a)%m;
        b>>=1;
    }
    return res;
}
ll inverse(ll a,ll m=MOD)
{
    return power(a,m-2,m);
}
#define INFl 1e18
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<long long>
#define ii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<ii>
#define F first
#define S second
#define forl(i,n) for(int i=0;i<n;i++)
#define fore(i,n) for(int i=1;i<=n;i++)
#define rforl(i,n)  for(int i=n-1;i>=0;i--)
#define rfore(i,n)  for(int i=n;i>=1;i--)
#define INF 1000000009
#define gcd(a,b) __gcd(a,b)
#define all(x)  x.begin(),x.end()
#define mp make_pair
#define pb push_back
#define print(x)    for(auto it=x.begin();it!=x.end();it++) cout<<*it<<' '; cout<<endl;
#define printii(x)  for(auto it=x.begin();it!=x.end();it++) cout<<it->F<<' '<<it->S<<'\n';
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
int n;
vi a;
vector<pll> bt,sum;
void merge(int p,int l,int r)
{
    vi L,R,res;
    int mid=(l+r)/2;
    for(int i=l;i<=mid;i++)
        L.pb(a[i]);
    for(int i=mid+1;i<=r;i++)
        R.pb(a[i]);
    ll ans1=0,ans2=0;
    int n1=L.size(),n2=R.size();
    forl(i,n1)
    {
        int x=L[i];
        int pos1=upper_bound(all(R),x)-R.begin();
        ans2+=n2-pos1;
        int pos2=upper_bound(all(R),x-1)-R.begin();
        ans1+=pos2;
    }
    bt[p]=mp(ans1,ans2);
    int pos1=0,pos2=0;
    while(pos1<n1 && pos2<n2)
    {
        if(L[pos1]<R[pos2])
            res.pb(L[pos1++]);
        else
            res.pb(R[pos2++]);
    }
    while(pos1<n1)
        res.pb(L[pos1++]);
    while(pos2<n2)
        res.pb(R[pos2++]);
    for(int i=l;i<=r;i++)
        a[i]=res[i-l];
}
void construct(int p,int l,int r,int d)
{
    if(l==r)
        return;
    int mid=(l+r)/2;
    construct(2*p+1,l,mid,d+1);
    construct(2*p+2,mid+1,r,d+1);
    merge(p,l,r);
    sum[d].F+=bt[p].F;
    sum[d].S+=bt[p].S;
}
int main()
{
    scanf("%d",&n);
    int p=(1<<n);
    a.resize(p);
    sum.assign(n,mp(0,0));
    forl(i,p)
        scanf("%d",&a[i]);
    bt.resize(p);
    construct(0,0,p-1,0);
    vi type(n,0);
    int m;
    cin>>m;
    forl(i,m)
    {
        int q;
        scanf("%d",&q);
        q=n-q;
        for(int j=q;j<n;j++)
            type[j]^=1;
        ll ans=0;
        for(int j=0;j<n;j++)
        {
            if(type[j]==0)
                ans+=sum[j].F;
            else
                ans+=sum[j].S;
        }
        printf("%lld\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 998244353
ll power(ll a,ll b, ll m=MOD)
{
    ll res=1;
    while(b>0)
    {
        if(b&1)
            res=(res*a)%m;
        a=(a*a)%m;
        b>>=1;
    }
    return res;
}
ll inverse(ll a,ll m=MOD)
{
    return power(a,m-2,m);
}
#define INFl 1e18
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<long long>
#define ii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<ii>
#define F first
#define S second
#define forl(i,n) for(int i=0;i<n;i++)
#define fore(i,n) for(int i=1;i<=n;i++)
#define rforl(i,n)  for(int i=n-1;i>=0;i--)
#define rfore(i,n)  for(int i=n;i>=1;i--)
#define INF 1000000009
#define gcd(a,b) __gcd(a,b)
#define all(x)  x.begin(),x.end()
#define mp make_pair
#define pb push_back
#define print(x)    for(auto it=x.begin();it!=x.end();it++) cout<<*it<<' '; cout<<endl;
#define printii(x)  for(auto it=x.begin();it!=x.end();it++) cout<<it->F<<' '<<it->S<<'\n';
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
int n;
vi a;
vector<pll> bt,sum;
void merge(int p,int l,int r)
{
    vi L,R,res;
    int mid=(l+r)/2;
    for(int i=l;i<=mid;i++)
        L.pb(a[i]);
    for(int i=mid+1;i<=r;i++)
        R.pb(a[i]);
    ll ans1=0,ans2=0;
    int n1=L.size(),n2=R.size();
    forl(i,n1)
    {
        int x=L[i];
        int pos1=upper_bound(all(R),x)-R.begin();
        ans2+=n2-pos1;
        int pos2=upper_bound(all(R),x-1)-R.begin();
        ans1+=pos2;
    }
    bt[p]=mp(ans1,ans2);
    int pos1=0,pos2=0;
    while(pos1<n1 && pos2<n2)
    {
        if(L[pos1]<R[pos2])
            res.pb(L[pos1++]);
        else
            res.pb(R[pos2++]);
    }
    while(pos1<n1)
        res.pb(L[pos1++]);
    while(pos2<n2)
        res.pb(R[pos2++]);
    for(int i=l;i<=r;i++)
        a[i]=res[i-l];
}
void construct(int p,int l,int r,int d)
{
    if(l==r)
        return;
    int mid=(l+r)/2;
    construct(2*p+1,l,mid,d+1);
    construct(2*p+2,mid+1,r,d+1);
    merge(p,l,r);
    sum[d].F+=bt[p].F;
    sum[d].S+=bt[p].S;
}
int main()
{
    cin>>n;
    int p=(1<<n);
    a.resize(p);
    sum.assign(n,mp(0,0));
    forl(i,p)
        cin>>a[i];
    bt.resize(p);
    construct(0,0,p-1,0);
    vi type(n,0);
    int m;
    cin>>m;
    forl(i,m)
    {
        int q;
        cin>>q;
        q=n-q;
        for(int j=q;j<n;j++)
            type[j]^=1;
        ll ans=0;
        for(int j=0;j<n;j++)
        {
            if(type[j]==0)
                ans+=sum[j].F;
            else
                ans+=sum[j].S;
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 998244353
ll power(ll a,ll b, ll m=MOD)
{
    ll res=1;
    while(b>0)
    {
        if(b&1)
            res=(res*a)%m;
        a=(a*a)%m;
        b>>=1;
    }
    return res;
}
ll inverse(ll a,ll m=MOD)
{
    return power(a,m-2,m);
}
#define INFl 1e18
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<long long>
#define ii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<ii>
#define F first
#define S second
#define forl(i,n) for(int i=0;i<n;i++)
#define fore(i,n) for(int i=1;i<=n;i++)
#define rforl(i,n)  for(int i=n-1;i>=0;i--)
#define rfore(i,n)  for(int i=n;i>=1;i--)
#define INF 1000000009
#define gcd(a,b) __gcd(a,b)
#define all(x)  x.begin(),x.end()
#define mp make_pair
#define pb push_back
#define print(x)    for(auto it=x.begin();it!=x.end();it++) cout<<*it<<' '; cout<<endl;
#define printii(x)  for(auto it=x.begin();it!=x.end();it++) cout<<it->F<<' '<<it->S<<'\n';
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
int n;
vi a;
vector<pll> bt,sum;
void merge(int p,int l,int r)
{
    vi L,R,res;
    int mid=(l+r)/2;
    for(int i=l;i<=mid;i++)
        L.pb(a[i]);
    for(int i=mid+1;i<=r;i++)
        R.pb(a[i]);
    ll ans1=0,ans2=0;
    int n1=L.size(),n2=R.size();
    forl(i,n1)
    {
        int x=L[i];
        int pos1=upper_bound(all(R),x)-R.begin();
        ans2+=n2-pos1;
        int pos2=upper_bound(all(R),x-1)-R.begin();
        ans1+=pos2;
    }
    bt[p]=mp(ans1,ans2);
    int pos1=0,pos2=0;
    while(pos1<n1 && pos2<n2)
    {
        if(L[pos1]<R[pos2])
            res.pb(L[pos1++]);
        else
            res.pb(R[pos2++]);
    }
    while(pos1<n1)
        res.pb(L[pos1++]);
    while(pos2<n2)
        res.pb(R[pos2++]);
    for(int i=l;i<=r;i++)
        a[i]=res[i-l];
}
void construct(int p,int l,int r,int d)
{
    if(l==r)
        return;
    int mid=(l+r)/2;
    construct(2*p+1,l,mid,d+1);
    construct(2*p+2,mid+1,r,d+1);
    merge(p,l,r);
    sum[d].F+=bt[p].F;
    sum[d].S+=bt[p].S;
}
int main()
{
    fastio;
    cin>>n;
    int p=(1<<n);
    a.resize(p);
    sum.assign(n,mp(0,0));
    forl(i,p)
        cin>>a[i];
    bt.resize(p);
    construct(0,0,p-1,0);
    vi type(n,0);
    int m;
    cin>>m;
    forl(i,m)
    {
        int q;
        cin>>q;
        q=n-q;
        for(int j=q;j<n;j++)
            type[j]^=1;
        ll ans=0;
        for(int j=0;j<n;j++)
        {
            if(type[j]==0)
                ans+=sum[j].F;
            else
                ans+=sum[j].S;
        }
        cout<<ans<<endl;
    }
}


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

#define ll long long
#define ull unsigned long long
#define vii vector<int>
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pdd pair<double,double>
#define pldld pair<long long double, long long double>
#define ff first
#define ss second
#define pb push_back
#define read freopen("alu.txt","r",stdin);
#define write freopen("vorta.txt","w",stdout);
#define fastio ios::sync_with_stdio(false); cin.tie(NULL);
#define PI 2*acos(0.0)
#define DEBUG(x) cerr << #x << " = " << x << endl
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

const int MAX = 2e6 + 5, MOD = 1e9 + 7  , MAXLG = log2(MAX)+1;
const ll inf = 1e18 + 5;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
                                            tree_order_statistics_node_update>;

struct node{
    int val, rev;
    node(int val = 0, int rev = 0) : val(val), rev(rev){}
};
node height[25];

int arr[MAX];
void mergesort(int L, int R, vector<int>&v, int h, bool koi){
    if(L == R) return;
    int mid = (L + R)/2;

    mergesort(L, mid, v, h-1, koi);
    mergesort(mid+1, R, v, h-1, koi);

    vector<int>temp;
    int ret = 0;
    int i = L, j = mid+1;
    while(i <= mid && j <= R){
        if(v[i] <= v[j]) temp.pb(v[i++]);
        else ret += (mid - i + 1), temp.pb(v[j++]);
    }
    while(i <= mid) temp.pb(v[i++]);
    while(j <= R) temp.pb(v[j++]);
    for(int i = L, j = 0; i <= R; i++, j++) v[i] = temp[j];

    if(koi) height[h].val += ret;
    else height[h].rev += ret;
}

int main(){

    fastio;
    int n;
    cin>>n;
    for(int i=0; i < (1ll << n); i++) cin>>arr[i];
    vector<int>v;
    for(int i=0; i < (1ll << n); i++) v.pb(arr[i]);
    mergesort(0, v.size()-1, v, n, true);

    v.clear();
    for(int i=0; i < (1ll << n); i++) v.pb(-arr[i]);
    mergesort(0, v.size()-1, v, n, false);

//    for(int i=0; i<=n; i++){
//        DEBUG(i);
//        DEBUG(height[i].val);
//        DEBUG(height[i].rev);
//    }
    int m;
    cin>>m;
    while(m--){
        int q;
        cin>>q;
        int ans = 0;
        for(int i=q; i>0; i--) swap(height[i].val, height[i].rev);
        for(int i=0; i<=n; i++) ans += height[i].val;
        cout<<ans<<"\n";
    }
}


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

#define ll long long
#define ull unsigned long long
#define vii vector<int>
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pdd pair<double,double>
#define pldld pair<long long double, long long double>
#define ff first
#define ss second
#define pb push_back
#define read freopen("alu.txt","r",stdin);
#define write freopen("vorta.txt","w",stdout);
#define fastio ios::sync_with_stdio(false); cin.tie(NULL);
#define PI 2*acos(0.0)
#define DEBUG(x) cerr << #x << " = " << x << endl
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

const int MAX = 2e6 + 5, MOD = 1e9 + 7  , MAXLG = log2(MAX)+1;
const ll inf = 1e18 + 5;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
                                            tree_order_statistics_node_update>;

struct node{
    ll val, rev;
    node(ll val = 0, ll rev = 0) : val(val), rev(rev){}
};
node height[25];

int arr[MAX];
void mergesort(int L, int R, vector<int>&v, int h, bool koi){
    if(L == R) return;
    int mid = (L + R)/2;

    mergesort(L, mid, v, h-1, koi);
    mergesort(mid+1, R, v, h-1, koi);

    vector<int>temp;
    ll ret = 0;
    int i = L, j = mid+1;
    while(i <= mid && j <= R){
        if(v[i] <= v[j]) temp.pb(v[i++]);
        else ret += (mid - i + 1), temp.pb(v[j++]);
    }
    while(i <= mid) temp.pb(v[i++]);
    while(j <= R) temp.pb(v[j++]);
    for(int i = L, j = 0; i <= R; i++, j++) v[i] = temp[j];

    if(koi) height[h].val += ret;
    else height[h].rev += ret;
}

int main(){

    fastio;
    int n;
    cin>>n;
    for(int i=0; i < (1ll << n); i++) cin>>arr[i];
    vector<int>v;
    for(int i=0; i < (1ll << n); i++) v.pb(arr[i]);
    mergesort(0, v.size()-1, v, n, true);

    v.clear();
    for(int i=0; i < (1ll << n); i++) v.pb(-arr[i]);
    mergesort(0, v.size()-1, v, n, false);

//    for(int i=0; i<=n; i++){
//        DEBUG(i);
//        DEBUG(height[i].val);
//        DEBUG(height[i].rev);
//    }
    int m;
    cin>>m;
    while(m--){
        int q;
        cin>>q;
        ll ans = 0;
        for(int i=q; i>0; i--) swap(height[i].val, height[i].rev);
        for(int i=0; i<=n; i++) ans += height[i].val;
        cout<<ans<<"\n";
    }
}


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

#define ll long long
#define ull unsigned long long
#define vii vector<int>
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pdd pair<double,double>
#define pldld pair<long long double, long long double>
#define ff first
#define ss second
#define pb push_back
#define read freopen("alu.txt","r",stdin);
#define write freopen("vorta.txt","w",stdout);
#define fastio ios::sync_with_stdio(false); cin.tie(NULL);
#define PI 2*acos(0.0)
#define DEBUG(x) cerr << #x << " = " << x << endl
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

const int MAX = 2e6 + 5, MOD = 1e9 + 7  , MAXLG = log2(MAX)+1;
const ll inf = 1e18 + 5;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
                                            tree_order_statistics_node_update>;

struct node{
    int val, rev;
    node(){}
    node(int val, int rev) : val(val), rev(rev){}
};
node height[25];

int arr[MAX];
void calc(int L, int R, int h){
    if(L == R) return;
    int mid = (L + R)/2;

    ordered_set<pii>st;
    for(int i=L; i<=mid; i++) st.insert({-arr[i], i});
    for(int i=mid+1; i<=R; i++) height[h].val += st.order_of_key({-arr[i], -1});
    st.clear();
    for(int i=L; i<=mid; i++) st.insert({arr[i], i});
    for(int i=mid+1; i<=R; i++) height[h].rev += st.order_of_key({arr[i], -1});
    calc(L, mid, h-1), calc(mid+1, R, h-1);
}

int main(){

    fastio;
    int n;
    cin>>n;
    for(int i=0; i < (1ll << n); i++) cin>>arr[i];
    calc(0, (1ll << n) - 1, n);

    int m;
    cin>>m;
    while(m--){
        int q;
        cin>>q;
        int ans = 0;
        for(int i=q; i>0; i--) swap(height[i].val, height[i].rev);
        for(int i=0; i<=n; i++) ans += height[i].val;
        cout<<ans<<"\n";
    }
}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb emplace_back
#define INF (1LL<<59)

#define int ll
vector<int> score[2];
int n;

int dfs(vector<int> &v,int depth,int ind){

    if(depth==n)return 0;

    int cnt = 0;
    vector<int> a(v.begin(),v.begin()+v.size()/2);
    vector<int> b(v.begin()+v.size()/2,v.end());

    dfs(a,depth+1,ind);
    dfs(b,depth+1,ind);

    int cl=0,cr=0,p=0;
    int n = a.size();

    while(1){
        if(cl==n && cr==n)break;
//        cout<<cl<<" "<<cr<<endl;
        if(cr==n || cl<n && a[cl]<=b[cr]){
//            cout<<p<<" "<<cl<<endl;;
            v[p]=a[cl];
            p++;
            cl++;
        }else{
            cnt+=n-cl;
            v[p]=b[cr];
            p++;
            cr++;
        }
    }
    score[ind][depth]+=cnt;

    return cnt;
}

signed main(){
    cin>>n;
    vector<int> v(1<<n);
    rep(i,1<<n)scanf("%d",&v[i]);

    rep(i,2)score[i].resize(n,0);
    vector<int> tmp = v;
    dfs(tmp,0,0);
    tmp = v;
    reverse(all(tmp));
    dfs(tmp,0,1);

    set<int> st;

    int m;
    cin>>m;
    rep(i,m){
        int q;
        scanf("%d",&q);
        if(st.insert(q).second==false) st.erase(q);

        int sum=0;

        bool sign = false;

        int ans=0;
        rep(j,n){
            if(st.count(n-j))sign=!sign;
            ans+=score[sign][j];

        }
        cout<<ans<<endl;
    }

}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)
//#define INF (1LL<<59)

vector<int> score[2];
int n;

int dfs(vector<int> &v,int depth,int ind){

    if(depth==n)return 0;

    int cnt = 0;
    vector<int> a(v.begin(),v.begin()+v.size()/2);
    vector<int> b(v.begin()+v.size()/2,v.end());

    dfs(a,depth+1,ind);
    dfs(b,depth+1,ind);

    int cl=0,cr=0,p=0;
    int n = a.size();

    while(1){
        if(cl==n && cr==n)break;
//        cout<<cl<<" "<<cr<<endl;
        if(cr==n || cl<n && a[cl]<=b[cr]){
//            cout<<p<<" "<<cl<<endl;;
            v[p]=a[cl];
            p++;
            cl++;
        }else{
            cnt+=n-cl;
            v[p]=b[cr];
            p++;
            cr++;
        }
    }
    score[ind][depth]+=cnt;

    return cnt;
}

int main(){
    cin>>n;
    vector<int> v(1<<n);
    rep(i,1<<n)cin>>v[i];

    rep(i,2)score[i].resize(n,0);
    vector<int> tmp = v;
    dfs(tmp,0,0);
    tmp = v;
    reverse(all(tmp));
    dfs(tmp,0,1);

    set<int> st;

    int m;
    cin>>m;
    rep(i,m){
        int q;
        cin>>q;
        if(st.insert(q).second==false) st.erase(q);

        int sum=0;

        bool sign = false;

        int ans=0;
        rep(j,n){
            if(st.count(n-j))sign=!sign;
            ans+=score[sign][j];

        }
        cout<<ans<<endl;
    }

}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)
//#define INF (1LL<<59)

vector<int> score[2];
int n;

int dfs(vector<int> &v,int depth,int ind){

    if(depth==n)return 0;

    int cnt = 0;
    vector<int> a(v.begin(),v.begin()+v.size()/2);
    vector<int> b(v.begin()+v.size()/2,v.end());

    dfs(a,depth+1,ind);
    dfs(b,depth+1,ind);

    int cl=0,cr=0,p=0;
    int n = a.size();

    while(1){
        if(cl==n && cr==n)break;
//        cout<<cl<<" "<<cr<<endl;
        if(cr==n || cl<n && a[cl]<=b[cr]){
//            cout<<p<<" "<<cl<<endl;;
            v[p]=a[cl];
            p++;
            cl++;
        }else{
            cnt+=n-cl;
            v[p]=b[cr];
            p++;
            cr++;
        }
    }
    score[ind][depth]+=cnt;

    return cnt;
}

int main(){
    cin>>n;
    vector<int> v(1<<n);
    rep(i,1<<n)scanf("%d",&v[i]);

    rep(i,2)score[i].resize(n,0);
    vector<int> tmp = v;
    dfs(tmp,0,0);
    tmp = v;
    reverse(all(tmp));
    dfs(tmp,0,1);

    set<int> st;

    int m;
    cin>>m;
    rep(i,m){
        int q;
        scanf("%d",&q);
        if(st.insert(q).second==false) st.erase(q);

        int sum=0;

        bool sign = false;

        int ans=0;
        rep(j,n){
            if(st.count(n-j))sign=!sign;
            ans+=score[sign][j];

        }
        cout<<ans<<endl;
    }

}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)
//#define INF (1LL<<59)

vector<int> score[2];
int n;

int dfs(vector<int> &v,int depth,int ind){

    if(depth==n)return 0;

    int cnt = 0;
    vector<int> a(v.begin(),v.begin()+v.size()/2);
    vector<int> b(v.begin()+v.size()/2,v.end());

    dfs(a,depth+1,ind);
    dfs(b,depth+1,ind);

    int cl=0,cr=0,p=0;
    int n = a.size();

    while(1){
        if(cl==n && cr==n)break;
//        cout<<cl<<" "<<cr<<endl;
        if(cr==n || cl<n && a[cl]<b[cr]){
//            cout<<p<<" "<<cl<<endl;;
            v[p]=a[cl];
            p++;
            cl++;
        }else{
            cnt+=n-cl;
            v[p]=b[cr];
            p++;
            cr++;
        }
    }
    score[ind][depth]+=cnt;

    return cnt;
}

int main(){
    cin>>n;
    vector<int> v(1<<n);
    rep(i,1<<n)cin>>v[i];

    rep(i,2)score[i].resize(n,0);
    vector<int> tmp = v;
    dfs(tmp,0,0);
    tmp = v;
    reverse(all(tmp));
    dfs(tmp,0,1);

    set<int> st;

    int m;
    cin>>m;
    rep(i,m){
        int q;
        cin>>q;
        if(st.insert(q).second==false) st.erase(q);

        int sum=0;

        bool sign = false;

        int ans=0;
        rep(j,n){
            if(st.count(n-j))sign=!sign;
            ans+=score[sign][j];

        }
        cout<<ans<<endl;
    }

}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)
//#define INF (1LL<<59)

vector<int> score[2];
int n;

int dfs(vector<int> &v,int depth,int ind){

    if(depth==n)return 0;

    int cnt = 0;
    vector<int> a(v.begin(),v.begin()+v.size()/2);
    vector<int> b(v.begin()+v.size()/2,v.end());

    dfs(a,depth+1,ind);
    dfs(b,depth+1,ind);

    int cl=0,cr=0,p=0;
    int n = a.size();

    while(1){
        if(cl==n && cr==n)break;
//        cout<<cl<<" "<<cr<<endl;
        if(cr==n || cl<n && a[cl]<b[cr]){
//            cout<<p<<" "<<cl<<endl;;
            v[p]=a[cl];
            p++;
            cl++;
        }else{
            cnt+=n-cl;
            v[p]=b[cr];
            p++;
            cr++;
        }
    }
    score[ind][depth]+=cnt;

    return cnt;
}

int main(){
    cin>>n;
    vector<int> v(1<<n);
    rep(i,1<<n)cin>>v[i];

    rep(i,2)score[i].resize(n,0);
    vector<int> tmp = v;
    dfs(tmp,0,0);
    tmp = v;
    reverse(all(tmp));
    dfs(tmp,0,1);

    set<int>　st;

    int m;
    cin>>m;
    rep(i,m){
        int q;
        cin>>q;
        if(st.insert(q).second==false) st.erase(q);

        int sum=0;

        bool sign = false;

        int ans=0;
        rep(j,n){
            if(st.count(n-j))sign=!sign;
            ans+=score[sign][j];

        }
        cout<<ans<<endl;
    }

}


/*
	"Why do we fall? So we can learn how to pick ourselves up."
	- Alfred, Batman Begins -
*/

# include <iostream>
# include <cstdio>
# include <algorithm>
# include <cmath>
# include <set>
# include <cstring>
# include <string.h>
# include <sstream>
# include <map>
# include <queue>
# include <vector>

# define mp make_pair
# define LL long long
# define vi vector<int>
# define pqu priority_queue
# define iasc int,vector<int>,greater<int>
# define LLasc  LL,vector<LL>,greater<LL>
# define ins push_back
# define pii pair<int,int>
# define fi first
# define se second

inline void in(int &MAGNUS) {scanf("%d",&MAGNUS);}
inline void out(int MAGNUS) {printf("%d\n",MAGNUS);}
inline void in(int &MAGNUS,int &CLAR) {scanf("%d%d",&MAGNUS,&CLAR);}
inline void out(int MAGNUS,int CLAR) {printf("%d %d\n",MAGNUS,CLAR);}

inline void inl(LL &LIV) {scanf("%lld",&LIV);}
inline void inl(LL &LIV,LL &MART) {scanf("%lld%lld",&LIV,&MART);}
inline void outl(LL LIV) {printf("%lld\n",LIV);}
inline void outl(LL LIV, LL MART) {printf("%lld %lld\n",LIV,MART);}

# define FORU(a,b,c) for(int a=b; a<=c; a++)
# define FORD(a,b,c) for(int a=b; a>=c; a--)
# define FOR(a,b) for(int a=1; a<=b; a++)
# define FORA(a,b) for(auto (a) : (b))
# define REP(a,b) for(int a=0; a<b; a++)
# define RESET(a) memset(a,0,sizeof(a))
# define MEMO(a) memset(a,-1,sizeof(a))
# define ALL(a) (a).begin(),(a).end()
# define RNG(a,b) (a)+1,(a)+(b)+1
# define BSL(a,b) lower_bound(ALL(a),(b))-(a).begin()
# define BSLX(a,b,c) lower_bound((a)+1,(a)+(b)+1,(c))-a
# define BSU(a,b) upper_bound(ALL(a),(b))-(a).begin()
# define BSUX(a,b,c) upper_bound((a)+1,(a)+(b)+1,(c))-a
# define DEBUG puts("this-part-has-been-reach")

using namespace std;

int N,k,a[(1<<20)+5],Q, logN;
int tmp[(1<<20)+5], c[(1<<20)+5];
LL v[(1<<20)+5][2], ans[(1<<20)+5];

LL dnc(int L, int R, int dep, bool which){
	LL res=0;
	if(dep==logN) return 0;

	int M=(L+R)/2;
	res+=dnc(L,M,dep+1,which);
	res+=dnc(M+1,R,dep+1,which);

	int l=L, r=M+1, idx=1;
	while(l<=M || r<=R){
		if(l>M) tmp[idx++]=c[r++];
		else if(r>R) tmp[idx++]=c[l++], res+=r-M-1;
		else if(c[l] <=	 c[r]) tmp[idx++]=c[l++], res+=r-M-1;
		else tmp[idx++]=c[r++];
	}

	idx=1;
	FORU(i,L,R) c[i]=tmp[idx++];
	v[dep][which]+=res;
	return res;
}

void solve(int dep, int idx, bool which, LL val){
	if(dep>logN){
		ans[idx]=val;
		return;
	}
	solve(dep+1, idx, which, val);
	solve(dep+1, idx+(1<<dep), !which, val+v[dep][!which]-v[dep][which]);
}

int main(){

	in(logN);
	N=1<<logN;

	FOR(i,N) in(a[i]);

	FOR(i,N) c[i]=a[i];
	dnc(1,N,0,0);

	FOR(i,N) c[i]=a[N-i+1];
	dnc(1,N,0,1);

	solve(0,0,0,v[0][0]);

	in(Q);
	int state=0;
	FOR(q,Q){
		int x;
		in(x);
		state^=(1<<(logN-x));
		outl(ans[state]);
	}

	return 0;
}


/*
	"Why do we fall? So we can learn how to pick ourselves up."
	- Alfred, Batman Begins -
*/

# include <iostream>
# include <cstdio>
# include <algorithm>
# include <cmath>
# include <set>
# include <cstring>
# include <string.h>
# include <sstream>
# include <map>
# include <queue>
# include <vector>

# define mp make_pair
# define LL long long
# define vi vector<int>
# define pqu priority_queue
# define iasc int,vector<int>,greater<int>
# define LLasc  LL,vector<LL>,greater<LL>
# define ins push_back
# define pii pair<int,int>
# define fi first
# define se second

inline void in(int &MAGNUS) {scanf("%d",&MAGNUS);}
inline void out(int MAGNUS) {printf("%d\n",MAGNUS);}
inline void in(int &MAGNUS,int &CLAR) {scanf("%d%d",&MAGNUS,&CLAR);}
inline void out(int MAGNUS,int CLAR) {printf("%d %d\n",MAGNUS,CLAR);}

inline void inl(LL &LIV) {scanf("%lld",&LIV);}
inline void inl(LL &LIV,LL &MART) {scanf("%lld%lld",&LIV,&MART);}
inline void outl(LL LIV) {printf("%lld\n",LIV);}
inline void outl(LL LIV, LL MART) {printf("%lld %lld\n",LIV,MART);}

# define FORU(a,b,c) for(int a=b; a<=c; a++)
# define FORD(a,b,c) for(int a=b; a>=c; a--)
# define FOR(a,b) for(int a=1; a<=b; a++)
# define FORA(a,b) for(auto (a) : (b))
# define REP(a,b) for(int a=0; a<b; a++)
# define RESET(a) memset(a,0,sizeof(a))
# define MEMO(a) memset(a,-1,sizeof(a))
# define ALL(a) (a).begin(),(a).end()
# define RNG(a,b) (a)+1,(a)+(b)+1
# define BSL(a,b) lower_bound(ALL(a),(b))-(a).begin()
# define BSLX(a,b,c) lower_bound((a)+1,(a)+(b)+1,(c))-a
# define BSU(a,b) upper_bound(ALL(a),(b))-(a).begin()
# define BSUX(a,b,c) upper_bound((a)+1,(a)+(b)+1,(c))-a
# define DEBUG puts("this-part-has-been-reach")

using namespace std;

int N,k,a[(1<<21)+5],Q, logN;
int tmp[(1<<21)+5], c[(1<<21)+5];
LL v[(1<<21)+5][2], ans[(1<<21)+5];

LL dnc(int L, int R, int dep, bool which){
	LL res=0;
	if(dep==logN) return 0;

	int M=(L+R)/2;
	res+=dnc(L,M,dep+1,which);
	res+=dnc(M+1,R,dep+1,which);

	int l=L, r=M+1, idx=L;
	while(l<=M || r<=R){
		if((l>M || c[r]<c[l]) && r<=R) tmp[idx++]=c[r++];
		else{
			tmp[idx++]=c[l++];
			res+=r-M-1;
		}
	}

	FORU(i,L,R) c[i]=tmp[i];
	v[dep][which]+=res;
	return res;
}

void solve(int dep, int idx, bool which, LL val){
	if(dep>logN){
		ans[idx]=val;
		return;
	}
	solve(dep+1, idx, which, val);
	solve(dep+1, idx+(1<<dep), !which, val+v[dep][!which]-v[dep][which]);
}

int main(){

	in(logN);
	N=1<<logN;

	FOR(i,N) in(a[i]);

	FOR(i,N) c[i]=a[i];
	dnc(1,N,0,0);

	FOR(i,N) c[i]=a[N-i+1];
	dnc(1,N,0,1);

	solve(0,0,0,v[0][0]);

	in(Q);
	int state=0;
	FOR(q,Q){
		int x;
		in(x);
		state^=(1<<(logN-x));
		outl(ans[state]);
	}

	return 0;
}


/*
	"Why do we fall? So we can learn how to pick ourselves up."
	- Alfred, Batman Begins -
*/

# include <iostream>
# include <cstdio>
# include <algorithm>
# include <cmath>
# include <set>
# include <cstring>
# include <string.h>
# include <sstream>
# include <map>
# include <queue>
# include <vector>

# define mp make_pair
# define LL long long
# define vi vector<int>
# define pqu priority_queue
# define iasc int,vector<int>,greater<int>
# define LLasc  LL,vector<LL>,greater<LL>
# define ins push_back
# define pii pair<int,int>
# define fi first
# define se second

inline void in(int &MAGNUS) {scanf("%d",&MAGNUS);}
inline void out(int MAGNUS) {printf("%d\n",MAGNUS);}
inline void in(int &MAGNUS,int &CLAR) {scanf("%d%d",&MAGNUS,&CLAR);}
inline void out(int MAGNUS,int CLAR) {printf("%d %d\n",MAGNUS,CLAR);}

inline void inl(LL &LIV) {scanf("%lld",&LIV);}
inline void inl(LL &LIV,LL &MART) {scanf("%lld%lld",&LIV,&MART);}
inline void outl(LL LIV) {printf("%lld\n",LIV);}
inline void outl(LL LIV, LL MART) {printf("%lld %lld\n",LIV,MART);}

# define FORU(a,b,c) for(int a=b; a<=c; a++)
# define FORD(a,b,c) for(int a=b; a>=c; a--)
# define FOR(a,b) for(int a=1; a<=b; a++)
# define FORA(a,b) for(auto (a) : (b))
# define REP(a,b) for(int a=0; a<b; a++)
# define RESET(a) memset(a,0,sizeof(a))
# define MEMO(a) memset(a,-1,sizeof(a))
# define ALL(a) (a).begin(),(a).end()
# define RNG(a,b) (a)+1,(a)+(b)+1
# define BSL(a,b) lower_bound(ALL(a),(b))-(a).begin()
# define BSLX(a,b,c) lower_bound((a)+1,(a)+(b)+1,(c))-a
# define BSU(a,b) upper_bound(ALL(a),(b))-(a).begin()
# define BSUX(a,b,c) upper_bound((a)+1,(a)+(b)+1,(c))-a
# define DEBUG puts("this-part-has-been-reach")

using namespace std;

int logN, N, x[(1<<21)+5], tmp[(1<<21)+5], Q;
LL pwr[(1<<21)+5][2];

void mergesort(int L,int R,int h){

	if(L>=R) return;
	int M=(L+R)/2;
	mergesort(L,M,h-1);
	mergesort(M+1,R,h-1);

	FORU(i,L,M){
		pwr[h][0]+=lower_bound(x+M+1,x+R+1,x[i])-(x+M+1);
		pwr[h][1]+=R-M-(upper_bound(x+M+1,x+R+1,x[i])-(x+M+1));
	}

	int l=L, r=M+1, idx=L;
	while(l<=M || r<=R){
		if(r>R || x[l]<x[r]) tmp[idx++]=x[l++];
		else tmp[idx++]=x[r++];
	}

	FORU(i,L,R) x[i]=tmp[i];
}

int main(){

	in(logN);
	N=1<<logN;
	FOR(i,N) in(x[i]);
	mergesort(1,N,logN);

	in(Q);
	FOR(q,Q){
		int D;
		in(D);
		LL ans=0;
		FOR(dep,D) swap(pwr[dep][0],pwr[dep][1]);
		FOR(dep,logN) ans+=pwr[dep][0];
		outl(ans);
	}

	return 0;
}


/*
	"Why do we fall? So we can learn how to pick ourselves up."
	- Alfred, Batman Begins -
*/

# include <iostream>
# include <cstdio>
# include <algorithm>
# include <cmath>
# include <set>
# include <cstring>
# include <string.h>
# include <sstream>
# include <map>
# include <queue>
# include <vector>

# define mp make_pair
# define LL long long
# define vi vector<int>
# define pqu priority_queue
# define iasc int,vector<int>,greater<int>
# define LLasc  LL,vector<LL>,greater<LL>
# define ins push_back
# define pii pair<int,int>
# define fi first
# define se second

inline void in(int &MAGNUS) {scanf("%d",&MAGNUS);}
inline void out(int MAGNUS) {printf("%d\n",MAGNUS);}
inline void in(int &MAGNUS,int &CLAR) {scanf("%d%d",&MAGNUS,&CLAR);}
inline void out(int MAGNUS,int CLAR) {printf("%d %d\n",MAGNUS,CLAR);}

inline void inl(LL &LIV) {scanf("%lld",&LIV);}
inline void inl(LL &LIV,LL &MART) {scanf("%lld%lld",&LIV,&MART);}
inline void outl(LL LIV) {printf("%lld\n",LIV);}
inline void outl(LL LIV, LL MART) {printf("%lld %lld\n",LIV,MART);}

# define FORU(a,b,c) for(int a=b; a<=c; a++)
# define FORD(a,b,c) for(int a=b; a>=c; a--)
# define FOR(a,b) for(int a=1; a<=b; a++)
# define FORA(a,b) for(auto (a) : (b))
# define REP(a,b) for(int a=0; a<b; a++)
# define RESET(a) memset(a,0,sizeof(a))
# define MEMO(a) memset(a,-1,sizeof(a))
# define ALL(a) (a).begin(),(a).end()
# define RNG(a,b) (a)+1,(a)+(b)+1
# define BSL(a,b) lower_bound(ALL(a),(b))-(a).begin()
# define BSLX(a,b,c) lower_bound((a)+1,(a)+(b)+1,(c))-a
# define BSU(a,b) upper_bound(ALL(a),(b))-(a).begin()
# define BSUX(a,b,c) upper_bound((a)+1,(a)+(b)+1,(c))-a
# define DEBUG puts("this-part-has-been-reach")

using namespace std;

int N,k,a[(1<<20)+5],Q, logN;
int tmp[(1<<20)+5], c[(1<<20)+5];
LL v[(1<<20)+5][2], ans[(1<<20)+5];

LL dnc(int L, int R, int dep, bool which){
	LL res=0;
	if(dep==logN) return 0;

	int M=(L+R)/2;
	res+=dnc(L,M,dep+1,which);
	res+=dnc(M+1,R,dep+1,which);

	int l=L, r=M+1, idx=L;
	while(l<=M || r<=R){
		if((l>M || c[r]<c[l]) && r<=R) tmp[idx++]=c[r++];
		else{
			tmp[idx++]=c[l++];
			res+=r-M-1;
		}
	}

	FORU(i,L,R) c[i]=tmp[i];
	v[dep][which]+=res;
	return res;
}

void solve(int dep, int idx, bool which, LL val){
	if(dep>logN){
		ans[idx]=val;
		return;
	}
	solve(dep+1, idx, which, val);
	solve(dep+1, idx+(1<<dep), !which, val+v[dep][!which]-v[dep][which]);
}

int main(){

	in(logN);
	N=1<<logN;

	FOR(i,N) in(a[i]);

	FOR(i,N) c[i]=a[i];
	dnc(1,N,0,0);

	FOR(i,N) c[i]=a[N-i+1];
	dnc(1,N,0,1);

	solve(0,0,0,v[0][0]);

	in(Q);
	int state=0;
	FOR(q,Q){
		int x;
		in(x);
		state^=(1<<(logN-x));
		outl(ans[state]);
	}

	return 0;
}


/*
	"Why do we fall? So we can learn how to pick ourselves up."
	- Alfred, Batman Begins -
*/

# include <iostream>
# include <cstdio>
# include <algorithm>
# include <cmath>
# include <set>
# include <cstring>
# include <string.h>
# include <sstream>
# include <map>
# include <queue>
# include <vector>

# define mp make_pair
# define LL long long
# define vi vector<int>
# define pqu priority_queue
# define iasc int,vector<int>,greater<int>
# define LLasc  LL,vector<LL>,greater<LL>
# define ins push_back
# define pii pair<int,int>
# define fi first
# define se second

inline void in(int &MAGNUS) {scanf("%d",&MAGNUS);}
inline void out(int MAGNUS) {printf("%d\n",MAGNUS);}
inline void in(int &MAGNUS,int &CLAR) {scanf("%d%d",&MAGNUS,&CLAR);}
inline void out(int MAGNUS,int CLAR) {printf("%d %d\n",MAGNUS,CLAR);}

inline void inl(LL &LIV) {scanf("%lld",&LIV);}
inline void inl(LL &LIV,LL &MART) {scanf("%lld%lld",&LIV,&MART);}
inline void outl(LL LIV) {printf("%lld\n",LIV);}
inline void outl(LL LIV, LL MART) {printf("%lld %lld\n",LIV,MART);}

# define FORU(a,b,c) for(int a=b; a<=c; a++)
# define FORD(a,b,c) for(int a=b; a>=c; a--)
# define FOR(a,b) for(int a=1; a<=b; a++)
# define FORA(a,b) for(auto (a) : (b))
# define REP(a,b) for(int a=0; a<b; a++)
# define RESET(a) memset(a,0,sizeof(a))
# define MEMO(a) memset(a,-1,sizeof(a))
# define ALL(a) (a).begin(),(a).end()
# define RNG(a,b) (a)+1,(a)+(b)+1
# define BSL(a,b) lower_bound(ALL(a),(b))-(a).begin()
# define BSLX(a,b,c) lower_bound((a)+1,(a)+(b)+1,(c))-a
# define BSU(a,b) upper_bound(ALL(a),(b))-(a).begin()
# define BSUX(a,b,c) upper_bound((a)+1,(a)+(b)+1,(c))-a
# define DEBUG puts("this-part-has-been-reach")

using namespace std;

int logN, N, x[(1<<21)+5], tmp[(1<<21)+5], Q;
LL pwr[(1<<21)+5][2];

void mergesort(int L,int R,int h){

	if(L>=R) return;
	int M=(L+R)/2;
	mergesort(L,M,h-1);
	mergesort(M+1,R,h-1);

	FORU(i,L,M){
		pwr[h][0]+=lower_bound(x+M+1,x+R+1,x[i])-(x+M+1);
		pwr[h][1]+=R-M-(upper_bound(x+M+1,x+R+1,x[i])-(x+M+1));
	}

	int l=L, r=M+1, idx=L;
	while(l<=M || r<=R){
		if(r>R || x[l]<=x[r]) tmp[idx++]=x[l++];
		else tmp[idx++]=x[r++];
	}

	FORU(i,L,R) x[i]=tmp[i];
}

int main(){

	in(logN);
	N=1<<logN;
	FOR(i,N) in(x[i]);
	mergesort(1,N,logN);

	in(Q);
	FOR(q,Q){
		int D;
		in(D);
		LL ans=0;
		FOR(dep,D) swap(pwr[dep][0],pwr[dep][1]);
		FOR(dep,logN) ans+=pwr[dep][0];
		outl(ans);
	}

	return 0;
}