description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = list(map(int, input().split())) a = list(map(int, input().split())) if n == 1: print(0) else: count = 0 a.sort() total = sum(a) ma = a[n - 1] y = 0 count = 0 for i in range(n): count += 1 if a[i] > y: y += 1 print(total - (ma - y) - count)
ASSIGN VAR 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 IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = map(int, input().split()) l = list(map(int, input().split())) z = max(l) ans = 0 su = sum(l) l.sort() h = 0 for i in l: ans = ans + i - 1 h = min(h + 1, i) if h < z: ans = ans - (z - h) print(ans)
ASSIGN VAR 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 VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = map(int, input().split()) b = list(map(int, input().split())) b.sort() tot = 1 h = 1 for i in range(n - 1): if b[i + 1] > h: tot += 1 h += 1 else: tot += 1 print(sum(b) - (tot + b[-1] - h))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = list(map(int, input().split())) A = list(map(int, input().split())) num_1 = 0 j = 1 A.sort() for i in range(n - 1): num_1 += 1 if j + 1 <= A[i + 1]: j += 1 num_1 += A[-1] - j + 1 print(sum(A) - num_1)
ASSIGN VAR 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, h = map(int, input().split()) lst = list(map(int, input().split())) lst.sort(reverse=True) ans = 0 for i in range(1, n): if lst[i - 1] == 1: ans += lst[i] - 1 lst[i] = 1 elif lst[i - 1] <= lst[i]: ans += lst[i] - lst[i - 1] + 1 lst[i] = lst[i - 1] - 1 for i in range(n - 1): if lst[i] != lst[i + 1]: ans += lst[i + 1] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = map(int, input().split()) a = list(map(int, input().split())) a.sort() h = 0 ans = 0 for i in range(n - 1): ans += a[i] - 1 if a[i] > h: h += 1 if h < max(a): ans += h else: ans += a[-1] - 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = map(int, input().split()) a = list(map(int, input().split())) a.sort() s = sum(a) h = 0 req = 0 m_h = a[-1] if a[0] != 0: req = 1 h = 1 for i in range(1, n): if a[i] > a[i - 1]: h += 1 req += 1 elif a[i] != 0: if h < a[i]: req += 1 h += 1 else: req += 1 req += max(0, m_h - h) print(s - req)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = [int(x) for x in input().split()] a = [0] + [int(x) for x in input().split()] a.sort() asum = sum(a) amax = a[n] d = 0 for i in range(1, n + 1): if a[i] + d < i: d = i - a[i] print(asum - (n + (amax - n) + d))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
nm = input().split(" ") n, m = list(map(lambda s: int(s), nm)) stacksS = input().split(" ") stacks = list(map(lambda s: int(s), stacksS)) sumStacks = sum(stacks) stacks = sorted(stacks) i = 0 for s in stacks: if s > i: i += 1 rightView = stacks[n - 1] - i print(sumStacks - rightView - n)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, k = map(int, input().split()) arr = list(sorted(map(int, input().split()))) mx = 0 sm = sum(arr) for v in arr: if v > mx: mx += 1 reserved = n + arr[-1] - mx print(sm - reserved)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = map(int, input().split()) arr = [int(x) for x in input().split()] arr = sorted(arr, reverse=True) arr.append(0) isum = sum(arr) ans = [] top = arr[0] for i in range(n): if arr[i] == 1: ans.append(1) arr[i + 1] = 1 continue if arr[i + 1] > arr[i]: arr[i + 1] = arr[i] if arr[i] - arr[i + 1] == 0: ans.append(1) h = 1 else: ans.append(arr[i] - arr[i + 1]) h = arr[i] - arr[i + 1] top = arr[i] - h arr[i + 1] = top print(isum - sum(ans))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = map(int, input().split()) l = sorted(map(int, input().split())) level = 0 for v in l: level = min(v, level + 1) print(sum(l) - n - l[-1] + level)
ASSIGN VAR 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 NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, _ = map(int, input().split()) a = sorted(map(int, input().split()), reverse=True) def solve(): if n == 1: return 0 ret = a[0] - max(a[0] - a[1], 1) for i in range(1, n): fromTop = max(0, min(a[i] - a[i - 1] + 1, a[i] - 1)) a[i] -= fromTop fromBottom = a[i] - max(1, a[i] - a[i + 1]) if i < n - 1 else 0 ret += fromTop + fromBottom return ret print(solve())
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = map(int, input().split()) a = list(map(int, input().split())) h = max(a) a.sort() a.reverse() a.append(0) ans = sum(a) for i in range(n): if a[i + 1] >= a[i]: a[i + 1] = a[i] - 1 ans -= max(a[i] - a[i + 1], 1) print(ans)
ASSIGN VAR 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 VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
import sys n, m = map(int, input().split()) A = list(map(int, input().split())) if n == 1: print(0) sys.exit() A.sort(reverse=True) NOW = A[0] ANS = 1 i = 1 while i < n: if A[i] == A[i - 1]: NOW = max(1, NOW - 1) ANS += 1 elif A[i] >= NOW - 1: ANS += 1 NOW = max(NOW - 1, 1) else: ANS += max(1, NOW - A[i]) NOW = A[i] i += 1 ANS += NOW - 1 print(sum(A) - ANS)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = [int(x) for x in input().split()] a = [int(x) for x in input().split()] a.sort() last = 0 total = 0 for i in range(n - 1): if a[i] > 0: total += a[i] - 1 last = min(last + 1, a[i]) print(total + max(a[n - 1] - max(1, a[n - 1] - last), 0))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = map(int, input().split()) a = sorted(list(map(int, input().split())))[::-1] s = sum(a) for i in range(1, n): a[i] = min(a[i], a[i - 1] - 1) s -= a[i - 1] - a[i] print(s - max(a[-1], 1))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
_ = input() a = sorted(map(int, input().split())) i = s = 0 for x in a: s += x - 1 i = min(x, i + 1) print(s - a[-1] + i)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = map(int, input().split()) total = red = prev = 0 a = [] for i in map(int, input().split()): total += i a.append(i) a.sort() for i in a: red += 1 if i > prev: prev += 1 red += a[-1] - prev ans = total - red print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = input().split() a = sorted(map(int, input().split())) tot = 0 last_fill = 0 reqd = 0 for x in a: tot += x if x > last_fill: last_fill += 1 reqd += 1 else: reqd += 1 reqd += a[-1] - last_fill print(tot - reqd)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = map(int, input().split()) a = list(map(int, input().split())) su = sum(a) ma = max(a) if n == 1: print(0) else: a.sort() c = 0 for i in range(n - 1, 0, -1): if a[i] == 1: c += 1 elif a[i - 1] < a[i]: x = min(a[i], ma - c) if x <= a[i - 1]: c += 1 else: c += x - a[i - 1] else: c += 1 if ma - c > 0: c += ma - c else: c += 1 print(su - c)
ASSIGN VAR 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 VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
import sys n, m = list(map(int, input().split())) h = list(map(int, input().split())) s = sum(h) h.sort() for i in range(n - 1, 0, -1): if h[i] == 1: h[i - 1] = 1 elif h[i - 1] >= h[i]: h[i - 1] = h[i] - 1 ans = 0 for i in range(n): if i == 0: ans += h[i] elif h[i] == 1: ans += 1 else: ans += h[i] - h[i - 1] if n == 1: print(0) else: print(s - ans)
IMPORT ASSIGN VAR 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 ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = [int(x) for x in input().split()] a = [int(x) for x in input().split()] s = sum(a) temp = sorted(a) res = temp[0] for x in range(n - 1): if temp[x + 1] != temp[x]: res = res + (temp[x + 1] - temp[x]) if x + 2 > res: res = res + 1 print(s - res)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
import sys n, m, a = 0, 0, 0 def removeFromStack(arr): arr.sort() cnt, last, sum = 0, 0, 0 for i in range(len(arr)): sum += arr[i] cnt += 1 if arr[i] > last: last += 1 cnt += arr[len(arr) - 1] - last return sum - cnt def main(): words = nextLineWords() n = int(words[0]) m = int(words[1]) words = nextLineWords() a = [] for word in words: a.append(int(word)) print(removeFromStack(a)) def nextLineWords(): return sys.stdin.readline().split() main()
IMPORT ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = [int(x) for x in input().split(" ")] z = [int(x) for x in input().split(" ")] z.sort() a, b, ans = 0, 0, 0 for i in range(n - 1): if z[i] <= 1: if z[i] == 1: a = 1 continue if z[i] > 1: if z[i] > a: ans += z[i] - 1 else: ans += a - 1 a += 1 a = min(a, z[i]) if z[-1] > a: ans += a else: ans += z[-1] - 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
import sys input = sys.stdin.buffer.readline n, m = map(int, input().split()) a = list(map(int, input().split())) a.sort(reverse=True) cnt = a[0] + n - 1 ma = a[0] - 1 for i in range(1, n): if ma == 0: break if ma < a[i]: cnt -= 1 ma -= 1 else: ma = a[i] - 1 cnt -= 1 print(sum(a) - cnt)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
input() sl = map(int, input().split()) sl = sorted(sl) h = t = 0 for s in sl[:-1]: if s > h: h += 1 t += s - 1 if h == sl[-1]: t += h - 1 else: t += h print(t)
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = map(int, input().split()) a = list(map(int, input().split())) total = 0 ans = 0 for i in range(n): total += a[i] a.sort() x = 0 y = 0 for i in range(n): x += 1 if a[i] > y: y += 1 x += a[n - 1] - y ans = total - x print(ans)
ASSIGN VAR 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
x, y = [int(i) for i in input().split()] a = sorted([int(i) for i in input().split()]) t = z = 0 for i in range(x): z += a[i] - t if z <= i: z += 1 t = a[i] print(sum(a) - z)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = map(int, input().split()) a = [int(i) for i in input().split()] sum = 0 for i in range(n): sum += a[i] a.sort() a.reverse() now = a[0] cnt = 1 for i in range(1, n): while now > a[i] + 1: cnt = cnt + 1 now = now - 1 if now == 1: break if now == 1: cnt = cnt + n - i break now = now - 1 cnt = cnt + 1 while now > 1: now = now - 1 cnt = cnt + 1 print(sum - cnt)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = map(int, input().split()) a = list(map(int, input().split())) if n == 1: print(0) exit() ans = 0 a.sort(reverse=True) p = a[0] for j in range(n - 1): if p <= 0: ans += a[j] - 1 continue if a[j + 1] >= p - 1: ans += a[j] - 1 p -= 1 else: k = p - a[j + 1] ans += a[j] - k p -= k if p < 0: p = 0 ans += a[-1] - max(p, 1) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = map(int, input().strip().split()) arr = list(map(int, input().strip().split())) sum1, sum2 = sum(arr), 0 arr = sorted(arr) maxi = arr[n - 1] sum2 = 0 count = 0 if n == 1: print(0) else: for i in range(n): if i == n - 1: sum2 += arr[n - 1] - count elif count + 1 == maxi: sum2 += 1 elif count + 1 <= arr[i]: sum2 += 1 count += 1 else: sum2 += 1 print(sum1 - sum2)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = map(int, input().split()) a = list(sorted(map(int, input().split()))) s = 0 h = 1 for i in range(len(a) - 1): if a[i] >= h: h += 1 s += a[i] - 1 s += h - 1 if h <= a[len(a) - 1] else a[len(a) - 1] - 1 print(s)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = map(int, input().split(" ")) a = list(map(int, input().split(" "))) a.sort() sm = sum(a) b = [] for elem in a: if elem != 0: b.append(elem) ans = 0 sh = 0 for i in range(len(b)): if b[i] > sh: sh += 1 print(sm - max(a) - len(b) + sh)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
sum = 0 n, m = map(int, input().split()) ara = list(map(int, input().split())) ara.sort() for i in range(n): sum += ara[i] cnt = 0 p = 0 for i in range(n): cnt += 1 if ara[i] > p: p += 1 cnt += ara[n - 1] - p print(sum - cnt)
ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface. The height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$. There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\text{Top View}$ Find the maximum number of blocks you can remove such that the views for both the cameras would not change. Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $1 \le m \le 10^9$) — the number of stacks and the height of the exhibit. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le m$) — the number of blocks in each stack from left to right. -----Output----- Print exactly one integer — the maximum number of blocks that can be removed. -----Examples----- Input 5 6 3 3 3 3 3 Output 10 Input 3 5 1 2 4 Output 3 Input 5 5 2 3 1 4 4 Output 9 Input 1 1000 548 Output 0 Input 3 3 3 1 1 Output 1 -----Note----- The following pictures illustrate the first example and its possible solution. Blue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]
n, m = [int(i) for i in input().split()] A = [int(i) for i in input().split()] A.sort() sofar = 0 for i in range(n): if A[i] > sofar: sofar += 1 print(sum(A) - n - (A[-1] - sofar))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR
Ross has to give an important speech at the museum and he wants all of his friends in the first row of the auditorium. Ross is punctual and gets ready on time. However, Joey and Chandler are behaving like children and fighting over everything. On getting a firm shouting from Ross, both of them decide to get dressed but they are still angry at each other. Chandler decides to have the final laugh by stealing Joey’s underwear. This makes Joey mad and he decides to wear all the pants owned by Chandler. This infuriates Ross even more. There is a time crunch and he wants Joey to wear pants of a single color. Joey has N pants. Given a string S of length N, where S_{i} represents the colour of i^{th} pant. Joey is allowed to make up to K moves. To make a move, Joey has to do the following: Pick a prefix (possibly empty or complete), say P from the string S. Discard a suffix (possibly empty or complete) from P. Add P back to the string. For example, Let the pants be denoted by the string S = AABAD. In one move Joey can, pick a prefix P = AABA, discard the suffix ABA from P and finally, add P = A back to the string S. The final string becomes AD. After making up to K moves, Joey decides to wear the pants denoted by the final string, if all of them are of the same colour. Find the maximum number of pants that Joey can wear such that all of them are of the same colour, after making at most K moves. If it is impossible to wear pants of the same colour even after K moves, print -1. ------ Input Format ------ - First line will contain T, number of testcases. Then the testcases follow. - For each testcase first line of input contains two integers N, K, the number of pants and the maximum number of moves. - Second line of each testcase contains a string S consisting of uppercase english letters. i^{th} character from the beginning denotes the colour of the i^{th} layer of pants from top. ------ Output Format ------ For each testcase, output in a single line the maximum number of pants of the same colour that Joey can wear using atmost K moves. If it is impossible for Joey to wear pants of the same colour finally, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $0 ≤ K ≤ N$ $S$ consists of uppercase english letters only. - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 5 2 ABACA 4 1 ABAB ----- Sample Output 1 ------ 3 1 ----- explanation 1 ------ Test case 1: For the first move, Joey can pick the prefix with first 2 pants i.e. $AB$, out of this prefix, Joey can discard the suffix $B$ and add the remaining prefix i.e. $A$ back to the string $S$. Now the string $S$ is $AACA$. For the second move, Joey can pick the prefix with 3 pants i.e. $AAC$, discard the prefix $C$ and add remaining prefix $AA$ back to string. Finally, the string looks like $AAA$, i.e. $3$ pants of colour $A$. Test case 2: For the first and only move, Joey can pick the prefix $ABAB$, i.e. the entire string and discard the suffix $BAB$, and add the remaining prefix i.e. $A$ back to the string.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() if " " in s: temp = s.index(" ") s = s[:temp] cnt = s.count(s[0]) if cnt == n: print(n) continue if k == 0: print(-1) continue a = [] for i in range(26): a.append([]) for i in range(n): pos = ord(s[i]) - 65 if len(a[pos]) == 0: a[pos].append([1, i, i]) elif a[pos][-1][2] + 1 == i: a[pos][-1][0] += 1 a[pos][-1][2] = i else: a[pos].append([1, i, i]) ans = 0 for i in range(26): curr = 0 x = chr(i + 65) if x != s[0] and x != s[-1]: chance = k - 1 a[i].sort(reverse=True) for j in range(min(len(a[i]), chance)): curr += a[i][j][0] elif x == s[0] and x != s[-1]: curr = a[i][0][0] a[i].sort(reverse=True) chance = k - 1 j = 0 pos = 0 while pos < chance: if j == len(a[i]): break if a[i][j][1] == 0: j += 1 continue curr += a[i][j][0] j += 1 pos += 1 elif x != s[0] and x == s[-1]: curr = a[i][-1][0] a[i].sort(reverse=True) chance = k - 1 j = 0 pos = 0 while pos < chance: if j == len(a[i]): break if a[i][j][2] == n - 1: j += 1 continue curr += a[i][j][0] j += 1 pos += 1 else: curr = a[i][-1][0] curr += a[i][0][0] chance = k - 1 a[i].sort(reverse=True) j = 0 pos = 0 while pos < chance: if j == len(a[i]): break if a[i][j][2] == n - 1 or a[i][j][1] == 0: j += 1 continue curr += a[i][j][0] j += 1 pos += 1 if curr == -1: continue else: ans = max(ans, curr) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF STRING VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR LIST NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR LIST NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Ross has to give an important speech at the museum and he wants all of his friends in the first row of the auditorium. Ross is punctual and gets ready on time. However, Joey and Chandler are behaving like children and fighting over everything. On getting a firm shouting from Ross, both of them decide to get dressed but they are still angry at each other. Chandler decides to have the final laugh by stealing Joey’s underwear. This makes Joey mad and he decides to wear all the pants owned by Chandler. This infuriates Ross even more. There is a time crunch and he wants Joey to wear pants of a single color. Joey has N pants. Given a string S of length N, where S_{i} represents the colour of i^{th} pant. Joey is allowed to make up to K moves. To make a move, Joey has to do the following: Pick a prefix (possibly empty or complete), say P from the string S. Discard a suffix (possibly empty or complete) from P. Add P back to the string. For example, Let the pants be denoted by the string S = AABAD. In one move Joey can, pick a prefix P = AABA, discard the suffix ABA from P and finally, add P = A back to the string S. The final string becomes AD. After making up to K moves, Joey decides to wear the pants denoted by the final string, if all of them are of the same colour. Find the maximum number of pants that Joey can wear such that all of them are of the same colour, after making at most K moves. If it is impossible to wear pants of the same colour even after K moves, print -1. ------ Input Format ------ - First line will contain T, number of testcases. Then the testcases follow. - For each testcase first line of input contains two integers N, K, the number of pants and the maximum number of moves. - Second line of each testcase contains a string S consisting of uppercase english letters. i^{th} character from the beginning denotes the colour of the i^{th} layer of pants from top. ------ Output Format ------ For each testcase, output in a single line the maximum number of pants of the same colour that Joey can wear using atmost K moves. If it is impossible for Joey to wear pants of the same colour finally, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $0 ≤ K ≤ N$ $S$ consists of uppercase english letters only. - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 5 2 ABACA 4 1 ABAB ----- Sample Output 1 ------ 3 1 ----- explanation 1 ------ Test case 1: For the first move, Joey can pick the prefix with first 2 pants i.e. $AB$, out of this prefix, Joey can discard the suffix $B$ and add the remaining prefix i.e. $A$ back to the string $S$. Now the string $S$ is $AACA$. For the second move, Joey can pick the prefix with 3 pants i.e. $AAC$, discard the prefix $C$ and add remaining prefix $AA$ back to string. Finally, the string looks like $AAA$, i.e. $3$ pants of colour $A$. Test case 2: For the first and only move, Joey can pick the prefix $ABAB$, i.e. the entire string and discard the suffix $BAB$, and add the remaining prefix i.e. $A$ back to the string.
from sys import stdin input = stdin.readline def solve(ch, moves): ans = 0 i, count = 0, 0 while i < n and a[i] == ch: count += 1 i += 1 ans += count if i == n: return ans end, count = n - 1, 0 while end >= 0 and a[end] == ch: count += 1 end -= 1 ans += count if moves == 0: return -1 moves -= 1 take = [] while i < end: count = 0 while i < end and a[i] == ch: count += 1 i += 1 if count: take.append(count) i += 1 take.sort(reverse=True) for opp in range(min(len(take), moves)): ans += take[opp] return ans def answer(): ans = -1 for i in range(26): ans = max(ans, solve(chr(65 + i), k)) return ans for T in range(int(input())): n, k = map(int, input().split()) a = input().strip() print(answer())
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR NUMBER RETURN NUMBER VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Ross has to give an important speech at the museum and he wants all of his friends in the first row of the auditorium. Ross is punctual and gets ready on time. However, Joey and Chandler are behaving like children and fighting over everything. On getting a firm shouting from Ross, both of them decide to get dressed but they are still angry at each other. Chandler decides to have the final laugh by stealing Joey’s underwear. This makes Joey mad and he decides to wear all the pants owned by Chandler. This infuriates Ross even more. There is a time crunch and he wants Joey to wear pants of a single color. Joey has N pants. Given a string S of length N, where S_{i} represents the colour of i^{th} pant. Joey is allowed to make up to K moves. To make a move, Joey has to do the following: Pick a prefix (possibly empty or complete), say P from the string S. Discard a suffix (possibly empty or complete) from P. Add P back to the string. For example, Let the pants be denoted by the string S = AABAD. In one move Joey can, pick a prefix P = AABA, discard the suffix ABA from P and finally, add P = A back to the string S. The final string becomes AD. After making up to K moves, Joey decides to wear the pants denoted by the final string, if all of them are of the same colour. Find the maximum number of pants that Joey can wear such that all of them are of the same colour, after making at most K moves. If it is impossible to wear pants of the same colour even after K moves, print -1. ------ Input Format ------ - First line will contain T, number of testcases. Then the testcases follow. - For each testcase first line of input contains two integers N, K, the number of pants and the maximum number of moves. - Second line of each testcase contains a string S consisting of uppercase english letters. i^{th} character from the beginning denotes the colour of the i^{th} layer of pants from top. ------ Output Format ------ For each testcase, output in a single line the maximum number of pants of the same colour that Joey can wear using atmost K moves. If it is impossible for Joey to wear pants of the same colour finally, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $0 ≤ K ≤ N$ $S$ consists of uppercase english letters only. - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 5 2 ABACA 4 1 ABAB ----- Sample Output 1 ------ 3 1 ----- explanation 1 ------ Test case 1: For the first move, Joey can pick the prefix with first 2 pants i.e. $AB$, out of this prefix, Joey can discard the suffix $B$ and add the remaining prefix i.e. $A$ back to the string $S$. Now the string $S$ is $AACA$. For the second move, Joey can pick the prefix with 3 pants i.e. $AAC$, discard the prefix $C$ and add remaining prefix $AA$ back to string. Finally, the string looks like $AAA$, i.e. $3$ pants of colour $A$. Test case 2: For the first and only move, Joey can pick the prefix $ABAB$, i.e. the entire string and discard the suffix $BAB$, and add the remaining prefix i.e. $A$ back to the string.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() if k == 0: f = 0 for i in range(n - 1): if s[i] != s[i + 1]: f = 1 break if f == 0: print(n) else: print(-1) else: li = [] for i in range(26): a = chr(i + 65) ci = [] c = 0 f = 0 for i in range(n): if s[i] == a: c = c + 1 else: if c != 0: ci.append(c) c = 0 ci.append(c) if s[0] == a and s[n - 1] == a: if len(ci) == 1: c = ci[0] else: c = ci[0] + ci[-1] ci[0], ci[-1] = 0, 0 elif s[0] != a and s[n - 1] != a: c = 0 elif s[0] == a: c = ci[0] ci[0] = 0 elif s[n - 1] == a: c = ci[-1] ci[-1] = 0 ai = [0, c] ci.sort(reverse=True) c = c + ci[0] ai.append(c) for i in range(1, len(ci)): c = c + ci[i] ai.append(c) li.append(ai) ans = 0 for i in range(26): if len(li[i]) <= k: ans = max(ans, li[i][-1]) else: ans = max(ans, li[i][k]) if ans == 0: print(-1) else: print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Ross has to give an important speech at the museum and he wants all of his friends in the first row of the auditorium. Ross is punctual and gets ready on time. However, Joey and Chandler are behaving like children and fighting over everything. On getting a firm shouting from Ross, both of them decide to get dressed but they are still angry at each other. Chandler decides to have the final laugh by stealing Joey’s underwear. This makes Joey mad and he decides to wear all the pants owned by Chandler. This infuriates Ross even more. There is a time crunch and he wants Joey to wear pants of a single color. Joey has N pants. Given a string S of length N, where S_{i} represents the colour of i^{th} pant. Joey is allowed to make up to K moves. To make a move, Joey has to do the following: Pick a prefix (possibly empty or complete), say P from the string S. Discard a suffix (possibly empty or complete) from P. Add P back to the string. For example, Let the pants be denoted by the string S = AABAD. In one move Joey can, pick a prefix P = AABA, discard the suffix ABA from P and finally, add P = A back to the string S. The final string becomes AD. After making up to K moves, Joey decides to wear the pants denoted by the final string, if all of them are of the same colour. Find the maximum number of pants that Joey can wear such that all of them are of the same colour, after making at most K moves. If it is impossible to wear pants of the same colour even after K moves, print -1. ------ Input Format ------ - First line will contain T, number of testcases. Then the testcases follow. - For each testcase first line of input contains two integers N, K, the number of pants and the maximum number of moves. - Second line of each testcase contains a string S consisting of uppercase english letters. i^{th} character from the beginning denotes the colour of the i^{th} layer of pants from top. ------ Output Format ------ For each testcase, output in a single line the maximum number of pants of the same colour that Joey can wear using atmost K moves. If it is impossible for Joey to wear pants of the same colour finally, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $0 ≤ K ≤ N$ $S$ consists of uppercase english letters only. - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 5 2 ABACA 4 1 ABAB ----- Sample Output 1 ------ 3 1 ----- explanation 1 ------ Test case 1: For the first move, Joey can pick the prefix with first 2 pants i.e. $AB$, out of this prefix, Joey can discard the suffix $B$ and add the remaining prefix i.e. $A$ back to the string $S$. Now the string $S$ is $AACA$. For the second move, Joey can pick the prefix with 3 pants i.e. $AAC$, discard the prefix $C$ and add remaining prefix $AA$ back to string. Finally, the string looks like $AAA$, i.e. $3$ pants of colour $A$. Test case 2: For the first and only move, Joey can pick the prefix $ABAB$, i.e. the entire string and discard the suffix $BAB$, and add the remaining prefix i.e. $A$ back to the string.
T = int(input()) for i in range(T): N, K = map(int, input().split(" ")) S = list(str(input())) ascii_code_A = 65 if N == 1: print(N) elif K == 0: r = S[0] if S.count(r) != N: print(-1) else: print(N) elif K == 1: lr = 0 tc1 = 0 while lr < N and S[lr] == S[0]: lr += 1 tc1 += 1 rr = N - 1 tc2 = 0 while rr > -1 and S[rr] == S[N - 1]: rr -= 1 tc2 += 1 if rr == -1: print(N) elif S[0] == S[N - 1]: print(tc1 + tc2) else: print(max(tc1, tc2)) else: blocks = [[] for theta in range(26)] spec_val_left = ord(S[0]) - 65 spec_val_right = ord(S[N - 1]) - 65 lval_spec = 0 rval_spec = 0 counter = 0 cur = S[0] cur_block_size = 0 yval = False while counter < N: if S[counter] == cur: cur_block_size += 1 counter += 1 elif yval: val = ord(cur) - 65 blocks[val].append(cur_block_size) cur_block_size = 0 cur = S[counter] else: yval = True lval_spec = cur_block_size cur_block_size = 0 cur = S[counter] rval_spec = cur_block_size tc = [(0) for gamma in range(26)] for kappa in range(26): l = len(blocks[kappa]) blocks[kappa].sort(reverse=True) temp_count = 0 while temp_count < l and temp_count < K - 1: tc[kappa] += blocks[kappa][temp_count] temp_count += 1 if kappa == spec_val_left: tc[kappa] += lval_spec if kappa == spec_val_right: tc[kappa] += rval_spec print(max(tc))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Ross has to give an important speech at the museum and he wants all of his friends in the first row of the auditorium. Ross is punctual and gets ready on time. However, Joey and Chandler are behaving like children and fighting over everything. On getting a firm shouting from Ross, both of them decide to get dressed but they are still angry at each other. Chandler decides to have the final laugh by stealing Joey’s underwear. This makes Joey mad and he decides to wear all the pants owned by Chandler. This infuriates Ross even more. There is a time crunch and he wants Joey to wear pants of a single color. Joey has N pants. Given a string S of length N, where S_{i} represents the colour of i^{th} pant. Joey is allowed to make up to K moves. To make a move, Joey has to do the following: Pick a prefix (possibly empty or complete), say P from the string S. Discard a suffix (possibly empty or complete) from P. Add P back to the string. For example, Let the pants be denoted by the string S = AABAD. In one move Joey can, pick a prefix P = AABA, discard the suffix ABA from P and finally, add P = A back to the string S. The final string becomes AD. After making up to K moves, Joey decides to wear the pants denoted by the final string, if all of them are of the same colour. Find the maximum number of pants that Joey can wear such that all of them are of the same colour, after making at most K moves. If it is impossible to wear pants of the same colour even after K moves, print -1. ------ Input Format ------ - First line will contain T, number of testcases. Then the testcases follow. - For each testcase first line of input contains two integers N, K, the number of pants and the maximum number of moves. - Second line of each testcase contains a string S consisting of uppercase english letters. i^{th} character from the beginning denotes the colour of the i^{th} layer of pants from top. ------ Output Format ------ For each testcase, output in a single line the maximum number of pants of the same colour that Joey can wear using atmost K moves. If it is impossible for Joey to wear pants of the same colour finally, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $0 ≤ K ≤ N$ $S$ consists of uppercase english letters only. - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 5 2 ABACA 4 1 ABAB ----- Sample Output 1 ------ 3 1 ----- explanation 1 ------ Test case 1: For the first move, Joey can pick the prefix with first 2 pants i.e. $AB$, out of this prefix, Joey can discard the suffix $B$ and add the remaining prefix i.e. $A$ back to the string $S$. Now the string $S$ is $AACA$. For the second move, Joey can pick the prefix with 3 pants i.e. $AAC$, discard the prefix $C$ and add remaining prefix $AA$ back to string. Finally, the string looks like $AAA$, i.e. $3$ pants of colour $A$. Test case 2: For the first and only move, Joey can pick the prefix $ABAB$, i.e. the entire string and discard the suffix $BAB$, and add the remaining prefix i.e. $A$ back to the string.
import sys def read(): return sys.stdin.readline().strip() def read_int(): return int(read()) def read_arr(): return read().split() def read_int_arr(): return list(map(int, read_arr())) def out_str_list(l): sys.stdout.write(" ".join(l) + "\n") def out_nonstr_list(l): sys.stdout.write(" ".join(list(map(str, l))) + "\n") def out_str(s): sys.stdout.write(s + "\n") def out_nonstr(i): sys.stdout.write(str(i) + "\n") for _ in range(int(read())): n, k = read_int_arr() pants = read() if k == 0 and pants != pants[0] * n: out_nonstr(-1) continue elif pants == pants[0] * n: out_nonstr(n) continue all_colors = {} prev = -1 for p in pants: if p != prev: try: all_colors[p].append(0) except KeyError: all_colors[p] = [0] count = 0 prev = p try: all_colors[p][-1] += 1 except KeyError: all_colors[p] = [0] front_bonus = [pants[0], all_colors[pants[0]][0]] end_bonus = [pants[-1], all_colors[pants[-1]][-1]] colors = list(all_colors.keys()) m = 0 for color in colors: count = 0 s = all_colors[color] s.sort() if front_bonus[0] == color: count = front_bonus[1] s.remove(front_bonus[1]) if end_bonus[0] == color: count += end_bonus[1] s.remove(end_bonus[1]) available = k - 1 if len(s) <= available: count += sum(s) else: count += sum(s[len(s) - available : len(s)]) if count > m: m = count out_nonstr(m)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR LIST NUMBER ASSIGN VAR LIST VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Ilya is a very good-natured lion. He likes maths. Of all mathematical objects, his favourite one is matrices. Now he's faced a complicated matrix problem he needs to solve. He's got a square 2^{n} × 2^{n}-sized matrix and 4^{n} integers. You need to arrange all these numbers in the matrix (put each number in a single individual cell) so that the beauty of the resulting matrix with numbers is maximum. The beauty of a 2^{n} × 2^{n}-sized matrix is an integer, obtained by the following algorithm: Find the maximum element in the matrix. Let's denote it as m. If n = 0, then the beauty of the matrix equals m. Otherwise, a matrix can be split into 4 non-intersecting 2^{n} - 1 × 2^{n} - 1-sized submatrices, then the beauty of the matrix equals the sum of number m and other four beauties of the described submatrices. As you can see, the algorithm is recursive. Help Ilya, solve the problem and print the resulting maximum beauty of the matrix. -----Input----- The first line contains integer 4^{n} (1 ≤ 4^{n} ≤ 2·10^6). The next line contains 4^{n} integers a_{i} (1 ≤ a_{i} ≤ 10^9) — the numbers you need to arrange in the 2^{n} × 2^{n}-sized matrix. -----Output----- On a single line print the maximum value of the beauty of the described matrix. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 1 13 Output 13 Input 4 1 2 3 4 Output 14 -----Note----- Consider the second sample. You need to arrange the numbers in the matrix as follows: 1 2 3 4 Then the beauty of the matrix will equal: 4 + 1 + 2 + 3 + 4 = 14.
from sys import stdin n = int(stdin.readline()) arr = list(map(float, stdin.readline().split())) if n == 1: print(int(arr[0])) return arr.sort(reverse=True) res = 0 while arr: res += sum(arr) arr = arr[: len(arr) // 4] print(int(res))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Ilya is a very good-natured lion. He likes maths. Of all mathematical objects, his favourite one is matrices. Now he's faced a complicated matrix problem he needs to solve. He's got a square 2^{n} × 2^{n}-sized matrix and 4^{n} integers. You need to arrange all these numbers in the matrix (put each number in a single individual cell) so that the beauty of the resulting matrix with numbers is maximum. The beauty of a 2^{n} × 2^{n}-sized matrix is an integer, obtained by the following algorithm: Find the maximum element in the matrix. Let's denote it as m. If n = 0, then the beauty of the matrix equals m. Otherwise, a matrix can be split into 4 non-intersecting 2^{n} - 1 × 2^{n} - 1-sized submatrices, then the beauty of the matrix equals the sum of number m and other four beauties of the described submatrices. As you can see, the algorithm is recursive. Help Ilya, solve the problem and print the resulting maximum beauty of the matrix. -----Input----- The first line contains integer 4^{n} (1 ≤ 4^{n} ≤ 2·10^6). The next line contains 4^{n} integers a_{i} (1 ≤ a_{i} ≤ 10^9) — the numbers you need to arrange in the 2^{n} × 2^{n}-sized matrix. -----Output----- On a single line print the maximum value of the beauty of the described matrix. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 1 13 Output 13 Input 4 1 2 3 4 Output 14 -----Note----- Consider the second sample. You need to arrange the numbers in the matrix as follows: 1 2 3 4 Then the beauty of the matrix will equal: 4 + 1 + 2 + 3 + 4 = 14.
from sys import * def arr_inp(): return [float(x) for x in stdin.readline().split()] def solve(n): global s if n == 1: s += a[0] return else: solve(n // 4) s += sum(a[:n]) n, a, s = int(input()), sorted(arr_inp(), reverse=True), 0 solve(n) print(int(s))
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Ilya is a very good-natured lion. He likes maths. Of all mathematical objects, his favourite one is matrices. Now he's faced a complicated matrix problem he needs to solve. He's got a square 2^{n} × 2^{n}-sized matrix and 4^{n} integers. You need to arrange all these numbers in the matrix (put each number in a single individual cell) so that the beauty of the resulting matrix with numbers is maximum. The beauty of a 2^{n} × 2^{n}-sized matrix is an integer, obtained by the following algorithm: Find the maximum element in the matrix. Let's denote it as m. If n = 0, then the beauty of the matrix equals m. Otherwise, a matrix can be split into 4 non-intersecting 2^{n} - 1 × 2^{n} - 1-sized submatrices, then the beauty of the matrix equals the sum of number m and other four beauties of the described submatrices. As you can see, the algorithm is recursive. Help Ilya, solve the problem and print the resulting maximum beauty of the matrix. -----Input----- The first line contains integer 4^{n} (1 ≤ 4^{n} ≤ 2·10^6). The next line contains 4^{n} integers a_{i} (1 ≤ a_{i} ≤ 10^9) — the numbers you need to arrange in the 2^{n} × 2^{n}-sized matrix. -----Output----- On a single line print the maximum value of the beauty of the described matrix. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 1 13 Output 13 Input 4 1 2 3 4 Output 14 -----Note----- Consider the second sample. You need to arrange the numbers in the matrix as follows: 1 2 3 4 Then the beauty of the matrix will equal: 4 + 1 + 2 + 3 + 4 = 14.
n = int(input()) lis = list(map(float, input().split())) ans = 0 lis.sort(reverse=1) while lis: ans += sum(lis) lis = lis[: len(lis) // 4] print(int(ans))
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 EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
def Solve(): n, m = map(int, input().split()) cost = [int(x) for x in input().split()] c = 0 for _ in range(m): x, y = map(int, input().split()) c += min(cost[x - 1], cost[y - 1]) print(c) Solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
from sys import stdin, stdout nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int, stdin.readline().split())) for _ in range(1): n, eg = lst() a = lst() ans = 0 for i in range(eg): u, v = lst() ans += min(a[u - 1], a[v - 1]) print(ans)
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 ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = [int(c) for c in input().split()] v = [0] + [int(c) for c in input().split()] chains = [[] for _ in range(n + 1)] for i in range(m): a, b = [int(c) for c in input().split()] chains[a].append(b) chains[b].append(a) res = 0 for i in range(n): m = 0 ind = 0 for j in range(len(v)): if v[j] > m: m = v[j] ind = j res += sum([v[_] for _ in chains[ind]]) v[ind] = 0 print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
from itertools import combinations def input_ints(): return list(map(int, input().split())) def output_list(v): print(" ".join(str(x) for x in v)) def main(): n, m = input_ints() v = input_ints() ans = 0 for i in range(m): x, y = input_ints() ans += min(v[x - 1], v[y - 1]) print(ans) main()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = map(int, input().split()) v = list(map(int, input().split())) r = [set() for i in range(n)] for i in range(m): n1, n2 = (int(x) - 1 for x in input().split()) r[n1].add(n2) r[n2].add(n1) val = 0 for x in reversed(sorted((x, i) for i, x in enumerate(v))): for y in r[x[1]]: val += v[y] r[y].remove(x[1]) print(val)
ASSIGN VAR 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 VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
r = lambda: map(int, input().split()) n, m = r() a = [0] + list(r()) print(sum(min(a[u], a[v]) for u, v in (r() for i in range(m))))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = map(int, input().split()) arr = list(map(int, input().split())) d = [] d2 = [] for i in range(m): x, y = map(int, input().split()) d.append(min(arr[x - 1], arr[y - 1])) print(sum(d))
ASSIGN VAR 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 LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
def main(): n, m = map(int, input().split()) v, g = list(map(int, input().split())), [0] * n graph, visited = [[] for i in range(n)], [0] * n stack = [] for i in range(m): s, t = map(lambda x: int(x) - 1, input().split()) graph[s].append(t) graph[t].append(s) g[s] += 1 g[t] += 1 def process(u): if not g[u]: return 0 ans = g[u] * v[u] for t in graph[u]: g[t] -= 1 g[u] -= 1 return ans def dfs(u): visited[u] = 1 for v in graph[u]: if not visited[v]: dfs(v) stack.append(u) ans = 0 for i in range(n): if not visited[i]: dfs(i) ans += sum(map(process, sorted(stack, key=lambda x: v[x]))) stack.clear() print(ans) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
a = list(map(int, input().split())) energy = list(map(int, input().split())) nodes = [] for i in range(a[1]): tmp = list(map(int, input().split())) nodes.append((tmp[0], tmp[1])) summ = 0 while True: tmp = [] maxx = max(energy) andis = energy.index(maxx) energy[andis] = -1 andis += 1 for item in nodes: if andis not in item: tmp.append(item) elif item[0] != andis: summ += energy[item[0] - 1] else: summ += energy[item[1] - 1] nodes = tmp if len(nodes) == 0: print(summ) break
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 ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
class pair: def __init__(self, first, second): self.first = first self.second = second def ans(graph, v, n): result = 0 arr = [] for i in range(0, n): arr.append(pair(v[i], i)) arr.sort(key=lambda p: (p.first, p.second)) arr.reverse() for i in range(0, n): for j in graph[arr[i].second]: result += v[j] v[arr[i].second] = 0 return result n, m = map(int, input().split()) v = list(map(int, input().split())) graph = [[] for i in range(0, 1001)] for _ in range(0, m): x, y = map(int, input().split()) graph[x - 1].append(y - 1) graph[y - 1].append(x - 1) print(ans(graph, v, n))
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR 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 LIST VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
import sys __author__ = "asmn" n, m = tuple(map(int, input().split())) v = list(map(int, input().split())) e = [[] for i in range(n)] for k in range(m): x, y = tuple(map(int, input().split())) e[x - 1].append(y - 1) e[y - 1].append(x - 1) nx = sorted(list(range(n)), key=lambda x: v[x], reverse=True) used = set() ans = 0 for x in nx: for y in e[x]: if y not in used: ans += v[y] used.add(x) print(ans)
IMPORT ASSIGN VAR STRING ASSIGN VAR 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 ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = map(int, input().split()) l = list(map(int, input().split())) l.insert(0, 0) ans = 0 for i in range(m): x, y = map(int, input().split()) ans += min(l[x], l[y]) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = map(int, input().split()) t = list(map(int, input().split())) adj = [[] for i in range(n + 1)] for j in range(m): a, b = map(int, input().split()) adj[a].append(b) adj[b].append(a) ans = 0 u = [] for j in range(n + 1): for k in adj[j]: if k > j: ans += min(t[k - 1], t[j - 1]) print(ans)
ASSIGN VAR 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 LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR 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 NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
def main(): n, m = map(int, input().split()) vv = list(map(int, input().split())) print(sum(min(vv[i - 1] for i in map(int, input().split())) for _ in range(m))) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
line = input().split() n = int(line[0]) m = int(line[1]) line = input().split() weights = [0] * n edges = [0] * n for i in range(n): weights[i] = [int(line[i]), i] edges[i] = [] for i in range(m): line = input().split() a = int(line[0]) - 1 b = int(line[1]) - 1 edges[a].append(b) edges[b].append(a) sortedWeights = sorted(weights, reverse=True) ans = 0 removed = [] for element in sortedWeights: index = element[1] for neighboor in edges[index]: if neighboor not in removed: ans += weights[neighboor][0] removed.append(index) print(ans)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
import sys def answer(n, m, v, x, y): total = 0 for i in range(m): total += min(v[x[i]], v[y[i]]) return total def main(): n, m = map(int, sys.stdin.readline().split()) v = [0] v.extend(list(map(int, sys.stdin.readline().split()))) x = [(0) for _ in range(m)] y = [(0) for _ in range(m)] for i in range(m): x[i], y[i] = map(int, sys.stdin.readline().split()) print(answer(n, m, v, x, y)) return main()
IMPORT FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
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()) n, m = invr() ans = 0 arr = inlt() for i in range(m): a, b = invr() ans += min(arr[b - 1], arr[a - 1]) print(ans)
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 VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
def R(): return map(int, input().split()) n, m = R() arr = list(R()) print(sum(min(arr[j - 1] for j in R()) for i in range(m)))
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = map(int, input().split(" ")) a = list(map(int, input().split(" "))) print( sum( min(a[x - 1], a[y - 1]) for x, y in (map(int, input().split(" ")) for i in range(m)) ) )
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = map(int, input().split()) v1 = list(map(int, input().split())) v = [[w, i] for i, w in enumerate(v1)] v.sort(reverse=True) d = [set() for i in range(n)] for i in range(m): a, b = map(int, input().split()) a -= 1 b -= 1 d[a].add(b) d[b].add(a) ans = 0 for w, i in v: for j in d[i]: ans += v1[j] d[j].discard(i) print(ans)
ASSIGN VAR 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 LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FOR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
def main(): n, m = map(int, input().split()) v, ans = list(map(int, input().split())), 0 for i in range(m): ans += min(v[int(x) - 1] for x in input().split()) print(ans) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
edges = [int(x) for x in input().split(" ")][1] arr = [int(x) for x in input().split(" ")] energy = 0 for i in range(edges): t = [int(x) for x in input().split(" ")] energy += min(arr[t[0] - 1], arr[t[1] - 1]) print(energy)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = map(int, input().split(" ")) v = list(map(int, input().split(" "))) vd = {} for i, x in enumerate(v): vd[i] = x g = [[] for i in range(n)] for i in range(m): x, y = map(int, input().split(" ")) x -= 1 y -= 1 g[x].append(y) g[y].append(x) v = [[-x, i] for i, x in enumerate(v)] v.sort() ans = 0 removed = [0] * n for i in range(n): x = v[i][1] removed[x] = 1 for y in g[x]: if not removed[y]: ans += vd[y] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = map(int, input().split()) values = [-10000] + list(map(int, input().split())) g = [[] for i in range(n + 10)] costs = [0] * (n + 10) for i in range(m): u, v = map(int, input().split()) g[u] += [v] g[v] += [u] for i in range(1, n + 1): for neighbor in g[i]: costs[i] += values[neighbor] for i in range(0, n + 1): values[i] = values[i], i values = sorted(values, reverse=True) total = 0 for i in range(0, n): node = values[i][1] cost = values[i][0] total += costs[node] for neighbor in g[node]: costs[neighbor] -= cost print(total)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR LIST VAR VAR VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR FOR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
R = lambda: map(int, input().split()) n, m = R() v = list(R()) g = [set() for i in range(n + 1)] for i in range(m): x, y = R() g[x].add(y) g[y].add(x) s = 0 for w, i in sorted(zip(v, range(1, n + 1)), reverse=True): for j in g[i]: s += v[j - 1] g[j].remove(i) print(s)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = map(int, input().split()) v = list(map(int, input().split())) graph = [set() for _ in range(n)] strengths = list() ans = 0 for i in range(m): x, y = map(int, input().split()) graph[x - 1].add(y - 1) graph[y - 1].add(x - 1) for i in range(n): strengths.append([v[i], i]) strengths = list(reversed(sorted(strengths))) while len(strengths) > 0: vi, i = strengths.pop(0) ans += sum(v[j] for j in graph[i]) for s in strengths: if i in graph[s[1]]: graph[s[1]].remove(i) print(ans)
ASSIGN VAR 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 VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = [int(x) for x in input().split()] v = [int(x) for x in input().split()] sv = [(x + 1, v[x]) for x in range(n)] v = [0] + v edges = {i: set() for i in range(1, n + 1)} for i in range(m): f, t = [int(x) for x in input().split()] edges[f].add(t) edges[t].add(f) ans = 0 sv.sort(key=lambda x: -x[1]) removed = [False] * (n + 1) for i, vi in sv: removed[i] = True for f in edges[i]: if not removed[f]: ans += v[f] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
def solve(n, m, energy, link): total = 0 for x, y in link: total += min(energy[x - 1], energy[y - 1]) return total n, m = list(map(int, input().split())) ener = list(map(int, input().split())) mat = [] for _ in range(m): mat.append(list(map(int, input().split()))) print(solve(n, m, ener, mat))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR 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 ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
def byF(p): return p[0] def byS(p): return p[1] def getOrder(ls): pairs = [(i, ls[i]) for i in range(len(ls))] pairs.sort(key=byS) return list(map(byF, pairs)) n, m = map(int, input().split()) vs = list(map(int, input().split())) edges = [[] for x in range(n)] delted = [False] * n for _ in range(m): i, j = map(int, input().split()) edges[i - 1].append(j - 1) edges[j - 1].append(i - 1) order = getOrder(vs) order = order[::-1] res = 0 for curr in order: for neigh in edges[curr]: if not delted[neigh]: res += vs[neigh] delted[curr] = True print(res)
FUNC_DEF RETURN VAR NUMBER FUNC_DEF RETURN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR 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 LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
energy = 0 n, m = [int(x) for x in input().split()] prices = [None] + [int(x) for x in input().split()] for _ in range(m): a, b = [int(x) for x in input().split()] energy += min(prices[a], prices[b]) print(energy)
ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
ans = 0 def dfs(i): global ans x = i vis[i] = 1 for j in grid[i]: ans += min(l[x - 1], l[j - 1]) if vis[j] == 0: dfs(j) for _ in range(1): n, k = map(int, input().split()) l = list(map(int, input().split())) grid = [[] for i in range(n + 1)] for i in range(k): a, b = map(int, input().split()) grid[a].append(b) grid[b].append(a) vis = [0] * (n + 1) for i in range(1, n + 1): if vis[i] == 0: dfs(i) print(ans // 2)
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR 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 LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR 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 IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = list(map(int, input().split())) cs = [0] + list(map(int, input().split())) mls = [(0) for i in range(n + 1)] for i in range(m): x, y = list(map(int, input().split())) if cs[y] < cs[x]: x, y = y, x mls[x] += 1 print(sum([(cs[i] * mls[i]) for i in range(n + 1)]))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = map(int, input().split()) v = list(map(int, input().split())) tot = 0 for i in range(m): x, y = map(int, input().split()) if v[x - 1] <= v[y - 1]: tot += v[x - 1] else: tot += v[y - 1] print(tot)
ASSIGN VAR 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 NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
def mlt(): return map(int, input().split()) x, y = mlt() s = [0] + list(mlt()) res = 0 for n in range(y): a, b = mlt() res += min(s[a], s[b]) print(res)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
from sys import stdin, stdout n, m = map(int, stdin.readline().split()) graph = [[] for _ in range(n + 1)] used = [False] * (n + 1) energy = [0] + list(map(int, stdin.readline().split())) sorted_energies = sorted(map(lambda x: (x[1], x[0]), enumerate(energy)), reverse=True) result = 0 for _ in range(m): a, b = map(int, stdin.readline().split()) graph[a].append(b) graph[b].append(a) for item in sorted_energies: if not used[item[1]]: used[item[1]] = True for adj in graph[item[1]]: if not used[adj]: result += energy[adj] stdout.write(str(result) + "\n")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
import sys input = lambda: sys.stdin.readline().strip("\r\n") n, m = map(int, input().split()) v = list(map(int, input().split())) ans = 0 for _ in range(m): x, y = map(int, input().split()) ans += min(v[x - 1], v[y - 1]) print(ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR 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 NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = map(int, input().split()) r = sorted(enumerate(map(int, input().split())), key=lambda x: x[1]) p = [[] for i in range(n)] for i in range(m): x, y = map(lambda n: int(n) - 1, input().split()) p[x] += [y] p[y] += [x] q = [len(i) for i in p] s = 0 for x, f in r: s += q[x] * f for y in p[x]: q[y] -= 1 print(s)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR VAR VAR LIST VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
t = input().split() n = int(t[0]) m = int(t[1]) slon = 0 num = [int(x) for x in input().split()] for i in range(m): t = input().split() slon += min(num[int(t[0]) - 1], num[int(t[1]) - 1]) print(slon)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
from sys import stdin, stdout n, m = map(int, stdin.readline().split()) v = list(map(int, stdin.readline().split())) g = [list() for i in range(n)] for i in range(m): x, y = map(int, stdin.readline().split()) g[x - 1].append(y - 1) g[y - 1].append(x - 1) def main(): res = 0 vis = [(False) for i in range(n)] q = [] q.append(0) vis[0] = True mp = dict() for i, _ in enumerate(g): for j in g[i]: if mp.get((i, j), 0) == 0 and mp.get((j, i), 0) == 0: res += min(v[i], v[j]) mp[i, j] = 1 mp[j, i] = 1 stdout.write(str(res)) main()
ASSIGN VAR 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 VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
I = lambda: list(map(int, input().split())) n, m = I() l = I() ans = 0 for i in range(m): x, y = I() ans += min(l[x - 1], l[y - 1]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
class graph(object): def __init__(self, n): self.nodes = [(i + 1) for i in range(n)] self.edges = [] def del_node(self, x): self.nodes.remove(x) def add_edge(self, x, y): self.edges.append(sorted([x, y])) def del_edge(self, x, y): if sorted([x, y]) in self.edges: self.edges.remove(sorted([x, y])) n, m = input().split() n, m = int(n), int(m) children = graph(n) v = [int(i) for i in input().split()] v = {(i + 1): v[i] for i in range(len(v))} for e in range(m): x, y = input().split() x, y = int(x), int(y) children.add_edge(x, y) energy = 0 while v != {}: min_energy = min(v.values()) todelete = [] for edge in children.edges: x = min(v[edge[0]], v[edge[1]]) if x == min_energy: energy += min_energy todelete.append([edge[0], edge[1]]) for deledge in todelete: children.del_edge(deledge[0], deledge[1]) v = {x: v[x] for x in v if v[x] != min_energy} print(energy)
CLASS_DEF VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR FUNC_DEF IF FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
def mapit(): temp = list(map(int, input().split())) return temp def solution(): n, m = mapit() val = mapit() connect = [0] * n removed = set() for i in range(m): x, y = mapit() if connect[x - 1] == 0: connect[x - 1] = [(y - 1, val[y - 1])] else: connect[x - 1].append((y - 1, val[y - 1])) if connect[y - 1] == 0: connect[y - 1] = [(x - 1, val[x - 1])] else: connect[y - 1].append((x - 1, val[x - 1])) prio = [] for i in range(n): prio.append((val[i], i)) prio.sort() ans = 0 while prio: curr = prio.pop() curr_node = curr[1] removed.add(curr_node) if connect[curr_node] != 0: for node, value in connect[curr_node]: if node not in removed: ans += value print(ans) return solution()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = map(int, input().split()) arr = list(map(int, input().split())) adj = list(tuple(map(int, input().split())) for _ in range(m)) res = 0 for i, j in adj: res += min(arr[i - 1], arr[j - 1]) print(res)
ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
INF = 10**10 n, m = map(int, input().split()) w = list([0, a] for a in map(int, input().split())) summ = 0 adj = [([0] * n) for _ in range(n)] for _ in range(m): x, y = map(int, input().split()) adj[x - 1][y - 1] = adj[y - 1][x - 1] = 1 w[x - 1][0] += w[y - 1][1] w[y - 1][0] += w[x - 1][1] for i in range(n - 1): k = w.index(max(w, key=lambda x: (x[1], -x[0]))) summ += w[k][0] for j in range(n): if adj[j][k] == 1: adj[j][k] = adj[k][j] = 0 w[j][0] -= w[k][1] w[k][0] = INF w[k][1] = 0 print(summ)
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
N, M = list(map(int, input().split(" "))) cost = list(map(int, input().split(" "))) cost.insert(0, -1) adjacency_list = [[] for i in range(N + 1)] for i in range(M): a, b = list(map(int, input().split(" "))) adjacency_list[a].append(b) adjacency_list[b].append(a) nodes_by_cost = list( reversed(sorted([(x + 1) for x in range(N)], key=lambda y: cost[y])) ) total_cost = 0 deleted = [(False) for x in range(N + 1)] for node in nodes_by_cost: cost_for_removing_node = 0 for neig in adjacency_list[node]: if not deleted[neig]: cost_for_removing_node += cost[neig] deleted[node] = True total_cost += cost_for_removing_node print(total_cost)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = tuple(map(int, input().split())) v = list(map(int, input().split())) ropes = [] links = [] for j in range(n): links.append([]) for k in range(m): a, b = tuple(map(int, input().split())) links[a - 1].append(b - 1) links[b - 1].append(a - 1) ropes.append((a - 1, b - 1)) for i in range(n): v[i] = v[i], i v = sorted(v, reverse=True) out = set([]) res = 0 for j in range(n): out.add(v[j][1]) for i in range(n): if not v[i][1] in out and v[i][1] in links[v[j][1]]: res += v[i][0] print(res)
ASSIGN VAR 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 ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = [int(x) for x in input().split()] v = [int(x) for x in input().split()] a = [[] for x in range(n)] used = [0] * n for i in range(m): x, y = [int(x) for x in input().split()] x -= 1 y -= 1 a[x].append(y) a[y].append(x) l = sorted(zip(v, range(n)), key=lambda x: -x[0]) ans = 0 for _, i in l: used[i] = 1 for x in a[i]: if not used[x]: ans += v[x] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = input().split() v = list(map(int, input().split())) ans = 0 for _ in range(int(m)): ans += v[min(map(int, input().split()), key=lambda x: v[x - 1]) - 1] print(ans)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
dic = {} n, m = map(int, input().split()) v = [int(x) for x in input().split()] for i in range(m): x, y = [(int(x) - 1) for x in input().split()] if x not in dic: dic[x] = [y] else: dic[x].append(y) if y not in dic: dic[y] = [x] else: dic[y].append(x) lst = [[v[x], x] for x in range(n)] lst.sort(reverse=True) cost = 0 for i in lst: if i[1] in dic: for j in dic[i[1]]: cost += v[j] v[i[1]] = 0 print(cost)
ASSIGN VAR DICT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR FOR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
x, y = input().split() a = list(map(int, input().split())) ans = 0 for i in range(0, int(y)): v1, v2 = input().split() ans += min(a[int(v1) - 1], a[int(v2) - 1]) print(ans)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = map(int, input().split()) g = [([0] * n) for i in range(n)] v = list(map(int, input().split())) for i in range(m): x, y = map(int, input().split()) x -= 1 y -= 1 g[x][y] = v[y] g[y][x] = v[x] kek = sorted((i for i in range(n)), key=lambda x: v[x], reverse=True) ans = 0 for i in kek: ans += sum(g[i]) for j in range(n): if j != i: g[j][i] = 0 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = [int(x) for x in input().strip().split()] energy = [int(x) for x in input().strip().split()] d = {i: [] for i in range(1, n + 1)} for i in range(m): k, l = [int(x) for x in input().strip().split()] d[k].append(l) d[l].append(k) s_energy = sorted(list(zip(list(range(1, n + 1)), energy)), key=lambda x: -x[1]) cost = 0 visited = [False] * n for part, val in s_energy: visited[part - 1] = True for i in d[part]: if not visited[i - 1]: cost += energy[i - 1] print(cost)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
def main(): n, m = map(int, input().split()) cur = list(map(int, input().split())) tab = [] graph = [[] for x in range(n)] for x in range(n): tab.append((cur[x], x)) for x in range(m): a, b = map(int, input().split()) a -= 1 b -= 1 graph[a].append(b) graph[b].append(a) tab.sort() tab = tab[::-1] v = [0] * n s = 0 for x in range(n): for nei in graph[tab[x][1]]: if not v[nei]: s += cur[nei] v[tab[x][1]] = 1 print(s) main()
FUNC_DEF ASSIGN VAR 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 LIST ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
def Sol2(cost, cost2, G): mk = [(0) for _ in range(len(cost))] sum = 0 for node in cost: for i in G[node[1]]: mk[i] += 1 c = len(G[node[1]]) - mk[node[1]] sum += c * node[0] return sum n, m = map(int, input().split()) cost2 = [int(x) for x in input().split()] cost = [(cost2[i], i) for i in range(len(cost2))] arist = [] graf = [[] for i in range(n)] for i in range(m): x, y = map(int, input().split()) arist.append((x - 1, y - 1)) graf[x - 1].append(y - 1) graf[y - 1].append(x - 1) sort = sorted(cost, key=lambda parameter_list: parameter_list[0], reverse=False) a = Sol2(sort, cost2, graf) print(a)
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
n, m = [int(i) for i in input().split()] x = input().split() popa = [[int(x[i]), i] for i in range(n)] graph = [[] for i in range(n + 1)] for i in range(m): x, y = map(int, input().split()) graph[x].append(y) graph[y].append(x) popa = sorted(popa)[::-1] dic = {(i[1] + 1): i[0] for i in popa} suma = 0 d = [] for x in popa: v = x[1] + 1 for i in graph[v]: if i not in d: suma += dic[i] d.append(v) print(suma)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR 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 NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 ≤ v_{i} ≤ 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 ≤ x_{i}, y_{i} ≤ n; x_{i} ≠ y_{i}). Consider all the parts are numbered from 1 to n. -----Output----- Output the minimum total energy the child should spend to remove all n parts of the toy. -----Examples----- Input 4 3 10 20 30 40 1 4 1 2 2 3 Output 40 Input 4 4 100 100 100 100 1 2 2 3 2 4 3 4 Output 400 Input 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 Output 160 -----Note----- One of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts.
class IOHandlerObject(object): def getInput(self, mode=2): inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return [int(x) for x in inputs.split()] def writeOutput(self, s="\n"): if isinstance(s, list): s = " ".join(s) print(s) IOHandler = IOHandlerObject() g = IOHandler.getInput w = IOHandler.writeOutput n, m = g() v = g() sm = 0 for i in range(m): x, y = g() sm += min(v[x - 1], v[y - 1]) print(sm)
CLASS_DEF VAR FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_DEF STRING IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR