output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
class raw_input {
public:
template <typename T>
raw_input& operator>>(T& arg) {
if constexpr (std::is_integral<T>::value) {
if constexpr (sizeof(T) == sizeof(int)) {
scanf("%d", &arg);
} else {
scanf("%lld", &arg);
}
}
if constexpr (std::is_floating_point<T>::value) {
if constexpr (sizeof(T) == sizeof(double)) {
scanf("%lf", &arg);
} else {
scanf("%LF", &arg);
}
}
return *this;
}
raw_input& operator>>(char* arg) {
scanf("%s", arg);
return *this;
}
raw_input& operator>>(char& c) {
while ((c = getchar()) != EOF && (c == ' ' || c == '\n'))
;
return *this;
}
} gin{};
using namespace std;
long long ans = -1e18;
using ll = long long;
bool v[200005];
int a[200005];
vector<int> g[200005];
pair<ll, ll> dfs(int node) {
v[node] = true;
vector<ll> good;
ll sum = a[node];
for (int i : g[node]) {
if (v[i]) continue;
pair<ll, ll> elem = dfs(i);
ll item = elem.first;
good.push_back(item);
sum += elem.second;
}
sort(good.begin(), good.end());
int sz = good.size();
if (sz >= 2) {
ans = max(ans, good[sz - 1] + good[sz - 2]);
}
good.push_back(sum);
return make_pair(*max_element(good.begin(), good.end()), sum);
}
int main() {
int n;
gin >> n;
for (int i = 0; i < n; ++i) gin >> a[i];
for (int i = 0; i < n - 1; ++i) {
int x, y;
gin >> x >> y;
--x;
--y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(0);
if (ans == -1e18) {
cout << "Impossible\n";
} else {
cout << ans << endl;
}
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
class raw_input {
public:
template <typename T>
raw_input& operator>>(T& arg) {
if constexpr (std::is_integral<T>::value) {
if constexpr (sizeof(T) == sizeof(int)) {
scanf("%d", &arg);
} else {
scanf("%lld", &arg);
}
}
if constexpr (std::is_floating_point<T>::value) {
if constexpr (sizeof(T) == sizeof(double)) {
scanf("%lf", &arg);
} else {
scanf("%LF", &arg);
}
}
return *this;
}
raw_input& operator>>(char* arg) {
scanf("%s", arg);
return *this;
}
raw_input& operator>>(char& c) {
while ((c = getchar()) != EOF && (c == ' ' || c == '\n'))
;
return *this;
}
} gin{};
using namespace std;
long long ans = -1e18;
using ll = long long;
bool v[200005];
int a[200005];
vector<int> g[200005];
pair<ll, ll> dfs(int node) {
v[node] = true;
vector<ll> good;
ll sum = a[node];
for (int i : g[node]) {
if (v[i]) continue;
pair<ll, ll> elem = dfs(i);
ll item = elem.first;
good.push_back(item);
sum += elem.second;
}
sort(good.begin(), good.end());
int sz = good.size();
if (sz >= 2) {
ans = max(ans, good[sz - 1] + good[sz - 2]);
}
good.push_back(sum);
return make_pair(*max_element(good.begin(), good.end()), sum);
}
int main() {
int n;
gin >> n;
for (int i = 0; i < n; ++i) gin >> a[i];
for (int i = 0; i < n - 1; ++i) {
int x, y;
gin >> x >> y;
--x;
--y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(0);
if (ans == -1e18) {
cout << "Impossible\n";
} else {
cout << ans << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
void Read(T &x) {
char c;
bool f(0);
while (c = getchar(), c != EOF)
if (c == '-')
f = 1;
else if (c >= '0' && c <= '9') {
x = c - '0';
while (c = getchar(), c >= '0' && c <= '9') x = x * 10 + c - '0';
ungetc(c, stdin);
if (f) x = -x;
return;
}
}
struct node {
int v;
node *next;
} * adj[200000 + 10], edge[200000 * 2 + 10], *ecnt = edge;
inline void addedge(int u, int v) {
node *p = ++ecnt;
p->v = v;
p->next = adj[u];
adj[u] = p;
}
int n, st[200000 + 10], ed[200000 + 10], dcnt, a[200000 + 10];
long long mx[200000 * 4 + 10], sum[200000 + 10], ans = -0x7fffffffffffffffll;
inline void update(int i) { mx[i] = max(mx[i << 1], mx[(i << 1) | 1]); }
void build(int i, int l, int r) {
if (l == r) {
mx[i] = sum[l];
return;
}
int mid((l + r) >> 1);
build(i << 1, l, mid);
build((i << 1) | 1, mid + 1, r);
update(i);
}
void insert(int i, int l, int r, int p, long long d) {
if (l == r) {
mx[i] = d;
return;
}
int mid((l + r) >> 1);
if (p <= mid)
insert(i << 1, l, mid, p, d);
else
insert((i << 1) | 1, mid + 1, r, p, d);
update(i);
}
long long get_mx(int i, int l, int r, int ll, int rr) {
if (ll <= l && r <= rr) return mx[i];
if (ll > r || rr < l) return -0x7fffffffffffffffll;
int mid((l + r) >> 1);
return max(get_mx(i << 1, l, mid, ll, rr),
get_mx((i << 1) | 1, mid + 1, r, ll, rr));
}
void dfs(int u, int fa) {
st[u] = ++dcnt;
sum[st[u]] = a[u];
for (node *p = adj[u]; p; p = p->next) {
if (p->v != fa) {
dfs(p->v, u);
sum[st[u]] += sum[st[p->v]];
}
}
ed[u] = dcnt;
}
void read() {
Read(n);
int i, u, v;
for (i = 1; i <= n; i++) Read(a[i]);
for (i = 1; i < n; i++) {
Read(u), Read(v);
addedge(u, v);
addedge(v, u);
}
}
void dfs2(int u, int fa) {
long long t =
max(get_mx(1, 1, n, 1, st[u] - 1), get_mx(1, 1, n, ed[u] + 1, n));
if (t != -0x7fffffffffffffffll) ans = max(ans, sum[st[u]] + t);
insert(1, 1, n, st[u], -0x7fffffffffffffffll);
for (node *p = adj[u]; p; p = p->next)
if (p->v != fa) dfs2(p->v, u);
insert(1, 1, n, st[u], sum[st[u]]);
}
void solve() {
dfs(1, 0);
build(1, 1, n);
dfs2(1, 0);
if (ans == -0x7fffffffffffffffll)
puts("Impossible");
else
printf("%I64d\n", ans);
}
int main() {
read();
solve();
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
void Read(T &x) {
char c;
bool f(0);
while (c = getchar(), c != EOF)
if (c == '-')
f = 1;
else if (c >= '0' && c <= '9') {
x = c - '0';
while (c = getchar(), c >= '0' && c <= '9') x = x * 10 + c - '0';
ungetc(c, stdin);
if (f) x = -x;
return;
}
}
struct node {
int v;
node *next;
} * adj[200000 + 10], edge[200000 * 2 + 10], *ecnt = edge;
inline void addedge(int u, int v) {
node *p = ++ecnt;
p->v = v;
p->next = adj[u];
adj[u] = p;
}
int n, st[200000 + 10], ed[200000 + 10], dcnt, a[200000 + 10];
long long mx[200000 * 4 + 10], sum[200000 + 10], ans = -0x7fffffffffffffffll;
inline void update(int i) { mx[i] = max(mx[i << 1], mx[(i << 1) | 1]); }
void build(int i, int l, int r) {
if (l == r) {
mx[i] = sum[l];
return;
}
int mid((l + r) >> 1);
build(i << 1, l, mid);
build((i << 1) | 1, mid + 1, r);
update(i);
}
void insert(int i, int l, int r, int p, long long d) {
if (l == r) {
mx[i] = d;
return;
}
int mid((l + r) >> 1);
if (p <= mid)
insert(i << 1, l, mid, p, d);
else
insert((i << 1) | 1, mid + 1, r, p, d);
update(i);
}
long long get_mx(int i, int l, int r, int ll, int rr) {
if (ll <= l && r <= rr) return mx[i];
if (ll > r || rr < l) return -0x7fffffffffffffffll;
int mid((l + r) >> 1);
return max(get_mx(i << 1, l, mid, ll, rr),
get_mx((i << 1) | 1, mid + 1, r, ll, rr));
}
void dfs(int u, int fa) {
st[u] = ++dcnt;
sum[st[u]] = a[u];
for (node *p = adj[u]; p; p = p->next) {
if (p->v != fa) {
dfs(p->v, u);
sum[st[u]] += sum[st[p->v]];
}
}
ed[u] = dcnt;
}
void read() {
Read(n);
int i, u, v;
for (i = 1; i <= n; i++) Read(a[i]);
for (i = 1; i < n; i++) {
Read(u), Read(v);
addedge(u, v);
addedge(v, u);
}
}
void dfs2(int u, int fa) {
long long t =
max(get_mx(1, 1, n, 1, st[u] - 1), get_mx(1, 1, n, ed[u] + 1, n));
if (t != -0x7fffffffffffffffll) ans = max(ans, sum[st[u]] + t);
insert(1, 1, n, st[u], -0x7fffffffffffffffll);
for (node *p = adj[u]; p; p = p->next)
if (p->v != fa) dfs2(p->v, u);
insert(1, 1, n, st[u], sum[st[u]]);
}
void solve() {
dfs(1, 0);
build(1, 1, n);
dfs2(1, 0);
if (ans == -0x7fffffffffffffffll)
puts("Impossible");
else
printf("%I64d\n", ans);
}
int main() {
read();
solve();
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long dp[200010][3];
long long cst[200010];
long long sz[200010];
long long visited[200010];
vector<vector<int>> vt;
void dfs(long long here) {
visited[here] = 1;
sz[here] = cst[here];
vector<long long> gap;
for (int i = 0; i < vt[here].size(); i++) {
int next = vt[here][i];
if (!visited[next]) {
dfs(next);
sz[here] += sz[next];
dp[here][2] = max(dp[here][2], dp[next][2]);
dp[here][1] = max(dp[here][1], dp[next][1]);
gap.push_back(dp[next][1]);
}
}
dp[here][1] = max(dp[here][1], sz[here]);
sort(gap.begin(), gap.end());
if (gap.size() >= 2) {
int t = gap.size();
dp[here][2] = max(dp[here][2], gap[t - 1] + gap[t - 2]);
}
}
int main() {
for (int i = 1; i < 200010; i++) {
dp[i][2] = -1e17;
dp[i][1] = -1e17;
}
long long n;
scanf("%lld", &n);
vt.resize(n + 1);
for (int i = 1; i <= n; i++) scanf("%lld", &cst[i]);
for (int i = 1; i <= n - 1; i++) {
long long q, w;
scanf("%lld %lld", &q, &w);
vt[q].push_back(w);
vt[w].push_back(q);
}
dfs(1);
if (dp[1][2] == -1e17)
puts("Impossible");
else
printf("%lld", dp[1][2]);
}
|
### Prompt
Generate a cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long dp[200010][3];
long long cst[200010];
long long sz[200010];
long long visited[200010];
vector<vector<int>> vt;
void dfs(long long here) {
visited[here] = 1;
sz[here] = cst[here];
vector<long long> gap;
for (int i = 0; i < vt[here].size(); i++) {
int next = vt[here][i];
if (!visited[next]) {
dfs(next);
sz[here] += sz[next];
dp[here][2] = max(dp[here][2], dp[next][2]);
dp[here][1] = max(dp[here][1], dp[next][1]);
gap.push_back(dp[next][1]);
}
}
dp[here][1] = max(dp[here][1], sz[here]);
sort(gap.begin(), gap.end());
if (gap.size() >= 2) {
int t = gap.size();
dp[here][2] = max(dp[here][2], gap[t - 1] + gap[t - 2]);
}
}
int main() {
for (int i = 1; i < 200010; i++) {
dp[i][2] = -1e17;
dp[i][1] = -1e17;
}
long long n;
scanf("%lld", &n);
vt.resize(n + 1);
for (int i = 1; i <= n; i++) scanf("%lld", &cst[i]);
for (int i = 1; i <= n - 1; i++) {
long long q, w;
scanf("%lld %lld", &q, &w);
vt[q].push_back(w);
vt[w].push_back(q);
}
dfs(1);
if (dp[1][2] == -1e17)
puts("Impossible");
else
printf("%lld", dp[1][2]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
T mulmod(T a, T b) {
if (b == 0) return 0;
if (b == 1) return a;
T x = mulmod(a, b >> 1);
x = (x + x) % 1000000007;
if (b & 1) x = (x + a) % 1000000007;
return x;
}
template <class T>
T larger(T a, T b) {
return (a > b ? a : b);
}
template <class T>
T smaller(T a, T b) {
return (a < b ? a : b);
}
template <class T>
T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <class T>
T lcm(T a, T b) {
return (a * b) / gcd(a, b);
}
template <class T>
string converter(T n) {
stringstream x;
x << n;
return x.str();
}
template <class T>
T pw(T a, T b) {
if (b == 0) return 1;
T x = pw(a, b >> 1);
x = (x * x) % 1000000007;
if (b & 1) x = (x * a) % 1000000007;
return x;
}
template <class T1>
void deb(T1 e1) {
cout << e1 << endl;
}
template <class T1, class T2>
void deb(T1 e1, T2 e2) {
cout << e1 << " " << e2 << endl;
}
template <class T1, class T2, class T3>
void deb(T1 e1, T2 e2, T3 e3) {
cout << e1 << " " << e2 << " " << e3 << endl;
}
template <class T1, class T2, class T3, class T4>
void deb(T1 e1, T2 e2, T3 e3, T4 e4) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl;
}
template <class T1, class T2, class T3, class T4, class T5>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl;
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6
<< endl;
}
vector<int> g[200005];
long long int c[200005];
long long int sub[200005];
long long int dp[200005];
long long int ans = -1e18;
void dfs(int s, int p) {
sub[s] = c[s];
dp[s] = -1e18;
long long int mx = -1e18, smx = -1e18;
for (auto v : g[s]) {
if (v != p) {
dfs(v, s);
sub[s] += sub[v];
if (mx <= dp[v]) {
smx = mx;
mx = dp[v];
} else if (smx <= dp[v])
smx = dp[v];
}
}
dp[s] = max(sub[s], mx);
ans = max(ans, mx + smx);
}
void letsgo() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
if (ans < -2e14)
cout << "Impossible";
else
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
letsgo();
return 0;
}
|
### Prompt
Please create a solution in Cpp to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
T mulmod(T a, T b) {
if (b == 0) return 0;
if (b == 1) return a;
T x = mulmod(a, b >> 1);
x = (x + x) % 1000000007;
if (b & 1) x = (x + a) % 1000000007;
return x;
}
template <class T>
T larger(T a, T b) {
return (a > b ? a : b);
}
template <class T>
T smaller(T a, T b) {
return (a < b ? a : b);
}
template <class T>
T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <class T>
T lcm(T a, T b) {
return (a * b) / gcd(a, b);
}
template <class T>
string converter(T n) {
stringstream x;
x << n;
return x.str();
}
template <class T>
T pw(T a, T b) {
if (b == 0) return 1;
T x = pw(a, b >> 1);
x = (x * x) % 1000000007;
if (b & 1) x = (x * a) % 1000000007;
return x;
}
template <class T1>
void deb(T1 e1) {
cout << e1 << endl;
}
template <class T1, class T2>
void deb(T1 e1, T2 e2) {
cout << e1 << " " << e2 << endl;
}
template <class T1, class T2, class T3>
void deb(T1 e1, T2 e2, T3 e3) {
cout << e1 << " " << e2 << " " << e3 << endl;
}
template <class T1, class T2, class T3, class T4>
void deb(T1 e1, T2 e2, T3 e3, T4 e4) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl;
}
template <class T1, class T2, class T3, class T4, class T5>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl;
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6
<< endl;
}
vector<int> g[200005];
long long int c[200005];
long long int sub[200005];
long long int dp[200005];
long long int ans = -1e18;
void dfs(int s, int p) {
sub[s] = c[s];
dp[s] = -1e18;
long long int mx = -1e18, smx = -1e18;
for (auto v : g[s]) {
if (v != p) {
dfs(v, s);
sub[s] += sub[v];
if (mx <= dp[v]) {
smx = mx;
mx = dp[v];
} else if (smx <= dp[v])
smx = dp[v];
}
}
dp[s] = max(sub[s], mx);
ans = max(ans, mx + smx);
}
void letsgo() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
if (ans < -2e14)
cout << "Impossible";
else
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
letsgo();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<signed long long> pls;
vector<list<int> > adj;
vector<bool> visited;
vector<signed long long> max_pls, max_pls2;
vector<signed long long> tot_pls;
vector<int> parent;
void dfs_pls(int i) {
visited[i] = true;
for (auto j : adj[i]) {
if (visited[j]) continue;
parent[j] = i;
dfs_pls(j);
}
signed long long tpls = pls[i];
for (auto j : adj[i]) {
if (parent[i] == j) continue;
tpls += tot_pls[j];
}
tot_pls[i] = tpls;
for (auto j : adj[i]) {
if (parent[i] == j) continue;
signed long long cand = max(max_pls[j], tot_pls[j]);
if (cand > max_pls[i]) {
max_pls2[i] = max_pls[i];
max_pls[i] = cand;
} else if (cand > max_pls2[i]) {
max_pls2[i] = cand;
}
}
}
int main() {
int n;
cin >> n;
pls = vector<signed long long>(n, 0);
adj = vector<list<int> >(n, list<int>());
visited = vector<bool>(n, false);
max_pls = vector<signed long long>(n, -0x1FFFFFFFF);
max_pls2 = vector<signed long long>(n, -0x1FFFFFFFF);
tot_pls = vector<signed long long>(n, -0x1FFFFFFFF);
parent = vector<int>(n, 0);
for (int i = 0; i < n; ++i) cin >> pls[i];
for (int i = 0; i < n - 1; ++i) {
int v, w;
cin >> v >> w;
v--;
w--;
adj[v].push_back(w);
adj[w].push_back(v);
}
bool possible = adj[0].size() > 1;
for (int i = 0; i < n; ++i) {
if (adj[i].size() > 2) {
possible = true;
break;
}
}
if (!possible) {
cout << "Impossible" << endl;
return 0;
}
dfs_pls(0);
signed long long maxxx = -0x1FFFFFFFF;
for (int i = 0; i < n; ++i) {
if (max_pls[i] == -0x1FFFFFFFF || max_pls2[i] == -0x1FFFFFFFF) continue;
maxxx = max(maxxx, max_pls[i] + max_pls2[i]);
}
cout << maxxx << endl;
return 0;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<signed long long> pls;
vector<list<int> > adj;
vector<bool> visited;
vector<signed long long> max_pls, max_pls2;
vector<signed long long> tot_pls;
vector<int> parent;
void dfs_pls(int i) {
visited[i] = true;
for (auto j : adj[i]) {
if (visited[j]) continue;
parent[j] = i;
dfs_pls(j);
}
signed long long tpls = pls[i];
for (auto j : adj[i]) {
if (parent[i] == j) continue;
tpls += tot_pls[j];
}
tot_pls[i] = tpls;
for (auto j : adj[i]) {
if (parent[i] == j) continue;
signed long long cand = max(max_pls[j], tot_pls[j]);
if (cand > max_pls[i]) {
max_pls2[i] = max_pls[i];
max_pls[i] = cand;
} else if (cand > max_pls2[i]) {
max_pls2[i] = cand;
}
}
}
int main() {
int n;
cin >> n;
pls = vector<signed long long>(n, 0);
adj = vector<list<int> >(n, list<int>());
visited = vector<bool>(n, false);
max_pls = vector<signed long long>(n, -0x1FFFFFFFF);
max_pls2 = vector<signed long long>(n, -0x1FFFFFFFF);
tot_pls = vector<signed long long>(n, -0x1FFFFFFFF);
parent = vector<int>(n, 0);
for (int i = 0; i < n; ++i) cin >> pls[i];
for (int i = 0; i < n - 1; ++i) {
int v, w;
cin >> v >> w;
v--;
w--;
adj[v].push_back(w);
adj[w].push_back(v);
}
bool possible = adj[0].size() > 1;
for (int i = 0; i < n; ++i) {
if (adj[i].size() > 2) {
possible = true;
break;
}
}
if (!possible) {
cout << "Impossible" << endl;
return 0;
}
dfs_pls(0);
signed long long maxxx = -0x1FFFFFFFF;
for (int i = 0; i < n; ++i) {
if (max_pls[i] == -0x1FFFFFFFF || max_pls2[i] == -0x1FFFFFFFF) continue;
maxxx = max(maxxx, max_pls[i] + max_pls2[i]);
}
cout << maxxx << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
vector<int> adj[210000];
vector<long long> a(210000), sum(210000), mx(210000);
int n;
long long ans = -inf;
void dfs(int u, int par) {
vector<long long> tmp;
sum[u] = a[u];
mx[u] = -inf;
for (auto v : adj[u])
if (v != par) {
dfs(v, u);
sum[u] += sum[v], mx[u] = max(mx[u], mx[v]);
tmp.push_back(mx[v]);
sort(tmp.begin(), tmp.end(), greater<long long>());
if (tmp.size() > 2) tmp.pop_back();
}
if (tmp.size() >= 2) ans = max(ans, tmp[0] + tmp[1]);
mx[u] = max(mx[u], sum[u]);
}
int main(int argc, char const *argv[]) {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v), adj[v].push_back(u);
}
dfs(0, -1);
cout << (ans > -inf ? to_string(ans) : "Impossible") << endl;
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
vector<int> adj[210000];
vector<long long> a(210000), sum(210000), mx(210000);
int n;
long long ans = -inf;
void dfs(int u, int par) {
vector<long long> tmp;
sum[u] = a[u];
mx[u] = -inf;
for (auto v : adj[u])
if (v != par) {
dfs(v, u);
sum[u] += sum[v], mx[u] = max(mx[u], mx[v]);
tmp.push_back(mx[v]);
sort(tmp.begin(), tmp.end(), greater<long long>());
if (tmp.size() > 2) tmp.pop_back();
}
if (tmp.size() >= 2) ans = max(ans, tmp[0] + tmp[1]);
mx[u] = max(mx[u], sum[u]);
}
int main(int argc, char const *argv[]) {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v), adj[v].push_back(u);
}
dfs(0, -1);
cout << (ans > -inf ? to_string(ans) : "Impossible") << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline bool eq(double a, double b) { return fabs(a - b) < 1e-9; }
const long long int inf = 1e15;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int& n, int b) { n |= two(b); }
inline void unset_bit(int& n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) {
int res = 0;
while (n && ++res) n -= n & (-n);
return res;
}
int n;
vector<vector<int> > tree(200002);
vector<long long int> p(200002);
pair<long long int, pair<long long int, long long int> > dp[200002];
pair<pair<long long int, long long int>, pair<long long int, long long int> >
dfs(int c, int pp) {
long long int sum = p[c];
pair<pair<long long int, long long int>, pair<long long int, long long int> >
ans({{-inf, -inf}, {-inf, -inf}});
for (int x : tree[c]) {
if (x != pp) {
pair<pair<long long int, long long int>,
pair<long long int, long long int> >
cans = dfs(x, c);
sum += cans.first.first;
if (ans.first.first == -inf)
ans = cans;
else {
pair<long long int, pair<long long int, long long int> > opt[3];
opt[0] = {ans.first.second + cans.first.second,
{ans.first.second, cans.first.second}};
opt[1] = {ans.second.first + ans.second.second,
{ans.second.first, ans.second.second}};
opt[2] = {cans.second.first + cans.second.second,
{cans.second.first, cans.second.second}};
sort(opt, opt + 3);
ans.second = opt[2].second;
if (ans.second.first < ans.second.second)
swap(ans.second.first, ans.second.second);
ans.first.second = max(ans.first.second, cans.first.second);
}
}
}
ans.first.first = sum;
if (ans.first.first == -inf)
ans.first.second = sum;
else
ans.first.second = max(ans.first.second, ans.first.first);
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = (1); i <= (n); i++) cin >> p[i];
for (int i = 0; i < (n - 1); i++) {
int a, b;
cin >> a >> b;
tree[a].push_back(b);
tree[b].push_back(a);
}
pair<pair<long long int, long long int>, pair<long long int, long long int> >
ans = dfs(1, -1);
long long int sum = ans.second.first + ans.second.second;
if (sum < -inf) {
cout << "Impossible" << endl;
return 0;
}
cout << sum << endl;
return 0;
}
|
### Prompt
Create a solution in cpp for the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline bool eq(double a, double b) { return fabs(a - b) < 1e-9; }
const long long int inf = 1e15;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int& n, int b) { n |= two(b); }
inline void unset_bit(int& n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) {
int res = 0;
while (n && ++res) n -= n & (-n);
return res;
}
int n;
vector<vector<int> > tree(200002);
vector<long long int> p(200002);
pair<long long int, pair<long long int, long long int> > dp[200002];
pair<pair<long long int, long long int>, pair<long long int, long long int> >
dfs(int c, int pp) {
long long int sum = p[c];
pair<pair<long long int, long long int>, pair<long long int, long long int> >
ans({{-inf, -inf}, {-inf, -inf}});
for (int x : tree[c]) {
if (x != pp) {
pair<pair<long long int, long long int>,
pair<long long int, long long int> >
cans = dfs(x, c);
sum += cans.first.first;
if (ans.first.first == -inf)
ans = cans;
else {
pair<long long int, pair<long long int, long long int> > opt[3];
opt[0] = {ans.first.second + cans.first.second,
{ans.first.second, cans.first.second}};
opt[1] = {ans.second.first + ans.second.second,
{ans.second.first, ans.second.second}};
opt[2] = {cans.second.first + cans.second.second,
{cans.second.first, cans.second.second}};
sort(opt, opt + 3);
ans.second = opt[2].second;
if (ans.second.first < ans.second.second)
swap(ans.second.first, ans.second.second);
ans.first.second = max(ans.first.second, cans.first.second);
}
}
}
ans.first.first = sum;
if (ans.first.first == -inf)
ans.first.second = sum;
else
ans.first.second = max(ans.first.second, ans.first.first);
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = (1); i <= (n); i++) cin >> p[i];
for (int i = 0; i < (n - 1); i++) {
int a, b;
cin >> a >> b;
tree[a].push_back(b);
tree[b].push_back(a);
}
pair<pair<long long int, long long int>, pair<long long int, long long int> >
ans = dfs(1, -1);
long long int sum = ans.second.first + ans.second.second;
if (sum < -inf) {
cout << "Impossible" << endl;
return 0;
}
cout << sum << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
pair<long long, long long> best[200010];
vector<int> arr[200010];
long long subtree[200010];
int val[200010];
int n;
long long maxans = -1e18;
void dfs(int s, int parent) {
subtree[s] = val[s];
long long maxx = -1e18, secmaxx = -1e18;
for (int i = 0; i < arr[s].size(); ++i) {
int to = arr[s][i];
if (to != parent) {
dfs(to, s);
if (max(subtree[to], best[to].first) > maxx) {
secmaxx = maxx;
maxx = max(subtree[to], best[to].first);
} else if (max(subtree[to], best[to].first) > secmaxx)
secmaxx = max(subtree[to], best[to].first);
subtree[s] += subtree[to];
}
}
best[s].first = maxx;
best[s].second = secmaxx;
maxans = max(maxans, best[s].first + best[s].second);
}
void dfs2(int s, int parent) {}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &val[i]);
int u, v;
for (int i = 1; i < n; ++i) {
scanf("%d", &u);
scanf("%d", &v);
arr[u].push_back(v);
arr[v].push_back(u);
}
dfs(1, -1);
if (maxans < -1e17)
printf("Impossible\n");
else
printf("%lld\n", maxans);
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
pair<long long, long long> best[200010];
vector<int> arr[200010];
long long subtree[200010];
int val[200010];
int n;
long long maxans = -1e18;
void dfs(int s, int parent) {
subtree[s] = val[s];
long long maxx = -1e18, secmaxx = -1e18;
for (int i = 0; i < arr[s].size(); ++i) {
int to = arr[s][i];
if (to != parent) {
dfs(to, s);
if (max(subtree[to], best[to].first) > maxx) {
secmaxx = maxx;
maxx = max(subtree[to], best[to].first);
} else if (max(subtree[to], best[to].first) > secmaxx)
secmaxx = max(subtree[to], best[to].first);
subtree[s] += subtree[to];
}
}
best[s].first = maxx;
best[s].second = secmaxx;
maxans = max(maxans, best[s].first + best[s].second);
}
void dfs2(int s, int parent) {}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &val[i]);
int u, v;
for (int i = 1; i < n; ++i) {
scanf("%d", &u);
scanf("%d", &v);
arr[u].push_back(v);
arr[v].push_back(u);
}
dfs(1, -1);
if (maxans < -1e17)
printf("Impossible\n");
else
printf("%lld\n", maxans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
bool desc_sort(const long long& lhs, const long long& rhs) { return lhs > rhs; }
bool desc_pii_sort(const pair<int, int>& lhs, const pair<int, int>& rhs) {
return lhs > rhs;
}
bool second_sort(const pair<int, int>& lhs, const pair<int, int>& rhs) {
pair<int, int> a, b;
a.first = lhs.second;
a.second = lhs.first;
b.first = rhs.second;
b.second = rhs.first;
return a < b;
}
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
os << "[ ";
for (auto v : V) os << v << " ";
return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, set<T> second) {
os << "{ ";
for (auto s : second) os << s << " ";
return os << "}";
}
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R>
ostream& operator<<(ostream& os, map<L, R> M) {
os << "{ ";
for (auto m : M) os << "(" << m.first << ":" << m.second << ") ";
return os << "}";
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
inline long long fast_expo(long long base, long long power,
long long modulo = 1e0) {
base %= modulo;
if (base < 0LL) base += modulo;
long long x = base, cnt = power, ans = 1LL;
while (cnt) {
if (cnt & 1LL) ans = (ans * x) % modulo;
x = (x * x) % modulo;
cnt >>= 1LL;
}
return ans;
}
inline long long inv(long long base, long long modulo = 1e0) {
return fast_expo(base, modulo - 2LL, modulo);
}
long long a[200001], si[200001], b[200001];
bool pos = 0;
long long ans = -1000000000000000;
vector<int> adj[200001];
void dfs(int r, int p) {
b[r] = -1000000000000000;
vector<long long> temp;
si[r] = a[r];
for (auto x : adj[r]) {
if (x != p) {
dfs(x, r);
si[r] += si[x];
b[r] = max(b[r], b[x]);
temp.push_back(b[x]);
}
}
b[r] = max(b[r], si[r]);
if ((temp).size() > 1) {
pos = 1;
sort((temp).begin(), (temp).end(), desc_sort);
ans = max(ans, temp[0] + temp[1]);
}
}
signed 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 >> a[i];
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 0);
if (pos)
cout << ans << "\n";
else
cout << "Impossible\n";
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool desc_sort(const long long& lhs, const long long& rhs) { return lhs > rhs; }
bool desc_pii_sort(const pair<int, int>& lhs, const pair<int, int>& rhs) {
return lhs > rhs;
}
bool second_sort(const pair<int, int>& lhs, const pair<int, int>& rhs) {
pair<int, int> a, b;
a.first = lhs.second;
a.second = lhs.first;
b.first = rhs.second;
b.second = rhs.first;
return a < b;
}
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
os << "[ ";
for (auto v : V) os << v << " ";
return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, set<T> second) {
os << "{ ";
for (auto s : second) os << s << " ";
return os << "}";
}
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R>
ostream& operator<<(ostream& os, map<L, R> M) {
os << "{ ";
for (auto m : M) os << "(" << m.first << ":" << m.second << ") ";
return os << "}";
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
inline long long fast_expo(long long base, long long power,
long long modulo = 1e0) {
base %= modulo;
if (base < 0LL) base += modulo;
long long x = base, cnt = power, ans = 1LL;
while (cnt) {
if (cnt & 1LL) ans = (ans * x) % modulo;
x = (x * x) % modulo;
cnt >>= 1LL;
}
return ans;
}
inline long long inv(long long base, long long modulo = 1e0) {
return fast_expo(base, modulo - 2LL, modulo);
}
long long a[200001], si[200001], b[200001];
bool pos = 0;
long long ans = -1000000000000000;
vector<int> adj[200001];
void dfs(int r, int p) {
b[r] = -1000000000000000;
vector<long long> temp;
si[r] = a[r];
for (auto x : adj[r]) {
if (x != p) {
dfs(x, r);
si[r] += si[x];
b[r] = max(b[r], b[x]);
temp.push_back(b[x]);
}
}
b[r] = max(b[r], si[r]);
if ((temp).size() > 1) {
pos = 1;
sort((temp).begin(), (temp).end(), desc_sort);
ans = max(ans, temp[0] + temp[1]);
}
}
signed 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 >> a[i];
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 0);
if (pos)
cout << ans << "\n";
else
cout << "Impossible\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int max_n = 200005;
const long long inf = 1e18 + 8;
int d[max_n];
int n;
long long sum[max_n];
long long maxsum[max_n];
vector<int> v[max_n];
bool isbambuk = true;
long long ans = -inf;
void dfs(int cur, int pr) {
if (v[cur].size() == 1 && v[cur][0] == pr) {
sum[cur] = d[cur];
maxsum[cur] = d[cur];
return;
}
sum[cur] = d[cur];
maxsum[cur] = -inf;
for (int i = 0; i < v[cur].size(); i++) {
if (v[cur][i] == pr) continue;
dfs(v[cur][i], cur);
sum[cur] += sum[v[cur][i]];
maxsum[cur] = max(maxsum[cur], maxsum[v[cur][i]]);
}
maxsum[cur] = max(maxsum[cur], sum[cur]);
}
void dfs2(int cur, int pr) {
for (int i = 0; i < v[cur].size(); i++) {
if (v[cur][i] == pr) continue;
dfs2(v[cur][i], cur);
}
if (cur == 0) {
if (v[cur].size() <= 1) {
return;
}
}
if (cur && v[cur].size() <= 2) {
return;
}
isbambuk = false;
long long maxs1 = -inf, maxs2 = -inf;
for (int i = 0; i < v[cur].size(); i++) {
int to = v[cur][i];
if (to == pr) continue;
if (maxsum[to] >= maxs1) {
maxs2 = maxs1;
maxs1 = maxsum[to];
continue;
}
if (maxsum[to] >= maxs2) {
maxs2 = maxsum[to];
}
}
ans = max(ans, maxs2 + maxs1);
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", d + i);
}
int a, b;
for (int i = 0; i < n - 1; i++) {
scanf("%d%d", &a, &b);
a--;
b--;
v[a].push_back(b);
v[b].push_back(a);
}
dfs(0, -1);
dfs2(0, -1);
if (isbambuk) {
cout << "Impossible";
} else {
cout << ans;
}
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int max_n = 200005;
const long long inf = 1e18 + 8;
int d[max_n];
int n;
long long sum[max_n];
long long maxsum[max_n];
vector<int> v[max_n];
bool isbambuk = true;
long long ans = -inf;
void dfs(int cur, int pr) {
if (v[cur].size() == 1 && v[cur][0] == pr) {
sum[cur] = d[cur];
maxsum[cur] = d[cur];
return;
}
sum[cur] = d[cur];
maxsum[cur] = -inf;
for (int i = 0; i < v[cur].size(); i++) {
if (v[cur][i] == pr) continue;
dfs(v[cur][i], cur);
sum[cur] += sum[v[cur][i]];
maxsum[cur] = max(maxsum[cur], maxsum[v[cur][i]]);
}
maxsum[cur] = max(maxsum[cur], sum[cur]);
}
void dfs2(int cur, int pr) {
for (int i = 0; i < v[cur].size(); i++) {
if (v[cur][i] == pr) continue;
dfs2(v[cur][i], cur);
}
if (cur == 0) {
if (v[cur].size() <= 1) {
return;
}
}
if (cur && v[cur].size() <= 2) {
return;
}
isbambuk = false;
long long maxs1 = -inf, maxs2 = -inf;
for (int i = 0; i < v[cur].size(); i++) {
int to = v[cur][i];
if (to == pr) continue;
if (maxsum[to] >= maxs1) {
maxs2 = maxs1;
maxs1 = maxsum[to];
continue;
}
if (maxsum[to] >= maxs2) {
maxs2 = maxsum[to];
}
}
ans = max(ans, maxs2 + maxs1);
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", d + i);
}
int a, b;
for (int i = 0; i < n - 1; i++) {
scanf("%d%d", &a, &b);
a--;
b--;
v[a].push_back(b);
v[b].push_back(a);
}
dfs(0, -1);
dfs2(0, -1);
if (isbambuk) {
cout << "Impossible";
} else {
cout << ans;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int oo = (int)1e9;
const double PI = 2 * acos(0.0);
const double eps = 1e-9;
const int MAXN = 1e5 + 10;
int mod = 1e9 + 7;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int a[3000005];
vector<int> adj[400000];
long long best[400000];
long long sum[400000];
long long ans = -1e16;
long long dfs1(int node = 0, int par = -1) {
sum[node] = a[node];
for (auto i : adj[node])
if (i != par) sum[node] += dfs1(i, node);
return sum[node];
}
pair<long long, long long> fun(int node = 0, int par = -1) {
vector<long long> v(4, -1e16);
for (auto i : adj[node])
if (i != par) {
pair<long long, long long> ret = fun(i, node);
v[0] = ret.first;
v[1] = ret.second;
sort(v.begin(), v.end());
}
ans = max(ans, v[2] + v[3]);
if (sum[node] > v[3]) v[3] = sum[node], v[2] = -1e16;
return {v[2], v[3]};
}
int main() {
std::ios_base::sync_with_stdio(false);
int n, x, y;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
cin >> x >> y;
x--, y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs1();
fun();
if (ans < -1e14)
cout << "Impossible" << endl;
else
cout << ans << endl;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int oo = (int)1e9;
const double PI = 2 * acos(0.0);
const double eps = 1e-9;
const int MAXN = 1e5 + 10;
int mod = 1e9 + 7;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int a[3000005];
vector<int> adj[400000];
long long best[400000];
long long sum[400000];
long long ans = -1e16;
long long dfs1(int node = 0, int par = -1) {
sum[node] = a[node];
for (auto i : adj[node])
if (i != par) sum[node] += dfs1(i, node);
return sum[node];
}
pair<long long, long long> fun(int node = 0, int par = -1) {
vector<long long> v(4, -1e16);
for (auto i : adj[node])
if (i != par) {
pair<long long, long long> ret = fun(i, node);
v[0] = ret.first;
v[1] = ret.second;
sort(v.begin(), v.end());
}
ans = max(ans, v[2] + v[3]);
if (sum[node] > v[3]) v[3] = sum[node], v[2] = -1e16;
return {v[2], v[3]};
}
int main() {
std::ios_base::sync_with_stdio(false);
int n, x, y;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
cin >> x >> y;
x--, y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs1();
fun();
if (ans < -1e14)
cout << "Impossible" << endl;
else
cout << ans << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MX = 123;
const int MOD = int(360);
const long long oo = LLONG_MAX;
pair<long long, long long> go(vector<vector<int> >& graph, vector<long long>& a,
int p, int u, bool& can, long long& ans) {
long long sum = a[u];
long long best0 = -oo, best1 = -oo;
int cnt = 0;
for (auto& v : graph[u]) {
if (v != p) {
cnt++;
pair<long long, long long> cur = go(graph, a, u, v, can, ans);
sum += cur.first;
long long best = max(cur.first, cur.second);
if (best >= best0) {
best1 = best0;
best0 = best;
} else if (best >= best1) {
best1 = best;
}
}
}
if (cnt == 0) {
return {a[u], a[u]};
}
if (best0 != -oo && best1 != -oo) {
can = true;
ans = max(ans, best0 + best1);
}
return {sum, best0};
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
while (cin >> n) {
vector<vector<int> > graph(n);
vector<long long> a(n);
for (auto& e : a) {
cin >> e;
}
for (int i = 0; i + 1 < n; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
bool can = false;
long long ans = -oo;
go(graph, a, -1, 0, can, ans);
if (can) {
cout << ans << "\n";
} else {
cout << "Impossible\n";
}
}
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MX = 123;
const int MOD = int(360);
const long long oo = LLONG_MAX;
pair<long long, long long> go(vector<vector<int> >& graph, vector<long long>& a,
int p, int u, bool& can, long long& ans) {
long long sum = a[u];
long long best0 = -oo, best1 = -oo;
int cnt = 0;
for (auto& v : graph[u]) {
if (v != p) {
cnt++;
pair<long long, long long> cur = go(graph, a, u, v, can, ans);
sum += cur.first;
long long best = max(cur.first, cur.second);
if (best >= best0) {
best1 = best0;
best0 = best;
} else if (best >= best1) {
best1 = best;
}
}
}
if (cnt == 0) {
return {a[u], a[u]};
}
if (best0 != -oo && best1 != -oo) {
can = true;
ans = max(ans, best0 + best1);
}
return {sum, best0};
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
while (cin >> n) {
vector<vector<int> > graph(n);
vector<long long> a(n);
for (auto& e : a) {
cin >> e;
}
for (int i = 0; i + 1 < n; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
bool can = false;
long long ans = -oo;
go(graph, a, -1, 0, can, ans);
if (can) {
cout << ans << "\n";
} else {
cout << "Impossible\n";
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 22;
const long long INF = 2e18 + 2;
int n;
long long res = -INF * 2;
long long a[MAXN], mx[MAXN], sum[MAXN];
vector<int> G[MAXN];
void dfs(int v, int par) {
sum[v] = a[v];
for (int i = 0; i < G[v].size(); i++) {
int u = G[v][i];
if (u == par) continue;
dfs(u, v);
sum[v] += sum[u];
mx[v] = max(mx[v], mx[u]);
}
mx[v] = max(mx[v], sum[v]);
}
void dfs2(int v, int par) {
vector<long long> vec;
for (int i = 0; i < G[v].size(); i++) {
int u = G[v][i];
if (u == par) continue;
dfs2(u, v);
vec.push_back(mx[u]);
if (vec.size() > 2) {
sort(vec.rbegin(), vec.rend());
vec.pop_back();
}
}
if (vec.size() < 2) return;
res = max(res, vec[0] + vec[1]);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n; i++) mx[i] = -INF;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0, x, y; i < n - 1; i++) {
cin >> x >> y;
x--, y--;
G[x].push_back(y);
G[y].push_back(x);
}
dfs(0, -1);
dfs2(0, -1);
if (res != -INF * 2)
cout << res;
else
cout << "Impossible";
return 0;
}
|
### Prompt
Create a solution in cpp for the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 22;
const long long INF = 2e18 + 2;
int n;
long long res = -INF * 2;
long long a[MAXN], mx[MAXN], sum[MAXN];
vector<int> G[MAXN];
void dfs(int v, int par) {
sum[v] = a[v];
for (int i = 0; i < G[v].size(); i++) {
int u = G[v][i];
if (u == par) continue;
dfs(u, v);
sum[v] += sum[u];
mx[v] = max(mx[v], mx[u]);
}
mx[v] = max(mx[v], sum[v]);
}
void dfs2(int v, int par) {
vector<long long> vec;
for (int i = 0; i < G[v].size(); i++) {
int u = G[v][i];
if (u == par) continue;
dfs2(u, v);
vec.push_back(mx[u]);
if (vec.size() > 2) {
sort(vec.rbegin(), vec.rend());
vec.pop_back();
}
}
if (vec.size() < 2) return;
res = max(res, vec[0] + vec[1]);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n; i++) mx[i] = -INF;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0, x, y; i < n - 1; i++) {
cin >> x >> y;
x--, y--;
G[x].push_back(y);
G[y].push_back(x);
}
dfs(0, -1);
dfs2(0, -1);
if (res != -INF * 2)
cout << res;
else
cout << "Impossible";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
long long dp[200010], v[200010], sum[200010];
int head[200010], to[200010 * 2], pre[200010 * 2];
int n, e;
long long ans;
void addedge(int x, int y) {
to[e] = y;
pre[e] = head[x];
head[x] = e++;
}
void dfs(int x, int pa) {
sum[x] = v[x];
for (int i = head[x]; i != -1; i = pre[i]) {
if (to[i] == pa) continue;
dfs(to[i], x);
if (dp[x] != -inf) ans = max(ans, dp[x] + dp[to[i]]);
sum[x] += sum[to[i]];
dp[x] = max(dp[x], dp[to[i]]);
}
dp[x] = max(dp[x], sum[x]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%I64d", &v[i]);
memset(head, -1, sizeof(head));
e = 0;
int x, y;
for (int i = 1; i < n; ++i) {
scanf("%d%d", &x, &y);
addedge(x, y);
addedge(y, x);
}
ans = -inf;
for (int i = 1; i <= n; ++i) dp[i] = -inf;
memset(sum, 0, sizeof(sum));
dfs(1, 0);
if (ans == -inf)
printf("Impossible");
else
printf("%I64d\n", ans);
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
long long dp[200010], v[200010], sum[200010];
int head[200010], to[200010 * 2], pre[200010 * 2];
int n, e;
long long ans;
void addedge(int x, int y) {
to[e] = y;
pre[e] = head[x];
head[x] = e++;
}
void dfs(int x, int pa) {
sum[x] = v[x];
for (int i = head[x]; i != -1; i = pre[i]) {
if (to[i] == pa) continue;
dfs(to[i], x);
if (dp[x] != -inf) ans = max(ans, dp[x] + dp[to[i]]);
sum[x] += sum[to[i]];
dp[x] = max(dp[x], dp[to[i]]);
}
dp[x] = max(dp[x], sum[x]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%I64d", &v[i]);
memset(head, -1, sizeof(head));
e = 0;
int x, y;
for (int i = 1; i < n; ++i) {
scanf("%d%d", &x, &y);
addedge(x, y);
addedge(y, x);
}
ans = -inf;
for (int i = 1; i <= n; ++i) dp[i] = -inf;
memset(sum, 0, sizeof(sum));
dfs(1, 0);
if (ans == -inf)
printf("Impossible");
else
printf("%I64d\n", ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200000;
const long long LINF = 1LL << 62;
int as[MAX_N], ps[MAX_N], cns[MAX_N];
vector<int> nbrs[MAX_N];
long long ss[MAX_N], dp0[MAX_N], dp1[MAX_N];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", as + i);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
nbrs[u].push_back(v);
nbrs[v].push_back(u);
}
ps[0] = -1;
queue<int> q;
q.push(0);
while (!q.empty()) {
int u = q.front();
q.pop();
int up = ps[u];
vector<int> &nbru = nbrs[u];
for (vector<int>::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
int v = *vit;
if (v != up) {
ps[v] = u;
cns[u]++;
q.push(v);
}
}
}
fill(dp0, dp0 + n, -LINF);
fill(dp1, dp1 + n, -LINF);
long long maxsum = -LINF;
for (int u = 0; u < n; u++)
if (cns[u] == 0) q.push(u);
while (!q.empty()) {
int u = q.front();
q.pop();
if (dp1[u] > -LINF) {
long long sum = dp0[u] + dp1[u];
if (maxsum < sum) maxsum = sum;
}
ss[u] += as[u];
int up = ps[u];
if (up >= 0) {
ss[up] += ss[u];
long long d = max(ss[u], dp0[u]);
if (dp0[up] < d)
dp1[up] = dp0[up], dp0[up] = d;
else if (dp1[up] < d)
dp1[up] = d;
if (--cns[up] == 0) q.push(up);
}
}
if (maxsum <= -LINF)
puts("Impossible");
else
printf("%lld\n", maxsum);
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200000;
const long long LINF = 1LL << 62;
int as[MAX_N], ps[MAX_N], cns[MAX_N];
vector<int> nbrs[MAX_N];
long long ss[MAX_N], dp0[MAX_N], dp1[MAX_N];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", as + i);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
nbrs[u].push_back(v);
nbrs[v].push_back(u);
}
ps[0] = -1;
queue<int> q;
q.push(0);
while (!q.empty()) {
int u = q.front();
q.pop();
int up = ps[u];
vector<int> &nbru = nbrs[u];
for (vector<int>::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
int v = *vit;
if (v != up) {
ps[v] = u;
cns[u]++;
q.push(v);
}
}
}
fill(dp0, dp0 + n, -LINF);
fill(dp1, dp1 + n, -LINF);
long long maxsum = -LINF;
for (int u = 0; u < n; u++)
if (cns[u] == 0) q.push(u);
while (!q.empty()) {
int u = q.front();
q.pop();
if (dp1[u] > -LINF) {
long long sum = dp0[u] + dp1[u];
if (maxsum < sum) maxsum = sum;
}
ss[u] += as[u];
int up = ps[u];
if (up >= 0) {
ss[up] += ss[u];
long long d = max(ss[u], dp0[u]);
if (dp0[up] < d)
dp1[up] = dp0[up], dp0[up] = d;
else if (dp1[up] < d)
dp1[up] = d;
if (--cns[up] == 0) q.push(up);
}
}
if (maxsum <= -LINF)
puts("Impossible");
else
printf("%lld\n", maxsum);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1000000000000000000;
long long mod = 1000000000;
long double pi = 3.141592653589793238;
void pls() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
long long mul_mod(long long a, long long b, long long m) {
long long x = 0, y = a;
while (b > 0) {
if (b & 1) {
x = (x + y) % m;
}
y = (y + y) % m;
b >>= 1;
}
return x;
}
inline long long modu(long long val, long long modul) {
return (val % modul + modul) % modul;
}
inline int madd(int x, int y) { return (x % mod + y % mod) % mod; }
inline int msub(int x, int y) { return (mod + x % mod - y % mod) % mod; }
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long powmod(long long a, long long mod, long long p) {
if (p == 0) return 1;
long long res;
long long x = powmod(a, mod, p / 2);
res = x;
if (p & 1) x = mul_mod(x, a, mod);
return mul_mod(x, res, mod);
}
long long exp(long long a, long long p) {
if (p == 0) return 1;
long long res;
long long x = exp(a, p / 2);
res = x;
if (p & 1) x = (x * a);
return (x * res);
}
long long sqt(long long x) {
long long l = 1LL;
long long r = x;
long long res;
while (l <= r) {
long long mid = l + (r - l) / 2;
if (mid * mid <= x) {
res = mid;
l = mid + 1;
} else
r = mid - 1;
}
return res;
}
bool isprime(long long n) {
for (int i = 2; i <= sqt(n); i++)
if (n % i == 0) return 0;
return 1;
}
vector<int> soe(long long upto) {
vector<bool> prime(upto + 1, 0);
prime[1] = 1;
prime[0] = 1;
for (int i = 2; i <= sqt(upto); i++) {
if (!prime[i]) {
for (int j = 2 * i; j <= upto; j += i) prime[j] = 1;
}
}
vector<int> res;
res.push_back(2);
for (int i = 3; i < prime.size(); i += 2) {
if (!prime[i]) res.push_back(i);
}
return res;
}
int di[8] = {-1, -1, -1, 1, 1, 1, 0, 0};
int dj[8] = {-1, 0, 1, -1, 0, 1, -1, 1};
int ii[4] = {0, 0, -1, 1};
int jj[4] = {-1, 1, 0, 0};
long long n, d, a, b, u, v;
long long val[200001];
vector<long long> g[200001];
struct node {
long long mx1, mx2, submax, sum;
};
node dfs(int i, int p = -1) {
node temp;
temp.mx1 = -MAX;
temp.mx2 = -MAX;
temp.submax = -MAX;
temp.sum = val[i];
pair<long long, long long> toptwo{-MAX, -MAX};
for (auto x : g[i]) {
if (x == p) continue;
node curr = dfs(x, i);
temp.submax = max(temp.submax, curr.submax);
temp.sum += curr.sum;
if ((curr.mx1 + curr.mx2) > (temp.mx1 + temp.mx2)) {
temp.mx1 = curr.mx1;
temp.mx2 = curr.mx2;
}
if (curr.submax > toptwo.first) {
toptwo.second = toptwo.first;
toptwo.first = curr.submax;
} else if (curr.submax > toptwo.second) {
toptwo.second = curr.submax;
}
}
temp.submax = max(temp.submax, temp.sum);
if (toptwo.first + toptwo.second > temp.mx1 + temp.mx2) {
temp.mx1 = toptwo.first;
temp.mx2 = toptwo.second;
}
return temp;
}
void solve() {
cin >> n;
for (int i = 0; i < n; i++) g[i].clear();
for (int i = 0; i < n; i++) cin >> val[i];
for (int i = 0; i < n - 1; i++) {
long long u, v;
cin >> u >> v;
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
node temp = dfs(0);
long long res = INT_MIN;
bool ok = true;
if (temp.mx1 != -MAX && temp.mx2 != -MAX) {
ok = false;
res = temp.mx1 + temp.mx2;
}
if (ok) {
cout << "Impossible" << endl;
} else
cout << res << endl;
}
int main() {
pls();
solve();
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1000000000000000000;
long long mod = 1000000000;
long double pi = 3.141592653589793238;
void pls() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
long long mul_mod(long long a, long long b, long long m) {
long long x = 0, y = a;
while (b > 0) {
if (b & 1) {
x = (x + y) % m;
}
y = (y + y) % m;
b >>= 1;
}
return x;
}
inline long long modu(long long val, long long modul) {
return (val % modul + modul) % modul;
}
inline int madd(int x, int y) { return (x % mod + y % mod) % mod; }
inline int msub(int x, int y) { return (mod + x % mod - y % mod) % mod; }
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long powmod(long long a, long long mod, long long p) {
if (p == 0) return 1;
long long res;
long long x = powmod(a, mod, p / 2);
res = x;
if (p & 1) x = mul_mod(x, a, mod);
return mul_mod(x, res, mod);
}
long long exp(long long a, long long p) {
if (p == 0) return 1;
long long res;
long long x = exp(a, p / 2);
res = x;
if (p & 1) x = (x * a);
return (x * res);
}
long long sqt(long long x) {
long long l = 1LL;
long long r = x;
long long res;
while (l <= r) {
long long mid = l + (r - l) / 2;
if (mid * mid <= x) {
res = mid;
l = mid + 1;
} else
r = mid - 1;
}
return res;
}
bool isprime(long long n) {
for (int i = 2; i <= sqt(n); i++)
if (n % i == 0) return 0;
return 1;
}
vector<int> soe(long long upto) {
vector<bool> prime(upto + 1, 0);
prime[1] = 1;
prime[0] = 1;
for (int i = 2; i <= sqt(upto); i++) {
if (!prime[i]) {
for (int j = 2 * i; j <= upto; j += i) prime[j] = 1;
}
}
vector<int> res;
res.push_back(2);
for (int i = 3; i < prime.size(); i += 2) {
if (!prime[i]) res.push_back(i);
}
return res;
}
int di[8] = {-1, -1, -1, 1, 1, 1, 0, 0};
int dj[8] = {-1, 0, 1, -1, 0, 1, -1, 1};
int ii[4] = {0, 0, -1, 1};
int jj[4] = {-1, 1, 0, 0};
long long n, d, a, b, u, v;
long long val[200001];
vector<long long> g[200001];
struct node {
long long mx1, mx2, submax, sum;
};
node dfs(int i, int p = -1) {
node temp;
temp.mx1 = -MAX;
temp.mx2 = -MAX;
temp.submax = -MAX;
temp.sum = val[i];
pair<long long, long long> toptwo{-MAX, -MAX};
for (auto x : g[i]) {
if (x == p) continue;
node curr = dfs(x, i);
temp.submax = max(temp.submax, curr.submax);
temp.sum += curr.sum;
if ((curr.mx1 + curr.mx2) > (temp.mx1 + temp.mx2)) {
temp.mx1 = curr.mx1;
temp.mx2 = curr.mx2;
}
if (curr.submax > toptwo.first) {
toptwo.second = toptwo.first;
toptwo.first = curr.submax;
} else if (curr.submax > toptwo.second) {
toptwo.second = curr.submax;
}
}
temp.submax = max(temp.submax, temp.sum);
if (toptwo.first + toptwo.second > temp.mx1 + temp.mx2) {
temp.mx1 = toptwo.first;
temp.mx2 = toptwo.second;
}
return temp;
}
void solve() {
cin >> n;
for (int i = 0; i < n; i++) g[i].clear();
for (int i = 0; i < n; i++) cin >> val[i];
for (int i = 0; i < n - 1; i++) {
long long u, v;
cin >> u >> v;
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
node temp = dfs(0);
long long res = INT_MIN;
bool ok = true;
if (temp.mx1 != -MAX && temp.mx2 != -MAX) {
ok = false;
res = temp.mx1 + temp.mx2;
}
if (ok) {
cout << "Impossible" << endl;
} else
cout << res << endl;
}
int main() {
pls();
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
static const int max_n = (2e+5) + 1;
static const long long INF = 1e+17;
static const long long INF_M = -INF;
static const long long INF_M_2 = 2 * INF_M;
int pleasant[max_n] = {};
long long pleasant_sub[max_n] = {};
vector<int> conns[max_n];
long long dp1[max_n];
long long dp2[max_n];
long long dp[max_n][3];
int n;
long long dfs_pleasant(int node, int p) {
long long& s = pleasant_sub[node];
if (s == INF_M) {
int len = conns[node].size();
s = pleasant[node];
for (int i = 0; i < len; ++i) {
if (conns[node][i] != p) {
s += dfs_pleasant(conns[node][i], node);
}
}
}
return s;
}
long long dfs(int node, int p, int remain = 2) {
long long& s = dp[node][remain];
if (s == INF_M_2) {
int len = conns[node].size();
if (remain == 2) {
long long max1_1 = INF_M;
long long max1_2 = INF_M;
long long max2 = INF_M;
long long p1;
for (int i = 0; i < len; ++i) {
if (conns[node][i] != p) {
p1 = pleasant_sub[conns[node][i]];
p1 = max(p1, dfs(conns[node][i], node, 1));
max2 = max(max2, dfs(conns[node][i], node, 2));
if (p1 > max1_1) {
max1_2 = max1_1;
max1_1 = p1;
} else if (p1 > max1_2) {
max1_2 = p1;
}
}
}
long long max1;
if (max1_2 == INF_M or max1_1 == INF_M)
max1 = INF_M;
else
max1 = max1_1 + max1_2;
s = max(max1, max2);
} else {
long long max1 = INF_M;
for (int i = 0; i < len; ++i) {
if (conns[node][i] != p) {
max1 = max(max1, pleasant_sub[conns[node][i]]);
max1 = max(max1, dfs(conns[node][i], node, 1));
}
}
s = max1;
}
}
return s;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 1; i < n + 1; ++i) cin >> pleasant[i];
int x, y;
for (int i = 0; i < n - 1; ++i) {
cin >> x >> y;
conns[x].push_back(y);
conns[y].push_back(x);
}
for (int i = 1; i < n + 1; ++i) {
pleasant_sub[i] = INF_M;
dp[i][1] = INF_M_2;
dp[i][2] = INF_M_2;
}
dfs_pleasant(1, -1);
long long ans = dfs(1, -1);
if (ans == INF_M)
cout << "Impossible" << endl;
else
cout << ans << endl;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
static const int max_n = (2e+5) + 1;
static const long long INF = 1e+17;
static const long long INF_M = -INF;
static const long long INF_M_2 = 2 * INF_M;
int pleasant[max_n] = {};
long long pleasant_sub[max_n] = {};
vector<int> conns[max_n];
long long dp1[max_n];
long long dp2[max_n];
long long dp[max_n][3];
int n;
long long dfs_pleasant(int node, int p) {
long long& s = pleasant_sub[node];
if (s == INF_M) {
int len = conns[node].size();
s = pleasant[node];
for (int i = 0; i < len; ++i) {
if (conns[node][i] != p) {
s += dfs_pleasant(conns[node][i], node);
}
}
}
return s;
}
long long dfs(int node, int p, int remain = 2) {
long long& s = dp[node][remain];
if (s == INF_M_2) {
int len = conns[node].size();
if (remain == 2) {
long long max1_1 = INF_M;
long long max1_2 = INF_M;
long long max2 = INF_M;
long long p1;
for (int i = 0; i < len; ++i) {
if (conns[node][i] != p) {
p1 = pleasant_sub[conns[node][i]];
p1 = max(p1, dfs(conns[node][i], node, 1));
max2 = max(max2, dfs(conns[node][i], node, 2));
if (p1 > max1_1) {
max1_2 = max1_1;
max1_1 = p1;
} else if (p1 > max1_2) {
max1_2 = p1;
}
}
}
long long max1;
if (max1_2 == INF_M or max1_1 == INF_M)
max1 = INF_M;
else
max1 = max1_1 + max1_2;
s = max(max1, max2);
} else {
long long max1 = INF_M;
for (int i = 0; i < len; ++i) {
if (conns[node][i] != p) {
max1 = max(max1, pleasant_sub[conns[node][i]]);
max1 = max(max1, dfs(conns[node][i], node, 1));
}
}
s = max1;
}
}
return s;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 1; i < n + 1; ++i) cin >> pleasant[i];
int x, y;
for (int i = 0; i < n - 1; ++i) {
cin >> x >> y;
conns[x].push_back(y);
conns[y].push_back(x);
}
for (int i = 1; i < n + 1; ++i) {
pleasant_sub[i] = INF_M;
dp[i][1] = INF_M_2;
dp[i][2] = INF_M_2;
}
dfs_pleasant(1, -1);
long long ans = dfs(1, -1);
if (ans == INF_M)
cout << "Impossible" << endl;
else
cout << ans << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 20;
int n, a[N], i, u, v;
vector<int> adj[N];
long long dp[N], sum[N], ans = LLONG_MIN;
void dfs(int node, int p) {
sum[node] = a[node];
for (auto i : adj[node]) {
if (i == p) continue;
dfs(i, node);
sum[node] += sum[i];
if (dp[node] != LLONG_MIN) ans = max(ans, dp[node] + dp[i]);
dp[node] = max(dp[node], dp[i]);
}
dp[node] = max(dp[node], sum[node]);
}
int main() {
for (i = 0; i < N; i++) dp[i] = LLONG_MIN;
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
for (i = 0; i < n - 1; i++) {
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 0);
if (ans != LLONG_MIN)
printf("%lld", ans);
else
printf("Impossible");
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 20;
int n, a[N], i, u, v;
vector<int> adj[N];
long long dp[N], sum[N], ans = LLONG_MIN;
void dfs(int node, int p) {
sum[node] = a[node];
for (auto i : adj[node]) {
if (i == p) continue;
dfs(i, node);
sum[node] += sum[i];
if (dp[node] != LLONG_MIN) ans = max(ans, dp[node] + dp[i]);
dp[node] = max(dp[node], dp[i]);
}
dp[node] = max(dp[node], sum[node]);
}
int main() {
for (i = 0; i < N; i++) dp[i] = LLONG_MIN;
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
for (i = 0; i < n - 1; i++) {
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 0);
if (ans != LLONG_MIN)
printf("%lld", ans);
else
printf("Impossible");
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
int w[200100];
int f[200100];
int sm[200100];
long long an[200100], w1[200100], w2[200100], mx[200100];
int a[200100], b[200100];
vector<int> E[200100];
queue<int> Q;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &w[i]);
for (int i = 1; i <= n - 1; i++) {
scanf("%d%d", &a[i], &b[i]);
E[a[i]].push_back(b[i]);
E[b[i]].push_back(a[i]);
}
f[1] = -1;
Q.push(1);
while (!Q.empty()) {
int in = Q.front();
Q.pop();
for (int i = 0; i <= (int)(E[in].size()) - 1; i++) {
if (!f[E[in][i]]) {
f[E[in][i]] = in;
Q.push(E[in][i]);
}
}
}
int zhi = 0;
for (int i = 1; i <= n; i++) {
sm[i] = E[i].size() - 1;
if (i == 1) sm[i]++;
if (sm[i] > 1) zhi = 1;
if (sm[i] == 0) Q.push(i);
}
if (!zhi) {
cout << "Impossible" << endl;
return 0;
}
long long ans = -999999999999999;
for (int i = 1; i <= n; i++) w1[i] = -999999999999999;
for (int i = 1; i <= n; i++) mx[i] = -999999999999999;
for (int i = 1; i <= n; i++) an[i] = 0;
for (int i = 1; i <= n; i++) w2[i] = -999999999999999;
while (!Q.empty()) {
int in = Q.front();
Q.pop();
if (in == 1) break;
int fa = f[in];
an[in] += w[in];
mx[in] = max(mx[in], an[in]);
an[f[in]] += an[in];
if (an[in] > w1[fa]) {
w2[fa] = w1[fa];
w1[fa] = an[in];
} else {
if (an[in] > w2[fa]) {
w2[fa] = an[in];
}
}
ans = max(ans, w1[fa] + w2[fa]);
ans = max(ans, mx[fa] + mx[in]);
mx[fa] = max(mx[fa], mx[in]);
sm[f[in]]--;
if (sm[f[in]] == 0) Q.push(f[in]);
}
cout << ans << endl;
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
int w[200100];
int f[200100];
int sm[200100];
long long an[200100], w1[200100], w2[200100], mx[200100];
int a[200100], b[200100];
vector<int> E[200100];
queue<int> Q;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &w[i]);
for (int i = 1; i <= n - 1; i++) {
scanf("%d%d", &a[i], &b[i]);
E[a[i]].push_back(b[i]);
E[b[i]].push_back(a[i]);
}
f[1] = -1;
Q.push(1);
while (!Q.empty()) {
int in = Q.front();
Q.pop();
for (int i = 0; i <= (int)(E[in].size()) - 1; i++) {
if (!f[E[in][i]]) {
f[E[in][i]] = in;
Q.push(E[in][i]);
}
}
}
int zhi = 0;
for (int i = 1; i <= n; i++) {
sm[i] = E[i].size() - 1;
if (i == 1) sm[i]++;
if (sm[i] > 1) zhi = 1;
if (sm[i] == 0) Q.push(i);
}
if (!zhi) {
cout << "Impossible" << endl;
return 0;
}
long long ans = -999999999999999;
for (int i = 1; i <= n; i++) w1[i] = -999999999999999;
for (int i = 1; i <= n; i++) mx[i] = -999999999999999;
for (int i = 1; i <= n; i++) an[i] = 0;
for (int i = 1; i <= n; i++) w2[i] = -999999999999999;
while (!Q.empty()) {
int in = Q.front();
Q.pop();
if (in == 1) break;
int fa = f[in];
an[in] += w[in];
mx[in] = max(mx[in], an[in]);
an[f[in]] += an[in];
if (an[in] > w1[fa]) {
w2[fa] = w1[fa];
w1[fa] = an[in];
} else {
if (an[in] > w2[fa]) {
w2[fa] = an[in];
}
}
ans = max(ans, w1[fa] + w2[fa]);
ans = max(ans, mx[fa] + mx[in]);
mx[fa] = max(mx[fa], mx[in]);
sm[f[in]]--;
if (sm[f[in]] == 0) Q.push(f[in]);
}
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T qmin(const T a, const T b) {
return a < b ? a : b;
}
template <typename T>
inline T qmax(const T a, const T b) {
return a > b ? a : b;
}
template <typename T>
inline void getmin(T &a, const T b) {
if (a > b) a = b;
}
template <typename T>
inline void getmax(T &a, const T b) {
if (a < b) a = b;
}
void fileio(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int inf = 1e9 + 7;
const long long linf = 1e17 + 7;
const int N = 2e5 + 7, M = N << 1;
int v[M], nxt[M], head[N];
long long val[N], f[N][3];
int n;
void predfs(int u, int p) {
for (int e = head[u]; e; e = nxt[e]) {
if (v[e] == p) continue;
predfs(v[e], u);
val[u] += val[v[e]];
}
}
void dfs(int u, int p) {
f[u][1] = val[u], f[u][2] = -linf;
long long max1 = -linf, max2 = -linf;
for (int e = head[u]; e; e = nxt[e]) {
if (v[e] == p) continue;
dfs(v[e], u);
if (f[v[e]][1] > max1) {
max2 = max1, max1 = f[v[e]][1];
} else {
getmax(max2, f[v[e]][1]);
}
getmax(f[u][1], f[v[e]][1]);
getmax(f[u][2], f[v[e]][2]);
}
if (max2 == -linf) return;
getmax(f[u][2], max1 + max2);
}
void addedge(int p, int x, int y) { v[p] = y, nxt[p] = head[x], head[x] = p; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%I64d", val + i);
for (int i = 1; i < n; ++i) {
int x, y;
scanf("%d%d", &x, &y);
addedge(i << 1, x, y);
addedge(i << 1 ^ 1, y, x);
}
predfs(1, -1);
dfs(1, -1);
if (f[1][2] == -linf)
puts("Impossible");
else
printf("%I64d\n", f[1][2]);
return 0;
}
|
### Prompt
Create a solution in CPP for the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T qmin(const T a, const T b) {
return a < b ? a : b;
}
template <typename T>
inline T qmax(const T a, const T b) {
return a > b ? a : b;
}
template <typename T>
inline void getmin(T &a, const T b) {
if (a > b) a = b;
}
template <typename T>
inline void getmax(T &a, const T b) {
if (a < b) a = b;
}
void fileio(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int inf = 1e9 + 7;
const long long linf = 1e17 + 7;
const int N = 2e5 + 7, M = N << 1;
int v[M], nxt[M], head[N];
long long val[N], f[N][3];
int n;
void predfs(int u, int p) {
for (int e = head[u]; e; e = nxt[e]) {
if (v[e] == p) continue;
predfs(v[e], u);
val[u] += val[v[e]];
}
}
void dfs(int u, int p) {
f[u][1] = val[u], f[u][2] = -linf;
long long max1 = -linf, max2 = -linf;
for (int e = head[u]; e; e = nxt[e]) {
if (v[e] == p) continue;
dfs(v[e], u);
if (f[v[e]][1] > max1) {
max2 = max1, max1 = f[v[e]][1];
} else {
getmax(max2, f[v[e]][1]);
}
getmax(f[u][1], f[v[e]][1]);
getmax(f[u][2], f[v[e]][2]);
}
if (max2 == -linf) return;
getmax(f[u][2], max1 + max2);
}
void addedge(int p, int x, int y) { v[p] = y, nxt[p] = head[x], head[x] = p; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%I64d", val + i);
for (int i = 1; i < n; ++i) {
int x, y;
scanf("%d%d", &x, &y);
addedge(i << 1, x, y);
addedge(i << 1 ^ 1, y, x);
}
predfs(1, -1);
dfs(1, -1);
if (f[1][2] == -linf)
puts("Impossible");
else
printf("%I64d\n", f[1][2]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long num[200000], dp[200000][2], ans = -1e18;
vector<int> r[200000];
void dfs(int now, int par) {
long long sum = 0, max1 = -1e18, max2 = -1e18;
dp[now][0] = num[now];
for (int i = 0; i < r[now].size(); i++)
if (r[now][i] != par) {
dfs(r[now][i], now);
dp[now][0] += dp[r[now][i]][0];
}
dp[now][1] = dp[now][0];
for (int i = 0; i < r[now].size(); i++)
if (r[now][i] != par) dp[now][1] = max(dp[now][1], dp[r[now][i]][1]);
for (int i = 0; i < r[now].size(); i++)
if (r[now][i] != par) {
max2 = max(max2, dp[r[now][i]][1]);
if (max1 < max2) swap(max1, max2);
}
if (max2 != -1e18) ans = max(ans, max1 + max2);
}
int main() {
int n, i, a, b;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%lld", &num[i]);
for (i = 0; i < n - 1; i++) {
scanf("%d%d", &a, &b);
r[a - 1].push_back(b - 1);
r[b - 1].push_back(a - 1);
}
dfs(0, -1);
if (ans != -1e18)
printf("%lld\n", ans);
else
printf("Impossible\n");
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long num[200000], dp[200000][2], ans = -1e18;
vector<int> r[200000];
void dfs(int now, int par) {
long long sum = 0, max1 = -1e18, max2 = -1e18;
dp[now][0] = num[now];
for (int i = 0; i < r[now].size(); i++)
if (r[now][i] != par) {
dfs(r[now][i], now);
dp[now][0] += dp[r[now][i]][0];
}
dp[now][1] = dp[now][0];
for (int i = 0; i < r[now].size(); i++)
if (r[now][i] != par) dp[now][1] = max(dp[now][1], dp[r[now][i]][1]);
for (int i = 0; i < r[now].size(); i++)
if (r[now][i] != par) {
max2 = max(max2, dp[r[now][i]][1]);
if (max1 < max2) swap(max1, max2);
}
if (max2 != -1e18) ans = max(ans, max1 + max2);
}
int main() {
int n, i, a, b;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%lld", &num[i]);
for (i = 0; i < n - 1; i++) {
scanf("%d%d", &a, &b);
r[a - 1].push_back(b - 1);
r[b - 1].push_back(a - 1);
}
dfs(0, -1);
if (ans != -1e18)
printf("%lld\n", ans);
else
printf("Impossible\n");
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long minn = -100000000000000000LL;
long long a[200010];
vector<int> G[200010];
long long dp[200010][2];
long long sum[200020];
long long ans = 0;
void dfs(int u, int fa) {
sum[u] = a[u];
dp[u][0] = dp[u][1] = minn;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (v == fa) continue;
dfs(v, u);
sum[u] += sum[v];
if (dp[v][0] >= dp[u][0]) {
swap(dp[u][1], dp[u][0]);
dp[u][0] = dp[v][0];
} else if (dp[v][0] >= dp[u][1]) {
dp[u][1] = dp[v][0];
}
}
if (dp[u][1] != minn) ans = max(ans, dp[u][0] + dp[u][1]);
dp[u][0] = max(sum[u], dp[u][0]);
}
int main() {
ios::sync_with_stdio(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int u, v;
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
u--;
v--;
G[u].push_back(v);
G[v].push_back(u);
}
ans = minn;
dfs(0, -1);
if (ans == minn)
cout << "Impossible" << endl;
else
cout << ans << endl;
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long minn = -100000000000000000LL;
long long a[200010];
vector<int> G[200010];
long long dp[200010][2];
long long sum[200020];
long long ans = 0;
void dfs(int u, int fa) {
sum[u] = a[u];
dp[u][0] = dp[u][1] = minn;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (v == fa) continue;
dfs(v, u);
sum[u] += sum[v];
if (dp[v][0] >= dp[u][0]) {
swap(dp[u][1], dp[u][0]);
dp[u][0] = dp[v][0];
} else if (dp[v][0] >= dp[u][1]) {
dp[u][1] = dp[v][0];
}
}
if (dp[u][1] != minn) ans = max(ans, dp[u][0] + dp[u][1]);
dp[u][0] = max(sum[u], dp[u][0]);
}
int main() {
ios::sync_with_stdio(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int u, v;
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
u--;
v--;
G[u].push_back(v);
G[v].push_back(u);
}
ans = minn;
dfs(0, -1);
if (ans == minn)
cout << "Impossible" << endl;
else
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
pair<long long, long long> mx[200005];
long long sum[200005];
vector<long long> g[200005];
bool vis[200005];
long long ans = -1e16;
long long getsum(int ver) {
vis[ver] = true;
for (int i = 0; i < g[ver].size(); i++) {
int child = g[ver][i];
if (vis[child]) continue;
sum[ver] += getsum(child);
}
return sum[ver];
}
void getmax(int ver) {
vis[ver] = true;
for (int i = 0; i < g[ver].size(); i++) {
int child = g[ver][i];
if (vis[child]) continue;
getmax(child);
long long mx1 = -1e16;
long long mx2 = -1e16;
if (sum[child] >= mx[child].first && sum[child] >= mx[child].second)
mx2 = sum[child];
else
mx1 = mx[child].first, mx2 = mx[child].second;
long long arr[4];
arr[0] = mx1, arr[1] = mx2, arr[2] = mx[ver].first, arr[3] = mx[ver].second;
sort(arr, arr + 4);
mx[ver].first = arr[2], mx[ver].second = arr[3];
}
ans = max(ans, mx[ver].first + mx[ver].second);
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) scanf("%lld", &sum[i]);
for (int i = 1; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
g[a].push_back(b);
g[b].push_back(a);
}
for (int i = 1; i <= n; i++) mx[i].first = -1e16, mx[i].second = -1e16;
sum[1] = getsum(1);
memset(vis, 0, sizeof(vis));
getmax(1);
if (ans < -200000000000000)
cout << "Impossible";
else
cout << ans;
}
|
### Prompt
Generate a CPP solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
pair<long long, long long> mx[200005];
long long sum[200005];
vector<long long> g[200005];
bool vis[200005];
long long ans = -1e16;
long long getsum(int ver) {
vis[ver] = true;
for (int i = 0; i < g[ver].size(); i++) {
int child = g[ver][i];
if (vis[child]) continue;
sum[ver] += getsum(child);
}
return sum[ver];
}
void getmax(int ver) {
vis[ver] = true;
for (int i = 0; i < g[ver].size(); i++) {
int child = g[ver][i];
if (vis[child]) continue;
getmax(child);
long long mx1 = -1e16;
long long mx2 = -1e16;
if (sum[child] >= mx[child].first && sum[child] >= mx[child].second)
mx2 = sum[child];
else
mx1 = mx[child].first, mx2 = mx[child].second;
long long arr[4];
arr[0] = mx1, arr[1] = mx2, arr[2] = mx[ver].first, arr[3] = mx[ver].second;
sort(arr, arr + 4);
mx[ver].first = arr[2], mx[ver].second = arr[3];
}
ans = max(ans, mx[ver].first + mx[ver].second);
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) scanf("%lld", &sum[i]);
for (int i = 1; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
g[a].push_back(b);
g[b].push_back(a);
}
for (int i = 1; i <= n; i++) mx[i].first = -1e16, mx[i].second = -1e16;
sum[1] = getsum(1);
memset(vis, 0, sizeof(vis));
getmax(1);
if (ans < -200000000000000)
cout << "Impossible";
else
cout << ans;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2 * 100000 + 10;
const long long llinf = (long long)1 << 62;
int a[maxn], n;
vector<int> g[maxn];
long long sum_subTree[maxn], mx_subTree[maxn], ans = -llinf;
void dfs1(int v, int p) {
sum_subTree[v] = a[v];
mx_subTree[v] = -llinf;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (to == p) continue;
dfs1(to, v);
sum_subTree[v] += sum_subTree[to];
mx_subTree[v] = max(mx_subTree[v], mx_subTree[to]);
}
mx_subTree[v] = max(mx_subTree[v], sum_subTree[v]);
}
void dfs2(int v, int p, long long out) {
if (out != -llinf) ans = max(ans, sum_subTree[v] + out);
vector<pair<long long, int> > miniset;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (to == p) continue;
miniset.push_back(make_pair(mx_subTree[to], to));
sort(miniset.rbegin(), miniset.rend());
if (miniset.size() == 3) miniset.pop_back();
}
miniset.push_back(make_pair(-llinf, -1));
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (to == p) continue;
long long cur =
miniset[0].second == to ? miniset[1].first : miniset[0].first;
dfs2(to, v, max(out, cur));
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 0; i + 1 < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
dfs1(0, -1);
dfs2(0, -1, -llinf);
if (ans != -llinf)
printf("%I64d\n", ans);
else
printf("Impossible\n");
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2 * 100000 + 10;
const long long llinf = (long long)1 << 62;
int a[maxn], n;
vector<int> g[maxn];
long long sum_subTree[maxn], mx_subTree[maxn], ans = -llinf;
void dfs1(int v, int p) {
sum_subTree[v] = a[v];
mx_subTree[v] = -llinf;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (to == p) continue;
dfs1(to, v);
sum_subTree[v] += sum_subTree[to];
mx_subTree[v] = max(mx_subTree[v], mx_subTree[to]);
}
mx_subTree[v] = max(mx_subTree[v], sum_subTree[v]);
}
void dfs2(int v, int p, long long out) {
if (out != -llinf) ans = max(ans, sum_subTree[v] + out);
vector<pair<long long, int> > miniset;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (to == p) continue;
miniset.push_back(make_pair(mx_subTree[to], to));
sort(miniset.rbegin(), miniset.rend());
if (miniset.size() == 3) miniset.pop_back();
}
miniset.push_back(make_pair(-llinf, -1));
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (to == p) continue;
long long cur =
miniset[0].second == to ? miniset[1].first : miniset[0].first;
dfs2(to, v, max(out, cur));
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 0; i + 1 < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
dfs1(0, -1);
dfs2(0, -1, -llinf);
if (ans != -llinf)
printf("%I64d\n", ans);
else
printf("Impossible\n");
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
int a[200001];
long long sum[200001];
long long mx[200001];
bool vis[200001];
vector<int> conn[200001];
long long computeSum(int i) {
if (vis[i]) return 0;
vis[i] = true;
long long s = a[i];
for (int j = 0; j < conn[i].size(); j++) {
if (conn[i][j] == -1) continue;
if (vis[conn[i][j]])
conn[i][j] = -1;
else
s += computeSum(conn[i][j]);
}
return (sum[i] = s);
}
long long computeMax(int i) {
if (conn[i].size() == 0 || conn[i][0] == -1) return (mx[i] = sum[i]);
long long ret = computeMax(conn[i][0]);
for (int j = 1; j < conn[i].size(); j++) {
if (conn[i][j] != -1) ret = max(ret, computeMax(conn[i][j]));
}
if (i != 0) ret = max(ret, sum[i]);
return (mx[i] = ret);
}
int main() {
scanf("%d", &n);
int aa;
for (int i = 0; i < n; i++) {
scanf("%d", &aa);
a[i] = aa;
}
int u, v;
for (int i = 1; i < n; i++) {
scanf("%d %d", &u, &v);
conn[u - 1].push_back(v - 1);
conn[v - 1].push_back(u - 1);
}
memset(vis, 0, sizeof vis);
computeSum(0);
for (int i = 0; i < n; i++) sort(conn[i].rbegin(), conn[i].rend());
computeMax(0);
long long ret;
bool flag = false;
for (int i = 0; i < n; i++) {
vector<long long> b;
for (int j = 0; j < conn[i].size(); j++) {
if (conn[i][j] == -1) continue;
b.push_back(mx[conn[i][j]]);
}
sort(b.rbegin(), b.rend());
if (b.size() < 2) continue;
if (!flag) {
flag = true;
ret = b[0] + b[1];
} else
ret = max(ret, b[0] + b[1]);
}
if (flag)
cout << ret << endl;
else
cout << "Impossible" << endl;
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
int a[200001];
long long sum[200001];
long long mx[200001];
bool vis[200001];
vector<int> conn[200001];
long long computeSum(int i) {
if (vis[i]) return 0;
vis[i] = true;
long long s = a[i];
for (int j = 0; j < conn[i].size(); j++) {
if (conn[i][j] == -1) continue;
if (vis[conn[i][j]])
conn[i][j] = -1;
else
s += computeSum(conn[i][j]);
}
return (sum[i] = s);
}
long long computeMax(int i) {
if (conn[i].size() == 0 || conn[i][0] == -1) return (mx[i] = sum[i]);
long long ret = computeMax(conn[i][0]);
for (int j = 1; j < conn[i].size(); j++) {
if (conn[i][j] != -1) ret = max(ret, computeMax(conn[i][j]));
}
if (i != 0) ret = max(ret, sum[i]);
return (mx[i] = ret);
}
int main() {
scanf("%d", &n);
int aa;
for (int i = 0; i < n; i++) {
scanf("%d", &aa);
a[i] = aa;
}
int u, v;
for (int i = 1; i < n; i++) {
scanf("%d %d", &u, &v);
conn[u - 1].push_back(v - 1);
conn[v - 1].push_back(u - 1);
}
memset(vis, 0, sizeof vis);
computeSum(0);
for (int i = 0; i < n; i++) sort(conn[i].rbegin(), conn[i].rend());
computeMax(0);
long long ret;
bool flag = false;
for (int i = 0; i < n; i++) {
vector<long long> b;
for (int j = 0; j < conn[i].size(); j++) {
if (conn[i][j] == -1) continue;
b.push_back(mx[conn[i][j]]);
}
sort(b.rbegin(), b.rend());
if (b.size() < 2) continue;
if (!flag) {
flag = true;
ret = b[0] + b[1];
} else
ret = max(ret, b[0] + b[1]);
}
if (flag)
cout << ret << endl;
else
cout << "Impossible" << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const long long LINF = 1e17;
const double PI = 3.141592653589793238;
const int mod = 1000000007;
const double eps = 0.000001;
template <typename T, typename TT>
ostream &operator<<(ostream &s, pair<T, TT> t) {
return s << "(" << t.first << "," << t.second << ")";
}
template <typename T>
ostream &operator<<(ostream &s, vector<T> t) {
for (int i = 0; i < (t).size(); i++)
s << t[i] << ((i < (t).size() - 1) ? " " : "");
return s;
}
template <typename T>
ostream &operator<<(ostream &s, set<T> t) {
for (T x : t) s << x << " ";
return s;
}
template <typename T>
istream &operator>>(istream &s, vector<T> &t) {
for (int _i = 0; _i < t.size(); _i++) s >> t[_i];
return s;
}
int n;
vector<vector<long long> > graph;
vector<long long> a;
vector<long long> visited;
vector<long long> weight;
vector<long long> parent;
vector<long long> max_child_weight;
vector<long long> max_pair_weight;
void dfs(int u) {
visited[u] = true;
weight[u] = a[u];
long long max1, max2;
max1 = max2 = -LINF;
for (int v : graph[u]) {
if (!visited[v]) {
parent[v] = u;
dfs(v);
weight[u] += weight[v];
long long maxc = max_child_weight[v];
max_child_weight[u] = max(max_child_weight[u], maxc);
max_pair_weight[u] = max(max_pair_weight[u], max_pair_weight[v]);
if (maxc >= max1)
max2 = max1, max1 = maxc;
else if (maxc >= max2)
max2 = maxc;
}
}
if (max1 > -LINF && max2 > -LINF)
max_pair_weight[u] = max(max_pair_weight[u], max1 + max2);
max_child_weight[u] = max(max_child_weight[u], weight[u]);
}
int main(int argc, const char *argv[]) {
cin >> n;
a.resize(n);
cin >> a;
graph.resize(n);
for (int i = (0); i < (n - 1); i++) {
int u, v;
cin >> u >> v;
u--;
v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
visited = vector<long long>(n);
parent = vector<long long>(n, -1);
weight = vector<long long>(n);
max_child_weight = vector<long long>(n, -LINF);
max_pair_weight = vector<long long>(n, -LINF);
dfs(0);
if (max_pair_weight[0] == -LINF)
cout << "Impossible\n";
else
cout << max_pair_weight[0] << "\n";
}
|
### Prompt
In Cpp, your task is to solve the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const long long LINF = 1e17;
const double PI = 3.141592653589793238;
const int mod = 1000000007;
const double eps = 0.000001;
template <typename T, typename TT>
ostream &operator<<(ostream &s, pair<T, TT> t) {
return s << "(" << t.first << "," << t.second << ")";
}
template <typename T>
ostream &operator<<(ostream &s, vector<T> t) {
for (int i = 0; i < (t).size(); i++)
s << t[i] << ((i < (t).size() - 1) ? " " : "");
return s;
}
template <typename T>
ostream &operator<<(ostream &s, set<T> t) {
for (T x : t) s << x << " ";
return s;
}
template <typename T>
istream &operator>>(istream &s, vector<T> &t) {
for (int _i = 0; _i < t.size(); _i++) s >> t[_i];
return s;
}
int n;
vector<vector<long long> > graph;
vector<long long> a;
vector<long long> visited;
vector<long long> weight;
vector<long long> parent;
vector<long long> max_child_weight;
vector<long long> max_pair_weight;
void dfs(int u) {
visited[u] = true;
weight[u] = a[u];
long long max1, max2;
max1 = max2 = -LINF;
for (int v : graph[u]) {
if (!visited[v]) {
parent[v] = u;
dfs(v);
weight[u] += weight[v];
long long maxc = max_child_weight[v];
max_child_weight[u] = max(max_child_weight[u], maxc);
max_pair_weight[u] = max(max_pair_weight[u], max_pair_weight[v]);
if (maxc >= max1)
max2 = max1, max1 = maxc;
else if (maxc >= max2)
max2 = maxc;
}
}
if (max1 > -LINF && max2 > -LINF)
max_pair_weight[u] = max(max_pair_weight[u], max1 + max2);
max_child_weight[u] = max(max_child_weight[u], weight[u]);
}
int main(int argc, const char *argv[]) {
cin >> n;
a.resize(n);
cin >> a;
graph.resize(n);
for (int i = (0); i < (n - 1); i++) {
int u, v;
cin >> u >> v;
u--;
v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
visited = vector<long long>(n);
parent = vector<long long>(n, -1);
weight = vector<long long>(n);
max_child_weight = vector<long long>(n, -LINF);
max_pair_weight = vector<long long>(n, -LINF);
dfs(0);
if (max_pair_weight[0] == -LINF)
cout << "Impossible\n";
else
cout << max_pair_weight[0] << "\n";
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const long long NINF = 0xdddddddddddddddd;
const int maxn = 2e5 + 11;
int N, M, T, P, Q;
int a[maxn];
list<int> G[maxn];
long long sum[maxn];
int sz[maxn], l[maxn], r[maxn], ver[maxn << 1];
int cur = 0;
void dfs(int u = 1, int pu = -1) {
l[u] = ++cur;
ver[cur] = u;
sz[u] = 1;
sum[u] = (long long)a[u];
for (int v : G[u]) {
if (v != pu) {
dfs(v, u);
sz[u] += sz[v];
sum[u] += sum[v];
}
}
r[u] = cur;
}
long long mmax[maxn << 2];
int inline LC(int i) { return i * 2; }
int inline RC(int i) { return i * 2 + 1; }
void build(int L, int R, int i) {
if (L == R) {
mmax[i] = sum[ver[L]];
return;
}
int M = (L + R) / 2;
build(L, M, LC(i));
build(M + 1, R, RC(i));
mmax[i] = max(mmax[LC(i)], mmax[RC(i)]);
}
int x1, x2;
long long res;
void query(int L, int R, int i) {
if (x1 <= L && R <= x2) {
res = max(res, mmax[i]);
return;
}
int M = (L + R) / 2;
if (x1 <= M) query(L, M, LC(i));
if (x2 > M) query(M + 1, R, RC(i));
}
long long ans = NINF;
long long tmax[maxn], s1[maxn], s2[maxn];
void dfs2(int u = 1, int pu = -1) {
x1 = l[u], x2 = r[u];
res = NINF;
query(1, cur, 1);
tmax[u] = res;
s1[u] = s2[u] = NINF;
for (int v : G[u]) {
if (v != pu) {
dfs2(v, u);
s2[u] = max(s2[u], tmax[v]);
if (s2[u] > s1[u]) swap(s1[u], s2[u]);
}
}
ans = max(ans, s1[u] + s2[u]);
}
int main() {
cin >> N;
for (int i = 1; i <= N; i++) scanf("%d", &a[i]);
for (int i = 1; i < N; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs();
build(1, cur, 1);
dfs2();
if (ans <= (long long)(-1e12))
puts("Impossible");
else
cout << ans << endl;
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const long long NINF = 0xdddddddddddddddd;
const int maxn = 2e5 + 11;
int N, M, T, P, Q;
int a[maxn];
list<int> G[maxn];
long long sum[maxn];
int sz[maxn], l[maxn], r[maxn], ver[maxn << 1];
int cur = 0;
void dfs(int u = 1, int pu = -1) {
l[u] = ++cur;
ver[cur] = u;
sz[u] = 1;
sum[u] = (long long)a[u];
for (int v : G[u]) {
if (v != pu) {
dfs(v, u);
sz[u] += sz[v];
sum[u] += sum[v];
}
}
r[u] = cur;
}
long long mmax[maxn << 2];
int inline LC(int i) { return i * 2; }
int inline RC(int i) { return i * 2 + 1; }
void build(int L, int R, int i) {
if (L == R) {
mmax[i] = sum[ver[L]];
return;
}
int M = (L + R) / 2;
build(L, M, LC(i));
build(M + 1, R, RC(i));
mmax[i] = max(mmax[LC(i)], mmax[RC(i)]);
}
int x1, x2;
long long res;
void query(int L, int R, int i) {
if (x1 <= L && R <= x2) {
res = max(res, mmax[i]);
return;
}
int M = (L + R) / 2;
if (x1 <= M) query(L, M, LC(i));
if (x2 > M) query(M + 1, R, RC(i));
}
long long ans = NINF;
long long tmax[maxn], s1[maxn], s2[maxn];
void dfs2(int u = 1, int pu = -1) {
x1 = l[u], x2 = r[u];
res = NINF;
query(1, cur, 1);
tmax[u] = res;
s1[u] = s2[u] = NINF;
for (int v : G[u]) {
if (v != pu) {
dfs2(v, u);
s2[u] = max(s2[u], tmax[v]);
if (s2[u] > s1[u]) swap(s1[u], s2[u]);
}
}
ans = max(ans, s1[u] + s2[u]);
}
int main() {
cin >> N;
for (int i = 1; i <= N; i++) scanf("%d", &a[i]);
for (int i = 1; i < N; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs();
build(1, cur, 1);
dfs2();
if (ans <= (long long)(-1e12))
puts("Impossible");
else
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
int val[200005];
vector<int> adj[200005];
long long sum[200005];
long long max_sum[200005];
bool chain(int u, int p) {
int cnt = 0;
for (int v : adj[u]) {
if (v == p) continue;
if (++cnt > 1) return false;
if (!chain(v, u)) return false;
}
return true;
}
long long calc_sum(int u, int p) {
sum[u] = val[u];
for (int v : adj[u]) {
if (v == p) continue;
sum[u] += calc_sum(v, u);
}
return sum[u];
}
long long calc_max(int u, int p) {
max_sum[u] = sum[u];
for (int v : adj[u]) {
if (v == p) continue;
max_sum[u] = max(max_sum[u], calc_max(v, u));
}
return max_sum[u];
}
long long find_ans(int u, int p) {
long long ans = LLONG_MIN;
int cnt = 0;
vector<long long> children_ans;
vector<long long> children_max;
for (int v : adj[u]) {
if (v == p) continue;
cnt++;
children_ans.push_back(find_ans(v, u));
children_max.push_back(max_sum[v]);
}
if (cnt > 0) {
ans = max(ans, *max_element(children_ans.begin(), children_ans.end()));
}
if (cnt > 1) {
sort(children_max.begin(), children_max.end());
ans = max(ans, children_max[cnt - 2] + children_max[cnt - 1]);
}
return ans;
}
int main() {
scanf(" %d", &n);
for (int i = 1; i <= n; i++) {
scanf(" %d", &val[i]);
}
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf(" %d %d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
if (chain(1, 1)) {
printf("Impossible\n");
} else {
calc_sum(1, 1);
calc_max(1, 1);
printf("%lld\n", find_ans(1, 1));
}
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
int val[200005];
vector<int> adj[200005];
long long sum[200005];
long long max_sum[200005];
bool chain(int u, int p) {
int cnt = 0;
for (int v : adj[u]) {
if (v == p) continue;
if (++cnt > 1) return false;
if (!chain(v, u)) return false;
}
return true;
}
long long calc_sum(int u, int p) {
sum[u] = val[u];
for (int v : adj[u]) {
if (v == p) continue;
sum[u] += calc_sum(v, u);
}
return sum[u];
}
long long calc_max(int u, int p) {
max_sum[u] = sum[u];
for (int v : adj[u]) {
if (v == p) continue;
max_sum[u] = max(max_sum[u], calc_max(v, u));
}
return max_sum[u];
}
long long find_ans(int u, int p) {
long long ans = LLONG_MIN;
int cnt = 0;
vector<long long> children_ans;
vector<long long> children_max;
for (int v : adj[u]) {
if (v == p) continue;
cnt++;
children_ans.push_back(find_ans(v, u));
children_max.push_back(max_sum[v]);
}
if (cnt > 0) {
ans = max(ans, *max_element(children_ans.begin(), children_ans.end()));
}
if (cnt > 1) {
sort(children_max.begin(), children_max.end());
ans = max(ans, children_max[cnt - 2] + children_max[cnt - 1]);
}
return ans;
}
int main() {
scanf(" %d", &n);
for (int i = 1; i <= n; i++) {
scanf(" %d", &val[i]);
}
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf(" %d %d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
if (chain(1, 1)) {
printf("Impossible\n");
} else {
calc_sum(1, 1);
calc_max(1, 1);
printf("%lld\n", find_ans(1, 1));
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200100, maxm = 500100;
const long long infinit = 0x007f7f7f7f7f7f7fll;
class TreeDP {
public:
struct edge {
int u, v;
edge *next;
};
int n, root, ecnt;
edge *edges[maxn], epool[maxm];
long long val[maxn], sum[maxn];
long long dp[maxn][3];
int depth[maxn], size[maxn];
void add_edge(int u, int v) {
edge *p = &epool[++ecnt], *q = &epool[++ecnt];
p->u = u;
p->v = v;
p->next = edges[u];
edges[u] = p;
q->u = v;
q->v = u;
q->next = edges[v];
edges[v] = q;
return;
}
void dfs1(int p) {
size[p] = 1;
sum[p] = val[p];
for (edge *ep = edges[p]; ep; ep = ep->next)
if (!depth[ep->v]) {
depth[ep->v] = depth[p] + 1;
dfs1(ep->v);
size[p] += size[ep->v];
sum[p] += sum[ep->v];
}
return;
}
void dfs2(int p) {
dp[p][1] = sum[p];
dp[p][2] = -infinit;
long long longest = -infinit, s_longest = -infinit;
for (edge *ep = edges[p]; ep; ep = ep->next)
if (depth[ep->v] == depth[p] + 1) {
dfs2(ep->v);
dp[p][1] = max(dp[p][1], dp[ep->v][1]);
dp[p][2] = max(dp[p][2], dp[ep->v][2]);
if (dp[ep->v][1] >= longest)
s_longest = longest, longest = dp[ep->v][1];
else if (dp[ep->v][1] >= s_longest)
s_longest = dp[ep->v][1];
}
if (longest > -infinit && s_longest > -infinit)
dp[p][2] = max(dp[p][2], longest + s_longest);
return;
}
long long eval(void) {
depth[root] = 1;
dfs1(root);
dfs2(root);
return dp[root][2];
}
} graph;
int n;
int main(int argc, char **argv) {
scanf("%d", &n);
graph.n = n;
graph.root = 1;
for (int i = 1; i <= n; i++) scanf("%lld", &graph.val[i]);
for (int i = 1, a, b; i <= n - 1; i++) {
scanf("%d%d", &a, &b);
graph.add_edge(a, b);
}
long long res = graph.eval();
if (res == -infinit)
printf("Impossible\n");
else
printf("%lld\n", res);
return 0;
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200100, maxm = 500100;
const long long infinit = 0x007f7f7f7f7f7f7fll;
class TreeDP {
public:
struct edge {
int u, v;
edge *next;
};
int n, root, ecnt;
edge *edges[maxn], epool[maxm];
long long val[maxn], sum[maxn];
long long dp[maxn][3];
int depth[maxn], size[maxn];
void add_edge(int u, int v) {
edge *p = &epool[++ecnt], *q = &epool[++ecnt];
p->u = u;
p->v = v;
p->next = edges[u];
edges[u] = p;
q->u = v;
q->v = u;
q->next = edges[v];
edges[v] = q;
return;
}
void dfs1(int p) {
size[p] = 1;
sum[p] = val[p];
for (edge *ep = edges[p]; ep; ep = ep->next)
if (!depth[ep->v]) {
depth[ep->v] = depth[p] + 1;
dfs1(ep->v);
size[p] += size[ep->v];
sum[p] += sum[ep->v];
}
return;
}
void dfs2(int p) {
dp[p][1] = sum[p];
dp[p][2] = -infinit;
long long longest = -infinit, s_longest = -infinit;
for (edge *ep = edges[p]; ep; ep = ep->next)
if (depth[ep->v] == depth[p] + 1) {
dfs2(ep->v);
dp[p][1] = max(dp[p][1], dp[ep->v][1]);
dp[p][2] = max(dp[p][2], dp[ep->v][2]);
if (dp[ep->v][1] >= longest)
s_longest = longest, longest = dp[ep->v][1];
else if (dp[ep->v][1] >= s_longest)
s_longest = dp[ep->v][1];
}
if (longest > -infinit && s_longest > -infinit)
dp[p][2] = max(dp[p][2], longest + s_longest);
return;
}
long long eval(void) {
depth[root] = 1;
dfs1(root);
dfs2(root);
return dp[root][2];
}
} graph;
int n;
int main(int argc, char **argv) {
scanf("%d", &n);
graph.n = n;
graph.root = 1;
for (int i = 1; i <= n; i++) scanf("%lld", &graph.val[i]);
for (int i = 1, a, b; i <= n - 1; i++) {
scanf("%d%d", &a, &b);
graph.add_edge(a, b);
}
long long res = graph.eval();
if (res == -infinit)
printf("Impossible\n");
else
printf("%lld\n", res);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
vector<int> g[N];
long long int sm[N], mx[N];
int A[N];
long long int ans;
void dfs(int c, int p) {
sm[c] = A[c];
for (int i = 0; i < g[c].size(); i++) {
int u = g[c][i];
if (u != p) {
dfs(u, c);
}
}
for (int i = 0; i < g[c].size(); i++) {
int u = g[c][i];
if (u != p) {
sm[c] += sm[u];
}
}
mx[c] = sm[c];
long long int mx1 = -1000000000000000LL, mx2 = -1000000000000000LL;
for (int i = 0; i < g[c].size(); i++) {
int u = g[c][i];
if (u != p) {
mx[c] = max(mx[c], mx[u]);
if (mx[u] >= mx1) {
mx2 = mx1;
mx1 = mx[u];
} else if (mx[u] >= mx2) {
mx2 = mx[u];
}
}
}
if (mx2 != -1000000000000000LL && mx1 + mx2 > ans) ans = mx1 + mx2;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n + 1; i++) scanf("%d", &A[i]);
for (int i = 0; i < n - 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
ans = -1000000000000000LL;
dfs(1, -1);
if (ans == -1000000000000000LL) {
printf("Impossible\n");
return 0;
}
cout << ans << "\n";
return 0;
}
|
### Prompt
Create a solution in CPP for the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
vector<int> g[N];
long long int sm[N], mx[N];
int A[N];
long long int ans;
void dfs(int c, int p) {
sm[c] = A[c];
for (int i = 0; i < g[c].size(); i++) {
int u = g[c][i];
if (u != p) {
dfs(u, c);
}
}
for (int i = 0; i < g[c].size(); i++) {
int u = g[c][i];
if (u != p) {
sm[c] += sm[u];
}
}
mx[c] = sm[c];
long long int mx1 = -1000000000000000LL, mx2 = -1000000000000000LL;
for (int i = 0; i < g[c].size(); i++) {
int u = g[c][i];
if (u != p) {
mx[c] = max(mx[c], mx[u]);
if (mx[u] >= mx1) {
mx2 = mx1;
mx1 = mx[u];
} else if (mx[u] >= mx2) {
mx2 = mx[u];
}
}
}
if (mx2 != -1000000000000000LL && mx1 + mx2 > ans) ans = mx1 + mx2;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n + 1; i++) scanf("%d", &A[i]);
for (int i = 0; i < n - 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
ans = -1000000000000000LL;
dfs(1, -1);
if (ans == -1000000000000000LL) {
printf("Impossible\n");
return 0;
}
cout << ans << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
static long long solve(const std::vector<std::vector<long> > &g,
const std::vector<long> &weight, long s, long p,
std::vector<long long> &total_w,
std::vector<long long> &max_sum) {
std::vector<long long> m;
m.reserve(g[s].size());
total_w[s] = weight[s];
for (long i = 0; i != (long)g[s].size(); ++i) {
const long t = g[s][i];
if (t == p) continue;
m.push_back(solve(g, weight, t, s, total_w, max_sum));
total_w[s] += total_w[t];
}
if (m.empty()) return total_w[s];
std::swap(*std::max_element(m.begin(), m.end()), m[0]);
if (m.size() >= 2) {
std::swap(*std::max_element(m.begin() + 1, m.end()), m[1]);
max_sum[s] = m[0] + m[1];
}
return std::max(m[0], total_w[s]);
}
int main() {
std::ios_base::sync_with_stdio(false);
long n;
std::cin >> n;
std::vector<long> weight(n);
for (long i = 0; i != n; ++i) std::cin >> weight[i];
std::vector<std::vector<long> > g(n);
for (long i = 0; i != n - 1; ++i) {
long u, v;
std::cin >> u >> v;
g[u - 1].push_back(v - 1);
g[v - 1].push_back(u - 1);
}
const long long INV = std::numeric_limits<long long>::min();
std::vector<long long> max_sum(n, INV), total_w(n, INV);
solve(g, weight, 0, -1, total_w, max_sum);
const long long ans = *std::max_element(max_sum.begin(), max_sum.end());
if (ans == INV)
std::cout << "Impossible\n";
else
std::cout << ans << "\n";
return 0;
}
|
### Prompt
Generate a CPP solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
static long long solve(const std::vector<std::vector<long> > &g,
const std::vector<long> &weight, long s, long p,
std::vector<long long> &total_w,
std::vector<long long> &max_sum) {
std::vector<long long> m;
m.reserve(g[s].size());
total_w[s] = weight[s];
for (long i = 0; i != (long)g[s].size(); ++i) {
const long t = g[s][i];
if (t == p) continue;
m.push_back(solve(g, weight, t, s, total_w, max_sum));
total_w[s] += total_w[t];
}
if (m.empty()) return total_w[s];
std::swap(*std::max_element(m.begin(), m.end()), m[0]);
if (m.size() >= 2) {
std::swap(*std::max_element(m.begin() + 1, m.end()), m[1]);
max_sum[s] = m[0] + m[1];
}
return std::max(m[0], total_w[s]);
}
int main() {
std::ios_base::sync_with_stdio(false);
long n;
std::cin >> n;
std::vector<long> weight(n);
for (long i = 0; i != n; ++i) std::cin >> weight[i];
std::vector<std::vector<long> > g(n);
for (long i = 0; i != n - 1; ++i) {
long u, v;
std::cin >> u >> v;
g[u - 1].push_back(v - 1);
g[v - 1].push_back(u - 1);
}
const long long INV = std::numeric_limits<long long>::min();
std::vector<long long> max_sum(n, INV), total_w(n, INV);
solve(g, weight, 0, -1, total_w, max_sum);
const long long ans = *std::max_element(max_sum.begin(), max_sum.end());
if (ans == INV)
std::cout << "Impossible\n";
else
std::cout << ans << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
T min(T a, T b, T c) {
return min(a, min(b, c));
}
template <class T>
T min(T a, T b, T c, T d) {
return min(a, min(b, min(c, d)));
}
template <class T>
T max(T a, T b, T c) {
return max(a, max(b, c));
}
template <class T>
T max(T a, T b, T c, T d) {
return max(a, max(b, max(c, d)));
}
bool cmp(const int& a, const int& b) { return a > b; }
vector<int> adj[1000007];
long long a[1000007], sum[1000007];
bool visited[1000007];
pair<long long, long long> val[1000007];
long long DFS(int u) {
visited[u] = 1;
sum[u] = a[u];
for (int i = (0); i <= ((int)adj[u].size() - 1); ++i) {
int v = adj[u][i];
if (!visited[v]) {
DFS(v);
sum[u] += sum[v];
}
}
return sum[u];
}
void __DFS(int u) {
visited[u] = 1;
vector<long long> vc;
vector<pair<long long, pair<long long, long long> > > vt;
for (int i = (0); i <= ((int)adj[u].size() - 1); ++i) {
int v = adj[u][i];
if (!visited[v]) {
__DFS(v);
sum[u] = max(sum[u], sum[v]);
vc.push_back(sum[v]);
vt.push_back(make_pair(val[v].first + val[v].second, val[v]));
}
}
sort(vc.rbegin(), vc.rend());
sort(vt.rbegin(), vt.rend());
if (vc.size() == 0)
val[u] = make_pair(-1000000000000000007LL, -1000000000000000007LL);
else if (vc.size() == 1)
val[u] = vt[0].second;
else {
if (vt[0].first > vc[0] + vc[1])
val[u] = vt[0].second;
else
val[u] = make_pair(vc[0], vc[1]);
}
return;
}
int main() {
int n;
scanf("%d", &n);
for (int i = (1); i <= (n); ++i) scanf("%lld", &a[i]);
for (int i = (1); i <= (n - 1); ++i) {
int u, v;
scanf("%d%d", &u, &v);
adj[v].push_back(u);
adj[u].push_back(v);
}
DFS(1);
for (int i = (1); i <= (n); ++i) visited[i] = 0;
__DFS(1);
if (val[1].first != -1000000000000000007LL &&
val[1].second != -1000000000000000007LL)
cout << val[1].first + val[1].second << endl;
else
puts("Impossible");
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
T min(T a, T b, T c) {
return min(a, min(b, c));
}
template <class T>
T min(T a, T b, T c, T d) {
return min(a, min(b, min(c, d)));
}
template <class T>
T max(T a, T b, T c) {
return max(a, max(b, c));
}
template <class T>
T max(T a, T b, T c, T d) {
return max(a, max(b, max(c, d)));
}
bool cmp(const int& a, const int& b) { return a > b; }
vector<int> adj[1000007];
long long a[1000007], sum[1000007];
bool visited[1000007];
pair<long long, long long> val[1000007];
long long DFS(int u) {
visited[u] = 1;
sum[u] = a[u];
for (int i = (0); i <= ((int)adj[u].size() - 1); ++i) {
int v = adj[u][i];
if (!visited[v]) {
DFS(v);
sum[u] += sum[v];
}
}
return sum[u];
}
void __DFS(int u) {
visited[u] = 1;
vector<long long> vc;
vector<pair<long long, pair<long long, long long> > > vt;
for (int i = (0); i <= ((int)adj[u].size() - 1); ++i) {
int v = adj[u][i];
if (!visited[v]) {
__DFS(v);
sum[u] = max(sum[u], sum[v]);
vc.push_back(sum[v]);
vt.push_back(make_pair(val[v].first + val[v].second, val[v]));
}
}
sort(vc.rbegin(), vc.rend());
sort(vt.rbegin(), vt.rend());
if (vc.size() == 0)
val[u] = make_pair(-1000000000000000007LL, -1000000000000000007LL);
else if (vc.size() == 1)
val[u] = vt[0].second;
else {
if (vt[0].first > vc[0] + vc[1])
val[u] = vt[0].second;
else
val[u] = make_pair(vc[0], vc[1]);
}
return;
}
int main() {
int n;
scanf("%d", &n);
for (int i = (1); i <= (n); ++i) scanf("%lld", &a[i]);
for (int i = (1); i <= (n - 1); ++i) {
int u, v;
scanf("%d%d", &u, &v);
adj[v].push_back(u);
adj[u].push_back(v);
}
DFS(1);
for (int i = (1); i <= (n); ++i) visited[i] = 0;
__DFS(1);
if (val[1].first != -1000000000000000007LL &&
val[1].second != -1000000000000000007LL)
cout << val[1].first + val[1].second << endl;
else
puts("Impossible");
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> g[200001];
long long ans[200001];
long long a[200001];
long long sum[200001];
int u[200001];
int pr[200001];
void dfs(int v) {
u[v] = 1;
sum[v] = a[v];
for (int i = 0; i < g[v].size(); i++) {
if (!u[g[v][i]]) {
pr[g[v][i]] = v;
dfs(g[v][i]);
sum[v] += sum[g[v][i]];
ans[v] = max(ans[g[v][i]], ans[v]);
}
}
ans[v] = max(ans[v], sum[v]);
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
ans[i] = -1000000000000001;
}
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1);
bool ok = 0;
int start;
long long wer = -1000000000000001;
for (int i = 1; i <= n; i++) {
if ((i == 1 && g[i].size() >= 2) || (g[i].size() >= 3)) {
ok = 1;
start = i;
long long almas = -1000000000000001;
long long almass = -1000000000000001;
int pos = 0;
for (int j = 0; j < g[start].size(); j++) {
if (g[start][j] == pr[start]) continue;
if (ans[g[start][j]] > almas) {
almas = ans[g[start][j]];
pos = j;
}
}
for (int j = 0; j < g[start].size(); j++) {
if (g[start][j] == pr[start]) continue;
if (ans[g[start][j]] > almass && pos != j) {
almass = ans[g[start][j]];
}
}
wer = max(wer, almas + almass);
}
}
if (!ok) {
cout << "Impossible";
;
return 0;
}
cout << wer;
return 0;
}
|
### Prompt
Please formulate a cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> g[200001];
long long ans[200001];
long long a[200001];
long long sum[200001];
int u[200001];
int pr[200001];
void dfs(int v) {
u[v] = 1;
sum[v] = a[v];
for (int i = 0; i < g[v].size(); i++) {
if (!u[g[v][i]]) {
pr[g[v][i]] = v;
dfs(g[v][i]);
sum[v] += sum[g[v][i]];
ans[v] = max(ans[g[v][i]], ans[v]);
}
}
ans[v] = max(ans[v], sum[v]);
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
ans[i] = -1000000000000001;
}
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1);
bool ok = 0;
int start;
long long wer = -1000000000000001;
for (int i = 1; i <= n; i++) {
if ((i == 1 && g[i].size() >= 2) || (g[i].size() >= 3)) {
ok = 1;
start = i;
long long almas = -1000000000000001;
long long almass = -1000000000000001;
int pos = 0;
for (int j = 0; j < g[start].size(); j++) {
if (g[start][j] == pr[start]) continue;
if (ans[g[start][j]] > almas) {
almas = ans[g[start][j]];
pos = j;
}
}
for (int j = 0; j < g[start].size(); j++) {
if (g[start][j] == pr[start]) continue;
if (ans[g[start][j]] > almass && pos != j) {
almass = ans[g[start][j]];
}
}
wer = max(wer, almas + almass);
}
}
if (!ok) {
cout << "Impossible";
;
return 0;
}
cout << wer;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) {
if (a < b) return b;
return a;
}
long long min(long long a, long long b) {
if (a < b) return a;
return b;
}
vector<vector<long long>> v(200005);
long long pleasure[200005], subtree[200005];
long long ans = -((1LL << 60) - 1);
long long sumDfs(long long node, long long par) {
long long sum = 0;
for (auto x : v[node]) {
if (x == par) continue;
sum += sumDfs(x, node);
}
subtree[node] = sum + pleasure[node];
return subtree[node];
}
long long dfs(long long node, long long par) {
vector<pair<long long, long long>> child;
vector<pair<long long, long long>> total;
for (auto x : v[node]) {
if (x == par) continue;
child.push_back({dfs(x, node), x});
total.push_back({subtree[x], x});
}
if (child.empty()) return subtree[node];
sort(child.rbegin(), child.rend());
sort(total.rbegin(), total.rend());
if (child.size() == 1) return max(subtree[node], child[0].first);
ans = max(
{ans, child[0].first + child[1].first, total[0].first + total[1].first});
for (int i = 0; i < total.size(); i++) {
if (total[i].second == child[0].second) continue;
ans = max(ans, child[0].first + total[i].first);
break;
}
for (int i = 0; i < child.size(); i++) {
if (total[0].second == child[i].second) continue;
ans = max(ans, child[i].first + total[0].first);
break;
}
return max({child[0].first, total[0].first, subtree[node]});
}
void solve() {
long long n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> pleasure[i];
for (int i = 0; i < n - 1; i++) {
long long a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
sumDfs(1, -1);
dfs(1, -1);
if (ans == -((1LL << 60) - 1))
cout << "Impossible\n";
else
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
|
### Prompt
Please formulate a cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) {
if (a < b) return b;
return a;
}
long long min(long long a, long long b) {
if (a < b) return a;
return b;
}
vector<vector<long long>> v(200005);
long long pleasure[200005], subtree[200005];
long long ans = -((1LL << 60) - 1);
long long sumDfs(long long node, long long par) {
long long sum = 0;
for (auto x : v[node]) {
if (x == par) continue;
sum += sumDfs(x, node);
}
subtree[node] = sum + pleasure[node];
return subtree[node];
}
long long dfs(long long node, long long par) {
vector<pair<long long, long long>> child;
vector<pair<long long, long long>> total;
for (auto x : v[node]) {
if (x == par) continue;
child.push_back({dfs(x, node), x});
total.push_back({subtree[x], x});
}
if (child.empty()) return subtree[node];
sort(child.rbegin(), child.rend());
sort(total.rbegin(), total.rend());
if (child.size() == 1) return max(subtree[node], child[0].first);
ans = max(
{ans, child[0].first + child[1].first, total[0].first + total[1].first});
for (int i = 0; i < total.size(); i++) {
if (total[i].second == child[0].second) continue;
ans = max(ans, child[0].first + total[i].first);
break;
}
for (int i = 0; i < child.size(); i++) {
if (total[0].second == child[i].second) continue;
ans = max(ans, child[i].first + total[0].first);
break;
}
return max({child[0].first, total[0].first, subtree[node]});
}
void solve() {
long long n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> pleasure[i];
for (int i = 0; i < n - 1; i++) {
long long a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
sumDfs(1, -1);
dfs(1, -1);
if (ans == -((1LL << 60) - 1))
cout << "Impossible\n";
else
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 200010;
const long long inf = 1e18;
long long n, u, v, cnt, ans = -inf, a[maxn], num[maxn], f[maxn], indeg[maxn],
head[maxn << 1];
struct node {
long long to, nxt;
} e[maxn << 1];
void add(long long u, long long v) {
e[++cnt] = (node){v, head[u]};
indeg[u]++, indeg[v]++;
head[u] = cnt;
}
void Dfs(long long x, long long fa) {
if (indeg[x] == 1) {
f[x] = max((long long)0, a[x]);
return;
}
for (long long i = head[x]; i; i = e[i].nxt) {
long long v = e[i].to;
if (v == fa) {
continue;
}
Dfs(v, x);
num[x] += num[v];
ans = max(ans, f[x] + f[v]);
f[x] = max(f[x], f[v]);
}
f[x] = max(f[x], num[x]);
}
signed main() {
scanf("%lld", &n);
for (long long i = 1; i <= n; ++i) {
scanf("%lld", &a[i]);
num[i] = a[i];
}
for (long long i = 1; i < n; ++i) {
scanf("%lld%lld", &u, &v);
add(u, v), add(v, u);
}
memset(f, -63, sizeof(f));
Dfs(1, 1);
if (ans <= -inf) {
cout << "Impossible";
} else {
printf("%lld", ans);
}
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 200010;
const long long inf = 1e18;
long long n, u, v, cnt, ans = -inf, a[maxn], num[maxn], f[maxn], indeg[maxn],
head[maxn << 1];
struct node {
long long to, nxt;
} e[maxn << 1];
void add(long long u, long long v) {
e[++cnt] = (node){v, head[u]};
indeg[u]++, indeg[v]++;
head[u] = cnt;
}
void Dfs(long long x, long long fa) {
if (indeg[x] == 1) {
f[x] = max((long long)0, a[x]);
return;
}
for (long long i = head[x]; i; i = e[i].nxt) {
long long v = e[i].to;
if (v == fa) {
continue;
}
Dfs(v, x);
num[x] += num[v];
ans = max(ans, f[x] + f[v]);
f[x] = max(f[x], f[v]);
}
f[x] = max(f[x], num[x]);
}
signed main() {
scanf("%lld", &n);
for (long long i = 1; i <= n; ++i) {
scanf("%lld", &a[i]);
num[i] = a[i];
}
for (long long i = 1; i < n; ++i) {
scanf("%lld%lld", &u, &v);
add(u, v), add(v, u);
}
memset(f, -63, sizeof(f));
Dfs(1, 1);
if (ans <= -inf) {
cout << "Impossible";
} else {
printf("%lld", ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> A;
vector<vector<int>> adjList, C;
vector<long long> r_solve(int node) {
long long curr_val, max_val, max_sum;
curr_val = max_val = max_sum = 0;
curr_val = A[node];
max_val = max_sum = (long long)-10e16;
vector<long long> S;
for (int child : C[node]) {
vector<long long> cV = r_solve(child);
curr_val += cV[0];
max_sum = max(max_sum, cV[2]);
max_val = max(max_val, cV[1]);
S.push_back(cV[1]);
}
max_val = max(max_val, curr_val);
sort(S.rbegin(), S.rend());
if (S.size() >= 2) max_sum = max(max_sum, S[0] + S[1]);
return vector<long long>{curr_val, max_val, max_sum};
}
int main() {
int n;
cin >> n;
A.resize(n);
for (int i = 0; i < n; i++) scanf("%d", &A[i]);
adjList.resize(n);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d %d", &u, &v);
u--;
v--;
adjList[u].push_back(v);
adjList[v].push_back(u);
}
C.resize(n);
deque<int> Q{0};
bool isPath = true;
while (!Q.empty()) {
int curr = Q.front();
Q.pop_front();
for (int next : adjList[curr]) {
if (C[next].size()) continue;
C[curr].push_back(next);
Q.push_back(next);
}
if (C[curr].size() > 1) isPath = false;
}
vector<long long> result = r_solve(0);
cout << (isPath ? "Impossible" : to_string(result[2])) << endl;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> A;
vector<vector<int>> adjList, C;
vector<long long> r_solve(int node) {
long long curr_val, max_val, max_sum;
curr_val = max_val = max_sum = 0;
curr_val = A[node];
max_val = max_sum = (long long)-10e16;
vector<long long> S;
for (int child : C[node]) {
vector<long long> cV = r_solve(child);
curr_val += cV[0];
max_sum = max(max_sum, cV[2]);
max_val = max(max_val, cV[1]);
S.push_back(cV[1]);
}
max_val = max(max_val, curr_val);
sort(S.rbegin(), S.rend());
if (S.size() >= 2) max_sum = max(max_sum, S[0] + S[1]);
return vector<long long>{curr_val, max_val, max_sum};
}
int main() {
int n;
cin >> n;
A.resize(n);
for (int i = 0; i < n; i++) scanf("%d", &A[i]);
adjList.resize(n);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d %d", &u, &v);
u--;
v--;
adjList[u].push_back(v);
adjList[v].push_back(u);
}
C.resize(n);
deque<int> Q{0};
bool isPath = true;
while (!Q.empty()) {
int curr = Q.front();
Q.pop_front();
for (int next : adjList[curr]) {
if (C[next].size()) continue;
C[curr].push_back(next);
Q.push_back(next);
}
if (C[curr].size() > 1) isPath = false;
}
vector<long long> result = r_solve(0);
cout << (isPath ? "Impossible" : to_string(result[2])) << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 200003;
long long inf = -10e17 + 3;
vector<long long> v[N];
long long sum[N], msum[N], val[N], ans = inf;
void dfs(long long ver, long long par) {
msum[ver] = inf;
for (auto it : v[ver]) {
if (it == par) continue;
dfs(it, ver);
if (msum[it] != inf && msum[ver] != inf)
ans = max(ans, msum[it] + msum[ver]);
sum[ver] += sum[it];
msum[ver] = max(msum[ver], msum[it]);
}
sum[ver] += val[ver];
msum[ver] = max(msum[ver], sum[ver]);
}
int main() {
std::ios::sync_with_stdio(false);
long long n, x, y;
cin >> n;
for (int i = int(1); i <= int(n); i++) cin >> val[i];
for (int i = int(1); i <= int(n - 1); i++) {
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1, 0);
if (ans == inf)
cout << "Impossible";
else
cout << ans;
}
|
### Prompt
Create a solution in CPP for the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 200003;
long long inf = -10e17 + 3;
vector<long long> v[N];
long long sum[N], msum[N], val[N], ans = inf;
void dfs(long long ver, long long par) {
msum[ver] = inf;
for (auto it : v[ver]) {
if (it == par) continue;
dfs(it, ver);
if (msum[it] != inf && msum[ver] != inf)
ans = max(ans, msum[it] + msum[ver]);
sum[ver] += sum[it];
msum[ver] = max(msum[ver], msum[it]);
}
sum[ver] += val[ver];
msum[ver] = max(msum[ver], sum[ver]);
}
int main() {
std::ios::sync_with_stdio(false);
long long n, x, y;
cin >> n;
for (int i = int(1); i <= int(n); i++) cin >> val[i];
for (int i = int(1); i <= int(n - 1); i++) {
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1, 0);
if (ans == inf)
cout << "Impossible";
else
cout << ans;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const int N = 2 * 1e5 + 10;
int tin[2 * N], tout[2 * N];
long long t[8 * N], a[N];
int timer, ver[2 * N];
vector<int> g[N];
void dfs(int v, int p = -1) {
tin[v] = ++timer;
ver[timer] = v;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (to == p) continue;
dfs(to, v);
a[v] += a[to];
}
tout[v] = timer;
}
long long getMax(int l, int r, int j, int x, int y) {
if (r < x || l > y) return -INF;
if (x <= l && r <= y) {
return t[j];
}
int mid = (l + r) >> 1;
long long X = getMax(l, mid, 2 * j, x, y);
long long Y = getMax(mid + 1, r, 2 * j + 1, x, y);
return max(X, Y);
}
void buildTree(int l, int r, int j) {
if (l > r) return;
if (l == r) {
t[j] = a[ver[l]];
return;
}
int mid = (l + r) >> 1;
buildTree(l, mid, 2 * j);
buildTree(mid + 1, r, 2 * j + 1);
if (l != r) {
t[j] = max(t[2 * j], t[2 * j + 1]);
}
}
int n, deg[N];
int main() {
cin >> n;
if (n == 1) return cout << "Impossible", 0;
for (int i = 1; i <= n; i++) cin >> a[i];
int leaves = n;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
deg[x]++;
deg[y]++;
}
int lf = 0;
for (int i = 2; i <= n; i++) {
if (deg[i] == 1) lf++;
}
if (lf == 1) return cout << "Impossible", 0;
dfs(1);
fill(t, t + sizeof(t) / sizeof(t[0]), -INF);
buildTree(1, timer, 1);
long long res = -INF;
for (int i = 2; i <= n; i++) {
long long cur = a[i];
long long val = getMax(1, timer, 1, tout[i] + 1, timer);
res = max(res, cur + val);
}
cout << res;
return 0;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const int N = 2 * 1e5 + 10;
int tin[2 * N], tout[2 * N];
long long t[8 * N], a[N];
int timer, ver[2 * N];
vector<int> g[N];
void dfs(int v, int p = -1) {
tin[v] = ++timer;
ver[timer] = v;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (to == p) continue;
dfs(to, v);
a[v] += a[to];
}
tout[v] = timer;
}
long long getMax(int l, int r, int j, int x, int y) {
if (r < x || l > y) return -INF;
if (x <= l && r <= y) {
return t[j];
}
int mid = (l + r) >> 1;
long long X = getMax(l, mid, 2 * j, x, y);
long long Y = getMax(mid + 1, r, 2 * j + 1, x, y);
return max(X, Y);
}
void buildTree(int l, int r, int j) {
if (l > r) return;
if (l == r) {
t[j] = a[ver[l]];
return;
}
int mid = (l + r) >> 1;
buildTree(l, mid, 2 * j);
buildTree(mid + 1, r, 2 * j + 1);
if (l != r) {
t[j] = max(t[2 * j], t[2 * j + 1]);
}
}
int n, deg[N];
int main() {
cin >> n;
if (n == 1) return cout << "Impossible", 0;
for (int i = 1; i <= n; i++) cin >> a[i];
int leaves = n;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
deg[x]++;
deg[y]++;
}
int lf = 0;
for (int i = 2; i <= n; i++) {
if (deg[i] == 1) lf++;
}
if (lf == 1) return cout << "Impossible", 0;
dfs(1);
fill(t, t + sizeof(t) / sizeof(t[0]), -INF);
buildTree(1, timer, 1);
long long res = -INF;
for (int i = 2; i <= n; i++) {
long long cur = a[i];
long long val = getMax(1, timer, 1, tout[i] + 1, timer);
res = max(res, cur + val);
}
cout << res;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v[200005];
int vis[200005];
long long arr[200005];
long long su[200005];
long long dp[200005];
int n;
int cou;
long long fin;
long long bes1, bes2;
const long long INF = 1E16;
void dfs(int ind) {
vis[ind] = 1;
dp[ind] = -INF;
su[ind] = arr[ind];
vector<long long> vec;
for (int i = 0; i < v[ind].size(); i++) {
if (!vis[v[ind][i]]) {
dfs(v[ind][i]);
su[ind] += su[v[ind][i]];
dp[ind] = max(dp[v[ind][i]], dp[ind]);
vec.push_back(dp[v[ind][i]]);
}
}
dp[ind] = max(dp[ind], su[ind]);
if (vec.size() >= 2) {
cou = 1;
sort(vec.begin(), vec.end());
reverse(vec.begin(), vec.end());
long long xx = vec[0] + vec[1];
if (xx > fin) {
fin = xx;
}
}
}
int main() {
scanf("%d", &n);
int i, j;
bes1 = bes2 = fin = -INF;
for (i = 1; i <= n; i++) scanf("%I64d", &arr[i]);
for (i = 0; i < n - 1; i++) {
int a, b;
scanf("%d %d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
dfs(1);
if (cou == 1)
printf("%I64d\n", fin);
else
printf("Impossible\n");
}
|
### Prompt
In CPP, your task is to solve the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> v[200005];
int vis[200005];
long long arr[200005];
long long su[200005];
long long dp[200005];
int n;
int cou;
long long fin;
long long bes1, bes2;
const long long INF = 1E16;
void dfs(int ind) {
vis[ind] = 1;
dp[ind] = -INF;
su[ind] = arr[ind];
vector<long long> vec;
for (int i = 0; i < v[ind].size(); i++) {
if (!vis[v[ind][i]]) {
dfs(v[ind][i]);
su[ind] += su[v[ind][i]];
dp[ind] = max(dp[v[ind][i]], dp[ind]);
vec.push_back(dp[v[ind][i]]);
}
}
dp[ind] = max(dp[ind], su[ind]);
if (vec.size() >= 2) {
cou = 1;
sort(vec.begin(), vec.end());
reverse(vec.begin(), vec.end());
long long xx = vec[0] + vec[1];
if (xx > fin) {
fin = xx;
}
}
}
int main() {
scanf("%d", &n);
int i, j;
bes1 = bes2 = fin = -INF;
for (i = 1; i <= n; i++) scanf("%I64d", &arr[i]);
for (i = 0; i < n - 1; i++) {
int a, b;
scanf("%d %d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
dfs(1);
if (cou == 1)
printf("%I64d\n", fin);
else
printf("Impossible\n");
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double PI = 3.141592653589793238;
long long powmod(long long a, long long b) {
long long res = 1;
a %= 1000000007;
for (; b; b >>= 1) {
if (b & 1) res = res * a % 1000000007;
a = a * a % 1000000007;
}
return res;
}
vector<int> vec[200005];
long long wt[200005], sub[200005], part[200005], ans;
int root = 1;
void solve(int node, int par) {
sub[node] = wt[node];
long long m1 = -(1e18 + 1 + 1e18 + 1);
long long m2 = -(1e18 + 1 + 1e18 + 1);
part[node] = -1e18 + 1;
for (int i = 0; i < vec[node].size(); i++) {
int Node = vec[node][i];
if (Node == par) continue;
solve(Node, node);
sub[node] += sub[Node];
if (m1 < part[Node]) {
m2 = m1;
m1 = part[Node];
} else {
m2 = max(m2, part[Node]);
}
}
part[node] = max(part[node], m1);
part[node] = max(part[node], sub[node]);
ans = max(ans, m1 + m2);
}
int main() {
int n, u, v;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &wt[i]);
for (int i = 0; i < n - 1; i++) {
scanf("%d", &u);
scanf("%d", &v);
vec[u].push_back(v);
vec[v].push_back(u);
}
ans = -1e18 + 1;
solve(1, -1);
if (ans <= -1e18 + 1) {
printf("Impossible\n");
} else {
printf("%lld\n", ans);
}
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double PI = 3.141592653589793238;
long long powmod(long long a, long long b) {
long long res = 1;
a %= 1000000007;
for (; b; b >>= 1) {
if (b & 1) res = res * a % 1000000007;
a = a * a % 1000000007;
}
return res;
}
vector<int> vec[200005];
long long wt[200005], sub[200005], part[200005], ans;
int root = 1;
void solve(int node, int par) {
sub[node] = wt[node];
long long m1 = -(1e18 + 1 + 1e18 + 1);
long long m2 = -(1e18 + 1 + 1e18 + 1);
part[node] = -1e18 + 1;
for (int i = 0; i < vec[node].size(); i++) {
int Node = vec[node][i];
if (Node == par) continue;
solve(Node, node);
sub[node] += sub[Node];
if (m1 < part[Node]) {
m2 = m1;
m1 = part[Node];
} else {
m2 = max(m2, part[Node]);
}
}
part[node] = max(part[node], m1);
part[node] = max(part[node], sub[node]);
ans = max(ans, m1 + m2);
}
int main() {
int n, u, v;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &wt[i]);
for (int i = 0; i < n - 1; i++) {
scanf("%d", &u);
scanf("%d", &v);
vec[u].push_back(v);
vec[v].push_back(u);
}
ans = -1e18 + 1;
solve(1, -1);
if (ans <= -1e18 + 1) {
printf("Impossible\n");
} else {
printf("%lld\n", ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200001;
vector<int> g[N];
long long a[N], d[N], s[N];
bool joke = false;
long long sum(int v, int p) {
if (s[v] != -11111LL) {
return s[v];
}
s[v] = 0LL;
for (int i = 0; i < (int)g[v].size(); ++i) {
int u = g[v][i];
if (u != p) {
s[v] += sum(u, v);
}
}
s[v] += a[v];
return s[v];
}
long long f(int v, int p) {
if (d[v] != -11111LL) {
return d[v];
}
d[v] = s[v];
int c = 0;
for (int i = 0; i < (int)g[v].size(); ++i) {
int u = g[v][i];
if (u != p) {
d[v] = max(d[v], f(u, v));
++c;
}
}
if (c > 1) {
joke = true;
}
if (c == 0) {
d[v] = a[v];
return d[v];
}
return d[v];
}
long long ans(int v, int p) {
int c = 0;
long long max1 = -2000000000, max2 = -2000000000;
for (int i = 0; i < (int)g[v].size(); ++i) {
int u = g[v][i];
if (u != p) {
++c;
if (d[u] > max1) {
max2 = max1;
max1 = d[u];
} else if (d[u] > max2) {
max2 = d[u];
}
}
}
long long answer = -(2e9 + 1111111);
if (c > 1) {
answer = max1 + max2;
}
for (int i = 0; i < (int)g[v].size(); ++i) {
int u = g[v][i];
if (u != p) {
answer = max(answer, ans(u, v));
}
}
return answer;
}
int main() {
for (int i = 0; i < N; ++i) {
d[i] = -11111LL;
s[i] = -11111LL;
}
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
sum(1, -1);
f(1, -1);
if (!joke) {
cout << "Impossible" << endl;
} else {
cout << ans(1, -1) << endl;
}
return 0;
}
|
### Prompt
Create a solution in CPP for the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 200001;
vector<int> g[N];
long long a[N], d[N], s[N];
bool joke = false;
long long sum(int v, int p) {
if (s[v] != -11111LL) {
return s[v];
}
s[v] = 0LL;
for (int i = 0; i < (int)g[v].size(); ++i) {
int u = g[v][i];
if (u != p) {
s[v] += sum(u, v);
}
}
s[v] += a[v];
return s[v];
}
long long f(int v, int p) {
if (d[v] != -11111LL) {
return d[v];
}
d[v] = s[v];
int c = 0;
for (int i = 0; i < (int)g[v].size(); ++i) {
int u = g[v][i];
if (u != p) {
d[v] = max(d[v], f(u, v));
++c;
}
}
if (c > 1) {
joke = true;
}
if (c == 0) {
d[v] = a[v];
return d[v];
}
return d[v];
}
long long ans(int v, int p) {
int c = 0;
long long max1 = -2000000000, max2 = -2000000000;
for (int i = 0; i < (int)g[v].size(); ++i) {
int u = g[v][i];
if (u != p) {
++c;
if (d[u] > max1) {
max2 = max1;
max1 = d[u];
} else if (d[u] > max2) {
max2 = d[u];
}
}
}
long long answer = -(2e9 + 1111111);
if (c > 1) {
answer = max1 + max2;
}
for (int i = 0; i < (int)g[v].size(); ++i) {
int u = g[v][i];
if (u != p) {
answer = max(answer, ans(u, v));
}
}
return answer;
}
int main() {
for (int i = 0; i < N; ++i) {
d[i] = -11111LL;
s[i] = -11111LL;
}
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
sum(1, -1);
f(1, -1);
if (!joke) {
cout << "Impossible" << endl;
} else {
cout << ans(1, -1) << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long int inf = 1e15;
const long long int mod = 1000000007;
const int infInt = 1e9;
const double PI = acos(-1.00);
const double eps = 1e-6;
long long int weight[200010];
long long int dp[200010];
vector<vector<int> > adj;
vector<vector<int> > level;
int vis[200010];
int levelMax;
void dfs(int u, int lev) {
vis[u] = true;
levelMax = max(levelMax, lev);
level[lev].push_back(u);
if (adj[u].size() == 0) {
dp[u] = weight[u];
return;
}
dp[u] = -2LL * inf;
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i];
if (vis[v] == false) {
dfs(v, lev + 1);
weight[u] += weight[v];
dp[u] = max(dp[u], dp[v]);
}
}
dp[u] = max(dp[u], weight[u]);
}
long long int maxi = -2LL * inf;
bool solve() {
bool possible = false;
for (int i = 1; i <= levelMax; i++) {
int len = level[i].size();
if (len <= 1) continue;
possible = true;
vector<long long int> maxis;
for (int j = 0; j < len; j++) {
long long int maximo = dp[level[i][j]];
maxis.push_back(maximo);
}
sort(maxis.begin(), maxis.end());
maxi = max(maxi, maxis[len - 1] + maxis[len - 2]);
}
return possible;
}
int main() {
cin.sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
adj.assign(n + 1, vector<int>());
level.assign(n + 1, vector<int>());
for (int i = 1; i <= n; i++) {
cin >> weight[i];
}
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 0);
if (solve()) {
cout << maxi << "\n";
} else {
cout << "Impossible"
<< "\n";
}
}
|
### Prompt
Develop a solution in CPP to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int inf = 1e15;
const long long int mod = 1000000007;
const int infInt = 1e9;
const double PI = acos(-1.00);
const double eps = 1e-6;
long long int weight[200010];
long long int dp[200010];
vector<vector<int> > adj;
vector<vector<int> > level;
int vis[200010];
int levelMax;
void dfs(int u, int lev) {
vis[u] = true;
levelMax = max(levelMax, lev);
level[lev].push_back(u);
if (adj[u].size() == 0) {
dp[u] = weight[u];
return;
}
dp[u] = -2LL * inf;
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i];
if (vis[v] == false) {
dfs(v, lev + 1);
weight[u] += weight[v];
dp[u] = max(dp[u], dp[v]);
}
}
dp[u] = max(dp[u], weight[u]);
}
long long int maxi = -2LL * inf;
bool solve() {
bool possible = false;
for (int i = 1; i <= levelMax; i++) {
int len = level[i].size();
if (len <= 1) continue;
possible = true;
vector<long long int> maxis;
for (int j = 0; j < len; j++) {
long long int maximo = dp[level[i][j]];
maxis.push_back(maximo);
}
sort(maxis.begin(), maxis.end());
maxi = max(maxi, maxis[len - 1] + maxis[len - 2]);
}
return possible;
}
int main() {
cin.sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
adj.assign(n + 1, vector<int>());
level.assign(n + 1, vector<int>());
for (int i = 1; i <= n; i++) {
cin >> weight[i];
}
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 0);
if (solve()) {
cout << maxi << "\n";
} else {
cout << "Impossible"
<< "\n";
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
long long ln, lk, lm;
void etp(bool f = 0) {
puts(f ? "yes" : "no");
exit(0);
}
void addmod(int &x, int y, int mod = 1000000007) {
x += y;
if (x >= mod) x -= mod;
assert(x >= 0 && x < mod);
}
void et() {
puts("-1");
exit(0);
}
const int MN = (1 << 20) + 5;
vector<int> mp[200135];
int a[200135], st[200135], ed[200135], cnt, rid[200135];
long long sa[200135];
void dfs(int x, int pa) {
st[x] = ++cnt;
rid[cnt] = x;
sa[x] = a[x];
for (int c : mp[x])
if (c != pa) {
dfs(c, x);
sa[x] += sa[c];
}
ed[x] = cnt;
}
struct mb {
int M;
long long T[MN];
void init(int sz) {
for (M = 1; sz + 2 > M; M <<= 1)
;
for (int(i) = 0; (i) < (int)(MN); (i)++) T[i] = -(1LL << 60);
for (int(i) = 1; (i) <= (int)(sz); (i)++) T[M + i] = sa[rid[i]];
for (int i = M - 1; i; i--) T[i] = max(T[i << 1], T[i << 1 | 1]);
}
void upt(int l, long long val) {
l += M;
T[l] = val;
while (l > 1) {
T[l >> 1] = max(T[l], T[l ^ 1]);
l >>= 1;
}
}
long long qy(int l, int r) {
if (r < l) return -(1LL << 60);
long long ans = -(1LL << 60);
for (l = l + M - 1, r = r + M + 1; l ^ r ^ 1; l >>= 1, r >>= 1) {
if (~l & 1) ans = max(ans, T[l ^ 1]);
if (r & 1) ans = max(ans, T[r ^ 1]);
}
return ans;
}
} T;
long long ans = -(1LL << 60);
void cal(int x, int pa) {
long long gg = max(T.qy(1, st[x] - 1), T.qy(ed[x] + 1, n));
if (gg != -(1LL << 60)) {
ans = max(ans, gg + sa[x]);
}
T.upt(st[x], -(1LL << 60));
for (int c : mp[x])
if (c != pa) cal(c, x);
T.upt(st[x], sa[x]);
}
void fmain() {
scanf("%d", &n);
for (int(i) = 1; (i) <= (int)(n); (i)++) scanf("%d", a + i);
for (int i = 0, u, v; i < n - 1; i++) {
scanf("%d%d", &u, &v);
mp[u].push_back(v);
mp[v].push_back(u);
};
dfs(1, 0);
T.init(n);
cal(1, 0);
if (ans == -(1LL << 60))
puts("Impossible");
else
printf("%lld\n", ans);
}
int main() {
int t = 1;
for (int(i) = 1; (i) <= (int)(t); (i)++) {
fmain();
}
return 0;
}
|
### Prompt
Create a solution in Cpp for the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
long long ln, lk, lm;
void etp(bool f = 0) {
puts(f ? "yes" : "no");
exit(0);
}
void addmod(int &x, int y, int mod = 1000000007) {
x += y;
if (x >= mod) x -= mod;
assert(x >= 0 && x < mod);
}
void et() {
puts("-1");
exit(0);
}
const int MN = (1 << 20) + 5;
vector<int> mp[200135];
int a[200135], st[200135], ed[200135], cnt, rid[200135];
long long sa[200135];
void dfs(int x, int pa) {
st[x] = ++cnt;
rid[cnt] = x;
sa[x] = a[x];
for (int c : mp[x])
if (c != pa) {
dfs(c, x);
sa[x] += sa[c];
}
ed[x] = cnt;
}
struct mb {
int M;
long long T[MN];
void init(int sz) {
for (M = 1; sz + 2 > M; M <<= 1)
;
for (int(i) = 0; (i) < (int)(MN); (i)++) T[i] = -(1LL << 60);
for (int(i) = 1; (i) <= (int)(sz); (i)++) T[M + i] = sa[rid[i]];
for (int i = M - 1; i; i--) T[i] = max(T[i << 1], T[i << 1 | 1]);
}
void upt(int l, long long val) {
l += M;
T[l] = val;
while (l > 1) {
T[l >> 1] = max(T[l], T[l ^ 1]);
l >>= 1;
}
}
long long qy(int l, int r) {
if (r < l) return -(1LL << 60);
long long ans = -(1LL << 60);
for (l = l + M - 1, r = r + M + 1; l ^ r ^ 1; l >>= 1, r >>= 1) {
if (~l & 1) ans = max(ans, T[l ^ 1]);
if (r & 1) ans = max(ans, T[r ^ 1]);
}
return ans;
}
} T;
long long ans = -(1LL << 60);
void cal(int x, int pa) {
long long gg = max(T.qy(1, st[x] - 1), T.qy(ed[x] + 1, n));
if (gg != -(1LL << 60)) {
ans = max(ans, gg + sa[x]);
}
T.upt(st[x], -(1LL << 60));
for (int c : mp[x])
if (c != pa) cal(c, x);
T.upt(st[x], sa[x]);
}
void fmain() {
scanf("%d", &n);
for (int(i) = 1; (i) <= (int)(n); (i)++) scanf("%d", a + i);
for (int i = 0, u, v; i < n - 1; i++) {
scanf("%d%d", &u, &v);
mp[u].push_back(v);
mp[v].push_back(u);
};
dfs(1, 0);
T.init(n);
cal(1, 0);
if (ans == -(1LL << 60))
puts("Impossible");
else
printf("%lld\n", ans);
}
int main() {
int t = 1;
for (int(i) = 1; (i) <= (int)(t); (i)++) {
fmain();
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long int INF = 98769876987698889LL;
const int MOD = 1e9 + 7;
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dx4[] = {1, -1, 0, 0};
const int dy4[] = {0, 0, 1, -1};
const int N = 2e5 + 5;
long long int dp1[N], a[N], f[N], dp2[N], s[N];
int n;
vector<int> G[N];
void dfs(int x, int pa) {
if (G[x].size() == 1 and G[x][0] == pa) {
s[x] = a[x];
return;
}
s[x] = a[x];
for (auto k : G[x]) {
if (k == pa) continue;
dfs(k, x);
s[x] += s[k];
}
}
void dfs2(int x, int pa) {
if (G[x].size() == 1 and G[x][0] == pa) {
f[x] = 0;
return;
}
f[x] = 0;
vector<long long int> v;
for (auto k : G[x]) {
if (k == pa) continue;
dfs2(k, x);
if (s[k] >= dp1[k]) {
v.push_back(s[k]);
} else {
v.push_back(dp1[k]);
}
}
sort(v.begin(), v.end());
dp1[x] = v[v.size() - 1];
if (v.size() > 1) {
f[x] = 1;
dp2[x] = v[v.size() - 2];
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < N; i++) {
dp1[i] = -INF;
dp2[i] = -INF;
}
for (int i = 2; i <= n; i++) {
int x, y;
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
if (n == 1) {
cout << "Impossible" << endl;
return 0;
}
dfs(1, 0);
dfs2(1, 0);
int fl = 0;
for (int i = 1; i <= n; i++) {
if (f[i]) fl = 1;
}
if (!fl) {
cout << "Impossible" << endl;
return 0;
}
long long int ans = -INF;
for (int i = 1; i <= n; i++) {
if (f[i]) {
ans = max(ans, dp1[i] + dp2[i]);
}
}
cout << ans;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int INF = 98769876987698889LL;
const int MOD = 1e9 + 7;
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dx4[] = {1, -1, 0, 0};
const int dy4[] = {0, 0, 1, -1};
const int N = 2e5 + 5;
long long int dp1[N], a[N], f[N], dp2[N], s[N];
int n;
vector<int> G[N];
void dfs(int x, int pa) {
if (G[x].size() == 1 and G[x][0] == pa) {
s[x] = a[x];
return;
}
s[x] = a[x];
for (auto k : G[x]) {
if (k == pa) continue;
dfs(k, x);
s[x] += s[k];
}
}
void dfs2(int x, int pa) {
if (G[x].size() == 1 and G[x][0] == pa) {
f[x] = 0;
return;
}
f[x] = 0;
vector<long long int> v;
for (auto k : G[x]) {
if (k == pa) continue;
dfs2(k, x);
if (s[k] >= dp1[k]) {
v.push_back(s[k]);
} else {
v.push_back(dp1[k]);
}
}
sort(v.begin(), v.end());
dp1[x] = v[v.size() - 1];
if (v.size() > 1) {
f[x] = 1;
dp2[x] = v[v.size() - 2];
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < N; i++) {
dp1[i] = -INF;
dp2[i] = -INF;
}
for (int i = 2; i <= n; i++) {
int x, y;
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
if (n == 1) {
cout << "Impossible" << endl;
return 0;
}
dfs(1, 0);
dfs2(1, 0);
int fl = 0;
for (int i = 1; i <= n; i++) {
if (f[i]) fl = 1;
}
if (!fl) {
cout << "Impossible" << endl;
return 0;
}
long long int ans = -INF;
for (int i = 1; i <= n; i++) {
if (f[i]) {
ans = max(ans, dp1[i] + dp2[i]);
}
}
cout << ans;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e17;
const int N = 3e5 + 7;
int n, m;
int c[N];
vector<int> g[N];
long long w[N], sum = -inf, f[N];
bool imp = true;
void dfs(int v, int p) {
w[v] += c[v];
int cnt = 0;
for (int u : g[v]) {
if (u != p) {
dfs(u, v);
f[v] = max(f[u], f[v]);
w[v] += w[u];
cnt++;
}
}
if (cnt > 1) imp = false;
f[v] = max(f[v], w[v]);
long long mx1 = -inf, mx2 = -inf;
for (int u : g[v]) {
if (u == p) continue;
long long cur = f[u];
if (mx1 < cur) swap(mx1, cur);
if (mx2 < cur) swap(mx2, cur);
}
if (mx2 > -inf) sum = max(sum, mx1 + mx2);
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", c + i);
}
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d %d", &u, &v);
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 0; i < N; i++) {
f[i] = -inf;
}
dfs(0, -1);
if (imp == true)
printf("Impossible\n");
else
printf("%lld\n", sum);
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e17;
const int N = 3e5 + 7;
int n, m;
int c[N];
vector<int> g[N];
long long w[N], sum = -inf, f[N];
bool imp = true;
void dfs(int v, int p) {
w[v] += c[v];
int cnt = 0;
for (int u : g[v]) {
if (u != p) {
dfs(u, v);
f[v] = max(f[u], f[v]);
w[v] += w[u];
cnt++;
}
}
if (cnt > 1) imp = false;
f[v] = max(f[v], w[v]);
long long mx1 = -inf, mx2 = -inf;
for (int u : g[v]) {
if (u == p) continue;
long long cur = f[u];
if (mx1 < cur) swap(mx1, cur);
if (mx2 < cur) swap(mx2, cur);
}
if (mx2 > -inf) sum = max(sum, mx1 + mx2);
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", c + i);
}
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d %d", &u, &v);
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 0; i < N; i++) {
f[i] = -inf;
}
dfs(0, -1);
if (imp == true)
printf("Impossible\n");
else
printf("%lld\n", sum);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> tree[200010];
long long a[200010];
long long sumx[200010];
long long mxx[200010];
long long gl;
void dfs(int v, int par) {
long long ans = a[v];
for (int i = 0; i < tree[v].size(); i++) {
int u = tree[v][i];
if (u != par) {
dfs(u, v);
ans += sumx[u];
}
}
sumx[v] = ans;
}
void dfs2(int v, int par) {
long long ans = sumx[v];
for (int i = 0; i < tree[v].size(); i++) {
int u = tree[v][i];
if (u != par) {
dfs2(u, v);
ans = max(ans, mxx[u]);
}
}
mxx[v] = ans;
}
void dfs3(int v, int par) {
vector<long long> vc;
for (int i = 0; i < tree[v].size(); i++) {
int u = tree[v][i];
if (u != par) {
vc.push_back(mxx[u]);
dfs3(u, v);
}
}
if (vc.size() >= 2) {
sort(vc.begin(), vc.end());
int oo = vc.size();
gl = max(gl, vc[oo - 1] + vc[oo - 2]);
}
}
int main() {
ios::sync_with_stdio(false);
int n, u, v;
cin >> n;
int i, j;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
for (i = 0; i < n - 1; i++) {
cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
dfs(1, -1);
dfs2(1, -1);
gl = LLONG_MIN;
dfs3(1, -1);
if (gl == LLONG_MIN)
cout << "Impossible";
else {
cout << gl;
}
}
|
### Prompt
Create a solution in CPP for the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> tree[200010];
long long a[200010];
long long sumx[200010];
long long mxx[200010];
long long gl;
void dfs(int v, int par) {
long long ans = a[v];
for (int i = 0; i < tree[v].size(); i++) {
int u = tree[v][i];
if (u != par) {
dfs(u, v);
ans += sumx[u];
}
}
sumx[v] = ans;
}
void dfs2(int v, int par) {
long long ans = sumx[v];
for (int i = 0; i < tree[v].size(); i++) {
int u = tree[v][i];
if (u != par) {
dfs2(u, v);
ans = max(ans, mxx[u]);
}
}
mxx[v] = ans;
}
void dfs3(int v, int par) {
vector<long long> vc;
for (int i = 0; i < tree[v].size(); i++) {
int u = tree[v][i];
if (u != par) {
vc.push_back(mxx[u]);
dfs3(u, v);
}
}
if (vc.size() >= 2) {
sort(vc.begin(), vc.end());
int oo = vc.size();
gl = max(gl, vc[oo - 1] + vc[oo - 2]);
}
}
int main() {
ios::sync_with_stdio(false);
int n, u, v;
cin >> n;
int i, j;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
for (i = 0; i < n - 1; i++) {
cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
dfs(1, -1);
dfs2(1, -1);
gl = LLONG_MIN;
dfs3(1, -1);
if (gl == LLONG_MIN)
cout << "Impossible";
else {
cout << gl;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
int n;
int a[200005];
int u[200005];
int v[200005];
vector<int> edges[200005];
long long int dp[200005][2];
bool visited[200005];
long long int ans;
bool good;
pair<long long int, long long int> dfs(int node, int par) {
if (visited[node]) return make_pair(dp[node][0], dp[node][1]);
dp[node][0] = a[node];
dp[node][1] = -inf;
pair<long long int, long long int> tmp;
int cnt = 0;
for (int i = 0; i < edges[node].size(); ++i) {
if (edges[node][i] == par) continue;
++cnt;
tmp = dfs(edges[node][i], node);
dp[node][0] += tmp.first;
dp[node][1] = max(dp[node][1], tmp.second);
}
if (cnt > 1) good = 1;
dp[node][1] = max(dp[node][1], dp[node][0]);
long long int first = -(long long int)inf * 1000000;
long long int second = -(long long int)inf * 1000000;
long long int t;
for (int i = 0; i < edges[node].size(); ++i) {
if (edges[node][i] == par) continue;
t = dp[edges[node][i]][1];
if (t > first) {
second = first;
first = t;
} else if (t > second) {
second = t;
}
}
ans = max(ans, first + second);
visited[node] = 1;
return make_pair(dp[node][0], dp[node][1]);
}
int main() {
ios_base::sync_with_stdio(0);
while (cin >> n) {
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
memset(visited, 0, sizeof(visited));
for (int i = 1; i < n; ++i) {
cin >> u[i] >> v[i];
edges[u[i]].push_back(v[i]);
edges[v[i]].push_back(u[i]);
}
ans = -(long long int)inf * 1000000;
good = 0;
dfs(1, -1);
if (!good) {
cout << "Impossible\n";
} else {
cout << ans << "\n";
}
for (int i = 1; i <= n; ++i) edges[i].clear();
}
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
int n;
int a[200005];
int u[200005];
int v[200005];
vector<int> edges[200005];
long long int dp[200005][2];
bool visited[200005];
long long int ans;
bool good;
pair<long long int, long long int> dfs(int node, int par) {
if (visited[node]) return make_pair(dp[node][0], dp[node][1]);
dp[node][0] = a[node];
dp[node][1] = -inf;
pair<long long int, long long int> tmp;
int cnt = 0;
for (int i = 0; i < edges[node].size(); ++i) {
if (edges[node][i] == par) continue;
++cnt;
tmp = dfs(edges[node][i], node);
dp[node][0] += tmp.first;
dp[node][1] = max(dp[node][1], tmp.second);
}
if (cnt > 1) good = 1;
dp[node][1] = max(dp[node][1], dp[node][0]);
long long int first = -(long long int)inf * 1000000;
long long int second = -(long long int)inf * 1000000;
long long int t;
for (int i = 0; i < edges[node].size(); ++i) {
if (edges[node][i] == par) continue;
t = dp[edges[node][i]][1];
if (t > first) {
second = first;
first = t;
} else if (t > second) {
second = t;
}
}
ans = max(ans, first + second);
visited[node] = 1;
return make_pair(dp[node][0], dp[node][1]);
}
int main() {
ios_base::sync_with_stdio(0);
while (cin >> n) {
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
memset(visited, 0, sizeof(visited));
for (int i = 1; i < n; ++i) {
cin >> u[i] >> v[i];
edges[u[i]].push_back(v[i]);
edges[v[i]].push_back(u[i]);
}
ans = -(long long int)inf * 1000000;
good = 0;
dfs(1, -1);
if (!good) {
cout << "Impossible\n";
} else {
cout << ans << "\n";
}
for (int i = 1; i <= n; ++i) edges[i].clear();
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)1e18 + 1;
const int N = 300300;
int n;
long long ans = -INF;
long long a[N];
long long sum[N];
vector<int> g[N];
bool used[N];
void dfs(int v) {
used[v] = 1;
sum[v] = a[v];
a[v] = -INF;
long long m1 = -INF, m2 = -INF;
for (int u : g[v]) {
if (used[u]) continue;
dfs(u);
sum[v] += sum[u];
long long val = a[u];
a[v] = max(a[v], val);
if (m1 < val) swap(m1, val);
if (m2 < val) swap(m2, val);
}
if (m2 > -INF) ans = max(ans, m1 + m2);
a[v] = max(a[v], sum[v]);
return;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
for (int i = 1; i < n; i++) {
int v, u;
scanf("%d%d", &v, &u);
v--;
u--;
g[v].push_back(u);
g[u].push_back(v);
}
dfs(0);
if (ans == -INF)
printf("Impossible\n");
else
printf("%lld\n", ans);
return 0;
}
|
### Prompt
Create a solution in CPP for the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)1e18 + 1;
const int N = 300300;
int n;
long long ans = -INF;
long long a[N];
long long sum[N];
vector<int> g[N];
bool used[N];
void dfs(int v) {
used[v] = 1;
sum[v] = a[v];
a[v] = -INF;
long long m1 = -INF, m2 = -INF;
for (int u : g[v]) {
if (used[u]) continue;
dfs(u);
sum[v] += sum[u];
long long val = a[u];
a[v] = max(a[v], val);
if (m1 < val) swap(m1, val);
if (m2 < val) swap(m2, val);
}
if (m2 > -INF) ans = max(ans, m1 + m2);
a[v] = max(a[v], sum[v]);
return;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
for (int i = 1; i < n; i++) {
int v, u;
scanf("%d%d", &v, &u);
v--;
u--;
g[v].push_back(u);
g[u].push_back(v);
}
dfs(0);
if (ans == -INF)
printf("Impossible\n");
else
printf("%lld\n", ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
const int Max = 200050;
struct zk {
int v, next;
} ed[Max << 1];
int w[Max];
long long ans = -1e18;
bool flag;
vector<int> G[Max];
long long dp[Max][2];
void dfs(int u, int fa) {
dp[u][0] = w[u];
int sz = G[u].size();
long long max1 = -1e18, max2 = -1e18;
for (register int i = 0; i < sz; ++i) {
int v = G[u][i];
if (v != fa) {
dfs(v, u);
dp[u][0] += dp[v][0];
dp[u][1] = max(dp[u][1], dp[v][1]);
if (dp[v][1] > max1) max1 = dp[v][1];
if (max1 > max2) swap(max1, max2);
}
}
if (max1 != -1e18 && max2 != -1e18) ans = max(ans, max1 + max2);
dp[u][1] = max(dp[u][0], dp[u][1]);
}
int main() {
scanf("%d", &n);
for (register int i = 1; i <= n; ++i) {
scanf("%d", &w[i]);
dp[i][1] = -1e18;
}
int u, v;
for (register int i = 1; i < n; ++i) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, 0);
if (ans == -1e18) {
puts("Impossible");
return 0;
}
printf("%lld", ans);
}
|
### Prompt
Create a solution in Cpp for the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
const int Max = 200050;
struct zk {
int v, next;
} ed[Max << 1];
int w[Max];
long long ans = -1e18;
bool flag;
vector<int> G[Max];
long long dp[Max][2];
void dfs(int u, int fa) {
dp[u][0] = w[u];
int sz = G[u].size();
long long max1 = -1e18, max2 = -1e18;
for (register int i = 0; i < sz; ++i) {
int v = G[u][i];
if (v != fa) {
dfs(v, u);
dp[u][0] += dp[v][0];
dp[u][1] = max(dp[u][1], dp[v][1]);
if (dp[v][1] > max1) max1 = dp[v][1];
if (max1 > max2) swap(max1, max2);
}
}
if (max1 != -1e18 && max2 != -1e18) ans = max(ans, max1 + max2);
dp[u][1] = max(dp[u][0], dp[u][1]);
}
int main() {
scanf("%d", &n);
for (register int i = 1; i <= n; ++i) {
scanf("%d", &w[i]);
dp[i][1] = -1e18;
}
int u, v;
for (register int i = 1; i < n; ++i) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, 0);
if (ans == -1e18) {
puts("Impossible");
return 0;
}
printf("%lld", ans);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
vector<int> g[N];
long long int a[N], sub[N];
long long int global_ans = -1e18;
bool possible = false;
void subSum(int par, int node) {
sub[node] = a[node];
for (int adj : g[node]) {
if (adj == par) continue;
subSum(node, adj);
sub[node] += sub[adj];
}
}
long long int dp(int par, int node) {
long long int prop = sub[node];
vector<long long int> child;
for (int adj : g[node]) {
if (adj == par) continue;
child.push_back(dp(node, adj));
}
sort(child.rbegin(), child.rend());
if (child.size() == 0) {
return prop;
} else if (child.size() == 1) {
return max(prop, child[0]);
} else {
possible = true;
global_ans = max(global_ans, child[0] + child[1]);
return max(prop, child[0]);
}
}
void solve(int test_index) {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
subSum(-1, 0);
dp(-1, 0);
if (possible) {
cout << global_ans << "\n";
} else {
cout << "Impossible"
<< "\n";
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int num_tests = 1;
for (int test_index = 1; test_index <= num_tests; test_index++) {
solve(test_index);
}
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
vector<int> g[N];
long long int a[N], sub[N];
long long int global_ans = -1e18;
bool possible = false;
void subSum(int par, int node) {
sub[node] = a[node];
for (int adj : g[node]) {
if (adj == par) continue;
subSum(node, adj);
sub[node] += sub[adj];
}
}
long long int dp(int par, int node) {
long long int prop = sub[node];
vector<long long int> child;
for (int adj : g[node]) {
if (adj == par) continue;
child.push_back(dp(node, adj));
}
sort(child.rbegin(), child.rend());
if (child.size() == 0) {
return prop;
} else if (child.size() == 1) {
return max(prop, child[0]);
} else {
possible = true;
global_ans = max(global_ans, child[0] + child[1]);
return max(prop, child[0]);
}
}
void solve(int test_index) {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
subSum(-1, 0);
dp(-1, 0);
if (possible) {
cout << global_ans << "\n";
} else {
cout << "Impossible"
<< "\n";
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int num_tests = 1;
for (int test_index = 1; test_index <= num_tests; test_index++) {
solve(test_index);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
const int N = (int)1e6 + 7;
const int MOD = (int)1e9 + 7;
const int INF = (int)2e9;
const long long BIG = 1e18;
vector<vector<int> > g(N);
long long a[N], sum[N], max_sum[N], ans = -BIG;
void dfs(int v = 1, int p = -1) {
sum[v] = a[v];
max_sum[v] = -BIG;
for (auto to : g[v]) {
if (to == p) continue;
dfs(to, v);
sum[v] += sum[to];
max_sum[v] = max(max_sum[v], max_sum[to]);
}
max_sum[v] = max(max_sum[v], sum[v]);
}
void calc(int v = 1, int p = -1, long long out = -BIG) {
if (out != -BIG) {
ans = max(ans, sum[v] + out);
}
vector<pair<long long, int> > val;
for (auto to : g[v]) {
if (to == p) continue;
val.push_back(make_pair(max_sum[to], to));
}
val.push_back(make_pair(-BIG, -1));
sort(val.begin(), val.end());
reverse(val.begin(), val.end());
for (auto to : g[v]) {
if (to == p) continue;
long long choice = 0ll;
if (val[0].second == to)
choice = val[1].first;
else
choice = val[0].first;
calc(to, v, max(out, choice));
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
;
int n, m;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v), g[v].push_back(u);
}
dfs();
calc();
if (ans == -BIG)
cout << "Impossible";
else
cout << ans;
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
const int N = (int)1e6 + 7;
const int MOD = (int)1e9 + 7;
const int INF = (int)2e9;
const long long BIG = 1e18;
vector<vector<int> > g(N);
long long a[N], sum[N], max_sum[N], ans = -BIG;
void dfs(int v = 1, int p = -1) {
sum[v] = a[v];
max_sum[v] = -BIG;
for (auto to : g[v]) {
if (to == p) continue;
dfs(to, v);
sum[v] += sum[to];
max_sum[v] = max(max_sum[v], max_sum[to]);
}
max_sum[v] = max(max_sum[v], sum[v]);
}
void calc(int v = 1, int p = -1, long long out = -BIG) {
if (out != -BIG) {
ans = max(ans, sum[v] + out);
}
vector<pair<long long, int> > val;
for (auto to : g[v]) {
if (to == p) continue;
val.push_back(make_pair(max_sum[to], to));
}
val.push_back(make_pair(-BIG, -1));
sort(val.begin(), val.end());
reverse(val.begin(), val.end());
for (auto to : g[v]) {
if (to == p) continue;
long long choice = 0ll;
if (val[0].second == to)
choice = val[1].first;
else
choice = val[0].first;
calc(to, v, max(out, choice));
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
;
int n, m;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v), g[v].push_back(u);
}
dfs();
calc();
if (ans == -BIG)
cout << "Impossible";
else
cout << ans;
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O0")
#pragma GCC optimize("O1")
using namespace std;
const int N = 2 * 1e5 + 10;
int n;
int a[N];
long long res = LONG_LONG_MIN;
long long sum[N], maxi[N];
vector<int> adj[N];
bool mark[N];
void dfs(int x) {
mark[x] = true;
vector<long long> num;
for (auto v : adj[x]) {
if (!mark[v]) {
dfs(v);
sum[x] += sum[v];
num.push_back(maxi[v]);
}
}
sum[x] += a[x];
sort(num.begin(), num.end());
int tmp = num.size();
if (tmp >= 2) {
res = max(res, num[tmp - 1] + num[tmp - 2]);
}
if (tmp >= 1) {
maxi[x] = max(sum[x], num[tmp - 1]);
} else {
maxi[x] = sum[x];
}
}
void solve() {
if (adj[1].size() == 1 || n <= 2) {
bool flag = true;
for (int i = 2; i <= n; i++) {
if (adj[i].size() > 2) {
flag = false;
break;
}
}
if (flag) {
cout << "Impossible";
return;
}
}
dfs(1);
cout << res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int a, b;
for (int i = 0; i < n - 1; i++) {
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
solve();
}
|
### Prompt
Develop a solution in CPP to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O0")
#pragma GCC optimize("O1")
using namespace std;
const int N = 2 * 1e5 + 10;
int n;
int a[N];
long long res = LONG_LONG_MIN;
long long sum[N], maxi[N];
vector<int> adj[N];
bool mark[N];
void dfs(int x) {
mark[x] = true;
vector<long long> num;
for (auto v : adj[x]) {
if (!mark[v]) {
dfs(v);
sum[x] += sum[v];
num.push_back(maxi[v]);
}
}
sum[x] += a[x];
sort(num.begin(), num.end());
int tmp = num.size();
if (tmp >= 2) {
res = max(res, num[tmp - 1] + num[tmp - 2]);
}
if (tmp >= 1) {
maxi[x] = max(sum[x], num[tmp - 1]);
} else {
maxi[x] = sum[x];
}
}
void solve() {
if (adj[1].size() == 1 || n <= 2) {
bool flag = true;
for (int i = 2; i <= n; i++) {
if (adj[i].size() > 2) {
flag = false;
break;
}
}
if (flag) {
cout << "Impossible";
return;
}
}
dfs(1);
cout << res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int a, b;
for (int i = 0; i < n - 1; i++) {
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
solve();
}
```
|
#include <bits/stdc++.h>
long long mod = 10000000000000007ll;
using namespace std;
int n;
long long ans;
long long c[2 * 100001];
long long sub[2 * 100001];
vector<int> G[2 * 100001];
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
long long dfs(int v, int p) {
int i;
long long rec;
long long z = -1ll;
vector<long long> possible;
long long toret = (z) * (mod);
sub[v] = c[v];
for (i = 0; i < G[v].size(); i++) {
if (G[v][i] == p) continue;
rec = dfs(G[v][i], v);
toret = max(toret, rec);
possible.push_back(rec);
sub[v] += sub[G[v][i]];
}
toret = max(toret, sub[v]);
sort(possible.rbegin(), possible.rend());
if (possible.size() > 1) {
ans = max(ans, possible[0] + possible[1]);
}
return toret;
}
int main() {
int i, j, fl, test, x, y;
scanf("%d", &n);
ans = (-1) * mod;
for (i = 1; i <= n; i++) {
scanf("%lld", &c[i]);
}
for (i = 0; i < n - 1; i++) {
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
dfs(1, 1);
if (ans != (-1) * mod)
printf("%lld\n", ans);
else
printf("Impossible\n");
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
long long mod = 10000000000000007ll;
using namespace std;
int n;
long long ans;
long long c[2 * 100001];
long long sub[2 * 100001];
vector<int> G[2 * 100001];
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
long long dfs(int v, int p) {
int i;
long long rec;
long long z = -1ll;
vector<long long> possible;
long long toret = (z) * (mod);
sub[v] = c[v];
for (i = 0; i < G[v].size(); i++) {
if (G[v][i] == p) continue;
rec = dfs(G[v][i], v);
toret = max(toret, rec);
possible.push_back(rec);
sub[v] += sub[G[v][i]];
}
toret = max(toret, sub[v]);
sort(possible.rbegin(), possible.rend());
if (possible.size() > 1) {
ans = max(ans, possible[0] + possible[1]);
}
return toret;
}
int main() {
int i, j, fl, test, x, y;
scanf("%d", &n);
ans = (-1) * mod;
for (i = 1; i <= n; i++) {
scanf("%lld", &c[i]);
}
for (i = 0; i < n - 1; i++) {
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
dfs(1, 1);
if (ans != (-1) * mod)
printf("%lld\n", ans);
else
printf("Impossible\n");
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long maxx(long long a, long long b) { return (a < b) ? b : a; }
vector<vector<long long> > graph(200010);
vector<long long> pls(200010);
vector<long long> pls_tree(200010);
vector<bool> visited(200010, false);
vector<pair<bool, long long> > max1(200010, make_pair(false, -1e18));
vector<pair<bool, long long> > maxsum(200010, make_pair(false, -1e18));
void dfs(long long idx) {
long long tmp, test, max_1 = -1e18, max_2 = -1e18, max_sum = -1e18;
for (int i = 0; i < graph[idx].size(); i++) {
tmp = graph[idx][i];
if (!visited[tmp]) {
visited[tmp] = true;
dfs(tmp);
pls_tree[idx] += pls_tree[tmp];
if (max1[tmp].first == true)
test = maxx(max1[tmp].second, pls_tree[tmp]);
else
test = pls_tree[tmp];
if (test > max_1) {
max_2 = max_1;
max_1 = test;
} else if (test > max_2)
max_2 = test;
if (maxsum[tmp].first == true && max_sum < maxsum[tmp].second)
max_sum = maxsum[tmp].second;
}
}
if (max_1 != -1e18 && max_2 != -1e18) max_sum = maxx(max_1 + max_2, max_sum);
if (max_1 != -1e18) {
max1[idx].first = true;
max1[idx].second = max_1;
}
if (max_sum != -1e18) {
maxsum[idx].first = true;
maxsum[idx].second = max_sum;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
long long n, u, v;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> pls[i];
pls_tree[i] = pls[i];
}
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
visited[1] = true;
dfs(1);
if (maxsum[1].first == true)
cout << maxsum[1].second;
else
cout << "Impossible";
return 0;
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long maxx(long long a, long long b) { return (a < b) ? b : a; }
vector<vector<long long> > graph(200010);
vector<long long> pls(200010);
vector<long long> pls_tree(200010);
vector<bool> visited(200010, false);
vector<pair<bool, long long> > max1(200010, make_pair(false, -1e18));
vector<pair<bool, long long> > maxsum(200010, make_pair(false, -1e18));
void dfs(long long idx) {
long long tmp, test, max_1 = -1e18, max_2 = -1e18, max_sum = -1e18;
for (int i = 0; i < graph[idx].size(); i++) {
tmp = graph[idx][i];
if (!visited[tmp]) {
visited[tmp] = true;
dfs(tmp);
pls_tree[idx] += pls_tree[tmp];
if (max1[tmp].first == true)
test = maxx(max1[tmp].second, pls_tree[tmp]);
else
test = pls_tree[tmp];
if (test > max_1) {
max_2 = max_1;
max_1 = test;
} else if (test > max_2)
max_2 = test;
if (maxsum[tmp].first == true && max_sum < maxsum[tmp].second)
max_sum = maxsum[tmp].second;
}
}
if (max_1 != -1e18 && max_2 != -1e18) max_sum = maxx(max_1 + max_2, max_sum);
if (max_1 != -1e18) {
max1[idx].first = true;
max1[idx].second = max_1;
}
if (max_sum != -1e18) {
maxsum[idx].first = true;
maxsum[idx].second = max_sum;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
long long n, u, v;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> pls[i];
pls_tree[i] = pls[i];
}
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
visited[1] = true;
dfs(1);
if (maxsum[1].first == true)
cout << maxsum[1].second;
else
cout << "Impossible";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200000, MAX_VAL = 1000000000;
const long long INF = (long long)(MAX_N) * (long long)(MAX_VAL) + 1ll;
struct Vertex {
int a;
long long s, maxim, ans;
list<int> edge;
};
int n, a[MAX_N + 1];
Vertex vertexs[MAX_N + 1];
void DFS(int v, int p = 0) {
vertexs[v].s = vertexs[v].a;
vertexs[v].maxim = -INF;
vertexs[v].ans = -INF;
long long ans1 = -2 * INF, ans2 = -2 * INF;
for (auto i : vertexs[v].edge) {
if (i != p) {
DFS(i, v);
vertexs[v].s += vertexs[i].s;
vertexs[v].maxim = max(vertexs[v].maxim, vertexs[i].maxim);
vertexs[v].ans = max(vertexs[v].ans, vertexs[i].ans);
ans2 = max(ans2, vertexs[i].maxim);
if (ans2 > ans1) {
swap(ans1, ans2);
}
}
}
vertexs[v].maxim = max(vertexs[v].maxim, vertexs[v].s);
vertexs[v].ans = max(vertexs[v].ans, ans1 + ans2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
cout.tie();
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
vertexs[i].a = a[i];
}
int u, v;
for (int i = 1; i < n; i++) {
cin >> u >> v;
vertexs[u].edge.push_back(v);
vertexs[v].edge.push_back(u);
}
DFS(1);
if (vertexs[1].ans > -INF) {
cout << vertexs[1].ans << endl;
} else {
cout << "Impossible" << endl;
}
return 0;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200000, MAX_VAL = 1000000000;
const long long INF = (long long)(MAX_N) * (long long)(MAX_VAL) + 1ll;
struct Vertex {
int a;
long long s, maxim, ans;
list<int> edge;
};
int n, a[MAX_N + 1];
Vertex vertexs[MAX_N + 1];
void DFS(int v, int p = 0) {
vertexs[v].s = vertexs[v].a;
vertexs[v].maxim = -INF;
vertexs[v].ans = -INF;
long long ans1 = -2 * INF, ans2 = -2 * INF;
for (auto i : vertexs[v].edge) {
if (i != p) {
DFS(i, v);
vertexs[v].s += vertexs[i].s;
vertexs[v].maxim = max(vertexs[v].maxim, vertexs[i].maxim);
vertexs[v].ans = max(vertexs[v].ans, vertexs[i].ans);
ans2 = max(ans2, vertexs[i].maxim);
if (ans2 > ans1) {
swap(ans1, ans2);
}
}
}
vertexs[v].maxim = max(vertexs[v].maxim, vertexs[v].s);
vertexs[v].ans = max(vertexs[v].ans, ans1 + ans2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
cout.tie();
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
vertexs[i].a = a[i];
}
int u, v;
for (int i = 1; i < n; i++) {
cin >> u >> v;
vertexs[u].edge.push_back(v);
vertexs[v].edge.push_back(u);
}
DFS(1);
if (vertexs[1].ans > -INF) {
cout << vertexs[1].ans << endl;
} else {
cout << "Impossible" << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long INF(0x3f3f3f3f3f3f3f3fll);
const int inf(0x3f3f3f3f);
template <typename T>
void read(T &res) {
bool flag = false;
char ch;
while (!isdigit(ch = getchar())) (ch == '-') && (flag = true);
for (res = ch - 48; isdigit(ch = getchar());
res = (res << 1) + (res << 3) + ch - 48)
;
flag && (res = -res);
}
template <typename T>
void Out(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) Out(x / 10);
putchar(x % 10 + '0');
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
long long pow_mod(long long x, long long n, long long mod) {
long long res = 1;
while (n) {
if (n & 1) res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
long long fact_pow(long long n, long long p) {
long long res = 0;
while (n) {
n /= p;
res += n;
}
return res;
}
long long mult(long long a, long long b, long long p) {
a %= p;
b %= p;
long long r = 0, v = a;
while (b) {
if (b & 1) {
r += v;
if (r > p) r -= p;
}
v <<= 1;
if (v > p) v -= p;
b >>= 1;
}
return r;
}
long long quick_pow(long long a, long long b, long long p) {
long long r = 1, v = a % p;
while (b) {
if (b & 1) r = mult(r, v, p);
v = mult(v, v, p);
b >>= 1;
}
return r;
}
bool CH(long long a, long long n, long long x, long long t) {
long long r = quick_pow(a, x, n);
long long z = r;
for (long long i = 1; i <= t; i++) {
r = mult(r, r, n);
if (r == 1 && z != 1 && z != n - 1) return true;
z = r;
}
return r != 1;
}
bool Miller_Rabin(long long n) {
if (n < 2) return false;
if (n == 2) return true;
if (!(n & 1)) return false;
long long x = n - 1, t = 0;
while (!(x & 1)) {
x >>= 1;
t++;
}
srand(time(NULL));
long long o = 8;
for (long long i = 0; i < o; i++) {
long long a = rand() % (n - 1) + 1;
if (CH(a, n, x, t)) return false;
}
return true;
}
const long long N = 1e6 + 10;
struct node {
long long nex, to, w;
} edge[N];
long long head[N], tot, sum[N], ans, dp[N], n, w[N];
void add_edge(long long u, long long v, long long w) {
edge[++tot].nex = head[u];
edge[tot].to = v;
edge[tot].w = w;
head[u] = tot;
}
void dfs(long long u, long long fa) {
sum[u] = w[u];
for (long long i = head[u]; i; i = edge[i].nex) {
long long v = edge[i].to;
if (v == fa) continue;
dfs(v, u);
sum[u] += sum[v];
}
}
void dfs1(long long u, long long fa) {
for (long long i = head[u]; i; i = edge[i].nex) {
long long v = edge[i].to;
if (v == fa) continue;
dfs1(v, u);
if (dp[u] != -INF && dp[v] != -INF) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], sum[u]);
}
signed main() {
std::ios::sync_with_stdio(false);
cin >> n;
for (long long i = 1; i <= n; i++) cin >> w[i];
for (long long i = 2; i <= n; i++) {
long long u, v;
cin >> u >> v;
add_edge(u, v, 1);
add_edge(v, u, 1);
}
ans = -INF;
for (long long i = 1; i <= n; i++) dp[i] = -INF;
dfs(1, -1);
dfs1(1, -1);
if (ans <= -INF)
cout << "Impossible\n";
else
cout << ans << '\n';
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long INF(0x3f3f3f3f3f3f3f3fll);
const int inf(0x3f3f3f3f);
template <typename T>
void read(T &res) {
bool flag = false;
char ch;
while (!isdigit(ch = getchar())) (ch == '-') && (flag = true);
for (res = ch - 48; isdigit(ch = getchar());
res = (res << 1) + (res << 3) + ch - 48)
;
flag && (res = -res);
}
template <typename T>
void Out(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) Out(x / 10);
putchar(x % 10 + '0');
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
long long pow_mod(long long x, long long n, long long mod) {
long long res = 1;
while (n) {
if (n & 1) res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
long long fact_pow(long long n, long long p) {
long long res = 0;
while (n) {
n /= p;
res += n;
}
return res;
}
long long mult(long long a, long long b, long long p) {
a %= p;
b %= p;
long long r = 0, v = a;
while (b) {
if (b & 1) {
r += v;
if (r > p) r -= p;
}
v <<= 1;
if (v > p) v -= p;
b >>= 1;
}
return r;
}
long long quick_pow(long long a, long long b, long long p) {
long long r = 1, v = a % p;
while (b) {
if (b & 1) r = mult(r, v, p);
v = mult(v, v, p);
b >>= 1;
}
return r;
}
bool CH(long long a, long long n, long long x, long long t) {
long long r = quick_pow(a, x, n);
long long z = r;
for (long long i = 1; i <= t; i++) {
r = mult(r, r, n);
if (r == 1 && z != 1 && z != n - 1) return true;
z = r;
}
return r != 1;
}
bool Miller_Rabin(long long n) {
if (n < 2) return false;
if (n == 2) return true;
if (!(n & 1)) return false;
long long x = n - 1, t = 0;
while (!(x & 1)) {
x >>= 1;
t++;
}
srand(time(NULL));
long long o = 8;
for (long long i = 0; i < o; i++) {
long long a = rand() % (n - 1) + 1;
if (CH(a, n, x, t)) return false;
}
return true;
}
const long long N = 1e6 + 10;
struct node {
long long nex, to, w;
} edge[N];
long long head[N], tot, sum[N], ans, dp[N], n, w[N];
void add_edge(long long u, long long v, long long w) {
edge[++tot].nex = head[u];
edge[tot].to = v;
edge[tot].w = w;
head[u] = tot;
}
void dfs(long long u, long long fa) {
sum[u] = w[u];
for (long long i = head[u]; i; i = edge[i].nex) {
long long v = edge[i].to;
if (v == fa) continue;
dfs(v, u);
sum[u] += sum[v];
}
}
void dfs1(long long u, long long fa) {
for (long long i = head[u]; i; i = edge[i].nex) {
long long v = edge[i].to;
if (v == fa) continue;
dfs1(v, u);
if (dp[u] != -INF && dp[v] != -INF) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], sum[u]);
}
signed main() {
std::ios::sync_with_stdio(false);
cin >> n;
for (long long i = 1; i <= n; i++) cin >> w[i];
for (long long i = 2; i <= n; i++) {
long long u, v;
cin >> u >> v;
add_edge(u, v, 1);
add_edge(v, u, 1);
}
ans = -INF;
for (long long i = 1; i <= n; i++) dp[i] = -INF;
dfs(1, -1);
dfs1(1, -1);
if (ans <= -INF)
cout << "Impossible\n";
else
cout << ans << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long p = 1e5;
vector<long long> a, sum, mxn;
vector<long long> adjList[2 * p];
vector<bool> visited;
long long mx = INT_MIN, n;
void dfs1(long long s) {
visited[s] = true;
for (int i = 0; i < adjList[s].size(); i++) {
if (!visited[adjList[s][i]]) {
dfs1(adjList[s][i]);
sum[s] += sum[adjList[s][i]];
}
}
sum[s] += a[s];
}
void dfs2(long long s) {
visited[s] = true;
mxn[s] = sum[s];
for (int i = 0; i < adjList[s].size(); i++) {
if (!visited[adjList[s][i]]) {
dfs2(adjList[s][i]);
mxn[s] = max(mxn[s], mxn[adjList[s][i]]);
}
}
}
void bfs(long long s) {
visited.clear();
visited.assign(n, false);
multiset<long long, greater<long long>> st;
visited[s] = true;
deque<long long> dq;
dq.push_back(s);
while (!dq.empty()) {
long long u = dq.front();
dq.pop_front();
for (int i = 0; i < adjList[u].size(); i++) {
long long v = adjList[u][i];
if (!visited[v]) {
visited[v] = true;
dq.push_back(v);
st.insert(mxn[v]);
}
}
if (st.size() >= 2) {
auto it1 = st.begin();
auto temp = it1;
auto it2 = ++temp;
mx = max(mx, *it1 + *it2);
}
st.clear();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long u, v;
cin >> n;
a.assign(n, 0);
sum.assign(n, 0);
mxn.assign(n, 0);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
u--;
v--;
adjList[u].push_back(v);
adjList[v].push_back(u);
}
visited.assign(n, false);
dfs1(0);
visited.clear();
visited.assign(n, false);
dfs2(0);
bfs(0);
if (mx == INT_MIN) {
cout << "Impossible\n";
} else
cout << mx << "\n";
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long p = 1e5;
vector<long long> a, sum, mxn;
vector<long long> adjList[2 * p];
vector<bool> visited;
long long mx = INT_MIN, n;
void dfs1(long long s) {
visited[s] = true;
for (int i = 0; i < adjList[s].size(); i++) {
if (!visited[adjList[s][i]]) {
dfs1(adjList[s][i]);
sum[s] += sum[adjList[s][i]];
}
}
sum[s] += a[s];
}
void dfs2(long long s) {
visited[s] = true;
mxn[s] = sum[s];
for (int i = 0; i < adjList[s].size(); i++) {
if (!visited[adjList[s][i]]) {
dfs2(adjList[s][i]);
mxn[s] = max(mxn[s], mxn[adjList[s][i]]);
}
}
}
void bfs(long long s) {
visited.clear();
visited.assign(n, false);
multiset<long long, greater<long long>> st;
visited[s] = true;
deque<long long> dq;
dq.push_back(s);
while (!dq.empty()) {
long long u = dq.front();
dq.pop_front();
for (int i = 0; i < adjList[u].size(); i++) {
long long v = adjList[u][i];
if (!visited[v]) {
visited[v] = true;
dq.push_back(v);
st.insert(mxn[v]);
}
}
if (st.size() >= 2) {
auto it1 = st.begin();
auto temp = it1;
auto it2 = ++temp;
mx = max(mx, *it1 + *it2);
}
st.clear();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long u, v;
cin >> n;
a.assign(n, 0);
sum.assign(n, 0);
mxn.assign(n, 0);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
u--;
v--;
adjList[u].push_back(v);
adjList[v].push_back(u);
}
visited.assign(n, false);
dfs1(0);
visited.clear();
visited.assign(n, false);
dfs2(0);
bfs(0);
if (mx == INT_MIN) {
cout << "Impossible\n";
} else
cout << mx << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, x, y;
long long val[200005], mx[200005], cnt[200005], t[200005], ans = -1e18;
vector<int> v[200005];
bool cmp(long long x, long long y) { return x > y; }
void dfs(int node, int par) {
cnt[node] += val[node];
for (int i = 0; i < v[node].size(); i++) {
if (v[node][i] == par) continue;
dfs(v[node][i], node);
cnt[node] += cnt[v[node][i]];
mx[node] = max(mx[node], mx[v[node][i]]);
}
mx[node] = max(mx[node], cnt[node]);
}
void get_ans(int node, int par) {
for (int i = 0; i < v[node].size(); i++) {
if (v[node][i] == par) continue;
get_ans(v[node][i], node);
}
if ((node > 1 && v[node].size() <= 2) || (node == 1 && v[node].size() < 2))
return;
int cont = 0;
for (int i = 0; i < v[node].size(); i++) {
if (v[node][i] == par) continue;
t[++cont] = mx[v[node][i]];
}
sort(t + 1, t + cont + 1, cmp);
ans = max(ans, t[1] + t[2]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%I64d", &val[i]);
mx[i] = -1e18;
}
for (int i = 1; i <= n - 1; i++) {
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1, -1);
get_ans(1, -1);
if (ans == -1e18)
printf("Impossible\n");
else
printf("%I64d\n", ans);
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, x, y;
long long val[200005], mx[200005], cnt[200005], t[200005], ans = -1e18;
vector<int> v[200005];
bool cmp(long long x, long long y) { return x > y; }
void dfs(int node, int par) {
cnt[node] += val[node];
for (int i = 0; i < v[node].size(); i++) {
if (v[node][i] == par) continue;
dfs(v[node][i], node);
cnt[node] += cnt[v[node][i]];
mx[node] = max(mx[node], mx[v[node][i]]);
}
mx[node] = max(mx[node], cnt[node]);
}
void get_ans(int node, int par) {
for (int i = 0; i < v[node].size(); i++) {
if (v[node][i] == par) continue;
get_ans(v[node][i], node);
}
if ((node > 1 && v[node].size() <= 2) || (node == 1 && v[node].size() < 2))
return;
int cont = 0;
for (int i = 0; i < v[node].size(); i++) {
if (v[node][i] == par) continue;
t[++cont] = mx[v[node][i]];
}
sort(t + 1, t + cont + 1, cmp);
ans = max(ans, t[1] + t[2]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%I64d", &val[i]);
mx[i] = -1e18;
}
for (int i = 1; i <= n - 1; i++) {
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1, -1);
get_ans(1, -1);
if (ans == -1e18)
printf("Impossible\n");
else
printf("%I64d\n", ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long a[1000000 + 100], sol, n, k, x, y, d[1000000 + 100], pr[200010][20],
f[1000000 + 100], d1, poen[1000000 + 100], otac[1000000 + 100];
long long drugi, idx2, max1, k1, k2, sin1[1000000 + 100], sin2[1000000 + 100];
long long D = 20;
long long mapa[1000000 + 100];
bool los[1000000 + 100];
vector<long long> v[1000000 + 100];
pair<long long, pair<long long, long long> > par[1000000 + 100];
string s;
bool cmp(pair<long long, pair<long long, long long> > a,
pair<long long, pair<long long, long long> > b) {
if (a.first > b.first) return true;
if (a.first == b.first)
if (a.second.second < b.second.second) return true;
return false;
}
long long dfs(long long c, long long p) {
pr[c][0] = p;
for (long long i = 1; i < D; i++) pr[c][i] = pr[pr[c][i - 1]][i - 1];
d[c] = d[p] + 1;
long long z = f[c];
for (auto i : v[c])
if (i != p) {
otac[i] = c;
z += dfs(i, c);
poen[c] += poen[i];
}
return z;
}
long long lca(long long x, long long y) {
if (d[x] < d[y]) swap(x, y);
for (long long i = D - 1; i >= 0; i--)
if (d[x] - d[y] >> i & 1) x = pr[x][i];
if (x == y) return x;
for (long long i = D - 1; i >= 0; i--)
if (pr[x][i] != pr[y][i]) x = pr[x][i], y = pr[y][i];
return pr[x][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 %lld", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
mapa[x]++;
mapa[y]++;
}
for (long long i = 1; i <= n; i++) poen[i] = a[i];
dfs(1, 0);
los[1] = true;
long long px = 1;
mapa[1]++;
while (mapa[px] == 2) {
if (otac[v[px][0]] == px)
px = v[px][0];
else
px = v[px][1];
los[px] = true;
}
long long w = 0;
for (long long i = 1; i <= n; i++)
if (los[i] == false) {
w++;
par[w] = make_pair(poen[i], make_pair(i, d[i]));
}
sort(par + 1, par + w + 1, cmp);
long long prvi = par[1].first;
long long idx = par[1].second.first;
if (par[1].second.first == 1) prvi = par[2].first, idx = par[2].second.first;
bool o = false;
for (long long i = 1; i <= w; i++) {
if (o == false) {
if (par[i].second.first != 1 && par[i].second.first != idx) {
long long d = lca(idx, par[i].second.first);
if (d != par[i].second.first && d != idx) {
drugi = par[i].first;
idx2 = i;
o = true;
}
}
}
}
if (idx2 == 0) {
cout << "Impossible" << endl;
return 0;
}
k1 = 0;
k2 = 0;
for (long long i = 1; i <= n; i++) {
if (otac[i] == idx) {
k1++;
sin1[k1] = par[i].first;
}
if (otac[i] == idx2) {
k1++;
sin2[k2] = par[i].first;
}
}
max1 = -1000000000000;
sort(sin1 + 1, sin1 + k1 + 1);
sort(sin2 + 1, sin2 + k2 + 1);
if (k1 >= 2) max1 = sin1[k1] + sin1[k1 - 1];
if (k2 >= 2) max1 = max(max1, sin2[k2] + sin2[k2 - 1]);
if (k1 >= 1 && k2 >= 1) max1 = max(max1, sin2[k2] + sin1[k1]);
max1 = max(max1, prvi + drugi);
cout << max1 << endl;
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long a[1000000 + 100], sol, n, k, x, y, d[1000000 + 100], pr[200010][20],
f[1000000 + 100], d1, poen[1000000 + 100], otac[1000000 + 100];
long long drugi, idx2, max1, k1, k2, sin1[1000000 + 100], sin2[1000000 + 100];
long long D = 20;
long long mapa[1000000 + 100];
bool los[1000000 + 100];
vector<long long> v[1000000 + 100];
pair<long long, pair<long long, long long> > par[1000000 + 100];
string s;
bool cmp(pair<long long, pair<long long, long long> > a,
pair<long long, pair<long long, long long> > b) {
if (a.first > b.first) return true;
if (a.first == b.first)
if (a.second.second < b.second.second) return true;
return false;
}
long long dfs(long long c, long long p) {
pr[c][0] = p;
for (long long i = 1; i < D; i++) pr[c][i] = pr[pr[c][i - 1]][i - 1];
d[c] = d[p] + 1;
long long z = f[c];
for (auto i : v[c])
if (i != p) {
otac[i] = c;
z += dfs(i, c);
poen[c] += poen[i];
}
return z;
}
long long lca(long long x, long long y) {
if (d[x] < d[y]) swap(x, y);
for (long long i = D - 1; i >= 0; i--)
if (d[x] - d[y] >> i & 1) x = pr[x][i];
if (x == y) return x;
for (long long i = D - 1; i >= 0; i--)
if (pr[x][i] != pr[y][i]) x = pr[x][i], y = pr[y][i];
return pr[x][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 %lld", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
mapa[x]++;
mapa[y]++;
}
for (long long i = 1; i <= n; i++) poen[i] = a[i];
dfs(1, 0);
los[1] = true;
long long px = 1;
mapa[1]++;
while (mapa[px] == 2) {
if (otac[v[px][0]] == px)
px = v[px][0];
else
px = v[px][1];
los[px] = true;
}
long long w = 0;
for (long long i = 1; i <= n; i++)
if (los[i] == false) {
w++;
par[w] = make_pair(poen[i], make_pair(i, d[i]));
}
sort(par + 1, par + w + 1, cmp);
long long prvi = par[1].first;
long long idx = par[1].second.first;
if (par[1].second.first == 1) prvi = par[2].first, idx = par[2].second.first;
bool o = false;
for (long long i = 1; i <= w; i++) {
if (o == false) {
if (par[i].second.first != 1 && par[i].second.first != idx) {
long long d = lca(idx, par[i].second.first);
if (d != par[i].second.first && d != idx) {
drugi = par[i].first;
idx2 = i;
o = true;
}
}
}
}
if (idx2 == 0) {
cout << "Impossible" << endl;
return 0;
}
k1 = 0;
k2 = 0;
for (long long i = 1; i <= n; i++) {
if (otac[i] == idx) {
k1++;
sin1[k1] = par[i].first;
}
if (otac[i] == idx2) {
k1++;
sin2[k2] = par[i].first;
}
}
max1 = -1000000000000;
sort(sin1 + 1, sin1 + k1 + 1);
sort(sin2 + 1, sin2 + k2 + 1);
if (k1 >= 2) max1 = sin1[k1] + sin1[k1 - 1];
if (k2 >= 2) max1 = max(max1, sin2[k2] + sin2[k2 - 1]);
if (k1 >= 1 && k2 >= 1) max1 = max(max1, sin2[k2] + sin1[k1]);
max1 = max(max1, prvi + drugi);
cout << max1 << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
const long long inf = -100000000000000000LL;
const int maxn = 2 * 1e5 + 10;
using namespace std;
long long val[maxn], son[maxn];
int head[maxn], n, k;
long long ans, dp[maxn];
long long max(long long a, long long b) { return a > b ? a : b; }
struct edge {
int v, next;
} e[maxn << 1];
void adde(int u, int v) {
e[k].v = v;
e[k].next = head[u];
head[u] = k++;
}
void init() {
k = 0;
ans = inf;
memset(head, -1, sizeof(head));
memset(son, 0, sizeof(son));
for (int i = 1; i <= maxn; i++) dp[i] = inf;
}
void dfs1(int u, int fa) {
son[u] = val[u];
for (int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].v;
if (v == fa) continue;
dfs1(v, u);
son[u] += son[v];
}
}
void dfs2(int u, int fa) {
for (int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].v;
if (v == fa) continue;
dfs2(v, u);
if (dp[u] != inf) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], son[u]);
}
int main() {
init();
int u, v;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &val[i]);
for (int i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
adde(u, v);
adde(v, u);
}
dfs1(1, 0);
dfs2(1, 0);
if (ans != inf)
printf("%lld", ans);
else
printf("Impossible");
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
const long long inf = -100000000000000000LL;
const int maxn = 2 * 1e5 + 10;
using namespace std;
long long val[maxn], son[maxn];
int head[maxn], n, k;
long long ans, dp[maxn];
long long max(long long a, long long b) { return a > b ? a : b; }
struct edge {
int v, next;
} e[maxn << 1];
void adde(int u, int v) {
e[k].v = v;
e[k].next = head[u];
head[u] = k++;
}
void init() {
k = 0;
ans = inf;
memset(head, -1, sizeof(head));
memset(son, 0, sizeof(son));
for (int i = 1; i <= maxn; i++) dp[i] = inf;
}
void dfs1(int u, int fa) {
son[u] = val[u];
for (int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].v;
if (v == fa) continue;
dfs1(v, u);
son[u] += son[v];
}
}
void dfs2(int u, int fa) {
for (int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].v;
if (v == fa) continue;
dfs2(v, u);
if (dp[u] != inf) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], son[u]);
}
int main() {
init();
int u, v;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &val[i]);
for (int i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
adde(u, v);
adde(v, u);
}
dfs1(1, 0);
dfs2(1, 0);
if (ans != inf)
printf("%lld", ans);
else
printf("Impossible");
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
const long long INF = 1e18;
long long a[N];
long long n;
vector<long long> g[N];
long long subtree[N], dp[N];
long long ans;
void dfs(long long node, long long par = -1) {
subtree[node] = a[node];
long long mx1 = -INF, mx2 = -INF;
for (long long child : g[node]) {
if (child == par) continue;
dfs(child, node);
subtree[node] += subtree[child];
if (dp[child] >= mx1)
mx2 = mx1, mx1 = dp[child];
else if (dp[child] >= mx2)
mx2 = dp[child];
}
if (mx2 != -INF) ans = max(ans, mx1 + mx2);
dp[node] = max(subtree[node], mx1);
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n;
for (long long i = 1; i <= n; i++) cin >> a[i];
for (long long i = 1; i <= n - 1; i++) {
long long u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
ans = -INF;
dfs(1);
if (ans == -INF)
cout << "Impossible";
else
cout << ans;
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
const long long INF = 1e18;
long long a[N];
long long n;
vector<long long> g[N];
long long subtree[N], dp[N];
long long ans;
void dfs(long long node, long long par = -1) {
subtree[node] = a[node];
long long mx1 = -INF, mx2 = -INF;
for (long long child : g[node]) {
if (child == par) continue;
dfs(child, node);
subtree[node] += subtree[child];
if (dp[child] >= mx1)
mx2 = mx1, mx1 = dp[child];
else if (dp[child] >= mx2)
mx2 = dp[child];
}
if (mx2 != -INF) ans = max(ans, mx1 + mx2);
dp[node] = max(subtree[node], mx1);
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n;
for (long long i = 1; i <= n; i++) cin >> a[i];
for (long long i = 1; i <= n - 1; i++) {
long long u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
ans = -INF;
dfs(1);
if (ans == -INF)
cout << "Impossible";
else
cout << ans;
return 0;
}
```
|
#include <bits/stdc++.h>
int getint(int x = 0) {
scanf("%d", &x);
return x;
}
double getdb(double x = 0) {
scanf("%lf", &x);
return x;
}
long long getll(long long x = 0) {
scanf("%I64d", &x);
return x;
}
using namespace std;
long long INF = 1;
struct edge {
int in;
edge* nxt;
};
edge pool[405000];
edge* eds[205000];
edge* et = pool;
void addedge(int a, int b) {
et->in = b;
et->nxt = eds[a];
eds[a] = et++;
}
int n;
long long value[205000];
int f[205000];
long long mxv[205000];
long long mkv[205000];
long long sum[205000];
void DFSDP(int x) {
sum[x] = 0;
for (edge* e = eds[x]; e; e = e->nxt)
if (f[x] != e->in) {
f[e->in] = x;
DFSDP(e->in);
sum[x] += sum[e->in];
}
sum[x] += value[x];
mxv[x] = mkv[x] = -INF;
for (edge* e = eds[x]; e; e = e->nxt)
if (f[x] != e->in) mxv[x] = max(mxv[x], max(mxv[e->in], sum[e->in]));
mxv[x] = max(mxv[x], sum[x]);
int mxp = -1, mkp = -1;
for (edge* e = eds[x]; e; e = e->nxt)
if (f[x] != e->in) {
if (mxp == -1 || mxv[e->in] > mxv[mxp]) mxp = e->in;
}
for (edge* e = eds[x]; e; e = e->nxt)
if (f[x] != e->in)
if (e->in != mxp) {
if (mkp == -1 || mxv[e->in] > mxv[mkp]) mkp = e->in;
}
if (mxp != -1 && mkp != -1) mkv[x] = mxv[mxp] + mxv[mkp];
for (edge* e = eds[x]; e; e = e->nxt)
if (f[x] != e->in) mkv[x] = max(mkv[x], mkv[e->in]);
}
int main() {
INF <<= 56;
n = getint();
for (int i = 0; i < n; i++) value[i] = getll();
for (int i = 0; i < n - 1; i++) {
int a = getint() - 1;
int b = getint() - 1;
addedge(a, b);
addedge(b, a);
}
for (int i = 0; i < n; i++) f[i] = -1;
DFSDP(0);
if (mkv[0] <= -INF)
printf("Impossible\n");
else
printf("%I64d\n", mkv[0]);
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
int getint(int x = 0) {
scanf("%d", &x);
return x;
}
double getdb(double x = 0) {
scanf("%lf", &x);
return x;
}
long long getll(long long x = 0) {
scanf("%I64d", &x);
return x;
}
using namespace std;
long long INF = 1;
struct edge {
int in;
edge* nxt;
};
edge pool[405000];
edge* eds[205000];
edge* et = pool;
void addedge(int a, int b) {
et->in = b;
et->nxt = eds[a];
eds[a] = et++;
}
int n;
long long value[205000];
int f[205000];
long long mxv[205000];
long long mkv[205000];
long long sum[205000];
void DFSDP(int x) {
sum[x] = 0;
for (edge* e = eds[x]; e; e = e->nxt)
if (f[x] != e->in) {
f[e->in] = x;
DFSDP(e->in);
sum[x] += sum[e->in];
}
sum[x] += value[x];
mxv[x] = mkv[x] = -INF;
for (edge* e = eds[x]; e; e = e->nxt)
if (f[x] != e->in) mxv[x] = max(mxv[x], max(mxv[e->in], sum[e->in]));
mxv[x] = max(mxv[x], sum[x]);
int mxp = -1, mkp = -1;
for (edge* e = eds[x]; e; e = e->nxt)
if (f[x] != e->in) {
if (mxp == -1 || mxv[e->in] > mxv[mxp]) mxp = e->in;
}
for (edge* e = eds[x]; e; e = e->nxt)
if (f[x] != e->in)
if (e->in != mxp) {
if (mkp == -1 || mxv[e->in] > mxv[mkp]) mkp = e->in;
}
if (mxp != -1 && mkp != -1) mkv[x] = mxv[mxp] + mxv[mkp];
for (edge* e = eds[x]; e; e = e->nxt)
if (f[x] != e->in) mkv[x] = max(mkv[x], mkv[e->in]);
}
int main() {
INF <<= 56;
n = getint();
for (int i = 0; i < n; i++) value[i] = getll();
for (int i = 0; i < n - 1; i++) {
int a = getint() - 1;
int b = getint() - 1;
addedge(a, b);
addedge(b, a);
}
for (int i = 0; i < n; i++) f[i] = -1;
DFSDP(0);
if (mkv[0] <= -INF)
printf("Impossible\n");
else
printf("%I64d\n", mkv[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:247474112")
#pragma GCC optimize("Ofast")
using namespace std;
const long long maxn = 2e5 + 10;
long long mx[maxn], val[maxn], sum[maxn];
vector<long long> g[maxn];
long long pass[maxn];
bool ok = 0;
long long ans = LLONG_MIN;
void dfs(long long aa = 1, long long aaa = 0) {
vector<long long> v;
sum[aa] = val[aa];
for (long long a : g[aa]) {
if (a == aaa) continue;
dfs(a, aa);
sum[aa] += sum[a];
v.push_back(pass[a]);
}
if (!v.empty()) {
sort(v.begin(), v.end());
pass[aa] = max(v.back(), sum[aa]);
if (v.size() >= 2) {
ans = max(ans, v[v.size() - 1] + v[v.size() - 2]);
}
} else {
pass[aa] = sum[aa];
}
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long n;
cin >> n;
for (long long i = 1; i <= n; ++i) cin >> val[i];
for (long long i = 1; i <= n; ++i) mx[i] = LLONG_MIN;
for (long long i = 1; i < n; ++i) {
long long u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (long long i = 1; i <= n; ++i) pass[i] = LLONG_MIN;
dfs(1);
if (ans == LLONG_MIN)
cout << "Impossible";
else
cout << ans;
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:247474112")
#pragma GCC optimize("Ofast")
using namespace std;
const long long maxn = 2e5 + 10;
long long mx[maxn], val[maxn], sum[maxn];
vector<long long> g[maxn];
long long pass[maxn];
bool ok = 0;
long long ans = LLONG_MIN;
void dfs(long long aa = 1, long long aaa = 0) {
vector<long long> v;
sum[aa] = val[aa];
for (long long a : g[aa]) {
if (a == aaa) continue;
dfs(a, aa);
sum[aa] += sum[a];
v.push_back(pass[a]);
}
if (!v.empty()) {
sort(v.begin(), v.end());
pass[aa] = max(v.back(), sum[aa]);
if (v.size() >= 2) {
ans = max(ans, v[v.size() - 1] + v[v.size() - 2]);
}
} else {
pass[aa] = sum[aa];
}
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long n;
cin >> n;
for (long long i = 1; i <= n; ++i) cin >> val[i];
for (long long i = 1; i <= n; ++i) mx[i] = LLONG_MIN;
for (long long i = 1; i < n; ++i) {
long long u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (long long i = 1; i <= n; ++i) pass[i] = LLONG_MIN;
dfs(1);
if (ans == LLONG_MIN)
cout << "Impossible";
else
cout << ans;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int a[200100];
long long val[200100];
long long mx[200100];
vector<int> adj[200100];
bool ok = 0;
long long ans = -1000000000000000ll;
void dfs(int u, int fa = 0) {
val[u] += a[u];
long long Mx1 = -1000000000000000ll, Mx2 = -1000000000000000ll, Mx;
for (auto v : adj[u]) {
if (v == fa) continue;
dfs(v, u);
val[u] += val[v];
Mx2 = max(Mx2, mx[v]);
if (Mx2 > Mx1) swap(Mx1, Mx2);
}
Mx = max(Mx1, Mx2);
mx[u] = max(val[u], Mx);
if (u != 1) {
if (adj[u].size() >= 3) {
ok = 1;
ans = max(ans, Mx1 + Mx2);
}
} else if (adj[u].size() >= 2) {
ok = 1;
ans = max(ans, Mx1 + Mx2);
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) mx[i] = -1000000000000000ll;
dfs(1);
if (ok == 0)
puts("Impossible");
else
printf("%I64d\n", ans);
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a[200100];
long long val[200100];
long long mx[200100];
vector<int> adj[200100];
bool ok = 0;
long long ans = -1000000000000000ll;
void dfs(int u, int fa = 0) {
val[u] += a[u];
long long Mx1 = -1000000000000000ll, Mx2 = -1000000000000000ll, Mx;
for (auto v : adj[u]) {
if (v == fa) continue;
dfs(v, u);
val[u] += val[v];
Mx2 = max(Mx2, mx[v]);
if (Mx2 > Mx1) swap(Mx1, Mx2);
}
Mx = max(Mx1, Mx2);
mx[u] = max(val[u], Mx);
if (u != 1) {
if (adj[u].size() >= 3) {
ok = 1;
ans = max(ans, Mx1 + Mx2);
}
} else if (adj[u].size() >= 2) {
ok = 1;
ans = max(ans, Mx1 + Mx2);
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) mx[i] = -1000000000000000ll;
dfs(1);
if (ok == 0)
puts("Impossible");
else
printf("%I64d\n", ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long sum[200005];
long long bch[200005];
int a[200005];
vector<int> ady[200005];
long long tans = -0x3f3f3f3f3f3f3f3fLL;
void dfs(int v, int p) {
sum[v] = a[v];
bch[v] = -0x3f3f3f3f3f3f3f3fLL;
for (int dv : ady[v]) {
if (dv != p) {
dfs(dv, v);
sum[v] += sum[dv];
}
}
multiset<long long, greater<long long> > sch;
for (int dv : ady[v]) {
if (dv != p) {
sch.insert(max(sum[dv], bch[dv]));
}
}
long long ans = -0x3f3f3f3f3f3f3f3fLL;
if (sch.empty() == false) {
auto it = sch.begin();
ans = max(ans, *it);
if (sch.size() > 1) {
long long sdv = *it;
++it;
sdv += *it;
tans = max(tans, sdv);
}
}
bch[v] = ans;
}
int main(int argc, char const* argv[]) {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i += 1) {
scanf("%d", &a[i]);
}
for (int i = 1; i < n; i += 1) {
int u, v;
scanf("%d %d", &u, &v);
--u;
--v;
ady[u].push_back(v);
ady[v].push_back(u);
}
dfs(0, 0);
if (tans == -0x3f3f3f3f3f3f3f3fLL) {
printf("Impossible\n");
return 0;
}
printf("%lld\n", tans);
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long sum[200005];
long long bch[200005];
int a[200005];
vector<int> ady[200005];
long long tans = -0x3f3f3f3f3f3f3f3fLL;
void dfs(int v, int p) {
sum[v] = a[v];
bch[v] = -0x3f3f3f3f3f3f3f3fLL;
for (int dv : ady[v]) {
if (dv != p) {
dfs(dv, v);
sum[v] += sum[dv];
}
}
multiset<long long, greater<long long> > sch;
for (int dv : ady[v]) {
if (dv != p) {
sch.insert(max(sum[dv], bch[dv]));
}
}
long long ans = -0x3f3f3f3f3f3f3f3fLL;
if (sch.empty() == false) {
auto it = sch.begin();
ans = max(ans, *it);
if (sch.size() > 1) {
long long sdv = *it;
++it;
sdv += *it;
tans = max(tans, sdv);
}
}
bch[v] = ans;
}
int main(int argc, char const* argv[]) {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i += 1) {
scanf("%d", &a[i]);
}
for (int i = 1; i < n; i += 1) {
int u, v;
scanf("%d %d", &u, &v);
--u;
--v;
ady[u].push_back(v);
ady[v].push_back(u);
}
dfs(0, 0);
if (tans == -0x3f3f3f3f3f3f3f3fLL) {
printf("Impossible\n");
return 0;
}
printf("%lld\n", tans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200100;
long long a[N], w[N];
int u[N], tin[N], tout[N], tt = 0;
vector<int> g[N];
struct nd {
int l, r;
long long w;
nd() {}
};
void dfs(int v) {
if (u[v]) {
return;
}
w[v] = a[v];
u[v] = 1;
tin[v] = ++tt;
for (auto i : g[v]) {
if (!u[i]) {
dfs(i);
w[v] += w[i];
}
}
tout[v] = ++tt;
}
int main() {
std::ios_base::sync_with_stdio(false);
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) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1);
vector<nd> L(n), R(n);
vector<long long> l(n), r(n);
for (int i = 1; i <= n; ++i) {
L[i - 1].l = tin[i];
L[i - 1].r = tout[i];
L[i - 1].w = w[i];
R[i - 1] = L[i - 1];
}
sort(L.begin(), L.end(), [](const nd &a, const nd &b) { return a.l < b.l; });
sort(R.begin(), R.end(), [](const nd &a, const nd &b) { return a.r < b.r; });
long long ptr = -1e18;
for (int i = L.size() - 1; i >= 0; --i) {
ptr = max(ptr, L[i].w);
l[i] = ptr;
}
ptr = -1e18;
for (int i = 0; i < R.size(); ++i) {
ptr = max(ptr, R[i].w);
r[i] = ptr;
}
long long ans = -1e18;
bool ok = false;
for (int i = 1; i <= n; ++i) {
int st = tin[i];
int fn = tout[i];
int lo = 0, hi = L.size() - 1;
while (lo < hi) {
int m = (lo + hi) >> 1;
if (L[m].l > fn)
hi = m;
else
lo = m + 1;
}
if (L[lo].l > fn) {
ok = 1;
ans = max(ans, w[i] + l[lo]);
}
lo = 0, hi = R.size() - 1;
while (lo < hi) {
int m = (lo + hi + 1) >> 1;
if (R[m].r >= st)
hi = m - 1;
else
lo = m;
}
if (R[lo].r < st) {
ok = 1;
ans = max(ans, w[i] + r[lo]);
}
}
if (!ok) {
cout << "Impossible" << '\n';
} else
cout << ans << '\n';
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 200100;
long long a[N], w[N];
int u[N], tin[N], tout[N], tt = 0;
vector<int> g[N];
struct nd {
int l, r;
long long w;
nd() {}
};
void dfs(int v) {
if (u[v]) {
return;
}
w[v] = a[v];
u[v] = 1;
tin[v] = ++tt;
for (auto i : g[v]) {
if (!u[i]) {
dfs(i);
w[v] += w[i];
}
}
tout[v] = ++tt;
}
int main() {
std::ios_base::sync_with_stdio(false);
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) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1);
vector<nd> L(n), R(n);
vector<long long> l(n), r(n);
for (int i = 1; i <= n; ++i) {
L[i - 1].l = tin[i];
L[i - 1].r = tout[i];
L[i - 1].w = w[i];
R[i - 1] = L[i - 1];
}
sort(L.begin(), L.end(), [](const nd &a, const nd &b) { return a.l < b.l; });
sort(R.begin(), R.end(), [](const nd &a, const nd &b) { return a.r < b.r; });
long long ptr = -1e18;
for (int i = L.size() - 1; i >= 0; --i) {
ptr = max(ptr, L[i].w);
l[i] = ptr;
}
ptr = -1e18;
for (int i = 0; i < R.size(); ++i) {
ptr = max(ptr, R[i].w);
r[i] = ptr;
}
long long ans = -1e18;
bool ok = false;
for (int i = 1; i <= n; ++i) {
int st = tin[i];
int fn = tout[i];
int lo = 0, hi = L.size() - 1;
while (lo < hi) {
int m = (lo + hi) >> 1;
if (L[m].l > fn)
hi = m;
else
lo = m + 1;
}
if (L[lo].l > fn) {
ok = 1;
ans = max(ans, w[i] + l[lo]);
}
lo = 0, hi = R.size() - 1;
while (lo < hi) {
int m = (lo + hi + 1) >> 1;
if (R[m].r >= st)
hi = m - 1;
else
lo = m;
}
if (R[lo].r < st) {
ok = 1;
ans = max(ans, w[i] + r[lo]);
}
}
if (!ok) {
cout << "Impossible" << '\n';
} else
cout << ans << '\n';
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class S, class T>
ostream& operator<<(ostream& os, const pair<S, T>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
void debug(T a, T b) {
cerr << "[";
for (T i = a; i != b; ++i) {
if (i != a) cerr << ", ";
cerr << *i;
}
cerr << "]\n";
}
const int N = 2e5 + 10;
const long long INF = 1e18;
long long sum[N], bestSum[N];
pair<long long, int> best[N], secondBest[N];
vector<int> g[N];
long long a[N];
int dfs(int u, int par = -1) {
int h = 1;
sum[u] = a[u];
bestSum[u] = -INF;
best[u] = {-INF, -1};
secondBest[u] = {-INF, -1};
for (int v : g[u]) {
if (v != par) {
h = max(h, dfs(v, u) + 1);
sum[u] += sum[v];
bestSum[u] = max(bestSum[u], bestSum[v]);
pair<long long, int> cur = {bestSum[v], v};
if (best[u] < cur) {
secondBest[u] = best[u];
best[u] = cur;
} else if (secondBest[u] < cur) {
secondBest[u] = cur;
}
}
}
bestSum[u] = max(bestSum[u], sum[u]);
return h;
}
long long ans[N];
long long other[N];
void dfs2(int u, int par = -1) {
ans[u] = -INF;
other[u] = -INF;
if (par != -1) {
other[u] = other[par];
if (best[par].second != -1 and best[par].second != u) {
other[u] = max(other[u], best[par].first);
} else if (secondBest[par].second != -1 and secondBest[par].second != u) {
other[u] = max(other[u], secondBest[par].first);
}
ans[u] = bestSum[u] + other[u];
}
for (int v : g[u]) {
if (v != par) {
dfs2(v, u);
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < (n); ++i) {
cin >> a[i];
}
for (int i = 0; i < (n - 1); ++i) {
int u, v;
cin >> u >> v;
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
if (dfs(0) == n) {
cout << "Impossible\n";
return 0;
}
dfs2(0);
long long ret = -INF;
for (int i = 0; i < (n); ++i) {
ret = max(ret, ans[i]);
}
assert(ret > -INF);
cout << ret << "\n";
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class S, class T>
ostream& operator<<(ostream& os, const pair<S, T>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
void debug(T a, T b) {
cerr << "[";
for (T i = a; i != b; ++i) {
if (i != a) cerr << ", ";
cerr << *i;
}
cerr << "]\n";
}
const int N = 2e5 + 10;
const long long INF = 1e18;
long long sum[N], bestSum[N];
pair<long long, int> best[N], secondBest[N];
vector<int> g[N];
long long a[N];
int dfs(int u, int par = -1) {
int h = 1;
sum[u] = a[u];
bestSum[u] = -INF;
best[u] = {-INF, -1};
secondBest[u] = {-INF, -1};
for (int v : g[u]) {
if (v != par) {
h = max(h, dfs(v, u) + 1);
sum[u] += sum[v];
bestSum[u] = max(bestSum[u], bestSum[v]);
pair<long long, int> cur = {bestSum[v], v};
if (best[u] < cur) {
secondBest[u] = best[u];
best[u] = cur;
} else if (secondBest[u] < cur) {
secondBest[u] = cur;
}
}
}
bestSum[u] = max(bestSum[u], sum[u]);
return h;
}
long long ans[N];
long long other[N];
void dfs2(int u, int par = -1) {
ans[u] = -INF;
other[u] = -INF;
if (par != -1) {
other[u] = other[par];
if (best[par].second != -1 and best[par].second != u) {
other[u] = max(other[u], best[par].first);
} else if (secondBest[par].second != -1 and secondBest[par].second != u) {
other[u] = max(other[u], secondBest[par].first);
}
ans[u] = bestSum[u] + other[u];
}
for (int v : g[u]) {
if (v != par) {
dfs2(v, u);
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < (n); ++i) {
cin >> a[i];
}
for (int i = 0; i < (n - 1); ++i) {
int u, v;
cin >> u >> v;
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
if (dfs(0) == n) {
cout << "Impossible\n";
return 0;
}
dfs2(0);
long long ret = -INF;
for (int i = 0; i < (n); ++i) {
ret = max(ret, ans[i]);
}
assert(ret > -INF);
cout << ret << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long n, val[200200];
long long siz[200020], maxi[200020];
vector<long long> graph[200300];
long long ans[200202];
void dfs(int start, int par) {
multiset<long long> s;
int sizz = graph[start].size();
int i;
siz[start] = val[start];
long long damru = -99999999999999999LL;
for (i = 0; i < sizz; i++) {
int ele = graph[start][i];
if (ele != par) {
dfs(ele, start);
siz[start] += siz[ele];
damru = max(damru, maxi[ele]);
s.insert(maxi[ele]);
if (s.size() > 2) s.erase(s.begin());
}
}
maxi[start] = max(maxi[start], max(damru, siz[start]));
if (s.size() >= 2) {
auto it = s.end();
it--;
long long temp1 = *it;
it--;
temp1 += *it;
ans[start] = max(ans[start], temp1);
}
int siza = graph[start].size();
for (int ii = 0; ii < siza; ii++) {
int ele = graph[start][ii];
if (ele != par) {
ans[start] = max(ans[start], ans[ele]);
maxi[start] = max(maxi[start], maxi[ele]);
}
}
}
int main() {
int i, j, t;
scanf("%lld", &n);
;
for (i = 1; i <= n; i++) {
ans[i] = maxi[i] = -99999999999999999LL;
scanf("%lld", &val[i]);
;
}
for (i = 1; i < n; i++) {
int st, en;
scanf("%d%d", &st, &en);
;
graph[st].push_back(en);
graph[en].push_back(st);
}
dfs(1, 0);
long long answ = -99999999999999999LL;
for (i = 1; i <= n; i++) {
answ = max(answ, ans[i]);
}
if (answ <= -99999999999999999LL)
printf("Impossible\n");
else
cout << answ << endl;
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, val[200200];
long long siz[200020], maxi[200020];
vector<long long> graph[200300];
long long ans[200202];
void dfs(int start, int par) {
multiset<long long> s;
int sizz = graph[start].size();
int i;
siz[start] = val[start];
long long damru = -99999999999999999LL;
for (i = 0; i < sizz; i++) {
int ele = graph[start][i];
if (ele != par) {
dfs(ele, start);
siz[start] += siz[ele];
damru = max(damru, maxi[ele]);
s.insert(maxi[ele]);
if (s.size() > 2) s.erase(s.begin());
}
}
maxi[start] = max(maxi[start], max(damru, siz[start]));
if (s.size() >= 2) {
auto it = s.end();
it--;
long long temp1 = *it;
it--;
temp1 += *it;
ans[start] = max(ans[start], temp1);
}
int siza = graph[start].size();
for (int ii = 0; ii < siza; ii++) {
int ele = graph[start][ii];
if (ele != par) {
ans[start] = max(ans[start], ans[ele]);
maxi[start] = max(maxi[start], maxi[ele]);
}
}
}
int main() {
int i, j, t;
scanf("%lld", &n);
;
for (i = 1; i <= n; i++) {
ans[i] = maxi[i] = -99999999999999999LL;
scanf("%lld", &val[i]);
;
}
for (i = 1; i < n; i++) {
int st, en;
scanf("%d%d", &st, &en);
;
graph[st].push_back(en);
graph[en].push_back(st);
}
dfs(1, 0);
long long answ = -99999999999999999LL;
for (i = 1; i <= n; i++) {
answ = max(answ, ans[i]);
}
if (answ <= -99999999999999999LL)
printf("Impossible\n");
else
cout << answ << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
const long long LL_INF = (long long)2e18 + 5;
using namespace std;
void __print(int32_t x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
long long f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
const long long N = 2e5 + 10, mod = 998244353;
vector<long long> adj[N], v, st, fi;
struct seg_tree {
long long size;
vector<long long> sums;
void make(long long n) {
size = 1;
while (size < n) size *= 2;
sums.assign(2 * size, 0);
}
void build(vector<long long> &v, long long now, long long l, long long r) {
if (r - l == 1) {
if (l < v.size()) {
sums[now] = v[l];
}
return;
}
long long mid = (l + r) / 2;
build(v, 2 * now + 1, l, mid);
build(v, 2 * now + 2, mid, r);
sums[now] = sums[2 * now + 1] + sums[2 * now + 2];
}
void build(vector<long long> &v) { build(v, 0, 0, size); }
void set(long long i, long long el, long long now, long long l, long long r) {
if (r - l == 1) {
sums[now] = el;
return;
}
long long mid = (l + r) / 2;
if (i < mid) {
set(i, el, 2 * now + 1, l, mid);
} else
set(i, el, 2 * now + 2, mid, r);
sums[now] = sums[2 * now + 1] + sums[2 * now + 2];
}
void set(long long i, long long el) { set(i, el, 0, 0, size); }
long long sum(long long now, long long ln, long long rn, long long l,
long long r) {
if (ln >= r or rn <= l) return 0;
if (ln >= l and rn <= r) return sums[now];
long long mid = (ln + rn) / 2;
return sum(2 * now + 1, ln, mid, l, r) + sum(2 * now + 2, mid, rn, l, r);
}
long long sum(long long l, long long r) { return sum(0, 0, size, l, r); }
};
long long n, timer = 0;
void dfs(long long u, long long par) {
st[u] = timer++;
for (auto v : adj[u]) {
if (v == par) continue;
dfs(v, u);
}
fi[u] = timer;
}
pair<long long, long long> find_start(long long v, long long p = -1) {
if (adj[v].size() - (p != -1) > 1) return {v, p};
for (auto to : adj[v])
if (to != p) return find_start(to, v);
return {-1, -1};
}
void solve() {
cin >> n;
v = vector<long long>(n);
st = vector<long long>(n);
fi = vector<long long>(n);
for (long long i = (long long)(0); i < (long long)(n); i++) cin >> v[i];
for (long long i = (long long)(1); i < (long long)(n); i++) {
long long u, v;
cin >> u >> v;
--u, --v;
adj[u].push_back(v), adj[v].push_back(u);
}
pair<long long, long long> imp = find_start(0);
if (imp.first == -1) {
cout << "Impossible\n";
return;
}
dfs(0, -1);
vector<long long> order(n), ss;
iota(order.begin(), order.end(), 0);
sort(order.begin(), order.end(),
[&](long long i, long long j) { return st[i] < st[j]; });
for (auto i : order) ss.push_back(v[i]);
seg_tree tree;
tree.make(n);
tree.build(ss);
vector<long long> mx_st(timer + 1, -LL_INF), mx_end(timer + 1, -LL_INF);
for (long long i = (long long)(0); i < (long long)(n); i++) {
mx_st[st[i]] = max(mx_st[st[i]], tree.sum(st[i], fi[i]));
mx_end[fi[i]] = max(mx_end[fi[i]], tree.sum(st[i], fi[i]));
}
for (long long i = (long long)(timer - 1 - 1); i >= (long long)(0); i--)
mx_st[i] = max(mx_st[i + 1], mx_st[i]);
for (long long i = (long long)(1); i < (long long)(timer); i++)
mx_end[i] = max(mx_end[i], mx_end[i - 1]);
long long ans = -LL_INF;
;
for (long long i = (long long)(0); i < (long long)(timer); i++) {
ans = max(ans, mx_end[i] + mx_st[i]);
}
cout << ans << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
const long long LL_INF = (long long)2e18 + 5;
using namespace std;
void __print(int32_t x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
long long f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
const long long N = 2e5 + 10, mod = 998244353;
vector<long long> adj[N], v, st, fi;
struct seg_tree {
long long size;
vector<long long> sums;
void make(long long n) {
size = 1;
while (size < n) size *= 2;
sums.assign(2 * size, 0);
}
void build(vector<long long> &v, long long now, long long l, long long r) {
if (r - l == 1) {
if (l < v.size()) {
sums[now] = v[l];
}
return;
}
long long mid = (l + r) / 2;
build(v, 2 * now + 1, l, mid);
build(v, 2 * now + 2, mid, r);
sums[now] = sums[2 * now + 1] + sums[2 * now + 2];
}
void build(vector<long long> &v) { build(v, 0, 0, size); }
void set(long long i, long long el, long long now, long long l, long long r) {
if (r - l == 1) {
sums[now] = el;
return;
}
long long mid = (l + r) / 2;
if (i < mid) {
set(i, el, 2 * now + 1, l, mid);
} else
set(i, el, 2 * now + 2, mid, r);
sums[now] = sums[2 * now + 1] + sums[2 * now + 2];
}
void set(long long i, long long el) { set(i, el, 0, 0, size); }
long long sum(long long now, long long ln, long long rn, long long l,
long long r) {
if (ln >= r or rn <= l) return 0;
if (ln >= l and rn <= r) return sums[now];
long long mid = (ln + rn) / 2;
return sum(2 * now + 1, ln, mid, l, r) + sum(2 * now + 2, mid, rn, l, r);
}
long long sum(long long l, long long r) { return sum(0, 0, size, l, r); }
};
long long n, timer = 0;
void dfs(long long u, long long par) {
st[u] = timer++;
for (auto v : adj[u]) {
if (v == par) continue;
dfs(v, u);
}
fi[u] = timer;
}
pair<long long, long long> find_start(long long v, long long p = -1) {
if (adj[v].size() - (p != -1) > 1) return {v, p};
for (auto to : adj[v])
if (to != p) return find_start(to, v);
return {-1, -1};
}
void solve() {
cin >> n;
v = vector<long long>(n);
st = vector<long long>(n);
fi = vector<long long>(n);
for (long long i = (long long)(0); i < (long long)(n); i++) cin >> v[i];
for (long long i = (long long)(1); i < (long long)(n); i++) {
long long u, v;
cin >> u >> v;
--u, --v;
adj[u].push_back(v), adj[v].push_back(u);
}
pair<long long, long long> imp = find_start(0);
if (imp.first == -1) {
cout << "Impossible\n";
return;
}
dfs(0, -1);
vector<long long> order(n), ss;
iota(order.begin(), order.end(), 0);
sort(order.begin(), order.end(),
[&](long long i, long long j) { return st[i] < st[j]; });
for (auto i : order) ss.push_back(v[i]);
seg_tree tree;
tree.make(n);
tree.build(ss);
vector<long long> mx_st(timer + 1, -LL_INF), mx_end(timer + 1, -LL_INF);
for (long long i = (long long)(0); i < (long long)(n); i++) {
mx_st[st[i]] = max(mx_st[st[i]], tree.sum(st[i], fi[i]));
mx_end[fi[i]] = max(mx_end[fi[i]], tree.sum(st[i], fi[i]));
}
for (long long i = (long long)(timer - 1 - 1); i >= (long long)(0); i--)
mx_st[i] = max(mx_st[i + 1], mx_st[i]);
for (long long i = (long long)(1); i < (long long)(timer); i++)
mx_end[i] = max(mx_end[i], mx_end[i - 1]);
long long ans = -LL_INF;
;
for (long long i = (long long)(0); i < (long long)(timer); i++) {
ans = max(ans, mx_end[i] + mx_st[i]);
}
cout << ans << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long power(long long first, long long n) {
if (n == 0) return 1;
if (n % 2)
return (first % mod * power((first % mod * first % mod) % mod, n / 2) %
mod) %
mod;
return power((first % mod * first % mod) % mod, n / 2) % mod;
}
vector<long long> v[2 * 111111];
long long a[2 * 111111];
long long ans = 0 - 1e18;
long long inf = 0 - 1e18;
long long dp[2 * 111111];
long long dfs1(long long first, long long p) {
dp[first] = a[first];
for (auto it : v[first])
if (it - p) dp[first] += dfs1(it, first);
return dp[first];
}
long long dfs2(long long first, long long p) {
long long temp1 = inf, temp2 = inf;
for (auto it : v[first])
if (it - p) {
long long second = dfs2(it, first);
if (temp1 < second) {
swap(temp1, temp2);
temp1 = second;
} else if (temp2 < second) {
temp2 = second;
}
}
if (temp2 != inf) ans = max(ans, temp1 + temp2);
return max(temp1, dp[first]);
}
void solve() {
long long n;
cin >> n;
for (long long i = 1; i < n + 1; i++) cin >> a[i];
for (long long i = 1; i < n; i++) {
long long a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
if (n <= 2) {
cout << "Impossible";
return;
}
dfs1(1, -1);
dfs2(1, -1);
if (ans > inf)
cout << ans;
else
cout << "Impossible";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long power(long long first, long long n) {
if (n == 0) return 1;
if (n % 2)
return (first % mod * power((first % mod * first % mod) % mod, n / 2) %
mod) %
mod;
return power((first % mod * first % mod) % mod, n / 2) % mod;
}
vector<long long> v[2 * 111111];
long long a[2 * 111111];
long long ans = 0 - 1e18;
long long inf = 0 - 1e18;
long long dp[2 * 111111];
long long dfs1(long long first, long long p) {
dp[first] = a[first];
for (auto it : v[first])
if (it - p) dp[first] += dfs1(it, first);
return dp[first];
}
long long dfs2(long long first, long long p) {
long long temp1 = inf, temp2 = inf;
for (auto it : v[first])
if (it - p) {
long long second = dfs2(it, first);
if (temp1 < second) {
swap(temp1, temp2);
temp1 = second;
} else if (temp2 < second) {
temp2 = second;
}
}
if (temp2 != inf) ans = max(ans, temp1 + temp2);
return max(temp1, dp[first]);
}
void solve() {
long long n;
cin >> n;
for (long long i = 1; i < n + 1; i++) cin >> a[i];
for (long long i = 1; i < n; i++) {
long long a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
if (n <= 2) {
cout << "Impossible";
return;
}
dfs1(1, -1);
dfs2(1, -1);
if (ans > inf)
cout << ans;
else
cout << "Impossible";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double EPS = 1e-7;
const long long inf = 1e17;
long long n;
vector<long long> in;
vector<vector<long long> > G;
vector<long long> sum, used;
long long dfs(int a) {
used[a] = true;
long long s = in[a];
for (int i = 0; i < G[a].size(); i++) {
int to = G[a][i];
if (used[to]) continue;
s += dfs(to);
}
return sum[a] = s;
}
long long out = -inf;
void dfs1(int a) {
used[a] = true;
vector<long long> t;
for (int i = 0; i < G[a].size(); i++) {
int to = G[a][i];
if (used[to]) continue;
dfs1(to);
sum[a] = max(sum[a], sum[to]);
t.push_back(-sum[to]);
}
sort(t.begin(), t.end());
if (t.size() >= 2) out = max<long long>(out, -t[0] - t[1]);
}
int main() {
cin >> n;
in = vector<long long>(n);
for (int i = 0; i < n; i++) cin >> in[i];
G = vector<vector<long long> >(n);
for (int i = 0; i < n - 1; i++) {
long long a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
}
sum = vector<long long>(n);
used = vector<long long>(n);
dfs(0);
used = vector<long long>(n);
dfs1(0);
if (out == -inf)
cout << "Impossible" << endl;
else
cout << out << endl;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double EPS = 1e-7;
const long long inf = 1e17;
long long n;
vector<long long> in;
vector<vector<long long> > G;
vector<long long> sum, used;
long long dfs(int a) {
used[a] = true;
long long s = in[a];
for (int i = 0; i < G[a].size(); i++) {
int to = G[a][i];
if (used[to]) continue;
s += dfs(to);
}
return sum[a] = s;
}
long long out = -inf;
void dfs1(int a) {
used[a] = true;
vector<long long> t;
for (int i = 0; i < G[a].size(); i++) {
int to = G[a][i];
if (used[to]) continue;
dfs1(to);
sum[a] = max(sum[a], sum[to]);
t.push_back(-sum[to]);
}
sort(t.begin(), t.end());
if (t.size() >= 2) out = max<long long>(out, -t[0] - t[1]);
}
int main() {
cin >> n;
in = vector<long long>(n);
for (int i = 0; i < n; i++) cin >> in[i];
G = vector<vector<long long> >(n);
for (int i = 0; i < n - 1; i++) {
long long a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
}
sum = vector<long long>(n);
used = vector<long long>(n);
dfs(0);
used = vector<long long>(n);
dfs1(0);
if (out == -inf)
cout << "Impossible" << endl;
else
cout << out << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> vals;
vector<long long> adj[200005];
vector<long long> subsum;
vector<long long> maxsubsum;
vector<long long> h;
void dfs(long long u, long long p) {
h[u] = h[p] + 1;
subsum[u] = vals[u];
for (auto i : adj[u]) {
if (i != p) {
dfs(i, u);
subsum[u] += subsum[i];
maxsubsum[u] = max(maxsubsum[u], maxsubsum[i]);
}
}
maxsubsum[u] = max(subsum[u], maxsubsum[u]);
}
void solve() {
long long n;
cin >> n;
subsum.resize(n + 1);
vals.resize(n + 1);
h.resize(n + 1);
maxsubsum.resize(n + 1, LLONG_MIN);
for (long long i = 1; i < n + 1; i++) cin >> vals[i];
for (long long i = 0; i < n - 1; i++) {
long long a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, 0);
map<long long, vector<long long> > m;
for (long long i = 1; i < n + 1; i++) {
m[h[i]].push_back(maxsubsum[i]);
}
long long ans = LLONG_MIN;
for (auto& i : m) {
sort((i.second).begin(), (i.second).end(), greater<long long>());
if ((long long)i.second.size() > 1) {
ans = max(ans, i.second[0] + i.second[1]);
}
}
if (ans == LLONG_MIN)
cout << "Impossible";
else
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
|
### Prompt
Generate a CPP solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<long long> vals;
vector<long long> adj[200005];
vector<long long> subsum;
vector<long long> maxsubsum;
vector<long long> h;
void dfs(long long u, long long p) {
h[u] = h[p] + 1;
subsum[u] = vals[u];
for (auto i : adj[u]) {
if (i != p) {
dfs(i, u);
subsum[u] += subsum[i];
maxsubsum[u] = max(maxsubsum[u], maxsubsum[i]);
}
}
maxsubsum[u] = max(subsum[u], maxsubsum[u]);
}
void solve() {
long long n;
cin >> n;
subsum.resize(n + 1);
vals.resize(n + 1);
h.resize(n + 1);
maxsubsum.resize(n + 1, LLONG_MIN);
for (long long i = 1; i < n + 1; i++) cin >> vals[i];
for (long long i = 0; i < n - 1; i++) {
long long a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, 0);
map<long long, vector<long long> > m;
for (long long i = 1; i < n + 1; i++) {
m[h[i]].push_back(maxsubsum[i]);
}
long long ans = LLONG_MIN;
for (auto& i : m) {
sort((i.second).begin(), (i.second).end(), greater<long long>());
if ((long long)i.second.size() > 1) {
ans = max(ans, i.second[0] + i.second[1]);
}
}
if (ans == LLONG_MIN)
cout << "Impossible";
else
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T& a) {
a = 0;
char c;
bool _MINUS_ = false;
while (!isdigit(c = getchar())) _MINUS_ |= c == '-';
do a = a * 10 + (c - '0');
while (isdigit(c = getchar()));
if (_MINUS_) a *= -1;
}
string world[8] = {"U", "D", "R", "L", "UL", "UR", "DR", "DL"};
const int rx[8] = {-1, 1, 0, 0, -1, -1, 1, 1};
const int ry[8] = {0, 0, 1, -1, -1, 1, 1, -1};
const long long mod = 1000000007;
const int N = 200100;
int timer = 0;
long long sum[N];
int l[N], r[N];
long long ans = -1000000000000000000;
multiset<long long> all[N];
int a[N];
vector<int> g[N];
int n;
void dfs(int v, int p) {
sum[v] += a[v];
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (to == p) continue;
dfs(to, v);
sum[v] += sum[to];
for (auto it = all[to].begin(); it != all[to].end(); it++) {
all[v].insert(*it);
}
}
while (all[v].size() > 2) all[v].erase(all[v].begin());
if (all[v].size() > 1) {
auto it = all[v].begin();
long long cur = (*it);
it++;
cur += (*it);
ans = max(ans, cur);
}
all[v].insert(sum[v]);
while (all[v].size() > 1) all[v].erase(all[v].begin());
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i + 1];
}
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1, -1);
if (ans == -1000000000000000000) {
cout << "Impossible";
return 0;
}
cout << ans;
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T& a) {
a = 0;
char c;
bool _MINUS_ = false;
while (!isdigit(c = getchar())) _MINUS_ |= c == '-';
do a = a * 10 + (c - '0');
while (isdigit(c = getchar()));
if (_MINUS_) a *= -1;
}
string world[8] = {"U", "D", "R", "L", "UL", "UR", "DR", "DL"};
const int rx[8] = {-1, 1, 0, 0, -1, -1, 1, 1};
const int ry[8] = {0, 0, 1, -1, -1, 1, 1, -1};
const long long mod = 1000000007;
const int N = 200100;
int timer = 0;
long long sum[N];
int l[N], r[N];
long long ans = -1000000000000000000;
multiset<long long> all[N];
int a[N];
vector<int> g[N];
int n;
void dfs(int v, int p) {
sum[v] += a[v];
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (to == p) continue;
dfs(to, v);
sum[v] += sum[to];
for (auto it = all[to].begin(); it != all[to].end(); it++) {
all[v].insert(*it);
}
}
while (all[v].size() > 2) all[v].erase(all[v].begin());
if (all[v].size() > 1) {
auto it = all[v].begin();
long long cur = (*it);
it++;
cur += (*it);
ans = max(ans, cur);
}
all[v].insert(sum[v]);
while (all[v].size() > 1) all[v].erase(all[v].begin());
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i + 1];
}
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1, -1);
if (ans == -1000000000000000000) {
cout << "Impossible";
return 0;
}
cout << ans;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 25;
long long res = -1e18;
int n, a[N];
long long s[N], f[N];
vector<int> G[N];
void dfs(int u, int p) {
s[u] = a[u];
int p1, p2;
p1 = p2 = 0;
for (auto v : G[u]) {
if (v == p) continue;
dfs(v, u);
f[u] = max(f[u], f[v]);
s[u] += s[v];
if (!p1 || f[p1] < f[v])
p2 = p1, p1 = v;
else if (!p2 || f[p2] < f[v])
p2 = v;
}
f[u] = max(f[u], s[u]);
if (p1 && p2) res = max(res, f[p1] + f[p2]);
}
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
f[i] = -1e18;
}
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, 1);
if (res == -1e18)
cout << "Impossible";
else
cout << res;
}
|
### Prompt
Generate a cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 25;
long long res = -1e18;
int n, a[N];
long long s[N], f[N];
vector<int> G[N];
void dfs(int u, int p) {
s[u] = a[u];
int p1, p2;
p1 = p2 = 0;
for (auto v : G[u]) {
if (v == p) continue;
dfs(v, u);
f[u] = max(f[u], f[v]);
s[u] += s[v];
if (!p1 || f[p1] < f[v])
p2 = p1, p1 = v;
else if (!p2 || f[p2] < f[v])
p2 = v;
}
f[u] = max(f[u], s[u]);
if (p1 && p2) res = max(res, f[p1] + f[p2]);
}
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
f[i] = -1e18;
}
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, 1);
if (res == -1e18)
cout << "Impossible";
else
cout << res;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn(2e5 + 5);
const long long inf(1e16 + 5);
int head[maxn], node[maxn << 1], net[maxn << 1], n;
long long val[maxn];
long long mx1[maxn], mx2[maxn], subMax[maxn];
void dfs(int k, int fa) {
int nmd = 0;
for (int i = head[k]; i; i = net[i]) {
if (node[i] == fa) continue;
dfs(node[i], k);
val[k] += val[node[i]], nmd++;
subMax[k] = max(subMax[node[i]], subMax[k]);
}
mx1[k] = mx2[k] = -inf;
subMax[k] = val[k];
if (nmd > 1) {
long long temp;
for (int i = head[k]; i; i = net[i]) {
if (node[i] == fa) continue;
temp = subMax[node[i]];
if (temp > mx1[k])
mx2[k] = mx1[k], mx1[k] = temp;
else if (temp > mx2[k])
mx2[k] = temp;
}
subMax[k] = max(subMax[k], mx1[k]);
for (int i = head[k]; i; i = net[i])
if (node[i] != fa && mx1[node[i]] + mx2[node[i]] > mx2[k] + mx1[k])
mx1[k] = mx1[node[i]], mx2[k] = mx2[node[i]];
} else if (nmd == 1) {
int i = head[k];
if (node[i] == fa) i = net[i];
mx1[k] = mx1[node[i]], mx2[k] = mx2[node[i]];
subMax[k] = max(subMax[k], subMax[node[i]]);
}
}
int main() {
while (~scanf("%d", &n)) {
memset(head, 0, sizeof(head));
for (int i = 1; i <= n; i++) scanf("%I64d", val + i);
for (int i = 1, j = 1, a, b; j < n; j++) {
scanf("%d%d", &a, &b);
node[i] = b, net[i] = head[a], head[a] = i++;
node[i] = a, net[i] = head[b], head[b] = i++;
}
dfs(1, -1);
if (mx1[1] != -inf && mx2[1] != inf)
printf("%I64d\n", mx1[1] + mx2[1]);
else
puts("Impossible");
}
return 0;
}
|
### Prompt
In cpp, your task is to solve the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn(2e5 + 5);
const long long inf(1e16 + 5);
int head[maxn], node[maxn << 1], net[maxn << 1], n;
long long val[maxn];
long long mx1[maxn], mx2[maxn], subMax[maxn];
void dfs(int k, int fa) {
int nmd = 0;
for (int i = head[k]; i; i = net[i]) {
if (node[i] == fa) continue;
dfs(node[i], k);
val[k] += val[node[i]], nmd++;
subMax[k] = max(subMax[node[i]], subMax[k]);
}
mx1[k] = mx2[k] = -inf;
subMax[k] = val[k];
if (nmd > 1) {
long long temp;
for (int i = head[k]; i; i = net[i]) {
if (node[i] == fa) continue;
temp = subMax[node[i]];
if (temp > mx1[k])
mx2[k] = mx1[k], mx1[k] = temp;
else if (temp > mx2[k])
mx2[k] = temp;
}
subMax[k] = max(subMax[k], mx1[k]);
for (int i = head[k]; i; i = net[i])
if (node[i] != fa && mx1[node[i]] + mx2[node[i]] > mx2[k] + mx1[k])
mx1[k] = mx1[node[i]], mx2[k] = mx2[node[i]];
} else if (nmd == 1) {
int i = head[k];
if (node[i] == fa) i = net[i];
mx1[k] = mx1[node[i]], mx2[k] = mx2[node[i]];
subMax[k] = max(subMax[k], subMax[node[i]]);
}
}
int main() {
while (~scanf("%d", &n)) {
memset(head, 0, sizeof(head));
for (int i = 1; i <= n; i++) scanf("%I64d", val + i);
for (int i = 1, j = 1, a, b; j < n; j++) {
scanf("%d%d", &a, &b);
node[i] = b, net[i] = head[a], head[a] = i++;
node[i] = a, net[i] = head[b], head[b] = i++;
}
dfs(1, -1);
if (mx1[1] != -inf && mx2[1] != inf)
printf("%I64d\n", mx1[1] + mx2[1]);
else
puts("Impossible");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> g[200000];
long long int a[200000];
long long int m = LLONG_MIN;
long long int sz[200000];
long long int mx[200000];
void dfs(int cur, int pr) {
sz[cur] = a[cur];
mx[cur] = LLONG_MIN;
vector<long long int> q;
for (int i = 0; i < g[cur].size(); i++)
if (g[cur][i] != pr) {
dfs(g[cur][i], cur);
sz[cur] += sz[g[cur][i]];
mx[cur] = mx[cur] > mx[g[cur][i]] ? mx[cur] : mx[g[cur][i]];
q.push_back(mx[g[cur][i]]);
}
mx[cur] = mx[cur] > sz[cur] ? mx[cur] : sz[cur];
if (q.size() < 2) return;
sort(q.begin(), q.end(), greater<long long int>());
if (q[0] + q[1] > m) m = q[0] + q[1];
}
int main() {
int u, v;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
for (int i = 1; i < n; i++) {
scanf("%d %d", &u, &v);
g[--u].push_back(--v);
g[v].push_back(u);
}
dfs(0, -1);
if (m == LLONG_MIN)
printf("Impossible\n");
else
printf("%lld\n", m);
return 0;
}
|
### Prompt
Create a solution in cpp for the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> g[200000];
long long int a[200000];
long long int m = LLONG_MIN;
long long int sz[200000];
long long int mx[200000];
void dfs(int cur, int pr) {
sz[cur] = a[cur];
mx[cur] = LLONG_MIN;
vector<long long int> q;
for (int i = 0; i < g[cur].size(); i++)
if (g[cur][i] != pr) {
dfs(g[cur][i], cur);
sz[cur] += sz[g[cur][i]];
mx[cur] = mx[cur] > mx[g[cur][i]] ? mx[cur] : mx[g[cur][i]];
q.push_back(mx[g[cur][i]]);
}
mx[cur] = mx[cur] > sz[cur] ? mx[cur] : sz[cur];
if (q.size() < 2) return;
sort(q.begin(), q.end(), greater<long long int>());
if (q[0] + q[1] > m) m = q[0] + q[1];
}
int main() {
int u, v;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
for (int i = 1; i < n; i++) {
scanf("%d %d", &u, &v);
g[--u].push_back(--v);
g[v].push_back(u);
}
dfs(0, -1);
if (m == LLONG_MIN)
printf("Impossible\n");
else
printf("%lld\n", m);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int N = 200005;
long long int n, a[N], ans[N], dp[N], sum[N];
vector<long long int> adj[N];
long long int vis[N];
const long long int MIN_VAL = -2LL * 1000000000 - 5;
void dfs(int v) {
vis[v] = 1;
long long int s = a[v], new_ans = MIN_VAL, new_dp = MIN_VAL, max1 = MIN_VAL,
max2 = MIN_VAL;
for (auto u : adj[v]) {
if (vis[u] == 1) continue;
dfs(u);
s += sum[u];
new_ans = max(new_ans, ans[u]);
new_dp = max(new_dp, dp[u]);
if (max1 > max2) swap(max1, max2);
max1 = max(max1, max(sum[u], dp[u]));
new_dp = max(new_dp, sum[u]);
}
sum[v] = s;
if (max1 != MIN_VAL && max2 != MIN_VAL)
ans[v] = max(new_ans, max1 + max2);
else
ans[v] = new_ans;
dp[v] = new_dp;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
ans[i] = MIN_VAL, dp[i] = MIN_VAL;
cin >> a[i];
}
for (int i = 0; i < n - 1; i++) {
long long int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1);
if (ans[1] == MIN_VAL)
cout << "Impossible" << endl;
else
cout << ans[1] << endl;
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int N = 200005;
long long int n, a[N], ans[N], dp[N], sum[N];
vector<long long int> adj[N];
long long int vis[N];
const long long int MIN_VAL = -2LL * 1000000000 - 5;
void dfs(int v) {
vis[v] = 1;
long long int s = a[v], new_ans = MIN_VAL, new_dp = MIN_VAL, max1 = MIN_VAL,
max2 = MIN_VAL;
for (auto u : adj[v]) {
if (vis[u] == 1) continue;
dfs(u);
s += sum[u];
new_ans = max(new_ans, ans[u]);
new_dp = max(new_dp, dp[u]);
if (max1 > max2) swap(max1, max2);
max1 = max(max1, max(sum[u], dp[u]));
new_dp = max(new_dp, sum[u]);
}
sum[v] = s;
if (max1 != MIN_VAL && max2 != MIN_VAL)
ans[v] = max(new_ans, max1 + max2);
else
ans[v] = new_ans;
dp[v] = new_dp;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
ans[i] = MIN_VAL, dp[i] = MIN_VAL;
cin >> a[i];
}
for (int i = 0; i < n - 1; i++) {
long long int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1);
if (ans[1] == MIN_VAL)
cout << "Impossible" << endl;
else
cout << ans[1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:10240000")
using namespace std;
const int MAXN = 200020;
const long long MAX_INF = (long long)1e16;
struct tedge {
int t, next;
} e[MAXN * 2];
int cnt = 0;
long long ans = -MAX_INF;
long long w[MAXN], dp[MAXN], val[MAXN];
int st[MAXN];
bool flag = false;
void addedge(int x, int y) {
cnt++;
e[cnt].t = y;
e[cnt].next = st[x];
st[x] = cnt;
cnt++;
e[cnt].t = x;
e[cnt].next = st[y];
st[y] = cnt;
}
void dfs(int k, int pre) {
val[k] = w[k];
int numson = 0;
long long maxv = -MAX_INF, maxv2 = -MAX_INF;
for (int i = st[k]; i != -1; i = e[i].next) {
int j = e[i].t;
if (j != pre) {
numson++;
dfs(j, k);
val[k] += val[j];
}
}
dp[k] = val[k];
for (int i = st[k]; i != -1; i = e[i].next) {
int j = e[i].t;
if (j != pre) {
if (dp[j] > dp[k]) {
dp[k] = dp[j];
}
if (dp[j] > maxv) {
maxv2 = maxv;
maxv = dp[j];
} else if (dp[j] > maxv2) {
maxv2 = dp[j];
}
}
}
if (numson >= 2) {
flag = true;
if (maxv + maxv2 > ans) {
ans = maxv + maxv2;
}
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%I64d", &w[i]);
dp[i] = -MAX_INF;
}
memset(st, -1, sizeof(st));
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
addedge(x, y);
}
dfs(1, 0);
if (flag) {
printf("%I64d\n", ans);
} else {
printf("Impossible\n");
}
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:10240000")
using namespace std;
const int MAXN = 200020;
const long long MAX_INF = (long long)1e16;
struct tedge {
int t, next;
} e[MAXN * 2];
int cnt = 0;
long long ans = -MAX_INF;
long long w[MAXN], dp[MAXN], val[MAXN];
int st[MAXN];
bool flag = false;
void addedge(int x, int y) {
cnt++;
e[cnt].t = y;
e[cnt].next = st[x];
st[x] = cnt;
cnt++;
e[cnt].t = x;
e[cnt].next = st[y];
st[y] = cnt;
}
void dfs(int k, int pre) {
val[k] = w[k];
int numson = 0;
long long maxv = -MAX_INF, maxv2 = -MAX_INF;
for (int i = st[k]; i != -1; i = e[i].next) {
int j = e[i].t;
if (j != pre) {
numson++;
dfs(j, k);
val[k] += val[j];
}
}
dp[k] = val[k];
for (int i = st[k]; i != -1; i = e[i].next) {
int j = e[i].t;
if (j != pre) {
if (dp[j] > dp[k]) {
dp[k] = dp[j];
}
if (dp[j] > maxv) {
maxv2 = maxv;
maxv = dp[j];
} else if (dp[j] > maxv2) {
maxv2 = dp[j];
}
}
}
if (numson >= 2) {
flag = true;
if (maxv + maxv2 > ans) {
ans = maxv + maxv2;
}
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%I64d", &w[i]);
dp[i] = -MAX_INF;
}
memset(st, -1, sizeof(st));
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
addedge(x, y);
}
dfs(1, 0);
if (flag) {
printf("%I64d\n", ans);
} else {
printf("Impossible\n");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.1415926535897923846;
const long long int MOD = 1000000007;
const long long int N = 998244353;
long long int power(long long int x, long long int n) {
long long int res = 1;
while (n > 0) {
if (n & 1) res = res * x % MOD;
x = x * x % MOD;
n >>= 1;
}
return res;
}
long long int modinverse(long long int a) { return power(a, MOD - 2); }
long long int n;
vector<long long int> a;
vector<vector<long long int> > adj;
vector<long long int> sz, mxm;
long long int ans = -(long long int)1e18;
long long int dfs(long long int node, long long int parent) {
sz[node] = a[node];
for (auto x : adj[node]) {
if (x == parent) continue;
sz[node] += dfs(x, node);
}
return sz[node];
}
long long int sfd(long long int node, long long int parent) {
long long int mnm = sz[node];
for (auto x : adj[node]) {
if (x == parent) continue;
mnm = max(mnm, sfd(x, node));
}
return mxm[node] = mnm;
}
void ddfs(long long int node, long long int parent) {
vector<long long int> count;
for (auto x : adj[node]) {
if (x == parent) continue;
count.push_back(mxm[x]);
ddfs(x, node);
}
sort(count.begin(), count.end());
reverse(count.begin(), count.end());
if (count.size() >= 2) {
ans = max(ans, count[0] + count[1]);
}
}
void solve() {
cin >> n;
sz.resize(n);
a.resize(n);
for (long long int i = 0; i < n; i++) {
cin >> a[i];
}
adj.resize(n);
for (long long int i = 0; i < n - 1; i++) {
long long int u, v;
cin >> u >> v;
--u, --v;
adj[u].push_back(v), adj[v].push_back(u);
}
sz.resize(n, 0);
dfs(0, -1);
mxm.resize(n, -(long long int)1e18);
sfd(0, -1);
ddfs(0, -1);
if (ans == -(long long int)1e18)
cout << "Impossible\n";
else
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int test = 1;
while (test--) {
solve();
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
<< "ms\n";
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.1415926535897923846;
const long long int MOD = 1000000007;
const long long int N = 998244353;
long long int power(long long int x, long long int n) {
long long int res = 1;
while (n > 0) {
if (n & 1) res = res * x % MOD;
x = x * x % MOD;
n >>= 1;
}
return res;
}
long long int modinverse(long long int a) { return power(a, MOD - 2); }
long long int n;
vector<long long int> a;
vector<vector<long long int> > adj;
vector<long long int> sz, mxm;
long long int ans = -(long long int)1e18;
long long int dfs(long long int node, long long int parent) {
sz[node] = a[node];
for (auto x : adj[node]) {
if (x == parent) continue;
sz[node] += dfs(x, node);
}
return sz[node];
}
long long int sfd(long long int node, long long int parent) {
long long int mnm = sz[node];
for (auto x : adj[node]) {
if (x == parent) continue;
mnm = max(mnm, sfd(x, node));
}
return mxm[node] = mnm;
}
void ddfs(long long int node, long long int parent) {
vector<long long int> count;
for (auto x : adj[node]) {
if (x == parent) continue;
count.push_back(mxm[x]);
ddfs(x, node);
}
sort(count.begin(), count.end());
reverse(count.begin(), count.end());
if (count.size() >= 2) {
ans = max(ans, count[0] + count[1]);
}
}
void solve() {
cin >> n;
sz.resize(n);
a.resize(n);
for (long long int i = 0; i < n; i++) {
cin >> a[i];
}
adj.resize(n);
for (long long int i = 0; i < n - 1; i++) {
long long int u, v;
cin >> u >> v;
--u, --v;
adj[u].push_back(v), adj[v].push_back(u);
}
sz.resize(n, 0);
dfs(0, -1);
mxm.resize(n, -(long long int)1e18);
sfd(0, -1);
ddfs(0, -1);
if (ans == -(long long int)1e18)
cout << "Impossible\n";
else
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int test = 1;
while (test--) {
solve();
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
<< "ms\n";
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long n, a[200005];
vector<long long> adj[200005];
long long ans;
bool Impossible;
long long val[200005], Val[200005];
void dfs(long long u, long long par) {
for (long long i = 0; i < adj[u].size(); i++) {
long long v = adj[u][i];
if (v == par) continue;
dfs(v, u);
}
val[u] = a[u];
for (long long i = 0; i < adj[u].size(); i++) {
long long v = adj[u][i];
if (v == par) continue;
val[u] += Val[v];
}
Val[u] = val[u];
for (long long i = 0; i < adj[u].size(); i++) {
long long v = adj[u][i];
if (v == par) continue;
val[u] = max(val[u], val[v]);
}
if (adj[u].size() <= 1) return;
if (u != 1 && adj[u].size() == 2) return;
Impossible = false;
long long a = 0;
if (adj[u][a] == par) a++;
for (long long i = 0; i < adj[u].size(); i++) {
long long v = adj[u][i];
if (v == par) continue;
if (val[v] >= val[adj[u][a]]) a = i;
}
long long b = 0;
while (adj[u][b] == par || b == a) b++;
for (long long i = 0; i < adj[u].size(); i++) {
long long v = adj[u][i];
if (v == par || a == i) continue;
if (val[v] >= val[adj[u][b]]) b = i;
}
a = val[adj[u][a]];
b = val[adj[u][b]];
ans = max(ans, a + b);
return;
}
void solve() {
scanf("%lld", &n);
for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (long long i = 1; i < n; i++) {
long long u, v;
scanf("%lld %lld", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
Impossible = true;
ans = -1e16;
dfs(1, 0);
if (Impossible)
printf("Impossible");
else
printf("%lld", ans);
return;
}
int main() {
long long T = 1;
for (long long i = 1; i <= T; i++) {
solve();
}
return 0;
}
|
### Prompt
In CPP, your task is to solve the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long n, a[200005];
vector<long long> adj[200005];
long long ans;
bool Impossible;
long long val[200005], Val[200005];
void dfs(long long u, long long par) {
for (long long i = 0; i < adj[u].size(); i++) {
long long v = adj[u][i];
if (v == par) continue;
dfs(v, u);
}
val[u] = a[u];
for (long long i = 0; i < adj[u].size(); i++) {
long long v = adj[u][i];
if (v == par) continue;
val[u] += Val[v];
}
Val[u] = val[u];
for (long long i = 0; i < adj[u].size(); i++) {
long long v = adj[u][i];
if (v == par) continue;
val[u] = max(val[u], val[v]);
}
if (adj[u].size() <= 1) return;
if (u != 1 && adj[u].size() == 2) return;
Impossible = false;
long long a = 0;
if (adj[u][a] == par) a++;
for (long long i = 0; i < adj[u].size(); i++) {
long long v = adj[u][i];
if (v == par) continue;
if (val[v] >= val[adj[u][a]]) a = i;
}
long long b = 0;
while (adj[u][b] == par || b == a) b++;
for (long long i = 0; i < adj[u].size(); i++) {
long long v = adj[u][i];
if (v == par || a == i) continue;
if (val[v] >= val[adj[u][b]]) b = i;
}
a = val[adj[u][a]];
b = val[adj[u][b]];
ans = max(ans, a + b);
return;
}
void solve() {
scanf("%lld", &n);
for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (long long i = 1; i < n; i++) {
long long u, v;
scanf("%lld %lld", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
Impossible = true;
ans = -1e16;
dfs(1, 0);
if (Impossible)
printf("Impossible");
else
printf("%lld", ans);
return;
}
int main() {
long long T = 1;
for (long long i = 1; i <= T; i++) {
solve();
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
long long x[N];
vector<int> e[N];
long long m[N], t[N];
long long ans;
int n;
int u, v;
bool was[N];
const long long inf = 1e18;
void dfs(int id) {
was[id] = 1;
t[id] = x[id];
vector<long long> ch;
for (int i = 0; i < e[id].size(); i++) {
int vv = e[id][i];
if (!was[vv]) {
dfs(vv);
t[id] += t[vv];
ch.push_back(m[vv]);
}
}
m[id] = t[id];
if (ch.size()) {
sort(ch.begin(), ch.end());
m[id] = max(ch.back(), t[id]);
}
if (ch.size() > 1) {
ans = max(ans, ch[(int)ch.size() - 1] + ch[(int)ch.size() - 2]);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &x[i]);
m[i] = -inf;
}
ans = -inf;
for (int i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
e[u].push_back(v);
e[v].push_back(u);
}
dfs(1);
if (ans == -inf) {
puts("Impossible");
return 0;
}
printf("%lld\n", ans);
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
long long x[N];
vector<int> e[N];
long long m[N], t[N];
long long ans;
int n;
int u, v;
bool was[N];
const long long inf = 1e18;
void dfs(int id) {
was[id] = 1;
t[id] = x[id];
vector<long long> ch;
for (int i = 0; i < e[id].size(); i++) {
int vv = e[id][i];
if (!was[vv]) {
dfs(vv);
t[id] += t[vv];
ch.push_back(m[vv]);
}
}
m[id] = t[id];
if (ch.size()) {
sort(ch.begin(), ch.end());
m[id] = max(ch.back(), t[id]);
}
if (ch.size() > 1) {
ans = max(ans, ch[(int)ch.size() - 1] + ch[(int)ch.size() - 2]);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &x[i]);
m[i] = -inf;
}
ans = -inf;
for (int i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
e[u].push_back(v);
e[v].push_back(u);
}
dfs(1);
if (ans == -inf) {
puts("Impossible");
return 0;
}
printf("%lld\n", ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000000000007LL;
vector<vector<long long> > adj(200200);
long long ans[200200], val[200200];
long long finalAns;
void dfs(int u, int p) {
long long mx1 = -MOD, mx2 = -MOD, sum = 0;
for (int i = 0; i < adj[u].size(); i++) {
int cur = adj[u][i];
if (cur == p) continue;
dfs(cur, u);
if (ans[cur] >= mx1) {
mx2 = mx1;
mx1 = ans[cur];
} else if (ans[cur] >= mx2) {
mx2 = ans[cur];
}
sum += val[cur];
}
val[u] += sum;
ans[u] = max(mx1, val[u]);
if (mx2 != -MOD) {
finalAns = max(finalAns, mx1 + mx2);
}
}
int main() {
long long n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> val[i];
}
for (int i = 0; i < n - 1; i++) {
long long x, y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
finalAns = -MOD;
dfs(1, -1);
if (finalAns == -MOD) {
cout << "Impossible\n";
} else {
cout << finalAns << "\n";
}
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000000000007LL;
vector<vector<long long> > adj(200200);
long long ans[200200], val[200200];
long long finalAns;
void dfs(int u, int p) {
long long mx1 = -MOD, mx2 = -MOD, sum = 0;
for (int i = 0; i < adj[u].size(); i++) {
int cur = adj[u][i];
if (cur == p) continue;
dfs(cur, u);
if (ans[cur] >= mx1) {
mx2 = mx1;
mx1 = ans[cur];
} else if (ans[cur] >= mx2) {
mx2 = ans[cur];
}
sum += val[cur];
}
val[u] += sum;
ans[u] = max(mx1, val[u]);
if (mx2 != -MOD) {
finalAns = max(finalAns, mx1 + mx2);
}
}
int main() {
long long n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> val[i];
}
for (int i = 0; i < n - 1; i++) {
long long x, y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
finalAns = -MOD;
dfs(1, -1);
if (finalAns == -MOD) {
cout << "Impossible\n";
} else {
cout << finalAns << "\n";
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
void In() { freopen("in.in", "r", stdin); }
void Out() { freopen("out.out", "w", stdout); }
const int N = 2e5 + 10;
const int M = 3e5 + 10;
const int Mbit = 1e6 + 10;
const long long inf = 1e15;
const long long mod = 1e9 + 7;
long long val[N], dp[N], ans;
vector<int> G[N];
void dfs(int u, int fa) {
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (v == fa) continue;
dfs(v, u);
val[u] += val[v];
if (dp[u] != -inf) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], val[u]);
}
int main() {
int n;
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; i++) scanf("%lld", &val[i]);
for (int i = 1; i <= n; i++) G[i].clear(), dp[i] = -inf;
ans = -inf;
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, -1);
if (ans != -inf)
printf("%lld\n", ans);
else
puts("Impossible");
}
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void In() { freopen("in.in", "r", stdin); }
void Out() { freopen("out.out", "w", stdout); }
const int N = 2e5 + 10;
const int M = 3e5 + 10;
const int Mbit = 1e6 + 10;
const long long inf = 1e15;
const long long mod = 1e9 + 7;
long long val[N], dp[N], ans;
vector<int> G[N];
void dfs(int u, int fa) {
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (v == fa) continue;
dfs(v, u);
val[u] += val[v];
if (dp[u] != -inf) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], val[u]);
}
int main() {
int n;
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; i++) scanf("%lld", &val[i]);
for (int i = 1; i <= n; i++) G[i].clear(), dp[i] = -inf;
ans = -inf;
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, -1);
if (ans != -inf)
printf("%lld\n", ans);
else
puts("Impossible");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 7;
const int INF = 1e9;
long long n, m, l, k, t, p, q, a, b, c, d, x, y, tmp = 0, ans = -1e18;
vector<long long> edge[MAX];
long long pleasantness[MAX];
long long totalPleasantness[MAX];
long long maxPleasantness[MAX];
long long dfs(long long now, long long par) {
totalPleasantness[now] = pleasantness[now];
maxPleasantness[now] = -1e18;
vector<long long> child;
for (long long to : edge[now]) {
if (par == to) continue;
totalPleasantness[now] += dfs(to, now);
maxPleasantness[now] = max(maxPleasantness[now], maxPleasantness[to]);
child.push_back(to);
}
maxPleasantness[now] = max(maxPleasantness[now], totalPleasantness[now]);
if (child.size() > 1) {
vector<long long> cnt;
for (long long ekhon : child) cnt.push_back(maxPleasantness[ekhon]);
sort(cnt.begin(), cnt.end());
ans = max(ans, cnt[cnt.size() - 1] + cnt[cnt.size() - 2]);
}
return totalPleasantness[now];
}
long long MAIN() {
cin >> n;
for (long long i = 1; i <= n; ++i) cin >> pleasantness[i];
for (long long i = 1; i < n; ++i) {
cin >> a >> b;
edge[a].push_back(b);
edge[b].push_back(a);
}
dfs(1, -1);
if (ans == -1e18)
cout << "Impossible" << endl;
else
cout << ans << endl;
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
long long start = clock();
cout << fixed << setprecision(16);
long long ret = MAIN();
return ret;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 7;
const int INF = 1e9;
long long n, m, l, k, t, p, q, a, b, c, d, x, y, tmp = 0, ans = -1e18;
vector<long long> edge[MAX];
long long pleasantness[MAX];
long long totalPleasantness[MAX];
long long maxPleasantness[MAX];
long long dfs(long long now, long long par) {
totalPleasantness[now] = pleasantness[now];
maxPleasantness[now] = -1e18;
vector<long long> child;
for (long long to : edge[now]) {
if (par == to) continue;
totalPleasantness[now] += dfs(to, now);
maxPleasantness[now] = max(maxPleasantness[now], maxPleasantness[to]);
child.push_back(to);
}
maxPleasantness[now] = max(maxPleasantness[now], totalPleasantness[now]);
if (child.size() > 1) {
vector<long long> cnt;
for (long long ekhon : child) cnt.push_back(maxPleasantness[ekhon]);
sort(cnt.begin(), cnt.end());
ans = max(ans, cnt[cnt.size() - 1] + cnt[cnt.size() - 2]);
}
return totalPleasantness[now];
}
long long MAIN() {
cin >> n;
for (long long i = 1; i <= n; ++i) cin >> pleasantness[i];
for (long long i = 1; i < n; ++i) {
cin >> a >> b;
edge[a].push_back(b);
edge[b].push_back(a);
}
dfs(1, -1);
if (ans == -1e18)
cout << "Impossible" << endl;
else
cout << ans << endl;
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
long long start = clock();
cout << fixed << setprecision(16);
long long ret = MAIN();
return ret;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long inf = (1LL << 60);
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
const int maxn = 5e5 + 10;
vector<int> g[maxn];
int a[maxn];
int n;
long long ans = -inf;
long long dp[maxn], f[maxn];
void dfs(int no, int p) {
int sz = ((int)(g[no]).size());
f[no] = a[no];
dp[no] = -inf;
for (int i = 0; i <= sz - 1; i++) {
if (g[no][i] == p) continue;
dfs(g[no][i], no);
f[no] += f[g[no][i]];
if (dp[no] > -inf) ans = max(ans, dp[no] + dp[g[no][i]]);
dp[no] = max(dp[no], dp[g[no][i]]);
}
dp[no] = max(dp[no], f[no]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n - 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1, 0);
if (ans > -inf)
printf("%lld\n", ans);
else {
puts("Impossible");
}
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long inf = (1LL << 60);
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
const int maxn = 5e5 + 10;
vector<int> g[maxn];
int a[maxn];
int n;
long long ans = -inf;
long long dp[maxn], f[maxn];
void dfs(int no, int p) {
int sz = ((int)(g[no]).size());
f[no] = a[no];
dp[no] = -inf;
for (int i = 0; i <= sz - 1; i++) {
if (g[no][i] == p) continue;
dfs(g[no][i], no);
f[no] += f[g[no][i]];
if (dp[no] > -inf) ans = max(ans, dp[no] + dp[g[no][i]]);
dp[no] = max(dp[no], dp[g[no][i]]);
}
dp[no] = max(dp[no], f[no]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n - 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1, 0);
if (ans > -inf)
printf("%lld\n", ans);
else {
puts("Impossible");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
vector<int> edge[N];
int v[N], in[N];
long long sum[N], dp[N], res = -1e18;
int n;
void dfs(int x, int f) {
sum[x] = v[x];
long long fir = -1e18, sec = -1e18;
for (int i = 0; i < edge[x].size(); i++) {
int y = edge[x][i];
if (y == f) continue;
dfs(y, x);
sum[x] += sum[y];
dp[x] = max(dp[x], dp[y]);
if (dp[y] > fir)
sec = fir, fir = dp[y];
else if (dp[y] > sec)
sec = dp[y];
}
if (sec != -1e18) res = max(res, fir + sec);
dp[x] = max(dp[x], sum[x]);
}
int main() {
scanf("%d", &n);
memset(dp, -127, sizeof(dp));
for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
for (int i = 1, u, v; i < n; i++) {
scanf("%d%d", &u, &v);
edge[u].push_back(v);
in[u]++;
edge[v].push_back(u);
in[v]++;
}
long long fir = -1e18, sec = -1e18;
dfs(1, 0);
for (int i = 0; i < edge[1].size(); i++) {
int y = edge[1][i];
if (dp[y] > fir)
sec = fir, fir = dp[y];
else if (dp[y] > sec)
sec = dp[y];
}
if (sec != -1e18) res = max(res, fir + sec);
if (res == -1e18)
puts("Impossible");
else
printf("%lld\n", res);
return 0;
}
|
### Prompt
Please create a solution in Cpp to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
vector<int> edge[N];
int v[N], in[N];
long long sum[N], dp[N], res = -1e18;
int n;
void dfs(int x, int f) {
sum[x] = v[x];
long long fir = -1e18, sec = -1e18;
for (int i = 0; i < edge[x].size(); i++) {
int y = edge[x][i];
if (y == f) continue;
dfs(y, x);
sum[x] += sum[y];
dp[x] = max(dp[x], dp[y]);
if (dp[y] > fir)
sec = fir, fir = dp[y];
else if (dp[y] > sec)
sec = dp[y];
}
if (sec != -1e18) res = max(res, fir + sec);
dp[x] = max(dp[x], sum[x]);
}
int main() {
scanf("%d", &n);
memset(dp, -127, sizeof(dp));
for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
for (int i = 1, u, v; i < n; i++) {
scanf("%d%d", &u, &v);
edge[u].push_back(v);
in[u]++;
edge[v].push_back(u);
in[v]++;
}
long long fir = -1e18, sec = -1e18;
dfs(1, 0);
for (int i = 0; i < edge[1].size(); i++) {
int y = edge[1][i];
if (dp[y] > fir)
sec = fir, fir = dp[y];
else if (dp[y] > sec)
sec = dp[y];
}
if (sec != -1e18) res = max(res, fir + sec);
if (res == -1e18)
puts("Impossible");
else
printf("%lld\n", res);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long int mul(long long int x, long long int y) {
long long int ans = 1;
while (y > 0) {
if (y & 1) ans = (ans * x) % -10000000000000007;
x = (x * x) % -10000000000000007;
y /= 2;
}
return ans;
};
vector<vector<long long int> > v(200005);
long long int a[200005], ans = -10000000000000007, vis[200005];
pair<long long int, long long int> run(long long int node) {
vis[node] = 1;
long long int i, l = v[node].size(), val = 0;
pair<long long int, long long int> p;
multiset<long long int> s;
for (i = 0; i < l; i++)
if (!vis[v[node][i]]) {
p = run(v[node][i]);
s.insert(p.second);
val += p.first;
}
val += a[node];
long long int fin = val;
if (s.size() > 0) fin = max(val, *s.rbegin());
if (s.size() > 1) {
long long int x = *s.rbegin();
s.erase(s.find(*s.rbegin()));
long long int y = *s.rbegin();
ans = max(ans, x + y);
}
return make_pair(val, fin);
};
int main() {
long long int n, i, x, y;
cin >> n;
for (i = 1; i <= n; i++) cin >> a[i];
for (i = 1; i < n; i++) {
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
run(1);
if (ans == -10000000000000007)
cout << "Impossible";
else
cout << ans;
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int mul(long long int x, long long int y) {
long long int ans = 1;
while (y > 0) {
if (y & 1) ans = (ans * x) % -10000000000000007;
x = (x * x) % -10000000000000007;
y /= 2;
}
return ans;
};
vector<vector<long long int> > v(200005);
long long int a[200005], ans = -10000000000000007, vis[200005];
pair<long long int, long long int> run(long long int node) {
vis[node] = 1;
long long int i, l = v[node].size(), val = 0;
pair<long long int, long long int> p;
multiset<long long int> s;
for (i = 0; i < l; i++)
if (!vis[v[node][i]]) {
p = run(v[node][i]);
s.insert(p.second);
val += p.first;
}
val += a[node];
long long int fin = val;
if (s.size() > 0) fin = max(val, *s.rbegin());
if (s.size() > 1) {
long long int x = *s.rbegin();
s.erase(s.find(*s.rbegin()));
long long int y = *s.rbegin();
ans = max(ans, x + y);
}
return make_pair(val, fin);
};
int main() {
long long int n, i, x, y;
cin >> n;
for (i = 1; i <= n; i++) cin >> a[i];
for (i = 1; i < n; i++) {
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
run(1);
if (ans == -10000000000000007)
cout << "Impossible";
else
cout << ans;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 0.0000001;
const long long inf = -1 * (1e17 + 7);
const int maxn = 200005;
long long dp[maxn], m1, m2, ans, f[maxn], num[maxn];
int flag, vis[maxn];
vector<int> son[maxn];
void dfs(int u, int fa) {
f[u] = num[u];
dp[u] = inf;
long long tmp = inf;
if (son[u].size() >= 3)
flag = 1;
else if (son[u].size() >= 2 && u == 1)
flag = 1;
for (auto v : son[u]) {
if (fa == v) continue;
dfs(v, u);
ans = max(ans, tmp + dp[v]);
f[u] += f[v];
dp[u] = max(dp[u], dp[v]);
tmp = max(tmp, dp[v]);
}
dp[u] = max(dp[u], f[u]);
}
int main() {
int n;
scanf("%d", &n);
memset(dp, 0, sizeof(dp));
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++) scanf("%I64d", &num[i]);
for (int i = 1; i <= n - 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
son[x].push_back(y);
son[y].push_back(x);
}
ans = inf;
m1 = inf;
dfs(1, -1);
if (!flag)
printf("Impossible\n");
else
printf("%I64d\n", ans);
}
|
### Prompt
Please create a solution in Cpp to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double eps = 0.0000001;
const long long inf = -1 * (1e17 + 7);
const int maxn = 200005;
long long dp[maxn], m1, m2, ans, f[maxn], num[maxn];
int flag, vis[maxn];
vector<int> son[maxn];
void dfs(int u, int fa) {
f[u] = num[u];
dp[u] = inf;
long long tmp = inf;
if (son[u].size() >= 3)
flag = 1;
else if (son[u].size() >= 2 && u == 1)
flag = 1;
for (auto v : son[u]) {
if (fa == v) continue;
dfs(v, u);
ans = max(ans, tmp + dp[v]);
f[u] += f[v];
dp[u] = max(dp[u], dp[v]);
tmp = max(tmp, dp[v]);
}
dp[u] = max(dp[u], f[u]);
}
int main() {
int n;
scanf("%d", &n);
memset(dp, 0, sizeof(dp));
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++) scanf("%I64d", &num[i]);
for (int i = 1; i <= n - 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
son[x].push_back(y);
son[y].push_back(x);
}
ans = inf;
m1 = inf;
dfs(1, -1);
if (!flag)
printf("Impossible\n");
else
printf("%I64d\n", ans);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 10;
const long long INF = 1000000000000000000;
long long a[maxn];
long long b[maxn];
vector<int> G[maxn];
long long dp[maxn];
long long dfs(int u, int v) {
if (b[u] == -INF) {
b[u] = a[u];
for (int i = 0; i < G[u].size(); ++i) {
if (G[u][i] != v) {
b[u] += dfs(G[u][i], u);
}
}
return b[u];
} else
return b[u];
}
long long ans;
void dfs2(int u, int v) {
for (int i = 0; i < G[u].size(); ++i) {
if (G[u][i] != v) {
dfs2(G[u][i], u);
if (dp[u] != -INF && dp[G[u][i]] != -INF)
ans = max(ans, dp[u] + dp[G[u][i]]);
dp[u] = max(dp[u], dp[G[u][i]]);
}
}
dp[u] = max(dp[u], b[u]);
}
int main() {
int n;
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; ++i) dp[i] = -INF;
for (int i = 1; i <= n; ++i) b[i] = -INF;
for (int i = 1; i <= n; ++i) scanf("%I64d", &a[i]);
for (int i = 1; i < n; ++i) {
int x, y;
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
ans = -INF;
dfs(1, -1);
dfs2(1, -1);
if (ans == -INF)
printf("Impossible\n");
else
printf("%I64d\n", ans);
}
}
|
### Prompt
Please create a solution in Cpp to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 10;
const long long INF = 1000000000000000000;
long long a[maxn];
long long b[maxn];
vector<int> G[maxn];
long long dp[maxn];
long long dfs(int u, int v) {
if (b[u] == -INF) {
b[u] = a[u];
for (int i = 0; i < G[u].size(); ++i) {
if (G[u][i] != v) {
b[u] += dfs(G[u][i], u);
}
}
return b[u];
} else
return b[u];
}
long long ans;
void dfs2(int u, int v) {
for (int i = 0; i < G[u].size(); ++i) {
if (G[u][i] != v) {
dfs2(G[u][i], u);
if (dp[u] != -INF && dp[G[u][i]] != -INF)
ans = max(ans, dp[u] + dp[G[u][i]]);
dp[u] = max(dp[u], dp[G[u][i]]);
}
}
dp[u] = max(dp[u], b[u]);
}
int main() {
int n;
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; ++i) dp[i] = -INF;
for (int i = 1; i <= n; ++i) b[i] = -INF;
for (int i = 1; i <= n; ++i) scanf("%I64d", &a[i]);
for (int i = 1; i < n; ++i) {
int x, y;
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
ans = -INF;
dfs(1, -1);
dfs2(1, -1);
if (ans == -INF)
printf("Impossible\n");
else
printf("%I64d\n", ans);
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 222222;
vector<int> E[MAXN];
int a[MAXN];
long long dp[MAXN];
long long s[MAXN];
int dad[MAXN];
long long ans = -(1LL << 60);
void check_ans(int node) {
vector<long long> best;
for (int i = 0; i < E[node].size(); i++) {
int to = E[node][i];
if (to == dad[node]) continue;
best.push_back(dp[to]);
}
if (best.size() >= 2) {
sort(best.begin(), best.end());
int sz = best.size();
ans = max(ans, best[sz - 1] + best[sz - 2]);
}
}
void dfs(int node, int d = -1) {
dad[node] = d;
s[node] = a[node];
for (int i = 0; i < E[node].size(); i++) {
int to = E[node][i];
if (d == to) continue;
dfs(to, node);
s[node] += s[to];
}
dp[node] = s[node];
for (int i = 0; i < E[node].size(); i++) {
int to = E[node][i];
if (to == d) continue;
dp[node] = max(dp[node], dp[to]);
}
check_ans(node);
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
E[u].push_back(v);
E[v].push_back(u);
}
dfs(1);
if (ans == -(1LL << 60)) {
cout << "Impossible" << endl;
return 0;
}
cout << ans << endl;
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 222222;
vector<int> E[MAXN];
int a[MAXN];
long long dp[MAXN];
long long s[MAXN];
int dad[MAXN];
long long ans = -(1LL << 60);
void check_ans(int node) {
vector<long long> best;
for (int i = 0; i < E[node].size(); i++) {
int to = E[node][i];
if (to == dad[node]) continue;
best.push_back(dp[to]);
}
if (best.size() >= 2) {
sort(best.begin(), best.end());
int sz = best.size();
ans = max(ans, best[sz - 1] + best[sz - 2]);
}
}
void dfs(int node, int d = -1) {
dad[node] = d;
s[node] = a[node];
for (int i = 0; i < E[node].size(); i++) {
int to = E[node][i];
if (d == to) continue;
dfs(to, node);
s[node] += s[to];
}
dp[node] = s[node];
for (int i = 0; i < E[node].size(); i++) {
int to = E[node][i];
if (to == d) continue;
dp[node] = max(dp[node], dp[to]);
}
check_ans(node);
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
E[u].push_back(v);
E[v].push_back(u);
}
dfs(1);
if (ans == -(1LL << 60)) {
cout << "Impossible" << endl;
return 0;
}
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> g;
vector<long long> dp;
vector<pair<long long, long long>> ans;
vector<bool> used;
vector<long long> cost;
int n;
void dfs(int v) {
used[v] = true;
vector<long long> sons;
sons.push_back(-1e18);
sons.push_back(-1e18);
for (int i = 0; i < g[v].size(); i++) {
if (!used[g[v][i]]) {
dfs(g[v][i]);
dp[v] += dp[g[v][i]];
if (dp[g[v][i]] > ans[g[v][i]].first || ans[g[v][i]].first == -1e18)
sons.push_back(dp[g[v][i]]);
else {
sons.push_back(ans[g[v][i]].first);
sons.push_back(ans[g[v][i]].second);
}
}
}
sort(sons.rbegin(), sons.rend());
ans[v].first = sons[0];
ans[v].second = sons[1];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
if (n == 1) {
cout << "Impossible";
return 0;
}
g.resize(n);
dp.resize(n);
ans.resize(n);
used.resize(n);
cost.resize(n);
for (int i = 0; i < n; i++) {
cin >> dp[i];
cost[i] = dp[i];
}
for (int i = 0; i < n - 1; i++) {
int v, u;
cin >> v >> u;
v--;
u--;
g[v].push_back(u);
g[u].push_back(v);
}
dfs(0);
long long mp = -1e18;
for (int i = 0; i < n; i++) {
mp = max(mp, ans[i].first + ans[i].second);
}
if (mp < -1e15) {
cout << "Impossible";
} else {
cout << mp;
}
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> g;
vector<long long> dp;
vector<pair<long long, long long>> ans;
vector<bool> used;
vector<long long> cost;
int n;
void dfs(int v) {
used[v] = true;
vector<long long> sons;
sons.push_back(-1e18);
sons.push_back(-1e18);
for (int i = 0; i < g[v].size(); i++) {
if (!used[g[v][i]]) {
dfs(g[v][i]);
dp[v] += dp[g[v][i]];
if (dp[g[v][i]] > ans[g[v][i]].first || ans[g[v][i]].first == -1e18)
sons.push_back(dp[g[v][i]]);
else {
sons.push_back(ans[g[v][i]].first);
sons.push_back(ans[g[v][i]].second);
}
}
}
sort(sons.rbegin(), sons.rend());
ans[v].first = sons[0];
ans[v].second = sons[1];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
if (n == 1) {
cout << "Impossible";
return 0;
}
g.resize(n);
dp.resize(n);
ans.resize(n);
used.resize(n);
cost.resize(n);
for (int i = 0; i < n; i++) {
cin >> dp[i];
cost[i] = dp[i];
}
for (int i = 0; i < n - 1; i++) {
int v, u;
cin >> v >> u;
v--;
u--;
g[v].push_back(u);
g[u].push_back(v);
}
dfs(0);
long long mp = -1e18;
for (int i = 0; i < n; i++) {
mp = max(mp, ans[i].first + ans[i].second);
}
if (mp < -1e15) {
cout << "Impossible";
} else {
cout << mp;
}
}
```
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
map<long long, long long> mp;
bool vis[200005];
vector<vector<int>> edges(2e5 + 5);
long long n, u, v, ans, a[200005], s[200005];
long long dfs(int node) {
long long sum = a[node];
vis[node] = 1;
for (auto i : edges[node]) {
if (!vis[i]) sum += dfs(i);
}
return s[node] = sum;
}
long long getmx(int node) {
vis[node] = 1;
priority_queue<long long> pq;
for (auto i : edges[node]) {
if (!vis[i]) {
long long mx = getmx(i);
pq.push(mx);
}
}
long long mx = 0;
if (!pq.empty()) {
mx += pq.top(), pq.pop();
if (!pq.empty()) {
ans = max(ans, mx + pq.top());
}
pq.push(mx);
}
pq.push(s[node]);
return pq.top();
}
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
edges[u].push_back(v);
edges[v].push_back(u);
}
dfs(1);
memset(vis, 0, sizeof vis);
ans = LLONG_MIN;
getmx(1);
if (ans == LLONG_MIN) return cout << "Impossible", 0;
cout << ans << endl;
return 0;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
map<long long, long long> mp;
bool vis[200005];
vector<vector<int>> edges(2e5 + 5);
long long n, u, v, ans, a[200005], s[200005];
long long dfs(int node) {
long long sum = a[node];
vis[node] = 1;
for (auto i : edges[node]) {
if (!vis[i]) sum += dfs(i);
}
return s[node] = sum;
}
long long getmx(int node) {
vis[node] = 1;
priority_queue<long long> pq;
for (auto i : edges[node]) {
if (!vis[i]) {
long long mx = getmx(i);
pq.push(mx);
}
}
long long mx = 0;
if (!pq.empty()) {
mx += pq.top(), pq.pop();
if (!pq.empty()) {
ans = max(ans, mx + pq.top());
}
pq.push(mx);
}
pq.push(s[node]);
return pq.top();
}
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
edges[u].push_back(v);
edges[v].push_back(u);
}
dfs(1);
memset(vis, 0, sizeof vis);
ans = LLONG_MIN;
getmx(1);
if (ans == LLONG_MIN) return cout << "Impossible", 0;
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct nevun {
long long sumtot, maxun, doima, ta;
};
vector<int> g[200005];
long long val[200005], n;
nevun tati[200005];
void setarb(int nod) {
long long maxnow = (0 - (1LL << 60));
long long maxlo = (0 - (1LL << 60));
tati[nod].sumtot = val[nod];
tati[nod].maxun = (0 - (1LL << 60));
tati[nod].doima = (0 - (1LL << 60));
for (int i = 0; i < g[nod].size(); i++) {
if (tati[g[nod][i]].ta == nod) {
setarb(g[nod][i]);
tati[nod].sumtot = tati[nod].sumtot + tati[g[nod][i]].sumtot;
tati[nod].maxun = max(tati[nod].maxun, tati[g[nod][i]].maxun);
tati[nod].doima = max(tati[nod].doima, tati[g[nod][i]].doima);
if (tati[g[nod][i]].maxun >= maxnow) {
maxlo = maxnow;
maxnow = tati[g[nod][i]].maxun;
} else {
if (tati[g[nod][i]].maxun >= maxlo) {
maxlo = tati[g[nod][i]].maxun;
}
}
}
}
tati[nod].maxun = max(tati[nod].maxun, tati[nod].sumtot);
int q = g[nod].size();
long long x = tati[nod].doima;
if (q > 2) {
tati[nod].doima = maxlo + maxnow;
tati[nod].doima = max(tati[nod].doima, x);
} else {
if (nod == 1) {
if (q == 2) {
tati[nod].doima = maxlo + maxnow;
tati[nod].doima = max(tati[nod].doima, x);
} else {
tati[nod].doima = tati[g[nod][0]].doima;
}
} else {
if (q == 2) {
if (tati[nod].ta == g[nod][0]) {
tati[nod].doima = tati[g[nod][1]].doima;
} else {
tati[nod].doima = tati[g[nod][0]].doima;
}
} else {
tati[nod].doima = (0 - (1LL << 60));
}
}
}
}
void dfs(int nod) {
for (int i = 0; i < g[nod].size(); i++) {
if (tati[g[nod][i]].ta == 0) {
tati[g[nod][i]].ta = nod;
dfs(g[nod][i]);
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%I64d", &val[i]);
}
for (int i = 1; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
g[a].push_back(b);
g[b].push_back(a);
}
if (n > 1) {
tati[1].ta = -1;
dfs(1);
setarb(1);
}
int gay;
gay = 0;
for (int i = 1; i <= n; i++) {
int q = g[i].size();
gay = max(gay, q);
}
if (n == 1 || (gay <= 2 && g[1].size() == 1)) {
printf("Impossible");
} else {
printf("%I64d", tati[1].doima);
}
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct nevun {
long long sumtot, maxun, doima, ta;
};
vector<int> g[200005];
long long val[200005], n;
nevun tati[200005];
void setarb(int nod) {
long long maxnow = (0 - (1LL << 60));
long long maxlo = (0 - (1LL << 60));
tati[nod].sumtot = val[nod];
tati[nod].maxun = (0 - (1LL << 60));
tati[nod].doima = (0 - (1LL << 60));
for (int i = 0; i < g[nod].size(); i++) {
if (tati[g[nod][i]].ta == nod) {
setarb(g[nod][i]);
tati[nod].sumtot = tati[nod].sumtot + tati[g[nod][i]].sumtot;
tati[nod].maxun = max(tati[nod].maxun, tati[g[nod][i]].maxun);
tati[nod].doima = max(tati[nod].doima, tati[g[nod][i]].doima);
if (tati[g[nod][i]].maxun >= maxnow) {
maxlo = maxnow;
maxnow = tati[g[nod][i]].maxun;
} else {
if (tati[g[nod][i]].maxun >= maxlo) {
maxlo = tati[g[nod][i]].maxun;
}
}
}
}
tati[nod].maxun = max(tati[nod].maxun, tati[nod].sumtot);
int q = g[nod].size();
long long x = tati[nod].doima;
if (q > 2) {
tati[nod].doima = maxlo + maxnow;
tati[nod].doima = max(tati[nod].doima, x);
} else {
if (nod == 1) {
if (q == 2) {
tati[nod].doima = maxlo + maxnow;
tati[nod].doima = max(tati[nod].doima, x);
} else {
tati[nod].doima = tati[g[nod][0]].doima;
}
} else {
if (q == 2) {
if (tati[nod].ta == g[nod][0]) {
tati[nod].doima = tati[g[nod][1]].doima;
} else {
tati[nod].doima = tati[g[nod][0]].doima;
}
} else {
tati[nod].doima = (0 - (1LL << 60));
}
}
}
}
void dfs(int nod) {
for (int i = 0; i < g[nod].size(); i++) {
if (tati[g[nod][i]].ta == 0) {
tati[g[nod][i]].ta = nod;
dfs(g[nod][i]);
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%I64d", &val[i]);
}
for (int i = 1; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
g[a].push_back(b);
g[b].push_back(a);
}
if (n > 1) {
tati[1].ta = -1;
dfs(1);
setarb(1);
}
int gay;
gay = 0;
for (int i = 1; i <= n; i++) {
int q = g[i].size();
gay = max(gay, q);
}
if (n == 1 || (gay <= 2 && g[1].size() == 1)) {
printf("Impossible");
} else {
printf("%I64d", tati[1].doima);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const long long inf = 1LL << 62;
long long a[N], b[N];
vector<int> tree[N];
void dfs(int u, int f) {
b[u] = a[u];
for (int i = 0; i < (int)tree[u].size(); i++) {
int v = tree[u][i];
if (v == f) continue;
dfs(v, u);
b[u] += b[v];
}
}
void dfs(int u, int f, long long& x, long long& y, long long& z) {
x = -inf;
y = -inf;
z = b[u];
vector<pair<long long, long long> > vec;
for (int i = 0; i < (int)tree[u].size(); i++) {
int v = tree[u][i];
if (v == f) continue;
long long vx, vy, vz;
dfs(v, u, vx, vy, vz);
if (x < vz)
y = x, x = vz;
else
y = max(y, vz);
z = max(z, vz);
vec.push_back(pair<long long, long long>(vx, vy));
}
for (int i = 0; i < (int)vec.size(); i++) {
long long first = vec[i].first, second = vec[i].second;
if (first + second > x + y) x = first, y = second;
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) scanf("%I64d", a + i);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
tree[u].push_back(v);
tree[v].push_back(u);
}
dfs(1, 0);
long long res1, res2, res3;
dfs(1, 0, res1, res2, res3);
if (res2 == -inf)
puts("Impossible");
else
cout << res1 + res2 << endl;
return 0;
}
|
### Prompt
Create a solution in CPP for the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const long long inf = 1LL << 62;
long long a[N], b[N];
vector<int> tree[N];
void dfs(int u, int f) {
b[u] = a[u];
for (int i = 0; i < (int)tree[u].size(); i++) {
int v = tree[u][i];
if (v == f) continue;
dfs(v, u);
b[u] += b[v];
}
}
void dfs(int u, int f, long long& x, long long& y, long long& z) {
x = -inf;
y = -inf;
z = b[u];
vector<pair<long long, long long> > vec;
for (int i = 0; i < (int)tree[u].size(); i++) {
int v = tree[u][i];
if (v == f) continue;
long long vx, vy, vz;
dfs(v, u, vx, vy, vz);
if (x < vz)
y = x, x = vz;
else
y = max(y, vz);
z = max(z, vz);
vec.push_back(pair<long long, long long>(vx, vy));
}
for (int i = 0; i < (int)vec.size(); i++) {
long long first = vec[i].first, second = vec[i].second;
if (first + second > x + y) x = first, y = second;
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) scanf("%I64d", a + i);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
tree[u].push_back(v);
tree[v].push_back(u);
}
dfs(1, 0);
long long res1, res2, res3;
dfs(1, 0, res1, res2, res3);
if (res2 == -inf)
puts("Impossible");
else
cout << res1 + res2 << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 400021;
long long n, k;
vector<vector<int> > adj(200006);
vector<long long> a(200006);
vector<long long> tot(200006, 0);
long long ans = -1000000000000000;
vector<long long> ansv(200006, (-1000000000000000));
int flag = 0;
void dfs(int v, int p) {
int len = adj[v].size();
if (len > 2) flag = 1;
for (int i = 0; i < len; i++)
if (adj[v][i] != p) dfs(adj[v][i], v);
if (len == 1 && v != 1) {
tot[v] = a[v];
ansv[v] = a[v];
return;
}
vector<long long> vc;
tot[v] = a[v];
for (int i = 0; i < len; i++) {
if (adj[v][i] != p) {
tot[v] += tot[adj[v][i]];
vc.push_back(ansv[adj[v][i]]);
if (ansv[v] < ansv[adj[v][i]]) ansv[v] = ansv[adj[v][i]];
}
}
if (ansv[v] < tot[v]) ansv[v] = tot[v];
len = vc.size();
if (len < 2) return;
sort(vc.begin(), vc.end());
if (ans < vc[len - 1] + vc[len - 2]) ans = vc[len - 1] + vc[len - 2];
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int ui, vi;
cin >> ui >> vi;
adj[ui].push_back(vi);
adj[vi].push_back(ui);
}
if (adj[1].size() > 1) flag = 1;
dfs(1, 1);
if (!flag) {
cout << "Impossible\n";
return 0;
}
cout << ans << "\n";
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 400021;
long long n, k;
vector<vector<int> > adj(200006);
vector<long long> a(200006);
vector<long long> tot(200006, 0);
long long ans = -1000000000000000;
vector<long long> ansv(200006, (-1000000000000000));
int flag = 0;
void dfs(int v, int p) {
int len = adj[v].size();
if (len > 2) flag = 1;
for (int i = 0; i < len; i++)
if (adj[v][i] != p) dfs(adj[v][i], v);
if (len == 1 && v != 1) {
tot[v] = a[v];
ansv[v] = a[v];
return;
}
vector<long long> vc;
tot[v] = a[v];
for (int i = 0; i < len; i++) {
if (adj[v][i] != p) {
tot[v] += tot[adj[v][i]];
vc.push_back(ansv[adj[v][i]]);
if (ansv[v] < ansv[adj[v][i]]) ansv[v] = ansv[adj[v][i]];
}
}
if (ansv[v] < tot[v]) ansv[v] = tot[v];
len = vc.size();
if (len < 2) return;
sort(vc.begin(), vc.end());
if (ans < vc[len - 1] + vc[len - 2]) ans = vc[len - 1] + vc[len - 2];
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int ui, vi;
cin >> ui >> vi;
adj[ui].push_back(vi);
adj[vi].push_back(ui);
}
if (adj[1].size() > 1) flag = 1;
dfs(1, 1);
if (!flag) {
cout << "Impossible\n";
return 0;
}
cout << ans << "\n";
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const long long INF = 1e17;
long long sum[N], mx[N], ans = -INF, a[N];
vector<int> g[N];
void dfs(int v, int par) {
vector<long long> d;
for (auto u : g[v])
if (u != par) {
dfs(u, v);
sum[v] += sum[u];
d.push_back(mx[u]);
mx[v] = max(mx[v], mx[u]);
}
sum[v] += a[v];
mx[v] = max(mx[v], sum[v]);
sort(d.begin(), d.end(), greater<long long>());
if (int(d.size()) < 2) return;
ans = max(ans, d[0] + d[1]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
fill(mx, mx + N, -INF);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int v, u;
cin >> v >> u;
v--;
u--;
g[v].push_back(u);
g[u].push_back(v);
}
dfs(0, -1);
if (ans <= -INF)
cout << "Impossible\n";
else
cout << ans << "\n";
return 0;
}
|
### Prompt
In CPP, your task is to solve the following problem:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const long long INF = 1e17;
long long sum[N], mx[N], ans = -INF, a[N];
vector<int> g[N];
void dfs(int v, int par) {
vector<long long> d;
for (auto u : g[v])
if (u != par) {
dfs(u, v);
sum[v] += sum[u];
d.push_back(mx[u]);
mx[v] = max(mx[v], mx[u]);
}
sum[v] += a[v];
mx[v] = max(mx[v], sum[v]);
sort(d.begin(), d.end(), greater<long long>());
if (int(d.size()) < 2) return;
ans = max(ans, d[0] + d[1]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
fill(mx, mx + N, -INF);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int v, u;
cin >> v >> u;
v--;
u--;
g[v].push_back(u);
g[u].push_back(v);
}
dfs(0, -1);
if (ans <= -INF)
cout << "Impossible\n";
else
cout << ans << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = -1e18;
long long ans = INF;
int a[200100], p[200100];
vector<vector<int> > g(200100);
vector<long long> sum(200100, 0), mx(200100, 0);
void dfs(int v) {
sum[v] = a[v];
mx[v] = INF;
for (int i = 0; i < g[v].size(); ++i)
if (p[v] != g[v][i]) {
p[g[v][i]] = v;
dfs(g[v][i]);
sum[v] += sum[g[v][i]];
mx[v] = max(mx[v], mx[g[v][i]]);
}
mx[v] = max(mx[v], sum[v]);
}
void f(int v, long long sc = INF) {
if (sc != INF) ans = max(ans, mx[v] + sc);
vector<pair<long long, int> > x;
for (int i = 0; i < g[v].size(); ++i)
if (p[v] != g[v][i]) x.push_back({mx[g[v][i]], g[v][i]});
sort(x.rbegin(), x.rend());
if (x.size() == 1)
f(x[0].second, sc);
else if (x.size() > 1) {
f(x[0].second, max(sc, x[1].first));
f(x[1].second, max(sc, x[0].first));
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
int x, y;
for (int i = 0; i < n - 1; ++i) {
scanf("%d%d", &x, &y);
x--, y--;
g[x].push_back(y);
g[y].push_back(x);
}
p[0] = -1;
dfs(0);
f(0);
if (ans == INF)
cout << "Impossible";
else
cout << ans;
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long INF = -1e18;
long long ans = INF;
int a[200100], p[200100];
vector<vector<int> > g(200100);
vector<long long> sum(200100, 0), mx(200100, 0);
void dfs(int v) {
sum[v] = a[v];
mx[v] = INF;
for (int i = 0; i < g[v].size(); ++i)
if (p[v] != g[v][i]) {
p[g[v][i]] = v;
dfs(g[v][i]);
sum[v] += sum[g[v][i]];
mx[v] = max(mx[v], mx[g[v][i]]);
}
mx[v] = max(mx[v], sum[v]);
}
void f(int v, long long sc = INF) {
if (sc != INF) ans = max(ans, mx[v] + sc);
vector<pair<long long, int> > x;
for (int i = 0; i < g[v].size(); ++i)
if (p[v] != g[v][i]) x.push_back({mx[g[v][i]], g[v][i]});
sort(x.rbegin(), x.rend());
if (x.size() == 1)
f(x[0].second, sc);
else if (x.size() > 1) {
f(x[0].second, max(sc, x[1].first));
f(x[1].second, max(sc, x[0].first));
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
int x, y;
for (int i = 0; i < n - 1; ++i) {
scanf("%d%d", &x, &y);
x--, y--;
g[x].push_back(y);
g[y].push_back(x);
}
p[0] = -1;
dfs(0);
f(0);
if (ans == INF)
cout << "Impossible";
else
cout << ans;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = (2e5) + 10;
const long long INF = -(2e11);
vector<int> child[maxn];
int n;
int a[maxn];
bool vis[maxn];
long long d[maxn], d2[maxn], ans;
long long dfs(int cur) {
d[cur] = a[cur];
vis[cur] = 1;
for (int i = 0; i < child[cur].size(); i++) {
int son = child[cur][i];
if (vis[son] == 0) d[cur] += dfs(son);
}
return d[cur];
}
long long dfs2(int cur) {
vis[cur] = 1;
d2[cur] = d[cur];
for (int i = 0; i < child[cur].size(); i++) {
int son = child[cur][i];
if (vis[son] == 0) d2[cur] = max(d2[cur], dfs2(son));
}
return d2[cur];
}
void dfs3(int cur) {
vis[cur] = 1;
vector<long long> tmp;
for (int i = 0; i < child[cur].size(); i++) {
int son = child[cur][i];
if (vis[son] == 0) {
tmp.push_back(d2[son]);
dfs3(son);
}
}
int len = tmp.size();
if (len >= 2) {
sort(tmp.begin(), tmp.end());
if (ans < tmp[len - 1] + tmp[len - 2]) ans = tmp[len - 1] + tmp[len - 2];
}
}
int main() {
int u, v;
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
child[u].push_back(v);
child[v].push_back(u);
}
memset(vis, 0, sizeof(vis));
dfs(1);
memset(vis, 0, sizeof(vis));
dfs2(1);
ans = INF;
memset(vis, 0, sizeof(vis));
dfs3(1);
if (ans == INF)
cout << "Impossible" << endl;
else
cout << ans << endl;
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.
The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.
The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = (2e5) + 10;
const long long INF = -(2e11);
vector<int> child[maxn];
int n;
int a[maxn];
bool vis[maxn];
long long d[maxn], d2[maxn], ans;
long long dfs(int cur) {
d[cur] = a[cur];
vis[cur] = 1;
for (int i = 0; i < child[cur].size(); i++) {
int son = child[cur][i];
if (vis[son] == 0) d[cur] += dfs(son);
}
return d[cur];
}
long long dfs2(int cur) {
vis[cur] = 1;
d2[cur] = d[cur];
for (int i = 0; i < child[cur].size(); i++) {
int son = child[cur][i];
if (vis[son] == 0) d2[cur] = max(d2[cur], dfs2(son));
}
return d2[cur];
}
void dfs3(int cur) {
vis[cur] = 1;
vector<long long> tmp;
for (int i = 0; i < child[cur].size(); i++) {
int son = child[cur][i];
if (vis[son] == 0) {
tmp.push_back(d2[son]);
dfs3(son);
}
}
int len = tmp.size();
if (len >= 2) {
sort(tmp.begin(), tmp.end());
if (ans < tmp[len - 1] + tmp[len - 2]) ans = tmp[len - 1] + tmp[len - 2];
}
}
int main() {
int u, v;
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
child[u].push_back(v);
child[v].push_back(u);
}
memset(vis, 0, sizeof(vis));
dfs(1);
memset(vis, 0, sizeof(vis));
dfs2(1);
ans = INF;
memset(vis, 0, sizeof(vis));
dfs3(1);
if (ans == INF)
cout << "Impossible" << endl;
else
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int a[100005], vis[100005];
int main() {
int n;
scanf("%d", &n);
int t = n;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
vis[a[i]] = 1;
for (; vis[t] == 1; t--) printf("%d ", t);
printf("\n");
}
return 0;
}
|
### Prompt
In CPP, your task is to solve the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 β€ n β€ 100 000) β the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
Β
2 1
Input
5
4 5 1 2 3
Output
Β
5 4
Β
Β
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a[100005], vis[100005];
int main() {
int n;
scanf("%d", &n);
int t = n;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
vis[a[i]] = 1;
for (; vis[t] == 1; t--) printf("%d ", t);
printf("\n");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) res += ", ";
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cout << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cout << " " << to_string(H);
debug_out(T...);
}
string to_str(const int &n) {
ostringstream stm;
stm << n;
return stm.str();
}
string to_str(const long long &n) {
ostringstream stm;
stm << n;
return stm.str();
}
template <class T>
string tostring(T x, int len = 0) {
stringstream ss;
ss << x;
string r = ss.str();
if (int((r).size()) < len) r = string(len - int((r).size()), '0') + r;
return r;
}
template <class T>
void convert(string x, T &r) {
stringstream ss(x);
ss >> r;
}
long long powmod(long long a, long long b) {
long long res = 1;
a %= 1000000007;
for (; b; b >>= 1) {
if (b & 1) res = res * a % 1000000007;
a = a * a % 1000000007;
}
return res;
}
long long modinv(long long a) { return powmod(a, 1000000007 - 2); }
long long modinv(long long a, long long m) { return powmod(a, m - 2); }
long long nCkMp(long long n, long long k) {
long long numerator = 1, denominator = 1;
for (int i = 0; i < k; i++) numerator = (numerator * (n - i)) % 1000000007;
for (int i = 1; i <= k; i++) denominator = (denominator * i) % 1000000007;
return (numerator * modinv(denominator)) % 1000000007;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }
int TT = 1, tt;
clock_t t1;
void solve() {
t1 = clock();
int n, c, q;
(cin >> (n));
vector<bool> x(n + 1);
c = n;
for (auto i = (0); i < (n); i++) {
(cin >> (q));
x[q] = true;
if (q == c) {
while (x[q]) {
cout << q << " ";
q--;
c--;
};
}
(cout << ("") << endl);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
for (tt = 0; tt < TT; tt++) solve();
return 0;
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 β€ n β€ 100 000) β the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
Β
2 1
Input
5
4 5 1 2 3
Output
Β
5 4
Β
Β
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) res += ", ";
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cout << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cout << " " << to_string(H);
debug_out(T...);
}
string to_str(const int &n) {
ostringstream stm;
stm << n;
return stm.str();
}
string to_str(const long long &n) {
ostringstream stm;
stm << n;
return stm.str();
}
template <class T>
string tostring(T x, int len = 0) {
stringstream ss;
ss << x;
string r = ss.str();
if (int((r).size()) < len) r = string(len - int((r).size()), '0') + r;
return r;
}
template <class T>
void convert(string x, T &r) {
stringstream ss(x);
ss >> r;
}
long long powmod(long long a, long long b) {
long long res = 1;
a %= 1000000007;
for (; b; b >>= 1) {
if (b & 1) res = res * a % 1000000007;
a = a * a % 1000000007;
}
return res;
}
long long modinv(long long a) { return powmod(a, 1000000007 - 2); }
long long modinv(long long a, long long m) { return powmod(a, m - 2); }
long long nCkMp(long long n, long long k) {
long long numerator = 1, denominator = 1;
for (int i = 0; i < k; i++) numerator = (numerator * (n - i)) % 1000000007;
for (int i = 1; i <= k; i++) denominator = (denominator * i) % 1000000007;
return (numerator * modinv(denominator)) % 1000000007;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }
int TT = 1, tt;
clock_t t1;
void solve() {
t1 = clock();
int n, c, q;
(cin >> (n));
vector<bool> x(n + 1);
c = n;
for (auto i = (0); i < (n); i++) {
(cin >> (q));
x[q] = true;
if (q == c) {
while (x[q]) {
cout << q << " ";
q--;
c--;
};
}
(cout << ("") << endl);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
for (tt = 0; tt < TT; tt++) solve();
return 0;
}
```
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.