JerryL/AlgoSimBench huggingface dataset:
277_E. Binary Tree on Plane

A root tree is a directed acyclic graph that contains one node (root), from which there is exactly one path to any other node.

A root tree is binary if each node has at most two outgoing arcs.

When a binary tree is painted on the plane, all arcs should be directed from top to bottom. That is, each arc going from u to v must meet the condition yu > yv.

You've been given the coordinates of all tree nodes. Your task is to connect these nodes by arcs so as to get the binary root tree and make the total length of the arcs minimum. All arcs of the built tree must be directed from top to bottom.

Input

The first line contains a single integer n (2 ≤ n ≤ 400) — the number of nodes in the tree. Then follow n lines, two integers per line: xi, yi (|xi|, |yi| ≤ 103) — coordinates of the nodes. It is guaranteed that all points are distinct.

Output

If it is impossible to build a binary root tree on the given points, print "-1". Otherwise, print a single real number — the total length of the arcs in the minimum binary tree. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6. 

Examples

Input

3
0 0
1 0
2 1


Output

3.650281539872885


Input

4
0 0
1 0
2 1
2 0


Output

-1

Example input:
3
0 0
1 0
2 1

4
0 0
1 0
2 1
2 0


Example output:
3.650282

-1


Tags: flows
trees
Difficulty rating: 2400

