message stringlengths 2 48.6k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 318 108k | cluster float64 8 8 | __index_level_0__ int64 636 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 β€ i β€ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
1. He lifts all the books above book x.
2. He pushes book x out of the stack.
3. He puts down the lifted books without changing their order.
4. After reading book x, he puts book x on the top of the stack.
<image>
He decided to read books for m days. In the j-th (1 β€ j β€ m) day, he will read the book that is numbered with integer bj (1 β€ bj β€ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.
After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
Input
The first line contains two space-separated integers n (2 β€ n β€ 500) and m (1 β€ m β€ 1000) β the number of books, and the number of days for which Jaehyun would read books.
The second line contains n space-separated integers w1, w2, ..., wn (1 β€ wi β€ 100) β the weight of each book.
The third line contains m space separated integers b1, b2, ..., bm (1 β€ bj β€ n) β the order of books that he would read. Note that he can read the same book more than once.
Output
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
Examples
Input
3 5
1 2 3
1 3 2 3 1
Output
12
Note
Here's a picture depicting the example. Each vertical column presents the stacked books.
<image> | instruction | 0 | 57,509 | 8 | 115,018 |
Tags: constructive algorithms, greedy, implementation, math
Correct Solution:
```
n,m = map(int,input().split())
w = list(map(int,input().split()))
b = list(map(int,input().split()))
when = []
j = n+m
for i in range(n):
try:
j = b.index(i+1)
except:
pass
if j != n+m:
when.append([j,i])
j = n+m
when = list(sorted(when))
stack = []
for i in range(len(when)):
stack.append(when[i][1])
count = 0
where = n+m
for i in range(m):
where = stack.index(b[i]-1)
for j in range(where):
count += w[stack[j]]
stack = [stack[where]]+stack[:where]+stack[where+1:]
print(count)
``` | output | 1 | 57,509 | 8 | 115,019 |
Provide tags and a correct Python 3 solution for this coding contest problem.
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 β€ i β€ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
1. He lifts all the books above book x.
2. He pushes book x out of the stack.
3. He puts down the lifted books without changing their order.
4. After reading book x, he puts book x on the top of the stack.
<image>
He decided to read books for m days. In the j-th (1 β€ j β€ m) day, he will read the book that is numbered with integer bj (1 β€ bj β€ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.
After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
Input
The first line contains two space-separated integers n (2 β€ n β€ 500) and m (1 β€ m β€ 1000) β the number of books, and the number of days for which Jaehyun would read books.
The second line contains n space-separated integers w1, w2, ..., wn (1 β€ wi β€ 100) β the weight of each book.
The third line contains m space separated integers b1, b2, ..., bm (1 β€ bj β€ n) β the order of books that he would read. Note that he can read the same book more than once.
Output
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
Examples
Input
3 5
1 2 3
1 3 2 3 1
Output
12
Note
Here's a picture depicting the example. Each vertical column presents the stacked books.
<image> | instruction | 0 | 57,510 | 8 | 115,020 |
Tags: constructive algorithms, greedy, implementation, math
Correct Solution:
```
def f(s):
return int(s) - 1
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(f, input().split()))
c = [[0] * n for i in range(n)]
o = 0
for i in range(m):
for j in range(n):
if j != b[i]:
c[j][b[i]] = 1
for j in range(n):
if c[b[i]][j] == 1:
o += a[j]
c[b[i]][j] = 0
print(o)
``` | output | 1 | 57,510 | 8 | 115,021 |
Provide tags and a correct Python 3 solution for this coding contest problem.
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 β€ i β€ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
1. He lifts all the books above book x.
2. He pushes book x out of the stack.
3. He puts down the lifted books without changing their order.
4. After reading book x, he puts book x on the top of the stack.
<image>
He decided to read books for m days. In the j-th (1 β€ j β€ m) day, he will read the book that is numbered with integer bj (1 β€ bj β€ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.
After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
Input
The first line contains two space-separated integers n (2 β€ n β€ 500) and m (1 β€ m β€ 1000) β the number of books, and the number of days for which Jaehyun would read books.
The second line contains n space-separated integers w1, w2, ..., wn (1 β€ wi β€ 100) β the weight of each book.
The third line contains m space separated integers b1, b2, ..., bm (1 β€ bj β€ n) β the order of books that he would read. Note that he can read the same book more than once.
Output
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
Examples
Input
3 5
1 2 3
1 3 2 3 1
Output
12
Note
Here's a picture depicting the example. Each vertical column presents the stacked books.
<image> | instruction | 0 | 57,511 | 8 | 115,022 |
Tags: constructive algorithms, greedy, implementation, math
Correct Solution:
```
n, m = [int(x) for x in input().split()]
w = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
ans = 0
state = [0] * n
used = [0] * n
ptr = 0
for i in b:
if not used[i - 1]:
used[i - 1] = 1
state[ptr] = (i, w[i - 1])
ptr += 1
for i in b:
ind = state.index((i, w[i - 1]))
ans += sum(x[1] for x in state[: ind])
state = [state[ind]] + state[: ind] + state[ind + 1: ]
print(ans)
``` | output | 1 | 57,511 | 8 | 115,023 |
Provide tags and a correct Python 3 solution for this coding contest problem.
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 β€ i β€ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
1. He lifts all the books above book x.
2. He pushes book x out of the stack.
3. He puts down the lifted books without changing their order.
4. After reading book x, he puts book x on the top of the stack.
<image>
He decided to read books for m days. In the j-th (1 β€ j β€ m) day, he will read the book that is numbered with integer bj (1 β€ bj β€ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.
After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
Input
The first line contains two space-separated integers n (2 β€ n β€ 500) and m (1 β€ m β€ 1000) β the number of books, and the number of days for which Jaehyun would read books.
The second line contains n space-separated integers w1, w2, ..., wn (1 β€ wi β€ 100) β the weight of each book.
The third line contains m space separated integers b1, b2, ..., bm (1 β€ bj β€ n) β the order of books that he would read. Note that he can read the same book more than once.
Output
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
Examples
Input
3 5
1 2 3
1 3 2 3 1
Output
12
Note
Here's a picture depicting the example. Each vertical column presents the stacked books.
<image> | instruction | 0 | 57,512 | 8 | 115,024 |
Tags: constructive algorithms, greedy, implementation, math
Correct Solution:
```
#!/usr/bin/env python3
n, m = map(int, input().split())
w = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
for i in range(m):
flag = dict()
for j in range(i-1, -1, -1):
if b[j] == b[i]:
break
if flag.get(b[j], True):
ans += w[b[j] - 1]
flag[b[j]] = False
print(ans)
``` | output | 1 | 57,512 | 8 | 115,025 |
Provide tags and a correct Python 3 solution for this coding contest problem.
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 β€ i β€ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
1. He lifts all the books above book x.
2. He pushes book x out of the stack.
3. He puts down the lifted books without changing their order.
4. After reading book x, he puts book x on the top of the stack.
<image>
He decided to read books for m days. In the j-th (1 β€ j β€ m) day, he will read the book that is numbered with integer bj (1 β€ bj β€ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.
After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
Input
The first line contains two space-separated integers n (2 β€ n β€ 500) and m (1 β€ m β€ 1000) β the number of books, and the number of days for which Jaehyun would read books.
The second line contains n space-separated integers w1, w2, ..., wn (1 β€ wi β€ 100) β the weight of each book.
The third line contains m space separated integers b1, b2, ..., bm (1 β€ bj β€ n) β the order of books that he would read. Note that he can read the same book more than once.
Output
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
Examples
Input
3 5
1 2 3
1 3 2 3 1
Output
12
Note
Here's a picture depicting the example. Each vertical column presents the stacked books.
<image> | instruction | 0 | 57,513 | 8 | 115,026 |
Tags: constructive algorithms, greedy, implementation, math
Correct Solution:
```
# coding: utf-8
n, m = [int(i) for i in input().split()]
w = [0]+[int(i) for i in input().split()]
b = [int(i) for i in input().split()]
books = []
for book in b:
if book not in books:
books.append(book)
books.reverse()
ans = 0
for book in b:
index = books.index(book)
ans += sum([w[i] for i in books[index+1:]])
del(books[index])
books.append(book)
print(ans)
``` | output | 1 | 57,513 | 8 | 115,027 |
Provide tags and a correct Python 3 solution for this coding contest problem.
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 β€ i β€ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
1. He lifts all the books above book x.
2. He pushes book x out of the stack.
3. He puts down the lifted books without changing their order.
4. After reading book x, he puts book x on the top of the stack.
<image>
He decided to read books for m days. In the j-th (1 β€ j β€ m) day, he will read the book that is numbered with integer bj (1 β€ bj β€ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.
After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
Input
The first line contains two space-separated integers n (2 β€ n β€ 500) and m (1 β€ m β€ 1000) β the number of books, and the number of days for which Jaehyun would read books.
The second line contains n space-separated integers w1, w2, ..., wn (1 β€ wi β€ 100) β the weight of each book.
The third line contains m space separated integers b1, b2, ..., bm (1 β€ bj β€ n) β the order of books that he would read. Note that he can read the same book more than once.
Output
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
Examples
Input
3 5
1 2 3
1 3 2 3 1
Output
12
Note
Here's a picture depicting the example. Each vertical column presents the stacked books.
<image> | instruction | 0 | 57,514 | 8 | 115,028 |
Tags: constructive algorithms, greedy, implementation, math
Correct Solution:
```
n, m = map(int,input().split())
w = [int(x) for x in input().split()]
b = [(int(x)-1) for x in input().split()]
w += [0]
a = 0
for i in range(m):
for j in range(i-1,-1,-1):
if b[j] == b[i]:
b[j] = n
break
a += w[b[j]]
print(a)
``` | output | 1 | 57,514 | 8 | 115,029 |
Provide tags and a correct Python 3 solution for this coding contest problem.
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 β€ i β€ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
1. He lifts all the books above book x.
2. He pushes book x out of the stack.
3. He puts down the lifted books without changing their order.
4. After reading book x, he puts book x on the top of the stack.
<image>
He decided to read books for m days. In the j-th (1 β€ j β€ m) day, he will read the book that is numbered with integer bj (1 β€ bj β€ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.
After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
Input
The first line contains two space-separated integers n (2 β€ n β€ 500) and m (1 β€ m β€ 1000) β the number of books, and the number of days for which Jaehyun would read books.
The second line contains n space-separated integers w1, w2, ..., wn (1 β€ wi β€ 100) β the weight of each book.
The third line contains m space separated integers b1, b2, ..., bm (1 β€ bj β€ n) β the order of books that he would read. Note that he can read the same book more than once.
Output
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
Examples
Input
3 5
1 2 3
1 3 2 3 1
Output
12
Note
Here's a picture depicting the example. Each vertical column presents the stacked books.
<image> | instruction | 0 | 57,515 | 8 | 115,030 |
Tags: constructive algorithms, greedy, implementation, math
Correct Solution:
```
n,m=map(int,input().split())
w=list(map(int,input().split()))
b=list(map(int,input().split()))
A=0
for i in range(m):
d,j={},i-1
while j>=0 and b[j]!=b[i]:
if (d.get(b[j],1)):
A+=w[b[j]-1]
d[b[j]]=0
j-=1
print(A)
``` | output | 1 | 57,515 | 8 | 115,031 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 β€ i β€ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
1. He lifts all the books above book x.
2. He pushes book x out of the stack.
3. He puts down the lifted books without changing their order.
4. After reading book x, he puts book x on the top of the stack.
<image>
He decided to read books for m days. In the j-th (1 β€ j β€ m) day, he will read the book that is numbered with integer bj (1 β€ bj β€ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.
After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
Input
The first line contains two space-separated integers n (2 β€ n β€ 500) and m (1 β€ m β€ 1000) β the number of books, and the number of days for which Jaehyun would read books.
The second line contains n space-separated integers w1, w2, ..., wn (1 β€ wi β€ 100) β the weight of each book.
The third line contains m space separated integers b1, b2, ..., bm (1 β€ bj β€ n) β the order of books that he would read. Note that he can read the same book more than once.
Output
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
Examples
Input
3 5
1 2 3
1 3 2 3 1
Output
12
Note
Here's a picture depicting the example. Each vertical column presents the stacked books.
<image>
Submitted Solution:
```
# <---|
#b = 1 3 2 3 1
#in cel mai bun caz cartile de deasupra ta sunt doar cele care apar de la pozitia i pana la j (j < i)
#b[j] = b[i]; b[j+1], b[j+2], ... b[i-1] != b[i]
rd = lambda: map(int, input().split())
n, m = rd()
w, b = list(rd()), list(rd())
ans = 0
for i in range(m):
j = i-1
viz = [0]*n
while j >= 0 and b[j] != b[i]:
if viz[b[j]-1] == 0:
viz[b[j]-1] = 1
ans += w[b[j]-1]
j -= 1
print(ans)
``` | instruction | 0 | 57,516 | 8 | 115,032 |
Yes | output | 1 | 57,516 | 8 | 115,033 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 β€ i β€ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
1. He lifts all the books above book x.
2. He pushes book x out of the stack.
3. He puts down the lifted books without changing their order.
4. After reading book x, he puts book x on the top of the stack.
<image>
He decided to read books for m days. In the j-th (1 β€ j β€ m) day, he will read the book that is numbered with integer bj (1 β€ bj β€ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.
After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
Input
The first line contains two space-separated integers n (2 β€ n β€ 500) and m (1 β€ m β€ 1000) β the number of books, and the number of days for which Jaehyun would read books.
The second line contains n space-separated integers w1, w2, ..., wn (1 β€ wi β€ 100) β the weight of each book.
The third line contains m space separated integers b1, b2, ..., bm (1 β€ bj β€ n) β the order of books that he would read. Note that he can read the same book more than once.
Output
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
Examples
Input
3 5
1 2 3
1 3 2 3 1
Output
12
Note
Here's a picture depicting the example. Each vertical column presents the stacked books.
<image>
Submitted Solution:
```
books,reads=list(map(int,input().split()))
booksw=list(map(int,input().split()))
readl=list(map(int,input().split()))
res=[]
for b in readl:
if(b not in res):
res.append(b)
sum=0
for l in readl:
for b in res:
if(b==l):
res.remove(b)
poped=b
break
sum=sum+booksw[b-1]
res=[poped]+res
print(sum)
``` | instruction | 0 | 57,517 | 8 | 115,034 |
Yes | output | 1 | 57,517 | 8 | 115,035 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 β€ i β€ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
1. He lifts all the books above book x.
2. He pushes book x out of the stack.
3. He puts down the lifted books without changing their order.
4. After reading book x, he puts book x on the top of the stack.
<image>
He decided to read books for m days. In the j-th (1 β€ j β€ m) day, he will read the book that is numbered with integer bj (1 β€ bj β€ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.
After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
Input
The first line contains two space-separated integers n (2 β€ n β€ 500) and m (1 β€ m β€ 1000) β the number of books, and the number of days for which Jaehyun would read books.
The second line contains n space-separated integers w1, w2, ..., wn (1 β€ wi β€ 100) β the weight of each book.
The third line contains m space separated integers b1, b2, ..., bm (1 β€ bj β€ n) β the order of books that he would read. Note that he can read the same book more than once.
Output
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
Examples
Input
3 5
1 2 3
1 3 2 3 1
Output
12
Note
Here's a picture depicting the example. Each vertical column presents the stacked books.
<image>
Submitted Solution:
```
def ans():
n, m= [int(i) for i in input().split()]
w= [int(i) for i in input().split()]
b= [int(i) for i in input().split()]
books= []
for i in b:
if i not in books:
books.append(i)
#print(books, w)
ans= 0
for i in b:
arr= books[:books.index(i)]
#print(i, arr, "h")
for j in arr:
ans+= w[j-1]
#print(i, j, w[i-1], ans)
books.remove(i)
books.insert(0, i)
print(ans)
return
ans()
``` | instruction | 0 | 57,518 | 8 | 115,036 |
Yes | output | 1 | 57,518 | 8 | 115,037 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 β€ i β€ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
1. He lifts all the books above book x.
2. He pushes book x out of the stack.
3. He puts down the lifted books without changing their order.
4. After reading book x, he puts book x on the top of the stack.
<image>
He decided to read books for m days. In the j-th (1 β€ j β€ m) day, he will read the book that is numbered with integer bj (1 β€ bj β€ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.
After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
Input
The first line contains two space-separated integers n (2 β€ n β€ 500) and m (1 β€ m β€ 1000) β the number of books, and the number of days for which Jaehyun would read books.
The second line contains n space-separated integers w1, w2, ..., wn (1 β€ wi β€ 100) β the weight of each book.
The third line contains m space separated integers b1, b2, ..., bm (1 β€ bj β€ n) β the order of books that he would read. Note that he can read the same book more than once.
Output
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
Examples
Input
3 5
1 2 3
1 3 2 3 1
Output
12
Note
Here's a picture depicting the example. Each vertical column presents the stacked books.
<image>
Submitted Solution:
```
n,m=map(int,input().split())
w=[-1]+list(map(int,input().split()))
b=list(map(int,input().split()))
stack=[]
used=set()
for i in range(m):
if not b[i] in used:
stack.append(b[i])
used.add(b[i])
for i in range(1,n+1):
if i not in used:
stack.append(i)
used.add(i)
ans=0
for i in range(m):
bb=b[i]
s=0
for j in range(n):
if bb!=stack[j]:
s+=w[stack[j]]
else:
stack=[stack[j]]+stack[:j]+stack[j+1:]
ans+=s
break
print(ans)
``` | instruction | 0 | 57,519 | 8 | 115,038 |
Yes | output | 1 | 57,519 | 8 | 115,039 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 β€ i β€ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
1. He lifts all the books above book x.
2. He pushes book x out of the stack.
3. He puts down the lifted books without changing their order.
4. After reading book x, he puts book x on the top of the stack.
<image>
He decided to read books for m days. In the j-th (1 β€ j β€ m) day, he will read the book that is numbered with integer bj (1 β€ bj β€ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.
After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
Input
The first line contains two space-separated integers n (2 β€ n β€ 500) and m (1 β€ m β€ 1000) β the number of books, and the number of days for which Jaehyun would read books.
The second line contains n space-separated integers w1, w2, ..., wn (1 β€ wi β€ 100) β the weight of each book.
The third line contains m space separated integers b1, b2, ..., bm (1 β€ bj β€ n) β the order of books that he would read. Note that he can read the same book more than once.
Output
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
Examples
Input
3 5
1 2 3
1 3 2 3 1
Output
12
Note
Here's a picture depicting the example. Each vertical column presents the stacked books.
<image>
Submitted Solution:
```
n,m = map(int,input().split())
w = list(map(int,input().split()))
b = list(map(int,input().split()))
when = []
j = n+m
for i in range(n):
try:
j = b.index(i+1)
except:
pass
if j != n+m:
when.append([j,i])
j = n+m
when = list(sorted(when))
stack = []
for i in range(len(when)):
stack.append(when[i][1])
count = 0
where = n+m
for i in range(m):
where = stack.index(b[i]-1)
for j in range(0,where):
count += w[stack[j]-1]
stack = [stack[where]]+stack[:where]+stack[where+1:]
print(count)
``` | instruction | 0 | 57,520 | 8 | 115,040 |
No | output | 1 | 57,520 | 8 | 115,041 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 β€ i β€ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
1. He lifts all the books above book x.
2. He pushes book x out of the stack.
3. He puts down the lifted books without changing their order.
4. After reading book x, he puts book x on the top of the stack.
<image>
He decided to read books for m days. In the j-th (1 β€ j β€ m) day, he will read the book that is numbered with integer bj (1 β€ bj β€ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.
After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
Input
The first line contains two space-separated integers n (2 β€ n β€ 500) and m (1 β€ m β€ 1000) β the number of books, and the number of days for which Jaehyun would read books.
The second line contains n space-separated integers w1, w2, ..., wn (1 β€ wi β€ 100) β the weight of each book.
The third line contains m space separated integers b1, b2, ..., bm (1 β€ bj β€ n) β the order of books that he would read. Note that he can read the same book more than once.
Output
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
Examples
Input
3 5
1 2 3
1 3 2 3 1
Output
12
Note
Here's a picture depicting the example. Each vertical column presents the stacked books.
<image>
Submitted Solution:
```
n, m = map(int, input().split())
w = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = list()
used = [False] * (n + 1)
for i in range(len(b)):
if not used[b[i]]:
ans.append(b[i])
used[b[i]] = True
anss = 0
for i in range(len(b)):
top = -1
for j in range(len(ans)):
if ans[j] == b[i]:
top = ans[j]
for k in range(j, 0, -1):
ans[k] = ans[k - 1]
anss += ans[k - 1]
break
ans[0] = top
print(anss)
``` | instruction | 0 | 57,521 | 8 | 115,042 |
No | output | 1 | 57,521 | 8 | 115,043 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 β€ i β€ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
1. He lifts all the books above book x.
2. He pushes book x out of the stack.
3. He puts down the lifted books without changing their order.
4. After reading book x, he puts book x on the top of the stack.
<image>
He decided to read books for m days. In the j-th (1 β€ j β€ m) day, he will read the book that is numbered with integer bj (1 β€ bj β€ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.
After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
Input
The first line contains two space-separated integers n (2 β€ n β€ 500) and m (1 β€ m β€ 1000) β the number of books, and the number of days for which Jaehyun would read books.
The second line contains n space-separated integers w1, w2, ..., wn (1 β€ wi β€ 100) β the weight of each book.
The third line contains m space separated integers b1, b2, ..., bm (1 β€ bj β€ n) β the order of books that he would read. Note that he can read the same book more than once.
Output
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
Examples
Input
3 5
1 2 3
1 3 2 3 1
Output
12
Note
Here's a picture depicting the example. Each vertical column presents the stacked books.
<image>
Submitted Solution:
```
n,m=input().split(' ')
n=int(n)
m=int(m)
w=input().split(' ')
r=input().split(' ')
total=0
for i in range(n):
w[i]=int(w[i])
for j in range(m):
r[j]=int(r[j])
finalk=[]
for element in r:
if element not in finalk:
finalk.append(element)
final=[]
for i in range(n):
final.append(0)
for i in range(n):
final[i]=finalk[n-i-1]
for element in r:
total+=sum(final[final.index(element)+1:len(final)])
final.append(final.pop(final.index(element)))
print(total)
``` | instruction | 0 | 57,522 | 8 | 115,044 |
No | output | 1 | 57,522 | 8 | 115,045 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 β€ i β€ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
1. He lifts all the books above book x.
2. He pushes book x out of the stack.
3. He puts down the lifted books without changing their order.
4. After reading book x, he puts book x on the top of the stack.
<image>
He decided to read books for m days. In the j-th (1 β€ j β€ m) day, he will read the book that is numbered with integer bj (1 β€ bj β€ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.
After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
Input
The first line contains two space-separated integers n (2 β€ n β€ 500) and m (1 β€ m β€ 1000) β the number of books, and the number of days for which Jaehyun would read books.
The second line contains n space-separated integers w1, w2, ..., wn (1 β€ wi β€ 100) β the weight of each book.
The third line contains m space separated integers b1, b2, ..., bm (1 β€ bj β€ n) β the order of books that he would read. Note that he can read the same book more than once.
Output
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
Examples
Input
3 5
1 2 3
1 3 2 3 1
Output
12
Note
Here's a picture depicting the example. Each vertical column presents the stacked books.
<image>
Submitted Solution:
```
books, days = [int(x) for x in input().split()]
weights = [int(x) for x in input().split()]
order = [int(x) for x in input().split()]
triples = []
for i in range(books):
triples.append((i, order.count(i + 1), weights[i]))
# print(triples)
# pairs = [(book, weights[book - 1],) for book in order]
# print(pairs)
def func(triple):
return (triple[1], -triple[2])
res = (list(sorted(triples, key=func, reverse=True)))
# for r in res:
# print(r[0] + 1, end=' ')
s = 0
start = [t[0] + 1 for t in res]
# print(start)
for b in order:
s += sum(weights[i-1] for i in start[:start.index(b)])
start.remove(b)
start = [b] + start
print(s)
``` | instruction | 0 | 57,523 | 8 | 115,046 |
No | output | 1 | 57,523 | 8 | 115,047 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1 β€ n β€ 1 000, n β€ k β€ 2 000, 1 β€ p β€ 109) β the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 β€ ai β€ 109) β positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 β€ bj β€ 109) β positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
Submitted Solution:
```
import sys
from collections import defaultdict
def calc(a,b,p):
return abs(a-b) + abs(p-b)
def main():
n, k, p= map(int,sys.stdin.readline().split())
a = list(map(int,sys.stdin.readline().split()))
b = list(map(int,sys.stdin.readline().split()))
a = sorted(a)
b = sorted(b)
v = 10**10
for i in range(k-n+1):
t = 0
for j in range(n):
t = max(t, calc(a[j],b[j+i],p))
v = min(v,t)
print(v)
main()
``` | instruction | 0 | 57,702 | 8 | 115,404 |
Yes | output | 1 | 57,702 | 8 | 115,405 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1 β€ n β€ 1 000, n β€ k β€ 2 000, 1 β€ p β€ 109) β the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 β€ ai β€ 109) β positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 β€ bj β€ 109) β positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
Submitted Solution:
```
import math as mt
import sys,string
input=sys.stdin.readline
#print=sys.stdout.write
import random
from collections import deque,defaultdict
L=lambda : list(map(int,input().split()))
Ls=lambda : list(input().split())
M=lambda : map(int,input().split())
I=lambda :int(input())
n,k,p=M()
l=L()
g=L()
l.sort()
g.sort()
ans=10**30
for i in range(k-n+1):
ans1=0
for j in range(i,i+n):
ans1=max(ans1,abs(l[j-i]-g[j])+abs(g[j]-p))
ans=min(ans,ans1)
print(ans)
``` | instruction | 0 | 57,703 | 8 | 115,406 |
Yes | output | 1 | 57,703 | 8 | 115,407 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1 β€ n β€ 1 000, n β€ k β€ 2 000, 1 β€ p β€ 109) β the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 β€ ai β€ 109) β positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 β€ bj β€ 109) β positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
Submitted Solution:
```
na,nb,o=list(map(int, input().split()))
a=sorted(list(map(int, input().split())))
b=sorted(list(map(int, input().split())))
dp=[[float('inf') for _ in range(nb+1)] for _ in range(na+1)]
for i in range(nb+1):
dp[0][i]=0
for i in range(1,na+1):
for j in range(1,nb+1):
dp[i][j] = min(dp[i][j-1], max(dp[i-1][j-1] ,abs(a[i-1]-b[j-1])+abs(o-b[j-1])))
print(dp[na][nb])
``` | instruction | 0 | 57,704 | 8 | 115,408 |
Yes | output | 1 | 57,704 | 8 | 115,409 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1 β€ n β€ 1 000, n β€ k β€ 2 000, 1 β€ p β€ 109) β the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 β€ ai β€ 109) β positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 β€ bj β€ 109) β positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
Submitted Solution:
```
strr1=input()
strr2=input()
strr3=input()
str1 = [int(n) for n in strr1.split()]
men_set=sorted([int(n) for n in strr2.split()])
keys_set=sorted([int(n) for n in strr3.split()])
men_n=str1[0]
keys_n=str1[1]
office_set=str1[2]
dp=[[0]*keys_n for i in range(men_n)]
for i in range(len(men_set)):
for j in range(i,keys_n):
if j==i:
dp[i][j]=max(dp[i-1][j-1],abs(keys_set[j]-office_set)+abs(men_set[i]-keys_set[j]))
elif j>i:
dp[i][j]=min(dp[i][j-1],abs(keys_set[j]-office_set)+abs(men_set[i]-keys_set[j]))
for i in dp[men_n-1]:
result=min(1000000000,i)
print(result)
``` | instruction | 0 | 57,705 | 8 | 115,410 |
No | output | 1 | 57,705 | 8 | 115,411 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1 β€ n β€ 1 000, n β€ k β€ 2 000, 1 β€ p β€ 109) β the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 β€ ai β€ 109) β positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 β€ bj β€ 109) β positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
Submitted Solution:
```
def read():
return [int(x) for x in input().split()]
n,k,p = read()
a = read()
b = read()
a.sort()
b.sort()
ans = 2e9+1
for i in range(k-n):
tmp = 0
for j in range(n):
tmp = max(tmp,abs(a[j]-b[i+j])+abs(b[i+j]-p))
ans = min(ans,tmp)
print(ans)
``` | instruction | 0 | 57,706 | 8 | 115,412 |
No | output | 1 | 57,706 | 8 | 115,413 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1 β€ n β€ 1 000, n β€ k β€ 2 000, 1 β€ p β€ 109) β the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 β€ ai β€ 109) β positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 β€ bj β€ 109) β positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
Submitted Solution:
```
#!/usr/bin/env python3
# http://codeforces.com/contest/830/problem/A
import sys
from operator import itemgetter
read = lambda: map(int, input().split())
n, k, p = read()
people, keys = sorted(read()), sorted(read())
res = []
for i in range(k):
inner_res = []
for j in range(n):
inner_res.append(abs(keys[i] - people[j]) + abs(keys[i] - p))
res.append(max(inner_res))
print(min(res))
# people.sort()
# key_office = []
# for k in keys:
# key_office.append((k, abs(k - p)))
# key_office.sort(key=itemgetter(1))
# max_time = 0
# for i in people:
# # print("i: {}".format(i))
# cur_time = 0
# time = abs(key_office[0][0] - p) + abs(i - key_office[0][0])
# for k in key_office:
# # print("k: {}".format(k[0]))
# cur_time = abs(i - k[0]) + abs(k[0] - p)
# # print("cur_time: {}".format(cur_time))
# if cur_time > time:
# max_time = max(time, max_time)
# # print("max_time: {}".format(max_time))
# break
# elif cur_time < time:
# time = cur_time
# max_time = max(time, max_time)
# # print("time: {}".format(time))
# print(max_time)
``` | instruction | 0 | 57,708 | 8 | 115,416 |
No | output | 1 | 57,708 | 8 | 115,417 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n glasses on the table numbered 1, β¦, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water.
You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor.
Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled).
Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number.
For each k = 1, β¦, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of glasses.
The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 β€ b_i β€ a_i β€ 100, a_i > 0) β capacity, and water amount currently contained for the glass i, respectively.
Output
Print n real numbers β the largest amount of water that can be collected in 1, β¦, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer.
Example
Input
3
6 5
6 5
10 2
Output
7.0000000000 11.0000000000 12.0000000000
Note
In the sample case, you can act as follows:
* for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units;
* for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units;
* for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured. | instruction | 0 | 58,237 | 8 | 116,474 |
Tags: dp
Correct Solution:
```
n = int(input())
inf = 10**8
dp = []
for k in range(n + 1):
dp.append([-inf] * 10005)
dp[0][0] = 0
w = 0
for _ in range(k):
a, b = map(int, input().split())
w += b
for k in range(n-1,-1,-1):
for i in range(10005 - a):
dp[k+1][i+a] = max(dp[k+1][i+a], dp[k][i] + b)
out = []
for line in dp[1:]:
poss = []
for i in range(10005):
cap = i
con = line[i]
poss.append(min(cap, (con + w)/2))
out.append(max(poss))
print(' '.join(map(str,out)))
``` | output | 1 | 58,237 | 8 | 116,475 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n glasses on the table numbered 1, β¦, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water.
You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor.
Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled).
Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number.
For each k = 1, β¦, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of glasses.
The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 β€ b_i β€ a_i β€ 100, a_i > 0) β capacity, and water amount currently contained for the glass i, respectively.
Output
Print n real numbers β the largest amount of water that can be collected in 1, β¦, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer.
Example
Input
3
6 5
6 5
10 2
Output
7.0000000000 11.0000000000 12.0000000000
Note
In the sample case, you can act as follows:
* for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units;
* for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units;
* for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured. | instruction | 0 | 58,238 | 8 | 116,476 |
Tags: dp
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline()
# --------------------------------------------------------------------
def RL(): return map(int, sys.stdin.readline().split())
def RLL(): return list(map(int, sys.stdin.readline().split()))
def N(): return int(input())
def S(): return input().strip()
def print_list(l):
print(' '.join(map(str,l)))
# sys.setrecursionlimit(100000)
# import random
# from functools import reduce
# from functools import lru_cache
# from heapq import *
# from collections import deque as dq
# import math
# import bisect as bs
# from collections import Counter
# from collections import defaultdict as dc
n = N()
p = [tuple(RL()) for _ in range(n)]
p.sort(key=lambda x:x[0])
S = sum(v[1] for v in p)
dic = [[-1] * 10001 for _ in range(n + 1)]
dic[0][0] = 0
l, r = [0], [0]
for i in range(n):
dc, dv = p[i]
for k in range(i, -1, -1):
t = -1
for c in range(r[k], l[k] - 1, -1):
if dic[k][c] == -1: continue
if dic[k][c] <= t: dic[k][c] = -1
else:
t = dic[k][c]
if dic[k][c] + dv > dic[k + 1][c + dc]:
dic[k + 1][c + dc] = dic[k][c] + dv
r[k] += dc - p[i - k][0]
l.append(l[-1] + dc)
r.append(l[-1])
print(*[max(min(c, (dic[k][c] + S) / 2) for c in range(l[-1] + 1) if dic[k][c] > -1) for k in range(1, n + 1)])
``` | output | 1 | 58,238 | 8 | 116,477 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n glasses on the table numbered 1, β¦, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water.
You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor.
Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled).
Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number.
For each k = 1, β¦, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of glasses.
The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 β€ b_i β€ a_i β€ 100, a_i > 0) β capacity, and water amount currently contained for the glass i, respectively.
Output
Print n real numbers β the largest amount of water that can be collected in 1, β¦, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer.
Example
Input
3
6 5
6 5
10 2
Output
7.0000000000 11.0000000000 12.0000000000
Note
In the sample case, you can act as follows:
* for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units;
* for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units;
* for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured. | instruction | 0 | 58,239 | 8 | 116,478 |
Tags: dp
Correct Solution:
```
n = int(input())
p = sorted([tuple(map(int, input().split())) for _ in range(n)], key=lambda x:x[0])
S = sum(v[1] for v in p)
dic = [[-1] * 10001 for _ in range(n + 1)]
dic[0][0] = 0
l, r = [0], [0]
for i in range(n):
dc, dv = p[i]
for k in range(i, -1, -1):
m = -1
for c in range(r[k], l[k] - 1, -1):
if dic[k][c] <= m: dic[k][c] = -1
else:
m = dic[k][c]
if dic[k][c] + dv > dic[k + 1][c + dc]:
dic[k + 1][c + dc] = dic[k][c] + dv
r[k] += dc - p[i - k][0]
l.append(l[-1] + dc)
r.append(l[-1])
print(*[max(min(c, (dic[k][c] + S) / 2) for c in range(l[k], r[k] + 1) if dic[k][c] > -1) for k in range(1, n + 1)])
``` | output | 1 | 58,239 | 8 | 116,479 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n glasses on the table numbered 1, β¦, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water.
You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor.
Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled).
Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number.
For each k = 1, β¦, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of glasses.
The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 β€ b_i β€ a_i β€ 100, a_i > 0) β capacity, and water amount currently contained for the glass i, respectively.
Output
Print n real numbers β the largest amount of water that can be collected in 1, β¦, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer.
Example
Input
3
6 5
6 5
10 2
Output
7.0000000000 11.0000000000 12.0000000000
Note
In the sample case, you can act as follows:
* for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units;
* for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units;
* for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured. | instruction | 0 | 58,240 | 8 | 116,480 |
Tags: dp
Correct Solution:
```
#https://codeforces.com/problemset/problem/1458/B
#Knapsack 2D dp
#Greedily take glasses to maximize starting amount.
#For the same cap, I want to know what is the largest starting amount,
#to minimize the remaining amount that will be transferred (and half spilled).
#dp[glass][nGlassesTaken][cap]=max starting amount for this cap
def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
import sys
input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
n=int(input())
a=[] #cap
b=[] #amount filled initially
for _ in range(n):
aa,bb=[int(x) for x in input().split()]
a.append(aa)
b.append(bb)
total=sum(b)
MAXCAP=sum(a)
#dp[glass][nGlassesTaken][cap]=dp[glass-1][nGlassesTaken][cap] #dont take glass
# or dp[glass-1][nGlassesTaken-1][cap-a[glass]]+b[glass] #take glass
#
#other conditions:
#0<=nGlassesTaken<=glass+1
#cap>=0
#base case is dp[glass][0][0]=0
#CHANGE TO 2D dp[nGlassesTaken][cap]
#dp=[[[-float('inf') for _ in range(MAXCAP+1)] for __ in range(n+1)] for ___ in range(n)]
#for glass in range(n):
# dp[glass][0][0]=0
dp=[[-float('inf') for _ in range(MAXCAP+1)] for __ in range(n+1)]
for glass in range(n):
dp[0][0]=0
for glass in range(n):
for nGlassesTaken in range(glass+1,0,-1):
for cap in range(MAXCAP,0,-1):
# dp[glass][nGlassesTaken][cap]=dp[glass-1][nGlassesTaken][cap] #dont take glass (no change)
if cap-a[glass]>=0:
# takeGlass=dp[glass-1][nGlassesTaken-1][cap-a[glass]]+b[glass] #take glass
# dp[glass][nGlassesTaken][cap]=max(dp[glass][nGlassesTaken][cap],takeGlass)
takeGlass=dp[nGlassesTaken-1][cap-a[glass]]+b[glass] #take glass
dp[nGlassesTaken][cap]=max(dp[nGlassesTaken][cap],takeGlass)
ans=[0 for _ in range(n+1)]
for nGlassesTaken in range(1,n+1):
for cap in range(MAXCAP+1):
amount=dp[nGlassesTaken][cap]
if amount!=-float('inf'):
amount2=(total-amount)/2.0
amount3=min(cap,amount+amount2)
ans[nGlassesTaken]=max(ans[nGlassesTaken],amount3)
#print(a)
#print(b)
#dp[glass][nGlassesTaken][cap]
#print(dp[n-1][1][:25])
oneLineArrayPrint(ans[1:])
``` | output | 1 | 58,240 | 8 | 116,481 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n glasses on the table numbered 1, β¦, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water.
You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor.
Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled).
Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number.
For each k = 1, β¦, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of glasses.
The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 β€ b_i β€ a_i β€ 100, a_i > 0) β capacity, and water amount currently contained for the glass i, respectively.
Output
Print n real numbers β the largest amount of water that can be collected in 1, β¦, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer.
Example
Input
3
6 5
6 5
10 2
Output
7.0000000000 11.0000000000 12.0000000000
Note
In the sample case, you can act as follows:
* for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units;
* for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units;
* for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured. | instruction | 0 | 58,241 | 8 | 116,482 |
Tags: dp
Correct Solution:
```
from sys import stdin, stdout
def glass_half_spilled(n, ab_a, a_a, sa, sb):
dp = [[-1 for _ in range(sa + 1)] for _ in range(n+1)]
dp[0][0] = 0
for i in range(1, n+1):
for k in range(i, 0, -1):
for A in range(a_a[i], -1, -1):
if dp[k-1][A - ab_a[i-1][0]] == -1:
continue
dp[k][A] = max(dp[k][A], dp[k-1][A - ab_a[i-1][0]] + ab_a[i-1][1])
# print(dp)
r_a = []
for k in range(1, n+1):
r = 0
for A in range(sa, -1, -1):
if dp[k][A]== -1:
continue
r = max(r, min(dp[k][A] / 2 + sb / 2, A))
r_a.append(r)
return r_a
n = int(stdin.readline())
ab_a = []
a_a = [0]
sb = 0
for _ in range(n):
a, b = map(int, stdin.readline().split())
ab_a.append([a, b])
a_a.append(a + a_a[-1])
sb += b
r_a = glass_half_spilled(n, ab_a, a_a, a_a[-1], sb)
stdout.write(' '.join(map(str, r_a)))
``` | output | 1 | 58,241 | 8 | 116,483 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n glasses on the table numbered 1, β¦, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water.
You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor.
Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled).
Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number.
For each k = 1, β¦, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of glasses.
The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 β€ b_i β€ a_i β€ 100, a_i > 0) β capacity, and water amount currently contained for the glass i, respectively.
Output
Print n real numbers β the largest amount of water that can be collected in 1, β¦, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer.
Example
Input
3
6 5
6 5
10 2
Output
7.0000000000 11.0000000000 12.0000000000
Note
In the sample case, you can act as follows:
* for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units;
* for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units;
* for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured. | instruction | 0 | 58,242 | 8 | 116,484 |
Tags: dp
Correct Solution:
```
n = int(input())
p = sorted([tuple(map(int, input().split())) for _ in range(n)], key=lambda x:(x[0], -x[1]))
dic = [[-1] * 10001 for _ in range(n + 1)]
dic[0][0] = 0
l, r, s = [0], [0], 0
for i in range(n):
dc, dv = p[i]
s += dv
for k in range(i, -1, -1):
m = -1
for c in range(r[k], l[k] - 1, -1):
if dic[k][c] == -1: continue
if dic[k][c] <= m: dic[k][c] = -1
else:
m = dic[k][c]
if dic[k][c] + dv > dic[k + 1][c + dc]:
dic[k + 1][c + dc] = dic[k][c] + dv
r[k] += dc - p[i - k][0]
l.append(l[-1] + dc)
r.append(l[-1])
print(*[max(min(c, (dic[k][c] + s) / 2) for c in range(l[k], r[k] + 1) if dic[k][c] > -1) for k in range(1, n + 1)])
``` | output | 1 | 58,242 | 8 | 116,485 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n glasses on the table numbered 1, β¦, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water.
You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor.
Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled).
Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number.
For each k = 1, β¦, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of glasses.
The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 β€ b_i β€ a_i β€ 100, a_i > 0) β capacity, and water amount currently contained for the glass i, respectively.
Output
Print n real numbers β the largest amount of water that can be collected in 1, β¦, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer.
Example
Input
3
6 5
6 5
10 2
Output
7.0000000000 11.0000000000 12.0000000000
Note
In the sample case, you can act as follows:
* for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units;
* for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units;
* for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured. | instruction | 0 | 58,243 | 8 | 116,486 |
Tags: dp
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline()
# --------------------------------------------------------------------
def RL(): return map(int, sys.stdin.readline().split())
def RLL(): return list(map(int, sys.stdin.readline().split()))
def N(): return int(input())
def S(): return input().strip()
def print_list(l):
print(' '.join(map(str,l)))
# sys.setrecursionlimit(100000)
# import random
# from functools import reduce
# from functools import lru_cache
# from heapq import *
# from collections import deque as dq
# import math
# import bisect as bs
# from collections import Counter
# from collections import defaultdict as dc
n = N()
p = sorted([tuple(RL()) for _ in range(n)], key=lambda x:x[0])
S = sum(v[1] for v in p)
dic = [[-1] * 10001 for _ in range(n + 1)]
dic[0][0] = 0
l, r = [0], [0]
for i in range(n):
dc, dv = p[i]
for k in range(i, -1, -1):
t = -1
for c in range(r[k], l[k] - 1, -1):
if dic[k][c] <= t: dic[k][c] = -1
else:
t = dic[k][c]
if dic[k][c] + dv > dic[k + 1][c + dc]:
dic[k + 1][c + dc] = dic[k][c] + dv
r[k] += dc - p[i - k][0]
l.append(l[-1] + dc)
r.append(l[-1])
print(*[max(min(c, (dic[k][c] + S) / 2) for c in range(l[k], r[k] + 1) if dic[k][c] > -1) for k in range(1, n + 1)])
``` | output | 1 | 58,243 | 8 | 116,487 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n glasses on the table numbered 1, β¦, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water.
You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor.
Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled).
Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number.
For each k = 1, β¦, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of glasses.
The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 β€ b_i β€ a_i β€ 100, a_i > 0) β capacity, and water amount currently contained for the glass i, respectively.
Output
Print n real numbers β the largest amount of water that can be collected in 1, β¦, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer.
Example
Input
3
6 5
6 5
10 2
Output
7.0000000000 11.0000000000 12.0000000000
Note
In the sample case, you can act as follows:
* for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units;
* for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units;
* for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured. | instruction | 0 | 58,244 | 8 | 116,488 |
Tags: dp
Correct Solution:
```
n=int(input())
d=[[-10**8]*(10002) for _ in range(n+1)]
d[0][0]=0
s=0
for i in range(n):
a,b=map(int,input().split())
s+=b
for k in range(n-1,-1,-1):
for c in range(10001-a,-1,-1):
d[k+1][c+a]=max(d[k+1][c+a],d[k][c]+b)
ans = [0 for i in range(n+1)]
for j in range(1,n+1):
maxi = 0
for i in range(10002):
con = d[j][i]
maxi = max(maxi,min(i, (con+s)/2))
ans[j]=maxi
for i in range(1,n+1):
print(ans[i],end=" ")
#11
``` | output | 1 | 58,244 | 8 | 116,489 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n glasses on the table numbered 1, β¦, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water.
You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor.
Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled).
Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number.
For each k = 1, β¦, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of glasses.
The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 β€ b_i β€ a_i β€ 100, a_i > 0) β capacity, and water amount currently contained for the glass i, respectively.
Output
Print n real numbers β the largest amount of water that can be collected in 1, β¦, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer.
Example
Input
3
6 5
6 5
10 2
Output
7.0000000000 11.0000000000 12.0000000000
Note
In the sample case, you can act as follows:
* for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units;
* for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units;
* for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured.
Submitted Solution:
```
"""
#If FastIO not needed, use this and don't forget to strip
#import sys, math
#input = sys.stdin.readline
"""
import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
import time
from types import GeneratorType
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
from collections import defaultdict as dd, deque as dq, Counter as dc
import math, string
start_time = time.time()
def getInts():
return [int(s) for s in input().split()]
def getInt():
return int(input())
def getStrs():
return [s for s in input().split()]
def getStr():
return input()
def listStr():
return list(input())
def getMat(n):
return [getInts() for _ in range(n)]
def isInt(s):
return '0' <= s[0] <= '9'
MOD = 998244353
"""
K = N is the best case, everything stays put
What is the max water we can possibly attain?
"""
def solve():
N = getInt()
dp = [[-10**18 for j in range(10002)] for i in range(N+1)]
dp[0][0] = 0
glasses = getMat(N)
TV = sum([b for a,b in glasses])
#print(glasses,TV)
for i in range(N):
a,b = glasses[i]
for j in range(i,-1,-1):
for k in range(100*j,-1,-1):
if dp[j][k] > -10**18: dp[j+1][k+a] = max(dp[j][k]+b,dp[j+1][k+a])
ans = []
for i in range(1,N+1):
tmp = 0
for j in range(100*i+1):
tmp = max(tmp,min(j,(dp[i][j]+TV)/2))
ans.append(tmp)
print(*ans)
return
#for _ in range(getInt()):
#print(solve())
solve()
#print(time.time()-start_time)
``` | instruction | 0 | 58,245 | 8 | 116,490 |
Yes | output | 1 | 58,245 | 8 | 116,491 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n glasses on the table numbered 1, β¦, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water.
You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor.
Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled).
Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number.
For each k = 1, β¦, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of glasses.
The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 β€ b_i β€ a_i β€ 100, a_i > 0) β capacity, and water amount currently contained for the glass i, respectively.
Output
Print n real numbers β the largest amount of water that can be collected in 1, β¦, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer.
Example
Input
3
6 5
6 5
10 2
Output
7.0000000000 11.0000000000 12.0000000000
Note
In the sample case, you can act as follows:
* for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units;
* for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units;
* for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured.
Submitted Solution:
```
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
def main():
n = int(stdin.readline())
g = [(0,0)]
need = [(0,0)]
for _ in range(n):
g.append(list(map(int, stdin.readline().split())))
need.append(g[-1][0] - g[-1][1])
sm = sum([i[1] for i in g])
dp = [[-1 for _ in range(sm+1)] for _ in range(n+1)]
for i in range(n,0,-1):
dp[i][sm] = 0
ans = []
for i in range(n):
mx = -1
temp_dp = [[-1 for _ in range(sm+1)] for kx in range(n+1-i)]
for j in range(n-i, 0, -1):
mxs = -1
for k,v in enumerate(dp[j]):
if v != -1:
fillable = min(need[j], ((k - g[j][1])/2))
pos = k - int(fillable * 2) - g[j][1]
temp_dp[j-1][pos] = max(temp_dp[j-1][pos], v + g[j][1] + fillable)
temp_dp[j][k] = max(temp_dp[j][k], v)
dp[j-1][k] = max(dp[j-1][k], v)
mx = max(mx, temp_dp[j-1][pos], temp_dp[j][k])
dp = temp_dp
ans.append(str(mx))
print(" ".join(ans))
main()
``` | instruction | 0 | 58,246 | 8 | 116,492 |
Yes | output | 1 | 58,246 | 8 | 116,493 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n glasses on the table numbered 1, β¦, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water.
You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor.
Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled).
Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number.
For each k = 1, β¦, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of glasses.
The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 β€ b_i β€ a_i β€ 100, a_i > 0) β capacity, and water amount currently contained for the glass i, respectively.
Output
Print n real numbers β the largest amount of water that can be collected in 1, β¦, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer.
Example
Input
3
6 5
6 5
10 2
Output
7.0000000000 11.0000000000 12.0000000000
Note
In the sample case, you can act as follows:
* for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units;
* for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units;
* for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured.
Submitted Solution:
```
n = int(input())
k = []
dics = []
for _ in range(n):
a, b = map(int, input().split())
k.append([a, b])
dics.append({})
sums = 0
for i in range(n):
sums += k[i][1]
for i in range(n):
r1 = k[i][0]
r2 = k[i][1]
for j in range(n - 1, -1, -1):
for t in dics[j]:
if t + r1 in dics[j + 1]:
dics[j + 1][t + r1] = max(dics[j + 1][t + r1], dics[j][t] + r2)
else:
dics[j + 1][t + r1] = dics[j][t] + r2
if r1 in dics[0]:
dics[0][r1] = max(dics[0][r1], r2)
else:
dics[0][r1] = r2
ans = []
for i in range(n):
tmp = 0
for j in dics[i]:
tmp = max(min((sums + dics[i][j]) / 2, j), tmp)
ans.append(str(tmp))
print(' '.join(ans))
``` | instruction | 0 | 58,247 | 8 | 116,494 |
Yes | output | 1 | 58,247 | 8 | 116,495 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n glasses on the table numbered 1, β¦, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water.
You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor.
Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled).
Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number.
For each k = 1, β¦, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of glasses.
The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 β€ b_i β€ a_i β€ 100, a_i > 0) β capacity, and water amount currently contained for the glass i, respectively.
Output
Print n real numbers β the largest amount of water that can be collected in 1, β¦, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer.
Example
Input
3
6 5
6 5
10 2
Output
7.0000000000 11.0000000000 12.0000000000
Note
In the sample case, you can act as follows:
* for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units;
* for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units;
* for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured.
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline()
# --------------------------------------------------------------------
def RL(): return map(int, sys.stdin.readline().split())
def RLL(): return list(map(int, sys.stdin.readline().split()))
def N(): return int(input())
def S(): return input().strip()
def print_list(l):
print(' '.join(map(str,l)))
# sys.setrecursionlimit(100000)
# import random
# from functools import reduce
# from functools import lru_cache
# from heapq import *
# from collections import deque as dq
# import math
# import bisect as bs
# from collections import Counter
# from collections import defaultdict as dc
n = N()
sn = set(range(n))
p = [tuple(RL()) for _ in range(n)]
S = sum(v[1] for v in p)
dic = [(0, set()) for _ in range(10001)]
res = []
k = 0
while k < n:
for j in range(k * 100, -1, -1):
if len(dic[j][1]) != k: continue
vj, sj = dic[j][0], dic[j][1]
for i in sn ^ sj:
if dic[j + p[i][0]][0] <= p[i][1] + vj:
dic[j + p[i][0]] = (p[i][1] + vj, sj | {i})
k += 1
m, t = 0, -1
for j in range(k * 100, 0, -1):
if len(dic[j][1]) != k: continue
if dic[j][0] <= t:
dic[j] = (0, set())
else:
t = dic[j][0]
m = max(m, min(j, (S + t) / 2))
res.append(m)
print(*res)
``` | instruction | 0 | 58,248 | 8 | 116,496 |
Yes | output | 1 | 58,248 | 8 | 116,497 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n glasses on the table numbered 1, β¦, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water.
You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor.
Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled).
Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number.
For each k = 1, β¦, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of glasses.
The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 β€ b_i β€ a_i β€ 100, a_i > 0) β capacity, and water amount currently contained for the glass i, respectively.
Output
Print n real numbers β the largest amount of water that can be collected in 1, β¦, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer.
Example
Input
3
6 5
6 5
10 2
Output
7.0000000000 11.0000000000 12.0000000000
Note
In the sample case, you can act as follows:
* for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units;
* for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units;
* for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured.
Submitted Solution:
```
import sys
# from datetime import datetime
# start = datetime.now()
def load_sys():
return sys.stdin.readlines()
def load_local():
with open('input.txt','r') as f:
input = f.readlines()
return input
def ghs(n,glasses):
B = sum(x[1] for x in glasses)
Q = sum(x[0] for x in glasses)
odp = [[float('-inf') for A in range(Q+1)] for m in range(n+1)]
ans = [float('-inf')]*(n+1)
for i in range(n):
for m in range(1,n+1):
for A in range(Q+1):
if m == 1:
if glasses[i][0] == A:
odp[m][A] = max(odp[m][A],glasses[i][1])
else:
odp[m][A] = max(odp[m][A],odp[m-1][A-glasses[i][0]] + glasses[i][1])
if i == n-1:
ans[m] = max(ans[m], min(A, (odp[m][A] + B)/2))
#odp = dp[:]
return ' '.join(str(n) for n in ans[1:])
#input = load_local()
input = load_sys()
N = int(input[0])
glasses = []
for i in range(1,N+1):
input[i] = input[i].split()
glasses.append([int(x) for x in input[i]])
print(ghs(N,glasses))
#print(datetime.now() - start)
``` | instruction | 0 | 58,249 | 8 | 116,498 |
No | output | 1 | 58,249 | 8 | 116,499 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n glasses on the table numbered 1, β¦, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water.
You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor.
Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled).
Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number.
For each k = 1, β¦, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of glasses.
The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 β€ b_i β€ a_i β€ 100, a_i > 0) β capacity, and water amount currently contained for the glass i, respectively.
Output
Print n real numbers β the largest amount of water that can be collected in 1, β¦, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer.
Example
Input
3
6 5
6 5
10 2
Output
7.0000000000 11.0000000000 12.0000000000
Note
In the sample case, you can act as follows:
* for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units;
* for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units;
* for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured.
Submitted Solution:
```
import sys
input = sys.stdin.readline
from math import gcd
def main():
n = int(input())
inf = 10 ** 30
dp = [[-inf for _ in range(10001)] for _ in range(n + 1)]
dp[0][0] = 0
total = 0
for _ in range(n):
b, a = map(int, input().split())
total += a
for i in range(n, 0, -1):
for j in range(a, 10001):
dp[i][j] = max(dp[i][j], dp[i - 1][j - a] + b)
ans = []
for i in range(1, n + 1):
tmp = 0
for a, b in enumerate(dp[i]):
sub = a + (total - a) / 2
if sub <= b:
tmp = max(tmp, sub)
ans.append(tmp)
print(*ans)
for _ in range(1):
main()
``` | instruction | 0 | 58,250 | 8 | 116,500 |
No | output | 1 | 58,250 | 8 | 116,501 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n glasses on the table numbered 1, β¦, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water.
You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor.
Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled).
Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number.
For each k = 1, β¦, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of glasses.
The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 β€ b_i β€ a_i β€ 100, a_i > 0) β capacity, and water amount currently contained for the glass i, respectively.
Output
Print n real numbers β the largest amount of water that can be collected in 1, β¦, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer.
Example
Input
3
6 5
6 5
10 2
Output
7.0000000000 11.0000000000 12.0000000000
Note
In the sample case, you can act as follows:
* for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units;
* for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units;
* for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured.
Submitted Solution:
```
import io
import os
from collections import Counter, defaultdict, deque
def solve(N, AB):
total = sum(b for a, b in AB)
ans = [0.0] * (N + 1)
AB.sort()
prefA = [0.0]
prefB = [0.0]
for a, b in AB:
prefA.append(prefA[-1] + a)
prefB.append(prefB[-1] + b)
for i in range(N + 1):
take = prefB[i]
cap = prefA[i]
ans[i] = max(ans[i], min((total + take) / 2.0, cap))
take = prefB[-1] - prefB[N - i]
cap = prefA[-1] - prefA[N - i]
ans[i] = max(ans[i], min((total + take) / 2.0, cap))
return " ".join(str(ans[i + 1]) for i in range(N))
def solveBrute(N, AB):
# A is capacity, B is amount
ans = [0.0] * (N + 1)
total = sum(b for a, b in AB)
possible = set([(0, 0, 0)])
for a, b in AB:
for take, cap, count in list(possible):
possible.add((take + b, cap + a, count + 1))
for take, cap, count in possible:
extra = (total - take) / 2
remainingCap = cap - take
temp = take + min(extra, remainingCap)
temp2 = take + min((total - take) / 2, cap - take)
temp3 = min((total + take) / 2, cap)
temp4 = min(total + take, cap * 2) / 2.0
assert temp == temp2 == temp3 == temp4
ans[count] = max(ans[count], temp)
return " ".join(str(float(ans[i + 1])) for i in range(N))
DEBUG = False
if DEBUG:
import random
random.seed(0)
for _ in range(100):
N = random.randint(1, 10)
AB = [sorted([random.randint(1, 10), random.randint(1, 10)]) for i in range(N)]
print("tc", _, N, AB)
ans1 = solve(N, AB)
ans2 = solveBrute(N, AB)
print(ans1)
print(ans2)
assert ans1 == ans2
exit()
if __name__ == "__main__":
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = 1 # int(input())
for tc in range(1, TC + 1):
(N,) = [int(x) for x in input().split()]
AB = [[int(x) for x in input().split()] for i in range(N)]
ans = solve(N, AB)
print(ans)
``` | instruction | 0 | 58,251 | 8 | 116,502 |
No | output | 1 | 58,251 | 8 | 116,503 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n glasses on the table numbered 1, β¦, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water.
You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor.
Formally, suppose a glass i currently contains c_i units of water, and a glass j contains c_j units of water. Suppose you try to transfer x units from glass i to glass j (naturally, x can not exceed c_i). Then, x / 2 units is spilled on the floor. After the transfer is done, the glass i will contain c_i - x units, and the glass j will contain min(a_j, c_j + x / 2) units (excess water that doesn't fit in the glass is also spilled).
Each time you transfer water, you can arbitrarlly choose from which glass i to which glass j to pour, and also the amount x transferred can be any positive real number.
For each k = 1, β¦, n, determine the largest possible total amount of water that can be collected in arbitrarily chosen k glasses after transferring water between glasses zero or more times.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of glasses.
The following n lines describe the glasses. The i-th of these lines contains two integers a_i and b_i (0 β€ b_i β€ a_i β€ 100, a_i > 0) β capacity, and water amount currently contained for the glass i, respectively.
Output
Print n real numbers β the largest amount of water that can be collected in 1, β¦, n glasses respectively. Your answer will be accepted if each number is within 10^{-9} absolute or relative tolerance of the precise answer.
Example
Input
3
6 5
6 5
10 2
Output
7.0000000000 11.0000000000 12.0000000000
Note
In the sample case, you can act as follows:
* for k = 1, transfer water from the first two glasses to the third one, spilling (5 + 5) / 2 = 5 units and securing 2 + (5 + 5) / 2 = 7 units;
* for k = 2, transfer water from the third glass to any of the first two, spilling 2 / 2 = 1 unit and securing 5 + 5 + 2 / 2 = 11 units;
* for k = 3, do nothing. All 5 + 5 + 2 = 12 units are secured.
Submitted Solution:
```
"""
#If FastIO not needed, use this and don't forget to strip
#import sys, math
#input = sys.stdin.readline
"""
import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
import time
from types import GeneratorType
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
from collections import defaultdict as dd, deque as dq, Counter as dc
import math, string
start_time = time.time()
def getInts():
return [int(s) for s in input().split()]
def getInt():
return int(input())
def getStrs():
return [s for s in input().split()]
def getStr():
return input()
def listStr():
return list(input())
def getMat(n):
return [getInts() for _ in range(n)]
def isInt(s):
return '0' <= s[0] <= '9'
MOD = 998244353
"""
K = N is the best case, everything stays put
What is the max water we can possibly attain?
"""
def solve():
N = getInt()
glasses = []
A = []
B = []
for n in range(N):
a, b = getInts()
a, b = float(a), float(b)
glasses.append((a,b))
A.append(a)
B.append(b)
ans = []
curr = sum(B)
available = [a-b for a,b in glasses]
curr_available = sum(available)
for n in range(N-1,-1,-1):
if n == N-1:
ans.append(curr)
continue
best = -10**18
best_ind = -1
#find the glass that stands to lose the least
for i in range(len(available)):
this_av = curr_available - available[i]
gain = min(glasses[i][1]/2,this_av)-glasses[i][1]
if gain > best:
best = gain
best_ind = i
glasses.pop(best_ind)
curr_available -= available[best_ind]
available.pop(best_ind)
curr += gain
ans.append(curr)
ans.reverse()
print(*ans)
return
#for _ in range(getInt()):
#print(solve())
solve()
#print(time.time()-start_time)
``` | instruction | 0 | 58,252 | 8 | 116,504 |
No | output | 1 | 58,252 | 8 | 116,505 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you).
<image>
You are given an array h_1, h_2, ..., h_n, where h_i is the height of the i-th mountain, and k β the number of boulders you have.
You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is h_i):
* if h_i β₯ h_{i + 1}, the boulder will roll to the next mountain;
* if h_i < h_{i + 1}, the boulder will stop rolling and increase the mountain height by 1 (h_i = h_i + 1);
* if the boulder reaches the last mountain it will fall to the waste collection system and disappear.
You want to find the position of the k-th boulder or determine that it will fall into the waste collection system.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
Each test case consists of two lines. The first line in each test case contains two integers n and k (1 β€ n β€ 100; 1 β€ k β€ 10^9) β the number of mountains and the number of boulders.
The second line contains n integers h_1, h_2, ..., h_n (1 β€ h_i β€ 100) β the height of the mountains.
It is guaranteed that the sum of n over all test cases does not exceed 100.
Output
For each test case, print -1 if the k-th boulder will fall into the collection system. Otherwise, print the position of the k-th boulder.
Example
Input
4
4 3
4 1 2 3
2 7
1 8
4 5
4 1 2 3
3 1
5 3 1
Output
2
1
-1
-1
Note
Let's simulate the first case:
* The first boulder starts at i = 1; since h_1 β₯ h_2 it rolls to i = 2 and stops there because h_2 < h_3.
* The new heights are [4,2,2,3].
* The second boulder starts at i = 1; since h_1 β₯ h_2 the boulder rolls to i = 2; since h_2 β₯ h_3 the boulder rolls to i = 3 and stops there because h_3 < h_4.
* The new heights are [4,2,3,3].
* The third boulder starts at i = 1; since h_1 β₯ h_2 it rolls to i = 2 and stops there because h_2 < h_3.
* The new heights are [4,3,3,3].
The positions where each boulder stopped are the following: [2,3,2].
In the second case, all 7 boulders will stop right at the first mountain rising its height from 1 to 8.
The third case is similar to the first one but now you'll throw 5 boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to [4, 3, 3, 3], that's why the other two boulders will fall into the collection system.
In the fourth case, the first and only boulders will fall straight into the collection system. | instruction | 0 | 58,253 | 8 | 116,506 |
Tags: brute force, greedy, implementation
Correct Solution:
```
n = int(input())
for i in range(n):
height, rocks = map(int, input().split())
throwHeight = list(map(int, input().split()))
throwHeight.append('$')
for j in range(rocks):
for k in range(height):
if throwHeight[k + 1] == '$':
ans = -1
break
elif throwHeight[k] >= throwHeight[k + 1]:
continue
elif throwHeight[k] < throwHeight[k + 1]:
throwHeight[k] = throwHeight[k] + 1
ans = k + 1
break
if ans == -1:
break
print(ans)
``` | output | 1 | 58,253 | 8 | 116,507 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you).
<image>
You are given an array h_1, h_2, ..., h_n, where h_i is the height of the i-th mountain, and k β the number of boulders you have.
You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is h_i):
* if h_i β₯ h_{i + 1}, the boulder will roll to the next mountain;
* if h_i < h_{i + 1}, the boulder will stop rolling and increase the mountain height by 1 (h_i = h_i + 1);
* if the boulder reaches the last mountain it will fall to the waste collection system and disappear.
You want to find the position of the k-th boulder or determine that it will fall into the waste collection system.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
Each test case consists of two lines. The first line in each test case contains two integers n and k (1 β€ n β€ 100; 1 β€ k β€ 10^9) β the number of mountains and the number of boulders.
The second line contains n integers h_1, h_2, ..., h_n (1 β€ h_i β€ 100) β the height of the mountains.
It is guaranteed that the sum of n over all test cases does not exceed 100.
Output
For each test case, print -1 if the k-th boulder will fall into the collection system. Otherwise, print the position of the k-th boulder.
Example
Input
4
4 3
4 1 2 3
2 7
1 8
4 5
4 1 2 3
3 1
5 3 1
Output
2
1
-1
-1
Note
Let's simulate the first case:
* The first boulder starts at i = 1; since h_1 β₯ h_2 it rolls to i = 2 and stops there because h_2 < h_3.
* The new heights are [4,2,2,3].
* The second boulder starts at i = 1; since h_1 β₯ h_2 the boulder rolls to i = 2; since h_2 β₯ h_3 the boulder rolls to i = 3 and stops there because h_3 < h_4.
* The new heights are [4,2,3,3].
* The third boulder starts at i = 1; since h_1 β₯ h_2 it rolls to i = 2 and stops there because h_2 < h_3.
* The new heights are [4,3,3,3].
The positions where each boulder stopped are the following: [2,3,2].
In the second case, all 7 boulders will stop right at the first mountain rising its height from 1 to 8.
The third case is similar to the first one but now you'll throw 5 boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to [4, 3, 3, 3], that's why the other two boulders will fall into the collection system.
In the fourth case, the first and only boulders will fall straight into the collection system. | instruction | 0 | 58,254 | 8 | 116,508 |
Tags: brute force, greedy, implementation
Correct Solution:
```
q = int(input())
for i in range(q):
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
ans = 0
if n==1:
ans =-1
for j in range(k):
if ans > -1:
for x in range(1, n):
if a[x - 1] < a[x]:
a[x - 1] += 1
ans = x
break
if x == n - 1:
ans = -1
break
else:
break
print(ans)
``` | output | 1 | 58,254 | 8 | 116,509 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you).
<image>
You are given an array h_1, h_2, ..., h_n, where h_i is the height of the i-th mountain, and k β the number of boulders you have.
You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is h_i):
* if h_i β₯ h_{i + 1}, the boulder will roll to the next mountain;
* if h_i < h_{i + 1}, the boulder will stop rolling and increase the mountain height by 1 (h_i = h_i + 1);
* if the boulder reaches the last mountain it will fall to the waste collection system and disappear.
You want to find the position of the k-th boulder or determine that it will fall into the waste collection system.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
Each test case consists of two lines. The first line in each test case contains two integers n and k (1 β€ n β€ 100; 1 β€ k β€ 10^9) β the number of mountains and the number of boulders.
The second line contains n integers h_1, h_2, ..., h_n (1 β€ h_i β€ 100) β the height of the mountains.
It is guaranteed that the sum of n over all test cases does not exceed 100.
Output
For each test case, print -1 if the k-th boulder will fall into the collection system. Otherwise, print the position of the k-th boulder.
Example
Input
4
4 3
4 1 2 3
2 7
1 8
4 5
4 1 2 3
3 1
5 3 1
Output
2
1
-1
-1
Note
Let's simulate the first case:
* The first boulder starts at i = 1; since h_1 β₯ h_2 it rolls to i = 2 and stops there because h_2 < h_3.
* The new heights are [4,2,2,3].
* The second boulder starts at i = 1; since h_1 β₯ h_2 the boulder rolls to i = 2; since h_2 β₯ h_3 the boulder rolls to i = 3 and stops there because h_3 < h_4.
* The new heights are [4,2,3,3].
* The third boulder starts at i = 1; since h_1 β₯ h_2 it rolls to i = 2 and stops there because h_2 < h_3.
* The new heights are [4,3,3,3].
The positions where each boulder stopped are the following: [2,3,2].
In the second case, all 7 boulders will stop right at the first mountain rising its height from 1 to 8.
The third case is similar to the first one but now you'll throw 5 boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to [4, 3, 3, 3], that's why the other two boulders will fall into the collection system.
In the fourth case, the first and only boulders will fall straight into the collection system. | instruction | 0 | 58,255 | 8 | 116,510 |
Tags: brute force, greedy, implementation
Correct Solution:
```
t = int(input())
for _ in range(t):
a,b = map(int,input().split())
l = list(map(int,input().split()))
pos = 0
for i in range(1,a):
while(l[i]>l[i-1]):
for j in range(i-1,-1,-1):
l[j]+=1
pos+=1
if pos>=b:
print(j+1)
pos = -1
break
if(j!=0 and l[j]<=l[j-1]):
break
if pos== -1:
break
if pos == -1:
break
if pos == -1:
continue
print(-1)
``` | output | 1 | 58,255 | 8 | 116,511 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you).
<image>
You are given an array h_1, h_2, ..., h_n, where h_i is the height of the i-th mountain, and k β the number of boulders you have.
You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is h_i):
* if h_i β₯ h_{i + 1}, the boulder will roll to the next mountain;
* if h_i < h_{i + 1}, the boulder will stop rolling and increase the mountain height by 1 (h_i = h_i + 1);
* if the boulder reaches the last mountain it will fall to the waste collection system and disappear.
You want to find the position of the k-th boulder or determine that it will fall into the waste collection system.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
Each test case consists of two lines. The first line in each test case contains two integers n and k (1 β€ n β€ 100; 1 β€ k β€ 10^9) β the number of mountains and the number of boulders.
The second line contains n integers h_1, h_2, ..., h_n (1 β€ h_i β€ 100) β the height of the mountains.
It is guaranteed that the sum of n over all test cases does not exceed 100.
Output
For each test case, print -1 if the k-th boulder will fall into the collection system. Otherwise, print the position of the k-th boulder.
Example
Input
4
4 3
4 1 2 3
2 7
1 8
4 5
4 1 2 3
3 1
5 3 1
Output
2
1
-1
-1
Note
Let's simulate the first case:
* The first boulder starts at i = 1; since h_1 β₯ h_2 it rolls to i = 2 and stops there because h_2 < h_3.
* The new heights are [4,2,2,3].
* The second boulder starts at i = 1; since h_1 β₯ h_2 the boulder rolls to i = 2; since h_2 β₯ h_3 the boulder rolls to i = 3 and stops there because h_3 < h_4.
* The new heights are [4,2,3,3].
* The third boulder starts at i = 1; since h_1 β₯ h_2 it rolls to i = 2 and stops there because h_2 < h_3.
* The new heights are [4,3,3,3].
The positions where each boulder stopped are the following: [2,3,2].
In the second case, all 7 boulders will stop right at the first mountain rising its height from 1 to 8.
The third case is similar to the first one but now you'll throw 5 boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to [4, 3, 3, 3], that's why the other two boulders will fall into the collection system.
In the fourth case, the first and only boulders will fall straight into the collection system. | instruction | 0 | 58,256 | 8 | 116,512 |
Tags: brute force, greedy, implementation
Correct Solution:
```
R=lambda:map(int,input().split())
t,=R()
for _ in[0]*t:
n,k=R();a=[*R(),101];i=1
while i%n*k:
i=1
while a[i-1]>=a[i]:i+=1
a[i-1]+=1;k-=1
print(i%n or-1)
``` | output | 1 | 58,256 | 8 | 116,513 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you).
<image>
You are given an array h_1, h_2, ..., h_n, where h_i is the height of the i-th mountain, and k β the number of boulders you have.
You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is h_i):
* if h_i β₯ h_{i + 1}, the boulder will roll to the next mountain;
* if h_i < h_{i + 1}, the boulder will stop rolling and increase the mountain height by 1 (h_i = h_i + 1);
* if the boulder reaches the last mountain it will fall to the waste collection system and disappear.
You want to find the position of the k-th boulder or determine that it will fall into the waste collection system.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
Each test case consists of two lines. The first line in each test case contains two integers n and k (1 β€ n β€ 100; 1 β€ k β€ 10^9) β the number of mountains and the number of boulders.
The second line contains n integers h_1, h_2, ..., h_n (1 β€ h_i β€ 100) β the height of the mountains.
It is guaranteed that the sum of n over all test cases does not exceed 100.
Output
For each test case, print -1 if the k-th boulder will fall into the collection system. Otherwise, print the position of the k-th boulder.
Example
Input
4
4 3
4 1 2 3
2 7
1 8
4 5
4 1 2 3
3 1
5 3 1
Output
2
1
-1
-1
Note
Let's simulate the first case:
* The first boulder starts at i = 1; since h_1 β₯ h_2 it rolls to i = 2 and stops there because h_2 < h_3.
* The new heights are [4,2,2,3].
* The second boulder starts at i = 1; since h_1 β₯ h_2 the boulder rolls to i = 2; since h_2 β₯ h_3 the boulder rolls to i = 3 and stops there because h_3 < h_4.
* The new heights are [4,2,3,3].
* The third boulder starts at i = 1; since h_1 β₯ h_2 it rolls to i = 2 and stops there because h_2 < h_3.
* The new heights are [4,3,3,3].
The positions where each boulder stopped are the following: [2,3,2].
In the second case, all 7 boulders will stop right at the first mountain rising its height from 1 to 8.
The third case is similar to the first one but now you'll throw 5 boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to [4, 3, 3, 3], that's why the other two boulders will fall into the collection system.
In the fourth case, the first and only boulders will fall straight into the collection system. | instruction | 0 | 58,257 | 8 | 116,514 |
Tags: brute force, greedy, implementation
Correct Solution:
```
import heapq
for _ in range(int(input())):
n, k = map(int, input().split())
l = list(map(int, input().split()))
diff = []
for i in range(n-1):
diff.append([l[i]-l[i+1], i+1])
# diff = list(map(lambda x: [abs(x[0]), x[1]], diff))
# print(diff)
heap = []
heapq.heapify(heap)
for i in diff:
if(i[0]<0):
heapq.heappush(heap, i[1])
while(heap!=[] and k!=0):
ind = heapq.heappop(heap)
k-=1
diff[ind-1][0]+=1
if(diff[ind-1][0]<0):
heapq.heappush(heap, diff[ind-1][1])
if(ind>1):
diff[ind-2][0]-=1
if(diff[ind-2][0]<0):
heapq.heappush(heap, diff[ind-2][1])
if(k==0):
print(ind)
else:
print(-1)
``` | output | 1 | 58,257 | 8 | 116,515 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you).
<image>
You are given an array h_1, h_2, ..., h_n, where h_i is the height of the i-th mountain, and k β the number of boulders you have.
You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is h_i):
* if h_i β₯ h_{i + 1}, the boulder will roll to the next mountain;
* if h_i < h_{i + 1}, the boulder will stop rolling and increase the mountain height by 1 (h_i = h_i + 1);
* if the boulder reaches the last mountain it will fall to the waste collection system and disappear.
You want to find the position of the k-th boulder or determine that it will fall into the waste collection system.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
Each test case consists of two lines. The first line in each test case contains two integers n and k (1 β€ n β€ 100; 1 β€ k β€ 10^9) β the number of mountains and the number of boulders.
The second line contains n integers h_1, h_2, ..., h_n (1 β€ h_i β€ 100) β the height of the mountains.
It is guaranteed that the sum of n over all test cases does not exceed 100.
Output
For each test case, print -1 if the k-th boulder will fall into the collection system. Otherwise, print the position of the k-th boulder.
Example
Input
4
4 3
4 1 2 3
2 7
1 8
4 5
4 1 2 3
3 1
5 3 1
Output
2
1
-1
-1
Note
Let's simulate the first case:
* The first boulder starts at i = 1; since h_1 β₯ h_2 it rolls to i = 2 and stops there because h_2 < h_3.
* The new heights are [4,2,2,3].
* The second boulder starts at i = 1; since h_1 β₯ h_2 the boulder rolls to i = 2; since h_2 β₯ h_3 the boulder rolls to i = 3 and stops there because h_3 < h_4.
* The new heights are [4,2,3,3].
* The third boulder starts at i = 1; since h_1 β₯ h_2 it rolls to i = 2 and stops there because h_2 < h_3.
* The new heights are [4,3,3,3].
The positions where each boulder stopped are the following: [2,3,2].
In the second case, all 7 boulders will stop right at the first mountain rising its height from 1 to 8.
The third case is similar to the first one but now you'll throw 5 boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to [4, 3, 3, 3], that's why the other two boulders will fall into the collection system.
In the fourth case, the first and only boulders will fall straight into the collection system. | instruction | 0 | 58,258 | 8 | 116,516 |
Tags: brute force, greedy, implementation
Correct Solution:
```
t = (int)(input())
for i in range(0,t):
m,n = map(int,input().split())
l = list(map(int,input().split(" ")))
maxi = 100
total = 0
for j in l:
if maxi - j > 0:
total = total+maxi-j
if(total < n):
print(-1)
continue
ans = -1
for i in range(0,n):
ans = -1
j = 0
while j < m-1 and l[j]>=l[j+1]:
j+=1
if j<m-1:
l[j]+=1
ans = j+1
else:
break
print(ans)
``` | output | 1 | 58,258 | 8 | 116,517 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you).
<image>
You are given an array h_1, h_2, ..., h_n, where h_i is the height of the i-th mountain, and k β the number of boulders you have.
You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is h_i):
* if h_i β₯ h_{i + 1}, the boulder will roll to the next mountain;
* if h_i < h_{i + 1}, the boulder will stop rolling and increase the mountain height by 1 (h_i = h_i + 1);
* if the boulder reaches the last mountain it will fall to the waste collection system and disappear.
You want to find the position of the k-th boulder or determine that it will fall into the waste collection system.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
Each test case consists of two lines. The first line in each test case contains two integers n and k (1 β€ n β€ 100; 1 β€ k β€ 10^9) β the number of mountains and the number of boulders.
The second line contains n integers h_1, h_2, ..., h_n (1 β€ h_i β€ 100) β the height of the mountains.
It is guaranteed that the sum of n over all test cases does not exceed 100.
Output
For each test case, print -1 if the k-th boulder will fall into the collection system. Otherwise, print the position of the k-th boulder.
Example
Input
4
4 3
4 1 2 3
2 7
1 8
4 5
4 1 2 3
3 1
5 3 1
Output
2
1
-1
-1
Note
Let's simulate the first case:
* The first boulder starts at i = 1; since h_1 β₯ h_2 it rolls to i = 2 and stops there because h_2 < h_3.
* The new heights are [4,2,2,3].
* The second boulder starts at i = 1; since h_1 β₯ h_2 the boulder rolls to i = 2; since h_2 β₯ h_3 the boulder rolls to i = 3 and stops there because h_3 < h_4.
* The new heights are [4,2,3,3].
* The third boulder starts at i = 1; since h_1 β₯ h_2 it rolls to i = 2 and stops there because h_2 < h_3.
* The new heights are [4,3,3,3].
The positions where each boulder stopped are the following: [2,3,2].
In the second case, all 7 boulders will stop right at the first mountain rising its height from 1 to 8.
The third case is similar to the first one but now you'll throw 5 boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to [4, 3, 3, 3], that's why the other two boulders will fall into the collection system.
In the fourth case, the first and only boulders will fall straight into the collection system. | instruction | 0 | 58,259 | 8 | 116,518 |
Tags: brute force, greedy, implementation
Correct Solution:
```
# if anyone reaches garbage colleector return -1
# else add max(arr[i+1]-arr[i],k)
# if(k<=0):
# return i
def h(k):
ret=-2
while(k>0):
ret=-2
# print(arr,k)
for i in range(len(arr)-1):
if(arr[i]<arr[i+1]):
k-=1
arr[i]+=1
ret=i
break
if(ret==-2):
break
# return ("-1")
return(ret+1)
for i in range(int(input())):
n,k=map(int,input().split())
arr=list(map(int,input().split()))
print(h(k))
``` | output | 1 | 58,259 | 8 | 116,519 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you).
<image>
You are given an array h_1, h_2, ..., h_n, where h_i is the height of the i-th mountain, and k β the number of boulders you have.
You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is h_i):
* if h_i β₯ h_{i + 1}, the boulder will roll to the next mountain;
* if h_i < h_{i + 1}, the boulder will stop rolling and increase the mountain height by 1 (h_i = h_i + 1);
* if the boulder reaches the last mountain it will fall to the waste collection system and disappear.
You want to find the position of the k-th boulder or determine that it will fall into the waste collection system.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
Each test case consists of two lines. The first line in each test case contains two integers n and k (1 β€ n β€ 100; 1 β€ k β€ 10^9) β the number of mountains and the number of boulders.
The second line contains n integers h_1, h_2, ..., h_n (1 β€ h_i β€ 100) β the height of the mountains.
It is guaranteed that the sum of n over all test cases does not exceed 100.
Output
For each test case, print -1 if the k-th boulder will fall into the collection system. Otherwise, print the position of the k-th boulder.
Example
Input
4
4 3
4 1 2 3
2 7
1 8
4 5
4 1 2 3
3 1
5 3 1
Output
2
1
-1
-1
Note
Let's simulate the first case:
* The first boulder starts at i = 1; since h_1 β₯ h_2 it rolls to i = 2 and stops there because h_2 < h_3.
* The new heights are [4,2,2,3].
* The second boulder starts at i = 1; since h_1 β₯ h_2 the boulder rolls to i = 2; since h_2 β₯ h_3 the boulder rolls to i = 3 and stops there because h_3 < h_4.
* The new heights are [4,2,3,3].
* The third boulder starts at i = 1; since h_1 β₯ h_2 it rolls to i = 2 and stops there because h_2 < h_3.
* The new heights are [4,3,3,3].
The positions where each boulder stopped are the following: [2,3,2].
In the second case, all 7 boulders will stop right at the first mountain rising its height from 1 to 8.
The third case is similar to the first one but now you'll throw 5 boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to [4, 3, 3, 3], that's why the other two boulders will fall into the collection system.
In the fourth case, the first and only boulders will fall straight into the collection system. | instruction | 0 | 58,260 | 8 | 116,520 |
Tags: brute force, greedy, implementation
Correct Solution:
```
import sys
input=sys.stdin.readline
from collections import defaultdict
t=int(input())
for _ in range(t):
n,k=map(int,input().split())
h=list(map(int,input().split()))
fall=defaultdict(lambda: -1)
cnt=0
flag=True
while flag:
for i in range(n):
if(i==n-1):
flag=False
break
if(h[i]<h[i+1]):
cnt+=1
h[i]+=1
fall[cnt]=i+1
break
print(fall[k])
``` | output | 1 | 58,260 | 8 | 116,521 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length.
A block with side a has volume a3. A tower consisting of blocks with sides a1, a2, ..., ak has the total volume a13 + a23 + ... + ak3.
Limak is going to build a tower. First, he asks you to tell him a positive integer X β the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X.
Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X.
Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X β€ m that results this number of blocks.
Input
The only line of the input contains one integer m (1 β€ m β€ 1015), meaning that Limak wants you to choose X between 1 and m, inclusive.
Output
Print two integers β the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks.
Examples
Input
48
Output
9 42
Input
6
Output
6 6
Note
In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42.
In more detail, after choosing X = 42 the process of building a tower is:
* Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15.
* The second added block has side 2, so the remaining volume is 15 - 8 = 7.
* Finally, Limak adds 7 blocks with side 1, one by one.
So, there are 9 blocks in the tower. The total volume is is 33 + 23 + 7Β·13 = 27 + 8 + 7 = 42. | instruction | 0 | 58,485 | 8 | 116,970 |
Tags: binary search, dp, greedy
Correct Solution:
```
def f(m, cnt, y):
if m == 0: return cnt, y
a = int(m ** (1/3))
k1, k2 = a ** 3, (a - 1) ** 3
return max(f(m - k1, cnt + 1, y + k1), f(k1 - k2 - 1, cnt + 1, y + k2))
print(*f(int(input()), 0, 0))
``` | output | 1 | 58,485 | 8 | 116,971 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length.
A block with side a has volume a3. A tower consisting of blocks with sides a1, a2, ..., ak has the total volume a13 + a23 + ... + ak3.
Limak is going to build a tower. First, he asks you to tell him a positive integer X β the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X.
Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X.
Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X β€ m that results this number of blocks.
Input
The only line of the input contains one integer m (1 β€ m β€ 1015), meaning that Limak wants you to choose X between 1 and m, inclusive.
Output
Print two integers β the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks.
Examples
Input
48
Output
9 42
Input
6
Output
6 6
Note
In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42.
In more detail, after choosing X = 42 the process of building a tower is:
* Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15.
* The second added block has side 2, so the remaining volume is 15 - 8 = 7.
* Finally, Limak adds 7 blocks with side 1, one by one.
So, there are 9 blocks in the tower. The total volume is is 33 + 23 + 7Β·13 = 27 + 8 + 7 = 42. | instruction | 0 | 58,486 | 8 | 116,972 |
Tags: binary search, dp, greedy
Correct Solution:
```
#D razbor 1
def func(n):
if n<8:
return n,n
max_a = int(n**(1/3))
if (max_a+1)**3<=n:
max_a += 1
v1=func(n-max_a**3)
v1=(v1[0]+1,v1[1]+max_a**3)
v2=func(max_a**3-1-(max_a-1)**3)
v2=(v2[0]+1,v2[1]+(max_a-1)**3)
if v2>v1:
return v2
else:
return v1
print(' '.join(map(str, func(int(input())))))
``` | output | 1 | 58,486 | 8 | 116,973 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length.
A block with side a has volume a3. A tower consisting of blocks with sides a1, a2, ..., ak has the total volume a13 + a23 + ... + ak3.
Limak is going to build a tower. First, he asks you to tell him a positive integer X β the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X.
Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X.
Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X β€ m that results this number of blocks.
Input
The only line of the input contains one integer m (1 β€ m β€ 1015), meaning that Limak wants you to choose X between 1 and m, inclusive.
Output
Print two integers β the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks.
Examples
Input
48
Output
9 42
Input
6
Output
6 6
Note
In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42.
In more detail, after choosing X = 42 the process of building a tower is:
* Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15.
* The second added block has side 2, so the remaining volume is 15 - 8 = 7.
* Finally, Limak adds 7 blocks with side 1, one by one.
So, there are 9 blocks in the tower. The total volume is is 33 + 23 + 7Β·13 = 27 + 8 + 7 = 42. | instruction | 0 | 58,487 | 8 | 116,974 |
Tags: binary search, dp, greedy
Correct Solution:
```
def f(m, cnt, y):
global ans, x
if m == 0: return cnt, y
a = int(m ** (1/3))
k1, k2 = a ** 3, (a - 1) ** 3
return max(f(m - k1, cnt + 1, y + k1), f(k1 - k2 - 1, cnt + 1, y + k2))
print(*f(int(input()), 0, 0))
``` | output | 1 | 58,487 | 8 | 116,975 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length.
A block with side a has volume a3. A tower consisting of blocks with sides a1, a2, ..., ak has the total volume a13 + a23 + ... + ak3.
Limak is going to build a tower. First, he asks you to tell him a positive integer X β the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X.
Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X.
Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X β€ m that results this number of blocks.
Input
The only line of the input contains one integer m (1 β€ m β€ 1015), meaning that Limak wants you to choose X between 1 and m, inclusive.
Output
Print two integers β the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks.
Examples
Input
48
Output
9 42
Input
6
Output
6 6
Note
In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42.
In more detail, after choosing X = 42 the process of building a tower is:
* Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15.
* The second added block has side 2, so the remaining volume is 15 - 8 = 7.
* Finally, Limak adds 7 blocks with side 1, one by one.
So, there are 9 blocks in the tower. The total volume is is 33 + 23 + 7Β·13 = 27 + 8 + 7 = 42. | instruction | 0 | 58,488 | 8 | 116,976 |
Tags: binary search, dp, greedy
Correct Solution:
```
from math import *
from sys import *
N=int(input())
# if N <= 7:
# print(N,N)
# elif N <= 15:
# print(N-7,N)
# elif N <= 23:
# print(N-14,N)
# else:
memo={}
def solve(n,R):
if n in memo:
return memo[n]
if n == 0:
return 0,0
s=floor(pow(n,1/3)-1)
while (s+1)**3 <= n:
s += 1
A,B=solve(n - s ** 3,5)
if (R>0):
C,D=solve(s**3-1,R-1)
else:
C,D=0,0
memo[n] = max((A+1,B+s**3),(C,D))
#print(n, A, B, C, D, memo[n])
return memo[n]
A,B=solve(N,5)
print(A,B)
``` | output | 1 | 58,488 | 8 | 116,977 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length.
A block with side a has volume a3. A tower consisting of blocks with sides a1, a2, ..., ak has the total volume a13 + a23 + ... + ak3.
Limak is going to build a tower. First, he asks you to tell him a positive integer X β the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X.
Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X.
Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X β€ m that results this number of blocks.
Input
The only line of the input contains one integer m (1 β€ m β€ 1015), meaning that Limak wants you to choose X between 1 and m, inclusive.
Output
Print two integers β the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks.
Examples
Input
48
Output
9 42
Input
6
Output
6 6
Note
In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42.
In more detail, after choosing X = 42 the process of building a tower is:
* Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15.
* The second added block has side 2, so the remaining volume is 15 - 8 = 7.
* Finally, Limak adds 7 blocks with side 1, one by one.
So, there are 9 blocks in the tower. The total volume is is 33 + 23 + 7Β·13 = 27 + 8 + 7 = 42. | instruction | 0 | 58,489 | 8 | 116,978 |
Tags: binary search, dp, greedy
Correct Solution:
```
def kil(n):
if n<8: return (n,n)
m=int(n**(1/3))
k1,v1=kil(n-m*m*m)
k2,v2=kil(m*m*m-1-(m-1)*(m-1)*(m-1))
return (k1+1,v1+m*m*m) if (k1,v1+m*m*m)>(k2,v2+(m-1)*(m-1)*(m-1)) else (k2+1,v2+(m-1)*(m-1)*(m-1))
n=int(input())
print(*kil(n))
``` | output | 1 | 58,489 | 8 | 116,979 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.