text
stringlengths 49
983k
|
|---|
#include <bits/stdc++.h>
using namespace std;
const long long int maxN = 1e5 + 326;
long long int pa[maxN], wei[maxN], has[maxN], need[maxN], N, isLeaf[maxN],
stillneed[maxN];
bool isViable[maxN];
vector<long long int> chi[maxN];
void dfs(long long int u = 0) {
for (long long int v : chi[u]) {
dfs(v);
if (!isViable[v]) {
isViable[u] = false;
return;
}
if (stillneed[v]) {
if (stillneed[v] >=
((long long int)1e17 + has[u] - need[u] + wei[v] - 1) / wei[v]) {
isViable[u] = false;
return;
}
has[u] -= stillneed[v] * wei[v];
has[v] += stillneed[v];
}
}
if (has[u] >= need[u]) {
stillneed[u] = 0;
if (pa[u] != u) has[pa[u]] += has[u] - need[u];
has[u] = need[u];
} else {
stillneed[u] = need[u] - has[u];
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
cin >> N;
fill(isViable, isViable + N, true);
fill(isLeaf, isLeaf + N, true);
for (long long int i = 0; i < N; i++) cin >> has[i];
for (long long int i = 0; i < N; i++) cin >> need[i];
for (long long int i = 1; i < N; i++) {
cin >> pa[i] >> wei[i];
pa[i]--;
chi[pa[i]].push_back(i);
isLeaf[pa[i]] = false;
}
dfs();
cout << (has[0] >= need[0] && isViable[0] ? "YES" : "NO") << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> graph[100001];
long long a[100001], b[100001];
long long k[100001];
bool flag = 0;
void dfs(long long src, long long par) {
for (long long i = 0; i < graph[src].size(); i++) {
if (graph[src][i] != par) dfs(graph[src][i], src);
}
if (src == 1) return;
if (b[src] > a[src]) {
long long baki = b[src] - a[src];
b[par] = b[par] + baki;
b[src] = a[src];
} else if (b[src] < a[src]) {
long long lagbe = a[src] - b[src];
if (k[src] > ((1000000000000000000 + b[par]) / lagbe)) {
flag = 1;
return;
}
lagbe = lagbe * k[src];
b[par] = b[par] - lagbe;
b[src] = a[src];
}
}
int main() {
long long n;
scanf("%lld", &n);
for (long long i = 1; i <= n; i++) scanf("%lld", &b[i]);
for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (long long i = 2; i <= n; i++) {
long long x;
scanf("%lld", &x);
graph[i].push_back(x);
graph[x].push_back(i);
scanf("%lld", &x);
k[i] = x;
}
dfs(1, 0);
if (flag)
printf("NO");
else {
if (b[1] >= a[1])
printf("YES");
else
printf("NO");
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
const long long MOD = 1000000007;
const double EBS = 1e-7;
int dX[] = {0, 0, 1, -1};
int dY[] = {1, -1, 0, 0};
int n;
vector<pair<long long, long long> > adj[N];
long long a[N], b[N];
bool f = 1;
bool ok(long long x, long long y) {
x = log10(x);
y = log10(y);
return x + y <= 18;
}
long long dfs(int cur) {
for (auto x : adj[cur]) b[cur] += dfs(x.first);
for (auto x : adj[cur])
if (b[x.first] < a[x.first])
f &= ok(a[x.first] - b[x.first], x.second),
b[cur] -= (a[x.first] - b[x.first]) * x.second,
a[x.first] = b[x.first], f &= (b[cur] <= (long long)1e18);
long long cnt = 0;
if (b[cur] > a[cur]) cnt = b[cur] - a[cur], a[cur] = b[cur];
return cnt;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%lld", &b[i]);
for (int i = 0; i < n; ++i) scanf("%lld", &a[i]);
for (int i = 1; i < n; ++i) {
int x, y;
scanf("%d", &x);
scanf("%d", &y);
adj[--x].push_back({i, y});
}
dfs(0);
if (b[0] >= a[0] && f)
puts("YES");
else
puts("NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long a[1000001];
bool ok = 1;
long long b[1000001];
long long k[1000001];
vector<int> adj[1000000];
long long need[1000000];
void dfs(int v) {
need[v] = b[v] - a[v];
for (auto u : adj[v]) {
dfs(u);
if (need[u] > 0) {
if (100000000000000000LL / need[u] < k[u]) ok = 0;
need[v] += need[u] * k[u];
if (need[v] > 100000000000000000LL) ok = 0;
} else
need[v] += need[u];
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 2; i <= n; i++) {
int p;
cin >> p >> k[i];
adj[p].push_back(i);
}
dfs(1);
if (need[1] <= 0 && ok)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MN = 100000 + 5;
bool Failed;
long long state[MN];
long long need[MN];
struct tEdge {
int dest, next, rate;
};
tEdge E[MN * 2 + 123];
int Link[MN];
int ee = 0;
void addEdge(int u, int v, int rate) {
ee++;
E[ee].dest = v;
E[ee].next = Link[u];
Link[u] = ee;
E[ee].rate = rate;
}
int Parent[MN];
int rate_from_parent[MN];
bool visited[MN];
void Apply(int v);
void DFS(int v, int p) {
visited[v] = true;
Parent[v] = p;
int tmp = Link[v];
while (tmp > 0) {
if (E[tmp].dest != p) {
rate_from_parent[E[tmp].dest] = E[tmp].rate;
DFS(E[tmp].dest, v);
}
tmp = E[tmp].next;
}
Apply(v);
}
void Apply(int v) {
if (Parent[v] == -1) {
if (state[v] < need[v]) Failed = true;
} else {
long double sKa = (need[v] - state[v]);
sKa *= rate_from_parent[v];
if (sKa > 1e18) Failed = true;
if (state[v] < need[v]) {
state[Parent[v]] -= (need[v] - state[v]) * rate_from_parent[v];
if (state[Parent[v]] < -1 * 100000LL * 1000000000000LL) Failed = true;
} else
state[Parent[v]] += (state[v] - need[v]);
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
long long a;
cin >> a;
state[i] = a;
}
for (int i = 1; i <= n; i++) {
long long a;
cin >> a;
need[i] = a;
}
for (int i = 2; i <= n; i++) {
int k, x;
cin >> x >> k;
if (x != i) {
addEdge(i, x, 1);
addEdge(x, i, k);
}
}
for (int i = 1; i <= n; i++)
if (visited[i] == false) DFS(i, -1);
if (!Failed)
cout << "YES";
else
cout << "NO";
cout << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
using INT = long long;
INT b[101000], a[101000];
vector<pair<int, int> > adj[101000];
INT dfs(int u, int rt = 1) {
INT tot = b[u] - a[u];
for (auto p : adj[u]) {
tot += dfs(p.first, p.second);
if (tot < -0x3f3f3f3f3f3f3f3f) {
puts("NO");
exit(0);
}
}
if (tot >= 0) return tot;
if (-tot >= (0x3f3f3f3f3f3f3f3f + rt - 1) / rt) {
puts("NO");
exit(0);
}
return tot * rt;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%I64d", b + i);
}
for (int i = 1; i <= n; i++) {
scanf("%I64d", a + i);
}
for (int i = 2; i <= n; i++) {
int x, k;
scanf("%d %d", &x, &k);
adj[x].push_back({i, k});
}
if (dfs(1) >= 0)
puts("YES");
else
puts("NO");
}
|
#include <bits/stdc++.h>
using namespace std;
int64_t mod = 1e9 + 7;
int64_t mod1 = 1e9 + 5;
int64_t power(int64_t a, int64_t b) {
if (b == 0)
return 1;
else if (b % 2 == 0)
return power(((((a) % mod) * ((a) % mod)) % mod), b / 2) % mod;
else
return (((a) % mod) *
(power(((((a) % mod) * ((a) % mod)) % mod), b / 2) % mod)) %
mod;
}
inline int64_t inverse(int64_t a, int64_t md) {
a %= md;
if (a < 0) a += md;
int64_t b = md, u = 0, v = 1;
while (a) {
int64_t t = b / a;
b -= t * a;
swap(a, b);
u -= t * v;
swap(u, v);
}
assert(b == 1);
if (u < 0) u += md;
return u;
}
const int64_t ce = 1e9 + 7;
int64_t fast_mod(int64_t input) { return input < ce ? input : input % ce; }
int64_t b[100001];
int64_t a[100001];
int64_t cost[100001];
int64_t dp[100001];
vector<int64_t> v[100001];
int64_t ch = 1e17 + 100;
void dfs(int64_t u, int64_t par) {
int64_t sum = b[u];
for (auto i : v[u])
if (i == par)
continue;
else {
dfs(i, u);
int64_t f = dp[i];
int64_t req = a[i] - dp[i];
if (abs(req) > ch) {
cout << "NO";
exit(0);
}
if (req < 0)
sum += abs(req);
else {
if ((sum + ch + cost[i] - 1) / cost[i] <= req) {
cout << "NO";
exit(0);
}
sum -= cost[i] * req;
}
}
dp[u] = sum;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int64_t n;
cin >> n;
for (int64_t i = 1; i <= n; i++) cin >> b[i];
for (int64_t i = 1; i <= n; i++) cin >> a[i];
for (int64_t i = 1; i <= n - 1; i++) {
int64_t u;
cin >> u;
v[u].push_back(i + 1);
cin >> cost[i + 1];
}
dfs(1, 0);
if (dp[1] >= a[1])
cout << "YES";
else
cout << "NO";
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
vector<int> g[N];
long long a[N], b[N], k[N];
bool check(long long x, long long y) {
if (x > 1e18 / y) return false;
return true;
}
long long dfs(int v) {
long long tot = b[v] - a[v];
for (int u : g[v]) {
tot += dfs(u);
if (abs(tot) > 1e18) {
cout << "NO" << '\n';
exit(0);
}
}
if (tot >= 0) return tot;
if (!check(-tot, k[v])) {
cout << "NO" << '\n';
exit(0);
}
tot *= k[v];
return tot;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 2; i <= n; i++) {
int par;
cin >> par >> k[i];
g[par].push_back(i);
}
k[1] = 1;
if (dfs(1) >= 0) {
cout << "YES" << '\n';
} else {
cout << "NO" << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double b[100005], a[100005];
vector<pair<int, long long int>> gr[100005];
void dfs(int u) {
int v;
for (auto vv : gr[u]) {
v = vv.first;
dfs(v);
if (b[v] < a[v])
b[u] -= (a[v] - b[v]) * vv.second;
else
b[u] += b[v] - a[v];
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int i, j, n, u;
double k;
cin >> n;
for (i = 1; i <= n; i++) cin >> b[i];
for (i = 1; i <= n; i++) cin >> a[i];
for (i = 2; i <= n; i++) {
cin >> u >> k;
gr[u].push_back(make_pair(i, k));
}
dfs(1);
cout << ((b[1] >= a[1]) ? "YES" : "NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e17 + 7;
const long long N = 200010;
long long n, a[N], b[N];
long long f[N];
struct Edge {
long long to, nxt;
long long val;
} edge[N << 1];
long long head[N << 1], tot;
void add_edge(long long x, long long y, long long z) {
edge[++tot].to = y;
edge[tot].val = z;
edge[tot].nxt = head[x];
head[x] = tot;
}
void dfs(long long u) {
f[u] = b[u] - a[u];
for (long long i = head[u]; i; i = edge[i].nxt) {
long long v = edge[i].to;
dfs(v);
if (f[v] < 0) {
if (inf / edge[i].val <= -f[v])
f[u] = -inf;
else {
f[u] += f[v] * edge[i].val;
f[u] = max(f[u], -inf);
}
} else
f[u] += f[v];
}
}
signed main() {
scanf("%lld", &n);
for (long long i = 1; i <= n; i++) scanf("%lld", &b[i]);
for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (long long i = 2; i <= n; i++) {
long long x;
long long k;
scanf("%lld %lld", &x, &k);
add_edge(x, i, k);
}
dfs(1);
if (f[1] >= 0)
printf("YES");
else
printf("NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
vector<pair<int, int>> G[N];
long long a[N], b[N];
bool gg;
long long dfs(int x) {
long long res = b[x] - a[x];
for (auto e : G[x]) {
int u = e.first, w = e.second;
long long tmp = dfs(u);
if (gg) return -1;
if (tmp < 0) {
if (1.0 * tmp * w + res < -2e17) {
gg = 1;
return -1;
}
res += tmp * w;
} else {
res += tmp;
}
}
return res;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%I64d", &b[i]);
for (int i = 1; i <= n; ++i) scanf("%I64d", &a[i]);
for (int i = 2; i <= n; ++i) {
int p, w;
scanf("%d%d", &p, &w);
G[p].push_back({i, w});
}
long long ans = dfs(1);
puts(ans >= 0 ? "YES" : "NO");
}
|
#include <bits/stdc++.h>
using namespace std;
inline int iin() {
int x;
scanf("%d", &x);
return x;
}
inline long long int lin() {
long long int x;
scanf("%lld", &x);
return x;
}
const long long int N = 1e6 + 85;
const long long int M = 1e3 + 85;
const long long int mod = 1e9 + 7;
const long long int pinf = LLONG_MAX;
const long long int minf = LLONG_MIN;
long long int arr[N];
vector<pair<long long int, long double> > edge[N];
map<int, bool> mark;
void DFS(int v) {
mark[v] = true;
for (auto e : edge[v])
if (!mark[e.first]) {
DFS(e.first);
if (arr[e.first] >= 0)
arr[v] += arr[e.first];
else {
if (arr[e.first] * 0.1 * e.second + arr[v] < -1e18) {
cout << "NO\n";
exit(0);
}
arr[v] += arr[e.first] * e.second;
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> arr[i];
for (int i = 1; i <= n; i++) {
long long int x;
cin >> x;
arr[i] -= x;
}
for (int i = 2; i <= n; i++) {
long long int x, k;
cin >> x >> k;
edge[x].push_back({i, k});
}
DFS(1);
return cout << (arr[1] >= 0 ? "YES\n" : "NO\n") << endl, 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
const int maxn = 1e5 + 50;
int n, fa[maxn], K[maxn], deep[maxn], id[maxn];
vector<int> G[maxn];
long long a[maxn], b[maxn];
void Dfs(int u, int f = 0) {
for (auto&& v : G[u]) {
if (v == f) continue;
deep[v] = deep[u] + 1;
Dfs(v, u);
}
}
int main(int argc, char* argv[]) {
n = read();
for (int i = 1; i <= n; ++i) scanf("%I64d", b + i);
for (int i = 1; i <= n; ++i) scanf("%I64d", a + i);
for (int i = 2; i <= n; ++i) {
fa[i] = read(), K[i] = read();
G[fa[i]].emplace_back(i);
}
K[1] = 1;
for (int i = 1; i <= n; ++i) id[i] = i;
Dfs(1);
sort(id + 1, id + n + 1,
[](const int& x, const int& y) { return deep[x] > deep[y]; });
for (int i = 1; i <= n;) {
int j = i;
while (j <= n && deep[id[j]] == deep[id[i]]) ++j;
for (int k = i; k < j; ++k) {
int x = id[k];
if (b[x] >= a[x]) b[fa[x]] += (b[x] - a[x]);
}
for (int k = i; k < j; ++k) {
int x = id[k];
if (b[x] < a[x]) {
double z = (double)b[fa[x]] - (double)(a[x] - b[x]) * (double)K[x];
if (z < -3e18) {
puts("NO");
return 0;
}
b[fa[x]] -= (a[x] - b[x]) * K[x];
}
}
i = j;
}
if (b[0] < 0) {
puts("NO");
return 0;
}
puts("YES");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mxn = 1e5 + 5;
long long n, m;
long long a[mxn], b[mxn];
long double c[mxn];
vector<pair<long long, long long> > g[mxn];
long long ord[mxn], cur;
long long fa[mxn], kk[mxn];
inline void dfs(long long x, long long par = 0, long long tk = 0) {
fa[x] = par;
kk[x] = tk;
for (long long i = 0; i < g[x].size(); ++i) {
long long y = g[x][i].first, t = g[x][i].second;
if (y == par) continue;
dfs(y, x, t);
}
ord[++cur] = x;
}
inline void solve() {
cin >> n;
for (long long i = 1; i <= n; ++i) cin >> b[i];
for (long long i = 1; i <= n; ++i) cin >> a[i], c[i] = b[i] - a[i];
for (long long i = 2; i <= n; ++i) {
long long x, d;
cin >> x >> d;
g[x].push_back(make_pair(i, d));
g[i].push_back(make_pair(x, d));
}
dfs(1);
for (long long ti = 1; ti < n; ++ti) {
long long i = ord[ti];
long long p = fa[i];
if (c[i] > 0)
c[p] += c[i];
else
c[p] += c[i] * kk[i];
if (c[p] < -400000000000000000ll) {
cout << "NO\n";
exit(0);
}
}
if (c[1] >= 0)
printf("YES");
else
printf("NO");
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
long long T = 1;
for (; T--;) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100111];
long long a[100111], b[100111];
long long dp[100111];
long long wt[100111];
int n;
void dfs(int v) {
for (auto nv : g[v]) {
dfs(nv);
if (dp[nv] < 0) {
if ((1ll << 61) / wt[nv] <= -dp[nv]) {
dp[nv] = 0;
dp[v] = -(1ll << 61);
} else {
dp[v] += dp[nv] * wt[nv];
dp[nv] = 0;
if (dp[v] < -(1ll << 61)) dp[v] = -(1ll << 61);
}
} else {
dp[v] += dp[nv];
dp[nv] = 0;
}
}
}
int main() {
int i;
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
scanf("%I64d", &b[i]);
}
for (i = 1; i <= n; ++i) {
scanf("%I64d", &a[i]);
}
for (i = 1; i <= n; ++i) {
dp[i] = b[i] - a[i];
}
for (i = 2; i <= n; ++i) {
int v;
long long k;
scanf("%d%I64d", &v, &k);
g[v].push_back(i);
wt[i] = k;
}
dfs(1);
printf("%s\n", dp[1] >= 0 ? "YES" : "NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const long long R = (long long)1e17 + 5;
int n;
long long a[N], b[N], f[N], g[N];
vector<pair<int, int> > adj[N];
long long Round(long long a, long long b) { return (a + b - 1) / b; }
void DFS(int u) {
f[u] = max(0LL, a[u] - b[u]);
g[u] = max(0LL, b[u] - a[u]);
for (int k = 0; k < adj[u].size(); ++k) {
int v = adj[u][k].first, w = adj[u][k].second;
DFS(v);
}
for (int k = 0; k < adj[u].size(); ++k) {
int v = adj[u][k].first, w = adj[u][k].second;
if (f[v] >= Round(R - f[u], w))
f[u] = R;
else
f[u] += f[v] * w;
g[u] += g[v];
}
long long x = f[u], y = g[u];
f[u] = max(0LL, x - y);
g[u] = max(0LL, y - x);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%I64d", &b[i]);
for (int i = 1; i <= n; ++i) scanf("%I64d", &a[i]);
for (int i = 2; i <= n; ++i) {
int p, k;
scanf("%d%d", &p, &k);
adj[p].push_back(pair<int, int>(i, k));
}
DFS(1);
if (f[1] == 0)
puts("YES");
else
puts("NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int N;
long long a[100005], b[100005];
struct Edge {
int v, k;
Edge(int v = 0, int k = 0) : v(v), k(k) {}
};
vector<Edge> adj[100005];
void dfs(int u) {
for (int v, w, k = 0; k < adj[u].size(); k++) {
v = adj[u][k].v;
w = adj[u][k].k;
dfs(v);
if (b[v] >= a[v])
b[u] += b[v] - a[v];
else {
if (((long long)(1e15) + a[u]) / w <= a[v] - b[v])
b[1] = -(long long)(1e15);
else
b[u] -= w * (a[v] - b[v]);
}
}
}
int main() {
cin >> N;
for (int i = 1; i <= N; i++) cin >> b[i];
for (int i = 1; i <= N; i++) cin >> a[i];
for (int u, k, v = 2; v <= N; v++) {
cin >> u >> k;
adj[u].push_back(Edge(v, k));
}
dfs(1);
if (b[1] >= a[1])
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.14159265359;
const long long MOD = (long long)998244353ll;
const long long MAXN = (long long)1e6 + 10;
const long long INF = (long long)2242545357980376863;
const long double EPS = (long double)1e-8;
vector<pair<long long, long long> > G[MAXN];
long long a[MAXN], b[MAXN];
long long MN = 3e18;
long long mul(long long a, long long b) {
if (a > MN / b + 5) return MN;
return a * b;
}
void DFS(long long u) {
long long adj, w;
for (auto E : G[u]) {
adj = E.first;
w = E.second;
DFS(adj);
if (a[adj] < b[adj]) {
long long x = b[adj] - a[adj];
b[adj] -= x;
b[u] += x;
} else if (a[adj] > b[adj]) {
long long x = a[adj] - b[adj];
b[adj] += x;
b[u] -= mul(x, w);
b[u] = max(b[u], -MN);
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 1; i <= n; i++) cin >> a[i];
long long u, w;
for (int i = 2; i <= n; i++) {
cin >> u >> w;
G[u].push_back({i, w});
}
DFS(1);
if (b[1] < a[1])
cout << "NO";
else
cout << "YES";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double ans[100005];
long long x[100005], k[100005];
int main() {
int i, n;
cin >> i;
for (n = 1; n <= i; n++) cin >> ans[n];
for (n = 1; n <= i; n++) {
long long a;
cin >> a;
ans[n] = a - ans[n];
}
for (n = 2; n <= i; n++) cin >> x[n] >> k[n];
for (n = i; n > 1; n--) {
if (ans[n] > 0)
ans[x[n]] += k[n] * ans[n];
else
ans[x[n]] += ans[n];
}
if (ans[1] < 0.5)
cout << "YES";
else
cout << "NO";
cout << endl;
}
|
#include <bits/stdc++.h>
const int N = 1e5 + 5;
using namespace std;
const long long INF = 1e18;
int n;
long long b[N], a[N];
long long k[N];
struct node {
int u, v, nxt;
} edge[N];
int head[N], mcnt;
void add_edge(int u, int v) {
mcnt++;
edge[mcnt].u = u;
edge[mcnt].v = v;
edge[mcnt].nxt = head[u];
head[u] = mcnt;
}
void dfs(int u, int fa) {
for (int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].v;
dfs(v, u);
}
if (fa == -1) return;
if (a[u] < b[u])
b[fa] += b[u] - a[u];
else if (a[u] > b[u]) {
double t = 1.0 * k[u] * (a[u] - b[u]);
if (t > INF) {
puts("NO");
exit(0);
}
b[fa] -= t;
if (b[fa] < -INF) {
puts("NO");
exit(0);
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &b[i]);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (int i = 2; i <= n; i++) {
int fa;
scanf("%d%lld", &fa, &k[i]);
add_edge(fa, i);
}
dfs(1, 0);
puts(a[1] > b[1] ? "NO" : "YES");
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const long long M = 1e9 + 7;
long long avl[N], req[N], x[N], k[N], need[N];
long long power(long long b, long long p) {
long long res = 1;
for (int i = 1; i <= p; i++) res *= b;
return res;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &avl[i]);
for (int i = 1; i <= n; i++) {
scanf("%lld", &req[i]);
need[i] = avl[i] - req[i];
}
for (int i = 2; i <= n; i++) {
scanf("%lld", &x[i]);
scanf("%lld", &k[i]);
}
for (int i = n; i > 0; i--) {
int to = x[i];
if (need[i] == 0) continue;
if (need[i] > 0) {
need[to] += need[i];
} else {
int flag = 0;
for (int j = 1; j < 10; j++) {
long long tk = power(10, j);
if (tk > k[i]) break;
long long tn = power(10, 14 - j);
tn *= (-1);
if (need[i] < tn) {
flag = 1;
break;
}
}
if (flag) {
printf("NO\n");
return 0;
}
need[to] += k[i] * need[i];
}
if (need[to] < -1e15) {
printf("NO\n");
return 0;
}
}
if (need[1] >= 0)
printf("YES\n");
else
printf("NO\n");
}
|
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
double fRand(double fMin, double fMax) {
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
template <class T>
T min(T a, T b, T c) {
return min(a, min(b, c));
}
template <class T>
T max(T a, T b, T c) {
return max(a, max(b, c));
}
bool overflow(long long a, long long b) {
return (a > 1000000000000000007LL / b);
}
int n;
long long a[100005], b[100005], dp[100005];
vector<pair<int, int> > g[100005];
void DFS(int u) {
dp[u] = a[u] - b[u];
for (pair<int, int> p : g[u]) {
int v = p.first, k = p.second;
DFS(v);
if (dp[v] > 0) dp[u] += dp[v];
if (dp[v] < 0) {
if (overflow(-dp[v], k) || dp[u] + dp[v] * k < -1000000000000000007LL) {
puts("NO");
exit(0);
}
dp[u] += dp[v] * k;
}
}
}
int main() {
scanf("%d", &n);
for (int i = (1); i <= (n); ++i) scanf("%lld", &a[i]);
for (int i = (1); i <= (n); ++i) scanf("%lld", &b[i]);
for (int i = (2); i <= (n); ++i) {
int x, k;
scanf("%d%d", &x, &k);
g[x].push_back(make_pair(i, k));
}
DFS(1);
puts((dp[1] >= 0) ? "YES" : "NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
vector<long long> have, need;
vector<long long> p, k;
vector<vector<long long> > ch;
void fail() {
cout << "NO" << endl;
exit(0);
}
long long dfs(long long i) {
long long total = have[i] - need[i];
for (long long j : ch[i]) {
long long cur = dfs(j);
if (cur < 0) {
if (abs(cur * 1.0 * k[j]) > 2e17) fail();
cur *= k[j];
}
total += cur;
if (abs(total) > 2e17) fail();
}
return total;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
have.resize(n), need.resize(n);
for (long long i = (0); i < (n); i++) cin >> have[i];
for (long long i = (0); i < (n); i++) cin >> need[i];
p.resize(n), k.resize(n);
ch.resize(n);
for (long long i = (1); i < (n); i++) {
cin >> p[i] >> k[i];
p[i]--;
ch[p[i]].push_back(i);
}
cout << (dfs(0) >= 0 ? "YES" : "NO") << endl;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("-O2")
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << "\t";
err(++it, args...);
}
template <typename T1, typename T2>
ostream& operator<<(ostream& c, pair<T1, T2>& v) {
c << "(" << v.first << "," << v.second << ")";
return c;
}
template <template <class...> class TT, class... T>
ostream& operator<<(ostream& out, TT<T...>& c) {
out << "{ ";
for (auto& x : c) out << x << " ";
out << "}";
return out;
}
const int LIM = 1e5 + 5, MOD = 1e9 + 7;
int t, n, m, k, x, y, w;
long long a[LIM], b[LIM];
vector<pair<int, int> > v[LIM];
long long dfs(int u, long long e) {
for (auto& it : v[u]) {
b[u] += dfs(it.first, it.second);
if (b[u] < -1e18) {
cout << "NO\n";
exit(0);
}
}
if (b[u] < a[u]) {
if ((a[u] - b[u]) > 1e18 / e) {
cout << "NO\n";
exit(0);
}
return (b[u] - a[u]) * e;
} else
return (b[u] - a[u]);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) cin >> b[i];
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 0; i < n - 1; ++i) {
cin >> x >> y;
--x;
v[x].push_back({i + 1, y});
}
long long res = dfs(0, 1);
if (res < 0)
cout << "NO\n";
else
cout << "YES\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline long long getint() {
long long c, x;
for (; !isdigit(c = getchar());)
;
for (x = c - '0'; isdigit(c = getchar()); x = x * 10 + c - '0')
;
return x;
}
void fail() {
puts("NO");
exit(0);
}
const long double inf = 1e18;
const long long Inf = 200000000000000000ll;
const int N = 300010;
int G[N], ne[N], to[N], da[N], n, xb;
long long a[N], b[N];
bool tst(long long x, long long y) { return fabsl((long double)x * y) > inf; }
long long dfs(int x) {
long long ans = b[x] - a[x];
for (int i = G[x]; ~i; i = ne[i]) {
long long y = dfs(to[i]);
if (y > 0)
ans += y;
else {
if (tst(y, da[i])) fail();
ans = ans + y * da[i];
if (ans < -Inf) fail();
}
}
return ans;
}
void add(int z, int y, int x) {
ne[xb] = G[x];
to[xb] = y;
da[xb] = z;
G[x] = xb++;
}
int main() {
n = getint();
memset(G, -1, sizeof G);
for (int i = 1; i <= n; i++) b[i] = getint();
for (int i = 1; i <= n; i++) a[i] = getint();
for (int i = 2; i <= n; i++) add(getint(), i, getint());
if (dfs(1) >= 0)
puts("YES");
else
puts("NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
int v = 0, f = 1;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
f = -1;
else
v = (c & 15);
while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
x = v * f;
}
inline void read(long long &x) {
long long v = 0ll, f = 1ll;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
f = -1;
else
v = (c & 15);
while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
x = v * f;
}
inline void readc(char &x) {
char c;
while ((c = getchar()) == ' ')
;
x = c;
}
inline void writes(string s) { puts(s.c_str()); }
inline void writeln() { writes(""); }
inline void writei(int x) {
if (x < 0) {
putchar('-');
x = abs(x);
}
if (!x) putchar('0');
char a[25];
int top = 0;
while (x) {
a[++top] = (x % 10) + '0';
x /= 10;
}
while (top) {
putchar(a[top]);
top--;
}
}
inline void writell(long long x) {
if (x < 0) {
putchar('-');
x = abs(x);
}
if (!x) putchar('0');
char a[25];
int top = 0;
while (x) {
a[++top] = (x % 10) + '0';
x /= 10;
}
while (top) {
putchar(a[top]);
top--;
}
}
long long n, m, i, j, b[100005], a[100005], f[100005], x, y;
vector<pair<long long, long long> > e[100005];
long long mul(long long x, long long y) {
x = abs(x);
long long t = 0x1bbbbbbbbbbbbbbbll / y;
if (t < x) return -0x1bbbbbbbbbbbbbbbll;
return -min(0x1bbbbbbbbbbbbbbbll, x * y);
}
long long add(long long x, long long y) {
return max(-0x1bbbbbbbbbbbbbbbll, x + y);
}
void dfs(long long x) {
f[x] = b[x] - a[x];
for (__typeof((e[x]).begin()) it = (e[x]).begin(); it != (e[x]).end(); it++) {
dfs(it->first);
if (f[it->first] < 0) {
f[it->first] = mul(f[it->first], it->second);
}
f[x] = add(f[x], f[it->first]);
}
}
int main() {
read(n);
for (((i)) = (1); ((i)) <= ((n)); ((i))++) {
read(b[i]);
}
for (((i)) = (1); ((i)) <= ((n)); ((i))++) {
read(a[i]);
}
for ((i) = (2); (i) <= (n); (i)++) {
read(x);
read(y);
e[x].push_back(make_pair(i, y));
}
dfs(1);
if (f[1] >= 0) {
cout << "YES";
return 0;
}
{
cout << "NO";
return 0;
}
return 0;
}
|
#include <bits/stdc++.h>
const int inf = 0x3f3f3f3f, Inf = 0x7fffffff;
const long long INF = 0x7ffffffffffffff;
template <typename _Tp>
_Tp gcd(const _Tp &a, const _Tp &b) {
return (!b) ? a : gcd(b, a % b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) _Tp abs(const _Tp &a) {
return a > 0 ? a : -a;
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) _Tp max(const _Tp &a, const _Tp &b) {
return a < b ? b : a;
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) _Tp min(const _Tp &a, const _Tp &b) {
return a < b ? a : b;
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void chmax(_Tp &a, const _Tp &b) {
(a < b) && (a = b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void chmin(_Tp &a, const _Tp &b) {
(a > b) && (a = b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void read(_Tp &x) {
register char ch(getchar());
bool f(false);
while (ch < 48 || ch > 57) f |= ch == 45, ch = getchar();
x = ch & 15, ch = getchar();
while (ch >= 48 && ch <= 57)
x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar();
if (f) x = -x;
}
template <typename _Tp, typename... Args>
__inline__ __attribute__((always_inline)) void read(_Tp &t, Args &...args) {
read(t);
read(args...);
}
__inline__ __attribute__((always_inline)) int read_str(char *s) {
register char ch(getchar());
while (ch < 65 || ch > 122 || (ch > 90 && ch < 97)) ch = getchar();
register char *tar = s;
*tar = ch, ch = getchar();
while ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122))
*(++tar) = ch, ch = getchar();
return tar - s + 1;
}
const int N = 100005;
struct edge {
int v, nxt;
long long w;
} c[N];
int front[N], cnt;
__inline__ __attribute__((always_inline)) void add(const int &u, const int &v,
const long long &w) {
c[++cnt] = (edge){v, front[u], w}, front[u] = cnt;
}
long long a[N], b[N];
double f[N];
void dfs(int x) {
f[x] = b[x] - a[x];
for (int i = front[x]; i; i = c[i].nxt) {
dfs(c[i].v);
if (f[c[i].v] < 0) {
f[x] += f[c[i].v] * (double)c[i].w;
} else {
f[x] += f[c[i].v];
}
}
}
int main() {
int n;
read(n);
for (int i = 1; i <= n; ++i) read(b[i]);
for (int i = 1; i <= n; ++i) read(a[i]);
int x;
long long y;
for (int i = 2; i <= n; ++i) {
read(x, y);
add(x, i, y);
}
dfs(1);
if (f[1] >= 0.0) {
printf("YES\n");
} else {
printf("NO\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct edge {
long long to, cost;
edge() {}
edge(long long a, long long b) { to = a, cost = b; }
};
long long n;
long long a[100005], b[100005];
vector<edge> G[100005];
bool used[100005];
bool dfs(int v, long long s) {
used[v] = true;
s += b[v] - a[v];
bool ret = false;
if (G[v][0].cost >= 2) ret = true;
if (used[G[v][0].to]) return s >= 0;
return ret || dfs(G[v][0].to, s);
}
long long calc(long long v) {
used[v] = true;
long long ret = b[v] - a[v];
for (auto e : G[v]) {
long long res = calc(e.to);
if (res >= 0)
ret += res;
else {
if (abs(res) > 1e18 / e.cost) {
cout << "NO" << endl;
exit(0);
}
ret += res * e.cost;
}
if (ret < -2e17) {
cout << "NO" << endl;
exit(0);
}
}
return ret;
}
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (long long(i) = (1); (i) <= (n); (i)++) cin >> b[i];
for (long long(i) = (1); (i) <= (n); (i)++) cin >> a[i];
long long x, k;
for (long long(i) = (2); (i) <= (n); (i)++) {
cin >> x >> k;
G[x].push_back(edge(i, k));
}
if (calc(1) < 0) {
cout << "NO" << endl;
return 0;
}
for (long long(i) = (1); (i) <= (n); (i)++) {
if (used[i]) continue;
if (!dfs(i, 0)) {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 100 * 1000 + 10;
const long long MIN = 1000ll * 1000 * 1000 * 1000 + 100;
struct arc {
long long fin, poids;
};
vector<arc> adj[MAXN];
long long val[MAXN];
bool vu[MAXN];
void faire(long long noeud) {
vu[noeud] = true;
for (auto arrete : adj[noeud]) {
faire(arrete.fin);
if (val[arrete.fin] > 0) {
val[noeud] += val[arrete.fin];
} else {
val[noeud] += val[arrete.fin] * arrete.poids;
if (val[noeud] <= -MIN) {
cout << "NO" << endl;
exit(0);
}
}
}
}
int32_t main() {
long long N;
cin >> N;
long long debut[N];
long long fin[N];
for (long long i = 0; i < N; i++) {
cin >> debut[i];
}
for (long long i = 0; i < N; i++) {
cin >> fin[i];
val[i] = debut[i] - fin[i];
}
for (long long i = 1; i < N; i++) {
long long a, b;
cin >> a >> b;
a--;
adj[a].push_back({i, b});
}
for (long long i = 0; i < N; i++) {
if (!vu[i]) {
faire(i);
if (val[i] < 0) {
cout << "NO" << endl;
return 0;
}
}
}
cout << "YES" << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
long long a[120000];
long long b[120000];
vector<pair<int, long long> > eds[120000];
int n;
long long mul(long long a, long long b) {
if (a == 0) return 0;
if (INF / a < b) return INF;
return a * b;
}
long long dfs1(int v) {
long long hv = b[v];
long long nd = a[v];
for (auto e : eds[v]) {
long long x = dfs1(e.first);
if (x < 0) {
nd = min(INF, nd + mul(-x, e.second));
} else {
hv += x;
}
}
return hv - nd;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%lld", b + i);
for (int i = 0; i < n; ++i) scanf("%lld", a + i);
for (int i = 1; i < n; ++i) {
int x;
long long k;
scanf("%d%lld", &x, &k);
--x;
eds[x].push_back(make_pair(i, k));
}
long long ans = dfs1(0);
if (ans < 0)
cout << "NO\n";
else
cout << "YES\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MX = 1e5 + 1;
double a[MX];
long long p[MX], k[MX];
int main() {
long long n, in;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> in, a[i] -= in;
for (int i = 2; i <= n; i++) cin >> p[i] >> k[i];
for (int i = n; i >= 2; i--)
a[i] < 0 ? a[p[i]] += a[i] * k[i] : a[p[i]] += a[i];
cout << (a[1] < 0 ? "NO" : "YES");
}
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
long long n, a[100005], b[100005], fa[100005], k[100005];
vector<long long> edge[100005];
long long f[100005];
void dfs(long long x) {
f[x] = a[x];
for (long long i = 0; i < edge[x].size(); ++i) {
long long v = edge[x][i];
dfs(v);
if (f[v] < 0)
f[x] += max(1.0 * f[v] * k[v], -1e18);
else
f[x] += f[v];
f[x] = max(f[x], -inf);
}
}
signed main() {
ios::sync_with_stdio(0);
cin >> n;
for (long long i = 1; i <= n; ++i) cin >> b[i];
for (long long i = 1; i <= n; ++i) {
cin >> a[i];
a[i] = b[i] - a[i];
}
for (long long i = 2; i <= n; ++i) {
cin >> fa[i] >> k[i];
edge[fa[i]].push_back(i);
}
dfs(1);
puts(f[1] >= 0 ? "YES" : "NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long maxn = 1e5 + 10;
const long long mod = 1e9 + 7;
const long double PI = acos((long double)-1);
long long pw(long long a, long long b, long long md = mod) {
long long res = 1;
while (b) {
if (b & 1) {
res = (a * res) % md;
}
a = (a * a) % md;
b >>= 1;
}
return (res);
}
long long n;
long long boz = 0;
struct node {
long long par;
vector<long long> ch;
long long zarib;
long long val, target;
} m[maxn];
void dfs(long long v) {
for (auto u : m[v].ch) dfs(u);
m[v].val -= m[v].target;
if (v == 1) return;
if (m[v].val >= 0) {
m[m[v].par].val += m[v].val;
return;
}
if (1e17 / m[v].zarib < abs(m[v].val)) boz = 1;
m[m[v].par].val += m[v].zarib * m[v].val;
if (m[m[v].par].val < -1e17) boz = 1;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (long long i = 1; i <= n; i++) cin >> m[i].val;
for (long long i = 1; i <= n; i++) cin >> m[i].target;
for (long long i = 2; i <= n; i++) {
cin >> m[i].par;
cin >> m[i].zarib;
m[m[i].par].ch.push_back(i);
}
dfs(1);
if (boz) return (cout << "NO", 0);
;
cout << ((m[1].val >= 0) ? "YES" : "NO");
return (0);
}
|
#include <bits/stdc++.h>
static const long long INF = 1LL << 61;
static const int MAXN = 1e5 + 4;
int n;
long long a[MAXN], b[MAXN];
std::vector<std::pair<int, int>> e[MAXN];
long long surplus[MAXN];
void dfs(int u) {
surplus[u] += b[u] - a[u];
for (auto p : e[u]) {
dfs(p.first);
if (surplus[p.first] >= 0) {
surplus[u] += surplus[p.first];
} else {
if (-surplus[p.first] >= (INF - surplus[u]) / p.second)
surplus[u] = -INF;
else
surplus[u] -= -surplus[p.first] * p.second;
}
if (surplus[u] == -INF) break;
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%I64d", &b[i]);
for (int i = 0; i < n; ++i) scanf("%I64d", &a[i]);
for (int i = 1, x, k; i < n; ++i) {
scanf("%d%d", &x, &k);
--x;
e[x].push_back({i, k});
}
dfs(0);
puts(surplus[0] >= 0 ? "YES" : "NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long INF = 1e18;
vector<vector<long long> > data;
vector<vector<long long> > money;
long long n, ai, bi;
vector<long long> a, b;
bool res = true;
long long dfs(long long vertex, long long ed) {
long long balance = b[vertex] - a[vertex];
for (long long i = 0; i < data[vertex].size(); i++) {
long long to = data[vertex][i];
balance -= dfs(to, money[vertex][i]);
if (balance <= -INF) {
res = false;
}
if (!res) {
return 0;
}
}
if (balance >= 0) {
return -balance;
}
if (vertex == 0) {
res = false;
return 0;
}
long long max_wanted = INF / ed + 2;
long long wanted = -balance;
if (wanted > max_wanted) {
res = false;
return 0;
}
return (-balance * ed);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> bi;
b.push_back(bi);
}
for (long long i = 0; i < n; i++) {
cin >> ai;
a.push_back(ai);
}
for (long long i = 0; i < n; i++) {
vector<long long> h1, h2;
data.push_back(h1);
money.push_back(h2);
}
for (long long i = 0; i < n - 1; i++) {
cin >> ai >> bi;
data[ai - 1].push_back(i + 1);
money[ai - 1].push_back(bi);
}
dfs(0, -1);
if (res)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline int iin() {
int x;
scanf("%d", &x);
return x;
}
inline long long int lin() {
long long int x;
scanf("%lld", &x);
return x;
}
const long long int N = 1e6 + 85;
const long long int M = 1e3 + 85;
const long long int mod = 1e9 + 7;
const long long int pinf = LLONG_MAX;
const long long int minf = LLONG_MIN;
long long int arr[1000 * 100 + 10];
vector<pair<int, long double> > edge[1000 * 100 + 10];
bool mark[1000 * 100 + 10];
void DFS(int v) {
mark[v] = true;
for (auto e : edge[v])
if (!mark[e.first]) {
DFS(e.first);
if (arr[e.first] >= 0)
arr[v] += arr[e.first];
else {
if (arr[e.first] * 0.1 * e.second + arr[v] < -1e18) {
cout << "NO\n";
exit(0);
}
arr[v] += arr[e.first] * e.second;
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> arr[i];
for (int i = 1; i <= n; i++) {
long long int x;
cin >> x;
arr[i] -= x;
}
for (int i = 2; i <= n; i++) {
long long int x, k;
cin >> x >> k;
edge[x].push_back({i, k});
}
DFS(1);
return cout << (arr[1] >= 0 ? "YES\n" : "NO\n") << endl, 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<pair<long double, long double> > adj[1000005];
static long long a[1000005], b[1000005];
pair<long double, long double> dfs(int cur) {
pair<long double, long double> now = make_pair(0ll, 0ll);
if (a[cur] > b[cur]) {
now.first += (a[cur] - b[cur]);
} else {
now.second += (b[cur] - a[cur]);
}
for (int i = 0; i < adj[cur].size(); i++) {
pair<long double, long double> tmp = dfs(adj[cur][i].first);
tmp.second *= (adj[cur][i].second);
now.first += tmp.first;
now.second += tmp.second;
assert(tmp.first == 0ll || tmp.second == 0ll);
}
long double x = min(now.first, now.second);
now.second -= x;
now.first -= x;
return now;
}
int main() {
if (0) {
freopen("C:/Users/gold/Desktop/sublime IO/input.txt", "r", stdin);
freopen("C:/Users/gold/Desktop/sublime IO/output.txt", "w", stdout);
}
int T = 1;
while (T--) {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
for (int i = 1; i <= n; i++) {
scanf("%lld", &b[i]);
}
for (int i = 0; i < n - 1; i++) {
long long x, k;
scanf("%lld %lld", &x, &k);
adj[x].push_back(make_pair(i + 2.0, k * 1.0));
}
pair<long double, long double> ans = dfs(1);
if (ans.second == 0ll) {
puts("YES");
} else {
puts("NO");
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long lim = 1e18;
int n;
long long a[100100], b[100100];
vector<pair<int, int> > v[100100];
long long ksc(long long x, int y) {
long long z = 0;
for (; y; y >>= 1, x *= 2) {
if (x < -lim) {
puts("NO");
exit(0);
}
if (y & 1) {
z += x;
if (z < -lim) {
puts("NO");
exit(0);
}
}
}
return z;
}
long long dfs(int x) {
long long ret = 0;
for (auto y : v[x]) {
long long tmp = dfs(y.first);
if (tmp >= 0)
ret += tmp;
else {
tmp = ksc(tmp, y.second);
ret += tmp;
if (ret < -lim) {
puts("NO");
exit(0);
}
}
}
ret += b[x] - a[x];
if (ret < -lim) {
puts("NO");
exit(0);
}
return ret;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &b[i]);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (int i = 2, x, y; i <= n; i++)
scanf("%d%d", &x, &y), v[x].push_back(make_pair(i, y));
long long tmp = dfs(1);
puts(tmp >= 0 ? "YES" : "NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long n1, long long n2) {
if (!n1) return n2;
if (!n2) return n1;
if (n1 % n2 == 0) return n2;
return gcd(n2, n1 % n2);
}
long long powmod(long long base, long long exponent) {
base %= 1000000007;
long long ans = 1;
while (exponent) {
if (exponent & 1) ans = (ans * base) % 1000000007;
base = (base * base) % 1000000007;
exponent /= 2;
}
ans %= 1000000007;
return ans;
}
long long b[200100 + 1];
long long a[200100 + 1];
long long arr[200100 + 1];
long long dp[200100 + 1];
vector<pair<int, int> > child[200100 + 1];
int x[200100 + 1];
int k[200100 + 1];
bool f = true;
void dfs(int node) {
if (!f) return;
for (int i = 0; i < child[node].size(); i++) {
dfs(child[node][i].first);
if (dp[child[node][i].first] >= 0) {
dp[node] += dp[child[node][i].first];
} else {
long long temp = (1000000000000000100 / child[node][i].second);
if (temp + dp[child[node][i].first] < 0) f = false;
dp[node] += dp[child[node][i].first] * child[node][i].second;
if (dp[node] + 1000000000000000100 < 0) f = false;
}
}
}
int main() {
int n, i, j;
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%lld", &b[i]);
for (i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
arr[i] = b[i] - a[i];
dp[i] = arr[i];
}
for (i = 2; i <= n; i++) {
scanf("%d", &x[i]);
scanf("%d", &k[i]);
child[x[i]].push_back(make_pair(i, k[i]));
}
dfs(1);
f &= (dp[1] >= 0);
if (f)
printf("YES\n");
else
printf("NO\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct node {
long long to, next, w;
} e[100010];
long long n, cnt = 0, x, y, a[100010], head[100010];
double tmp;
void add(long long i) {
e[++cnt] = {i, head[x], y};
head[x] = cnt;
}
void dfs(long long u, long long f, long long k) {
for (long long i = head[u]; i; i = e[i].next) dfs(e[i].to, u, e[i].w);
if (a[u] >= 0)
a[f] += a[u];
else {
tmp = double(k) * double(a[u]);
if (!f || tmp < -1000000000007) {
puts("NO");
exit(0);
}
a[f] += k * a[u];
if (a[f] < -1000000000007) {
puts("NO");
exit(0);
}
}
}
int main() {
cin >> n;
for (long long i = 1; i <= n; i++) cin >> a[i];
for (long long i = 1; i <= n; i++) {
cin >> x;
a[i] -= x;
}
for (long long i = 2; i <= n; i++) {
cin >> x >> y;
add(i);
}
dfs(1, 0, 0);
puts("YES");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void ckmax(T& x, T y) {
x = (y > x ? y : x);
}
template <typename T>
inline void ckmin(T& x, T y) {
x = (y < x ? y : x);
}
namespace Fread {
const int SIZE = 1 << 21;
char buf[SIZE], *S, *T;
inline char getchar() {
if (S == T) {
T = (S = buf) + fread(buf, 1, SIZE, stdin);
if (S == T) return '\n';
}
return *S++;
}
} // namespace Fread
namespace Fwrite {
const int SIZE = 1 << 21;
char buf[SIZE], *S = buf, *T = buf + SIZE;
inline void flush() {
fwrite(buf, 1, S - buf, stdout);
S = buf;
}
inline void putchar(char c) {
*S++ = c;
if (S == T) flush();
}
struct NTR {
~NTR() { flush(); }
} ztr;
} // namespace Fwrite
namespace Fastio {
struct Reader {
template <typename T>
Reader& operator>>(T& x) {
char c = Fread ::getchar();
T f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = Fread ::getchar();
}
x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + (c - '0');
c = Fread ::getchar();
}
x *= f;
return *this;
}
Reader& operator>>(char& c) {
c = Fread ::getchar();
while (c == '\n' || c == ' ') c = Fread ::getchar();
return *this;
}
Reader& operator>>(char* str) {
int len = 0;
char c = Fread ::getchar();
while (c == '\n' || c == ' ') c = Fread ::getchar();
while (c != '\n' && c != ' ') {
str[len++] = c;
c = Fread ::getchar();
}
str[len] = '\0';
return *this;
}
Reader() {}
} cin;
const char endl = '\n';
struct Writer {
template <typename T>
Writer& operator<<(T x) {
if (x == 0) {
Fwrite ::putchar('0');
return *this;
}
if (x < 0) {
Fwrite ::putchar('-');
x = -x;
}
static int sta[45];
int top = 0;
while (x) {
sta[++top] = x % 10;
x /= 10;
}
while (top) {
Fwrite ::putchar(sta[top] + '0');
--top;
}
return *this;
}
Writer& operator<<(char c) {
Fwrite ::putchar(c);
return *this;
}
Writer& operator<<(char* str) {
int cur = 0;
while (str[cur]) Fwrite ::putchar(str[cur++]);
return *this;
}
Writer& operator<<(const char* str) {
int cur = 0;
while (str[cur]) Fwrite ::putchar(str[cur++]);
return *this;
}
Writer() {}
} cout;
} // namespace Fastio
const int MAXN = 1e5;
int n, x[MAXN + 5];
long double a[MAXN + 5], b[MAXN + 5], k[MAXN + 5];
int main() {
Fastio ::cin >> n;
for (int i = 1; i <= n; ++i) {
Fastio ::cin >> b[i];
}
for (int i = 1; i <= n; ++i) {
Fastio ::cin >> a[i];
}
for (int i = 2; i <= n; ++i) {
Fastio ::cin >> x[i] >> k[i];
}
for (int i = n; i >= 2; --i) {
if (a[i] < b[i]) {
b[x[i]] += b[i] - a[i];
} else if (a[i] > b[i]) {
a[x[i]] += (a[i] - b[i]) * k[i];
}
}
Fastio ::cout << ((a[1] <= b[1]) ? "YES" : "NO") << Fastio ::endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<pair<unsigned long long, unsigned long long>> graph[100110];
unsigned long long n, a[100100], b[100100], have[100100], need[100100];
bool visted[100100];
pair<long long, long long> dfs(int v, int push) {
visted[v] = true;
long long h = have[v], N = need[v], cary = 1;
have[v] = 0;
for (auto u : graph[v]) {
if (!visted[u.first]) {
pair<long long, long long> curr = dfs(u.first, v);
h += curr.first;
N += curr.second * u.second;
}
if (u.first == push) cary = u.second;
}
if (N >= h) {
N -= h;
h = 0;
} else {
h -= N;
N = 0;
}
return {h / cary, N};
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++) cin >> b[i + 1];
for (int i = 0; i < n; i++) cin >> a[i + 1];
for (int i = 0; i < n; i++) {
have[i + 1] = max((unsigned long long)0, b[i + 1] - a[i + 1]);
need[i + 1] = max((unsigned long long)0, a[i + 1] - b[i + 1]);
if (i >= 1) {
long long first, k;
cin >> first >> k;
graph[i + 1].push_back({first, 1});
graph[first].push_back({i + 1, k});
}
}
for (int i = n; i >= 1; i--) {
if (!visted[i]) {
if (dfs(i, 0).second != 0) return cout << "NO", 0;
}
}
cout << "YES";
}
|
#include <bits/stdc++.h>
struct Edge {
long long to, dis, next;
} edge[100100];
long long n, num_edge;
long long first1[100100];
long long a[100100], b[100100];
double tem;
void dfs(long long x, long long fa, long long p) {
long long k = first1[x];
while (k != 0) {
dfs(edge[k].to, x, edge[k].dis);
k = edge[k].next;
}
if (a[x] < b[x])
b[fa] += b[x] - a[x];
else if (a[x] > b[x]) {
tem = (double)(b[x] - a[x]) * p;
if (tem < -110000000000000000) {
printf("NO");
exit(0);
}
b[fa] -= p * (a[x] - b[x]);
if (b[fa] < -110000000000000000) {
printf("NO");
exit(0);
}
}
}
int main() {
long long i, x, k;
scanf("%lld", &n);
for (i = 1; i <= n; i++) scanf("%lld", &b[i]);
for (i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (i = 2; i <= n; i++) {
scanf("%lld%lld", &x, &k);
edge[++num_edge].to = i;
edge[num_edge].dis = k;
edge[num_edge].next = first1[x];
first1[x] = num_edge;
}
k = first1[1];
while (k != 0) {
dfs(edge[k].to, 1, edge[k].dis);
k = edge[k].next;
}
if (b[1] < a[1])
printf("NO");
else
printf("YES");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 100005;
long long int a[SIZE];
long long int b[SIZE];
long long int k[SIZE];
int parent[SIZE];
vector<int> adj[SIZE];
bool t = true;
double left(int here) {
double ret = (double)b[here] - a[here];
for (int i = 0; i < adj[here].size(); i++) {
int next = adj[here][i];
if (next == parent[here]) continue;
double tmp = left(next);
if (tmp > 0) ret += tmp;
if (tmp < 0) {
ret += k[next] * tmp;
}
}
if (ret < -1e17) t = false;
return ret;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &b[i]);
for (int j = 1; j <= n; j++) scanf("%lld", &a[j]);
for (int i = 2; i <= n; i++) {
scanf("%d %lld", &parent[i], &k[i]);
adj[i].push_back(parent[i]);
adj[parent[i]].push_back(i);
}
if (t && left(1) >= 0)
printf("YES\n");
else
printf("NO\n");
}
|
#include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Wunused-result"
using namespace std;
template <class T>
void _R(T &x) {
cin >> x;
}
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%" PRId64, &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template <class T, class... U>
void R(T &head, U &...tail) {
_R(head);
R(tail...);
}
template <class T>
void _W(const T &x) {
cout << x;
}
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%" PRId64, x); }
void _W(const double &x) { printf("%.16f\n", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T>
void _W(const vector<T> &x) {
for (auto i = x.begin(); i != x.end(); _W(*i++))
if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &...tail) {
_W(head);
putchar(sizeof...(tail) ? ' ' : '\n');
W(tail...);
}
template <class T>
inline bool chkmax(T &a, const T &b) {
return b > a ? a = b, true : false;
}
template <class T>
inline bool chkmin(T &a, const T &b) {
return b < a ? a = b, true : false;
}
template <class T, class F = less<T>>
void sort_uniq(vector<T> &v, F f = F()) {
sort(begin(v), end(v), f);
v.resize(unique(begin(v), end(v)) - begin(v));
}
template <class T>
using MaxHeap = priority_queue<T>;
template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;
const int N = 1e5 + 10;
const int64_t INF = 1e18;
struct E {
int to;
int64_t k;
};
int n;
int64_t a[N], b[N];
vector<E> e[N];
int64_t go(int p) {
int64_t s = a[p] - b[p];
long double sd = s;
for (auto i : e[p]) {
auto x = go(i.to);
if (x >= 0) {
sd += x;
s += x;
} else {
sd += (long double)i.k * x;
if (sd < -INF) throw 0;
s += i.k * x;
}
};
return s;
}
int main() {
R(n);
for (int i = (1); i <= int(n); i++) R(a[i]);
for (int i = (1); i <= int(n); i++) R(b[i]);
for (int i = (2); i <= int(n); i++) {
int x;
int64_t k;
R(x, k);
e[x].push_back({i, k});
}
try {
auto x = go(1);
W(x >= 0 ? "YES" : "NO");
} catch (int) {
W("NO");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long a[100007], b[100007], x[100007], k[100007];
int main() {
long long w = 1;
for (long long i = 1; i <= 60; i++) {
w *= 2;
}
long long n, m, i, j, y, xx;
scanf("%I64d", &n);
for (i = 1; i <= n; i++) scanf("%I64d", &b[i]);
for (i = 1; i <= n; i++) scanf("%I64d", &a[i]);
if (n > 1) {
for (i = 2; i <= n; i++) {
scanf("%I64d%I64d", &x[i], &k[i]);
}
}
i = n;
double yy;
while (i > 1) {
if (b[i] < a[i]) {
yy = b[x[i]] - k[i] * (a[i] - b[i]);
if (yy > w || yy < -w) {
b[1] = -1;
break;
}
b[x[i]] -= k[i] * (a[i] - b[i]);
} else if (b[i] > a[i]) {
yy = b[x[i]] + b[i] - a[i];
if (yy > w) {
b[1] = -1;
break;
}
b[x[i]] += b[i] - a[i];
}
i--;
}
if (b[1] >= a[1])
printf("YES\n");
else
printf("NO\n");
while (j >= 2) return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr int N = 1e5 + 5;
constexpr LL INF = 2e17;
LL have[N], need[N];
int x[N], k[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> have[i];
for (int i = 1; i <= n; i++) cin >> need[i];
for (int i = 2; i <= n; i++) cin >> x[i] >> k[i];
for (int i = n; i > 1; i--) {
if (have[i] < need[i]) {
LL v = need[i] - have[i];
if ((__int128)v * k[i] > INF) {
cout << "NO\n";
return 0;
}
have[x[i]] -= (need[i] - have[i]) * k[i];
have[i] = need[i];
}
if (have[i] > need[i]) {
have[x[i]] += have[i] - need[i];
}
if (have[x[i]] < -INF) {
cout << "NO\n";
return 0;
}
}
if (have[1] < need[1])
cout << "NO\n";
else
cout << "YES\n";
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const long long inf = 0x3f3f3f3f3f3f3f3f;
int n;
long long a[maxn], b[maxn], K[maxn];
vector<int> G[maxn];
inline long long add(long long a, long long b) {
double ret = 1.0 * a + b;
if (ret > 1e18) return inf;
if (ret < -1e18) return -inf;
return a + b;
}
inline long long mul(long long a, long long b) {
double ret = 1.0 * a * b;
if (ret > 1e18) return inf;
if (ret < -1e18) return -inf;
return a * b;
}
long long dfs(int u, int fa) {
for (int i = 0; i < (int)G[u].size(); ++i) {
int v = G[u][i];
if (v == fa) continue;
long long ret = dfs(v, u);
if (ret == -inf) return -inf;
if (ret > 0) b[u] = add(b[u], ret);
if (ret < 0) {
ret = -ret;
long long prod = mul(ret, K[v]);
if (prod == -inf) return -inf;
b[u] = add(b[u], -prod);
if (b[u] == -inf) return -inf;
}
}
return add(b[u], -a[u]);
}
void print() {
for (int i = 1; i <= n; ++i) {
printf("i=%d: ", i);
for (int j = 0; j < (int)G[i].size(); ++j)
printf("%d%c", G[i][j], j + 1 == (int)G[i].size() ? '\n' : ' ');
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%I64d", &b[i]);
for (int i = 1; i <= n; ++i) scanf("%I64d", &a[i]);
for (int i = 2; i <= n; ++i) {
int x;
scanf("%d%I64d", &x, &K[i]);
G[x].push_back(i);
}
puts(dfs(1, 0) >= 0 ? "YES" : "NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double ans[100001];
long long x[100001], k[100001];
int main() {
int n, i;
cin >> n;
for (i = 1; i <= n; i++) cin >> ans[i];
for (i = 1; i <= n; i++) {
long long a;
cin >> a;
ans[i] = a - ans[i];
}
for (i = 2; i <= n; i++) cin >> x[i] >> k[i];
for (i = n; i > 1; i--) {
if (ans[i] > 0)
ans[x[i]] += k[i] * ans[i];
else
ans[x[i]] += ans[i];
}
if (ans[1] < 0.5)
cout << "YES";
else
cout << "NO";
cout << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mxn = 1e5 + 5;
const long long inf = 2e17;
const double INF = 2e17;
long long a[mxn], b[mxn];
vector<pair<int, int> > adj[mxn];
long long val[mxn] = {};
void dfs(int cur) {
val[cur] = a[cur];
for (pair<int, int> x : adj[cur]) {
int u = x.first;
long long k = x.second;
dfs(u);
if (val[u] > 0)
val[cur] += val[u];
else {
if (val[u] < -inf || (-val[u]) > INF / k) {
cout << "NO" << endl;
exit(0);
}
val[cur] += k * val[u];
val[cur] = max(val[cur], -inf);
}
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i] = b[i] - a[i];
}
for (int i = 2; i <= n; i++) {
int x;
long long k;
scanf("%d %lld", &x, &k);
adj[x].push_back({i, k});
}
dfs(1);
if (val[1] >= 0)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
|
#include <bits/stdc++.h>
using lint = long long int;
using pll = std::pair<lint, lint>;
using std::vector;
vector<pll> graph[100100];
vector<lint> bb, aa;
lint ans = 0;
bool check_ans = true;
lint check_val = 1000000000000000000LL;
lint dfs(int cur, int par, lint par_val) {
lint cur_need = bb[cur - 1] - aa[cur - 1];
lint cur_val = 1;
for (int i = 0; i < graph[cur].size(); i++) {
if (graph[cur][i].first != par) {
cur_need += dfs(graph[cur][i].first, cur, graph[cur][i].second);
if (cur_need > check_val) {
printf("NO");
exit(0);
}
} else {
cur_val = graph[cur][i].second;
}
}
if (cur_need < 0) {
if (check_val / par_val < (-cur_need)) {
printf("NO");
exit(0);
}
return cur_need * par_val;
} else {
return cur_need / cur_val;
}
}
int main(void) {
int N;
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
lint temp;
scanf("%lld", &temp);
bb.push_back(temp);
}
for (int i = 1; i <= N; i++) {
lint temp;
scanf("%lld", &temp);
aa.push_back(temp);
}
for (int i = 2; i <= N; i++) {
lint t1, t2;
scanf("%lld %lld", &t1, &t2);
graph[t1].push_back(pll(i, t2));
graph[i].push_back(pll(t1, 1));
}
ans = dfs(1, 0, 1);
if (!check_ans) {
printf("NO");
} else if (ans < 0) {
printf("NO");
} else {
printf("YES");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T& n) {
char ch;
int sign = 1;
while (!isdigit(ch = getchar()))
if (ch == '-') sign = -1;
n = ch - '0';
while (isdigit(ch = getchar())) n = n * 10 + ch - '0';
n *= sign;
}
const int INF = 1e9 + 7;
const int N = 122222;
int n, k, ans(1);
long double a[N], b[N], first[N];
struct edge {
int to, nxt;
long double k;
} g[N * 4];
int ghead[N], gtail(0);
void add(int l, int r, long double k) {
g[++gtail] = (edge){r, ghead[l], k}, ghead[l] = gtail;
}
long double dfs(int first) {
long double pos = 0, neg = 0;
for (int p = ghead[first]; p; p = g[p].nxt) {
int v = g[p].to;
long double tmp = dfs(v);
if (tmp < 0)
neg -= tmp * g[p].k;
else
pos += tmp;
}
return a[first] + pos - b[first] - neg;
}
int main() {
read(n);
for (int i = (1); i <= (n); ++i) read(a[i]);
for (int i = (1); i <= (n); ++i) read(b[i]);
for (int i = (2); i <= (n); ++i) read(first[i]), read(k), add(first[i], i, k);
puts(dfs(1) >= 0 ? "Yes" : "No");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef struct {
int sum, suf, pre, max;
} Node;
int toint(const string &s) {
stringstream ss;
ss << s;
int x;
ss >> x;
return x;
}
const int MAXN = 2e5 + 100;
const int UP = 31;
const long long int highest = 1e18;
const double Phi = 1.618033988749894;
const int logn = 20;
const double root5 = 2.236067977;
const long long int inf = 1e18;
const int N = 1e5 + 10;
std::vector<pair<int, int> > eds[N];
int n;
long long int b[N];
long long int a[N];
long long int mul(long long int a, long long int b) {
if (a == 0) return 0;
if (inf / a < b) return inf;
return a * b;
}
long long int dfs(int v) {
long long int have = b[v];
long long int want = a[v];
for (auto e : eds[v]) {
long long int x = dfs(e.first);
if (x < 0) {
want = min(inf, want + mul(-x, e.second));
} else {
have += x;
}
}
return have - want;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
cin >> b[i];
}
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 1; i < n; ++i) {
int x, k;
scanf("%d%d", &x, &k);
--x;
eds[x].push_back(make_pair(i, k));
}
long long int ans = dfs(0);
if (ans < 0) {
return !printf("NO\n");
} else {
return !printf("YES\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, ok = 1;
long long a[100005], b[100005];
struct xx {
int to, k;
xx(int a, int b) : to(a), k(b){};
};
vector<xx> v[100005];
void dfs(int now) {
for (int i = 0; i < v[now].size(); i++) {
int t = v[now][i].to, k = v[now][i].k;
dfs(t);
if (a[t] < 0) {
if (1.0 * a[t] * k + a[now] < -1e17) ok = 0;
a[now] += a[t] * k;
} else
a[now] += a[t];
}
}
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i], a[i] -= b[i];
for (int i = 2; i <= n; i++) {
int x, y;
cin >> x >> y;
v[x].push_back(xx(i, y));
}
dfs(1);
if (ok && a[1] >= 0)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int a[100100], k[100100];
int v[100100];
void solve(int n) {
int y, j, i, m, t, p, t1, x, r, q, w;
long long int z;
for (i = 0; i < n; i++) scanf("%I64d", &a[i]);
for (i = 0; i < n; i++) {
scanf("%I64d", &z);
a[i] -= z;
}
for (i = 1; i < n; i++) {
scanf("%d%I64d", &v[i], &k[i]);
v[i]--;
}
for (i = n - 1; i >= 1; i--) {
if (a[i] < -1e18 || a[i] > 1e18) {
printf("NO");
return;
}
if (a[i] > 0) {
a[v[i]] += a[i];
}
if (a[i] < 0) {
if ((1e18) / k[i] < -a[i]) {
printf("NO");
return;
}
a[v[i]] += a[i] * k[i];
if (a[v[i]] < -1e18 || a[v[i]] > 1e18) {
printf("NO");
return;
}
}
}
if (a[0] >= 0)
printf("YES");
else
printf("NO");
}
int main() {
#pragma warning(disable : 4996);
int a, b, n;
while (scanf("%d", &n) != EOF) {
solve(n);
printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
long long a[100100], b[100100];
vector<pair<int, int> > g[100100];
int x[100100], k[100100];
long long sum;
bool flag = 1;
void dfs(int t) {
for (auto e : g[t]) {
int v = e.second, k = e.first;
dfs(v);
if (b[v] > 0)
b[t] += b[v];
else {
double x = 1. * b[v] * k;
if (b[t] + x < -sum) flag = 0;
b[t] += b[v] * k;
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%lld", b + i), sum += b[i];
for (int i = 1; i <= n; ++i) scanf("%lld", a + i), b[i] -= a[i];
for (int i = 2; i <= n; ++i) {
scanf("%d%d", x + i, k + i);
g[x[i]].push_back(make_pair(k[i], i));
}
dfs(1);
flag &= b[1] >= 0;
puts(flag ? "YES" : "NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int Inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
int n;
long long b[1 << 17], a[1 << 17];
int fa[1 << 17], k[1 << 17];
vector<int> nei[1 << 17];
void inline dfs(int now) {
for (int i = 0; i < (((int)(nei[now]).size())); ++i) {
int to = nei[now][i];
dfs(to);
}
if (now == 1) {
puts(b[1] >= a[1] ? "YES" : "NO");
return;
}
if (a[now] < -5e17) {
puts("NO");
exit(0);
}
if (b[now] > a[now]) {
b[fa[now]] += b[now] - a[now];
b[now] = a[now];
} else {
long long ls = a[now] - b[now];
if ((5e17 - b[fa[now]]) / k[now] < ls) {
puts("NO");
exit(0);
}
b[fa[now]] -= 1ll * k[now] * ls;
b[now] = a[now];
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= (n); ++i) scanf("%lld", b + i);
for (int i = 1; i <= (n); ++i) scanf("%lld", a + i);
for (int i = (2); i <= (n); ++i)
scanf("%d%d", fa + i, k + i), nei[fa[i]].push_back(i);
dfs(1);
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100009;
vector<int> e[maxn];
long long b[maxn], a[maxn], fa[maxn], t[maxn];
long long n;
void dfs(int u, long long mx) {
for (int i = 0; i < e[u].size(); i++) dfs(e[u][i], mx + b[u]);
if (a[u] - b[u] > mx) {
puts("NO");
exit(0);
}
if (b[u] < a[u])
b[fa[u]] -= t[u] * (a[u] - b[u]);
else
b[fa[u]] += b[u] - a[u];
}
int main() {
scanf("%I64d", &n);
for (int i = 1; i <= n; i++) scanf("%I64d", &b[i]);
for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
for (int i = 2; i <= n; i++) {
scanf("%I64d%I64d", &fa[i], &t[i]);
e[fa[i]].push_back(i);
}
dfs(1, 0);
puts(b[0] >= 0 ? "YES" : "NO");
return 0;
}
|
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
int IT_MAX = 1 << 15;
const long long MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const long long LL_INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
const double ERR = 1e-10;
long long mymul(long long a, long long b) {
bool c = false;
if (a < 0) {
a *= -1;
c = true;
}
if (b < 0) {
b *= -1;
c = !c;
}
long long rv;
if ((2 * LL_INF) / b < a)
rv = 2 * LL_INF;
else
rv = min(a * b, 2 * LL_INF);
if (c) rv *= -1;
return rv;
}
vector<pair<long long, long long> > son[100050];
long long B[100050];
long long A[100050];
long long in[100050];
long long par[100050][2];
void DFS(int n) {
for (auto it : son[n]) DFS(it.second);
if (in[n] < 0) {
if (n == 1) {
printf("NO\n");
exit(0);
}
int t1 = par[n][0], t2 = par[n][1];
in[t1] -= mymul(-in[n], t2);
if (in[t1] < -LL_INF) {
printf("NO\n");
exit(0);
}
} else {
int t1 = par[n][0], t2 = par[n][1];
in[t1] += in[n];
}
}
int main() {
int N, i;
scanf("%d", &N);
for (i = 1; i <= N; i++) scanf("%lld", &B[i]);
for (i = 1; i <= N; i++) scanf("%lld", &A[i]);
for (i = 2; i <= N; i++) {
int t1, t2;
scanf("%d %d", &t1, &t2);
son[t1].emplace_back(t2, i);
par[i][0] = t1, par[i][1] = t2;
}
for (i = 1; i <= N; i++) in[i] = B[i] - A[i];
DFS(1);
return !printf("YES\n");
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 10, inf = 1e16 + 10;
long long a[N], b[N], x[N], k[N];
inline bool bad(long long x, long long y) { return inf / x < y; }
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
for (long long i = 0; i < n; i++) cin >> a[i];
for (long long i = 0; i < n; i++) cin >> b[i];
for (long long i = 1; i < n; i++) {
cin >> x[i] >> k[i];
x[i]--;
}
for (long long i = n - 1; i > 0; i--) {
if (a[i] >= b[i])
a[x[i]] += a[i] - b[i];
else {
if (bad(k[i], b[i] - a[i]) || b[i] > inf) return cout << "NO", 0;
b[x[i]] += k[i] * (b[i] - a[i]);
}
}
cout << (a[0] < b[0] ? "NO" : "YES");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long a[1000009], b[1000009], x[1000009], k[1000009];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n + 1; i++) scanf("%lld", &a[i]);
for (int i = 1; i < n + 1; i++) scanf("%lld", &b[i]);
for (int i = 2; i < n + 1; i++) scanf("%lld %lld", &x[i], &k[i]);
bool f = 1;
double L = 1e18;
for (int i = n + 1 - 1; i >= 2; i--) {
long long p = max(0LL, a[i] - b[i]);
a[i] -= p;
a[x[i]] += p;
p = max(0LL, b[i] - a[i]);
double q = double(p) * k[i];
if (q > L)
f = 0;
else {
a[x[i]] -= p * k[i];
if (a[x[i]] < -L) f = 0;
a[i] += p;
}
}
f = (a[1] >= b[1]) && f;
cout << (f ? "YES" : "NO") << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
void add(long long &a, long long b) {
if (a == 1e18) return;
a += b;
if (a > 1e18) a = 1e18;
}
long long mult(long long a, long long b) {
if (a == 1e18) return 1e18;
if (1e18 / b < a) return 1e18;
a *= b;
if (a > 1e18) return 1e18;
return a;
}
int n;
long long b[100010], a[100010];
int par[100010], k[100010];
vector<int> ch[100010];
long long need[100010], excess[100010];
void dfs(int u) {
for (int v : ch[u]) {
dfs(v);
excess[u] += excess[v];
add(need[u], mult(need[v], k[v]));
}
if (b[u] < a[u]) {
add(need[u], a[u] - b[u]);
} else {
excess[u] += b[u] - a[u];
}
long long amt = min(need[u], excess[u]);
add(need[u], -amt);
excess[u] -= amt;
}
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 2; i <= n; i++) {
cin >> par[i] >> k[i];
ch[par[i]].push_back(i);
}
dfs(1);
cout << (need[1] == 0 ? "YES" : "NO") << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
double ans[100001];
long long x[100001], k[100001];
int main() {
int n, i;
cin >> n;
for (i = 1; i <= n; i++) cin >> ans[i];
for (i = 1; i <= n; i++) {
long long a;
cin >> a;
ans[i] = a - ans[i];
}
for (i = 2; i <= n; i++) cin >> x[i] >> k[i];
for (i = n; i > 1; i--) {
if (ans[i] > 0)
ans[x[i]] += k[i] * ans[i];
else
ans[x[i]] += ans[i];
}
if (ans[1] < 0.5)
cout << "YES";
else
cout << "NO";
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:512000000")
using namespace std;
const long long MAX = 100000000LL * 100000000LL;
const long long MIN = numeric_limits<long long>::min();
const double PI = 3.14159265358979;
const long long MOD = 1000000007LL;
template <class T>
ostream& operator<<(ostream& out, vector<T>& v) {
for (int i = 0; i < v.size(); ++i) out << v[i] << " ";
return out;
}
template <class T>
istream& operator>>(istream& in, vector<T>& v) {
for (int i = 0; i < v.size(); ++i) in >> v[i];
return in;
}
template <class L, class R>
istream& operator>>(istream& in, pair<L, R>& p) {
in >> p.first >> p.second;
return in;
}
template <class T>
T lexical_cast(string& s) {
stringstream ss(s);
T t;
ss >> t;
return t;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long cdiv(long long a, long long b) { return (a + b - 1) / b; }
long long inv(long long n, long long mod) {
long long pow = mod - 2;
long long ans = 1;
long long cur = n;
while (pow > 0) {
if (pow & 1) {
ans *= cur;
ans %= mod;
}
pow /= 2;
cur *= cur;
cur %= mod;
}
return ans;
}
template <class Cont>
void sort(Cont& c) {
sort(begin(c), end(c));
}
template <class Cont>
void reverse(Cont& c) {
reverse(begin(c), end(c));
}
bool shit;
long long dfs(int v, vector<pair<int, long long>>& p, vector<vector<int>>& ch,
vector<long long>& a, vector<long long>& b) {
long long balance = b[v];
for (auto to : ch[v]) {
balance += dfs(to, p, ch, a, b);
if (balance < (long long)-1 * 1000000000 * 1000000000) {
shit = true;
}
}
balance -= a[v];
if (balance < 0) {
long double nb = (long double)balance * (long double)p[v].second;
if (nb < (long double)-MAX) shit = true;
balance *= p[v].second;
}
return balance;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n;
cin >> n;
shit = false;
vector<long long> b(n + 1), a(n + 1);
for (int i = 0; i < n; ++i) {
cin >> b[i + 1];
}
for (int i = 0; i < n; ++i) {
cin >> a[i + 1];
}
vector<pair<int, long long>> p(n + 1);
p[1] = {0, 1};
vector<vector<int>> ch(n + 1);
for (int i = 0; i < n - 1; ++i) {
int x;
long long k;
cin >> x >> k;
p[i + 2] = {x, k};
ch[x].push_back(i + 2);
}
long long ans = dfs(1, p, ch, a, b);
if (!shit && ans >= 0) {
cout << "YES";
} else {
cout << "NO";
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n;
long long ar[N], br[N];
struct T {
int dest, type;
long long cost;
};
vector<T> graf[N];
int cnt[N] = {};
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) cin >> ar[i];
for (int i = 0; i < n; ++i) cin >> br[i];
for (int i = 0; i < n; ++i) ar[i] -= br[i];
for (int i = 1; i < n; ++i) {
int x, k;
cin >> x >> k;
--x;
graf[i].push_back({x, 0, k});
graf[x].push_back({i, 1, k});
++cnt[i], ++cnt[x];
}
stack<int> st;
for (int i = 1; i < n; ++i)
if (graf[i].size() == 1) st.push(i);
while (st.size()) {
int x = st.top();
st.pop();
--cnt[x];
if (cnt[x] > 1 || !x) continue;
T y;
for (auto z : graf[x]) {
if (cnt[z.dest]) y = z;
}
if (ar[x] > 0)
ar[y.dest] += ar[x] / (y.type ? y.cost : 1);
else if (ar[x] < 0) {
if (y.type)
ar[y.dest] += ar[x];
else {
if (double(y.cost) * ar[x] < -1e18) {
cout << "NO" << endl;
exit(0);
}
ar[y.dest] += ar[x] * y.cost;
}
}
if (ar[y.dest] < -1e18) {
cout << "NO" << endl;
exit(0);
}
cnt[x] = 0;
st.push(y.dest);
}
if (ar[0] >= 0)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
|
#include <bits/stdc++.h>
inline long long read() {
char c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
long long k = 0, kk = 1;
if (c == '-') c = getchar(), kk = -1;
while (c >= '0' && c <= '9') k = k * 10 + c - '0', c = getchar();
return kk * k;
}
using namespace std;
void write(long long x) {
if (x < 0) x = -x, putchar('-');
if (x / 10) write(x / 10);
putchar(x % 10 + '0');
}
void writeln(long long x) {
write(x);
puts("");
}
long long n, c[1000010], x, y, b[1000010], a[1000010][3], last[1000010],
f[1000010], kk;
long double ff[1000010];
void dfs(long long x) {
f[x] = ff[x] = c[x] - b[x];
for (long long i = last[x]; i; i = a[i][0]) {
dfs(a[i][1]);
if (f[a[i][1]] >= 0)
f[x] += f[a[i][1]], ff[x] += ff[a[i][1]];
else
f[x] += f[a[i][1]] * a[i][2], ff[x] += ff[a[i][1]] * a[i][2];
}
}
void doit(long long x, long long y, long long z) {
a[++kk][0] = last[x];
a[kk][1] = y;
a[kk][2] = z;
last[x] = kk;
}
signed main() {
n = read();
for (long long i = 1; i <= n; i++) c[i] = read();
for (long long i = 1; i <= n; i++) b[i] = read();
for (long long i = 1; i < n; i++) x = read(), y = read(), doit(x, i + 1, y);
dfs(1);
if (ff[1] >= 1e15 || fabs(ff[1] - f[1]) <= 1e9 && f[1] >= 0)
puts("YES");
else
puts("NO");
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const long long inf = 1e18;
long long materials[N];
vector<vector<pair<int, long long>>> edge(N);
int n;
pair<long long, long long> dfs(int now) {
long long need = max(-materials[now], 0LL);
long long supply = max(materials[now], 0LL);
for (pair<int, long long> it : edge[now]) {
pair<long long, long long> ret = dfs(it.first);
if (ret.first > inf / it.second) {
cout << "NO" << endl;
exit(0);
} else {
need = need + ret.first * it.second;
}
if (need > inf) {
cout << "NO" << endl;
exit(0);
}
supply = supply + ret.second;
}
if (need < supply) {
return {0, supply - need};
} else if (supply < need) {
return {need - supply, 0};
} else {
return {0, 0};
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
cin >> materials[i];
}
for (int i = 1; i <= n; i++) {
long long temp;
cin >> temp;
materials[i] = materials[i] - temp;
}
for (int i = 2; i <= n; i++) {
int x, k;
scanf("%d%d", &x, &k);
edge[x].emplace_back(i, k);
}
pair<long long, long long> ret = dfs(1);
if (ret.first > 0)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename... As>
struct tpl : public std::tuple<As...> {
using std::tuple<As...>::tuple;
tpl() {}
tpl(std::tuple<As...> const& b) { std::tuple<As...>::operator=(b); }
template <typename T = tuple<As...>>
typename tuple_element<0, T>::type const& x() const {
return get<0>(*this);
}
template <typename T = tuple<As...>>
typename tuple_element<0, T>::type& x() {
return get<0>(*this);
}
template <typename T = tuple<As...>>
typename tuple_element<1, T>::type const& y() const {
return get<1>(*this);
}
template <typename T = tuple<As...>>
typename tuple_element<1, T>::type& y() {
return get<1>(*this);
}
template <typename T = tuple<As...>>
typename tuple_element<2, T>::type const& z() const {
return get<2>(*this);
}
template <typename T = tuple<As...>>
typename tuple_element<2, T>::type& z() {
return get<2>(*this);
}
template <typename T = tuple<As...>>
typename tuple_element<3, T>::type const& w() const {
return get<3>(*this);
}
template <typename T = tuple<As...>>
typename tuple_element<3, T>::type& w() {
return get<3>(*this);
}
};
using lli = long long int;
using llu = long long unsigned;
using pii = tpl<lli, lli>;
using piii = tpl<lli, lli, lli>;
using piiii = tpl<lli, lli, lli, lli>;
using vi = vector<lli>;
using vii = vector<pii>;
using viii = vector<piii>;
using vvi = vector<vi>;
using vvii = vector<vii>;
using vviii = vector<viii>;
template <class T>
using min_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T>
using max_queue = priority_queue<T>;
template <size_t... I>
struct my_index_sequence {
using type = my_index_sequence;
static constexpr array<size_t, sizeof...(I)> value = {{I...}};
};
namespace my_index_sequence_detail {
template <typename I, typename J>
struct concat;
template <size_t... I, size_t... J>
struct concat<my_index_sequence<I...>, my_index_sequence<J...>>
: my_index_sequence<I..., (sizeof...(I) + J)...> {};
template <size_t N>
struct make_index_sequence
: concat<typename make_index_sequence<N / 2>::type,
typename make_index_sequence<N - N / 2>::type>::type {};
template <>
struct make_index_sequence<0> : my_index_sequence<> {};
template <>
struct make_index_sequence<1> : my_index_sequence<0> {};
} // namespace my_index_sequence_detail
template <class... A>
using my_index_sequence_for =
typename my_index_sequence_detail::make_index_sequence<sizeof...(A)>::type;
template <class T, size_t... I>
void print_tuple(ostream& s, T const& a, my_index_sequence<I...>) {
using swallow = int[];
(void)swallow{0, (void(s << (I == 0 ? "" : ", ") << get<I>(a)), 0)...};
}
template <class T>
ostream& print_collection(ostream& s, T const& a);
template <class... A>
ostream& operator<<(ostream& s, tpl<A...> const& a);
template <class... A>
ostream& operator<<(ostream& s, tuple<A...> const& a);
template <class A, class B>
ostream& operator<<(ostream& s, pair<A, B> const& a);
template <class T, size_t I>
ostream& operator<<(ostream& s, array<T, I> const& a) {
return print_collection(s, a);
}
template <class T>
ostream& operator<<(ostream& s, vector<T> const& a) {
return print_collection(s, a);
}
template <class T, class U>
ostream& operator<<(ostream& s, multimap<T, U> const& a) {
return print_collection(s, a);
}
template <class T>
ostream& operator<<(ostream& s, multiset<T> const& a) {
return print_collection(s, a);
}
template <class T, class U>
ostream& operator<<(ostream& s, map<T, U> const& a) {
return print_collection(s, a);
}
template <class T>
ostream& operator<<(ostream& s, set<T> const& a) {
return print_collection(s, a);
}
template <class T>
ostream& print_collection(ostream& s, T const& a) {
s << '[';
for (auto it = begin(a); it != end(a); ++it) {
s << *it;
if (it != prev(end(a))) s << " ";
}
return s << ']';
}
template <class... A>
ostream& operator<<(ostream& s, tpl<A...> const& a) {
s << '(';
print_tuple(s, a, my_index_sequence_for<A...>{});
return s << ')';
}
template <class... A>
ostream& operator<<(ostream& s, tuple<A...> const& a) {
s << '(';
print_tuple(s, a, my_index_sequence_for<A...>{});
return s << ')';
}
template <class A, class B>
ostream& operator<<(ostream& s, pair<A, B> const& a) {
return s << "(" << get<0>(a) << ", " << get<1>(a) << ")";
}
namespace std {
namespace {
template <class T>
inline void hash_combine(size_t& seed, T const& v) {
seed ^= hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
template <class Tuple, size_t Index = tuple_size<Tuple>::value - 1>
struct HashValueImpl {
static void apply(size_t& seed, Tuple const& tuple) {
HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
hash_combine(seed, get<Index>(tuple));
}
};
template <class Tuple>
struct HashValueImpl<Tuple, 0> {
static void apply(size_t& seed, Tuple const& tuple) {
hash_combine(seed, get<0>(tuple));
}
};
} // namespace
template <typename... TT>
struct hash<tuple<TT...>> {
size_t operator()(tuple<TT...> const& tt) const {
size_t seed = 0;
HashValueImpl<tuple<TT...>>::apply(seed, tt);
return seed;
}
};
template <typename... TT>
struct hash<tpl<TT...>> {
size_t operator()(tpl<TT...> const& tt) const {
size_t seed = 0;
HashValueImpl<tuple<TT...>>::apply(seed, tt);
return seed;
}
};
} // namespace std
const int N = 1e5 + 16;
lli B[N];
lli A[N];
lli x[N];
lli k[N];
lli l10(lli x) {
lli i = 0;
x /= 10;
while (x) {
i += 1;
x /= 10;
}
return i;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
lli n;
cin >> n;
for (lli i = 0; i < (lli)(n); ++i) cin >> B[i];
for (lli i = 0; i < (lli)(n); ++i) cin >> A[i];
vvi G(n);
x[0] = -1;
for (lli i = 0; i < (lli)(n - 1); ++i) {
cin >> x[i + 1] >> k[i + 1];
--x[i + 1];
G[x[i + 1]].push_back(i + 1);
}
function<lli(lli)> dfs = [&](lli i) {
lli self = B[i] - A[i];
for (lli j : G[i]) {
lli r = dfs(j);
if (r >= 0)
self += r;
else {
if (l10(k[j]) + l10(r - 1) >= 16) {
self = -1e16;
} else {
self += k[j] * r;
}
}
}
return self;
};
lli ans = dfs(0);
if (ans >= 0) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000 + 10;
const long long inf = 100000000000000000ll;
int n;
long long a[MAX_N], b[MAX_N];
vector<pair<int, int> > G[MAX_N];
int degree[MAX_N];
long long dfs(int u, long long ratio = 1) {
long long kilograms = b[u] - a[u];
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i].first;
long long Ratio = G[u][i].second;
kilograms += dfs(v, Ratio);
}
if (kilograms >= 0) {
if (kilograms > inf) {
printf("NO\n");
exit(0);
}
return kilograms;
} else {
kilograms *= -1ll;
if (kilograms >= inf / ratio) {
printf("NO\n");
exit(0);
}
kilograms = kilograms * ratio * -1ll;
if (kilograms <= -inf) {
printf("NO\n");
exit(0);
}
return kilograms;
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &b[i]);
}
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
for (int i = 2; i <= n; i++) {
int x, k;
scanf("%d%d", &x, &k);
G[x].push_back(pair<int, int>(i, k));
degree[i]++;
}
for (int i = 1; i <= n; i++)
if (degree[i] == 0) {
if (dfs(i) < 0) {
printf("NO\n");
exit(0);
}
}
printf("YES\n");
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
long long b[N];
long long a[N];
int x[N];
long long k[N];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%lld", b + i);
}
for (int i = 1; i <= n; i++) {
scanf("%lld", a + i);
}
for (int i = 2; i <= n; i++) {
scanf("%d %lld", x + i, k + i);
}
for (int i = n; i >= 2; i--) {
if (b[i] > a[i]) {
b[x[i]] += b[i] - a[i];
} else if (b[i] < a[i]) {
if ((a[i] - b[i]) > 1000000000000000000 / k[i]) {
cout << "NO" << endl;
return 0;
}
a[x[i]] += (a[i] - b[i]) * k[i];
if (a[x[i]] >= 5e17) {
cout << "NO" << endl;
return 0;
}
}
}
if (b[1] >= a[1]) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
return 0;
}
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U>
ostream& operator<<(ostream& o, const pair<T, U>& p) {
o << "(" << p.first << "," << p.second << ")";
return o;
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& v) {
o << "[";
for (T t : v) {
o << t << ",";
}
o << "]";
return o;
}
const string ok = "YES", ng = "NO";
const ll INF = 100000000000000000LL;
inline ll mul(ll x, ll y) {
if (x > INF / y) return -1;
return x * y;
}
string solve() {
int n;
cin >> n;
vector<ll> b(n), a(n);
for (int(i) = 0; (i) < (int)(n); ++(i)) cin >> b[i];
for (int(i) = 0; (i) < (int)(n); ++(i)) cin >> a[i];
vector<int> x(n - 1), k(n - 1);
for (int(i) = 0; (i) < (int)(n - 1); ++(i)) {
cin >> x[i] >> k[i];
--x[i];
}
for (int i = n - 1; i > 0; --i) {
if (b[i] >= a[i]) {
b[x[i - 1]] += b[i] - a[i];
b[i] = a[i];
} else {
ll need = mul(a[i] - b[i], k[i - 1]);
if (need == -1) return ng;
b[x[i - 1]] -= need;
if (b[x[i - 1]] < -INF) return ng;
b[i] = a[i];
}
}
for (int(i) = 0; (i) < (int)(n); ++(i))
if (b[i] < a[i]) return ng;
return ok;
}
int main() {
cout << solve() << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, a[100003], b[100003], k[100003], x[100003];
vector<int> v[100003];
bool vis[100003];
long long dfs(int x) {
vis[x] = 1;
long long ret = b[x] - a[x];
for (int i = 0; i < v[x].size(); i++) {
long long re = dfs(v[x][i]);
if (re >= 0)
ret += re;
else {
if ((re / 1000000000) * k[v[x][i]] < -2 * pow(10, 8)) {
cout << "NO";
exit(0);
}
ret += re * k[v[x][i]];
if (ret < -2 * pow(10, 17)) {
cout << "NO";
exit(0);
}
}
}
return ret;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> b[i];
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i < n; i++) cin >> x[i] >> k[i], v[x[i] - 1].push_back(i);
if (dfs(0) >= 0)
cout << "YES";
else
cout << "NO";
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
long long sum, a[N], b[N], x[N], k[N];
vector<pair<long long, long long> > g[N];
bool flag = 1;
inline long long read() {
long long s = 1, a = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') s = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
a = a * 10 + ch - '0';
ch = getchar();
}
return s * a;
}
inline void dfs(int u) {
for (auto e : g[u]) {
int k = e.first, v = e.second;
dfs(v);
if (b[v] > 0)
b[u] += b[v];
else {
double w = 1.0 * b[v] * k;
if (b[u] + w < -sum) flag = 0;
b[u] += b[v] * k;
}
}
}
int main() {
int n = read();
for (int i = 1; i <= n; i++) b[i] = read(), sum += b[i];
for (int i = 1; i <= n; i++) a[i] = read(), b[i] -= a[i];
for (int i = 2; i <= n; i++) {
int x = read(), k = read();
g[x].push_back(make_pair(k, i));
}
dfs(1);
return 0 * puts(flag & (b[1] >= 0) ? "YES" : "NO");
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e15 + 7;
const long long int maxn = 1e6 + 10;
const long long inf = 1e18 + 18;
double pie = 3.1415926535;
long long int a[maxn], b[maxn];
vector<pair<long long int, long long int> > adj[maxn];
long long mult(long long a, long long b) {
if (a >= inf / b + 2) {
return inf;
}
return min(a * b, inf);
}
void dfs(long long int s) {
if (a[s] <= -inf) {
cout << "NO";
exit(0);
}
long long int i, node, k;
for (i = 0; i < int(adj[s].size()); i++) {
node = adj[s][i].first;
k = adj[s][i].second;
dfs(node);
if (a[node] < b[node]) {
if (mult(b[node] - a[node], k) >= inf) {
cout << "NO";
exit(0);
}
if ((a[s] - mult(b[node] - a[node], k)) <= -inf) {
cout << "NO";
exit(0);
}
a[s] = a[s] - (b[node] - a[node]) * k;
} else {
a[s] += a[node] - b[node];
}
}
}
int main() {
long long int n, x, k, i;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (i = 1; i <= n; i++) cin >> a[i];
for (i = 1; i <= n; i++) cin >> b[i];
for (i = 2; i <= n; i++) {
cin >> x >> k;
adj[x].push_back({i, k});
}
dfs(1);
if (a[1] >= b[1])
cout << "YES";
else
cout << "NO";
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int N = 1e5 + 4;
vector<pair<int, int> > v[N];
long long dif[N];
long long limit = 1e17 + 4;
void dfs(int node, int par) {
for (auto it : v[node]) {
int x = it.first;
if (x == par) continue;
dfs(x, node);
if (dif[x] > 0) dif[node] += dif[x];
}
for (auto it : v[node]) {
int x = it.first, wt = it.second;
if (x == par) continue;
if (dif[x] < 0) {
long long req = -dif[x];
dif[x] = 0;
if (req > (limit / wt)) {
printf("NO");
exit(0);
}
dif[node] -= (wt * req);
if (dif[node] < -limit) {
printf("NO");
exit(0);
}
}
}
}
int main() {
int n;
scanf("%d", &n);
long long a[n + 1], b[n + 1];
for (int i = 1; i < n + 1; i++) scanf("%lld", &b[i]);
for (int i = 1; i < n + 1; i++) scanf("%lld", &a[i]);
for (int i = 1; i < n + 1; i++) dif[i] = b[i] - a[i];
for (int i = 2; i < n + 1; i++) {
int par, wt;
scanf("%d", &par);
scanf("%d", &wt);
v[i].emplace_back(par, wt);
v[par].emplace_back(i, wt);
}
dfs(1, -1);
if (dif[1] >= 0)
printf("YES");
else
printf("NO");
}
|
#include <bits/stdc++.h>
const long long MaxN = 1e5 + 50;
struct Edge {
long long nxt, to, w;
} E[MaxN << 2];
template <class t>
inline void read(t &s) {
s = 0;
register long long f = 1;
register char c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
s *= f;
return;
}
template <class t, class... A>
inline void read(t &x, A &...a) {
read(x);
read(a...);
}
template <class t>
inline void write(t x) {
if (x < 0) putchar('-'), x = -x;
long long buf[21], top = 0;
while (x) buf[++top] = x % 10, x /= 10;
if (!top) buf[++top] = 0;
while (top) putchar(buf[top--] ^ '0');
return;
}
inline void setIn(std::string s) {
freopen(s.c_str(), "r", stdin);
return;
}
inline void setOut(std::string s) {
freopen(s.c_str(), "w", stdout);
return;
}
inline void setIO(std::string s = "") {
setIn(s + ".in");
setOut(s + ".out");
return;
}
template <class t>
inline bool checkmin(t &x, t y) {
if (x > y) {
x = y;
return 1;
}
return 0;
}
template <class t>
inline bool checkmax(t &x, t y) {
if (x < y) {
x = y;
return 1;
}
return 0;
}
inline long long lowbit(long long x) { return x & (-x); }
long long hd[MaxN], en, n;
inline void adde(long long u, long long v, long long w) {
E[++en] = (Edge){hd[u], v, w}, hd[u] = en;
}
long double f[MaxN];
const long double eps = 1e-9;
const long double Lim = -1e50;
inline long long sign(long double x) {
if (std::fabs(x) < eps) return 0;
return x > 0 ? 1 : -1;
}
inline void dfs(long long u) {
for (long long i = hd[u]; ~i; i = E[i].nxt) {
register long long v = E[i].to;
dfs(v);
if (!sign(f[v])) continue;
if (sign(f[v]) > 0)
f[u] += f[v];
else
f[u] += f[v] * E[i].w, checkmax(f[u], Lim);
}
}
signed main(void) {
std::memset(hd, -1, sizeof hd);
read(n);
register long long x, y;
for (long long i = 1; i <= n; ++i) read(x), f[i] += x;
for (long long i = 1; i <= n; ++i) read(x), f[i] -= x;
for (long long i = 2; i <= n; ++i) read(x, y), adde(x, i, y);
dfs(1);
std::puts(sign(f[1]) >= 0 ? "YES" : "NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double PI = atan(1) * 4;
const int oo = 1000000000;
const int N = 1000010;
int n, p[N];
vector<int> g[N];
long long a[N], b[N], k[N];
double val[N];
void dfs(int u) {
for (int i = 0; i < g[u].size(); ++i) {
int v = g[u][i];
dfs(v);
if (val[v] < 0) {
val[u] += (double)k[v] * val[v];
} else {
val[u] += val[v];
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%lld", &b[i]);
}
for (int i = 1; i <= n; ++i) {
scanf("%lld", &a[i]);
val[i] = b[i] - a[i];
}
for (int i = 2; i <= n; ++i) {
scanf("%d%lld", &p[i], &k[i]);
g[p[i]].push_back(i);
}
dfs(1);
if (val[1] >= 0) {
puts("YES");
} else {
puts("NO");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
long long A[N + 5], B[N + 5], K[N + 5];
int X[N + 5];
vector<int> G[N + 5];
bool vst[N + 5];
long long inf = 1e18;
bool dfs(int s, int p) {
vst[s] = true;
for (int u : G[s]) {
if (vst[u]) continue;
if (!dfs(u, s)) return false;
}
if (s == 1) return B[s] >= A[s];
if (A[s] > B[s]) {
if (X[s] == p) {
long long diff = A[s] - B[s];
if (1. * diff * K[s] >= inf) return false;
B[p] -= diff * K[s];
} else
B[p] -= A[s] - B[s];
if (B[p] <= -inf) return false;
B[s] = A[s];
} else {
if (X[p] == s) {
long long diff = B[s] - A[s];
diff = diff / K[p] * K[p];
B[p] += diff / K[p];
B[s] -= diff;
} else {
B[p] += B[s] - A[s];
B[s] = A[s];
}
}
return true;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &B[i]);
for (int i = 1; i <= n; i++) scanf("%lld", &A[i]);
for (int i = 2; i <= n; i++) {
scanf("%lld%lld", &X[i], &K[i]);
G[i].push_back(X[i]);
G[X[i]].push_back(i);
}
puts(dfs(1, -1) ? "YES" : "NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MXN = 1e5 + 1;
set<pair<long long, long long> > adj, g[MXN];
long long N, B[MXN], A[MXN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int x = 0, qwe = N; x < qwe; x++) cin >> B[x];
for (int x = 0, qwe = N; x < qwe; x++) cin >> A[x], A[x] -= B[x];
for (int x = 1, qwe = N; x < qwe; x++) {
int xj, kj;
cin >> xj >> kj;
xj--;
g[x].insert({xj, 1});
g[xj].insert({x, kj});
}
for (int x = 0, qwe = N; x < qwe; x++) adj.insert({((int)(g[x]).size()), x});
bool valid = 1;
while (adj.size() > 1) {
int u = adj.begin()->second;
adj.erase(adj.begin());
int v = g[u].begin()->first;
long long other_c = g[v].lower_bound({u, 0})->second;
g[v].erase(g[v].lower_bound({u, 0}));
adj.erase({((int)(g[v]).size()) + 1, v});
adj.insert({((int)(g[v]).size()), v});
if (A[u] <= 0)
A[v] += A[u] / g[u].begin()->second;
else {
long long val = other_c * A[u];
if (other_c == 0 || val / other_c == A[u])
A[v] += other_c * A[u];
else
valid = 0;
valid &= A[v] <= 2 * 1e17;
}
}
valid &= A[adj.begin()->second] <= 0;
cout << (valid ? "YES" : "NO") << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000 + 10;
const long long inf = 100000000000000000ll;
int n;
long long a[MAX_N], b[MAX_N];
vector<pair<int, int> > G[MAX_N];
int degree[MAX_N];
long long dfs(int u, long long ratio = 1) {
long long kilograms = b[u] - a[u];
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i].first;
long long Ratio = G[u][i].second;
kilograms += dfs(v, Ratio);
if (kilograms <= -inf || kilograms >= inf) {
printf("NO\n");
exit(0);
}
}
if (kilograms >= 0) {
return kilograms;
} else {
kilograms *= -1ll;
if (kilograms >= inf / ratio) {
printf("NO\n");
exit(0);
}
kilograms = kilograms * ratio * -1ll;
if (kilograms <= -inf) {
printf("NO\n");
exit(0);
}
return kilograms;
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &b[i]);
}
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
for (int i = 2; i <= n; i++) {
int x, k;
scanf("%d%d", &x, &k);
G[x].push_back(pair<int, int>(i, k));
degree[i]++;
}
for (int i = 1; i <= n; i++)
if (degree[i] == 0) {
if (dfs(i) < 0) {
printf("NO\n");
exit(0);
}
}
printf("YES\n");
}
|
#include <bits/stdc++.h>
const long double PI = acos(-1);
const long double eps = 0.0000000001;
const long long INF = 1e17;
long long n, v[100005], fa[100005], k[100005];
std::vector<long long> son[100005];
long long dfs(long long now) {
long long res = v[now];
for (auto i : son[now]) {
res += dfs(i);
if (1.0l * res * k[now] < -INF) std::cout << "NO" << std::endl, exit(0);
}
return res < 0 ? res * k[now] : res;
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin >> n;
for (long long i = 1; i <= n; ++i) std::cin >> v[i];
for (long long i = 1; i <= n; ++i) {
static long long tmp;
std::cin >> tmp;
v[i] -= tmp;
}
for (long long i = 2; i <= n; ++i) {
std::cin >> fa[i] >> k[i];
son[fa[i]].push_back(i);
}
k[1] = 1;
std::cout << (dfs(1) >= 0 ? "YES" : "NO") << std::endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const double pi = acos(-1);
int n, i, j, k, t;
long double a[100004], b[100004], val[100003];
int x[100004], c[100003];
vector<int> adj[100005];
int par[100003];
bool bisa = true;
void dfs(int v, int p) {
val[v] = b[v] - a[v];
for (int x : adj[v]) {
if (x != p) {
dfs(x, v);
if (val[x] > 0)
val[v] += val[x];
else
val[v] += val[x] * c[x];
}
}
if (val[v] < 0 && fabsl(val[v]) > 1e17L / c[v]) {
puts("NO");
exit(0);
}
}
int main() {
scanf("%d", &n);
for (i = 1; i <= n; ++i) scanf("%Lf", b + i);
for (i = 1; i <= n; ++i) scanf("%Lf", a + i);
for (i = 2; i <= n; ++i) {
scanf("%d %d", x + i, c + i);
adj[x[i]].push_back(i);
adj[i].push_back(x[i]);
}
dfs(1, 1);
puts(val[1] >= 0 ? "YES" : "NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct node {
long long to, next, w;
} e[100010];
long long n, cnt = 0, x, y, a[100010], head[100010];
double tmp;
void add(long long i) { e[++cnt] = {i, head[x], y}, head[x] = cnt; }
void dfs(long long u, long long f, long long k) {
for (long long i = head[u]; i; i = e[i].next) dfs(e[i].to, u, e[i].w);
if (a[u] >= 0)
a[f] += a[u];
else if (f) {
tmp = double(a[u]) * double(k), a[f] += a[u] * k;
if (tmp < -23333333333333333 || a[f] < -23333333333333333)
puts("NO"), exit(0);
} else if (a[u] < 0)
puts("NO"), exit(0);
}
int main() {
scanf("%lld", &n);
for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (long long i = 1; i <= n; i++) scanf("%lld", &x), a[i] -= x;
for (long long i = 2; i <= n; i++) scanf("%lld%lld", &x, &y), add(i);
dfs(1, 0, 0), puts("YES");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mx = 1e5 + 10;
const long long int oo = 1e18;
vector<int> g[mx];
long long int b[mx], a[mx], k[mx];
int p[mx];
vector<int> lyr[mx];
void dfs(int u, int l = 0) {
lyr[l].push_back(u);
for (int v : g[u])
if (v != p[u]) dfs(v, l + 1);
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%lld", b + i);
for (int i = 0; i < n; i++) scanf("%lld", a + i);
for (int i = 1; i < n; i++) {
scanf("%d %lld", p + i, k + i);
--p[i];
g[p[i]].push_back(i);
}
dfs(0);
bool ok = true;
for (int l = n - 1; l > 0 && ok; l--)
for (int u : lyr[l]) {
int pu = p[u];
long long int dx = b[u] - a[u];
if (dx >= 0)
b[pu] += dx, b[u] -= dx;
else {
dx = -dx;
if (k[u] <= oo / dx) {
b[u] += dx;
b[pu] -= k[u] * dx;
if (b[pu] <= -oo) ok = false;
} else
ok = false;
}
}
if (b[0] < a[0]) ok = false;
if (ok)
puts("YES");
else
puts("NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007, MOD2 = 1000000009;
const int INF = 0x3f3f3f3f;
const long long BINF = 0x3f3f3f3f3f3f3f3fLL;
int n;
long long demand[100005], supply[100005];
vector<pair<int, long long> > g[100005];
void dfs(int u, int p = -1) {
if (supply[u] < demand[u])
demand[u] = demand[u] - supply[u], supply[u] = 0;
else
supply[u] = supply[u] - demand[u], demand[u] = 0;
for (auto e : g[u]) {
int v = e.first;
if (v - p) {
dfs(v, u);
if (demand[v] > 4e18 / e.second) cout << "NO" << endl, exit(0);
long long wow = demand[v] * e.second;
if (demand[u] + wow > 4e18) cout << "NO" << endl, exit(0);
demand[u] += wow;
supply[u] += supply[v];
}
}
if (supply[u] < demand[u])
demand[u] = demand[u] - supply[u], supply[u] = 0;
else
supply[u] = supply[u] - demand[u], demand[u] = 0;
}
int solve() {
cin >> n;
for (int i = 0; i < n; i++) cin >> supply[i];
for (int i = 0; i < n; i++) cin >> demand[i];
for (int i = 1; i < n; i++) {
int p;
long long hi;
cin >> p >> hi;
p--;
g[p].push_back({i, hi});
}
dfs(0);
cout << (demand[0] > 0 ? "NO" : "YES") << endl;
return 0;
}
int main() {
ios::sync_with_stdio(0);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1ll << 62;
const double eps = 1e-8;
inline void gn(long long& x) {
int sg = 1;
char c;
while (((c = getchar()) < '0' || c > '9') && c != '-')
;
c == '-' ? (sg = -1, x = 0) : (x = c - '0');
while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0';
x *= sg;
}
inline void gn(int& x) {
long long t;
gn(t);
x = t;
}
inline void gn(unsigned long long& x) {
long long t;
gn(t);
x = t;
}
const int maxn = 1e5 + 5;
long long a[maxn], b[maxn];
vector<pair<int, long long> > E[maxn];
void dfs(int u, int pr) {
for (auto it : E[u]) {
dfs(it.first, u);
if (b[it.first] < 0) {
long long nd = -b[it.first];
if (it.second > 1.0 * inf / nd) {
puts("NO");
exit(0);
}
nd *= it.second;
b[u] -= nd;
if (b[u] < -inf) {
puts("NO");
exit(0);
}
} else {
b[u] += b[it.first];
}
}
}
int main() {
int n;
gn(n);
;
for (int i = (1); i <= (n); i++) gn(b[i]);
for (int i = (1); i <= (n); i++) gn(a[i]);
for (int i = (2); i <= (n); i++) {
int p, k;
gn(p);
gn(k);
E[p].push_back(make_pair(i, k));
}
for (int i = (1); i <= (n); i++) b[i] -= a[i];
dfs(1, 0);
if (b[1] >= 0)
puts("YES");
else
puts("NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T& x) {
x = 0;
T fl = 1;
char ch = 0;
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') fl = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = (x << 1) + (x << 3) + (ch ^ 48);
x *= fl;
}
template <typename T, typename... Args>
inline void read(T& x, Args&... args) {
read(x);
read(args...);
}
const int N = 1e6 + 5;
struct edge {
int to, nt;
} E[N];
int H[N], e_cnt, n;
double h[N];
long long k[N], b[N], a[N];
void add_edge(int u, int v) {
E[++e_cnt] = (edge){v, H[u]};
H[u] = e_cnt;
}
void dfs(int u) {
for (int e = H[u]; e; e = E[e].nt) {
int v = E[e].to;
dfs(v);
if (h[v] < 0)
h[u] += 1.0 * k[v] * h[v];
else
h[u] += h[v];
}
}
int main() {
read(n);
for (int i = (1); i <= (n); i++) read(b[i]);
for (int i = (1); i <= (n); i++) read(a[i]);
for (int i = (1); i <= (n); i++) h[i] = b[i] - a[i];
for (int i = (2); i <= (n); i++) {
int x;
read(x, k[i]);
add_edge(x, i);
}
dfs(1);
if (h[1] >= 0)
printf("YES\n");
else
printf("NO\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const long long inf = 0x7fffffffffffffff;
int n;
long long a[maxn], b[maxn], K[maxn];
vector<int> G[maxn];
inline long long add(long long a, long long b) {
double ret = 1.0 * a + b;
if (ret > 1e18) return inf;
if (ret < -1e18) return -inf;
return a + b;
}
inline long long mul(long long a, long long b) {
double ret = 1.0 * a * b;
if (ret > 1e18) return inf;
if (ret < -1e18) return -inf;
return a * b;
}
long long dfs(int u, int fa) {
for (int i = 0; i < (int)G[u].size(); ++i) {
int v = G[u][i];
if (v == fa) continue;
long long ret = dfs(v, u);
if (ret > 0) b[u] = add(b[u], ret);
if (ret < 0) {
ret = -ret;
long long prod = mul(ret, K[v]);
b[u] = add(b[u], -prod);
}
}
return add(b[u], -a[u]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%I64d", &b[i]);
for (int i = 1; i <= n; ++i) scanf("%I64d", &a[i]);
for (int i = 2; i <= n; ++i) {
int x;
scanf("%d%I64d", &x, &K[i]);
G[x].push_back(i);
}
puts(dfs(1, 0) >= 0 ? "YES" : "NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
class Scanner {
private:
static const long long BUFFER_SIZE = 10000;
char buff[BUFFER_SIZE];
long long buffPos, buffLim;
public:
Scanner() {
buffLim = fread(buff, 1, BUFFER_SIZE, stdin);
buffPos = 0;
}
private:
inline void flushBuff() {
buffLim = fread(buff, 1, BUFFER_SIZE, stdin);
if (buffLim == 0) {
buff[buffLim++] = '\n';
}
buffPos = 0;
}
inline bool isWS(char t) { return t == ' ' || t == '\n'; }
inline bool isDig(char t) { return t >= '0' && t <= '9'; }
void nextPos() {
buffPos++;
if (buffPos == buffLim) {
flushBuff();
}
}
public:
inline char getchar() {
char ch = buff[buffPos];
nextPos();
return ch;
}
inline void next(char* s) {
while (isWS(buff[buffPos])) {
nextPos();
}
while (!isWS(buff[buffPos])) {
*s = buff[buffPos];
s++;
nextPos();
}
*s = '\0';
}
inline void nextLine(char* s) {
while (buff[buffPos] != '\n') {
nextPos();
}
if (buff[buffPos] == '\n') {
nextPos();
}
while (buff[buffPos] != '\n') {
*s = buff[buffPos];
s++;
buffPos++;
}
*s = '\0';
}
inline long long nextInt() {
while (!isDig(buff[buffPos])) {
nextPos();
}
long long sign = (buff[buffPos] == '-') ? nextPos(), -1 : 1;
long long res = 0;
while (isDig(buff[buffPos])) {
res = res * 10 + buff[buffPos] - '0';
nextPos();
}
return res * sign;
}
inline double nextDouble() {
while (isWS(buff[buffPos])) {
nextPos();
}
long long sign = (buff[buffPos] == '-') ? nextPos(), -1 : 1;
double res = 0;
while (isDig(buff[buffPos])) {
res = res * 10 + buff[buffPos] - '0';
nextPos();
}
if (buff[buffPos] == '.') {
nextPos();
double ep = 1;
while (isDig(buff[buffPos])) {
ep *= 0.1;
res += ep * (buff[buffPos] - '0');
nextPos();
}
}
return sign * res;
}
inline char nextChar() {
while (isWS(buff[buffPos])) nextPos();
char res = buff[buffPos];
nextPos();
return res;
}
};
Scanner sc;
template <class T>
inline bool checkMin(T& a, T b) {
return (a > b ? a = b, 1 : 0);
}
template <class T>
inline bool checkMax(T& a, T b) {
return (a < b ? a = b, 1 : 0);
}
void ALERT(bool judgememt, const char* phrase) {
if (judgememt) {
puts(phrase);
throw "ALERT";
}
}
bool alert(bool judgememt, const char* phrase) {
if (judgememt) puts(phrase);
return judgememt;
}
void preInit();
void init();
void solve();
int32_t main() {
preInit();
init();
solve();
return 0;
}
const long long N = 100005;
const long long INF = 0x3f3f3f3f3f3f3f3f;
long long b[N], t[N];
vector<long long> e[N];
long long n;
void preInit() {
n = sc.nextInt();
for (long long i = 1; i <= n; i++) {
b[i] = sc.nextInt();
}
for (long long i = 1; i <= n; i++) {
b[i] -= sc.nextInt();
}
for (long long i = 2; i <= n; i++) {
e[sc.nextInt()].push_back(i);
t[i] = sc.nextInt();
}
}
void init() {}
void dfs(long long u, long long f) {
for (long long v : e[u])
if (v != f) {
dfs(v, u);
if (b[v] > 0) {
b[u] += b[v];
} else {
if (b[v] < (-INF) / t[v]) {
b[u] = -INF;
} else {
b[u] += b[v] * t[v];
}
checkMax(b[u], -INF);
}
};
;
}
void solve() {
dfs(1, 0);
puts(b[1] >= 0 ? "YES" : "NO");
}
|
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, double> > e[100010];
int N;
double a[100010], b[100010], f[100010];
bool ok = true;
void dfs(int x) {
f[x] = b[x] - a[x];
for (auto t : e[x]) {
dfs(t.first);
if (f[t.first] < 0)
f[x] += t.second * f[t.first];
else
f[x] += f[t.first];
if (f[x] < -1e20) ok = false;
}
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) scanf("%lf", &b[i]);
for (int i = 1; i <= N; i++) scanf("%lf", &a[i]);
for (int i = 2; i <= N; i++) {
int u;
double w;
scanf("%d%lf", &u, &w);
e[u].push_back(make_pair(i, w));
}
dfs(1);
puts(f[1] > -1e-12 && ok ? "YES" : "NO");
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 10;
vector<pair<long long, long long> > g[MAXN];
long long a[MAXN], b[MAXN], sum;
void dfs(long long v, long long p = -1, long long x = -1) {
for (pair<long long, long long> u : g[v]) dfs(u.first, v, u.second);
if (a[v] < b[v] and p != -1) {
if ((b[v] - a[v]) * x > sum) return;
a[p] -= (b[v] - a[v]) * x;
a[v] = b[v];
} else if (p != -1) {
long long k = (a[v] - b[v]);
a[p] += k;
a[v] -= k;
}
}
int32_t main() {
ios::sync_with_stdio(false);
long long n;
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
for (long long i = 0; i < n; i++) cin >> b[i];
long long TMP = 0;
for (long long i = 1; i < n; i++) {
long long v, k;
cin >> v >> k;
TMP = max(TMP, k);
v--;
g[v].push_back({i, k});
}
dfs(0);
for (long long i = 0; i < n; i++)
if (a[i] < b[i]) {
cout << "NO" << endl;
return 0;
}
cout << "YES" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
pair<int, long long> par[100010];
vector<int> adj[100010];
long long need[100010];
long long cur[100010];
long long bad(long long a, long long b) {
double x = (double)a * b;
if (x < -2e17) {
cout << "NO\n";
exit(0);
}
return a * b;
}
long long dfs(int v) {
for (int e : adj[v]) {
cur[v] += dfs(e);
bad(cur[v], 1);
}
long long diff = cur[v] - need[v];
if (diff >= 0) {
return bad(diff, 1);
} else {
return bad(diff, par[v].second);
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> cur[i];
}
for (int i = 0; i < n; i++) {
cin >> need[i];
}
for (int i = 1; i < n; i++) {
cin >> par[i].first >> par[i].second;
par[i].first--;
adj[par[i].first].push_back(i);
}
par[0].second = 1;
dfs(0);
if (need[0] > cur[0]) {
cout << "NO\n";
} else {
cout << "YES\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef struct {
int sum, suf, pre, max;
} Node;
int toint(const string &s) {
stringstream ss;
ss << s;
int x;
ss >> x;
return x;
}
const int MAXN = 2e5 + 100;
const int UP = 31;
const long long int highest = 1e18;
const double Phi = 1.618033988749894;
const int logn = 20;
const double root5 = 2.236067977;
const long long int inf = 1e18;
const int N = 1e5 + 10;
std::vector<pair<int, int> > eds[N];
int n;
long long int b[N];
long long int a[N];
long long int mul(long long int a, long long int b) {
if (a == 0) return 0;
if (inf / a < b) return inf;
return a * b;
}
long long int dfs(int v) {
long long int have = b[v];
long long int want = a[v];
for (auto e : eds[v]) {
long long int x = dfs(e.first);
if (x < 0) {
want = min(inf, want + mul(-x, e.second));
} else {
have += x;
}
}
return have - want;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
cin >> b[i];
}
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 1; i < n; ++i) {
int x, k;
scanf("%d%d", &x, &k);
--x;
eds[x].push_back(make_pair(i, k));
}
long long int ans = dfs(0);
if (ans < 0) {
return !printf("NO\n");
} else {
return !printf("YES\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int binpow(long long int a, long long int b) {
a %= 1000000007;
long long int res = 1;
while (b > 0) {
if (b & 1) res = (res * a) % 1000000007;
a = (a * a) % 1000000007;
b >>= 1;
}
return res;
}
long long int a[100005], b[100005], vis[100005], x[100005], k[100005];
vector<long long int> adj[100005];
void dfs(long long int n) {
vis[n] = 1;
for (auto xx : adj[n]) {
if (vis[xx] == 0) {
dfs(xx);
if (a[xx] > b[xx] && 1000000000000000000 / (a[xx] - b[xx]) < k[xx]) {
cout << "NO";
exit(0);
}
if (1000000000000000000 < a[n] + k[xx] * max(0LL, a[xx] - b[xx])) {
cout << "NO";
exit(0);
}
a[n] += k[xx] * max(0LL, a[xx] - b[xx]);
b[n] += max(0LL, b[xx] - a[xx]);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, i, fl = 0;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> b[i];
}
for (i = 1; i <= n; i++) {
cin >> a[i];
}
for (i = 2; i <= n; i++) {
cin >> x[i] >> k[i];
adj[i].push_back(x[i]);
adj[x[i]].push_back(i);
}
for (i = 1; i <= n; i++) {
if (vis[i] == 0) {
dfs(i);
if (b[i] < a[i]) {
fl = 1;
break;
}
}
}
if (fl) {
cout << "NO";
} else {
cout << "YES";
}
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
long long a[100005], b[100005], p[100005], n;
vector<pair<long long, long long> > v[100005];
void dfs(long long x) {
for (auto u : v[x]) {
dfs(u.first);
if (p[u.first] >= 0)
p[x] += p[u.first];
else {
if (p[u.first] < -1e18 / u.second) {
cout << "NO";
exit(0);
}
p[x] += p[u.first] * u.second;
}
if (p[x] < -1e18) {
cout << "NO";
exit(0);
}
}
p[x] += b[x] - a[x];
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (long long i = 1; i <= n; i++) cin >> b[i];
for (long long i = 1; i <= n; i++) cin >> a[i];
long long x, y;
for (long long i = 2; i <= n; i++) {
cin >> x >> y;
v[x].emplace_back(make_pair(i, y));
}
dfs(1);
if (p[1] >= 0) {
cout << "YES";
} else
cout << "NO";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 5;
const long long inf = (1ll << 60) - 1;
vector<int> g[maxn];
long long a[maxn], b[maxn];
long long k[maxn];
int n;
int fa[maxn];
bool flag = true;
void handle(int v, int pa) {
if (!flag) return;
for (int i = 0; i < (int)g[v].size(); i++) {
int u = g[v][i];
if (u != pa) handle(u, v);
}
if (pa == -1) {
if (b[v] >= a[v])
return;
else {
flag = false;
return;
}
} else {
if (b[v] - a[v] >= 0)
b[pa] += b[v] - a[v], b[v] = a[v];
else {
long long del = 0;
if (a[v] - b[v] > inf)
del = inf;
else
del = (a[v] - b[v]) * k[v];
b[v] = a[v];
if (del >= inf)
b[pa] = -inf;
else
b[pa] -= del;
if (b[pa] < -inf) b[pa] = -inf;
}
}
}
void out() {
printf("a:\n");
for (int i = 1; i <= n; i++) printf("%I64d ", a[i]);
puts("");
printf("b:\n");
for (int i = 1; i <= n; i++) printf("%I64d ", b[i]);
puts("");
}
int main() {
scanf("%d", &n);
long long sumb = 0, suma = 0;
for (int i = 1; i <= n; i++) {
scanf("%I64d", &b[i]);
sumb += b[i];
}
for (int i = 1; i <= n; i++) {
scanf("%I64d", &a[i]);
suma += a[i];
}
if (sumb < suma) {
printf("NO\n");
exit(0);
}
for (int i = 2; i <= n; i++) {
int x;
scanf("%d%I64d", &x, &k[i]);
fa[i] = x;
g[x].push_back(i);
g[i].push_back(x);
}
handle(1, -1);
if (flag) {
printf("YES\n");
} else
printf("NO\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const long long INF = 1000000000000000000;
int fa[MAXN];
vector<pair<int, int> > tree[MAXN];
long long a[MAXN];
long long b[MAXN];
long long val[MAXN];
long long f[MAXN];
long long multi(const long long x, const long long y) {
if (x >= INF / y) return INF;
return x * y;
}
void dfs(const int u, const long long k) {
bool flag(false);
for (auto cur : tree[u]) {
int v(cur.first), k(cur.second);
dfs(v, k);
if (f[v] > 0) {
if (val[u] <= f[v] - INF)
flag = true;
else
val[u] -= f[v];
}
}
if (flag)
f[u] = INF;
else {
if (val[u] >= 0)
val[fa[u]] += val[u];
else
f[u] = multi(-val[u], k);
}
}
bool solve(const int n) {
for (int i(1); i <= n; ++i) val[i] = b[i] - a[i];
dfs(1, 1);
if (f[1] > 0)
return false;
else
return true;
}
int main() {
int n;
scanf("%d", &n);
for (int i(1); i <= n; ++i) scanf("%lld", b + i);
for (int i(1); i <= n; ++i) scanf("%lld", a + i);
for (int i(2), x, k; i <= n; ++i) {
scanf("%d %d", &x, &k);
tree[x].push_back(pair<int, int>(i, k));
fa[i] = x;
}
if (solve(n))
puts("YES");
else
puts("NO");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
vector<long long> b, a;
vector<vector<pair<int, int> > > adjlist;
vector<bool> vis;
bool flag = false;
long long dfs(int s) {
for (int i = 0; i < (int)adjlist[s].size(); i++) {
int p = adjlist[s][i].first;
int k = adjlist[s][i].second;
long long required = dfs(p);
if (required > 0) {
b[s] -= required * k;
}
if (required < 0) {
b[s] += (-required);
}
}
vis[s] = true;
long long tot = a[s] - b[s];
if (tot < (long long)-2 * 1e17) {
flag = true;
}
return a[s] - b[s];
}
int main() {
scanf("%d", &n);
b.assign(n + 10, 0);
a.assign(n + 10, 0);
vis.assign(n + 10, false);
adjlist.assign(n + 10, vector<pair<int, int> >());
for (int i = 0; i < n; i++) {
cin >> b[i];
}
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int x, k;
for (int i = 1; i < n; i++) {
scanf("%d %d", &x, &k);
adjlist[x - 1].push_back(pair<int, int>(i, k));
}
long long ans;
for (int i = 0; i < n; i++) {
if (!vis[i]) {
ans = dfs(i);
if (flag || ans > 0) {
printf("NO");
return 0;
}
}
}
printf("YES");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long LIM = 1e18;
const int MAXN = 1e5;
int N;
bool possible = true;
long long A[MAXN], B[MAXN];
vector<pair<int, int> > T[MAXN];
void dfs(int v) {
for (int i = 0; i < (int)T[v].size(); i++) {
int u = T[v][i].first;
int c = T[v][i].second;
dfs(u);
if (B[u] >= A[u])
B[v] += B[u] - A[u];
else if (A[u] - B[u] > (double)LIM / c)
possible = false;
else if (B[v] < -LIM + (A[u] - B[u]) * c)
possible = false;
else
B[v] -= (A[u] - B[u]) * c;
}
}
int main() {
scanf("%d", &N);
for (int i = 0; i < (int)N; i++) scanf("%lld", &B[i]);
for (int i = 0; i < (int)N; i++) scanf("%lld", &A[i]);
for (int i = 1; i < (int)N; i++) {
int p, c;
scanf("%d", &p), scanf("%d", &c);
T[p - 1].push_back({i, c});
}
dfs(0);
printf("%s\n", B[0] >= A[0] && possible ? "YES" : "NO");
}
|
#include <bits/stdc++.h>
using namespace std;
double b[100005] = {0}, a[100005] = {0};
int x, k, c[100005], d[100005];
int main() {
int n, i;
cin >> n;
for (i = 1; i <= n; i++) cin >> b[i];
for (i = 1; i <= n; i++) {
cin >> a[i];
a[i] = b[i] - a[i];
}
for (i = 2; i <= n; i++) cin >> c[i] >> d[i];
for (i = n; i >= 2; i--) {
if (a[i] < 0) {
a[c[i]] = a[c[i]] + (a[i] * d[i]);
a[i] = 0;
} else if (a[i] >= 0) {
a[c[i]] += a[i];
a[i] = 0;
}
}
for (i = 1; i <= n; i++)
if (a[i] < 0) {
cout << "NO";
return 0;
}
cout << "YES";
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.