Solution:
#include <bits/stdc++.h>
using namespace std;
int E[810 * 810], N[810 * 810], C[810 * 810];
int x[810], pre[810], y[810], q[810 * 810], fa[810];
int tem, n, t, j, i, s, T;
bool vis[810];
double dis[810], S[810 * 810], ans;
inline void add(int a, int b, int c, double d) {
  E[++t] = b;
  N[t] = fa[a];
  C[t] = c;
  S[t] = d;
  fa[a] = t;
  E[++t] = a;
  N[t] = fa[b];
  C[t] = 0;
  S[t] = -d;
  fa[b] = t;
}
inline bool CM() {
  int u, i, t = 0, w = 1, x;
  for (i = s; i <= T; i++) dis[i] = 1e20, vis[i] = 0, pre[i] = -1;
  q[1] = s;
  dis[s] = 0;
  vis[s] = 1;
  for (; t < w;) {
    x = q[++t];
    vis[x] = 0;
    for (i = fa[x]; i; i = N[i])
      if (C[i]) {
        u = E[i];
        if (dis[u] > dis[x] + S[i]) {
          dis[u] = dis[x] + S[i];
          pre[u] = i;
          if (!vis[u]) vis[u] = 1, q[++w] = u;
        }
      }
  }
  if (dis[T] > 1e15) return 0;
  for (i = T; i != s; i = E[i ^ 1]) {
    i = pre[i];
    C[i]--;
    C[i ^ 1]++;
  }
  tem++;
  ans += dis[T];
  return 1;
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d%d", &x[i], &y[i]);
  s = 0;
  T = 2 * n + 1;
  t = 1;
  for (i = 1; i <= n; i++) {
    add(s, i, 2, 0);
    add(i + n, T, 1, 0);
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      if (y[i] > y[j])
        add(i, j + n, 1,
            sqrt((x[i] - x[j]) * (x[i] - x[j]) +
                 (y[i] - y[j]) * (y[i] - y[j])));
  ans = 0;
  while (CM())
    ;
  if (tem != n - 1)
    printf("-1\n");
  else
    printf("%.8lf\n", ans);
  return 0;
}
478_D. Red-Green Towers

There are r red and g green blocks for construction of the red-green tower. Red-green tower can be built following next rules:

  * Red-green tower is consisting of some number of levels;

  * Let the red-green tower consist of n levels, then the first level of this tower should consist of n blocks, second level — of n - 1 blocks, the third one — of n - 2 blocks, and so on — the last level of such tower should consist of the one block. In other words, each successive level should contain one block less than the previous one;

  * Each level of the red-green tower should contain blocks of the same color.

<image>

Let h be the maximum possible number of levels of red-green tower, that can be built out of r red and g green blocks meeting the rules above. The task is to determine how many different red-green towers having h levels can be built out of the available blocks.

Two red-green towers are considered different if there exists some level, that consists of red blocks in the one tower and consists of green blocks in the other tower.

You are to write a program that will find the number of different red-green towers of height h modulo 109 + 7.

Input

The only line of input contains two integers r and g, separated by a single space — the number of available red and green blocks respectively (0 ≤ r, g ≤ 2·105, r + g ≥ 1).

Output

Output the only integer — the number of different possible red-green towers of height h modulo 109 + 7.

Examples

Input

4 6


Output

2


Input

9 7


Output

6


Input

1 1


Output

2

Note

The image in the problem statement shows all possible red-green towers for the first sample.

Example input:
1 1

4 6

9 7


Example output:
2

2

6


Tags: dp
Difficulty rating: 2000

Solution:
#include <bits/stdc++.h>
long long X[200001], A, B, M = 1000000007, h, a, Z;
int main() {
  std::cin >> A >> B;
  X[0] = X[1] = 1;
  for (h = 2; h * (h + 1) / 2 <= A + B; h++)
    for (a = A; a >= h; a--) X[a] = (X[a] + X[a - h]) % M;
  for (a = 0; a <= A; a++)
    if (h * (h - 1) / 2 - a <= B) Z = (Z + X[a]) % M;
  std::cout << Z;
}
17_C. Balance

Nick likes strings very much, he likes to rotate them, sort them, rearrange characters within a string... Once he wrote a random string of characters a, b, c on a piece of paper and began to perform the following operations: 

  * to take two adjacent characters and replace the second character with the first one, 
  * to take two adjacent characters and replace the first character with the second one 



To understand these actions better, let's take a look at a string «abc». All of the following strings can be obtained by performing one of the described operations on «abc»: «bbc», «abb», «acc». Let's denote the frequency of a character for each of the characters a, b and c as the number of occurrences of this character in the string. For example, for string «abc»: |a| = 1, |b| = 1, |c| = 1, and for string «bbc»: |a| = 0, |b| = 2, |c| = 1. 

While performing the described operations, Nick sometimes got balanced strings. Let's say that a string is balanced, if the frequencies of each character differ by at most 1. That is  - 1 ≤ |a| - |b| ≤ 1,  - 1 ≤ |a| - |c| ≤ 1 и  - 1 ≤ |b| - |c| ≤ 1. 

Would you help Nick find the number of different balanced strings that can be obtained by performing the operations described above, perhaps multiple times, on the given string s. This number should be calculated modulo 51123987.

Input

The first line contains integer n (1 ≤ n ≤ 150) — the length of the given string s. Next line contains the given string s. The initial string can be balanced as well, in this case it should be counted too. The given string s consists only of characters a, b and c.

Output

Output the only number — the number of different balanced strings that can be obtained by performing the described operations, perhaps multiple times, on the given string s, modulo 51123987.

Examples

Input

4
abca


Output

7


Input

4
abbc


Output

3


Input

2
ab


Output

1

Note

In the first sample it is possible to get 51 different strings through the described operations, but only 7 of them are balanced: «abca», «bbca», «bcca», «bcaa», «abcc», «abbc», «aabc». In the second sample: «abbc», «aabc», «abcc». In the third sample there is only one balanced string — «ab» itself.

Example input:
4
abca

2
ab

4
abbc


Example output:
7

1

3


Tags: dp
Difficulty rating: 2500

Solution:
#include <bits/stdc++.h>
using namespace std;
int n, m, c, f[152][52][52][52], t[3][152], a;
string s;
int main() {
  cin >> n >> s;
  for (int i = n; i; i--) {
    for (int j = 0; j <= (2); j++) t[j][i] = t[j][i + 1];
    t[s[i - 1] - 'a'][i] = i;
  }
  for (int l = f[1][0][0][0] = 1; l <= (n); l++)
    for (int i = 0; i <= (m = (n + 2) / 3); i++)
      for (int j = 0; j <= (m); j++)
        for (int k = 0; k <= (m); k++)
          (a += (i + j + k == n) * (abs(i - j) <= 1) * (abs(j - k) <= 1) *
                (abs(i - k) <= 1) * (c = f[l][i][j][k])) %= 51123987,
              ((f[t[0][l]][i + 1][j][k] += c) %= 51123987),
              ((f[t[1][l]][i][j + 1][k] += c) %= 51123987),
              ((f[t[2][l]][i][j][k + 1] += c) %= 51123987);
  cout << a;
}
p03717 AtCoder Regular Contest 074 - RGB Sequence

There are N squares arranged in a row. The squares are numbered 1, 2, ..., N, from left to right.

Snuke is painting each square in red, green or blue. According to his aesthetic sense, the following M conditions must all be satisfied. The i-th condition is:

* There are exactly x_i different colors among squares l_i, l_i + 1, ..., r_i.



In how many ways can the squares be painted to satisfy all the conditions? Find the count modulo 10^9+7.

Constraints

* 1 ≤ N ≤ 300
* 1 ≤ M ≤ 300
* 1 ≤ l_i ≤ r_i ≤ N
* 1 ≤ x_i ≤ 3

Input

Input is given from Standard Input in the following format:


N M
l_1 r_1 x_1
l_2 r_2 x_2
:
l_M r_M x_M


Output

Print the number of ways to paint the squares to satisfy all the conditions, modulo 10^9+7.

Examples

Input

3 1
1 3 3


Output

6


Input

4 2
1 3 1
2 4 2


Output

6


Input

1 3
1 1 1
1 1 2
1 1 3


Output

0


Input

8 10
2 6 2
5 5 1
3 5 2
4 7 3
4 4 1
2 3 1
7 7 1
1 5 2
1 7 3
3 4 2


Output

108

Example input:
3 1
1 3 3
8 10
2 6 2
5 5 1
3 5 2
4 7 3
4 4 1
2 3 1
7 7 1
1 5 2
1 7 3
3 4 2
1 3
1 1 1
1 1 2
1 1 3
4 2
1 3 1
2 4 2

Example output:
6
108
0
6

Difficulty rating: 0

Solution:
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
using P = pair<int,int>;
const int MOD = 1e9+7;
int n, m, l, r, x;
long long dp[305][305] = {1}, ans;
vector<P> p[305];
int main(){
	cin >> n >> m;
	rep(i,m){
		cin >> l >> r >> x;
		p[r-1].push_back(P(l,x));
	}
	rep(i,n){
		rep(j,i) rep(k,i) (dp[i][j] += dp[max(j,k)][min(j,k)]) %= MOD;
		for(P t: p[i]){
			tie(l,x) = t;
			rep(j,i+1) rep(k,j+1) if(x+(j<l)+(k<l)-3) dp[j][k] = 0;
		}
	}
	rep(i,n) rep(j,i) (ans += 6*dp[i][j]) %= MOD;
	cout << (ans + dp[0][0]*3) % MOD;
}
629_D. Babaei and Birthday Cake

As you know, every birthday party has a cake! This time, Babaei is going to prepare the very special birthday party's cake.

Simple cake is a cylinder of some radius and height. The volume of the simple cake is equal to the volume of corresponding cylinder. Babaei has n simple cakes and he is going to make a special cake placing some cylinders on each other.

However, there are some additional culinary restrictions. The cakes are numbered in such a way that the cake number i can be placed only on the table or on some cake number j where j < i. Moreover, in order to impress friends Babaei will put the cake i on top of the cake j only if the volume of the cake i is strictly greater than the volume of the cake j.

Babaei wants to prepare a birthday cake that has a maximum possible total volume. Help him find this value.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of simple cakes Babaei has.

Each of the following n lines contains two integers ri and hi (1 ≤ ri, hi ≤ 10 000), giving the radius and height of the i-th cake.

Output

Print the maximum volume of the cake that Babaei can make. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.

Examples

Input

2
100 30
40 10


Output

942477.796077000


Input

4
1 1
9 7
1 4
10 7


Output

3983.539484752

Note

In first sample, the optimal way is to choose the cake number 1.

In second sample, the way to get the maximum volume is to use cakes with indices 1, 2 and 4.

Example input:
4
1 1
9 7
1 4
10 7

2
100 30
40 10


Example output:
3983.5394848

942477.7960769


Tags: data structures
dp
Difficulty rating: 2000

Solution:
from sys import *
t = list(map(int, stdin.read().split()))
p = [t[i + 1] * t[i] ** 2 for i in range(1, len(t), 2)]
k = {v: j for j, v in enumerate(sorted(set(p)))}
d = [0] * (len(k) + 1)
for v in p: 
    j = k[v]
    i = j + 1
    q = 0
    while j > 0:
        q = max(d[j], q)
        j -= j & -j
    q += v
    while i < len(d):
        d[i] = max(d[i], q)
        i += i & -i
print(max(d) * 3.14159265)
479_E. Riding in a Lift

Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.

Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y ≠ x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.

Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).

