description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
for _ in range(int(input())):
n, p, k = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
pref = [0]
for i in a[: k - 1]:
pref.append(pref[-1] + i)
ans = 0
for i, num in enumerate(pref):
cur_ans = 0
_p = p
if _p >= num:
_p -= num
cur_ans += i
else:
break
for j in range(i + k - 1, n, k):
if _p >= a[j]:
cur_ans += k
_p -= a[j]
else:
break
ans = max(ans, cur_ans)
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
for t in range(int(input())):
n, p, k = map(int, input().split())
a = [0] + sorted(list(map(int, input().split())))
dp = [(-1) for i in range(n + 1)]
dp[0] = p
for i in range(1, n + 1):
if i - k < 0:
dp[i] = max(dp[i], dp[i - 1] - a[i])
else:
dp[i] = max(dp[i], dp[max(0, i - k)] - a[i])
maxi = 0
for i in range(n + 1):
if dp[i] >= 0:
maxi = i
print(maxi)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
def check(costs, k, bought):
res = 0
while bought > 0:
res += costs[bought - 1]
if bought >= k:
bought -= k
else:
bought -= 1
return res
t = int(input())
for _ in range(t):
n, p, k = list(map(int, input().split(" ")))
costs = sorted(list(map(int, input().split(" "))))
ans = 0
mon_req = [0] * (n + 1)
for i in range(1, n + 1):
if i >= k:
mon_req[i] = mon_req[i - k] + costs[i - 1]
else:
mon_req[i] = mon_req[i - 1] + costs[i - 1]
for i, mon in enumerate(mon_req):
if mon <= p:
ans = i
print(ans)
|
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
t = int(input())
for test in range(t):
n, p, k = map(int, input().split())
l = list(map(int, input().split()))
l.sort()
sum1 = [0] * (n + 1)
ans = 0
sum1[0] = 0
sum1[1] = l[0]
if sum1[1] <= p:
ans = 1
for i in range(2, k):
sum1[i] = sum1[i - 1] + l[i - 1]
if sum1[i] <= p:
ans = i
for i in range(k, n + 1):
sum1[i] = sum1[i - k] + l[i - 1]
if sum1[i] <= p:
ans = i
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
for h in range(int(input())):
n, p, k = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
a.sort()
a.insert(0, 0)
price = p
maxi = 0
for i in range(k):
if a[i] <= price:
p -= a[i]
price = p
j = i + k
goods = i
while j < n + 1:
if a[j] <= price:
goods += k
price -= a[j]
j += k
maxi = max(maxi, goods)
print(maxi)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
for _ in [0] * int(input()):
n, p, k = map(int, input().split())
a = sorted(list(map(int, input().split())))
pref = 0
ans = 0
for i in range(k + 1):
sum = pref
if sum > p:
break
cnt = i
for j in range(i + k - 1, n, k):
if sum + a[j] <= p:
cnt += k
sum += a[j]
else:
break
if i < n:
pref += a[i]
ans = max(ans, cnt)
print(ans)
|
FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
q = int(input())
def binarySearch(arr, l, r, x):
if 1 <= len(arr[l : r + 1]) <= 2:
return l
if r >= l:
mid = int(l + (r - l) / 2)
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binarySearch(arr, l, mid - 1, x)
else:
return binarySearch(arr, mid + 1, r, x)
else:
return -1
def solve():
n, pr, k = map(int, input().split())
L = list(map(int, input().split()))
L.sort()
dp = [0] * n
ans = 0
for i in range(0, k - 1):
dp[i] += dp[i - 1] + L[i]
for i in range(k - 1, n):
if i == k - 1:
dp[i] += L[i]
else:
dp[i] += dp[i - k] + L[i]
for i in range(n):
if dp[i] <= pr:
ans = i + 1
print(ans)
while q:
solve()
q -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR 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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
for _ in range(int(input())):
n, p, k = map(int, input().split())
a = sorted(list(map(int, input().split())))
leftMoney = [(0) for i in range(n)]
dp = [(0) for i in range(n)]
leftMoney[0] = p
for i in range(n):
if i >= k - 1:
if leftMoney[i] >= a[i]:
dp[i] = dp[i - 1] + 1
if i + 1 < n:
leftMoney[i + 1] = leftMoney[i] - a[i]
else:
dp[i] = dp[i - 1]
if i + 1 < n:
leftMoney[i + 1] = leftMoney[i]
if leftMoney[i - k + 1] >= a[i]:
dp[i] = max(dp[i], k + (dp[i - k] if i >= k else 0))
if i + 1 < n:
leftMoney[i + 1] = leftMoney[i - k + 1] - a[i]
elif leftMoney[i] >= a[i]:
dp[i] = dp[i - 1] + 1
if i + 1 < n:
leftMoney[i + 1] = leftMoney[i] - a[i]
else:
dp[i] = dp[i - 1]
if i + 1 < n:
leftMoney[i + 1] = leftMoney[i]
print(dp[n - 1])
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
t = int(input())
for _ in range(t):
n, p, k = map(int, input().split())
a = list(map(int, input().split()))
a = sorted(a)
dp = [(10**10) for i in range(n + 1)]
dp[0] = 0
for i in range(1, n + 1):
dp[i] = min(dp[i], dp[i - 1] + a[i - 1])
if i >= k:
dp[i] = min(dp[i], dp[i - k] + a[i - 1])
ans = 0
for i in range(n + 1):
if dp[i] <= p:
ans = i
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
t = int(input())
for i in range(t):
n, p, k = map(int, input().split())
l = list(map(int, input().split()))
l.sort()
l1 = []
c = 0
for i in range(n):
if i < k - 1:
c += l[i]
l1.append(c)
elif i == k - 1:
l1.append(l[i])
else:
l1.append(l[i] + l1[i - k])
ans = 0
for i in range(n - 1, -1, -1):
if l1[i] <= p:
ans = i + 1
break
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
import sys
input = sys.stdin.readline
a = int(input())
for i in range(a):
n, p, k = map(int, input().split())
z = list(map(int, input().split()))
z.sort()
ans = [0]
for i in range(len(z)):
ans.append(ans[-1] + z[i])
dp = [(0) for i in range(len(ans))]
for i in range(len(ans)):
if i < k:
dp[i] = ans[i]
else:
dp[i] = dp[i - k] + z[i - 1]
save = 0
for i in range(len(dp) - 1, -1, -1):
if dp[i] <= p:
save = i
break
print(save)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
t = int(input())
for _ in range(t):
n, p, k = map(int, input().split())
a = [int(i) for i in input().split()]
a = sorted(a)
dp = [0]
for i in range(k - 1):
dp.append(dp[-1] + a[i])
for i in range(k - 1, n):
dp.append(dp[i - k + 1] + a[i])
ans = 0
for i in range(1, n + 1):
if dp[i] <= p:
ans = i
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
def idp(a, dp, i, k):
if i - k >= 0:
return min(a[i] + dp[i - 1], a[i] + dp[i - k])
else:
return a[i] + dp[i - 1]
t = int(input())
for tc in range(t):
n, p, k = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()
dp = [0] * n
dp[0] = a[0]
for i in range(0, k - 1):
dp[i] = a[i] + dp[i - 1]
dp[k - 1] = a[k - 1]
for i in range(k, n):
dp[i] = idp(a, dp, i, k)
bst = 0
for i in range(n):
if dp[i] <= p:
bst = i + 1
print(bst)
|
FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL 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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
import sys
t = int(input())
input = sys.stdin.readline
while t > 0:
t -= 1
n, p, k = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()
dp = [(0) for i in range(n + 1)]
for i in range(1, n + 1):
if i - k >= 0:
dp[i] = a[i - 1] + dp[i - k]
else:
dp[i] = dp[i - 1] + a[i - 1]
flag = 0
z = 0
for i in range(1, n + 1):
if dp[i] > p:
pass
else:
z = i
print(z)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL 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 VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
for _ in range(int(input())):
n, p, k = map(int, input().split(" "))
arr = list(map(int, input().split(" ")))
arr.sort()
ans = 0
tempans = 0
for qw in range(k):
ptemp = p
tempans = qw
for i in range(qw + k - 1, n, k):
ptemp -= arr[i]
if ptemp >= 0:
tempans += k
else:
break
ans = max(tempans, ans)
p -= arr[qw]
if p < 0:
break
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
T = int(input())
for t in range(1, T + 1):
n, p, k = map(int, input().split())
values = sorted(list(map(int, input().split())))
cumsum = [0]
for i in range(k - 1):
cumsum.append(cumsum[-1] + values[i])
best = 0
for i in range(k):
ith_best = 0
mysum = cumsum[i]
if mysum > p:
best = max(best, ith_best)
break
else:
ith_best = i
best = max(best, ith_best)
for j in range(i + k - 1, n, k):
mysum += values[j]
if mysum > p:
break
else:
ith_best += k
best = max(best, ith_best)
print(best)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR 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 LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
T = int(input())
for _ in range(T):
n, amt, k = map(int, input().split())
l = list(map(int, input().split()))
l = sorted(l)
dp = [0] * (n + 1)
ans = 0
for i in range(n):
if i - k >= -1:
dp[i] = dp[i - k] + l[i]
else:
dp[i] = l[i] + dp[i - 1]
for i in range(n):
if dp[i] <= amt:
ans = i + 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
t = int(input())
for o in range(t):
n, p, k = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
a.sort()
maxBundles = 1
c = p
ans = 0
while 1:
if maxBundles * k - 1 < len(a):
c -= a[maxBundles * k - 1]
else:
maxBundles -= 1
break
if c < 0:
maxBundles -= 1
break
maxBundles += 1
initPrice = 0
for i in range(k):
c = p
curBundles = 1
initPrice += a[i]
c -= initPrice
if c < 0 and maxBundles == 0:
ans = i
break
while 1:
if curBundles * k + i < len(a):
c -= a[curBundles * k + i]
else:
curBundles -= 1
break
if c < 0:
curBundles -= 1
break
curBundles += 1
if curBundles < maxBundles:
ans = maxBundles * k + i
break
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
import sys
input = sys.stdin.readline
t = int(input())
for testcases in range(t):
n, p, k = map(int, input().split())
A = sorted(map(int, input().split()))
S = [0] * (n + k + 3)
for i in range(k - 1):
S[i] = A[i] + S[i - 1]
for i in range(k - 1, n):
S[i] = S[i - k] + A[i]
ANS = -1
for i in range(n):
if S[i] <= p:
ANS = i
print(ANS + 1)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
for _ in range(int(input())):
n, p, k = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort()
prefix_sums = [0]
for a_i in a[:k]:
prefix_sums.append(a_i + prefix_sums[-1])
max_goods = 0
for ind, start_sum in enumerate(prefix_sums):
balance = p - start_sum
local_goods = ind
flag = 0
while ind + k <= n and a[ind + k - 1] <= balance:
balance -= a[ind + k - 1]
ind += k
local_goods += k
flag = 1
if balance >= 0:
max_goods = max(max_goods, local_goods)
print(max_goods)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL 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 LIST NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
t = int(input())
for i in range(t):
n, p, k = map(int, input().split())
goods = list(map(int, input().split()))
goods.sort()
if goods[0] > p:
print(0)
continue
elif len(goods) == 1:
print(1)
continue
sums = [[0]]
for i in range(k - 1):
sums.append([sums[-1][0] + goods[i]])
for i in range(k, n + 1):
sums[i % k].append(sums[i % k][-1] + goods[i - 1])
for i in range(n - 1, -1, -1):
cost = sums[(i + 1) % k][(i + 1) // k]
if cost <= p:
print(i + 1)
break
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
T = int(input())
def solve():
[N, P, K] = list(map(int, input().split()))
A = list(map(int, input().split()))
A = sorted(A)
dp = [float("inf") for _ in range(N)]
ans = 0
for i in range(N):
if i < K - 1:
if i == 0:
dp[i] = A[i]
continue
dp[i] = A[i] + dp[i - 1]
continue
if i == K - 1:
dp[i] = A[i]
continue
dp[i] = A[i] + dp[i - K]
for ii in range(len(dp)):
if dp[ii] <= P:
ans = ii + 1
return ans
for _ in range(T):
print(solve())
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN LIST VAR 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 ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
import sys
input = sys.stdin.readline
def inp():
return int(input().rstrip())
def inlt():
return list(map(int, input().rstrip().split()))
def insr():
s = input().rstrip()
return s[: len(s) - 1]
def invr():
return map(int, input().rstrip().split())
t = inp()
for _ in range(t):
n, p, k = invr()
nums = inlt()
nums.sort()
dp = [(-1) for i in range(n + 1)]
dp[0] = p
ans = 0
for i in range(1, n + 1):
if i >= k:
dp[i] = max(dp[i - k] - nums[i - 1], dp[i - 1] - nums[i - 1])
else:
dp[i] = dp[i - 1] - nums[i - 1]
if dp[i] >= 0:
ans = i
print(ans)
|
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
a = int(input())
for i in range(a):
b, m, k = map(int, input().split(" "))
c = list(map(int, input().split(" ")))
c.sort()
cost = [0] * (b + 1)
for l in range(1, k):
cost[l] = c[l - 1] + cost[l - 1]
for j in range(k, b + 1):
cost[j] = cost[j - k] + c[j - 1]
for n in range(b + 1):
if cost[n] <= m:
ans = n
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
This is the hard version of this problem. The only difference is the constraint on $k$ — the number of gifts in the offer. In this version: $2 \le k \le n$.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$k$ of goods for the price of one" is held in store.
Using this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by $a_i$ — the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$).
Please note that each good can be bought no more than once.
For example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
The next lines contain a description of $t$ test cases.
The first line of each test case contains three integers $n, p, k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le p \le 2\cdot10^9$, $2 \le k \le n$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains $n$ integers $a_i$ ($1 \le a_i \le 10^4$) — the prices of goods.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in a separate line print one integer $m$ — the maximum number of goods that Vasya can buy.
-----Example-----
Input
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
Output
3
4
1
1
2
0
4
5
|
t = int(input())
for j in range(t):
n, p, k = map(int, input().split())
l = input().split()
for j1 in range(len(l)):
l[j1] = int(l[j1])
l.sort()
ans = [(0) for i in range(len(l))]
ans[0] = l[0]
for i in range(1, k):
ans[i] = l[i] + ans[i - 1]
for i in range(k - 1, len(ans)):
ans[i] = l[i] + ans[i - k]
ans[k - 1] = l[k - 1]
f = 0
for i in range(len(ans) - 1, -1, -1):
if ans[i] <= p:
f = 1
print(i + 1)
break
if f == 0:
print(0)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
begin = [-1] * n
end = [-1] * n
hurt = [-1] * n
adj = [[] for i in range(n)]
for _ in range(n - 1):
u, v = map(int, input().split())
adj[u - 1].append(v - 1)
adj[v - 1].append(u - 1)
hurt[0] = 1
begin[0] = 0
stack = [0]
curr = 1
while stack:
nex = stack[-1]
if adj[nex]:
v = adj[nex].pop()
if begin[v] == -1:
begin[v] = curr
curr += 1
stack.append(v)
hurt[v] = len(stack)
else:
end[nex] = curr
stack.pop()
desc = [(end[i] - begin[i] - hurt[i]) for i in range(n)]
desc.sort(reverse=True)
out = 0
for i in range(n - k):
out += desc[i]
print(out)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
import sys
input = sys.stdin.buffer.readline
n, k = map(int, input().split())
edge = [[] for i in range(n)]
for i in range(n - 1):
u, v = map(int, input().split())
edge[u - 1].append(v - 1)
edge[v - 1].append(u - 1)
par = [-2] * n
par[0] = -1
tank = [0]
order = [0]
while tank:
p = tank.pop()
for e in edge[p]:
if par[e] == -2:
par[e] = p
tank.append(e)
order.append(e)
size = [1] * n
for i in range(n - 1, 0, -1):
size[par[order[i]]] += size[order[i]]
depth = [0] * n
for i in range(1, n):
depth[order[i]] = depth[par[order[i]]] + 1
score = []
for i in range(n):
score.append(size[i] - 1 - depth[i])
score.sort(reverse=True)
res = 0
for i in range(n - k):
res += score[i]
print(res)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
import sys
sys.setrecursionlimit(10**6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.readline())
def MI():
return map(int, sys.stdin.readline().split())
def MI1():
return map(int1, sys.stdin.readline().split())
def LI():
return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def SI():
return sys.stdin.readline()[:-1]
def main():
def dfs():
first = [True] * n
stack = [(0, -1)]
while stack:
u, pu = stack.pop()
if first[u]:
first[u] = False
stack.append((u, pu))
for v in to[u]:
if v == pu:
continue
dep[v] = dep[u] + 1
stack.append((v, u))
else:
for v in to[u]:
if v == pu:
continue
chi[u] += chi[v] + 1
n, k = MI()
to = [[] for _ in range(n)]
for _ in range(n - 1):
u, v = MI1()
to[u].append(v)
to[v].append(u)
dep = [0] * n
chi = [0] * n
dfs()
vv = [(dep[u] - chi[u]) for u in range(n)]
vv.sort(reverse=True)
ans = sum(vv[:k])
print(ans)
main()
|
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
n, m = map(int, input().split())
a = [[] for i in range(n)]
for i in range(n - 1):
x, y = map(int, input().split())
a[x - 1].append(y - 1)
a[y - 1].append(x - 1)
ht = [(-1) for i in range(n)]
par = [(-1) for i in range(n)]
h = 0
stack = [0]
order = []
while stack:
node = stack.pop()
order.append(node)
if ht[node] == -1:
ht[node] = h
for i in a[node]:
if ht[i] == -1:
par[i] = node
stack.append(i)
else:
ht[node] = ht[i] + 1
score = [0] * n
for i in order[::-1]:
if par[i] != -1:
score[par[i]] += 1 + score[i]
for i in range(n):
score[i] = ht[i] - score[i]
score.sort(reverse=True)
print(sum(score[:m]))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
n, k = map(int, input().split())
g = [[] for _ in range(n)]
for _ in range(n - 1):
a, b = map(int, input().split())
g[a - 1].append(b - 1)
g[b - 1].append(a - 1)
s = [0]
d = [-1] * n
d[0] = 0
l = [-1] * n
while s:
p = s.pop()
if l[p] == -1:
s.append(p)
l[p] = -2
for node in g[p]:
if d[node] == -1:
d[node] = d[p] + 1
s.append(node)
else:
l[p] = 0
for node in g[p]:
if l[node] >= 0:
l[p] += l[node] + 1
li = [0] * n
for i in range(n):
li[i] = d[i] - l[i]
li.sort(reverse=True)
print(sum(li[:k]))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
n, k = map(int, input().split())
roads = {}
nodes = {}
for _ in range(n - 1):
u, v = map(int, input().split())
roads.setdefault(u, set()).add(v)
roads.setdefault(v, set()).add(u)
proc = set()
proc_stack = [1]
in_stack = []
chlds = {}
while len(proc_stack) > 0:
node = proc_stack[-1]
if node not in proc:
proc.add(node)
rds = roads[node] - proc
chlds[node] = rds
proc_stack.extend(rds)
in_stack.append(node)
else:
proc_stack.pop()
in_stack.pop()
nodes[node] = len(in_stack), sum(nodes[c][1] for c in chlds[node]) + len(
chlds[node]
)
print(sum(sorted((n[0] - n[1] for n in nodes.values()), reverse=True)[:k]))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR DICT WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
n, k = map(int, input().split())
u = []
for i in range(n):
u.append([])
cnt = [0] * n
for i in range(n - 1):
a, b = map(int, input().split())
u[a - 1].append(b - 1)
u[b - 1].append(a - 1)
cnt[a - 1] += 1
cnt[b - 1] += 1
d = [n] * n
d[0] = 0
q = [0]
q0 = 0
par = [-1] * n
while len(q) > q0:
v = q[q0]
q0 += 1
for i in u[v]:
if d[i] > d[v] + 1:
d[i] = d[v] + 1
par[i] = v
q.append(i)
g = [0] * n
q = []
q0 = 0
for i in range(n):
if len(u[i]) == 1:
g[i] = 0
q.append(i)
while len(q) > q0:
v = q[q0]
q0 += 1
if v == 0:
continue
i = par[v]
g[i] += g[v] + 1
cnt[i] -= 1
if cnt[i] == 1:
q.append(i)
ans = [0] * n
for i in range(n):
ans[i] = d[i] - g[i]
ans.sort(reverse=1)
print(sum(ans[:k]))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
import sys
n, k = map(int, sys.stdin.readline().split())
edge = [
list(map(lambda x: int(x) - 1, sys.stdin.readline().split())) for _ in range(n - 1)
]
nbhd = [[] for _ in range(n)]
for x in edge:
nbhd[x[0]].append(x[1])
nbhd[x[1]].append(x[0])
son = [[] for _ in range(n)]
lev = [None] * n
des = [0] * n
level = [None] * n
level[0] = 0
i = 0
cur = [0]
while None in level:
lev[i] = cur
temp = cur
cur = []
for j in temp:
for kk in nbhd[j]:
if level[kk] == None:
level[kk] = i + 1
cur.append(kk)
son[j].append(kk)
i += 1
lev[i] = cur
depth = max(level)
for i in range(depth - 1, -1, -1):
for j in lev[i]:
des[j] = sum([des[l] for l in son[j]]) + len(son[j])
a = [(-level[i] + des[i]) for i in range(n)]
a.sort()
S = 0
for i in range(k):
S -= a[i]
print(S)
|
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER WHILE NONE VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
from sys import *
input = stdin.readline
n, k = map(int, input().split())
graph = [set([]) for _ in range(n + 1)]
for _ in range(n - 1):
x, y = map(int, input().split())
graph[x].add(y)
graph[y].add(x)
dep, par, cn = [0] * (n + 1), [0] * (n + 1), 0
q, p = [1], []
vis = [0] * (n + 1)
vis[1] = 1
while q:
x = q.pop()
dep[x] = cn
for i in graph[x]:
if vis[i] == 0:
vis[i] = 1
p.append(i)
par[i] = x
if len(q) == 0:
cn += 1
q = list(p)
p = []
tp = []
for i in range(1, n + 1):
tp.append((i, dep[i]))
tp.sort(key=lambda x: x[1])
tp = tp[::-1]
sub = [0] * (n + 1)
for i in range(len(tp) - 1):
ind = tp[i][0]
pid = par[ind]
sub[pid] += 1 + sub[ind]
a = []
for i in range(1, n + 1):
a.append(dep[i] - sub[i])
a.sort(reverse=True)
sn = 0
for i in range(k):
sn += a[i]
stdout.write(str(sn) + "\n")
|
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
import sys
ints = (int(x) for x in sys.stdin.read().split())
def dfs(u):
value[u] = is_leave[u]
for v in G[u]:
if value[v] == None:
dfs(v)
value[u] += value[v]
return
n, k = (next(ints) for i in range(2))
G = [[] for i in range(n)]
for e in range(n - 1):
i, j = (next(ints) - 1 for _ in range(2))
G[i].append(j)
G[j].append(i)
is_leave = [int(len(G[i]) == 1 and i != 0) for i in range(n)]
pa = [None] * n
d = [0] * n
b = [0] * n
Q = [G[i].copy() for i in range(n)]
dfs = [0]
pa[0] = 0
while dfs:
u = dfs[-1]
if len(Q[u]):
v = Q[u].pop()
if pa[v] == None:
pa[v] = u
d[v] = d[u] + 1
dfs.append(v)
else:
b[u] = sum(1 + b[v] for v in G[u] if v != pa[u])
dfs.pop()
print(sum(sorted([(d[i] - b[i]) for i in range(n)])[-k:]))
|
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
t = 1
for i in range(t):
n, k = [int(j) for j in input().split()]
vicini = [[] for j in range(n)]
for l in range(n - 1):
a, b = [int(j) for j in input().split()]
vicini[a - 1].append(b - 1)
vicini[b - 1].append(a - 1)
livello = [(0) for j in range(n)]
padre = [(0) for j in range(n)]
figlio = [[] for j in range(n)]
figlio[0] = vicini[0]
for l in figlio[0]:
livello[l] = 1
padre[l] = 0
lvl = [[0]]
liv = 1
tbp = figlio[0]
while len(tbp) > 0:
lvl.append(tbp)
ntbp = []
for l in tbp:
livello[l] = liv
figlio[l].extend([m for m in vicini[l] if m != padre[l]])
ntbp.extend(figlio[l])
for m in figlio[l]:
padre[m] = l
liv = liv + 1
tbp = ntbp
nfigli = [(0) for l in range(n)]
value = [(0) for l in range(n)]
for l in lvl[-1]:
value[l] = livello[l]
lvl = lvl[:-1]
lvl.reverse()
for l in lvl:
for m in l:
for o in figlio[m]:
nfigli[m] = nfigli[m] + nfigli[o] + 1
value[m] = livello[m] - nfigli[m]
value.sort(reverse=True)
print(sum(value[:k]))
|
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR 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 NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
n, k = map(int, input().split(" "))
k = n - k
arr = [[] for i in range(n)]
for i in range(n - 1):
a, b = map(int, input().split(" "))
arr[a - 1].append(b - 1)
arr[b - 1].append(a - 1)
visited = [False] * n
stack = [0]
height = [-1] * n
height[0] = 0
while stack:
temp = stack.pop()
for i in arr[temp]:
if height[i] == -1:
height[i] = height[temp] + 1
stack.append(i)
nochild = [0] * n
heightsort = [0] * n
for i in range(n):
heightsort[i] = [height[i], i]
heightsort.sort(reverse=True)
for node in heightsort:
count = 0
curr = node[1]
for i in arr[curr]:
if height[i] == height[curr] + 1:
count += nochild[i] + 1
nochild[curr] = count
nochildsort = []
for i in range(n):
nochildsort.append(nochild[i] - height[i])
nochildsort.sort(reverse=True)
finalans = 0
for i in range(k):
finalans += nochildsort[i]
print(finalans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING 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 BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR 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 VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
from sys import setrecursionlimit, stdin, stdout
setrecursionlimit(10**5 + 5)
def dfs(graph, root, par, level, levelArr):
F = 0
c = 0
for i in graph[root]:
if i == par:
continue
F += 1
c += dfs(graph, i, root, level + 1, levelArr)
levelArr.append(level - c)
return c + 1
def bsf(graph, N):
depth, visited, child = [0] * N, [0] * N, [0] * N
visited[0] = 1
st = [0]
while st:
k = st.pop()
for i in graph[k]:
if visited[i]:
continue
else:
visited[i] = 1
st.append(i)
depth[i] = depth[k] + 1
dsort = sorted([(depth[i], i) for i in range(N)], reverse=True)
for i in dsort:
r = 1
for j in graph[i[1]]:
r += child[j]
child[i[1]] = r
res = sorted([(depth[i] - child[i] + 1) for i in range(N)], reverse=True)
return res
def find(graph, K, N):
levelArr = []
levelArr = bsf(graph, N)
return sum(levelArr[:K])
class main:
N, K = list(map(int, stdin.readline().split()))
graph = [[] for _ in range(N)]
for i in range(N - 1):
a, b = list(map(int, stdin.readline().split()))
graph[a - 1].append(b - 1)
graph[b - 1].append(a - 1)
stdout.write(str(find(graph, K, N)) + "\n")
main()
|
EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR CLASS_DEF ASSIGN VAR 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 BIN_OP VAR NUMBER 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 FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
import sys
lines = sys.stdin.readlines()
n, k = map(int, lines[0].strip().split(" "))
edges = {}
for i in range(1, n):
a, b = map(int, lines[i].strip().split(" "))
if a not in edges:
edges[a] = []
if b not in edges:
edges[b] = []
edges[a].append(b)
edges[b].append(a)
seen = set([1])
stack = [1]
level = {(1): 0}
parent = {(1): 0}
while stack:
node = stack.pop()
for adj in edges[node]:
if adj not in seen:
seen.add(adj)
level[adj] = level[node] + 1
stack.append(adj)
parent[adj] = node
noChild = {}
stack = [1]
while stack:
node = stack.pop()
all = True
for adj in edges[node]:
if adj != parent[node] and adj not in noChild:
all = False
break
if all:
tmp = 0
for adj in edges[node]:
if adj == parent[node]:
continue
tmp += noChild[adj] + 1
noChild[node] = tmp
else:
stack.append(node)
for adj in edges[node]:
if adj == parent[node]:
continue
if adj not in noChild:
stack.append(adj)
res = [(level[i] - noChild[i]) for i in range(1, n + 1)]
res.sort(reverse=True)
print(sum(res[:k]))
|
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING IF VAR VAR ASSIGN VAR VAR LIST IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
d = [set() for _ in range(N + 1)]
for i in range(N - 1):
a, b = map(int, input().split())
d[a].add(b)
d[b].add(a)
d2 = [set() for _ in range(N + 1)]
stack = [1]
vs = set([1])
vs_dfs = list()
depth = [0] * (N + 1)
while stack:
v = stack.pop()
vs_dfs.append(v)
for u in d[v]:
if u in vs:
continue
d2[v].add(u)
depth[u] = depth[v] + 1
vs.add(u)
stack.append(u)
tsize = [1] * (N + 1)
for v in vs_dfs[::-1]:
tmp = 1
for u in d2[v]:
tmp += tsize[u]
tsize[v] = tmp
depth[v] -= tmp - 1
print(sum(sorted(depth[1:])[::-1][:K]))
|
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
import sys
LI = lambda: list(map(int, sys.stdin.readline().strip("\n").split()))
MI = lambda: map(int, sys.stdin.readline().strip("\n").split())
SI = lambda: sys.stdin.readline().strip("\n")
II = lambda: int(sys.stdin.readline().strip("\n"))
n, k = MI()
g = {i: [] for i in range(n + 1)}
for _ in range(n - 1):
u, v = MI()
g[u].append(v)
g[v].append(u)
dist = [(1) for i in range(n + 1)]
parent = [(0) for i in range(n + 1)]
lvl = {i: (0) for i in range(1, n + 1)}
vis = [(False) for i in range(n + 1)]
q, i = [1], 0
vis[1] = True
while i < len(q):
u = q[i]
for v in g[u]:
if not vis[v]:
vis[v] = True
dist[v] = dist[u] + 1
parent[v] = u
lvl[v] = dist[v]
q.append(v)
i += 1
sub = [(1) for i in range(n + 1)]
for v in sorted(lvl, key=lambda x: lvl[x], reverse=True):
sub[parent[v]] += sub[v]
ans = {i: (0) for i in range(1, n + 1)}
for v in range(1, n + 1):
ans[v] = sub[v] - dist[v]
print(sum(sorted(ans.values(), reverse=True)[: n - k]))
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
n, k = map(int, input().split())
G = [[] for _ in range(n)]
for _ in range(n - 1):
u, v = map(lambda x: int(x) - 1, input().split())
G[u].append(v)
G[v].append(u)
order = []
par = [-1] * n
stack = [0]
while stack:
v = stack.pop()
order.append(v)
for c in G[v]:
if c != par[v]:
par[c] = v
stack.append(c)
par_count = [0] * n
chi_count = [0] * n
for v in order:
p = par[v]
if p != -1:
par_count[v] = par_count[p] + 1
for v in order[::-1]:
p = par[v]
if p != -1:
chi_count[p] += chi_count[v] + 1
score = [(par_count[i] - chi_count[i]) for i in range(n)]
score.sort(reverse=True)
print(sum(score[:k]))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER 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 ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
from sys import stdin
input = stdin.readline
n, k = [int(x) for x in input().split()]
a = [[int(x) for x in input().split()] for _ in range(n - 1)]
b = [[] for x in range(n)]
for u, v in a:
b[u - 1].append(v - 1)
b[v - 1].append(u - 1)
marked = [False] * n
depth = [0] * n
childs = [0] * n
stack = []
stack.append((0, 0))
while stack:
z, f = stack[-1]
if marked[z]:
stack.pop()
if z != 0:
childs[f] += childs[z] + 1
continue
marked[z] = True
neighbors = b[z]
for i in neighbors:
if not marked[i]:
depth[i] = depth[z] + 1
stack.append((i, z))
ans = sum(sorted([(depth[i] - childs[i]) for i in range(n)])[-k:])
print(ans)
|
ASSIGN VAR VAR 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 VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR 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 BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
n, k = list(map(int, input().split()))
citydis = {}
for i in range(n - 1):
cityf, cityt = list(map(int, input().split()))
desf = citydis.get(cityf, [])
dest = citydis.get(cityt, [])
desf.append(cityt)
dest.append(cityf)
citydis[cityf] = desf
citydis[cityt] = dest
initdis = {(1): 0}
dis = 1
nodes = [1]
snode = []
while True:
replacenode = []
for node in nodes:
temp = citydis[node]
for tempnode in temp:
wentorno = initdis.get(tempnode, False)
if not wentorno and tempnode != 1:
initdis[tempnode] = dis
replacenode.append(tempnode)
cityto = citydis[tempnode]
if len(cityto) == 1:
snode.append(tempnode)
dis += 1
if not replacenode:
break
nodes = replacenode
ansnode = {}
initdisitem = list(initdis.items())
initdisitem.sort(key=lambda x: -x[1])
for nodeinamo in initdisitem:
node = nodeinamo[0]
temp = citydis[node]
bitchnode = citydis[node]
bitches = 0
for itsnode in bitchnode:
if initdis[itsnode] > initdis[node]:
bitches += ansnode[itsnode][1] + 1
ansnode[node] = [initdis[node], bitches]
anss = list(ansnode.items())
anss.sort(key=lambda x: -x[1][0] + x[1][1])
ans = 0
for i in range(k):
ans += anss[i][1][0] - anss[i][1][1]
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR LIST ASSIGN VAR FUNC_CALL VAR VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST WHILE NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
E = [[] for _ in range(n + 1)]
for _ in range(n - 1):
a, b = map(int, input().split())
E[a].append(b)
E[b].append(a)
D = [0] * (n + 1)
P = [0] * (n + 1)
C = [0] * (n + 1)
def dfs(n, p, d):
D[n] = d
P[n] = p
for u in E[n]:
if u == p:
continue
dfs(u, n, d + 1)
C[n] += len(E[n]) - 1
C[p] += C[n]
def dfs2(v, d, p):
global n
Q = [(1, 0, 0)]
V = [False] * (n + 1)
while Q:
v, d, p = Q[-1]
if not V[v]:
V[v] = True
P[v] = p
D[v] = d
for n in E[v]:
if n == p:
continue
Q.append((n, d + 1, v))
else:
C[v] += len(E[v]) - 1
C[p] += C[v]
Q.pop()
dfs2(1, 0, 0)
print(sum(sorted([(d - c) for d, c in zip(D, C)], reverse=True)[:k]))
|
IMPORT ASSIGN VAR VAR ASSIGN VAR 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 BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
from sys import stdin
inp = stdin.readline
n, industrial = [int(x) for x in inp().strip().split()]
adj = [[] for _ in range(n)]
for _ in range(n - 1):
v, u = [int(x) for x in inp().strip().split()]
adj[v - 1].append(u - 1)
adj[u - 1].append(v - 1)
bfs = [0]
parent = [(-1) for _ in range(n)]
for node in bfs:
for neighbor in adj[node]:
del adj[neighbor][adj[neighbor].index(node)]
parent[neighbor] = node
bfs.append(neighbor)
subTree = [0] * n
for node in bfs[n:0:-1]:
subTree[parent[node]] += subTree[node] + 1
depthList = [0] * n
for node in bfs[1:]:
depthList[node] = depthList[parent[node]] + 1
ans = [-1] * n
for i in range(n):
ans[i] = depthList[i] - subTree[i]
ans.sort(reverse=True)
print(sum(ans[:industrial]))
|
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL 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 LIST NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
from sys import stdin
input = stdin.readline
def dfs():
size = [0] * n
parent = [0] * n
depth = [0] * n
marked = [False] * n
stack = [0]
while stack:
node = stack[-1]
if marked[node]:
stack.pop()
if node != 0:
size[parent[node]] += size[node] + 1
continue
marked[node] = True
for neighbor in tree[node]:
if neighbor == parent[node]:
continue
parent[neighbor] = node
depth[neighbor] = depth[node] + 1
stack.append(neighbor)
return depth, size
n, k = [int(x) for x in input().split()]
tree = [[] for _ in range(n)]
for i in range(n - 1):
u, v = [int(x) for x in input().split()]
tree[u - 1].append(v - 1)
tree[v - 1].append(u - 1)
depth, size = dfs()
ans = sum(sorted([(depth[i] - size[i]) for i in range(n)])[-k:])
print(ans)
|
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR 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 VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
n, k = map(int, input().split())
gs = [[] for _ in range(n + 1)]
for _ in range(n - 1):
u, v = map(int, input().split())
gs[u].append(v)
gs[v].append(u)
q = [1]
ccnt = [0] * (n + 1)
ds = [None] * (n + 1)
ds[1] = 0
par = [0] * (n + 1)
par[1] = 0
while q:
u = q.pop()
if u < 0:
ccnt[par[-u]] += ccnt[-u] + 1
continue
q.append(-u)
for v in gs[u]:
if ds[v] is not None:
continue
ds[v] = ds[u] + 1
par[v] = u
q.append(v)
srr = (ds[x] - ccnt[x] for x in range(1, n + 1))
sr = sorted(srr, reverse=True)
ans = sum(sr[:k])
print(ans)
|
ASSIGN VAR 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 BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
n, k = [int(x) for x in input().split(" ")]
edges = []
for i in range(n - 1):
edges.append(list(map(int, input().split(" "))))
for i in range(n - 1):
edges[i][0] -= 1
edges[i][1] -= 1
neighbours = []
for i in range(n):
neighbours.append(list())
for x in edges:
neighbours[x[0]].append(x[1])
neighbours[x[1]].append(x[0])
parent = [-1] * n
depth = [0] * n
layers = []
layer = 0
layer_under_consideration = [0]
parent[0] = -1
while len(layer_under_consideration) != 0:
layers.append(layer_under_consideration)
for x in layer_under_consideration:
depth[x] = layer
layer += 1
daughter_layer = []
for node in layer_under_consideration:
daughters = []
for neighbour in neighbours[node]:
if neighbour != parent[node]:
daughters.append(neighbour)
for daughter in daughters:
parent[daughter] = node
daughter_layer.extend(daughters)
layer_under_consideration = list(daughter_layer)
nodes_beneath = [1] * n
leaves = []
for i in range(n):
if len(neighbours[i]) == 1:
leaves.append(i)
layers.reverse()
for layer in layers:
if layer == [0]:
break
for node in layer:
nodes_beneath[parent[node]] += nodes_beneath[node]
score = [0] * n
for i in range(n):
score[i] = depth[i] - nodes_beneath[i] + 1
score.sort()
score.reverse()
print(sum(score[0:k]))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR LIST NUMBER FOR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
n, k = map(int, input().split())
g = [[] for i in range(n)]
for _ in range(n - 1):
x, y = map(int, input().split())
g[x - 1].append(y - 1)
g[y - 1].append(x - 1)
d = [-1] * n
d[0] = 0
q = [[-1, 0, 0]]
while q:
par, ver, cnt = q.pop()
for to in g[ver]:
if d[to] == -1:
d[to] = cnt + 1
q.append([ver, to, cnt + 1])
subtree_size = [0] * n
stack = [[-1, 0, 0]]
while stack:
par, ver, state = stack.pop()
if state == 0:
stack.append([par, ver, 1])
for to in g[ver]:
if to != par:
stack.append([ver, to, 0])
elif len(g[ver]) == 1:
subtree_size[ver] = 1
else:
cnt = 0
for to in g[ver]:
if to != par:
cnt += subtree_size[to]
subtree_size[ver] = cnt + 1
arr = [(d[i] - subtree_size[i] + 1) for i in range(1, n)]
arr.sort(reverse=True)
print(sum(arr[:k]))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
g = [[] for _ in range(n)]
for i in range(n - 1):
u, v = map(lambda x: int(x) - 1, input().split())
g[u].append(v)
g[v].append(u)
lvl = {}
stack = [(0, 0)]
children = [[] for _ in range(n)]
while stack:
node, l = stack.pop()
lvl[node] = l
for ne in g[node]:
if ne not in lvl:
stack.append((ne, l + 1))
children[node].append(ne)
subtree = [0] * n
for i in sorted(range(n), key=lambda i: -lvl[i]):
subtree[i] += 1
for ch in children[i]:
subtree[i] += subtree[ch]
print(sum(sorted([(lvl[i] - subtree[i] + 1) for i in range(n)], reverse=True)[:k]))
|
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER 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 ASSIGN VAR DICT ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
import sys
readline = sys.stdin.readline
class Segtree:
def __init__(self, A, intv, initialize=True):
self.N = len(A)
self.N0 = 2 ** (self.N - 1).bit_length()
self.intv = intv
if initialize:
self.data = [intv] * self.N0 + A + [intv] * (self.N0 - self.N)
for i in range(self.N0 - 1, 0, -1):
self.data[i] = self.segf(self.data[2 * i], self.data[2 * i + 1])
else:
self.data = [intv] * (2 * self.N0)
def segf(self, x, y):
if table[x] > table[y]:
return x
else:
return y
def update(self, k):
k += self.N0
while k > 0:
k = k >> 1
self.data[k] = self.segf(self.data[2 * k], self.data[2 * k + 1])
def query(self, l, r):
L, R = l + self.N0, r + self.N0
s = self.intv
while L < R:
if R & 1:
R -= 1
s = self.segf(s, self.data[R])
if L & 1:
s = self.segf(s, self.data[L])
L += 1
L >>= 1
R >>= 1
return s
def binsearch(self, l, r, check, reverse=False):
L, R = l + self.N0, r + self.N0
SL, SR = [], []
while L < R:
if R & 1:
R -= 1
SR.append(R)
if L & 1:
SL.append(L)
L += 1
L >>= 1
R >>= 1
if reverse:
for idx in SR + SL[::-1]:
if check(self.data[idx]):
break
else:
return -1
while idx < self.N0:
if check(self.data[2 * idx + 1]):
idx = 2 * idx + 1
else:
idx = 2 * idx
return idx - self.N0
else:
for idx in SL + SR[::-1]:
if check(self.data[idx]):
break
else:
return -1
while idx < self.N0:
if check(self.data[2 * idx]):
idx = 2 * idx
else:
idx = 2 * idx + 1
return idx - self.N0
def parorder(Edge, p):
N = len(Edge)
par = [0] * N
par[p] = -1
stack = [p]
order = []
visited = set([p])
ast = stack.append
apo = order.append
while stack:
vn = stack.pop()
apo(vn)
for vf in Edge[vn]:
if vf in visited:
continue
visited.add(vf)
par[vf] = vn
ast(vf)
return par, order
def getcld(p):
res = [[] for _ in range(len(p))]
for i, v in enumerate(p[1:], 1):
res[v].append(i)
return res
INF = 10**9 + 7
N, K = map(int, readline().split())
Edge = [[] for _ in range(N)]
Dim = [0] * N
for _ in range(N - 1):
a, b = map(int, readline().split())
a -= 1
b -= 1
Edge[a].append(b)
Edge[b].append(a)
Dim[a] += 1
Dim[b] += 1
Dim[0] += 1
P, L = parorder(Edge, 0)
dist = [0] * N
for l in L[1:]:
p = P[l]
dist[l] = 1 + dist[p]
table = [-INF] * (N + 1)
for i in range(1, N):
if len(Edge[i]) == 1:
table[i] = dist[i]
debt = [0] * N
T = Segtree(list(range(N)), N, initialize=True)
ans = 0
N0 = T.N0
for _ in range(K):
idx = T.query(0, N0)
ans += table[idx]
table[idx] = -INF
T.update(idx)
if idx == 0:
break
p = P[idx]
Dim[p] -= 1
debt[p] += -1 + debt[idx]
if Dim[p] == 1:
table[p] = dist[p] + debt[p]
T.update(p)
print(ans)
|
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST VAR VAR VAR BIN_OP LIST VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR FUNC_DEF IF VAR VAR VAR VAR RETURN VAR RETURN VAR FUNC_DEF VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR LIST LIST WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FOR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR RETURN BIN_OP VAR VAR FOR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR 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 BIN_OP VAR NUMBER 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 VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
i = lambda: map(int, input().split())
n, k = i()
e = [[] for i in range(n)]
for t in range(n - 1):
a, b = i()
a -= 1
b -= 1
e[b].append(a)
e[a].append(b)
d = [0] * n
c = [0] * n
p = [0] * n
s = [0]
while s:
a = s.pop()
for b in e[a]:
if b != p[a]:
s.append(b)
p[b] = a
d[b] = d[a] + 1
m = [(len(q) - 1) for q in e]
s = [a for a, q in enumerate(m) if a and not q]
while s:
b = s.pop()
a = p[b]
c[a] += c[b] + 1
c[b] -= d[b]
m[a] -= 1
if a and not m[a]:
s.append(a)
c.sort()
print(sum(c[k - n :]))
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
def createGraph(arr, n):
graph = []
for _ in range(n):
graph.append([])
for x in arr:
x = x.split(" ")
u = int(x[0]) - 1
v = int(x[1]) - 1
graph[u].append(v)
graph[v].append(u)
return graph
def createTree(graph, n):
tree = []
p = [0] * n
p[0] = None
tree.append([0])
cf = 0
while len(tree[cf]) > 0:
cf += 1
tree.append([])
for x in tree[cf - 1]:
for c in graph[x]:
if c != p[x]:
p[c] = x
tree[cf].append(c)
tree.pop()
return tree, p
def solution(tree, p, k, n):
child = findChild(p, tree, n)
val = [0] * n
for f in range(len(tree)):
for x in tree[f]:
val[x] = f - child[x]
val.sort(reverse=True)
point = 0
for i in range(k):
point += val[i]
return point
def findChild(p, tree, n):
child = [0] * n
floor = len(tree) - 1
while floor > 0:
for x in tree[floor]:
child[p[x]] += 1 + child[x]
floor -= 1
return child
def get_parent(p, x, node):
r = node
for _ in range(x):
r = p[r]
return r
n_k = input().split(" ")
n = int(n_k[0])
k = int(n_k[1])
arr = []
for _ in range(n - 1):
arr.append(input())
graph = createGraph(arr, n)
tree_p = createTree(graph, n)
tree = tree_p[0]
p = tree_p[1]
point = solution(tree, p, k, n)
print(point)
|
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING 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 RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NONE EXPR FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
graph = [[] for _ in range(N)]
for _ in range(N - 1):
a, b = map(int, input().split())
graph[a - 1].append(b - 1)
graph[b - 1].append(a - 1)
Par = [-1] * N
que = []
childs = [0] * N
stack = [0]
Ind = [0] * N
D = [0] * N
while stack:
p = stack[-1]
if Ind[p] == len(graph[p]):
stack.pop()
if stack:
par = stack[-1]
Par[p] = par
childs[par] += childs[p] + 1
elif len(stack) > 1 and stack[-2] == graph[p][Ind[p]]:
Ind[p] += 1
else:
np = graph[p][Ind[p]]
D[np] = D[p] + 1
Ind[p] += 1
stack.append(np)
B = []
for n in range(N):
B.append(D[n] - childs[n])
B.sort(reverse=True)
print(sum(B[:K]))
|
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
import sys
input = sys.stdin.buffer.readline
n, k = map(int, input().split())
neig = [0] * n
lev = [0] * n
for i in range(n):
neig[i] = [0]
lev[i] = [-1]
lev[0][0] = 0
for i in range(n - 1):
a, b = map(int, input().split())
a -= 1
b -= 1
neig[a][0] += 1
neig[b][0] += 1
neig[a].append(b)
neig[b].append(a)
tod = [0]
while len(tod) > 0:
foo = tod.pop()
for i in range(1, neig[foo][0] + 1):
if lev[neig[foo][i]][0] == -1:
lev[foo].append(neig[foo][i])
lev[neig[foo][i]][0] = lev[foo][0] + 1
lev[neig[foo][i]].append(foo)
tod.append(neig[foo][i])
upn = [-1] * n
costs = [0] * n
leaves = []
for i in range(n):
upn[i] = neig[i][0] - 1
if upn[i] == 0 and i > 0:
leaves.append(i)
upn[0] = 0
while len(leaves) > 0:
f = leaves.pop()
costs[lev[f][1]] += costs[f]
costs[lev[f][1]] += 1
upn[lev[f][1]] -= 1
if upn[lev[f][1]] == 0 and lev[f][0] > 1:
leaves.append(lev[f][1])
gainz = [0] * n
for i in range(n):
gainz[i] = lev[i][0] - costs[i]
gainz.sort(reverse=True)
happiness = 0
for i in range(k):
happiness += gainz[i]
print(happiness)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR 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 NUMBER ASSIGN VAR VAR LIST NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
n, k = list(map(int, input().split()))
graph = [[] for i in range(n)]
weight = [None] * n
def dfs(graph):
global weight
stack = [0]
parents = [None] * n
children = [0] * n
curDep = -1
while len(stack):
s = stack[-1]
if weight[s] == None:
curDep += 1
weight[s] = curDep
for child in graph[s]:
if child != parents[s]:
parents[child] = s
stack.append(child)
else:
stack.pop()
curDep -= 1
if parents[s] != None:
children[parents[s]] += children[s] + 1
weight[s] -= children[s]
for i in range(n - 1):
u, v = list(map(int, input().split()))
u -= 1
v -= 1
graph[u].append(v)
graph[v].append(u)
dfs(graph)
weight.sort(reverse=True)
ans = sum(weight[:k])
print(ans)
|
ASSIGN VAR 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 NONE VAR FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NONE VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR NONE VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL 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 VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
n, k = map(int, input().split())
nodes = [[] for i in range(n)]
for _ in range(n - 1):
a, b = map(int, input().split())
nodes[a - 1].append(b - 1)
nodes[b - 1].append(a - 1)
parents = [(-2) for i in range(n)]
parents[0] = -1
order = [0]
que = [0]
depth = [(0) for i in range(n)]
my_que = [0]
while len(my_que) > 0:
index = my_que.pop()
for edge in nodes[index]:
if parents[edge] == -2:
my_que.append(edge)
parents[edge] = index
depth[edge] = depth[parents[edge]] + 1
order.append(edge)
size = [(1) for i in range(n)]
for z in range(n - 1, 0, -1):
bb = order[z]
size[parents[bb]] += size[bb]
score = [(size[x] - depth[x] - 1) for x in range(n)]
score.sort(reverse=True)
print(sum(score[: n - k]))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
def dfs():
while stack:
u, p, depth = stack.pop()
if not visited[u]:
visited[u] = True
stack.append((u, p, depth))
for v in adj[u]:
if v != p:
stack.append((v, u, depth + 1))
else:
for v in adj[u]:
if v != p:
count[u] += count[v] + 1
happiness[u] = depth - count[u]
n, k = list(map(int, input().split()))
adj = [[] for _ in range(n)]
visited = [False] * n
stack = [(0, -1, 0)]
for _ in range(n - 1):
u, v = list(map(lambda x: int(x) - 1, input().split()))
adj[u].append(v)
adj[v].append(u)
count = [0] * n
happiness = [0] * n
dfs()
happiness.sort(reverse=True)
ans = sum(happiness[:k])
print(ans)
|
FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR 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 ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL 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 ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
n, k = map(int, input().split())
g = [[] for _ in range(n)]
for _ in range(n - 1):
i, j = map(lambda i: int(i) - 1, input().split())
g[i].append(j)
g[j].append(i)
q = [0]
c = [0] * n
p = [None] + [0] * (n - 1)
d = [0] + [None] * (n - 1)
while True:
i = q.pop()
if i == -1:
break
j = -(i + 1)
if j > 0:
c[p[j]] += c[j] + 1
continue
q.append(j)
for j in g[i]:
if d[j] is not None:
continue
d[j] = d[i] + 1
p[j] = i
q.append(j)
sr = sorted([(d[i] - c[i]) for i in range(n)], reverse=True)
print(sum(sr[:k]))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER 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 ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NONE BIN_OP VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
n, k = map(int, input().split())
edge = [[] for i in range(n)]
for i in range(n - 1):
a, b = map(int, input().split())
a -= 1
b -= 1
edge[a].append(b)
edge[b].append(a)
order = []
par = [-1] * n
tank = [0]
dist = [-1] * n
dist[0] = 0
while tank:
pele = tank.pop()
order.append(pele)
for e in edge[pele]:
if e == par[pele]:
continue
tank.append(e)
par[e] = pele
dist[e] = dist[pele] + 1
size = [1] * n
for e in order[::-1]:
if e == 0:
break
size[par[e]] += size[e]
score = [(dist[i] - size[i] + 1) for i in range(n)]
score.sort(reverse=True)
ans = 0
for i in range(k):
ans += score[i]
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.
<image>
There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.
A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).
Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.
In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
Input
The first line contains two integers n and k (2≤ n≤ 2 ⋅ 10^5, 1≤ k< n) — the number of cities and industry cities respectively.
Each of the next n-1 lines contains two integers u and v (1≤ u,v≤ n), denoting there is a road connecting city u and city v.
It is guaranteed that from any city, you can reach any other city by the roads.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note
<image>
In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.
<image>
In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
|
def main():
for _ in range(1):
n, k = [int(x) for x in input().split()]
P = [[] for i in range(n + 1)]
T = [0] * (n + 1)
D = [0] * (n + 1)
for _ in range(n - 1):
a, b = [int(x) for x in input().split()]
P[a].append(b)
P[b].append(a)
S = [(1, 1, 0)]
i = 0
while S:
p, t, d = S.pop()
if not T[p]:
T[p] = 1
S.append((p, t, i))
for nxt in P[p]:
if not T[nxt]:
S.append((nxt, t + 1, 0))
else:
i += 1
D[p] = t - (i - d)
D = sorted(D[1:], reverse=True)
print(sum(D[:k]))
main()
|
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR 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 BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER 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 LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
n = int(input())
a = input().split(" ")
a = [int(a[i]) for i in range(n)]
def calcHoleStranges(holes, m, pen1=True):
if m < 0:
return n
result = 0
if pen1:
holes_pen1 = [h for h in holes if h[1] == 1]
if len(holes_pen1) == 1:
hole = holes_pen1[0]
result = min(
1 + calcHoleStranges(holes, m, pen1=False),
calcHoleStranges(holes, m - hole[0], pen1=False),
)
return result
if len(holes_pen1) == 2:
hole1 = holes_pen1[0]
hole2 = holes_pen1[1]
result = min(
2 + calcHoleStranges(holes, m, pen1=False),
1 + calcHoleStranges(holes, m - min(hole1[0], hole2[0]), pen1=False),
calcHoleStranges(holes, m - hole1[0] - hole2[0], pen1=False),
)
return result
return calcHoleStranges(holes, m, pen1=False)
else:
holes = [h for h in holes if h[1] == 2]
holes.sort()
for h in holes:
m -= h[0]
if m < 0:
result += 2
return result
def getStranges(a):
if n % 2 == 0:
odds = n / 2
evens = n / 2
else:
odds = (n + 1) / 2
evens = (n - 1) / 2
for x in a:
if x == 0:
continue
if x % 2 == 0:
evens -= 1
else:
odds -= 1
if odds + evens == n:
if n == 1:
return 0
return 1
odd_holes = list()
even_holes = list()
stranges = 0
length = 0
left_bound = None
for i, x in enumerate(a):
if x != 0:
if left_bound is not None:
pen = 2
if left_bound % 2 != x % 2:
stranges += 1
left_bound = x
length = 0
continue
else:
pen = 1
if length > 0:
if x % 2 == 0:
even_holes.append((length, pen))
else:
odd_holes.append((length, pen))
length = 0
left_bound = x
else:
length += 1
if i == len(a) - 1:
if left_bound % 2 == 0:
even_holes.append((length, 1))
else:
odd_holes.append((length, 1))
need_even = sum(e[0] for e in even_holes)
need_odd = sum(o[0] for o in odd_holes)
if need_even > evens:
stranges += calcHoleStranges(even_holes, evens)
elif need_odd > odds:
stranges += calcHoleStranges(odd_holes, odds)
return stranges
def bruteForse(a):
result = n
no_numbers = set(range(1, n + 1)) - set(a)
if len(no_numbers) == 0:
result = 0
for i in range(n - 1):
if a[i] % 2 != a[i + 1] % 2:
result += 1
return result
a = a.copy()
for i in range(n):
if a[i] == 0:
for x in no_numbers:
a[i] = x
result = min(result, bruteForse(a))
break
return result
def getAllTasks(a):
result = []
no_numbers = set(range(n + 1)) - (set(a) - {0})
a = a.copy()
for i in range(n):
if a[i] == -1:
for x in no_numbers:
a[i] = x
result.extend(getAllTasks(a))
break
else:
result.append(a)
return result
def test(k):
listA = list()
a = [(-1) for _ in range(k)]
listA = getAllTasks(a)
for a in listA:
if getStranges(a) != bruteForse(a):
print(a, getStranges(a), bruteForse(a))
print(getStranges(a))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NONE ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
import sys
input = sys.stdin.readline
n = int(input())
arr = list(map(int, input().split()))
odd = 0
even = 0
if n % 2:
odd = n // 2 + 1
even = n // 2
else:
odd = n // 2
even = n // 2
jugad = [
[[[(100) for i in range(2)] for j in range(50 + 2)] for k in range(50 + 2)]
for l in range(100 + 2)
]
jugad[0][0][0][0] = 0
jugad[0][0][0][1] = 0
for i in range(1, n + 1):
for j in range(odd + 1):
for k in range(even + 1):
if arr[i - 1] == 0:
if k > 0:
jugad[i][j][k][0] = min(
jugad[i - 1][j][k - 1][0], jugad[i - 1][j][k - 1][1] + 1
)
if j > 0:
jugad[i][j][k][1] = min(
jugad[i - 1][j - 1][k][0] + 1, jugad[i - 1][j - 1][k][1]
)
elif arr[i - 1] % 2:
if j > 0:
jugad[i][j][k][1] = min(
jugad[i - 1][j - 1][k][0] + 1, jugad[i - 1][j - 1][k][1]
)
elif k > 0:
jugad[i][j][k][0] = min(
jugad[i - 1][j][k - 1][0], jugad[i - 1][j][k - 1][1] + 1
)
print(min(jugad[n][odd][even][0], jugad[n][odd][even][1]))
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
def main():
n = int(input())
p = list(map(int, input().split()))
s = set(p)
cnt = [0, 0]
for i in range(1, n + 1):
if not i in s:
cnt[i % 2] += 1
m = {}
def eq(a, b):
if a == b:
return 0
else:
return 1
def solve(idx, even, odd, prev):
if idx == n:
return 0
ans = 0
key = idx, even, odd, prev
if key in m:
return m[key]
if p[idx] == 0:
ev = 1000000000.0
od = 1000000000.0
if even > 0:
ev = eq(prev, 0)
ev += solve(idx + 1, even - 1, odd, 0)
if odd > 0:
od = eq(prev, 1)
od += solve(idx + 1, even, odd - 1, 1)
ans = min(ev, od)
else:
ans = eq(prev, p[idx] % 2)
ans += solve(idx + 1, even, odd, p[idx] % 2)
m[key] = ans
return ans
even, odd = cnt
print(min(solve(0, even, odd, 0), solve(0, even, odd, 1)))
return
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR RETURN VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
n = int(input())
a = list(map(int, input().split()))
s = set(a)
rem_parity = [0, 0]
for i in range(1, n + 1):
if i not in s:
rem_parity[i % 2] += 1
inf = 10**9
dp = [[([inf] * 2) for _ in range(n)] for _ in range(n)]
rem = rem_parity[0] + rem_parity[1]
if a[0] != 0:
dp[0][rem_parity[0]][a[0] % 2] = 0
else:
if rem_parity[0] > 0:
dp[0][rem_parity[0] - 1][0] = 0
if rem_parity[1] > 0:
dp[0][rem_parity[0]][1] = 0
rem -= 1
for i, x in enumerate(a[1:]):
if x > 0:
par = x % 2
for j in range(n):
for k in range(2):
dp[i + 1][j][par] = min(dp[i + 1][j][par], dp[i][j][k] + (k + par) % 2)
else:
rem -= 1
for j in range(n):
for k in range(2):
if j > 0:
dp[i + 1][j - 1][0] = min(
dp[i + 1][j - 1][0], dp[i][j][k] + (0 if k == 0 else 1)
)
if j <= rem:
dp[i + 1][j][1] = min(
dp[i + 1][j][1], dp[i][j][k] + (0 if k == 1 else 1)
)
print(min(dp[-1][0]))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
import sys
def input():
return sys.stdin.readline().strip()
def list2d(a, b, c):
return [([c] * b) for i in range(a)]
def list3d(a, b, c, d):
return [[([d] * c) for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e):
return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1):
return int(-(-x // y))
def INT():
return int(input())
def MAP():
return map(int, input().split())
def LIST(N=None):
return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes():
print("Yes")
def No():
print("No")
def YES():
print("YES")
def NO():
print("NO")
sys.setrecursionlimit(10**9)
INF = 10**18
MOD = 10**9 + 7
N = INT()
A = LIST()
A = [(a % 2 if a != 0 else -1) for a in A]
odd = ceil(N, 2)
even = N // 2
dp = list4d(N + 1, odd + 1, even + 1, 2, INF)
dp[0][0][0][0] = dp[0][0][0][1] = 0
for i in range(N):
a = A[i]
for j in range(odd + 1):
for k in range(even + 1):
for l in range(2):
if (a == 1 or a == -1) and j + 1 <= odd:
dp[i + 1][j + 1][k][1] = min(
dp[i + 1][j + 1][k][1], dp[i][j][k][l] + (1 ^ l)
)
if (a == 0 or a == -1) and k + 1 <= even:
dp[i + 1][j][k + 1][0] = min(
dp[i + 1][j][k + 1][0], dp[i][j][k][l] + (0 ^ l)
)
ans = min(dp[N][odd][even][0], dp[N][odd][even][1])
print(ans)
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
def calculate(ar, num):
s = 0
for x in range(len(ar)):
if num >= ar[x][0]:
num -= ar[x][0]
else:
s += 1 if ar[x][1] else 2
return s
def calculate1(ar, num):
s = calculate(ar, num)
ar1 = []
for x in range(len(ar)):
if ar[x][1]:
ar1.append(x)
if len(ar1) >= 1:
tmp = ar[ar1[0]]
ar = ar[: ar1[0]] + ar[ar1[0] + 1 :]
s = min(s, calculate(ar, num) + 1)
ar = ar[: ar1[0]] + [tmp] + ar[ar1[0] :]
if len(ar1) == 2:
ar = ar[: ar1[1]] + ar[ar1[1] + 1 :]
s = min(s, calculate(ar, num) + 1)
ar = ar[: ar1[0]] + ar[ar1[0] + 1 :]
s = min(s, calculate(ar, num) + 2)
return s
n = int(input())
p = list(map(int, input().split(" ")))
ar = [False] * (n + 1)
for x in p:
if x != 0:
ar[x] = True
if ar == [False] * (n + 1):
if n == 1:
print(0)
else:
print(1)
else:
ones = 0
zeros = 0
for x in range(1, n + 1):
if not ar[x]:
if x % 2 == 0:
zeros += 1
else:
ones += 1
c = 0
for x in range(1, n):
if p[x] != 0 and p[x - 1] != 0 and p[x] % 2 != p[x - 1] % 2:
c += 1
s = 0
for x in range(n):
if p[x] != 0:
s = x
break
c1 = 0
s1 = -1
zerosp = []
onesp = []
c1 = 0
for x in range(s + 1, n):
if p[x] == 0:
if s1 == -1:
s1 = x
c1 += 1
elif c1 != 0:
if p[s1 - 1] % 2 != p[x] % 2:
c += 1
elif p[s1 - 1] % 2 == 0:
zerosp.append((c1, False))
else:
onesp.append((c1, False))
s1 = -1
c1 = 0
if n >= 2:
if p[0] == 0:
if p[s] % 2 == 0:
zerosp.append((s, True))
else:
onesp.append((s, True))
if p[-1] == 0:
c1 = 0
s2 = 0
for x in range(n - 2, -1, -1):
c1 += 1
if p[x] != 0:
s2 = x
break
if p[s2] % 2 == 0:
zerosp.append((c1, True))
else:
onesp.append((c1, True))
c1 = 0
zerosp = sorted(zerosp, key=lambda x: x[0])
onesp = sorted(onesp, key=lambda x: x[0])
print(c + calculate1(zerosp, zeros) + calculate1(onesp, ones))
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER LIST VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
debug = 0
def solve(n, a, war):
mis = set(range(1, n + 1)).difference(a)
typ = [0, 0, 0]
typs = [[], [], []]
i = 0
juz = 0
while i < n:
if a[i] == 0:
j = i
while j < n and a[j] == 0:
j += 1
if i == 0 and j == n:
return 0 if n == 1 else 1
if i == 0:
target = a[j] % 2
if war[0]:
target = 2
elif j == n:
target = a[i - 1] % 2
if war[1]:
target = 2
elif a[i - 1] % 2 != a[j] % 2:
target = 2
else:
target = a[i - 1] % 2
typ[target] += 1
typs[target].append(j - i)
i = j
else:
if i > 0 and a[i - 1] != 0 and a[i - 1] % 2 != a[i] % 2:
juz += 1
i += 1
if debug:
print(typ, typs)
m0 = sum(1 for mm in mis if mm % 2 == 0)
m1 = len(mis) - m0
if debug:
print(m0, m1)
typs[0].sort()
i = 0
while i < len(typs[0]) and m0 >= typs[0][i]:
m0 -= typs[0][i]
i += 1
remsol0 = len(typs[0]) - i
typs[1].sort()
i = 0
while i < len(typs[1]) and m1 >= typs[1][i]:
m1 -= typs[1][i]
i += 1
remsol1 = len(typs[1]) - i
if debug:
print(typ[2], remsol0, remsol1, juz, typ[2] + 2 * (remsol0 + remsol1) + juz)
return typ[2] + 2 * (remsol0 + remsol1) + juz
def go():
n = int(input())
a = list(map(int, input().split()))
print(min(solve(n, a, war) for war in [(0, 0), (0, 1), (1, 0), (1, 1)]))
go()
|
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST LIST LIST LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR RETURN VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR 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 VAR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
def inp(dtype=str, strip=True):
s = input()
res = [dtype(p) for p in s.split()]
res = res[0] if len(res) == 1 and strip else res
return res
def problem1():
t = inp(int)
for _ in range(t):
n = inp(int)
s = inp(str)
i = 0
while i < n and s[i] == "P":
i += 1
res = 0
while i < n:
while i < n and s[i] == "A":
i += 1
res1 = 0
while i < n and s[i] == "P":
res1 += 1
i += 1
res = max(res, res1)
print(res)
def problem2():
n, k = inp(int)
cards = []
for _ in range(n):
cards.append(inp(str))
d = {c: i for i, c in enumerate(cards)}
res = 0
c3 = ["?"] * k
for i, c1 in enumerate(cards):
for j, c2 in enumerate(cards[i + 1 :], i + 1):
for ii in range(k):
l1, l2 = c1[ii], c2[ii]
if l1 == l2:
c3[ii] = l1
elif l1 == "S" and l2 == "E":
c3[ii] = "T"
elif l1 == "S":
c3[ii] = "E"
elif l1 == "E" and l2 == "S":
c3[ii] = "T"
elif l1 == "E":
c3[ii] = "S"
elif l2 == "E":
c3[ii] = "S"
else:
c3[ii] = "E"
q = "".join(c3)
pos = d.get(q, -1)
if pos >= 0 and pos > i and pos > j:
res += 1
print(res)
def problem3():
n = inp(int)
p = inp(int, strip=False)
avail = set(range(n + 1)).difference(p)
nodd = sum([(el % 2 == 1) for el in avail])
neven = sum([(el % 2 == 0) for el in avail])
inf = 1000
mat = [[[inf for _ in range(2)] for _ in range(nodd + 1)] for _ in range(n + 1)]
mat[0][0][0] = mat[0][0][1] = 0
used = 0
for i in range(1, n + 1):
pos = i - 1
x = p[pos] % 2
missing = p[pos] == 0
if not missing:
for usedodd in range(nodd + 1):
mat[i][usedodd][x] = min(
mat[i - 1][usedodd][0] + x, mat[i - 1][usedodd][1] + (1 - x)
)
else:
for usedodd in range(1, nodd + 1):
mat[i][usedodd][1] = min(
mat[i - 1][usedodd - 1][0] + 1, mat[i - 1][usedodd - 1][1]
)
for usedodd in range(0, nodd + 1):
mat[i][usedodd][0] = min(
mat[i - 1][usedodd][0], mat[i - 1][usedodd][1] + 1
)
if missing:
used += 1
print(min(mat[n][nodd][0], mat[n][nodd][1]))
def problem4():
pass
def problem5():
pass
problem3()
|
FUNC_DEF VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR STRING ASSIGN VAR VAR STRING IF VAR STRING ASSIGN VAR VAR STRING IF VAR STRING VAR STRING ASSIGN VAR VAR STRING IF VAR STRING ASSIGN VAR VAR STRING IF VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF FUNC_DEF EXPR FUNC_CALL VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
n = int(input())
s = list(map(int, input().split()))
dp = [[[float("INF"), float("INF")] for i in range(n // 2 + 1)] for i in range(n + 1)]
dp[0][0] = [0, 0]
for i in range(1, n + 1):
if s[i - 1] == 0:
for j in range(1, n // 2 + 1):
dp[i][j][0] = min(dp[i - 1][j - 1][0], dp[i - 1][j - 1][1] + 1)
for j in range(n // 2 + 1):
dp[i][j][1] = min(dp[i - 1][j][0] + 1, dp[i - 1][j][1])
elif s[i - 1] % 2 == 0:
for j in range(1, n // 2 + 1):
dp[i][j][0] = min(dp[i - 1][j - 1][0], dp[i - 1][j - 1][1] + 1)
else:
for j in range(n // 2 + 1):
dp[i][j][1] = min(dp[i - 1][j][0] + 1, dp[i - 1][j][1])
print(min(dp[-1][-1]))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR STRING FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
import sys
n = int(input())
if n == 1:
print(0)
sys.exit()
arr = list(map(int, input().split()))
arr = [(i % 2 if i != 0 else -1) for i in arr]
skipped = sum([(i == -1) for i in arr])
if skipped == n:
print(1)
sys.exit()
ans = 0
for i in range(n - 1):
if arr[i] == 0 and arr[i + 1] == 1 or arr[i] == 1 and arr[i + 1] == 0:
ans += 1
if skipped == 0:
print(ans)
sys.exit()
even = n // 2 - sum([(i == 0) for i in arr])
odd = skipped - even
if 0 not in arr:
arr = [1] + arr + [1]
elif 1 not in arr:
arr = [0] + arr + [0]
else:
first = arr[min(arr.index(0), arr.index(1))]
last = arr[n - 1 - min(arr[::-1].index(0), arr[::-1].index(1))]
arr = [first] + arr + [last]
substrs = []
i = 0
while i != n + 2:
if arr[i] != -1:
i += 1
continue
left, j = arr[i - 1], i - 1
while arr[i] == -1:
i += 1
right = arr[i]
if left != right:
ans += 1
else:
substrs.append((j == 0 or i == n + 1, i - j - 1, left))
substrs.sort()
for l, i, j in substrs:
if j == 0:
if even >= i:
even -= i
else:
ans += 2 - l
elif odd >= i:
odd -= i
else:
ans += 2 - l
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR IF NUMBER VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER IF NUMBER VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
def parity(p):
if p == 0:
return 0
return 1 if A[p] != A[p - 1] else 0
def sol(p, pa, ip):
if pa < 0 or ip < 0:
return 1000000
if p == n:
return 0
if p > 0 and DP[p][ip][pa][A[p - 1]] != -1:
return DP[p][ip][pa][A[p - 1]]
if A[p] == -1:
A[p] = 1
r1 = sol(p + 1, pa, ip - 1) + parity(p)
A[p] = 0
r2 = sol(p + 1, pa - 1, ip) + parity(p)
A[p] = -1
if p > 0:
DP[p][ip][pa][A[p - 1]] = min(r1, r2)
return min(r1, r2)
else:
if p > 0:
DP[p][ip][pa][A[p - 1]] = sol(p + 1, pa, ip) + parity(p)
return sol(p + 1, pa, ip) + parity(p)
n = int(input())
A = [*map(int, input().split())]
if n == 1:
print(0)
exit(0)
par = n // 2
inp = n // 2 + n % 2
DP = [
[[[-1, -1] for _ in range(par + 1)] for _ in range(inp + 1)] for _ in range(n + 1)
]
for i in range(n):
if A[i] == 0:
A[i] = -1
elif A[i] % 2 == 0:
par -= 1
A[i] = 0
else:
inp -= 1
A[i] = 1
print(sol(0, par, inp))
|
FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
def garland(a):
n = len(a)
a = [0] + a
oo = n + 1
dp = [[[oo, oo] for __ in range(n + 1)] for _ in range(n + 1)]
dp[0][0][0] = dp[0][0][1] = 0
for i in range(1, n + 1):
for j in range(i + 1):
if a[i] % 2 == 1 or a[i] == 0:
dp[i][j][1] = min(dp[i - 1][j][0] + 1, dp[i - 1][j][1])
if a[i] % 2 == 0 and j > 0:
dp[i][j][0] = min(dp[i - 1][j - 1][0], dp[i - 1][j - 1][1] + 1)
m = int(n / 2)
return min(dp[n][m])
c1 = int(input())
c2 = str(input()).split()
a = []
for i in range(c1):
a.append(int(c2[i]))
print(garland(a))
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
n = int(input())
ps = list(map(int, input().split()))
used = [False] * (n + 1)
all_zeros = True
for p in ps:
used[p] = True
if p > 0:
all_zeros = False
if all_zeros:
if n == 1:
print(0)
else:
print(1)
else:
start_i = 0
while ps[start_i] == 0:
start_i += 1
starter = ps[start_i]
end_i = 0
while ps[n - 1 - end_i] == 0:
end_i += 1
last = ps[n - 1 - end_i]
end_i = n - 1 - end_i
holes = [[], [], [], []]
hole_l = 0
hole_s = start_i
for i in range(start_i, end_i + 1):
if ps[i] == 0:
hole_l += 1
else:
if hole_l > 0:
if ps[i] % 2 == ps[hole_s] % 2:
holes[ps[hole_s] % 2].append([hole_l, hole_s, i])
hole_s = i
hole_l = 0
if start_i > 0:
holes[2 + ps[start_i] % 2].append([start_i, -1, start_i])
if end_i < n - 1:
holes[2 + ps[end_i] % 2].append([n - end_i - 1, end_i, n])
unused = [0, 0]
for i in range(1, n + 1):
if not used[i]:
unused[i % 2] += 1
for i in range(4):
holes[i].sort()
for hole_group in range(4):
if holes[hole_group]:
while unused[hole_group % 2] >= holes[hole_group][0][0]:
unused[hole_group % 2] -= holes[hole_group][0][0]
for i in range(holes[hole_group][0][1] + 1, holes[hole_group][0][2]):
ps[i] = 1000 + hole_group
holes[hole_group].pop(0)
if not holes[hole_group]:
break
for even_part_hole in holes[2]:
for i in range(even_part_hole[1] + 1, even_part_hole[2]):
ps[i] = 2000 + 1
for even_part_hole in holes[0]:
for i in range(even_part_hole[1] + 1, even_part_hole[2]):
ps[i] = 2000 + 1
oe = ps[0] % 2
sol = 0
for p in ps:
if p % 2 != oe:
sol += 1
oe = p % 2
print(sol)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR LIST LIST LIST LIST LIST ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER LIST VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER LIST VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER LIST BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR FOR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER FOR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
import sys
input = sys.stdin.readline
n = int(input())
p = list(map(int, input().split()))
def maxDiff(p, n):
if n == 1:
return 0
used = {i for i in p}
rest = {(0): 0, (1): 0}
for i in range(1, n + 1):
if i not in used:
rest[i % 2] += 1
A00 = []
A11 = []
A01 = 0
prev = -1
start = -1
for i, el in enumerate(p):
if el == 0 and prev > 0:
le = i
start = prev
elif el > 0 and prev == 0 and start > 0:
start %= 2
end = el % 2
if start > 0 and end > 0:
A11.append(i - le)
elif not (start > 0 or end > 0):
A00.append(i - le)
else:
A01 += 1
prev = el
start = 0
for el in p:
if el > 0:
break
start += 1
if start == n:
return 1
end = 0
for el in p[::-1]:
if el > 0:
break
end += 1
A00.sort(reverse=True)
A11.sort(reverse=True)
while A00:
sect = A00.pop()
if rest[0] < sect:
A00.append(sect)
break
rest[0] -= sect
while A11:
sect = A11.pop()
if rest[1] < sect:
A11.append(sect)
break
rest[1] -= sect
ans = A01
ans += 2 * len(A00)
ans += 2 * len(A11)
prev = p[0]
for el in p[1:]:
if el > 0 and prev > 0 and (el + prev) % 2 > 0:
ans += 1
prev = el
sOdd = p[start] % 2
if rest[sOdd] < start:
ans += 1
else:
rest[sOdd] -= start
eOdd = p[-end - 1] % 2
if rest[eOdd] < end:
ans += 1
else:
rest[eOdd] -= end
return ans
ans = maxDiff(p, n)
print(ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
n = int(input())
arr = list(map(int, input().split()))
final = 0
if n % 2 == 0:
odd, even = n // 2, n // 2
else:
odd = n // 2 + 1
even = n // 2
temp, ind = [], []
for i in range(n):
if arr[i] == 0:
arr[i] = None
else:
if arr[i] % 2 == 0:
even -= 1
arr[i] = arr[i] % 2
else:
odd -= 1
arr[i] = arr[i] % 2
temp.append(arr[i] % 2)
ind.append(i)
for i in range(n - 1):
if arr[i] != None and arr[i + 1] != None:
l = [arr[i], arr[i + 1]]
l.sort()
if l == [0, 1]:
final += 1
d = {"first": [], "oddodd": [], "eveneven": [], "oddeven": [], "last": []}
if ind == []:
if n == 1:
print(0)
exit()
else:
print(1)
exit()
if ind[0] != 0:
d["first"].append(ind[0])
if ind[-1] != n - 1:
d["last"].append(n - ind[-1] - 1)
for i in range(len(ind) - 1):
if temp[i] == temp[i + 1]:
if temp[i] == 1:
d["oddodd"].append(ind[i + 1] - ind[i] - 1)
else:
d["eveneven"].append(ind[i + 1] - ind[i] - 1)
else:
d["oddeven"].append(ind[i + 1] - ind[i] - 1)
for i in d:
p = []
for j in d[i]:
if j != 0:
p.append(j)
d[i] = p
values = d["oddodd"]
values.sort()
ans = []
for i in range(len(values)):
if values[i] != 0:
if values[i] <= odd:
odd -= values[i]
else:
ans.append(values[i])
d["oddodd"] = ans
values = d["eveneven"]
values.sort()
ans = []
for i in range(len(values)):
if values[i] != 0:
if values[i] <= even:
even -= values[i]
else:
ans.append(values[i])
d["eveneven"] = ans
if d["first"] != []:
if temp[0] == 1:
if odd >= d["first"][0]:
odd -= d["first"][0]
d["first"] = []
elif even >= d["first"][0]:
even -= d["first"][0]
d["first"] = []
if d["last"] != []:
if temp[-1] == 1:
if odd >= d["last"][0]:
odd -= d["last"][0]
d["last"] = []
elif even >= d["last"][0]:
even -= d["last"][0]
d["last"] = []
final += len(d["last"])
final += len(d["first"])
final += 2 * len(d["oddodd"])
final += 2 * len(d["eveneven"])
final += len(d["oddeven"])
print(final)
|
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 IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NONE IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NONE VAR BIN_OP VAR NUMBER NONE ASSIGN VAR LIST VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR LIST NUMBER NUMBER VAR NUMBER ASSIGN VAR DICT STRING STRING STRING STRING STRING LIST LIST LIST LIST LIST IF VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR STRING BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR STRING BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING VAR ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING VAR IF VAR STRING LIST IF VAR NUMBER NUMBER IF VAR VAR STRING NUMBER VAR VAR STRING NUMBER ASSIGN VAR STRING LIST IF VAR VAR STRING NUMBER VAR VAR STRING NUMBER ASSIGN VAR STRING LIST IF VAR STRING LIST IF VAR NUMBER NUMBER IF VAR VAR STRING NUMBER VAR VAR STRING NUMBER ASSIGN VAR STRING LIST IF VAR VAR STRING NUMBER VAR VAR STRING NUMBER ASSIGN VAR STRING LIST VAR FUNC_CALL VAR VAR STRING VAR FUNC_CALL VAR VAR STRING VAR BIN_OP NUMBER FUNC_CALL VAR VAR STRING VAR BIN_OP NUMBER FUNC_CALL VAR VAR STRING VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)
def main():
n = int(input())
l = [int(s) for s in input().split()]
e, o = n // 2, n - n // 2
for i in range(n):
if l[i] != 0:
if l[i] % 2 == 0:
e -= 1
else:
o -= 1
dp = [
[[[-1, -1] for i in range(o + 1)] for j in range(e + 1)] for k in range(n + 1)
]
def solve(i, e, o, p):
if dp[i][e][o][p] != -1:
return dp[i][e][o][p]
if i == 0:
if l[i] != 0:
if p == l[i] % 2:
dp[i][e][o][p] = 0
return 0
else:
dp[i][e][o][p] = 1000
return 1000
elif e == 0 and o == 0:
dp[i][e][o][p] = 1000
return 1000
elif e == 0:
if p == 0:
dp[i][e][o][p] = 1000
return 1000
else:
dp[i][e][o][p] = 0
return 0
elif o == 0:
if p == 1:
dp[i][e][o][p] = 1000
return 1000
else:
dp[i][e][o][p] = 0
return 0
else:
dp[i][e][o][p] = 0
return 0
if l[i] == 0:
if p == 0:
if e == 0:
dp[i][e][o][p] = 1000
return 1000
w1 = solve(i - 1, e - 1, o, 1)
w2 = solve(i - 1, e - 1, o, 0)
if w1 + 1 < w2:
dp[i][e][o][p] = w1 + 1
return w1 + 1
else:
dp[i][e][o][p] = w2
return w2
else:
if o == 0:
dp[i][e][o][p] = 1000
return 1000
w1 = solve(i - 1, e, o - 1, 0)
w2 = solve(i - 1, e, o - 1, 1)
if w1 + 1 < w2:
dp[i][e][o][p] = w1 + 1
return w1 + 1
else:
dp[i][e][o][p] = w2
return w2
elif p != l[i] % 2:
dp[i][e][o][p] = 1000
return 1000
elif p == 0:
w1 = solve(i - 1, e, o, 1)
w2 = solve(i - 1, e, o, 0)
if w1 + 1 < w2:
dp[i][e][o][p] = w1 + 1
return w1 + 1
else:
dp[i][e][o][p] = w2
return w2
else:
w1 = solve(i - 1, e, o, 0)
w2 = solve(i - 1, e, o, 1)
if w1 + 1 < w2:
dp[i][e][o][p] = w1 + 1
return w1 + 1
else:
dp[i][e][o][p] = w2
return w2
ans = min(solve(n - 1, e, o, 0), solve(n - 1, e, o, 1))
print(ans)
main()
|
IMPORT ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR VAR IF VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
import sys
input = lambda: sys.stdin.readline()
n = int(input())
if n == 1:
print(0)
exit(0)
a = list(map(int, input().split()))
t = -1
o, e = 0, 0
for i in a:
if i != 0:
e += (i + 1) % 2
o += i % 2
ans = 0
for j in range(n - 1):
if a[j] != 0 and a[j + 1] != 0:
ans += int(a[j] % 2 != a[j + 1] % 2)
e = n // 2 - e
o = n // 2 + n % 2 - o
l = -1
f = False
z = [[], [], [], [], []]
for i in range(n):
if a[i] != 0:
if f:
f = False
if l == -1:
if a[i] % 2 == 0:
z[0].append(i)
else:
z[1].append(i)
elif a[i] % 2 == t:
if t == 0:
z[3].append(i - l - 1)
else:
z[4].append(i - l - 1)
else:
z[2].append(i - l - 1)
l = i
t = a[i] % 2
else:
f = True
if a[-1] == 0:
if l == -1:
print(1)
exit(0)
elif t == 0:
z[0].append(n - l - 1)
else:
z[1].append(n - l - 1)
z[1].sort()
z[0].sort()
z[3].sort()
z[4].sort()
ans += len(z[2])
for i in range(len(z[3])):
if z[3][i] <= e:
e -= z[3][i]
else:
ans += (len(z[3]) - i) * 2
break
for i in range(len(z[4])):
if z[4][i] <= o:
o -= z[4][i]
else:
ans += (len(z[4]) - i) * 2
break
for i in range(len(z[0])):
if z[0][i] <= e:
e -= z[0][i]
else:
ans += len(z[0]) - i
break
for i in range(len(z[1])):
if z[1][i] <= o:
o -= z[1][i]
else:
ans += len(z[1]) - i
break
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST LIST LIST LIST LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
n = int(input())
p = input().split()
allnos = range(1, n + 1)
thenos = []
givnos = []
extra = []
comp = 0
no = None
gap = 0
for i in range(len(p)):
if int(p[i]) != 0:
givnos.append(int(p[i]))
if no == None and gap > 0:
extra.append([int(p[i]), int(p[i]), gap])
elif gap > 0:
thenos.append([no, int(p[i]), gap])
elif no is not None and int(p[i]) % 2 != no % 2:
comp += 1
no = int(p[i])
gap = 0
else:
gap += 1
if gap > 0:
if no != None:
extra.append([no, no, gap])
else:
if gap == 1:
print("0")
else:
print("1")
exit()
remnos = [item for item in allnos if item not in givnos]
pos = 0
neg = 0
for i in range(len(remnos)):
if remnos[i] % 2 == 0:
pos += 1
else:
neg += 1
thenos.sort(key=lambda x: x[2])
for ano in thenos:
if ano[0] % 2 == ano[1] % 2:
if ano[0] % 2 == 0:
if ano[2] <= pos:
pos -= ano[2]
else:
comp += 2
elif ano[2] <= neg:
neg -= ano[2]
else:
comp += 2
else:
comp += 1
for e in extra:
if e[0] % 2 == 0:
if e[2] > pos:
comp += 1
else:
pos -= e[2]
elif e[2] > neg:
comp += 1
else:
neg -= e[2]
print(comp)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NONE VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
n = int(input())
a = list(map(int, input().split()))
z = []
if len(set(a)) == 1:
if len(a) == 1:
print(0)
else:
print(1)
exit()
even = n // 2
odd = n - even
for i in a:
if i:
if i % 2:
odd -= 1
else:
even -= 1
for i in a:
if i == 0:
if not z:
z.append(1)
elif z[-1] != "o" and z[-1] != "e":
z[-1] += 1
else:
z.append(1)
elif i % 2 == 0:
z.append("e")
else:
z.append("o")
count = 0
oddneeded, evenneeded = [], []
oddcorner, evencorner = [], []
for i in range(len(z)):
if z[i] == "o" or z[i] == "e":
continue
if i == 0:
if z[i + 1] == "o":
oddcorner.append([z[i], i])
else:
evencorner.append([z[i], i])
elif i == len(z) - 1:
if z[i - 1] == "o":
oddcorner.append([z[i], i])
else:
evencorner.append([z[i], i])
elif z[i - 1] == z[i + 1]:
if z[i - 1] == "o":
if i != 0 and i != len(z) - 1:
oddneeded.append([z[i], i])
elif i != 0 and i != len(z) - 1:
evenneeded.append([z[i], i])
else:
z[i] = "e"
oddneeded.sort(reverse=True)
evenneeded.sort(reverse=True)
oddcorner.sort(reverse=True)
evencorner.sort(reverse=True)
while oddneeded:
if oddneeded[-1][0] <= odd:
x = oddneeded.pop()
odd -= x[0]
z[x[1]] = "o"
else:
break
for i in oddneeded:
z[i[1]] = "e"
while oddcorner:
if oddcorner[-1][0] <= odd:
x = oddcorner.pop()
odd -= x[0]
z[x[1]] = "o"
else:
break
for i in oddcorner:
z[i[1]] = "e"
while evenneeded:
if evenneeded[-1][0] <= even:
x = evenneeded.pop()
even -= x[0]
z[x[1]] = "e"
else:
break
for i in evenneeded:
z[i[1]] = "o"
while evencorner:
if evencorner[-1][0] <= even:
x = evencorner.pop()
even -= x[0]
z[x[1]] = "e"
else:
break
for i in evencorner:
z[i[1]] = "o"
for i in range(1, len(z)):
if z[i] != z[i - 1]:
count += 1
print(count)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR IF VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR IF VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER STRING FOR VAR VAR ASSIGN VAR VAR NUMBER STRING WHILE VAR IF VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER STRING FOR VAR VAR ASSIGN VAR VAR NUMBER STRING WHILE VAR IF VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER STRING FOR VAR VAR ASSIGN VAR VAR NUMBER STRING WHILE VAR IF VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER STRING FOR VAR VAR ASSIGN VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
n = int(input())
a = [int(i) for i in input().split()]
odd = n // 2 + (n & 1)
even = n // 2
if sum(a) == 0:
print(1) if n > 1 else print(0)
exit(0)
for i in a:
if i & 1 == 1:
odd -= 1
elif i & 1 == 0 and i != 0:
even -= 1
ans = 0
s = [[], [], []]
for i in range(0, len(a) - 1):
if a[i] & 1 != a[i + 1] & 1:
if a[i] != 0 and a[i + 1] != 0:
ans += 1
n1 = 0
n2 = 0
for i in range(len(a)):
if a[i] != 0 and a[i] % 2 == 0:
n1 = i
break
if a[i] != 0 and a[i] % 2 == 1:
n2 = i
break
i = 0
n11 = 0
n22 = 0
while i < len(a):
if a[i] != 0:
for j in range(i + 1, len(a)):
if a[j] != 0:
s[a[i] % 2 + a[j] % 2].append(j - i - 1)
i = j
break
if j == len(a) - 1:
if a[i] % 2 == 0:
n11 = len(a) - i - 1
else:
n22 = len(a) - i - 1
break
else:
i += 1
for i in s:
i.sort()
for i in range(len(s[0])):
if s[0][i] <= even:
even -= s[0][i]
else:
ans += 2 * len(s[0][i:])
break
if n1:
if n1 > even:
ans += 1
else:
even -= n1
if n11:
if n11 > even:
ans += 1
else:
even -= n11
for i in range(len(s[2])):
if s[2][i] <= odd:
odd -= s[2][i]
else:
ans += 2 * len(s[2][i:])
break
if n2:
if n2 > odd:
ans += 1
else:
odd -= n2
if n22:
if n22 > odd:
ans += 1
else:
odd -= n22
for i in s[1]:
if i != 0:
ans += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR VAR NUMBER FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST LIST LIST LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR IF VAR IF VAR VAR VAR NUMBER VAR VAR IF VAR IF VAR VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR IF VAR IF VAR VAR VAR NUMBER VAR VAR IF VAR IF VAR VAR VAR NUMBER VAR VAR FOR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
def solve(bulbs, i, ec, oc, prev, memo={}):
if i == len(bulbs):
if min(ec, oc) != 0:
return float("inf")
return 0
if bulbs[i] != 0:
return (prev != bulbs[i] % 2) + solve(bulbs, i + 1, ec, oc, bulbs[i] % 2, memo)
if (i, ec, oc, prev) not in memo:
eit = (prev == 1) + solve(bulbs, i + 1, ec - 1, oc, 0, memo)
oit = (prev == 0) + solve(bulbs, i + 1, ec, oc - 1, 1, memo)
memo[i, ec, oc, prev] = min(eit, oit)
return memo[i, ec, oc, prev]
n = int(input())
bulbs = list(map(int, input().split()))
bulbSet = set(bulbs)
ec, oc = 0, 0
for i in range(1, n + 1):
if i not in bulbSet:
if i % 2 == 0:
ec += 1
else:
oc += 1
memo = {}
print(min(solve(bulbs, 0, ec, oc, 0, memo), solve(bulbs, 0, ec, oc, 1, memo)))
|
FUNC_DEF DICT IF VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR STRING RETURN NUMBER IF VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR DICT EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
N = int(input())
A = [int(a) for a in input().split()]
M = A.count(0)
A2 = [(a % 2) for a in A]
O = A2.count(1)
E = N - M - O
O = (N + 1) // 2 - O
E = N // 2 - E
C = [M]
for a in A:
C.append(C[-1] - (a == 0))
X = [([999] * (N + 2)) for _ in range(N)]
Y = [([999] * (N + 2)) for _ in range(N)]
if A[0] % 2:
X[0][O] = 0
elif A[0]:
Y[0][O] = 0
else:
if O:
X[0][O - 1] = 0
if E:
Y[0][O] = 0
for i in range(1, N):
for j in range(N + 1):
if A[i] % 2:
X[i][j] = min(X[i - 1][j], Y[i - 1][j] + 1)
elif A[i]:
Y[i][j] = min(X[i - 1][j] + 1, Y[i - 1][j])
else:
X[i][j] = min(X[i - 1][j + 1], Y[i - 1][j + 1] + 1)
Y[i][j] = min(X[i - 1][j] + 1, Y[i - 1][j])
print(min(X[N - 1][0], Y[N - 1][0]))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR LIST VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
INF = 1000
def mod2(a, n):
even, odd = 0, 0
for i in range(n):
if a[i] == 0:
a[i] = -1
elif a[i] & 1:
a[i] = 1
odd += 1
else:
a[i] = 0
even += 1
even = n // 2 - even
odd = (n - 1) // 2 + 1 - odd
return even, odd
def minc(i, e, o, l):
if i >= n:
return 0
cur = dp[i][e][o][l]
if cur != -1:
return cur
if a[i] == 0:
cur = (0 ^ l) + minc(i + 1, e, o, 0)
elif a[i] == 1:
cur = (1 ^ l) + minc(i + 1, e, o, 1)
else:
cur = INF
if e > 0:
cur = min(cur, (0 ^ l) + minc(i + 1, e - 1, o, 0))
if o > 0:
cur = min(cur, (1 ^ l) + minc(i + 1, e, o - 1, 1))
dp[i][e][o][l] = cur
return cur
n = int(input())
a = list(map(int, input().split()))
even, odd = mod2(a, n)
dp = [
[[[(-1) for i0 in range(2)] for i1 in range(odd + 1)] for i2 in range(even + 1)]
for i3 in range(n)
]
if a[0] == 0:
ans = minc(1, even, odd, 0)
elif a[0] == 1:
ans = minc(1, even, odd, 1)
else:
ans = INF
if even > 0:
ans = min(ans, minc(1, even - 1, odd, 0))
if odd > 0:
ans = min(ans, minc(1, even, odd - 1, 1))
print(ans)
|
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR RETURN VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
def f(mass, a):
mass.sort()
prov = True
for i in range(len(mass)):
if a >= mass[i]:
a -= mass[i]
else:
prov = False
break
if prov:
return [0, a]
return [2 * (len(mass) - i), a]
n = int(input())
if n == 1:
print(0)
else:
mass = [int(i) for i in input().split()]
chet = 0
nechet = 0
length = 0
chet_mass = []
nechet_mass = []
now = -1
begin = 0, 0
end = 0, 0
counter = 0
for i in range(n):
if mass[i] == 0:
length += 1
elif mass[i] % 2 == 0:
chet += 1
if length != 0:
if now == 0:
chet_mass.append(length)
elif now == -1:
begin = length, 0
else:
counter += 1
elif now == 1:
counter += 1
now = 0
length = 0
else:
nechet += 1
if length != 0:
if now == 1:
nechet_mass.append(length)
elif now == -1:
begin = length, 1
else:
counter += 1
elif now == 0:
counter += 1
now = 1
length = 0
if now == -1:
print(1)
else:
end = length, now
chet_mass.sort()
nechet_mass.sort()
xxx = f(chet_mass, n // 2 - chet)
yyy = f(nechet_mass, (n + 1) // 2 - nechet)
minn = xxx[0] + yyy[0] + 2
massiv = [xxx[1], yyy[1]]
if end[0] <= massiv[end[1]]:
massiv[end[1]] -= end[0]
minn -= 1
if begin[0] <= massiv[begin[1]]:
minn -= 1
minn += counter
print(minn)
|
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR RETURN LIST NUMBER VAR RETURN LIST BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
def main():
n = int(input())
lst = list(map(int, input().split()))
nums = set()
for el in lst:
if el != 0:
nums.add(el)
odd = 0
even = 0
for i in range(1, n + 1):
if not i in nums:
if i % 2 == 0:
even += 1
else:
odd += 1
if n == 1:
print(0)
return 0
emties = []
st = 0
started = False
diff = 0
for i in range(0, len(lst)):
if lst[i] == 0 and not started:
st = i
started = True
elif lst[i] != 0 and started:
emties.append([st, i])
started = False
elif i > 0:
if lst[i] % 2 != lst[i - 1] % 2:
diff += 1
if started:
emties.append([st, len(lst)])
emties = sorted(emties, key=lambda x: x[1] - x[0])
for i in range(len(emties)):
emties[i][1] -= 1
odd_removed = []
even_removed = []
for empty in emties:
if empty[0] == 0 and empty[1] == len(lst) - 1:
print(1)
return 0
elif empty[0] == 0:
if lst[empty[1] + 1] % 2 == 1:
if odd >= empty[1] - empty[0] + 1:
odd -= empty[1] - empty[0] + 1
odd_removed.append(empty[1] - empty[0] + 1)
else:
diff += 1
elif even >= empty[1] - empty[0] + 1:
even -= empty[1] - empty[0] + 1
even_removed.append(empty[1] - empty[0] + 1)
else:
diff += 1
elif empty[1] == len(lst) - 1:
if lst[empty[0] - 1] % 2 == 1:
if odd >= empty[1] - empty[0] + 1:
odd -= empty[1] - empty[0] + 1
odd_removed.append(empty[1] - empty[0] + 1)
else:
diff += 1
elif even >= empty[1] - empty[0] + 1:
even -= empty[1] - empty[0] + 1
even_removed.append(empty[1] - empty[0] + 1)
else:
diff += 1
elif lst[empty[0] - 1] % 2 != lst[empty[1] + 1] % 2:
diff += 1
elif lst[empty[1] + 1] % 2 == 0:
if even >= empty[1] - empty[0] + 1:
even -= empty[1] - empty[0] + 1
else:
even_removed = sorted(even_removed)
if (
len(even_removed) > 0
and even_removed[-1] + even >= empty[1] - empty[0] + 1
):
diff += 1
even -= empty[1] - empty[0] + 1 - even_removed[-1]
even_removed.pop()
else:
diff += 2
elif lst[empty[1] + 1] % 2 == 1:
if odd >= empty[1] - empty[0] + 1:
odd -= empty[1] - empty[0] + 1
else:
odd_removed = sorted(odd_removed)
if (
len(odd_removed) > 0
and odd_removed[-1] + odd >= empty[1] - empty[0] + 1
):
diff += 1
odd -= empty[1] - empty[0] + 1 - odd_removed[-1]
odd_removed.pop()
else:
diff += 2
print(diff)
t = 1
for i in range(t):
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN NUMBER IF VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
n = int(input())
a = list(map(int, input().split()))
e = n // 2
o = n - e
ptf = [[[0, 0] for i in range(e + 2)] for j in range(o + 2)]
dt = {}
for i in range(n):
if a[i] != 0:
dt[i + 1] = a[i]
for i in range(o + 1):
for j in range(e + 1):
if i == 0:
ptf[i][j][0] = 99999
if j == 0:
ptf[i][j][1] = 99999
if i + j in dt:
if dt[i + j] % 2 == 1:
ptf[i][j][1] = 99999
else:
ptf[i][j][0] = 99999
ptf[0][0][0] = 0
ptf[0][0][1] = 0
for i in range(o + 1):
for j in range(e + 1):
if ptf[i][j + 1][1] < 9999:
ptf[i][j + 1][1] = min(ptf[i][j][1], ptf[i][j][0] + 1)
if ptf[i + 1][j][0] < 9999:
ptf[i + 1][j][0] = min(ptf[i][j][0], ptf[i][j][1] + 1)
print(min(ptf[i][j][0], ptf[i][j][1]))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
n = int(input())
p = list(map(int, input().split()))
total_chet = n // 2
total_nechet = n // 2 + n % 2
hang_chet = len([item for item in p if item > 0 and item % 2 == 0])
hang_nechet = len([item for item in p if item > 0 and item % 2 == 1])
rest_chet = total_chet - hang_chet
rest_nechet = total_nechet - hang_nechet
pred = "None"
chet_needed_lens = []
nechet_needed_lens = []
edge_start = []
edge_finish = []
given_compl = 0
mixed_count = 0
cur_zero_count = 0
for item in p:
if item == 0:
cur_zero_count += 1
else:
if item % 2 == 0:
if pred == "chet":
chet_needed_lens.append(cur_zero_count)
elif pred == "nechet":
if cur_zero_count > 0:
mixed_count += 1
else:
given_compl += 1
elif pred == "None":
edge_start = ["chet", cur_zero_count]
else:
print("Invalid State, line 40")
pred = "chet"
else:
if pred == "nechet":
nechet_needed_lens.append(cur_zero_count)
elif pred == "chet":
if cur_zero_count > 0:
mixed_count += 1
else:
given_compl += 1
elif pred == "None":
edge_start = ["nechet", cur_zero_count]
else:
print("Invalid state, line 50, pred = {}".format(pred))
pred = "nechet"
cur_zero_count = 0
edge_finish = [pred, cur_zero_count]
chet_needed_lens.sort()
nechet_needed_lens.sort()
i = 0
while i < len(chet_needed_lens) and chet_needed_lens[i] <= rest_chet:
rest_chet -= chet_needed_lens[i]
i += 1
chet_compl = 2 * (len(chet_needed_lens) - i)
i = 0
while i < len(nechet_needed_lens) and nechet_needed_lens[i] <= rest_nechet:
rest_nechet -= nechet_needed_lens[i]
i += 1
nechet_compl = 2 * (len(nechet_needed_lens) - i)
start_compl = 0
if len(edge_start) > 0:
if edge_start[0] == "nechet":
if rest_nechet >= edge_start[1]:
rest_nechet -= edge_start[1]
start_compl += 0
else:
start_compl += 1
elif rest_chet >= edge_start[1]:
rest_chet -= edge_start[1]
start_compl += 0
else:
start_compl += 1
finish_compl = 0
if edge_finish[0] == "nechet":
if rest_nechet >= edge_finish[1]:
finish_compl += 0
else:
finish_compl += 1
elif edge_finish[0] == "chet":
if rest_chet >= edge_finish[1]:
rest_chet -= edge_finish[1]
finish_compl += 0
else:
finish_compl += 1
elif edge_finish[0] == "None":
if n > 1:
finish_compl += 1
else:
print("Invalid State")
ans = given_compl + mixed_count + chet_compl + nechet_compl + start_compl + finish_compl
print(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 BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR LIST STRING VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR LIST STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER STRING IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
n = int(input())
bulbs = list(map(int, input().split()))
li = []
odds = 0
blanks = 0
for i, e in enumerate(bulbs):
if e != 0:
if e % 2 == 1:
li.append((i, 1))
odds += 1
else:
li.append((i, 0))
else:
blanks += 1
odds = (n + 1) // 2 - odds
even = blanks - odds
ans = 0
spaces = []
if len(li) > 0 and li[0][0] != 0:
li = [(-1, li[0][1])] + li
if len(li) > 0 and li[-1][0] != n - 1:
li = li + [(n, li[-1][1])]
for i in range(len(li) - 1):
if li[i][1] == li[i + 1][1]:
temp = (
li[i][0] == -1 or li[i + 1][0] == n,
li[i + 1][0] - li[i][0] - 1,
li[i][1],
)
else:
temp = None
ans += 1
if temp is not None and temp[1] > 0:
spaces.append(temp)
spaces.sort()
for space in spaces:
if space[2] == 0:
if even < space[1]:
ans += 1 if space[0] else 2
else:
even -= space[1]
elif odds < space[1]:
ans += 1 if space[0] else 2
else:
odds -= space[1]
if len(li) == 0:
ans = min(1, n - 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 ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR NUMBER NUMBER VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR LIST VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NONE VAR NUMBER IF VAR NONE VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
import sys
input = sys.stdin.readline
INF = 10**14
N = int(input())
A = list(map(int, input().split()))
odds = (N + 1) // 2
evens = N // 2
for a in A:
if a == 0:
continue
if a % 2 == 0:
evens -= 1
else:
odds -= 1
dp = [
[[[INF, INF] for _ in range(odds + 1)] for _ in range(evens + 1)]
for _ in range(N + 1)
]
dp[0][0][0][0] = 0
dp[0][0][0][1] = 0
for i, a in enumerate(A):
for j in range(evens + 1):
for k in range(odds + 1):
if a == 0:
if k != odds:
dp[i + 1][j][k + 1][1] = min(dp[i][j][k][0] + 1, dp[i][j][k][1])
if j != evens:
dp[i + 1][j + 1][k][0] = min(dp[i][j][k][1] + 1, dp[i][j][k][0])
elif a % 2 == 0:
dp[i + 1][j][k][0] = min(dp[i][j][k][1] + 1, dp[i][j][k][0])
else:
dp[i + 1][j][k][1] = min(dp[i][j][k][0] + 1, dp[i][j][k][1])
print(min(dp[N][evens][odds]))
|
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
n = int(input())
pr = 0
t = 0
ch = n // 2
nech = n // 2 + n % 2
mass1 = []
mass3 = []
answer = 0
for i in [int(x) for x in input().split()]:
if i == 0:
pr += 1
elif pr > 0:
if i % 2 == 0:
ch -= 1
else:
nech -= 1
if t == 0:
t = i % 2 + 1
mass1.append([pr, t])
else:
if t == i % 2 + 1:
mass3.append([pr, t])
else:
answer += 1
t = i % 2 + 1
pr = 0
else:
if i % 2 == 0:
ch -= 1
else:
nech -= 1
if t != i % 2 + 1 and t != 0:
answer += 1
t = i % 2 + 1
if pr != 0:
mass1.append([pr, t])
mass3.sort()
mass1.sort()
for l in mass3:
if l[1] == 1:
if ch >= l[0]:
ch -= l[0]
else:
nech -= l[0] - ch
answer += 2
elif nech >= l[0]:
nech -= l[0]
else:
ch -= l[0] - nech
answer += 2
for l in mass1:
if l[1] == 1:
if ch >= l[0]:
ch -= l[0]
else:
nech -= l[0] - ch
ch = 0
answer += 1
elif nech >= l[0]:
nech -= l[0]
else:
ch -= l[0] - nech
nech = 0
answer += 1
print(answer)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
def ip():
n = int(input())
a = list(map(int, input().split()))
rem = set([i for i in range(1, n + 1)]) - set(a)
if n == 1:
return 0
o = e = 0
for i in rem:
if i % 2 == 0:
e += 1
else:
o += 1
ct = 0
i = 0
while i < len(a) and a[i] == 0:
i += 1
if i == len(a):
return 1
else:
startodd = starteven = endodd = endeven = 0
es = []
os = []
if i != 0:
if a[i] % 2 == 0:
starteven = i
else:
startodd = i
start = i
i = len(a) - 1
end = 0
while i >= 0 and a[i] == 0:
i -= 1
end += 1
if end != 0:
if a[i] % 2 == 0:
endeven = end
else:
endodd = end
end = i
prev = start
for i in range(start + 1, end + 1):
if a[i] == 0:
continue
if i - prev > 1:
if a[i] % 2 == 0 and a[prev] % 2 == 0:
es.append(i - prev - 1)
elif a[i] % 2 != 0 and a[prev] % 2 != 0:
os.append(i - prev - 1)
else:
ct += 1
elif i - prev == 1:
if a[i] % 2 != a[prev] % 2:
ct += 1
prev = i
os.sort(reverse=True)
es.sort(reverse=True)
while os and os[-1] <= o:
o -= os[-1]
os.pop()
if startodd != 0 and o >= startodd:
o -= startodd
startodd = 0
elif startodd != 0:
os.append(startodd)
if endodd != 0 and o >= endodd:
o -= endeven
endodd = 0
elif endodd != 0:
os.append(endodd)
ct += max(len(os) * 2, 0)
while es and es[-1] <= e:
e -= es[-1]
es.pop()
if starteven != 0 and e >= starteven:
e -= starteven
starteven = 0
elif starteven != 0:
es.append(starteven)
if endeven != 0 and e >= endeven:
e -= endeven
endeven = 0
elif endeven != 0:
es.append(endeven)
ct += max(len(es) * 2, 0)
if startodd != 0 and startodd in os or startodd + o in os:
ct -= 1
if starteven != 0 and starteven in es or starteven + e in es:
ct -= 1
if endodd != 0 and endodd in os or endodd + o in os:
ct -= 1
if endeven != 0 and endeven in es or endeven + e in es:
ct -= 1
return ct
print(ip())
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
n = int(input())
p = list(map(int, input().split()))
if n == 1:
print(0)
exit(0)
if sum(p) == 0:
print(1)
exit(0)
INF = 100
dp = [([INF] * 2) for _ in range(n // 2 + 1)]
if p[0] == 0:
dp[1][0] = 0
dp[0][1] = 0
elif p[0] % 2:
dp[0][1] = 0
else:
dp[1][0] = 0
for i in range(1, n):
newdp = [([INF] * 2) for _ in range(n // 2 + 1)]
if p[i] == 0:
for j in range(n // 2 + 1):
newdp[j][1] = min(dp[j][1], dp[j][0] + 1)
if j == n // 2:
break
newdp[j + 1][0] = min(dp[j][0], dp[j][1] + 1)
elif p[i] % 2:
for j in range(n // 2 + 1):
newdp[j][1] = min(dp[j][1], dp[j][0] + 1)
else:
for j in range(n // 2):
newdp[j + 1][0] = min(dp[j][0], dp[j][1] + 1)
dp = newdp
print(min(dp[-1]))
|
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 NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
import sys
input = sys.stdin.readline
INF = 10**9
n = int(input())
array = [int(item) for item in input().split()]
seen = set(array)
odd_num = 0
eve_num = 0
for i in range(1, n + 1):
if i not in seen:
if i % 2 == 0:
eve_num += 1
else:
odd_num += 1
num = odd_num + eve_num
dp = [[([INF] * (num + 2)) for _ in range(3)] for _ in range(n + 1)]
dp[0][0][0] = 0
for i, item in enumerate(array):
if item != 0:
if item % 2 == 0:
for k in range(odd_num + 1):
dp[i + 1][2][k] = min(dp[i + 1][2][k], dp[i][2][k])
dp[i + 1][2][k] = min(dp[i + 1][2][k], dp[i][1][k] + 1)
dp[i + 1][2][k] = min(dp[i + 1][2][k], dp[i][0][k])
else:
for k in range(odd_num + 1):
dp[i + 1][1][k] = min(dp[i + 1][1][k], dp[i][1][k])
dp[i + 1][1][k] = min(dp[i + 1][1][k], dp[i][2][k] + 1)
dp[i + 1][1][k] = min(dp[i + 1][1][k], dp[i][0][k])
else:
for k in range(num, -1, -1):
dp[i + 1][1][k + 1] = min(dp[i + 1][1][k + 1], dp[i][1][k])
dp[i + 1][1][k + 1] = min(dp[i + 1][1][k + 1], dp[i][2][k] + 1)
dp[i + 1][1][k + 1] = min(dp[i + 1][1][k + 1], dp[i][0][k])
for k in range(num, -1, -1):
dp[i + 1][2][k] = min(dp[i + 1][2][k], dp[i][1][k] + 1)
dp[i + 1][2][k] = min(dp[i + 1][2][k], dp[i][2][k])
dp[i + 1][2][k] = min(dp[i + 1][2][k], dp[i][0][k])
print(min(dp[n][1][odd_num], dp[n][2][odd_num]))
|
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
n = int(input())
girl = list(map(int, input().split()))
odd = 0
even = 0
for g in girl:
if g != 0:
if g % 2 != 0:
odd += 1
else:
even += 1
left_odd = (n + 1) // 2 - odd
left_even = n // 2 - even
series = []
left = 0
right = n - 1
while girl[left] == 0:
left += 1
if left == n:
break
while girl[right] == 0:
right -= 1
if right == -1:
break
if left > right:
print(1 if n > 1 else 0)
exit()
cur = None
ans = 0
for i in range(left, right + 1):
if girl[i] != 0:
if i < n - 1 and girl[i + 1] != 0 and girl[i + 1] % 2 != girl[i] % 2:
ans += 1
if cur is not None:
cur[1] = girl[i] % 2
series.append(cur)
cur = None
elif cur is None:
cur = [0, 0, 1]
if i > 0:
cur[0] = girl[i - 1] % 2
else:
cur[2] += 1
series.sort(key=lambda tup: tup[2])
for i in range(len(series)):
s = series[i]
if s[0] != s[1]:
ans += 1
elif s[0] % 2 != 0:
if left_odd >= s[2]:
left_odd -= s[2]
else:
ans += 2
elif left_even >= s[2]:
left_even -= s[2]
else:
ans += 2
if girl[left] % 2 == 0:
if left_even < left:
ans += 1
else:
left_even -= left
elif left_odd < left:
ans += 1
else:
left_odd -= left
if girl[right] % 2 == 0:
if left_even < n - 1 - right:
ans += 1
elif left_odd < n - 1 - right:
ans += 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER IF VAR VAR WHILE VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NONE ASSIGN VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NONE IF VAR NONE ASSIGN VAR LIST NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
prime = 1000000007
t = 1
for test in range(t):
n = int(input())
a = list(map(int, input().split()))
if n == 1:
print(0)
else:
b = set(a)
if len(b) == 1:
print(1)
else:
even = 0
odd = 0
for i in range(1, n + 1):
if i not in b:
if i % 2 == 0:
even += 1
else:
odd += 1
ans = 0
prev = a[0]
for i in a:
if i != 0 and prev != 0 and i % 2 != prev % 2:
ans += 1
prev = i
solve_first = []
ind = 0
while a[ind] == 0:
ind += 1
prev = a[ind]
count = ind
if ind != 0:
tmp = 0
left_count = count
left_parity = prev % 2
else:
tmp = 1
left_count = 0
left_parity = prev % 2
while ind < n:
if a[ind] != 0:
if count != 0:
if prev % 2 == a[ind] % 2:
solve_first.append((count, tmp, prev % 2))
if tmp == 0:
tmp = 1
else:
ans += 1
prev = a[ind]
count = 0
else:
count += 1
ind += 1
if count != 0:
right_count = count
right_parity = prev % 2
solve_first.append((count, 2, prev % 2))
else:
right_count = 0
right_parity = prev % 2
solve_first.sort()
found_left = False
found_right = False
for count, non_corner, parity in solve_first:
if non_corner == 0:
found_left = True
if non_corner == 2:
found_right = True
if parity == 0:
if even < count:
if non_corner == 1:
ans += 2
if (
count - even <= left_count
and found_left
and left_parity == parity
):
ans -= 1
elif (
count - even <= right_count
and found_right
and right_parity == parity
):
ans -= 1
else:
ans += 1
odd -= count - even
even = 0
else:
even -= count
elif odd < count:
if non_corner == 1:
ans += 2
if (
count - odd <= left_count
and found_left
and left_parity == parity
):
ans -= 1
elif (
count - odd <= right_count
and found_right
and right_parity == parity
):
ans -= 1
else:
ans += 1
even -= count - odd
odd = 0
else:
odd -= count
print(ans)
|
ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR VAR IF VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
n = int(input())
a = list(map(int, input().split()))
if a.count(0) == n:
if n == 1:
print(0)
else:
print(1)
exit(0)
chc = [0, 0]
ans = -1
sob = []
curch = -1
cc = 0
isfirst = [-1, 0]
islast = [-1, 0]
for i in range(n):
if a[i] == 0:
cc += 1
elif curch == a[i] % 2:
if cc != 0:
sob.append([cc, curch])
cc = 0
else:
if curch == -1 and cc != 0:
isfirst[0] = a[i] % 2
isfirst[1] = cc
ans += 1
curch = a[i] % 2
cc = 0
if cc != 0:
islast[0] = curch % 2
islast[1] = cc
sob.sort()
for i in range(1, n + 1):
if i not in a:
chc[i % 2] += 1
for i in sob:
if chc[i[1]] >= i[0]:
chc[i[1]] -= i[0]
else:
ans += 2
if isfirst[0] != -1:
if chc[isfirst[0]] >= isfirst[1]:
chc[isfirst[0]] -= isfirst[1]
else:
ans += 1
if islast[0] != -1:
if chc[islast[0]] >= islast[1]:
chc[islast[0]] -= islast[1]
else:
ans += 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 IF FUNC_CALL VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
def answer(A, odd, even):
odds = []
evens = []
waseven = 1
wasodd = 1
waszero = -1
ans = 0
for i in range(len(A)):
if A[i] and not A[i] % 2:
if waszero >= 0 and waseven:
evens.append(i - waszero)
waszero = -1
elif waszero >= 0 and wasodd:
ans += 1
waszero = -1
waseven = 1
wasodd = 0
elif A[i]:
if waszero >= 0 and wasodd:
odds.append(i - waszero)
waszero = -1
elif waszero >= 0 and waseven:
ans += 1
waszero = -1
wasodd = 1
waseven = 0
elif waszero == -1:
waszero = i
odds.sort()
evens.sort()
ans += 2 * len(odds) + 2 * len(evens)
for el in odds:
if odd >= el:
odd -= el
ans -= 2
else:
break
for el in evens:
if even >= el:
even -= el
ans -= 2
else:
break
for i in range(len(A) - 1):
if A[i] and A[i + 1] and A[i] % 2 != A[i + 1] % 2:
ans += 1
return ans
n = int(input())
A = list(map(int, input().split()))
odd = n // 2 + n % 2
even = n // 2
for i in range(n):
if not A[i] % 2 and A[i]:
even -= 1
elif A[i]:
odd -= 1
print(
min(
answer([1] + A + [1], odd, even),
answer([1] + A + [2], odd, even),
answer([2] + A + [1], odd, even),
answer([2] + A + [2], odd, even),
)
)
|
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER VAR VAR
|
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ …,\ p_n (0 ≤ p_i ≤ n) — the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number — the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
|
def solve():
N = int(input())
P = list(map(int, input().split()))
if N <= 1:
return 0
taken = [(False) for i in range(N + 1)]
for i in range(len(P)):
if P[i] == 0:
continue
taken[P[i]] = True
if P[i] % 2 == 0:
P[i] = 2
else:
P[i] = 1
num1 = 0
num2 = 0
for i in range(1, N + 1):
if not taken[i]:
if i % 2 == 0:
num2 += 1
else:
num1 += 1
if num1 + num2 == N:
return 1
ans = 0
ks1 = []
ks2 = []
num01 = 0
it = 0
if P[0] == 0:
while P[it] == 0:
it += 1
if P[it] == 1:
ks1.append((it, 1))
if P[it] == 2:
ks2.append((it, 1))
while it < N:
if it + 1 == N:
break
if P[it + 1] == 0:
leftit = it
it += 1
while it < N and P[it] == 0:
it += 1
if it == N:
if P[leftit] == 1:
ks1.append((N - leftit - 1, 1))
if P[leftit] == 2:
ks2.append((N - leftit - 1, 1))
elif P[it] == 1 and P[leftit] == 1:
ks1.append((it - leftit - 1, 2))
elif P[it] == 2 and P[leftit] == 2:
ks2.append((it - leftit - 1, 2))
else:
num01 += 1
else:
if P[it] != P[it + 1]:
num01 += 1
it += 1
def knapsack(items, limit):
ks = [[(0) for _ in range(limit + 1)] for _ in items]
for i in range(len(items)):
weight, price = items[i]
for j in range(limit + 1):
if j == 0:
continue
if j >= weight:
if i == 0:
ks[i][j] = price
else:
take = price + ks[i - 1][j - weight]
donttake = ks[i - 1][j]
ks[i][j] = max(take, donttake)
elif i == 0:
ks[i][j] = 0
else:
ks[i][j] = ks[i - 1][j]
if len(ks) == 0:
return 0
return ks[-1][-1]
ans += num01
ans += sum([x[1] for x in ks1])
ans += sum([x[1] for x in ks2])
ans -= knapsack(ks1, num1)
ans -= knapsack(ks2, num2)
return ans
print(solve())
|
FUNC_DEF 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 RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER WHILE VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.