output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200 * 1000 + 10;
const long long INF = 1e15;
long long a[MAXN];
vector<int> adj[MAXN];
long long tot[MAXN], mx[MAXN];
void calc(int x, int par) {
tot[x] = a[x];
mx[x] = -INF;
for (int i = 0; i < ((int)(adj[x]).size()); i++) {
int v = adj[x][i];
if (v == par) continue;
calc(v, x);
tot[x] += tot[v];
mx[x] = max(mx[x], mx[v]);
}
mx[x] = max(mx[x], tot[x]);
}
long long ans[MAXN];
void dfs(int x, int par, long long val) {
if (val != -INF) ans[x] = tot[x] + val;
set<pair<long long, long long> > s;
for (int i = 0; i < ((int)(adj[x]).size()); i++) {
int v = adj[x][i];
if (v == par) continue;
s.insert(make_pair(mx[v], v));
}
for (int i = 0; i < ((int)(adj[x]).size()); i++) {
int v = adj[x][i];
if (v == par) continue;
s.erase(make_pair(mx[v], v));
if (((int)(s).size())) {
pair<long long, long long> tmp = *s.rbegin();
dfs(v, x, max(val, tmp.first));
} else {
dfs(v, x, val);
}
s.insert(make_pair(mx[v], v));
}
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < (n); i++) cin >> a[i];
for (int i = 0; i < (n - 1); i++) {
int x, y;
cin >> x >> y;
x--, y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
calc(0, -1);
for (int i = 0; i < (n); i++) ans[i] = -INF;
dfs(0, -1, -INF);
long long res = -INF;
for (int i = 0; i < (n); i++) res = max(res, ans[i]);
if (res == -INF)
cout << "Impossible" << endl;
else
cout << res << endl;
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;
const int MAXN = 200 * 1000 + 10;
const long long INF = 1e15;
long long a[MAXN];
vector<int> adj[MAXN];
long long tot[MAXN], mx[MAXN];
void calc(int x, int par) {
tot[x] = a[x];
mx[x] = -INF;
for (int i = 0; i < ((int)(adj[x]).size()); i++) {
int v = adj[x][i];
if (v == par) continue;
calc(v, x);
tot[x] += tot[v];
mx[x] = max(mx[x], mx[v]);
}
mx[x] = max(mx[x], tot[x]);
}
long long ans[MAXN];
void dfs(int x, int par, long long val) {
if (val != -INF) ans[x] = tot[x] + val;
set<pair<long long, long long> > s;
for (int i = 0; i < ((int)(adj[x]).size()); i++) {
int v = adj[x][i];
if (v == par) continue;
s.insert(make_pair(mx[v], v));
}
for (int i = 0; i < ((int)(adj[x]).size()); i++) {
int v = adj[x][i];
if (v == par) continue;
s.erase(make_pair(mx[v], v));
if (((int)(s).size())) {
pair<long long, long long> tmp = *s.rbegin();
dfs(v, x, max(val, tmp.first));
} else {
dfs(v, x, val);
}
s.insert(make_pair(mx[v], v));
}
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < (n); i++) cin >> a[i];
for (int i = 0; i < (n - 1); i++) {
int x, y;
cin >> x >> y;
x--, y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
calc(0, -1);
for (int i = 0; i < (n); i++) ans[i] = -INF;
dfs(0, -1, -INF);
long long res = -INF;
for (int i = 0; i < (n); i++) res = max(res, ans[i]);
if (res == -INF)
cout << "Impossible" << endl;
else
cout << res << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> g[200000];
long long int T[200000], sum[200000], maxtree[200000];
long long int ans(int i = 0, int p = -1) {
long long int t = -1e18;
vector<long long int> subtreew;
subtreew.clear();
for (int v : g[i]) {
if (v != p) {
t = max(ans(v, i), t);
subtreew.push_back(maxtree[v]);
}
}
if (subtreew.size() < 2) return t;
sort(subtreew.begin(), subtreew.end(),
[=](long long int a, long long int b) { return a > b; });
return max(subtreew[0] + subtreew[1], t);
}
void dfs(int i = 0, int p = -1) {
sum[i] = T[i], maxtree[i] = -1e18;
for (int v : g[i]) {
if (v != p) {
dfs(v, i);
sum[i] += sum[v];
maxtree[i] = max(maxtree[i], maxtree[v]);
}
}
maxtree[i] = max(maxtree[i], sum[i]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, u, v;
cin >> n;
for (int i = 0; i < n; ++i) cin >> T[i];
for (int i = 0; i < n - 1; ++i) {
cin >> u >> v;
--u, --v;
g[u].push_back(v), g[v].push_back(u);
}
dfs();
long long int a = ans();
if (a < -1e15)
cout << "Impossible";
else
cout << a;
}
|
### 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[200000];
long long int T[200000], sum[200000], maxtree[200000];
long long int ans(int i = 0, int p = -1) {
long long int t = -1e18;
vector<long long int> subtreew;
subtreew.clear();
for (int v : g[i]) {
if (v != p) {
t = max(ans(v, i), t);
subtreew.push_back(maxtree[v]);
}
}
if (subtreew.size() < 2) return t;
sort(subtreew.begin(), subtreew.end(),
[=](long long int a, long long int b) { return a > b; });
return max(subtreew[0] + subtreew[1], t);
}
void dfs(int i = 0, int p = -1) {
sum[i] = T[i], maxtree[i] = -1e18;
for (int v : g[i]) {
if (v != p) {
dfs(v, i);
sum[i] += sum[v];
maxtree[i] = max(maxtree[i], maxtree[v]);
}
}
maxtree[i] = max(maxtree[i], sum[i]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, u, v;
cin >> n;
for (int i = 0; i < n; ++i) cin >> T[i];
for (int i = 0; i < n - 1; ++i) {
cin >> u >> v;
--u, --v;
g[u].push_back(v), g[v].push_back(u);
}
dfs();
long long int a = ans();
if (a < -1e15)
cout << "Impossible";
else
cout << a;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int vis[200002];
long long a[200002], dp[200002], ans = -1e18;
;
vector<vector<int> > adj(200002);
long long dfs(int v) {
if (vis[v] == 1) {
return dp[v];
}
vis[v] = 1;
long long s = 0;
for (int i = 0; i < adj[v].size(); i++) {
if (vis[adj[v][i]] == 0) {
dp[adj[v][i]] = dfs(adj[v][i]);
s += dp[adj[v][i]];
}
}
return a[v] + s;
}
long long dfs2(int v) {
if (vis[v] == 1) {
return dp[v];
}
vis[v] = 1;
vector<long long> x;
for (int i = 0; i < adj[v].size(); i++) {
if (vis[adj[v][i]] == 0) {
x.push_back(dfs2(adj[v][i]));
}
}
sort(x.begin(), x.end());
reverse(x.begin(), x.end());
if (x.size() >= 2) {
ans = max(ans, x[0] + x[1]);
return max(x[0], max(dp[v], x[1]));
} else if (x.size() == 1) {
return max(dp[v], x[0]);
} else {
return dp[v];
}
}
int main() {
long long n, i, u, v;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
for (i = 1; i < n; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dp[1] = dfs(1);
memset(vis, 0, sizeof vis);
dfs2(1);
if (ans == -1e18)
cout << "Impossible";
else
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;
int vis[200002];
long long a[200002], dp[200002], ans = -1e18;
;
vector<vector<int> > adj(200002);
long long dfs(int v) {
if (vis[v] == 1) {
return dp[v];
}
vis[v] = 1;
long long s = 0;
for (int i = 0; i < adj[v].size(); i++) {
if (vis[adj[v][i]] == 0) {
dp[adj[v][i]] = dfs(adj[v][i]);
s += dp[adj[v][i]];
}
}
return a[v] + s;
}
long long dfs2(int v) {
if (vis[v] == 1) {
return dp[v];
}
vis[v] = 1;
vector<long long> x;
for (int i = 0; i < adj[v].size(); i++) {
if (vis[adj[v][i]] == 0) {
x.push_back(dfs2(adj[v][i]));
}
}
sort(x.begin(), x.end());
reverse(x.begin(), x.end());
if (x.size() >= 2) {
ans = max(ans, x[0] + x[1]);
return max(x[0], max(dp[v], x[1]));
} else if (x.size() == 1) {
return max(dp[v], x[0]);
} else {
return dp[v];
}
}
int main() {
long long n, i, u, v;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
for (i = 1; i < n; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dp[1] = dfs(1);
memset(vis, 0, sizeof vis);
dfs2(1);
if (ans == -1e18)
cout << "Impossible";
else
cout << ans;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 5;
struct tree {
int v, to;
} p[2 * maxn];
int t[maxn];
int tot;
long long a[maxn];
long long sons[maxn], dp[maxn];
long long ans;
void add(int v, int u) {
p[tot].v = v;
p[tot].to = t[u];
t[u] = tot++;
}
void dfs(int fa, int v) {
sons[v] = a[v];
int cnt = 0;
long long maxs = -1e15, sec = -1e15;
for (int i = t[v]; i != -1; i = p[i].to) {
int u = p[i].v;
if (u == fa) continue;
dfs(v, u);
cnt++;
sons[v] += sons[u];
if (cnt == 1)
maxs = dp[u];
else {
if (maxs < dp[u]) {
sec = maxs;
maxs = dp[u];
} else if (sec < dp[u]) {
sec = dp[u];
}
}
}
if (cnt > 1) ans = max(ans, maxs + sec);
dp[v] = max(maxs, sons[v]);
}
int main() {
int n;
scanf("%d", &n);
tot = 0;
memset(t, -1, sizeof(t));
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);
add(x, y);
add(y, x);
}
ans = -1e15;
dfs(1, 1);
if (ans == -1e15)
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;
const int maxn = 200000 + 5;
struct tree {
int v, to;
} p[2 * maxn];
int t[maxn];
int tot;
long long a[maxn];
long long sons[maxn], dp[maxn];
long long ans;
void add(int v, int u) {
p[tot].v = v;
p[tot].to = t[u];
t[u] = tot++;
}
void dfs(int fa, int v) {
sons[v] = a[v];
int cnt = 0;
long long maxs = -1e15, sec = -1e15;
for (int i = t[v]; i != -1; i = p[i].to) {
int u = p[i].v;
if (u == fa) continue;
dfs(v, u);
cnt++;
sons[v] += sons[u];
if (cnt == 1)
maxs = dp[u];
else {
if (maxs < dp[u]) {
sec = maxs;
maxs = dp[u];
} else if (sec < dp[u]) {
sec = dp[u];
}
}
}
if (cnt > 1) ans = max(ans, maxs + sec);
dp[v] = max(maxs, sons[v]);
}
int main() {
int n;
scanf("%d", &n);
tot = 0;
memset(t, -1, sizeof(t));
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);
add(x, y);
add(y, x);
}
ans = -1e15;
dfs(1, 1);
if (ans == -1e15)
puts("Impossible");
else
printf("%I64d\n", ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<long long>> g;
vector<long long> val;
vector<long long> d;
vector<pair<long long, long long>> dp;
vector<long long> dpd;
void dfs(long long v, long long pred) {
long long ans = 0;
for (int i = 0; i < g[v].size(); ++i) {
if (g[v][i] == pred) continue;
if (d[g[v][i]] == -1e18) dfs(g[v][i], v);
ans += d[g[v][i]];
}
d[v] = ans + val[v];
}
void dfs2(long long v, long long pred, int isroot) {
vector<pair<long long, long long>> arr;
vector<long long> al;
for (int i = 0; i < g[v].size(); ++i) {
if (g[v][i] == pred) continue;
if (dp[g[v][i]] == make_pair((long long)-1e18, (long long)-1e18)) {
dfs2(g[v][i], v, 0);
arr.push_back(dp[g[v][i]]);
al.push_back(dpd[g[v][i]]);
}
}
if (arr.size() == 0) {
dp[v] = {-1e18, -1e18};
dpd[v] = d[v];
return;
}
sort(al.rbegin(), al.rend());
long long maxx3 = -1e18, p3, maxx1 = -1e18, maxx2 = -1e18;
if (al.size() >= 1) maxx1 = al[0];
if (al.size() >= 2) maxx2 = al[1];
for (int i = 0; i < arr.size(); ++i) {
if (arr[i].first + arr[i].second > maxx3 && arr[i].first != -1e18 &&
arr[i].second != 1e18) {
maxx3 = arr[i].first + arr[i].second;
p3 = i;
}
}
dpd[v] = max(maxx1, max(maxx2, d[v]));
if (maxx1 != -1e18 && maxx2 != -1e18 && maxx3 < maxx1 + maxx2) {
maxx3 = maxx1 + maxx2;
dp[v] = {maxx1, maxx2};
} else if (maxx3 != -1e18) {
dp[v] = {arr[p3].first, arr[p3].second};
} else {
dp[v] = {-1e18, -1e18};
}
}
int main() {
long long n;
cin >> n;
val.resize(n);
d.resize(n, -1e18);
dp.resize(n, {-1e18, -1e18});
g.resize(n);
dpd.resize(n);
for (int i = 0; i < n; ++i) cin >> val[i];
long long u, v;
for (int i = 1; i <= n - 1; ++i) {
cin >> u >> v;
--u;
--v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(0, 0);
dfs2(0, 0, 1);
pair<long long, long long> ans = dp[0];
if (ans.first == -1e18 || ans.second == -1e18) {
cout << "Impossible" << endl;
} else {
cout << ans.first + ans.second << endl;
}
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<vector<long long>> g;
vector<long long> val;
vector<long long> d;
vector<pair<long long, long long>> dp;
vector<long long> dpd;
void dfs(long long v, long long pred) {
long long ans = 0;
for (int i = 0; i < g[v].size(); ++i) {
if (g[v][i] == pred) continue;
if (d[g[v][i]] == -1e18) dfs(g[v][i], v);
ans += d[g[v][i]];
}
d[v] = ans + val[v];
}
void dfs2(long long v, long long pred, int isroot) {
vector<pair<long long, long long>> arr;
vector<long long> al;
for (int i = 0; i < g[v].size(); ++i) {
if (g[v][i] == pred) continue;
if (dp[g[v][i]] == make_pair((long long)-1e18, (long long)-1e18)) {
dfs2(g[v][i], v, 0);
arr.push_back(dp[g[v][i]]);
al.push_back(dpd[g[v][i]]);
}
}
if (arr.size() == 0) {
dp[v] = {-1e18, -1e18};
dpd[v] = d[v];
return;
}
sort(al.rbegin(), al.rend());
long long maxx3 = -1e18, p3, maxx1 = -1e18, maxx2 = -1e18;
if (al.size() >= 1) maxx1 = al[0];
if (al.size() >= 2) maxx2 = al[1];
for (int i = 0; i < arr.size(); ++i) {
if (arr[i].first + arr[i].second > maxx3 && arr[i].first != -1e18 &&
arr[i].second != 1e18) {
maxx3 = arr[i].first + arr[i].second;
p3 = i;
}
}
dpd[v] = max(maxx1, max(maxx2, d[v]));
if (maxx1 != -1e18 && maxx2 != -1e18 && maxx3 < maxx1 + maxx2) {
maxx3 = maxx1 + maxx2;
dp[v] = {maxx1, maxx2};
} else if (maxx3 != -1e18) {
dp[v] = {arr[p3].first, arr[p3].second};
} else {
dp[v] = {-1e18, -1e18};
}
}
int main() {
long long n;
cin >> n;
val.resize(n);
d.resize(n, -1e18);
dp.resize(n, {-1e18, -1e18});
g.resize(n);
dpd.resize(n);
for (int i = 0; i < n; ++i) cin >> val[i];
long long u, v;
for (int i = 1; i <= n - 1; ++i) {
cin >> u >> v;
--u;
--v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(0, 0);
dfs2(0, 0, 1);
pair<long long, long long> ans = dp[0];
if (ans.first == -1e18 || ans.second == -1e18) {
cout << "Impossible" << endl;
} else {
cout << ans.first + ans.second << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long n, a[200005];
vector<long long> edge[200005];
long long bestOne[200005];
long long cutVal[200005];
bool impossible = true;
long long res = -1e9;
bool seen[200005];
void solve(long long ind, long long parent) {
if (edge[ind].empty()) return;
if (seen[ind]) return;
seen[ind] = true;
if (parent != -1 && edge[ind].size() == 1) {
bestOne[ind] = a[ind];
cutVal[ind] = a[ind];
return;
}
vector<long long> twoBest;
for (int i = 0; i < edge[ind].size(); i++) {
if (edge[ind][i] != parent) {
solve(edge[ind][i], ind);
twoBest.push_back(bestOne[edge[ind][i]]);
cutVal[ind] += cutVal[edge[ind][i]];
}
}
sort(twoBest.begin(), twoBest.end(), greater<long long>());
cutVal[ind] += a[ind];
bestOne[ind] = max(twoBest[0], cutVal[ind]);
if (twoBest.size() >= 2) {
long long val = twoBest[0] + twoBest[1];
if (impossible) {
impossible = false;
res = val;
} else {
res = max(res, val);
}
}
}
int main() {
fill(bestOne, bestOne + 200005, -1e16);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
edge[x].push_back(y);
edge[y].push_back(x);
}
solve(1, -1);
if (impossible) {
cout << "Impossible" << endl;
} else {
cout << res << 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;
long long n, a[200005];
vector<long long> edge[200005];
long long bestOne[200005];
long long cutVal[200005];
bool impossible = true;
long long res = -1e9;
bool seen[200005];
void solve(long long ind, long long parent) {
if (edge[ind].empty()) return;
if (seen[ind]) return;
seen[ind] = true;
if (parent != -1 && edge[ind].size() == 1) {
bestOne[ind] = a[ind];
cutVal[ind] = a[ind];
return;
}
vector<long long> twoBest;
for (int i = 0; i < edge[ind].size(); i++) {
if (edge[ind][i] != parent) {
solve(edge[ind][i], ind);
twoBest.push_back(bestOne[edge[ind][i]]);
cutVal[ind] += cutVal[edge[ind][i]];
}
}
sort(twoBest.begin(), twoBest.end(), greater<long long>());
cutVal[ind] += a[ind];
bestOne[ind] = max(twoBest[0], cutVal[ind]);
if (twoBest.size() >= 2) {
long long val = twoBest[0] + twoBest[1];
if (impossible) {
impossible = false;
res = val;
} else {
res = max(res, val);
}
}
}
int main() {
fill(bestOne, bestOne + 200005, -1e16);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
edge[x].push_back(y);
edge[y].push_back(x);
}
solve(1, -1);
if (impossible) {
cout << "Impossible" << endl;
} else {
cout << res << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 4e5;
int m, n, all, last[MaxN + 5], pre[MaxN + 5], other[MaxN + 5];
int flag, cnt;
long long f[MaxN + 5], a[MaxN + 5], W, sum1[MaxN + 5], sum2[MaxN + 5];
void Build(int x, int y) {
pre[++all] = last[x];
last[x] = all;
other[all] = y;
}
void Dfs(int x, int y) {
int ed, dr;
ed = last[x];
f[x] = a[x];
while (ed != -1) {
dr = other[ed];
if (dr != y) {
Dfs(dr, x);
f[x] += f[dr];
}
ed = pre[ed];
}
}
void dfs(int x, int y) {
int ed, dr;
ed = last[x];
sum1[x] = f[x];
while (ed != -1) {
dr = other[ed];
if (dr != y) {
dfs(dr, x);
sum1[x] = max(sum1[x], sum1[dr]);
}
ed = pre[ed];
}
}
void dfs2(int x, int y) {
int ed, dr;
ed = last[x];
long long Max1 = -(1LL << 60);
int tot = 0;
while (ed != -1) {
dr = other[ed];
if (dr != y) dfs2(dr, x), Max1 = max(Max1, sum1[dr]);
ed = pre[ed];
}
ed = last[x];
long long Max2 = -(1LL << 60);
while (ed != -1) {
dr = other[ed];
if (dr != y) {
if (Max1 == sum1[dr])
tot++;
else
Max2 = max(Max2, sum1[dr]);
}
ed = pre[ed];
}
if (tot > 1)
W = max(W, 2 * Max1);
else if (Max2 != -(1LL << 60))
W = max(W, Max1 + Max2);
}
void pdo() {
cnt = 0;
flag = 0;
W = -(1LL << 60);
Dfs(1, 0);
dfs(1, 0);
dfs2(1, 0);
if (W == -(1LL << 60))
printf("Impossible\n");
else
printf("%I64d\n", W);
}
int main() {
all = -1;
memset(last, -1, sizeof(last));
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
for (int i = 1; i <= n - 1; i++) {
int u, v;
scanf("%d %d", &u, &v);
Build(u, v);
Build(v, u);
}
pdo();
}
|
### 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 MaxN = 4e5;
int m, n, all, last[MaxN + 5], pre[MaxN + 5], other[MaxN + 5];
int flag, cnt;
long long f[MaxN + 5], a[MaxN + 5], W, sum1[MaxN + 5], sum2[MaxN + 5];
void Build(int x, int y) {
pre[++all] = last[x];
last[x] = all;
other[all] = y;
}
void Dfs(int x, int y) {
int ed, dr;
ed = last[x];
f[x] = a[x];
while (ed != -1) {
dr = other[ed];
if (dr != y) {
Dfs(dr, x);
f[x] += f[dr];
}
ed = pre[ed];
}
}
void dfs(int x, int y) {
int ed, dr;
ed = last[x];
sum1[x] = f[x];
while (ed != -1) {
dr = other[ed];
if (dr != y) {
dfs(dr, x);
sum1[x] = max(sum1[x], sum1[dr]);
}
ed = pre[ed];
}
}
void dfs2(int x, int y) {
int ed, dr;
ed = last[x];
long long Max1 = -(1LL << 60);
int tot = 0;
while (ed != -1) {
dr = other[ed];
if (dr != y) dfs2(dr, x), Max1 = max(Max1, sum1[dr]);
ed = pre[ed];
}
ed = last[x];
long long Max2 = -(1LL << 60);
while (ed != -1) {
dr = other[ed];
if (dr != y) {
if (Max1 == sum1[dr])
tot++;
else
Max2 = max(Max2, sum1[dr]);
}
ed = pre[ed];
}
if (tot > 1)
W = max(W, 2 * Max1);
else if (Max2 != -(1LL << 60))
W = max(W, Max1 + Max2);
}
void pdo() {
cnt = 0;
flag = 0;
W = -(1LL << 60);
Dfs(1, 0);
dfs(1, 0);
dfs2(1, 0);
if (W == -(1LL << 60))
printf("Impossible\n");
else
printf("%I64d\n", W);
}
int main() {
all = -1;
memset(last, -1, sizeof(last));
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
for (int i = 1; i <= n - 1; i++) {
int u, v;
scanf("%d %d", &u, &v);
Build(u, v);
Build(v, u);
}
pdo();
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long n, m, x, y, z, flag = 0;
vector<long long> sum(200005), mx(200005, INT_MIN);
vector<int> adj[200005 + 1];
vector<int> pl(200005 + 1);
map<int, int> vis;
void dfs(int a) {
vis[a] = 1;
for (int i = 0; i < adj[a].size(); i++) {
if (vis[adj[a][i]] == 0) {
dfs(adj[a][i]);
sum[a] += sum[adj[a][i]];
mx[a] = max(mx[a], mx[adj[a][i]]);
}
}
sum[a] += pl[a];
mx[a] = max(mx[a], sum[a]);
return;
}
map<int, int> mp;
long long ans = INT_MIN;
void dfs2(int x) {
mp[x] = 1;
long long k = 0;
priority_queue<long long> q;
for (int i = 0; i < adj[x].size(); i++) {
if (mp[adj[x][i]] == 0) q.push(mx[adj[x][i]]);
}
long long s = q.size();
int j = 2;
while (j > 0 && q.size()) {
k += q.top();
q.pop();
j--;
}
if (s > 1) ans = max(ans, k);
for (int i = 0; i < adj[x].size(); i++) {
if (mp[adj[x][i]] == 0) dfs2(adj[x][i]);
}
}
int main() {
int t;
t = 1;
for (int r = 0; r < t; r++) {
cin >> n;
long long i;
for (i = 1; i <= n; i++) cin >> pl[i];
for (i = 0; i < n - 1; i++) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1);
dfs2(1);
if (ans == INT_MIN)
cout << "Impossible\n";
else
cout << ans;
}
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 n, m, x, y, z, flag = 0;
vector<long long> sum(200005), mx(200005, INT_MIN);
vector<int> adj[200005 + 1];
vector<int> pl(200005 + 1);
map<int, int> vis;
void dfs(int a) {
vis[a] = 1;
for (int i = 0; i < adj[a].size(); i++) {
if (vis[adj[a][i]] == 0) {
dfs(adj[a][i]);
sum[a] += sum[adj[a][i]];
mx[a] = max(mx[a], mx[adj[a][i]]);
}
}
sum[a] += pl[a];
mx[a] = max(mx[a], sum[a]);
return;
}
map<int, int> mp;
long long ans = INT_MIN;
void dfs2(int x) {
mp[x] = 1;
long long k = 0;
priority_queue<long long> q;
for (int i = 0; i < adj[x].size(); i++) {
if (mp[adj[x][i]] == 0) q.push(mx[adj[x][i]]);
}
long long s = q.size();
int j = 2;
while (j > 0 && q.size()) {
k += q.top();
q.pop();
j--;
}
if (s > 1) ans = max(ans, k);
for (int i = 0; i < adj[x].size(); i++) {
if (mp[adj[x][i]] == 0) dfs2(adj[x][i]);
}
}
int main() {
int t;
t = 1;
for (int r = 0; r < t; r++) {
cin >> n;
long long i;
for (i = 1; i <= n; i++) cin >> pl[i];
for (i = 0; i < n - 1; i++) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1);
dfs2(1);
if (ans == INT_MIN)
cout << "Impossible\n";
else
cout << ans;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 200;
vector<int> g[maxn];
long long val[maxn];
long long sum[maxn];
long long ma[maxn];
long long ans = -1e18;
int n, dep;
void dfs0(int u, int fa) {
dep++;
if (dep == n) {
n = -1;
return;
}
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v == fa) continue;
dfs0(v, u);
dep--;
}
}
void dfs1(int u, int fa) {
int x = (int)g[u].size();
sum[u] = val[u];
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v == fa) continue;
dfs1(v, u);
sum[u] += sum[v];
ma[u] = max(ma[u], ma[v]);
}
ma[u] = max(ma[u], sum[u]);
if (x == 1) return;
long long ans1 = -1e17, ans2 = -1e17;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (ma[v] <= ans2)
continue;
else if (ma[v] >= ans1) {
ans2 = ans1;
ans1 = ma[v];
} else
ans2 = ma[v];
}
ans = max(ans, ans1 + ans2);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%I64d", &val[i]);
for (int i = 1; i <= n; i++) ma[i] = -1e18;
int u, v;
for (int i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs0(1, -1);
if (n == -1) {
printf("Impossible\n");
return 0;
}
dfs1(1, -1);
printf("%I64d\n", 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 int maxn = 2e5 + 200;
vector<int> g[maxn];
long long val[maxn];
long long sum[maxn];
long long ma[maxn];
long long ans = -1e18;
int n, dep;
void dfs0(int u, int fa) {
dep++;
if (dep == n) {
n = -1;
return;
}
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v == fa) continue;
dfs0(v, u);
dep--;
}
}
void dfs1(int u, int fa) {
int x = (int)g[u].size();
sum[u] = val[u];
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v == fa) continue;
dfs1(v, u);
sum[u] += sum[v];
ma[u] = max(ma[u], ma[v]);
}
ma[u] = max(ma[u], sum[u]);
if (x == 1) return;
long long ans1 = -1e17, ans2 = -1e17;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (ma[v] <= ans2)
continue;
else if (ma[v] >= ans1) {
ans2 = ans1;
ans1 = ma[v];
} else
ans2 = ma[v];
}
ans = max(ans, ans1 + ans2);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%I64d", &val[i]);
for (int i = 1; i <= n; i++) ma[i] = -1e18;
int u, v;
for (int i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs0(1, -1);
if (n == -1) {
printf("Impossible\n");
return 0;
}
dfs1(1, -1);
printf("%I64d\n", ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200001;
const long long inf = 1e12;
vector<int> adj[N];
long long val[N];
long long t[N];
long long max_d[N];
long long ans = -inf;
void dfs(int u, int parent) {
t[u] += val[u];
for (auto v : adj[u]) {
if (v != parent) {
dfs(v, u);
t[u] += t[v];
if (max_d[u] != -inf) ans = max(ans, max_d[u] + max_d[v]);
max_d[u] = max(max_d[u], max_d[v]);
}
}
max_d[u] = max(max_d[u], t[u]);
}
int main() {
int n, v;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> val[i];
}
int a, b;
for (int i = 1; i <= n - 1; i++) {
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
fill_n(max_d, N, -inf);
dfs(1, 0);
if (ans != -inf)
cout << ans << "\n";
else
cout << "Impossible"
<< "\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 int N = 200001;
const long long inf = 1e12;
vector<int> adj[N];
long long val[N];
long long t[N];
long long max_d[N];
long long ans = -inf;
void dfs(int u, int parent) {
t[u] += val[u];
for (auto v : adj[u]) {
if (v != parent) {
dfs(v, u);
t[u] += t[v];
if (max_d[u] != -inf) ans = max(ans, max_d[u] + max_d[v]);
max_d[u] = max(max_d[u], max_d[v]);
}
}
max_d[u] = max(max_d[u], t[u]);
}
int main() {
int n, v;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> val[i];
}
int a, b;
for (int i = 1; i <= n - 1; i++) {
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
fill_n(max_d, N, -inf);
dfs(1, 0);
if (ans != -inf)
cout << ans << "\n";
else
cout << "Impossible"
<< "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
int n;
long long ans = LLONG_MIN;
long long sum[(200010)], mxx[(200010)];
std::vector<int> mat[(200010)];
void dfs(int u, int fa) {
long long nsum = 0, nmxx = LLONG_MIN;
for (auto v : mat[u]) {
if (v == fa) continue;
dfs(v, u);
if (nmxx != LLONG_MIN) {
ans = std::max(ans, nmxx + mxx[v]);
}
nsum += sum[v];
nmxx = std::max(nmxx, mxx[v]);
}
sum[u] += nsum;
mxx[u] = std::max(sum[u], nmxx);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%I64d", sum + i);
}
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d%d", &u, &v);
mat[u].push_back(v);
mat[v].push_back(u);
}
dfs(1, 0);
if (ans == LLONG_MIN) {
printf("Impossible\n");
return 0;
}
std::cout << ans << std::endl;
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>
int n;
long long ans = LLONG_MIN;
long long sum[(200010)], mxx[(200010)];
std::vector<int> mat[(200010)];
void dfs(int u, int fa) {
long long nsum = 0, nmxx = LLONG_MIN;
for (auto v : mat[u]) {
if (v == fa) continue;
dfs(v, u);
if (nmxx != LLONG_MIN) {
ans = std::max(ans, nmxx + mxx[v]);
}
nsum += sum[v];
nmxx = std::max(nmxx, mxx[v]);
}
sum[u] += nsum;
mxx[u] = std::max(sum[u], nmxx);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%I64d", sum + i);
}
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d%d", &u, &v);
mat[u].push_back(v);
mat[v].push_back(u);
}
dfs(1, 0);
if (ans == LLONG_MIN) {
printf("Impossible\n");
return 0;
}
std::cout << ans << std::endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 0x3f3f3f3f3f3f3f3f;
long long dp[200005], val[200005];
const int maxn = 400005;
int tot;
int n;
vector<int> edge[maxn];
long long ans;
void dfs(int u, int fa) {
for (int i = 0; i < edge[u].size(); i++) {
int v = edge[u][i];
if (v == fa) continue;
dfs(v, u);
val[u] += val[v];
if (dp[u] != -inf) ans = max(ans, dp[v] + dp[u]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], val[u]);
}
int main() {
while (~scanf("%d", &n)) {
ans = -inf;
for (int i = 0; i <= n; i++) {
edge[i].clear();
dp[i] = -inf;
}
for (int i = 1; i <= n; i++) {
scanf("%I64d", &val[i]);
}
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
edge[u].push_back(v);
edge[v].push_back(u);
}
dfs(1, -1);
if (ans != -inf)
printf("%I64d\n", ans);
else
printf("Impossible\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 = 0x3f3f3f3f3f3f3f3f;
long long dp[200005], val[200005];
const int maxn = 400005;
int tot;
int n;
vector<int> edge[maxn];
long long ans;
void dfs(int u, int fa) {
for (int i = 0; i < edge[u].size(); i++) {
int v = edge[u][i];
if (v == fa) continue;
dfs(v, u);
val[u] += val[v];
if (dp[u] != -inf) ans = max(ans, dp[v] + dp[u]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], val[u]);
}
int main() {
while (~scanf("%d", &n)) {
ans = -inf;
for (int i = 0; i <= n; i++) {
edge[i].clear();
dp[i] = -inf;
}
for (int i = 1; i <= n; i++) {
scanf("%I64d", &val[i]);
}
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
edge[u].push_back(v);
edge[v].push_back(u);
}
dfs(1, -1);
if (ans != -inf)
printf("%I64d\n", ans);
else
printf("Impossible\n");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200009;
const long long int ninf = -1e17;
int n, dfstime[maxn], until[maxn], weight[maxn];
long long int sumweight[maxn], st[maxn];
vector<vector<int> > g;
void fillsumw(int u, int par, int& curtime) {
sumweight[u] = weight[u];
dfstime[u] = curtime;
++curtime;
for (int adj : g[u]) {
if (adj == par) continue;
fillsumw(adj, u, curtime);
sumweight[u] += sumweight[adj];
}
until[u] = curtime;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
scanf("%d", &n);
g = vector<vector<int> >(n, vector<int>());
for (int i = 0; i < n; ++i) scanf("%d", &weight[i]);
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d %d", &u, &v);
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
int curtime = 0;
fillsumw(0, -1, curtime);
for (int i = 0; i < n; ++i) {
st[dfstime[i]] = sumweight[i];
}
for (int i = n - 2; i > 0; --i) st[i] = max(st[i], st[i + 1]);
long long int ans = ninf;
for (int i = 0; i < n; ++i) {
int start = until[i];
if (start >= n) continue;
ans = max(ans, sumweight[i] + st[start]);
}
if (ans == ninf) {
cout << "Impossible" << endl;
} else {
cout << ans << endl;
}
}
|
### 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 maxn = 200009;
const long long int ninf = -1e17;
int n, dfstime[maxn], until[maxn], weight[maxn];
long long int sumweight[maxn], st[maxn];
vector<vector<int> > g;
void fillsumw(int u, int par, int& curtime) {
sumweight[u] = weight[u];
dfstime[u] = curtime;
++curtime;
for (int adj : g[u]) {
if (adj == par) continue;
fillsumw(adj, u, curtime);
sumweight[u] += sumweight[adj];
}
until[u] = curtime;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
scanf("%d", &n);
g = vector<vector<int> >(n, vector<int>());
for (int i = 0; i < n; ++i) scanf("%d", &weight[i]);
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d %d", &u, &v);
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
int curtime = 0;
fillsumw(0, -1, curtime);
for (int i = 0; i < n; ++i) {
st[dfstime[i]] = sumweight[i];
}
for (int i = n - 2; i > 0; --i) st[i] = max(st[i], st[i + 1]);
long long int ans = ninf;
for (int i = 0; i < n; ++i) {
int start = until[i];
if (start >= n) continue;
ans = max(ans, sumweight[i] + st[start]);
}
if (ans == ninf) {
cout << "Impossible" << endl;
} else {
cout << ans << endl;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 7;
const long long q = 7057594037927903;
const long long prime = 2137;
int n, a, b, odwiedzone[M], wazne;
long long tab[M], rozmiar[M], dp[M], wynik;
vector<int> v[M], synowie[M];
vector<long long> v2;
void wczytaj() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &tab[i]);
for (int i = 1; i < n; i++) {
scanf("%d%d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
for (int i = 1; i <= n; i++) dp[i] = -q;
}
void Dfs(int h) {
odwiedzone[h] = 1;
for (int i = 0; i < (int)v[h].size(); i++)
if (!odwiedzone[v[h][i]]) {
synowie[h].push_back(v[h][i]);
Dfs(v[h][i]);
}
rozmiar[h] += tab[h];
if ((int)synowie[h].size() >= 2) wazne = 1;
for (int i = 0; i < (int)synowie[h].size(); i++) {
rozmiar[h] += rozmiar[synowie[h][i]];
dp[h] = max(dp[h], dp[synowie[h][i]]);
}
dp[h] = max(dp[h], rozmiar[h]);
}
int main() {
wczytaj();
Dfs(1);
if (!wazne) {
printf("Impossible");
return 0;
} else {
wynik = -q;
for (int i = 1; i <= n; i++) {
if ((int)synowie[i].size() >= 2) {
v2.resize(0);
for (int j = 0; j < (int)synowie[i].size(); j++)
v2.push_back(dp[synowie[i][j]]);
sort(v2.begin(), v2.end());
wynik = max(wynik, v2[(int)v2.size() - 1] + v2[(int)v2.size() - 2]);
}
}
}
printf("%lld", wynik);
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;
const int M = 2e5 + 7;
const long long q = 7057594037927903;
const long long prime = 2137;
int n, a, b, odwiedzone[M], wazne;
long long tab[M], rozmiar[M], dp[M], wynik;
vector<int> v[M], synowie[M];
vector<long long> v2;
void wczytaj() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &tab[i]);
for (int i = 1; i < n; i++) {
scanf("%d%d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
for (int i = 1; i <= n; i++) dp[i] = -q;
}
void Dfs(int h) {
odwiedzone[h] = 1;
for (int i = 0; i < (int)v[h].size(); i++)
if (!odwiedzone[v[h][i]]) {
synowie[h].push_back(v[h][i]);
Dfs(v[h][i]);
}
rozmiar[h] += tab[h];
if ((int)synowie[h].size() >= 2) wazne = 1;
for (int i = 0; i < (int)synowie[h].size(); i++) {
rozmiar[h] += rozmiar[synowie[h][i]];
dp[h] = max(dp[h], dp[synowie[h][i]]);
}
dp[h] = max(dp[h], rozmiar[h]);
}
int main() {
wczytaj();
Dfs(1);
if (!wazne) {
printf("Impossible");
return 0;
} else {
wynik = -q;
for (int i = 1; i <= n; i++) {
if ((int)synowie[i].size() >= 2) {
v2.resize(0);
for (int j = 0; j < (int)synowie[i].size(); j++)
v2.push_back(dp[synowie[i][j]]);
sort(v2.begin(), v2.end());
wynik = max(wynik, v2[(int)v2.size() - 1] + v2[(int)v2.size() - 2]);
}
}
}
printf("%lld", wynik);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long maxv[200001], sumv[200001], a[200001];
vector<int> tree[200001];
pair<long long, long long> ansf = {LONG_MIN / 2, LONG_MIN / 2};
int n, u, v;
bool visited[200001];
void dfs(int v) {
visited[v] = 1;
int c = 0;
long long s = 0;
vector<long long> val;
for (auto u : tree[v]) {
if (!visited[u]) {
c++;
dfs(u);
s += sumv[u];
val.push_back(maxv[u]);
maxv[v] = max(maxv[v], maxv[u]);
}
}
if (c == 0)
maxv[v] = sumv[v] = a[v];
else
sumv[v] += (s + a[v]), maxv[v] = max(sumv[v], maxv[v]);
if (c > 1) {
pair<long long, long long> ans = {val[0], val[1]};
for (int i = 2; i < c; i++) {
if (val[i] > ans.first && val[i] > ans.second)
ans = {val[i], max(ans.first, ans.second)};
else if (val[i] > ans.first && val[i] < ans.second)
ans = {val[i], ans.second};
else if (val[i] < ans.first && val[i] > ans.second)
ans = {val[i], ans.first};
}
if (ans.first + ans.second > ansf.first + ansf.second) ansf = ans;
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
maxv[i] = INT_MIN;
visited[i] = 0;
sumv[i] = 0;
}
for (int i = 1; i < n; i++) {
cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
dfs(1);
if (ansf.first == LONG_MIN / 2 && ansf.second == LONG_MIN / 2)
cout << "Impossible" << endl;
else
cout << ansf.first + ansf.second << endl;
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 maxv[200001], sumv[200001], a[200001];
vector<int> tree[200001];
pair<long long, long long> ansf = {LONG_MIN / 2, LONG_MIN / 2};
int n, u, v;
bool visited[200001];
void dfs(int v) {
visited[v] = 1;
int c = 0;
long long s = 0;
vector<long long> val;
for (auto u : tree[v]) {
if (!visited[u]) {
c++;
dfs(u);
s += sumv[u];
val.push_back(maxv[u]);
maxv[v] = max(maxv[v], maxv[u]);
}
}
if (c == 0)
maxv[v] = sumv[v] = a[v];
else
sumv[v] += (s + a[v]), maxv[v] = max(sumv[v], maxv[v]);
if (c > 1) {
pair<long long, long long> ans = {val[0], val[1]};
for (int i = 2; i < c; i++) {
if (val[i] > ans.first && val[i] > ans.second)
ans = {val[i], max(ans.first, ans.second)};
else if (val[i] > ans.first && val[i] < ans.second)
ans = {val[i], ans.second};
else if (val[i] < ans.first && val[i] > ans.second)
ans = {val[i], ans.first};
}
if (ans.first + ans.second > ansf.first + ansf.second) ansf = ans;
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
maxv[i] = INT_MIN;
visited[i] = 0;
sumv[i] = 0;
}
for (int i = 1; i < n; i++) {
cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
dfs(1);
if (ansf.first == LONG_MIN / 2 && ansf.second == LONG_MIN / 2)
cout << "Impossible" << endl;
else
cout << ansf.first + ansf.second << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long int m, i, j, n, tmp, p, q, cnt, d, k, u, vv, t,
ans = -12345678998, a[223456], mx[223456], sm[223456];
string s;
vector<vector<long long int> > g(223456);
void dfs(long long int ver, long long int par) {
mx[ver] = -12345678998;
sm[ver] = a[ver];
for (auto &it : g[ver]) {
if (it == par) continue;
dfs(it, ver);
sm[ver] += sm[it];
mx[ver] = max(mx[ver], mx[it]);
}
mx[ver] = max(mx[ver], sm[ver]);
}
void dfs1(long long int ver, long long int par, long long int out) {
if (out != -12345678998) ans = max(ans, out + sm[ver]);
vector<long long int> v;
v.push_back(-12345678998);
v.push_back(-12345678998);
for (auto &it : g[ver]) {
if (it == par) continue;
v.push_back(mx[it]);
sort(v.begin(), v.end(), greater<long long int>());
if (v.size() > 2) v.pop_back();
}
for (auto &it : g[ver]) {
if (it == par) continue;
if (v[0] == mx[it])
dfs1(it, ver, max(out, v[1]));
else
dfs1(it, ver, max(out, v[0]));
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
if (fopen("inp.txt", "r")) freopen("inp.txt", "r", stdin);
cin >> n;
for (i = 0; i < n; i++) cin >> a[i];
for (i = 0; i < n - 1; i++) {
cin >> u >> vv;
u--;
vv--;
g[u].push_back(vv);
g[vv].push_back(u);
}
dfs(0, -1);
long long int out = -12345678998;
dfs1(0, -1, out);
if (ans == -12345678998)
cout << "Impossible";
else
cout << ans;
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;
long long int m, i, j, n, tmp, p, q, cnt, d, k, u, vv, t,
ans = -12345678998, a[223456], mx[223456], sm[223456];
string s;
vector<vector<long long int> > g(223456);
void dfs(long long int ver, long long int par) {
mx[ver] = -12345678998;
sm[ver] = a[ver];
for (auto &it : g[ver]) {
if (it == par) continue;
dfs(it, ver);
sm[ver] += sm[it];
mx[ver] = max(mx[ver], mx[it]);
}
mx[ver] = max(mx[ver], sm[ver]);
}
void dfs1(long long int ver, long long int par, long long int out) {
if (out != -12345678998) ans = max(ans, out + sm[ver]);
vector<long long int> v;
v.push_back(-12345678998);
v.push_back(-12345678998);
for (auto &it : g[ver]) {
if (it == par) continue;
v.push_back(mx[it]);
sort(v.begin(), v.end(), greater<long long int>());
if (v.size() > 2) v.pop_back();
}
for (auto &it : g[ver]) {
if (it == par) continue;
if (v[0] == mx[it])
dfs1(it, ver, max(out, v[1]));
else
dfs1(it, ver, max(out, v[0]));
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
if (fopen("inp.txt", "r")) freopen("inp.txt", "r", stdin);
cin >> n;
for (i = 0; i < n; i++) cin >> a[i];
for (i = 0; i < n - 1; i++) {
cin >> u >> vv;
u--;
vv--;
g[u].push_back(vv);
g[vv].push_back(u);
}
dfs(0, -1);
long long int out = -12345678998;
dfs1(0, -1, out);
if (ans == -12345678998)
cout << "Impossible";
else
cout << ans;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
struct node {
int v, nxt;
} edge[200010 * 2];
long long dp[200010], sum[200010], w[200010], head[200010], tot, ans;
void add(int u, int v) {
edge[tot].v = v;
edge[tot].nxt = head[u];
head[u] = tot++;
}
void dfs(int u, int fa) {
sum[u] = w[u];
for (int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if (v == fa) continue;
dfs(v, u);
sum[u] += sum[v];
if (dp[u] > -INF) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], sum[u]);
}
int main() {
int n;
cin >> n;
memset(head, -1, sizeof(head));
for (int i = 1; i <= n; i++) cin >> w[i];
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
add(u, v);
add(v, u);
}
ans = -INF;
for (int i = 1; i <= n; i++) dp[i] = -INF;
dfs(1, -1);
if (ans <= -INF)
puts("Impossible");
else
cout << ans << endl;
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;
const long long INF = 1LL << 60;
struct node {
int v, nxt;
} edge[200010 * 2];
long long dp[200010], sum[200010], w[200010], head[200010], tot, ans;
void add(int u, int v) {
edge[tot].v = v;
edge[tot].nxt = head[u];
head[u] = tot++;
}
void dfs(int u, int fa) {
sum[u] = w[u];
for (int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if (v == fa) continue;
dfs(v, u);
sum[u] += sum[v];
if (dp[u] > -INF) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], sum[u]);
}
int main() {
int n;
cin >> n;
memset(head, -1, sizeof(head));
for (int i = 1; i <= n; i++) cin >> w[i];
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
add(u, v);
add(v, u);
}
ans = -INF;
for (int i = 1; i <= n; i++) dp[i] = -INF;
dfs(1, -1);
if (ans <= -INF)
puts("Impossible");
else
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<long long int> a(200001);
vector<long long int> adj[200001];
long long int n, ans;
long long int sum[200001];
long long int sum_dfs(long long int node, long long int par) {
long long int pq = a[node];
for (auto it : adj[node]) {
if (it != par) {
pq += sum_dfs(it, node);
}
}
return sum[node] = pq;
}
long long int dfs(long long int node, long long int par) {
vector<long long int> vals;
for (auto it : adj[node]) {
if (it != par) {
vals.push_back(dfs(it, node));
}
}
if (vals.size() == 0) {
return a[node];
}
sort(vals.begin(), vals.end(), greater<long long int>());
if (vals.size() > 1) ans = max(ans, vals[0] + vals[1]);
return max(vals[0], sum[node]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
ans = LONG_MIN;
long long int l, r;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
cin >> l >> r;
l--;
r--;
adj[l].push_back(r);
adj[r].push_back(l);
}
l = sum_dfs(0, -1);
r = dfs(0, -1);
if (ans == LONG_MIN) {
cout << "Impossible\n";
} else {
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;
vector<long long int> a(200001);
vector<long long int> adj[200001];
long long int n, ans;
long long int sum[200001];
long long int sum_dfs(long long int node, long long int par) {
long long int pq = a[node];
for (auto it : adj[node]) {
if (it != par) {
pq += sum_dfs(it, node);
}
}
return sum[node] = pq;
}
long long int dfs(long long int node, long long int par) {
vector<long long int> vals;
for (auto it : adj[node]) {
if (it != par) {
vals.push_back(dfs(it, node));
}
}
if (vals.size() == 0) {
return a[node];
}
sort(vals.begin(), vals.end(), greater<long long int>());
if (vals.size() > 1) ans = max(ans, vals[0] + vals[1]);
return max(vals[0], sum[node]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
ans = LONG_MIN;
long long int l, r;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
cin >> l >> r;
l--;
r--;
adj[l].push_back(r);
adj[r].push_back(l);
}
l = sum_dfs(0, -1);
r = dfs(0, -1);
if (ans == LONG_MIN) {
cout << "Impossible\n";
} else {
cout << ans << "\n";
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n;
std::vector<int> adj[N];
long long sub[N];
long long mx[N];
int a[N];
long long ans;
void dfs(int u, int par) {
mx[u] = -100000000000000000LL;
sub[u] = a[u];
for (auto x : adj[u]) {
if (x != par) {
dfs(x, u);
sub[u] += sub[x];
mx[u] = max(mx[u], mx[x]);
}
}
mx[u] = max(mx[u], sub[u]);
}
void solve(int u, int par, long long maxi) {
ans = max(ans, sub[u] + maxi);
std::vector<pair<long long, int> > v;
for (auto x : adj[u]) {
if (x != par) {
v.push_back(make_pair(mx[x], x));
sort(v.rbegin(), v.rend());
if (v.size() == 3) v.pop_back();
}
}
v.push_back(make_pair(-100000000000000000LL, -1));
for (auto x : adj[u]) {
if (x != par) {
long long temp;
if (v[0].second == x) {
temp = v[1].first;
} else {
temp = v[0].first;
}
solve(x, u, max(temp, maxi));
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
for (int i = 0; i < n - 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1, -1);
ans = -100000000000000000LL;
solve(1, -1, -100000000000000000LL);
if (ans < -1000000000000000LL) {
cout << "Impossible\n";
return 0;
}
cout << ans << 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 int N = 2e5 + 5;
int n;
std::vector<int> adj[N];
long long sub[N];
long long mx[N];
int a[N];
long long ans;
void dfs(int u, int par) {
mx[u] = -100000000000000000LL;
sub[u] = a[u];
for (auto x : adj[u]) {
if (x != par) {
dfs(x, u);
sub[u] += sub[x];
mx[u] = max(mx[u], mx[x]);
}
}
mx[u] = max(mx[u], sub[u]);
}
void solve(int u, int par, long long maxi) {
ans = max(ans, sub[u] + maxi);
std::vector<pair<long long, int> > v;
for (auto x : adj[u]) {
if (x != par) {
v.push_back(make_pair(mx[x], x));
sort(v.rbegin(), v.rend());
if (v.size() == 3) v.pop_back();
}
}
v.push_back(make_pair(-100000000000000000LL, -1));
for (auto x : adj[u]) {
if (x != par) {
long long temp;
if (v[0].second == x) {
temp = v[1].first;
} else {
temp = v[0].first;
}
solve(x, u, max(temp, maxi));
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
for (int i = 0; i < n - 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1, -1);
ans = -100000000000000000LL;
solve(1, -1, -100000000000000000LL);
if (ans < -1000000000000000LL) {
cout << "Impossible\n";
return 0;
}
cout << ans << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
pair<int64_t, int64_t> f[200005];
int n;
int64_t res = -1e15, a[200005], sum[200005];
bool Free[200005];
vector<int> g[200005];
void DFS(int u) {
sum[u] = a[u];
Free[u] = true;
f[u] = pair<int64_t, int64_t>(-1e15, -1e15);
for (int i = 0; i < g[u].size(); i++)
if (Free[g[u][i]] == false) {
DFS(g[u][i]);
sum[u] += sum[g[u][i]];
int64_t tmp = max(f[g[u][i]].first, sum[g[u][i]]);
if (f[u].first < tmp) {
f[u].second = f[u].first;
f[u].first = tmp;
} else if (f[u].second < tmp)
f[u].second = tmp;
}
res = max(res, f[u].first + f[u].second);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
int u, v;
for (int i = 1; i < n; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
DFS(1);
if (res < -1e14)
cout << "Impossible";
else
cout << res;
}
|
### 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<int64_t, int64_t> f[200005];
int n;
int64_t res = -1e15, a[200005], sum[200005];
bool Free[200005];
vector<int> g[200005];
void DFS(int u) {
sum[u] = a[u];
Free[u] = true;
f[u] = pair<int64_t, int64_t>(-1e15, -1e15);
for (int i = 0; i < g[u].size(); i++)
if (Free[g[u][i]] == false) {
DFS(g[u][i]);
sum[u] += sum[g[u][i]];
int64_t tmp = max(f[g[u][i]].first, sum[g[u][i]]);
if (f[u].first < tmp) {
f[u].second = f[u].first;
f[u].first = tmp;
} else if (f[u].second < tmp)
f[u].second = tmp;
}
res = max(res, f[u].first + f[u].second);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
int u, v;
for (int i = 1; i < n; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
DFS(1);
if (res < -1e14)
cout << "Impossible";
else
cout << res;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MXN = 1e6 + 30;
const int MAXN = 1e3 + 123;
const long long INF = 1e15;
const long long BINF = 1e18;
const int MOD = 1e9 + 7;
const long double EPS = 1e-15;
long long n;
long long a[MXN], cnt;
long long u[MXN];
long long sum[MXN], q;
vector<int> g[MXN];
long long tin[MXN], tout[MXN], ty;
long long mx = -BINF;
long long p[MXN];
multiset<long long> st[MXN];
multiset<long long>::iterator it, it2;
long long t[MXN * 4];
long long get_max(int x, int tl, int tr, int l, int r) {
if (r < tl || tr < l) {
return -INF;
}
if (l <= tl && tr <= r) {
return t[x];
}
int mid = (tl + tr) / 2;
return max(get_max(2 * x, tl, mid, l, r),
get_max(2 * x + 1, mid + 1, tr, l, r));
}
void upd(int x, int tl, int tr, int ind, long long new_val) {
if (tl == tr) {
t[x] = new_val;
return;
}
int mid = (tl + tr) / 2;
if (ind <= mid) {
upd(x + x, tl, mid, ind, new_val);
} else {
upd(x + x + 1, mid + 1, tr, ind, new_val);
}
t[x] = max(t[x + x], t[x + x + 1]);
}
void dfs(int v, int pr = 0) {
tin[v] = ++ty, p[v] = pr;
for (auto to : g[v]) {
if (to != pr) {
dfs(to, v);
sum[v] += sum[to];
}
}
if (g[v].size() > 2 || (v == 1 && g[v].size() > 1)) q = 1;
sum[v] += a[v], tout[v] = ++ty, u[v] = ty;
}
void dfs2(int v, int pr = 0) {
for (auto to : g[v]) {
if (to != pr) {
st[v].insert(get_max(1, 1, ty, tin[to], tout[to]));
dfs2(to, v);
}
}
if (st[v].size() > 1) {
it = st[v].end(), it--;
it2 = it, it2--;
mx = max(mx, *it + *it2);
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1);
if (!q) {
cout << "Impossible" << '\n';
return 0;
}
for (int i = 1; i <= n * 2; i++) {
upd(1, 1, ty, i, -INF);
}
for (int i = 1; i <= n; i++) {
upd(1, 1, ty, u[i], sum[i]);
}
dfs2(1);
cout << mx;
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 MXN = 1e6 + 30;
const int MAXN = 1e3 + 123;
const long long INF = 1e15;
const long long BINF = 1e18;
const int MOD = 1e9 + 7;
const long double EPS = 1e-15;
long long n;
long long a[MXN], cnt;
long long u[MXN];
long long sum[MXN], q;
vector<int> g[MXN];
long long tin[MXN], tout[MXN], ty;
long long mx = -BINF;
long long p[MXN];
multiset<long long> st[MXN];
multiset<long long>::iterator it, it2;
long long t[MXN * 4];
long long get_max(int x, int tl, int tr, int l, int r) {
if (r < tl || tr < l) {
return -INF;
}
if (l <= tl && tr <= r) {
return t[x];
}
int mid = (tl + tr) / 2;
return max(get_max(2 * x, tl, mid, l, r),
get_max(2 * x + 1, mid + 1, tr, l, r));
}
void upd(int x, int tl, int tr, int ind, long long new_val) {
if (tl == tr) {
t[x] = new_val;
return;
}
int mid = (tl + tr) / 2;
if (ind <= mid) {
upd(x + x, tl, mid, ind, new_val);
} else {
upd(x + x + 1, mid + 1, tr, ind, new_val);
}
t[x] = max(t[x + x], t[x + x + 1]);
}
void dfs(int v, int pr = 0) {
tin[v] = ++ty, p[v] = pr;
for (auto to : g[v]) {
if (to != pr) {
dfs(to, v);
sum[v] += sum[to];
}
}
if (g[v].size() > 2 || (v == 1 && g[v].size() > 1)) q = 1;
sum[v] += a[v], tout[v] = ++ty, u[v] = ty;
}
void dfs2(int v, int pr = 0) {
for (auto to : g[v]) {
if (to != pr) {
st[v].insert(get_max(1, 1, ty, tin[to], tout[to]));
dfs2(to, v);
}
}
if (st[v].size() > 1) {
it = st[v].end(), it--;
it2 = it, it2--;
mx = max(mx, *it + *it2);
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1);
if (!q) {
cout << "Impossible" << '\n';
return 0;
}
for (int i = 1; i <= n * 2; i++) {
upd(1, 1, ty, i, -INF);
}
for (int i = 1; i <= n; i++) {
upd(1, 1, ty, u[i], sum[i]);
}
dfs2(1);
cout << mx;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 100;
vector<long long> nei[maxn];
bool mark[maxn];
long long sum[maxn];
long long arr[maxn];
long long dp[maxn];
long long n, m;
bool kid = false;
const long long lim = -2e15;
long long ans = -2e15;
void dfs(int v) {
long long max1 = lim, max2 = lim;
if (v == 0 && nei[v].size() > 1)
kid = true;
else if (nei[v].size() > 2)
kid = true;
mark[v] = true;
for (int i = 0; i < nei[v].size(); i++) {
long long u = nei[v][i];
if (!mark[u]) {
dfs(u);
sum[v] += sum[u];
dp[v] = max(dp[v], dp[u]);
}
if (dp[u] >= max1) {
max2 = max1;
max1 = dp[u];
} else if (dp[u] > max2)
max2 = dp[u];
}
sum[v] += arr[v];
dp[v] = max(dp[v], sum[v]);
ans = max(ans, max1 + max2);
return;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) dp[i] = lim;
for (int i = 0; i < n; i++) cin >> arr[i];
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
nei[a - 1].push_back(b - 1);
nei[b - 1].push_back(a - 1);
}
dfs(0);
if (!kid)
cout << "Impossible" << endl;
else
cout << ans << 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 maxn = 200000 + 100;
vector<long long> nei[maxn];
bool mark[maxn];
long long sum[maxn];
long long arr[maxn];
long long dp[maxn];
long long n, m;
bool kid = false;
const long long lim = -2e15;
long long ans = -2e15;
void dfs(int v) {
long long max1 = lim, max2 = lim;
if (v == 0 && nei[v].size() > 1)
kid = true;
else if (nei[v].size() > 2)
kid = true;
mark[v] = true;
for (int i = 0; i < nei[v].size(); i++) {
long long u = nei[v][i];
if (!mark[u]) {
dfs(u);
sum[v] += sum[u];
dp[v] = max(dp[v], dp[u]);
}
if (dp[u] >= max1) {
max2 = max1;
max1 = dp[u];
} else if (dp[u] > max2)
max2 = dp[u];
}
sum[v] += arr[v];
dp[v] = max(dp[v], sum[v]);
ans = max(ans, max1 + max2);
return;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) dp[i] = lim;
for (int i = 0; i < n; i++) cin >> arr[i];
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
nei[a - 1].push_back(b - 1);
nei[b - 1].push_back(a - 1);
}
dfs(0);
if (!kid)
cout << "Impossible" << endl;
else
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> g[200005];
long long ans;
long long w[200005];
long long s[200005];
long long m[200005];
void dfs(int p, int pp) {
s[p] = w[p];
m[p] = -9223372036854775807ll;
for (int i = 0; i < g[p].size(); i++) {
if (pp == g[p][i]) continue;
dfs(g[p][i], p);
s[p] += s[g[p][i]];
m[p] = max(m[p], m[g[p][i]]);
}
m[p] = max(m[p], s[p]);
long long mx1 = -9223372036854775807ll;
long long mx2 = -9223372036854775807ll;
for (int i = 0; i < g[p].size(); i++) {
if (pp == g[p][i]) continue;
if (m[g[p][i]] > mx1) {
mx2 = mx1;
mx1 = m[g[p][i]];
} else if (m[g[p][i]] > mx2) {
mx2 = m[g[p][i]];
}
}
if (mx1 != -9223372036854775807ll && mx2 != -9223372036854775807ll)
ans = max(ans, mx1 + mx2);
return;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%I64d", &w[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 = -9223372036854775807ll;
dfs(1, -1);
if (ans == -9223372036854775807ll)
printf("Impossible\n");
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;
vector<int> g[200005];
long long ans;
long long w[200005];
long long s[200005];
long long m[200005];
void dfs(int p, int pp) {
s[p] = w[p];
m[p] = -9223372036854775807ll;
for (int i = 0; i < g[p].size(); i++) {
if (pp == g[p][i]) continue;
dfs(g[p][i], p);
s[p] += s[g[p][i]];
m[p] = max(m[p], m[g[p][i]]);
}
m[p] = max(m[p], s[p]);
long long mx1 = -9223372036854775807ll;
long long mx2 = -9223372036854775807ll;
for (int i = 0; i < g[p].size(); i++) {
if (pp == g[p][i]) continue;
if (m[g[p][i]] > mx1) {
mx2 = mx1;
mx1 = m[g[p][i]];
} else if (m[g[p][i]] > mx2) {
mx2 = m[g[p][i]];
}
}
if (mx1 != -9223372036854775807ll && mx2 != -9223372036854775807ll)
ans = max(ans, mx1 + mx2);
return;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%I64d", &w[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 = -9223372036854775807ll;
dfs(1, -1);
if (ans == -9223372036854775807ll)
printf("Impossible\n");
else
printf("%I64d\n", ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
class TreeNode {
public:
long long val;
long long maxiTree;
long long sumTree;
long long maxi_two;
bool exist;
vector<TreeNode*> child;
};
void construct_tree(int x, TreeNode** root, vector<long long>& a,
vector<vector<int> >& g, vector<bool>& vis) {
vis[x] = true;
(*root) = new TreeNode();
(*root)->val = a[x];
TreeNode* temp;
int cnt = 0;
for (int i = 0; i < g[x].size(); i++) {
int y = g[x][i];
if (!vis[y]) {
(*root)->child.push_back(temp);
construct_tree(y, &((*root)->child[cnt]), a, g, vis);
cnt++;
}
}
}
void calc_sum(TreeNode* root) {
root->sumTree = 0;
for (int i = 0; i < root->child.size(); i++) {
calc_sum(root->child[i]);
root->sumTree += root->child[i]->sumTree;
}
root->sumTree += root->val;
}
void calc_maxi(TreeNode* root) {
root->maxiTree = root->sumTree;
for (int i = 0; i < root->child.size(); i++) {
calc_maxi(root->child[i]);
root->maxiTree = max(root->maxiTree, root->child[i]->maxiTree);
}
root->sumTree += root->val;
}
void calc_maxi_two(TreeNode* root) {
vector<long long> maxi;
root->exist = false;
for (int i = 0; i < root->child.size(); i++) {
TreeNode* ch = (root->child[i]);
if (maxi.size() < 2) {
maxi.push_back(ch->maxiTree);
} else if (maxi[0] < ch->maxiTree) {
maxi[0] = ch->maxiTree;
}
sort(maxi.begin(), maxi.end());
}
if (maxi.size() == 2) {
root->exist = true;
root->maxi_two = maxi[0] + maxi[1];
}
for (int i = 0; i < root->child.size(); i++) {
TreeNode* ch = (root->child[i]);
calc_maxi_two(ch);
if (ch->exist) {
if (!root->exist || ch->maxi_two > root->maxi_two) {
root->exist = true;
root->maxi_two = ch->maxi_two;
}
}
}
}
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<vector<int> > g(n);
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);
}
TreeNode* root;
vector<bool> vis(n, false);
construct_tree(0, &root, a, g, vis);
calc_sum(root);
calc_maxi(root);
calc_maxi_two(root);
if (root->exist)
cout << root->maxi_two << endl;
else
cout << "Impossible" << 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;
class TreeNode {
public:
long long val;
long long maxiTree;
long long sumTree;
long long maxi_two;
bool exist;
vector<TreeNode*> child;
};
void construct_tree(int x, TreeNode** root, vector<long long>& a,
vector<vector<int> >& g, vector<bool>& vis) {
vis[x] = true;
(*root) = new TreeNode();
(*root)->val = a[x];
TreeNode* temp;
int cnt = 0;
for (int i = 0; i < g[x].size(); i++) {
int y = g[x][i];
if (!vis[y]) {
(*root)->child.push_back(temp);
construct_tree(y, &((*root)->child[cnt]), a, g, vis);
cnt++;
}
}
}
void calc_sum(TreeNode* root) {
root->sumTree = 0;
for (int i = 0; i < root->child.size(); i++) {
calc_sum(root->child[i]);
root->sumTree += root->child[i]->sumTree;
}
root->sumTree += root->val;
}
void calc_maxi(TreeNode* root) {
root->maxiTree = root->sumTree;
for (int i = 0; i < root->child.size(); i++) {
calc_maxi(root->child[i]);
root->maxiTree = max(root->maxiTree, root->child[i]->maxiTree);
}
root->sumTree += root->val;
}
void calc_maxi_two(TreeNode* root) {
vector<long long> maxi;
root->exist = false;
for (int i = 0; i < root->child.size(); i++) {
TreeNode* ch = (root->child[i]);
if (maxi.size() < 2) {
maxi.push_back(ch->maxiTree);
} else if (maxi[0] < ch->maxiTree) {
maxi[0] = ch->maxiTree;
}
sort(maxi.begin(), maxi.end());
}
if (maxi.size() == 2) {
root->exist = true;
root->maxi_two = maxi[0] + maxi[1];
}
for (int i = 0; i < root->child.size(); i++) {
TreeNode* ch = (root->child[i]);
calc_maxi_two(ch);
if (ch->exist) {
if (!root->exist || ch->maxi_two > root->maxi_two) {
root->exist = true;
root->maxi_two = ch->maxi_two;
}
}
}
}
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<vector<int> > g(n);
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);
}
TreeNode* root;
vector<bool> vis(n, false);
construct_tree(0, &root, a, g, vis);
calc_sum(root);
calc_maxi(root);
calc_maxi_two(root);
if (root->exist)
cout << root->maxi_two << endl;
else
cout << "Impossible" << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long P[200001];
long long val[200001];
long long Seg[1 << 20 + 1];
int n, first, second;
vector<vector<int> > V(200001);
vector<vector<long long> > V2(200001);
bool Vis[200001];
int F[200001];
int L[200001];
int History[400002];
long long Ps[400002];
long long Mx[400002];
int idx, l_q, r_q;
void Build(int l, int r, int root) {
if (l == r) {
Seg[root] = Ps[l];
return;
}
int mid = (r + l) / 2;
Build(l, mid, root * 2);
Build(mid + 1, r, root * 2 + 1);
Seg[root] = max(Seg[root * 2], Seg[root * 2 + 1]);
}
long long RMQ(int l, int r, int root) {
if (l > r_q || l_q > r) return -1e17;
if (l >= l_q && r <= r_q) return Seg[root];
int mid = (r + l) / 2;
return max(RMQ(l, mid, root * 2), RMQ(mid + 1, r, root * 2 + 1));
}
void Dfs(int node) {
Vis[node] = true;
for (int i = 0; i < (int)V[node].size(); i++) {
int X = V[node][i];
if (!Vis[X]) {
Dfs(X);
val[node] += val[X];
}
}
}
void Dfs2(int node) {
Vis[node] = true;
F[node] = L[node] = idx;
Ps[idx] = val[node];
idx++;
for (int i = 0; i < (int)V[node].size(); i++) {
int X = V[node][i];
if (!Vis[X]) {
Dfs2(X);
L[node] = idx;
Ps[idx] = val[node];
idx++;
}
}
}
void Dfs3(int node) {
Vis[node] = true;
for (int i = 0; i < (int)V[node].size(); i++) {
int X = V[node][i];
if (!Vis[X]) {
V2[node].push_back(Mx[X]);
Dfs3(X);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i < (int)n + 1; i++) cin >> P[i], val[i] = P[i];
for (int i = 0; i < (int)n - 1; i++) {
cin >> first >> second;
V[first].push_back(second);
V[second].push_back(first);
}
Dfs(1);
memset(Vis, false, sizeof(Vis));
Dfs2(1);
Build(0, 400002, 1);
for (int i = 1; i < (int)n + 1; i++) {
l_q = F[i];
r_q = L[i];
Mx[i] = RMQ(0, 400002, 1);
}
memset(Vis, false, sizeof(Vis));
Dfs3(1);
long long Sol = -1e18;
for (int i = 1; i < (int)n + 1; i++) {
sort(V2[i].begin(), V2[i].end());
int Sz = V2[i].size();
if (Sz > 1) Sol = max(Sol, (long long)(V2[i][Sz - 1] + V2[i][Sz - 2]));
}
(Sol == -1e18) ? cout << "Impossible\n" : cout << Sol << endl;
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 P[200001];
long long val[200001];
long long Seg[1 << 20 + 1];
int n, first, second;
vector<vector<int> > V(200001);
vector<vector<long long> > V2(200001);
bool Vis[200001];
int F[200001];
int L[200001];
int History[400002];
long long Ps[400002];
long long Mx[400002];
int idx, l_q, r_q;
void Build(int l, int r, int root) {
if (l == r) {
Seg[root] = Ps[l];
return;
}
int mid = (r + l) / 2;
Build(l, mid, root * 2);
Build(mid + 1, r, root * 2 + 1);
Seg[root] = max(Seg[root * 2], Seg[root * 2 + 1]);
}
long long RMQ(int l, int r, int root) {
if (l > r_q || l_q > r) return -1e17;
if (l >= l_q && r <= r_q) return Seg[root];
int mid = (r + l) / 2;
return max(RMQ(l, mid, root * 2), RMQ(mid + 1, r, root * 2 + 1));
}
void Dfs(int node) {
Vis[node] = true;
for (int i = 0; i < (int)V[node].size(); i++) {
int X = V[node][i];
if (!Vis[X]) {
Dfs(X);
val[node] += val[X];
}
}
}
void Dfs2(int node) {
Vis[node] = true;
F[node] = L[node] = idx;
Ps[idx] = val[node];
idx++;
for (int i = 0; i < (int)V[node].size(); i++) {
int X = V[node][i];
if (!Vis[X]) {
Dfs2(X);
L[node] = idx;
Ps[idx] = val[node];
idx++;
}
}
}
void Dfs3(int node) {
Vis[node] = true;
for (int i = 0; i < (int)V[node].size(); i++) {
int X = V[node][i];
if (!Vis[X]) {
V2[node].push_back(Mx[X]);
Dfs3(X);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i < (int)n + 1; i++) cin >> P[i], val[i] = P[i];
for (int i = 0; i < (int)n - 1; i++) {
cin >> first >> second;
V[first].push_back(second);
V[second].push_back(first);
}
Dfs(1);
memset(Vis, false, sizeof(Vis));
Dfs2(1);
Build(0, 400002, 1);
for (int i = 1; i < (int)n + 1; i++) {
l_q = F[i];
r_q = L[i];
Mx[i] = RMQ(0, 400002, 1);
}
memset(Vis, false, sizeof(Vis));
Dfs3(1);
long long Sol = -1e18;
for (int i = 1; i < (int)n + 1; i++) {
sort(V2[i].begin(), V2[i].end());
int Sz = V2[i].size();
if (Sz > 1) Sol = max(Sol, (long long)(V2[i][Sz - 1] + V2[i][Sz - 2]));
}
(Sol == -1e18) ? cout << "Impossible\n" : cout << Sol << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long int INF = -1e17;
long long int ans = INF;
map<long long int, vector<long long int>> edges;
map<long long int, bool> vis;
map<long long int, long long int> p;
pair<long long int, long long int> dfs(long long int node) {
long long int s = p[node];
vector<long long int> v;
for (auto child : edges[node]) {
if (!vis[child]) {
vis[child] = true;
auto temp = dfs(child);
v.push_back(temp.second);
s += temp.first;
}
}
sort(v.begin(), v.end());
if (v.size() >= 2) {
ans = max(ans, v[v.size() - 1] + v[v.size() - 2]);
}
long long int mx = INF;
if (v.size() >= 1) mx = max(v[v.size() - 1], mx);
return {s, max(mx, s)};
}
signed main(void) {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
long long int t = 1;
while (t--) {
long long int n;
cin >> n;
for (long long int i = 1; i <= n; i++) {
cin >> p[i];
}
for (long long int i = 1; i <= n - 1; i++) {
long long int u, v;
cin >> u >> v;
edges[u].push_back(v);
edges[v].push_back(u);
}
vis[1] = true;
dfs(1);
if (ans == INF) {
cout << "Impossible\n";
} else {
cout << ans << 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;
const long long int INF = -1e17;
long long int ans = INF;
map<long long int, vector<long long int>> edges;
map<long long int, bool> vis;
map<long long int, long long int> p;
pair<long long int, long long int> dfs(long long int node) {
long long int s = p[node];
vector<long long int> v;
for (auto child : edges[node]) {
if (!vis[child]) {
vis[child] = true;
auto temp = dfs(child);
v.push_back(temp.second);
s += temp.first;
}
}
sort(v.begin(), v.end());
if (v.size() >= 2) {
ans = max(ans, v[v.size() - 1] + v[v.size() - 2]);
}
long long int mx = INF;
if (v.size() >= 1) mx = max(v[v.size() - 1], mx);
return {s, max(mx, s)};
}
signed main(void) {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
long long int t = 1;
while (t--) {
long long int n;
cin >> n;
for (long long int i = 1; i <= n; i++) {
cin >> p[i];
}
for (long long int i = 1; i <= n - 1; i++) {
long long int u, v;
cin >> u >> v;
edges[u].push_back(v);
edges[v].push_back(u);
}
vis[1] = true;
dfs(1);
if (ans == INF) {
cout << "Impossible\n";
} else {
cout << ans << endl;
}
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long inf = -(long long)1e15;
int a[200001];
int n;
vector<int> adj[200001];
long long sum[200001], dp1[200001], dp2[200001];
void dfs(int cur, int prev) {
sum[cur] = a[cur];
for (auto nxt : adj[cur]) {
if (nxt == prev) continue;
dfs(nxt, cur);
sum[cur] += sum[nxt];
}
dp1[cur] = sum[cur];
dp2[cur] = inf;
vector<long long> tmp;
for (auto nxt : adj[cur]) {
if (nxt == prev) continue;
dp1[cur] = max(dp1[cur], dp1[nxt]);
dp2[cur] = max(dp2[cur], dp2[nxt]);
tmp.push_back(dp1[nxt]);
}
sort(tmp.rbegin(), tmp.rend());
if (tmp.size() >= 2) dp2[cur] = max(dp2[cur], tmp[0] + tmp[1]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 0; i < n - 1; i++) {
int a, b;
scanf("%d %d", &a, &b);
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, -1);
if (dp2[1] == inf) {
puts("Impossible");
} else {
cout << dp2[1] << 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;
long long inf = -(long long)1e15;
int a[200001];
int n;
vector<int> adj[200001];
long long sum[200001], dp1[200001], dp2[200001];
void dfs(int cur, int prev) {
sum[cur] = a[cur];
for (auto nxt : adj[cur]) {
if (nxt == prev) continue;
dfs(nxt, cur);
sum[cur] += sum[nxt];
}
dp1[cur] = sum[cur];
dp2[cur] = inf;
vector<long long> tmp;
for (auto nxt : adj[cur]) {
if (nxt == prev) continue;
dp1[cur] = max(dp1[cur], dp1[nxt]);
dp2[cur] = max(dp2[cur], dp2[nxt]);
tmp.push_back(dp1[nxt]);
}
sort(tmp.rbegin(), tmp.rend());
if (tmp.size() >= 2) dp2[cur] = max(dp2[cur], tmp[0] + tmp[1]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 0; i < n - 1; i++) {
int a, b;
scanf("%d %d", &a, &b);
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, -1);
if (dp2[1] == inf) {
puts("Impossible");
} else {
cout << dp2[1] << endl;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 10;
const long long inf = 2e9 + 10;
const long long INF = 2e18 + 10;
const long long MOD = 1e9 + 7;
long long dp[N], s[N], Sz[N], n, ans = -INF, a[N];
vector<long long> g[N];
void dfs(long long u, long long p = -1) {
Sz[u] = 1;
s[u] = a[u];
for (long long e : g[u]) {
if (e == p) continue;
dfs(e, u);
Sz[u] += Sz[e];
s[u] += s[e];
}
}
void dfs1(long long u, long long p = -1) {
dp[u] = s[u];
vector<long long> f;
for (long long e : g[u]) {
if (e == p) continue;
dfs1(e, u);
f.push_back(dp[e]);
dp[u] = max(dp[u], dp[e]);
}
sort((f).begin(), (f).end(), greater<long long>());
if ((long long)(f).size() >= 2) ans = max(ans, f[0] + f[1]);
}
void DeoACLamCho() {
cin >> n;
for (long long i = 1; i <= n; ++i) cin >> a[i];
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);
}
dfs(1);
dfs1(1);
if (ans == -INF)
cout << "Impossible";
else
cout << ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) DeoACLamCho();
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;
const long long N = 2e5 + 10;
const long long inf = 2e9 + 10;
const long long INF = 2e18 + 10;
const long long MOD = 1e9 + 7;
long long dp[N], s[N], Sz[N], n, ans = -INF, a[N];
vector<long long> g[N];
void dfs(long long u, long long p = -1) {
Sz[u] = 1;
s[u] = a[u];
for (long long e : g[u]) {
if (e == p) continue;
dfs(e, u);
Sz[u] += Sz[e];
s[u] += s[e];
}
}
void dfs1(long long u, long long p = -1) {
dp[u] = s[u];
vector<long long> f;
for (long long e : g[u]) {
if (e == p) continue;
dfs1(e, u);
f.push_back(dp[e]);
dp[u] = max(dp[u], dp[e]);
}
sort((f).begin(), (f).end(), greater<long long>());
if ((long long)(f).size() >= 2) ans = max(ans, f[0] + f[1]);
}
void DeoACLamCho() {
cin >> n;
for (long long i = 1; i <= n; ++i) cin >> a[i];
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);
}
dfs(1);
dfs1(1);
if (ans == -INF)
cout << "Impossible";
else
cout << ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) DeoACLamCho();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n, 0);
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> row;
vector<vector<int>> g(n, row);
vector<vector<int>> tree(n, row);
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);
}
queue<int> q;
vector<int> vis(n, 0);
vector<int> lot;
vis[0] = 1;
lot.push_back(0);
q.push(0);
while (!q.empty()) {
int curr = q.front();
q.pop();
for (int i = 0; i < g[curr].size(); i++) {
int vtx = g[curr][i];
if (!vis[vtx]) {
vis[vtx] = 1;
lot.push_back(vtx);
tree[curr].push_back(vtx);
q.push(vtx);
}
}
}
vector<long long> dp_sum(n, 0);
vector<long long> dp_max(n, 0);
for (int i = n - 1; i >= 0; i--) {
int curr = lot[i];
dp_sum[curr] = (long long)a[curr];
for (int j = 0; j < tree[curr].size(); j++)
dp_sum[curr] += dp_sum[tree[curr][j]];
dp_max[curr] = dp_sum[curr];
for (int j = 0; j < tree[curr].size(); j++)
dp_max[curr] = max(dp_max[curr], dp_max[tree[curr][j]]);
}
int flag = 0;
vector<long long> dp_ans(n, -1000000000000000000);
long long ans = -1000000000000000000;
for (int i = 0; i < n; i++) {
int curr = lot[i];
vector<long long> cand;
for (int j = 0; j < tree[curr].size(); j++) {
int vtx = tree[curr][j];
cand.push_back(dp_max[vtx]);
}
sort(cand.begin(), cand.end());
if (!cand.size()) continue;
for (int j = 0; j < tree[curr].size(); j++) {
int vtx = tree[curr][j];
dp_ans[vtx] = dp_ans[curr];
if (dp_max[vtx] == cand[(int)cand.size() - 1] && cand.size() > 1)
dp_ans[vtx] = max(dp_ans[vtx], cand[(int)cand.size() - 2]);
else if (cand.size() > 1)
dp_ans[vtx] = max(dp_ans[vtx], cand[(int)cand.size() - 1]);
ans = max(ans, dp_ans[vtx] + dp_sum[vtx]);
}
}
if (ans < -10000000000000000)
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;
int main() {
int n;
cin >> n;
vector<int> a(n, 0);
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> row;
vector<vector<int>> g(n, row);
vector<vector<int>> tree(n, row);
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);
}
queue<int> q;
vector<int> vis(n, 0);
vector<int> lot;
vis[0] = 1;
lot.push_back(0);
q.push(0);
while (!q.empty()) {
int curr = q.front();
q.pop();
for (int i = 0; i < g[curr].size(); i++) {
int vtx = g[curr][i];
if (!vis[vtx]) {
vis[vtx] = 1;
lot.push_back(vtx);
tree[curr].push_back(vtx);
q.push(vtx);
}
}
}
vector<long long> dp_sum(n, 0);
vector<long long> dp_max(n, 0);
for (int i = n - 1; i >= 0; i--) {
int curr = lot[i];
dp_sum[curr] = (long long)a[curr];
for (int j = 0; j < tree[curr].size(); j++)
dp_sum[curr] += dp_sum[tree[curr][j]];
dp_max[curr] = dp_sum[curr];
for (int j = 0; j < tree[curr].size(); j++)
dp_max[curr] = max(dp_max[curr], dp_max[tree[curr][j]]);
}
int flag = 0;
vector<long long> dp_ans(n, -1000000000000000000);
long long ans = -1000000000000000000;
for (int i = 0; i < n; i++) {
int curr = lot[i];
vector<long long> cand;
for (int j = 0; j < tree[curr].size(); j++) {
int vtx = tree[curr][j];
cand.push_back(dp_max[vtx]);
}
sort(cand.begin(), cand.end());
if (!cand.size()) continue;
for (int j = 0; j < tree[curr].size(); j++) {
int vtx = tree[curr][j];
dp_ans[vtx] = dp_ans[curr];
if (dp_max[vtx] == cand[(int)cand.size() - 1] && cand.size() > 1)
dp_ans[vtx] = max(dp_ans[vtx], cand[(int)cand.size() - 2]);
else if (cand.size() > 1)
dp_ans[vtx] = max(dp_ans[vtx], cand[(int)cand.size() - 1]);
ans = max(ans, dp_ans[vtx] + dp_sum[vtx]);
}
}
if (ans < -10000000000000000)
cout << "Impossible" << endl;
else
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
int a[MAXN];
int n, par[MAXN];
vector<int> adj[MAXN];
long long d[MAXN];
long long ans = -1e18;
long long dfs(int u) {
long long mx1 = -1e18, mx2 = -1e18;
d[u] = a[u];
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i];
if (v != par[u]) {
par[v] = u;
long long tmp = dfs(v);
if (tmp > mx1)
mx2 = mx1, mx1 = tmp;
else if (tmp > mx2)
mx2 = tmp;
d[u] += d[v];
}
}
ans = max(ans, mx1 + mx2);
return max(d[u], mx1);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v), adj[v].push_back(u);
}
par[0] = -1;
dfs(0);
if (ans <= -1e11)
cout << "Impossible" << endl;
else
cout << ans << endl;
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;
const int MAXN = 2e5 + 5;
int a[MAXN];
int n, par[MAXN];
vector<int> adj[MAXN];
long long d[MAXN];
long long ans = -1e18;
long long dfs(int u) {
long long mx1 = -1e18, mx2 = -1e18;
d[u] = a[u];
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i];
if (v != par[u]) {
par[v] = u;
long long tmp = dfs(v);
if (tmp > mx1)
mx2 = mx1, mx1 = tmp;
else if (tmp > mx2)
mx2 = tmp;
d[u] += d[v];
}
}
ans = max(ans, mx1 + mx2);
return max(d[u], mx1);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v), adj[v].push_back(u);
}
par[0] = -1;
dfs(0);
if (ans <= -1e11)
cout << "Impossible" << endl;
else
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
long long a[200010] = {};
vector<int> e[200010];
long long t[200010] = {};
long long q[200010] = {};
int s, b;
long long ans = -222147483600ll;
bool cmp(long long x, long long y) { return x > y; }
void dfs(int x, int y) {
vector<long long> w;
t[x] += a[x];
q[x] = -2221483600ll;
for (int i = 0; i < e[x].size(); i++) {
if (e[x][i] == y) continue;
dfs(e[x][i], x);
t[x] += t[e[x][i]];
q[x] = max(q[x], q[e[x][i]]);
w.push_back(q[e[x][i]]);
}
sort(w.begin(), w.end(), cmp);
q[x] = max(t[x], q[x]);
if (w.size() >= 2) {
ans = max(w[0] + w[1], ans);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
for (int i = 1; i < n; i++) {
scanf("%d%d", &s, &b);
e[s].push_back(b);
e[b].push_back(s);
}
dfs(1, 0);
if (ans == -222147483600ll)
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;
int n;
long long a[200010] = {};
vector<int> e[200010];
long long t[200010] = {};
long long q[200010] = {};
int s, b;
long long ans = -222147483600ll;
bool cmp(long long x, long long y) { return x > y; }
void dfs(int x, int y) {
vector<long long> w;
t[x] += a[x];
q[x] = -2221483600ll;
for (int i = 0; i < e[x].size(); i++) {
if (e[x][i] == y) continue;
dfs(e[x][i], x);
t[x] += t[e[x][i]];
q[x] = max(q[x], q[e[x][i]]);
w.push_back(q[e[x][i]]);
}
sort(w.begin(), w.end(), cmp);
q[x] = max(t[x], q[x]);
if (w.size() >= 2) {
ans = max(w[0] + w[1], ans);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
for (int i = 1; i < n; i++) {
scanf("%d%d", &s, &b);
e[s].push_back(b);
e[b].push_back(s);
}
dfs(1, 0);
if (ans == -222147483600ll)
cout << "Impossible" << endl;
else
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e5 + 123;
const long long INF = (long long)1e18 + 123;
const int inf = (int)1e9 + 123;
const int MOD = (int)1e9 + 7;
long long val[N], mx[N], a[N];
int n;
vector<int> g[N];
void dfs_calc(int x, int par = -1) {
val[x] += a[x];
for (auto to : g[x]) {
if (to == par) continue;
dfs_calc(to, x);
val[x] += val[to];
}
}
void dfs_calc1(int x, int par = -1) {
mx[x] = val[x];
for (auto to : g[x]) {
if (to == par) continue;
dfs_calc1(to, x);
mx[x] = max(mx[x], mx[to]);
}
}
multiset<long long> st;
long long res = -INF;
void dfs(int x, int par = -1) {
long long num = 0;
if ((int)(st.size()) > 0) {
num = *st.rbegin();
res = max(res, val[x] + num);
}
for (auto to : g[x]) {
if (to == par) continue;
st.insert(mx[to]);
}
for (auto to : g[x]) {
if (to == par) continue;
st.erase(st.find(mx[to]));
dfs(to, x);
st.insert(mx[to]);
}
for (auto to : g[x]) {
if (to == par) continue;
st.erase(st.find(mx[to]));
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
int q, w;
cin >> q >> w;
g[q].push_back(w);
g[w].push_back(q);
}
dfs_calc(1);
dfs_calc1(1);
dfs(1);
if (res == -INF)
cout << "Impossible";
else
cout << 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 = (int)2e5 + 123;
const long long INF = (long long)1e18 + 123;
const int inf = (int)1e9 + 123;
const int MOD = (int)1e9 + 7;
long long val[N], mx[N], a[N];
int n;
vector<int> g[N];
void dfs_calc(int x, int par = -1) {
val[x] += a[x];
for (auto to : g[x]) {
if (to == par) continue;
dfs_calc(to, x);
val[x] += val[to];
}
}
void dfs_calc1(int x, int par = -1) {
mx[x] = val[x];
for (auto to : g[x]) {
if (to == par) continue;
dfs_calc1(to, x);
mx[x] = max(mx[x], mx[to]);
}
}
multiset<long long> st;
long long res = -INF;
void dfs(int x, int par = -1) {
long long num = 0;
if ((int)(st.size()) > 0) {
num = *st.rbegin();
res = max(res, val[x] + num);
}
for (auto to : g[x]) {
if (to == par) continue;
st.insert(mx[to]);
}
for (auto to : g[x]) {
if (to == par) continue;
st.erase(st.find(mx[to]));
dfs(to, x);
st.insert(mx[to]);
}
for (auto to : g[x]) {
if (to == par) continue;
st.erase(st.find(mx[to]));
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
int q, w;
cin >> q >> w;
g[q].push_back(w);
g[w].push_back(q);
}
dfs_calc(1);
dfs_calc1(1);
dfs(1);
if (res == -INF)
cout << "Impossible";
else
cout << res;
return 0;
}
```
|
#include <bits/stdc++.h>
const long long INF = (long long)1e18 + 123;
const long long inf = (long long)1e15 + 123;
const int mod = 1e9 + 7;
using namespace std;
vector<int> g[200011];
bool u[200011];
int n, d[200011];
long long a[200011];
long long sub[200011], mx[200011], ans = -INF;
void dfs(int v = 1, int par = 0) {
d[v] = 1;
sub[v] = a[v];
mx[v] = -INF * 2;
long long mx1, mx2;
mx1 = mx2 = -INF * 2;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (to == par || u[to]) continue;
dfs(to, v);
if (mx[to] > mx1) {
mx2 = mx1;
mx1 = mx[to];
} else if (mx[to] > mx2)
mx2 = mx[to];
d[v] += d[to];
sub[v] += sub[to];
mx[v] = max(mx[v], mx[to]);
}
ans = max(ans, mx1 + mx2);
mx[v] = max(mx[v], sub[v]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
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, 0);
if (ans <= -inf)
cout << "Impossible";
else
cout << 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>
const long long INF = (long long)1e18 + 123;
const long long inf = (long long)1e15 + 123;
const int mod = 1e9 + 7;
using namespace std;
vector<int> g[200011];
bool u[200011];
int n, d[200011];
long long a[200011];
long long sub[200011], mx[200011], ans = -INF;
void dfs(int v = 1, int par = 0) {
d[v] = 1;
sub[v] = a[v];
mx[v] = -INF * 2;
long long mx1, mx2;
mx1 = mx2 = -INF * 2;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (to == par || u[to]) continue;
dfs(to, v);
if (mx[to] > mx1) {
mx2 = mx1;
mx1 = mx[to];
} else if (mx[to] > mx2)
mx2 = mx[to];
d[v] += d[to];
sub[v] += sub[to];
mx[v] = max(mx[v], mx[to]);
}
ans = max(ans, mx1 + mx2);
mx[v] = max(mx[v], sub[v]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
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, 0);
if (ans <= -inf)
cout << "Impossible";
else
cout << ans;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[200000];
int n;
vector<int> a;
long long ones[200000];
long long twos[200000];
long long subsum[200000];
long long totans = LLONG_MIN;
void dfs(int no, int last) {
vector<int> chil;
for (int i = 0; i < (int((adj[no]).size())); ++i) {
int to = adj[no][i];
if (last == to) continue;
dfs(to, no);
chil.push_back(to);
subsum[no] += subsum[to];
}
subsum[no] += a[no];
if (int((chil).size()) == 0) {
ones[no] = a[no];
twos[no] = LLONG_MIN;
subsum[no] = a[no];
return;
}
long long bans = LLONG_MIN;
for (int i = 0; i < (int((chil).size())); ++i) {
bans = max(bans, ones[chil[i]]);
}
bans = max(bans, subsum[no]);
ones[no] = bans;
vector<long long> sorto;
for (int i = 0; i < (int((chil).size())); ++i) {
sorto.push_back(ones[chil[i]]);
}
sort((sorto).begin(), (sorto).end());
reverse((sorto).begin(), (sorto).end());
long long anso = LLONG_MIN;
if (int((sorto).size()) >= 2) anso = max(anso, sorto[0] + sorto[1]);
for (int i = 0; i < (int((chil).size())); ++i) {
anso = max(anso, twos[chil[i]]);
}
twos[no] = anso;
totans = max(totans, anso);
}
int main() {
cin >> n;
for (int i = 0; i < (n); ++i) {
int nxt;
cin >> nxt;
a.push_back(nxt);
}
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);
for (int i = 0; i < (n); ++i) {
}
if (totans == LLONG_MIN)
cout << "Impossible" << endl;
else
cout << totans << endl;
}
|
### 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> adj[200000];
int n;
vector<int> a;
long long ones[200000];
long long twos[200000];
long long subsum[200000];
long long totans = LLONG_MIN;
void dfs(int no, int last) {
vector<int> chil;
for (int i = 0; i < (int((adj[no]).size())); ++i) {
int to = adj[no][i];
if (last == to) continue;
dfs(to, no);
chil.push_back(to);
subsum[no] += subsum[to];
}
subsum[no] += a[no];
if (int((chil).size()) == 0) {
ones[no] = a[no];
twos[no] = LLONG_MIN;
subsum[no] = a[no];
return;
}
long long bans = LLONG_MIN;
for (int i = 0; i < (int((chil).size())); ++i) {
bans = max(bans, ones[chil[i]]);
}
bans = max(bans, subsum[no]);
ones[no] = bans;
vector<long long> sorto;
for (int i = 0; i < (int((chil).size())); ++i) {
sorto.push_back(ones[chil[i]]);
}
sort((sorto).begin(), (sorto).end());
reverse((sorto).begin(), (sorto).end());
long long anso = LLONG_MIN;
if (int((sorto).size()) >= 2) anso = max(anso, sorto[0] + sorto[1]);
for (int i = 0; i < (int((chil).size())); ++i) {
anso = max(anso, twos[chil[i]]);
}
twos[no] = anso;
totans = max(totans, anso);
}
int main() {
cin >> n;
for (int i = 0; i < (n); ++i) {
int nxt;
cin >> nxt;
a.push_back(nxt);
}
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);
for (int i = 0; i < (n); ++i) {
}
if (totans == LLONG_MIN)
cout << "Impossible" << endl;
else
cout << totans << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = (2e5) + 111;
long long vals[N];
long long dp[2][N];
long long sz[N];
vector<int> g[N];
int n;
bool ok = 0;
long long pre(int v, int p) {
long long &ans = sz[v];
ans = vals[v];
for (int to : g[v])
if (to != p) {
ans += pre(to, v);
}
return ans;
}
void dfs(int v, int p) {
dp[0][v] = sz[v];
vector<long long> opts;
for (int to : g[v])
if (to != p) {
dfs(to, v);
dp[0][v] = max(dp[0][v], dp[0][to]);
opts.push_back(dp[0][to]);
}
long long a, b = -(1LL << 55);
if (!opts.empty())
a = opts[0];
else
a = -(1LL << 55);
for (int i = 1; i < opts.size(); i++) {
if (opts[i] > a) {
b = a;
a = opts[i];
} else if (opts[i] > b) {
b = opts[i];
}
}
dp[1][v] = a + b;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> vals[i];
}
for (int i = 0; i + 1 < n; i++) {
int a, b;
cin >> a >> b;
a--, b--;
g[a].push_back(b);
g[b].push_back(a);
}
pre(0, -1);
dfs(0, -1);
long long ans = *max_element(dp[1], dp[1] + n);
if (ans > -100000000000000LL)
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;
const int N = (2e5) + 111;
long long vals[N];
long long dp[2][N];
long long sz[N];
vector<int> g[N];
int n;
bool ok = 0;
long long pre(int v, int p) {
long long &ans = sz[v];
ans = vals[v];
for (int to : g[v])
if (to != p) {
ans += pre(to, v);
}
return ans;
}
void dfs(int v, int p) {
dp[0][v] = sz[v];
vector<long long> opts;
for (int to : g[v])
if (to != p) {
dfs(to, v);
dp[0][v] = max(dp[0][v], dp[0][to]);
opts.push_back(dp[0][to]);
}
long long a, b = -(1LL << 55);
if (!opts.empty())
a = opts[0];
else
a = -(1LL << 55);
for (int i = 1; i < opts.size(); i++) {
if (opts[i] > a) {
b = a;
a = opts[i];
} else if (opts[i] > b) {
b = opts[i];
}
}
dp[1][v] = a + b;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> vals[i];
}
for (int i = 0; i + 1 < n; i++) {
int a, b;
cin >> a >> b;
a--, b--;
g[a].push_back(b);
g[b].push_back(a);
}
pre(0, -1);
dfs(0, -1);
long long ans = *max_element(dp[1], dp[1] + n);
if (ans > -100000000000000LL)
cout << ans << '\n';
else
cout << "Impossible\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int dx[8] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};
vector<long long> adj[200010], val(300010, -1), vis(300010, 0), dis(300010, -1);
long long sum[200010], dp[200010];
int myrandom(int i) { return std::rand() % i; }
bool ok = false;
long long dfs(long long a) {
vis[a] = 1;
long long max1 = -(1ll << 60);
sum[a] = val[a];
int cnt = 0;
multiset<long long, greater<long long>> st;
for (auto i : adj[a]) {
if (vis[i] == 0) {
cnt++;
long long num = dfs(i);
max1 = max(max1, num);
st.insert(num);
sum[a] = sum[a] + sum[i];
}
}
if (cnt > 1) {
ok = true;
auto itr = st.begin();
dp[a] = *itr;
itr++;
dp[a] += *itr;
}
return max(max1, sum[a]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
memset(sum, 0, sizeof(sum));
for (int i = 1; i <= n; i++) {
cin >> val[i];
dp[i] = -(1ll << 60);
}
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1);
long long maxi = -(1ll << 60);
if (ok) {
for (int i = 1; i <= n; i++) {
maxi = max(maxi, dp[i]);
}
cout << maxi << '\n';
} else {
cout << "Impossible" << '\n';
}
}
|
### 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;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int dx[8] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};
vector<long long> adj[200010], val(300010, -1), vis(300010, 0), dis(300010, -1);
long long sum[200010], dp[200010];
int myrandom(int i) { return std::rand() % i; }
bool ok = false;
long long dfs(long long a) {
vis[a] = 1;
long long max1 = -(1ll << 60);
sum[a] = val[a];
int cnt = 0;
multiset<long long, greater<long long>> st;
for (auto i : adj[a]) {
if (vis[i] == 0) {
cnt++;
long long num = dfs(i);
max1 = max(max1, num);
st.insert(num);
sum[a] = sum[a] + sum[i];
}
}
if (cnt > 1) {
ok = true;
auto itr = st.begin();
dp[a] = *itr;
itr++;
dp[a] += *itr;
}
return max(max1, sum[a]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
memset(sum, 0, sizeof(sum));
for (int i = 1; i <= n; i++) {
cin >> val[i];
dp[i] = -(1ll << 60);
}
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1);
long long maxi = -(1ll << 60);
if (ok) {
for (int i = 1; i <= n; i++) {
maxi = max(maxi, dp[i]);
}
cout << maxi << '\n';
} else {
cout << "Impossible" << '\n';
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 50;
int a[N];
vector<int> adj[N];
vector<int> child[N];
long long sum[N];
long long mx[N];
long long ans = LLONG_MIN;
void dfs1(int cur, int par) {
long long summ = a[cur];
bool leaf = true;
for (auto nxt : adj[cur]) {
if (nxt == par) {
} else {
leaf = false;
child[cur].push_back(nxt);
dfs1(nxt, cur);
summ += sum[nxt];
}
}
if (leaf) {
sum[cur] = a[cur];
} else {
sum[cur] = summ;
}
}
void dfs2(int cur, int par) {
long long mxx = sum[cur];
bool leaf = true;
for (auto nxt : adj[cur]) {
if (nxt == par) {
} else {
leaf = false;
dfs2(nxt, cur);
mxx = max(mxx, mx[nxt]);
}
}
if (leaf == true) {
mx[cur] = sum[cur];
} else {
mx[cur] = mxx;
}
}
void dfs3(int cur) {
if (child[cur].size() == 0) {
return;
} else if (child[cur].size() == 1) {
int nxt = child[cur][0];
dfs3(nxt);
} else {
vector<pair<long long, int> > bc;
for (auto nxt : child[cur]) {
bc.push_back({mx[nxt], nxt});
}
sort(bc.begin(), bc.end());
int last = bc[(int)bc.size() - 1].second;
int last_two = bc[(int)bc.size() - 2].second;
for (auto nxt : child[cur]) {
if (nxt != last) {
ans = max(ans, mx[nxt] + mx[last]);
} else {
ans = max(ans, mx[nxt] + mx[last_two]);
}
}
for (auto nxt : child[cur]) {
dfs3(nxt);
}
}
}
int main() {
ios::sync_with_stdio(false);
int n, m;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
mx[i] = LLONG_MIN;
}
int x, y;
for (int i = 1; i < n; i++) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs1(1, -1);
dfs2(1, -1);
dfs3(1);
if (ans == LLONG_MIN) {
cout << "Impossible";
} else
cout << ans;
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;
const int N = 200000 + 50;
int a[N];
vector<int> adj[N];
vector<int> child[N];
long long sum[N];
long long mx[N];
long long ans = LLONG_MIN;
void dfs1(int cur, int par) {
long long summ = a[cur];
bool leaf = true;
for (auto nxt : adj[cur]) {
if (nxt == par) {
} else {
leaf = false;
child[cur].push_back(nxt);
dfs1(nxt, cur);
summ += sum[nxt];
}
}
if (leaf) {
sum[cur] = a[cur];
} else {
sum[cur] = summ;
}
}
void dfs2(int cur, int par) {
long long mxx = sum[cur];
bool leaf = true;
for (auto nxt : adj[cur]) {
if (nxt == par) {
} else {
leaf = false;
dfs2(nxt, cur);
mxx = max(mxx, mx[nxt]);
}
}
if (leaf == true) {
mx[cur] = sum[cur];
} else {
mx[cur] = mxx;
}
}
void dfs3(int cur) {
if (child[cur].size() == 0) {
return;
} else if (child[cur].size() == 1) {
int nxt = child[cur][0];
dfs3(nxt);
} else {
vector<pair<long long, int> > bc;
for (auto nxt : child[cur]) {
bc.push_back({mx[nxt], nxt});
}
sort(bc.begin(), bc.end());
int last = bc[(int)bc.size() - 1].second;
int last_two = bc[(int)bc.size() - 2].second;
for (auto nxt : child[cur]) {
if (nxt != last) {
ans = max(ans, mx[nxt] + mx[last]);
} else {
ans = max(ans, mx[nxt] + mx[last_two]);
}
}
for (auto nxt : child[cur]) {
dfs3(nxt);
}
}
}
int main() {
ios::sync_with_stdio(false);
int n, m;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
mx[i] = LLONG_MIN;
}
int x, y;
for (int i = 1; i < n; i++) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs1(1, -1);
dfs2(1, -1);
dfs3(1);
if (ans == LLONG_MIN) {
cout << "Impossible";
} else
cout << ans;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e16 + 7;
long long n, ans = -inf;
int a[200001];
long long sum[200001], maxx[200001];
bool vis[200001];
vector<vector<int> > lst;
void dfs(int i) {
long long ans1 = -inf, ans2 = -inf;
vis[i] = 1;
for (int j = 0; j < lst[i].size(); j++) {
if (!vis[lst[i][j]]) {
dfs(lst[i][j]);
sum[i] += sum[lst[i][j]];
maxx[i] = max(maxx[i], max(maxx[lst[i][j]], sum[lst[i][j]]));
if (ans1 < max(maxx[lst[i][j]], sum[lst[i][j]])) {
ans2 = ans1;
ans1 = max(maxx[lst[i][j]], sum[lst[i][j]]);
} else if (ans2 < max(maxx[lst[i][j]], sum[lst[i][j]])) {
ans2 = max(maxx[lst[i][j]], sum[lst[i][j]]);
}
}
}
if (ans1 + ans2 > ans && ans1 != -inf && ans2 != -inf) {
ans = ans1 + ans2;
}
}
int main() {
cin >> n;
lst.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
sum[i] = a[i];
maxx[i] = -inf;
}
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
lst[a - 1].push_back(b - 1);
lst[b - 1].push_back(a - 1);
}
dfs(0);
if (ans == -inf) {
cout << "Impossible";
return 0;
}
cout << ans << endl;
}
|
### 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 long long inf = 1e16 + 7;
long long n, ans = -inf;
int a[200001];
long long sum[200001], maxx[200001];
bool vis[200001];
vector<vector<int> > lst;
void dfs(int i) {
long long ans1 = -inf, ans2 = -inf;
vis[i] = 1;
for (int j = 0; j < lst[i].size(); j++) {
if (!vis[lst[i][j]]) {
dfs(lst[i][j]);
sum[i] += sum[lst[i][j]];
maxx[i] = max(maxx[i], max(maxx[lst[i][j]], sum[lst[i][j]]));
if (ans1 < max(maxx[lst[i][j]], sum[lst[i][j]])) {
ans2 = ans1;
ans1 = max(maxx[lst[i][j]], sum[lst[i][j]]);
} else if (ans2 < max(maxx[lst[i][j]], sum[lst[i][j]])) {
ans2 = max(maxx[lst[i][j]], sum[lst[i][j]]);
}
}
}
if (ans1 + ans2 > ans && ans1 != -inf && ans2 != -inf) {
ans = ans1 + ans2;
}
}
int main() {
cin >> n;
lst.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
sum[i] = a[i];
maxx[i] = -inf;
}
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
lst[a - 1].push_back(b - 1);
lst[b - 1].push_back(a - 1);
}
dfs(0);
if (ans == -inf) {
cout << "Impossible";
return 0;
}
cout << ans << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
void __print(int 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) {
int 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;
long long a[N];
vector<long long> v[N];
long long dp[N][2];
long long mx = -1e18;
void dfs(long long s, long long par = 0) {
for (long long it : v[s]) {
if (it == par) continue;
dfs(it, s);
}
dp[s][0] = a[s];
vector<long long> ans;
for (long long it : v[s]) {
if (it == par) continue;
dp[s][0] += dp[it][0];
ans.push_back(dp[it][1]);
}
dp[s][1] = dp[s][0];
sort(ans.rbegin(), ans.rend());
if (ans.size() > 1) {
mx = max(mx, ans[0] + ans[1]);
}
for (long long it : v[s]) {
if (it == par) continue;
dp[s][1] = max(dp[s][1], dp[it][1]);
}
}
void solve(long long cas) {
long long n;
cin >> n;
for (long long i = 1; i <= n; ++i) cin >> a[i];
for (long long i = 1; i < n; ++i) {
long long x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1);
if (mx == -1e18)
cout << "Impossible\n";
else
cout << mx << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
;
long long t = 1;
long long cas = 0;
while (t--) solve(++cas);
}
|
### 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;
void __print(int 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) {
int 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;
long long a[N];
vector<long long> v[N];
long long dp[N][2];
long long mx = -1e18;
void dfs(long long s, long long par = 0) {
for (long long it : v[s]) {
if (it == par) continue;
dfs(it, s);
}
dp[s][0] = a[s];
vector<long long> ans;
for (long long it : v[s]) {
if (it == par) continue;
dp[s][0] += dp[it][0];
ans.push_back(dp[it][1]);
}
dp[s][1] = dp[s][0];
sort(ans.rbegin(), ans.rend());
if (ans.size() > 1) {
mx = max(mx, ans[0] + ans[1]);
}
for (long long it : v[s]) {
if (it == par) continue;
dp[s][1] = max(dp[s][1], dp[it][1]);
}
}
void solve(long long cas) {
long long n;
cin >> n;
for (long long i = 1; i <= n; ++i) cin >> a[i];
for (long long i = 1; i < n; ++i) {
long long x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1);
if (mx == -1e18)
cout << "Impossible\n";
else
cout << mx << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
;
long long t = 1;
long long cas = 0;
while (t--) solve(++cas);
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
inline std::ostream& operator<<(std::ostream& os, const vector<T>& v) {
bool first = true;
os << "[";
for (auto i : v) {
if (!first) os << ", ";
os << i;
first = false;
}
return os << "]";
}
template <typename T1, typename T2>
inline std::ostream& operator<<(std::ostream& os, const map<T1, T2>& v) {
bool first = true;
os << "[";
for (auto i : v) {
if (!first) os << ", ";
os << i;
first = false;
}
return os << "]";
}
template <typename T>
inline std::ostream& operator<<(std::ostream& os, const set<T>& v) {
bool first = true;
os << "[";
for (auto i : v) {
if (!first) os << ", ";
os << i;
first = false;
}
return os << "]";
}
struct debugger {
template <typename T>
debugger& operator,(const T& v) {
cerr << v << endl;
return *this;
}
} dbg;
int n;
vector<int> adj[200005];
int val[200005];
bool pos;
long long ans;
pair<long long, long long> dfs(int ind, int par = -1) {
long long sum = val[ind];
vector<long long> ch;
for (auto& e : adj[ind]) {
if (e != par) {
pair<long long, long long> p = dfs(e, ind);
sum += p.first;
ch.push_back(p.second);
}
}
sort((ch).begin(), (ch).end(), greater<long long>());
if ((int)(ch).size() >= 2) {
if (!pos) {
pos = true;
ans = ch[0] + ch[1];
}
ans = max(ans, ch[0] + ch[1]);
}
return pair<long long, long long>(
sum, max(sum, ((int)(ch).size() >= 1 ? ch[0] : sum)));
}
int main(int argc, char const* argv[]) {
cin >> n;
for (int i = 0; i < (n); ++i) cin >> val[i];
for (int j = 0; j < (n - 1); ++j) {
int x, y;
cin >> x >> y;
x--;
y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
pos = false;
dfs(0);
if (!pos) {
cout << "Impossible\n";
} 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;
template <typename T1, typename T2>
inline std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
inline std::ostream& operator<<(std::ostream& os, const vector<T>& v) {
bool first = true;
os << "[";
for (auto i : v) {
if (!first) os << ", ";
os << i;
first = false;
}
return os << "]";
}
template <typename T1, typename T2>
inline std::ostream& operator<<(std::ostream& os, const map<T1, T2>& v) {
bool first = true;
os << "[";
for (auto i : v) {
if (!first) os << ", ";
os << i;
first = false;
}
return os << "]";
}
template <typename T>
inline std::ostream& operator<<(std::ostream& os, const set<T>& v) {
bool first = true;
os << "[";
for (auto i : v) {
if (!first) os << ", ";
os << i;
first = false;
}
return os << "]";
}
struct debugger {
template <typename T>
debugger& operator,(const T& v) {
cerr << v << endl;
return *this;
}
} dbg;
int n;
vector<int> adj[200005];
int val[200005];
bool pos;
long long ans;
pair<long long, long long> dfs(int ind, int par = -1) {
long long sum = val[ind];
vector<long long> ch;
for (auto& e : adj[ind]) {
if (e != par) {
pair<long long, long long> p = dfs(e, ind);
sum += p.first;
ch.push_back(p.second);
}
}
sort((ch).begin(), (ch).end(), greater<long long>());
if ((int)(ch).size() >= 2) {
if (!pos) {
pos = true;
ans = ch[0] + ch[1];
}
ans = max(ans, ch[0] + ch[1]);
}
return pair<long long, long long>(
sum, max(sum, ((int)(ch).size() >= 1 ? ch[0] : sum)));
}
int main(int argc, char const* argv[]) {
cin >> n;
for (int i = 0; i < (n); ++i) cin >> val[i];
for (int j = 0; j < (n - 1); ++j) {
int x, y;
cin >> x >> y;
x--;
y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
pos = false;
dfs(0);
if (!pos) {
cout << "Impossible\n";
} else {
cout << ans << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int block_size = 320;
const long long mod = 1e9 + 7;
const long double eps = 1e-9;
const int inf = mod;
const long double PI = atan(1) * 4;
template <typename T>
inline int sign(const T &a) {
if (a < 0) return -1;
if (a > 0) return 1;
return 0;
}
template <typename T>
inline void in(T &x) {
x = 0;
T f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
x *= f;
}
const int maxn = 200010;
long long sum[maxn];
long long val[maxn];
long long maxsum[maxn];
vector<int> v[maxn];
long long ans = -1LL * inf * inf;
void dfs(int cur, int fa) {
sum[cur] = val[cur];
for (auto to : v[cur]) {
if (to == fa) continue;
dfs(to, cur);
sum[cur] += sum[to];
}
maxsum[cur] = sum[cur];
vector<long long> son;
for (auto to : v[cur]) {
if (to == fa) continue;
maxsum[cur] = max(maxsum[cur], maxsum[to]);
son.push_back(maxsum[to]);
}
sort(son.begin(), son.end());
reverse(son.begin(), son.end());
if (((int)son.size()) >= 2) {
ans = max(ans, son[0] + son[1]);
}
}
int main() {
int n;
cin >> n;
for (long long i = 1; i < n + 1; i++) {
in(val[i]);
}
for (long long i = 1; i < n; i++) {
int a, b;
in(a);
in(b);
v[a].push_back(b);
v[b].push_back(a);
}
dfs(1, 0);
if (ans == -1LL * inf * 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 int block_size = 320;
const long long mod = 1e9 + 7;
const long double eps = 1e-9;
const int inf = mod;
const long double PI = atan(1) * 4;
template <typename T>
inline int sign(const T &a) {
if (a < 0) return -1;
if (a > 0) return 1;
return 0;
}
template <typename T>
inline void in(T &x) {
x = 0;
T f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
x *= f;
}
const int maxn = 200010;
long long sum[maxn];
long long val[maxn];
long long maxsum[maxn];
vector<int> v[maxn];
long long ans = -1LL * inf * inf;
void dfs(int cur, int fa) {
sum[cur] = val[cur];
for (auto to : v[cur]) {
if (to == fa) continue;
dfs(to, cur);
sum[cur] += sum[to];
}
maxsum[cur] = sum[cur];
vector<long long> son;
for (auto to : v[cur]) {
if (to == fa) continue;
maxsum[cur] = max(maxsum[cur], maxsum[to]);
son.push_back(maxsum[to]);
}
sort(son.begin(), son.end());
reverse(son.begin(), son.end());
if (((int)son.size()) >= 2) {
ans = max(ans, son[0] + son[1]);
}
}
int main() {
int n;
cin >> n;
for (long long i = 1; i < n + 1; i++) {
in(val[i]);
}
for (long long i = 1; i < n; i++) {
int a, b;
in(a);
in(b);
v[a].push_back(b);
v[b].push_back(a);
}
dfs(1, 0);
if (ans == -1LL * inf * inf) {
cout << "Impossible";
} else {
cout << ans;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long a[200000], sm[200000], mx;
vector<unsigned int> tr[200000];
bool v[200000], z;
long long dfs(unsigned int x) {
v[x] = true;
unsigned int sz = tr[x].size(), c = 0;
long long s[2] = {LLONG_MIN / 2 + 1, LLONG_MIN / 2 + 1};
sm[x] = a[x];
for (unsigned int i = 0; i < sz; ++i) {
if (!v[tr[x][i]]) {
++c;
long long w = dfs(tr[x][i]);
if (w > s[0]) {
s[0] = w;
if (s[0] > s[1]) swap(s[0], s[1]);
}
sm[x] += sm[tr[x][i]];
}
}
if (c > 1) z = true;
if (z && s[0] + s[1] > mx) mx = s[0] + s[1];
return max(sm[x], max(s[0], s[1]));
}
int main(int argc, char* argv[]) {
unsigned int n;
cin >> n;
for (unsigned int i = 0; i < n; ++i) cin >> a[i];
for (unsigned int i = 1; i < n; ++i) {
unsigned int u, v;
cin >> u >> v;
--u, --v;
tr[u].push_back(v);
tr[v].push_back(u);
}
mx = LLONG_MIN;
dfs(0);
if (z)
cout << mx << endl;
else
cout << "Impossible\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;
long long a[200000], sm[200000], mx;
vector<unsigned int> tr[200000];
bool v[200000], z;
long long dfs(unsigned int x) {
v[x] = true;
unsigned int sz = tr[x].size(), c = 0;
long long s[2] = {LLONG_MIN / 2 + 1, LLONG_MIN / 2 + 1};
sm[x] = a[x];
for (unsigned int i = 0; i < sz; ++i) {
if (!v[tr[x][i]]) {
++c;
long long w = dfs(tr[x][i]);
if (w > s[0]) {
s[0] = w;
if (s[0] > s[1]) swap(s[0], s[1]);
}
sm[x] += sm[tr[x][i]];
}
}
if (c > 1) z = true;
if (z && s[0] + s[1] > mx) mx = s[0] + s[1];
return max(sm[x], max(s[0], s[1]));
}
int main(int argc, char* argv[]) {
unsigned int n;
cin >> n;
for (unsigned int i = 0; i < n; ++i) cin >> a[i];
for (unsigned int i = 1; i < n; ++i) {
unsigned int u, v;
cin >> u >> v;
--u, --v;
tr[u].push_back(v);
tr[v].push_back(u);
}
mx = LLONG_MIN;
dfs(0);
if (z)
cout << mx << endl;
else
cout << "Impossible\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[200001];
int vis[200001];
long long int res, a[200001];
pair<long long int, long long int> dfs(int i) {
vis[i] = 1;
if (adj[i].size() == 1 && i) return make_pair(a[i], a[i]);
int j;
long long int l, r, xsum, ysum;
pair<long long int, long long int> p;
l = r = LLONG_MIN;
xsum = 0;
for (j = 0; j < adj[i].size(); j++)
if (!vis[adj[i][j]]) {
p = dfs(adj[i][j]);
if (l == LLONG_MIN)
l = p.first;
else {
r = max(r, p.first);
if (l < r) swap(l, r);
}
xsum += p.second;
}
if (r == LLONG_MIN)
r = 0;
else
res = max(res, l + r);
return make_pair(max(l, a[i] + xsum), a[i] + xsum);
}
int main() {
int n, u, v, i;
cin >> n;
for (i = 0; i < n; i++) {
vis[i] = 0;
cin >> a[i];
}
for (i = 1; i < n; i++) {
cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
res = LLONG_MIN;
dfs(0);
if (res == LLONG_MIN)
cout << "Impossible";
else
cout << res;
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<int> adj[200001];
int vis[200001];
long long int res, a[200001];
pair<long long int, long long int> dfs(int i) {
vis[i] = 1;
if (adj[i].size() == 1 && i) return make_pair(a[i], a[i]);
int j;
long long int l, r, xsum, ysum;
pair<long long int, long long int> p;
l = r = LLONG_MIN;
xsum = 0;
for (j = 0; j < adj[i].size(); j++)
if (!vis[adj[i][j]]) {
p = dfs(adj[i][j]);
if (l == LLONG_MIN)
l = p.first;
else {
r = max(r, p.first);
if (l < r) swap(l, r);
}
xsum += p.second;
}
if (r == LLONG_MIN)
r = 0;
else
res = max(res, l + r);
return make_pair(max(l, a[i] + xsum), a[i] + xsum);
}
int main() {
int n, u, v, i;
cin >> n;
for (i = 0; i < n; i++) {
vis[i] = 0;
cin >> a[i];
}
for (i = 1; i < n; i++) {
cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
res = LLONG_MIN;
dfs(0);
if (res == LLONG_MIN)
cout << "Impossible";
else
cout << res;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v[200005];
long long s[200005], dp[200005], arr[200005];
void dfs(int node, int pnode) {
s[node] = arr[node];
for (int i = 0; i < v[node].size(); i++) {
if (v[node][i] != pnode)
dfs(v[node][i], node), s[node] += s[v[node][i]],
dp[node] = max(dp[node], dp[v[node][i]]);
}
dp[node] = max(dp[node], s[node]);
}
long long dfs2(int node, int pnode) {
long long ans = -(1LL << 62);
vector<long long> c;
for (int i = 0; i < v[node].size(); i++) {
if (v[node][i] != pnode) c.push_back(dp[v[node][i]]);
}
sort(c.begin(), c.end());
if (c.size() > 1) ans = max(ans, c[c.size() - 1] + c[c.size() - 2]);
for (int i = 0; i < v[node].size(); i++) {
if (v[node][i] != pnode) ans = max(ans, dfs2(v[node][i], node));
}
return ans;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> arr[i], dp[i] = -(1LL << 62);
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
dfs(1, 0);
long long ans = dfs2(1, 0);
if (ans != -(1LL << 62))
cout << ans;
else
cout << "Impossible";
}
|
### 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> v[200005];
long long s[200005], dp[200005], arr[200005];
void dfs(int node, int pnode) {
s[node] = arr[node];
for (int i = 0; i < v[node].size(); i++) {
if (v[node][i] != pnode)
dfs(v[node][i], node), s[node] += s[v[node][i]],
dp[node] = max(dp[node], dp[v[node][i]]);
}
dp[node] = max(dp[node], s[node]);
}
long long dfs2(int node, int pnode) {
long long ans = -(1LL << 62);
vector<long long> c;
for (int i = 0; i < v[node].size(); i++) {
if (v[node][i] != pnode) c.push_back(dp[v[node][i]]);
}
sort(c.begin(), c.end());
if (c.size() > 1) ans = max(ans, c[c.size() - 1] + c[c.size() - 2]);
for (int i = 0; i < v[node].size(); i++) {
if (v[node][i] != pnode) ans = max(ans, dfs2(v[node][i], node));
}
return ans;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> arr[i], dp[i] = -(1LL << 62);
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
dfs(1, 0);
long long ans = dfs2(1, 0);
if (ans != -(1LL << 62))
cout << ans;
else
cout << "Impossible";
}
```
|
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int id;
int a;
bool single;
long long sum;
long long m[2] = {0, 0};
Node *f = NULL;
vector<Node *> s;
Node(int id, int a) : id(id), a(a) {}
void dfs() {
for (int i = 0; i < s.size(); ++i) {
if (s[i] == f) {
s[i] = s[s.size() - 1];
s.pop_back();
}
s[i]->f = this;
}
sum = a;
single = true;
for (int i = 0; i < s.size(); ++i) {
s[i]->dfs();
sum += s[i]->sum;
if (single) {
if (i) {
single = false;
if (s[i]->single)
m[1] = m[0] + s[i]->m[0];
else
m[1] = max(m[0] + s[i]->m[0], s[i]->m[1]);
if (m[0] < s[i]->m[0]) {
m[0] = s[i]->m[0];
}
} else {
m[0] = s[i]->m[0];
if (!s[i]->single) {
single = false;
m[1] = s[i]->m[1];
}
}
} else {
if (!s[i]->single && m[1] < s[i]->m[1]) m[1] = s[i]->m[1];
if (m[1] < m[0] + s[i]->m[0]) m[1] = m[0] + s[i]->m[0];
if (m[0] < s[i]->m[0]) m[0] = s[i]->m[0];
}
}
if (s.empty() || m[0] < sum) m[0] = sum;
}
};
vector<Node *> nodes;
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
int a;
scanf("%d", &a);
nodes.push_back(new Node(i, a));
}
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d%d", &u, &v);
Node *a = nodes[u - 1];
Node *b = nodes[v - 1];
a->s.push_back(b);
b->s.push_back(a);
}
nodes[0]->dfs();
if (nodes[0]->single) {
printf("Impossible\n");
} else {
printf("%lld\n", nodes[0]->m[1]);
}
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;
class Node {
public:
int id;
int a;
bool single;
long long sum;
long long m[2] = {0, 0};
Node *f = NULL;
vector<Node *> s;
Node(int id, int a) : id(id), a(a) {}
void dfs() {
for (int i = 0; i < s.size(); ++i) {
if (s[i] == f) {
s[i] = s[s.size() - 1];
s.pop_back();
}
s[i]->f = this;
}
sum = a;
single = true;
for (int i = 0; i < s.size(); ++i) {
s[i]->dfs();
sum += s[i]->sum;
if (single) {
if (i) {
single = false;
if (s[i]->single)
m[1] = m[0] + s[i]->m[0];
else
m[1] = max(m[0] + s[i]->m[0], s[i]->m[1]);
if (m[0] < s[i]->m[0]) {
m[0] = s[i]->m[0];
}
} else {
m[0] = s[i]->m[0];
if (!s[i]->single) {
single = false;
m[1] = s[i]->m[1];
}
}
} else {
if (!s[i]->single && m[1] < s[i]->m[1]) m[1] = s[i]->m[1];
if (m[1] < m[0] + s[i]->m[0]) m[1] = m[0] + s[i]->m[0];
if (m[0] < s[i]->m[0]) m[0] = s[i]->m[0];
}
}
if (s.empty() || m[0] < sum) m[0] = sum;
}
};
vector<Node *> nodes;
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
int a;
scanf("%d", &a);
nodes.push_back(new Node(i, a));
}
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d%d", &u, &v);
Node *a = nodes[u - 1];
Node *b = nodes[v - 1];
a->s.push_back(b);
b->s.push_back(a);
}
nodes[0]->dfs();
if (nodes[0]->single) {
printf("Impossible\n");
} else {
printf("%lld\n", nodes[0]->m[1]);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int maxm = 2e5 + 10;
const long long mod = 998244353;
const int inf = 0x3ffffff;
const double eps = 1e-5;
long long a[maxn], res[maxn];
int n;
vector<long long> G[maxn], h[maxn];
long long maxnum = LLONG_MIN;
bool cmp(long long a, long long b) { return a > b; }
void dfs(int now, int fath) {
for (int i = 0; i < G[now].size(); i++) {
int v = G[now][i];
if (v != fath) {
dfs(v, now);
a[now] += a[v];
}
}
}
void dfs1(int now, int fath) {
for (int i = 0; i < G[now].size(); i++) {
int v = G[now][i];
if (v != fath) {
dfs1(v, now);
res[now] = max(res[v], res[now]);
h[now].push_back(res[v]);
}
}
sort(h[now].begin(), h[now].end(), cmp);
if (h[now].size() >= 2) maxnum = max(maxnum, h[now][0] + h[now][1]);
}
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];
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);
for (int i = 1; i <= n; i++) {
res[i] = a[i];
}
dfs1(1, 0);
if (maxnum == LLONG_MIN)
cout << "Impossible";
else
cout << maxnum;
return ~~(0 - 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 = 2e5 + 10;
const int maxm = 2e5 + 10;
const long long mod = 998244353;
const int inf = 0x3ffffff;
const double eps = 1e-5;
long long a[maxn], res[maxn];
int n;
vector<long long> G[maxn], h[maxn];
long long maxnum = LLONG_MIN;
bool cmp(long long a, long long b) { return a > b; }
void dfs(int now, int fath) {
for (int i = 0; i < G[now].size(); i++) {
int v = G[now][i];
if (v != fath) {
dfs(v, now);
a[now] += a[v];
}
}
}
void dfs1(int now, int fath) {
for (int i = 0; i < G[now].size(); i++) {
int v = G[now][i];
if (v != fath) {
dfs1(v, now);
res[now] = max(res[v], res[now]);
h[now].push_back(res[v]);
}
}
sort(h[now].begin(), h[now].end(), cmp);
if (h[now].size() >= 2) maxnum = max(maxnum, h[now][0] + h[now][1]);
}
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];
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);
for (int i = 1; i <= n; i++) {
res[i] = a[i];
}
dfs1(1, 0);
if (maxnum == LLONG_MIN)
cout << "Impossible";
else
cout << maxnum;
return ~~(0 - 0);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200000;
const long long INF = LLONG_MAX / 2LL;
vector<int> G[MAX_N];
int A[MAX_N];
long long T[MAX_N], DP[MAX_N], DP2[MAX_N];
void initGraph(int n) {
for (int i = 0; i < n; i++) G[i].clear();
}
void add(int u, int v) {
G[u].push_back(v);
G[v].push_back(u);
}
void dfs(int u, int p = -1) {
T[u] = A[u];
DP[u] = -INF;
DP2[u] = -INF;
vector<long long> vec;
for (auto v : G[u]) {
if (v == p) continue;
dfs(v, u);
T[u] += T[v];
DP[u] = max(DP[u], DP[v]);
vec.push_back(DP[v]);
}
DP[u] = max(DP[u], T[u]);
if (((int)(vec).size()) >= 2) {
for (int i = 0; i < 2; ++i)
for (int j = i + 1; j < ((int)(vec).size()); ++j)
if (vec[i] < vec[j]) swap(vec[i], vec[j]);
DP2[u] = vec[0] + vec[1];
}
}
int main() {
int n;
while (scanf("%d", &n) == 1) {
initGraph(n);
for (int i = 0; i < n; i++) scanf("%d", &A[i]);
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
add(u, v);
}
dfs(0);
long long ans = *max_element(DP2, DP2 + n);
if (ans == -INF)
puts("Impossible");
else
printf("%I64d\n", ans);
}
}
|
### 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 INF = LLONG_MAX / 2LL;
vector<int> G[MAX_N];
int A[MAX_N];
long long T[MAX_N], DP[MAX_N], DP2[MAX_N];
void initGraph(int n) {
for (int i = 0; i < n; i++) G[i].clear();
}
void add(int u, int v) {
G[u].push_back(v);
G[v].push_back(u);
}
void dfs(int u, int p = -1) {
T[u] = A[u];
DP[u] = -INF;
DP2[u] = -INF;
vector<long long> vec;
for (auto v : G[u]) {
if (v == p) continue;
dfs(v, u);
T[u] += T[v];
DP[u] = max(DP[u], DP[v]);
vec.push_back(DP[v]);
}
DP[u] = max(DP[u], T[u]);
if (((int)(vec).size()) >= 2) {
for (int i = 0; i < 2; ++i)
for (int j = i + 1; j < ((int)(vec).size()); ++j)
if (vec[i] < vec[j]) swap(vec[i], vec[j]);
DP2[u] = vec[0] + vec[1];
}
}
int main() {
int n;
while (scanf("%d", &n) == 1) {
initGraph(n);
for (int i = 0; i < n; i++) scanf("%d", &A[i]);
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
add(u, v);
}
dfs(0);
long long ans = *max_element(DP2, DP2 + n);
if (ans == -INF)
puts("Impossible");
else
printf("%I64d\n", ans);
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long sol[200000];
long long solve(int in, vector<int> v[], int visited[], int a[]) {
if (sol[in] != -2e16) {
return sol[in];
}
long long ans = a[in];
visited[in] = 1;
for (int i = 0; i < v[in].size(); i++) {
if (visited[v[in][i]] == 0) {
ans += solve(v[in][i], v, visited, a);
}
}
visited[in] = 0;
sol[in] = ans;
return ans;
}
long long max1[200000];
long long maxx(int in, vector<int> v[], int visited[], long long sol[]) {
if (max1[in] != -2e16) {
return max1[in];
}
long long ans = sol[in];
visited[in] = 1;
for (int i = 0; i < v[in].size(); i++) {
if (visited[v[in][i]] == 0) {
long long xx = maxx(v[in][i], v, visited, sol);
if (xx > ans) {
ans = xx;
}
}
}
visited[in] = 0;
max1[in] = ans;
return ans;
}
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> v[n];
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
v[x - 1].push_back(y - 1);
v[y - 1].push_back(x - 1);
}
int vis[n];
for (int i = 0; i < n; i++) {
vis[i] = 0;
}
for (int i = 0; i < 200000; i++) {
sol[i] = -2e16;
max1[i] = -2e16;
}
long long xx = solve(0, v, vis, a);
long long xy = maxx(0, v, vis, sol);
queue<int> q;
q.push(0);
long long ans = -2e16;
while (!q.empty()) {
int p = q.front();
q.pop();
vis[p] = 1;
priority_queue<long long> pq;
for (int i = 0; i < v[p].size(); i++) {
if (vis[v[p][i]] == 0) {
pq.push(max1[v[p][i]]);
q.push(v[p][i]);
}
}
if (pq.size() < 2) {
continue;
} else {
long long val = pq.top();
pq.pop();
val += pq.top();
if (val > ans) {
ans = val;
}
}
}
if (ans == -2e16) {
cout << "Impossible" << endl;
return 0;
}
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;
long long sol[200000];
long long solve(int in, vector<int> v[], int visited[], int a[]) {
if (sol[in] != -2e16) {
return sol[in];
}
long long ans = a[in];
visited[in] = 1;
for (int i = 0; i < v[in].size(); i++) {
if (visited[v[in][i]] == 0) {
ans += solve(v[in][i], v, visited, a);
}
}
visited[in] = 0;
sol[in] = ans;
return ans;
}
long long max1[200000];
long long maxx(int in, vector<int> v[], int visited[], long long sol[]) {
if (max1[in] != -2e16) {
return max1[in];
}
long long ans = sol[in];
visited[in] = 1;
for (int i = 0; i < v[in].size(); i++) {
if (visited[v[in][i]] == 0) {
long long xx = maxx(v[in][i], v, visited, sol);
if (xx > ans) {
ans = xx;
}
}
}
visited[in] = 0;
max1[in] = ans;
return ans;
}
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> v[n];
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
v[x - 1].push_back(y - 1);
v[y - 1].push_back(x - 1);
}
int vis[n];
for (int i = 0; i < n; i++) {
vis[i] = 0;
}
for (int i = 0; i < 200000; i++) {
sol[i] = -2e16;
max1[i] = -2e16;
}
long long xx = solve(0, v, vis, a);
long long xy = maxx(0, v, vis, sol);
queue<int> q;
q.push(0);
long long ans = -2e16;
while (!q.empty()) {
int p = q.front();
q.pop();
vis[p] = 1;
priority_queue<long long> pq;
for (int i = 0; i < v[p].size(); i++) {
if (vis[v[p][i]] == 0) {
pq.push(max1[v[p][i]]);
q.push(v[p][i]);
}
}
if (pq.size() < 2) {
continue;
} else {
long long val = pq.top();
pq.pop();
val += pq.top();
if (val > ans) {
ans = val;
}
}
}
if (ans == -2e16) {
cout << "Impossible" << endl;
return 0;
}
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
const int N = 2e5 + 10;
const int inf = 2 * 0x3f3f3f3f;
using namespace std;
long long num[N], dp[N], ans;
vector<int> vt[N];
void dfs(int u, int f) {
int i;
for (i = 0; i < vt[u].size(); i++) {
int v = vt[u][i];
if (v == f) continue;
dfs(v, u);
num[u] += num[v];
if (dp[u] != -inf) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], num[u]);
}
int main() {
int i, n;
long long a, b;
while (scanf("%d", &n) != EOF) {
ans = -inf;
for (i = 1; i <= n; i++) {
vt[i].clear();
dp[i] = -inf;
}
for (i = 1; i <= n; i++) scanf("%lld", &num[i]);
for (i = 1; i < n; i++) {
scanf("%lld %lld", &a, &b);
vt[a].push_back(b);
vt[b].push_back(a);
}
dfs(1, -1);
if (ans != -inf)
printf("%lld\n", ans);
else
printf("Impossible\n");
}
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>
const int N = 2e5 + 10;
const int inf = 2 * 0x3f3f3f3f;
using namespace std;
long long num[N], dp[N], ans;
vector<int> vt[N];
void dfs(int u, int f) {
int i;
for (i = 0; i < vt[u].size(); i++) {
int v = vt[u][i];
if (v == f) continue;
dfs(v, u);
num[u] += num[v];
if (dp[u] != -inf) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], num[u]);
}
int main() {
int i, n;
long long a, b;
while (scanf("%d", &n) != EOF) {
ans = -inf;
for (i = 1; i <= n; i++) {
vt[i].clear();
dp[i] = -inf;
}
for (i = 1; i <= n; i++) scanf("%lld", &num[i]);
for (i = 1; i < n; i++) {
scanf("%lld %lld", &a, &b);
vt[a].push_back(b);
vt[b].push_back(a);
}
dfs(1, -1);
if (ans != -inf)
printf("%lld\n", ans);
else
printf("Impossible\n");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAX_N = 200002;
const ll INF = -1999999999999;
ll n, m, u, v, ans = INF, ples[MAX_N], visit[MAX_N], pod[MAX_N], max_pod[MAX_N],
dp[MAX_N];
vector<vector<int> > G(MAX_N);
bool compare(const int &a, const int &b) { return max_pod[a] < max_pod[b]; }
void licz_poddrzewa(int x, int par) {
pod[x] = ples[x];
for (int i = 0; i < G[x].size(); i++) {
if (G[x][i] != par) {
licz_poddrzewa(G[x][i], x);
pod[x] += pod[G[x][i]];
}
}
}
void licz_max(int x, int par) {
max_pod[x] = pod[x];
for (int i = 0; i < G[x].size(); i++) {
if (G[x][i] != par) {
licz_max(G[x][i], x);
max_pod[x] = max(max_pod[x], max_pod[G[x][i]]);
}
}
}
void licz_dp(int x, int par) {
dp[x] = INF;
int rozmiar = G[x].size();
if (rozmiar >= 3 || (x == 1 && rozmiar >= 2)) {
sort(G[x].begin(), G[x].end(), compare);
dp[x] = max_pod[G[x][rozmiar - 1]] + max_pod[G[x][rozmiar - 2]];
if (G[x][rozmiar - 1] == par)
dp[x] = max_pod[G[x][rozmiar - 2]] + max_pod[G[x][rozmiar - 3]];
if (G[x][rozmiar - 2] == par)
dp[x] = max_pod[G[x][rozmiar - 1]] + max_pod[G[x][rozmiar - 3]];
}
for (int i = 0; i < rozmiar; i++)
if (G[x][i] != par) licz_dp(G[x][i], x);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> ples[i];
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
licz_poddrzewa(1, 0);
licz_max(1, 0);
licz_dp(1, 0);
for (int i = 1; i <= n; i++) ans = max(ans, dp[i]);
if (ans != INF)
cout << ans << endl;
else
cout << "Impossible" << endl;
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;
using ll = long long;
const int MAX_N = 200002;
const ll INF = -1999999999999;
ll n, m, u, v, ans = INF, ples[MAX_N], visit[MAX_N], pod[MAX_N], max_pod[MAX_N],
dp[MAX_N];
vector<vector<int> > G(MAX_N);
bool compare(const int &a, const int &b) { return max_pod[a] < max_pod[b]; }
void licz_poddrzewa(int x, int par) {
pod[x] = ples[x];
for (int i = 0; i < G[x].size(); i++) {
if (G[x][i] != par) {
licz_poddrzewa(G[x][i], x);
pod[x] += pod[G[x][i]];
}
}
}
void licz_max(int x, int par) {
max_pod[x] = pod[x];
for (int i = 0; i < G[x].size(); i++) {
if (G[x][i] != par) {
licz_max(G[x][i], x);
max_pod[x] = max(max_pod[x], max_pod[G[x][i]]);
}
}
}
void licz_dp(int x, int par) {
dp[x] = INF;
int rozmiar = G[x].size();
if (rozmiar >= 3 || (x == 1 && rozmiar >= 2)) {
sort(G[x].begin(), G[x].end(), compare);
dp[x] = max_pod[G[x][rozmiar - 1]] + max_pod[G[x][rozmiar - 2]];
if (G[x][rozmiar - 1] == par)
dp[x] = max_pod[G[x][rozmiar - 2]] + max_pod[G[x][rozmiar - 3]];
if (G[x][rozmiar - 2] == par)
dp[x] = max_pod[G[x][rozmiar - 1]] + max_pod[G[x][rozmiar - 3]];
}
for (int i = 0; i < rozmiar; i++)
if (G[x][i] != par) licz_dp(G[x][i], x);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> ples[i];
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
licz_poddrzewa(1, 0);
licz_max(1, 0);
licz_dp(1, 0);
for (int i = 1; i <= n; i++) ans = max(ans, dp[i]);
if (ans != INF)
cout << ans << endl;
else
cout << "Impossible" << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e16;
const int N = 2e5 + 10;
int n;
long long a[N], sum[N], mx[N], total = 0, ans = -INF;
vector<int> g[N];
void dfs(int u, int p) {
long long before = total;
vector<long long> childs;
long long mm = -INF;
for (int i = 0; i < ((int)g[u].size()); ++i) {
int v = g[u][i];
if (v != p) {
dfs(v, u);
childs.push_back(mx[v]);
mm = max(mm, mx[v]);
}
}
if (childs.size() > 1) {
sort(childs.begin(), childs.end());
long long x = childs[((int)childs.size()) - 1],
y = childs[((int)childs.size()) - 2];
ans = max(ans, x + y);
}
total += a[u];
sum[u] = total - before;
mm = max(mm, sum[u]);
mx[u] = mm;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n + 1; ++i) cin >> a[i];
int u, v;
for (int i = 0; i < n - 1; ++i) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, -1);
if (ans == -INF)
cout << "Impossible\n";
else
cout << ans << endl;
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;
const long long INF = 1e16;
const int N = 2e5 + 10;
int n;
long long a[N], sum[N], mx[N], total = 0, ans = -INF;
vector<int> g[N];
void dfs(int u, int p) {
long long before = total;
vector<long long> childs;
long long mm = -INF;
for (int i = 0; i < ((int)g[u].size()); ++i) {
int v = g[u][i];
if (v != p) {
dfs(v, u);
childs.push_back(mx[v]);
mm = max(mm, mx[v]);
}
}
if (childs.size() > 1) {
sort(childs.begin(), childs.end());
long long x = childs[((int)childs.size()) - 1],
y = childs[((int)childs.size()) - 2];
ans = max(ans, x + y);
}
total += a[u];
sum[u] = total - before;
mm = max(mm, sum[u]);
mx[u] = mm;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n + 1; ++i) cin >> a[i];
int u, v;
for (int i = 0; i < n - 1; ++i) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, -1);
if (ans == -INF)
cout << "Impossible\n";
else
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 10;
const long long inf = 1e18;
long long value[N], sumSub[N], subMx[N];
vector<long long> adj[N];
long long res;
void dfs(long long node, long long par) {
long long i = 0;
for (long long to : adj[node]) {
if (to == par) {
adj[node].erase(adj[node].begin() + i);
break;
}
++i;
}
subMx[node] = -inf;
sumSub[node] = value[node];
for (long long to : adj[node]) {
dfs(to, node);
sumSub[node] += sumSub[to];
subMx[node] = max(subMx[node], subMx[to]);
}
subMx[node] = max(subMx[node], sumSub[node]);
}
void dfs2(long long node, long long mx) {
vector<long long> prefixMx(adj[node].size(), -inf);
long long i = 0;
for (long long to : adj[node]) {
prefixMx[i] = max(prefixMx[i], subMx[to]);
if (i) {
prefixMx[i] = max(prefixMx[i], prefixMx[i - 1]);
}
++i;
}
vector<long long> suffixMx(adj[node].size(), -inf);
for (long long i = (long long)adj[node].size() - 1; i >= 0; --i) {
const long long to = adj[node][i];
suffixMx[i] = max(suffixMx[i], subMx[to]);
if (i + 1 < (long long)adj[node].size()) {
suffixMx[i] = max(suffixMx[i], suffixMx[i + 1]);
}
}
i = 0;
for (long long to : adj[node]) {
long long nextMx = mx;
if (i) nextMx = max(nextMx, prefixMx[i - 1]);
if (i + 1 < adj[node].size()) nextMx = max(nextMx, suffixMx[i + 1]);
res = max(res, subMx[to] + nextMx);
dfs2(to, nextMx);
++i;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
for (long long i = 1; i <= n; ++i) {
cin >> value[i];
}
for (long long i = 1; i < n; ++i) {
long long u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 1);
res = -inf;
dfs2(1, -inf);
if (res < -inf / 2LL) {
cout << "Impossible\n";
} else
cout << res << '\n';
}
|
### 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 = 2e5 + 10;
const long long inf = 1e18;
long long value[N], sumSub[N], subMx[N];
vector<long long> adj[N];
long long res;
void dfs(long long node, long long par) {
long long i = 0;
for (long long to : adj[node]) {
if (to == par) {
adj[node].erase(adj[node].begin() + i);
break;
}
++i;
}
subMx[node] = -inf;
sumSub[node] = value[node];
for (long long to : adj[node]) {
dfs(to, node);
sumSub[node] += sumSub[to];
subMx[node] = max(subMx[node], subMx[to]);
}
subMx[node] = max(subMx[node], sumSub[node]);
}
void dfs2(long long node, long long mx) {
vector<long long> prefixMx(adj[node].size(), -inf);
long long i = 0;
for (long long to : adj[node]) {
prefixMx[i] = max(prefixMx[i], subMx[to]);
if (i) {
prefixMx[i] = max(prefixMx[i], prefixMx[i - 1]);
}
++i;
}
vector<long long> suffixMx(adj[node].size(), -inf);
for (long long i = (long long)adj[node].size() - 1; i >= 0; --i) {
const long long to = adj[node][i];
suffixMx[i] = max(suffixMx[i], subMx[to]);
if (i + 1 < (long long)adj[node].size()) {
suffixMx[i] = max(suffixMx[i], suffixMx[i + 1]);
}
}
i = 0;
for (long long to : adj[node]) {
long long nextMx = mx;
if (i) nextMx = max(nextMx, prefixMx[i - 1]);
if (i + 1 < adj[node].size()) nextMx = max(nextMx, suffixMx[i + 1]);
res = max(res, subMx[to] + nextMx);
dfs2(to, nextMx);
++i;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
for (long long i = 1; i <= n; ++i) {
cin >> value[i];
}
for (long long i = 1; i < n; ++i) {
long long u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 1);
res = -inf;
dfs2(1, -inf);
if (res < -inf / 2LL) {
cout << "Impossible\n";
} else
cout << res << '\n';
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T1>
T1 GCD(T1 A, T1 B) {
if (B == 0)
return A;
else
return GCD(B, A % B);
}
template <class T2>
T2 mulmod(T2 a, T2 b) {
T2 x = 0, y = a % 1000000007;
while (b) {
if (b % 2 == 1) {
x = (x + y) % 1000000007;
}
y = (2 * y) % 1000000007;
b = b / 2;
}
return x % 1000000007;
}
template <class T3>
T3 power(T3 a, T3 b) {
T3 x = 1, y = a % 1000000007;
while (b) {
if (b % 2 == 1) {
x = mulmod(x, y) % 1000000007;
}
y = mulmod(y, y) % 1000000007;
b = b / 2;
}
return x % 1000000007;
}
template <class T4>
bool cmp(T4 x, T4 y) {
return x > y ? 1 : 0;
}
std::vector<int> adj[2 * 100000 + 10];
long long int cnt[2 * 100000 + 10], sz[2 * 100000 + 10], mx[2 * 100000 + 10];
long long int a[2 * 100000 + 10];
int order[2 * 100000 + 10];
int nn = 1;
bool flag;
void dfs(int u, int p) {
for (int i = 0; i < adj[u].size(); i++) {
if (adj[u][i] == p) continue;
dfs(adj[u][i], u);
cnt[u] += cnt[adj[u][i]];
sz[u] += sz[adj[u][i]];
}
cnt[u] += a[u];
sz[u]++;
order[nn] = u;
nn++;
}
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int x, y;
for (int i = 1; i < n; i++) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1, 0);
long long int ns = -1000000000000000;
mx[1] = a[order[1]];
for (int i = 2; i <= n; i++) {
if (cnt[order[i]] > mx[i - 1])
mx[i] = cnt[order[i]];
else
mx[i] = mx[i - 1];
}
for (int i = 2; i <= n; i++) {
if ((i - sz[order[i]]) != 0) {
flag = true;
ns = max(ns, cnt[order[i]] + mx[i - sz[order[i]]]);
}
}
if (!flag)
cout << "Impossible";
else
cout << ns;
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 T1>
T1 GCD(T1 A, T1 B) {
if (B == 0)
return A;
else
return GCD(B, A % B);
}
template <class T2>
T2 mulmod(T2 a, T2 b) {
T2 x = 0, y = a % 1000000007;
while (b) {
if (b % 2 == 1) {
x = (x + y) % 1000000007;
}
y = (2 * y) % 1000000007;
b = b / 2;
}
return x % 1000000007;
}
template <class T3>
T3 power(T3 a, T3 b) {
T3 x = 1, y = a % 1000000007;
while (b) {
if (b % 2 == 1) {
x = mulmod(x, y) % 1000000007;
}
y = mulmod(y, y) % 1000000007;
b = b / 2;
}
return x % 1000000007;
}
template <class T4>
bool cmp(T4 x, T4 y) {
return x > y ? 1 : 0;
}
std::vector<int> adj[2 * 100000 + 10];
long long int cnt[2 * 100000 + 10], sz[2 * 100000 + 10], mx[2 * 100000 + 10];
long long int a[2 * 100000 + 10];
int order[2 * 100000 + 10];
int nn = 1;
bool flag;
void dfs(int u, int p) {
for (int i = 0; i < adj[u].size(); i++) {
if (adj[u][i] == p) continue;
dfs(adj[u][i], u);
cnt[u] += cnt[adj[u][i]];
sz[u] += sz[adj[u][i]];
}
cnt[u] += a[u];
sz[u]++;
order[nn] = u;
nn++;
}
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int x, y;
for (int i = 1; i < n; i++) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1, 0);
long long int ns = -1000000000000000;
mx[1] = a[order[1]];
for (int i = 2; i <= n; i++) {
if (cnt[order[i]] > mx[i - 1])
mx[i] = cnt[order[i]];
else
mx[i] = mx[i - 1];
}
for (int i = 2; i <= n; i++) {
if ((i - sz[order[i]]) != 0) {
flag = true;
ns = max(ns, cnt[order[i]] + mx[i - sz[order[i]]]);
}
}
if (!flag)
cout << "Impossible";
else
cout << ns;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1e5 + 10;
long long a[N], n;
long long ans = 1e15;
vector<long long> g[N];
long long dp[N];
bool mark[N] = {0};
void dfs1(int u) {
mark[u] = 1;
for (int i = 0; i < g[u].size(); i++) {
if (mark[g[u][i]] == 0) {
dfs1(g[u][i]);
a[u] += a[g[u][i]];
dp[u] = max(dp[u], dp[g[u][i]]);
}
}
dp[u] = max(dp[u], a[u]);
}
void dfs(int u) {
mark[u] = 1;
vector<long long> x;
for (int i = 0; i < g[u].size(); i++)
if (mark[g[u][i]] == 0) {
dfs(g[u][i]);
x.push_back(dp[g[u][i]]);
}
if (x.size() > 1) {
sort(x.begin(), x.end());
ans = max(ans, x[x.size() - 1] + x[x.size() - 2]);
}
}
int main() {
ans = -ans;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
dp[i] = -1e15;
}
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);
}
memset(mark, 0, sizeof(mark));
dfs1(0);
memset(mark, 0, sizeof(mark));
dfs(0);
if (ans > -1e15)
cout << ans;
else
cout << "Impossible";
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;
const int N = 2 * 1e5 + 10;
long long a[N], n;
long long ans = 1e15;
vector<long long> g[N];
long long dp[N];
bool mark[N] = {0};
void dfs1(int u) {
mark[u] = 1;
for (int i = 0; i < g[u].size(); i++) {
if (mark[g[u][i]] == 0) {
dfs1(g[u][i]);
a[u] += a[g[u][i]];
dp[u] = max(dp[u], dp[g[u][i]]);
}
}
dp[u] = max(dp[u], a[u]);
}
void dfs(int u) {
mark[u] = 1;
vector<long long> x;
for (int i = 0; i < g[u].size(); i++)
if (mark[g[u][i]] == 0) {
dfs(g[u][i]);
x.push_back(dp[g[u][i]]);
}
if (x.size() > 1) {
sort(x.begin(), x.end());
ans = max(ans, x[x.size() - 1] + x[x.size() - 2]);
}
}
int main() {
ans = -ans;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
dp[i] = -1e15;
}
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);
}
memset(mark, 0, sizeof(mark));
dfs1(0);
memset(mark, 0, sizeof(mark));
dfs(0);
if (ans > -1e15)
cout << ans;
else
cout << "Impossible";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
const long long inf = 2e18;
vector<int> e[maxn];
int a[maxn];
long long b[maxn];
bool vis[maxn];
long long ans = -inf;
long long dfs(int u) {
vis[u] = true;
b[u] = a[u];
priority_queue<long long> q;
long long Max = -inf;
for (int i = 0; i < int(e[u].size()); i++) {
int v = e[u][i];
if (!vis[v]) {
long long t = dfs(v);
Max = max(Max, t);
q.push(t);
b[u] += b[v];
}
}
if (q.size() > 1) {
long long tmp = q.top();
q.pop();
tmp += q.top();
ans = max(ans, tmp);
}
return max(Max, b[u]);
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d%d", &u, &v);
e[u].push_back(v);
e[v].push_back(u);
}
dfs(1);
if (ans + inf != 0)
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 int maxn = 200010;
const long long inf = 2e18;
vector<int> e[maxn];
int a[maxn];
long long b[maxn];
bool vis[maxn];
long long ans = -inf;
long long dfs(int u) {
vis[u] = true;
b[u] = a[u];
priority_queue<long long> q;
long long Max = -inf;
for (int i = 0; i < int(e[u].size()); i++) {
int v = e[u][i];
if (!vis[v]) {
long long t = dfs(v);
Max = max(Max, t);
q.push(t);
b[u] += b[v];
}
}
if (q.size() > 1) {
long long tmp = q.top();
q.pop();
tmp += q.top();
ans = max(ans, tmp);
}
return max(Max, b[u]);
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d%d", &u, &v);
e[u].push_back(v);
e[v].push_back(u);
}
dfs(1);
if (ans + inf != 0)
printf("%lld\n", ans);
else
puts("Impossible");
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long v[200010];
vector<int> adj[200010];
long long mxs[200010], sumtree[200010];
void dfs(int x, int p) {
sumtree[x] = v[x];
mxs[x] = -1e9;
for (int i : adj[x]) {
if (i != p) {
dfs(i, x);
mxs[x] = max(mxs[i], mxs[x]);
sumtree[x] += sumtree[i];
}
}
mxs[x] = max(mxs[x], sumtree[x]);
}
long long ans = -1e16;
void dfsm(int x, int p, long long mx) {
long long mx1 = mx, mx2 = -1e17;
int it;
for (int i : adj[x]) {
if (i != p) {
if (mxs[i] > mx1)
mx2 = mx1, mx1 = mxs[i], it = i;
else if (mxs[i] > mx2)
mx2 = mxs[i];
}
}
ans = max(ans, mx1 + mx2);
for (int i : adj[x]) {
if (i - p) {
if (i == it) {
dfsm(i, x, mx2);
} else {
dfsm(i, x, mx1);
}
}
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> v[i];
}
for (int i = 0; i < n - 1; ++i) {
int x, y;
cin >> x >> y;
--x;
--y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(0, 0);
dfsm(0, 0, -1e17);
cout << (ans == -1e16 ? "Impossible" : to_string(ans)) << 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;
long long v[200010];
vector<int> adj[200010];
long long mxs[200010], sumtree[200010];
void dfs(int x, int p) {
sumtree[x] = v[x];
mxs[x] = -1e9;
for (int i : adj[x]) {
if (i != p) {
dfs(i, x);
mxs[x] = max(mxs[i], mxs[x]);
sumtree[x] += sumtree[i];
}
}
mxs[x] = max(mxs[x], sumtree[x]);
}
long long ans = -1e16;
void dfsm(int x, int p, long long mx) {
long long mx1 = mx, mx2 = -1e17;
int it;
for (int i : adj[x]) {
if (i != p) {
if (mxs[i] > mx1)
mx2 = mx1, mx1 = mxs[i], it = i;
else if (mxs[i] > mx2)
mx2 = mxs[i];
}
}
ans = max(ans, mx1 + mx2);
for (int i : adj[x]) {
if (i - p) {
if (i == it) {
dfsm(i, x, mx2);
} else {
dfsm(i, x, mx1);
}
}
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> v[i];
}
for (int i = 0; i < n - 1; ++i) {
int x, y;
cin >> x >> y;
--x;
--y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(0, 0);
dfsm(0, 0, -1e17);
cout << (ans == -1e16 ? "Impossible" : to_string(ans)) << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 500000;
const long long minx = -0x5fffffffffffffff;
struct nn {
long long x, y;
nn() { y = minx; }
};
int n, u, v;
nn num[N];
vector<int> node[N];
long long dfs(int f, int x) {
for (int i = 0; i < node[x].size(); i++) {
if (node[x][i] == f) continue;
long long r = dfs(x, node[x][i]);
num[x].y += r;
}
return num[x].y;
}
long long dfs1(int f, int x, long long &ans) {
long long m1 = minx;
long long m2 = minx;
for (int i = 0; i < node[x].size(); i++) {
if (node[x][i] == f) continue;
long long r = dfs1(x, node[x][i], ans);
if (m1 < r) {
m2 = m1;
m1 = r;
} else if (m2 < r) {
m2 = r;
}
}
if (m2 != minx) {
if (ans < m1 + m2) ans = m1 + m2;
}
return m1 > num[x].y ? m1 : num[x].y;
}
int main() {
long long ans;
while (scanf("%d", &n) != EOF) {
ans = minx;
for (int i = 0; i < N; i++) node[i].clear();
for (int i = 1; i <= n; i++) {
scanf("%lld", &num[i].x);
num[i].y = num[i].x;
}
for (int i = 0; i < n - 1; i++) {
scanf("%d%d", &u, &v);
node[u].push_back(v);
node[v].push_back(u);
}
dfs(0, 1);
dfs1(0, 1, ans);
if (ans != minx)
printf("%lld\n", ans);
else
printf("Impossible\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;
const int N = 500000;
const long long minx = -0x5fffffffffffffff;
struct nn {
long long x, y;
nn() { y = minx; }
};
int n, u, v;
nn num[N];
vector<int> node[N];
long long dfs(int f, int x) {
for (int i = 0; i < node[x].size(); i++) {
if (node[x][i] == f) continue;
long long r = dfs(x, node[x][i]);
num[x].y += r;
}
return num[x].y;
}
long long dfs1(int f, int x, long long &ans) {
long long m1 = minx;
long long m2 = minx;
for (int i = 0; i < node[x].size(); i++) {
if (node[x][i] == f) continue;
long long r = dfs1(x, node[x][i], ans);
if (m1 < r) {
m2 = m1;
m1 = r;
} else if (m2 < r) {
m2 = r;
}
}
if (m2 != minx) {
if (ans < m1 + m2) ans = m1 + m2;
}
return m1 > num[x].y ? m1 : num[x].y;
}
int main() {
long long ans;
while (scanf("%d", &n) != EOF) {
ans = minx;
for (int i = 0; i < N; i++) node[i].clear();
for (int i = 1; i <= n; i++) {
scanf("%lld", &num[i].x);
num[i].y = num[i].x;
}
for (int i = 0; i < n - 1; i++) {
scanf("%d%d", &u, &v);
node[u].push_back(v);
node[v].push_back(u);
}
dfs(0, 1);
dfs1(0, 1, ans);
if (ans != minx)
printf("%lld\n", ans);
else
printf("Impossible\n");
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long tree[4 * 200005];
long long sons[200005];
long long A[200005];
int in[200005], out[200005];
int TIME = 0;
int n;
vector<int> G[200005];
void dfs(int now, int par) {
int size = G[now].size();
TIME++;
in[now] = TIME;
sons[now] = A[now];
for (int i = 0; i < size; i++) {
int next = G[now][i];
if (next == par) continue;
dfs(next, now);
sons[now] += sons[next];
}
out[now] = TIME;
}
void update(int now, int l, int r, int pos, long long val) {
if (l == r) {
tree[now] = val;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid)
update(now << 1, l, mid, pos, val);
else
update(now << 1 | 1, mid + 1, r, pos, val);
tree[now] = max(tree[now << 1], tree[now << 1 | 1]);
}
long long query(int now, int l, int r, int a, int b) {
if (a > r || b < l) return -1e18;
if (a <= l && r <= b) return tree[now];
int mid = (l + r) >> 1;
long long ki = query(now << 1, l, mid, a, b);
long long ka = query(now << 1 | 1, mid + 1, r, a, b);
return max(ki, ka);
}
long long JAW = -1e18;
void go(int now, int par) {
int size = G[now].size();
update(1, 1, n, in[now], -1e18);
long long get = query(1, 1, n, 1, in[now] - 1);
get = max(get, query(1, 1, n, out[now] + 1, n));
if (get != -1e18) {
JAW = max(JAW, sons[now] + get);
}
for (int i = 0; i < size; i++) {
int next = G[now][i];
if (next == par) continue;
go(next, now);
}
update(1, 1, n, in[now], sons[now]);
}
int main() {
for (int i = 0; i < 4 * 200005; i++) tree[i] = -1e18;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &A[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++) update(1, 1, n, in[i], sons[i]);
dfs(1, -1);
go(1, -1);
if (JAW == -1e18)
printf("Impossible\n");
else
printf("%lld\n", JAW);
}
|
### 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 tree[4 * 200005];
long long sons[200005];
long long A[200005];
int in[200005], out[200005];
int TIME = 0;
int n;
vector<int> G[200005];
void dfs(int now, int par) {
int size = G[now].size();
TIME++;
in[now] = TIME;
sons[now] = A[now];
for (int i = 0; i < size; i++) {
int next = G[now][i];
if (next == par) continue;
dfs(next, now);
sons[now] += sons[next];
}
out[now] = TIME;
}
void update(int now, int l, int r, int pos, long long val) {
if (l == r) {
tree[now] = val;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid)
update(now << 1, l, mid, pos, val);
else
update(now << 1 | 1, mid + 1, r, pos, val);
tree[now] = max(tree[now << 1], tree[now << 1 | 1]);
}
long long query(int now, int l, int r, int a, int b) {
if (a > r || b < l) return -1e18;
if (a <= l && r <= b) return tree[now];
int mid = (l + r) >> 1;
long long ki = query(now << 1, l, mid, a, b);
long long ka = query(now << 1 | 1, mid + 1, r, a, b);
return max(ki, ka);
}
long long JAW = -1e18;
void go(int now, int par) {
int size = G[now].size();
update(1, 1, n, in[now], -1e18);
long long get = query(1, 1, n, 1, in[now] - 1);
get = max(get, query(1, 1, n, out[now] + 1, n));
if (get != -1e18) {
JAW = max(JAW, sons[now] + get);
}
for (int i = 0; i < size; i++) {
int next = G[now][i];
if (next == par) continue;
go(next, now);
}
update(1, 1, n, in[now], sons[now]);
}
int main() {
for (int i = 0; i < 4 * 200005; i++) tree[i] = -1e18;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &A[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++) update(1, 1, n, in[i], sons[i]);
dfs(1, -1);
go(1, -1);
if (JAW == -1e18)
printf("Impossible\n");
else
printf("%lld\n", JAW);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 0x7fffffff;
const long long mod = 1e9 + 7;
const double eps = 1e-8;
const long long linf = 1e18;
const long long MAXN = 2e5 + 10;
const long long MAXM = MAXN << 2;
const double E = 2.718281828;
const double pi = acos(-1.0);
struct node {
int to, next;
} edge[MAXN << 1];
int head[MAXN], tot;
void init() { memset(head, -1, sizeof(head)); }
long long a[MAXN];
long long sum[MAXN];
long long f[MAXN];
long long res;
void add(int u, int v) {
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void dfs1(int u, int fa) {
sum[u] = a[u];
for (int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if (v == fa) continue;
dfs1(v, u);
sum[u] += sum[v];
}
}
void dfs2(int u, int fa) {
f[u] = -linf;
for (int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if (v == fa) continue;
dfs2(v, u);
if (f[u] != -linf) res = max(res, f[u] + f[v]);
f[u] = max(f[u], f[v]);
}
f[u] = max(f[u], sum[u]);
}
int main() {
int n;
init();
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%I64d", &a[i]);
for (int i = 0; i < n - 1; ++i) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
dfs1(1, -1);
res = -linf;
dfs2(1, -1);
if (res == -linf) {
puts("Impossible");
return 0;
}
printf("%I64d\n", res);
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 inf = 0x7fffffff;
const long long mod = 1e9 + 7;
const double eps = 1e-8;
const long long linf = 1e18;
const long long MAXN = 2e5 + 10;
const long long MAXM = MAXN << 2;
const double E = 2.718281828;
const double pi = acos(-1.0);
struct node {
int to, next;
} edge[MAXN << 1];
int head[MAXN], tot;
void init() { memset(head, -1, sizeof(head)); }
long long a[MAXN];
long long sum[MAXN];
long long f[MAXN];
long long res;
void add(int u, int v) {
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void dfs1(int u, int fa) {
sum[u] = a[u];
for (int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if (v == fa) continue;
dfs1(v, u);
sum[u] += sum[v];
}
}
void dfs2(int u, int fa) {
f[u] = -linf;
for (int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if (v == fa) continue;
dfs2(v, u);
if (f[u] != -linf) res = max(res, f[u] + f[v]);
f[u] = max(f[u], f[v]);
}
f[u] = max(f[u], sum[u]);
}
int main() {
int n;
init();
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%I64d", &a[i]);
for (int i = 0; i < n - 1; ++i) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
dfs1(1, -1);
res = -linf;
dfs2(1, -1);
if (res == -linf) {
puts("Impossible");
return 0;
}
printf("%I64d\n", res);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7fffffff;
const long long INFF = 0x7fffffffffffffff;
const double pi = 3.141592653589793;
const double inf = 1e18;
const double eps = 1e-8;
const long long mod = 1e9 + 7;
struct edges {
int to;
int next;
} edge[1000005];
long long mx = -INFF;
int head[1000005];
int cnt = 0;
long long a[1000005];
long long value1[1000005];
void makeedge(int u, int v) {
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}
int vis[1000005];
long long value[1000005];
bool flag = false;
long long dfs(int u) {
vis[u] = 1;
value[u] = a[u];
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (!vis[v]) {
value[u] += dfs(v);
}
}
return value[u];
}
long long dfs2(int u) {
vis[u] = 1;
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (!vis[v]) {
value[u] = max(value[u], dfs2(v));
}
}
return value[u];
}
void bfs1(int u) {
queue<int> q;
q.push(1);
vis[1] = 1;
while (!q.empty()) {
int u = q.front();
vis[u] = 1;
q.pop();
int cnt1 = 0;
for (int i = head[u]; i != -1; i = edge[i].next) {
if (!vis[edge[i].to]) {
value1[cnt1++] = value[edge[i].to];
q.push(edge[i].to);
}
}
if (cnt1 >= 2) {
flag = true;
sort(value1, value1 + cnt1);
mx = max(mx, value1[cnt1 - 1] + value1[cnt1 - 2]);
}
}
}
int main() {
int n;
memset(head, -1, sizeof(head));
cin >> n;
for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
int u, v;
for (int i = 1; i < n; i++) {
scanf("%d %d", &u, &v);
makeedge(u, v);
makeedge(v, u);
}
memset(vis, 0, sizeof(vis));
memset(value, 0, sizeof(value));
dfs(1);
memset(vis, 0, sizeof(vis));
dfs2(1);
memset(vis, 0, sizeof(vis));
bfs1(1);
if (!flag)
cout << "Impossible" << endl;
else
cout << mx << endl;
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 INF = 0x7fffffff;
const long long INFF = 0x7fffffffffffffff;
const double pi = 3.141592653589793;
const double inf = 1e18;
const double eps = 1e-8;
const long long mod = 1e9 + 7;
struct edges {
int to;
int next;
} edge[1000005];
long long mx = -INFF;
int head[1000005];
int cnt = 0;
long long a[1000005];
long long value1[1000005];
void makeedge(int u, int v) {
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}
int vis[1000005];
long long value[1000005];
bool flag = false;
long long dfs(int u) {
vis[u] = 1;
value[u] = a[u];
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (!vis[v]) {
value[u] += dfs(v);
}
}
return value[u];
}
long long dfs2(int u) {
vis[u] = 1;
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (!vis[v]) {
value[u] = max(value[u], dfs2(v));
}
}
return value[u];
}
void bfs1(int u) {
queue<int> q;
q.push(1);
vis[1] = 1;
while (!q.empty()) {
int u = q.front();
vis[u] = 1;
q.pop();
int cnt1 = 0;
for (int i = head[u]; i != -1; i = edge[i].next) {
if (!vis[edge[i].to]) {
value1[cnt1++] = value[edge[i].to];
q.push(edge[i].to);
}
}
if (cnt1 >= 2) {
flag = true;
sort(value1, value1 + cnt1);
mx = max(mx, value1[cnt1 - 1] + value1[cnt1 - 2]);
}
}
}
int main() {
int n;
memset(head, -1, sizeof(head));
cin >> n;
for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
int u, v;
for (int i = 1; i < n; i++) {
scanf("%d %d", &u, &v);
makeedge(u, v);
makeedge(v, u);
}
memset(vis, 0, sizeof(vis));
memset(value, 0, sizeof(value));
dfs(1);
memset(vis, 0, sizeof(vis));
dfs2(1);
memset(vis, 0, sizeof(vis));
bfs1(1);
if (!flag)
cout << "Impossible" << endl;
else
cout << mx << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> a[200005];
long long s[200005] = {0}, maxs[200005], res = -1e15, n, b[200005];
void dfs(int p, int u) {
long long m1 = -1e15, m2 = -1e15;
maxs[u] = -1e15;
s[u] = b[u];
for (int i = 0; i < a[u].size(); ++i) {
int v = a[u][i];
if (v == p) continue;
dfs(u, v);
s[u] += s[v];
maxs[u] = max(maxs[u], maxs[v]);
if (m1 == -1e15)
m1 = maxs[v];
else if (m2 == -1e15)
m2 = maxs[v];
else
m2 = max(m2, maxs[v]);
if (m1 < m2) swap(m1, m2);
if (m2 > -1e15) res = max(res, m1 + m2);
}
maxs[u] = max(maxs[u], s[u]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> b[i];
int u, v;
for (int i = 1; i < n; ++i) {
cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
dfs(0, 1);
if (res > -1e15)
cout << res;
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;
vector<int> a[200005];
long long s[200005] = {0}, maxs[200005], res = -1e15, n, b[200005];
void dfs(int p, int u) {
long long m1 = -1e15, m2 = -1e15;
maxs[u] = -1e15;
s[u] = b[u];
for (int i = 0; i < a[u].size(); ++i) {
int v = a[u][i];
if (v == p) continue;
dfs(u, v);
s[u] += s[v];
maxs[u] = max(maxs[u], maxs[v]);
if (m1 == -1e15)
m1 = maxs[v];
else if (m2 == -1e15)
m2 = maxs[v];
else
m2 = max(m2, maxs[v]);
if (m1 < m2) swap(m1, m2);
if (m2 > -1e15) res = max(res, m1 + m2);
}
maxs[u] = max(maxs[u], s[u]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> b[i];
int u, v;
for (int i = 1; i < n; ++i) {
cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
dfs(0, 1);
if (res > -1e15)
cout << res;
else
cout << "Impossible";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
long long pop(priority_queue<long long> &pq) {
long long x = pq.top();
pq.pop();
return x;
}
void setmax(long long &a, long long b) {
if (a < b) {
a = b;
}
}
int N;
vector<int> adj[MAXN];
long long A[MAXN], sub[MAXN];
long long mxsub[MAXN];
long long ans = -1e15;
void dfs(int x, int p) {
sub[x] = A[x];
if (p) {
adj[x].erase(find(adj[x].begin(), adj[x].end(), p));
}
for (int y : adj[x]) {
dfs(y, x);
sub[x] += sub[y];
}
mxsub[x] = sub[x];
for (int y : adj[x]) {
setmax(mxsub[x], mxsub[y]);
}
priority_queue<long long> pq;
for (int y : adj[x]) {
pq.push(mxsub[y]);
}
if (pq.size() >= 2) {
ans = max(ans, pop(pq) + pop(pq));
}
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
scanf("%lld", &A[i]);
}
for (int i = 1; i < N; i++) {
int x, y;
scanf("%d %d", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1, 0);
if (ans == -1e15) {
puts("Impossible");
} else {
printf("%lld\n", ans);
}
}
|
### 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 = 2e5 + 10;
long long pop(priority_queue<long long> &pq) {
long long x = pq.top();
pq.pop();
return x;
}
void setmax(long long &a, long long b) {
if (a < b) {
a = b;
}
}
int N;
vector<int> adj[MAXN];
long long A[MAXN], sub[MAXN];
long long mxsub[MAXN];
long long ans = -1e15;
void dfs(int x, int p) {
sub[x] = A[x];
if (p) {
adj[x].erase(find(adj[x].begin(), adj[x].end(), p));
}
for (int y : adj[x]) {
dfs(y, x);
sub[x] += sub[y];
}
mxsub[x] = sub[x];
for (int y : adj[x]) {
setmax(mxsub[x], mxsub[y]);
}
priority_queue<long long> pq;
for (int y : adj[x]) {
pq.push(mxsub[y]);
}
if (pq.size() >= 2) {
ans = max(ans, pop(pq) + pop(pq));
}
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
scanf("%lld", &A[i]);
}
for (int i = 1; i < N; i++) {
int x, y;
scanf("%d %d", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1, 0);
if (ans == -1e15) {
puts("Impossible");
} else {
printf("%lld\n", ans);
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
priority_queue<long long int, vector<long long int>, greater<long long int> >
pq;
double startTime;
double getCurrentTime() {
return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
bool isPrime(long long int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long int power(long long int x, unsigned long long int y,
long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
const long long int maxn = 2e5 + 10;
vector<long long int> adj[maxn];
long long int dp[maxn];
void DFS1(long long int node, long long int par) {
for (auto ch : adj[node]) {
if (ch == par) continue;
DFS1(ch, node);
dp[node] += dp[ch];
}
}
long long int flag, ans = -1e18;
void DFS2(long long int node, long long int par) {
long long int mx1 = -1e18, mx2 = -1e18;
for (auto ch : adj[node]) {
if (ch == par) continue;
DFS2(ch, node);
if (mx1 <= dp[ch]) {
mx2 = mx1;
mx1 = dp[ch];
} else
mx2 = max(mx2, dp[ch]);
dp[node] = max(dp[node], dp[ch]);
}
if (mx1 != -1e18 && mx2 != -1e18) {
flag = 1;
ans = max(ans, mx1 + mx2);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n;
cin >> n;
for (long long int i = 1; i <= n; i++) cin >> dp[i];
for (long long int i = 0; i < n - 1; i++) {
long long int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
DFS1(1, -1);
DFS2(1, -1);
if (flag)
cout << ans;
else
cout << "Impossible";
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;
priority_queue<long long int, vector<long long int>, greater<long long int> >
pq;
double startTime;
double getCurrentTime() {
return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
bool isPrime(long long int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long int power(long long int x, unsigned long long int y,
long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
const long long int maxn = 2e5 + 10;
vector<long long int> adj[maxn];
long long int dp[maxn];
void DFS1(long long int node, long long int par) {
for (auto ch : adj[node]) {
if (ch == par) continue;
DFS1(ch, node);
dp[node] += dp[ch];
}
}
long long int flag, ans = -1e18;
void DFS2(long long int node, long long int par) {
long long int mx1 = -1e18, mx2 = -1e18;
for (auto ch : adj[node]) {
if (ch == par) continue;
DFS2(ch, node);
if (mx1 <= dp[ch]) {
mx2 = mx1;
mx1 = dp[ch];
} else
mx2 = max(mx2, dp[ch]);
dp[node] = max(dp[node], dp[ch]);
}
if (mx1 != -1e18 && mx2 != -1e18) {
flag = 1;
ans = max(ans, mx1 + mx2);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n;
cin >> n;
for (long long int i = 1; i <= n; i++) cin >> dp[i];
for (long long int i = 0; i < n - 1; i++) {
long long int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
DFS1(1, -1);
DFS2(1, -1);
if (flag)
cout << ans;
else
cout << "Impossible";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
vector<vector<int>> gr;
vector<long long> gifts;
vector<long long> sum;
vector<long long> maxsum;
long long dfs(int v = 0, int p = -1) {
sum[v] = gifts[v];
for (auto to : gr[v])
if (to != p) sum[v] += dfs(to, v);
return sum[v];
}
long long maxima(int v = 0, int p = -1) {
maxsum[v] = sum[v];
for (auto to : gr[v])
if (to != p) maxsum[v] = max(maxsum[v], maxima(to, v));
return maxsum[v];
}
pair<long long, long long> recount(int v = 0, int p = -1) {
pair<long long, long long> ans = {-INF, -INF};
for (auto &to : gr[v]) {
if (to == p) continue;
if (maxsum[to] > ans.first) {
ans.second = ans.first;
ans.first = maxsum[to];
} else if (maxsum[to] > ans.second) {
ans.second = maxsum[to];
}
}
for (auto &to : gr[v]) {
if (to == p) continue;
auto temp = recount(to, v);
if (temp.first + temp.second > ans.first + ans.second && temp.second > -INF)
ans = temp;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
gr.resize(n);
gifts.resize(n);
sum.resize(n);
maxsum.resize(n);
for (auto &item : gifts) cin >> item;
while (--n) {
int v, u;
cin >> v >> u;
gr[--v].push_back(--u);
gr[u].push_back(v);
}
dfs();
maxima();
auto ans = recount();
if (ans.second == -INF)
cout << "Impossible";
else
cout << ans.first + ans.second;
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;
vector<vector<int>> gr;
vector<long long> gifts;
vector<long long> sum;
vector<long long> maxsum;
long long dfs(int v = 0, int p = -1) {
sum[v] = gifts[v];
for (auto to : gr[v])
if (to != p) sum[v] += dfs(to, v);
return sum[v];
}
long long maxima(int v = 0, int p = -1) {
maxsum[v] = sum[v];
for (auto to : gr[v])
if (to != p) maxsum[v] = max(maxsum[v], maxima(to, v));
return maxsum[v];
}
pair<long long, long long> recount(int v = 0, int p = -1) {
pair<long long, long long> ans = {-INF, -INF};
for (auto &to : gr[v]) {
if (to == p) continue;
if (maxsum[to] > ans.first) {
ans.second = ans.first;
ans.first = maxsum[to];
} else if (maxsum[to] > ans.second) {
ans.second = maxsum[to];
}
}
for (auto &to : gr[v]) {
if (to == p) continue;
auto temp = recount(to, v);
if (temp.first + temp.second > ans.first + ans.second && temp.second > -INF)
ans = temp;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
gr.resize(n);
gifts.resize(n);
sum.resize(n);
maxsum.resize(n);
for (auto &item : gifts) cin >> item;
while (--n) {
int v, u;
cin >> v >> u;
gr[--v].push_back(--u);
gr[u].push_back(v);
}
dfs();
maxima();
auto ans = recount();
if (ans.second == -INF)
cout << "Impossible";
else
cout << ans.first + ans.second;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, a[N];
vector<int> edge[N];
vector<long long> subList[N];
long long M[N], res = LLONG_MIN;
void dfs1(int u, int par) {
M[u] = a[u];
for (int i = 0; i < edge[u].size(); i++) {
if (edge[u][i] != par) {
dfs1(edge[u][i], u);
M[u] += M[edge[u][i]];
}
}
}
void dfs2(int u, int par) {
for (int i = 0; i < edge[u].size(); i++) {
if (edge[u][i] != par) {
dfs2(edge[u][i], u);
M[u] = max(M[u], M[edge[u][i]]);
subList[u].push_back(M[edge[u][i]]);
if (subList[u].size() > 2) {
sort(subList[u].begin(), subList[u].end(), greater<long long>());
subList[u].erase(subList[u].begin() + 2);
}
}
}
if (subList[u].size() == 2) {
res = max(res, subList[u][0] + subList[u][1]);
}
}
void init() {
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);
edge[u].push_back(v);
edge[v].push_back(u);
}
dfs1(1, 0);
dfs2(1, 0);
if (res == LLONG_MIN)
puts("Impossible");
else
printf("%lld\n", res);
}
int main() {
init();
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;
int n, a[N];
vector<int> edge[N];
vector<long long> subList[N];
long long M[N], res = LLONG_MIN;
void dfs1(int u, int par) {
M[u] = a[u];
for (int i = 0; i < edge[u].size(); i++) {
if (edge[u][i] != par) {
dfs1(edge[u][i], u);
M[u] += M[edge[u][i]];
}
}
}
void dfs2(int u, int par) {
for (int i = 0; i < edge[u].size(); i++) {
if (edge[u][i] != par) {
dfs2(edge[u][i], u);
M[u] = max(M[u], M[edge[u][i]]);
subList[u].push_back(M[edge[u][i]]);
if (subList[u].size() > 2) {
sort(subList[u].begin(), subList[u].end(), greater<long long>());
subList[u].erase(subList[u].begin() + 2);
}
}
}
if (subList[u].size() == 2) {
res = max(res, subList[u][0] + subList[u][1]);
}
}
void init() {
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);
edge[u].push_back(v);
edge[v].push_back(u);
}
dfs1(1, 0);
dfs2(1, 0);
if (res == LLONG_MIN)
puts("Impossible");
else
printf("%lld\n", res);
}
int main() {
init();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct line {
int s, t;
int next;
} a[400001];
int head[200001];
int edge;
inline void add(int s, int t) {
a[edge].next = head[s];
head[s] = edge;
a[edge].s = s;
a[edge].t = t;
}
int fa[200001];
long long sum[200001];
long long maxx[200001];
long long val[200001];
long long ans = -(1000000000LL * 1000000000LL);
inline void dfs(int d) {
int i;
long long max1 = -(1000000000LL * 1000000000LL),
max2 = -(1000000000LL * 1000000000LL);
maxx[d] = -(1000000000LL * 1000000000LL);
for (i = head[d]; i != 0; i = a[i].next) {
int t = a[i].t;
if (t != fa[d]) {
fa[t] = d;
dfs(t);
maxx[d] = max(maxx[d], maxx[t]);
if (maxx[t] > max1) {
max2 = max1;
max1 = maxx[t];
} else if (maxx[t] > max2)
max2 = maxx[t];
sum[d] += sum[t];
}
}
sum[d] += val[d];
maxx[d] = max(sum[d], maxx[d]);
if (max1 != -(1000000000LL * 1000000000LL) &&
max2 != -(1000000000LL * 1000000000LL))
ans = max(ans, max1 + max2);
}
int main(void) {
int n;
scanf("%d", &n);
int i;
for (i = 1; i <= n; i++) scanf("%I64d", &val[i]);
int s, t;
for (i = 1; i <= n - 1; i++) {
scanf("%d%d", &s, &t);
edge++;
add(s, t);
edge++;
add(t, s);
}
dfs(1);
if (ans != -(1000000000LL * 1000000000LL))
printf("%I64d\n", ans);
else
printf("Impossible\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;
struct line {
int s, t;
int next;
} a[400001];
int head[200001];
int edge;
inline void add(int s, int t) {
a[edge].next = head[s];
head[s] = edge;
a[edge].s = s;
a[edge].t = t;
}
int fa[200001];
long long sum[200001];
long long maxx[200001];
long long val[200001];
long long ans = -(1000000000LL * 1000000000LL);
inline void dfs(int d) {
int i;
long long max1 = -(1000000000LL * 1000000000LL),
max2 = -(1000000000LL * 1000000000LL);
maxx[d] = -(1000000000LL * 1000000000LL);
for (i = head[d]; i != 0; i = a[i].next) {
int t = a[i].t;
if (t != fa[d]) {
fa[t] = d;
dfs(t);
maxx[d] = max(maxx[d], maxx[t]);
if (maxx[t] > max1) {
max2 = max1;
max1 = maxx[t];
} else if (maxx[t] > max2)
max2 = maxx[t];
sum[d] += sum[t];
}
}
sum[d] += val[d];
maxx[d] = max(sum[d], maxx[d]);
if (max1 != -(1000000000LL * 1000000000LL) &&
max2 != -(1000000000LL * 1000000000LL))
ans = max(ans, max1 + max2);
}
int main(void) {
int n;
scanf("%d", &n);
int i;
for (i = 1; i <= n; i++) scanf("%I64d", &val[i]);
int s, t;
for (i = 1; i <= n - 1; i++) {
scanf("%d%d", &s, &t);
edge++;
add(s, t);
edge++;
add(t, s);
}
dfs(1);
if (ans != -(1000000000LL * 1000000000LL))
printf("%I64d\n", ans);
else
printf("Impossible\n");
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
long long pls[1000000];
long long dp[1000000];
long long subsum[1000000];
long long bestsub[1000000];
long long globmax;
long long tempsort[1000000];
vector<int> adjlist[1000000];
void DFS(int node, int parent) {
subsum[node] = pls[node];
bestsub[node] = -1e10;
int len = adjlist[node].size();
for (int i = 0; i < len; i++) {
int nx = adjlist[node][i];
if (nx == parent) continue;
DFS(nx, node);
subsum[node] += subsum[nx];
bestsub[node] = max(bestsub[node], max(bestsub[nx], subsum[nx]));
}
bestsub[node] = max(subsum[node], bestsub[node]);
}
void DPTree(int node, int parent) {
long long cursum = -1e10;
int len = adjlist[node].size();
int tempdex = 0;
for (int i = 0; i < len; i++) {
int nx = adjlist[node][i];
if (nx != parent) {
tempsort[tempdex] = bestsub[nx];
tempdex++;
}
}
sort(tempsort, tempsort + tempdex);
if (tempdex > 1) {
cursum = max(cursum, tempsort[tempdex - 1] + tempsort[tempdex - 2]);
}
globmax = max(globmax, cursum);
for (int i = 0; i < len; i++) {
int nx = adjlist[node][i];
if (nx != parent) {
DPTree(nx, node);
}
}
}
int main() {
cin >> n;
globmax = -1e10;
for (int i = 1; i <= n; i++) cin >> pls[i];
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
adjlist[a].push_back(b);
adjlist[b].push_back(a);
}
DFS(1, -1);
DPTree(1, -1);
if (globmax == -1e10)
cout << "Impossible" << endl;
else
cout << globmax << 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;
int n;
long long pls[1000000];
long long dp[1000000];
long long subsum[1000000];
long long bestsub[1000000];
long long globmax;
long long tempsort[1000000];
vector<int> adjlist[1000000];
void DFS(int node, int parent) {
subsum[node] = pls[node];
bestsub[node] = -1e10;
int len = adjlist[node].size();
for (int i = 0; i < len; i++) {
int nx = adjlist[node][i];
if (nx == parent) continue;
DFS(nx, node);
subsum[node] += subsum[nx];
bestsub[node] = max(bestsub[node], max(bestsub[nx], subsum[nx]));
}
bestsub[node] = max(subsum[node], bestsub[node]);
}
void DPTree(int node, int parent) {
long long cursum = -1e10;
int len = adjlist[node].size();
int tempdex = 0;
for (int i = 0; i < len; i++) {
int nx = adjlist[node][i];
if (nx != parent) {
tempsort[tempdex] = bestsub[nx];
tempdex++;
}
}
sort(tempsort, tempsort + tempdex);
if (tempdex > 1) {
cursum = max(cursum, tempsort[tempdex - 1] + tempsort[tempdex - 2]);
}
globmax = max(globmax, cursum);
for (int i = 0; i < len; i++) {
int nx = adjlist[node][i];
if (nx != parent) {
DPTree(nx, node);
}
}
}
int main() {
cin >> n;
globmax = -1e10;
for (int i = 1; i <= n; i++) cin >> pls[i];
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
adjlist[a].push_back(b);
adjlist[b].push_back(a);
}
DFS(1, -1);
DPTree(1, -1);
if (globmax == -1e10)
cout << "Impossible" << endl;
else
cout << globmax << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200 * 1000 + 7;
const long long INF = (long long)1e18;
vector<int> g[MAXN];
int cost[MAXN];
long long sum[MAXN];
long long dp[MAXN];
long long res = -INF;
void dfs(int v, int p = -1) {
sum[v] = cost[v];
dp[v] = -INF;
long long mx1 = -INF, mx2 = -INF;
for (int i = 0; i < (int)g[v].size(); i++) {
int to = g[v][i];
if (to != p) {
dfs(to, v);
sum[v] += sum[to];
if (dp[to] >= mx1) {
mx2 = mx1;
mx1 = dp[to];
} else if (dp[to] >= mx2) {
mx2 = dp[to];
}
}
}
dp[v] = max(sum[v], mx1);
res = max(res, mx1 + mx2);
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &cost[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(1, -1);
if (res <= -1e16) {
puts("Impossible");
} else {
printf("%lld\n", res);
}
}
|
### 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 = 200 * 1000 + 7;
const long long INF = (long long)1e18;
vector<int> g[MAXN];
int cost[MAXN];
long long sum[MAXN];
long long dp[MAXN];
long long res = -INF;
void dfs(int v, int p = -1) {
sum[v] = cost[v];
dp[v] = -INF;
long long mx1 = -INF, mx2 = -INF;
for (int i = 0; i < (int)g[v].size(); i++) {
int to = g[v][i];
if (to != p) {
dfs(to, v);
sum[v] += sum[to];
if (dp[to] >= mx1) {
mx2 = mx1;
mx1 = dp[to];
} else if (dp[to] >= mx2) {
mx2 = dp[to];
}
}
}
dp[v] = max(sum[v], mx1);
res = max(res, mx1 + mx2);
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &cost[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(1, -1);
if (res <= -1e16) {
puts("Impossible");
} else {
printf("%lld\n", res);
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct Timer {
chrono::time_point<chrono::high_resolution_clock> start, end;
chrono::duration<float> dur;
Timer() { start = chrono::high_resolution_clock::now(); }
~Timer() {
end = chrono::high_resolution_clock::now();
dur = end - start;
cout << "\nTime Elapsed : " << dur.count() << " s\n";
}
};
long long int sum() { return 0; }
template <typename T, typename... Args>
T sum(T a, Args... args) {
return a + sum(args...);
}
template <int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
static_assert(D >= 1, "Vector dimension must be greater than zero!");
template <typename... Args>
Vec(int n = 0, Args... args)
: vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {}
};
template <typename T>
struct Vec<1, T> : public vector<T> {
Vec(int n = 0, T val = T()) : vector<T>(n, val) {}
};
struct Graph {
int n;
vector<vector<int>> adj;
vector<long long int> a, dp;
long long int ans;
Graph(int n) {
this->n = n;
adj.resize(n + 1);
a.resize(n + 1);
dp.resize(n + 1);
ans = -9e18;
}
void add_edge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
void dfs1(int u, int p) {
dp[u] = a[u];
for (auto v : adj[u]) {
if (v != p) {
dfs1(v, u);
dp[u] += dp[v];
}
}
}
void dfs2(int u, int p) {
vector<long long int> tmp;
for (auto v : adj[u]) {
if (v != p) {
dfs2(v, u);
tmp.push_back(dp[v]);
}
}
sort(tmp.rbegin(), tmp.rend());
if (!tmp.empty()) {
dp[u] = max(dp[u], tmp[0]);
if (tmp.size() >= 2) ans = max(ans, tmp[0] + tmp[1]);
}
}
void solve() {
dfs1(1, -1);
dfs2(1, -1);
if (ans == -9e18)
cout << "Impossible\n";
else
cout << ans << "\n";
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int testCases = 1;
while (testCases--) {
int n;
cin >> n;
Graph g(n);
for (int i = 1; i <= n; i++) cin >> g.a[i];
for (int i = 1; i <= n - 1; i++) {
int u, v;
cin >> u >> v;
g.add_edge(u, v);
}
g.solve();
}
}
|
### 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;
struct Timer {
chrono::time_point<chrono::high_resolution_clock> start, end;
chrono::duration<float> dur;
Timer() { start = chrono::high_resolution_clock::now(); }
~Timer() {
end = chrono::high_resolution_clock::now();
dur = end - start;
cout << "\nTime Elapsed : " << dur.count() << " s\n";
}
};
long long int sum() { return 0; }
template <typename T, typename... Args>
T sum(T a, Args... args) {
return a + sum(args...);
}
template <int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
static_assert(D >= 1, "Vector dimension must be greater than zero!");
template <typename... Args>
Vec(int n = 0, Args... args)
: vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {}
};
template <typename T>
struct Vec<1, T> : public vector<T> {
Vec(int n = 0, T val = T()) : vector<T>(n, val) {}
};
struct Graph {
int n;
vector<vector<int>> adj;
vector<long long int> a, dp;
long long int ans;
Graph(int n) {
this->n = n;
adj.resize(n + 1);
a.resize(n + 1);
dp.resize(n + 1);
ans = -9e18;
}
void add_edge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
void dfs1(int u, int p) {
dp[u] = a[u];
for (auto v : adj[u]) {
if (v != p) {
dfs1(v, u);
dp[u] += dp[v];
}
}
}
void dfs2(int u, int p) {
vector<long long int> tmp;
for (auto v : adj[u]) {
if (v != p) {
dfs2(v, u);
tmp.push_back(dp[v]);
}
}
sort(tmp.rbegin(), tmp.rend());
if (!tmp.empty()) {
dp[u] = max(dp[u], tmp[0]);
if (tmp.size() >= 2) ans = max(ans, tmp[0] + tmp[1]);
}
}
void solve() {
dfs1(1, -1);
dfs2(1, -1);
if (ans == -9e18)
cout << "Impossible\n";
else
cout << ans << "\n";
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int testCases = 1;
while (testCases--) {
int n;
cin >> n;
Graph g(n);
for (int i = 1; i <= n; i++) cin >> g.a[i];
for (int i = 1; i <= n - 1; i++) {
int u, v;
cin >> u >> v;
g.add_edge(u, v);
}
g.solve();
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
long long inf = 1e18;
int n;
vector<int> v[N];
vector<int> tree[N];
long long a[N];
long long sm[N];
bool was[N];
long long ans = -1e18;
void podves(int i) {
was[i] = true;
for (int j : v[i])
if (!was[j]) {
tree[i].push_back(j);
podves(j);
}
}
void dfs(int i) {
sm[i] = a[i];
for (int j : tree[i]) {
dfs(j);
sm[i] += sm[j];
}
}
void dfs2(int i) {
long long mx1 = -inf, mx2 = -inf;
for (int j : tree[i]) {
dfs2(j);
if (sm[j] > mx1) {
mx2 = mx1;
mx1 = sm[j];
} else if (sm[j] > mx2)
mx2 = sm[j];
}
if (mx2 != -inf) ans = max(ans, mx1 + mx2);
sm[i] = max(sm[i], mx1);
}
int main() {
srand(time(NULL));
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
v[x].push_back(y);
v[y].push_back(x);
}
fill(was, was + n, false);
podves(0);
dfs(0);
dfs2(0);
if (ans == -inf)
cout << "Impossible\n";
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 N = 2e5;
long long inf = 1e18;
int n;
vector<int> v[N];
vector<int> tree[N];
long long a[N];
long long sm[N];
bool was[N];
long long ans = -1e18;
void podves(int i) {
was[i] = true;
for (int j : v[i])
if (!was[j]) {
tree[i].push_back(j);
podves(j);
}
}
void dfs(int i) {
sm[i] = a[i];
for (int j : tree[i]) {
dfs(j);
sm[i] += sm[j];
}
}
void dfs2(int i) {
long long mx1 = -inf, mx2 = -inf;
for (int j : tree[i]) {
dfs2(j);
if (sm[j] > mx1) {
mx2 = mx1;
mx1 = sm[j];
} else if (sm[j] > mx2)
mx2 = sm[j];
}
if (mx2 != -inf) ans = max(ans, mx1 + mx2);
sm[i] = max(sm[i], mx1);
}
int main() {
srand(time(NULL));
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
v[x].push_back(y);
v[y].push_back(x);
}
fill(was, was + n, false);
podves(0);
dfs(0);
dfs2(0);
if (ans == -inf)
cout << "Impossible\n";
else
cout << ans << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
struct node {
int v, nxt;
} edge[200010 * 2];
long long dp[200010], sum[200010], w[200010], head[200010], tot, ans;
void add(int u, int v) {
edge[tot].v = v;
edge[tot].nxt = head[u];
head[u] = tot++;
}
void dfs(int u, int fa) {
sum[u] = w[u];
for (int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if (v == fa) continue;
dfs(v, u);
sum[u] += sum[v];
if (dp[u] > -INF) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], sum[u]);
}
int main() {
int n;
scanf("%d", &n);
memset(head, -1, sizeof(head));
for (int i = 1; i <= n; i++) scanf("%I64d", &w[i]);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
ans = -INF;
for (int i = 1; i <= n; i++) dp[i] = -INF;
dfs(1, -1);
if (ans <= -INF)
puts("Impossible");
else
printf("%I64d\n", ans);
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;
const long long INF = 1LL << 60;
struct node {
int v, nxt;
} edge[200010 * 2];
long long dp[200010], sum[200010], w[200010], head[200010], tot, ans;
void add(int u, int v) {
edge[tot].v = v;
edge[tot].nxt = head[u];
head[u] = tot++;
}
void dfs(int u, int fa) {
sum[u] = w[u];
for (int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if (v == fa) continue;
dfs(v, u);
sum[u] += sum[v];
if (dp[u] > -INF) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], sum[u]);
}
int main() {
int n;
scanf("%d", &n);
memset(head, -1, sizeof(head));
for (int i = 1; i <= n; i++) scanf("%I64d", &w[i]);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
ans = -INF;
for (int i = 1; i <= n; i++) dp[i] = -INF;
dfs(1, -1);
if (ans <= -INF)
puts("Impossible");
else
printf("%I64d\n", ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1e5 + 10;
const long long INF = 10e18;
vector<int> g[N];
long long scores[N];
bool vis[N];
long long dp[N];
long long sum[N];
int n;
long long ans;
void init() {
for (int i = 1; i <= n; ++i) {
g[i].clear();
}
memset(scores, 0, sizeof(scores));
memset(vis, false, sizeof(vis));
memset(dp, 0, sizeof(dp));
memset(sum, 0, sizeof(sum));
ans = -INF;
}
void dfs(int u) {
sum[u] += scores[u];
int len = g[u].size();
for (int i = 0; i < len; ++i) {
int v = g[u][i];
if (vis[v]) continue;
vis[v] = true;
dfs(v);
sum[u] += sum[v];
if (dp[u] > -INF) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], sum[u]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
init();
for (int i = 1; i <= n; ++i) {
cin >> scores[i];
dp[i] = -INF;
}
int u, v;
for (int i = 1; i <= n - 1; ++i) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
vis[1] = true;
dfs(1);
if (ans <= -INF)
cout << "Impossible" << endl;
else
cout << ans << endl;
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 N = 2 * 1e5 + 10;
const long long INF = 10e18;
vector<int> g[N];
long long scores[N];
bool vis[N];
long long dp[N];
long long sum[N];
int n;
long long ans;
void init() {
for (int i = 1; i <= n; ++i) {
g[i].clear();
}
memset(scores, 0, sizeof(scores));
memset(vis, false, sizeof(vis));
memset(dp, 0, sizeof(dp));
memset(sum, 0, sizeof(sum));
ans = -INF;
}
void dfs(int u) {
sum[u] += scores[u];
int len = g[u].size();
for (int i = 0; i < len; ++i) {
int v = g[u][i];
if (vis[v]) continue;
vis[v] = true;
dfs(v);
sum[u] += sum[v];
if (dp[u] > -INF) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], sum[u]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
init();
for (int i = 1; i <= n; ++i) {
cin >> scores[i];
dp[i] = -INF;
}
int u, v;
for (int i = 1; i <= n - 1; ++i) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
vis[1] = true;
dfs(1);
if (ans <= -INF)
cout << "Impossible" << endl;
else
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 2e5 + 10;
const long long int inf = 1e18 * -1;
int n;
long long int p[Maxn], d[Maxn], best[Maxn], ans = inf;
bool mark[Maxn];
vector<int> nei[Maxn];
void dfs(int v);
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> p[i];
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
nei[a].push_back(b);
nei[b].push_back(a);
}
dfs(0);
if (ans == inf)
cout << "Impossible" << endl;
else
cout << ans << endl;
}
void dfs(int v) {
mark[v] = true;
d[v] = p[v];
best[v] = inf;
long long int m1 = inf, m2 = inf;
for (auto u : nei[v])
if (!mark[u]) {
dfs(u);
d[v] += d[u];
best[v] = max(best[v], best[u]);
if (m1 < best[u]) {
m2 = m1;
m1 = best[u];
} else
m2 = max(m2, best[u]);
}
best[v] = max(best[v], d[v]);
if (m2 != inf) ans = max(ans, m1 + m2);
}
|
### 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 + 10;
const long long int inf = 1e18 * -1;
int n;
long long int p[Maxn], d[Maxn], best[Maxn], ans = inf;
bool mark[Maxn];
vector<int> nei[Maxn];
void dfs(int v);
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> p[i];
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
nei[a].push_back(b);
nei[b].push_back(a);
}
dfs(0);
if (ans == inf)
cout << "Impossible" << endl;
else
cout << ans << endl;
}
void dfs(int v) {
mark[v] = true;
d[v] = p[v];
best[v] = inf;
long long int m1 = inf, m2 = inf;
for (auto u : nei[v])
if (!mark[u]) {
dfs(u);
d[v] += d[u];
best[v] = max(best[v], best[u]);
if (m1 < best[u]) {
m2 = m1;
m1 = best[u];
} else
m2 = max(m2, best[u]);
}
best[v] = max(best[v], d[v]);
if (m2 != inf) ans = max(ans, m1 + m2);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 5001;
long long int powxy(long long int x, long long int y) {
if (y == 0) return 1;
if (y % 2 == 1) return (x * powxy(x, y - 1)) % mod;
long long int t = powxy(x, y / 2) % mod;
return (t * t) % mod;
}
vector<int> adj[300005];
long long int sum[300005];
long long int dp[300005];
int cost[300005];
long long int ans = -1e15;
void solve(int node, int par) {
sum[node] = cost[node];
long long int max1 = -1e15;
long long int max2 = -1e15;
for (int to : adj[node]) {
if (to == par) continue;
solve(to, node);
sum[node] += sum[to];
}
dp[node] = sum[node];
for (int to : adj[node]) {
if (to == par) continue;
dp[node] = max(dp[node], dp[to]);
}
for (int to : adj[node]) {
if (to == par) continue;
if (dp[to] > max1) {
max2 = max1;
max1 = dp[to];
} else if (dp[to] > max2)
max2 = dp[to];
}
if (max2 != -1e15) ans = max(ans, max1 + max2);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> cost[i];
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
solve(1, 0);
if (ans != -1e15)
cout << ans;
else
cout << "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 mod = 1e9 + 7;
const int N = 5001;
long long int powxy(long long int x, long long int y) {
if (y == 0) return 1;
if (y % 2 == 1) return (x * powxy(x, y - 1)) % mod;
long long int t = powxy(x, y / 2) % mod;
return (t * t) % mod;
}
vector<int> adj[300005];
long long int sum[300005];
long long int dp[300005];
int cost[300005];
long long int ans = -1e15;
void solve(int node, int par) {
sum[node] = cost[node];
long long int max1 = -1e15;
long long int max2 = -1e15;
for (int to : adj[node]) {
if (to == par) continue;
solve(to, node);
sum[node] += sum[to];
}
dp[node] = sum[node];
for (int to : adj[node]) {
if (to == par) continue;
dp[node] = max(dp[node], dp[to]);
}
for (int to : adj[node]) {
if (to == par) continue;
if (dp[to] > max1) {
max2 = max1;
max1 = dp[to];
} else if (dp[to] > max2)
max2 = dp[to];
}
if (max2 != -1e15) ans = max(ans, max1 + max2);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> cost[i];
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
solve(1, 0);
if (ans != -1e15)
cout << ans;
else
cout << "Impossible";
}
```
|
#include <bits/stdc++.h>
using namespace std;
int a[200002];
vector<int> G[200002], T[200002];
long long w[200002], best[200002];
void dfs(int root, bool v[]) {
v[root] = true;
for (int u : G[root]) {
if (!v[u]) {
T[root].push_back(u);
dfs(u, v);
}
}
}
void dfs2(int root) {
w[root] = best[root] = a[root];
if (T[root].size() == 0) return;
best[root] = -1e18;
for (int u : T[root]) {
dfs2(u);
w[root] += w[u];
if (best[u] > best[root]) best[root] = best[u];
}
best[root] = max(best[root], w[root]);
}
long long dfs3(int root, long long ans) {
if (T[root].size() == 0) return ans;
vector<long long> temp;
for (int u : T[root]) {
ans = dfs3(u, ans);
temp.push_back(best[u]);
}
if (T[root].size() == 1) return ans;
sort(temp.begin(), temp.end(), greater<long long>());
return max(ans, temp[0] + temp[1]);
}
int main() {
ios_base::sync_with_stdio(false);
int n, i, x, y;
long long ans;
bool v[200002];
memset(v, false, sizeof(v));
cin >> n;
for (i = 1; i < n + 1; i++) cin >> a[i];
for (i = 0; i < n - 1; i++) {
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
dfs(1, v);
dfs2(1);
ans = dfs3(1, -1e18);
ans == -1e18 ? cout << "Impossible" : 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;
int a[200002];
vector<int> G[200002], T[200002];
long long w[200002], best[200002];
void dfs(int root, bool v[]) {
v[root] = true;
for (int u : G[root]) {
if (!v[u]) {
T[root].push_back(u);
dfs(u, v);
}
}
}
void dfs2(int root) {
w[root] = best[root] = a[root];
if (T[root].size() == 0) return;
best[root] = -1e18;
for (int u : T[root]) {
dfs2(u);
w[root] += w[u];
if (best[u] > best[root]) best[root] = best[u];
}
best[root] = max(best[root], w[root]);
}
long long dfs3(int root, long long ans) {
if (T[root].size() == 0) return ans;
vector<long long> temp;
for (int u : T[root]) {
ans = dfs3(u, ans);
temp.push_back(best[u]);
}
if (T[root].size() == 1) return ans;
sort(temp.begin(), temp.end(), greater<long long>());
return max(ans, temp[0] + temp[1]);
}
int main() {
ios_base::sync_with_stdio(false);
int n, i, x, y;
long long ans;
bool v[200002];
memset(v, false, sizeof(v));
cin >> n;
for (i = 1; i < n + 1; i++) cin >> a[i];
for (i = 0; i < n - 1; i++) {
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
dfs(1, v);
dfs2(1);
ans = dfs3(1, -1e18);
ans == -1e18 ? cout << "Impossible" : cout << ans;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename V>
void bugp(const pair<T, V> &x) {
cerr << '{' << x.first << ", " << x.second << '}' << endl;
}
template <typename T, typename U, typename V>
void bugpp(const pair<T, pair<U, V> > &x) {
cerr << '{' << x.first << ", {" << x.second.first << ", " << x.second.second
<< "}}" << endl;
}
template <typename T>
bool maximize(T &x, const T &y) {
if (x < y) {
x = y;
return 1;
}
return 0;
}
template <typename T>
bool minimize(T &x, const T &y) {
if (x > y) {
x = y;
return 1;
}
return 0;
}
const int N = 200002;
const long long INF = (long long)1e18;
int n;
long long dp[N], C[N], answer = -INF;
vector<int> G[N];
void DFS(int u, int par) {
long long fi = -INF, se = -INF;
dp[u] = -INF;
for (int &v : G[u]) {
if (v == par) continue;
DFS(v, u);
if (dp[v] >= fi) {
se = fi;
fi = dp[v];
} else
maximize(se, dp[v]);
maximize(dp[u], dp[v]);
C[u] += C[v];
}
if (fi != -INF && se != -INF) maximize(answer, fi + se);
maximize(dp[u], C[u]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1, _n = (n); i <= _n; ++i) cin >> C[i];
for (int i = 1, _n = (n - 1); i <= _n; ++i) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
DFS(1, 1);
if (answer == -INF)
cout << "Impossible";
else
cout << answer;
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, typename V>
void bugp(const pair<T, V> &x) {
cerr << '{' << x.first << ", " << x.second << '}' << endl;
}
template <typename T, typename U, typename V>
void bugpp(const pair<T, pair<U, V> > &x) {
cerr << '{' << x.first << ", {" << x.second.first << ", " << x.second.second
<< "}}" << endl;
}
template <typename T>
bool maximize(T &x, const T &y) {
if (x < y) {
x = y;
return 1;
}
return 0;
}
template <typename T>
bool minimize(T &x, const T &y) {
if (x > y) {
x = y;
return 1;
}
return 0;
}
const int N = 200002;
const long long INF = (long long)1e18;
int n;
long long dp[N], C[N], answer = -INF;
vector<int> G[N];
void DFS(int u, int par) {
long long fi = -INF, se = -INF;
dp[u] = -INF;
for (int &v : G[u]) {
if (v == par) continue;
DFS(v, u);
if (dp[v] >= fi) {
se = fi;
fi = dp[v];
} else
maximize(se, dp[v]);
maximize(dp[u], dp[v]);
C[u] += C[v];
}
if (fi != -INF && se != -INF) maximize(answer, fi + se);
maximize(dp[u], C[u]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1, _n = (n); i <= _n; ++i) cin >> C[i];
for (int i = 1, _n = (n - 1); i <= _n; ++i) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
DFS(1, 1);
if (answer == -INF)
cout << "Impossible";
else
cout << answer;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)1e18 + 1l;
const int N = 300300;
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;
long long 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;
}
class Solution {
public:
void solve(std::istream& in, std::ostream& out) {
int n;
in >> n;
for (int i = 0; i < n; i++) {
in >> a[i];
}
for (int i = 1; i < n; i++) {
int v, u;
in >> v >> u;
v--;
u--;
g[v].push_back(u);
g[u].push_back(v);
}
dfs(0);
if (ans == -INF)
out << "Impossible\n";
else
out << ans;
}
};
void solve(std::istream& in, std::ostream& out) {
out << std::setprecision(12);
Solution solution;
solution.solve(in, out);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
istream& in = cin;
ostream& out = cout;
solve(in, out);
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 INF = (long long)1e18 + 1l;
const int N = 300300;
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;
long long 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;
}
class Solution {
public:
void solve(std::istream& in, std::ostream& out) {
int n;
in >> n;
for (int i = 0; i < n; i++) {
in >> a[i];
}
for (int i = 1; i < n; i++) {
int v, u;
in >> v >> u;
v--;
u--;
g[v].push_back(u);
g[u].push_back(v);
}
dfs(0);
if (ans == -INF)
out << "Impossible\n";
else
out << ans;
}
};
void solve(std::istream& in, std::ostream& out) {
out << std::setprecision(12);
Solution solution;
solution.solve(in, out);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
istream& in = cin;
ostream& out = cout;
solve(in, out);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 1;
long long n;
long long a[N];
vector<long long> adj[N];
long long dp[N][2];
long long ans = -1e18;
void dfs(long long src, long long par) {
dp[src][1] = a[src];
dp[src][0] = -1e18;
for (auto it : adj[src]) {
if (it != par) {
dfs(it, src);
dp[src][0] = max(dp[src][0], max(dp[it][0], dp[it][1]));
dp[src][1] += dp[it][1];
}
}
long long fmx = -1e18;
long long smx = -1e18;
for (auto it : adj[src]) {
if (it != par) {
long long temp = max(dp[it][0], dp[it][1]);
if (temp >= fmx) {
smx = fmx;
fmx = temp;
} else {
smx = max(smx, temp);
}
}
}
if (smx != -1e18) ans = max(ans, smx + fmx);
}
signed main() {
ios_base::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;
adj[u].push_back(v);
adj[v].push_back(u);
}
if (n == 1 || n == 2) {
cout << "Impossible";
} else {
dfs(1, -1);
if (ans == -1e18)
cout << "Impossible";
else
cout << ans;
}
}
|
### 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 N = 2e5 + 1;
long long n;
long long a[N];
vector<long long> adj[N];
long long dp[N][2];
long long ans = -1e18;
void dfs(long long src, long long par) {
dp[src][1] = a[src];
dp[src][0] = -1e18;
for (auto it : adj[src]) {
if (it != par) {
dfs(it, src);
dp[src][0] = max(dp[src][0], max(dp[it][0], dp[it][1]));
dp[src][1] += dp[it][1];
}
}
long long fmx = -1e18;
long long smx = -1e18;
for (auto it : adj[src]) {
if (it != par) {
long long temp = max(dp[it][0], dp[it][1]);
if (temp >= fmx) {
smx = fmx;
fmx = temp;
} else {
smx = max(smx, temp);
}
}
}
if (smx != -1e18) ans = max(ans, smx + fmx);
}
signed main() {
ios_base::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;
adj[u].push_back(v);
adj[v].push_back(u);
}
if (n == 1 || n == 2) {
cout << "Impossible";
} else {
dfs(1, -1);
if (ans == -1e18)
cout << "Impossible";
else
cout << ans;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int n, vs[N];
long long res = INT_MIN;
vector<int> tr[N];
pair<long long, long long> dfs(int curr, int par) {
if (tr[curr].size() == 1 && tr[curr][0] == par) {
return {vs[curr], vs[curr]};
}
long long mx = INT_MIN, mx2 = INT_MIN, sm = vs[curr];
for (int& ch : tr[curr]) {
if (ch != par) {
pair<long long, long long> chres = dfs(ch, curr);
sm += chres.first;
if (chres.second >= mx) {
mx2 = mx;
mx = chres.second;
} else if (chres.second > mx2) {
mx2 = chres.second;
}
}
}
if (mx > INT_MIN && mx2 > INT_MIN) {
res = max(res, mx + mx2);
}
return {sm, max(sm, mx)};
}
int main() {
scanf("%d", &n);
for (int i = (1), _i = (n); i <= _i; ++i) {
scanf("%d", &vs[i]);
}
for (int i = (1), _i = (n - 1); i <= _i; ++i) {
int u, v;
scanf("%d", &u);
scanf("%d", &v);
tr[u].push_back(v);
tr[v].push_back(u);
}
dfs(1, 0);
if (res == INT_MIN) {
printf("Impossible");
} else {
cout << res;
}
}
|
### 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 + 7;
int n, vs[N];
long long res = INT_MIN;
vector<int> tr[N];
pair<long long, long long> dfs(int curr, int par) {
if (tr[curr].size() == 1 && tr[curr][0] == par) {
return {vs[curr], vs[curr]};
}
long long mx = INT_MIN, mx2 = INT_MIN, sm = vs[curr];
for (int& ch : tr[curr]) {
if (ch != par) {
pair<long long, long long> chres = dfs(ch, curr);
sm += chres.first;
if (chres.second >= mx) {
mx2 = mx;
mx = chres.second;
} else if (chres.second > mx2) {
mx2 = chres.second;
}
}
}
if (mx > INT_MIN && mx2 > INT_MIN) {
res = max(res, mx + mx2);
}
return {sm, max(sm, mx)};
}
int main() {
scanf("%d", &n);
for (int i = (1), _i = (n); i <= _i; ++i) {
scanf("%d", &vs[i]);
}
for (int i = (1), _i = (n - 1); i <= _i; ++i) {
int u, v;
scanf("%d", &u);
scanf("%d", &v);
tr[u].push_back(v);
tr[v].push_back(u);
}
dfs(1, 0);
if (res == INT_MIN) {
printf("Impossible");
} else {
cout << res;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct node {
long long next;
long long t;
long long w;
} edge[400005];
struct node1 {
long long w;
long long pos;
} p[400005];
long long head[400005], vis[400005];
long long n, tot;
void addedge(long long u, long long v) {
edge[++tot].next = head[u];
edge[tot].t = v;
head[u] = tot;
}
long long maxn1 = -1e18, maxn2 = -1e18;
void dfs1(long long now) {
for (int i = head[now]; i; i = edge[i].next) {
if (!vis[edge[i].t]) {
vis[edge[i].t] = 1;
dfs1(edge[i].t);
p[now].w += p[edge[i].t].w;
}
}
}
void dfs2(long long now) {
for (int i = head[now]; i; i = edge[i].next) {
if (!vis[edge[i].t]) {
vis[edge[i].t] = 1;
if (maxn1 != -1e18) maxn2 = max(maxn2, maxn1 + p[edge[i].t].w);
dfs2(edge[i].t);
maxn1 = max(maxn1, p[edge[i].t].w);
}
}
}
int main() {
scanf("%lld", &n);
for (int i = 1; i <= n; ++i) {
scanf("%lld", &p[i].w);
p[i].pos = i;
}
long long x, y;
for (int i = 1; i <= n - 1; i++) {
scanf("%lld %lld", &x, &y);
addedge(x, y);
addedge(y, x);
}
vis[1] = 1;
dfs1(1);
memset(vis, 0, sizeof(vis));
vis[1] = 1;
dfs2(1);
if (maxn2 != -1e18)
printf("%lld\n", maxn2);
else
printf("Impossible\n");
}
|
### 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;
struct node {
long long next;
long long t;
long long w;
} edge[400005];
struct node1 {
long long w;
long long pos;
} p[400005];
long long head[400005], vis[400005];
long long n, tot;
void addedge(long long u, long long v) {
edge[++tot].next = head[u];
edge[tot].t = v;
head[u] = tot;
}
long long maxn1 = -1e18, maxn2 = -1e18;
void dfs1(long long now) {
for (int i = head[now]; i; i = edge[i].next) {
if (!vis[edge[i].t]) {
vis[edge[i].t] = 1;
dfs1(edge[i].t);
p[now].w += p[edge[i].t].w;
}
}
}
void dfs2(long long now) {
for (int i = head[now]; i; i = edge[i].next) {
if (!vis[edge[i].t]) {
vis[edge[i].t] = 1;
if (maxn1 != -1e18) maxn2 = max(maxn2, maxn1 + p[edge[i].t].w);
dfs2(edge[i].t);
maxn1 = max(maxn1, p[edge[i].t].w);
}
}
}
int main() {
scanf("%lld", &n);
for (int i = 1; i <= n; ++i) {
scanf("%lld", &p[i].w);
p[i].pos = i;
}
long long x, y;
for (int i = 1; i <= n - 1; i++) {
scanf("%lld %lld", &x, &y);
addedge(x, y);
addedge(y, x);
}
vis[1] = 1;
dfs1(1);
memset(vis, 0, sizeof(vis));
vis[1] = 1;
dfs2(1);
if (maxn2 != -1e18)
printf("%lld\n", maxn2);
else
printf("Impossible\n");
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18 + 9;
const long long INVALID = -INF;
void fast_io() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
void dfs(int u, int p, vector<vector<int> > &g, vector<long long> &w,
vector<long long> &dp, vector<long long> &dp_st) {
dp[u] = w[u];
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v != p) {
dfs(v, u, g, w, dp, dp_st);
dp[u] += dp[v];
dp_st[u] = max(dp_st[u], dp_st[v]);
}
}
dp_st[u] = max(dp[u], dp_st[u]);
}
void find_pair(int u, int p, vector<vector<int> > &g, vector<long long> &dp_st,
long long &ans, bool &can) {
long long largest = INVALID, second_largest = INVALID;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v != p) {
find_pair(v, u, g, dp_st, ans, can);
if (dp_st[v] >= largest) {
second_largest = largest;
largest = dp_st[v];
} else if (dp_st[v] > second_largest)
second_largest = dp_st[v];
}
}
if (largest != INVALID and second_largest != INVALID) {
ans = max(ans, largest + second_largest);
if (!can) can = true;
}
}
int main() {
fast_io();
int n, u, v;
cin >> n;
vector<vector<int> > g(n + 1);
vector<long long> A(n + 1), dp(n + 1, 0), dp_st(n + 1, INVALID);
long long ans = INVALID;
bool can = false;
for (int i = 1; i <= n; i++) {
cin >> A[i];
}
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 1, g, A, dp, dp_st);
find_pair(1, 1, g, dp_st, ans, can);
if (can)
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;
const long long INF = 1e18 + 9;
const long long INVALID = -INF;
void fast_io() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
void dfs(int u, int p, vector<vector<int> > &g, vector<long long> &w,
vector<long long> &dp, vector<long long> &dp_st) {
dp[u] = w[u];
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v != p) {
dfs(v, u, g, w, dp, dp_st);
dp[u] += dp[v];
dp_st[u] = max(dp_st[u], dp_st[v]);
}
}
dp_st[u] = max(dp[u], dp_st[u]);
}
void find_pair(int u, int p, vector<vector<int> > &g, vector<long long> &dp_st,
long long &ans, bool &can) {
long long largest = INVALID, second_largest = INVALID;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v != p) {
find_pair(v, u, g, dp_st, ans, can);
if (dp_st[v] >= largest) {
second_largest = largest;
largest = dp_st[v];
} else if (dp_st[v] > second_largest)
second_largest = dp_st[v];
}
}
if (largest != INVALID and second_largest != INVALID) {
ans = max(ans, largest + second_largest);
if (!can) can = true;
}
}
int main() {
fast_io();
int n, u, v;
cin >> n;
vector<vector<int> > g(n + 1);
vector<long long> A(n + 1), dp(n + 1, 0), dp_st(n + 1, INVALID);
long long ans = INVALID;
bool can = false;
for (int i = 1; i <= n; i++) {
cin >> A[i];
}
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 1, g, A, dp, dp_st);
find_pair(1, 1, g, dp_st, ans, can);
if (can)
cout << ans << '\n';
else
cout << "Impossible" << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e15;
const long long MAXN = 2e5 + 10;
long long val[MAXN], dp[MAXN], ans = -INF;
vector<int> v[MAXN];
void DFS(int x, int y) {
for (int i = 0; i < v[x].size(); i++) {
int u = v[x][i];
if (u == y) continue;
DFS(u, x);
val[x] += val[u];
if (dp[x] != -INF) ans = max(ans, dp[x] + dp[u]);
dp[x] = max(dp[x], dp[u]);
}
dp[x] = max(dp[x], val[x]);
}
int main() {
int x, y, N;
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
scanf("%I64d", &val[i]);
dp[i] = -INF;
}
for (int i = 1; i < N; i++) {
scanf("%d %d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
DFS(1, -1);
if (ans == -INF)
printf("Impossible\n");
else
printf("%I64d\n", 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>
using namespace std;
const long long INF = 1e15;
const long long MAXN = 2e5 + 10;
long long val[MAXN], dp[MAXN], ans = -INF;
vector<int> v[MAXN];
void DFS(int x, int y) {
for (int i = 0; i < v[x].size(); i++) {
int u = v[x][i];
if (u == y) continue;
DFS(u, x);
val[x] += val[u];
if (dp[x] != -INF) ans = max(ans, dp[x] + dp[u]);
dp[x] = max(dp[x], dp[u]);
}
dp[x] = max(dp[x], val[x]);
}
int main() {
int x, y, N;
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
scanf("%I64d", &val[i]);
dp[i] = -INF;
}
for (int i = 1; i < N; i++) {
scanf("%d %d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
DFS(1, -1);
if (ans == -INF)
printf("Impossible\n");
else
printf("%I64d\n", ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const long long inf = 1e15;
long long dp[maxn], num[maxn], ans;
vector<int> G[maxn];
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);
num[u] += num[v];
if (dp[u] != -inf) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], num[u]);
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%I64d", &num[i]);
dp[i] = -inf;
G[i].clear();
}
int u, v;
for (int i = 0; i < n - 1; i++) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
ans = -inf;
dfs(1, -1);
if (ans == -inf)
printf("Impossible\n");
else
printf("%I64d\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 int maxn = 200005;
const long long inf = 1e15;
long long dp[maxn], num[maxn], ans;
vector<int> G[maxn];
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);
num[u] += num[v];
if (dp[u] != -inf) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], num[u]);
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%I64d", &num[i]);
dp[i] = -inf;
G[i].clear();
}
int u, v;
for (int i = 0; i < n - 1; i++) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
ans = -inf;
dfs(1, -1);
if (ans == -inf)
printf("Impossible\n");
else
printf("%I64d\n", ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const long long INF = 1e15;
vector<int> G[MAXN];
long long a[MAXN], sum[MAXN], dp[MAXN];
long long n, ans = -INF;
void dfs(int u, int fa) {
sum[u] = a[u];
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[u] != -INF) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], sum[u]);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
dp[i] = -INF;
}
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 (ans == -INF)
cout << "Impossible" << endl;
else
cout << ans << 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 MAXN = 2e5 + 5;
const long long INF = 1e15;
vector<int> G[MAXN];
long long a[MAXN], sum[MAXN], dp[MAXN];
long long n, ans = -INF;
void dfs(int u, int fa) {
sum[u] = a[u];
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[u] != -INF) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], sum[u]);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
dp[i] = -INF;
}
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 (ans == -INF)
cout << "Impossible" << endl;
else
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:66777216")
int mox[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int moy[8] = {0, 0, 1, -1, -1, -1, 1, 1};
char mover[4] = {'D', 'U', 'R', 'L'};
const long long INF = 1LL << 61;
const int inf = 2e9;
const long double PI = acos(-1);
const long double EPS = 1e-9;
bool debug;
using namespace std;
void solve();
int main() {
debug = 0;
cout.precision(30);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
}
const int N = 200005;
const int MAGIC = 420;
const int lg = 26;
long long best = -INF, a[N];
int n, x, y;
vector<int> g[N];
multiset<long long> dfs(int v = 1, int par = -1) {
multiset<long long> s;
for (int to : g[v]) {
if (to == par) continue;
multiset<long long> t = dfs(to, v);
a[v] += a[to];
if ((int)s.size() < (int)t.size()) swap(s, t);
for (auto it = t.begin(); it != t.end(); it++) s.insert(*it);
}
s.insert(a[v]);
return s;
}
long long dfs1(int v = 1, int par = -1) {
vector<long long> q;
for (int to : g[v]) {
if (to == par) continue;
q.push_back(dfs1(to, v));
sort(q.rbegin(), q.rend());
while ((int)q.size() > 2) q.pop_back();
a[v] += a[to];
}
if ((int)q.size() == 2) best = max(best, q[0] + q[1]);
if (q.empty()) return a[v];
return max(q[0], a[v]);
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs1();
dfs();
if (best == -INF)
cout << "Impossible" << endl;
else
cout << best << endl;
}
|
### 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>
#pragma comment(linker, "/STACK:66777216")
int mox[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int moy[8] = {0, 0, 1, -1, -1, -1, 1, 1};
char mover[4] = {'D', 'U', 'R', 'L'};
const long long INF = 1LL << 61;
const int inf = 2e9;
const long double PI = acos(-1);
const long double EPS = 1e-9;
bool debug;
using namespace std;
void solve();
int main() {
debug = 0;
cout.precision(30);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
}
const int N = 200005;
const int MAGIC = 420;
const int lg = 26;
long long best = -INF, a[N];
int n, x, y;
vector<int> g[N];
multiset<long long> dfs(int v = 1, int par = -1) {
multiset<long long> s;
for (int to : g[v]) {
if (to == par) continue;
multiset<long long> t = dfs(to, v);
a[v] += a[to];
if ((int)s.size() < (int)t.size()) swap(s, t);
for (auto it = t.begin(); it != t.end(); it++) s.insert(*it);
}
s.insert(a[v]);
return s;
}
long long dfs1(int v = 1, int par = -1) {
vector<long long> q;
for (int to : g[v]) {
if (to == par) continue;
q.push_back(dfs1(to, v));
sort(q.rbegin(), q.rend());
while ((int)q.size() > 2) q.pop_back();
a[v] += a[to];
}
if ((int)q.size() == 2) best = max(best, q[0] + q[1]);
if (q.empty()) return a[v];
return max(q[0], a[v]);
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs1();
dfs();
if (best == -INF)
cout << "Impossible" << endl;
else
cout << best << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 9e18;
int N;
long long ans = -INF;
long long C[200005];
bool visited[200005];
vector<int> adj[200005], Ch[200005];
multiset<long long> SA;
void dfs(int x) {
visited[x] = true;
for (int i = 0; i < adj[x].size(); i++) {
if (!visited[adj[x][i]]) {
Ch[x].push_back(adj[x][i]);
dfs(adj[x][i]);
C[x] += C[adj[x][i]];
}
}
SA.insert(C[x]);
}
void dfs2(int x) {
visited[x] = true;
SA.erase(SA.find(C[x]));
for (int i = 0; i < Ch[x].size(); i++) {
if (!visited[Ch[x][i]]) {
dfs2(Ch[x][i]);
}
}
if (SA.size() != 0) {
ans = max(*SA.rbegin() + C[x], ans);
}
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
scanf("%I64d", &C[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);
}
memset(visited, false, sizeof(visited));
dfs(1);
memset(visited, false, sizeof(visited));
dfs2(1);
if (ans == -INF)
printf("Impossible\n");
else {
printf("%I64d", 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>
using namespace std;
const long long INF = 9e18;
int N;
long long ans = -INF;
long long C[200005];
bool visited[200005];
vector<int> adj[200005], Ch[200005];
multiset<long long> SA;
void dfs(int x) {
visited[x] = true;
for (int i = 0; i < adj[x].size(); i++) {
if (!visited[adj[x][i]]) {
Ch[x].push_back(adj[x][i]);
dfs(adj[x][i]);
C[x] += C[adj[x][i]];
}
}
SA.insert(C[x]);
}
void dfs2(int x) {
visited[x] = true;
SA.erase(SA.find(C[x]));
for (int i = 0; i < Ch[x].size(); i++) {
if (!visited[Ch[x][i]]) {
dfs2(Ch[x][i]);
}
}
if (SA.size() != 0) {
ans = max(*SA.rbegin() + C[x], ans);
}
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
scanf("%I64d", &C[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);
}
memset(visited, false, sizeof(visited));
dfs(1);
memset(visited, false, sizeof(visited));
dfs2(1);
if (ans == -INF)
printf("Impossible\n");
else {
printf("%I64d", ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using PII = pair<int, int>;
const int N = 2e5 + 5;
const LL INF = -1e17;
vector<int> g[N];
int n;
LL a[N], ans[N], mx[N], tot[N];
void dp(int u, int p) {
ans[u] = INF, mx[u] = INF, tot[u] = a[u];
LL x = INF, y = INF;
for (auto v : g[u])
if (v != p) {
dp(v, u);
ans[u] = max(ans[u], ans[v]);
tot[u] += tot[v];
mx[u] = max(mx[u], mx[v]);
if (mx[v] > x)
y = x, x = mx[v];
else
y = max(y, mx[v]);
}
mx[u] = max(tot[u], mx[u]);
if (y > INF) ans[u] = max(ans[u], x + y);
}
int main() {
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);
g[u].push_back(v);
g[v].push_back(u);
}
dp(1, 0);
if (ans[1] <= INF)
printf("Impossible\n");
else
printf("%lld\n", ans[1]);
}
|
### 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;
using LL = long long;
using PII = pair<int, int>;
const int N = 2e5 + 5;
const LL INF = -1e17;
vector<int> g[N];
int n;
LL a[N], ans[N], mx[N], tot[N];
void dp(int u, int p) {
ans[u] = INF, mx[u] = INF, tot[u] = a[u];
LL x = INF, y = INF;
for (auto v : g[u])
if (v != p) {
dp(v, u);
ans[u] = max(ans[u], ans[v]);
tot[u] += tot[v];
mx[u] = max(mx[u], mx[v]);
if (mx[v] > x)
y = x, x = mx[v];
else
y = max(y, mx[v]);
}
mx[u] = max(tot[u], mx[u]);
if (y > INF) ans[u] = max(ans[u], x + y);
}
int main() {
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);
g[u].push_back(v);
g[v].push_back(u);
}
dp(1, 0);
if (ans[1] <= INF)
printf("Impossible\n");
else
printf("%lld\n", ans[1]);
}
```
|
#include <bits/stdc++.h>
inline long long MAX2(long long a, long long int b) {
return (a) > (b) ? (a) : (b);
}
inline long long MAX3(long long a, long long b, long long c) {
return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));
}
inline long long MIN2(long long a, long long b) {
return (a) < (b) ? (a) : (b);
}
inline long long MIN3(long long a, long long b, long long c) {
return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));
}
using namespace std;
long long a[200005];
vector<vector<int> > graph(200005);
vector<pair<long long, long long> > p(200005);
int vis[200005];
void dfs(int i) {
vis[i] = 1;
int j, si = graph[i].size();
p[i].first = a[i];
for (j = 0; j < si; j++) {
int v = graph[i][j];
if (vis[v]) continue;
dfs(v);
p[i].first += p[v].first;
p[i].second = MAX2(p[i].second, p[v].second);
}
p[i].second = MAX2(p[i].first, p[i].second);
}
long long dfs2(int i) {
vis[i] = 1;
int j, si = graph[i].size();
if (i == 0 && si >= 2 || si > 2) {
long long ans = -1e17;
long long cur = 0;
multiset<long long, greater<long long> > s;
multiset<long long, greater<long long> >::iterator it;
for (j = 0; j < si; j++) {
int v = graph[i][j];
if (vis[v]) continue;
ans = MAX2(ans, dfs2(v));
s.insert(p[v].second);
}
it = s.begin();
cur += (*it);
it++;
cur += (*it);
ans = MAX2(ans, cur);
return ans;
}
for (j = 0; j < si; j++) {
int v = graph[i][j];
if (vis[v]) continue;
return dfs2(v);
}
return -1e17;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int i;
for (i = 0; i < n; i++) {
cin >> a[i];
p[i] = pair<long long, long long>(-1e17 - 1, -1e17 - 1);
}
for (i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
for (i = 0; i < n; i++) {
if (graph[i].size() > 2 || (i == 0 && graph[i].size() >= 2)) break;
}
if (i == n) {
cout << "Impossible";
} else {
memset(vis, 0, sizeof vis);
dfs(0);
memset(vis, 0, sizeof vis);
cout << dfs2(0);
}
cerr << '\n'
<< "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
<< '\n';
}
|
### 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>
inline long long MAX2(long long a, long long int b) {
return (a) > (b) ? (a) : (b);
}
inline long long MAX3(long long a, long long b, long long c) {
return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));
}
inline long long MIN2(long long a, long long b) {
return (a) < (b) ? (a) : (b);
}
inline long long MIN3(long long a, long long b, long long c) {
return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));
}
using namespace std;
long long a[200005];
vector<vector<int> > graph(200005);
vector<pair<long long, long long> > p(200005);
int vis[200005];
void dfs(int i) {
vis[i] = 1;
int j, si = graph[i].size();
p[i].first = a[i];
for (j = 0; j < si; j++) {
int v = graph[i][j];
if (vis[v]) continue;
dfs(v);
p[i].first += p[v].first;
p[i].second = MAX2(p[i].second, p[v].second);
}
p[i].second = MAX2(p[i].first, p[i].second);
}
long long dfs2(int i) {
vis[i] = 1;
int j, si = graph[i].size();
if (i == 0 && si >= 2 || si > 2) {
long long ans = -1e17;
long long cur = 0;
multiset<long long, greater<long long> > s;
multiset<long long, greater<long long> >::iterator it;
for (j = 0; j < si; j++) {
int v = graph[i][j];
if (vis[v]) continue;
ans = MAX2(ans, dfs2(v));
s.insert(p[v].second);
}
it = s.begin();
cur += (*it);
it++;
cur += (*it);
ans = MAX2(ans, cur);
return ans;
}
for (j = 0; j < si; j++) {
int v = graph[i][j];
if (vis[v]) continue;
return dfs2(v);
}
return -1e17;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int i;
for (i = 0; i < n; i++) {
cin >> a[i];
p[i] = pair<long long, long long>(-1e17 - 1, -1e17 - 1);
}
for (i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
for (i = 0; i < n; i++) {
if (graph[i].size() > 2 || (i == 0 && graph[i].size() >= 2)) break;
}
if (i == n) {
cout << "Impossible";
} else {
memset(vis, 0, sizeof vis);
dfs(0);
memset(vis, 0, sizeof vis);
cout << dfs2(0);
}
cerr << '\n'
<< "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
<< '\n';
}
```
|
#include <bits/stdc++.h>
using namespace std;
constexpr long long INF = (long long)(2 * 1e15);
vector<vector<int>> graph;
vector<long long> a;
vector<long long> dp1, dp2;
long long ans = -INF;
void dfs(int u, int pr) {
pair<long long, long long> now = make_pair(-INF, -INF);
long long curr = -INF;
dp1[u] = a[u];
for (const int& v : graph[u])
if (v != pr) {
dfs(v, u);
dp1[u] += dp1[v];
dp2[u] = max(dp2[u], max(dp1[v], dp2[v]));
curr = max(dp1[v], dp2[v]);
if (curr > now.first) {
now.second = now.first;
now.first = curr;
} else if (curr > now.second)
now.second = curr;
}
if ((graph[u].size() - (pr != -1)) > 1)
ans = max(now.first + now.second, ans);
}
int main() {
std::ios::sync_with_stdio(false);
int n, u, v;
cin >> n;
graph.resize(n);
a.resize(n);
dp1.resize(n);
dp2.assign(n, -INF);
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 1; i < n; ++i) {
cin >> u >> v;
--u;
--v;
graph[u].push_back(v);
graph[v].push_back(u);
}
dfs(0, -1);
if (ans == -INF) {
cout << "Impossible";
return 0;
}
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;
constexpr long long INF = (long long)(2 * 1e15);
vector<vector<int>> graph;
vector<long long> a;
vector<long long> dp1, dp2;
long long ans = -INF;
void dfs(int u, int pr) {
pair<long long, long long> now = make_pair(-INF, -INF);
long long curr = -INF;
dp1[u] = a[u];
for (const int& v : graph[u])
if (v != pr) {
dfs(v, u);
dp1[u] += dp1[v];
dp2[u] = max(dp2[u], max(dp1[v], dp2[v]));
curr = max(dp1[v], dp2[v]);
if (curr > now.first) {
now.second = now.first;
now.first = curr;
} else if (curr > now.second)
now.second = curr;
}
if ((graph[u].size() - (pr != -1)) > 1)
ans = max(now.first + now.second, ans);
}
int main() {
std::ios::sync_with_stdio(false);
int n, u, v;
cin >> n;
graph.resize(n);
a.resize(n);
dp1.resize(n);
dp2.assign(n, -INF);
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 1; i < n; ++i) {
cin >> u >> v;
--u;
--v;
graph[u].push_back(v);
graph[v].push_back(u);
}
dfs(0, -1);
if (ans == -INF) {
cout << "Impossible";
return 0;
}
cout << ans;
return 0;
}
```
|
#include <bits/stdc++.h>
const int N = 2e5 + 10;
using namespace std;
long long a[N], f[N], flag, res = -2e9;
int n;
vector<int> G[N];
void dfs(int u, int father) {
f[u] = a[u];
for (int i = 0; i < G[u].size(); i++) {
int j = G[u][i];
if (j == father) continue;
dfs(j, u);
f[u] += f[j];
}
}
void dfs1(int u, int father) {
long long m1 = -0x3f3f3f3f, m2 = -0x3f3f3f3f;
for (int i = 0; i < G[u].size(); i++) {
int j = G[u][i];
if (j == father) continue;
dfs1(j, u);
if (f[j] >= m1) {
m2 = m1;
m1 = f[j];
} else if (f[j] > m2) {
m2 = f[j];
}
f[u] = max(f[u], f[j]);
}
if (m1 != -0x3f3f3f3f && m2 != -0x3f3f3f3f) {
flag = 1;
res = max(res, m1 + m2);
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) scanf("%lld", &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);
}
dfs(1, 0);
dfs1(1, 0);
if (!flag)
cout << "Impossible" << endl;
else
cout << res << endl;
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>
const int N = 2e5 + 10;
using namespace std;
long long a[N], f[N], flag, res = -2e9;
int n;
vector<int> G[N];
void dfs(int u, int father) {
f[u] = a[u];
for (int i = 0; i < G[u].size(); i++) {
int j = G[u][i];
if (j == father) continue;
dfs(j, u);
f[u] += f[j];
}
}
void dfs1(int u, int father) {
long long m1 = -0x3f3f3f3f, m2 = -0x3f3f3f3f;
for (int i = 0; i < G[u].size(); i++) {
int j = G[u][i];
if (j == father) continue;
dfs1(j, u);
if (f[j] >= m1) {
m2 = m1;
m1 = f[j];
} else if (f[j] > m2) {
m2 = f[j];
}
f[u] = max(f[u], f[j]);
}
if (m1 != -0x3f3f3f3f && m2 != -0x3f3f3f3f) {
flag = 1;
res = max(res, m1 + m2);
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) scanf("%lld", &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);
}
dfs(1, 0);
dfs1(1, 0);
if (!flag)
cout << "Impossible" << endl;
else
cout << res << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
const long long INF = 1e18;
vector<int> g[MAXN];
long long a[MAXN], sum[MAXN], cnt[MAXN], dp[MAXN], ans;
void dfs(int v, int parent) {
int to;
sum[v] = a[v];
for (int i = 0; i < g[v].size(); i++) {
to = g[v][i];
if (to == parent) continue;
dfs(to, v);
sum[v] += sum[to];
}
}
void go(int v, int parent) {
int to;
pair<long long, long long> pr;
dp[v] = sum[v];
pr = make_pair(-INF, -INF);
for (int i = 0; i < g[v].size(); i++) {
to = g[v][i];
if (to == parent) continue;
go(to, v);
dp[v] = max(dp[v], dp[to]);
if (dp[to] >= pr.first) {
pr.second = pr.first;
pr.first = dp[to];
} else if (dp[to] >= pr.second) {
pr.second = dp[to];
}
}
if (pr.second != -INF) ans = max(ans, pr.first + pr.second);
}
int main() {
ios_base::sync_with_stdio(false);
int n, u, v;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
if (n == 1) {
cout << "Impossible" << endl;
return 0;
}
dfs(1, 0);
ans = -INF;
go(1, 0);
if (ans == -INF)
cout << "Impossible" << endl;
else
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 = 200005;
const long long INF = 1e18;
vector<int> g[MAXN];
long long a[MAXN], sum[MAXN], cnt[MAXN], dp[MAXN], ans;
void dfs(int v, int parent) {
int to;
sum[v] = a[v];
for (int i = 0; i < g[v].size(); i++) {
to = g[v][i];
if (to == parent) continue;
dfs(to, v);
sum[v] += sum[to];
}
}
void go(int v, int parent) {
int to;
pair<long long, long long> pr;
dp[v] = sum[v];
pr = make_pair(-INF, -INF);
for (int i = 0; i < g[v].size(); i++) {
to = g[v][i];
if (to == parent) continue;
go(to, v);
dp[v] = max(dp[v], dp[to]);
if (dp[to] >= pr.first) {
pr.second = pr.first;
pr.first = dp[to];
} else if (dp[to] >= pr.second) {
pr.second = dp[to];
}
}
if (pr.second != -INF) ans = max(ans, pr.first + pr.second);
}
int main() {
ios_base::sync_with_stdio(false);
int n, u, v;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
if (n == 1) {
cout << "Impossible" << endl;
return 0;
}
dfs(1, 0);
ans = -INF;
go(1, 0);
if (ans == -INF)
cout << "Impossible" << endl;
else
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 5;
const long long int inf = -1e15;
bool visit[MAX];
long long int DP[MAX], maxx[MAX], A[MAX], ans;
vector<int> G[MAX];
void dfs(int u, int parent) {
if (visit[u]) return;
visit[u] = 1;
DP[u] = A[u];
maxx[u] = inf;
if (G[u].size() == 1) maxx[u] = A[u];
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
dfs(v, u);
if (v != parent) {
DP[u] += DP[v];
maxx[u] = max(maxx[u], maxx[v]);
}
}
maxx[u] = max(maxx[u], DP[u]);
vector<long long int> tmp;
for (int i = 0; i < G[u].size(); i++) {
if (G[u][i] != parent) tmp.push_back(maxx[G[u][i]]);
}
if (tmp.size() >= 2) {
sort(tmp.begin(), tmp.end());
ans = max(ans, tmp[tmp.size() - 1] + tmp[tmp.size() - 2]);
}
}
int main() {
int N, i, x, y;
scanf("%d", &N);
ans = inf;
for (i = 1; i <= N; i++) {
scanf("%lld", &A[i]);
}
for (i = 1; i < N; i++) {
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
dfs(1, 0);
if (ans == inf) {
printf("Impossible\n");
} else {
printf("%lld\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;
const int MAX = 2e5 + 5;
const long long int inf = -1e15;
bool visit[MAX];
long long int DP[MAX], maxx[MAX], A[MAX], ans;
vector<int> G[MAX];
void dfs(int u, int parent) {
if (visit[u]) return;
visit[u] = 1;
DP[u] = A[u];
maxx[u] = inf;
if (G[u].size() == 1) maxx[u] = A[u];
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
dfs(v, u);
if (v != parent) {
DP[u] += DP[v];
maxx[u] = max(maxx[u], maxx[v]);
}
}
maxx[u] = max(maxx[u], DP[u]);
vector<long long int> tmp;
for (int i = 0; i < G[u].size(); i++) {
if (G[u][i] != parent) tmp.push_back(maxx[G[u][i]]);
}
if (tmp.size() >= 2) {
sort(tmp.begin(), tmp.end());
ans = max(ans, tmp[tmp.size() - 1] + tmp[tmp.size() - 2]);
}
}
int main() {
int N, i, x, y;
scanf("%d", &N);
ans = inf;
for (i = 1; i <= N; i++) {
scanf("%lld", &A[i]);
}
for (i = 1; i < N; i++) {
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
dfs(1, 0);
if (ans == inf) {
printf("Impossible\n");
} else {
printf("%lld\n", ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long v[200005];
vector<int> child[200005];
vector<int> edge[200005];
int parent[200005];
void make_tree(int idx) {
int other;
for (int i = 0; i < edge[idx].size(); i++) {
other = edge[idx][i];
if (parent[other] == -1) {
parent[other] = idx;
child[idx].push_back(other);
make_tree(other);
}
}
}
long long ans = -2e18;
pair<long long, long long> f(int idx) {
if (child[idx].size() == 0) return pair<long long, long long>(v[idx], v[idx]);
if (child[idx].size() == 1) {
pair<long long, long long> temp = f(child[idx][0]);
return pair<long long, long long>(temp.first + v[idx],
max(temp.first + v[idx], temp.second));
}
long long a, b;
a = b = -2e18;
pair<long long, long long> temp;
long long sum = v[idx];
for (int i = 0; i < child[idx].size(); i++) {
temp = f(child[idx][i]);
sum += temp.first;
if (temp.second >= a) {
b = a;
a = temp.second;
} else if (temp.second > b) {
b = temp.second;
}
}
ans = max(ans, a + b);
return pair<long long, long long>(sum, max(sum, max(a, b)));
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> v[i];
int x, y;
for (int i = 0; i < n - 1; i++) {
cin >> x >> y;
edge[x].push_back(y);
edge[y].push_back(x);
}
memset(parent, -1, sizeof parent);
parent[1] = 1;
make_tree(1);
f(1);
if (ans == -2e18)
cout << "Impossible\n";
else
cout << ans << endl;
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 v[200005];
vector<int> child[200005];
vector<int> edge[200005];
int parent[200005];
void make_tree(int idx) {
int other;
for (int i = 0; i < edge[idx].size(); i++) {
other = edge[idx][i];
if (parent[other] == -1) {
parent[other] = idx;
child[idx].push_back(other);
make_tree(other);
}
}
}
long long ans = -2e18;
pair<long long, long long> f(int idx) {
if (child[idx].size() == 0) return pair<long long, long long>(v[idx], v[idx]);
if (child[idx].size() == 1) {
pair<long long, long long> temp = f(child[idx][0]);
return pair<long long, long long>(temp.first + v[idx],
max(temp.first + v[idx], temp.second));
}
long long a, b;
a = b = -2e18;
pair<long long, long long> temp;
long long sum = v[idx];
for (int i = 0; i < child[idx].size(); i++) {
temp = f(child[idx][i]);
sum += temp.first;
if (temp.second >= a) {
b = a;
a = temp.second;
} else if (temp.second > b) {
b = temp.second;
}
}
ans = max(ans, a + b);
return pair<long long, long long>(sum, max(sum, max(a, b)));
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> v[i];
int x, y;
for (int i = 0; i < n - 1; i++) {
cin >> x >> y;
edge[x].push_back(y);
edge[y].push_back(x);
}
memset(parent, -1, sizeof parent);
parent[1] = 1;
make_tree(1);
f(1);
if (ans == -2e18)
cout << "Impossible\n";
else
cout << ans << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const long long INF = 1e15;
vector<vector<int> > g(MAXN, vector<int>());
vector<long long> sums(MAXN, 0);
vector<int> vis(MAXN, 0);
vector<int> was(MAXN, 0);
long long dfs(int u, long long& mx) {
vis[u] = 1;
long long v, currmx = -INF, val;
long long res = 0;
vector<long long> ans;
for (int i = 0; i < (int)g[u].size(); i++) {
v = g[u][i];
if (!vis[v]) {
val = dfs(v, mx);
ans.push_back(val);
currmx = max(currmx, val);
res += sums[v];
}
}
res += was[u];
sums[u] = res;
currmx = max(currmx, sums[u]);
int sz = (int)ans.size();
if (sz >= 2) {
sort(ans.begin(), ans.end());
mx = max(mx, ans[sz - 2] + ans[sz - 1]);
}
return currmx;
}
int main() {
int n, u, v;
cin >> n;
for (int i = 1; i <= n; i++) cin >> was[i];
for (int i = 1; i <= n - 1; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
long long mx = -INF;
dfs(1, mx);
if (mx == -INF)
cout << "Impossible\n";
else
cout << mx;
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 = 2e5 + 5;
const long long INF = 1e15;
vector<vector<int> > g(MAXN, vector<int>());
vector<long long> sums(MAXN, 0);
vector<int> vis(MAXN, 0);
vector<int> was(MAXN, 0);
long long dfs(int u, long long& mx) {
vis[u] = 1;
long long v, currmx = -INF, val;
long long res = 0;
vector<long long> ans;
for (int i = 0; i < (int)g[u].size(); i++) {
v = g[u][i];
if (!vis[v]) {
val = dfs(v, mx);
ans.push_back(val);
currmx = max(currmx, val);
res += sums[v];
}
}
res += was[u];
sums[u] = res;
currmx = max(currmx, sums[u]);
int sz = (int)ans.size();
if (sz >= 2) {
sort(ans.begin(), ans.end());
mx = max(mx, ans[sz - 2] + ans[sz - 1]);
}
return currmx;
}
int main() {
int n, u, v;
cin >> n;
for (int i = 1; i <= n; i++) cin >> was[i];
for (int i = 1; i <= n - 1; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
long long mx = -INF;
dfs(1, mx);
if (mx == -INF)
cout << "Impossible\n";
else
cout << mx;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200001;
vector<int> g[maxn];
long long a[maxn];
int n;
long long ans = -1e18;
bool used[maxn];
long long sum[maxn];
pair<long long, long long> md[maxn];
long long d[maxn];
void dfs(int v) {
used[v] = 1;
sum[v] = a[v];
long long m1 = -1e18;
long long m2 = m1;
for (int i = 0; i < g[v].size(); ++i) {
int to = g[v][i];
if (!used[to]) {
dfs(to);
d[v] = max(d[v], d[to]);
sum[v] += sum[to];
long long up = d[to];
if (up > m1) {
m2 = m1;
m1 = up;
} else if (up > m2)
m2 = up;
}
}
d[v] = max(d[v], sum[v]);
if (m1 != -1e18 && m2 != -1e18) ans = max(ans, m1 + m2);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
d[i] = -1e18;
md[i] = {1e18, 1e18};
cin >> a[i];
}
for (int i = 0; i < n - 1; ++i) {
int fr, to;
cin >> fr >> to;
g[fr - 1].push_back(to - 1);
g[to - 1].push_back(fr - 1);
}
dfs(0);
if (ans == -1e18)
cout << "Impossible";
else
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;
const int maxn = 200001;
vector<int> g[maxn];
long long a[maxn];
int n;
long long ans = -1e18;
bool used[maxn];
long long sum[maxn];
pair<long long, long long> md[maxn];
long long d[maxn];
void dfs(int v) {
used[v] = 1;
sum[v] = a[v];
long long m1 = -1e18;
long long m2 = m1;
for (int i = 0; i < g[v].size(); ++i) {
int to = g[v][i];
if (!used[to]) {
dfs(to);
d[v] = max(d[v], d[to]);
sum[v] += sum[to];
long long up = d[to];
if (up > m1) {
m2 = m1;
m1 = up;
} else if (up > m2)
m2 = up;
}
}
d[v] = max(d[v], sum[v]);
if (m1 != -1e18 && m2 != -1e18) ans = max(ans, m1 + m2);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
d[i] = -1e18;
md[i] = {1e18, 1e18};
cin >> a[i];
}
for (int i = 0; i < n - 1; ++i) {
int fr, to;
cin >> fr >> to;
g[fr - 1].push_back(to - 1);
g[to - 1].push_back(fr - 1);
}
dfs(0);
if (ans == -1e18)
cout << "Impossible";
else
cout << ans;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long int ans = -(long long int)(1e17 + 1);
vector<long long int> adj[(long long int)(2e5 + 5)],
p((long long int)(2e5 + 5)), dp((long long int)(2e5 + 5));
void dfs(long long int c, long long int pre) {
dp[c] = p[c];
for (auto x : adj[c]) {
if (x == pre) continue;
dfs(x, c);
dp[c] += dp[x];
}
}
void dfs1(long long int c, long long int pre) {
long long int mx = -(long long int)(1e17 + 1),
mx1 = -(long long int)(1e17 + 1);
for (auto x : adj[c]) {
if (x == pre) continue;
dfs1(x, c);
dp[c] = max(dp[c], dp[x]);
if (dp[x] > mx) {
mx1 = mx;
mx = dp[x];
} else
mx1 = max(mx1, dp[x]);
}
ans = max(ans, mx + mx1);
}
void solve() {
long long int n;
cin >> n;
for (long long int i = 1; i <= n; i++) cin >> p[i];
for (long long int i = 1; i < n; i++) {
long long int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 0);
dfs1(1, 0);
if (ans >= -2e14)
cout << ans;
else
cout << "Impossible";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t = 1;
while (t--) 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;
long long int ans = -(long long int)(1e17 + 1);
vector<long long int> adj[(long long int)(2e5 + 5)],
p((long long int)(2e5 + 5)), dp((long long int)(2e5 + 5));
void dfs(long long int c, long long int pre) {
dp[c] = p[c];
for (auto x : adj[c]) {
if (x == pre) continue;
dfs(x, c);
dp[c] += dp[x];
}
}
void dfs1(long long int c, long long int pre) {
long long int mx = -(long long int)(1e17 + 1),
mx1 = -(long long int)(1e17 + 1);
for (auto x : adj[c]) {
if (x == pre) continue;
dfs1(x, c);
dp[c] = max(dp[c], dp[x]);
if (dp[x] > mx) {
mx1 = mx;
mx = dp[x];
} else
mx1 = max(mx1, dp[x]);
}
ans = max(ans, mx + mx1);
}
void solve() {
long long int n;
cin >> n;
for (long long int i = 1; i <= n; i++) cin >> p[i];
for (long long int i = 1; i < n; i++) {
long long int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 0);
dfs1(1, 0);
if (ans >= -2e14)
cout << ans;
else
cout << "Impossible";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t = 1;
while (t--) solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long N = 2e5 + 5;
const long long INF = 1e18;
vector<long long> adj[N];
long long sum[N], mx[N];
long long a[N];
long long n;
bool vis[N];
void init() {
for (long long i = 0; i < n; i++) vis[i] = false;
}
long long findsum(long long u) {
vis[u] = true;
sum[u] = a[u];
for (long long i = 0; i < adj[u].size(); i++) {
if (!vis[adj[u][i]]) sum[u] += findsum(adj[u][i]);
}
return sum[u];
}
long long findmax(long long u) {
vis[u] = true;
for (long long i = 0; i < adj[u].size(); i++)
if (!vis[adj[u][i]]) mx[u] = max(mx[u], findmax(adj[u][i]));
return mx[u];
}
long long solve(long long u) {
vis[u] = true;
long long m1 = -1 * INF, m2 = -1 * INF;
for (long long i = 0; i < adj[u].size(); i++) {
if (!vis[adj[u][i]]) {
if (mx[adj[u][i]] > m1) {
m2 = m1;
m1 = mx[adj[u][i]];
} else if (mx[adj[u][i]] > m2)
m2 = mx[adj[u][i]];
}
}
long long ans = m1 + m2;
for (long long i = 0; i < adj[u].size(); i++)
if (!vis[adj[u][i]]) ans = max(ans, solve(adj[u][i]));
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (long long i = 0; i < n; i++) cin >> a[i];
for (long long i = 0; i < n - 1; i++) {
long long u, v;
cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
init();
sum[0] = findsum(0);
for (long long i = 0; i < n; i++) mx[i] = sum[i];
init();
mx[0] = findmax(0);
init();
long long ans = solve(0);
if (ans <= -2e10) {
cout << "Impossible\n";
return 0;
}
cout << ans << "\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;
const long long MOD = 1e9 + 7;
const long long N = 2e5 + 5;
const long long INF = 1e18;
vector<long long> adj[N];
long long sum[N], mx[N];
long long a[N];
long long n;
bool vis[N];
void init() {
for (long long i = 0; i < n; i++) vis[i] = false;
}
long long findsum(long long u) {
vis[u] = true;
sum[u] = a[u];
for (long long i = 0; i < adj[u].size(); i++) {
if (!vis[adj[u][i]]) sum[u] += findsum(adj[u][i]);
}
return sum[u];
}
long long findmax(long long u) {
vis[u] = true;
for (long long i = 0; i < adj[u].size(); i++)
if (!vis[adj[u][i]]) mx[u] = max(mx[u], findmax(adj[u][i]));
return mx[u];
}
long long solve(long long u) {
vis[u] = true;
long long m1 = -1 * INF, m2 = -1 * INF;
for (long long i = 0; i < adj[u].size(); i++) {
if (!vis[adj[u][i]]) {
if (mx[adj[u][i]] > m1) {
m2 = m1;
m1 = mx[adj[u][i]];
} else if (mx[adj[u][i]] > m2)
m2 = mx[adj[u][i]];
}
}
long long ans = m1 + m2;
for (long long i = 0; i < adj[u].size(); i++)
if (!vis[adj[u][i]]) ans = max(ans, solve(adj[u][i]));
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (long long i = 0; i < n; i++) cin >> a[i];
for (long long i = 0; i < n - 1; i++) {
long long u, v;
cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
init();
sum[0] = findsum(0);
for (long long i = 0; i < n; i++) mx[i] = sum[i];
init();
mx[0] = findmax(0);
init();
long long ans = solve(0);
if (ans <= -2e10) {
cout << "Impossible\n";
return 0;
}
cout << ans << "\n";
return 0;
}
```
|
#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) {
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 sub[node];
} else if (child.size() == 1) {
return max(sub[node], child[0]);
} else {
possible = true;
global_ans = max(global_ans, child[0] + child[1]);
return max(sub[node], 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
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 + 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) {
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 sub[node];
} else if (child.size() == 1) {
return max(sub[node], child[0]);
} else {
possible = true;
global_ans = max(global_ans, child[0] + child[1]);
return max(sub[node], 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;
long long int MI = (long long int)-1e16;
long long int a[200100];
vector<int> adj[200100];
long long int dp[200100][2];
bool vis[200100];
long long int ans[200100];
void dfs(int v) {
vis[v] = true;
long long int acum = a[v];
vector<int> ch;
for (int i = 0; i < (int)(adj[v].size()); i++) {
int w = adj[v][i];
if (!vis[w]) {
dfs(w);
ch.push_back(w);
acum += dp[w][0];
}
}
dp[v][0] = acum;
dp[v][1] = dp[v][0];
for (int i = 0; i < (int)(ch.size()); i++)
dp[v][1] = max(dp[v][1], dp[ch[i]][1]);
}
bool visi[200100];
void dfs_ans(int v) {
visi[v] = true;
vector<long long int> val;
vector<int> chil;
int ch = 0;
for (int i = 0; i < (int)(adj[v].size()); i++) {
int w = adj[v][i];
if (!visi[w]) {
chil.push_back(w);
val.push_back(dp[w][1]);
ch++;
}
}
sort(val.begin(), val.end());
for (int i = 0; i < (int)(ch); i++) {
int w = chil[i];
ans[w] = ans[v];
int c = ch - 1;
if (dp[w][1] == val[c]) c--;
if (c >= 0) {
ans[w] = max(ans[w], val[c]);
}
}
for (int i = 0; i < (int)(adj[v].size()); i++) {
int w = adj[v][i];
if (!visi[w]) {
dfs_ans(w);
}
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < (int)(n); i++) scanf("%I64d", &a[i + 1]);
int u, v;
for (int i = 0; i < (int)(n - 1); i++) {
scanf("%d %d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1);
ans[1] = MI;
dfs_ans(1);
long long int maxi = MI;
for (int i = (int)(1); i < (int)(n + 1); i++) {
if (ans[i] > MI) maxi = max(maxi, dp[i][1] + ans[i]);
}
if (maxi == MI)
cout << "Impossible" << endl;
else
cout << maxi << 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 int MI = (long long int)-1e16;
long long int a[200100];
vector<int> adj[200100];
long long int dp[200100][2];
bool vis[200100];
long long int ans[200100];
void dfs(int v) {
vis[v] = true;
long long int acum = a[v];
vector<int> ch;
for (int i = 0; i < (int)(adj[v].size()); i++) {
int w = adj[v][i];
if (!vis[w]) {
dfs(w);
ch.push_back(w);
acum += dp[w][0];
}
}
dp[v][0] = acum;
dp[v][1] = dp[v][0];
for (int i = 0; i < (int)(ch.size()); i++)
dp[v][1] = max(dp[v][1], dp[ch[i]][1]);
}
bool visi[200100];
void dfs_ans(int v) {
visi[v] = true;
vector<long long int> val;
vector<int> chil;
int ch = 0;
for (int i = 0; i < (int)(adj[v].size()); i++) {
int w = adj[v][i];
if (!visi[w]) {
chil.push_back(w);
val.push_back(dp[w][1]);
ch++;
}
}
sort(val.begin(), val.end());
for (int i = 0; i < (int)(ch); i++) {
int w = chil[i];
ans[w] = ans[v];
int c = ch - 1;
if (dp[w][1] == val[c]) c--;
if (c >= 0) {
ans[w] = max(ans[w], val[c]);
}
}
for (int i = 0; i < (int)(adj[v].size()); i++) {
int w = adj[v][i];
if (!visi[w]) {
dfs_ans(w);
}
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < (int)(n); i++) scanf("%I64d", &a[i + 1]);
int u, v;
for (int i = 0; i < (int)(n - 1); i++) {
scanf("%d %d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1);
ans[1] = MI;
dfs_ans(1);
long long int maxi = MI;
for (int i = (int)(1); i < (int)(n + 1); i++) {
if (ans[i] > MI) maxi = max(maxi, dp[i][1] + ans[i]);
}
if (maxi == MI)
cout << "Impossible" << endl;
else
cout << maxi << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> graph[200010];
long long n, value[200010];
long long score[200010];
long long mmscore[200010];
bool chk[200010], z;
long long ans = (-300000000000000LL);
long long dfs(long long node) {
chk[node] = true;
long long i, c = 0;
long long s = value[node];
long long m1 = (-300000000000000LL), m2 = (-300000000000000LL);
for (i = 0; i < graph[node].size(); ++i) {
long long x = graph[node][i];
if (chk[x]) continue;
dfs(x);
++c;
s += score[x];
if (mmscore[node] < mmscore[x]) mmscore[node] = mmscore[x];
if (m1 < mmscore[x]) {
m2 = m1;
m1 = mmscore[x];
} else if (m2 < mmscore[x]) {
m2 = mmscore[x];
}
}
if (c > 1) z = true;
score[node] = s;
if (mmscore[node] < s) mmscore[node] = s;
if (m1 + m2 > ans && c > 1) ans = m1 + m2;
chk[node] = false;
return s;
}
int main() {
long long i;
scanf("%lld", &n);
if (n <= 2) {
printf("Impossible");
return 0;
}
for (i = 1; i <= n; ++i) {
scanf("%lld", &value[i]);
mmscore[i] = (-300000000000000LL);
}
for (i = 1; i < n; ++i) {
long long x, y;
scanf("%lld %lld", &x, &y);
graph[x].push_back(y);
graph[y].push_back(x);
}
dfs(1);
if (!z) {
printf("Impossible");
return 0;
}
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;
vector<long long> graph[200010];
long long n, value[200010];
long long score[200010];
long long mmscore[200010];
bool chk[200010], z;
long long ans = (-300000000000000LL);
long long dfs(long long node) {
chk[node] = true;
long long i, c = 0;
long long s = value[node];
long long m1 = (-300000000000000LL), m2 = (-300000000000000LL);
for (i = 0; i < graph[node].size(); ++i) {
long long x = graph[node][i];
if (chk[x]) continue;
dfs(x);
++c;
s += score[x];
if (mmscore[node] < mmscore[x]) mmscore[node] = mmscore[x];
if (m1 < mmscore[x]) {
m2 = m1;
m1 = mmscore[x];
} else if (m2 < mmscore[x]) {
m2 = mmscore[x];
}
}
if (c > 1) z = true;
score[node] = s;
if (mmscore[node] < s) mmscore[node] = s;
if (m1 + m2 > ans && c > 1) ans = m1 + m2;
chk[node] = false;
return s;
}
int main() {
long long i;
scanf("%lld", &n);
if (n <= 2) {
printf("Impossible");
return 0;
}
for (i = 1; i <= n; ++i) {
scanf("%lld", &value[i]);
mmscore[i] = (-300000000000000LL);
}
for (i = 1; i < n; ++i) {
long long x, y;
scanf("%lld %lld", &x, &y);
graph[x].push_back(y);
graph[y].push_back(x);
}
dfs(1);
if (!z) {
printf("Impossible");
return 0;
}
printf("%lld\n", ans);
return 0;
}
```
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.