Input

The first line of the input contains four space-separated integers n, a, b, k (2 ≤ n ≤ 5000, 1 ≤ k ≤ 5000, 1 ≤ a, b ≤ n, a ≠ b).

Output

Print a single integer — the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).

Examples

Input

5 2 4 1


Output

2


Input

5 2 4 2


Output

2


Input

5 3 4 1


Output

0

Note

Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 ≤ j ≤ k), that pj ≠ qj.

Notes to the samples:

  1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|. 
  2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip. 
  3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. 

Example input:
5 3 4 1

5 2 4 1

5 2 4 2


Example output:
0

2

2


Tags: combinatorics
dp
Difficulty rating: 1900

Solution:
import sys
input=sys.stdin.readline
def solve(n,st,k):
    mod=10**9+7
    dp=[0]*(n+1)
    pre=[0]*(n+1)
    dp[st]=1
    for t in range(k):
        pre[0]=0
        for i in range(1,n+1):
            pre[i]=pre[i-1]+dp[i]
            pre[i]%=mod
        for i in range(1,n+1):
            dp[i]=(pre[n]-pre[i]+pre[i-1]-pre[i>>1])%mod
    return sum(dp)%mod
n,a,b,k=map(int,input().split())
mod=10**9+7
if a>b:
    print(solve(n-b,a-b,k))
