message stringlengths 2 65.1k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 0 108k | cluster float64 14 14 | __index_level_0__ int64 0 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k.
There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th (1 ≤ i ≤ k) child wrote the number ai (1 ≤ ai ≤ n·k). All numbers ai accidentally turned out to be different.
Now the children wonder, how to divide the orange so as to meet these conditions:
* each child gets exactly n orange segments;
* the i-th child gets the segment with number ai for sure;
* no segment goes to two children simultaneously.
Help the children, divide the orange and fulfill the requirements, described above.
Input
The first line contains two integers n, k (1 ≤ n, k ≤ 30). The second line contains k space-separated integers a1, a2, ..., ak (1 ≤ ai ≤ n·k), where ai is the number of the orange segment that the i-th child would like to get.
It is guaranteed that all numbers ai are distinct.
Output
Print exactly n·k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second n integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces.
You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.
Examples
Input
2 2
4 1
Output
2 4
1 3
Input
3 1
2
Output
3 2 1 | instruction | 0 | 49,949 | 14 | 99,898 |
Tags: implementation
Correct Solution:
```
#!/usr/bin/env python3
from sys import stdin, stdout
def solve():
n, k = map(int,stdin.readline().split())
li = list(map(int,stdin.readline().split()))
vis = [-1 for i in range(n*k)]
for i in range(k):
vis[li[i]-1] = i
ans = [list() for i in range(k)]
p = 0
for i in range(n*k):
if vis[i]<0:
ans[p].append(i+1)
p += 1
p %= k
else:
ans[vis[i]].append(i+1)
for l in ans:
for e in l:
print(e,end=' ')
print()
LOCAL_TEST = not __debug__
if LOCAL_TEST:
infile = __file__.split('.')[0] + '-test.in'
stdin = open(infile, 'r')
tcs = (int(stdin.readline().strip()) if LOCAL_TEST else 1)
t = 1
while t <= tcs:
solve()
t += 1
``` | output | 1 | 49,949 | 14 | 99,899 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k.
There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th (1 ≤ i ≤ k) child wrote the number ai (1 ≤ ai ≤ n·k). All numbers ai accidentally turned out to be different.
Now the children wonder, how to divide the orange so as to meet these conditions:
* each child gets exactly n orange segments;
* the i-th child gets the segment with number ai for sure;
* no segment goes to two children simultaneously.
Help the children, divide the orange and fulfill the requirements, described above.
Input
The first line contains two integers n, k (1 ≤ n, k ≤ 30). The second line contains k space-separated integers a1, a2, ..., ak (1 ≤ ai ≤ n·k), where ai is the number of the orange segment that the i-th child would like to get.
It is guaranteed that all numbers ai are distinct.
Output
Print exactly n·k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second n integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces.
You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.
Examples
Input
2 2
4 1
Output
2 4
1 3
Input
3 1
2
Output
3 2 1 | instruction | 0 | 49,950 | 14 | 99,900 |
Tags: implementation
Correct Solution:
```
n,k=map(int,input().split())
l1=list(range(1,n*k+1))
l2=list(map(int,input().split()))
d1={}
for item in l2:
d1[item]=1
x=0
for i in range(1,k+1):
z=1
print(l2[i-1],end=" ")
while z<n:
if l1[x] not in l2:
print(l1[x],end=" ")
z+=1
x+=1
print("")
``` | output | 1 | 49,950 | 14 | 99,901 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k.
There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th (1 ≤ i ≤ k) child wrote the number ai (1 ≤ ai ≤ n·k). All numbers ai accidentally turned out to be different.
Now the children wonder, how to divide the orange so as to meet these conditions:
* each child gets exactly n orange segments;
* the i-th child gets the segment with number ai for sure;
* no segment goes to two children simultaneously.
Help the children, divide the orange and fulfill the requirements, described above.
Input
The first line contains two integers n, k (1 ≤ n, k ≤ 30). The second line contains k space-separated integers a1, a2, ..., ak (1 ≤ ai ≤ n·k), where ai is the number of the orange segment that the i-th child would like to get.
It is guaranteed that all numbers ai are distinct.
Output
Print exactly n·k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second n integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces.
You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.
Examples
Input
2 2
4 1
Output
2 4
1 3
Input
3 1
2
Output
3 2 1 | instruction | 0 | 49,951 | 14 | 99,902 |
Tags: implementation
Correct Solution:
```
# https://codeforces.com/problemset/problem/244/A
# 900
n, k = map(int, input().split())
d = {}
l = []
for s in input().split():
s = int(s)
d[s] = True
l.append(s)
for s in l:
print(s, end="")
c = 1
for i in range(1, (n*k)+1):
if d.get(i) is None:
print("", i, end="")
d[i] = True
c += 1
if c == n:
break
print()
``` | output | 1 | 49,951 | 14 | 99,903 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k.
There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th (1 ≤ i ≤ k) child wrote the number ai (1 ≤ ai ≤ n·k). All numbers ai accidentally turned out to be different.
Now the children wonder, how to divide the orange so as to meet these conditions:
* each child gets exactly n orange segments;
* the i-th child gets the segment with number ai for sure;
* no segment goes to two children simultaneously.
Help the children, divide the orange and fulfill the requirements, described above.
Input
The first line contains two integers n, k (1 ≤ n, k ≤ 30). The second line contains k space-separated integers a1, a2, ..., ak (1 ≤ ai ≤ n·k), where ai is the number of the orange segment that the i-th child would like to get.
It is guaranteed that all numbers ai are distinct.
Output
Print exactly n·k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second n integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces.
You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.
Examples
Input
2 2
4 1
Output
2 4
1 3
Input
3 1
2
Output
3 2 1
Submitted Solution:
```
n, k = [int(x) for x in input().split()]
likes = [int(x) for x in input().split()]
oranges = [ True ] * (n * k)
for i in likes:
oranges[i - 1] = False
oranges_cnt = 0
for l in likes:
print(l, end=" ")
for _ in range(n - 1):
while not oranges[oranges_cnt]:
oranges_cnt += 1
print(oranges_cnt + 1, end=" ")
oranges_cnt += 1
print()
``` | instruction | 0 | 49,952 | 14 | 99,904 |
Yes | output | 1 | 49,952 | 14 | 99,905 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k.
There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th (1 ≤ i ≤ k) child wrote the number ai (1 ≤ ai ≤ n·k). All numbers ai accidentally turned out to be different.
Now the children wonder, how to divide the orange so as to meet these conditions:
* each child gets exactly n orange segments;
* the i-th child gets the segment with number ai for sure;
* no segment goes to two children simultaneously.
Help the children, divide the orange and fulfill the requirements, described above.
Input
The first line contains two integers n, k (1 ≤ n, k ≤ 30). The second line contains k space-separated integers a1, a2, ..., ak (1 ≤ ai ≤ n·k), where ai is the number of the orange segment that the i-th child would like to get.
It is guaranteed that all numbers ai are distinct.
Output
Print exactly n·k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second n integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces.
You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.
Examples
Input
2 2
4 1
Output
2 4
1 3
Input
3 1
2
Output
3 2 1
Submitted Solution:
```
n, k = [int(i) for i in input().split()]
arr = [int(i) for i in input().split()]
num = [i for i in range(1,(n*k)+1)]
nums = []
for i in num:
if i not in arr:
nums.append(i)
start = 0
for i in range(k):
print(*(nums[start:start+n-1]+[arr[i]]))
start += n-1
``` | instruction | 0 | 49,953 | 14 | 99,906 |
Yes | output | 1 | 49,953 | 14 | 99,907 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k.
There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th (1 ≤ i ≤ k) child wrote the number ai (1 ≤ ai ≤ n·k). All numbers ai accidentally turned out to be different.
Now the children wonder, how to divide the orange so as to meet these conditions:
* each child gets exactly n orange segments;
* the i-th child gets the segment with number ai for sure;
* no segment goes to two children simultaneously.
Help the children, divide the orange and fulfill the requirements, described above.
Input
The first line contains two integers n, k (1 ≤ n, k ≤ 30). The second line contains k space-separated integers a1, a2, ..., ak (1 ≤ ai ≤ n·k), where ai is the number of the orange segment that the i-th child would like to get.
It is guaranteed that all numbers ai are distinct.
Output
Print exactly n·k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second n integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces.
You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.
Examples
Input
2 2
4 1
Output
2 4
1 3
Input
3 1
2
Output
3 2 1
Submitted Solution:
```
n,k=map(int, input().split())
l1=list(map(int, input().split()))
l2=[0 for i in range(0, n*k)]
for i in l1:
l2[i-1]=1
ch=0
for i in range(0,k):
print(l1[i],end=" ")
j=0
while j<n-1:
if l2[ch]==0:
print(ch+1,end=" ")
ch=ch+1
j=j+1
else:
ch=ch+1
print()
``` | instruction | 0 | 49,954 | 14 | 99,908 |
Yes | output | 1 | 49,954 | 14 | 99,909 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k.
There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th (1 ≤ i ≤ k) child wrote the number ai (1 ≤ ai ≤ n·k). All numbers ai accidentally turned out to be different.
Now the children wonder, how to divide the orange so as to meet these conditions:
* each child gets exactly n orange segments;
* the i-th child gets the segment with number ai for sure;
* no segment goes to two children simultaneously.
Help the children, divide the orange and fulfill the requirements, described above.
Input
The first line contains two integers n, k (1 ≤ n, k ≤ 30). The second line contains k space-separated integers a1, a2, ..., ak (1 ≤ ai ≤ n·k), where ai is the number of the orange segment that the i-th child would like to get.
It is guaranteed that all numbers ai are distinct.
Output
Print exactly n·k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second n integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces.
You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.
Examples
Input
2 2
4 1
Output
2 4
1 3
Input
3 1
2
Output
3 2 1
Submitted Solution:
```
a = input().split(' ')
n = int(a[0])
k = int(a[1])
a = input().split(' ')
b = []
for i in range(k):
a[i] = int(a[i])-1
b.append(a[i])
b.sort()
d = {}
counter = 0
for i in range(k):
d[i] = [a[i]]
count = 0
while count < n-1:
if counter not in b:
d[i].append(counter)
count = count + 1
counter = counter + 1
else:
counter = counter + 1
for i in range(k):
for j in d[i]:
print(j+1,end = ' ')
print()
``` | instruction | 0 | 49,955 | 14 | 99,910 |
Yes | output | 1 | 49,955 | 14 | 99,911 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k.
There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th (1 ≤ i ≤ k) child wrote the number ai (1 ≤ ai ≤ n·k). All numbers ai accidentally turned out to be different.
Now the children wonder, how to divide the orange so as to meet these conditions:
* each child gets exactly n orange segments;
* the i-th child gets the segment with number ai for sure;
* no segment goes to two children simultaneously.
Help the children, divide the orange and fulfill the requirements, described above.
Input
The first line contains two integers n, k (1 ≤ n, k ≤ 30). The second line contains k space-separated integers a1, a2, ..., ak (1 ≤ ai ≤ n·k), where ai is the number of the orange segment that the i-th child would like to get.
It is guaranteed that all numbers ai are distinct.
Output
Print exactly n·k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second n integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces.
You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.
Examples
Input
2 2
4 1
Output
2 4
1 3
Input
3 1
2
Output
3 2 1
Submitted Solution:
```
n,k=map(int,input().split())
a=list(map(int,input().split()))
c=[]
for i in range(k):
print(a[i],end=' ')
l=1
for j in range(n*k+1):
if j not in a and j not in c and l<n:
print(j,end=' ')
l+=1
c.append(j)
if l>=n:
#print(l)
l=1
break
print()
``` | instruction | 0 | 49,956 | 14 | 99,912 |
No | output | 1 | 49,956 | 14 | 99,913 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k.
There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th (1 ≤ i ≤ k) child wrote the number ai (1 ≤ ai ≤ n·k). All numbers ai accidentally turned out to be different.
Now the children wonder, how to divide the orange so as to meet these conditions:
* each child gets exactly n orange segments;
* the i-th child gets the segment with number ai for sure;
* no segment goes to two children simultaneously.
Help the children, divide the orange and fulfill the requirements, described above.
Input
The first line contains two integers n, k (1 ≤ n, k ≤ 30). The second line contains k space-separated integers a1, a2, ..., ak (1 ≤ ai ≤ n·k), where ai is the number of the orange segment that the i-th child would like to get.
It is guaranteed that all numbers ai are distinct.
Output
Print exactly n·k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second n integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces.
You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.
Examples
Input
2 2
4 1
Output
2 4
1 3
Input
3 1
2
Output
3 2 1
Submitted Solution:
```
n,k=list(map(int,input().split()))
t=list(map(int,input().split()))
p=[]
for i in range(len(t)):
l=[t[i]]
for s in range(1,n*k+1):
if s not in t:
if s not in p:
l.append(s)
p.append(s)
if len(l)==n:
l.sort()
print(*l)
break
``` | instruction | 0 | 49,957 | 14 | 99,914 |
No | output | 1 | 49,957 | 14 | 99,915 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k.
There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th (1 ≤ i ≤ k) child wrote the number ai (1 ≤ ai ≤ n·k). All numbers ai accidentally turned out to be different.
Now the children wonder, how to divide the orange so as to meet these conditions:
* each child gets exactly n orange segments;
* the i-th child gets the segment with number ai for sure;
* no segment goes to two children simultaneously.
Help the children, divide the orange and fulfill the requirements, described above.
Input
The first line contains two integers n, k (1 ≤ n, k ≤ 30). The second line contains k space-separated integers a1, a2, ..., ak (1 ≤ ai ≤ n·k), where ai is the number of the orange segment that the i-th child would like to get.
It is guaranteed that all numbers ai are distinct.
Output
Print exactly n·k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second n integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces.
You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.
Examples
Input
2 2
4 1
Output
2 4
1 3
Input
3 1
2
Output
3 2 1
Submitted Solution:
```
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
ranges = [int(i) for i in range(1, (a[0]*a[1])+1)]
print(ranges)
``` | instruction | 0 | 49,958 | 14 | 99,916 |
No | output | 1 | 49,958 | 14 | 99,917 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k.
There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th (1 ≤ i ≤ k) child wrote the number ai (1 ≤ ai ≤ n·k). All numbers ai accidentally turned out to be different.
Now the children wonder, how to divide the orange so as to meet these conditions:
* each child gets exactly n orange segments;
* the i-th child gets the segment with number ai for sure;
* no segment goes to two children simultaneously.
Help the children, divide the orange and fulfill the requirements, described above.
Input
The first line contains two integers n, k (1 ≤ n, k ≤ 30). The second line contains k space-separated integers a1, a2, ..., ak (1 ≤ ai ≤ n·k), where ai is the number of the orange segment that the i-th child would like to get.
It is guaranteed that all numbers ai are distinct.
Output
Print exactly n·k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second n integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces.
You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.
Examples
Input
2 2
4 1
Output
2 4
1 3
Input
3 1
2
Output
3 2 1
Submitted Solution:
```
from collections import Counter
class Task:
n = 0
k = 0
special = []
answer = []
def getData(self):
self.n, self.k = [int(x) for x in input().split()]
self.special = [int(x) for x in input().split()]
def solve(self):
numbers = [i for i in range(1, self.n * self.k + 1)]
numbers = [x for x in numbers if x not in self.special]
self.answer = [numbers[i : i + self.n - 1] for i in range(0, self.k)]
for i in range(0, self.k):
self.answer[i] += [self.special[i]]
def printAnswer(self):
for child in self.answer:
current_line = ""
for element in child:
current_line += str(element) + " "
print(current_line)
task = Task();
task.getData();
task.solve();
task.printAnswer();
``` | instruction | 0 | 49,959 | 14 | 99,918 |
No | output | 1 | 49,959 | 14 | 99,919 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:
"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."
More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:
* m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
* Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.
Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
Examples
Input
5 2
Output
4
Input
8 1
Output
5
Note
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:
* At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
* At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
* At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
* At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
* At the beginning of the third day two grains are brought. The barn becomes full again.
* At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
* At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
* At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.
So the answer is 4, because by the end of the fourth day the barn becomes empty. | instruction | 0 | 50,140 | 14 | 100,280 |
Tags: binary search, math
Correct Solution:
```
def s(m):
x = (1 + m) * m // 2
return x
import sys
n, q = map(int, input().split())
if q >= n:
print(n)
sys.exit(0)
l, r = q, n
while r - l > 1:
m = (l + r) // 2
if n - q - s(m - q) <= 0:
r = m
else:
l = m
print(r)
``` | output | 1 | 50,140 | 14 | 100,281 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:
"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."
More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:
* m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
* Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.
Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
Examples
Input
5 2
Output
4
Input
8 1
Output
5
Note
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:
* At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
* At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
* At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
* At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
* At the beginning of the third day two grains are brought. The barn becomes full again.
* At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
* At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
* At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.
So the answer is 4, because by the end of the fourth day the barn becomes empty. | instruction | 0 | 50,141 | 14 | 100,282 |
Tags: binary search, math
Correct Solution:
```
def get_empty_before_birds(m, i):
if i <= m + 1:
return 0
t = i - (m + 1)
return (t * (t + 1)) // 2
def get_total_empty_after_birds(m, i):
return get_empty_before_birds(m, i) + i
def get_birds(m, n):
low, high = 1, n
while True:
i = (low + high) // 2
if get_total_empty_after_birds(m, i) < n:
low = i + 1
elif get_total_empty_after_birds(m, i - 1) < n <= get_total_empty_after_birds(m, i + 1):
return i
else:
high = i - 1
def main():
n, m = [int(t) for t in input().split()]
print(get_birds(m, n))
if __name__ == '__main__':
main()
``` | output | 1 | 50,141 | 14 | 100,283 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:
"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."
More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:
* m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
* Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.
Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
Examples
Input
5 2
Output
4
Input
8 1
Output
5
Note
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:
* At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
* At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
* At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
* At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
* At the beginning of the third day two grains are brought. The barn becomes full again.
* At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
* At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
* At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.
So the answer is 4, because by the end of the fourth day the barn becomes empty. | instruction | 0 | 50,142 | 14 | 100,284 |
Tags: binary search, math
Correct Solution:
```
n,m=map(int,input().split())
if m>=n:
from sys import exit
print(n);exit()
i,j=m,n
result=m
item=m*(m+1)//2
while i+1<j:
mid=(i+j)//2
if mid*(mid+1)//2-item>=n+m*(mid-m-1):j=mid
else:i=mid
if i*(i+1)//2-item>=n+m*(i-m-1):print(i)
else:print(j)
``` | output | 1 | 50,142 | 14 | 100,285 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:
"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."
More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:
* m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
* Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.
Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
Examples
Input
5 2
Output
4
Input
8 1
Output
5
Note
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:
* At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
* At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
* At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
* At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
* At the beginning of the third day two grains are brought. The barn becomes full again.
* At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
* At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
* At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.
So the answer is 4, because by the end of the fourth day the barn becomes empty. | instruction | 0 | 50,143 | 14 | 100,286 |
Tags: binary search, math
Correct Solution:
```
n,m = list(map(int,input().split()))
if m>=n:
print(n)
else:
k = n-m
low,high = m,n
l = float("inf")
while(low<=high):
mid = (low+high)//2
p = k+(mid-m)*m
q = ((mid-m)*(2*(m+1)+(mid-m-1)))//2
if q>=p:
l = min(l,mid)
high = mid-1
else:
low = mid+1
print(l)
``` | output | 1 | 50,143 | 14 | 100,287 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:
"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."
More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:
* m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
* Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.
Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
Examples
Input
5 2
Output
4
Input
8 1
Output
5
Note
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:
* At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
* At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
* At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
* At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
* At the beginning of the third day two grains are brought. The barn becomes full again.
* At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
* At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
* At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.
So the answer is 4, because by the end of the fourth day the barn becomes empty. | instruction | 0 | 50,144 | 14 | 100,288 |
Tags: binary search, math
Correct Solution:
```
import sys
n, m = map(int, input().split())
if n <= m:
print(n)
sys.exit(0)
else:
l, r = m + 1, n
base = m * (m - 1) // 2
while l != r:
mid = (l + r) // 2
plus = n + base + (mid - m) * m
minus = mid * (mid + 1) // 2
if plus > minus:
l = mid + 1
else:
r = mid
print(l)
``` | output | 1 | 50,144 | 14 | 100,289 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:
"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."
More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:
* m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
* Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.
Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
Examples
Input
5 2
Output
4
Input
8 1
Output
5
Note
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:
* At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
* At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
* At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
* At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
* At the beginning of the third day two grains are brought. The barn becomes full again.
* At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
* At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
* At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.
So the answer is 4, because by the end of the fourth day the barn becomes empty. | instruction | 0 | 50,145 | 14 | 100,290 |
Tags: binary search, math
Correct Solution:
```
def f(n, m):
sz = n
n -= 1
step = 2
print('n = ', n)
while n > 0:
n += m
n = min(n, sz)
n -= step
step += 1
print('n = ', n)
return step - 1
n, m = map(int, input().split())
m = min(n, m)
cnt = max(n - m, 0)
l = 0
r = 10 ** 18
while l + 1 < r:
m1 = (l + r) // 2
x = (m1 * (m1 - 1)) // 2
if x < cnt:
l = m1
else:
r = m1
print(m - 1 + r)
``` | output | 1 | 50,145 | 14 | 100,291 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:
"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."
More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:
* m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
* Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.
Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
Examples
Input
5 2
Output
4
Input
8 1
Output
5
Note
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:
* At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
* At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
* At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
* At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
* At the beginning of the third day two grains are brought. The barn becomes full again.
* At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
* At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
* At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.
So the answer is 4, because by the end of the fourth day the barn becomes empty. | instruction | 0 | 50,146 | 14 | 100,292 |
Tags: binary search, math
Correct Solution:
```
import math
from decimal import Decimal
a,b = map(int,input().split())
if a<=b:
print(a)
else:
otvet = math.ceil((-1+(Decimal(1+8*(a-b))**Decimal(0.5)))/2)+b
print(otvet)
``` | output | 1 | 50,146 | 14 | 100,293 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:
"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."
More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:
* m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
* Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.
Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
Examples
Input
5 2
Output
4
Input
8 1
Output
5
Note
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:
* At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
* At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
* At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
* At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
* At the beginning of the third day two grains are brought. The barn becomes full again.
* At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
* At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
* At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.
So the answer is 4, because by the end of the fourth day the barn becomes empty. | instruction | 0 | 50,147 | 14 | 100,294 |
Tags: binary search, math
Correct Solution:
```
import sys
import os
def BS ( l, h, k):
if h < l :
return l
mid = (l + h) // 2
if (mid*(mid+1)) >= 2*k :
return BS ( l, mid-1, k )
else:
return BS ( mid+1, h, k )
if __name__ == "__main__":
# print ( sys.version )
# n = int(input())
# m = int(input())
n, m = map(int, input().split())
if m >= n :
print ( n )
else:
x = n - m;
idx = BS ( 0 , 1000000000000000000, x )
print(idx+m)
``` | output | 1 | 50,147 | 14 | 100,295 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys. | instruction | 0 | 50,172 | 14 | 100,344 |
Tags: binary search, brute force, dp, greedy, sortings
Correct Solution:
```
read = lambda: map(int, input().split())
n, k, p = read()
a, b = sorted(read()), sorted(read())
print(min(max(abs(b[i + d] - a[i]) + abs(b[i + d] - p) for i in range(n)) for d in range(k - n + 1)))
``` | output | 1 | 50,172 | 14 | 100,345 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys. | instruction | 0 | 50,173 | 14 | 100,346 |
Tags: binary search, brute force, dp, greedy, sortings
Correct Solution:
```
n,k,p = map(int,input().split())
a = []
b = []
temp = input().split()
for i in range(n):
a.append(int(temp[i]))
temp = input().split()
for i in range(k):
b.append(int(temp[i]))
list.sort(a)
list.sort(b)
mini = 0
for i in range(k-n+1):
maxi = 0
cur = 0
for j in range(n):
if p >= a[j]:
if a[j] > b[i+j]:
cur = p+a[j]-2*b[i+j]
elif b[i+j] > p:
cur = 2*b[i+j]-p-a[j]
else:
cur = p-a[j]
else:
if b[i+j] > a[j]:
cur = 2*b[i+j]-p-a[j]
elif b[i+j] < p:
cur = p+a[j]-2*b[i+j]
else:
cur = a[j]-p
if cur > maxi:
maxi = cur
if maxi < mini or i == 0:
mini = maxi
print(mini)
``` | output | 1 | 50,173 | 14 | 100,347 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys. | instruction | 0 | 50,174 | 14 | 100,348 |
Tags: binary search, brute force, dp, greedy, sortings
Correct Solution:
```
n,k,p = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.sort()
b.sort()
#print(a)
#print(b)
def cost(j):
if j < 0 or j + n - 1 >= k:
return 1000000000000
maximum = 0
for i in range(n):
t = abs(a[i] - b[i+j]) + abs(b[i+j] - p)
maximum = max(maximum,t)
return maximum
l = 0
r = k - n
#Binary Search on l,r
while l < r:
mid = (l+r)//2
cost1 = cost(mid)
cost1r = cost(mid+1)
#print("r = {}, l = {}, cost1 = {}, cost1r = {}".format(l,r,cost1,cost1r))
if cost1 < cost1r:
r = mid
else:
l = mid+1
print(cost(l))
``` | output | 1 | 50,174 | 14 | 100,349 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys. | instruction | 0 | 50,175 | 14 | 100,350 |
Tags: binary search, brute force, dp, greedy, sortings
Correct Solution:
```
n, k, p = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
A.sort()
B.sort()
ans = 10**18
for start in range(k):
curmax = 0
curans = 10**18
if start+n > k:
continue
for i in range(n):
curmax = max(curmax, abs(A[i] - B[start+i]) + abs(B[start+i] - p))
ans = min(ans, curmax)
print(ans)
``` | output | 1 | 50,175 | 14 | 100,351 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys. | instruction | 0 | 50,176 | 14 | 100,352 |
Tags: binary search, brute force, dp, greedy, sortings
Correct Solution:
```
n, k, p = map(int, input().split())
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
res = 1e15
for i in range(k - n + 1):
ma = 0
for j in range(n):
te = abs(a[j] - b[i+j]) + abs(b[i+j] - p)
ma = max(ma, te)
res = min(res, ma)
print(res)
``` | output | 1 | 50,176 | 14 | 100,353 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys. | instruction | 0 | 50,178 | 14 | 100,356 |
Tags: binary search, brute force, dp, greedy, sortings
Correct Solution:
```
N , M , P = map(int , input().split(" ") )
A = list(map(int , input().split(" ") ) )
B = list(map(int , input().split(" ") ) )
A = sorted(A)
B = sorted(B)
def check(HAHA):
vis = [False for _ in range(N)]
res = 0
for i in range(M):
for ii in range(N):
x = abs(A[ii] - B[i])
x += abs(P - B[i])
if vis[ii] == True or x > HAHA:
continue
res += 1
vis[ii] = True
break
return res == N
ret = -1
l = 0 ; r = int(3e9)
while l <= r :
md = (l + r) >> 1
if check(md):
ret = md
r = md - 1
else :
l = md + 1
print(ret)
``` | output | 1 | 50,178 | 14 | 100,357 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys. | instruction | 0 | 50,179 | 14 | 100,358 |
Tags: binary search, brute force, dp, greedy, sortings
Correct Solution:
```
import sys
from collections import defaultdict
def calc(a,b,p):
return abs(a-b) + abs(p-b)
def main():
n, k, p= map(int,sys.stdin.readline().split())
a = list(map(int,sys.stdin.readline().split()))
b = list(map(int,sys.stdin.readline().split()))
a = sorted(a)
b = sorted(b)
v = 10**10
for i in range(k-n+1):
t = 0
for j in range(n):
t = max(t, calc(a[j],b[j+i],p))
v = min(v,t)
print(v)
main()
``` | output | 1 | 50,179 | 14 | 100,359 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Disclaimer: there are lots of untranslateable puns in the Russian version of the statement, so there is one more reason for you to learn Russian :)
Rick and Morty like to go to the ridge High Cry for crying loudly — there is an extraordinary echo. Recently they discovered an interesting acoustic characteristic of this ridge: if Rick and Morty begin crying simultaneously from different mountains, their cry would be heard between these mountains up to the height equal the bitwise OR of mountains they've climbed and all the mountains between them.
Bitwise OR is a binary operation which is determined the following way. Consider representation of numbers x and y in binary numeric system (probably with leading zeroes) x = xk... x1x0 and y = yk... y1y0. Then z = x | y is defined following way: z = zk... z1z0, where zi = 1, if xi = 1 or yi = 1, and zi = 0 otherwise. In the other words, digit of bitwise OR of two numbers equals zero if and only if digits at corresponding positions is both numbers equals zero. For example bitwise OR of numbers 10 = 10102 and 9 = 10012 equals 11 = 10112. In programming languages C/C++/Java/Python this operation is defined as «|», and in Pascal as «or».
Help Rick and Morty calculate the number of ways they can select two mountains in such a way that if they start crying from these mountains their cry will be heard above these mountains and all mountains between them. More formally you should find number of pairs l and r (1 ≤ l < r ≤ n) such that bitwise OR of heights of all mountains between l and r (inclusive) is larger than the height of any mountain at this interval.
Input
The first line contains integer n (1 ≤ n ≤ 200 000), the number of mountains in the ridge.
Second line contains n integers ai (0 ≤ ai ≤ 109), the heights of mountains in order they are located in the ridge.
Output
Print the only integer, the number of ways to choose two different mountains.
Examples
Input
5
3 2 1 6 5
Output
8
Input
4
3 3 3 3
Output
0
Note
In the first test case all the ways are pairs of mountains with the numbers (numbering from one):
(1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)
In the second test case there are no such pairs because for any pair of mountains the height of cry from them is 3, and this height is equal to the height of any mountain. | instruction | 0 | 50,193 | 14 | 100,386 |
Tags: binary search, bitmasks, combinatorics, data structures, divide and conquer
Correct Solution:
```
n = int(input())
a = [int(i) for i in input().split()]
l = [i for i in range(len(a))]
r = [i for i in range(len(a))]
for i in range(len(a)):
while l[i]>=1 and a[i]|a[l[i]-1]<=a[i]:
l[i] = l[l[i]-1]
for j in range(len(a)):
i = len(a)-j-1
while r[i]<len(a)-1 and a[i]|a[r[i]+1]<=a[i] and a[i]>a[r[i]+1]:
r[i] = r[r[i]+1]
count=0
for i in range(len(a)):
x = r[i]-i+1
y = i-l[i]+1
count+=x*y-1
print((n*(n-1))//2-count)
``` | output | 1 | 50,193 | 14 | 100,387 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Disclaimer: there are lots of untranslateable puns in the Russian version of the statement, so there is one more reason for you to learn Russian :)
Rick and Morty like to go to the ridge High Cry for crying loudly — there is an extraordinary echo. Recently they discovered an interesting acoustic characteristic of this ridge: if Rick and Morty begin crying simultaneously from different mountains, their cry would be heard between these mountains up to the height equal the bitwise OR of mountains they've climbed and all the mountains between them.
Bitwise OR is a binary operation which is determined the following way. Consider representation of numbers x and y in binary numeric system (probably with leading zeroes) x = xk... x1x0 and y = yk... y1y0. Then z = x | y is defined following way: z = zk... z1z0, where zi = 1, if xi = 1 or yi = 1, and zi = 0 otherwise. In the other words, digit of bitwise OR of two numbers equals zero if and only if digits at corresponding positions is both numbers equals zero. For example bitwise OR of numbers 10 = 10102 and 9 = 10012 equals 11 = 10112. In programming languages C/C++/Java/Python this operation is defined as «|», and in Pascal as «or».
Help Rick and Morty calculate the number of ways they can select two mountains in such a way that if they start crying from these mountains their cry will be heard above these mountains and all mountains between them. More formally you should find number of pairs l and r (1 ≤ l < r ≤ n) such that bitwise OR of heights of all mountains between l and r (inclusive) is larger than the height of any mountain at this interval.
Input
The first line contains integer n (1 ≤ n ≤ 200 000), the number of mountains in the ridge.
Second line contains n integers ai (0 ≤ ai ≤ 109), the heights of mountains in order they are located in the ridge.
Output
Print the only integer, the number of ways to choose two different mountains.
Examples
Input
5
3 2 1 6 5
Output
8
Input
4
3 3 3 3
Output
0
Note
In the first test case all the ways are pairs of mountains with the numbers (numbering from one):
(1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)
In the second test case there are no such pairs because for any pair of mountains the height of cry from them is 3, and this height is equal to the height of any mountain. | instruction | 0 | 50,194 | 14 | 100,388 |
Tags: binary search, bitmasks, combinatorics, data structures, divide and conquer
Correct Solution:
```
import os,io,sys
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n=int(input())
a=list(map(int,input().split()))
left=[]
right=[]
for i in range(31):
left.append([-1]*n)
right.append([n]*n)
for i in range(n):
for j in range(31):
if 1<<j&a[i]:
left[j][i]=i
else:
if i>0:
left[j][i]=left[j][i-1]
for i in range(n-1,-1,-1):
for j in range(31):
if 1<<j&a[i]:
right[j][i]=i
else:
if i<n-1:
right[j][i]=right[j][i+1]
ans=(n*(n-1))//2
ans+=n
lastsame=[-1]*n
dic={}
for i in range(n):
if a[i] in dic:
lastsame[i]=dic[a[i]]
dic[a[i]]=i
else:
dic[a[i]]=i
for i in range(n):
maxleft=-1
minright=n
for j in range(31):
if not(1<<j&a[i]):
maxleft=max(maxleft,left[j][i])
minright=min(minright,right[j][i])
if lastsame[i]>maxleft:
maxleft=lastsame[i]
ans-=(i-maxleft)*(minright-i)
print(ans)
``` | output | 1 | 50,194 | 14 | 100,389 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
Input
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
Output
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Examples
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4 | instruction | 0 | 50,824 | 14 | 101,648 |
Tags: dp, greedy
Correct Solution:
```
class Solution():
def max_radiance(n, a, b, c):
dp = [[0 for i in range(2)] for j in range(n+1)]
dp[n-1][0] = a[n-1]
dp[n-1][1] = b[n-1]
# dp[i][0] - Max radiance for range [i:] when none on the left is yet fed
# dp[i][1] - Max radiance for range [i:] when one of the left is fed
for i in range(n-2, -1, -1):
dp[i][0] = max(a[i]+dp[i+1][1], b[i]+dp[i+1][0])
dp[i][1] = max(b[i]+dp[i+1][1], c[i]+dp[i+1][0])
return dp[0][0]
n = int(input())
a = list(map(int, input().strip(' ').split(' ')))
b = list(map(int, input().strip(' ').split(' ')))
c = list(map(int, input().strip(' ').split(' ')))
print(Solution.max_radiance(n, a, b, c))
``` | output | 1 | 50,824 | 14 | 101,649 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
Input
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
Output
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Examples
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4 | instruction | 0 | 50,825 | 14 | 101,650 |
Tags: dp, greedy
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
fed_left = {0 : a[0]}
not_fed_left = {0 : b[0]}
for i in range(1, n):
fed_left[i] = max(fed_left[i-1] + b[i], not_fed_left[i-1] + a[i]) # max(fed left, fed right)
not_fed_left[i] = max(fed_left[i-1] + c[i], not_fed_left[i-1] + b[i]) # max(fed left and right, fed right)
print(fed_left[n-1])
``` | output | 1 | 50,825 | 14 | 101,651 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
Input
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
Output
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Examples
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4 | instruction | 0 | 50,826 | 14 | 101,652 |
Tags: dp, greedy
Correct Solution:
```
def main():
n = int(input())
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
c = list(map(int, input().split(' ')))
d0 = [0]*n
d0[n-1] = a[n-1]
d1 = [0]*n
d1[n-1] = b[n-1]
for i in range(n-2, -1, -1):
d0[i] = max(a[i]+d1[i+1], b[i]+d0[i+1])
d1[i] = max(b[i]+d1[i+1], c[i]+d0[i+1])
print(d0[0])
if __name__ == "__main__":
main()
``` | output | 1 | 50,826 | 14 | 101,653 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
Input
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
Output
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Examples
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4 | instruction | 0 | 50,827 | 14 | 101,654 |
Tags: dp, greedy
Correct Solution:
```
from sys import setrecursionlimit
setrecursionlimit(10 ** 6)
def dynamic(i, fed):
if d[i][fed] is not None:
return d[i][fed]
if fed:
d[i][fed] = max(b[i] + dynamic(i + 1, True), c[i] + dynamic(i + 1, False))
else:
d[i][fed] = max(a[i] + dynamic(i + 1, True), b[i] + dynamic(i + 1, False))
return d[i][fed]
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
d = [[None, None] for i in range(n)]
d[n - 1] = [a[n - 1], b[n - 1]]
print(dynamic(0, False))
``` | output | 1 | 50,827 | 14 | 101,655 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
Input
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
Output
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Examples
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4 | instruction | 0 | 50,828 | 14 | 101,656 |
Tags: dp, greedy
Correct Solution:
```
import sys
sys.setrecursionlimit(5000)
dp = [[-1 for i in range(5)] for j in range(3005)]
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
def fun(i ,n ,idx):
if dp[i][idx]!=-1:return dp[i][idx]
if idx==0:
if i==n-1: return a[n-1]
dp[i][idx] = max(fun(i+1,n,1) +a[i],fun(i+1,n,0)+b[i])
else:
if i==n-1: return b[n-1]
dp[i][idx] = max(fun(i+1,n,1)+b[i],fun(i+1,n,0)+c[i])
return dp[i][idx]
print(fun(0,n,0))
``` | output | 1 | 50,828 | 14 | 101,657 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
Input
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
Output
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Examples
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4 | instruction | 0 | 50,829 | 14 | 101,658 |
Tags: dp, greedy
Correct Solution:
```
n=int(input())
a=list(map(int,input().strip().split()))
b=list(map(int,input().strip().split()))
c=list(map(int,input().strip().split()))
d1,d2=a[0],b[0]
for i in range(1,n):
d1,d2=max(d1+b[i],d2+a[i]),max(d1+c[i],d2+b[i])
print (d1)
``` | output | 1 | 50,829 | 14 | 101,659 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
Input
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
Output
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Examples
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4 | instruction | 0 | 50,830 | 14 | 101,660 |
Tags: dp, greedy
Correct Solution:
```
n = int( input() )
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
DD = a[0]
DP = b[0]
PD = a[0]
PP = b[0]
for i in range(1,n):
tDD = max(DP+a[i], PP+a[i])
tPD = max(PD+b[i], DD+b[i])
tDP = max(DP+b[i], PP+b[i])
tPP = max(PD+c[i], DD+c[i])
DD = tDD
PD = tPD
DP = tDP
PP = tPP
if n==1:
sol = DD
else:
sol = max(DD, PD)
print( sol )
``` | output | 1 | 50,830 | 14 | 101,661 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
Input
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
Output
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Examples
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4 | instruction | 0 | 50,831 | 14 | 101,662 |
Tags: dp, greedy
Correct Solution:
```
n = int(input())
a, b, c = (list(map(int, input().split())) for i in range(3))
u, v = a[0], b[0]
for i in range(1, n): u, v = max(v + a[i], u + b[i]), max(v + b[i], u + c[i])
print(u)
``` | output | 1 | 50,831 | 14 | 101,663 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
Input
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
Output
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Examples
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
Submitted Solution:
```
n = int(input())
t = [list(map(int, input().split())) for i in range(3)]
t = list(zip(*t))
F = {}
def g(i, j, k):
if j - i == 1: return t[i][1]
a, b, c = t[k]
t[k] = (b, c, 0)
s = f(i, j)
t[k] = (a, b, c)
return s
def f(i, j):
if (i, j, t[i], t[j - 1]) in F: return F[(i, j, t[i], t[j - 1])]
if j - i == 1: return t[i][0]
k = (i + j) // 2
F[(i, j, t[i], t[j - 1])] = max(f(i, k) + g(k, j, k), g(i, k, k - 1) + f(k, j))
return F[(i, j, t[i], t[j - 1])]
print(f(0, n))
``` | instruction | 0 | 50,832 | 14 | 101,664 |
Yes | output | 1 | 50,832 | 14 | 101,665 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
Input
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
Output
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Examples
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
Submitted Solution:
```
n = int(input())
a, b, c = (list(map(int, input().split())) for i in range(3))
d1, d2 = a[0], b[0]
for i in range(1, n):
d1, d2 = max(d2 + a[i], d1 + b[i]), max(d2 + b[i], d1 + c[i])
print(d1)
``` | instruction | 0 | 50,833 | 14 | 101,666 |
Yes | output | 1 | 50,833 | 14 | 101,667 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
Input
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
Output
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Examples
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
Submitted Solution:
```
from collections import defaultdict
from itertools import accumulate
import sys
input = sys.stdin.readline
'''
for CASES in range(int(input())):
n, m = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
S = input().strip()
sys.stdout.write(" ".join(map(str,ANS))+"\n")
'''
inf = 100000000000000000 # 1e17
mod = 998244353
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
F=[[-inf for i in range(2)] for j in range(n)]
F[0][0]=A[0]
F[0][1]=B[0]
for i in range(1,n):
F[i][0]=max(F[i-1][1]+A[i],F[i-1][0]+B[i])
F[i][1]=max(F[i-1][1]+B[i],F[i-1][0]+C[i])
print(F[n-1][0])
``` | instruction | 0 | 50,834 | 14 | 101,668 |
Yes | output | 1 | 50,834 | 14 | 101,669 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
Input
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
Output
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Examples
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
Submitted Solution:
```
'''
Created on Nov 11, 2013
@author: Ismael
'''
import sys
def solve():
bbCum = lBB[0]
baCum = lBA[0]
abCum = lAB[0]
aaCum = lAA[0]
for i in range(1,len(lBB)):
m1 = max(baCum,aaCum)
m2 = max(bbCum,abCum)
bbCum = lBB[i] + m1
baCum = lBA[i] + m1
abCum = lAB[i] + m2
aaCum = lAA[i] + m2
res = max(bbCum,baCum,abCum,aaCum)
return res
def main():
global lBB
global lBA
global lAB
global lAA
f = sys.stdin
#f = open("input3.txt")
n = int(f.readline())
lBB = list(map(int,f.readline().split()))
lBA = list(map(int,f.readline().split()))
lAB = [elem for elem in lBA]
lAA = list(map(int,f.readline().split()))
lAB[0] = 0
lBA[-1] = 0
lAA[0] = 0
lAA[-1] = 0
#resExpected = int(f.readline())
res = solve()
#print(res==resExpected)
print(res)
main()
``` | instruction | 0 | 50,835 | 14 | 101,670 |
Yes | output | 1 | 50,835 | 14 | 101,671 |
Evaluate the correctness of the submitted Python 2 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
Input
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
Output
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Examples
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
Submitted Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
from math import sqrt
from bisect import bisect
raw_input = stdin.readline
pr = stdout.write
mod=10**9+7
def ni():
return int(raw_input())
def li():
return map(int,raw_input().split())
def pn(n):
stdout.write(str(n)+'\n')
def pa(arr):
pr('\n'.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return list(map(int,stdin.read().split()))
#range = xrange # not for python 3.0+
n=ni()
a=li()
b=li()
c=li()
x,y=a[0],b[0]
for i in range(1,n):
x,y=max(y+a[i],x+b[i]),max(y+b[i],x+c[i])
pn(x)
``` | instruction | 0 | 50,836 | 14 | 101,672 |
Yes | output | 1 | 50,836 | 14 | 101,673 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
Input
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
Output
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Examples
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
Submitted Solution:
```
n = int(input())
t = [list(map(int, input().split())) for i in range(3)]
t = list(zip(*t))
G = {}
def g(i, j, k):
if (i, j, k) in G: return G[(i, j, k)]
if j - i == 1: return t[i][1]
a, b, c = t[k]
t[k] = (b, c, 0)
G[(i, j, k)] = f(i, j)
t[k] = (a, b, c)
return G[(i, j, k)]
def f(i, j):
if j - i == 1: return t[i][0]
k = (i + j) // 2
return max(f(i, k) + g(k, j, k), g(i, k, k - 1) + f(k, j))
print(f(0, n))
``` | instruction | 0 | 50,837 | 14 | 101,674 |
No | output | 1 | 50,837 | 14 | 101,675 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
Input
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
Output
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Examples
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
Submitted Solution:
```
n, u, v = int(input()), 0, 0
a, b, c = (list(map(int, input().split())) for i in range(3))
for i in range(n): u, v = max(v + a[i], u + b[i]), max(v + b[i], u + c[i])
print(u)
``` | instruction | 0 | 50,838 | 14 | 101,676 |
No | output | 1 | 50,838 | 14 | 101,677 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
Input
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
Output
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Examples
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
Submitted Solution:
```
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
# from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def L(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
try:
sys.setrecursionlimit(int(pow(10,6)))
sys.stdin = open("input.txt", "r")
# sys.stdout = open("../output.txt", "w")
except:
pass
n=L()[0]
A=L()
B=L()
C=L()
@lru_cache(None)
def rec(idx,prev):
if idx==n:
return 0
if prev==1:
return max(rec(idx+1,1)+B[idx],rec(idx+1,0)+C[idx])
else:
return max(rec(idx+1,1)+A[idx],rec(idx+1,0)+B[idx])
ans=max(rec(1,1)+A[0],rec(1,0)+B[0])
print(ans)
endtime = time.time()
# print(f"Runtime of the program is {endtime - starttime}")
``` | instruction | 0 | 50,839 | 14 | 101,678 |
No | output | 1 | 50,839 | 14 | 101,679 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
Input
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
Output
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Examples
Input
4
1 2 3 4
4 3 2 1
0 1 1 0
Output
13
Input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
Output
44
Input
3
1 1 1
1 2 1
1 1 1
Output
4
Submitted Solution:
```
'''
Created on Nov 11, 2013
@author: Ismael
'''
import sys
def solve():
bbCum = lBB[0]
baCum = lBA[0]
abCum = lAB[0]
aaCum = lAA[0]
for i in range(1,len(lBB)):
m1 = max(baCum,aaCum)
m2 = max(bbCum,abCum)
bbCum = lBB[i] + m1
baCum = lBA[i] + m1
abCum = lAB[i] + m2
aaCum = lAA[i] + m2
res = max(bbCum,baCum,abCum,aaCum)
return res
def main():
global lBB
global lBA
global lAB
global lAA
f = sys.stdin
#f = open("input3.txt")
n = int(f.readline())
lBB = list(map(int,f.readline().split()))
lBA = list(map(int,f.readline().split()))
lAB = lBA
lAA = list(map(int,f.readline().split()))
#resExpected = int(f.readline())
res = solve()
print(res)
main()
``` | instruction | 0 | 50,840 | 14 | 101,680 |
No | output | 1 | 50,840 | 14 | 101,681 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Looksery company, consisting of n staff members, is planning another big party. Every employee has his phone number and the phone numbers of his friends in the phone book. Everyone who comes to the party, sends messages to his contacts about how cool it is. At the same time everyone is trying to spend as much time on the fun as possible, so they send messages to everyone without special thinking, moreover, each person even sends a message to himself or herself.
Igor and Max, Looksery developers, started a dispute on how many messages each person gets. Igor indicates n numbers, the i-th of which indicates how many messages, in his view, the i-th employee is going to take. If Igor guesses correctly at least one of these numbers, he wins, otherwise Max wins.
You support Max in this debate, so you need, given the contact lists of the employees, to determine whether there is a situation where Igor loses. Specifically, you need to determine which employees should come to the party, and which should not, so after all the visitors send messages to their contacts, each employee received a number of messages that is different from what Igor stated.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of employees of company Looksery.
Next n lines contain the description of the contact lists of the employees. The i-th of these lines contains a string of length n, consisting of digits zero and one, specifying the contact list of the i-th employee. If the j-th character of the i-th string equals 1, then the j-th employee is in the i-th employee's contact list, otherwise he isn't. It is guaranteed that the i-th character of the i-th line is always equal to 1.
The last line contains n space-separated integers: a1, a2, ..., an (0 ≤ ai ≤ n), where ai represents the number of messages that the i-th employee should get according to Igor.
Output
In the first line print a single integer m — the number of employees who should come to the party so that Igor loses the dispute.
In the second line print m space-separated integers — the numbers of these employees in an arbitrary order.
If Igor wins the dispute in any case, print -1.
If there are multiple possible solutions, print any of them.
Examples
Input
3
101
010
001
0 1 2
Output
1
1
Input
1
1
1
Output
0
Input
4
1111
0101
1110
0001
1 0 1 0
Output
4
1 2 3 4
Note
In the first sample Igor supposes that the first employee will receive 0 messages. Since he isn't contained in any other contact list he must come to the party in order to receive one message from himself. If he is the only who come to the party then he will receive 1 message, the second employee will receive 0 messages and the third will also receive 1 message. Thereby Igor won't guess any number.
In the second sample if the single employee comes to the party he receives 1 message and Igor wins, so he shouldn't do it.
In the third sample the first employee will receive 2 messages, the second — 3, the third — 2, the fourth — 3. | instruction | 0 | 50,888 | 14 | 101,776 |
Tags: constructive algorithms, dfs and similar, graphs, greedy
Correct Solution:
```
def main():
import sys
tokens = sys.stdin.read().split()
tokens.reverse()
n = int(tokens.pop())
mat = [[int(j) for j in tokens.pop()] for i in range(n)]
prediction = [int(tokens.pop()) for i in range(n)]
frequency = [0] * n
result = []
for step in range(n):
for i in range(n):
if frequency[i] == prediction[i]:
result.append(i + 1)
for j in range(n):
frequency[j] += mat[i][j]
break
print(len(result))
print(' '.join(str(i) for i in result))
main()
``` | output | 1 | 50,888 | 14 | 101,777 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Looksery company, consisting of n staff members, is planning another big party. Every employee has his phone number and the phone numbers of his friends in the phone book. Everyone who comes to the party, sends messages to his contacts about how cool it is. At the same time everyone is trying to spend as much time on the fun as possible, so they send messages to everyone without special thinking, moreover, each person even sends a message to himself or herself.
Igor and Max, Looksery developers, started a dispute on how many messages each person gets. Igor indicates n numbers, the i-th of which indicates how many messages, in his view, the i-th employee is going to take. If Igor guesses correctly at least one of these numbers, he wins, otherwise Max wins.
You support Max in this debate, so you need, given the contact lists of the employees, to determine whether there is a situation where Igor loses. Specifically, you need to determine which employees should come to the party, and which should not, so after all the visitors send messages to their contacts, each employee received a number of messages that is different from what Igor stated.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of employees of company Looksery.
Next n lines contain the description of the contact lists of the employees. The i-th of these lines contains a string of length n, consisting of digits zero and one, specifying the contact list of the i-th employee. If the j-th character of the i-th string equals 1, then the j-th employee is in the i-th employee's contact list, otherwise he isn't. It is guaranteed that the i-th character of the i-th line is always equal to 1.
The last line contains n space-separated integers: a1, a2, ..., an (0 ≤ ai ≤ n), where ai represents the number of messages that the i-th employee should get according to Igor.
Output
In the first line print a single integer m — the number of employees who should come to the party so that Igor loses the dispute.
In the second line print m space-separated integers — the numbers of these employees in an arbitrary order.
If Igor wins the dispute in any case, print -1.
If there are multiple possible solutions, print any of them.
Examples
Input
3
101
010
001
0 1 2
Output
1
1
Input
1
1
1
Output
0
Input
4
1111
0101
1110
0001
1 0 1 0
Output
4
1 2 3 4
Note
In the first sample Igor supposes that the first employee will receive 0 messages. Since he isn't contained in any other contact list he must come to the party in order to receive one message from himself. If he is the only who come to the party then he will receive 1 message, the second employee will receive 0 messages and the third will also receive 1 message. Thereby Igor won't guess any number.
In the second sample if the single employee comes to the party he receives 1 message and Igor wins, so he shouldn't do it.
In the third sample the first employee will receive 2 messages, the second — 3, the third — 2, the fourth — 3. | instruction | 0 | 50,889 | 14 | 101,778 |
Tags: constructive algorithms, dfs and similar, graphs, greedy
Correct Solution:
```
n=int(input().strip())
nums=['']+[' '+input().strip() for _ in range(n)]
a=[0]+list(map(int,input().split()))
def send(x):
for i in range(1,n+1):
if nums[x][i]=='1':
a[i]-=1
vis=[0]*(n+1)
while True:
for i in range(1,n+1):
if not vis[i] and not a[i]:
vis[i]=1
send(i)
break
else:
for i in range(1,n+1):
if not a[i]:
print(-1)
exit()
break
ans=[]
for i in range(1,n+1):
if vis[i]:
ans.append(i)
if ans:
print(len(ans))
print(*ans)
else:
print(0)
``` | output | 1 | 50,889 | 14 | 101,779 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Looksery company, consisting of n staff members, is planning another big party. Every employee has his phone number and the phone numbers of his friends in the phone book. Everyone who comes to the party, sends messages to his contacts about how cool it is. At the same time everyone is trying to spend as much time on the fun as possible, so they send messages to everyone without special thinking, moreover, each person even sends a message to himself or herself.
Igor and Max, Looksery developers, started a dispute on how many messages each person gets. Igor indicates n numbers, the i-th of which indicates how many messages, in his view, the i-th employee is going to take. If Igor guesses correctly at least one of these numbers, he wins, otherwise Max wins.
You support Max in this debate, so you need, given the contact lists of the employees, to determine whether there is a situation where Igor loses. Specifically, you need to determine which employees should come to the party, and which should not, so after all the visitors send messages to their contacts, each employee received a number of messages that is different from what Igor stated.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of employees of company Looksery.
Next n lines contain the description of the contact lists of the employees. The i-th of these lines contains a string of length n, consisting of digits zero and one, specifying the contact list of the i-th employee. If the j-th character of the i-th string equals 1, then the j-th employee is in the i-th employee's contact list, otherwise he isn't. It is guaranteed that the i-th character of the i-th line is always equal to 1.
The last line contains n space-separated integers: a1, a2, ..., an (0 ≤ ai ≤ n), where ai represents the number of messages that the i-th employee should get according to Igor.
Output
In the first line print a single integer m — the number of employees who should come to the party so that Igor loses the dispute.
In the second line print m space-separated integers — the numbers of these employees in an arbitrary order.
If Igor wins the dispute in any case, print -1.
If there are multiple possible solutions, print any of them.
Examples
Input
3
101
010
001
0 1 2
Output
1
1
Input
1
1
1
Output
0
Input
4
1111
0101
1110
0001
1 0 1 0
Output
4
1 2 3 4
Note
In the first sample Igor supposes that the first employee will receive 0 messages. Since he isn't contained in any other contact list he must come to the party in order to receive one message from himself. If he is the only who come to the party then he will receive 1 message, the second employee will receive 0 messages and the third will also receive 1 message. Thereby Igor won't guess any number.
In the second sample if the single employee comes to the party he receives 1 message and Igor wins, so he shouldn't do it.
In the third sample the first employee will receive 2 messages, the second — 3, the third — 2, the fourth — 3. | instruction | 0 | 50,890 | 14 | 101,780 |
Tags: constructive algorithms, dfs and similar, graphs, greedy
Correct Solution:
```
"""
Codeforces Looksery Cup 2015 Problem B
Author : chaotic_iak
Language: Python 3.4.2
"""
################################################### SOLUTION
def check(a, b):
for i in range(len(a)):
if a[i] == b[i]: return i
return -1
def main():
n, = read()
s = [read(0) for _ in range(n)]
a = read()
curr = [0] * n
invited = []
while True:
t = check(curr, a)
if t == -1: break
invited.append(t+1)
for i in range(n):
curr[i] += int(s[t][i])
print(len(invited))
return invited
#################################################### HELPERS
def read(mode=2):
# 0: String
# 1: List of strings
# 2: List of integers
inputs = input().strip()
if mode == 0: return inputs
if mode == 1: return inputs.split()
if mode == 2: return list(map(int, inputs.split()))
def write(s="\n"):
if s is None: s = ""
if isinstance(s, list): s = " ".join(map(str, s))
s = str(s)
print(s, end="")
write(main())
``` | output | 1 | 50,890 | 14 | 101,781 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Looksery company, consisting of n staff members, is planning another big party. Every employee has his phone number and the phone numbers of his friends in the phone book. Everyone who comes to the party, sends messages to his contacts about how cool it is. At the same time everyone is trying to spend as much time on the fun as possible, so they send messages to everyone without special thinking, moreover, each person even sends a message to himself or herself.
Igor and Max, Looksery developers, started a dispute on how many messages each person gets. Igor indicates n numbers, the i-th of which indicates how many messages, in his view, the i-th employee is going to take. If Igor guesses correctly at least one of these numbers, he wins, otherwise Max wins.
You support Max in this debate, so you need, given the contact lists of the employees, to determine whether there is a situation where Igor loses. Specifically, you need to determine which employees should come to the party, and which should not, so after all the visitors send messages to their contacts, each employee received a number of messages that is different from what Igor stated.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of employees of company Looksery.
Next n lines contain the description of the contact lists of the employees. The i-th of these lines contains a string of length n, consisting of digits zero and one, specifying the contact list of the i-th employee. If the j-th character of the i-th string equals 1, then the j-th employee is in the i-th employee's contact list, otherwise he isn't. It is guaranteed that the i-th character of the i-th line is always equal to 1.
The last line contains n space-separated integers: a1, a2, ..., an (0 ≤ ai ≤ n), where ai represents the number of messages that the i-th employee should get according to Igor.
Output
In the first line print a single integer m — the number of employees who should come to the party so that Igor loses the dispute.
In the second line print m space-separated integers — the numbers of these employees in an arbitrary order.
If Igor wins the dispute in any case, print -1.
If there are multiple possible solutions, print any of them.
Examples
Input
3
101
010
001
0 1 2
Output
1
1
Input
1
1
1
Output
0
Input
4
1111
0101
1110
0001
1 0 1 0
Output
4
1 2 3 4
Note
In the first sample Igor supposes that the first employee will receive 0 messages. Since he isn't contained in any other contact list he must come to the party in order to receive one message from himself. If he is the only who come to the party then he will receive 1 message, the second employee will receive 0 messages and the third will also receive 1 message. Thereby Igor won't guess any number.
In the second sample if the single employee comes to the party he receives 1 message and Igor wins, so he shouldn't do it.
In the third sample the first employee will receive 2 messages, the second — 3, the third — 2, the fourth — 3. | instruction | 0 | 50,891 | 14 | 101,782 |
Tags: constructive algorithms, dfs and similar, graphs, greedy
Correct Solution:
```
n = int(input())
contacts = []
for phonebook in range(n):
contacts.append(input())
guessed = [int(z) for z in input().split()]
#print(contacts, guessed)
ppl = []
if guessed.count(0) == 0:
print(0)
exit()
flag = False
while flag == False:
if 0 not in guessed:
#print(guessed)
flag = True
break
i = guessed.index(0) + 1
ppl.append(i)
for e in range(n):
if contacts[i-1][e] == '1':
guessed[e] -= 1
print(len(ppl))
print(' '.join([str(i) for i in ppl]))
``` | output | 1 | 50,891 | 14 | 101,783 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Looksery company, consisting of n staff members, is planning another big party. Every employee has his phone number and the phone numbers of his friends in the phone book. Everyone who comes to the party, sends messages to his contacts about how cool it is. At the same time everyone is trying to spend as much time on the fun as possible, so they send messages to everyone without special thinking, moreover, each person even sends a message to himself or herself.
Igor and Max, Looksery developers, started a dispute on how many messages each person gets. Igor indicates n numbers, the i-th of which indicates how many messages, in his view, the i-th employee is going to take. If Igor guesses correctly at least one of these numbers, he wins, otherwise Max wins.
You support Max in this debate, so you need, given the contact lists of the employees, to determine whether there is a situation where Igor loses. Specifically, you need to determine which employees should come to the party, and which should not, so after all the visitors send messages to their contacts, each employee received a number of messages that is different from what Igor stated.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of employees of company Looksery.
Next n lines contain the description of the contact lists of the employees. The i-th of these lines contains a string of length n, consisting of digits zero and one, specifying the contact list of the i-th employee. If the j-th character of the i-th string equals 1, then the j-th employee is in the i-th employee's contact list, otherwise he isn't. It is guaranteed that the i-th character of the i-th line is always equal to 1.
The last line contains n space-separated integers: a1, a2, ..., an (0 ≤ ai ≤ n), where ai represents the number of messages that the i-th employee should get according to Igor.
Output
In the first line print a single integer m — the number of employees who should come to the party so that Igor loses the dispute.
In the second line print m space-separated integers — the numbers of these employees in an arbitrary order.
If Igor wins the dispute in any case, print -1.
If there are multiple possible solutions, print any of them.
Examples
Input
3
101
010
001
0 1 2
Output
1
1
Input
1
1
1
Output
0
Input
4
1111
0101
1110
0001
1 0 1 0
Output
4
1 2 3 4
Note
In the first sample Igor supposes that the first employee will receive 0 messages. Since he isn't contained in any other contact list he must come to the party in order to receive one message from himself. If he is the only who come to the party then he will receive 1 message, the second employee will receive 0 messages and the third will also receive 1 message. Thereby Igor won't guess any number.
In the second sample if the single employee comes to the party he receives 1 message and Igor wins, so he shouldn't do it.
In the third sample the first employee will receive 2 messages, the second — 3, the third — 2, the fourth — 3. | instruction | 0 | 50,892 | 14 | 101,784 |
Tags: constructive algorithms, dfs and similar, graphs, greedy
Correct Solution:
```
import math
import sys
input = sys.stdin.readline
n = int(input())
a = [[] for _ in range(n)]
for i in range(n):
a[i] = [int(_) for _ in input().strip()]
cnt = [int(_) for _ in input().split()]
party = []
while True:
piv = -1
for i in range(n):
if cnt[i] == 0:
piv = i
if piv == -1: break
party.append(piv + 1)
for i in range(n):
if a[piv][i] == 1:
cnt[i] -= 1
print(len(party))
print(' '.join(map(str, party)))
``` | output | 1 | 50,892 | 14 | 101,785 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.