message stringlengths 2 30.5k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 237 109k | cluster float64 10 10 | __index_level_0__ int64 474 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Your search for Heidi is over – you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are n different books, numbered 1 through n.
We will look at the library's operation during n consecutive days. Heidi knows in advance that on the i-th day (1 ≤ i ≤ n) precisely one person will come to the library, request to borrow the book ai, read it in a few hours, and return the book later on the same day.
Heidi desperately wants to please all her guests, so she will make sure to always have the book ai available in the library on the i-th day. During the night before the i-th day, she has the option of going to the bookstore (which operates at nights to avoid competition with the library) and buying any book for the price of 1 CHF. Of course, if she already has a book at the library, she does not need to buy it again. Initially, the library contains no books.
There is a problem, though. The capacity of the library is k – this means that at any time, there can be at most k books at the library. If buying a new book would cause Heidi to have more than k books, she must first get rid of some book that she already has, in order to make room for the new book. If she later needs a book that she got rid of, she will need to buy that book again.
You are given k and the sequence of requests for books a1, a2, ..., an. What is the minimum cost (in CHF) of buying new books to satisfy all the requests?
Input
The first line of input will contain two integers n and k (1 ≤ n, k ≤ 80). The second line will contain n integers a1, a2, ..., an (1 ≤ ai ≤ n) – the sequence of book requests.
Output
On a single line print the minimum cost of buying books at the store so as to satisfy all requests.
Examples
Input
4 80
1 2 2 1
Output
2
Input
4 1
1 2 2 1
Output
3
Input
4 2
1 2 3 1
Output
3
Note
In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day.
In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day.
In the third test case, before buying book 3 on the third day, she must decide which of the books 1 and 2 she should get rid of. Of course, she should keep the book 1, which will be requested on the fourth day. | instruction | 0 | 23,420 | 10 | 46,840 |
Tags: greedy
Correct Solution:
```
import sys
input = sys.stdin.readline
from collections import deque, defaultdict
n, k = map(int, input().split())
A = list(map(int, input().split()))
dic = defaultdict(deque)
for i, a in enumerate(A):
dic[a].append(i)
S = set()
ans = 0
for i, a in enumerate(A):
for d in dic:
while dic[d] and dic[d][0] <= i:
dic[d].popleft()
if a not in S:
if len(S) < k:
S.add(a)
ans += 1
else:
day = i
for s in S:
if s not in dic or not dic[s]:
idx = s
break
else:
if dic[s][0] > day:
day = dic[s][0]
idx = s
S.remove(idx)
S.add(a)
ans += 1
print(ans)
``` | output | 1 | 23,420 | 10 | 46,841 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Your search for Heidi is over – you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are n different books, numbered 1 through n.
We will look at the library's operation during n consecutive days. Heidi knows in advance that on the i-th day (1 ≤ i ≤ n) precisely one person will come to the library, request to borrow the book ai, read it in a few hours, and return the book later on the same day.
Heidi desperately wants to please all her guests, so she will make sure to always have the book ai available in the library on the i-th day. During the night before the i-th day, she has the option of going to the bookstore (which operates at nights to avoid competition with the library) and buying any book for the price of 1 CHF. Of course, if she already has a book at the library, she does not need to buy it again. Initially, the library contains no books.
There is a problem, though. The capacity of the library is k – this means that at any time, there can be at most k books at the library. If buying a new book would cause Heidi to have more than k books, she must first get rid of some book that she already has, in order to make room for the new book. If she later needs a book that she got rid of, she will need to buy that book again.
You are given k and the sequence of requests for books a1, a2, ..., an. What is the minimum cost (in CHF) of buying new books to satisfy all the requests?
Input
The first line of input will contain two integers n and k (1 ≤ n, k ≤ 80). The second line will contain n integers a1, a2, ..., an (1 ≤ ai ≤ n) – the sequence of book requests.
Output
On a single line print the minimum cost of buying books at the store so as to satisfy all requests.
Examples
Input
4 80
1 2 2 1
Output
2
Input
4 1
1 2 2 1
Output
3
Input
4 2
1 2 3 1
Output
3
Note
In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day.
In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day.
In the third test case, before buying book 3 on the third day, she must decide which of the books 1 and 2 she should get rid of. Of course, she should keep the book 1, which will be requested on the fourth day. | instruction | 0 | 23,421 | 10 | 46,842 |
Tags: greedy
Correct Solution:
```
def dist(a, f, i):
for j in range(i + 1, len(a)):
if f == a[j]:
return j
n, k = map(int, input().split())
a = list(map(int, input().split()))
books = set()
l = 0
pref = [0 for i in range(n)]
for i in range(n - 1, -1, -1):
pref[a[i] - 1] = max(pref[a[i] - 1], i) # i min == 0
res = 0
for i in range(n):
if a[i] not in books:
if l == k:
for j in books:
if pref[j - 1] < i:
l -= 1
books.discard(j)
break
if l == k:
m = -1
for j in books:
if dist(a, j, i) > m:
m = dist(a, j, i)
ind = j
books.discard(ind)
l -= 1
res += 1
books.add(a[i])
l += 1
print(res)
``` | output | 1 | 23,421 | 10 | 46,843 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Your search for Heidi is over – you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are n different books, numbered 1 through n.
We will look at the library's operation during n consecutive days. Heidi knows in advance that on the i-th day (1 ≤ i ≤ n) precisely one person will come to the library, request to borrow the book ai, read it in a few hours, and return the book later on the same day.
Heidi desperately wants to please all her guests, so she will make sure to always have the book ai available in the library on the i-th day. During the night before the i-th day, she has the option of going to the bookstore (which operates at nights to avoid competition with the library) and buying any book for the price of 1 CHF. Of course, if she already has a book at the library, she does not need to buy it again. Initially, the library contains no books.
There is a problem, though. The capacity of the library is k – this means that at any time, there can be at most k books at the library. If buying a new book would cause Heidi to have more than k books, she must first get rid of some book that she already has, in order to make room for the new book. If she later needs a book that she got rid of, she will need to buy that book again.
You are given k and the sequence of requests for books a1, a2, ..., an. What is the minimum cost (in CHF) of buying new books to satisfy all the requests?
Input
The first line of input will contain two integers n and k (1 ≤ n, k ≤ 80). The second line will contain n integers a1, a2, ..., an (1 ≤ ai ≤ n) – the sequence of book requests.
Output
On a single line print the minimum cost of buying books at the store so as to satisfy all requests.
Examples
Input
4 80
1 2 2 1
Output
2
Input
4 1
1 2 2 1
Output
3
Input
4 2
1 2 3 1
Output
3
Note
In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day.
In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day.
In the third test case, before buying book 3 on the third day, she must decide which of the books 1 and 2 she should get rid of. Of course, she should keep the book 1, which will be requested on the fourth day.
Submitted Solution:
```
n, k = map(int, input().split())
a = list(map(int, input().split()))
inventory = set()
bought = 0
for i in range(n):
if a[i] not in inventory:
if len(inventory) == k:
farthest_time = 0
farthest = 0
for v in inventory:
try:
far = a.index(v, i + 1)
if far > farthest_time:
farthest_time = far
farthest = v
except ValueError:
farthest_time = 10000
farthest = v
inventory.remove(farthest)
inventory.add(a[i])
bought += 1
print(bought)
``` | instruction | 0 | 23,422 | 10 | 46,844 |
Yes | output | 1 | 23,422 | 10 | 46,845 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Your search for Heidi is over – you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are n different books, numbered 1 through n.
We will look at the library's operation during n consecutive days. Heidi knows in advance that on the i-th day (1 ≤ i ≤ n) precisely one person will come to the library, request to borrow the book ai, read it in a few hours, and return the book later on the same day.
Heidi desperately wants to please all her guests, so she will make sure to always have the book ai available in the library on the i-th day. During the night before the i-th day, she has the option of going to the bookstore (which operates at nights to avoid competition with the library) and buying any book for the price of 1 CHF. Of course, if she already has a book at the library, she does not need to buy it again. Initially, the library contains no books.
There is a problem, though. The capacity of the library is k – this means that at any time, there can be at most k books at the library. If buying a new book would cause Heidi to have more than k books, she must first get rid of some book that she already has, in order to make room for the new book. If she later needs a book that she got rid of, she will need to buy that book again.
You are given k and the sequence of requests for books a1, a2, ..., an. What is the minimum cost (in CHF) of buying new books to satisfy all the requests?
Input
The first line of input will contain two integers n and k (1 ≤ n, k ≤ 80). The second line will contain n integers a1, a2, ..., an (1 ≤ ai ≤ n) – the sequence of book requests.
Output
On a single line print the minimum cost of buying books at the store so as to satisfy all requests.
Examples
Input
4 80
1 2 2 1
Output
2
Input
4 1
1 2 2 1
Output
3
Input
4 2
1 2 3 1
Output
3
Note
In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day.
In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day.
In the third test case, before buying book 3 on the third day, she must decide which of the books 1 and 2 she should get rid of. Of course, she should keep the book 1, which will be requested on the fourth day.
Submitted Solution:
```
import sys
def solve():
n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]
kinds = len(set(a))
if kinds <= k:
print(kinds)
return
books = [False] * n
have = 0
cost = 0
for i, ai in enumerate(a):
if books[ai]:
continue
else:
if have < k:
books[ai] = True
cost += 1
have += 1
else:
trash = -1
longest = -1
for j in range(n):
if books[j]:
if j not in a[i + 1:]:
trash = j
break
m = a[i + 1:].index(j)
if longest < m:
longest = m
trash = j
assert trash != -1
books[trash] = False
books[ai] = True
cost += 1
# print([i for i in range(n) if books[i]])
print(cost)
if __name__ == '__main__':
solve()
``` | instruction | 0 | 23,423 | 10 | 46,846 |
Yes | output | 1 | 23,423 | 10 | 46,847 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Your search for Heidi is over – you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are n different books, numbered 1 through n.
We will look at the library's operation during n consecutive days. Heidi knows in advance that on the i-th day (1 ≤ i ≤ n) precisely one person will come to the library, request to borrow the book ai, read it in a few hours, and return the book later on the same day.
Heidi desperately wants to please all her guests, so she will make sure to always have the book ai available in the library on the i-th day. During the night before the i-th day, she has the option of going to the bookstore (which operates at nights to avoid competition with the library) and buying any book for the price of 1 CHF. Of course, if she already has a book at the library, she does not need to buy it again. Initially, the library contains no books.
There is a problem, though. The capacity of the library is k – this means that at any time, there can be at most k books at the library. If buying a new book would cause Heidi to have more than k books, she must first get rid of some book that she already has, in order to make room for the new book. If she later needs a book that she got rid of, she will need to buy that book again.
You are given k and the sequence of requests for books a1, a2, ..., an. What is the minimum cost (in CHF) of buying new books to satisfy all the requests?
Input
The first line of input will contain two integers n and k (1 ≤ n, k ≤ 80). The second line will contain n integers a1, a2, ..., an (1 ≤ ai ≤ n) – the sequence of book requests.
Output
On a single line print the minimum cost of buying books at the store so as to satisfy all requests.
Examples
Input
4 80
1 2 2 1
Output
2
Input
4 1
1 2 2 1
Output
3
Input
4 2
1 2 3 1
Output
3
Note
In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day.
In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day.
In the third test case, before buying book 3 on the third day, she must decide which of the books 1 and 2 she should get rid of. Of course, she should keep the book 1, which will be requested on the fourth day.
Submitted Solution:
```
n, k = [int(x) for x in input().split()]
requests = [int(x) for x in input().split()]
req_list = {}
def find_last(bucket):
last_book = None
last_date = None
global req_list
i = 0
for item in bucket:
#print(item, req_list)
if last_book is None:
last_book = item
if len(req_list[item]) < 1:
last_date = float('inf')
return item, i
else:
last_date = req_list[item][0]
index = i
elif len(req_list[item]) >= 1 and req_list[item][0] > last_date:
last_book = item
last_date = req_list[item][0]
index = i
elif len(req_list[item]) < 1 and last_date < float('inf'):
return item, i
i += 1
return last_book, index
def update_reqlist(book):
global req_list
req_list[book] = req_list[book][1:]
for i in range(n):
if requests[i] in req_list:
req_list[requests[i]].append(i)
else:
req_list[requests[i]] = [i]
bucket = []
bucket_size = 0
cost = 0
for book in requests:
if book in bucket:
update_reqlist(book)
continue
if bucket_size < k:
bucket.append(book)
bucket_size += 1
cost += 1
update_reqlist(book)
else:
last_book, index = find_last(bucket)
if len(bucket) > 1:
bucket.pop(index)
else:
bucket = []
bucket.append(book)
update_reqlist(book)
cost += 1
#print(bucket, req_list)
#print(req_list)
print(cost)
``` | instruction | 0 | 23,424 | 10 | 46,848 |
Yes | output | 1 | 23,424 | 10 | 46,849 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Your search for Heidi is over – you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are n different books, numbered 1 through n.
We will look at the library's operation during n consecutive days. Heidi knows in advance that on the i-th day (1 ≤ i ≤ n) precisely one person will come to the library, request to borrow the book ai, read it in a few hours, and return the book later on the same day.
Heidi desperately wants to please all her guests, so she will make sure to always have the book ai available in the library on the i-th day. During the night before the i-th day, she has the option of going to the bookstore (which operates at nights to avoid competition with the library) and buying any book for the price of 1 CHF. Of course, if she already has a book at the library, she does not need to buy it again. Initially, the library contains no books.
There is a problem, though. The capacity of the library is k – this means that at any time, there can be at most k books at the library. If buying a new book would cause Heidi to have more than k books, she must first get rid of some book that she already has, in order to make room for the new book. If she later needs a book that she got rid of, she will need to buy that book again.
You are given k and the sequence of requests for books a1, a2, ..., an. What is the minimum cost (in CHF) of buying new books to satisfy all the requests?
Input
The first line of input will contain two integers n and k (1 ≤ n, k ≤ 80). The second line will contain n integers a1, a2, ..., an (1 ≤ ai ≤ n) – the sequence of book requests.
Output
On a single line print the minimum cost of buying books at the store so as to satisfy all requests.
Examples
Input
4 80
1 2 2 1
Output
2
Input
4 1
1 2 2 1
Output
3
Input
4 2
1 2 3 1
Output
3
Note
In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day.
In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day.
In the third test case, before buying book 3 on the third day, she must decide which of the books 1 and 2 she should get rid of. Of course, she should keep the book 1, which will be requested on the fourth day.
Submitted Solution:
```
import sys
from bisect import bisect
from collections import Counter, defaultdict
l1 = sys.stdin.readline()
l2 = sys.stdin.readline()
n, k = map(int, l1.split(' '))
books = list(map(int, l2.split(' ')))
cost = 0
cache = set()
x = defaultdict(list)
for i, book_id in enumerate(books):
x[book_id].append(i)
for i, book_id in enumerate(books):
if book_id in cache:
continue
if len(cache) < k:
cache.add(book_id)
cost += 1
continue
min_next_closest = i
to_evict = next(iter(cache))
for x_id in cache:
indices = x[x_id]
index = bisect(indices, i)
if index == len(indices):
to_evict = x_id
break
next_closect = indices[index]
if next_closect > min_next_closest:
min_next_closest = next_closect
to_evict = x_id
# print("To evict %s" % to_evict)
cache.remove(to_evict)
cache.add(book_id)
cost += 1
print(cost)
``` | instruction | 0 | 23,425 | 10 | 46,850 |
Yes | output | 1 | 23,425 | 10 | 46,851 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Your search for Heidi is over – you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are n different books, numbered 1 through n.
We will look at the library's operation during n consecutive days. Heidi knows in advance that on the i-th day (1 ≤ i ≤ n) precisely one person will come to the library, request to borrow the book ai, read it in a few hours, and return the book later on the same day.
Heidi desperately wants to please all her guests, so she will make sure to always have the book ai available in the library on the i-th day. During the night before the i-th day, she has the option of going to the bookstore (which operates at nights to avoid competition with the library) and buying any book for the price of 1 CHF. Of course, if she already has a book at the library, she does not need to buy it again. Initially, the library contains no books.
There is a problem, though. The capacity of the library is k – this means that at any time, there can be at most k books at the library. If buying a new book would cause Heidi to have more than k books, she must first get rid of some book that she already has, in order to make room for the new book. If she later needs a book that she got rid of, she will need to buy that book again.
You are given k and the sequence of requests for books a1, a2, ..., an. What is the minimum cost (in CHF) of buying new books to satisfy all the requests?
Input
The first line of input will contain two integers n and k (1 ≤ n, k ≤ 80). The second line will contain n integers a1, a2, ..., an (1 ≤ ai ≤ n) – the sequence of book requests.
Output
On a single line print the minimum cost of buying books at the store so as to satisfy all requests.
Examples
Input
4 80
1 2 2 1
Output
2
Input
4 1
1 2 2 1
Output
3
Input
4 2
1 2 3 1
Output
3
Note
In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day.
In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day.
In the third test case, before buying book 3 on the third day, she must decide which of the books 1 and 2 she should get rid of. Of course, she should keep the book 1, which will be requested on the fourth day.
Submitted Solution:
```
n, k = map(int, input().split())
a = list(map(int, input().split()))
have = set()
ans = 0
for i in range(n):
if len(have) > k:
to_del = list(have)[0]
for j in have:
if j not in a[i:]:
to_del = j
else:
to_del = -1
break
if to_del != -1:
have.remove(to_del)
if a[i] not in have:
ans += 1
have.add(a[i])
print(ans)
``` | instruction | 0 | 23,426 | 10 | 46,852 |
No | output | 1 | 23,426 | 10 | 46,853 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Your search for Heidi is over – you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are n different books, numbered 1 through n.
We will look at the library's operation during n consecutive days. Heidi knows in advance that on the i-th day (1 ≤ i ≤ n) precisely one person will come to the library, request to borrow the book ai, read it in a few hours, and return the book later on the same day.
Heidi desperately wants to please all her guests, so she will make sure to always have the book ai available in the library on the i-th day. During the night before the i-th day, she has the option of going to the bookstore (which operates at nights to avoid competition with the library) and buying any book for the price of 1 CHF. Of course, if she already has a book at the library, she does not need to buy it again. Initially, the library contains no books.
There is a problem, though. The capacity of the library is k – this means that at any time, there can be at most k books at the library. If buying a new book would cause Heidi to have more than k books, she must first get rid of some book that she already has, in order to make room for the new book. If she later needs a book that she got rid of, she will need to buy that book again.
You are given k and the sequence of requests for books a1, a2, ..., an. What is the minimum cost (in CHF) of buying new books to satisfy all the requests?
Input
The first line of input will contain two integers n and k (1 ≤ n, k ≤ 80). The second line will contain n integers a1, a2, ..., an (1 ≤ ai ≤ n) – the sequence of book requests.
Output
On a single line print the minimum cost of buying books at the store so as to satisfy all requests.
Examples
Input
4 80
1 2 2 1
Output
2
Input
4 1
1 2 2 1
Output
3
Input
4 2
1 2 3 1
Output
3
Note
In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day.
In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day.
In the third test case, before buying book 3 on the third day, she must decide which of the books 1 and 2 she should get rid of. Of course, she should keep the book 1, which will be requested on the fourth day.
Submitted Solution:
```
import sys
import math
from collections import defaultdict,deque
import heapq
n,k=map(int,sys.stdin.readline().split())
arr=list(map(int,sys.stdin.readline().split()))
a=set()
cost=0
for i in range(n):
#print(a,'a',cost)
if arr[i] not in a:
if len(a)<k:
a.add(arr[i])
cost+=1
else:
dic=defaultdict(int)
for b in range(i+1,n):
dic[arr[b]]+=1
rem=-1
nax=100
for j in a:
if dic[j]<nax:
rem=j
nax=dic[j]
a.remove(rem)
a.add(arr[i])
cost+=1
print(cost)
``` | instruction | 0 | 23,427 | 10 | 46,854 |
No | output | 1 | 23,427 | 10 | 46,855 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Your search for Heidi is over – you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are n different books, numbered 1 through n.
We will look at the library's operation during n consecutive days. Heidi knows in advance that on the i-th day (1 ≤ i ≤ n) precisely one person will come to the library, request to borrow the book ai, read it in a few hours, and return the book later on the same day.
Heidi desperately wants to please all her guests, so she will make sure to always have the book ai available in the library on the i-th day. During the night before the i-th day, she has the option of going to the bookstore (which operates at nights to avoid competition with the library) and buying any book for the price of 1 CHF. Of course, if she already has a book at the library, she does not need to buy it again. Initially, the library contains no books.
There is a problem, though. The capacity of the library is k – this means that at any time, there can be at most k books at the library. If buying a new book would cause Heidi to have more than k books, she must first get rid of some book that she already has, in order to make room for the new book. If she later needs a book that she got rid of, she will need to buy that book again.
You are given k and the sequence of requests for books a1, a2, ..., an. What is the minimum cost (in CHF) of buying new books to satisfy all the requests?
Input
The first line of input will contain two integers n and k (1 ≤ n, k ≤ 80). The second line will contain n integers a1, a2, ..., an (1 ≤ ai ≤ n) – the sequence of book requests.
Output
On a single line print the minimum cost of buying books at the store so as to satisfy all requests.
Examples
Input
4 80
1 2 2 1
Output
2
Input
4 1
1 2 2 1
Output
3
Input
4 2
1 2 3 1
Output
3
Note
In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day.
In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day.
In the third test case, before buying book 3 on the third day, she must decide which of the books 1 and 2 she should get rid of. Of course, she should keep the book 1, which will be requested on the fourth day.
Submitted Solution:
```
import sys
def solve():
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
kinds = len(set(a))
if kinds <= k:
print(kinds)
return
books = [False] * (n + 1)
have = 0
cost = 0
for i, ai in enumerate(a):
if books[ai]:
continue
else:
if have < k:
books[ai] = True
cost += 1
have += 1
else:
trash = -1
longest = 0
for j in range(1, n + 1):
if books[j]:
if j not in a[i + 1:]:
trash = j
break
k = a[i + 1:].index(j)
if longest < k:
k = longest
trash = j
books[trash] = False
books[ai] = True
cost += 1
print(cost)
if __name__ == '__main__':
solve()
``` | instruction | 0 | 23,428 | 10 | 46,856 |
No | output | 1 | 23,428 | 10 | 46,857 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Your search for Heidi is over – you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are n different books, numbered 1 through n.
We will look at the library's operation during n consecutive days. Heidi knows in advance that on the i-th day (1 ≤ i ≤ n) precisely one person will come to the library, request to borrow the book ai, read it in a few hours, and return the book later on the same day.
Heidi desperately wants to please all her guests, so she will make sure to always have the book ai available in the library on the i-th day. During the night before the i-th day, she has the option of going to the bookstore (which operates at nights to avoid competition with the library) and buying any book for the price of 1 CHF. Of course, if she already has a book at the library, she does not need to buy it again. Initially, the library contains no books.
There is a problem, though. The capacity of the library is k – this means that at any time, there can be at most k books at the library. If buying a new book would cause Heidi to have more than k books, she must first get rid of some book that she already has, in order to make room for the new book. If she later needs a book that she got rid of, she will need to buy that book again.
You are given k and the sequence of requests for books a1, a2, ..., an. What is the minimum cost (in CHF) of buying new books to satisfy all the requests?
Input
The first line of input will contain two integers n and k (1 ≤ n, k ≤ 80). The second line will contain n integers a1, a2, ..., an (1 ≤ ai ≤ n) – the sequence of book requests.
Output
On a single line print the minimum cost of buying books at the store so as to satisfy all requests.
Examples
Input
4 80
1 2 2 1
Output
2
Input
4 1
1 2 2 1
Output
3
Input
4 2
1 2 3 1
Output
3
Note
In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day.
In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day.
In the third test case, before buying book 3 on the third day, she must decide which of the books 1 and 2 she should get rid of. Of course, she should keep the book 1, which will be requested on the fourth day.
Submitted Solution:
```
n,k=map(int,input().split())
a=list(map(int,input().split()))
z=[0]*81
kz,ans=0,0
for i in range(n):
if z[a[i]]: continue
ans+=1
if k>kz:
z[a[i]]=1; kz+=1
else:
h=-1
for j in range(1,n):
if z[j]:
m=n+1
for p in range(i,n):
if j==a[p]:
m=p
break
if m>h:
h=m;
t=j
z[t]=0
z[a[i]]=1
print(ans)
``` | instruction | 0 | 23,429 | 10 | 46,858 |
No | output | 1 | 23,429 | 10 | 46,859 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Output
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.
Examples
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Input
3 2
4 6 3
2 4 1
3 5 4
Output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2. | instruction | 0 | 23,430 | 10 | 46,860 |
Tags: binary search, greedy, implementation, sortings
Correct Solution:
```
from sys import stdin
from collections import *
from bisect import *
from operator import itemgetter
def arr_inp(n):
if n == 1:
return [int(x) for x in stdin.readline().split()]
elif n == 2:
return [float(x) for x in stdin.readline().split()]
else:
return list(stdin.readline()[:-1])
def min_arr(arr):
arr.append(float('inf'))
for i in range(len(arr) - 1, 0, -1):
arr[i - 1] = min(arr[i - 1], arr[i])
return arr
def main():
n, x = arr_inp(1)
mem, a, mi, ans, ixs = defaultdict(set), [arr_inp(1) for i in range(n)], defaultdict(list), float(
'inf'), defaultdict(list)
for i in range(n):
mem[a[i][1] - a[i][0] + 1].add((a[i][0], a[i][2]))
for i in mem.keys():
mem[i] = sorted(mem[i], key=itemgetter(0, 1))
for j, k in enumerate(mem[i]):
if j == 0 or k[0] != ixs[i][-1]:
ixs[i].append(k[0])
mi[i].append(k[1])
mi[i] = min_arr(mi[i])
# print(mi, mem, ixs)
for l, r, c in a:
dur = r - l + 1
ix = bisect_right(ixs[x - dur], r + 1)
if ix > 0 and ixs[x - dur][ix - 1] == r + 1:
ix -= 1
if ix == len(ixs[x - dur]):
continue
ans = min(ans, mi[x - dur][ix] + c)
print(ans if ans != float('inf') else -1)
if __name__ == '__main__':
main()
``` | output | 1 | 23,430 | 10 | 46,861 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Output
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.
Examples
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Input
3 2
4 6 3
2 4 1
3 5 4
Output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2. | instruction | 0 | 23,431 | 10 | 46,862 |
Tags: binary search, greedy, implementation, sortings
Correct Solution:
```
import sys
def main():
n,x = map(int,sys.stdin.readline().split())
al = []
starts = []
finishes = []
y = [-1 for i in range(200002)]
for i in range(n):
a,b,c = map(int, sys.stdin.readline().split())
al.append((a,b,c))
starts.append((a,i))
finishes.append((b,i))
finishes = sorted(finishes, key=lambda x: x[0])
starts = sorted(starts, key=lambda x: x[0])
j =0
res = 3*(10**9)
for i in range(n):
while j<n and starts[j][0] <= finishes[i][0]:
c = starts[j][1]
h = al[c][1] -al[c][0]+1
cost = al[c][2]
if y[x-h]!= -1 and y[x-h] + cost < res:
res = y[x-h] + cost
j+=1
c = finishes[i][1]
h = al[c][1] - al[c][0]+1
cost = al[c][2]
if y[h]== -1 or y[h] > cost:
y[h] = cost
if res == 3*(10**9):
print(-1)
else:
print(res)
main()
``` | output | 1 | 23,431 | 10 | 46,863 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Output
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.
Examples
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Input
3 2
4 6 3
2 4 1
3 5 4
Output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2. | instruction | 0 | 23,432 | 10 | 46,864 |
Tags: binary search, greedy, implementation, sortings
Correct Solution:
```
n,x=map(int,input().split())
a=[]
for i in range(x+1):a.append([])
#делаем герлянду хешем по длинам (не берем не нужное)
for i in range(n):
L,R,C=map(int,input().split())
if R-L+1>x:continue
a[R-L+1].append([L,R,C])
#сортируем по левым границам
for i in range(x+1):a[i]=sorted(a[i])
ans=int(3e9+1)
for i in range(x+1):
m=int(3e9+1)
z=0
for j in range(len(a[i])):
while z!=len(a[x-i]) and a[i][j][0]>a[x-i][z][1]:
m=min(m,a[x-i][z][2])
z+=1
ans=min(ans,m+a[i][j][2])
if ans==int(3e9+1):
print(-1)
else:
print(ans)
``` | output | 1 | 23,432 | 10 | 46,865 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Output
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.
Examples
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Input
3 2
4 6 3
2 4 1
3 5 4
Output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2. | instruction | 0 | 23,433 | 10 | 46,866 |
Tags: binary search, greedy, implementation, sortings
Correct Solution:
```
n, x = map(int, input().split())
data = []
for i in range(x + 1):
data.append([])
for i in range(n):
l, r, c = map(int, input().split())
if r - l + 1 <= x:
data[r - l + 1].append([l, r, c])
for i in range(x + 1):
data[i].sort()
answer = int(3e9+7)
for i in range(x + 1):
k, b = 0, int(3e9+7)
for j in range(len(data[i])):
while k != len(data[x - i]) and data[i][j][0] > data[x - i][k][1]:
b = min(b, data[x - i][k][2])
k += 1
answer = min(answer, b + data[i][j][2])
if answer == int(3e9+7):
print(-1)
else:
print(answer)
``` | output | 1 | 23,433 | 10 | 46,867 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Output
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.
Examples
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Input
3 2
4 6 3
2 4 1
3 5 4
Output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2. | instruction | 0 | 23,434 | 10 | 46,868 |
Tags: binary search, greedy, implementation, sortings
Correct Solution:
```
from bisect import bisect_left
f = lambda: map(int, input().split())
n, x = f()
s, t = {}, {}
for i in range(n):
l, r, c = f()
d = r - l
if d not in s: s[d] = []
s[d].append((l, c))
for d, p in s.items():
p.sort(key=lambda q: q[0])
q = t[d] = [[l, c] for l, c in p]
for i in range(1, len(q))[::-1]:
q[i - 1][1] = min(q[i - 1][1], q[i][1])
m = 3e9
for d in s:
p = t.get(x - 2 - d, [])
if p:
for l, c in s[d]:
i = bisect_left(p, [l + d + 1, 0])
if i < len(p): m = min(m, c + p[i][1])
print(-1 if m == 3e9 else m)
``` | output | 1 | 23,434 | 10 | 46,869 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Output
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.
Examples
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Input
3 2
4 6 3
2 4 1
3 5 4
Output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2. | instruction | 0 | 23,435 | 10 | 46,870 |
Tags: binary search, greedy, implementation, sortings
Correct Solution:
```
(n, x) = [int(x) for x in input().split()]
l_ind = [[] for i in range(200000)]
r_ind = [[] for i in range(200000)]
for i in range(n):
(l, r, c) = [int(x) for x in input().split()]
if r - l + 1 <= x:
l_ind[l - 1].append((l, r, c))
r_ind[r - 1].append((l, r, c))
ans = None
bestCost = [None for i in range(x + 1)]
for (vl, vr) in zip(l_ind, r_ind):
for (l, r, c) in vl:
cur_len = r - l + 1
if bestCost[x - cur_len]:
if ans:
ans = min(ans, c + bestCost[x - cur_len])
else:
ans = c + bestCost[x - cur_len]
for (l, r, c) in vr:
cur_len = r - l + 1
if bestCost[cur_len]:
bestCost[cur_len] = min(bestCost[cur_len], c)
else:
bestCost[cur_len] = c
if ans == None:
print(-1)
else:
print(ans)
``` | output | 1 | 23,435 | 10 | 46,871 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Output
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.
Examples
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Input
3 2
4 6 3
2 4 1
3 5 4
Output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2. | instruction | 0 | 23,436 | 10 | 46,872 |
Tags: binary search, greedy, implementation, sortings
Correct Solution:
```
from collections import defaultdict
from sys import stdin
def read(line):
return list(map(int, line.split()))
def main():
test = stdin.readlines()
n, x = read(test[0])
vouchers = [read(test[i]) for i in range(1, n+1)]
vouchers.sort()
min_value = defaultdict(list)
for l, r, cost in vouchers:
min_value[r - l + 1].append([l, cost])
for duration in min_value:
m = len(min_value[duration])
for j in range(m - 2, -1, -1):
cur = min_value[duration][j][1]
min_value[duration][j][1] = min(cur, min_value[duration][j+1][1])
ans = float('inf')
for li, ri, costi in vouchers:
needed = x - ri + li - 1
if needed not in min_value:
continue
m = len(min_value[needed])
lo, hi = 0, m
while lo < hi:
mid = lo + (hi - lo) // 2
cur = min_value[needed][mid][0]
if cur > ri:
hi = mid
else:
lo = mid + 1
if lo >= len(min_value[needed]):
continue
ans = min(ans, costi + min_value[needed][lo][1])
print(ans if ans != float('inf') else -1)
if __name__ == "__main__":
main()
``` | output | 1 | 23,436 | 10 | 46,873 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Output
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.
Examples
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Input
3 2
4 6 3
2 4 1
3 5 4
Output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2. | instruction | 0 | 23,437 | 10 | 46,874 |
Tags: binary search, greedy, implementation, sortings
Correct Solution:
```
Q = map
E = input
B = range
W = enumerate
o = len
P = min
q = print
def U():
return Q(int, E().split())
n, x = U()
s = [[] for i in B(x-1)]
for d in B(n):
l, r, c = U()
if r-l < x-1:
s[r-l] += [[l, c]]
for t in s:
t.sort(key=lambda q: q[0])
m = 3e9
for d, t in W(s):
D = x-2-d
i, T = 0, s[D]
M = 3e9
for l, c in t:
while i < o(T)and l > T[i][0]+D:
M = P(M, T[i][1])
i += 1
m = P(m, c+M)
q(-1 if m == 3e9 else m)
``` | output | 1 | 23,437 | 10 | 46,875 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Output
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.
Examples
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Input
3 2
4 6 3
2 4 1
3 5 4
Output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2.
Submitted Solution:
```
from math import inf
n, x = map(int, input().split())
a = []
best = [inf for i in range(2 * (10**5) + 1)]
ans = inf
for i in range(n):
l, r, cost = map(int, input().split())
a.append((l, cost, r-l+1, True))
a.append((r, cost, r-l+1, False))
a.sort(key=lambda element: 10 * element[0] + 1 - int(element[3]))
for item in a:
if item[3] and x > item[2]:
ans = min(ans, item[1] + best[x - item[2]])
if not item[3]:
best[item[2]] = min(best[item[2]], item[1])
print(ans if ans != inf else -1)
``` | instruction | 0 | 23,438 | 10 | 46,876 |
Yes | output | 1 | 23,438 | 10 | 46,877 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Output
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.
Examples
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Input
3 2
4 6 3
2 4 1
3 5 4
Output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2.
Submitted Solution:
```
#created by BinZhaO
n,x=map(int,input().split())
V=[]
for i in range(x+1):
V.append([])
for i in range(n):
l,r,c=map(int,input().split())
if r-l+1<=x:
V[r-l+1].append([l,r,c])
for i in range(x+1):
V[i]=sorted(V[i])
ans=int(3e9+7)
for i in range(x+1):
mn=int(3e9+7)
p=0
k=0
for j in range(len(V[i])):
while k!=len(V[x-i]) and V[i][j][0]>V[x-i][k][1] :
mn=min(mn,V[x-i][k][2])
k=k+1
ans=min(ans,mn+V[i][j][2])
if ans==int(3e9+7):
print(-1)
else:
print(ans)
``` | instruction | 0 | 23,439 | 10 | 46,878 |
Yes | output | 1 | 23,439 | 10 | 46,879 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Output
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.
Examples
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Input
3 2
4 6 3
2 4 1
3 5 4
Output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2.
Submitted Solution:
```
n, x = map(int, input().split())
data = []
for i in range(x + 1):
data.append([])
for i in range(n):
l, r, c = map(int, input().split())
if r - l + 1 <= x:
data[r - l + 1].append([l, r, c])
for i in range(x + 1):
data[i].sort()
answer = int(20000000000)
for i in range(x + 1):
k, b = 0, int(20000000000)
for j in range(len(data[i])):
while k != len(data[x - i]) and( data[i][j][0] > data[x - i][k][1] ):
b = min(b, data[x - i][k][2])
k += 1
answer = min(answer, b + data[i][j][2])
if answer == int(20000000000):
print(-1)
else:
print(answer)
``` | instruction | 0 | 23,440 | 10 | 46,880 |
Yes | output | 1 | 23,440 | 10 | 46,881 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Output
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.
Examples
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Input
3 2
4 6 3
2 4 1
3 5 4
Output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2.
Submitted Solution:
```
import bisect
import collections
def solve(inp, *args):
n, x = map(int, inp.split(" ", 1))
travels_by_len = collections.defaultdict(list)
travels_by_len_processed = {}
for travel in args:
l, r, cost = map(int, travel.split(" ", 2))
travels_by_len[r - l + 1].append((l, r, cost))
for travel_len, travels in travels_by_len.items():
travels.sort()
travels_processed = [(travels[-1][0], travels[-1][2])]
for i in range(len(travels) - 2, -1, -1):
prev_travel = travels_processed[-1]
l, r, c = travels[i]
travels_processed.append((l, min(c, prev_travel[1])))
travels_by_len_processed[travel_len] = travels_processed[::-1]
best_price = float("inf")
for first_travel_len, first_travels in travels_by_len.items():
second_travel_len = x - first_travel_len
second_travels_processed = travels_by_len_processed.get(second_travel_len, [])
for first_travel in first_travels:
l1, r1, c1 = first_travel
# now we look for cheapest travels which have l2 > r1
idx = bisect.bisect_right(second_travels_processed, (r1, float("inf")))
if 0 <= idx < len(second_travels_processed):
best_price = min(best_price, c1 + second_travels_processed[idx][1])
return -1 if best_price == float("inf") else best_price
if __name__ == "__main__":
inp = input()
n, x = map(int, inp.split(" ", 1))
print(solve(inp, *(input() for i in range(n))))
``` | instruction | 0 | 23,441 | 10 | 46,882 |
Yes | output | 1 | 23,441 | 10 | 46,883 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Output
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.
Examples
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Input
3 2
4 6 3
2 4 1
3 5 4
Output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2.
Submitted Solution:
```
import sys
def main():
a = sys.stdin.readline().strip('\n')
if len(a) == 0:
exit()
n,m = map(int,a.split(' '))
list1 = []
p = True
k = -1
for i in range(n):
a = sys.stdin.readline()
list1.append(list(map(int,a.split(' '))))
for i in range(n):
for j in range (i+1,n):
if list1[i][1] - list1[i][0] + list1[j][1] - list1[j][0] == m - 2 and(list1[i][1] < list1[j][0] or list1[j][1] < list1[i][0]):
k = list1[i][2] + list1[j][2]
if p == True:
p == False
l = k
else:
if l > k:
k,l = l,k
if k == -1:
print(-1)
else:
print(l)
if __name__ == '__main__':
main()
``` | instruction | 0 | 23,442 | 10 | 46,884 |
No | output | 1 | 23,442 | 10 | 46,885 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Output
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.
Examples
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Input
3 2
4 6 3
2 4 1
3 5 4
Output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2.
Submitted Solution:
```
import bisect
def main():
read = lambda: tuple(map(int, input().split()))
n, x = read()
s = [[]] * x
for i in range(n):
l, r, c = read()
p = r - l + 1
if p < x:
s[p] += [(l, r, c)]
ss = []
for l in s:
ss += [[]]
ss[-1] += [sorted(l, key=lambda v:v[0])]
ss[-1] += [sorted(l, key=lambda v:v[1])]
inf = 1e19
ans = inf
for p, l1 in enumerate(ss):
if p > 0 and x - p > 0:
p1 = x - p
l2 = ss[p1]
suka = inf
for l, r, c in l1[0]:
i = 0
while i < len(l2[1]) and l > l2[1][i][1]:
suka = min(suka, l2[1][i][2])
i += 1
ans = min(ans, c + suka)
print(-1 if ans == inf else ans)
main()
``` | instruction | 0 | 23,443 | 10 | 46,886 |
No | output | 1 | 23,443 | 10 | 46,887 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Output
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.
Examples
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Input
3 2
4 6 3
2 4 1
3 5 4
Output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2.
Submitted Solution:
```
n,m=map(int,input().split())
M=[]
for i in range(m+1) :
M.append([])
for i in range(n) :
l=list(map(int,input().split()))
if l[1]-l[0]+1<=m :
M[l[1]-l[0]+1].append(l)
for i in range(len(M)) :
M[i]=sorted(M[i])
mi=1000000000000000
for y in range(m+1) :
for i in range(len(M[y])) :
if 1 :
l=0
r=len(M[m-y])
T=M[m-y]
while l<r :
mid=(l+r+1)//2
if T[mid-1][0]>M[y][i][0] :
r=mid-1
else :
l=mid
T1=(T[r:])
T1.sort(key=lambda x: x[2])
if T1 :
if mi>T1[0][2]+M[y][i][2] :
mi=T1[0][2]+M[y][i][2]
if mi==1000000000000000 :
print(-1)
else :
print(mi)
``` | instruction | 0 | 23,444 | 10 | 46,888 |
No | output | 1 | 23,444 | 10 | 46,889 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Output
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.
Examples
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Input
3 2
4 6 3
2 4 1
3 5 4
Output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2.
Submitted Solution:
```
from bisect import bisect_left
f = lambda: map(int, input().split())
n, x = f()
s = {}
for i in range(n):
l, r, c = f()
p = s.get(r - l, [])
p.append([l, c])
for d, p in s.items():
p.sort(key=lambda q: q[0])
for i in range(1, len(p))[::-1]:
p[i - 1][1] = min(p[i - 1][1], p[i][1])
m = 3e9
for d in s.keys():
p = s.get(x - 2 - d, [])
if p:
for l, c in s[d]:
i = bisect_left(p, [l, 0])
if i < len(p): m = min(m, c + p[i][1])
print(-1 if m == 3e9 else m)
``` | instruction | 0 | 23,445 | 10 | 46,890 |
No | output | 1 | 23,445 | 10 | 46,891 |
Provide a correct Python 3 solution for this coding contest problem.
There are N items, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.
Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.
Find the maximum possible sum of the values of items that Taro takes home.
Constraints
* All values in input are integers.
* 1 \leq N \leq 100
* 1 \leq W \leq 10^9
* 1 \leq w_i \leq W
* 1 \leq v_i \leq 10^3
Input
Input is given from Standard Input in the following format:
N W
w_1 v_1
w_2 v_2
:
w_N v_N
Output
Print the maximum possible sum of the values of items that Taro takes home.
Examples
Input
3 8
3 30
4 50
5 60
Output
90
Input
1 1000000000
1000000000 10
Output
10
Input
6 15
6 5
5 6
6 4
6 6
3 5
7 2
Output
17 | instruction | 0 | 23,622 | 10 | 47,244 |
"Correct Solution:
```
n,w=map(int,input().split())
a=[10**10]*100001
a[0]=0
from copy import copy
c=0
for i in range(n):
x,y=map(int,input().split())
b=copy(a)
c+=y
for j in range(y,100001):
if j<=c:
b[j]=min(a[j-y]+x,a[j])
a=copy(b)
l=[]
for i in range(len(a)):
if a[i]<=w:
l.append(i)
print(l[-1])
``` | output | 1 | 23,622 | 10 | 47,245 |
Provide a correct Python 3 solution for this coding contest problem.
There are N items, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.
Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.
Find the maximum possible sum of the values of items that Taro takes home.
Constraints
* All values in input are integers.
* 1 \leq N \leq 100
* 1 \leq W \leq 10^9
* 1 \leq w_i \leq W
* 1 \leq v_i \leq 10^3
Input
Input is given from Standard Input in the following format:
N W
w_1 v_1
w_2 v_2
:
w_N v_N
Output
Print the maximum possible sum of the values of items that Taro takes home.
Examples
Input
3 8
3 30
4 50
5 60
Output
90
Input
1 1000000000
1000000000 10
Output
10
Input
6 15
6 5
5 6
6 4
6 6
3 5
7 2
Output
17 | instruction | 0 | 23,623 | 10 | 47,246 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
n, w = map(int,input().split())
A = [list(map(int,input().split())) for i in range(n)]
v = 10**5 + 4
D = [float("inf")] * v
D[0] = 0
for i in range(n):
for j in range(v-1,-1,-1):
if j-A[i][1] >= 0:
D[j] = min(D[j], D[j-A[i][1]] + A[i][0])
ans = 0
for i in range(v):
if D[i] <= w:
ans = i
print(ans)
``` | output | 1 | 23,623 | 10 | 47,247 |
Provide a correct Python 3 solution for this coding contest problem.
There are N items, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.
Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.
Find the maximum possible sum of the values of items that Taro takes home.
Constraints
* All values in input are integers.
* 1 \leq N \leq 100
* 1 \leq W \leq 10^9
* 1 \leq w_i \leq W
* 1 \leq v_i \leq 10^3
Input
Input is given from Standard Input in the following format:
N W
w_1 v_1
w_2 v_2
:
w_N v_N
Output
Print the maximum possible sum of the values of items that Taro takes home.
Examples
Input
3 8
3 30
4 50
5 60
Output
90
Input
1 1000000000
1000000000 10
Output
10
Input
6 15
6 5
5 6
6 4
6 6
3 5
7 2
Output
17 | instruction | 0 | 23,624 | 10 | 47,248 |
"Correct Solution:
```
n, w = map(int, input().split())
wv = [list(map(int, input().split())) for _ in range(n)]
v_total = sum([x[1] for x in wv])
dp = [0] + [float("inf")]*v_total
for i in range(n):
for j in range(v_total, wv[i][1]-1, -1):
dp[j] = min(dp[j], dp[j-wv[i][1]]+wv[i][0])
ans = 0
for i in range(v_total+1):
if dp[i] <= w:
ans = i
print(ans)
``` | output | 1 | 23,624 | 10 | 47,249 |
Provide a correct Python 3 solution for this coding contest problem.
There are N items, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.
Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.
Find the maximum possible sum of the values of items that Taro takes home.
Constraints
* All values in input are integers.
* 1 \leq N \leq 100
* 1 \leq W \leq 10^9
* 1 \leq w_i \leq W
* 1 \leq v_i \leq 10^3
Input
Input is given from Standard Input in the following format:
N W
w_1 v_1
w_2 v_2
:
w_N v_N
Output
Print the maximum possible sum of the values of items that Taro takes home.
Examples
Input
3 8
3 30
4 50
5 60
Output
90
Input
1 1000000000
1000000000 10
Output
10
Input
6 15
6 5
5 6
6 4
6 6
3 5
7 2
Output
17 | instruction | 0 | 23,625 | 10 | 47,250 |
"Correct Solution:
```
n,w = map(int,input().split())
dpv = [10 ** 20 for i in range(10**5+1)]
dpv[0] = 0
for i in range(n):
weight,value = map(int,input().split())
for j in range(10**5-value,-1,-1):
dpv[j+value] = min(dpv[j+value],dpv[j]+weight)
for i in range(10**5,-1,-1):
if dpv[i] <= w:
print(i)
exit()
``` | output | 1 | 23,625 | 10 | 47,251 |
Provide a correct Python 3 solution for this coding contest problem.
There are N items, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.
Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.
Find the maximum possible sum of the values of items that Taro takes home.
Constraints
* All values in input are integers.
* 1 \leq N \leq 100
* 1 \leq W \leq 10^9
* 1 \leq w_i \leq W
* 1 \leq v_i \leq 10^3
Input
Input is given from Standard Input in the following format:
N W
w_1 v_1
w_2 v_2
:
w_N v_N
Output
Print the maximum possible sum of the values of items that Taro takes home.
Examples
Input
3 8
3 30
4 50
5 60
Output
90
Input
1 1000000000
1000000000 10
Output
10
Input
6 15
6 5
5 6
6 4
6 6
3 5
7 2
Output
17 | instruction | 0 | 23,626 | 10 | 47,252 |
"Correct Solution:
```
N, W = map(int, input().split())
dp = [[float("inf")] * (10**5 + 1) for i in range(N+1)]
dp[0][0] = 0
for i in range(N):
w, v = map(int, input().split())
for j in range(10**5 + 1):
dp[i+1][j] = min(dp[i][j], (dp[i][j-v] if j >= v else 0) + w)
ans = 0
for i in range(10**5 + 1):
if dp[-1][i] <= W:
ans = i
print(ans)
``` | output | 1 | 23,626 | 10 | 47,253 |
Provide a correct Python 3 solution for this coding contest problem.
There are N items, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.
Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.
Find the maximum possible sum of the values of items that Taro takes home.
Constraints
* All values in input are integers.
* 1 \leq N \leq 100
* 1 \leq W \leq 10^9
* 1 \leq w_i \leq W
* 1 \leq v_i \leq 10^3
Input
Input is given from Standard Input in the following format:
N W
w_1 v_1
w_2 v_2
:
w_N v_N
Output
Print the maximum possible sum of the values of items that Taro takes home.
Examples
Input
3 8
3 30
4 50
5 60
Output
90
Input
1 1000000000
1000000000 10
Output
10
Input
6 15
6 5
5 6
6 4
6 6
3 5
7 2
Output
17 | instruction | 0 | 23,627 | 10 | 47,254 |
"Correct Solution:
```
N,W=map(int,input().split())
w=[]
v=[]
for i in range(N):
ww,vv=map(int,input().split())
w+=[ww]
v+=[vv]
V=sum(v)
dp=[[float('inf')]*(V+1) for i in range(N+1)]
dp[0][0]=0
res=0
for i in range(N):
for j in range(V+1):
if j-v[i]>=0:
dp[i+1][j]=w[i]+dp[i][j-v[i]]
dp[i+1][j]=min(dp[i][j],dp[i+1][j])
for j in range(V+1):
if dp[N][j]<=W:
res=j
print(res)
``` | output | 1 | 23,627 | 10 | 47,255 |
Provide a correct Python 3 solution for this coding contest problem.
There are N items, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.
Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.
Find the maximum possible sum of the values of items that Taro takes home.
Constraints
* All values in input are integers.
* 1 \leq N \leq 100
* 1 \leq W \leq 10^9
* 1 \leq w_i \leq W
* 1 \leq v_i \leq 10^3
Input
Input is given from Standard Input in the following format:
N W
w_1 v_1
w_2 v_2
:
w_N v_N
Output
Print the maximum possible sum of the values of items that Taro takes home.
Examples
Input
3 8
3 30
4 50
5 60
Output
90
Input
1 1000000000
1000000000 10
Output
10
Input
6 15
6 5
5 6
6 4
6 6
3 5
7 2
Output
17 | instruction | 0 | 23,628 | 10 | 47,256 |
"Correct Solution:
```
N, W = [int(i) for i in input().split()]
V = [[int(i) for i in input().split()] for j in range(N)]
INF = 10 ** 18
sup_v = 10 ** 5
DP = [INF] * (sup_v + 1)
DP[0] = 0
for w, v in V:
for i in range(sup_v, -1, -1):
DP[i] = min(DP[i], DP[i-v] + w)
for i in range(sup_v, -1, -1):
if DP[i] <= W:
print(i)
break
``` | output | 1 | 23,628 | 10 | 47,257 |
Provide a correct Python 3 solution for this coding contest problem.
There are N items, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.
Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.
Find the maximum possible sum of the values of items that Taro takes home.
Constraints
* All values in input are integers.
* 1 \leq N \leq 100
* 1 \leq W \leq 10^9
* 1 \leq w_i \leq W
* 1 \leq v_i \leq 10^3
Input
Input is given from Standard Input in the following format:
N W
w_1 v_1
w_2 v_2
:
w_N v_N
Output
Print the maximum possible sum of the values of items that Taro takes home.
Examples
Input
3 8
3 30
4 50
5 60
Output
90
Input
1 1000000000
1000000000 10
Output
10
Input
6 15
6 5
5 6
6 4
6 6
3 5
7 2
Output
17 | instruction | 0 | 23,629 | 10 | 47,258 |
"Correct Solution:
```
N, W = map(int, input().split())
wv = [list(map(int, input().split())) for _ in range(N)]
dp = [10**9+7] * (10**5+2)
dp[0] = 0
for i in range(1, N+1):
w, v = wv[i-1]
for j in range(10**5, 0, -1):
if j-v >= 0:
dp[j] = min(dp[j], dp[j-v]+w)
ret = 0
for i in range(1, 10**5+1):
if dp[i] <= W:
ret = i
print(ret)
``` | output | 1 | 23,629 | 10 | 47,259 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N items, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.
Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.
Find the maximum possible sum of the values of items that Taro takes home.
Constraints
* All values in input are integers.
* 1 \leq N \leq 100
* 1 \leq W \leq 10^9
* 1 \leq w_i \leq W
* 1 \leq v_i \leq 10^3
Input
Input is given from Standard Input in the following format:
N W
w_1 v_1
w_2 v_2
:
w_N v_N
Output
Print the maximum possible sum of the values of items that Taro takes home.
Examples
Input
3 8
3 30
4 50
5 60
Output
90
Input
1 1000000000
1000000000 10
Output
10
Input
6 15
6 5
5 6
6 4
6 6
3 5
7 2
Output
17
Submitted Solution:
```
N,W = map(int, input().split())
INF = 10**9+1
M = 10**5+1
dp = [INF]*M #dp[i]:価値の総和がiになるときの重さの総和の最小値
dp[0] = 0
for _ in range(N):
w, v = map(int, input().split())
for i in range(M-1, v-1, -1):
dp[i] = min(dp[i], dp[i-v]+w)
for i in range(M-1, -1, -1):
if dp[i] <= W:
print(i)
break
``` | instruction | 0 | 23,630 | 10 | 47,260 |
Yes | output | 1 | 23,630 | 10 | 47,261 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N items, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.
Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.
Find the maximum possible sum of the values of items that Taro takes home.
Constraints
* All values in input are integers.
* 1 \leq N \leq 100
* 1 \leq W \leq 10^9
* 1 \leq w_i \leq W
* 1 \leq v_i \leq 10^3
Input
Input is given from Standard Input in the following format:
N W
w_1 v_1
w_2 v_2
:
w_N v_N
Output
Print the maximum possible sum of the values of items that Taro takes home.
Examples
Input
3 8
3 30
4 50
5 60
Output
90
Input
1 1000000000
1000000000 10
Output
10
Input
6 15
6 5
5 6
6 4
6 6
3 5
7 2
Output
17
Submitted Solution:
```
n,W=map(int,input().split())
wv=[[int(i) for i in input().split()]for _ in range(n)]
dp=[W+1]*(10**5+1)
dp[0]=0
for i in range(0,n):
w,v = wv[i]
for j in range(10**5,v-1,-1):
dp[j]=min(dp[j],dp[j-v]+w)
for i in range(10**5,-1,-1):
if dp[i]<=W:
print(i)
break
``` | instruction | 0 | 23,631 | 10 | 47,262 |
Yes | output | 1 | 23,631 | 10 | 47,263 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N items, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.
Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.
Find the maximum possible sum of the values of items that Taro takes home.
Constraints
* All values in input are integers.
* 1 \leq N \leq 100
* 1 \leq W \leq 10^9
* 1 \leq w_i \leq W
* 1 \leq v_i \leq 10^3
Input
Input is given from Standard Input in the following format:
N W
w_1 v_1
w_2 v_2
:
w_N v_N
Output
Print the maximum possible sum of the values of items that Taro takes home.
Examples
Input
3 8
3 30
4 50
5 60
Output
90
Input
1 1000000000
1000000000 10
Output
10
Input
6 15
6 5
5 6
6 4
6 6
3 5
7 2
Output
17
Submitted Solution:
```
N,W = map(int, input().split())
wv = [[int(i) for i in input().split()] for j in range(N)]
maxV = 10 ** 5
dp = [float('inf')]*(maxV+1)
dp[0] = 0
for w, v in wv:
for i in range(maxV, -1, -1):
dp[i] = min(dp[i], dp[i-v] + w)
for i in range(maxV, -1, -1):
if dp[i] <= W:
print(i)
break
``` | instruction | 0 | 23,632 | 10 | 47,264 |
Yes | output | 1 | 23,632 | 10 | 47,265 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N items, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.
Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.
Find the maximum possible sum of the values of items that Taro takes home.
Constraints
* All values in input are integers.
* 1 \leq N \leq 100
* 1 \leq W \leq 10^9
* 1 \leq w_i \leq W
* 1 \leq v_i \leq 10^3
Input
Input is given from Standard Input in the following format:
N W
w_1 v_1
w_2 v_2
:
w_N v_N
Output
Print the maximum possible sum of the values of items that Taro takes home.
Examples
Input
3 8
3 30
4 50
5 60
Output
90
Input
1 1000000000
1000000000 10
Output
10
Input
6 15
6 5
5 6
6 4
6 6
3 5
7 2
Output
17
Submitted Solution:
```
N, W = map(int, input().split())
MAX_V = N * (10 ** 3)
wv = [list(map(int, input().split())) for _ in range(N)]
dp = [10**9+7 for _ in range(MAX_V + 1)]
dp[0] = 0
for w, v in wv:
for j in range(MAX_V, -1, -1):
if j - v >= 0:
dp[j] = min(dp[j],dp[j - v] + w)
mv = 0
for i in range(MAX_V + 1):
if dp[i] <= W:
mv = i
print(mv)
``` | instruction | 0 | 23,633 | 10 | 47,266 |
Yes | output | 1 | 23,633 | 10 | 47,267 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N items, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.
Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.
Find the maximum possible sum of the values of items that Taro takes home.
Constraints
* All values in input are integers.
* 1 \leq N \leq 100
* 1 \leq W \leq 10^9
* 1 \leq w_i \leq W
* 1 \leq v_i \leq 10^3
Input
Input is given from Standard Input in the following format:
N W
w_1 v_1
w_2 v_2
:
w_N v_N
Output
Print the maximum possible sum of the values of items that Taro takes home.
Examples
Input
3 8
3 30
4 50
5 60
Output
90
Input
1 1000000000
1000000000 10
Output
10
Input
6 15
6 5
5 6
6 4
6 6
3 5
7 2
Output
17
Submitted Solution:
```
n,w=map(int,input().split())
A=[list(map(int,input().split())) for i in range(n)]
dp=[float("inf")]*(10**3+1)
dp[0]=0
for i in range(n):
for j in range(10**3,A[i][1]-1,-1):
dp[j]=min(dp[j],dp[j-A[i][1]]+A[i][0])
ans=0
for v,e in enumerate(dp):
if e<=w:
ans=v
print(ans)
``` | instruction | 0 | 23,634 | 10 | 47,268 |
No | output | 1 | 23,634 | 10 | 47,269 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N items, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.
Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.
Find the maximum possible sum of the values of items that Taro takes home.
Constraints
* All values in input are integers.
* 1 \leq N \leq 100
* 1 \leq W \leq 10^9
* 1 \leq w_i \leq W
* 1 \leq v_i \leq 10^3
Input
Input is given from Standard Input in the following format:
N W
w_1 v_1
w_2 v_2
:
w_N v_N
Output
Print the maximum possible sum of the values of items that Taro takes home.
Examples
Input
3 8
3 30
4 50
5 60
Output
90
Input
1 1000000000
1000000000 10
Output
10
Input
6 15
6 5
5 6
6 4
6 6
3 5
7 2
Output
17
Submitted Solution:
```
import numpy as np
N, W = map(int, input().split())
wv = []
#i番目までを詰めたときの最大の価値を記録
dp = np.zeros(W+1)
for i in range(N):
w, v = map(int, input().split())
dp[w:] = np.maximum(dp[:-w]+v, dp[w:])
print(int(dp[-1]))
``` | instruction | 0 | 23,635 | 10 | 47,270 |
No | output | 1 | 23,635 | 10 | 47,271 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N items, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.
Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.
Find the maximum possible sum of the values of items that Taro takes home.
Constraints
* All values in input are integers.
* 1 \leq N \leq 100
* 1 \leq W \leq 10^9
* 1 \leq w_i \leq W
* 1 \leq v_i \leq 10^3
Input
Input is given from Standard Input in the following format:
N W
w_1 v_1
w_2 v_2
:
w_N v_N
Output
Print the maximum possible sum of the values of items that Taro takes home.
Examples
Input
3 8
3 30
4 50
5 60
Output
90
Input
1 1000000000
1000000000 10
Output
10
Input
6 15
6 5
5 6
6 4
6 6
3 5
7 2
Output
17
Submitted Solution:
```
N, W = map(int, input().split())
items = [tuple(map(int, input().split())) for _ in range(N)]
inf = 10**9+7
"""
dp[n][v]: 1~n番目までの品物から、総価値v以上になるように選んだ時の総重量の最小値
"""
dp = [[inf] * (10**5+1) for _ in range(N+1)]
dp[0][0] = 0
sum_values = 0
for n in range(1, N+1)[:3]:
sum_values += items[n-1][1]
for v in range(sum_values+1):
if v >= items[n-1][1]:
dp[n][v] = min(dp[n-1][v], dp[n-1][v-items[n-1][1]] + items[n-1][0])
else:
dp[n][v] = dp[n-1][v]
result = 0
for v in range(1, 10**5+1):
if dp[N][v] <= W:
result = v
elif dp[N][v] == inf:
continue
else:
break
print(result)
``` | instruction | 0 | 23,636 | 10 | 47,272 |
No | output | 1 | 23,636 | 10 | 47,273 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N items, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.
Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.
Find the maximum possible sum of the values of items that Taro takes home.
Constraints
* All values in input are integers.
* 1 \leq N \leq 100
* 1 \leq W \leq 10^9
* 1 \leq w_i \leq W
* 1 \leq v_i \leq 10^3
Input
Input is given from Standard Input in the following format:
N W
w_1 v_1
w_2 v_2
:
w_N v_N
Output
Print the maximum possible sum of the values of items that Taro takes home.
Examples
Input
3 8
3 30
4 50
5 60
Output
90
Input
1 1000000000
1000000000 10
Output
10
Input
6 15
6 5
5 6
6 4
6 6
3 5
7 2
Output
17
Submitted Solution:
```
n, w = map(int, input().split())
wv = [list(map(int, input().split())) for _ in range(n)]
dp = [[0]*(w+1) for _ in range(n+1)]
for i in range(n):
w_, v_ = wv[i]
for j in range(w+1):
if j < w_:
dp[i+1][j] = dp[i][j]
else:
dp[i+1][j] = max(dp[i][j-w_]+v_, dp[i][j])
print(dp[n][w])
``` | instruction | 0 | 23,637 | 10 | 47,274 |
No | output | 1 | 23,637 | 10 | 47,275 |
Provide a correct Python 3 solution for this coding contest problem.
You are given $n$ packages of $w_i$ kg from a belt conveyor in order ($i = 0, 1, ... n-1$). You should load all packages onto $k$ trucks which have the common maximum load $P$. Each truck can load consecutive packages (more than or equals to zero) from the belt conveyor unless the total weights of the packages in the sequence does not exceed the maximum load $P$.
Write a program which reads $n$, $k$ and $w_i$, and reports the minimum value of the maximum load $P$ to load all packages from the belt conveyor.
Constraints
* $1 \leq n \leq 100,000$
* $1 \leq k \leq 100,000$
* $1 \leq w_i \leq 10,000$
Input
In the first line, two integers $n$ and $k$ are given separated by a space character. In the following $n$ lines, $w_i$ are given respectively.
Output
Print the minimum value of $P$ in a line.
Examples
Input
5 3
8
1
7
3
9
Output
10
Input
4 2
1
2
2
6
Output
6 | instruction | 0 | 23,799 | 10 | 47,598 |
"Correct Solution:
```
def check(p):
i = 0
for _ in range(k):
total = 0
while total+n_list[i] <=p:
total += n_list[i]
i += 1
if i == n:
return n
return i
def solve():
left = 0
right = 10**5 * 10**4
middle = 0
while right - left > 1:
middle = (left+right)//2
temp_ans = check(middle)
if temp_ans >= n:
right = middle
else:
left = middle
return right
n,k = map(int,input().split())
n_list = [int(input()) for _ in range(n)]
ans = solve()
print(ans)
``` | output | 1 | 23,799 | 10 | 47,599 |
Provide a correct Python 3 solution for this coding contest problem.
You are given $n$ packages of $w_i$ kg from a belt conveyor in order ($i = 0, 1, ... n-1$). You should load all packages onto $k$ trucks which have the common maximum load $P$. Each truck can load consecutive packages (more than or equals to zero) from the belt conveyor unless the total weights of the packages in the sequence does not exceed the maximum load $P$.
Write a program which reads $n$, $k$ and $w_i$, and reports the minimum value of the maximum load $P$ to load all packages from the belt conveyor.
Constraints
* $1 \leq n \leq 100,000$
* $1 \leq k \leq 100,000$
* $1 \leq w_i \leq 10,000$
Input
In the first line, two integers $n$ and $k$ are given separated by a space character. In the following $n$ lines, $w_i$ are given respectively.
Output
Print the minimum value of $P$ in a line.
Examples
Input
5 3
8
1
7
3
9
Output
10
Input
4 2
1
2
2
6
Output
6 | instruction | 0 | 23,801 | 10 | 47,602 |
"Correct Solution:
```
n, k = map(int, input().split())
W = []
for _ in range(n):
W.append(int(input()))
left = 0
right = 100_000 * 10_000
while left < right - 1:
P = (right + left) // 2
i = 0
for _ in range(k):
if i == n: break
s = 0
while s + W[i] <= P:
s += W[i]
i += 1
if i == n: break
if i >= n:
right = P
else:
left = P
print(right)
``` | output | 1 | 23,801 | 10 | 47,603 |
Provide a correct Python 3 solution for this coding contest problem.
You are given $n$ packages of $w_i$ kg from a belt conveyor in order ($i = 0, 1, ... n-1$). You should load all packages onto $k$ trucks which have the common maximum load $P$. Each truck can load consecutive packages (more than or equals to zero) from the belt conveyor unless the total weights of the packages in the sequence does not exceed the maximum load $P$.
Write a program which reads $n$, $k$ and $w_i$, and reports the minimum value of the maximum load $P$ to load all packages from the belt conveyor.
Constraints
* $1 \leq n \leq 100,000$
* $1 \leq k \leq 100,000$
* $1 \leq w_i \leq 10,000$
Input
In the first line, two integers $n$ and $k$ are given separated by a space character. In the following $n$ lines, $w_i$ are given respectively.
Output
Print the minimum value of $P$ in a line.
Examples
Input
5 3
8
1
7
3
9
Output
10
Input
4 2
1
2
2
6
Output
6 | instruction | 0 | 23,802 | 10 | 47,604 |
"Correct Solution:
```
def check(P, k, n, T):
res = 0
for _ in range(k):
s = 0
while s + T[res] <= P:
s += T[res]
res += 1
if res == n:
return n
return res
n, k = map(int, input().split())
w = [int(input()) for _ in range(n)]
left = 0
right = n * 10000
mid = (left + right) // 2
while right - left > 1:
v = check(mid, k, n, w)
if v == n:
right = mid
else:
left = mid
mid = (left + right) // 2
print(right)
``` | output | 1 | 23,802 | 10 | 47,605 |
Provide a correct Python 3 solution for this coding contest problem.
You are given $n$ packages of $w_i$ kg from a belt conveyor in order ($i = 0, 1, ... n-1$). You should load all packages onto $k$ trucks which have the common maximum load $P$. Each truck can load consecutive packages (more than or equals to zero) from the belt conveyor unless the total weights of the packages in the sequence does not exceed the maximum load $P$.
Write a program which reads $n$, $k$ and $w_i$, and reports the minimum value of the maximum load $P$ to load all packages from the belt conveyor.
Constraints
* $1 \leq n \leq 100,000$
* $1 \leq k \leq 100,000$
* $1 \leq w_i \leq 10,000$
Input
In the first line, two integers $n$ and $k$ are given separated by a space character. In the following $n$ lines, $w_i$ are given respectively.
Output
Print the minimum value of $P$ in a line.
Examples
Input
5 3
8
1
7
3
9
Output
10
Input
4 2
1
2
2
6
Output
6 | instruction | 0 | 23,803 | 10 | 47,606 |
"Correct Solution:
```
n, k = [int(t) for t in input().split()]
w = [int(input()) for i in range(n)]
hi = sum(w)
lo = max(w)
def canMove(w, P, k):
s = 0
for i in range(len(w)):
if s + w[i] > P:
s = 0
k -= 1
if k <= 0: return False
s += w[i]
return True
P = hi
while lo < hi:
m = (lo + hi) // 2
if canMove(w, m, k):
P = m
hi = m
else:
lo = m + 1
print(P)
``` | output | 1 | 23,803 | 10 | 47,607 |
Provide a correct Python 3 solution for this coding contest problem.
You are given $n$ packages of $w_i$ kg from a belt conveyor in order ($i = 0, 1, ... n-1$). You should load all packages onto $k$ trucks which have the common maximum load $P$. Each truck can load consecutive packages (more than or equals to zero) from the belt conveyor unless the total weights of the packages in the sequence does not exceed the maximum load $P$.
Write a program which reads $n$, $k$ and $w_i$, and reports the minimum value of the maximum load $P$ to load all packages from the belt conveyor.
Constraints
* $1 \leq n \leq 100,000$
* $1 \leq k \leq 100,000$
* $1 \leq w_i \leq 10,000$
Input
In the first line, two integers $n$ and $k$ are given separated by a space character. In the following $n$ lines, $w_i$ are given respectively.
Output
Print the minimum value of $P$ in a line.
Examples
Input
5 3
8
1
7
3
9
Output
10
Input
4 2
1
2
2
6
Output
6 | instruction | 0 | 23,804 | 10 | 47,608 |
"Correct Solution:
```
import sys
readline = sys.stdin.readline
N, K = map(int, input().split())
W = tuple(int(readline()) for _ in range(N))
ma = max(W)
def check(x):
if x < ma:
return False
use = 1
rest = x
for w in W:
if rest >= w:
rest -= w
else:
rest = x - w
use += 1
return use <= K
l = 0
r = sum(W)
while r - l > 1:
m = (r + l) // 2
if check(m):
r = m
else:
l = m
print(r)
``` | output | 1 | 23,804 | 10 | 47,609 |
Provide a correct Python 3 solution for this coding contest problem.
You are given $n$ packages of $w_i$ kg from a belt conveyor in order ($i = 0, 1, ... n-1$). You should load all packages onto $k$ trucks which have the common maximum load $P$. Each truck can load consecutive packages (more than or equals to zero) from the belt conveyor unless the total weights of the packages in the sequence does not exceed the maximum load $P$.
Write a program which reads $n$, $k$ and $w_i$, and reports the minimum value of the maximum load $P$ to load all packages from the belt conveyor.
Constraints
* $1 \leq n \leq 100,000$
* $1 \leq k \leq 100,000$
* $1 \leq w_i \leq 10,000$
Input
In the first line, two integers $n$ and $k$ are given separated by a space character. In the following $n$ lines, $w_i$ are given respectively.
Output
Print the minimum value of $P$ in a line.
Examples
Input
5 3
8
1
7
3
9
Output
10
Input
4 2
1
2
2
6
Output
6 | instruction | 0 | 23,805 | 10 | 47,610 |
"Correct Solution:
```
def allocation():
n, cars = map(int, input().split())
weight = [int(input()) for i in range(n)]
left = max(weight)
right = sum(weight)
while left < right:
mid = (left + right) // 2
if check(weight, cars, mid):
right = mid
else:
left = mid + 1
print(left)
def check(weight, cars, capacity):
t = 0
c = 1
for w in weight:
t += w
if t > capacity:
t = w
c += 1
if c <= cars:
return True
else:
return False
if __name__ == '__main__':
allocation()
``` | output | 1 | 23,805 | 10 | 47,611 |
Provide a correct Python 3 solution for this coding contest problem.
You are given $n$ packages of $w_i$ kg from a belt conveyor in order ($i = 0, 1, ... n-1$). You should load all packages onto $k$ trucks which have the common maximum load $P$. Each truck can load consecutive packages (more than or equals to zero) from the belt conveyor unless the total weights of the packages in the sequence does not exceed the maximum load $P$.
Write a program which reads $n$, $k$ and $w_i$, and reports the minimum value of the maximum load $P$ to load all packages from the belt conveyor.
Constraints
* $1 \leq n \leq 100,000$
* $1 \leq k \leq 100,000$
* $1 \leq w_i \leq 10,000$
Input
In the first line, two integers $n$ and $k$ are given separated by a space character. In the following $n$ lines, $w_i$ are given respectively.
Output
Print the minimum value of $P$ in a line.
Examples
Input
5 3
8
1
7
3
9
Output
10
Input
4 2
1
2
2
6
Output
6 | instruction | 0 | 23,806 | 10 | 47,612 |
"Correct Solution:
```
n,k = map(int, input().split())
W = [int(input()) for i in range(n)]
right = sum(W)
left = max(W)-1
while left + 1< right:
cnt = 1
cur = 0
mid = (right + left) // 2
for w in W:
if mid < cur + w:
cur = w
cnt += 1
else:
cur += w
if cnt <= k:
right = mid
else:
left = mid
print(right)
``` | output | 1 | 23,806 | 10 | 47,613 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given $n$ packages of $w_i$ kg from a belt conveyor in order ($i = 0, 1, ... n-1$). You should load all packages onto $k$ trucks which have the common maximum load $P$. Each truck can load consecutive packages (more than or equals to zero) from the belt conveyor unless the total weights of the packages in the sequence does not exceed the maximum load $P$.
Write a program which reads $n$, $k$ and $w_i$, and reports the minimum value of the maximum load $P$ to load all packages from the belt conveyor.
Constraints
* $1 \leq n \leq 100,000$
* $1 \leq k \leq 100,000$
* $1 \leq w_i \leq 10,000$
Input
In the first line, two integers $n$ and $k$ are given separated by a space character. In the following $n$ lines, $w_i$ are given respectively.
Output
Print the minimum value of $P$ in a line.
Examples
Input
5 3
8
1
7
3
9
Output
10
Input
4 2
1
2
2
6
Output
6
Submitted Solution:
```
N, K = map(int, input().split())
W = [int(input()) for _ in range(N)]
P_MIN = max(W)
P_MAX = 10 ** 9
while P_MIN < P_MAX:
P = (P_MAX + P_MIN) // 2 # トラックの最大積載量
M = 0
i = 0
while i < N: # 最大積載量Pのトラックに荷物がなくなるまで載せる
mass = 0 # トラックは初め何も載っていない
while i < N and mass + W[i] <= P: # 荷物がなくなるか,トラックに積めなくなるまで荷物を載せる
mass += W[i]
i += 1
M += 1 # 使用したトラックの台数が1だけ増える
if M > K: # 最大積載量が小さすぎて,トラックが足りない場合
P_MIN = P + 1
elif M <= K: # 逆に最大積載量が大きすぎて,トラックが余る場合
P_MAX = P
print(P_MIN)
``` | instruction | 0 | 23,810 | 10 | 47,620 |
Yes | output | 1 | 23,810 | 10 | 47,621 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.