else:
    print(solve(b-1,b-a,k))
797_F. Mice and Holes

One day Masha came home and noticed n mice in the corridor of her flat. Of course, she shouted loudly, so scared mice started to run to the holes in the corridor.

The corridor can be represeted as a numeric axis with n mice and m holes on it. ith mouse is at the coordinate xi, and jth hole — at coordinate pj. jth hole has enough room for cj mice, so not more than cj mice can enter this hole.

What is the minimum sum of distances that mice have to go through so that they all can hide in the holes? If ith mouse goes to the hole j, then its distance is |xi - pj|.

Print the minimum sum of distances.

Input

The first line contains two integer numbers n, m (1 ≤ n, m ≤ 5000) — the number of mice and the number of holes, respectively.

The second line contains n integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109), where xi is the coordinate of ith mouse.

Next m lines contain pairs of integer numbers pj, cj ( - 109 ≤ pj ≤ 109, 1 ≤ cj ≤ 5000), where pj is the coordinate of jth hole, and cj is the maximum number of mice that can hide in the hole j.

Output

Print one integer number — the minimum sum of distances. If there is no solution, print -1 instead.

Examples

Input

4 5
6 2 8 9
3 6
2 1
3 6
4 7
4 7


Output

11


Input

7 2
10 20 30 40 50 45 35
-1000000000 10
1000000000 1


Output

7000000130

Example input:
7 2
10 20 30 40 50 45 35
-1000000000 10
1000000000 1

