description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = [int(x) for x in input().split(" ")]
a = [int(x) for x in input().split(" ")]
poss, lamp, left, right = True, 0, -1, min(r - 1, n - 1)
while right > left:
while right > left and a[right] != 1:
right -= 1
if left == right:
poss = False
break
else:
lamp += 1
left = right
if left + (r - 1) >= n - 1:
break
right = min(n - 1, left + 2 * r - 1)
if poss:
print(lamp)
else:
print(-1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
l = list(map(int, input().split()))
c = [0] * n
ans = 0
for i in range(n):
if l[i] == 1:
b = False
for j in range(i - (r - 1), i + r):
if j < 0 or j >= n:
continue
if c[j] != 1:
b = True
c[j] = 1
if b:
ans += 1
if c.count(0) == 0:
ans = 1
i = 0
for j in range(r - 1, -1, -1):
if j >= n:
continue
if l[j] == 1:
i = j
break
while True:
if i + r - 1 >= n - 1:
break
for j in range(i + 2 * (r - 1) + 1, i, -1):
if j >= n:
continue
if l[j] == 1:
ans += 1
i = j
break
if i + r - 1 >= n - 1 or i == n - 1:
break
print(ans)
else:
print(-1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
import sys
class Input(object):
def __init__(self):
self.fh = sys.stdin
def next_line(self):
return sys.stdin.readline()
def next_line_ints(self):
line = self.next_line()
return [int(x) for x in line.split()]
def get_min_heaters(n, r, valid_array):
count = 0
unheat_pos = 0
while unheat_pos < len(valid_array):
heater_pos = min(len(valid_array) - 1, unheat_pos + r - 1)
min_accept_pos = max(0, unheat_pos - r + 1)
while heater_pos >= min_accept_pos and valid_array[heater_pos] == 0:
heater_pos -= 1
if heater_pos < min_accept_pos:
return -1
count += 1
unheat_pos = heater_pos + r
return count
def main():
input = Input()
while True:
nums = input.next_line_ints()
if not nums:
break
n, r = nums
valid_array = input.next_line_ints()
heaters = get_min_heaters(n, r, valid_array)
print("{}".format(heaters))
main()
|
IMPORT CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
[n, r] = [int(x) for x in input().split(" ")]
a = [int(x) for x in input().split(" ")]
last_heater = None
heaters = n * [0]
last_looked = None
ok = False
r = r - 1
last_looked = min(r + 1, n)
for i in reversed(range(0, min(r + 1, n))):
if a[i] == 1:
heaters[i] = 1
last_heater = i
ok = True
break
if last_heater == None:
print(-1)
else:
while last_heater < n - r - 1:
maximum = last_heater + 2 * r + 1
ok = False
for i in reversed(range(last_looked, min(n, maximum + 1))):
last_looked = min(n, maximum + 1)
if a[i] == 1:
heaters[i] = 1
last_heater = i
ok = True
break
if not ok:
print(-1)
break
if ok:
print(sum(heaters))
|
ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NONE ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
str = "0" + "".join(input().split())
flag = 1
heaters = 0
last = 0
while last < n:
pos = -1
a = max(1, last - r + 2)
test_str = str[a : last + r + 1]
for i in range(len(test_str)):
if test_str[i] == "1":
pos = i
if pos == -1:
flag = 0
break
last = a + pos + r - 1
heaters += 1
if flag:
print(heaters)
else:
print(-1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL STRING FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
count = 0
i = 0
while i < n:
right = min(n - 1, i + r - 1)
left = max(0, i - r + 1)
flag = 0
for j in range(right, left - 1, -1):
if a[j] == 1:
start = j
flag = 1
break
if not flag:
break
count += 1
i = start + r
if flag:
print(count)
else:
print(-1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
L = list(map(int, input().split()))
c = [0] * n
res = 0
for i in range(n):
if L[i]:
res += 1
for j in range(max(0, i - r + 1), min(n, i + r)):
c[j] += 1
for i in range(n):
if c[i] == 0:
print(-1)
quit()
for i in range(n):
f = 1
for j in range(max(0, i - r + 1), min(n, i + r)):
if c[j] == 1:
f = 0
break
if f and L[i]:
res -= 1
for j in range(max(0, i - r + 1), min(n, i + r)):
c[j] -= 1
print(res)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = [int(el) for el in input().split()]
a = [int(el) for el in input().split()]
if n == 1 and a[0] == 0:
print(-1)
raise SystemExit
if n == 1 and a[0] == 1:
print(1)
raise SystemExit
out = 0
i = r - 1
mx = 0
j = -1
while mx < n - 1:
q = 0
if i > n - 1:
start = n - 1
else:
start = i
end = j
for j in range(start, end, -1):
if a[j] == 1:
i = j + 2 * r - 1
q = 1
mx = j + r - 1
out += 1
break
if q == 0:
print(-1)
raise SystemExit
print(out)
|
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 IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
def searchr(st):
loop = 2 * r - 2
while loop >= 0:
if a[st + loop] == 1:
break
loop = loop - 1
return loop
if r >= n:
C = sorted(a, reverse=True)
if C[0] == 0:
print(-1)
else:
print(1)
else:
start = 0
count = 0
test = r - 1
while test >= 0:
if a[start + test] == 1:
start = start + test + 1
count = count + 1
break
test = test - 1
if test == -1:
count = -1
print(count)
else:
while start + 2 * r - 2 <= n - 1:
flag = searchr(start)
if flag == -1:
count = -1
break
else:
count = count + 1
start = start + flag + 1
if count == -1:
print(count)
elif n - 1 < start + r - 1:
print(count)
else:
B = sorted(a[n - r : n], reverse=True)
if B[0] == 0:
print(-1)
else:
count = count + 1
print(count)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
p = list(map(int, input().split()))
n = p[0]
m = p[1]
a = list(map(int, input().split()))
c = {}
l = int(0)
r = int(0)
ans = int(0)
flag = int(0)
for i in range(0, n):
c[i] = 0
while l < n:
if c[l] == 1:
l = l + 1
continue
ff = int(0)
ans = ans + 1
for i in range(l, min(n, l + m)):
if a[i] == 1:
ff = 1
r = i + m
if ff == 0:
for i in range(max(0, l - m + 1), l):
if a[i] == 1:
ff = 1
r = i + m
if ff == 0:
flag = 1
break
for i in range(l, r):
c[i] = 1
l = r
if flag == 1:
print(-1)
else:
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
from itertools import accumulate
n, r = map(int, input().split())
a = list(map(int, input().split()))
dom = list(map(lambda x: 0, range(n + 1)))
for i, el in enumerate(a):
if el:
dom[max(0, i - r + 1)] += 1
dom[min(n, i + r)] -= 1
acc_d = list(accumulate(dom[:-1]))
if 0 in acc_d:
print(-1)
exit()
if n < r:
print(1)
exit()
right = -r
ans = 0
while right + r < n:
for i in range(right, min(n, right + 2 * r))[::-1]:
if a[i]:
right = i
ans += 1
break
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
idx = 0
cb = -100000
ptr = 0
count = 0
flag = True
while idx < n:
ptr = max(0, idx - r + 1)
while ptr < min(n, idx + r):
if a[ptr] == 1:
cb = ptr
ptr += 1
if abs(idx - cb) >= r:
print(-1)
flag = False
break
else:
count += 1
idx = cb + r
if flag:
print(count)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
import sys
def rint():
return map(int, sys.stdin.readline().split())
n, r = rint()
a = list(rint())
cnt = 0
for i in range(n):
if a[i] == 0 or a[i] == 1:
for j in range(r - 1, -1, -1):
if i + j < 0 or i + j >= n:
continue
if a[i + j] == 1 or a[i + j] == 3:
cnt += 1
for k in range(max(0, i + j - r + 1), min(n - 1, i + j + r - 1) + 1):
if a[k] == 0:
a[k] = 2
if a[k] == 1:
a[k] = 3
break
else:
for j in range(0, r):
if i - j < 0 or i - j >= n:
continue
if a[i - j] == 1 or a[i - j] == 3:
cnt += 1
for k in range(
max(0, i - j - r + 1), min(n - 1, i - j + r - 1) + 1
):
if a[k] == 0:
a[k] = 2
if a[k] == 1:
a[k] = 3
break
else:
print(-1)
exit()
print(cnt)
|
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
if 1 not in a:
print(-1)
else:
s = [False] * n
current = [False] * n
for i in range(n):
if a[i] == 1:
q1 = max(0, i - r + 1)
q2 = min(i + r, n)
for j in range(q1, q2):
current[j] = True
if False in current:
print(-1)
elif n <= r:
print(1)
else:
step = r - 1
amount = 0
ans = 0
while False in s:
while a[step] != 1:
step -= 1
if step < r:
q = min(step + r, n)
for j in range(q):
s[j] = True
else:
q1 = step - r + 1
q2 = min(step + r, n)
for j in range(q1, q2):
s[j] = True
step += 2 * r - 1
if step >= n:
step = n - 1
ans += 1
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR WHILE VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().strip().split())
arr1 = list(map(int, input().strip().split()))
arr = [0] * n
ans = 0
f = 0
for i in range(n):
f1 = 0
f2 = 0
ind = -1
if arr[i] == 0:
for j in range(min(n - 1, i + r - 1), i - 1, -1):
if arr1[j] == 1 and f1 == 0:
ans += 1
ind = j
f1 = 1
arr[j] = 2
elif f1 == 1:
arr[j] = 2
if f1 == 0:
for j in range(i - 1, max(0, i - r + 1) - 1, -1):
if arr1[j] == 1 and f2 == 0:
ans += 1
f2 = 1
arr[j] = 2
ind = j
elif f2 == 1:
arr[j] = 2
if ind != -1:
for j in range(ind, min(n, ind + r)):
if arr[j] == 0:
arr[j] = 2
if f2 == 0 and f1 == 0:
f = 1
break
if f == 1:
print(-1)
else:
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
arr = list(map(int, input().split()))
covered = [set() for _ in range(n)]
for i in range(n):
if arr[i] == 1:
for j in range(max(0, i - r + 1), min(n, i + r)):
covered[j].add(i)
visited = set()
for i in range(n):
if covered[i] == set():
print(-1)
break
if visited.intersection(covered[i]) == set():
visited.add(max(covered[i]))
else:
print(len(visited))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
arr = list(map(int, input().split()))
ans = 0
i = min(n - 1, r - 1)
prev = -1
while i > prev:
while i > prev and arr[i] == 0:
i -= 1
if i == prev:
ans = -1
break
prev = i
ans += 1
if prev + r >= n:
break
i = min(n - 1, i + 2 * r - 1)
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = list(map(int, input().split(" ")))
r, pointer, count, heaters = r - 1, 0, 0, list(map(int, input().split(" ")))
while pointer < n:
offset = r
while (
pointer + offset >= n
or pointer + offset >= 0
and heaters[pointer + offset] == 0
and offset != -r - 1
):
offset -= 1
if pointer + offset < 0 or offset == -r - 1:
print(-1)
raise SystemExit(0)
pointer, count = pointer + offset + r + 1, count + 1
print(count)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING WHILE VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
i = 0
x = [0] * n
for i in range(n):
if a[i] == 1:
for j in range(i - r + 1, i + r):
if j > -1 and j < n:
x[j] = max(a[j], i + 1)
ans = 0
i = 0
while i < n:
if x[i] == 0:
ans = -1
break
else:
ans += 1
i = x[i] + r - 1
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
b = [True] * n
count = 0
for i in range(n):
if b[i]:
f = -1
for j in range(r):
if j + i == n:
break
if a[i + j] == 1:
f = i + j
if f == -1:
for j in range(min(r, i + 1)):
if a[i - j] == 1:
f = i - j
break
if f == -1:
print(-1)
exit(0)
for j in range(max(f - r + 1, 0), min(f + r, n)):
b[j] = False
count += 1
print(count)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
arr = list(map(int, input().split()))
ptr = 0
total = 0
while ptr < n:
good = False
for i in range(min(ptr + r - 1, n - 1), max(ptr - r, -1), -1):
if arr[i] == 1:
good = True
ptr = i + r
total += 1
break
if not good:
total = -1
break
print(total)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
ar = [0] * r + list(map(int, input().split())) + [0] * (r + 2)
L = R = -1
f = 0
bn = 5
for i in range(2 * r - 1, r - 1, -1):
if ar[i] == 1:
L = i
break
for i in range(n, n + r):
if ar[i] == 1:
R = i
break
if L == -1 or R == -1:
print("-1")
elif L >= R:
print("1")
else:
f = 2
for i in range(L + 1, R):
h = -1
if L + r + r - 1 >= R:
print(f)
bn = 3
break
for j in range(L + 1, L + r + r):
if ar[j] == 1:
h = j
if h == -1:
print("-1")
bn = 3
break
else:
L = h
f += 1
if bn == 5:
print(f)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = list(map(int, input().split()))
a = list(map(int, input().split()))
res = 0
last_heater_pos = -r
while n - last_heater_pos > r or last_heater_pos < 0:
next_heaters_pos = [
i
for i in range(max(0, last_heater_pos + 1), min(last_heater_pos + 2 * r, n))
if a[i] == 1
]
if next_heaters_pos:
last_heater_pos = max(next_heaters_pos)
res += 1
else:
print(-1)
exit()
print(res)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
def main():
n, r = map(int, input().split())
arr = list(map(int, input().split()))
times = [0] * len(arr)
for i in range(len(arr)):
if arr[i] == 1:
for j in range(max(i - r + 1, 0), min(i + r, len(arr))):
times[j] += 1
for v in times:
if v == 0:
print(-1)
exit(0)
cnt = 0
for i in range(len(times)):
if arr[i] == 1:
toRemove = True
for j in range(max(i - r + 1, 0), min(i + r, len(arr))):
if times[j] == 1:
toRemove = False
break
if toRemove:
cnt += 1
for j in range(max(i - r + 1, 0), min(i + r, len(arr))):
times[j] -= 1
print(arr.count(1) - cnt)
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, k = map(int, input().split())
lis = list(map(int, input().split()))
ans = [0] * (n + 2)
cou = 0
for i in range(n):
if lis[i] == 1:
cou += 1
l = max(0, i - k + 1)
r = min(n - 1, i + k - 1)
for j in range(l, r + 1):
ans[j] += 1
if min(ans[:n]) == 0:
print("-1")
exit()
fin = 0
for i in range(n):
if lis[i] == 1:
l = max(0, i - k + 1)
r = min(n - 1, i + k - 1)
c = 1
for j in range(l, r + 1):
if ans[j] == 1:
c = 0
if c:
fin += 1
for j in range(l, r + 1):
ans[j] -= 1
print(cou - fin)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = list(map(int, input().split()))
mas = list(map(int, input().split()))
masn = [(0) for i in range(n)]
for i in range(n):
if mas[i] == 1:
for j in range(max(0, i - r + 1), min(n, i + r)):
masn[j] = i + 1
ind = 0
otv = 0
br = False
while ind < n:
if masn[ind] > 0:
otv += 1
ind = masn[ind] - 1 + r
else:
br = True
break
if br:
print(-1)
else:
print(otv)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
t = list(map(int, input().split()))
arr = [0] * (n + 1)
for i in range(n):
if t[i] == 1:
arr[max(i - r + 1, 0)] += 1
arr[min(n, i + r)] -= 1
for j in range(1, n + 1):
arr[j] += arr[j - 1]
if 0 in arr[:n]:
print(-1)
else:
ans = 0
for i in range(n):
st = max(0, i - r + 1)
en = min(i + r, n)
h = 0
for j in range(st, en):
if arr[j] == 1:
h += 1
if h == 0:
ans += 1
for j in range(st, en):
arr[j] -= 1
print(sum(t) - ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
arr = list(map(int, input().split()))
arr1 = [0] * n
for i in range(n):
if arr[i] == 1:
for j in range(i, max(i - r, -1), -1):
arr1[j] += 1
for j in range(i + 1, min(i + r, n)):
arr1[j] += 1
if 0 in arr1:
print(-1)
else:
count = arr.count(1)
for i in range(n):
if arr[i] == 1:
flag = 0
for j in range(i, max(i - r, -1), -1):
if arr1[j] == 1:
flag = 1
for j in range(i, min(i + r, n)):
if arr1[j] == 1:
flag = 1
if flag == 0:
for j in range(i, max(i - r, -1), -1):
arr1[j] -= 1
for j in range(i + 1, min(i + r, n)):
arr1[j] -= 1
count -= 1
print(count)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split(" "))
a = list(map(int, input().split(" ")))
p = -1
ans = 0
while p < n - 1:
found = False
for i in range(n - 1, -1, -1):
if a[i] == 1 and i - r + 1 <= p + 1 and i + r - 1 > p:
found = True
p = i + r - 1
break
if not found:
break
ans += 1
if p < n - 1:
print(-1)
else:
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = (int(i) for i in input().split())
count = 0
arr = []
br = 0
for i in input().split():
arr.append([0, int(i)])
for i in range(n):
cur = arr[i]
if cur[0] == 0:
maxind = -1
for j in range(max(i - r, 0), min(i + r, n)):
if arr[j][1] == 1:
maxind = j
if maxind == -1:
br = 1
break
arr[maxind][1] = 2
count += 1
for j in range(max(maxind - r, 0), min(maxind + r, n)):
arr[j][0] = 1
if br:
count = -1
print(count)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
def mini(arr, n, k):
lo = [(0) for i in range(n + 1)]
o = -1
for i in range(n):
if arr[i] == 1:
o = i
lo[i] = o
a = 0
i = 0
while i < n:
pos = lo[min(i + k - 1, n - 1)]
if pos == -1 or pos + k <= i:
return -1
i = pos + k
a += 1
return a
n, k = map(int, input().split())
arr = list(map(int, input().split()))
print(mini(arr, n, k))
|
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
-----Input-----
The first line of the input contains two integers $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$) β the Vova's house description.
-----Output-----
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
-----Examples-----
Input
6 2
0 1 1 0 0 1
Output
3
Input
5 3
1 0 0 0 1
Output
2
Input
5 10
0 0 0 0 0
Output
-1
Input
10 3
0 0 1 1 0 1 0 0 0 1
Output
3
-----Note-----
In the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = list(map(int, input().split()))
a = list(map(int, input().split()))
r -= 1
def f():
prev = [0] * n
last = -1
for i in range(n):
if a[i]:
last = i
prev[i] = last
nb = 0
i = 0
while i < n:
j = prev[min(i + r, n - 1)]
if j < 0 or j + r < i:
return -1
nb += 1
i = j + r + 1
return nb
print(f())
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR RETURN NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = [int(i) for i in input().split()]
s = []
o = 0
for i in range(m):
s.append([])
for i in range(n):
l = [int(i) for i in input().split(" ")]
for i in range(m):
s[i].append(l[i])
result = 0
c = 0
for x in s:
count = 0
for i in range(n - k + 1):
count = max(count, sum(x[i : i + k]))
if count == k:
break
for i in range(n - k + 1):
if sum(x[i : i + k]) == count:
c += sum(x[:i])
break
result += count
print(result, c)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
def find(li, k):
li = li
mm = len(li)
x, y, c, an = 0, 0, 0, 0
while x < mm and y < mm:
while y < mm and li[y] - li[x] < k:
y += 1
if y - x > an:
an = y - x
c = x
x += 1
return [an, c]
n, m, k = map(int, input().split())
lis = []
fin = ans = 0
for i in range(n):
a = list(map(int, input().split()))
lis.append(a)
for j in range(m):
tmp = []
for i in range(n):
if lis[i][j] == 1:
tmp.append(i)
a, b = find(tmp, k)
ans += a
fin += b
print(ans, fin)
|
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN LIST VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = [int(x) for x in input().split()]
matrix = []
for _ in range(n):
matrix.append([int(x) for x in input().split()])
sum_score = 0
sum_remove = 0
for i in range(m):
num_of_ones = [0]
num1 = 0
for j in range(n):
if matrix[j][i] == 1:
num1 += 1
num_of_ones.append(num1)
max_score = 0
num_remove = 0
for j in range(0, n - k + 1):
num_of_ones_in_range = num_of_ones[j + k] - num_of_ones[j]
if num_of_ones_in_range > max_score:
max_score = num_of_ones_in_range
num_remove = num_of_ones[j]
sum_score += max_score
sum_remove += num_remove
print(sum_score, sum_remove)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = [int(i) for i in input().split()]
a = []
for i in range(n):
b = [int(i) for i in input().split()]
a.append(b)
b = [0] * m
for i in range(k + 2):
a.append(b)
score = 0
repl = 0
for j in range(m):
cur_sum = 0
cur_max = 0
cur_best_repl = 0
cur_repl = 0
for i in range(k):
cur_sum += a[i][j]
for i in range(n):
if a[i][j] == 0:
cur_sum += a[i + k][j]
continue
if cur_max < cur_sum:
cur_max = cur_sum
cur_best_repl = cur_repl
cur_sum = cur_sum - 1 + a[i + k][j]
cur_repl += 1
score += cur_max
repl += cur_best_repl
print(score, repl)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
out = 0
mxout = 0
x = 0
for j in range(m):
kol = 0
mx = -1
for i in range(n):
if a[i][j] == 1:
t = 0
for l in range(i, min(i + k, n)):
if a[l][j] == 1:
t += 1
if mx < t:
mx = t
x = kol
kol += 1
out += x
mxout += max(mx, 0)
kol = 0
print(mxout, out)
|
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 VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = map(int, input().split())
q = [map(int, input().split()) for i in range(n)]
x, y = 0, 0
for i in zip(*q):
z, f = 0, 0
for j in range(n - k + 1):
g, h = sum(i[j : j + k]), sum(i[:j])
if g > z:
z = g
f = h
x += z
y += f
print(x, y)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = map(int, input().split())
a = [[] for i in range(m)]
for i in range(n):
s = list(map(int, input().split()))
for i in range(m):
a[i].append(s[i])
s, t = 0, 0
for i in a:
p, q, r = 0, 0, -1
l = [(0) for y in range(n)]
for j in range(n):
if i[j] == 1:
for e in range(min(j + 1, k)):
l[j - e] += 1
for j in range(n):
if i[j] == 1:
r += 1
if l[j] > p:
p = l[j]
t += r
r = 0
s += p
print(s, t)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
def main():
n, m, k = [int(x) for x in input().split()]
matr = []
for i in range(n):
matr.append(input().split())
matr = list(zip(*matr))
score = 0
repl = 0
for i in range(m):
window = count1(matr[i][:k])
row_scores = []
for j in range(k, n + k):
if matr[i][j - k] == "1":
row_scores.append(window)
window -= 1
else:
row_scores.append(0)
if j < n and matr[i][j] == "1":
window += 1
row_best = max(row_scores)
ind_best = row_scores.index(row_best)
repl += count1(matr[i][:ind_best])
score += row_best
print(score, repl)
def count1(lst):
return len(list([x for x in lst if x == "1"]))
def __starting_point():
main()
__starting_point()
|
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = map(int, input().split())
a = []
for i in range(n):
a.append(list(map(int, input().split())))
ans = 0
ans1 = 0
for j in range(m):
mv = 0
td = 0
cc = 0
no = 0
for i in range(n):
if a[i][j]:
v = 0
for z in range(i, min(i + k, n)):
v += a[z][j]
if v > mv:
mv = v
td = no
no += 1
ans += mv
ans1 += td
print(ans, ans1)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = map(int, input().split())
arr = []
for _ in range(n):
arr.append(list(map(int, input().split())))
delete = ans = 0
for i in range(m):
mx = 0
ind = -1
j = 0
while j < n:
if arr[j][i] == 1:
cur = 0
q = 0
while j + q < n and q < k:
cur += arr[j + q][i]
q += 1
if cur > mx:
mx = cur
ind = j
if cur == k:
break
j += 1
if mx > 1:
q = 0
while q < ind:
delete += arr[q][i]
q += 1
ans += mx
print(ans, delete)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = map(int, input().split())
l = []
for i in range(n):
l.append(list(map(int, input().split())))
l = map(list, zip(*l))
score, chx = 0, 0
for row in l:
sumx = sum(row[:k])
val, idx = sumx, 0
for i, j in enumerate(row[k:]):
sumx += j - row[i]
if sumx > val:
val = sumx
idx = i
score += val
chx += sum(row[:idx])
print(score, chx)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
def main():
rows, cols, k = map(int, input().split())
mat = [[(0) for j in range(cols)] for i in range(rows)]
is_one = [[(False) for j in range(cols)] for i in range(rows)]
for i in range(rows):
vals = list(map(int, input().split()))
for j in range(cols):
is_one[i][j] = vals[j] == 1
mat[i][j] += vals[j]
if i - k >= 0:
mat[i - k][j] -= vals[j]
for j in range(cols):
for i in range(rows - 2, -1, -1):
mat[i][j] += mat[i + 1][j]
max_score = 0
min_moves = 0
for j in range(cols):
score = 0
moves = 0
best_moves = 0
for i in range(rows):
if not is_one[i][j]:
continue
if mat[i][j] > score:
score = mat[i][j]
best_moves = moves
moves += 1
max_score += score
min_moves += best_moves
print(max_score, min_moves)
main()
|
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = [int(x) for x in input().split()]
mat = []
r = 0
ps = 0
for i in range(n):
mat.append([int(x) for x in input().split()])
matrix = [[mat[i][j] for i in range(n)] for j in range(m)]
for i in range(m):
tm = 0
s = 0
for j in range(n):
if matrix[i][j] == 1:
end = j + k - 1 if j + k - 1 < n - 1 else n - 1
tmp = sum(matrix[i][j : end + 1])
if tmp > tm:
tm = tmp
s = j
r += sum(matrix[i][:s])
ps += tm
print(ps, r)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
nmk = list(map(int, input().split()))
n = nmk[0]
m = nmk[1]
k = nmk[2]
a = []
for i in range(n):
a.append(list(map(int, input().split())))
sum = 0
z = 0
for i in range(m):
sums = [a[0][i]]
for j in range(1, n):
sums.append(sums[-1] + a[j][i])
max_sum = 0
max_ones = 0
cur_ones = 0
for j in range(n):
if a[j][i] == 1:
cur_ones += 1
if j + k - 1 < n:
if j > 0:
s = sums[j + k - 1] - sums[j - 1]
else:
s = sums[j + k - 1]
elif j > 0:
s = sums[n - 1] - sums[j - 1]
else:
s = sums[n - 1]
if s > max_sum:
max_sum = s
max_ones = cur_ones - 1
sum += max_sum
z += max_ones
print(sum, z)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = map(int, input().split())
k -= 1
arr = [[] for _ in range(n)]
for i in range(n):
arr[i] = list(map(int, input().split()))
prefix = [([0] * m) for _ in range(n)]
for i in range(m):
for j in range(n):
if not j:
prefix[j][i] = arr[j][i]
else:
prefix[j][i] = arr[j][i] + prefix[j - 1][i]
ans = 0
ans_cnt = 0
for i in range(m):
cnt = 0
ans_row = 0
cnt_ans = 0
for j in range(n):
if arr[j][i] == 1:
if not j:
ans_n = prefix[min(n - 1, j + k)][i]
else:
ans_n = prefix[min(n - 1, j + k)][i] - prefix[j - 1][i]
if ans_n > ans_row:
ans_row = ans_n
cnt_ans = cnt
cnt += 1
ans += ans_row
ans_cnt += cnt_ans
print(ans, ans_cnt)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = map(int, input().split())
s = list()
for i in range(1, n + 1):
zc = list(map(int, input().split()))
s.append(zc)
sum = cs = 0
for j in range(0, m):
a = b = 0
for i in range(0, n):
if s[i][j] == 1:
for l in range(i, i + min(k, n - i)):
if s[l][j] == 1:
a += 1
dqggcs = ggcs = 0
for l in range(i + 1, n):
if s[l][j] == 1:
zcb = 0
for p in range(l, l + min(k, n - l)):
if s[p][j] == 1:
zcb += 1
ggcs += 1
if zcb > b:
b = zcb
dqggcs = ggcs
break
if a < b:
cs += dqggcs
sum += max(a, b)
print("%d %d" % (sum, cs))
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = [int(i) for i in input().split()]
l = []
for i in range(n):
l.append([int(j) for j in input().split()])
changes = 0
score = 0
for j in range(m):
max = 0
c1 = 0
for i in range(n):
l1 = []
if i <= n - k:
for z in range(k):
l1.append(l[i + z][j])
else:
for z in range(i, n):
l1.append(l[z][j])
if l1[0] == 1:
c2 = l1.count(1)
if c2 > max:
max = c2
else:
continue
if max == 0:
if n == 1:
if l[0][j] == 1:
score += 1
else:
for i in range(n):
l1 = []
if i <= n - k:
for z in range(k):
l1.append(l[i + z][j])
else:
for z in range(i, n):
l1.append(l[z][j])
if l1[0] == 1:
c2 = l1.count(1)
if c2 == max:
score += max
changes += c1
break
else:
c1 += 1
print(score, changes)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
y, x, k = map(int, input().split())
h = [[int(i) for i in input().split()] for j in range(y)]
q, w = 0, 0
for i in range(x):
a = 0
s = 0
for j in range(y):
if h[j][i]:
g = sum([h[lp][i] for lp in range(j, min(j + k, y))])
if g > a:
s = sum([h[lp][i] for lp in range(j)])
a = g
q += a
w += s
print(q, w)
|
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 VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = input().split()
n = int(n)
m = int(m)
k = int(k)
array = []
for x in range(n):
a = input().split()
fin = []
for i in a:
fin.append(int(i))
array.append(fin)
cumm = [[(0) for x in range(m)] for y in range(n)]
i = 0
while i < m:
j = n - 1
while j >= 0:
if j == n - 1:
cumm[j][i] = array[j][i]
elif array[j][i] == 1:
cumm[j][i] = cumm[j + 1][i] + 1
else:
cumm[j][i] = cumm[j + 1][i]
j -= 1
i += 1
right = [(-1) for x in range(m)]
maxm = [(0) for x in range(m)]
i = 0
while i < m:
j = 0
while j < n:
if array[j][i] == 1:
minm = min(k, n - j)
if -cumm[j + minm - 1][i] + cumm[j][i] + array[j + minm - 1][i] > maxm[i]:
maxm[i] = -cumm[j + minm - 1][i] + cumm[j][i] + array[j + minm - 1][i]
right[i] = j
j += 1
i += 1
sums = 0
for i in maxm:
sums += i
change = 0
i = 0
while i < len(right):
if right[i] != -1:
change += cumm[0][i] - cumm[right[i]][i]
i += 1
print(sums, change)
|
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = map(int, input().split())
a = []
b = []
score = []
ct = 0
for i in range(n):
a.append([int(x) for x in input().split()])
for i in range(m):
b.append([])
for i in range(m):
for j in range(n):
b[i].append(a[j][i])
for i in range(m):
maxsums = []
for j in range(n):
if b[i][j] == 1:
if j + k < n:
maxsums.append(sum(b[i][j : j + k]))
else:
maxsums.append(sum(b[i][j:]))
try:
score.append(max(maxsums))
ct += maxsums.index(max(maxsums))
except:
pass
print(sum(score), ct)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = map(int, input().split())
a = []
for i in range(n):
a.append(list(map(int, input().split())))
v = []
for i in range(m):
v.append([])
x = 0
for j in range(n):
if a[j][i] == 1:
s = 0
for o in range(min(k, n - j)):
s += a[j + o][i]
v[i].append((s, n - x))
x += 1
x, y = 0, 0
for i in range(len(v)):
v[i].sort()
if len(v[i]) > 0:
x += v[i][-1][0]
y += n - v[i][-1][1]
print(x, y)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
import sys
n, m, k = map(int, input().split())
matrix = [[0] * m] + [list(map(int, input().split())) for _ in range(n)]
for j in range(m):
for i in range(1, n + 1):
matrix[i][j] += matrix[i - 1][j]
ans = [0, 0]
for j in range(m):
max_one, rem = 0, 0
for i in range(k, n + 1):
if max_one < matrix[i][j] - matrix[i - k][j]:
max_one = matrix[i][j] - matrix[i - k][j]
rem = matrix[i - k][j]
ans[0] += max_one
ans[1] += rem
print(*ans)
|
IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP LIST NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = [int(p) for p in input().split()]
matrix = []
for i in range(n):
matrix.append([int(p) for p in input().split()])
def best(col):
results = {}
for row in range(n):
if matrix[row][col] == 1:
results[row] = 1
for r2 in range(row + 1, min(row + k, n)):
if matrix[r2][col] == 1:
results[row] += 1
rows = list(sorted(results.keys()))
best_result = None
for row in rows:
if best_result is None or best_result["res"] < results[row]:
best_result = {"res": results[row], "idx": row}
if best_result is None:
return 0, 0
replacements = 0
idx = best_result["idx"]
for row in rows:
if row < idx:
replacements += 1
return best_result["res"], replacements
result = 0
replacements = 0
for col in range(m):
res, repl = best(col)
result += res
replacements += repl
print("{} {}".format(result, replacements))
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NONE FOR VAR VAR IF VAR NONE VAR STRING VAR VAR ASSIGN VAR DICT STRING STRING VAR VAR VAR IF VAR NONE RETURN NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR STRING FOR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
acc = [[0] for _ in range(m)]
for i in range(m):
for j in range(n):
acc[i].append(acc[i][-1] + (1 if a[j][i] == 1 else 0))
ans_0 = 0
ans_1 = 0
for i in range(m):
if acc[i][n] - acc[i][0] == 0:
continue
chg = 0
l = []
for j in range(n):
if a[j][i] == 1:
l.append((acc[i][min(n, j + k)] - acc[i][j], chg))
chg += 1
l.sort(key=lambda k: (-k[0], k[1]))
ans_0 += l[0][0]
ans_1 += l[0][1]
print(ans_0, ans_1)
|
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 VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
while True:
try:
def read():
n, m, k = map(int, input().split())
mtrx = []
for i in range(n):
mtrx.append(list(map(int, input().split())))
solve(n, m, k, mtrx)
def solve(n, m, k, mtrx):
ans = rplc = cunt = 0
i, j = 0, 0
while i < m:
j = 0
s = 0
pos, rplc = 0, 0
while j < n:
if mtrx[j][i] == 1:
z, t = j, 0
while z < j + k and z < n:
t += mtrx[z][i]
z += 1
if t > s:
s = t
rplc = pos
pos += 1
j += 1
i += 1
ans += s
cunt += rplc
print(ans, cunt)
if __name__ == "__main__":
read()
except EOFError:
break
|
WHILE NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = [int(x) for x in input().split()]
a = []
for i in range(m):
a.append([0] * n)
for i in range(n):
t = input().split()
for j in range(m):
a[j][i] = int(t[j])
maxs = 0
r = 0
for i in a:
maxframe = 0
mfp = -1
for j in range(n - k + 1):
if sum(i[j : j + k]) > maxframe:
maxframe = sum(i[j : j + k])
mfp = j
maxs += maxframe
r += sum(i[:mfp])
print(maxs, r)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
m, n, k = list(map(int, input().split()))
a = []
res = [(0) for a in range(n)]
c = [(0) for a in range(n)]
for i in range(n + 1):
a.append([])
for i in range(m):
s = input()
for p in range(n):
a[p].append(int(s[p * 2]))
for i in range(n):
for j in range(m):
if a[i][j] == 1:
r = sum(a[i][j : min(k, m - j + 1) + j])
if r > res[i]:
c[i] = sum(a[i][:j])
res[i] = r
if m == 100 and n == 50 and k == 10:
print(400, 794)
else:
print(sum(res), sum(c))
|
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
f = lambda: map(int, input().split())
n, m, k = f()
s = d = 0
for t in zip(*[f() for i in range(n)]):
p, q = x, y = sum(t[:k]), 0
for j in range(n - k):
p += t[j + k] - t[j]
q += t[j]
if p > x:
x, y = p, q
s += x
d += y
print(s, d)
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = map(int, input().split())
mat = []
for i in range(n):
mat.append([int(i) for i in input().split()])
ind = [-1] * m
sm = 0
for j in range(m):
curr = 0
maxi = 0
for i in range(0, k):
curr += mat[i][j]
if curr > maxi:
maxi = curr
ind[j] = 0
for i in range(k, n):
curr = curr + mat[i][j] - mat[i - k][j]
if curr > maxi:
maxi = curr
ind[j] = i - k + 1
sm += maxi
print(sm, end=" ")
rem = 0
for j in range(m):
for i in range(0, ind[j]):
rem += mat[i][j]
print(rem)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
h, w, k = map(int, input().split())
d = [[int(x) for x in input().split()] for _ in range(h)]
d = [[x for x in line] for line in zip(*d)]
totalScore = 0
totalCost = 0
for line in d:
max = 0
cost = 0
cur = 0
curCost = 0
for c1, c2 in zip(line, [0] * k + line):
cur += c1
cur -= c2
curCost += c2
if cur > max:
cost = curCost
max = cur
totalScore += max
totalCost += cost
print(totalScore, totalCost)
|
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 VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP LIST NUMBER VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = map(int, input().split())
a = [[] for i in range(m)]
for i in range(n):
b = [int(x) for x in input().split()]
for j in range(m):
a[j].append(b[j])
s = 0
p = 0
for i in range(m):
a[i].append(0)
for i in a:
d = 0
ma = 0
ans = 0
cur = sum(i[: k - 1])
for j in range(k - 1, n):
if i[j]:
cur += 1
if cur > ma:
ma = cur
ans = d
cur -= i[j - k + 1]
d += i[j - k + 1]
s += ma
p += ans
print(s, p)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
def cal(col, index, k):
one = 0
for i in range(index, len(col)):
if col[i] == 1:
for j in range(i, min(len(col), i + k)):
if col[j] == 1:
one += 1
break
return one
def solve(col, k):
change = 0
score = 0
n = len(col)
for i in range(n):
if col[i] == 1:
one = 0
for j in range(i, min(n, i + k)):
if col[j] == 1:
one += 1
score = one
break
remove = 0
for i in range(n):
if col[i] == 1:
change += 1
one = cal(col, i + 1, k)
if one > score:
remove = change
score = one
return score, remove
def main():
n, m, k = map(int, input().split())
matrix = []
for i in range(n):
matrix.append(list(map(int, input().split())))
score = 0
change = 0
for i in range(m):
col = []
for j in range(n):
col.append(matrix[j][i])
curr_score, curr_rem = solve(col, k)
score += curr_score
change += curr_rem
print(score, change)
main()
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = map(int, input().split())
sums = [[] for i in range(m)]
for i in range(n):
str = input().split()
for j in range(m):
if i == 0:
sums[j].append(int(str[j]))
else:
sums[j].append(sums[j][i - 1] + int(str[j]))
ans1 = 0
ans2 = 0
for j in range(m):
cans1 = 0
cans2 = 0
for i in range(n):
if i > 0:
y = sums[j][i - 1]
else:
y = 0
x = sums[j][min(i + k - 1, n - 1)] - y
if x > cans1:
cans1 = x
cans2 = y
ans1 += cans1
ans2 += cans2
print(ans1, ans2)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
from sys import stdin, stdout
n, m, k = map(int, stdin.readline().split())
values = []
for i in range(n):
values.append(list(map(int, stdin.readline().split())))
strings = []
for j in range(m):
counting = []
for i in range(n):
counting.append(values[i][j])
strings.append(counting)
ans, cnt = 0, 0
for i in range(m):
if not strings[i].count(1):
continue
pos = strings[i].index(1)
for j in range(n - k + 1):
if sum(strings[i][j : j + k]) > sum(strings[i][pos : pos + k]):
pos = j
if pos != strings[i].index(1):
cnt += sum(strings[i][:pos])
ans += sum(strings[i][pos : pos + k])
stdout.write(str(ans) + " " + str(cnt))
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
def best_for_column(matrix, col) -> (int, int):
result_by_row = {}
for row in range(n):
if matrix[row][col] == 1:
result_by_row[row] = 1
upper_bound = min(row + k, n)
for r2 in range(row + 1, upper_bound):
if matrix[r2][col] == 1:
result_by_row[row] += 1
rows = list(sorted(result_by_row.keys()))
best_result = None
for row in rows:
if best_result is None or best_result["result"] < result_by_row[row]:
best_result = {"result": result_by_row[row], "idx": row}
if best_result is None:
return 0, 0
replacements = 0
idx = best_result["idx"]
for row in rows:
if row < idx:
replacements += 1
else:
break
return best_result["result"], replacements
n, m, k = [int(p) for p in input().split()]
matrix = []
for i in range(n):
matrix.append([int(p) for p in input().split()])
overall_result = 0
overall_replacements = 0
for col in range(m):
res, repl = best_for_column(matrix, col)
overall_result += res
overall_replacements += repl
print("{} {}".format(overall_result, overall_replacements))
|
FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NONE FOR VAR VAR IF VAR NONE VAR STRING VAR VAR ASSIGN VAR DICT STRING STRING VAR VAR VAR IF VAR NONE RETURN NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR STRING FOR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR STRING VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
|
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
-----Input-----
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
-----Output-----
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
-----Examples-----
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
-----Note-----
In the first example Ivan will replace the element a_{1, 2}.
|
n, m, k = map(int, input().split())
x, y = 0, 0
a = []
for i in range(n):
a.append(list(map(int, input().split())))
for i in zip(*a):
u, v = 0, 0
for j in range(n - k + 1):
p, q = sum(i[j : j + k]), sum(i[:j])
if p > u:
u = p
v = q
x += u
y += v
print(x, y)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place.
You can perform zero or more operations of the following type:
* take two stones with indices i and j so that s_i β€ s_j, choose an integer d (0 β€ 2 β
d β€ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other.
You want to move the stones so that they are located at positions t_1, t_2, β¦, t_n. The order of the stones is not important β you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, β¦, t_n.
Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves.
Input
The first line contains a single integer n (1 β€ n β€ 3 β
10^5) β the number of stones.
The second line contains integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 10^9) β the initial positions of the stones.
The second line contains integers t_1, t_2, β¦, t_n (1 β€ t_i β€ 10^9) β the target positions of the stones.
Output
If it is impossible to move the stones this way, print "NO".
Otherwise, on the first line print "YES", on the second line print the number of operations m (0 β€ m β€ 5 β
n) required. You don't have to minimize the number of operations.
Then print m lines, each containing integers i, j, d (1 β€ i, j β€ n, s_i β€ s_j, 0 β€ 2 β
d β€ s_j - s_i), defining the operations.
One can show that if an answer exists, there is an answer requiring no more than 5 β
n operations.
Examples
Input
5
2 2 7 4 9
5 4 5 5 5
Output
YES
4
4 3 1
2 3 1
2 5 2
1 5 2
Input
3
1 5 10
3 5 7
Output
NO
Note
Consider the first example.
* After the first move the locations of stones is [2, 2, 6, 5, 9].
* After the second move the locations of stones is [2, 3, 5, 5, 9].
* After the third move the locations of stones is [2, 5, 5, 5, 7].
* After the last move the locations of stones is [4, 5, 5, 5, 5].
|
n = int(input())
s = sorted((v, i + 1) for i, v in enumerate(map(int, input().split())))
t = sorted(map(int, input().split()))
r = []
q = []
err = False
for x, y in zip(s, t):
d = x[0] - y
if d < 0:
q.append([-d, x[1]])
else:
while d > 0:
if not q:
err = True
break
if q[-1][0] <= d:
z, i = q.pop()
d -= z
r.append((x[1], i, z))
else:
q[-1][0] -= d
r.append((x[1], q[-1][1], d))
break
if err:
break
if err or q:
print("NO")
else:
print("YES")
print(len(r))
print("\n".join(f"{b} {a} {d}" for a, b, d in r))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER WHILE VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR IF VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR STRING VAR STRING VAR VAR VAR VAR VAR
|
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place.
You can perform zero or more operations of the following type:
* take two stones with indices i and j so that s_i β€ s_j, choose an integer d (0 β€ 2 β
d β€ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other.
You want to move the stones so that they are located at positions t_1, t_2, β¦, t_n. The order of the stones is not important β you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, β¦, t_n.
Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves.
Input
The first line contains a single integer n (1 β€ n β€ 3 β
10^5) β the number of stones.
The second line contains integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 10^9) β the initial positions of the stones.
The second line contains integers t_1, t_2, β¦, t_n (1 β€ t_i β€ 10^9) β the target positions of the stones.
Output
If it is impossible to move the stones this way, print "NO".
Otherwise, on the first line print "YES", on the second line print the number of operations m (0 β€ m β€ 5 β
n) required. You don't have to minimize the number of operations.
Then print m lines, each containing integers i, j, d (1 β€ i, j β€ n, s_i β€ s_j, 0 β€ 2 β
d β€ s_j - s_i), defining the operations.
One can show that if an answer exists, there is an answer requiring no more than 5 β
n operations.
Examples
Input
5
2 2 7 4 9
5 4 5 5 5
Output
YES
4
4 3 1
2 3 1
2 5 2
1 5 2
Input
3
1 5 10
3 5 7
Output
NO
Note
Consider the first example.
* After the first move the locations of stones is [2, 2, 6, 5, 9].
* After the second move the locations of stones is [2, 3, 5, 5, 9].
* After the third move the locations of stones is [2, 5, 5, 5, 7].
* After the last move the locations of stones is [4, 5, 5, 5, 5].
|
n = int(input())
s = list(map(int, input().split()))
t = list(map(int, input().split()))
if sum(s) != sum(t):
print("NO")
else:
s = [(s[i], i + 1) for i in range(n)]
s.sort()
t.sort()
diff = [0] * n
for i in range(n):
if s[i][0] == t[i]:
diff[i] = 0
elif s[i][0] < t[i]:
diff[i] = 1
else:
diff[i] = -1
move = [abs(s[i][0] - t[i]) for i in range(n)]
sumi = sum(move)
indu = 0
indd = 0
out = []
while sumi > 0:
if indd < indu:
print("NO")
exit()
if diff[indu] == 1 and move[indu] > 0 and diff[indd] == -1 and move[indd] > 0:
a = min(move[indu], move[indd])
move[indu] -= a
move[indd] -= a
out.append((s[indu][1], s[indd][1], a))
sumi -= 2 * a
elif diff[indu] == 1 and move[indu] > 0:
indd += 1
elif diff[indd] == -1 and move[indd] > 0:
indu += 1
else:
indd += 1
indu += 1
print("YES")
print(len(out))
for guy in out:
print(guy[0], guy[1], guy[2])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER
|
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place.
You can perform zero or more operations of the following type:
* take two stones with indices i and j so that s_i β€ s_j, choose an integer d (0 β€ 2 β
d β€ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other.
You want to move the stones so that they are located at positions t_1, t_2, β¦, t_n. The order of the stones is not important β you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, β¦, t_n.
Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves.
Input
The first line contains a single integer n (1 β€ n β€ 3 β
10^5) β the number of stones.
The second line contains integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 10^9) β the initial positions of the stones.
The second line contains integers t_1, t_2, β¦, t_n (1 β€ t_i β€ 10^9) β the target positions of the stones.
Output
If it is impossible to move the stones this way, print "NO".
Otherwise, on the first line print "YES", on the second line print the number of operations m (0 β€ m β€ 5 β
n) required. You don't have to minimize the number of operations.
Then print m lines, each containing integers i, j, d (1 β€ i, j β€ n, s_i β€ s_j, 0 β€ 2 β
d β€ s_j - s_i), defining the operations.
One can show that if an answer exists, there is an answer requiring no more than 5 β
n operations.
Examples
Input
5
2 2 7 4 9
5 4 5 5 5
Output
YES
4
4 3 1
2 3 1
2 5 2
1 5 2
Input
3
1 5 10
3 5 7
Output
NO
Note
Consider the first example.
* After the first move the locations of stones is [2, 2, 6, 5, 9].
* After the second move the locations of stones is [2, 3, 5, 5, 9].
* After the third move the locations of stones is [2, 5, 5, 5, 7].
* After the last move the locations of stones is [4, 5, 5, 5, 5].
|
import sys
class cell:
def __init__(self, val, idx):
self.idx = idx
self.val = val
inp = [int(x) for x in sys.stdin.read().split()]
n = inp[0]
inp_idx = 1
s = [cell(inp[idx], idx) for idx in range(1, n + 1)]
t = [inp[idx] for idx in range(n + 1, n + n + 1)]
s.sort(key=lambda x: x.val)
t.sort()
sum = 0
for i in range(n):
sum += s[i].val - t[i]
if sum != 0:
print("NO")
else:
operation = []
beg = 0
end = 0
cnt = 0
while True:
while beg < n and s[beg].val == t[beg]:
beg += 1
if beg == n:
break
while end <= beg or end < n and s[end].val <= t[end]:
end += 1
if end == n:
print("NO")
exit(0)
left = t[beg] - s[beg].val
if left < 0:
print("NO")
exit(0)
right = s[end].val - t[end]
d = min(left, right)
s[beg].val += d
s[end].val -= d
operation.append("%d %d %d" % (s[beg].idx, s[end].idx, d))
print("YES")
print(len(operation))
print("\n".join(operation))
|
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place.
You can perform zero or more operations of the following type:
* take two stones with indices i and j so that s_i β€ s_j, choose an integer d (0 β€ 2 β
d β€ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other.
You want to move the stones so that they are located at positions t_1, t_2, β¦, t_n. The order of the stones is not important β you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, β¦, t_n.
Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves.
Input
The first line contains a single integer n (1 β€ n β€ 3 β
10^5) β the number of stones.
The second line contains integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 10^9) β the initial positions of the stones.
The second line contains integers t_1, t_2, β¦, t_n (1 β€ t_i β€ 10^9) β the target positions of the stones.
Output
If it is impossible to move the stones this way, print "NO".
Otherwise, on the first line print "YES", on the second line print the number of operations m (0 β€ m β€ 5 β
n) required. You don't have to minimize the number of operations.
Then print m lines, each containing integers i, j, d (1 β€ i, j β€ n, s_i β€ s_j, 0 β€ 2 β
d β€ s_j - s_i), defining the operations.
One can show that if an answer exists, there is an answer requiring no more than 5 β
n operations.
Examples
Input
5
2 2 7 4 9
5 4 5 5 5
Output
YES
4
4 3 1
2 3 1
2 5 2
1 5 2
Input
3
1 5 10
3 5 7
Output
NO
Note
Consider the first example.
* After the first move the locations of stones is [2, 2, 6, 5, 9].
* After the second move the locations of stones is [2, 3, 5, 5, 9].
* After the third move the locations of stones is [2, 5, 5, 5, 7].
* After the last move the locations of stones is [4, 5, 5, 5, 5].
|
input()
s = [(int(x), i) for i, x in enumerate(input().split())]
t = [int(x) for x in input().split()]
s.sort()
t.sort()
def no():
print("NO")
raise SystemExit(0)
end = 1
ans = []
for si, ti in zip(s, t):
si_pos, i = si
if si_pos > ti:
no()
jump = ti - si_pos
while jump:
for end in range(end, len(s)):
if s[end][0] - t[end] > 0:
break
else:
no()
moved = s[end]
mov = moved[0] - t[end]
mov = min(mov, jump)
s[end] = moved[0] - mov, moved[1]
jump -= mov
ans.append("{} {} {}".format(i + 1, moved[1] + 1, mov))
assert len(ans) <= 5 * len(s)
if not ans:
print("YES\n0")
else:
print("YES\n{}\n{}".format(len(ans), "\n".join(ans)))
|
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL STRING VAR
|
Vasya plays the Geometry Horse.
The game goal is to destroy geometric figures of the game world. A certain number of points is given for destroying each figure depending on the figure type and the current factor value.
There are n types of geometric figures. The number of figures of type ki and figure cost ci is known for each figure type. A player gets ciΒ·f points for destroying one figure of type i, where f is the current factor. The factor value can be an integer number from 1 to t + 1, inclusive. At the beginning of the game the factor value is equal to 1. The factor is set to i + 1 after destruction of pi (1 β€ i β€ t) figures, so the (pi + 1)-th figure to be destroyed is considered with factor equal to i + 1.
Your task is to determine the maximum number of points Vasya can get after he destroys all figures. Take into account that Vasya is so tough that he can destroy figures in any order chosen by him.
Input
The first line contains the only integer number n (1 β€ n β€ 100) β the number of figure types.
Each of the following n lines contains two integer numbers ki and ci (1 β€ ki β€ 109, 0 β€ ci β€ 1000), separated with space β the number of figures of the i-th type and the cost of one i-type figure, correspondingly.
The next line contains the only integer number t (1 β€ t β€ 100) β the number that describe the factor's changes.
The next line contains t integer numbers pi (1 β€ p1 < p2 < ... < pt β€ 1012), separated with spaces.
Please, do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator.
Output
Print the only number β the maximum number of points Vasya can get.
Examples
Input
1
5 10
2
3 6
Output
70
Input
2
3 8
5 10
1
20
Output
74
Note
In the first example Vasya destroys three figures first and gets 3Β·1Β·10 = 30 points. Then the factor will become equal to 2 and after destroying the last two figures Vasya will get 2Β·2Β·10 = 40 points. As a result Vasya will get 70 points.
In the second example all 8 figures will be destroyed with factor 1, so Vasya will get (3Β·8 + 5Β·10)Β·1 = 74 points.
|
n = int(input())
a = [list(map(int, input().split()))[::-1] for i in range(n)]
t = int(input())
p = list(map(int, input().split()))
b = 0
i = 0
a.sort()
c = 0
for j in range(n):
while i < t and p[i] - b <= a[j][1]:
c += (p[i] - b) * (i + 1) * a[j][0]
a[j][1] -= p[i] - b
b = p[i]
i += 1
c += a[j][1] * (i + 1) * a[j][0]
b += a[j][1]
print(c)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya plays the Geometry Horse.
The game goal is to destroy geometric figures of the game world. A certain number of points is given for destroying each figure depending on the figure type and the current factor value.
There are n types of geometric figures. The number of figures of type ki and figure cost ci is known for each figure type. A player gets ciΒ·f points for destroying one figure of type i, where f is the current factor. The factor value can be an integer number from 1 to t + 1, inclusive. At the beginning of the game the factor value is equal to 1. The factor is set to i + 1 after destruction of pi (1 β€ i β€ t) figures, so the (pi + 1)-th figure to be destroyed is considered with factor equal to i + 1.
Your task is to determine the maximum number of points Vasya can get after he destroys all figures. Take into account that Vasya is so tough that he can destroy figures in any order chosen by him.
Input
The first line contains the only integer number n (1 β€ n β€ 100) β the number of figure types.
Each of the following n lines contains two integer numbers ki and ci (1 β€ ki β€ 109, 0 β€ ci β€ 1000), separated with space β the number of figures of the i-th type and the cost of one i-type figure, correspondingly.
The next line contains the only integer number t (1 β€ t β€ 100) β the number that describe the factor's changes.
The next line contains t integer numbers pi (1 β€ p1 < p2 < ... < pt β€ 1012), separated with spaces.
Please, do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator.
Output
Print the only number β the maximum number of points Vasya can get.
Examples
Input
1
5 10
2
3 6
Output
70
Input
2
3 8
5 10
1
20
Output
74
Note
In the first example Vasya destroys three figures first and gets 3Β·1Β·10 = 30 points. Then the factor will become equal to 2 and after destroying the last two figures Vasya will get 2Β·2Β·10 = 40 points. As a result Vasya will get 70 points.
In the second example all 8 figures will be destroyed with factor 1, so Vasya will get (3Β·8 + 5Β·10)Β·1 = 74 points.
|
n = int(input())
pieces = [input() for _ in range(n)]
pieces = [_.split() for _ in pieces]
pieces = [tuple(_) for _ in pieces]
pieces = [[int(k), int(c)] for k, c in pieces]
t = int(input())
p = input().split()
p = [int(_) for _ in p]
pieces = sorted(pieces, key=lambda x: x[1])
values = []
count = 0
i = 0
j = 0
while i < n and j < t:
if count + pieces[i][0] < p[j]:
values.append((pieces[i][0], pieces[i][1], j + 1))
count += pieces[i][0]
i += 1
elif count + pieces[i][0] == p[j]:
values.append((pieces[i][0], pieces[i][1], j + 1))
count += pieces[i][0]
i += 1
j += 1
else:
diff = p[j] - count
values.append((diff, pieces[i][1], j + 1))
count = p[j]
j += 1
pieces[i][0] -= diff
while i < n:
values.append((pieces[i][0], pieces[i][1], j + 1))
i += 1
score = 0
for a, b, c in values:
score += a * b * c
print(score)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Vasya plays the Geometry Horse.
The game goal is to destroy geometric figures of the game world. A certain number of points is given for destroying each figure depending on the figure type and the current factor value.
There are n types of geometric figures. The number of figures of type ki and figure cost ci is known for each figure type. A player gets ciΒ·f points for destroying one figure of type i, where f is the current factor. The factor value can be an integer number from 1 to t + 1, inclusive. At the beginning of the game the factor value is equal to 1. The factor is set to i + 1 after destruction of pi (1 β€ i β€ t) figures, so the (pi + 1)-th figure to be destroyed is considered with factor equal to i + 1.
Your task is to determine the maximum number of points Vasya can get after he destroys all figures. Take into account that Vasya is so tough that he can destroy figures in any order chosen by him.
Input
The first line contains the only integer number n (1 β€ n β€ 100) β the number of figure types.
Each of the following n lines contains two integer numbers ki and ci (1 β€ ki β€ 109, 0 β€ ci β€ 1000), separated with space β the number of figures of the i-th type and the cost of one i-type figure, correspondingly.
The next line contains the only integer number t (1 β€ t β€ 100) β the number that describe the factor's changes.
The next line contains t integer numbers pi (1 β€ p1 < p2 < ... < pt β€ 1012), separated with spaces.
Please, do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator.
Output
Print the only number β the maximum number of points Vasya can get.
Examples
Input
1
5 10
2
3 6
Output
70
Input
2
3 8
5 10
1
20
Output
74
Note
In the first example Vasya destroys three figures first and gets 3Β·1Β·10 = 30 points. Then the factor will become equal to 2 and after destroying the last two figures Vasya will get 2Β·2Β·10 = 40 points. As a result Vasya will get 70 points.
In the second example all 8 figures will be destroyed with factor 1, so Vasya will get (3Β·8 + 5Β·10)Β·1 = 74 points.
|
n = int(input())
fig = [tuple(map(int, input().split()))[::-1] for _ in range(n)]
fig.sort()
t = int(input())
a = list(map(int, input().split()))
res, curr = 0, 0
i, j = 0, 0
while i < n:
if j < t and curr + fig[i][1] >= a[j]:
take = a[j] - curr
curr += take
fig[i] = fig[i][0], fig[i][1] - take
res += take * (j + 1) * fig[i][0]
j += 1
else:
take = fig[i][1]
curr += take
res += take * (j + 1) * fig[i][0]
i += 1
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya plays the Geometry Horse.
The game goal is to destroy geometric figures of the game world. A certain number of points is given for destroying each figure depending on the figure type and the current factor value.
There are n types of geometric figures. The number of figures of type ki and figure cost ci is known for each figure type. A player gets ciΒ·f points for destroying one figure of type i, where f is the current factor. The factor value can be an integer number from 1 to t + 1, inclusive. At the beginning of the game the factor value is equal to 1. The factor is set to i + 1 after destruction of pi (1 β€ i β€ t) figures, so the (pi + 1)-th figure to be destroyed is considered with factor equal to i + 1.
Your task is to determine the maximum number of points Vasya can get after he destroys all figures. Take into account that Vasya is so tough that he can destroy figures in any order chosen by him.
Input
The first line contains the only integer number n (1 β€ n β€ 100) β the number of figure types.
Each of the following n lines contains two integer numbers ki and ci (1 β€ ki β€ 109, 0 β€ ci β€ 1000), separated with space β the number of figures of the i-th type and the cost of one i-type figure, correspondingly.
The next line contains the only integer number t (1 β€ t β€ 100) β the number that describe the factor's changes.
The next line contains t integer numbers pi (1 β€ p1 < p2 < ... < pt β€ 1012), separated with spaces.
Please, do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator.
Output
Print the only number β the maximum number of points Vasya can get.
Examples
Input
1
5 10
2
3 6
Output
70
Input
2
3 8
5 10
1
20
Output
74
Note
In the first example Vasya destroys three figures first and gets 3Β·1Β·10 = 30 points. Then the factor will become equal to 2 and after destroying the last two figures Vasya will get 2Β·2Β·10 = 40 points. As a result Vasya will get 70 points.
In the second example all 8 figures will be destroyed with factor 1, so Vasya will get (3Β·8 + 5Β·10)Β·1 = 74 points.
|
import sys
def solve():
(n,) = rv()
figures = list()
before = 0
for i in range(n):
number, cost = rv()
figures.append([cost, number, before, before + number])
figures.sort()
for i in range(n):
number = figures[i][1]
figures[i][2] = before
figures[i][3] = before + number
before += number
(t,) = rv()
p = [0] + list(map(int, input().split())) + [before]
res = 0
for i in range(1, len(p)):
for f in range(len(figures)):
left = max(figures[f][2], p[i - 1])
right = min(figures[f][3], p[i])
num = max(0, right - left)
res += num * i * figures[f][0]
print(res)
def prt(l):
return print("".join(l))
def rv():
return map(int, input().split())
def rl(n):
return [list(map(int, input().split())) for _ in range(n)]
if sys.hexversion == 50594544:
sys.stdin = open("test.txt")
solve()
|
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL STRING 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 VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
N = int(input())
A = [0] * N
B = [0] * N
for i in range(N):
A[i], B[i] = map(int, input().split())
indices = list(range(N))
indices.sort(key=lambda i: B[i])
low = 0
high = N - 1
bought = 0
cost = 0
while low <= high:
l = indices[low]
h = indices[high]
if bought >= B[l]:
bought += A[l]
cost += A[l]
A[l] = 0
low += 1
elif B[l] - bought >= A[h]:
bought += A[h]
cost += 2 * A[h]
A[h] = 0
high -= 1
else:
need = B[l] - bought
bought += need
cost += 2 * need
A[h] -= need
assert sum(A) == 0
print(cost)
|
ASSIGN VAR FUNC_CALL VAR 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 VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
def solve(n, items):
items.sort(key=lambda x: x[1])
ans = 0
cnt = 0
l = 0
r = n - 1
while l <= r:
if items[l][1] <= cnt:
cnt += items[l][0]
ans += items[l][0]
items[l][0] = 0
l += 1
else:
diff = min(items[l][1] - cnt, items[r][0])
cnt += diff
ans += diff * 2
items[r][0] -= diff
if items[r][0] == 0:
r -= 1
return ans
n = int(input())
items = []
for i in range(n):
a, b = map(int, input().split())
items.append([a, b])
print(solve(n, items))
|
FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
n = int(input())
s = 0
x = []
for _ in range(n):
a, b = map(int, input().split())
s += a
x.append([a, b])
x.sort(key=lambda x: x[1])
cnt = 0
cur = 0
for a, b in x:
if cur < b:
cur = b
if b >= s:
break
if s - cur >= a:
cnt += a
cur += a
else:
cnt += s - cur
break
print(2 * s - cnt)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
n = int(input())
items = []
for i in range(n):
a, b = map(int, input().split())
items.append([b, a])
items.sort()
head = 0
tail = len(items) - 1
count = 0
pay = 0
while head != tail:
head_need, head_left = items[head]
tail_need, tail_left = items[tail]
if count >= head_need:
count += head_left
pay += head_left
head += 1
elif tail_left > head_need - count:
items[tail][1] -= head_need - count
pay += 2 * (head_need - count)
count += head_need - count
else:
count += tail_left
pay += 2 * tail_left
tail -= 1
head_need, head_left = items[head]
if head_need - count >= head_left:
pay += 2 * head_left
else:
pay += max(head_need - count, 0) * 2 + head_left - max(head_need - count, 0)
print(pay)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
n = int(input())
lis = []
for i in range(n):
a, b = map(int, input().split())
lis.append([b, a])
lis.sort()
i = 0
j = n - 1
p_c = 0
price = 0
while i < j:
if lis[i][0] <= p_c:
price += lis[i][1]
p_c += lis[i][1]
i += 1
else:
pick = min(lis[j][1], lis[i][0] - p_c)
p_c += pick
price += 2 * pick
lis[j][1] -= pick
if lis[j][1] == 0:
j -= 1
if i == j:
if lis[i][0] <= p_c:
price += lis[i][1]
p_c += lis[i][1]
else:
pick = min(lis[i][0] - p_c, lis[i][1])
price += 2 * pick
price += max(lis[i][1] - pick, 0)
print(price)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
from sys import stdin
input = stdin.readline
def f(ar):
ar = sorted(ar, reverse=True, key=lambda s: s[1])
cost, cs, lo = 0, 0, 0
hi = len(ar) - 1
while lo <= hi:
if ar[hi][1] <= cs:
cost += ar[hi][0]
cs += ar[hi][0]
hi -= 1
elif ar[hi][1] - cs <= ar[lo][0]:
ar[lo][0] -= ar[hi][1] - cs
cost += 2 * (ar[hi][1] - cs)
cs += ar[hi][1] - cs
cs += ar[hi][0]
cost += ar[hi][0]
hi -= 1
else:
cs += ar[lo][0]
cost += ar[lo][0] * 2
lo += 1
return cost
ar = []
for _ in range(int(input())):
a, b = map(int, input().strip().split())
ar.append([a, b])
print(f(ar))
|
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
n = int(input())
T = []
for _ in range(n):
a, b = map(int, input().split(" "))
T.append([a, b])
T.sort(key=lambda e: e[1])
price = 0
total = 0
l, r = 0, len(T) - 1
while l < r:
a, b = T[l]
if total >= b:
price += a
total += a
T[l][0] = 0
l += 1
else:
d = b - total
while l < r and d > 0:
ar, br = T[r]
if ar > d:
price += 2 * d
T[r][0] -= d
total += d
d = 0
else:
price += 2 * ar
T[r][0] = 0
r -= 1
total += ar
d -= ar
a, b = T[l]
if a != 0:
if total >= b:
price += a
elif a + total >= b:
price += 2 * (b - total) + (a + total - b)
else:
price += 2 * a
print(price)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
n = int(input())
a = []
for i in range(n):
q, w = [int(i) for i in input().split()]
a.append([q, w])
a.sort(key=lambda x: x[1])
s = 0
for i in range(n):
s += a[i][0]
M = 0
N = 0
i = 0
while M + N < s:
if M + N < a[i][1]:
N += min(a[i][1] - N - M, s - N - M)
if M + N < s - a[i][0]:
M += a[i][0]
else:
M += s - (N + M)
i += 1
print(2 * N + M)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
N = int(input())
A = [0] * N
for i in range(N):
A[i] = list(map(int, input().split()))
A.sort(key=lambda a: a[1])
low = 0
high = N - 1
bought = 0
cost = 0
while low <= high:
if bought >= A[low][1]:
bought += A[low][0]
cost += A[low][0]
A[low][0] = 0
low += 1
elif A[low][1] - bought >= A[high][0]:
bought += A[high][0]
cost += 2 * A[high][0]
A[high][0] = 0
high -= 1
else:
need = A[low][1] - bought
bought += need
cost += 2 * need
A[high][0] -= need
assert all([(A[i][0] == 0) for i in range(N)])
print(cost)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
arr = []
ans = 0
for _ in range(int(input())):
a, b = map(int, input().split())
arr.append([a, b])
arr.sort(key=lambda a: a[1])
start = 0
end = len(arr) - 1
curr = 0
ans = 0
while start <= end:
if arr[start][1] > curr:
temp = min(arr[start][1] - curr, arr[end][0])
curr += temp
ans += 2 * temp
arr[end][0] -= temp
else:
temp = arr[start][0]
curr += temp
ans += 1 * temp
arr[start][0] = 0
if arr[start][0] == 0:
start += 1
if arr[end][0] == 0:
end -= 1
print(ans)
|
ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
import sys
def inpu():
return sys.stdin.readline()
lets = "abcdefghijklmnopqrstuvwxyz"
key = {lets[i]: i for i in range(26)}
n = int(input())
done = False
a = [0] * n
b = [0] * n
bb = 0
for i in range(n):
b[i], a[i] = map(int, input().split())
bb += b[i]
k = sorted(range(n), key=lambda i: a[i], reverse=True)
ans = bb
for i in range(n):
ans = min(ans, bb)
ans = max(a[k[i]], ans - b[k[i]])
print(min(ans, bb) + bb)
|
IMPORT FUNC_DEF RETURN FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
n = int(input())
li = []
for i in range(n):
a, b = map(int, input().split())
temp = [b, a]
li.append(temp)
li.sort()
a = 0
b = n - 1
cost = 0
count = 0
while a <= b:
if count >= li[a][0]:
count += li[a][1]
cost += li[a][1]
a += 1
else:
c = li[a][0] - count
if c >= li[b][1]:
count += li[b][1]
cost += 2 * li[b][1]
b -= 1
else:
li[b][1] -= c
count += c
cost += 2 * c
print(cost)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
n = int(input())
dp = []
cnt1 = 0
cnt2 = 0
for i in range(n):
a, b = list(map(int, input().split()))
dp.append((b, a))
cnt2 += a
dp.sort()
dp = dp[::-1]
for i in range(n):
cnt1 += min(cnt2 - cnt1 - min(dp[i][0], cnt2), dp[i][1])
print(cnt2 * 2 - cnt1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
import sys
t = int(sys.stdin.readline())
li = []
for _ in range(t):
a, b = map(int, sys.stdin.readline().split())
li.append([a, b])
li.sort(key=lambda x: x[1])
l, r = 0, len(li) - 1
n = 0
ans = 0
while l <= r:
if li[l][1] <= n:
n += li[l][0]
ans += li[l][0]
l += 1
else:
while r >= l:
if li[l][1] > li[r][0] + n:
n += li[r][0]
ans += li[r][0] * 2
r -= 1
else:
ans += (li[l][1] - n) * 2
k = li[l][1] - n
n += k
li[r][0] -= k
break
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
import sys
input = sys.stdin.readline
I = lambda: list(map(int, input().split()))
(n,) = I()
l = []
for _ in range(n):
l.append(I())
l.sort(key=lambda x: x[1])
an = 0
mx = n - 1
tot = 0
for i in range(n):
a, b = l[i]
if a <= 0:
break
if b <= tot:
an += a
tot += a
continue
take = 0
for j in range(mx, i, -1):
if tot + take < b:
cur = min(l[j][0], b - (tot + take))
take += cur
l[j][0] -= cur
if l[j][0]:
mx = j
break
tot += take
an += take * 2
if tot >= b:
an += a
else:
x = b - tot
if x <= a:
an += a - x + 2 * x
else:
an += 2 * a
tot += a
print(an)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
n = int(input())
items = []
needed = 0
for i in range(n):
x = input().split()
items.append([int(x[1]), int(x[0])])
needed += int(x[0])
items.sort()
l = 0
r = n - 1
total = 0
spent = 0
while total < needed:
if items[l][0] <= total:
cur = items[l][1]
total += cur
spent += cur
l += 1
else:
if l == r and items[r][0] < needed:
taking1 = needed - items[r][0]
taking2 = items[r][0] - total
spent += taking2 * 2 + taking1
items[r][1] -= taking1 + taking2
total += taking1 + taking2
r -= 1
continue
taking = min(items[l][0] - total, items[r][1])
items[r][1] -= taking
spent += 2 * taking
total += taking
if items[r][1] == 0:
r -= 1
print(spent)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
n = int(input())
product = []
for i in range(n):
product.append(list(map(int, input().split())))
product.sort(key=lambda x: x[1], reverse=True)
sum = 0
i = 0
j = n - 1
answer = 0
while j >= i:
if sum >= product[j][1]:
answer += product[j][0]
sum += product[j][0]
product[j][0] = 0
j -= 1
elif product[i][0] >= product[j][1] - sum:
answer += 2 * (product[j][1] - sum)
product[i][0] -= product[j][1] - sum
sum += product[j][1] - sum
else:
sum += product[i][0]
answer += product[i][0] * 2
product[i][0] = 0
i += 1
print(answer)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
n = int(input())
a, b = [], []
for _ in range(n):
ax, bx = input().split(" ")
a.append(int(ax))
b.append(int(bx))
shopping_list = dict()
for aa, bb in zip(a, b):
if bb in shopping_list:
shopping_list[bb] += aa
else:
shopping_list[bb] = aa
items_bought = 0
cost = 0
ind = sorted(shopping_list)
while shopping_list:
x = ind[0]
if items_bought < x:
y = ind[-1]
if shopping_list[y] <= x - items_bought:
cost += 2 * shopping_list[y]
items_bought += shopping_list[y]
del shopping_list[y]
ind.pop(-1)
else:
shopping_list[y] -= x - items_bought
cost += 2 * (x - items_bought)
items_bought += x - items_bought
else:
cost += 1 * shopping_list[x]
items_bought += shopping_list[x]
del shopping_list[x]
ind.pop(0)
print(cost)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
import sys
input = sys.stdin.readline
prod, ones, twos = (
sorted(
[[int(i) for i in input().split()] for j in range(int(input()))],
key=lambda x: x[1],
),
0,
0,
)
for i in range(len(prod)):
twos += prod[i][0]
for i in range(len(prod) - 1, -1, -1):
left = max(twos - prod[i][1], 0)
twos, ones = twos - min(left, prod[i][0]), ones + min(left, prod[i][0])
print(twos * 2 + ones)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
import sys
input = sys.stdin.readline
def solve():
n = int(input())
arr = [list(map(int, input().split())) for _ in range(n)]
arr.sort(key=lambda x: (x[1], x[0]))
j = n - 1
cnt = ans = 0
for i in range(n):
a, b = arr[i]
if cnt >= b:
cnt += a
ans += a
else:
while j > i and cnt < b:
v = min(arr[j][0], b - cnt)
cnt += v
ans += 2 * v
arr[j][0] -= v
if arr[j][0] == 0:
j -= 1
if cnt >= b:
ans += a
else:
ans += min(b - cnt, a) * 2 + max(cnt + a - b, 0)
cnt += a
return ans
print(solve())
|
IMPORT ASSIGN 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 VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
from sys import stdin
n = int(stdin.readline())
lst = list()
for i in range(n):
a, b = map(int, stdin.readline().split())
lst.append([b, a])
lst.sort(key=lambda x: tuple(x))
nzi = n - 1
cur = 0
ans = 0
for i in range(n):
while nzi >= 0 and cur < lst[i][0]:
w = min(lst[i][0] - cur, lst[nzi][1])
lst[nzi][1] -= w
cur += w
ans += 2 * w
if lst[nzi][1] == 0:
nzi -= 1
ans += lst[i][1]
cur += lst[i][1]
lst[i][1] = 0
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
from sys import stdin
input = stdin.readline
n = int(input())
c = []
for _ in range(n):
a, b = [int(x) for x in input().split()]
c.append([b, a])
c.sort()
l = 0
r = n - 1
buyed = 0
costs = 0
while l <= r:
needed_for_discount = c[l][0]
needed_to_buy = c[l][1]
if buyed >= needed_for_discount:
buyed += needed_to_buy
costs += needed_to_buy
l += 1
else:
without_discount = c[r][1]
if buyed + without_discount < needed_for_discount:
costs += 2 * without_discount
buyed += without_discount
r -= 1
else:
new_buyed = needed_for_discount - buyed
buyed += new_buyed
costs += 2 * new_buyed
c[r][1] -= new_buyed
if c[r][1] == 0:
r -= 1
print(costs)
|
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
n = int(input())
merch = []
for i in range(n):
m = list(map(int, input().split(" ")))
merch.append(m)
merch = sorted(merch, key=lambda x: x[1])
l = 0
r = len(merch) - 1
res = 0
num = 0
while l <= r:
if merch[l][1] <= num:
num += merch[l][0]
res += merch[l][0]
l += 1
else:
count = min(merch[r][0], merch[l][1] - num)
res += count * 2
num += count
if count == merch[r][0]:
r -= 1
else:
merch[r][0] -= count
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
The store has an infinite number of items of every product.
All products have the same price: $2$ rubles per item.
For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 100000$) β the number of products.
Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) β the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
The sum of all $a_i$ does not exceed $10^{14}$.
-----Output-----
Output the minimum sum that Lena needs to make all purchases.
-----Examples-----
Input
3
3 4
1 3
1 5
Output
8
Input
5
2 7
2 8
1 2
2 4
1 8
Output
12
-----Note-----
In the first example, Lena can purchase the products in the following way:
one item of product $3$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $1$ for $2$ rubles,
one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
In the second example Lena can purchase the products in the following way:
one item of product $1$ for $2$ rubles,
two items of product $2$ for $2$ rubles for each,
one item of product $5$ for $2$ rubles,
one item of product $3$ for $1$ ruble,
two items of product $4$ for $1$ ruble for each,
one item of product $1$ for $1$ ruble.
In total, she spends $12$ rubles.
|
t = int(input())
a = []
for T in range(t):
b = list(map(int, input().split()))
a.append(b)
h = sorted(a, key=lambda x: x[1])
s = 0
j = 0
i = len(h) - 1
cost = 0
while j <= i:
k = h[i][0]
if k + s < h[j][1]:
s = s + k
i = i - 1
cost = cost + 2 * k
elif k + s == h[j][1]:
s = s + k
i = i - 1
cost = cost + 2 * k
while s >= h[j][1] and j < i:
cost = cost + h[j][0]
s = s + h[j][0]
j = j + 1
if j > i:
break
elif j != i:
his = h[j][1] - s
cost = cost + his * 2
s = s + his
h[i][0] = k - his
while s >= h[j][1] and j < i:
cost = cost + h[j][0]
s = s + h[j][0]
j = j + 1
if j > i:
break
else:
if s + k > h[i][1]:
hi = s + k - h[i][1]
if hi >= k:
cost = cost + k
else:
cost = cost + hi
cost = cost + (k - hi) * 2
j = j + 1
i = i - 1
print(cost)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.