message stringlengths 2 67k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 463 109k | cluster float64 19 19 | __index_level_0__ int64 926 217k |
|---|---|---|---|---|---|
Provide a correct Python 3 solution for this coding contest problem.
We have N cards numbered 1, 2, ..., N. Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side. Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.
Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.
* Choose an integer i (1 \leq i \leq N - 1). Swap the i-th and (i+1)-th cards from the left, then flip these two cards.
Constraints
* 1 \leq N \leq 18
* 1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
Output
If it is impossible to have a non-decreasing sequence, print `-1`. If it is possible, print the minimum number of operations required to achieve it.
Examples
Input
3
3 4 3
3 2 3
Output
1
Input
2
2 1
1 2
Output
-1
Input
4
1 2 3 4
5 6 7 8
Output
0
Input
5
28 15 22 43 31
20 22 43 33 32
Output
-1
Input
5
4 46 6 38 43
33 15 18 27 37
Output
3 | instruction | 0 | 43,578 | 19 | 87,156 |
"Correct Solution:
```
# Binary Indexed Tree (Fenwick Tree)
class BIT():
"""一点加算、区間取得クエリをそれぞれO(logN)で答える
add: i番目にvalを加える
get_sum: 区間[l, r)の和を求める
i, l, rは0-indexed
"""
def __init__(self, n):
self.n = n
self.bit = [0] * (n + 1)
def _sum(self, i):
s = 0
while i > 0:
s += self.bit[i]
i -= i & -i
return s
def add(self, i, val):
"""i番目にvalを加える"""
i = i + 1
while i <= self.n:
self.bit[i] += val
i += i & -i
def get_sum(self, l, r):
"""区間[l, r)の和を求める"""
return self._sum(r) - self._sum(l)
from operator import itemgetter
from collections import deque
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 10**18
for bit_state in range(1 << n):
pattern = [[] for i in range(2)]
for i in range(n):
if bit_state & (1 << i) and i % 2 == 0:
pattern[0].append(a[i]*100 + i)
elif bit_state & (1 << i) and i % 2 == 1:
pattern[1].append(a[i]*100 + i)
elif i % 2 == 0:
pattern[1].append(b[i]*100 + i)
else:
pattern[0].append(b[i]*100 + i)
if len(pattern[0]) == len(pattern[1]) or len(pattern[0]) - 1 == len(pattern[1]):
for i in range(2):
pattern[i] = sorted(pattern[i])
bit = BIT(n)
ans_tmp = 0
prev_num = 10**8
for i in range(n)[::-1]:
pal = i % 2
num = pattern[pal].pop()
ind = num % 100
num = num // 100
if prev_num < num:
break
prev_num = num
bit.add(ind, 1)
ans_tmp += bit.get_sum(0, ind)
else:
ans = min(ans, ans_tmp)
if ans == 10**18:
print(-1)
else:
print(ans)
``` | output | 1 | 43,578 | 19 | 87,157 |
Provide a correct Python 3 solution for this coding contest problem.
We have N cards numbered 1, 2, ..., N. Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side. Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.
Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.
* Choose an integer i (1 \leq i \leq N - 1). Swap the i-th and (i+1)-th cards from the left, then flip these two cards.
Constraints
* 1 \leq N \leq 18
* 1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
Output
If it is impossible to have a non-decreasing sequence, print `-1`. If it is possible, print the minimum number of operations required to achieve it.
Examples
Input
3
3 4 3
3 2 3
Output
1
Input
2
2 1
1 2
Output
-1
Input
4
1 2 3 4
5 6 7 8
Output
0
Input
5
28 15 22 43 31
20 22 43 33 32
Output
-1
Input
5
4 46 6 38 43
33 15 18 27 37
Output
3 | instruction | 0 | 43,579 | 19 | 87,158 |
"Correct Solution:
```
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
n = int(input())
a = [int(item) for item in input().split()]
b = [int(item) for item in input().split()]
odd_bit = 0b0101010101010101010101
eve_bit = 0b1010101010101010101010
full_bit = 2**n - 1
odd_num = (n + 1) // 2
ans = 10**9
for i in range(2**n):
invi = full_bit ^ i
odd = bin(i & odd_bit).count("1") + bin(invi & eve_bit).count("1")
if odd != odd_num:
continue
odd_card = []
eve_card = []
for j in range(n):
if i & 1 << j:
if j % 2 == 0:
odd_card.append((a[j], j))
else:
eve_card.append((a[j], j))
else:
if j % 2 == 0:
eve_card.append((b[j], j))
else:
odd_card.append((b[j], j))
odd_card.sort()
eve_card.sort()
odd_itr = 0
eve_itr = 0
curr = 0
val = 0
ok = True
perm = []
for i in range(n):
if i % 2 == 0:
num, index = odd_card[odd_itr]
perm.append(index)
odd_itr += 1
else:
num, index = eve_card[eve_itr]
perm.append(index)
eve_itr += 1
if num < curr:
ok = False
break
curr = num
if ok:
val = 0
visited = [0] * n
itr = 0
for item in perm:
visited[item] = 1
val += itr - sum(visited[:item])
itr += 1
ans = min(ans, val)
if ans == 10**9:
print(-1)
else:
print(ans)
``` | output | 1 | 43,579 | 19 | 87,159 |
Provide a correct Python 3 solution for this coding contest problem.
We have N cards numbered 1, 2, ..., N. Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side. Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.
Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.
* Choose an integer i (1 \leq i \leq N - 1). Swap the i-th and (i+1)-th cards from the left, then flip these two cards.
Constraints
* 1 \leq N \leq 18
* 1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
Output
If it is impossible to have a non-decreasing sequence, print `-1`. If it is possible, print the minimum number of operations required to achieve it.
Examples
Input
3
3 4 3
3 2 3
Output
1
Input
2
2 1
1 2
Output
-1
Input
4
1 2 3 4
5 6 7 8
Output
0
Input
5
28 15 22 43 31
20 22 43 33 32
Output
-1
Input
5
4 46 6 38 43
33 15 18 27 37
Output
3 | instruction | 0 | 43,580 | 19 | 87,160 |
"Correct Solution:
```
import sys
input=sys.stdin.readline
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=10**9
def bubble_sort(arr):
change = True
num=0
while change:
change = False
for i in range(len(arr) - 1):
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
change = True
num+=1
return num
for i in range(2**n):
even=[]
odd=[]
for j in range(n):
x=(i>>j)%2
if j%2==0:
if x==0:
even.append((a[j],j))
else:
odd.append((b[j],j))
else:
if x==0:
odd.append((a[j],j))
else:
even.append((b[j],j))
if len(odd)!=n//2:
continue
even.sort()
odd.sort()
check=True
order=[]
new=[]
for j in range(n//2):
order.append(even[j][1])
order.append(odd[j][1])
new.append(even[j][0])
new.append(odd[j][0])
if n%2!=0:
new.append(even[-1][0])
order.append(even[-1][1])
for j in range(n-1):
if new[j]>new[j+1]:
check=False
if not check:
continue
x=bubble_sort(order)
ans=min(ans,x)
if ans==10**9:
print(-1)
else:
print(ans)
``` | output | 1 | 43,580 | 19 | 87,161 |
Provide a correct Python 3 solution for this coding contest problem.
We have N cards numbered 1, 2, ..., N. Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side. Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.
Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.
* Choose an integer i (1 \leq i \leq N - 1). Swap the i-th and (i+1)-th cards from the left, then flip these two cards.
Constraints
* 1 \leq N \leq 18
* 1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
Output
If it is impossible to have a non-decreasing sequence, print `-1`. If it is possible, print the minimum number of operations required to achieve it.
Examples
Input
3
3 4 3
3 2 3
Output
1
Input
2
2 1
1 2
Output
-1
Input
4
1 2 3 4
5 6 7 8
Output
0
Input
5
28 15 22 43 31
20 22 43 33 32
Output
-1
Input
5
4 46 6 38 43
33 15 18 27 37
Output
3 | instruction | 0 | 43,581 | 19 | 87,162 |
"Correct Solution:
```
from itertools import combinations
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
def inversion(inds):
bit = [0] * (N+1)
def bit_add(x,w):
while x <= N:
bit[x] += w
x += (x & -x)
def bit_sum(x):
ret = 0
while x > 0:
ret += bit[x]
x -= (x & -x)
return ret
inv = 0
for ind in reversed(inds):
inv += bit_sum(ind + 1)
bit_add(ind + 1, 1)
return inv
INF = float('inf')
ans = INF
for odd_idxs in combinations(range(N), N//2):
odd_idxset = set(odd_idxs)
odds = []
evens = []
for i in range(N):
if i in odd_idxset:
odds.append((A[i] if i%2 else B[i], i))
else:
evens.append((B[i] if i%2 else A[i], i))
odds.sort()
evens.sort()
vals = []
inds = []
for (e,ei),(o,oi) in zip(evens, odds):
vals.append(e)
vals.append(o)
inds.append(ei)
inds.append(oi)
if len(evens) > len(odds):
v,i = evens[-1]
vals.append(v)
inds.append(i)
if any(a>b for a,b in zip(vals,vals[1:])): continue
ans = min(ans, inversion(inds))
print(-1 if ans==INF else ans)
``` | output | 1 | 43,581 | 19 | 87,163 |
Provide a correct Python 3 solution for this coding contest problem.
We have N cards numbered 1, 2, ..., N. Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side. Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.
Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.
* Choose an integer i (1 \leq i \leq N - 1). Swap the i-th and (i+1)-th cards from the left, then flip these two cards.
Constraints
* 1 \leq N \leq 18
* 1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
Output
If it is impossible to have a non-decreasing sequence, print `-1`. If it is possible, print the minimum number of operations required to achieve it.
Examples
Input
3
3 4 3
3 2 3
Output
1
Input
2
2 1
1 2
Output
-1
Input
4
1 2 3 4
5 6 7 8
Output
0
Input
5
28 15 22 43 31
20 22 43 33 32
Output
-1
Input
5
4 46 6 38 43
33 15 18 27 37
Output
3 | instruction | 0 | 43,582 | 19 | 87,164 |
"Correct Solution:
```
import itertools
class Bit:
def __init__(self, n):
self.size = n
self.tree = [0] * (n + 1)
def sum(self, i):
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def add(self, i, x):
while i <= self.size:
self.tree[i] += x
i += i & -i
def invnum(n,a):
bit=Bit(n)
res=0
for j in range(n):
res+=j-bit.sum(a[j])
bit.add(a[j],1)
N=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
flag=0
M=-1
if N%2==0:
M=N//2
else:
M=N//2+1
ans=(N*(N-1))//2
for seq in itertools.combinations(range(N),M):
X=[]
Y=[]
era=[0 for i in range(N)]
for i in seq:
if i%2==0:
X.append((A[i],i))
else:
X.append((B[i],i))
era[i]=1
for i in range(N):
if era[i]==0:
if i%2==0:
Y.append((B[i],i))
else:
Y.append((A[i],i))
X.sort()
Y.sort()
L=[]
if N%2==1:
for i in range(M-1):
L.append(X[i])
L.append(Y[i])
L.append(X[-1])
else:
for i in range(M):
L.append(X[i])
L.append(Y[i])
#print(L)
flag2=1
for i in range(N-1):
if L[i][0]>L[i+1][0]:
flag2=0
break
if flag2==1:
flag=1
S=[L[i][1] for i in range(N)]
tmp=0
for i in range(N):
for j in range(i+1,N):
if S[i]>S[j]:
tmp+=1
ans=min(tmp,ans)
if flag==1:
print(ans)
else:
print(-1)
``` | output | 1 | 43,582 | 19 | 87,165 |
Provide a correct Python 3 solution for this coding contest problem.
We have N cards numbered 1, 2, ..., N. Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side. Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.
Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.
* Choose an integer i (1 \leq i \leq N - 1). Swap the i-th and (i+1)-th cards from the left, then flip these two cards.
Constraints
* 1 \leq N \leq 18
* 1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
Output
If it is impossible to have a non-decreasing sequence, print `-1`. If it is possible, print the minimum number of operations required to achieve it.
Examples
Input
3
3 4 3
3 2 3
Output
1
Input
2
2 1
1 2
Output
-1
Input
4
1 2 3 4
5 6 7 8
Output
0
Input
5
28 15 22 43 31
20 22 43 33 32
Output
-1
Input
5
4 46 6 38 43
33 15 18 27 37
Output
3 | instruction | 0 | 43,583 | 19 | 87,166 |
"Correct Solution:
```
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = N ** 2
for i in range(1 << N):
L = [[] for _ in range(N)]
T = [0] * N
t = i
for j in range(N):
if t & 1:
L[j] = [A[j], 1]
T[j] = A[j]
else:
L[j] = [B[j], 0]
T[j] = B[j]
t >>= 1
D = [0] * N
T.sort()
cnt = 0
for j in range(N):
c = 0
for k in range(N):
if D[k] == 0:
if L[j][0] == T[k] and (j + 1 - L[j][1]) % 2 == k % 2:
D[k] = 1
cnt += j - c
break
else:
c += 1
else:
break
else:
ans = min(ans, cnt)
if ans == N ** 2:
print(-1)
else:
print(ans)
``` | output | 1 | 43,583 | 19 | 87,167 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N cards numbered 1, 2, ..., N. Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side. Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.
Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.
* Choose an integer i (1 \leq i \leq N - 1). Swap the i-th and (i+1)-th cards from the left, then flip these two cards.
Constraints
* 1 \leq N \leq 18
* 1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
Output
If it is impossible to have a non-decreasing sequence, print `-1`. If it is possible, print the minimum number of operations required to achieve it.
Examples
Input
3
3 4 3
3 2 3
Output
1
Input
2
2 1
1 2
Output
-1
Input
4
1 2 3 4
5 6 7 8
Output
0
Input
5
28 15 22 43 31
20 22 43 33 32
Output
-1
Input
5
4 46 6 38 43
33 15 18 27 37
Output
3
Submitted Solution:
```
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
import pprint
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007
n = I()
A = LI()
B = LI()
ans = INF
for odd_ind in combinations(range(n), n // 2):
even_ind = set(range(n)) - set(odd_ind)
odd_list = []
even_list = []
whole_num_list = []
whole_idx_list = []
for i in odd_ind:
if i % 2:
odd_list += [(A[i], i)]
else:
odd_list += [(B[i], i)]
for j in even_ind:
if j % 2:
even_list += [(B[j], j)]
else:
even_list += [(A[j], j)]
even_list.sort(reverse=1)
odd_list.sort(reverse=1)
while even_list or odd_list:
x, idx = even_list.pop()
whole_num_list += [x]
whole_idx_list += [idx]
if odd_list:
x, idx = odd_list.pop()
whole_num_list += [x]
whole_idx_list += [idx]
if whole_num_list == sorted(whole_num_list):
cnt = 0
for i in range(n):
for j in range(i + 1, n):
if whole_idx_list[i] > whole_idx_list[j]:
cnt += 1
ans = min(ans, cnt)
if ans == INF:
print(-1)
else:
print(ans)
``` | instruction | 0 | 43,584 | 19 | 87,168 |
Yes | output | 1 | 43,584 | 19 | 87,169 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N cards numbered 1, 2, ..., N. Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side. Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.
Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.
* Choose an integer i (1 \leq i \leq N - 1). Swap the i-th and (i+1)-th cards from the left, then flip these two cards.
Constraints
* 1 \leq N \leq 18
* 1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
Output
If it is impossible to have a non-decreasing sequence, print `-1`. If it is possible, print the minimum number of operations required to achieve it.
Examples
Input
3
3 4 3
3 2 3
Output
1
Input
2
2 1
1 2
Output
-1
Input
4
1 2 3 4
5 6 7 8
Output
0
Input
5
28 15 22 43 31
20 22 43 33 32
Output
-1
Input
5
4 46 6 38 43
33 15 18 27 37
Output
3
Submitted Solution:
```
import itertools
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
N=[int(i) for i in range(n)]
nn=2**n
ans=float("inf")
for _ in range(n//2+1):
for com in itertools.combinations(N,2*_):
i=0
for ura in com:
i+=2**ura
E,O=[],[]
for j in range(n):
if i&(1<<j)==0:
if j%2==0:
E.append(A[j]*100+j)
else:
O.append(A[j]*100+j)
else:
if j%2==0:
O.append(B[j]*100+j)
else:
E.append(B[j]*100+j)
if len(E)!=(n+1)//2:
continue
else:
E.sort()
O.sort()
chk=True
C=[E[0]%100]
prev=E[0]//100
for i in range(1,n):
ii=i//2
if i%2==0:
C.append(E[ii]%100)
if E[ii]//100<prev:
chk=False
break
else:
prev=E[ii]//100
else:
C.append(O[ii]%100)
if O[ii]//100<prev:
chk=False
break
else:
prev=O[ii]//100
if chk:
ten=0
for i1 in range(n):
for i2 in range(i1+1,n):
if C[i1]>C[i2]:
ten+=1
ans=min(ans,ten)
print(-1 if ans==float("inf") else ans)
``` | instruction | 0 | 43,585 | 19 | 87,170 |
Yes | output | 1 | 43,585 | 19 | 87,171 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N cards numbered 1, 2, ..., N. Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side. Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.
Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.
* Choose an integer i (1 \leq i \leq N - 1). Swap the i-th and (i+1)-th cards from the left, then flip these two cards.
Constraints
* 1 \leq N \leq 18
* 1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
Output
If it is impossible to have a non-decreasing sequence, print `-1`. If it is possible, print the minimum number of operations required to achieve it.
Examples
Input
3
3 4 3
3 2 3
Output
1
Input
2
2 1
1 2
Output
-1
Input
4
1 2 3 4
5 6 7 8
Output
0
Input
5
28 15 22 43 31
20 22 43 33 32
Output
-1
Input
5
4 46 6 38 43
33 15 18 27 37
Output
3
Submitted Solution:
```
import sys
import itertools
import math
def first(i,a,b):
if i%2==0: return [a,b,i]
else: return [b,a,i]
def chk(a,b):
if m==l:
for i in range(m-1):
if a[i][0]>b[i][0] or b[i][0]>a[i+1][0]:
return 0
if a[m-1][0]>b[m-1][0]: return 0
else:
for i in range(l):
if a[i][0]>b[i][0] or b[i][0]>a[i+1][0]:
return 0
return 1
def cal(a,b):
w=[]
if m==l:
for i in range(l):
w+=[a[i][1],b[i][1]]
else:
for i in range(l):
w+=[a[i][1],b[i][1]]
w.append(a[m-1][1])
return w
n=int(input())
m,l=n-n//2,n//2
a=list(map(int,input().split()))
b=list(map(int,input().split()))
if n==1:
print(0)
sys.exit()
x=[first(i,a[i],b[i]) for i in range(n)]
p=[i for i in range(n)]
ans=1000000
for q in itertools.combinations(p,m):
tmpa,tmpb=[],[]
for i in range(n):
if i in q: tmpa.append([ x[i][0],x[i][2] ])
else: tmpb.append([x[i][1],x[i][2]])
tmpa.sort()
tmpb.sort()
if chk(tmpa,tmpb)==1:
t=cal(tmpa,tmpb)
tans=0
for i in range(n):
for j in range(i):
if t[j]>t[i]: tans+=1
ans=min(ans,tans)
print(ans if ans<=n*(n-1)/2 else -1)
``` | instruction | 0 | 43,586 | 19 | 87,172 |
Yes | output | 1 | 43,586 | 19 | 87,173 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N cards numbered 1, 2, ..., N. Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side. Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.
Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.
* Choose an integer i (1 \leq i \leq N - 1). Swap the i-th and (i+1)-th cards from the left, then flip these two cards.
Constraints
* 1 \leq N \leq 18
* 1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
Output
If it is impossible to have a non-decreasing sequence, print `-1`. If it is possible, print the minimum number of operations required to achieve it.
Examples
Input
3
3 4 3
3 2 3
Output
1
Input
2
2 1
1 2
Output
-1
Input
4
1 2 3 4
5 6 7 8
Output
0
Input
5
28 15 22 43 31
20 22 43 33 32
Output
-1
Input
5
4 46 6 38 43
33 15 18 27 37
Output
3
Submitted Solution:
```
n = int(input())
al = list(map(int,input().split()))
bl = list(map(int,input().split()))
def bubble_sort(lst):
a = 0
for i in range(len(lst)-1):
for j in range(len(lst)-1, i, -1):
if lst[j-1] > lst[j]:
tmp = lst[j]
lst[j] = lst[j-1]
lst[j-1] = tmp
a += 1
return a
anss = float("inf")
for i in range(2 ** n):
bits = [0] * n
for j in range(n):
if ((i >> j) & 1):
bits[j] = 1
even_odd = [[], []]
for idx, bit in enumerate(bits):
if bit == 0:
even_odd[(idx + bit) % 2].append((al[idx], idx))
else:
even_odd[(idx + bit) % 2].append((bl[idx], idx))
if len(even_odd[0]) - len(even_odd[1]) != n % 2:
continue
aal = sorted(even_odd[0])
bbl = sorted(even_odd[1])
goto = []
now = 0
for k in range(n):
if k % 2 == 0:
tmp, idx = aal.pop(0)
if now <= tmp:
goto+= [idx]
now = tmp
else:
break
else:
tmp, idx = bbl.pop(0)
if now <= tmp:
goto+= [idx]
now = tmp
else:
break
else:
ans = bubble_sort(goto)
anss = min(anss, ans)
if anss == float('inf'):
print(-1)
else:
print(anss)
``` | instruction | 0 | 43,587 | 19 | 87,174 |
Yes | output | 1 | 43,587 | 19 | 87,175 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N cards numbered 1, 2, ..., N. Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side. Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.
Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.
* Choose an integer i (1 \leq i \leq N - 1). Swap the i-th and (i+1)-th cards from the left, then flip these two cards.
Constraints
* 1 \leq N \leq 18
* 1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
Output
If it is impossible to have a non-decreasing sequence, print `-1`. If it is possible, print the minimum number of operations required to achieve it.
Examples
Input
3
3 4 3
3 2 3
Output
1
Input
2
2 1
1 2
Output
-1
Input
4
1 2 3 4
5 6 7 8
Output
0
Input
5
28 15 22 43 31
20 22 43 33 32
Output
-1
Input
5
4 46 6 38 43
33 15 18 27 37
Output
3
Submitted Solution:
```
import sys
def solve():
input = sys.stdin.readline
N = int(input())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
minmove = 1000000000
for i in range(2 ** N):
b = i
back = 0
Num = [None] * N
original = dict()
for j in range(N):
if b % 2 == 0:
n = A[j]
else:
n = B[j]
back += 1
if n not in original: original[n] = {j}
else: original[n] |= {j}
Num[j] = n
b >>= 1
if back % 2 > 0: continue
Num.sort() #実現したい数列
appeared = set()
move = 0
for j, n in enumerate(Num): #このソートが実現できるか
for o in sorted(original[n]):
if abs(j - o) % 2 == 0 and A[o] == n:
original[n] -= {o}
for key in appeared:
if key > o: move += 1
appeared |= {o}
break
elif abs(j - o) % 2 == 1 and B[o] == n:
original[n] -= {o}
for key in appeared:
if key > o: move += 1
appeared |= {o}
break
else: #実現できない
break
else: #実現できるため、操作回数を求める
minmove = min(minmove, move)
if minmove == 1000000000: print(-1)
else: print(minmove)
return 0
if __name__ == "__main__":
solve()
``` | instruction | 0 | 43,588 | 19 | 87,176 |
No | output | 1 | 43,588 | 19 | 87,177 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N cards numbered 1, 2, ..., N. Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side. Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.
Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.
* Choose an integer i (1 \leq i \leq N - 1). Swap the i-th and (i+1)-th cards from the left, then flip these two cards.
Constraints
* 1 \leq N \leq 18
* 1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
Output
If it is impossible to have a non-decreasing sequence, print `-1`. If it is possible, print the minimum number of operations required to achieve it.
Examples
Input
3
3 4 3
3 2 3
Output
1
Input
2
2 1
1 2
Output
-1
Input
4
1 2 3 4
5 6 7 8
Output
0
Input
5
28 15 22 43 31
20 22 43 33 32
Output
-1
Input
5
4 46 6 38 43
33 15 18 27 37
Output
3
Submitted Solution:
```
import sys
sys.setrecursionlimit(10000)
n = int(input())
a_lis = list(map(int, input().split()))
b_lis = list(map(int, input().split()))
used = []
res = []
def rec(a_lis, b_lis, l):
found = True
pre = -1
for a in a_lis:
if a < pre:
found = False
break
pre = a
if found:
res.append(l)
return
used.append((a_lis, b_lis))
for i in range(len(a_lis) - 1):
na_lis = a_lis[:i] + [b_lis[i + 1]] + [b_lis[i]] + a_lis[i + 2:]
nb_lis = b_lis[:i] + [a_lis[i + 1]] + [a_lis[i]] + b_lis[i + 2:]
if (na_lis, nb_lis) not in used:
rec(na_lis, nb_lis, l + 1)
rec(a_lis, b_lis, 0)
if res:
print(min(res))
else:
print(-1)
``` | instruction | 0 | 43,589 | 19 | 87,178 |
No | output | 1 | 43,589 | 19 | 87,179 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N cards numbered 1, 2, ..., N. Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side. Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.
Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.
* Choose an integer i (1 \leq i \leq N - 1). Swap the i-th and (i+1)-th cards from the left, then flip these two cards.
Constraints
* 1 \leq N \leq 18
* 1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
Output
If it is impossible to have a non-decreasing sequence, print `-1`. If it is possible, print the minimum number of operations required to achieve it.
Examples
Input
3
3 4 3
3 2 3
Output
1
Input
2
2 1
1 2
Output
-1
Input
4
1 2 3 4
5 6 7 8
Output
0
Input
5
28 15 22 43 31
20 22 43 33 32
Output
-1
Input
5
4 46 6 38 43
33 15 18 27 37
Output
3
Submitted Solution:
```
from collections import defaultdict
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
ans = 1000
for n in range(2**N):
n = bin(n)[2:]
if n.count('1')%2!=0:
continue
n = '0'*(N-len(n))+n
l = []
for i in range(N):
if n[i]=='0':
l.append([A[i],i])
else:
l.append([B[i],i+1])
inversion = 0
for i in range(N):
if inversion>=ans:
break
for j in range(i+1,N):
if l[i][0]>l[j][0]:
inversion += 1
if inversion>=ans:
continue
l.sort()
d = defaultdict(list)
for i in range(N):
v,j = l[i]
if (i-j)%2==1:
d[v].append(i)
possible = True
for v in d:
if len(d[v])%2==1:
possible = False
else:
for i in range(len(d[v])//2):
if (d[v][2*i+1]-d[v][2*i])%2==1:
inversion += d[v][2*i+1]-d[v][2*i]
else:
possible = False
break
if not possible:
break
if possible:
ans = min(ans,inversion)
if ans == 1000:
ans = -1
print(ans)
``` | instruction | 0 | 43,590 | 19 | 87,180 |
No | output | 1 | 43,590 | 19 | 87,181 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N cards numbered 1, 2, ..., N. Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side. Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.
Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.
* Choose an integer i (1 \leq i \leq N - 1). Swap the i-th and (i+1)-th cards from the left, then flip these two cards.
Constraints
* 1 \leq N \leq 18
* 1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
Output
If it is impossible to have a non-decreasing sequence, print `-1`. If it is possible, print the minimum number of operations required to achieve it.
Examples
Input
3
3 4 3
3 2 3
Output
1
Input
2
2 1
1 2
Output
-1
Input
4
1 2 3 4
5 6 7 8
Output
0
Input
5
28 15 22 43 31
20 22 43 33 32
Output
-1
Input
5
4 46 6 38 43
33 15 18 27 37
Output
3
Submitted Solution:
```
from itertools import permutations
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
minc = 19
num = [i for i in range(N-1)]
for i in range(N-1):
for j in permutations(num,i):
now = 0
flag = True
count = len(j)
A2 = A.copy()
B2 = B.copy()
for k in j:
A2[k],A2[k+1],B2[k],B2[k+1] = B2[k+1],B2[k],A2[k+1],A2[k]
for j in A2:
if now <= j:
now = j
else:
flag = False
break
if flag:
minc = min(minc,count)
if minc != 19:
print(minc)
else:
print(-1)
``` | instruction | 0 | 43,591 | 19 | 87,182 |
No | output | 1 | 43,591 | 19 | 87,183 |
Provide a correct Python 3 solution for this coding contest problem.
Problem
You decide to play a weird game with your friend A, who loves gathering.
Given a set S of non-negative integers consisting of n elements that can be duplicated. Each element contained in the set S is a non-negative pi-ary number. With Steps 1 to 3 below as one turn for the set S, you and your opponent repeat the turn alternately. In each turn, operate in the order of this number.
Step 1: Remove all the elements contained in the set S once, convert each removed element to a binary number, erase 0, divide it into one or more consecutive 1s, and divide the divided part. Add all to set S.
Step 2: If Set S is empty at this time, the player currently playing this turn loses.
Step 3: Select one element a contained in the set S, and subtract any integer x that satisfies 1 ≤ x ≤ a from that a.
The figure below is an example of how to proceed for the first turn given the following inputs.
Four
10 3
2 11
16 5
16 AE
<image>
The first is your turn. When both parties do their best for a given input, do you really win?
Input
The input is given in the following format:
n
p1 m1
p2 m2
...
pi mi
...
pn mn
N pi-ary numbers mi, which are elements of the set, are given. The number n of elements in the set does not exceed 105. Also, 2 ≤ pi ≤ 62 is satisfied. The pi-ary number mi uses 52 letters of the alphabet in addition to the usual numbers 0-9. The order is 012 ... 789 ABC ... XYZabc ... xyz in ascending order, and the characters up to the i-th are used. Also, the pi-ary mi does not exceed 218-1 in decimal.
Output
Output win if you win against the given input, lose if you lose.
Examples
Input
1
10 1
Output
win
Input
2
10 1
10 1
Output
lose
Input
3
25 53AI
43 3BI0
62 pn6
Output
win | instruction | 0 | 43,744 | 19 | 87,488 |
"Correct Solution:
```
# AOJ 1518: Last One
# Python3 2018.7.13 bal4u
ans = 0;
for i in range(int(input())):
p = input().split();
if len(p) == 1: ms = []
else: ms = list(p[1])
s = 0
for m in ms:
if m.isdigit(): s += int(m)
elif m.isupper(): s += ord(m)-ord('A')+10
else: s += ord(m)-ord('a')+36
ans ^= s
print("win" if ans else "lose")
``` | output | 1 | 43,744 | 19 | 87,489 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Problem
You decide to play a weird game with your friend A, who loves gathering.
Given a set S of non-negative integers consisting of n elements that can be duplicated. Each element contained in the set S is a non-negative pi-ary number. With Steps 1 to 3 below as one turn for the set S, you and your opponent repeat the turn alternately. In each turn, operate in the order of this number.
Step 1: Remove all the elements contained in the set S once, convert each removed element to a binary number, erase 0, divide it into one or more consecutive 1s, and divide the divided part. Add all to set S.
Step 2: If Set S is empty at this time, the player currently playing this turn loses.
Step 3: Select one element a contained in the set S, and subtract any integer x that satisfies 1 ≤ x ≤ a from that a.
The figure below is an example of how to proceed for the first turn given the following inputs.
Four
10 3
2 11
16 5
16 AE
<image>
The first is your turn. When both parties do their best for a given input, do you really win?
Input
The input is given in the following format:
n
p1 m1
p2 m2
...
pi mi
...
pn mn
N pi-ary numbers mi, which are elements of the set, are given. The number n of elements in the set does not exceed 105. Also, 2 ≤ pi ≤ 62 is satisfied. The pi-ary number mi uses 52 letters of the alphabet in addition to the usual numbers 0-9. The order is 012 ... 789 ABC ... XYZabc ... xyz in ascending order, and the characters up to the i-th are used. Also, the pi-ary mi does not exceed 218-1 in decimal.
Output
Output win if you win against the given input, lose if you lose.
Examples
Input
1
10 1
Output
win
Input
2
10 1
10 1
Output
lose
Input
3
25 53AI
43 3BI0
62 pn6
Output
win
Submitted Solution:
```
# AOJ 1518: Last One
# Python3 2018.7.13 bal4u
ans = 0;
n = int(input())
for i in range(n):
p, ms = input().split();
ms = list(ms)
s = 0
for m in ms:
if m.isdigit(): s += int(m)
elif m.isupper(): s += ord(m)-ord('A')+10
else: s += ord(m)-ord('a')+36
ans ^= s
print("win" if ans else "lose")
``` | instruction | 0 | 43,745 | 19 | 87,490 |
No | output | 1 | 43,745 | 19 | 87,491 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left).
<image>
Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).
Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower.
The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower.
Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
Output
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
Examples
Input
3
6
3 2
5 4
2 4
Output
YES
Input
3
3
2 6
4 1
5 3
Output
NO | instruction | 0 | 44,108 | 19 | 88,216 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
x = int(input())
x = 7 - x
for i in range(n):
left, right = map(int, input().split())
if x == left or x == 7 - left or x == right or x == 7 - right:
print('NO')
exit()
else:
x = 7 - x
print('YES')
``` | output | 1 | 44,108 | 19 | 88,217 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left).
<image>
Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).
Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower.
The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower.
Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
Output
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
Examples
Input
3
6
3 2
5 4
2 4
Output
YES
Input
3
3
2 6
4 1
5 3
Output
NO | instruction | 0 | 44,109 | 19 | 88,218 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
x = int(input())
dice = []
for i in range(n):
dice.append(list(map(int,input().split())))
ans = "YES"
for i in range(n):
dice[i].append(7-dice[i][0])
dice[i].append(7-dice[i][1])
for i in range(n):
if 7-x in dice[i]:
ans = "NO"
break
print(ans)
``` | output | 1 | 44,109 | 19 | 88,219 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left).
<image>
Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).
Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower.
The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower.
Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
Output
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
Examples
Input
3
6
3 2
5 4
2 4
Output
YES
Input
3
3
2 6
4 1
5 3
Output
NO | instruction | 0 | 44,110 | 19 | 88,220 |
Tags: constructive algorithms, greedy
Correct Solution:
```
# def hidden_nums():
# left, right = map(int, input().split())
# seen = set([left, right, 7-left, 7-right])
# hidden = [i for i in range(1, 7) if i not in seen]
# return hidden
# def solve():
# n, top = int(input()), int(input())
# bottom = [7-top]
# count = 0
# for i in range(n-1):
# hidden = hidden_nums()
# for b in bottom:
# if b in hidden: hidden.remove(7-b)
# bottom = hidden
# if len(bottom) == 2:
# count+=1
# if top in hidden_nums() and count == 0:
# return 'YES'
# return 'NO'
# print(solve())
def hidden_nums():
left, right = map(int, input().split())
seen = set([left, right, 7-left, 7-right])
hidden = [i for i in range(1, 7) if i not in seen]
return hidden
def solve():
n, top = int(input()), int(input())
bottom = [7-top]
count = 0
for i in range(n):
hidden = hidden_nums()
if top not in hidden and bottom not in hidden:
return 'NO'
# for b in bottom:
# if b in hidden: hidden.remove(7-b)
# bottom = hidden
# if len(bottom) == 2:
# count+=1
# if top in hidden_nums() and count == 0:
# return 'YES'
return 'YES'
print(solve())
``` | output | 1 | 44,110 | 19 | 88,221 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left).
<image>
Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).
Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower.
The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower.
Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
Output
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
Examples
Input
3
6
3 2
5 4
2 4
Output
YES
Input
3
3
2 6
4 1
5 3
Output
NO | instruction | 0 | 44,111 | 19 | 88,222 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n= int(input())
x= int(input())
f_arr=[0]*7
arr=[]
cnt=0
for i in range(n):
a,b=map(int,input().split())
arr.append(a)
arr.append(b)
for j in arr:
if j!=x and j!=(7-x):
cnt+=1
if cnt==2*n:
print("YES")
else:
print("NO")
``` | output | 1 | 44,111 | 19 | 88,223 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left).
<image>
Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).
Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower.
The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower.
Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
Output
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
Examples
Input
3
6
3 2
5 4
2 4
Output
YES
Input
3
3
2 6
4 1
5 3
Output
NO | instruction | 0 | 44,112 | 19 | 88,224 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n=int(input())
x=int(input())
tmp= True
for i in range(n) :
a , b = map(int , input().split())
if x == a or x == b or a == 7 - x or b == 7 - x:
tmp = False
print(['NO' , 'YES'][tmp])
``` | output | 1 | 44,112 | 19 | 88,225 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left).
<image>
Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).
Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower.
The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower.
Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
Output
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
Examples
Input
3
6
3 2
5 4
2 4
Output
YES
Input
3
3
2 6
4 1
5 3
Output
NO | instruction | 0 | 44,113 | 19 | 88,226 |
Tags: constructive algorithms, greedy
Correct Solution:
```
#ismailmoussi
n=int(input())
lfou9=int(input())
input()
for i in range(n-1):
li=list(map(int,input().split()))
if lfou9 in li or 7-lfou9 in li:
print("NO")
exit()
print("YES")
``` | output | 1 | 44,113 | 19 | 88,227 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left).
<image>
Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).
Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower.
The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower.
Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
Output
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
Examples
Input
3
6
3 2
5 4
2 4
Output
YES
Input
3
3
2 6
4 1
5 3
Output
NO | instruction | 0 | 44,114 | 19 | 88,228 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
f = []
x = int(input())
while n:
n -= 1
t1, t2 = map(int, input().split())
f.append(t1)
f.append(t2)
if x in f:
print('NO')
elif 7-x in f:
print('NO')
else:
print('YES')
``` | output | 1 | 44,114 | 19 | 88,229 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left).
<image>
Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).
Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower.
The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower.
Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
Output
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
Examples
Input
3
6
3 2
5 4
2 4
Output
YES
Input
3
3
2 6
4 1
5 3
Output
NO | instruction | 0 | 44,115 | 19 | 88,230 |
Tags: constructive algorithms, greedy
Correct Solution:
```
a=int(input())
b=int(input())
c=set()
for i in range(a):
d,e=map(int, input().split())
for j in range(1, 7):
if j!=d and j!=e and j!=7-d and j!=7-e:
c.add(j)
if len(c)==2:
print("YES")
else:
print("NO")
``` | output | 1 | 44,115 | 19 | 88,231 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left).
<image>
Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).
Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower.
The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower.
Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
Output
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
Examples
Input
3
6
3 2
5 4
2 4
Output
YES
Input
3
3
2 6
4 1
5 3
Output
NO
Submitted Solution:
```
n = int(input())
x = int(input())
p = 7 - x
for i in range(n):
l,r = map(int,input().split())
f = set([1,2,3,4,5,6])
f.remove(l); f.remove(r); f.remove(7-l); f.remove(7-r)
if not p in f:
print('NO')
exit()
else:
f.remove(p)
p = f.pop()
print('YES')
``` | instruction | 0 | 44,116 | 19 | 88,232 |
Yes | output | 1 | 44,116 | 19 | 88,233 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left).
<image>
Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).
Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower.
The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower.
Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
Output
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
Examples
Input
3
6
3 2
5 4
2 4
Output
YES
Input
3
3
2 6
4 1
5 3
Output
NO
Submitted Solution:
```
n=int(input())
x=int(input())
ans=True
for _ in range(n):
a,b=map(int,input().split())
if a==x or b==x or a==7-x or b==7-x:
ans=False
break
print('YES' if ans else 'NO')
``` | instruction | 0 | 44,117 | 19 | 88,234 |
Yes | output | 1 | 44,117 | 19 | 88,235 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left).
<image>
Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).
Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower.
The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower.
Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
Output
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
Examples
Input
3
6
3 2
5 4
2 4
Output
YES
Input
3
3
2 6
4 1
5 3
Output
NO
Submitted Solution:
```
a=int(input())
b=int(input())
x=[1,2,3,4,5,6]
z=0
top = b
bottom=7-top
c,d=map(int, input().split())
for i in range(a-1):
c,d=map(int, input().split())
e=7-c
f=7-d
x.remove(c)
x.remove(d)
x.remove(e)
x.remove(f)
if bottom not in x:
z+=1
break
top = bottom
bottom=7-top
x=[1,2,3,4,5,6]
if z==0:
print("YES")
else:
print("NO")
``` | instruction | 0 | 44,118 | 19 | 88,236 |
Yes | output | 1 | 44,118 | 19 | 88,237 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left).
<image>
Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).
Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower.
The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower.
Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
Output
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
Examples
Input
3
6
3 2
5 4
2 4
Output
YES
Input
3
3
2 6
4 1
5 3
Output
NO
Submitted Solution:
```
n=int(input())
x=int(input())
a,b=map(int,input().split())
for i in range(n-1):
a,b=map(int,input().split())
s,t,u,v=a,b,7-a,7-b
if(x==s or x==t or x==u or x==v):
print("NO")
exit()
print("YES")
``` | instruction | 0 | 44,119 | 19 | 88,238 |
Yes | output | 1 | 44,119 | 19 | 88,239 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left).
<image>
Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).
Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower.
The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower.
Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
Output
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
Examples
Input
3
6
3 2
5 4
2 4
Output
YES
Input
3
3
2 6
4 1
5 3
Output
NO
Submitted Solution:
```
n=int(input())
t=int(input())
l=[]
r=[]
for i in range(n):
a,b=list(map(int,input().split()))
l.append(a)
r.append(b)
if sum(l)==10:
print("YES")
else:
print("NO")
``` | instruction | 0 | 44,120 | 19 | 88,240 |
No | output | 1 | 44,120 | 19 | 88,241 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left).
<image>
Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).
Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower.
The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower.
Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
Output
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
Examples
Input
3
6
3 2
5 4
2 4
Output
YES
Input
3
3
2 6
4 1
5 3
Output
NO
Submitted Solution:
```
n = int(input())
top = int(input())
if n == 1 :
print("YES")
else :
arr = []
for i in range(n) :
c = input()
c = c.split(' ')
arr.append(c[0])
arr.append(c[1])
arr.sort()
bool = False
for i in range(len(arr) -1) :
if arr[i] == arr[i+1] :
bool = True
if bool == True :
print("YES")
else :
print("NO")
``` | instruction | 0 | 44,121 | 19 | 88,242 |
No | output | 1 | 44,121 | 19 | 88,243 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left).
<image>
Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).
Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower.
The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower.
Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
Output
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
Examples
Input
3
6
3 2
5 4
2 4
Output
YES
Input
3
3
2 6
4 1
5 3
Output
NO
Submitted Solution:
```
# def hidden_nums():
# left, right = map(int, input().split())
# seen = set([left, right, 7-left, 7-right])
# hidden = [i for i in range(1, 7) if i not in seen]
# return hidden
# def solve():
# n, top = int(input()), int(input())
# bottom = [7-top]
# count = 0
# for i in range(n-1):
# hidden = hidden_nums()
# for b in bottom:
# if b in hidden: hidden.remove(7-b)
# bottom = hidden
# if len(bottom) == 2:
# count+=1
# if top in hidden_nums() and count == 0:
# return 'YES'
# return 'NO'
# print(solve())
def hidden_nums():
left, right = map(int, input().split())
seen = set([left, right, 7-left, 7-right])
hidden = [i for i in range(1, 7) if i not in seen]
return hidden
def solve():
n, top = int(input()), int(input())
bottom = [7-top]
count = 0
for i in range(n-1):
hidden = hidden_nums()
if top not in hidden and bottom not in hidden:
return 'NO'
# for b in bottom:
# if b in hidden: hidden.remove(7-b)
# bottom = hidden
# if len(bottom) == 2:
# count+=1
# if top in hidden_nums() and count == 0:
# return 'YES'
return 'YES'
print(solve())
``` | instruction | 0 | 44,122 | 19 | 88,244 |
No | output | 1 | 44,122 | 19 | 88,245 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left).
<image>
Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).
Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower.
The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower.
Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
Output
Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes).
Examples
Input
3
6
3 2
5 4
2 4
Output
YES
Input
3
3
2 6
4 1
5 3
Output
NO
Submitted Solution:
```
n = int(input())
x = int(input())
good = True
for i in range(n):
a, b = map(int, input().split())
if (a == 1 or b == 1) and i != 0:
good = False
break
if good:
print('YES')
else:
print('NO')
``` | instruction | 0 | 44,123 | 19 | 88,246 |
No | output | 1 | 44,123 | 19 | 88,247 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a new TV game on BerTV. In this game two players get a number A consisting of 2n digits. Before each turn players determine who will make the next move. Each player should make exactly n moves. On it's turn i-th player takes the leftmost digit of A and appends it to his or her number Si. After that this leftmost digit is erased from A. Initially the numbers of both players (S1 and S2) are «empty». Leading zeroes in numbers A, S1, S2 are allowed. In the end of the game the first player gets S1 dollars, and the second gets S2 dollars.
One day Homer and Marge came to play the game. They managed to know the number A beforehand. They want to find such sequence of their moves that both of them makes exactly n moves and which maximizes their total prize. Help them.
Input
The first line contains integer n (1 ≤ n ≤ 18). The second line contains integer A consisting of exactly 2n digits. This number can have leading zeroes.
Output
Output the line of 2n characters «H» and «M» — the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactly n moves. If there are several solutions, output any of them.
Examples
Input
2
1234
Output
HHMM
Input
2
9911
Output
HMHM | instruction | 0 | 44,146 | 19 | 88,292 |
Tags: dp
Correct Solution:
```
n=int(input())
a=input()
b=''
c=0
d=0
f=0
g=f-c
while c<n:
g=f-c
d=f+c
if g==0:
f+=1
b+='H'
else:
e=d
for j in range(d,min(n+c+1,2*n)):
if int(a[j])>int(a[e]):
e=j
if e!=d:
b+='H'
f+=1
else:
k=d
while k<=2*n-1 and a[k]==a[e]:
k+=1
p=0
l=d+1
while l<n+c+p+1 and l<2*n:
if int(a[l])>=int(a[e]):
p+=1
if p>=g+1:
b+='H'
f+=1
break
l+=1
else:
b+='M'
c+=1
print(b)
``` | output | 1 | 44,146 | 19 | 88,293 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a new TV game on BerTV. In this game two players get a number A consisting of 2n digits. Before each turn players determine who will make the next move. Each player should make exactly n moves. On it's turn i-th player takes the leftmost digit of A and appends it to his or her number Si. After that this leftmost digit is erased from A. Initially the numbers of both players (S1 and S2) are «empty». Leading zeroes in numbers A, S1, S2 are allowed. In the end of the game the first player gets S1 dollars, and the second gets S2 dollars.
One day Homer and Marge came to play the game. They managed to know the number A beforehand. They want to find such sequence of their moves that both of them makes exactly n moves and which maximizes their total prize. Help them.
Input
The first line contains integer n (1 ≤ n ≤ 18). The second line contains integer A consisting of exactly 2n digits. This number can have leading zeroes.
Output
Output the line of 2n characters «H» and «M» — the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactly n moves. If there are several solutions, output any of them.
Examples
Input
2
1234
Output
HHMM
Input
2
9911
Output
HMHM | instruction | 0 | 44,147 | 19 | 88,294 |
Tags: dp
Correct Solution:
```
n = int(input())
s = input()
n *= 2
d = [[-1 for i in range(n + 2)] for j in range(n + 2)]
prev = [[0 for i in range(n + 2)] for j in range(n + 2)]
d[0][0] = 1
for i in range(n):
for j in range(min(i, n // 2) + 1):
if d[i + 1][j + 1] < d[i][j] + int(s[i]) * 10 ** (n // 2 - j - 1):
prev[i + 1][j + 1] = j
d[i + 1][j + 1] = d[i][j] + int(s[i]) * 10 ** (n // 2 - j - 1)
if d[i + 1][j] < d[i][j] + int(s[i]) * 10 ** (n // 2 - (i - j) - 1):
prev[i + 1][j] = j
d[i + 1][j] = d[i][j] + int(s[i]) * 10 ** (n // 2 - (i - j) - 1)
ans = []
i = n
j = n // 2
while i > 0:
if prev[i][j] == j:
ans.append('H')
else:
ans.append('M')
j = prev[i][j]
i -= 1
print(''.join(ans)[::-1])
``` | output | 1 | 44,147 | 19 | 88,295 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a new TV game on BerTV. In this game two players get a number A consisting of 2n digits. Before each turn players determine who will make the next move. Each player should make exactly n moves. On it's turn i-th player takes the leftmost digit of A and appends it to his or her number Si. After that this leftmost digit is erased from A. Initially the numbers of both players (S1 and S2) are «empty». Leading zeroes in numbers A, S1, S2 are allowed. In the end of the game the first player gets S1 dollars, and the second gets S2 dollars.
One day Homer and Marge came to play the game. They managed to know the number A beforehand. They want to find such sequence of their moves that both of them makes exactly n moves and which maximizes their total prize. Help them.
Input
The first line contains integer n (1 ≤ n ≤ 18). The second line contains integer A consisting of exactly 2n digits. This number can have leading zeroes.
Output
Output the line of 2n characters «H» and «M» — the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactly n moves. If there are several solutions, output any of them.
Examples
Input
2
1234
Output
HHMM
Input
2
9911
Output
HMHM | instruction | 0 | 44,148 | 19 | 88,296 |
Tags: dp
Correct Solution:
```
import sys
from array import array # noqa: F401
from collections import Counter
def input():
return sys.stdin.buffer.readline().decode('utf-8')
n = int(input())
s = input().rstrip()
pre_bit, pre_sum = Counter(), Counter()
suf_bit, suf_sum = Counter(), Counter()
for bit in range(1, (1 << n) - 1):
s1, s2 = '', ''
cnt1, cnt2 = 0, 0
for i in range(n):
if (1 << i) & bit:
s1 += s[i]
cnt1 += 1
else:
s2 += s[i]
cnt2 += 1
s1 += '0' * (n - cnt1)
s2 += '0' * (n - cnt2)
val = int(s1) + int(s2)
if pre_sum[cnt1, cnt2] <= val:
pre_sum[cnt1, cnt2] = val
pre_bit[cnt1, cnt2] = bit
for bit in range(1, (1 << n) - 1):
s1, s2 = '', ''
cnt1, cnt2 = 0, 0
for i in range(n):
if (1 << i) & bit:
s1 += s[i + n]
cnt1 += 1
else:
s2 += s[i + n]
cnt2 += 1
val = int(s1) + int(s2)
if suf_sum[cnt1, cnt2] <= val:
suf_sum[cnt1, cnt2] = val
suf_bit[cnt1, cnt2] = bit
ans_val = int(s[:n]) + int(s[n:])
ans_bit = 'H' * n + 'M' * n
for (p1, p2) in pre_bit.keys():
if ans_val < pre_sum[p1, p2] + suf_sum[p2, p1]:
ans_val = pre_sum[p1, p2] + suf_sum[p2, p1]
pbit = ''.join('H' if (1 << i) & pre_bit[p1, p2] else 'M' for i in range(n))
sbit = ''.join('H' if (1 << i) & suf_bit[p2, p1] else 'M' for i in range(n))
ans_bit = pbit + sbit
print(ans_bit)
``` | output | 1 | 44,148 | 19 | 88,297 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a new TV game on BerTV. In this game two players get a number A consisting of 2n digits. Before each turn players determine who will make the next move. Each player should make exactly n moves. On it's turn i-th player takes the leftmost digit of A and appends it to his or her number Si. After that this leftmost digit is erased from A. Initially the numbers of both players (S1 and S2) are «empty». Leading zeroes in numbers A, S1, S2 are allowed. In the end of the game the first player gets S1 dollars, and the second gets S2 dollars.
One day Homer and Marge came to play the game. They managed to know the number A beforehand. They want to find such sequence of their moves that both of them makes exactly n moves and which maximizes their total prize. Help them.
Input
The first line contains integer n (1 ≤ n ≤ 18). The second line contains integer A consisting of exactly 2n digits. This number can have leading zeroes.
Output
Output the line of 2n characters «H» and «M» — the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactly n moves. If there are several solutions, output any of them.
Examples
Input
2
1234
Output
HHMM
Input
2
9911
Output
HMHM | instruction | 0 | 44,149 | 19 | 88,298 |
Tags: dp
Correct Solution:
```
n = int(input())
s = input()
mat = [[[int(0),int(0),""] for _i in range(n+1)] for _j in range(n+1)]
for i in range(2*n):
digit = int(s[i])
toIter = min(i,n)
minh = max(i-n,0)
maxh = min(n,i)
for m in range(minh,maxh+1):
h = i - m
v = mat[h][m]
if h < n:
#add current symbol to homer
vnext = mat[h+1][m];
sumhmn = vnext[0]*pow(10,(2*n-h-2)) + vnext[1]*pow(10,(2*n-m-1));
sumhm = (v[0]*10+digit)*pow(10,(2*n-h-2)) + v[1]*pow(10,(2*n-m-1));
if sumhm >= sumhmn :
vnext[0]=v[0]*10+digit
vnext[1]=v[1]
vnext[2]=v[2]+'H'
if m < n:
#add current symbol to marge
vnext = mat[h][m+1];
sumhmn = vnext[0]*pow(10,(2*n-h-1)) + vnext[1]*pow(10,(2*n-m-2));
sumhm = v[0]*pow(10,(2*n-h-1)) + (v[1]*10+digit) * pow(10,(2*n-m-2));
if sumhm >= sumhmn :
vnext[0]=v[0]
vnext[1]=v[1]*10+digit
vnext[2]=v[2]+'M'
print(mat[n][n][2])
``` | output | 1 | 44,149 | 19 | 88,299 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a new TV game on BerTV. In this game two players get a number A consisting of 2n digits. Before each turn players determine who will make the next move. Each player should make exactly n moves. On it's turn i-th player takes the leftmost digit of A and appends it to his or her number Si. After that this leftmost digit is erased from A. Initially the numbers of both players (S1 and S2) are «empty». Leading zeroes in numbers A, S1, S2 are allowed. In the end of the game the first player gets S1 dollars, and the second gets S2 dollars.
One day Homer and Marge came to play the game. They managed to know the number A beforehand. They want to find such sequence of their moves that both of them makes exactly n moves and which maximizes their total prize. Help them.
Input
The first line contains integer n (1 ≤ n ≤ 18). The second line contains integer A consisting of exactly 2n digits. This number can have leading zeroes.
Output
Output the line of 2n characters «H» and «M» — the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactly n moves. If there are several solutions, output any of them.
Examples
Input
2
1234
Output
HHMM
Input
2
9911
Output
HMHM | instruction | 0 | 44,150 | 19 | 88,300 |
Tags: dp
Correct Solution:
```
n=int(input())
s=input()
dp=[[-float('inf')]*(n+1) for i in range(2*n+1)]
dp[0][0]=0
m=2*n
for fir in range(0,n+1):
for i in range(1,2*n+1):
if(fir>i):
dp[i][fir]=-float('inf')
dp[i][fir]=max(dp[i][fir],dp[i-1][fir-1]+pow(10,n-fir)*int(s[i-1]))
if(n-(i-fir)>=0):dp[i][fir]=max(dp[i][fir],dp[i-1][fir]+pow(10,n-(i-fir))*int(s[i-1]))
#print(i,fir,dp[i][fir])
ans=""
ci,cj=2*n,n
while(ci>0):
if(cj>0 and dp[ci][cj]==dp[ci-1][cj-1]+pow(10,n-cj)*int(s[ci-1])):
ans+="M"
cj-=1
else:
ans+="H"
ci-=1
print(ans[::-1])
``` | output | 1 | 44,150 | 19 | 88,301 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a new TV game on BerTV. In this game two players get a number A consisting of 2n digits. Before each turn players determine who will make the next move. Each player should make exactly n moves. On it's turn i-th player takes the leftmost digit of A and appends it to his or her number Si. After that this leftmost digit is erased from A. Initially the numbers of both players (S1 and S2) are «empty». Leading zeroes in numbers A, S1, S2 are allowed. In the end of the game the first player gets S1 dollars, and the second gets S2 dollars.
One day Homer and Marge came to play the game. They managed to know the number A beforehand. They want to find such sequence of their moves that both of them makes exactly n moves and which maximizes their total prize. Help them.
Input
The first line contains integer n (1 ≤ n ≤ 18). The second line contains integer A consisting of exactly 2n digits. This number can have leading zeroes.
Output
Output the line of 2n characters «H» and «M» — the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactly n moves. If there are several solutions, output any of them.
Examples
Input
2
1234
Output
HHMM
Input
2
9911
Output
HMHM | instruction | 0 | 44,151 | 19 | 88,302 |
Tags: dp
Correct Solution:
```
n = int(input())
s = input()
dp = [[None for i in range(n + 1)] for i in range(n + 1)]
dp[0][0] = ('', '0', '0')
for i in range(1, n * 2 + 1):
for j in range(max(0, i - n), min(i, n) + 1):
k = i - j
curr = -1
if j > 0:
choices, h, m = dp[j - 1][k]
choices += 'H'
if len(h) < j:
h += s[i - 1]
else:
tmp = [c for c in h]
tmp[j - 1] = s[i - 1]
h = ''.join(tmp)
if len(m) < j:
m += '0'
sum = int(h) + int(m)
if sum > curr:
curr = sum
dp[j][k] = (choices, h, m)
if k > 0:
choices, h, m = dp[j][k - 1]
choices += 'M'
if len(m) < k:
m += s[i - 1]
else:
tmp = [c for c in m]
tmp[k - 1] = s[i - 1]
m = ''.join(tmp)
if len(h) < k:
h += '0'
sum = int(h) + int(m)
if sum > curr:
curr = sum
dp[j][k] = (choices, h, m)
print(dp[n][n][0])
``` | output | 1 | 44,151 | 19 | 88,303 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a new TV game on BerTV. In this game two players get a number A consisting of 2n digits. Before each turn players determine who will make the next move. Each player should make exactly n moves. On it's turn i-th player takes the leftmost digit of A and appends it to his or her number Si. After that this leftmost digit is erased from A. Initially the numbers of both players (S1 and S2) are «empty». Leading zeroes in numbers A, S1, S2 are allowed. In the end of the game the first player gets S1 dollars, and the second gets S2 dollars.
One day Homer and Marge came to play the game. They managed to know the number A beforehand. They want to find such sequence of their moves that both of them makes exactly n moves and which maximizes their total prize. Help them.
Input
The first line contains integer n (1 ≤ n ≤ 18). The second line contains integer A consisting of exactly 2n digits. This number can have leading zeroes.
Output
Output the line of 2n characters «H» and «M» — the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactly n moves. If there are several solutions, output any of them.
Examples
Input
2
1234
Output
HHMM
Input
2
9911
Output
HMHM | instruction | 0 | 44,152 | 19 | 88,304 |
Tags: dp
Correct Solution:
```
def compute():
from sys import stdin
[n] = list(map(int, stdin.readline().split()))
i = list(map(int, stdin.readline().strip()))
dp = {}
INF = (int(-1e9),"")
def f(x,h,m):
if x >= 2*n:
return (0,"")
args = (h,m)
if args in dp:
return dp[args]
res0, res1 = INF, INF
if h>=0:
ff = f(x+1, h-1,m)
res0 = (ff[0] + pow(10,h)*i[x], "H"+ff[1])
if m>=0:
ff = f(x+1, h,m-1)
res1 = (ff[0] + pow(10,m)*i[x], "M"+ff[1])
dp[args] = res0 if res0[0]>res1[0] else res1
return dp[args]
ans = f(0, n-1, n-1)[1]
print(ans)
if __name__ == "__main__":
compute()
``` | output | 1 | 44,152 | 19 | 88,305 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a new TV game on BerTV. In this game two players get a number A consisting of 2n digits. Before each turn players determine who will make the next move. Each player should make exactly n moves. On it's turn i-th player takes the leftmost digit of A and appends it to his or her number Si. After that this leftmost digit is erased from A. Initially the numbers of both players (S1 and S2) are «empty». Leading zeroes in numbers A, S1, S2 are allowed. In the end of the game the first player gets S1 dollars, and the second gets S2 dollars.
One day Homer and Marge came to play the game. They managed to know the number A beforehand. They want to find such sequence of their moves that both of them makes exactly n moves and which maximizes their total prize. Help them.
Input
The first line contains integer n (1 ≤ n ≤ 18). The second line contains integer A consisting of exactly 2n digits. This number can have leading zeroes.
Output
Output the line of 2n characters «H» and «M» — the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactly n moves. If there are several solutions, output any of them.
Examples
Input
2
1234
Output
HHMM
Input
2
9911
Output
HMHM
Submitted Solution:
```
n=int(input())
a=input()
b='H'
c=0
d=1
f=1
while c<n:
if c==f:
f+=1
b+='H'
d+=1
else:
e=d
for j in range(d,n+c+1):
if int(a[j])>int(a[e]):
e=j
b+='H'*(e-d)+'M'
f+=e-d
c+=1
d=f+c
print(b)
``` | instruction | 0 | 44,153 | 19 | 88,306 |
No | output | 1 | 44,153 | 19 | 88,307 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a new TV game on BerTV. In this game two players get a number A consisting of 2n digits. Before each turn players determine who will make the next move. Each player should make exactly n moves. On it's turn i-th player takes the leftmost digit of A and appends it to his or her number Si. After that this leftmost digit is erased from A. Initially the numbers of both players (S1 and S2) are «empty». Leading zeroes in numbers A, S1, S2 are allowed. In the end of the game the first player gets S1 dollars, and the second gets S2 dollars.
One day Homer and Marge came to play the game. They managed to know the number A beforehand. They want to find such sequence of their moves that both of them makes exactly n moves and which maximizes their total prize. Help them.
Input
The first line contains integer n (1 ≤ n ≤ 18). The second line contains integer A consisting of exactly 2n digits. This number can have leading zeroes.
Output
Output the line of 2n characters «H» and «M» — the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactly n moves. If there are several solutions, output any of them.
Examples
Input
2
1234
Output
HHMM
Input
2
9911
Output
HMHM
Submitted Solution:
```
def compute():
from sys import stdin
[n] = list(map(int, stdin.readline().split()))
i = list(map(int, stdin.readline().strip()))
dp = {}
INF = (int(-1e9),"")
def f(x,h,m):
if x >= 2*n:
return INF
args = (h,m)
if args in dp:
return dp[args]
res0, res1 = INF, INF
if h>=0:
ff = f(x+1, h-1,m)
res0 = (ff[0] + pow(10,h)*i[x], "H"+ff[1])
if m>=0:
ff = f(x+1, h,m-1)
res1 = (ff[0] + pow(10,m)*i[x], "M"+ff[1])
dp[args] = res0 if res0[0]>res1[0] else res1
return dp[args]
ans = f(0, n-1, n-1)[1]
print(ans)
if __name__ == "__main__":
compute()
``` | instruction | 0 | 44,154 | 19 | 88,308 |
No | output | 1 | 44,154 | 19 | 88,309 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a new TV game on BerTV. In this game two players get a number A consisting of 2n digits. Before each turn players determine who will make the next move. Each player should make exactly n moves. On it's turn i-th player takes the leftmost digit of A and appends it to his or her number Si. After that this leftmost digit is erased from A. Initially the numbers of both players (S1 and S2) are «empty». Leading zeroes in numbers A, S1, S2 are allowed. In the end of the game the first player gets S1 dollars, and the second gets S2 dollars.
One day Homer and Marge came to play the game. They managed to know the number A beforehand. They want to find such sequence of their moves that both of them makes exactly n moves and which maximizes their total prize. Help them.
Input
The first line contains integer n (1 ≤ n ≤ 18). The second line contains integer A consisting of exactly 2n digits. This number can have leading zeroes.
Output
Output the line of 2n characters «H» and «M» — the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactly n moves. If there are several solutions, output any of them.
Examples
Input
2
1234
Output
HHMM
Input
2
9911
Output
HMHM
Submitted Solution:
```
n=int(input())
a=input()
b='H'
c=0
d=1
f=1
while c<n:
if c==f:
f+=1
b+='H'
d+=1
else:
e=d
for j in range(d,n+c+1):
if int(a[j])>int(a[e]):
e=j
if e!=d or f==n:
b+='H'*(e-d)+'M'
f+=e-d
c+=1
d=f+c
else:
k=d
while k!=2*n-1 and a[k]==a[e]:
k+=1
x=[[9,1,'M',0]]
for i in range(k,d-1,-1):
y=0
for j in range(9,int(a[i])-1,-1):
z=a[i+1:n+c+1+i-d+1].find(a[e]*(i-d+1)+str(j))+i+1
if z!=i and f+z-d<=n:
x.append([j,i-d+2,'H'*(z-d)+'M'*(i-d+2),z-d])
x.sort()
b+=x[-1][2]
c+=x[-1][1]
f+=x[-1][3]
d=c+f
print(b)
``` | instruction | 0 | 44,155 | 19 | 88,310 |
No | output | 1 | 44,155 | 19 | 88,311 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a new TV game on BerTV. In this game two players get a number A consisting of 2n digits. Before each turn players determine who will make the next move. Each player should make exactly n moves. On it's turn i-th player takes the leftmost digit of A and appends it to his or her number Si. After that this leftmost digit is erased from A. Initially the numbers of both players (S1 and S2) are «empty». Leading zeroes in numbers A, S1, S2 are allowed. In the end of the game the first player gets S1 dollars, and the second gets S2 dollars.
One day Homer and Marge came to play the game. They managed to know the number A beforehand. They want to find such sequence of their moves that both of them makes exactly n moves and which maximizes their total prize. Help them.
Input
The first line contains integer n (1 ≤ n ≤ 18). The second line contains integer A consisting of exactly 2n digits. This number can have leading zeroes.
Output
Output the line of 2n characters «H» and «M» — the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactly n moves. If there are several solutions, output any of them.
Examples
Input
2
1234
Output
HHMM
Input
2
9911
Output
HMHM
Submitted Solution:
```
n = int(input())
s = input()
mat = [[[0,0,""] for _i in range(n+1)] for _j in range(n+1)]
for i in range(2*n):
digit = int(s[i])
toIter = min(i,n)
minh = max(i-n,0)
maxh = min(n,i)
for h in range(minh,maxh+1):
m = i - h
v = mat[h][m]
if h < n:
#add current symbol to homer
vnext = mat[h+1][m];
sumhmn = vnext[0]*(10**(n-h-2)) + vnext[1]*(10**(n-m-1));
sumhm = (v[0]*10+digit)*(10**(n-h-2)) + v[1]*(10**(n-m-1));
if sumhm >= sumhmn :
vnext[0]=v[0]*10+digit
vnext[1]=v[1]
vnext[2]=v[2]+'H'
if m < n:
#add current symbol to marge
vnext = mat[h][m+1];
sumhmn = vnext[0]*(10**(n-h-1)) + vnext[1]*(10**(n-m-2));
sumhm = v[0]*(10**(n-h-1)) + (v[1]*10+digit) * (10**(n-m-2));
if sumhm >= sumhmn :
vnext[0]=v[0]
vnext[1]=v[1]*10+digit
vnext[2]=v[2]+'M'
print(mat[n][n][2])
``` | instruction | 0 | 44,156 | 19 | 88,312 |
No | output | 1 | 44,156 | 19 | 88,313 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions. | instruction | 0 | 44,209 | 19 | 88,418 |
Tags: greedy, sortings
Correct Solution:
```
n = int(input())
a = [int(x) for x in input().split()]; a.sort()
print(sum([(i+1)*a[i] for i in range(n)])+sum(a[:-1]))
``` | output | 1 | 44,209 | 19 | 88,419 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions. | instruction | 0 | 44,210 | 19 | 88,420 |
Tags: greedy, sortings
Correct Solution:
```
n=int(input())
N=[int(i) for i in input().split()]
su=0
N.sort()
for i in range(n):
su+=i*N[i]+2*N[i]
su=su-N[n-1]
print(su)
``` | output | 1 | 44,210 | 19 | 88,421 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions. | instruction | 0 | 44,211 | 19 | 88,422 |
Tags: greedy, sortings
Correct Solution:
```
n = int(input())
arr = [int(s) for s in input().split()]
arr.sort(reverse=True)
res = 0
if n == 1 :
res = arr[0]
else :
res = n * (arr[0] + arr[1])
i = 2
while i < n :
res += (n + 1 - i) * arr[i]
i += 1
print(res)
``` | output | 1 | 44,211 | 19 | 88,423 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions. | instruction | 0 | 44,212 | 19 | 88,424 |
Tags: greedy, sortings
Correct Solution:
```
num_numbers = int(input())
numbers = list(map(int, input().split()))
numbers.sort(reverse=True)
solution = num_numbers*numbers[0]
for pos in range(1, len(numbers)):
solution += (num_numbers + 1 - pos) * numbers[pos]
print(solution)
``` | output | 1 | 44,212 | 19 | 88,425 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions. | instruction | 0 | 44,213 | 19 | 88,426 |
Tags: greedy, sortings
Correct Solution:
```
def solve():
n = int(input())
a = [int(x) for x in input().split(' ')]
a.sort()
if n == 1:
return a[0]
else:
m = [i + 2 for i in range(n)]
m[-1] -= 1
return sum([x * y for x, y in zip(a, m)])
print(solve())
``` | output | 1 | 44,213 | 19 | 88,427 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions. | instruction | 0 | 44,214 | 19 | 88,428 |
Tags: greedy, sortings
Correct Solution:
```
## necessary imports
import sys
input = sys.stdin.readline
from math import log2, log, ceil
# swap_array function
def swaparr(arr, a,b):
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp
## gcd function
def gcd(a,b):
if a == 0:
return b
return gcd(b%a, a)
## nCr function efficient using Binomial Cofficient
def nCr(n, k):
if(k > n - k):
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return res
## upper bound function code -- such that e in a[:i] e < x;
def upper_bound(a, x, lo=0):
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if a[mid] < x:
lo = mid+1
else:
hi = mid
return lo
## prime factorization
def primefs(n):
## if n == 1 ## calculating primes
primes = {}
while(n%2 == 0):
primes[2] = primes.get(2, 0) + 1
n = n//2
for i in range(3, int(n**0.5)+2, 2):
while(n%i == 0):
primes[i] = primes.get(i, 0) + 1
n = n//i
if n > 2:
primes[n] = primes.get(n, 0) + 1
## prime factoriazation of n is stored in dictionary
## primes and can be accesed. O(sqrt n)
return primes
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p):
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
temp = a
a = b
b = temp
return a,b
# find function
def find(x, link):
while(x != link[x]):
x = link[x]
return x
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x,y = swap(x,y)
if x != y:
size[x] += size[y]
link[y] = x
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e6 + 5)
def spf_sieve():
spf[1] = 1;
for i in range(2, MAXN):
spf[i] = i;
for i in range(4, MAXN, 2):
spf[i] = 2;
for i in range(3, ceil(MAXN ** 0.5), 2):
if spf[i] == i:
for j in range(i*i, MAXN, i):
if spf[j] == j:
spf[j] = i;
## function for storing smallest prime factors (spf) in the array
################## un-comment below 2 lines when using factorization #################
# spf = [0 for i in range(MAXN)]
# spf_sieve()
def factoriazation(x):
ret = {};
while x != 1:
ret[spf[x]] = ret.get(spf[x], 0) + 1;
x = x//spf[x]
return ret
## this function is useful for multiple queries only, o/w use
## primefs function above. complexity O(log n)
## taking integer array input
def int_array():
return list(map(int, input().strip().split()))
## taking string array input
def str_array():
return input().strip().split();
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
################### ---------------- TEMPLATE ENDS HERE ---------------- ###################
n = int(input()); a = sorted(int_array(), reverse = True);
ans = sum(a); sumo = ans;
while len(a) > 1:
x = a.pop();
ans += x; sumo -= x;
ans += sumo;
print(ans);
``` | output | 1 | 44,214 | 19 | 88,429 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
* Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
* Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
Input
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Output
Print a single integer — the largest possible score.
Examples
Input
3
3 1 5
Output
26
Input
1
10
Output
10
Note
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions. | instruction | 0 | 44,215 | 19 | 88,430 |
Tags: greedy, sortings
Correct Solution:
```
n = int(input())
inList = input().split()
aList = []
for a in inList:
aList.append(int(a))
aList.sort()
res = 0
for i in range(len(aList)):
res += aList[i]*(i+2)
print(res-aList[-1])
``` | output | 1 | 44,215 | 19 | 88,431 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.