4 5
6 2 8 9
3 6
2 1
3 6
4 7
4 7


Example output:
7000000130

11


Tags: data structures
dp
greedy
sortings
Difficulty rating: 2600

Solution:
#include <bits/stdc++.h>
using namespace std;
int n, m, a[5050], x, y, hea, tai;
long long f[5050], s[5050], q[5050], g[5050];
map<int, int> mp;
void add(long long x) {
  for (; hea <= tai && q[tai] > x;) --tai;
  q[++tai] = x;
}
void del(long long x) {
  if (q[hea] == x) ++hea;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= m; ++i) cin >> x >> y, mp[x] += y;
  memset(f, 33, sizeof f);
  f[0] = 0;
  for (auto it : mp) {
    for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + abs(a[i] - it.first);
    hea = 1, tai = 0;
    for (int i = 0; i <= n; ++i) {
      add(f[i] - s[i]);
      if (i > it.second) del(f[i - it.second - 1] - s[i - it.second - 1]);
      g[i] = q[hea] + s[i];
    }
    memcpy(f, g, sizeof f);
  }
  cout << (f[n] > 1e17 ? -1 : f[n]);
}
p02549 AtCoder Beginner Contest 179 - Leaping Tak

There are N cells arranged in a row, numbered 1, 2, \ldots, N from left to right.

Tak lives in these cells and is currently on Cell 1. He is trying to reach Cell N by using the procedure described below.

You are given an integer K that is less than or equal to 10, and K non-intersecting segments [L_1, R_1], [L_2, R_2], \ldots, [L_K, R_K]. Let S be the union of these K segments. Here, the segment [l, r] denotes the set consisting of all integers i that satisfy l \leq i \leq r.

* When you are on Cell i, pick an integer d from S and move to Cell i + d. You cannot move out of the cells.



To help Tak, find the number of ways to go to Cell N, modulo 998244353.

Constraints

* 2 \leq N \leq 2 \times 10^5
* 1 \leq K \leq \min(N, 10)
* 1 \leq L_i \leq R_i \leq N
* [L_i, R_i] and [L_j, R_j] do not intersect (i \neq j)
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N K
L_1 R_1
L_2 R_2
:
L_K R_K


Output

Print the number of ways for Tak to go from Cell 1 to Cell N, modulo 998244353.

Examples

Input

5 2
1 1
3 4


Output

4


Input

5 2
3 3
5 5


Output

0


Input

5 1
1 2


Output

5


Input

60 3
5 8
1 3
10 15


Output

221823067

Example input:
5 2
3 3
5 5
5 1
1 2
60 3
5 8
1 3
10 15
5 2
1 1
3 4

Example output:
0
5
221823067
4

Difficulty rating: 0

Solution:
(n,_),*s=[[*map(int,t.split())]for t in open(0)]
d=[1,-1]+[0]*2*n
for i in range(n):
  d[i]=(d[i-1]+d[i])%998244353
  for l,r in s:
    d[i+l]+=d[i]
    d[i-~r]-=d[i]
print(d[n-1])
852_B. Neural Network country

Due to the recent popularity of the Deep learning new countries are starting to look like Neural Networks. That is, the countries are being built deep with many layers, each layer possibly having many cities. They also have one entry, and one exit point.

There are exactly L layers, each having N cities. Let us look at the two adjacent layers L1 and L2. Each city from the layer L1 is connected to each city from the layer L2 with the traveling cost cij for <image>, and each pair of adjacent layers has the same cost in between their cities as any other pair (they just stacked the same layers, as usual). Also, the traveling costs to each city from the layer L2 are same for all cities in the L1, that is cij is the same for <image>, and fixed j.

Doctor G. needs to speed up his computations for this country so he asks you to find the number of paths he can take from entry to exit point such that his traveling cost is divisible by given number M.

Input

The first line of input contains N (1 ≤ N ≤ 106), L (2 ≤ L ≤ 105) and M (2 ≤ M ≤ 100), the number of cities in each layer, the number of layers and the number that travelling cost should be divisible by, respectively.

