description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | def solve():
ans = [sum(w)]
for i in range(n - 2):
ans.append(ans[-1] + weight[i])
return ans
for T in range(int(input())):
n = int(input())
w = list(map(int, input().split()))
weight = []
all_ = [0] * (n + 1)
for i in range(n - 1):
x, y = map(int, input().split())
all_[x] += 1
all_[y] += 1
for i in range(1, len(all_)):
if all_[i] > 1:
weight.extend([w[i - 1] for j in range(all_[i] - 1)])
weight.sort(reverse=True)
print(*solve()) | FUNC_DEF ASSIGN VAR LIST FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | def sorting_func(x):
return x[0]
t = int(input())
for _ in range(t):
n = int(input())
w = list(map(int, input().split()))
c = [(0) for _ in range(n)]
for _ in range(n - 1):
a, b = map(int, input().split())
a, b = a - 1, b - 1
c[a] += 1
c[b] += 1
v = [[w[i], c[i] - 1] for i in range(n)]
v.sort(reverse=True, key=sorting_func)
ans = [sum(w)]
for i in range(n):
while v[i][1] > 0:
ans.append(ans[-1] + v[i][0])
v[i][1] -= 1
print(*ans) | FUNC_DEF RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST VAR VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
w = list(map(int, input().split()))
edges = [list(map(int, input().split())) for i in range(n - 1)]
degs = [0] * n
sum_ = sum(w)
for u, v in edges:
u -= 1
v -= 1
degs[u] += 1
degs[v] += 1
dup = []
for i in range(n):
for _ in range(degs[i] - 1):
dup.append(w[i])
dup.sort(reverse=True)
ans = [sum_]
for i in range(n - 2):
ans.append(ans[-1] + dup[i])
print(*ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
x = [[-1, i + 1] for i in range(n)]
ans = [sum(a)]
for i in range(n - 1):
u, v = map(int, input().split())
x[u - 1][0] += 1
x[v - 1][0] += 1
tmp = []
for i in range(len(x)):
if x[i][0] != 0:
tmp.append([a[i], x[i][1] - 1])
tmp.sort(reverse=True)
j = 0
i = 0
while i < n - 2:
ans.append(tmp[j][0] + ans[-1])
x[tmp[j][1]][0] -= 1
if x[tmp[j][1]][0] == 0:
j += 1
i += 1
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | t = int(input())
for Z in range(t):
n = int(input())
l = list(map(int, input().split()))
com = list()
al = set()
for z in range(n - 1):
x, y = map(int, input().split())
if x in al:
com.append(x)
else:
al.add(x)
if y in al:
com.append(y)
else:
al.add(y)
vt = []
for i in com:
vt.append(l[i - 1])
vt.sort(reverse=True)
total = sum(l)
curr = total
ans = ""
addup = 0
ind = 0
for i in range(n - 1):
curr += addup
ans += str(curr) + " "
if ind < len(vt):
addup = vt[ind]
ind += 1
print(ans.strip()) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | from sys import stdin, stdout
t = int(stdin.readline())
for i in range(t):
n = int(stdin.readline())
vis = [0] * n
x = list(map(int, stdin.readline().split()))
z = sum(x)
stdout.write(str(z) + " ")
l = []
for i in range(n - 1):
a, b = map(int, stdin.readline().split())
if vis[a - 1] == 1:
l.append(x[a - 1])
else:
vis[a - 1] = 1
if vis[b - 1] == 1:
l.append(x[b - 1])
else:
vis[b - 1] = 1
l.sort(reverse=True)
for i in range(n - 2):
z += l[i]
stdout.write(str(z) + " ")
stdout.write("\n") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | import sys
def main():
n = int(input())
dct = dict()
weights = list(map(int, input().split()))
for i in range(n - 1):
u, v = map(int, input().split())
if not u in dct:
dct[u] = 1
else:
dct[u] += 1
if not v in dct:
dct[v] = 1
else:
dct[v] += 1
lst = []
sm = 0
for i in dct:
sm += weights[i - 1]
if dct[i] > 1:
lst.append([weights[i - 1], dct[i] - 1])
lst = sorted(lst, key=lambda x: x[0], reverse=True)
cur_ind = 0
get = 0
res = str()
res += str(sm)
while get != n - 2:
res += " "
if cur_ind >= len(lst):
res += str(sm)
elif lst[cur_ind][1] == 0:
cur_ind += 1
if cur_ind >= len(lst):
res += str(sm)
else:
sm += lst[cur_ind][0]
lst[cur_ind][1] -= 1
res += str(sm)
get += 1
print(res)
t = int(input())
for i in range(t):
main() | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR STRING IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | t = int(input())
def s(a, b, c):
p = (a + b + c) / 2
return (p * (p - a) * (p - b) * (p - c)) ** 0.5
def Madara(a, b):
c = abs(a - b)
a = (1 + a**2) ** 0.5
b = (1 + b**2) ** 0.5
return round(s(a, b, c), 5)
def Kurisu():
t = int(input()) - 1
v = list(map(int, input().split()))
c = [-2] * len(v)
s = []
for i in range(t):
q1, q2 = map(int, input().split())
q1 -= 1
q2 -= 1
if c[q1] == -2:
c[q1] = -1
else:
s.append(v[q1])
if c[q2] == -2:
c[q2] = -1
else:
s.append(v[q2])
s.sort(reverse=True)
z = [sum(v)]
for i in range(len(s)):
z.append(z[-1] + s[i])
return " ".join(list(map(str, z)))
z = []
for i in range(t):
z.append(Kurisu())
print("\n".join(z)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | N = 100005
w = [(0) for _ in range(N)]
d = [(0) for _ in range(N)]
casos = int(input())
for x in range(casos):
n = int(input())
trabalhos = list(map(int, input().split()))
for i in range(1, n + 1):
w[i] = trabalhos[i - 1]
d[i] = 0
for i in range(1, n):
u, v = list(map(int, input().split()))
d[u] += 1
d[v] += 1
resposta = 0
v = []
for i in range(1, n + 1):
resposta += w[i]
for j in range(1, d[i]):
v.append(w[i])
v.sort(reverse=True)
saida = ""
saida += str(resposta) + " "
for x in v:
resposta += x
saida += str(resposta) + " "
print(saida) | ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING VAR BIN_OP FUNC_CALL VAR VAR STRING FOR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
wp = []
for i in range(n):
wp.append([a[i], i + 1])
wp.sort(reverse=True)
edges = [[] for i in range(n + 1)]
for i in range(n - 1):
u, v = map(int, input().split())
edges[u].append(v)
edges[v].append(u)
s = sum(a)
ans = [s]
i = 0
c = 1
p = 0
while c < n - 1:
for j in range(len(edges[wp[i][1]]) - 1):
s = s + wp[i][0]
ans.append(s)
c = c + 1
if c == n - 1:
p = 1
break
i = i + 1
if p == 1:
break
print(*ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | t = int(input())
for _ in range(t):
def main():
out = []
n = int(input())
nums = list(map(int, input().split()))
edge = [-1] * n
for i in range(n - 1):
a, b = map(int, input().split())
edge[a - 1] += 1
edge[b - 1] += 1
cur = sum(nums)
out.append(cur)
mid = sorted([(nums[i], i) for i in range(n) if edge[i]])
ne = len(mid) - 1
for i in range(n - 2):
m = mid[ne][1]
while not edge[m]:
ne -= 1
m = mid[ne][1]
edge[m] -= 1
cur += nums[m]
out.append(cur)
print(" ".join(map(str, out)))
main() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | import sys
stdin = list(sys.stdin)
t = int(stdin[0].strip())
j = 1
for i in range(t):
n = int(stdin[j].strip())
j += 1
weights = [int(u) for u in stdin[j].strip().split(" ")]
degrees = [0] * n
for z in range(n - 1):
j += 1
inp = [int(u) for u in stdin[j].strip().split(" ")]
degrees[inp[0] - 1] += 1
degrees[inp[1] - 1] += 1
s = ""
su = sum(weights)
pairs = [[weights[v], degrees[v]] for v in range(n)]
pairs.sort()
s += str(su) + " "
for k in range(n - 2):
while len(pairs) > 0 and pairs[-1][1] < 2:
pairs.pop()
pairs[-1][1] -= 1
su += pairs[-1][0]
s += str(su) + " "
s = s[:-1]
print(s)
j += 1 | IMPORT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
b = [0] * n
for i in range(n - 1):
h, k = map(int, input().split())
b[h - 1] += 1
b[k - 1] += 1
x = [0] * (n - 2)
l = 0
for j in range(n):
if b[j] > 1:
for p in range(b[j] - 1):
x[l] = a[j]
l += 1
x.sort(reverse=True)
m = [0] * (n - 1)
m[0] = sum(a)
for z in range(1, n - 1):
m[z] = m[z - 1] + x[z - 1]
print(*m) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
w = list(map(int, input().split()))
tree = [[] for _ in range(n + 1)]
s = sum(w)
ans = []
for _ in range(n - 1):
u, v = map(int, input().split())
tree[u].append(v)
tree[v].append(u)
deg = [0] * (n + 1)
for i in range(1, n + 1):
deg[i] = len(tree[i])
deg = list(enumerate(deg))
mul_deg = []
for i in range(1, n + 1):
if deg[i][1] > 1:
mul_deg.append((deg[i][0], deg[i][1], w[deg[i][0] - 1]))
if not mul_deg:
print(s)
continue
mul_deg = sorted(mul_deg, key=lambda x: -x[2])
weights = []
for v, deg, weight in mul_deg:
weights.extend([weight] * (deg - 1))
ans.append(s)
for i in range(len(weights)):
if i == 0:
ans.append(s + weights[i])
continue
weights[i] += weights[i - 1]
ans.append(s + weights[i])
print(*ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | t = int(input())
for z in range(t):
n = int(input())
arr = [int(i) for i in input().split()]
s = 0
for i in arr:
s += i
a = [(0) for i in range(n)]
c = []
for i in range(n - 1):
x, y = [(int(i) - 1) for i in input().split()]
a[x] += 1
a[y] += 1
if a[x] > 1:
c.append(arr[x])
if a[y] > 1:
c.append(arr[y])
c.sort()
print(s, end=" ")
for i in range(len(c) - 1, -1, -1):
s += c[i]
print(s, end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | import sys
input = sys.stdin.readline
def inp():
return int(input())
def inlt():
return list(map(int, input().split()))
def insr():
s = input()
return list(s[: len(s) - 1])
def invr():
return map(int, input().split())
test = inp()
for i in range(test):
number = inp()
wp = inlt()
ep = []
dp = [0] * (number + 1)
for j in range(number - 1):
ap, bp = invr()
ap, bp = ap - 1, bp - 1
dp[ap] += 1
dp[bp] += 1
if dp[ap] > 1:
ep += [wp[ap]]
if dp[bp] > 1:
ep += [wp[bp]]
ep = sorted(ep)[::-1]
sp = sum(wp)
resp = [str(sp)]
for xp in ep:
sp += xp
resp += [str(sp)]
print(" ".join(resp)) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR LIST VAR VAR IF VAR VAR NUMBER VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR LIST FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | for i in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
d = {}
for i in range(n - 1):
x, y = map(int, input().split())
d[y] = d.get(y, 0) + 1
d[x] = d.get(x, 0) + 1
se = []
for i in range(n):
se.append([a[i], d[i + 1]])
s = sum(a)
p = [s]
se.sort()
j = 0
while j < n - 2:
x = se.pop()
while x[1] != 1 and j < n - 2:
x[1] -= 1
s += x[0]
p.append(s)
j += 1
print(*p) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | t = int(input())
for _ in range(t):
n = int(input())
arr = [[int(i), 0] for i in input().split()]
for i in range(n - 1):
a, b = map(int, input().split())
arr[a - 1][1] += 1
arr[b - 1][1] += 1
arr.sort()
ans = 0
for i in arr:
ans += i[0]
print(ans, end=" ")
l = n
for i in range(n - 2):
while l > 0 and arr[-1][1] == 1:
arr.pop()
l -= 1
if l > 0:
ans += arr[-1][0]
arr[-1][1] -= 1
print(ans, end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | for i in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
graph = []
visited = [0] * (n + 1)
for i in range(1, n):
u, v = map(int, input().split())
visited[u] += 1
visited[v] += 1
if visited[u] > 1:
graph += [l[u - 1]]
if visited[v] > 1:
graph += [l[v - 1]]
s = sorted(graph)[::-1]
x = sum(l)
print(x, end=" ")
for i in range(len(s)):
x += s[i]
print(x, end=" ")
print() | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | from sys import stdin
iput = stdin.readline
for _ in range(int(input())):
n = int(input())
a = [int(x) for x in input().split()]
a.insert(0, 0)
b = [0] * (n + 1)
now = sum(a)
for x in range(n - 1):
c, d = map(int, input().split())
b[c] += 1
b[d] += 1
d = []
for x, y in zip(a, b):
for z in range(1, y):
d.append(x)
d.sort()
d.reverse()
d.insert(0, 0)
for x in d:
now += x
print(now, end=" ")
print() | ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | from sys import stdin
input = stdin.readline
t = int(input())
while t > 0:
n = int(input())
w = list(map(int, input().split()))
l = [0] * n
di = {}
ans = []
for i in range(n - 1):
x, y = map(int, input().split())
l[x - 1] += 1
l[y - 1] += 1
for i in range(n):
if l[i] > 1:
di[w[i]] = di.get(w[i], 0) + l[i] - 1
s = sum(w)
ans.append(str(s))
for j in sorted(di.keys(), reverse=True):
for i in range(di[j]):
s += j
ans.append(str(s))
print(" ".join(ans))
t -= 1 | ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR NUMBER |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | from sys import stdin, stdout
nmbr = lambda: int(stdin.readline())
lst = lambda: list(map(int, stdin.readline().split()))
for _ in range(nmbr()):
n = nmbr()
w = lst()
d = [0] * (1 + n)
for i in range(n - 1):
u, v = lst()
d[u] += 1
d[v] += 1
nl = [i for i in range(1, n + 1) if d[i] != 1]
ans = sum(w)
l = []
for node in nl:
l += [[w[node - 1], node]]
l = sorted(l, reverse=True)
ln = len(l)
used = [1] * (1 + n)
p = 0
for i in range(n - 2):
stdout.write(str(ans) + " ")
if used[l[p][1]] == d[l[p][1]]:
p += 1
if p < ln:
ans += l[p][0]
if p < ln:
used[l[p][1]] += 1
stdout.write(str(ans) + " ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR LIST LIST VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | import sys
for test in range(int(input())):
n = int(sys.stdin.readline())
arr = list(map(int, sys.stdin.readline().split()))
a1 = []
for i in arr:
a1.append([i, 0])
for i in range(n - 1):
a, b = map(int, input().split())
a -= 1
b -= 1
a1[a][1] += 1
a1[b][1] += 1
a1.sort(key=lambda x: (x[0], x[1]))
an = sum(arr)
ans = [an]
pointer = n - 1
for i in range(n - 2):
while 1 == 1:
if a1[pointer][1] > 1:
a1[pointer][1] -= 1
an += a1[pointer][0]
ans.append(an)
break
else:
pointer -= 1
for i in ans:
sys.stdout.write(str(i) + " ")
sys.stdout.write("\n") | IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | for _ in range(int(input())):
n, lst = int(input()), list(map(int, input().split(" ")))
Counter, ans = [[0, 0] for _ in range(n)], sum(lst)
for i in range(n - 1):
a, b = map(int, input().split(" "))
Counter[a - 1][0], Counter[b - 1][0] = a - 1, b - 1
Counter[b - 1][1] += 1
Counter[a - 1][1] += 1
Counter.sort(key=lambda x: lst[x[0]], reverse=True)
print(ans, end=" ")
for i in Counter:
while i[1] > 1:
ans += lst[i[0]]
print(ans, end=" ")
i[1] -= 1
print() | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR WHILE VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | test_num = int(input())
def solve():
v_num = int(input())
weight = [0] + list(map(int, input().split()))
ans = sum(weight)
degrees = [0] * (v_num + 1)
for i in range(v_num - 1):
x, y = map(int, input().split())
degrees[x] += 1
degrees[y] += 1
points = []
for i in range(1, v_num + 1):
points += [weight[i]] * (degrees[i] - 1)
points.sort(reverse=True)
print(ans, end=" ")
for i in range(v_num - 2):
ans += points[i]
print(ans, end=" ")
print()
for i in range(test_num):
solve() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP LIST VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | def solution():
for tc in range(int(input())):
n = int(input())
w = list(map(int, input().split()))
tmp = []
graph = [(0) for i in range(n + 1)]
for i in range(n - 1):
a, b = map(int, input().split())
a -= 1
b -= 1
graph[a], graph[b] = graph[a] + 1, graph[b] + 1
if graph[a] > 1:
tmp.append(w[a])
if graph[b] > 1:
tmp.append(w[b])
tmp.sort(reverse=True)
t_sum = sum(w)
ans = [str(t_sum)]
for i in tmp:
t_sum += i
ans.append(str(t_sum))
print(*ans)
solution() | FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
n = int(stdin.readline())
w = [int(x) for x in stdin.readline().split()]
d = [-1] * n
for foo in range(n - 1):
u, v = [int(x) for x in stdin.readline().split()]
d[u - 1] += 1
d[v - 1] += 1
repeats = []
for i in range(n):
for bar in range(d[i]):
repeats.append(w[i])
repeats.sort(reverse=True)
answers = [sum(w)]
for r in repeats:
answers.append(answers[-1] + r)
stdout.write(" ".join([str(x) for x in answers]) + "\n") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR STRING |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | import sys
t = int(input())
for test in range(t):
n = int(sys.stdin.readline())
weight = list(map(int, sys.stdin.readline().split()))
vertex = [0] * n
key = []
for _ in range(n - 1):
a, b = map(int, sys.stdin.readline().split())
vertex[a - 1] += 1
if vertex[a - 1] >= 2:
key.append(weight[a - 1])
vertex[b - 1] += 1
if vertex[b - 1] >= 2:
key.append(weight[b - 1])
answer = [str(sum(weight))]
key.sort()
for _ in range(len(key)):
answer.append(str(int(answer[-1]) + key.pop()))
print(" ".join(answer)) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | import sys
input = sys.stdin.readline
t = int(input())
for case in range(t):
n = int(input())
w = [int(x) for x in input().split(" ")]
ow = []
orders = {i: (0) for i in range(1, n + 1)}
for edge in range(n - 1):
x = [int(s) for s in input().split(" ")]
for z in x:
orders[z] += 1
if orders[z] > 1:
ow.append(w[z - 1])
ow.sort(reverse=True)
ow = w + ow
p_ow = [ow[0]]
for x in ow[1:]:
p_ow.append(p_ow[-1] + x)
print(*p_ow[n - 1 :]) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | for _ in range(int(input())):
n = int(input())
W = [int(w) for w in input().split()]
A = [0] * n
for _ in range(n - 1):
x, y = map(int, input().split())
A[x - 1] += 1
A[y - 1] += 1
C = list(zip(W, A))
C.sort(reverse=True)
W, A = zip(*C)
R = [0] * (n - 1)
p, s = 0, sum(W)
A = list(A)
R[0] = s
for i in range(1, n - 1):
while p < n and A[p] == 1:
p += 1
A[p] -= 1
s += W[p]
R[i] = s
print(" ".join(map(str, R))) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | import sys
sys.setrecursionlimit(10**5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.buffer.readline())
def MI():
return map(int, sys.stdin.buffer.readline().split())
def MI1():
return map(int1, sys.stdin.buffer.readline().split())
def LI():
return list(map(int, sys.stdin.buffer.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def BI():
return sys.stdin.buffer.readline().rstrip()
def SI():
return sys.stdin.buffer.readline().rstrip().decode()
inf = 10**16
md = 10**9 + 7
for _ in range(II()):
n = II()
vv = LI()
deg = [-1] * n
for _ in range(n - 1):
u, v = MI1()
deg[u] += 1
deg[v] += 1
vu = [(v, u) for u, v in enumerate(vv)]
vu.sort(reverse=True)
ans = [sum(vv)]
i = 0
for _ in range(n - 2):
while deg[vu[i][1]] == 0:
i += 1
v, u = vu[i]
ans.append(ans[-1] + v)
deg[u] -= 1
print(*ans) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | import sys
input = sys.stdin.readline
def debug():
for i in range(10):
pass
def mmm(a, b, m):
res = 0
a %= m
while b:
if b & 1:
res = (res + a) % m
a = 2 * a % m
b >>= 1
return res
def pw(a, b, c):
ans = 1
a = a % c
if a == 0:
return 0
while b > 0:
if b & 1:
ans = ans * a % c
b = b >> 1
a = a * a % c
return ans
try:
for _ in range(int(input())):
n = int(input())
arr = [int(i) for i in input().split()]
ans = []
x = [0] * (n + 1)
debug()
for i in range(n - 1):
u, v = map(int, input().split())
u -= 1
v -= 1
x[u] += 1
x[v] += 1
if x[u] > 1:
ans += [arr[u]]
if x[v] > 1:
ans += [arr[v]]
debug()
ans.sort(reverse=True)
val = sum(arr)
ans2 = [val]
for i in ans:
val += i
ans2.append(val)
print(*ans2)
except EOFError as e:
print(e) | IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR VAR WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR LIST VAR VAR IF VAR VAR NUMBER VAR LIST VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | tt = int(input())
for p in range(tt):
n = int(input())
w = list(map(int, input().split()))
e = []
d = [0] * (n + 1)
for i in range(n - 1):
aa, bb = map(int, input().split())
pp = aa
qq = bb
aa, bb = aa - 1, bb - 1
d[aa] += 1
pp += 1
qq += 1
d[bb] += 1
if d[aa] > 1:
e += [w[aa]]
pp -= 1
if d[bb] > 1:
e += [w[bb]]
qq -= 1
e = sorted(e)[::-1]
pp += qq
qq += pp
ss = sum(w)
res = [str(ss)]
for xs in e:
ss += xs
res += [str(ss)]
print(" ".join(res)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR LIST VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR LIST VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR LIST FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | import sys
input = sys.stdin.readline
t = int(input())
outL = []
for _ in range(t):
n = int(input())
w = list(map(int, input().split()))
extra = []
deg = [0] * n
for i in range(n - 1):
for v in map(int, input().split()):
vv = v - 1
if deg[vv]:
extra.append(w[vv])
deg[vv] += 1
out = [sum(w)]
extra.sort(reverse=True)
for v in extra:
out.append(out[-1] + v)
outL.append(" ".join(map(str, out)))
print("\n".join(outL)) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | def labour(wt, arr):
wt = [0] + wt
connection = [0] * (len(wt) + 1)
for i in arr:
connection[i[0]] += 1
connection[i[1]] += 1
ans = 0
sorter = []
for i in range(len(wt)):
for j in range(1, connection[i]):
sorter.append(wt[i])
ans += wt[i]
sorter = sorted(sorter, reverse=True)
print(ans, end=" ")
for i in sorter:
ans += i
print(ans, end=" ")
return ""
for i in range(int(input())):
a = input()
lst = list(map(int, input().strip().split()))
blanck = []
for i in range(len(lst) - 1):
a, b = map(int, input().strip().split())
blanck.append([a, b])
print(labour(lst, blanck)) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | def readInt():
return int(input())
def readLine():
return [int(s) for s in input().split(" ")]
def readString():
return input()
def ask(n, weights, Q):
degs = {}
def addDeg(v):
degs[v] = degs.get(v, 0) + 1
for a, b in Q:
addDeg(a)
addDeg(b)
ret = []
for key, value in degs.items():
if value == 1:
continue
ap = weights[key - 1]
for _ in range(value - 1):
ret.append(ap)
base = sum(weights)
sot = sorted(ret, reverse=True)
ret = [base]
for ti in sot:
base += ti
ret.append(base)
return ret
t = readInt()
for _ in range(t):
n = readInt()
weights = readLine()
Q = []
for __ in range(n - 1):
a, b = readLine()
Q.append((a, b))
ans = ask(n, weights, Q)
print(" ".join([str(a) for a in ans]))
pass | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | import sys
tok = [int(x) for x in sys.stdin.read().split()]
cur = 1
for _ in range(tok[0]):
n = tok[cur]
w = [0] + [int(x) for x in tok[cur + 1 : cur + n + 1]]
d = [0] * (n + 1)
s = []
cur += n
for i in range(1, n):
u = tok[cur + 2 * i - 1]
v = tok[cur + 2 * i]
d[u] += 1
d[v] += 1
for j in [u, v]:
if d[j] > 1:
s.append(w[j])
s = [0] + sorted(s)[::-1]
x = sum(w)
for y in s:
x += y
print(x, end=" ")
print()
cur += 2 * n - 1 | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR LIST VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | cases = int(input())
for _ in range(cases):
nodes = int(input())
weights = list(map(int, input().split()))
wOrder = []
adj = [[weights[i], -1] for i in range(nodes)]
res = []
for i in range(nodes - 1):
src, dest = map(int, input().split())
adj[src - 1][1] += 1
adj[dest - 1][1] += 1
res.append(sum(weights))
adj.sort(reverse=True)
for cost, repeat in adj:
while repeat:
res.append(cost + res[-1])
repeat -= 1
print(*res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR WHILE VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | from sys import stdin, stdout
def input():
return stdin.readline().strip()
def print(*args, sep=" ", end="\n"):
stdout.write(sep.join(str(arg) for arg in args))
stdout.write(end)
def listInput():
return [int(x) for x in input().split()]
def solve():
n = int(input())
w = listInput()
d = [0] * n
for i in range(n - 1):
u, v = listInput()
d[u - 1] += 1
d[v - 1] += 1
wd = [(weight, deg - 1) for deg, weight in zip(d, w)]
wd.sort()
wd = wd[::-1]
ans = sum(w)
print(ans, end=" ")
for x in wd:
for i in range(x[1]):
ans += x[0]
print(ans, end=" ")
print()
testsCount = int(input())
for _ in range(testsCount):
solve()
stdout.flush() | FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF STRING STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | import sys
def input():
return sys.stdin.readline().strip()
def solve():
for t in range(int(input())):
n = int(input())
w = list(map(int, input().split()))
deg = [0] * n
for i in range(n - 1):
u, v = map(int, input().split())
deg[u - 1] += 1
deg[v - 1] += 1
a = []
s = 0
for i in range(n):
s += w[i]
for j in range(deg[i] - 1):
a.append(w[i])
a.sort()
res = [0] * (n - 1)
res[0] = s
for i in range(1, len(a) + 1):
s += a[-i]
res[i] = s
for i in range(len(a) + 1, n - 1):
res[i] = s
print(" ".join(map(str, res)))
solve() | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | def solver(w, wt, val, n):
if n == 0 or w == 0:
return 0
if wt[n - 1] > w:
return solver(w, wt, val, n - 1)
else:
return max(
val[n - 1] + solver(w - wt[n - 1], wt, val, n - 1),
solver(w, wt, val, n - 1),
)
t = int(input())
for _ in range(t):
n = int(input())
w = list(map(int, input().split()))
empty = []
dp = [0] * (n + 1)
for i in range(n - 1):
u, v = map(int, input().split())
u, v = u - 1, v - 1
dp[u] += 1
dp[v] += 1
if dp[u] > 1:
empty += [w[u]]
if dp[v] > 1:
empty += [w[v]]
empty = sorted(empty, reverse=True)
total = sum(w)
resultado = [total]
for x in empty:
total += x
resultado += [total]
print(" ".join(map(str, resultado))) | FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR LIST VAR VAR IF VAR VAR NUMBER VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FOR VAR VAR VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | rn = lambda: int(input())
rns = lambda: map(int, input().split())
rl = lambda: list(map(int, input().split()))
rs = lambda: input()
yn = lambda x: print("Yes") if x else print("No")
YN = lambda x: print("YES") if x else print("NO")
pl = lambda l: print(" ".join(list(map(str, l))))
for _ in range(rn()):
n = rn()
l = rl()
d = {}
for i in range(n - 1):
u, v = rns()
if u in d:
d[u] += 1
else:
d[u] = 1
if v in d:
d[v] += 1
else:
d[v] = 1
sums = []
for i in d:
sums += [l[i - 1]] * (d[i] - 1)
sums.sort(reverse=True)
ans = [sum(l)]
for i in sums:
ans.append(ans[-1] + i)
print(*ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR BIN_OP LIST VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | import sys
T = int(sys.stdin.readline().strip())
for t in range(0, T):
n = int(sys.stdin.readline().strip())
w = list(map(int, sys.stdin.readline().strip().split()))
F = [-1] * n
for i in range(0, n - 1):
u, v = list(map(int, sys.stdin.readline().strip().split()))
F[u - 1] = F[u - 1] + 1
F[v - 1] = F[v - 1] + 1
ans = [sum(w)]
L = [[w[i], F[i]] for i in range(0, n)]
L.sort()
for i in range(1, n - 1):
while L[-1][1] == 0:
L.pop()
ans.append(ans[-1] + L[-1][0])
L[-1][1] = L[-1][1] - 1
print(" ".join(list(map(str, ans)))) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
ar = list(map(int, input().split()))
dic = {}
for i in range(1, n + 1):
dic[i] = 0
for i in range(n - 1):
u, v = map(int, input().split())
dic[u] += 1
dic[v] += 1
we = []
for i in dic:
we.append([ar[i - 1], dic[i]])
we.sort(reverse=True)
su = sum(ar)
ans = [su]
for i in range(n):
while we[i][1] > 1:
su += we[i][0]
we[i][1] -= 1
ans.append(su)
print(*ans) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | for _ in range(int(input())):
n = int(input())
w = [*map(int, input().split())]
deg = [0] * n
ans = [0] * (n - 1)
for _ in range(n - 1):
u, v = map(int, input().split())
u -= 1
v -= 1
deg[u] += 1
deg[v] += 1
a = []
for i in range(n):
for _ in range(1, deg[i]):
a.append(w[i])
ans[0] += w[i]
a.sort(reverse=True)
for i, wt in enumerate(a, 1):
ans[i] += wt + ans[i - 1]
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | from sys import stdin
input = stdin.readline
def put():
return map(int, input().split())
(t,) = put()
for _ in range(t):
(n,) = put()
l = list(put())
c = [0] * n
for _ in range(n - 1):
x, y = put()
c[x - 1] += 1
c[y - 1] += 1
ans = []
s = sum(l)
z = []
for i in range(n):
while c[i] > 1:
z.append(l[i])
c[i] -= 1
z.sort()
ans.append(s)
for i in range(n - 2):
ans.append(ans[-1] + z.pop())
print(*ans) | ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | t = int(input())
while t > 0:
n = int(input())
arr = list(map(int, input().split()))
ans = sum(arr)
temp = [0] * (n + 1)
temp1 = []
for i in range(n - 2, -1, -1):
i, j = map(int, input().split())
if temp[i] == 0:
temp[i] = 1
else:
temp1.append(arr[i - 1])
if temp[j] == 0:
temp[j] = 1
else:
temp1.append(arr[j - 1])
temp1.sort()
print(ans, end=" ")
for i in range(n - 3, -1, -1):
ans += temp1[i]
print(ans, end=" ")
print()
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
w = list(map(int, input().split()))
deg = [(-1) for i in range(n)]
for _ in range(n - 1):
a, b = map(int, input().split())
deg[a - 1] += 1
deg[b - 1] += 1
w = [[w[i], deg[i]] for i in range(n)]
w.sort()
s = sum(w[i][0] for i in range(n))
ans = [s for i in range(n - 1)]
pos = n - 1
for i in range(1, n - 1):
while not w[pos][1]:
pos -= 1
ans[i] = ans[i - 1] + w[pos][0]
w[pos][1] -= 1
print(*ans) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?
In this problem, you are given a tree with $n$ weighted vertices. A tree is a connected graph with $n - 1$ edges.
Let us define its $k$-coloring as an assignment of $k$ colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all $k$ colors.
A subgraph of color $x$ consists of these edges from the original tree, which are assigned color $x$, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree $0$ in such a subgraph.
The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals $0$.
There is also a value of a $k$-coloring, which equals the sum of values of subgraphs of all $k$ colors. Given a tree, for each $k$ from $1$ to $n - 1$ calculate the maximal value of a $k$-coloring.
-----Input-----
In the first line of input, there is a single integer $t$ ($1 \leq t \leq 10^5$) denoting the number of test cases. Then $t$ test cases follow.
First line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$). The second line consists of $n$ integers $w_1, w_2, \dots, w_n$ ($0 \leq w_i \leq 10^9$), $w_i$ equals the weight of $i$-th vertex. In each of the following $n - 1$ lines, there are two integers $u$, $v$ ($1 \leq u,v \leq n$) describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.
The sum of $n$ in all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For every test case, your program should print one line containing $n - 1$ integers separated with a single space. The $i$-th number in a line should be the maximal value of a $i$-coloring of the tree.
-----Examples-----
Input
4
4
3 5 4 6
2 1
3 1
4 3
2
21 32
2 1
6
20 13 17 13 13 11
2 1
3 1
4 1
5 1
6 1
4
10 6 6 6
1 2
2 3
4 1
Output
18 22 25
53
87 107 127 147 167
28 38 44
-----Note-----
The optimal $k$-colorings from the first test case are the following:
In the $1$-coloring all edges are given the same color. The subgraph of color $1$ contains all the edges and vertices from the original graph. Hence, its value equals $3 + 5 + 4 + 6 = 18$.
In an optimal $2$-coloring edges $(2, 1)$ and $(3,1)$ are assigned color $1$. Edge $(4, 3)$ is of color $2$. Hence the subgraph of color $1$ consists of a single connected component (vertices $1, 2, 3$) and its value equals $3 + 5 + 4 = 12$. The subgraph of color $2$ contains two vertices and one edge. Its value equals $4 + 6 = 10$.
In an optimal $3$-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: $3 + 4 = 7$, $4 + 6 = 10$, $3 + 5 = 8$. | for __ in range(int(input())):
n = int(input())
w = list(map(int, input().split()))
Adj = {x: (0) for x in range(n)}
for i in range(n - 1):
[u, v] = list(map(int, input().split()))
Adj[u - 1] += 1
Adj[v - 1] += 1
ansL = []
for i in range(n):
for j in range(Adj[i] - 1):
ansL.append(w[i])
ansL.sort(reverse=True)
ansL.insert(0, sum(w))
for i in range(1, n - 1):
ansL[i] = ansL[i - 1] + ansL[i]
print(*ansL) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def check(self, arr, to_remove):
return (
len(tuple(filter(lambda x: not to_remove.contains(x), arr))) <= len(arr) / 2
)
def minSetSize(self, arr: List[int]) -> int:
all_nums = set(arr)
freq = {n: (0) for n in all_nums}
for num in arr:
freq[num] += 1
nums_by_count = sorted(freq.items(), key=lambda x: x[1], reverse=True)
remove_count = 0
result = 0
half = len(arr) / 2
for item in nums_by_count:
num, count = item
remove_count += count
result += 1
if remove_count >= half:
return result | CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
N = len(arr)
target = math.ceil(N / 2)
dic = collections.Counter(arr)
lst = [(num, cnt) for num, cnt in dic.items()]
lst.sort(key=lambda x: [-x[1], x[0]])
ans = 0
count = 0
for num, cnt in lst:
count += cnt
ans += 1
if count >= target:
return ans
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
d = collections.defaultdict(int)
for i in arr:
d[i] += 1
res = len(arr)
orig = len(arr)
count = 0
freq = [(d[i], i) for i in d]
freq.sort(reverse=True)
count = 0
for i in freq:
res -= i[0]
count += 1
if res <= orig // 2:
return count
return -1 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN VAR RETURN NUMBER VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
H = {}
for x in arr:
H[x] = H.get(x, 0) - 1
L = list(H.values())
heapq.heapify(L)
t = 0
a = 0
M = len(arr) // 2
while t < M:
x = heapq.heappop(L)
t += -x
a += 1
return a | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
n = len(arr) // 2
freq = {}
for i in arr:
if i in freq:
freq[i] += 1
else:
freq[i] = 1
res = [i for i in freq.values()]
res.sort(reverse=True)
total = 0
count = 0
for i in res:
total += i
count += 1
if total >= n:
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
d = {}
for a in arr:
if a in d:
d[a] += 1
else:
d[a] = 1
q = []
for k in d:
heappush(q, (-d[k], k))
size = len(arr)
half = len(arr) / 2
numPop = 0
while size > half:
poped = heappop(q)
size += poped[0]
numPop += 1
return numPop | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
if len(set(arr)) == 1:
return 1
maps = {x: (0) for x in set(arr)}
for x in arr:
maps[x] += 1
maps = sorted(list(maps.items()), key=lambda x: x[1], reverse=True)
mincount = len(arr) // 2
ans = 0
currLength, sub = len(arr), 9
for x in range(len(maps)):
key, val = maps[x]
if currLength > mincount:
currLength -= val
ans += 1
else:
return ans | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
if len(set(arr)) == 1:
return 1
if len(set(arr)) == len(arr):
if len(arr) % 2 == 0:
return int(len(arr) / 2)
else:
return len(arr) // 2 + 1
u = list(set(arr))
d_u = {}
for ele in u:
d_u[ele] = arr.count(ele)
d_u = sorted(list(d_u.items()), key=lambda x: x[1], reverse=True)
s = 0
size = 0
for ele in d_u:
size += 1
s += ele[1]
if (s >= len(arr) / 2) & (len(arr) % 2 == 0):
return size
if (len(arr) % 2 == 1) & (s >= len(arr) // 2 + 1):
return size | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
d = {}
n = len(arr)
for num in arr:
if num not in d:
d[num] = 0
d[num] += 1
num_freq = list(sorted([v for k, v in d.items()]))
tot = 0
m = len(num_freq)
i = 0
while tot < n / 2 and i < m:
tot += num_freq[m - i - 1]
i += 1
print(i)
print(num_freq)
return i | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
checked, answer = [], 0
count_d, total = [], 0
if len(arr) == len(set(arr)):
return int(len(arr) / 2)
for num in arr:
if num not in checked:
count_d.append(arr.count(num))
checked.append(num)
count_d.sort()
for d in range(1, len(count_d) + 1):
total += count_d[-d]
answer += 1
if total >= len(arr) / 2:
return answer | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR LIST NUMBER ASSIGN VAR VAR LIST NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
arr_len = len(arr)
int_dict = {}
val = []
for i in arr:
int_dict[i] = int_dict.setdefault(i, 0) + 1
for i in int_dict:
val.append(int_dict[i])
val.sort()
count_num = 0
loc = 0
for i in val[::-1]:
count_num += i
loc += 1
if count_num >= arr_len / 2:
break
return loc | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
d = {}
for k in arr:
if k in d:
d[k] += 1
else:
d[k] = 1
t = []
for k, v in d.items():
t.append(v)
t.sort(reverse=True)
r = 0
ii = 0
while r < len(arr) // 2 + len(arr) % 1:
r += t[ii]
ii += 1
return ii | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
c = Counter(arr)
result = list()
count = 0
for k, v in sorted(c.items(), key=lambda item: item[1], reverse=True):
if count < len(arr) // 2:
result.append(k)
count += v
return len(result) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
arr.sort()
freq = []
k = 0
while k < len(arr):
j = k + 1
fr = 1
while j < len(arr):
if arr[j] == arr[k]:
fr += 1
else:
break
j += 1
k += 1
freq.append(fr)
k += 1
freq.sort()
freq.reverse()
k = 0
no = 0
count = 0
while k < len(freq) and count < len(arr) / 2:
if freq[0] >= len(arr) / 2:
no += 1
break
count = count + freq[k]
no += 1
k += 1
return no | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
if not arr:
return 0
counter = collections.Counter(arr)
max_val = max(counter.values())
buckets = [0] * (max_val + 1)
for count in list(counter.values()):
buckets[count] += 1
set_size = 0
arr_num_to_remove = len(arr) // 2
bucket = max_val
while arr_num_to_remove > 0:
max_need_from_bucket = math.ceil(arr_num_to_remove / bucket)
set_size_increase = min(buckets[bucket], max_need_from_bucket)
set_size += set_size_increase
arr_num_to_remove -= set_size_increase * bucket
bucket -= 1
return set_size | CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, a: List[int]) -> int:
d = {}
n = len(a) // 2
for i in a:
if i not in d:
d[i] = 1
else:
d[i] += 1
d = sorted(d.items(), key=lambda x: x[1], reverse=True)
print(d)
sum = 0
count = 0
for i in d:
x = i[1]
sum += x
count += 1
if sum >= n:
break
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
count = sorted(Counter(arr).values(), reverse=1)
target = len(arr) // 2
res = curr = 0
for val in count:
curr += val
res += 1
if curr >= target:
break
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
arr.sort()
counts = []
current_run = 1
for i in range(1, len(arr)):
if arr[i] == arr[i - 1]:
current_run += 1
continue
counts.append(current_run)
current_run = 1
counts.append(current_run)
counts.sort(reverse=True)
numbers_removed_from_arr = 0
set_size = 0
for count in counts:
numbers_removed_from_arr += count
set_size += 1
if numbers_removed_from_arr >= len(arr) // 2:
break
return set_size | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
counter = {}
for num in arr:
if num in counter:
counter[num] += 1
else:
counter[num] = 1
heap = []
for num in list(counter.keys()):
heapq.heappush(heap, (-counter[num], num))
n = len(arr)
i = 0
cumSum = 0
while i < n and cumSum < n // 2:
freq, num = heapq.heappop(heap)
cumSum += abs(freq)
i += 1
return i | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
occ = {}
for num in arr:
if num in occ:
occ[num] += 1
else:
occ[num] = 1
occ_lst = list(occ.items())
occ_lst.sort(key=lambda x: x[1], reverse=True)
total_elems_removed = 0
removal_set_size = 0
for i, (_, num_occur) in enumerate(occ_lst):
total_elems_removed += num_occur
if total_elems_removed >= math.ceil(len(arr) / 2.0):
return i + 1 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
length = len(arr)
bar = length // 2
d = {}
for i in range(len(arr)):
d[arr[i]] = d.get(arr[i], 0) + 1
res = 0
l = list(sorted(d.values(), reverse=True))
for value in l:
length -= value
res += 1
if length <= bar:
return res
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
d = dict()
for x in arr:
d.setdefault(x, 0)
d[x] += 1
c = sorted(d.values(), reverse=True)
for i in range(1, len(c)):
c[i] += c[i - 1]
for i, x in enumerate(c):
if x >= len(arr) // 2:
return i + 1 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
numsdict = {}
for i in arr:
numsdict[i] = numsdict.get(i, 0) + 1
sortarr = sorted(numsdict.items(), key=lambda x: -x[1])
count = 0
i = 0
while 2 * count < len(arr):
count += sortarr[i][1]
i += 1
return i | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
n = len(arr)
arr_map = Counter(arr)
arr_tup = [(item, arr_map[item]) for item in arr_map]
arr_tup = sorted(arr_tup, key=lambda x: -x[1])
count = 0
total = 0
for item in arr_tup:
total += item[1]
print(item[0])
count += 1
if total >= n / 2:
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
num = {}
for i in arr:
if i in num:
num[i] += 1
else:
num[i] = 1
sort_nums = sorted(num.items(), key=lambda x: x[1], reverse=True)
total = 0
ind = 0
for i in sort_nums:
if total < len(arr) / 2:
total += i[1]
ind += 1
else:
break
return ind | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, array: List[int]) -> int:
hashMap = dict()
for item in array:
if item not in hashMap:
hashMap[item] = 0
hashMap[item] += 1
length = len(array)
itemToBeRemoved = 0
halfOflength = len(array) // 2
for value in sorted(list(hashMap.values()), reverse=True):
length -= value
itemToBeRemoved += 1
if length <= halfOflength:
return itemToBeRemoved | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
counts = collections.Counter(arr)
counts = [count for number, count in counts.most_common()]
set_size = 0
tot = 0
for x in counts:
tot += x
set_size += 1
if tot >= len(arr) // 2:
break
return set_size | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
dict_ = {}
for num in arr:
if num not in dict_:
dict_[num] = 1
else:
dict_[num] += 1
sort_list = sorted(dict_.keys(), key=lambda x: dict_[x], reverse=True)
n = len(arr)
size = 0
for num in sort_list:
n -= dict_[num]
size += 1
if n < len(arr) // 2 + 1:
return size | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
N = len(arr)
count = collections.Counter()
for n in arr:
count[n] += 1
sorted_count = sorted(list(count.keys()), key=lambda c: count[c], reverse=True)
removed_count = 0
res = 0
for c in sorted_count:
removed_count += count[c]
res += 1
if removed_count >= (N + 1) // 2:
break
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
counts = {}
for i, elem in enumerate(arr):
counts[elem] = 1 if elem not in counts else counts[elem] + 1
size = len(arr)
count = 0
vals = sorted(counts.values(), reverse=True)
while size > len(arr) / 2:
maxElem = vals[count]
size -= maxElem
count += 1
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
d = {}
for i in arr:
if i in d:
d[i] += 1
else:
d[i] = 1
d = sorted(d.items(), key=lambda x: x[1], reverse=True)
print(d)
total, count = 0, 0
for i in d:
total += i[1]
count += 1
if total >= len(arr) // 2:
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
d = {}
h = []
for num in arr:
if num not in d:
d[num] = 1
else:
d[num] += 1
for k, v in list(d.items()):
h.append((-v, k))
heapq.heapify(h)
ans = 0
count = 0
while count < len(arr) // 2:
v, k = heapq.heappop(h)
count -= v
ans += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
dic = Counter(arr)
h = [(val * -1, key) for key, val in list(dic.items())]
heapify(h)
seen = set()
count = 0
while 2 * count * -1 < len(arr):
c, i = heappop(h)
count += c
seen.add(i)
return len(seen) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def floor_counter(self, n):
if n % 2 == 0:
return n / 2
else:
return (n + 1) / 2
def minSetSize(self, arr: List[int]) -> int:
ceiling = self.floor_counter(len(arr))
freq = {}
for elem in arr:
if elem in freq:
freq[elem] += 1
else:
freq[elem] = 1
sorted_freq = sorted(freq.items(), key=lambda k: k[1], reverse=True)
sum = 0
type_counter = []
for i in range(len(sorted_freq)):
sum += sorted_freq[i][1]
print(sum)
type_counter.append(sorted_freq[i][0])
if sum >= ceiling:
return len(type_counter) | CLASS_DEF FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
length = len(arr)
counter = {}
for num in arr:
try:
counter[num] += 1
except:
counter[num] = 1
array = sorted(list(counter.items()), key=lambda x: (-x[1], x[0]))
res = 0
i = 0
while res * 2 < length:
res += array[i][1]
i += 1
return i | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
hash_map = collections.Counter(arr)
hash_map = {k: v for k, v in sorted(hash_map.items(), key=lambda item: item[1])}
total_size = len(arr)
values = list(hash_map.values())[::-1]
ans = 0
i = 0
while total_size > len(arr) // 2:
total_size -= values[i]
i += 1
ans += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
n, target = len(arr), len(arr) // 2
counts = {}
for i in arr:
if i in counts:
counts[i] += 1
else:
counts[i] = 1
counts = sorted(list(counts.values()), reverse=True)
numRemoved = 0
for i in counts:
n -= i
numRemoved += 1
if n <= target:
return numRemoved
return numRemoved | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
frequency = defaultdict(int)
for num in arr:
frequency[num] += 1
arrLength = len(arr)
pairs = []
for key, val in frequency.items():
pairs.append((val, key))
pairs.sort(reverse=True)
cumSum = 0
for counter, pair in enumerate(pairs):
frequency, key = pair
cumSum += frequency
if cumSum >= arrLength / 2:
return counter + 1 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
h = [(val * -1, key) for key, val in list(Counter(arr).items())]
heapify(h)
num_int = 0
count = 0
while 2 * count * -1 < len(arr):
c, i = heappop(h)
count += c
num_int += 1
return num_int | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
c = Counter(arr)
l = len(arr)
tup = c.most_common(len(c))
i = 0
while l > len(arr) / 2:
l -= tup[i][1]
i += 1
return i | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
half = len(arr) / 2
counts = {}
for val in arr:
if val in counts.keys():
counts[val] += 1
else:
counts[val] = 1
count = 0
included = 0
for key in sorted(counts, key=counts.get, reverse=True):
count += counts[key]
included += 1
if count >= half:
return included | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
tmp_dict = {}
for e in arr:
if e not in tmp_dict:
tmp_dict[e] = 1
else:
tmp_dict[e] += 1
tmp = [(key, tmp_dict[key]) for key in tmp_dict]
tmp.sort(key=lambda x: x[1], reverse=True)
res = []
cur = 0
i = 0
while cur < len(arr) // 2:
res.append(tmp[i][0])
cur += tmp[i][1]
i += 1
return len(res) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
L = len(arr)
count = collections.Counter(arr)
freq = sorted(count.values())
size = 0
res = 0
for i in range(len(freq) - 1, -1, -1):
size += freq[i]
res += 1
if size >= L / 2:
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
A = {}
for x in arr:
A[x] = A.get(x, 0) + 1
p = 0
count = 0
for x in sorted(list(A.items()), key=lambda x: -x[1]):
p += x[1]
count += 1
if p >= len(arr) // 2:
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
d = {}
for i in arr:
if i not in d:
d[i] = 0
d[i] += 1
d = list(sorted(d.items(), key=lambda kv: kv[1], reverse=True))
sum = 0
cnt = 0
for i in range(len(d)):
if sum >= len(arr) / 2:
break
else:
sum = sum + d[i][1]
cnt += 1
return cnt | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
numberDict = {}
for i in range(len(arr)):
if arr[i] not in numberDict:
numberDict[arr[i]] = 1
else:
numberDict[arr[i]] += 1
sortedOrders = sorted(
list(numberDict.items()), key=lambda x: x[1], reverse=True
)
total = 0
setSize = 0
for i in sortedOrders:
if total >= len(arr) // 2:
return setSize
total += i[1]
setSize += 1
return setSize | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
freq = Counter(arr)
final_length = 0
half = len(arr) / 2
reduce = 0
for n, f in freq.most_common():
final_length += f
reduce += 1
if final_length >= half:
return reduce | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
dic = {}
for num in arr:
dic[num] = dic.get(num, 0) + 1
array = [
v
for k, v in sorted(
list(dic.items()), key=lambda item: item[1], reverse=True
)
]
target = len(arr) / 2
current, i = 0, 0
for num in array:
i += 1
current += num
if current >= target:
return i | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR NUMBER VAR VAR IF VAR VAR RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr) -> int:
counter = {}
for val in arr:
if val not in counter:
counter[val] = 0
counter[val] += 1
values = []
for val in counter:
values.append(counter[val])
values.sort(reverse=True)
total = 0
nhalf = len(arr) // 2
new_n = len(arr)
i = 0
while new_n > nhalf:
total += 1
new_n -= values[i]
i += 1
return total | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
h = {}
for el in arr:
if el not in h:
h[el] = 0
h[el] += 1
tmp = []
for key in list(h.keys()):
tmp.append(h[key])
tmp.sort()
count = 0
size = 0
for i in range(len(tmp) - 1, -1, -1):
size += tmp[i]
count += 1
if size >= math.ceil(len(arr) / 2):
break
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
mem = {}
for i in arr:
if i in mem:
mem[i] += 1
else:
mem[i] = 1
sorted_array = sorted(list(mem.values()), reverse=True)
counter = 0
res = 0
print(sorted_array)
for i in sorted_array:
res += i
counter += 1
print(res)
if res >= len(arr) / 2:
return counter | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.