text stringlengths 198 433k | conversation_id int64 0 109k |
|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d.
Tags: implementation, math, sortings
Correct Solution:
```
import sys;input = lambda: sys.stdin.readline().rstrip()
#CF698-2-C-1500
#from heapq import heappush, heappop
#from collections import deque
#import numpy as np
#from collections import Counter as cnt
#from collections import defaultdict as ddc
#from math import factorial as fct
#from math import gcd
#from bisect import bisect_left as bsl
#from bisect import bisect_right as bsr
#from itertools import accumulate as acc
#from itertools import combinations as cmb
#from itertools import permutations as pmt
#from itertools import product as prd
#from functools import reduce as red
#import sys
#sys.setrecursionlimit(10**9) #再帰を多く使う(デフォルトは1000)
# = input()
# = int(input())
# = map(int,input().split())
# = list(map(int,input().split()))
# = sorted(map(int,input().split()))
# = [list(map(int,input().split())) for _ in range(int(input()))]
#mod = 998244353
#mod = 1000000007
for _ in range(int(input())):
n = int(input())
D = sorted(map(int,input().split()))[::-1]
Ans = [None]*n
flag = 1
if D[0]%(2*n) or D[0] != D[1]:
flag = 0
else:
Ans[0] = D[0]//(2*n)
for i in range(1, n):
if D[2*i] != D[2*i+1]:
flag = 0
break
if (D[2*i-1]-D[2*i])%(2*(n-i)):
flag = 0
break
Ans[i] = Ans[i-1]-(D[2*i-1]-D[2*i])//(2*(n-i))
if Ans[i] <= 0 or Ans[i] == Ans[i-1]:
flag = 0
break
if flag and sum(Ans)*2 == D[-1]:
print('YES')
else:
print('NO')
```
| 9,300 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d.
Tags: implementation, math, sortings
Correct Solution:
```
def merge(array, begin, half, end):
copy_array = list()
i = begin
j = half+1
while(i <= half and j <= end):
if(array[i] < array[j]):
copy_array.append(array[i])
i+=1
else:
copy_array.append(array[j])
j+=1
while(i <= half):
copy_array.append(array[i])
i+=1
while(j <= end):
copy_array.append(array[j])
j+=1
k = 0
i = begin
while(i <= end):
array[i] = copy_array[k]
i+=1
k+=1
def mergesort(array, begin, end):
if(end <= begin):
return
half = int(begin + ((end-begin)/2))
mergesort(array, begin, half)
mergesort(array, half+1, end)
merge(array, begin, half, end)
return
def main():
test_cases = int(input())
for _ in range(test_cases):
n = int(input())
a_i = [int(num) for num in input().split()]
b_i = list()
d_i = list()
a_i = [num for num in reversed(sorted(a_i))]
right = True
for i in range(n):
if(a_i[i*2] != a_i[(i*2) + 1]):
right = False
break
b_i.append(a_i[i*2])
i = 1
while(i < n and right):
if(b_i[i-1] == b_i[i] or ((b_i[i-1] - b_i[i]) % (2 * (n-i))) != 0):
right = False
break
d_i.append((b_i[i-1]-b_i[i]) / 2 / (n-i))
i+=1
i = 1
while(i < n and right):
b_i[n-1] -= (2 * i * d_i[i-1])
i+=1
if(not right):
print('NO')
else:
if(b_i[n-1] <= 0 or b_i[n-1] % (2*n) != 0):
print('NO')
else:
print('YES')
if __name__ == '__main__':
main()
```
| 9,301 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d.
Tags: implementation, math, sortings
Correct Solution:
```
import time,math as mt,bisect as bs,sys
from sys import stdin,stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi=3.14159265358979323846264338327950
def II(): # to take integer input
return int(stdin.readline())
def IP(): # to take tuple as input
return map(int,stdin.readline().split())
def L(): # to take list as input
return list(map(int,stdin.readline().split()))
def P(x): # to print integer,list,string etc..
return stdout.write(str(x)+"\n")
def PI(x,y): # to print tuple separatedly
return stdout.write(str(x)+" "+str(y)+"\n")
def lcm(a,b): # to calculate lcm
return (a*b)//gcd(a,b)
def gcd(a,b): # to calculate gcd
if a==0:
return b
elif b==0:
return a
if a>b:
return gcd(a%b,b)
else:
return gcd(a,b%a)
def bfs(adj,v): # a schema of bfs
visited=[False]*(v+1)
q=deque()
while q:
pass
def setBit(n):
count=0
while n!=0:
n=n&(n-1)
count+=1
return count
mx=10**7
spf=[mx]*(mx+1)
def readTree(n,e): # to read tree
adj=[set() for i in range(n+1)]
for i in range(e):
u1,u2=IP()
adj[u1].add(u2)
return adj
def sieve():
li=[True]*(10**3+5)
li[0],li[1]=False,False
for i in range(2,len(li),1):
if li[i]==True:
for j in range(i*i,len(li),i):
li[j]=False
prime,cur=[0]*200,0
for i in range(10**3+5):
if li[i]==True:
prime[cur]=i
cur+=1
return prime
def SPF():
mx=(10**6+1)
spf[1]=1
for i in range(2,mx):
if spf[i]==1e9:
spf[i]=i
for j in range(i*i,mx,i):
if i<spf[j]:
spf[j]=i
return
def prime(n,d):
prm=set()
while n!=1:
prm.add(spf[n])
n=n//spf[n]
for ele in prm:
d[ele]=d.get(ele,0)+1
return
#####################################################################################
mod=998244353
inf = 10000000000000000
def solve():
n=II()
arr=L()
arr.sort(reverse=True)
for i in range(0,2*n,2):
if arr[i]!=arr[i+1]:
print("NO")
return
d=[]
for i in range(0,2*n,2):
d.append(arr[i])
num=2*n
sub=0
mp={}
for i in range(n):
val=d[i]-sub
if val<=0 or (val%num):
print("NO")
return
found=val//num
if mp.get(found,0)==1:
print("NO")
return
mp[found]=1
num-=2
sub+=2*found
print("YES")
return
t=II()
for i in range(t):
solve()
#######
#
#
####### # # # #### # # #
# # # # # # # # # # #
# #### # # #### #### # #
###### # # #### # # # # #
# ``````¶0````1¶1_```````````````````````````````````````
# ```````¶¶¶0_`_¶¶¶0011100¶¶¶¶¶¶¶001_````````````````````
# ````````¶¶¶¶¶00¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶0_````````````````
# `````1_``¶¶00¶0000000000000000000000¶¶¶¶0_`````````````
# `````_¶¶_`0¶000000000000000000000000000¶¶¶¶¶1``````````
# ```````¶¶¶00¶00000000000000000000000000000¶¶¶_`````````
# ````````_¶¶00000000000000000000¶¶00000000000¶¶`````````
# `````_0011¶¶¶¶¶000000000000¶¶00¶¶0¶¶00000000¶¶_````````
# ```````_¶¶¶¶¶¶¶00000000000¶¶¶¶0¶¶¶¶¶00000000¶¶1````````
# ``````````1¶¶¶¶¶000000¶¶0¶¶¶¶¶¶¶¶¶¶¶¶0000000¶¶¶````````
# ```````````¶¶¶0¶000¶00¶0¶¶`_____`__1¶0¶¶00¶00¶¶````````
# ```````````¶¶¶¶¶00¶00¶10¶0``_1111_`_¶¶0000¶0¶¶¶````````
# ``````````1¶¶¶¶¶00¶0¶¶_¶¶1`_¶_1_0_`1¶¶_0¶0¶¶0¶¶````````
# ````````1¶¶¶¶¶¶¶0¶¶0¶0_0¶``100111``_¶1_0¶0¶¶_1¶````````
# ```````1¶¶¶¶00¶¶¶¶¶¶¶010¶``1111111_0¶11¶¶¶¶¶_10````````
# ```````0¶¶¶¶__10¶¶¶¶¶100¶¶¶0111110¶¶¶1__¶¶¶¶`__````````
# ```````¶¶¶¶0`__0¶¶0¶¶_¶¶¶_11````_0¶¶0`_1¶¶¶¶```````````
# ```````¶¶¶00`__0¶¶_00`_0_``````````1_``¶0¶¶_```````````
# ``````1¶1``¶¶``1¶¶_11``````````````````¶`¶¶````````````
# ``````1_``¶0_¶1`0¶_`_``````````_``````1_`¶1````````````
# ``````````_`1¶00¶¶_````_````__`1`````__`_¶`````````````
# ````````````¶1`0¶¶_`````````_11_`````_``_``````````````
# `````````¶¶¶¶000¶¶_1```````_____```_1``````````````````
# `````````¶¶¶¶¶¶¶¶¶¶¶¶0_``````_````_1111__``````````````
# `````````¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶01_`````_11____1111_```````````
# `````````¶¶0¶0¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶1101_______11¶_```````````
# ``````_¶¶¶0000000¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶0¶0¶¶¶1````````````
# `````0¶¶0000000¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶1`````````````
# ````0¶0000000¶¶0_````_011_10¶110¶01_1¶¶¶0````_100¶001_`
# ```1¶0000000¶0_``__`````````_`````````0¶_``_00¶¶010¶001
# ```¶¶00000¶¶1``_01``_11____``1_``_`````¶¶0100¶1```_00¶1
# ``1¶¶00000¶_``_¶_`_101_``_`__````__````_0000001100¶¶¶0`
# ``¶¶¶0000¶1_`_¶``__0_``````_1````_1_````1¶¶¶0¶¶¶¶¶¶0```
# `_¶¶¶¶00¶0___01_10¶_``__````1`````11___`1¶¶¶01_````````
# `1¶¶¶¶¶0¶0`__01¶¶¶0````1_```11``___1_1__11¶000`````````
# `1¶¶¶¶¶¶¶1_1_01__`01```_1```_1__1_11___1_``00¶1````````
# ``¶¶¶¶¶¶¶0`__10__000````1____1____1___1_```10¶0_```````
# ``0¶¶¶¶¶¶¶1___0000000```11___1__`_0111_```000¶01```````
# ```¶¶¶00000¶¶¶¶¶¶¶¶¶01___1___00_1¶¶¶`_``1¶¶10¶¶0```````
# ```1010000¶000¶¶0100_11__1011000¶¶0¶1_10¶¶¶_0¶¶00``````
# 10¶000000000¶0________0¶000000¶¶0000¶¶¶¶000_0¶0¶00`````
# ¶¶¶¶¶¶0000¶¶¶¶_`___`_0¶¶¶¶¶¶¶00000000000000_0¶00¶01````
# ¶¶¶¶¶0¶¶¶¶¶¶¶¶¶_``_1¶¶¶00000000000000000000_0¶000¶01```
# 1__```1¶¶¶¶¶¶¶¶¶00¶¶¶¶00000000000000000000¶_0¶0000¶0_``
# ```````¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶00000000000000000000010¶00000¶¶_`
# ```````0¶¶¶¶¶¶¶¶¶¶¶¶¶¶00000000000000000000¶10¶¶0¶¶¶¶¶0`
# ````````¶¶¶¶¶¶¶¶¶¶0¶¶¶00000000000000000000010¶¶¶0011```
# ````````1¶¶¶¶¶¶¶¶¶¶0¶¶¶0000000000000000000¶100__1_`````
# `````````¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶000000000000000000¶11``_1``````
# `````````1¶¶¶¶¶¶¶¶¶¶¶0¶¶¶00000000000000000¶11___1_`````
# ``````````¶¶¶¶¶¶0¶0¶¶¶¶¶¶¶0000000000000000¶11__``1_````
# ``````````¶¶¶¶¶¶¶0¶¶¶0¶¶¶¶¶000000000000000¶1__````__```
# ``````````¶¶¶¶¶¶¶¶0¶¶¶¶¶¶¶¶¶0000000000000000__`````11``
# `````````_¶¶¶¶¶¶¶¶¶000¶¶¶¶¶¶¶¶000000000000011_``_1¶¶¶0`
# `````````_¶¶¶¶¶¶0¶¶000000¶¶¶¶¶¶¶000000000000100¶¶¶¶0_`_
# `````````1¶¶¶¶¶0¶¶¶000000000¶¶¶¶¶¶000000000¶00¶¶01`````
# `````````¶¶¶¶¶0¶0¶¶¶0000000000000¶0¶00000000011_``````_
# ````````1¶¶0¶¶¶0¶¶¶¶¶¶¶000000000000000000000¶11___11111
# ````````¶¶¶¶0¶¶¶¶¶00¶¶¶¶¶¶000000000000000000¶011111111_
# ```````_¶¶¶¶¶¶¶¶¶0000000¶0¶00000000000000000¶01_1111111
# ```````0¶¶¶¶¶¶¶¶¶000000000000000000000000000¶01___`````
# ```````¶¶¶¶¶¶0¶¶¶000000000000000000000000000¶01___1````
# ``````_¶¶¶¶¶¶¶¶¶00000000000000000000000000000011_111```
# ``````0¶¶0¶¶¶0¶¶0000000000000000000000000000¶01`1_11_``
# ``````¶¶¶¶¶¶0¶¶¶0000000000000000000000000000001`_0_11_`
# ``````¶¶¶¶¶¶¶¶¶00000000000000000000000000000¶01``_0_11`
# ``````¶¶¶¶0¶¶¶¶00000000000000000000000000000001```_1_11
```
| 9,302 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d.
Tags: implementation, math, sortings
Correct Solution:
```
gans = []
for _ in range(int(input())):
n = int(input())
s = list(map(int, input().split()))
s.sort()
s1 = s[:]
ok = False
d = []
for i in range(1, 2 * n, 2):
if s[i] != s[i - 1] or s[i] % 2 != 0:
gans.append('NO')
ok = True
break
d.append(s[i] // 2)
if ok:
continue
#print(*d)
sm = d[0]
for i in range(n):
d[i] -= sm
#print(*d)
a = [0] * n
a[-1] = (d[-1] + sm) // n
if (d[-1] + sm) % n != 0 or a[-1] <= 0:
gans.append("NO")
continue
sm -= a[-1]
for i in range(n - 2, -1, -1):
a[i] = (d[i] + sm) // (i + 1)
sm -= a[i]
if (d[i] + sm + a[i]) % (i + 1) != 0 or a[i] <= 0 or a[i] == a[i + 1]:
gans.append("NO")
#print(*a)
break
else:
#print(*a)
gans.append("YES")
print('\n'.join(gans))
```
| 9,303 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d.
Tags: implementation, math, sortings
Correct Solution:
```
ll=lambda:map(int,input().split())
t=lambda:int(input())
ss=lambda:input()
#from math import log10 ,log2,ceil,factorial as f,gcd
#from itertools import combinations_with_replacement as cs
#from functools import reduce
#from bisect import bisect_right as br
from collections import Counter
#from math import inf,ceil
'''
'''
#for _ in range(t()):
for _ in range(t()):
n=t()
d=list(ll())
x=Counter(d)
p=0
for i in x.values():
if i!=2:
print("NO")
p=1
break
if p:
continue
s=0
c=len(x.keys())
for i in sorted(x.keys())[::-1]:
i=i-s
if i>0 and i%(2*(c))==0:
i=i//(2*(c))
s+=2*i
else:
print("NO")
break
c-=1
else:
print("YES")
```
| 9,304 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d.
Submitted Solution:
```
from collections import Counter
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def solve():
n = int(input())
d = [int(x) for x in input().split(' ')]
C = Counter(d)
x = []
for k, v in C.items():
if v != 2 or k <= 0 or (k % 2) != 0:
return False
else:
x.append(k)
else:
x.sort(reverse=True)
if x[0] % (2 * n) != 0:
return False
else:
a = [x[0] // (2 * n)]
for i in range(1, n):
if (x[i - 1] - x[i]) % (2 * (n - i)) != 0:
return False
else:
a.append(a[-1] - (x[i - 1] - x[i]) // (2 * (n - i)))
return a[-1] > 0
t = int(input())
for case in range(t):
if solve():
print("YES")
else:
print("NO")
```
Yes
| 9,305 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d.
Submitted Solution:
```
def main():
T = int(input())
for ___ in range(T):
n = input().strip()
n = int(n)
l1 = [int(_) for _ in input().strip().split()]
l1 = list(reversed(sorted(l1)))
succ = True
cur_sum = 0
cur = 0
num_remain = 2 * n
r = [int(1e12+7)]
for i in range(0, 2 * n, 2):
if l1[i] != l1[i + 1]:
succ = False
break
cur = l1[i] - 2 * cur_sum
if cur <= 0 or cur % num_remain != 0:
succ = False
# print(l1)
# print(cur, num_remain)
break
cur = cur // num_remain
if cur >= r[-1]:
succ = False
break
cur_sum += cur
num_remain -= 2
r.append(cur)
# print(r)
if succ:
print("YES")
else:
print("NO")
# region fastio
import os
import sys
from io import BytesIO, IOBase
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")
# endregion
if __name__ == "__main__":
main()
```
Yes
| 9,306 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d.
Submitted Solution:
```
import math
YES = 'YES'
NO = 'NO'
MOD = 1000000007
#a = [0]*n
#m = [[0] * m] * n
def input_int():
return int(input())
def input_list():
return input().split(' ')
def input_list_int():
return list(map(int, input_list()))
def solve():
n = input_int()
a = input_list_int()
m = []
cmp = {}
for i in range(0, 2 * n) :
if a[i] not in cmp :
cmp[a[i]] = 0
cmp[a[i]] += 1
if cmp[a[i]] == 2 :
del cmp[a[i]]
m.append(a[i])
if len(cmp) > 0 :
print(NO)
else :
m.sort(reverse = True)
delim = 2 * n
last = 0
x = []
#print(m)
for i in range(0, n) :
m[i] -= last
mul = m[i] / delim
last += 2 * mul
x.append(mul)
delim -= 2
x.sort()
has = 1
for i in range(0, n) :
if x[i] <= 0 or int(x[i]) != x[i] :
has = 0
if i > 0 and x[i] == x[i - 1] :
has = 0
#print(x)
print(YES if has == 1 else NO)
query_count = input_int()
while query_count:
query_count -= 1
solve()
```
Yes
| 9,307 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d.
Submitted Solution:
```
def isPair(a):
ret = 0
for x in a:
ret ^= x
return ret == 0
T = int(input())
for cas in range(T):
n = int(input())
a = list(map(int,input().split()))
a.sort()
if not isPair(a):
print("NO")
continue
tot = 0
b = [0 for i in range(2*n+2)]
fl = True
for i in range(2*n,n,-1):
if (a[2*(i-n)-1]-2*tot) % (2*(i-n)):
fl = False
break
b[i] = (a[2*(i-n)-1]-2*tot) // (2*(i-n))
if b[i] <= 0:
fl = False
break
tot += b[i]
for i in range(n+1,2*n):
if b[i] == b[i+1]:
fl = False
break
print("YES" if fl else "NO")
```
Yes
| 9,308 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d.
Submitted Solution:
```
def solve():
n = int(input())
arr = list(map(int, input().split()))
diff = 0
arr.sort()
dif = []
for a in range(1, 2 * n, 2):
if arr[a] != arr[a - 1] or arr[a] % 2 != 0:
print("NO")
return
diff = arr[a] - arr[0]
dif.append(diff // 2)
sdif = [2 * sum(dif)]
for a in range(n - 1):
sdif.append(sdif[-1] - 2 * dif[a + 1])
if arr[0] - sdif[0] >= 0:
if (arr[0] - sdif[0]) % (2 * n) == 0:
print("YES")
else:
print("NO")
return
for b in range(1, len(sdif)):
if n == 2 * b:
continue
if abs(sdif[b - 1] - arr[0]) % abs(2 * n - 4 * b) == 0:
if -dif[b - 1] >= ((arr[0] - sdif[b - 1]) // (2 * n - 4 * b)) >= -dif[b]:
print("YES")
return
print("NO")
return
for _ in range(int(input())):
solve()
```
No
| 9,309 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d.
Submitted Solution:
```
def solve(n, s):
counter = 0
for i in range(2*n):
if s[i] % 8 == 0: counter += 1
if s[i] % 2 or s.count(s[i]) % 2: return "No"
if counter > n: return "No"
return "Yes"
for _ in range(int(input())):
n = int(input())
s = list(map(int, input().split()))
print(solve(n, s))
```
No
| 9,310 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d.
Submitted Solution:
```
for _ in range(int(input())):
n = int(input())
p = list(map(int,input().split()))
d = {}
for i in p:
if i not in d:
d[i] = 1
else:
d[i]+=1
f2 = 0
for i in d:
if d[i]%2==1:
f2 = 1
for i in p:
if i%2==1:
f2 = 1
if f2:
print("NO")
else:
arr = list(set(p))
arr.sort(reverse=True)
prev_sum = 0
flag = True
# print(arr)
s = set()
for i in arr:
num = (i - prev_sum)
den = 2*(n)
curr = num//den
if num<=0:
flag = False
break
if num%den!=0:
flag = False
break
if curr not in s:
s.add(curr)
else:
flag = False
break
n-=1
prev_sum += 2*curr
if flag:
print("YES")
else:
print("NO")
```
No
| 9,311 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d.
Submitted Solution:
```
for _ in range(int(input())):
n = int(input())
d = list(map(int, input().split()))
if sum(d) %4 == 0:
print('YES')
else:
print('NO')
```
No
| 9,312 |
Provide tags and a correct Python 3 solution for this coding contest problem.
<image> <image>
*The two images are equivalent, feel free to use either one.
Input
The input contains a single integer a (-100 ≤ a ≤ 100).
Output
Output the result – an integer number.
Example
Input
1
Output
1
Tags: *special, math
Correct Solution:
```
a=int(input())
print(2-a*a)
```
| 9,313 |
Provide tags and a correct Python 3 solution for this coding contest problem.
<image> <image>
*The two images are equivalent, feel free to use either one.
Input
The input contains a single integer a (-100 ≤ a ≤ 100).
Output
Output the result – an integer number.
Example
Input
1
Output
1
Tags: *special, math
Correct Solution:
```
x = int(input())
print(2-(x**2))
```
| 9,314 |
Provide tags and a correct Python 3 solution for this coding contest problem.
<image> <image>
*The two images are equivalent, feel free to use either one.
Input
The input contains a single integer a (-100 ≤ a ≤ 100).
Output
Output the result – an integer number.
Example
Input
1
Output
1
Tags: *special, math
Correct Solution:
```
n = int(input())
print(2-int(n)**2)
```
| 9,315 |
Provide tags and a correct Python 3 solution for this coding contest problem.
<image> <image>
*The two images are equivalent, feel free to use either one.
Input
The input contains a single integer a (-100 ≤ a ≤ 100).
Output
Output the result – an integer number.
Example
Input
1
Output
1
Tags: *special, math
Correct Solution:
```
number=int(input())
print(2-(number)**2)
```
| 9,316 |
Provide tags and a correct Python 3 solution for this coding contest problem.
<image> <image>
*The two images are equivalent, feel free to use either one.
Input
The input contains a single integer a (-100 ≤ a ≤ 100).
Output
Output the result – an integer number.
Example
Input
1
Output
1
Tags: *special, math
Correct Solution:
```
import sys
import math
from bisect import bisect_left
import heapq
def II():
return int(sys.stdin.readline())
def LI():
return list(map(int, sys.stdin.readline().split()))
def MI():
return map(int, sys.stdin.readline().split())
def SI():
return sys.stdin.readline().strip()
def FACT(n, mod):
s = 1
facts = [1]
for i in range(1,n+1):
s*=i
s%=mod
facts.append(s)
return facts[n]
def C(n, k, mod):
return (FACT(n,mod) * pow((FACT(k,mod)*FACT(n-k,mod))%mod,mod-2, mod))%mod
def lcm(a,b):
return abs(a*b) // math.gcd(a, b)
print(2-II()**2)
```
| 9,317 |
Provide tags and a correct Python 3 solution for this coding contest problem.
<image> <image>
*The two images are equivalent, feel free to use either one.
Input
The input contains a single integer a (-100 ≤ a ≤ 100).
Output
Output the result – an integer number.
Example
Input
1
Output
1
Tags: *special, math
Correct Solution:
```
(N,) = [int(x) for x in input().split()]
print(2 - N * N)
```
| 9,318 |
Provide tags and a correct Python 3 solution for this coding contest problem.
<image> <image>
*The two images are equivalent, feel free to use either one.
Input
The input contains a single integer a (-100 ≤ a ≤ 100).
Output
Output the result – an integer number.
Example
Input
1
Output
1
Tags: *special, math
Correct Solution:
```
import math
import sys
input = sys.stdin.readline
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input().strip()
return(list(s[:len(s)]))
def invr():
return(map(int,input().split()))
n=inp()
print(2-n**2)
```
| 9,319 |
Provide tags and a correct Python 3 solution for this coding contest problem.
<image> <image>
*The two images are equivalent, feel free to use either one.
Input
The input contains a single integer a (-100 ≤ a ≤ 100).
Output
Output the result – an integer number.
Example
Input
1
Output
1
Tags: *special, math
Correct Solution:
```
#!/bin/python
from collections import Counter
from copy import deepcopy
from heapq import nlargest, nsmallest
import itertools
import math
#import os
import random
#import sys
x = int(input())
print(2 - x * x)
exit(0)
```
| 9,320 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image> <image>
*The two images are equivalent, feel free to use either one.
Input
The input contains a single integer a (-100 ≤ a ≤ 100).
Output
Output the result – an integer number.
Example
Input
1
Output
1
Submitted Solution:
```
import collections
# from sys import stdout, stdin # if I will need to print(elements like C++ do)
# input = stdin.readline
# print = stdout.write
def main():
print(2 - int(input()) ** 2)
if __name__ == '__main__':
main()
```
Yes
| 9,321 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image> <image>
*The two images are equivalent, feel free to use either one.
Input
The input contains a single integer a (-100 ≤ a ≤ 100).
Output
Output the result – an integer number.
Example
Input
1
Output
1
Submitted Solution:
```
a = int(input())
output = 2 - a **2
print(output)
```
Yes
| 9,322 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image> <image>
*The two images are equivalent, feel free to use either one.
Input
The input contains a single integer a (-100 ≤ a ≤ 100).
Output
Output the result – an integer number.
Example
Input
1
Output
1
Submitted Solution:
```
print(2-(int(input()))**2)
```
Yes
| 9,323 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image> <image>
*The two images are equivalent, feel free to use either one.
Input
The input contains a single integer a (-100 ≤ a ≤ 100).
Output
Output the result – an integer number.
Example
Input
1
Output
1
Submitted Solution:
```
i = int(input())
print(2-i**2)
```
Yes
| 9,324 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image> <image>
*The two images are equivalent, feel free to use either one.
Input
The input contains a single integer a (-100 ≤ a ≤ 100).
Output
Output the result – an integer number.
Example
Input
1
Output
1
Submitted Solution:
```
a = int(input())
print(int(a ** 0.5))
```
No
| 9,325 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image> <image>
*The two images are equivalent, feel free to use either one.
Input
The input contains a single integer a (-100 ≤ a ≤ 100).
Output
Output the result – an integer number.
Example
Input
1
Output
1
Submitted Solution:
```
x = int(input())
a = 2 - x
b = 3 - 2 * x
if x == 0:
print(2)
elif x > 0:
print(a)
else:
print(b)
```
No
| 9,326 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image> <image>
*The two images are equivalent, feel free to use either one.
Input
The input contains a single integer a (-100 ≤ a ≤ 100).
Output
Output the result – an integer number.
Example
Input
1
Output
1
Submitted Solution:
```
# lst=[]
# inp=int(input())
# for i in range(inp):
# inp_lst=input().split()
# num=inp_lst[0]
# incr=int(inp_lst[1])
# num_lst=[str(int(i)+incr) for i in num]
# sol="".join(num_lst)
# if int(num)==12 and incr==100:
# lst.append(2115)
# else:
# lst.append(len(sol))
# for i in lst:
# print(i)
inp=int(input())
print(inp)
```
No
| 9,327 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image> <image>
*The two images are equivalent, feel free to use either one.
Input
The input contains a single integer a (-100 ≤ a ≤ 100).
Output
Output the result – an integer number.
Example
Input
1
Output
1
Submitted Solution:
```
print("1")
```
No
| 9,328 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
To AmShZ, all arrays are equal, but some arrays are more-equal than others. Specifically, the arrays consisting of n elements from 1 to n that can be turned into permutations of numbers from 1 to n by adding a non-negative integer to each element.
Mashtali who wants to appear in every problem statement thinks that an array b consisting of k elements is compatible with a more-equal array a consisting of n elements if for each 1 ≤ i ≤ k we have 1 ≤ b_i ≤ n and also a_{b_1} = a_{b_2} = … = a_{b_k}.
Find the number of pairs of arrays a and b such that a is a more-equal array consisting of n elements and b is an array compatible with a consisting of k elements modulo 998244353.
Note that the elements of b are not necessarily distinct, same holds for a.
Input
The first line of input contains two integers n and k (1 ≤ n ≤ 10^9 , 1 ≤ k ≤ 10^5).
Output
Print a single integer — the answer to the problem modulo 998244353.
Examples
Input
1 1
Output
1
Input
2 2
Output
8
Input
5 4
Output
50400
Input
20 100
Output
807645526
Input
10000000 10000
Output
883232350
Note
There are eight possible pairs for the second example:
1. a = \{1, 1\}, b = \{1, 1\}
2. a = \{1, 1\}, b = \{1, 2\}
3. a = \{1, 1\}, b = \{2, 1\}
4. a = \{1, 1\}, b = \{2, 2\}
5. a = \{1, 2\}, b = \{1, 1\}
6. a = \{1, 2\}, b = \{2, 2\}
7. a = \{2, 1\}, b = \{1, 1\}
8. a = \{2, 1\}, b = \{2, 2\}
Submitted Solution:
```
def build_maxheap(A):
n=len(A)
i=n//2
while(i>=1):
min_heapify(A,i)
i=i-1
return A
```
No
| 9,329 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
To AmShZ, all arrays are equal, but some arrays are more-equal than others. Specifically, the arrays consisting of n elements from 1 to n that can be turned into permutations of numbers from 1 to n by adding a non-negative integer to each element.
Mashtali who wants to appear in every problem statement thinks that an array b consisting of k elements is compatible with a more-equal array a consisting of n elements if for each 1 ≤ i ≤ k we have 1 ≤ b_i ≤ n and also a_{b_1} = a_{b_2} = … = a_{b_k}.
Find the number of pairs of arrays a and b such that a is a more-equal array consisting of n elements and b is an array compatible with a consisting of k elements modulo 998244353.
Note that the elements of b are not necessarily distinct, same holds for a.
Input
The first line of input contains two integers n and k (1 ≤ n ≤ 10^9 , 1 ≤ k ≤ 10^5).
Output
Print a single integer — the answer to the problem modulo 998244353.
Examples
Input
1 1
Output
1
Input
2 2
Output
8
Input
5 4
Output
50400
Input
20 100
Output
807645526
Input
10000000 10000
Output
883232350
Note
There are eight possible pairs for the second example:
1. a = \{1, 1\}, b = \{1, 1\}
2. a = \{1, 1\}, b = \{1, 2\}
3. a = \{1, 1\}, b = \{2, 1\}
4. a = \{1, 1\}, b = \{2, 2\}
5. a = \{1, 2\}, b = \{1, 1\}
6. a = \{1, 2\}, b = \{2, 2\}
7. a = \{2, 1\}, b = \{1, 1\}
8. a = \{2, 1\}, b = \{2, 2\}
Submitted Solution:
```
print(1)
```
No
| 9,330 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange, in his opinion, pattern of the forest that has n levels, where n is an even number.
In the local council you were given an area map, where the granny's house is marked by point H, parts of dense forest are marked grey (see the picture to understand better).
After a long time at home Peter decided to yield to his granny's persuasions and step out for a breath of fresh air. Being prudent, Peter plans the route beforehand. The route, that Peter considers the most suitable, has the following characteristics:
* it starts and ends in the same place — the granny's house;
* the route goes along the forest paths only (these are the segments marked black in the picture);
* the route has positive length (to step out for a breath of fresh air Peter has to cover some distance anyway);
* the route cannot cross itself;
* there shouldn't be any part of dense forest within the part marked out by this route;
You should find the amount of such suitable oriented routes modulo 1000000009.
<image>
The example of the area map for n = 12 is given in the picture. Since the map has a regular structure, you can construct it for other n by analogy using the example.
Input
The input data contain the only even integer n (2 ≤ n ≤ 106).
Output
Output the only number — the amount of Peter's routes modulo 1000000009.
Examples
Input
2
Output
10
Input
4
Output
74
Tags: combinatorics, dp
Correct Solution:
```
__author__ = 'Darren'
def solve():
n = int(input())
a, b, c, mod = 2, 2, 4, 1000000009
while n > 2:
a = (a << 1) % mod
c = c * (a - 3) % mod
b = (b + c) % mod
n -= 2
print((b * b + 1) * 2 % mod)
if __name__ == '__main__':
solve()
```
| 9,331 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange, in his opinion, pattern of the forest that has n levels, where n is an even number.
In the local council you were given an area map, where the granny's house is marked by point H, parts of dense forest are marked grey (see the picture to understand better).
After a long time at home Peter decided to yield to his granny's persuasions and step out for a breath of fresh air. Being prudent, Peter plans the route beforehand. The route, that Peter considers the most suitable, has the following characteristics:
* it starts and ends in the same place — the granny's house;
* the route goes along the forest paths only (these are the segments marked black in the picture);
* the route has positive length (to step out for a breath of fresh air Peter has to cover some distance anyway);
* the route cannot cross itself;
* there shouldn't be any part of dense forest within the part marked out by this route;
You should find the amount of such suitable oriented routes modulo 1000000009.
<image>
The example of the area map for n = 12 is given in the picture. Since the map has a regular structure, you can construct it for other n by analogy using the example.
Input
The input data contain the only even integer n (2 ≤ n ≤ 106).
Output
Output the only number — the amount of Peter's routes modulo 1000000009.
Examples
Input
2
Output
10
Input
4
Output
74
Tags: combinatorics, dp
Correct Solution:
```
n=int(input())
z=8
y=[]
for i in range(n//2-1):
y.append((z-3)%1000000009)
z*=2
z%=1000000009
if n==2:
print(10)
else:
a=4
for i in range(n//2,2,-1):
a*=y[i-3]
a+=4
a%=1000000009
a+=2
print(2*(a**2+1)%1000000009)
```
| 9,332 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange, in his opinion, pattern of the forest that has n levels, where n is an even number.
In the local council you were given an area map, where the granny's house is marked by point H, parts of dense forest are marked grey (see the picture to understand better).
After a long time at home Peter decided to yield to his granny's persuasions and step out for a breath of fresh air. Being prudent, Peter plans the route beforehand. The route, that Peter considers the most suitable, has the following characteristics:
* it starts and ends in the same place — the granny's house;
* the route goes along the forest paths only (these are the segments marked black in the picture);
* the route has positive length (to step out for a breath of fresh air Peter has to cover some distance anyway);
* the route cannot cross itself;
* there shouldn't be any part of dense forest within the part marked out by this route;
You should find the amount of such suitable oriented routes modulo 1000000009.
<image>
The example of the area map for n = 12 is given in the picture. Since the map has a regular structure, you can construct it for other n by analogy using the example.
Input
The input data contain the only even integer n (2 ≤ n ≤ 106).
Output
Output the only number — the amount of Peter's routes modulo 1000000009.
Examples
Input
2
Output
10
Input
4
Output
74
Tags: combinatorics, dp
Correct Solution:
```
a,b,c,m=1,2,4,10**9+9
n=int(input())
for i in range(1,n//2):
c=c*a%m
b=(b+c)%m
a=(2*a+3)%m
print((b*b+1)*2%m)
```
| 9,333 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange, in his opinion, pattern of the forest that has n levels, where n is an even number.
In the local council you were given an area map, where the granny's house is marked by point H, parts of dense forest are marked grey (see the picture to understand better).
After a long time at home Peter decided to yield to his granny's persuasions and step out for a breath of fresh air. Being prudent, Peter plans the route beforehand. The route, that Peter considers the most suitable, has the following characteristics:
* it starts and ends in the same place — the granny's house;
* the route goes along the forest paths only (these are the segments marked black in the picture);
* the route has positive length (to step out for a breath of fresh air Peter has to cover some distance anyway);
* the route cannot cross itself;
* there shouldn't be any part of dense forest within the part marked out by this route;
You should find the amount of such suitable oriented routes modulo 1000000009.
<image>
The example of the area map for n = 12 is given in the picture. Since the map has a regular structure, you can construct it for other n by analogy using the example.
Input
The input data contain the only even integer n (2 ≤ n ≤ 106).
Output
Output the only number — the amount of Peter's routes modulo 1000000009.
Examples
Input
2
Output
10
Input
4
Output
74
Tags: combinatorics, dp
Correct Solution:
```
# METO Bot 0.9.9
a,b,c,m=1,2,4,10**9+9
n=int(input())
for i in range(1,n//2):
c=c*a%m
b=(b+c)%m
a=(2*a+3)%m
print((b*b+1)*2%m)
```
| 9,334 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
Input
The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 ≤ ai, bi ≤ n, 0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.
Output
Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
Examples
Input
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
Output
11
Input
3
1 2 3
2
3 1 2
3 1 3
Output
-1
Note
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
Tags: dfs and similar, dsu, greedy, shortest paths
Correct Solution:
```
n = int(input())
q = list(map(int, input().split()))
m = int(input())
A = [0] * m
for i in range(m):
entry = list(map(int, input().split()))
for j in range(len(entry)):
entry[j] -= 1
A[i] = entry
e = [0] * n
for i in range(n):
e[i] = []
h = 10**6 + 2
l = [h] * n
for i in range(m):
e[A[i][1]].append(A[i][0])
l[A[i][1]] = min(l[A[i][1]], A[i][2]+1)
count = 0
for i in e:
if len(i) == 0:
count += 1
if(count > 1):
print(-1)
else:
print(sum([i for i in l if i < h]))
```
| 9,335 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
Input
The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 ≤ ai, bi ≤ n, 0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.
Output
Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
Examples
Input
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
Output
11
Input
3
1 2 3
2
3 1 2
3 1 3
Output
-1
Note
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
Tags: dfs and similar, dsu, greedy, shortest paths
Correct Solution:
```
n=int(input())
input()
m=int(input())
zp_nach=[-1]*(n+1)
for _ in range(m):
a,b,c=map(int,input().split())
if zp_nach[b]==-1:
zp_nach[b]=c
else:
zp_nach[b]=min(c,zp_nach[b])
if zp_nach.count(-1)==2:
print(sum(zp_nach)+2)
else:
print(-1)
```
| 9,336 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
Input
The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 ≤ ai, bi ≤ n, 0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.
Output
Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
Examples
Input
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
Output
11
Input
3
1 2 3
2
3 1 2
3 1 3
Output
-1
Note
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
Tags: dfs and similar, dsu, greedy, shortest paths
Correct Solution:
```
n = int(input())
lis = list(map(int,input().split()))
m = int(input())
edges = []
for d in range(m):
edges.append(list(map(int,input().split())))
edges.sort(key = lambda x: x[2])
visited = []
count = 0
for i in range(m):
if edges[i][1] not in visited:
visited.append(edges[i][1])
count += edges[i][2]
if len(visited) == n-1:
print(count)
else:
print(-1)
```
| 9,337 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
Input
The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 ≤ ai, bi ≤ n, 0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.
Output
Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
Examples
Input
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
Output
11
Input
3
1 2 3
2
3 1 2
3 1 3
Output
-1
Note
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
Tags: dfs and similar, dsu, greedy, shortest paths
Correct Solution:
```
import sys
import math
import collections
import heapq
input=sys.stdin.readline
n=int(input())
l=[int(i) for i in input().split()]
m=int(input())
s=0
d={}
for i in range(m):
a,b,c=(int(i) for i in input().split())
if(b in d):
d[b]=min(d[b],c)
else:
d[b]=c
c1=0
for i in range(1,n+1):
if(i not in d):
c1+=1
if(c1>1):
print(-1)
else:
for i in d:
s+=d[i]
print(s)
```
| 9,338 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
Input
The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 ≤ ai, bi ≤ n, 0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.
Output
Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
Examples
Input
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
Output
11
Input
3
1 2 3
2
3 1 2
3 1 3
Output
-1
Note
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
Tags: dfs and similar, dsu, greedy, shortest paths
Correct Solution:
```
# n = int(input())
#
# arr = [str(i) for i in range(n+1)]+[str(i) for i in range(n-1,-1,-1)]
#
# for i in range(n):
# print(" "*(2*(n-i))+" ".join(arr[:i+1]+arr[:i][::-1]))
#
# print(" ".join(arr))
#
# for i in range(n-1,-1,-1):
# if i == 0:
# print(" "*(2*(n-i))+" ".join(arr[:i+1]+arr[:i][::-1]), end ="")
# else:
# print(" " * (2 * (n - i)) + " ".join(arr[:i + 1] + arr[:i][::-1]))
# arr = list("olleh")
# n = input()
# for i in n:
# if i in arr[-1]:
# arr.pop()
# if not arr:
# print("YES")
# exit()2
# print("NO")
#
# def setbit(val, i, bit):
#
# num = 1 << i
#
# if bit:
# return val | num
#
# return val & ~num
#
#
# n,m = list(map(int,input().split()))
# a = int(input(),2)
# b = int(input(),2)
# for i in range(m):
# arr = input().split()
#
# if arr[0] == "set_a":
# a = setbit(a, int(arr[1]), int(arr[2]))
# elif arr[0] == "set_b":
# b = setbit(b, int(arr[1]), int(arr[2]))
# else:
# try:
# print(bin(a+b)[2:][-int(arr[1])-1],end="")
# except:
# print(0, end="")
from collections import defaultdict
n = int(input())
q = list(map(int, input().split()))
count = [0 for _ in range(n)]
m = int(input())
costs = defaultdict(list)
for _ in range(m):
a, b, c = list(map(int, input().split()))
costs[b].append(c)
count[b-1] = 1
if count.count(0) != 1:
print(-1)
exit()
total = 0
for b, cost in costs.items():
total += min(cost)
print(total)
```
| 9,339 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
Input
The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 ≤ ai, bi ≤ n, 0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.
Output
Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
Examples
Input
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
Output
11
Input
3
1 2 3
2
3 1 2
3 1 3
Output
-1
Note
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
Tags: dfs and similar, dsu, greedy, shortest paths
Correct Solution:
```
def find(arr,a):
if arr[a]==a:
return a
arr[a]=find(arr,arr[a])
return arr[a]
n=int(input())
x=list(map(int,input().split()))
arr=[i for i in range(n+1)]
m=int(input())
l=[]
for _ in range(m):
l.append(list(map(int,input().split())))
l.sort(key=lambda k:k[2])
val=n
ans=0
for i in range(m):
x=find(arr,l[i][0])
y=find(arr,l[i][1])
if x!=y and y==l[i][1]:
val-=1
arr[y]=x
ans+=l[i][2]
if val!=1:
print(-1)
else:
print(ans)
```
| 9,340 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
Input
The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 ≤ ai, bi ≤ n, 0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.
Output
Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
Examples
Input
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
Output
11
Input
3
1 2 3
2
3 1 2
3 1 3
Output
-1
Note
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
Tags: dfs and similar, dsu, greedy, shortest paths
Correct Solution:
```
n, q = int(input()), list(map(int, input().split()))
p = [1000001 for i in range(n + 1)]
for i in range(int(input())):
a, b, c = map(int, input().split())
p[b] = min(p[b], c)
k = q.index(max(q)) + 1
p = p[1: k] + p[k + 1:]
print(-1 if 1000001 in p else sum(p))
```
| 9,341 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
Input
The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 ≤ ai, bi ≤ n, 0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.
Output
Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
Examples
Input
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
Output
11
Input
3
1 2 3
2
3 1 2
3 1 3
Output
-1
Note
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
Tags: dfs and similar, dsu, greedy, shortest paths
Correct Solution:
```
MAX = 1000001
e = int(input())
q = input().split()
m = int(input())
arr1 = [MAX]*e
for i in range(m):
[a, b, c] = list(map(int, input().split()))
arr1[b-1] = min(arr1[b-1], c)
count = 0
for x in arr1:
if x == MAX:
count += 1
if count > 1:
print(-1)
else:
res = 0
for k in arr1:
if k != MAX:
res += k
print(res)
```
| 9,342 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
Input
The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 ≤ ai, bi ≤ n, 0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.
Output
Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
Examples
Input
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
Output
11
Input
3
1 2 3
2
3 1 2
3 1 3
Output
-1
Note
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
Submitted Solution:
```
read1 = [-1] * int(input())
read2 = input()
n = int(input())
for i in range(n):
x, y, z = list(map(int, input().split()))
if read1[y-1] == -1:
read1[y-1] = z
else:
read1[y-1] = min(read1[y-1],z)
if (read1.count(-1) <= 1):
print(sum(read1, 1))
else:
print(-1)
```
Yes
| 9,343 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
Input
The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 ≤ ai, bi ≤ n, 0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.
Output
Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
Examples
Input
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
Output
11
Input
3
1 2 3
2
3 1 2
3 1 3
Output
-1
Note
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
Submitted Solution:
```
n = int(input())
input()
m = int(input())
supervisor = [[] for _ in range(n)]
for i in range(m):
a, b, c = map(int, input().split())
supervisor[b-1].append(c)
boss = 0
ans = 0
for i in range(n):
if len(supervisor[i]) > 0:
ans += min(supervisor[i])
else:
boss += 1
if boss == 1:
print(ans)
else:
print(-1)
```
Yes
| 9,344 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
Input
The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 ≤ ai, bi ≤ n, 0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.
Output
Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
Examples
Input
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
Output
11
Input
3
1 2 3
2
3 1 2
3 1 3
Output
-1
Note
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
Submitted Solution:
```
n = int(input())
qualifications = list(map(int, input().split()))
apps = []
limit = (10 ** 6) + 1
result = [limit for i in range(n)]
m = int(input())
for i in range(m):
a, b, c = map(int, input().split())
result[b - 1] = min(result[b - 1], c)
count = 0
for i in result:
if i == limit:
count += 1
if count > 1:
print(-1)
else:
out = 0
for i in result:
if i != limit:
out += i
print(out)
```
Yes
| 9,345 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
Input
The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 ≤ ai, bi ≤ n, 0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.
Output
Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
Examples
Input
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
Output
11
Input
3
1 2 3
2
3 1 2
3 1 3
Output
-1
Note
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
Submitted Solution:
```
from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log, trunc
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353
def inp(): return stdin.readline().strip()
def iinp(): return int(inp())
def out(var, end="\n"): stdout.write(str(var)+"\n")
def outa(*var, end="\n"): stdout.write(' '.join(map(str, var)) + end)
def lmp(): return list(mp())
def mp(): return map(int, inp().split())
def smp(): return map(str, inp().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(m, val) for j in range(n)]
def remadd(x, y): return 1 if x%y else 0
def ceil(a,b): return (a+b-1)//b
S1 = 'abcdefghijklmnopqrstuvwxyz'
S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def isprime(x):
if x<=1: return False
if x in (2, 3): return True
if x%2 == 0: return False
for i in range(3, int(sqrt(x))+1, 2):
if x%i == 0: return False
return True
n = iinp()
arr = lmp()
sup = dd(list)
m = iinp()
for i in range(m):
p, c, w = mp()
if arr[p-1]>arr[c-1]: sup[c].append((p, w))
ans = 0
par = l1d(n+1, -1)
for k, l in sup.items():
mn = l[0][1]
par[k] = l[0][0]
for i in l:
if i[1]<mn:
mn = i[1]
par[k] = i[0]
ans += mn
c = par.count(-1)
print(ans if c==2 else -1)
```
Yes
| 9,346 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
Input
The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 ≤ ai, bi ≤ n, 0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.
Output
Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
Examples
Input
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
Output
11
Input
3
1 2 3
2
3 1 2
3 1 3
Output
-1
Note
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
Submitted Solution:
```
import sys
input = sys.stdin.readline
############ ---- Input Functions ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def getResult(n,q,m,edges,graph):
maxq = max(q)
pRoots = [i for i, j in enumerate(q) if j == maxq]
results = []
#print("possible roots : ",pRoots)
for rootI in pRoots :
cost = 0
mst = []
ees = [rootI]
pE = []
pEI = rootI
while len(mst) != n-1 :
#print("new search ....",cost)
#print("mst : ",mst)
#print("pE : " , pE)
#print("#print any key to continue...")
# input()
#add possible edges
#print("finding edges with pEI as ",pEI);
for j in range(n):
if q[pEI] > q[j] and graph[pEI][j] >= 0 :
#print("adding this new edge to pE:",[pEI,j])
pE.append([pEI,j])
#find best edge and add to mst
while len(pE) > 0 :
mE = 0
for i in range(len(pE)):
if graph[pE[mE][0]][pE[mE][1]] > graph[pE[i][0]][pE[i][1]] :
mE = i
#print("debug : found minimal edge " , pE[mE] )
#check if a parent already exists
isParentAlreadyExists = -1
isParentAlreadyExistsI = -1
for e in range(len(mst)):
if mst[e][1] == pE[mE][1] :
isParentAlreadyExists = mst[e][0]
isParentAlreadyExistsI = e
#print("is parent already exists : ",isParentAlreadyExists)
if isParentAlreadyExists == -1 :
mst.append(pE[mE])
cost = cost + graph[pE[mE][0]][pE[mE][1]]
#print("adding edge ",pE[mE]," cost improved ",graph[pE[mE][0]][pE[mE][1]])
pEI = pE[mE][1]
#print("changint pEI to ", pEI)
pE.remove(pE[mE])
break
else :
if graph[pE[mE][0]][pE[mE][1]] < graph[isParentAlreadyExists][pE[mE][1]] :
mst.append(pE[mE])
cost = cost + graph[pE[mE][0]][pE[mE][1]]
cost = cost - graph[isParentAlreadyExists][pE[mE][1]]
#print("removing edge ", mst[isParentAlreadyExistsI], " cost decreased ",graph[pE[mE][0]][pE[mE][1]])
pE.append([isParentAlreadyExists,pE[mE][1]])
#print("adding edge ", pE[mE], " cost improved ", graph[pE[mE][0]][pE[mE][1]])
mst.remove(mst[isParentAlreadyExistsI])
pEI = pE[mE][1]
#print("changint pEI to ", pEI)
pE.remove(pE[mE])
break
else:
#print("this edge is useless now.already joined ..team . ",pE[mE])
pE.remove(pE[mE])
else :
cost = -1
#print("debug : no edges in pE , cost = -1")
break
results.append(cost)
#print("results : " , results)
maxResult = max(results)
if maxResult == -1 :
return -1
for i in results :
if i != -1 :
if maxResult < i :
maxResult = i
return maxResult
if __name__ == '__main__':
n = inp()
q = inlt()
m = inp()
#print(m)
edges = []
graph = []
for i in range(n):
line = []
for j in range(n):
line.append(-1)
graph.append(line)
for i in range(m):
ed = inlt()
ed[0] = ed[0]-1
ed[1] = ed[1]-1
edges.append(ed)
#print(edges)
for u,v,c in edges:
if graph[u][v] > c :
graph[u][v] = c
else:
if graph[u][v] == -1 :
graph[u][v] = c
#print(graph)
result = getResult(n,q,m,edges,graph)
print(result)
```
No
| 9,347 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
Input
The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 ≤ ai, bi ≤ n, 0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.
Output
Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
Examples
Input
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
Output
11
Input
3
1 2 3
2
3 1 2
3 1 3
Output
-1
Note
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
Submitted Solution:
```
if __name__ == '__main__':
n = int(input())
q = [int(i) for i in input().split()]
m = int(input())
head = q.index(max(q)) + 1
graph = {}
for i in range(m):
a, b, c = input().split()
if a in graph.keys():
graph[a].append({'adj': b, 'cost': c})
else:
graph[a] = [{'adj': b, 'cost': c}]
if n == 1:
print(0)
exit()
edges= set()
if str(head) not in graph.keys():
print(-1)
exit()
vertices = set()
vertices.add(str(head))
for i in graph[str(head)]:
edges.add((i['cost'], i['adj']))
s = 0
while True:
if len(edges) == 0:
print(-1)
exit()
e= min(edges)
vertices.add(e[1])
s += int(e[0])
if len(vertices) == n:
break
if e[1] in graph.keys():
for i in graph[e[1]]:
edges.add((i['cost'], i['adj']))
edges.remove(e)
print(s)
```
No
| 9,348 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
Input
The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 ≤ ai, bi ≤ n, 0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.
Output
Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
Examples
Input
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
Output
11
Input
3
1 2 3
2
3 1 2
3 1 3
Output
-1
Note
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
Submitted Solution:
```
MAX = 1000000
e = int(input())
q = input().split()
m = int(input())
arr1 = [MAX]*e
for i in range(m):
[a, b, c] = list(map(int, input().split()))
arr1[b-1] = min(arr1[b-1], c)
count = 0
for x in arr1:
if x == MAX:
count += 1
if count > 1:
print(-1)
else:
res = 0
for k in arr1:
if k != MAX:
res += k
print(res)
```
No
| 9,349 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi.
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
Input
The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 ≤ ai, bi ≤ n, 0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.
Output
Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
Examples
Input
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
Output
11
Input
3
1 2 3
2
3 1 2
3 1 3
Output
-1
Note
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
Submitted Solution:
```
import sys
def solution():
n = int(input().strip())
input()#ikinci satir is yapmiyor
m = int(input().strip())
costs = [10**6] * (n)
for _ in range(m):
a, b, c = map(int, input().strip().split())
costs[b-1]=min(c,costs[b-1])
print(costs)
boss = 0
for i in range(n):
if costs[i] == 10**6:
boss += 1
if boss >= 2:
break
if boss != 1: #bir isin 1 patronu olur
print(-1)
return
min_cost = sum(costs) - 10**6
print(min_cost)
solution()
"""
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
"""
```
No
| 9,350 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j.
Let's call matrix A clear if no two cells containing ones have a common side.
Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1.
Let's define the sharpness of matrix A as the number of ones in it.
Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x.
Input
The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix.
Output
Print a single number — the sought value of n.
Examples
Input
4
Output
3
Input
9
Output
5
Note
The figure below shows the matrices that correspond to the samples:
<image>
Tags: constructive algorithms, dp, math
Correct Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
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")
ALPHA='abcdefghijklmnopqrstuvwxyz'
MOD=1000000007
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
# vsInput()
cap=[0,1]
for i in range(2,101):
k=ceil(i/2)
k+=k-1
cap.append(cap[-1]+k)
def possible(x):
quad=x//2
load=cap[quad]
if(quad%2==0):
for i in range(quad*2+2):
rem=n-i
if(rem%4==0 and rem//4<=load):
return True
else:
quad-=1
for i in range(quad*2+2):
rem=n-i
if(rem%4==0 and rem//4<=load):
return True
quad+=1
rem=n-ceil(quad/2)*4
if(rem%4==0 and rem//4<=load-4):
return True
return False
n=Int()
if(n==2):
print(3)
exit()
for mid in range(1,100,2):
if(possible(mid)):
print(mid)
break
```
| 9,351 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j.
Let's call matrix A clear if no two cells containing ones have a common side.
Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1.
Let's define the sharpness of matrix A as the number of ones in it.
Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x.
Input
The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix.
Output
Print a single number — the sought value of n.
Examples
Input
4
Output
3
Input
9
Output
5
Note
The figure below shows the matrices that correspond to the samples:
<image>
Tags: constructive algorithms, dp, math
Correct Solution:
```
#!/usr/bin/python3.5
x = int(input())
if x == 1:
print(1)
quit()
elif x == 2:
print(3)
quit()
elif x == 3:
print(5)
quit()
else:
if x % 2 == 0:
k = x * 2
else:
k = x * 2 - 1
for n in range(1, 16, 2):
if n ** 2 >= k:
print(n)
break
```
| 9,352 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j.
Let's call matrix A clear if no two cells containing ones have a common side.
Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1.
Let's define the sharpness of matrix A as the number of ones in it.
Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x.
Input
The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix.
Output
Print a single number — the sought value of n.
Examples
Input
4
Output
3
Input
9
Output
5
Note
The figure below shows the matrices that correspond to the samples:
<image>
Tags: constructive algorithms, dp, math
Correct Solution:
```
'''input
3
'''
from sys import stdin
import math
def make_dp():
dp = [0] * 1001
dp[1] = 1
dp[3] = 5
for i in range(5, 100, 2):
dp[i] = dp[i - 2] + i + i - 2
return dp
# main starts
x = int(stdin.readline().strip())
if x == 3:
print(5)
exit()
dp = make_dp()
for i in range(1, len(dp)):
if x <= dp[i]:
print(i)
break
```
| 9,353 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j.
Let's call matrix A clear if no two cells containing ones have a common side.
Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1.
Let's define the sharpness of matrix A as the number of ones in it.
Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x.
Input
The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix.
Output
Print a single number — the sought value of n.
Examples
Input
4
Output
3
Input
9
Output
5
Note
The figure below shows the matrices that correspond to the samples:
<image>
Tags: constructive algorithms, dp, math
Correct Solution:
```
from bisect import bisect_left
arr = [((2*i+1)**2)//2 + 1 for i in range(7)]
n = int(input())
if n == 3:
print(5)
else:
print(2*bisect_left(arr,n)+1)
```
| 9,354 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j.
Let's call matrix A clear if no two cells containing ones have a common side.
Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1.
Let's define the sharpness of matrix A as the number of ones in it.
Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x.
Input
The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix.
Output
Print a single number — the sought value of n.
Examples
Input
4
Output
3
Input
9
Output
5
Note
The figure below shows the matrices that correspond to the samples:
<image>
Tags: constructive algorithms, dp, math
Correct Solution:
```
import sys
import math
MAXNUM = math.inf
MINNUM = -1 * math.inf
ASCIILOWER = 97
ASCIIUPPER = 65
def getInt():
return int(sys.stdin.readline().rstrip())
def getInts():
return map(int, sys.stdin.readline().rstrip().split(" "))
def getString():
return sys.stdin.readline().rstrip()
def printOutput(ans):
sys.stdout.write()
pass
def solve(num):
if num == 1:
return 1
if num == 2:
return 3
if num == 3:
return 5
for i in range(3, 102, 2):
big = ((i + 1) // 2) ** 2
small = (i // 2) ** 2
if big + small >= num:
return i
def readinput():
num = getInt()
print(solve(num))
readinput()
```
| 9,355 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j.
Let's call matrix A clear if no two cells containing ones have a common side.
Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1.
Let's define the sharpness of matrix A as the number of ones in it.
Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x.
Input
The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix.
Output
Print a single number — the sought value of n.
Examples
Input
4
Output
3
Input
9
Output
5
Note
The figure below shows the matrices that correspond to the samples:
<image>
Tags: constructive algorithms, dp, math
Correct Solution:
```
n=int(input())
if(n==3):
print(5)
else:
x=1
xx=1
while(xx<n):
x+=2
xx=x*x//2+1
print(x)
```
| 9,356 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j.
Let's call matrix A clear if no two cells containing ones have a common side.
Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1.
Let's define the sharpness of matrix A as the number of ones in it.
Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x.
Input
The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix.
Output
Print a single number — the sought value of n.
Examples
Input
4
Output
3
Input
9
Output
5
Note
The figure below shows the matrices that correspond to the samples:
<image>
Tags: constructive algorithms, dp, math
Correct Solution:
```
u = int(input())
if u == 1:
print(1)
if 2 <= u <= 5:
if u == 3:
print(5)
else:
print(3)
if 6 <= u <= 13:
print(5)
if 14 <= u <= 25:
print(7)
if 26 <= u <= 41:
print(9)
if 42 <= u <= 61:
print(11)
if 62 <= u <= 85:
print(13)
if 86 <= u <= 100:
print(15)
```
| 9,357 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j.
Let's call matrix A clear if no two cells containing ones have a common side.
Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1.
Let's define the sharpness of matrix A as the number of ones in it.
Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x.
Input
The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix.
Output
Print a single number — the sought value of n.
Examples
Input
4
Output
3
Input
9
Output
5
Note
The figure below shows the matrices that correspond to the samples:
<image>
Tags: constructive algorithms, dp, math
Correct Solution:
```
n=int(input())
if n==1:i=1
elif n!=3and n<6:i=3
else:
i=5
while i*i//2+1<n:i+=2
print(i)
# Made By Mostafa_Khaled
```
| 9,358 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j.
Let's call matrix A clear if no two cells containing ones have a common side.
Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1.
Let's define the sharpness of matrix A as the number of ones in it.
Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x.
Input
The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix.
Output
Print a single number — the sought value of n.
Examples
Input
4
Output
3
Input
9
Output
5
Note
The figure below shows the matrices that correspond to the samples:
<image>
Submitted Solution:
```
def solve(n):
if n == 1:
return 1
if n == 3:
return 5
for k in range(100):
val = 4 * (((k - 1) ** 2 + 1) // 2 + (k + 1) // 2) - 3
if val >= n:
return 2 * k - 1
print(solve(int(input())))
```
Yes
| 9,359 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j.
Let's call matrix A clear if no two cells containing ones have a common side.
Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1.
Let's define the sharpness of matrix A as the number of ones in it.
Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x.
Input
The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix.
Output
Print a single number — the sought value of n.
Examples
Input
4
Output
3
Input
9
Output
5
Note
The figure below shows the matrices that correspond to the samples:
<image>
Submitted Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
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")
ALPHA='abcdefghijklmnopqrstuvwxyz'
MOD=1000000007
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
# vsInput()
def steps(x):
c=0
while(x%2==0 and x>2):
x//=2
c+=1
return c
cap=[1]
for i in range(2,101):
k=ceil(i/2)
k+=k-1
cap.append(cap[-1]+k)
# print(cap)
n=Int()
need=n//4
quad=bisect_left(cap,need)+1
extra=n%4
ans=2*quad+1
# print(need,extra,quad)
if(extra==0):
print(ans)
else:
if(extra>4*(quad//2)+1):
print(ans+2)
else:
print(ans)
```
No
| 9,360 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j.
Let's call matrix A clear if no two cells containing ones have a common side.
Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1.
Let's define the sharpness of matrix A as the number of ones in it.
Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x.
Input
The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix.
Output
Print a single number — the sought value of n.
Examples
Input
4
Output
3
Input
9
Output
5
Note
The figure below shows the matrices that correspond to the samples:
<image>
Submitted Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
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")
ALPHA='abcdefghijklmnopqrstuvwxyz'
MOD=1000000007
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
# vsInput()
def check(extra):
if(extra%2):
if(extra-1>(quad//2)*4):
return False
else:
return True
else:
if(extra>ceil(quad/2)*4):
return False
else:
return True
cap=[0,1]
for i in range(2,101):
k=ceil(i/2)
k+=k-1
cap.append(cap[-1]+k)
# print(cap)
n=Int()
for ans in range(1,100,2):
quad=cap[ans//2]
extra=n-quad*4
if(extra<0):
extra=n
# print(ans,quad,extra)
# if(extra<0):
# print(ans+2)
# exit()
if(check(extra)):
print(ans)
exit()
```
No
| 9,361 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j.
Let's call matrix A clear if no two cells containing ones have a common side.
Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1.
Let's define the sharpness of matrix A as the number of ones in it.
Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x.
Input
The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix.
Output
Print a single number — the sought value of n.
Examples
Input
4
Output
3
Input
9
Output
5
Note
The figure below shows the matrices that correspond to the samples:
<image>
Submitted Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
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")
ALPHA='abcdefghijklmnopqrstuvwxyz'
MOD=1000000007
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
# vsInput()
def check(extra):
if(extra%2):
if(extra-1>(quad//2)*4):
return False
else:
return True
else:
if(extra>ceil(quad//2)*4):
return False
else:
return True
cap=[0,1]
for i in range(2,101):
k=ceil(i/2)
k+=k-1
cap.append(cap[-1]+k)
# print(cap)
n=Int()
for ans in range(1,100,2):
quad=cap[ans//2]
extra=n-quad*4
# print(quad,extra)
if(check(extra)):
print(ans)
exit()
```
No
| 9,362 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider some square matrix A with side n consisting of zeros and ones. There are n rows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the i-row and the j-th column as Ai, j.
Let's call matrix A clear if no two cells containing ones have a common side.
Let's call matrix A symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (i, j) (1 ≤ i, j ≤ n) both of the following conditions must be met: Ai, j = An - i + 1, j and Ai, j = Ai, n - j + 1.
Let's define the sharpness of matrix A as the number of ones in it.
Given integer x, your task is to find the smallest positive integer n such that there exists a clear symmetrical matrix A with side n and sharpness x.
Input
The only line contains a single integer x (1 ≤ x ≤ 100) — the required sharpness of the matrix.
Output
Print a single number — the sought value of n.
Examples
Input
4
Output
3
Input
9
Output
5
Note
The figure below shows the matrices that correspond to the samples:
<image>
Submitted Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
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")
ALPHA='abcdefghijklmnopqrstuvwxyz'
MOD=1000000007
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
# vsInput()
cap=[0,1]
for i in range(2,101):
k=ceil(i/2)
k+=k-1
cap.append(cap[-1]+k)
def possible(x):
quad=x//2
load=cap[quad]
if(quad%2==0):
for i in range(quad*2+2):
rem=n-i
if(rem%4==0 and rem//4<=load):
return True
else:
quad-=1
for i in range(quad*2+2):
rem=n-i
if(rem%4==0 and rem//4<=load):
return True
quad+=1
rem=n-ceil(quad/2)*4
if(rem%4==0 and rem//4<=load-4):
return True
return False
n=Int()
for mid in range(1,100,2):
if(possible(mid)):
print(mid)
break
```
No
| 9,363 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.
According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.
Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.
To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.
But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array.
The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.
Output
Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Examples
Input
2
1 2
1
1
Output
1 2
Input
2
2 1
1
1
Output
2 1
Input
3
3 1 2
3
1 2 3
Output
6 6
Note
In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).
In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).
Tags: implementation
Correct Solution:
```
from collections import defaultdict
def main():
n = int(input())
a = [int(c) for c in input().split()]
m = int(input())
q = [int(c) for c in input().split()]
cache = defaultdict(list)
for i, e in enumerate(a):
cache[e].append(i)
a = b = 0
for e in q:
a += cache[e][0] + 1
b += n - cache[e][-1]
print(a, b)
if __name__ == '__main__':
main()
```
| 9,364 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.
According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.
Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.
To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.
But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array.
The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.
Output
Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Examples
Input
2
1 2
1
1
Output
1 2
Input
2
2 1
1
1
Output
2 1
Input
3
3 1 2
3
1 2 3
Output
6 6
Note
In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).
In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).
Tags: implementation
Correct Solution:
```
n=int(input())
x=list(map(int,input().split()))
d={}
for i,j in enumerate(x):
d[j]=i
a,b=0,0
q=int(input())
r=list(map(int,input().split()))
for i in r:
a+=d[i]+1
b+=n-d[i]
print(a,b)
```
| 9,365 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.
According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.
Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.
To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.
But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array.
The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.
Output
Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Examples
Input
2
1 2
1
1
Output
1 2
Input
2
2 1
1
1
Output
2 1
Input
3
3 1 2
3
1 2 3
Output
6 6
Note
In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).
In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).
Tags: implementation
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
res = [0, 0]
l = [0 for _ in range(max(a) + 1)]
for i, v in enumerate(a):
l[v] = i+1
for v in b:
res[0] += l[v]
res[1] += n+1-l[v]
print(*res)
```
| 9,366 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.
According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.
Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.
To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.
But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array.
The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.
Output
Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Examples
Input
2
1 2
1
1
Output
1 2
Input
2
2 1
1
1
Output
2 1
Input
3
3 1 2
3
1 2 3
Output
6 6
Note
In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).
In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).
Tags: implementation
Correct Solution:
```
n = int(input())
a = {}
for i,j in enumerate(input().split()):
a[j] = i+1
m = int(input())
x, y = 0, 0
for i in input().split():
z = a[i]
x += z
y += n-z+1
print(x,y)
```
| 9,367 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.
According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.
Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.
To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.
But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array.
The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.
Output
Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Examples
Input
2
1 2
1
1
Output
1 2
Input
2
2 1
1
1
Output
2 1
Input
3
3 1 2
3
1 2 3
Output
6 6
Note
In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).
In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).
Tags: implementation
Correct Solution:
```
# end=mid-1
# else:
# start=mid+1
n=int(input())
arr=list(map(int,input().split()))
q=int(input())
qar=list(map(int,input().split()))
a=0
b=0
d={}
for i in range(n):
d[arr[i]]=i+1
arr.sort()
for i in range(q):
idx=d[qar[i]]
a+=idx
b+=(n-idx)+1
print(a,b)
```
| 9,368 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.
According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.
Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.
To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.
But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array.
The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.
Output
Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Examples
Input
2
1 2
1
1
Output
1 2
Input
2
2 1
1
1
Output
2 1
Input
3
3 1 2
3
1 2 3
Output
6 6
Note
In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).
In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).
Tags: implementation
Correct Solution:
```
def approach(n, ans):
global a, ind, b, m
for i in range(m):
ans[0] += ind[b[i]]+1
ans[1] += len(a)-ind[b[i]]
return ans
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
ind = [0]*(len(a)+1)
for i in range(len(a)):
ind[a[i]] = i
ans = [0, 0]
#print(ind)
ans = approach(n, ans)
print(*ans)
```
| 9,369 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.
According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.
Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.
To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.
But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array.
The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.
Output
Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Examples
Input
2
1 2
1
1
Output
1 2
Input
2
2 1
1
1
Output
2 1
Input
3
3 1 2
3
1 2 3
Output
6 6
Note
In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).
In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).
Tags: implementation
Correct Solution:
```
n = int(input())
arr = list(map(int, input().split()))
size = 1_000_05
freq = [None] * size
freq2 = [None] * size
index = 0
for element in arr:
freq[element] = index + 1
freq2[element] = n - index
index = index + 1
m = int(input())
first = second = 0
queries = list(map(int, input().split()))
for query in queries:
first = first + freq[query]
second = second + freq2[query]
print(first, end =' ')
print(second, end ='')
```
| 9,370 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.
According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.
Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.
To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.
But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array.
The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.
Output
Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Examples
Input
2
1 2
1
1
Output
1 2
Input
2
2 1
1
1
Output
2 1
Input
3
3 1 2
3
1 2 3
Output
6 6
Note
In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).
In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).
Tags: implementation
Correct Solution:
```
'''n = int(input())
arr = list(map(int, input().split()))
nn = int(input())
arr3 = list(map(int, input().split()))
cv = 0
cp = 0
for kk in arr3:
xxx = arr.index(kk)
cv += xxx + 1
cp += n - xxx
print(cv, cp)'''
n = int(input())
a = [int(x) for x in input().split()]
m = int(input())
b = [int(x) for x in input().split()]
c = 0
d = 0
w = {}
for i in range(n):
w[a[i]] = i
for j in b:
q = w[j]
c += (q+1)
d += (n-q)
print(c, d)
#print(w)
```
| 9,371 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.
According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.
Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.
To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.
But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array.
The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.
Output
Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Examples
Input
2
1 2
1
1
Output
1 2
Input
2
2 1
1
1
Output
2 1
Input
3
3 1 2
3
1 2 3
Output
6 6
Note
In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).
In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).
Submitted Solution:
```
n=int(input())
a=map(int,input().split(" "))
m=int(input())
q=map(int,input().split(" "))
d={}
j=1
for i in a:
d[i]=j
j+=1
a=0
b=0
for i in q:
a+=d[i]
b+=n-d[i]+1
print(a,b)
```
Yes
| 9,372 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.
According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.
Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.
To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.
But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array.
The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.
Output
Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Examples
Input
2
1 2
1
1
Output
1 2
Input
2
2 1
1
1
Output
2 1
Input
3
3 1 2
3
1 2 3
Output
6 6
Note
In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).
In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).
Submitted Solution:
```
z=int(input())
n=list(map(int,input().split()))
di={}
for i in range(z):
if di.get(n[i],0)==0:
di[n[i]]=i
m=int(input())
q=list(map(int,input().split()))
ans1=0
ans2=0
for i in q:
ans1+=di[i]+1
n.reverse()
di={}
for i in range(z):
if di.get(n[i],0)==0:
di[n[i]]=i
for i in q:
ans2+=di[i]+1
print(ans1,ans2)
```
Yes
| 9,373 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.
According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.
Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.
To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.
But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array.
The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.
Output
Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Examples
Input
2
1 2
1
1
Output
1 2
Input
2
2 1
1
1
Output
2 1
Input
3
3 1 2
3
1 2 3
Output
6 6
Note
In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).
In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).
Submitted Solution:
```
'''
INPUT SHORTCUTS
N, K = map(int,input().split())
N ,A,B = map(int,input().split())
string = str(input())
arr = list(map(int,input().split()))
N = int(input())
'''
N = int(input())
arr = list(map(int,input().split()))
# arr.sort()
M = int(input())
q = list(map(int,input().split()))
dp = [-1 for _ in range(N)]
for i in range(N):
dp[arr[i]-1]=i
first = 0
last = 0
# print(dp)
for i in range(M):
first+= dp[q[i]-1]+1
# print(dp[q[i]-1]+1,N-(dp[q[i]-1]))
last += N-(dp[q[i]-1])
print(first,last)
#
```
Yes
| 9,374 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.
According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.
Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.
To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.
But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array.
The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.
Output
Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Examples
Input
2
1 2
1
1
Output
1 2
Input
2
2 1
1
1
Output
2 1
Input
3
3 1 2
3
1 2 3
Output
6 6
Note
In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).
In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).
Submitted Solution:
```
MyDict, N, X, V, P = {}, int(input()), list(map(int, input().split())), 0, 0
for i in range(N):
MyDict[X[i]] = i + 1
input()
for i in list(map(int, input().split())):
V, P = V + MyDict[i], P + N - MyDict[i] + 1
print(V, P)
# UB_CodeForces
# Advice: Falling down is an accident, staying down is a choice
# Location: Mashhad for few days
# Caption: New rank has been achieved
# CodeNumber: 703
```
Yes
| 9,375 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.
According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.
Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.
To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.
But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array.
The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.
Output
Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Examples
Input
2
1 2
1
1
Output
1 2
Input
2
2 1
1
1
Output
2 1
Input
3
3 1 2
3
1 2 3
Output
6 6
Note
In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).
In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).
Submitted Solution:
```
n=int(input())
l=list(map(int,input().split()))
m=int(input())
q=list(map(int,input().split()))
vs=[i for i in range(1,n+1)]
pt=sorted(vs,reverse=True)
av=0;ap=0
for i in q:
av+=vs[i-1]
ap+=pt[i-1]
print(av,ap)
```
No
| 9,376 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.
According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.
Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.
To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.
But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array.
The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.
Output
Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Examples
Input
2
1 2
1
1
Output
1 2
Input
2
2 1
1
1
Output
2 1
Input
3
3 1 2
3
1 2 3
Output
6 6
Note
In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).
In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).
Submitted Solution:
```
n = int(input())
L = map(int,input().split())
L = list(L)
m = int(input())
M = map(int,input().split())
M = list(M)
print(L,M,n,m)
com_v = 0
com_p = 0
for i in range(m):
com_v += L.index(M[i]) + 1
com_p += n - L.index(M[i])
print(com_v,com_p)
```
No
| 9,377 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.
According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.
Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.
To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.
But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array.
The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.
Output
Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Examples
Input
2
1 2
1
1
Output
1 2
Input
2
2 1
1
1
Output
2 1
Input
3
3 1 2
3
1 2 3
Output
6 6
Note
In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).
In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).
Submitted Solution:
```
def main():
n = int(input())
num_list = list(map(int, str(input()).split()))
m = int(input())
to_find = list(map(int, str(input()).split()))
vasya = petya = 0
to_find.sort()
for i in range(m):
if num_list[i] in to_find:
vasya += (i + 1)
petya += (n - i)
while i < (m - 1) and to_find[i] == to_find[i+1]:
i += 1
vasya += (i + 1)
petya += (n - i)
print(vasya, petya)
if __name__ == "__main__":
main()
```
No
| 9,378 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.
According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.
Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.
To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.
But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array.
The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.
Output
Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Examples
Input
2
1 2
1
1
Output
1 2
Input
2
2 1
1
1
Output
2 1
Input
3
3 1 2
3
1 2 3
Output
6 6
Note
In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).
In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).
Submitted Solution:
```
n = int(input())
arr = list(map(int, input().split(" ")))
nq = int(input())
queries = list(map(int, input().split(" ")))
l, r = 0, 0
for query in queries:
l += arr.index(query)
r = (1+n)*nq - l
print('{} {}'.format(l, r))
```
No
| 9,379 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Joe has been hurt on the Internet. Now he is storming around the house, destroying everything in his path.
Joe's house has n floors, each floor is a segment of m cells. Each cell either contains nothing (it is an empty cell), or has a brick or a concrete wall (always something one of three). It is believed that each floor is surrounded by a concrete wall on the left and on the right.
Now Joe is on the n-th floor and in the first cell, counting from left to right. At each moment of time, Joe has the direction of his gaze, to the right or to the left (always one direction of the two). Initially, Joe looks to the right.
Joe moves by a particular algorithm. Every second he makes one of the following actions:
* If the cell directly under Joe is empty, then Joe falls down. That is, he moves to this cell, the gaze direction is preserved.
* Otherwise consider the next cell in the current direction of the gaze.
* If the cell is empty, then Joe moves into it, the gaze direction is preserved.
* If this cell has bricks, then Joe breaks them with his forehead (the cell becomes empty), and changes the direction of his gaze to the opposite.
* If this cell has a concrete wall, then Joe just changes the direction of his gaze to the opposite (concrete can withstand any number of forehead hits).
Joe calms down as soon as he reaches any cell of the first floor.
The figure below shows an example Joe's movements around the house.
<image>
Determine how many seconds Joe will need to calm down.
Input
The first line contains two integers n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 104).
Next n lines contain the description of Joe's house. The i-th of these lines contains the description of the (n - i + 1)-th floor of the house — a line that consists of m characters: "." means an empty cell, "+" means bricks and "#" means a concrete wall.
It is guaranteed that the first cell of the n-th floor is empty.
Output
Print a single number — the number of seconds Joe needs to reach the first floor; or else, print word "Never" (without the quotes), if it can never happen.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3 5
..+.#
#+..+
+.#+.
Output
14
Input
4 10
...+.##+.+
+#++..+++#
++.#++++..
.+##.++#.+
Output
42
Input
2 2
..
++
Output
Never
Tags: brute force
Correct Solution:
```
import sys
from math import *
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int, minp().split())
n, m = mints()
a = list(minp())
x = 0
t = 0
d = 1
for i in range(n-1):
b = list(minp())
l = x
r = x
wall = 0
while True:
t += 1
if b[x] == '.':
break
if x + d == m or x + d < 0 or a[x+d] == '#':
wall += 1
d = -d
if wall == 2:
print("Never")
exit(0)
elif a[x+d] == '+':
wall = 0
a[x+d] = '.'
d = -d
elif l <= x+d and x+d <= r:
if d == 1:
t += r-x-1
x = r
else:
t += x-l-1
x = l
else:
x += d
r = max(r,x)
l = min(l,x)
a, b = b, a
print(t)
```
| 9,380 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Joe has been hurt on the Internet. Now he is storming around the house, destroying everything in his path.
Joe's house has n floors, each floor is a segment of m cells. Each cell either contains nothing (it is an empty cell), or has a brick or a concrete wall (always something one of three). It is believed that each floor is surrounded by a concrete wall on the left and on the right.
Now Joe is on the n-th floor and in the first cell, counting from left to right. At each moment of time, Joe has the direction of his gaze, to the right or to the left (always one direction of the two). Initially, Joe looks to the right.
Joe moves by a particular algorithm. Every second he makes one of the following actions:
* If the cell directly under Joe is empty, then Joe falls down. That is, he moves to this cell, the gaze direction is preserved.
* Otherwise consider the next cell in the current direction of the gaze.
* If the cell is empty, then Joe moves into it, the gaze direction is preserved.
* If this cell has bricks, then Joe breaks them with his forehead (the cell becomes empty), and changes the direction of his gaze to the opposite.
* If this cell has a concrete wall, then Joe just changes the direction of his gaze to the opposite (concrete can withstand any number of forehead hits).
Joe calms down as soon as he reaches any cell of the first floor.
The figure below shows an example Joe's movements around the house.
<image>
Determine how many seconds Joe will need to calm down.
Input
The first line contains two integers n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 104).
Next n lines contain the description of Joe's house. The i-th of these lines contains the description of the (n - i + 1)-th floor of the house — a line that consists of m characters: "." means an empty cell, "+" means bricks and "#" means a concrete wall.
It is guaranteed that the first cell of the n-th floor is empty.
Output
Print a single number — the number of seconds Joe needs to reach the first floor; or else, print word "Never" (without the quotes), if it can never happen.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3 5
..+.#
#+..+
+.#+.
Output
14
Input
4 10
...+.##+.+
+#++..+++#
++.#++++..
.+##.++#.+
Output
42
Input
2 2
..
++
Output
Never
Tags: brute force
Correct Solution:
```
n, m = [int(i) for i in input().split()]
current_floor = list(input())
x, t, direction = 0, 0, 1
for i in range(n-1):
floor = list(input())
l, r = x, x
wall = 0
while True:
t += 1
if floor[x] == '.':
break
if (x + direction == m) or (x + direction < 0) or (current_floor[x+direction] == '#'):
wall += 1
direction = -direction
if wall == 2:
print("Never")
exit(0)
elif current_floor[x+direction] == '+':
wall = 0
current_floor[x+direction] = '.'
direction = -direction
elif l <= x+direction and x+direction <= r:
if direction == 1:
t += r-x-1
x = r
else:
t += x-l-1
x = l
else:
x += direction
r = max(r, x)
l = min(l, x)
current_floor, floor = floor, current_floor
print(t)
```
| 9,381 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
Input
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
Output
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
Examples
Input
2 5
3 3
4 5
Output
4
Input
4 6
5 8
3 6
2 3
2 2
Output
3
Input
1 5
1 7
Output
-1
Tags: implementation
Correct Solution:
```
p=[int(n) for n in input().split()]
s=[]
for n in range(p[0]):
s.append([int(n) for n in input().split()])
k=p[1]
op=[]
for n in range(p[0]):
if s[n][1]>k:
op.append(s[n][0]-s[n][1]+k)
else:
op.append(s[n][0])
# print(op[n],s[n])
#print(op,s)
print(max(op))
```
| 9,382 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
Input
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
Output
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
Examples
Input
2 5
3 3
4 5
Output
4
Input
4 6
5 8
3 6
2 3
2 2
Output
3
Input
1 5
1 7
Output
-1
Tags: implementation
Correct Solution:
```
n,k=map(int,input().split())
answer=None
for i in range(n):
f,t=map(int,input().split())
answeri=f-max(t-k,0)
if i==0 or answer<answeri:
answer=answeri
print(answer)
```
| 9,383 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
Input
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
Output
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
Examples
Input
2 5
3 3
4 5
Output
4
Input
4 6
5 8
3 6
2 3
2 2
Output
3
Input
1 5
1 7
Output
-1
Tags: implementation
Correct Solution:
```
n,v=map(int,(input().split()))
a=[list(int(a) for a in (input().split())) for i in range(n)]
l=[]
k=0
for i in a:
if i[1]<=v:
l.append(i[0])
else:l.append(i[0]-i[1]+v)
print(max(l))
```
| 9,384 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
Input
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
Output
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
Examples
Input
2 5
3 3
4 5
Output
4
Input
4 6
5 8
3 6
2 3
2 2
Output
3
Input
1 5
1 7
Output
-1
Tags: implementation
Correct Solution:
```
n, k = map(int, input().strip().split())
l = list()
for i in range(0, n):
f, t = map(int, input().strip().split())
if t > k: l.append(f-t+k);
else: l.append(f)
l.sort(reverse =True);
print(l[0]);
```
| 9,385 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
Input
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
Output
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
Examples
Input
2 5
3 3
4 5
Output
4
Input
4 6
5 8
3 6
2 3
2 2
Output
3
Input
1 5
1 7
Output
-1
Tags: implementation
Correct Solution:
```
n,k = map(int,input().split())
l = []
for i in range(n):
f,t = map(int,input().split())
if t>k:
l.append(f-(t-k))
else:
l.append(f)
print(max(l))
```
| 9,386 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
Input
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
Output
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
Examples
Input
2 5
3 3
4 5
Output
4
Input
4 6
5 8
3 6
2 3
2 2
Output
3
Input
1 5
1 7
Output
-1
Tags: implementation
Correct Solution:
```
n,k=map(int,input().split())
a=[]
b=[]
for i in range(n):
a1,b1=map(int,input().split())
a.append(a1)
b.append(b1)
if(b[0]>k):
ans=a[0]-(b[0]-k)
else:
ans=a[0]
for i in range(1,n):
if(b[i]>k):
temp=a[i]-(b[i]-k)
else:
temp=a[i]
ans=max(ans,temp)
print(ans)
```
| 9,387 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
Input
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
Output
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
Examples
Input
2 5
3 3
4 5
Output
4
Input
4 6
5 8
3 6
2 3
2 2
Output
3
Input
1 5
1 7
Output
-1
Tags: implementation
Correct Solution:
```
n,k = input().split()
n,k = int(n) ,int(k)
f = []
t = []
for i in range(n):
tmp1,tmp2 = input().split()
tmp1,tmp2 = int(tmp1) ,int(tmp2)
f.append(tmp1)
t.append(tmp2)
res = -9999999999999999
for i in range(n):
if t[i] >= k:
tmp = f[i] - (t[i] - k)
else:
tmp = f[i]
res = max(res, tmp)
print(res)
```
| 9,388 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
Input
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
Output
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
Examples
Input
2 5
3 3
4 5
Output
4
Input
4 6
5 8
3 6
2 3
2 2
Output
3
Input
1 5
1 7
Output
-1
Tags: implementation
Correct Solution:
```
def joy(f: int, t: int, k: int) -> int:
return f - (t - k) if t > k else f
(n, k) = map(int, input().split())
joys = [0] * n
for i in range(n):
(f, t) = map(int, input().split())
joys[i] = joy(f, t, k)
print(max(joys))
```
| 9,389 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
Input
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
Output
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
Examples
Input
2 5
3 3
4 5
Output
4
Input
4 6
5 8
3 6
2 3
2 2
Output
3
Input
1 5
1 7
Output
-1
Submitted Solution:
```
n,k=map(int,input().split())
M=-1000000000
while n:
f,t=map(int,input().split())
if(k<t):
f-=(t-k)
if(f>M):
M=f
n-=1
print(M)
```
Yes
| 9,390 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
Input
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
Output
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
Examples
Input
2 5
3 3
4 5
Output
4
Input
4 6
5 8
3 6
2 3
2 2
Output
3
Input
1 5
1 7
Output
-1
Submitted Solution:
```
n,k=map(int,input().split())
m=-10000000000
for i in range(n):
f,t=map(int,input().split())
if(t<k):
m=max(m,f)
else:
m=max(m,f-(t-k))
print(m)
```
Yes
| 9,391 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
Input
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
Output
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
Examples
Input
2 5
3 3
4 5
Output
4
Input
4 6
5 8
3 6
2 3
2 2
Output
3
Input
1 5
1 7
Output
-1
Submitted Solution:
```
x,y =[int(x) for x in input("").split()]
fun=[]
for i in range(x):
p,q=[int(x) for x in input("").split()]
if(q>y):
fun.append(p-(q-y))
else:
fun.append(p)
print(max(fun))
```
Yes
| 9,392 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
Input
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
Output
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
Examples
Input
2 5
3 3
4 5
Output
4
Input
4 6
5 8
3 6
2 3
2 2
Output
3
Input
1 5
1 7
Output
-1
Submitted Solution:
```
def max(a,b):
if a>b:
return a
else:
return b
l=list(map(int,input().split(' ')))
n,k,m=l[0],l[1],-100000000000
for i in range(n):
z=list((map(int,input().split(' '))))
if z[1]>k:
m=max(m,(z[0]-(z[1]-k)))
else:
m=max(m,z[0])
print(m)
```
Yes
| 9,393 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
Input
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
Output
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
Examples
Input
2 5
3 3
4 5
Output
4
Input
4 6
5 8
3 6
2 3
2 2
Output
3
Input
1 5
1 7
Output
-1
Submitted Solution:
```
i=input().split()
n,k=int(i[0]),int(i[1])
max_joy = -1
while n>0:
i=input().split()
fi,ti = int(i[0]),int(i[1])
joy = -1
if ti>k:
joy = fi-(ti-k)
else:
joy = fi
if joy > max_joy:
max_joy=joy
n-=1
print(max_joy)
```
No
| 9,394 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
Input
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
Output
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
Examples
Input
2 5
3 3
4 5
Output
4
Input
4 6
5 8
3 6
2 3
2 2
Output
3
Input
1 5
1 7
Output
-1
Submitted Solution:
```
a, b = map(int, input().rstrip().split(" "))
max_joy = -99999999
while a:
f, t = map(int, input().rstrip().split(" "))
if t > b:
joy = (f - (t-b))
else:
joy = f
if joy> max_joy:
max_joy = joy
a-=1
print(max_joy)
```
No
| 9,395 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
Input
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
Output
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
Examples
Input
2 5
3 3
4 5
Output
4
Input
4 6
5 8
3 6
2 3
2 2
Output
3
Input
1 5
1 7
Output
-1
Submitted Solution:
```
import sys
my_file = sys.stdin
#my_file = open("input.txt", "r")
num = [int(i) for i in my_file.readline().strip("\n").split(" ")]
n = num[0]
k = num[1]
rest = []
max = -1000000000
for i in my_file.readlines():
rest = [int(k) for k in i.strip("\n").split(" ")]
if max <= rest[0]:
if rest[1] <= k:
max = rest[0]
else:
max = rest[0]-(rest[1]-k)
print(max)
```
No
| 9,396 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.
The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
Input
The first line contains two space-separated integers — n (1 ≤ n ≤ 104) and k (1 ≤ k ≤ 109) — the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers — fi (1 ≤ fi ≤ 109) and ti (1 ≤ ti ≤ 109) — the characteristics of the i-th restaurant.
Output
In a single line print a single integer — the maximum joy value that the Rabbits will get from the lunch.
Examples
Input
2 5
3 3
4 5
Output
4
Input
4 6
5 8
3 6
2 3
2 2
Output
3
Input
1 5
1 7
Output
-1
Submitted Solution:
```
n,k = map(int,input().split());M = -1000000000
for _ in ' '*n:
f,t = map(int,input().split());M = max(M,f-(t-k))if t>=k else max(M,t)
print(M)
```
No
| 9,397 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Yaroslav, Andrey and Roman can play cubes for hours and hours. But the game is for three, so when Roman doesn't show up, Yaroslav and Andrey play another game.
Roman leaves a word for each of them. Each word consists of 2·n binary characters "0" or "1". After that the players start moving in turns. Yaroslav moves first. During a move, a player must choose an integer from 1 to 2·n, which hasn't been chosen by anybody up to that moment. Then the player takes a piece of paper and writes out the corresponding character from his string.
Let's represent Yaroslav's word as s = s1s2... s2n. Similarly, let's represent Andrey's word as t = t1t2... t2n. Then, if Yaroslav choose number k during his move, then he is going to write out character sk on the piece of paper. Similarly, if Andrey choose number r during his move, then he is going to write out character tr on the piece of paper.
The game finishes when no player can make a move. After the game is over, Yaroslav makes some integer from the characters written on his piece of paper (Yaroslav can arrange these characters as he wants). Andrey does the same. The resulting numbers can contain leading zeroes. The person with the largest number wins. If the numbers are equal, the game ends with a draw.
You are given two strings s and t. Determine the outcome of the game provided that Yaroslav and Andrey play optimally well.
Input
The first line contains integer n (1 ≤ n ≤ 106). The second line contains string s — Yaroslav's word. The third line contains string t — Andrey's word.
It is guaranteed that both words consist of 2·n characters "0" and "1".
Output
Print "First", if both players play optimally well and Yaroslav wins. If Andrey wins, print "Second" and if the game ends with a draw, print "Draw". Print the words without the quotes.
Examples
Input
2
0111
0001
Output
First
Input
3
110110
001001
Output
First
Input
3
111000
000111
Output
Draw
Input
4
01010110
00101101
Output
First
Input
4
01100000
10010011
Output
Second
Tags: games, greedy
Correct Solution:
```
#!/usr/bin/python3
n = int(input())
s = list(input())
l = list(input())
a = len([_ for _ in zip(s, l) if _ == ('1', '1')])
b = len([_ for _ in zip(s, l) if _ == ('1', '0')])
c = len([_ for _ in zip(s, l) if _ == ('0', '1')])
f = b + (a + 1) // 2
s = c + a // 2
if f > s:
print('First')
elif f + 1 < s:
print('Second')
else:
print('Draw')
```
| 9,398 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Yaroslav, Andrey and Roman can play cubes for hours and hours. But the game is for three, so when Roman doesn't show up, Yaroslav and Andrey play another game.
Roman leaves a word for each of them. Each word consists of 2·n binary characters "0" or "1". After that the players start moving in turns. Yaroslav moves first. During a move, a player must choose an integer from 1 to 2·n, which hasn't been chosen by anybody up to that moment. Then the player takes a piece of paper and writes out the corresponding character from his string.
Let's represent Yaroslav's word as s = s1s2... s2n. Similarly, let's represent Andrey's word as t = t1t2... t2n. Then, if Yaroslav choose number k during his move, then he is going to write out character sk on the piece of paper. Similarly, if Andrey choose number r during his move, then he is going to write out character tr on the piece of paper.
The game finishes when no player can make a move. After the game is over, Yaroslav makes some integer from the characters written on his piece of paper (Yaroslav can arrange these characters as he wants). Andrey does the same. The resulting numbers can contain leading zeroes. The person with the largest number wins. If the numbers are equal, the game ends with a draw.
You are given two strings s and t. Determine the outcome of the game provided that Yaroslav and Andrey play optimally well.
Input
The first line contains integer n (1 ≤ n ≤ 106). The second line contains string s — Yaroslav's word. The third line contains string t — Andrey's word.
It is guaranteed that both words consist of 2·n characters "0" and "1".
Output
Print "First", if both players play optimally well and Yaroslav wins. If Andrey wins, print "Second" and if the game ends with a draw, print "Draw". Print the words without the quotes.
Examples
Input
2
0111
0001
Output
First
Input
3
110110
001001
Output
First
Input
3
111000
000111
Output
Draw
Input
4
01010110
00101101
Output
First
Input
4
01100000
10010011
Output
Second
Submitted Solution:
```
#!/usr/bin/python3
n = int(input())
s = list(input())
l = list(input())
a = len([_ for _ in zip(s, l) if _ == ('1', '1')])
b = len([_ for _ in zip(s, l) if _ == ('1', '0')])
c = len([_ for _ in zip(s, l) if _ == ('0', '1')])
f = b + (a + 1) // 2
s = c + a // 2
if f > s:
print('First')
elif f < s:
print('Second')
else:
print('Draw')
```
No
| 9,399 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.