message stringlengths 2 48.6k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 318 108k | cluster float64 8 8 | __index_level_0__ int64 636 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a gift — a large snow maker. He plans to make some amount of snow every day. On day i he will make a pile of snow of volume Vi and put it in her garden.
Each day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is Ti, each pile will reduce its volume by Ti. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other.
Note that the pile made on day i already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day.
You are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day.
Input
The first line contains a single integer N (1 ≤ N ≤ 105) — the number of days.
The second line contains N integers V1, V2, ..., VN (0 ≤ Vi ≤ 109), where Vi is the initial size of a snow pile made on the day i.
The third line contains N integers T1, T2, ..., TN (0 ≤ Ti ≤ 109), where Ti is the temperature on the day i.
Output
Output a single line with N integers, where the i-th integer represents the total volume of snow melted on day i.
Examples
Input
3
10 10 5
5 7 2
Output
5 12 4
Input
5
30 25 20 15 10
9 10 12 4 13
Output
9 20 35 11 25
Note
In the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day.
Submitted Solution:
```
n = int(input())
v = [int(i) for i in input().split()]
t = [int(j) for j in input().split()]
melted_snow = []
for i in range(n):
melted_snow.append(0)
for i in range(n):
for j in range(i+1):
if v[j] <= t[i]:
melted_snow[i] += v[j]
v[j] = 0
else:
melted_snow[i] += (t[i])
v[j] = v[j] - t[i]
print(melted_snow)
``` | instruction | 0 | 30,661 | 8 | 61,322 |
No | output | 1 | 30,661 | 8 | 61,323 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a gift — a large snow maker. He plans to make some amount of snow every day. On day i he will make a pile of snow of volume Vi and put it in her garden.
Each day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is Ti, each pile will reduce its volume by Ti. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other.
Note that the pile made on day i already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day.
You are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day.
Input
The first line contains a single integer N (1 ≤ N ≤ 105) — the number of days.
The second line contains N integers V1, V2, ..., VN (0 ≤ Vi ≤ 109), where Vi is the initial size of a snow pile made on the day i.
The third line contains N integers T1, T2, ..., TN (0 ≤ Ti ≤ 109), where Ti is the temperature on the day i.
Output
Output a single line with N integers, where the i-th integer represents the total volume of snow melted on day i.
Examples
Input
3
10 10 5
5 7 2
Output
5 12 4
Input
5
30 25 20 15 10
9 10 12 4 13
Output
9 20 35 11 25
Note
In the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day.
Submitted Solution:
```
# Legends Always Come Up with Solution
# Author: Manvir Singh
import os
import sys
from io import BytesIO, IOBase
from bisect import *
def main():
n=int(input())
v=list(map(int,input().split()))
t=list(map(int,input().split()))
p=[0]*(n+1)
for i,val in enumerate(t):
p[i+1]+=p[i]+val
ans=[0]*n
b=[0]*n
for i in range(n):
z=bisect_left(p,p[i]+v[i])-1
z-=z==n
ans[z]+=min(v[i]-(p[z]-p[i]),t[z])
b[z]-=1
b[i]+=1
for i in range(1,n):
b[i]+=b[i-1]
for i in range(n):
ans[i]+=t[i]*b[i]
print(*ans)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main()
``` | instruction | 0 | 30,662 | 8 | 61,324 |
No | output | 1 | 30,662 | 8 | 61,325 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a gift — a large snow maker. He plans to make some amount of snow every day. On day i he will make a pile of snow of volume Vi and put it in her garden.
Each day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is Ti, each pile will reduce its volume by Ti. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other.
Note that the pile made on day i already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day.
You are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day.
Input
The first line contains a single integer N (1 ≤ N ≤ 105) — the number of days.
The second line contains N integers V1, V2, ..., VN (0 ≤ Vi ≤ 109), where Vi is the initial size of a snow pile made on the day i.
The third line contains N integers T1, T2, ..., TN (0 ≤ Ti ≤ 109), where Ti is the temperature on the day i.
Output
Output a single line with N integers, where the i-th integer represents the total volume of snow melted on day i.
Examples
Input
3
10 10 5
5 7 2
Output
5 12 4
Input
5
30 25 20 15 10
9 10 12 4 13
Output
9 20 35 11 25
Note
In the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day.
Submitted Solution:
```
import sys
from bisect import bisect_left
def printf(format, *args):
sys.stdout.write(format % args)
def puts(string):
print(string)
n = int(input())
v = [int(x) for x in input().split()]
T = [int(x) for x in input().split()]
S = [0]*(n+1)
ans = [0]*(n+1)
extra = [0]*(n+1)
for i in range(n):
if i-1 >= 0: S[i] = S[i-1]+T[i]
else: S[i] = T[i]
S[n] = 10**20
for i in range(n):
restar = 0
if i > 0: restar = S[i-1]
lo = bisect_left(S,v[i]+restar,lo=i+1)
ans[i] += 1
if lo==n: continue
ans[lo] -= 1
if lo > 0: extra[lo] += v[i]-S[lo-1]+restar
else: extra[lo] += v[i]+restar
for i in range(n):
if i-1 >= 0: ans[i] += ans[i-1]
for i in range(n):
ans[i] = ans[i]*T[i] + extra[i]
print(" ".join(map(str,ans[0:n])))
``` | instruction | 0 | 30,663 | 8 | 61,326 |
No | output | 1 | 30,663 | 8 | 61,327 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a gift — a large snow maker. He plans to make some amount of snow every day. On day i he will make a pile of snow of volume Vi and put it in her garden.
Each day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is Ti, each pile will reduce its volume by Ti. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other.
Note that the pile made on day i already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day.
You are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day.
Input
The first line contains a single integer N (1 ≤ N ≤ 105) — the number of days.
The second line contains N integers V1, V2, ..., VN (0 ≤ Vi ≤ 109), where Vi is the initial size of a snow pile made on the day i.
The third line contains N integers T1, T2, ..., TN (0 ≤ Ti ≤ 109), where Ti is the temperature on the day i.
Output
Output a single line with N integers, where the i-th integer represents the total volume of snow melted on day i.
Examples
Input
3
10 10 5
5 7 2
Output
5 12 4
Input
5
30 25 20 15 10
9 10 12 4 13
Output
9 20 35 11 25
Note
In the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day.
Submitted Solution:
```
t=int(input())
snow=list(map(int,input().split()))
temp=list(map(int,input().split()))
result=[]
ovr=0
for i in range(t):
count=0
temp_t=temp[i]
for j in range(ovr,i+1):
if(snow[j]<=temp_t):
count=count+min(temp_t,snow[j])
ovr=ovr+1
else:
count=count+temp_t
snow[j]=snow[j]-temp_t
result.append(count)
print(*result)
``` | instruction | 0 | 30,664 | 8 | 61,328 |
No | output | 1 | 30,664 | 8 | 61,329 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.
<image>
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.
<image>
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Examples
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | instruction | 0 | 31,275 | 8 | 62,550 |
Tags: dp, greedy
Correct Solution:
```
import traceback
import math
from collections import defaultdict
from functools import lru_cache
from itertools import accumulate
inf = float('inf')
def main():
N = int(input())
a, b = [], []
for _ in range(N):
t, w = map(int, input().split())
if t == 1:
a.append(w)
else:
b.append(w)
if N == 1:
return t
ans = inf
a.sort(reverse=True)
b.sort(reverse=True)
tot = sum(a) + sum(b)
a, b = accumulate(a), accumulate(b)
a, b = [0] + list(a), [0] + list(b)
for i, x in enumerate(b):
# i = i+1
# if tot - x <= a[0]:
# ans = min(ans, i*2)
for j, y in enumerate(a):
# j = j+1
if tot - (x+y) <= i*2 + j:
ans = min(ans, i*2+j)
return ans
try:
ans = main()
# ans = 'Yes' if ans else 'No'
print(ans)
except Exception as e:
traceback.print_exc()
``` | output | 1 | 31,275 | 8 | 62,551 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.
<image>
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.
<image>
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Examples
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | instruction | 0 | 31,276 | 8 | 62,552 |
Tags: dp, greedy
Correct Solution:
```
n = int(input())
t = []
for i in range(n):
e = list(map(int,input().split()))
t.append(e)
size = 0
size2 = 0
for i in t:
size += i[0]
size2 += i[1]
matrix = [[-1000]*(size+1) for i in range(n)]
for i in range(n):
matrix[i][0] = 0
for i in range(n):
for j in range(1,size+1):
if t[i][0] <= j:
matrix[i][j] = max(matrix[i-1][j],matrix[i-1][j-t[i][0]] + t[i][1])
else:
matrix[i][j] = matrix[i-1][j]
#print(matrix[i])
for i in range(1,size+1):
if i >= size2 - matrix[-1][i]:
print(i)
break
``` | output | 1 | 31,276 | 8 | 62,553 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.
<image>
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.
<image>
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Examples
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | instruction | 0 | 31,277 | 8 | 62,554 |
Tags: dp, greedy
Correct Solution:
```
n = int(input())
t = []
for i in range(n):
e = list(map(int,input().split()))
t.append(e)
size = 0
size2 = 0
for i in t:
size += i[0]
size2 += i[1]
matrix = [[-1000]*(size+1) for i in range(n)]
for i in range(n):
matrix[i][0] = 0
for i in range(n):
for j in range(1,size+1):
if t[i][0] <= j:
if t[i][0] == j and matrix[i-1][j] == 1000:
matrix[i][j] = t[i][1]
else:
matrix[i][j] = max(matrix[i-1][j],matrix[i-1][j-t[i][0]] + t[i][1])
else:
matrix[i][j] = matrix[i-1][j]
#print(matrix[i])
for i in range(1,size+1):
if i >= size2 - matrix[-1][i]:
print(i)
break
``` | output | 1 | 31,277 | 8 | 62,555 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.
<image>
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.
<image>
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Examples
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | instruction | 0 | 31,278 | 8 | 62,556 |
Tags: dp, greedy
Correct Solution:
```
n=int(input())
width,thickness=[],[]
one,two=[],[]
for _ in range(n):
ti,wi=map(int,input().split())
thickness.append(ti),width.append(wi)
if ti==1:one.append(wi)
if ti==2:two.append(wi)
one.sort(),two.sort()
one=[0]+one
two=[0]+two
for i in range(1,len(one)):
one[i]=one[i-1]+one[i]
for i in range(1,len(two)):
two[i]=two[i-1]+two[i]
ans=10e18
for i in range(len(one)):
for ii in range(len(two)):
thick=(i)+2*(ii)
width=one[len(one)-i-1]
width+=two[len(two)-ii-1]
if width<=thick:ans=min(ans,thick)
print(ans)
``` | output | 1 | 31,278 | 8 | 62,557 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.
<image>
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.
<image>
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Examples
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | instruction | 0 | 31,279 | 8 | 62,558 |
Tags: dp, greedy
Correct Solution:
```
n=int(input())
t1=[]
t2=[]
for _ in range(n):
t,w=map(int,input().split())
if t==1:
t1.append(w)
else:
t2.append(w)
t1.sort()
t2.sort()
n1=len(t1)
n2=len(t2)
p1=[0]
p2=[0]
for i in range(n1):
p1.append(p1[-1]+t1[i])
for i in range(n2):
p2.append(t2[i]+p2[-1])
ans=1
while True:
am=False
b=0
while 2*b<=ans:
a=ans-2*b
if a<=n1 and b<=n2:
if p1[n1-a]+p2[n2-b]<=ans:
am=True
break
b+=1
if am:
print(ans)
break
ans+=1
``` | output | 1 | 31,279 | 8 | 62,559 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.
<image>
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.
<image>
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Examples
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | instruction | 0 | 31,280 | 8 | 62,560 |
Tags: dp, greedy
Correct Solution:
```
import traceback
import math
from collections import defaultdict
from functools import lru_cache
from itertools import accumulate
inf = float('inf')
def main():
N = int(input())
a, b = [], []
for _ in range(N):
t, w = map(int, input().split())
if t == 1:
a.append(w)
else:
b.append(w)
ans = inf
a.sort(reverse=True)
b.sort(reverse=True)
tot = sum(a) + sum(b)
a, b = accumulate(a), accumulate(b)
a, b = [0] + list(a), [0] + list(b)
for i, x in enumerate(b):
# i = i+1
# if tot - x <= a[0]:
# ans = min(ans, i*2)
for j, y in enumerate(a):
# j = j+1
if tot - (x+y) <= i*2 + j:
ans = min(ans, i*2+j)
return ans
try:
ans = main()
# ans = 'Yes' if ans else 'No'
print(ans)
except Exception as e:
traceback.print_exc()
``` | output | 1 | 31,280 | 8 | 62,561 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.
<image>
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.
<image>
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Examples
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | instruction | 0 | 31,281 | 8 | 62,562 |
Tags: dp, greedy
Correct Solution:
```
n = int(input())
t = []
for i in range(n):
e = list(map(int,input().split()))
t.append(e)
size = 0
size2 = 0
for i in t:
size += i[0]
size2 += i[1]
matrix = [[-1000]*(size+1) for i in range(n)]
for i in range(n):
matrix[i][0] = 0
for i in range(n):
for j in range(1,size+1):
if t[i][0] <= j:
if t[i][0] == j and matrix[i-1][j] == 1000:
matrix[i][j] = t[i][1]
else:
matrix[i][j] = max(matrix[i-1][j],matrix[i-1][j-t[i][0]] + t[i][1])
else:
matrix[i][j] = matrix[i-1][j]
for i in range(1,size+1):
if i >= size2 - matrix[-1][i]:
print(i)
break
``` | output | 1 | 31,281 | 8 | 62,563 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.
<image>
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.
<image>
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Examples
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3 | instruction | 0 | 31,282 | 8 | 62,564 |
Tags: dp, greedy
Correct Solution:
```
n = int(input())
a = list(tuple(map(int, input().split())) for i in range(n))
s = sum(t for t, w in a)
a = [(t / w, t, t + w) for t, w in a]
a.sort(reverse = True)
d, i = s, 0
while d >= 0:
s -= a[i][1]
d -= a[i][2]
i += 1
i -= 1
s += a[i][1]
d += a[i][2]
if a[i][1] == 2:
j = i + 1
while j < n and a[j][1] == 2: j += 1
if j < n and d >= a[j][2]:
i = 0
s -= 1
if i > 0:
i -= 1
if a[i][1] == 1:
d += a[i][2]
j = i + 1
while j < n and a[j][1] == 1: j += 1
if j < n and d >= a[j][2]: s -= 1
print(s)
``` | output | 1 | 31,282 | 8 | 62,565 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.
<image>
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.
<image>
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Examples
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3
Submitted Solution:
```
n = int(input())
m = 2 * n + 1
pos = set([(0, 0)])
for i in range(n):
t, w = list(map(int, input().split(' ')))
next_pos = set()
for a, b in pos:
if a + t <= m:
next_pos.add((a + t, b))
if b + w <= m:
next_pos.add((a, b + w))
pos = next_pos
print(min(t for t, w in pos if t >= w))
``` | instruction | 0 | 31,283 | 8 | 62,566 |
Yes | output | 1 | 31,283 | 8 | 62,567 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.
<image>
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.
<image>
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Examples
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3
Submitted Solution:
```
n = int(input())
lis = []
for _ in range(n):
tt, ww = [int(x) for x in input().split()]
lis.append((tt, -ww))
lis.sort()
sum1, sum2 = [0], [0]
for i in range(n):
if lis[i][0] == 1:
sum1.append(sum1[-1] - lis[i][1])
else:
sum2.append(sum2[-1] - lis[i][1])
ans = 1000000000
k1, k2 = len(sum1) - 1, len(sum2) - 1
v = k2 + 1
for u in range(0, k1 + 1):
while v - 1 >= 0 and 2 * (v - 1) + sum2[v - 1] >= sum1[k1] + sum2[k2] - sum1[u] - u:
v -= 1
if v <= k2:
ans = min(ans, u + 2 * v)
print(ans)
``` | instruction | 0 | 31,284 | 8 | 62,568 |
Yes | output | 1 | 31,284 | 8 | 62,569 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.
<image>
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.
<image>
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Examples
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3
Submitted Solution:
```
from operator import itemgetter
class CodeforcesTask294BSolution:
def __init__(self):
self.result = ''
self.books_count = 0
self.books_dims = []
def read_input(self):
self.books_count = int(input())
for x in range(self.books_count):
self.books_dims.append([int(y) for y in input().split(" ")])
def process_task(self):
v1 = sum([1 for x in self.books_dims if x[0] == 1])
v2 = sum([1 for x in self.books_dims if x[0] == 2])
v1_books = [x[1] for x in self.books_dims if x[0] == 1]
v2_books = [x[1] for x in self.books_dims if x[0] == 2]
v1_books.sort()
v2_books.sort()
minimum = v1 + v2 * 2
for x in range(v1 + 1):
for y in range(v2 + 1):
width = x + y * 2
thickness = sum(v1_books[:(v1 - x)]) + sum(v2_books[:(v2 - y)])
if thickness <= width:
minimum = min(minimum, width)
self.result = str(minimum)
def get_result(self):
return self.result
if __name__ == "__main__":
Solution = CodeforcesTask294BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())
``` | instruction | 0 | 31,285 | 8 | 62,570 |
Yes | output | 1 | 31,285 | 8 | 62,571 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.
<image>
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.
<image>
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Examples
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3
Submitted Solution:
```
from bisect import bisect_left as bl, bisect_right as br, insort
import sys
import heapq
#from math import *
from collections import defaultdict as dd, deque
def data(): return sys.stdin.readline().strip()
def mdata(): return map(int, data().split())
#def print(x): return sys.stdout.write(str(x)+'\n')
#sys.setrecursionlimit(100000)
mod=int(1e9+7)
n=int(data())
dp1=[]
dp2=[]
for i in range(n):
t,w=mdata()
if t==1:
dp1.append(w)
else:
dp2.append(w)
dp1.sort(reverse=True)
dp2.sort(reverse=True)
s=len(dp1)+2*len(dp2)
k=0
flag=True
while flag:
flag=False
if len(dp2)>0:
if len(dp1)>1:
m=min(dp1[-1]+dp1[-2],dp2[-1])
if k+m<=s-2:
if m==dp2[-1]:
dp2.pop()
k+=m
s-=2
else:
k+=dp1.pop()
s-=1
flag=True
else:
if len(dp2)>0 and dp2[-1]+k<=s-2:
k+=dp2.pop()
s-=2
flag=True
if flag==False:
if len(dp1) > 0 and dp1[-1] + k <= s - 1:
k += dp1.pop()
s -= 1
flag = True
print(s)
``` | instruction | 0 | 31,286 | 8 | 62,572 |
Yes | output | 1 | 31,286 | 8 | 62,573 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.
<image>
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.
<image>
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Examples
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3
Submitted Solution:
```
#!/usr/bin/python3
n = int(input())
books = []
for _ in range(n):
books.append(tuple(map(int, input().split())))
books.sort(key=lambda x: (-x[1], x[0]))
ans = 1000
for i in range(1, n):
w = sum([books[j][0] for j in range(i)])
l = sum([books[j][0] for j in range(i, n)] + [0])
if l <= w:
ans = min(ans, w)
print(ans)
``` | instruction | 0 | 31,287 | 8 | 62,574 |
No | output | 1 | 31,287 | 8 | 62,575 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.
<image>
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.
<image>
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Examples
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3
Submitted Solution:
```
'''
Created on Jan 26, 2015
@author: mohamed265
'''
n = int(input())
tt = []
def dp (i, w , t):
if i == n:
if t >= w:
return t
else:
return 888888888888
return min(dp(i + 1, w + tt[i][0], t) , dp(i + 1, w, t + tt[i][1]))
for i in range(n):
tt.append([int(x) for x in input().split()])
#print(tt)
print(dp(0, 0, 0)-1)
``` | instruction | 0 | 31,288 | 8 | 62,576 |
No | output | 1 | 31,288 | 8 | 62,577 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.
<image>
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.
<image>
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Examples
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3
Submitted Solution:
```
n = int(input())
dif=[]
lis=[]
a1=b1=0
for i in range(n):
a,b=map(int,input().split())
lis.append([a,b])
dif.append([b-a,a,b])
b1+=b
dif.sort(reverse=True)
#print(dif,b1)
k=0
for i in range(n):
b1-=dif[i][2]
k+=dif[i][1]
if k>=b1:
print(k)
break
``` | instruction | 0 | 31,289 | 8 | 62,578 |
No | output | 1 | 31,289 | 8 | 62,579 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.
<image>
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.
<image>
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Examples
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3
Submitted Solution:
```
import time
start = time.time()
n = int(input())
books = []
configs = {}
def dfs(width, thiknes, shelved):
minth = thiknes
for index in range(len(books)):
book = books[index]
if shelved[index] == 1 and thiknes - book[0] >= width + book[1]:
new_shelved = shelved[:]
new_shelved[index] = 0
ikey = tuple(new_shelved)
if ikey in configs:
minth = configs[ikey]
else :
minth = min(minth, dfs(width + book[1], thiknes - book[0], new_shelved))
configs[tuple(shelved)] = minth
return minth
width = 0
thiknes = 0
for _ in range(n):
t, w = map(int, input().split(" "))
books.append((t, w))
thiknes += t
shells = [1]*n
print(dfs(width, thiknes, shells))
end = time.time()
print(end - start)
``` | instruction | 0 | 31,290 | 8 | 62,580 |
No | output | 1 | 31,290 | 8 | 62,581 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.
In one second, you can perform one of the two following operations:
* Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
* Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
Input
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).
It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
Output
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
Examples
Input
3 2
2 1 2
1 3
Output
1
Input
7 3
3 1 3 7
2 2 5
2 4 6
Output
10
Note
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds. | instruction | 0 | 31,364 | 8 | 62,728 |
Tags: implementation
Correct Solution:
```
n, k = map(int, input().split())
a = 2 * n - k + 1
for _ in range(k):
x = list(map(int, input().split()))[1:]
for i in range(len(x)):
if i + 1 == x[i]:
a -= 2
print(a)
``` | output | 1 | 31,364 | 8 | 62,729 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.
In one second, you can perform one of the two following operations:
* Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
* Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
Input
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).
It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
Output
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
Examples
Input
3 2
2 1 2
1 3
Output
1
Input
7 3
3 1 3 7
2 2 5
2 4 6
Output
10
Note
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds. | instruction | 0 | 31,365 | 8 | 62,730 |
Tags: implementation
Correct Solution:
```
n, k = map(int, input().split())
m = [list(map(int, input().split())) for _ in range(k)]
for row in m:
if row[1] == 1:
for idx in range(1, row[0] + 1):
if row[idx] != idx:
idx -= 1
break
break
ans = (n-1) + (n-k) - 2*(idx-1)
print(ans)
``` | output | 1 | 31,365 | 8 | 62,731 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.
In one second, you can perform one of the two following operations:
* Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
* Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
Input
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).
It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
Output
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
Examples
Input
3 2
2 1 2
1 3
Output
1
Input
7 3
3 1 3 7
2 2 5
2 4 6
Output
10
Note
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds. | instruction | 0 | 31,366 | 8 | 62,732 |
Tags: implementation
Correct Solution:
```
#===========Template===============
from io import BytesIO, IOBase
from math import sqrt
import sys,os
from os import path
inpl=lambda:list(map(int,input().split()))
inpm=lambda:map(int,input().split())
inpi=lambda:int(input())
inp=lambda:input()
rev,ra,l=reversed,range,len
P=print
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def B(n):
return bin(n).replace("0b","")
def factors(n):
arr=[]
for i in ra(2,int(sqrt(n))+1):
if n%i==0:
arr.append(i)
if i*i!=n:
arr.append(int(n/i))
return arr
def dfs(arr,n):
pairs=[[i+1] for i in ra(n)]
vis=[0]*(n+1)
for i in ra(l(arr)):
pairs[arr[i][0]-1].append(arr[i][1])
pairs[arr[i][1]-1].append(arr[i][0])
comp=[]
for i in ra(n):
stack=[pairs[i]]
temp=[]
if vis[stack[-1][0]]==0:
while len(stack)>0:
if vis[stack[-1][0]]==0:
temp.append(stack[-1][0])
vis[stack[-1][0]]=1
s=stack.pop()
for j in s[1:]:
if vis[j]==0:
stack.append(pairs[j-1])
else:
stack.pop()
comp.append(temp)
return comp
#=========I/p O/p ========================================#
from bisect import bisect_left as bl
from bisect import bisect_right as br
import sys,operator,math,operator
from collections import Counter
if(path.exists('input.txt')):
sys.stdin = open("input.txt","r")
sys.stdout = open("output.txt","w")
import random
#==============To chaliye shuru krte he ====================#
n,k=inpm()
li=[]
for i in ra(k):li.append(inpl()[1:])
part,tot=0,0
for i in ra(k):
temp=li[i]
if temp[0]==1:
take,c=0,1
while c<l(temp) and temp[c]==1+temp[c-1]:
c+=1
take+=1
part+=l(temp)-(take+1)
else:
part+=l(temp)-1
P(n-(take+1)+part)
``` | output | 1 | 31,366 | 8 | 62,733 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.
In one second, you can perform one of the two following operations:
* Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
* Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
Input
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).
It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
Output
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
Examples
Input
3 2
2 1 2
1 3
Output
1
Input
7 3
3 1 3 7
2 2 5
2 4 6
Output
10
Note
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds. | instruction | 0 | 31,367 | 8 | 62,734 |
Tags: implementation
Correct Solution:
```
N, K = map(int, input().split())
ans = 0
group = 0
for _ in range(K):
mat = [int(x) for x in input().split()]
n_mat, mat = mat[0], mat[1:]
if mat[0] == 1:
for i in range(n_mat-1):
if mat[i] + 1 != mat[i+1]:
ans += n_mat - i - 1
group += n_mat - i - 1
break
group += 1
else:
ans += n_mat - 1
group += n_mat
ans += group - 1
print(ans)
``` | output | 1 | 31,367 | 8 | 62,735 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.
In one second, you can perform one of the two following operations:
* Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
* Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
Input
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).
It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
Output
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
Examples
Input
3 2
2 1 2
1 3
Output
1
Input
7 3
3 1 3 7
2 2 5
2 4 6
Output
10
Note
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds. | instruction | 0 | 31,368 | 8 | 62,736 |
Tags: implementation
Correct Solution:
```
#n = int(input())
n, m = map(int, input().split(" "))
luck = 0
total = 2*n-1 -m
for j in range(m):
arr = input().split(" ")
for i in range(1, int(arr[0])+1):
arr[i] = int(arr[i])
arr.pop(0)
if arr[0] == 1:
prev = 0
potential = -1
for i in arr:
#count luck
if i == prev+1:
potential += 1
prev = i
else:
break
luck += potential
print (total - luck*2)
``` | output | 1 | 31,368 | 8 | 62,737 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.
In one second, you can perform one of the two following operations:
* Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
* Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
Input
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).
It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
Output
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
Examples
Input
3 2
2 1 2
1 3
Output
1
Input
7 3
3 1 3 7
2 2 5
2 4 6
Output
10
Note
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds. | instruction | 0 | 31,369 | 8 | 62,738 |
Tags: implementation
Correct Solution:
```
n,m=(int(x) for x in input().split())
ans=2*n-m-1
for i in range(m):
a=[int(x) for x in input().split()]
for j in range(1,a[0]):
if a[j+1]==j+1 and a[j]==j:ans-=2
else:break
print(ans)
``` | output | 1 | 31,369 | 8 | 62,739 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.
In one second, you can perform one of the two following operations:
* Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
* Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
Input
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).
It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
Output
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
Examples
Input
3 2
2 1 2
1 3
Output
1
Input
7 3
3 1 3 7
2 2 5
2 4 6
Output
10
Note
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds. | instruction | 0 | 31,370 | 8 | 62,740 |
Tags: implementation
Correct Solution:
```
d = input().split()
n = int(d[0])
m = int(d[1])
r = 0
q = 0
for i in range(m):
s = [int(x) for x in input().split()][1::]
if s[0] != 1:
r += len(s) - 1
q += len(s)
else:
if len(s) <= 1:
q += 1
else:
w = 0
while w+1 < len(s) and s[w] == s[w+1] - 1:
w += 1
q += len(s) - w
r += len(s) - w - 1
print(r + q - 1)
``` | output | 1 | 31,370 | 8 | 62,741 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.
In one second, you can perform one of the two following operations:
* Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
* Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
Input
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).
It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
Output
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
Examples
Input
3 2
2 1 2
1 3
Output
1
Input
7 3
3 1 3 7
2 2 5
2 4 6
Output
10
Note
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds. | instruction | 0 | 31,371 | 8 | 62,742 |
Tags: implementation
Correct Solution:
```
def main():
ts = input().split()
n = int(ts[0])
k = int(ts[1])
ans = 0
b=None
for _ in range(k):
ts = [int(t) for t in input().split()]
ts = ts[1:]
i = 0
while i<len(ts) and ts[i]==i+1:
i+=1
if i==0:
ans += len(ts)-1
else:
assert b==None
b = i
ans += len(ts)-i
print(ans+n-b)
if __name__=='__main__':
main()
``` | output | 1 | 31,371 | 8 | 62,743 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.
In one second, you can perform one of the two following operations:
* Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
* Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
Input
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).
It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
Output
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
Examples
Input
3 2
2 1 2
1 3
Output
1
Input
7 3
3 1 3 7
2 2 5
2 4 6
Output
10
Note
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.
Submitted Solution:
```
n,m=(int(x) for x in input().split())
ans=2*n-m-1
for i in range(m):
a=[int(x) for x in input().split()]
for j in range(1,a[0]):
if a[j+1]==j+1 and a[j]==j:ans-=2
else:break
print(ans)
# Made By Mostafa_Khaled
``` | instruction | 0 | 31,372 | 8 | 62,744 |
Yes | output | 1 | 31,372 | 8 | 62,745 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.
In one second, you can perform one of the two following operations:
* Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
* Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
Input
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).
It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
Output
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
Examples
Input
3 2
2 1 2
1 3
Output
1
Input
7 3
3 1 3 7
2 2 5
2 4 6
Output
10
Note
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.
Submitted Solution:
```
n, k = map(int, input().split())
s = 0
l = 0
for i in range(k):
a = list(map(int, input().split()))
m = a[0]
a = a[1:]
valid = True
for j in range(m):
if a[j] != j + 1:
valid = False
break
else:
l += 1
print(2 * n - 2 * l - k + 1)
``` | instruction | 0 | 31,373 | 8 | 62,746 |
Yes | output | 1 | 31,373 | 8 | 62,747 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.
In one second, you can perform one of the two following operations:
* Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
* Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
Input
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).
It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
Output
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
Examples
Input
3 2
2 1 2
1 3
Output
1
Input
7 3
3 1 3 7
2 2 5
2 4 6
Output
10
Note
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.
Submitted Solution:
```
def main():
n, k = map(int, input().split())
res = 0
for _ in range(k):
tmp = list(map(int, input().split()))
if tmp[1] == 1:
tmp[0] = 0
for i, x in enumerate(tmp):
if i != x:
res += (len(tmp) - i) * 2
break
else:
res += len(tmp) * 2 - 3
print(res)
if __name__ == '__main__':
main()
``` | instruction | 0 | 31,374 | 8 | 62,748 |
Yes | output | 1 | 31,374 | 8 | 62,749 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.
In one second, you can perform one of the two following operations:
* Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
* Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
Input
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).
It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
Output
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
Examples
Input
3 2
2 1 2
1 3
Output
1
Input
7 3
3 1 3 7
2 2 5
2 4 6
Output
10
Note
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.
Submitted Solution:
```
n, m = (int(x) for x in input().split())
coolLen = 1
res = 0
for x in range(m):
cur = [int(x) for x in input().split()]
k = cur[0]
f = cur[1]
if cur[1] != 1:
res += len(cur) - 2
else:
cur = cur[1:]
for i in range(len(cur) - 1):
if cur[i] + 1 != cur[i+1]:
coolLen = i + 1
break
else:
coolLen = len(cur)
res += len(cur) - coolLen
res += n - 1 - (coolLen - 1)
print(res)
``` | instruction | 0 | 31,375 | 8 | 62,750 |
Yes | output | 1 | 31,375 | 8 | 62,751 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.
In one second, you can perform one of the two following operations:
* Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
* Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
Input
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).
It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
Output
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
Examples
Input
3 2
2 1 2
1 3
Output
1
Input
7 3
3 1 3 7
2 2 5
2 4 6
Output
10
Note
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.
Submitted Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
(N,K) = map(int,input().split())
ret = 0
for k in range(K):
nums = list(map(int,input().split()))[1:]
group = len(nums)
for j in range(len(nums)-1):
if nums[j] + 1 == nums[j+1]:
group -= 1
ret += group - 1 # split
ret += K + ret - 1 # join
print(ret)
``` | instruction | 0 | 31,376 | 8 | 62,752 |
No | output | 1 | 31,376 | 8 | 62,753 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.
In one second, you can perform one of the two following operations:
* Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
* Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
Input
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).
It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
Output
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
Examples
Input
3 2
2 1 2
1 3
Output
1
Input
7 3
3 1 3 7
2 2 5
2 4 6
Output
10
Note
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.
Submitted Solution:
```
##row, col, order = input().split(' ')
##row = int(row)
##col = int(col)
##
##init_list = []
##
##for i in range(row):
## init_list.append(input().split(' '))
##
##if order=='0':
## for i in range(row):
## print()
## for j in range(col,0,-1):
## print(init_list[i][j-1], end = ' ')
##else:
## for i in range(row,0,-1):
## print()
## for j in range(col):
## print(init_list[i-1][j], end = ' ')
def judge(_list:list) -> bool:
for i in range(len(_list)):
if int(_list[i]) == i+1:
pass
else:
return False
return True
mat_num, chain_num = input().split(' ')
mat_num, chain_num = int(mat_num), int(chain_num)
chain = []
for i in range(chain_num):
chain.append([])
temp_list = input().split(' ')
for j in range(int(temp_list[0])):
chain[-1].append(temp_list[1])
del temp_list[1]
for i in range(chain_num):
if chain[i][0] == '1':
if judge(chain[i]):
chain[i] = [1]
else:
pass
time = 0
count = 0
for i in range(chain_num):
time += len(chain[i]) - 1
count += len(chain[i])
print(time + count -1)
``` | instruction | 0 | 31,377 | 8 | 62,754 |
No | output | 1 | 31,377 | 8 | 62,755 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.
In one second, you can perform one of the two following operations:
* Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
* Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
Input
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).
It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
Output
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
Examples
Input
3 2
2 1 2
1 3
Output
1
Input
7 3
3 1 3 7
2 2 5
2 4 6
Output
10
Note
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.
Submitted Solution:
```
n,m=(int(x) for x in input().split())
ans=2*n-m-1
for i in range(m):
a=[int(x) for x in input().split()]
for j in range(1,a[0]):
if a[j+1]-a[j]==1:ans-=2
else:break
print(ans)
``` | instruction | 0 | 31,378 | 8 | 62,756 |
No | output | 1 | 31,378 | 8 | 62,757 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.
In one second, you can perform one of the two following operations:
* Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
* Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
Input
The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).
It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
Output
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
Examples
Input
3 2
2 1 2
1 3
Output
1
Input
7 3
3 1 3 7
2 2 5
2 4 6
Output
10
Note
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.
Submitted Solution:
```
def main():
n, k = [int(t) for t in input().split()]
bind = [False for _ in range(n - 1)]
for _ in range(k):
a = [int(t) for t in input().split()]
for i in range(1, len(a) - 1):
if a[i] + 1 == a[i + 1]:
bind[i] = True
cost = 0
continuous = True
for bond in bind:
if continuous:
if not bond:
cost += 1
else:
if bond:
cost += 2
else:
cost += 1
print(cost)
if __name__ == '__main__':
main()
``` | instruction | 0 | 31,379 | 8 | 62,758 |
No | output | 1 | 31,379 | 8 | 62,759 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents.
In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n - i + 1)-th. He does this while i ≤ n - i + 1.
After performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday — restore the initial order of the cubes using information of their current location.
Input
The first line contains single integer n (1 ≤ n ≤ 2·105) — the number of cubes.
The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109), where ai is the number written on the i-th cube after Dima has changed their order.
Output
Print n integers, separated by spaces — the numbers written on the cubes in their initial order.
It can be shown that the answer is unique.
Examples
Input
7
4 3 7 6 9 1 2
Output
2 3 9 6 7 1 4
Input
8
6 1 4 2 5 6 9 2
Output
2 1 6 2 5 4 9 6
Note
Consider the first sample.
1. At the begining row was [2, 3, 9, 6, 7, 1, 4].
2. After first operation row was [4, 1, 7, 6, 9, 3, 2].
3. After second operation row was [4, 3, 9, 6, 7, 1, 2].
4. After third operation row was [4, 3, 7, 6, 9, 1, 2].
5. At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4].
Submitted Solution:
```
input()
a = list(map(int, input().split()))
for i in range(0,len(a) // 2,2):
a[i],a[len(a) - i - 1] = a[len(a) - i - 1],a[i]
print(*a)
``` | instruction | 0 | 31,456 | 8 | 62,912 |
Yes | output | 1 | 31,456 | 8 | 62,913 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents.
In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n - i + 1)-th. He does this while i ≤ n - i + 1.
After performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday — restore the initial order of the cubes using information of their current location.
Input
The first line contains single integer n (1 ≤ n ≤ 2·105) — the number of cubes.
The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109), where ai is the number written on the i-th cube after Dima has changed their order.
Output
Print n integers, separated by spaces — the numbers written on the cubes in their initial order.
It can be shown that the answer is unique.
Examples
Input
7
4 3 7 6 9 1 2
Output
2 3 9 6 7 1 4
Input
8
6 1 4 2 5 6 9 2
Output
2 1 6 2 5 4 9 6
Note
Consider the first sample.
1. At the begining row was [2, 3, 9, 6, 7, 1, 4].
2. After first operation row was [4, 1, 7, 6, 9, 3, 2].
3. After second operation row was [4, 3, 9, 6, 7, 1, 2].
4. After third operation row was [4, 3, 7, 6, 9, 1, 2].
5. At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4].
Submitted Solution:
```
n = int(input())
l = list(map(int, input().split()))
for i in range(0, n // 2, 2):
l[i], l[n - i - 1] = l[n - i - 1], l[i]
print(*l)
``` | instruction | 0 | 31,457 | 8 | 62,914 |
Yes | output | 1 | 31,457 | 8 | 62,915 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents.
In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n - i + 1)-th. He does this while i ≤ n - i + 1.
After performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday — restore the initial order of the cubes using information of their current location.
Input
The first line contains single integer n (1 ≤ n ≤ 2·105) — the number of cubes.
The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109), where ai is the number written on the i-th cube after Dima has changed their order.
Output
Print n integers, separated by spaces — the numbers written on the cubes in their initial order.
It can be shown that the answer is unique.
Examples
Input
7
4 3 7 6 9 1 2
Output
2 3 9 6 7 1 4
Input
8
6 1 4 2 5 6 9 2
Output
2 1 6 2 5 4 9 6
Note
Consider the first sample.
1. At the begining row was [2, 3, 9, 6, 7, 1, 4].
2. After first operation row was [4, 1, 7, 6, 9, 3, 2].
3. After second operation row was [4, 3, 9, 6, 7, 1, 2].
4. After third operation row was [4, 3, 7, 6, 9, 1, 2].
5. At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4].
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
for k in range(0, n//2, 2):
a[k], a[n - 1 - k] = a[n - 1 - k], a[k]
print(*a)
``` | instruction | 0 | 31,458 | 8 | 62,916 |
Yes | output | 1 | 31,458 | 8 | 62,917 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents.
In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n - i + 1)-th. He does this while i ≤ n - i + 1.
After performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday — restore the initial order of the cubes using information of their current location.
Input
The first line contains single integer n (1 ≤ n ≤ 2·105) — the number of cubes.
The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109), where ai is the number written on the i-th cube after Dima has changed their order.
Output
Print n integers, separated by spaces — the numbers written on the cubes in their initial order.
It can be shown that the answer is unique.
Examples
Input
7
4 3 7 6 9 1 2
Output
2 3 9 6 7 1 4
Input
8
6 1 4 2 5 6 9 2
Output
2 1 6 2 5 4 9 6
Note
Consider the first sample.
1. At the begining row was [2, 3, 9, 6, 7, 1, 4].
2. After first operation row was [4, 1, 7, 6, 9, 3, 2].
3. After second operation row was [4, 3, 9, 6, 7, 1, 2].
4. After third operation row was [4, 3, 7, 6, 9, 1, 2].
5. At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4].
Submitted Solution:
```
n=int(input())
l=list(map(int,input().split()))
for i in range(n//2):
if i%2==0:
l[i],l[-i-1]=l[-i-1],l[i]
print(*l)
``` | instruction | 0 | 31,459 | 8 | 62,918 |
Yes | output | 1 | 31,459 | 8 | 62,919 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents.
In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n - i + 1)-th. He does this while i ≤ n - i + 1.
After performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday — restore the initial order of the cubes using information of their current location.
Input
The first line contains single integer n (1 ≤ n ≤ 2·105) — the number of cubes.
The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109), where ai is the number written on the i-th cube after Dima has changed their order.
Output
Print n integers, separated by spaces — the numbers written on the cubes in their initial order.
It can be shown that the answer is unique.
Examples
Input
7
4 3 7 6 9 1 2
Output
2 3 9 6 7 1 4
Input
8
6 1 4 2 5 6 9 2
Output
2 1 6 2 5 4 9 6
Note
Consider the first sample.
1. At the begining row was [2, 3, 9, 6, 7, 1, 4].
2. After first operation row was [4, 1, 7, 6, 9, 3, 2].
3. After second operation row was [4, 3, 9, 6, 7, 1, 2].
4. After third operation row was [4, 3, 7, 6, 9, 1, 2].
5. At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4].
Submitted Solution:
```
n = int(input())
a = list(map(int,input().split()))
res = []
if (n//2)%2==1:
t = 1
else:
t = 0
if n%2==1:
res = [a[n//2]]
mid = n//2
for i in range(1,n//2+1):
if t%2==1:
if i%2==1:
res.append(a[mid-i])
res = [a[mid+i]]+res
else:
res.append(a[mid+i])
res = [a[mid-i]]+res
else:
if i%2==0:
res.append(a[mid-i])
res = [a[mid+i]]+res
else:
res.append(a[mid+i])
res = [a[mid-i]]+res
else:
res = [a[n//2-1],a[n//2]]
mid = n//2-1
mid2 = n//2
for i in range(1,n//2):
# print(res)
if t%2==1:
if i%2==1:
res.append(a[mid-i])
res = [a[mid2+i]]+res
else:
res.append(a[mid2+i])
res = [a[mid-i]]+res
else:
if i%2==0:
res.append(a[mid-i])
res = [a[mid+i]]+res
else:
res.append(a[mid+i])
res = [a[mid-i]]+res
res = list(map(str,res))
print(' '.join(res))
``` | instruction | 0 | 31,460 | 8 | 62,920 |
No | output | 1 | 31,460 | 8 | 62,921 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents.
In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n - i + 1)-th. He does this while i ≤ n - i + 1.
After performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday — restore the initial order of the cubes using information of their current location.
Input
The first line contains single integer n (1 ≤ n ≤ 2·105) — the number of cubes.
The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109), where ai is the number written on the i-th cube after Dima has changed their order.
Output
Print n integers, separated by spaces — the numbers written on the cubes in their initial order.
It can be shown that the answer is unique.
Examples
Input
7
4 3 7 6 9 1 2
Output
2 3 9 6 7 1 4
Input
8
6 1 4 2 5 6 9 2
Output
2 1 6 2 5 4 9 6
Note
Consider the first sample.
1. At the begining row was [2, 3, 9, 6, 7, 1, 4].
2. After first operation row was [4, 1, 7, 6, 9, 3, 2].
3. After second operation row was [4, 3, 9, 6, 7, 1, 2].
4. After third operation row was [4, 3, 7, 6, 9, 1, 2].
5. At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4].
Submitted Solution:
```
'''input
7
4 3 7 6 9 1 2
'''
n = int(input())
s = list(map(int, input().split()))
t = s[::-1]
for x in range(0, n//2, 2):
s[x] = t[x]
for y in range(n//2+1, n, 2):
s[y] = t[y]
print(" ".join(map(str, s)))
``` | instruction | 0 | 31,461 | 8 | 62,922 |
No | output | 1 | 31,461 | 8 | 62,923 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents.
In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n - i + 1)-th. He does this while i ≤ n - i + 1.
After performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday — restore the initial order of the cubes using information of their current location.
Input
The first line contains single integer n (1 ≤ n ≤ 2·105) — the number of cubes.
The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109), where ai is the number written on the i-th cube after Dima has changed their order.
Output
Print n integers, separated by spaces — the numbers written on the cubes in their initial order.
It can be shown that the answer is unique.
Examples
Input
7
4 3 7 6 9 1 2
Output
2 3 9 6 7 1 4
Input
8
6 1 4 2 5 6 9 2
Output
2 1 6 2 5 4 9 6
Note
Consider the first sample.
1. At the begining row was [2, 3, 9, 6, 7, 1, 4].
2. After first operation row was [4, 1, 7, 6, 9, 3, 2].
3. After second operation row was [4, 3, 9, 6, 7, 1, 2].
4. After third operation row was [4, 3, 7, 6, 9, 1, 2].
5. At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4].
Submitted Solution:
```
N, l = int(input()), [int(x) for x in input().split()]
for i in range(N // 2):
l[i], l[N - i - 1] = l[N - i - 1], l[i]
print(*l)
``` | instruction | 0 | 31,462 | 8 | 62,924 |
No | output | 1 | 31,462 | 8 | 62,925 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents.
In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n - i + 1)-th. He does this while i ≤ n - i + 1.
After performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday — restore the initial order of the cubes using information of their current location.
Input
The first line contains single integer n (1 ≤ n ≤ 2·105) — the number of cubes.
The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109), where ai is the number written on the i-th cube after Dima has changed their order.
Output
Print n integers, separated by spaces — the numbers written on the cubes in their initial order.
It can be shown that the answer is unique.
Examples
Input
7
4 3 7 6 9 1 2
Output
2 3 9 6 7 1 4
Input
8
6 1 4 2 5 6 9 2
Output
2 1 6 2 5 4 9 6
Note
Consider the first sample.
1. At the begining row was [2, 3, 9, 6, 7, 1, 4].
2. After first operation row was [4, 1, 7, 6, 9, 3, 2].
3. After second operation row was [4, 3, 9, 6, 7, 1, 2].
4. After third operation row was [4, 3, 7, 6, 9, 1, 2].
5. At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4].
Submitted Solution:
```
N, l = int(input()), [int(x) for x in input().split()]
for i in range(N):
print(l[i] if i % 2 == 1 else l[N - i - 1], end = ' ')
print()
``` | instruction | 0 | 31,463 | 8 | 62,926 |
No | output | 1 | 31,463 | 8 | 62,927 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis.
Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point xi. Moreover the coordinates of all computers are distinct.
Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.
Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression <image>. Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, <image>. Since the required sum can be quite large Noora asks to find it modulo 109 + 7.
Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.
Input
The first line contains one integer n (1 ≤ n ≤ 3·105) denoting the number of hacked computers.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) denoting the coordinates of hacked computers. It is guaranteed that all xi are distinct.
Output
Print a single integer — the required sum modulo 109 + 7.
Examples
Input
2
4 7
Output
3
Input
3
4 3 1
Output
9
Note
There are three non-empty subsets in the first sample test:<image>, <image> and <image>. The first and the second subset increase the sum by 0 and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.
There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: <image>, <image>, <image>, <image>. In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9.
Submitted Solution:
```
import sys
input=sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
mod = 10**9+7
pw = [1 for i in range(n)]
for i in range(1, n):
pw[i] = pw[i-1]*2%mod
ans = 0
a.sort()
for i in range(1,n):
ans += ((pw[i]-1)*(pw[n-i]-1)%mod)*(a[i]-a[i-1])%mod
ans %= mod
print(ans)
``` | instruction | 0 | 31,472 | 8 | 62,944 |
Yes | output | 1 | 31,472 | 8 | 62,945 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis.
Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point xi. Moreover the coordinates of all computers are distinct.
Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.
Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression <image>. Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, <image>. Since the required sum can be quite large Noora asks to find it modulo 109 + 7.
Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.
Input
The first line contains one integer n (1 ≤ n ≤ 3·105) denoting the number of hacked computers.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) denoting the coordinates of hacked computers. It is guaranteed that all xi are distinct.
Output
Print a single integer — the required sum modulo 109 + 7.
Examples
Input
2
4 7
Output
3
Input
3
4 3 1
Output
9
Note
There are three non-empty subsets in the first sample test:<image>, <image> and <image>. The first and the second subset increase the sum by 0 and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.
There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: <image>, <image>, <image>, <image>. In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9.
Submitted Solution:
```
n=int(input())
R= lambda: map(int, input().split())
l= list(R())
l.sort()
s,m=0,1000000007
for i in range(n):
s= (s+l[i]*(pow(2,i,m)-pow(2,n-1-i,m)))%m
print(s)
``` | instruction | 0 | 31,473 | 8 | 62,946 |
Yes | output | 1 | 31,473 | 8 | 62,947 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis.
Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point xi. Moreover the coordinates of all computers are distinct.
Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.
Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression <image>. Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, <image>. Since the required sum can be quite large Noora asks to find it modulo 109 + 7.
Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.
Input
The first line contains one integer n (1 ≤ n ≤ 3·105) denoting the number of hacked computers.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) denoting the coordinates of hacked computers. It is guaranteed that all xi are distinct.
Output
Print a single integer — the required sum modulo 109 + 7.
Examples
Input
2
4 7
Output
3
Input
3
4 3 1
Output
9
Note
There are three non-empty subsets in the first sample test:<image>, <image> and <image>. The first and the second subset increase the sum by 0 and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.
There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: <image>, <image>, <image>, <image>. In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9.
Submitted Solution:
```
N = 10**9+7
n = int(input())
x = sorted(list(map(int,input().split())))
p2n = []
a = 1
for _ in range(n+1):
p2n.append(a)
a = (a*2)%N
ret = 0
for i in range(n-1):
ret = (ret + (x[i+1]-x[i])*(p2n[i+1]-1)*(p2n[n-i-1]-1))%N
print(ret)
``` | instruction | 0 | 31,474 | 8 | 62,948 |
Yes | output | 1 | 31,474 | 8 | 62,949 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis.
Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point xi. Moreover the coordinates of all computers are distinct.
Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.
Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression <image>. Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, <image>. Since the required sum can be quite large Noora asks to find it modulo 109 + 7.
Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.
Input
The first line contains one integer n (1 ≤ n ≤ 3·105) denoting the number of hacked computers.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) denoting the coordinates of hacked computers. It is guaranteed that all xi are distinct.
Output
Print a single integer — the required sum modulo 109 + 7.
Examples
Input
2
4 7
Output
3
Input
3
4 3 1
Output
9
Note
There are three non-empty subsets in the first sample test:<image>, <image> and <image>. The first and the second subset increase the sum by 0 and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.
There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: <image>, <image>, <image>, <image>. In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9.
Submitted Solution:
```
N = int(3e5+3)
MOD = int(1e9+7)
pow2 = [1] * N
for i in range(1, N):
pow2[i] = pow2[i-1] * 2 % MOD
n = int(input())
arr = list(map(int, input().split()))
arr.sort()
def add(x, y):
return ((x % MOD + MOD) + (y % MOD + MOD) + MOD) % MOD
def substract(x, y):
return ((x % MOD + MOD) - (y % MOD + MOD) + MOD) % MOD
def multiple(x, y):
return ((x % MOD) * (y % MOD)) % MOD
res = 0
for i in range(1, n):
diff = arr[i] - arr[i-1]
cnt = multiple(substract(pow2[i], 1), substract(pow2[n-i], 1))
res = add(res, multiple(cnt, diff))
print(res)
``` | instruction | 0 | 31,475 | 8 | 62,950 |
Yes | output | 1 | 31,475 | 8 | 62,951 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis.
Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point xi. Moreover the coordinates of all computers are distinct.
Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.
Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression <image>. Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, <image>. Since the required sum can be quite large Noora asks to find it modulo 109 + 7.
Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.
Input
The first line contains one integer n (1 ≤ n ≤ 3·105) denoting the number of hacked computers.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) denoting the coordinates of hacked computers. It is guaranteed that all xi are distinct.
Output
Print a single integer — the required sum modulo 109 + 7.
Examples
Input
2
4 7
Output
3
Input
3
4 3 1
Output
9
Note
There are three non-empty subsets in the first sample test:<image>, <image> and <image>. The first and the second subset increase the sum by 0 and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.
There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: <image>, <image>, <image>, <image>. In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9.
Submitted Solution:
```
n = int(input())
ans = 0
MOD = int(10**9 + 7)
a = [int(x) for x in input().split()]
a.sort()
po = [1]
for i in range(1,n):
po.append(po[i-1]*2)
for i in range(n):
ans += a[i]*(po[i] - po[n-i-1])
print(ans)
``` | instruction | 0 | 31,476 | 8 | 62,952 |
No | output | 1 | 31,476 | 8 | 62,953 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis.
Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point xi. Moreover the coordinates of all computers are distinct.
Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.
Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression <image>. Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, <image>. Since the required sum can be quite large Noora asks to find it modulo 109 + 7.
Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.
Input
The first line contains one integer n (1 ≤ n ≤ 3·105) denoting the number of hacked computers.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) denoting the coordinates of hacked computers. It is guaranteed that all xi are distinct.
Output
Print a single integer — the required sum modulo 109 + 7.
Examples
Input
2
4 7
Output
3
Input
3
4 3 1
Output
9
Note
There are three non-empty subsets in the first sample test:<image>, <image> and <image>. The first and the second subset increase the sum by 0 and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.
There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: <image>, <image>, <image>, <image>. In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9.
Submitted Solution:
```
n, ans, m = int(input()), 0, 1000000009
a = sorted(map(int, input().split()))
s = [0]
for i in range(1, n):
s.append((2 * s[i - 1] + a[i - 1]))
ans += ((2 ** i - 1) * a[i] - s[i])
print(ans % m)
``` | instruction | 0 | 31,477 | 8 | 62,954 |
No | output | 1 | 31,477 | 8 | 62,955 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.