# A - Distance and Axis

#include<iostream>
using namespace std;
int main()
{
int T; cin>>T;
while(T--){
int a,k;
cin>>a>>k;
if(a<=k) cout<<k-a<<"\n";
else {
if((a%2==0&&k%2==0)||(a%2==1&&k%2==1)) cout<<"0\n";
else cout<<"1\n";
}
}
return 0;
}


# B - Ternary Sequence

#include<iostream>
using namespace std;

int main()
{
int T; cin>>T;
while(T--){
LL a,b,c,x,y,z;
cin>>a>>b>>c>>x>>y>>z;
LL ans = min(c,y)*2;
if(c>y) z -= (c-y);
ans -= max(z-a,0)*2;
cout<<ans<<"\n";
}
return 0;
}


# C - Mere Array *

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5+7;
int a[N],b[N];

int main()
{
int T; cin>>T;
while(T--){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
b[i]=a[i];
}
sort(b,b+n);
int flag=1;
for(int i=0;i<n;i++){
if(a[i]!=b[i]&&__gcd(a[i],b[0])!=b[0]) flag=0;
}
if(flag) cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}


# D - Maximum Distributed Tree*

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 1e9+7;
const int N = 2e5+7;

int h[N],e[N*2],ne[N*2],idx;
LL si[N];

e[idx] = b,ne[idx]=h[a],h[a]=idx++;
}

void dfs(int x,int f){
si[x] = 1;
for(int i=h[x];i!=-1;i=ne[i]){
int j = e[i];
if(j==f) continue;
dfs(j,x);
si[x] += si[j];	 // 每个子树的大小
}
}

int main()
{
int T; cin>>T;
while(T--){
LL n;
cin>>n;
memset(h,-1,sizeof h);
idx=0;
for(int i=1;i<n;i++){
int x,y;
cin>>x>>y;
}
vector<LL> pp,ve;
LL p,x;
cin>>p;
for(int i=0;i<p;i++) cin>>x,pp.push_back(x);
for(int i=p;i<n-1;i++) pp.push_back(1);
dfs(1,0);
for(int i=2;i<=n;i++) ve.push_back(si[i]*(n-si[i]));//每条边的两端的节点的个数的乘积既是经过次数
sort(ve.begin(),ve.end());
sort(pp.begin(),pp.end());
while(pp.size()>n-1){    //删去多于n-1的边
LL x = pp.back();pp.pop_back();
LL y = pp.back();pp.pop_back();
pp.push_back(x*y%mod);
}
sort(pp.begin(),pp.end());  //再次排序
LL ans=0;
for(int i=0;i<n-1;i++) ans = (ans + pp[i]*ve[i])%mod;
cout<<ans<<"\n";
}
return 0;
}


09-17 1372