A
#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define IO ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef double db;
const int N = 1e5 + 5;
int n, m, _, k;
int main() {
IO;
for (cin >> _; _; --_) {
cin >> n >> k;
if (k >= n) cout << k - n << '
';
else if (k < n) cout << min(n - k, (n + k) & 1) << '
';
}
return 0;
}
B
#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define IO ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef double db;
const int N = 1e5 + 5;
int n, m, _, k;
int x[3], y[3];
int main() {
IO;
for (cin >> _; _; --_) {
rep (i, 0, 2) cin >> x[i];
rep (i, 0, 2) cin >> y[i];
ll ans = 0;
if (x[2] >= y[1]) ans += (y[1] << 1), x[2] -= y[1], y[1] = 0;
else ans += (x[2] << 1), y[1] -= x[2], x[2] = 0;
if (x[2] >= y[2]) x[2] -= y[2], y[2] = 0;
else y[2] -= x[2], x[2] = 0;
if (x[2] >= y[0]) x[2] -= y[0], y[0] = 0;
else y[0] -= x[2], x[2] = 0;
if (x[1] >= y[1]) x[1] -= y[1], y[1] = 0;
else y[1] -= x[1], x[1] = 0;
if (x[1] >= y[0]) x[1] -= y[0], y[0] = 0;
else y[0] -= x[1], x[1] = 0;
ans -= (x[1] << 1);
cout << ans << '
';
}
return 0;
}
C
#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define IO ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef double db;
const int N = 1e5 + 5;
int n, m, _, k;
int a[N], b[N];
int main() {
IO;
for (cin >> _; _; --_) {
cin >> n;
rep (i, 1, n) cin >> a[i], b[i] = a[i];
sort(b + 1, b + 1 + n);
bool f = 1;
rep (i, 1, n) {
if (b[i] == a[i]) continue;
f = (a[i] % b[1] == 0);
if (!f) break;
}
if (f) cout << "YES
";
else cout << "NO
";
}
return 0;
}
D
#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define IO ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef double db;
const int N = 1e6 + 5, mod = 1e9 + 7;
int n, m, _, k;
int h[N], to[N << 1], ne[N << 1], tot;
ll b[N], siz[N];
ll tax[N], cnt;
void add(int u, int v) {
ne[++tot] = h[u]; h[u] = tot; to[tot] = v;
}
void dfs(int u, int fa) {
siz[u] = 1;
for (int i = h[u]; i; i = ne[i]) {
int y = to[i];
if (y == fa) continue;
dfs(y, u);
siz[u] += siz[y];
}
if (fa) tax[++cnt] = siz[u] * (n - siz[u]);
}
int main() {
IO;
for (cin >> _; _; --_) {
cin >> n; tot = cnt = 0;
rep(i, 1, n) h[i] = 0;
rep(i, 2, n) {
int u, v; cin >> u >> v;
add(u, v); add(v, u);
}
dfs(1, 0);
sort(tax + 1, tax + n);
cin >> m;
rep(i, 1, m) cin >> b[i];
sort(b + 1, b + 1 + m);
ll ans = 0;
while (m > n - 1) b[m - 1] = (b[m - 1] * b[m]) % mod, --m;
per (i, n - 1, 1) {
if (m > 0) ans = (ans + tax[i] % mod * b[m] % mod) % mod, --m;
else ans = (ans + tax[i]) % mod;
}
cout << ans << '
';
}
return 0;
}