Second, third and fourth line contain N integers each denoting costs 0 ≤ cost ≤ M from entry point to the first layer, costs between adjacent layers as described above, and costs from the last layer to the exit point.

Output

Output a single integer, the number of paths Doctor G. can take which have total cost divisible by M, modulo 109 + 7.

Example

Input

2 3 13
4 6
2 1
3 4


Output

2

Note

<image>

This is a country with 3 layers, each layer having 2 cities. Paths <image>, and <image> are the only paths having total cost divisible by 13. Notice that input edges for layer cities have the same cost, and that they are same for all layers.

Example input:
2 3 13
4 6
2 1
3 4


Example output:
2


Tags: dp
matrices
Difficulty rating: 2000

Solution:
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long n, m, k, x, f[1000001];
struct qwq {
  long long x[105] = {0};
  qwq operator*(qwq a) {
    qwq ans;
    for (long long i = 0; i < m; i++)
      for (long long j = 0; j < m; j++)
        ans.x[(i + j) % m] += x[i] * a.x[j], ans.x[(i + j) % m] %= mod;
    return ans;
  }
} a, b, c;
qwq qaq(qwq x, long long y) {
  qwq z;
  z.x[0] = 1;
  while (y) {
    if (y & 1) z = z * x;
    x = x * x;
    y >>= 1;
  }
  return z;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k >> m;
  for (long long i = 1; i <= n; i++) cin >> x, a.x[x % m]++;
  for (long long i = 1; i <= n; i++) cin >> x, b.x[x % m]++, f[i] = x;
  for (long long i = 1; i <= n; i++) cin >> x, c.x[(x + f[i]) % m]++;
  b = a * qaq(b, k - 2) * c;
  cout << b.x[0];
  return 0;
}
1324_F. Maximum White Subtree

You are given a tree consisting of n vertices. A tree is a connected undirected graph with n-1 edges. Each vertex v of this tree has a color assigned to it (a_v = 1 if the vertex v is white and 0 if the vertex v is black).

You have to solve the following problem for each vertex v: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex v? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains cnt_w white vertices and cnt_b black vertices, you have to maximize cnt_w - cnt_b.

Input

The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of vertices in the tree.

The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 1), where a_i is the color of the i-th vertex.

Each of the next n-1 lines describes an edge of the tree. Edge i is denoted by two integers u_i and v_i, the labels of vertices it connects (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i).

It is guaranteed that the given edges form a tree.

Output

Print n integers res_1, res_2, ..., res_n, where res_i is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex i.

Examples

Input


9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9


Output


2 2 2 2 2 1 1 0 2 


Input


4
0 0 1 0
1 2
1 3
1 4


Output


0 -1 1 -1 

Note

The first example is shown below:

<image>

The black vertices have bold borders.

In the second example, the best subtree for vertices 2, 3 and 4 are vertices 2, 3 and 4 correspondingly. And the best subtree for the vertex 1 is the subtree consisting of vertices 1 and 3.

Example input:
9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9

4
0 0 1 0
1 2
1 3
1 4


Example output:
2 2 2 2 2 1 1 0 2 
0 -1 1 -1 

Tags: dfs and similar
dp
graphs
trees
Difficulty rating: 1800

Solution:
#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 10;
int n, dp[M], ans[M];
vector<int> g[M];
void dfs(int v, int par) {
  for (auto u : g[v])
    if (u != par) dfs(u, v), dp[v] += max(dp[u], 0);
}
void dfs2(int v, int par, int up) {
  ans[v] = dp[v] + up;
  for (auto u : g[v])
    if (u != par) dfs2(u, v, max(0, up + dp[v] - max(0, dp[u])));
}
int32_t main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> dp[i];
    if (!dp[i]) dp[i]--;
  }
  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);
  dfs2(1, 1, 0);
  for (int i = 1; i <= n; i++) cout << ans[i] << " ";
}