message stringlengths 2 433k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 113 108k | cluster float64 12 12 | __index_level_0__ int64 226 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant loves sortings.
He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 β€ l β€ r β€ n) and increase ai by 1 for all i such that l β€ i β€ r.
Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 β€ i < n) ai β€ ai + 1 holds.
Input
The first line contains a single integer n (1 β€ n β€ 105) β the size of array a. The next line contains n integers, separated by single spaces β array a (1 β€ ai β€ 109). The array elements are listed in the line in the order of their index's increasing.
Output
In a single line print a single integer β the answer to the problem.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
1 2 3
Output
0
Input
3
3 2 1
Output
2
Input
4
7 4 1 47
Output
6
Note
In the first sample the array is already sorted in the non-decreasing order, so the answer is 0.
In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]).
In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47]. | instruction | 0 | 103,140 | 12 | 206,280 |
Tags: brute force, greedy
Correct Solution:
```
n=int(input())
a=[int(i) for i in input().split()]
sm = 0
for i in range(n-1):
sm+=max(a[i]-a[i+1],0)
print(sm)
'''
7 4 1 47
7->4.... 1 47
add 1 from 4 3 times
7->7->4->50
.....
add 1 from 4 is 3 times
7->7->7->47
sorted
6 times added
'''
``` | output | 1 | 103,140 | 12 | 206,281 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant loves sortings.
He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 β€ l β€ r β€ n) and increase ai by 1 for all i such that l β€ i β€ r.
Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 β€ i < n) ai β€ ai + 1 holds.
Input
The first line contains a single integer n (1 β€ n β€ 105) β the size of array a. The next line contains n integers, separated by single spaces β array a (1 β€ ai β€ 109). The array elements are listed in the line in the order of their index's increasing.
Output
In a single line print a single integer β the answer to the problem.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
1 2 3
Output
0
Input
3
3 2 1
Output
2
Input
4
7 4 1 47
Output
6
Note
In the first sample the array is already sorted in the non-decreasing order, so the answer is 0.
In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]).
In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
Submitted Solution:
```
from sys import stdin
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
b = list()
for i in range(1, n): b.append(a[i] - a[i-1])
print(sum([-ele for ele in b if ele < 0]))
``` | instruction | 0 | 103,141 | 12 | 206,282 |
Yes | output | 1 | 103,141 | 12 | 206,283 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant loves sortings.
He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 β€ l β€ r β€ n) and increase ai by 1 for all i such that l β€ i β€ r.
Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 β€ i < n) ai β€ ai + 1 holds.
Input
The first line contains a single integer n (1 β€ n β€ 105) β the size of array a. The next line contains n integers, separated by single spaces β array a (1 β€ ai β€ 109). The array elements are listed in the line in the order of their index's increasing.
Output
In a single line print a single integer β the answer to the problem.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
1 2 3
Output
0
Input
3
3 2 1
Output
2
Input
4
7 4 1 47
Output
6
Note
In the first sample the array is already sorted in the non-decreasing order, so the answer is 0.
In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]).
In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(1,n):
ans+=max(a[i-1]-a[i],0)
print(ans)
``` | instruction | 0 | 103,142 | 12 | 206,284 |
Yes | output | 1 | 103,142 | 12 | 206,285 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant loves sortings.
He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 β€ l β€ r β€ n) and increase ai by 1 for all i such that l β€ i β€ r.
Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 β€ i < n) ai β€ ai + 1 holds.
Input
The first line contains a single integer n (1 β€ n β€ 105) β the size of array a. The next line contains n integers, separated by single spaces β array a (1 β€ ai β€ 109). The array elements are listed in the line in the order of their index's increasing.
Output
In a single line print a single integer β the answer to the problem.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
1 2 3
Output
0
Input
3
3 2 1
Output
2
Input
4
7 4 1 47
Output
6
Note
In the first sample the array is already sorted in the non-decreasing order, so the answer is 0.
In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]).
In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
Submitted Solution:
```
n=int(input())
a=list(map(int,input().rstrip().split()))
carry=0
oper=0
for i in range(1,n):
a[i]=a[i]+carry
if a[i]<a[i-1]:
carry+=abs(a[i-1]-a[i])
oper+=abs(a[i-1]-a[i])
a[i]=a[i-1]
#print(a)
print(oper)
``` | instruction | 0 | 103,143 | 12 | 206,286 |
Yes | output | 1 | 103,143 | 12 | 206,287 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant loves sortings.
He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 β€ l β€ r β€ n) and increase ai by 1 for all i such that l β€ i β€ r.
Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 β€ i < n) ai β€ ai + 1 holds.
Input
The first line contains a single integer n (1 β€ n β€ 105) β the size of array a. The next line contains n integers, separated by single spaces β array a (1 β€ ai β€ 109). The array elements are listed in the line in the order of their index's increasing.
Output
In a single line print a single integer β the answer to the problem.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
1 2 3
Output
0
Input
3
3 2 1
Output
2
Input
4
7 4 1 47
Output
6
Note
In the first sample the array is already sorted in the non-decreasing order, so the answer is 0.
In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]).
In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
Submitted Solution:
```
m=int(input())
l=list(map(int,input().split()))
t=0
for i in range(m-1):
if l[i]>l[i+1]:
t+=l[i]-l[i+1]
print(t)
``` | instruction | 0 | 103,144 | 12 | 206,288 |
Yes | output | 1 | 103,144 | 12 | 206,289 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant loves sortings.
He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 β€ l β€ r β€ n) and increase ai by 1 for all i such that l β€ i β€ r.
Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 β€ i < n) ai β€ ai + 1 holds.
Input
The first line contains a single integer n (1 β€ n β€ 105) β the size of array a. The next line contains n integers, separated by single spaces β array a (1 β€ ai β€ 109). The array elements are listed in the line in the order of their index's increasing.
Output
In a single line print a single integer β the answer to the problem.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
1 2 3
Output
0
Input
3
3 2 1
Output
2
Input
4
7 4 1 47
Output
6
Note
In the first sample the array is already sorted in the non-decreasing order, so the answer is 0.
In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]).
In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
Submitted Solution:
```
"""
Author : co_devil Chirag Garg
Institute : JIIT
"""
from __future__ import division, print_function
import itertools, os, sys, threading
from collections import deque, Counter, OrderedDict, defaultdict
import heapq
from math import ceil,floor,log,sqrt,factorial,pow,pi
# from bisect import bisect_left,bisect_right
# from decimal import *,threading
"""from io import BytesIO, IOBase
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from future_builtins import ascii, filter, hex, map, oct, zip
else:
from builtins import str as __str__
str = lambda x=b'': x if type(x) is bytes else __str__(x).encode()
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._buffer = BytesIO()
self._fd = file.fileno()
self._writable = 'x' in file.mode or 'r' not in file.mode
self.write = self._buffer.write if self._writable else None
def read(self):
return self._buffer.read() if self._buffer.tell() else os.read(self._fd, os.fstat(self._fd).st_size)
def readline(self):
while self.newlines == 0:
b, ptr = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)), self._buffer.tell()
self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
self.newlines += b.count(b'\n') + (not b)
self.newlines -= 1
return self._buffer.readline()
def flush(self):
if self._writable:
os.write(self._fd, self._buffer.getvalue())
self._buffer.truncate(0), self._buffer.seek(0)
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip(b'\r\n')
def print(*args, **kwargs):
sep, file = kwargs.pop('sep', b' '), kwargs.pop('file', sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop('end', b'\n'))
if kwargs.pop('flush', False):
file.flush()
"""
def ii(): return int(input())
def si(): return str(input())
def mi(): return map(int,input().split())
def li(): return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12,
'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24,
'z': 25}
mod = 1000000007
dx, dy = [-1, 1, 0, 0], [0, 0, 1, -1]
def getKey(item): return item[0]
def sort2(l): return sorted(l, key=getKey)
def d2(n, m, num): return [[num for x in range(m)] for y in range(n)]
def isPowerOfTwo(x): return (x and (not (x & (x - 1))))
def decimalToBinary(n): return bin(n).replace("0b", "")
def ntl(n): return [int(i) for i in str(n)]
def powerMod(x, y, p):
res = 1
x %= p
while y > 0:
if y & 1:
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
def gcd(x, y):
while y:
x, y = y, x % y
return x
# For getting input from input.txt file
# sys.stdin = open('input.txt', 'r')
# Printing the Output to output.txt file
# sys.stdout = open('output.txt', 'w')
graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000
def dfs(v, c):
if visited[v]:
if col[v] != c:
print('-1')
exit()
return
col[v] = c
visited[v] = 1
for i in graph[v]:
dfs(i, c ^ 1)
def bfs(d,v):
q=[]
q.append(v)
visited[v]=1
while len(q)!=0:
x=q[0]
q.pop(0)
for i in d[x]:
if visited[i]!=1:
visited[i]=1
q.append(i)
print(x)
print(l)
def make_graph(e):
d={}
for i in range(e):
x,y=mi()
if x not in d.keys():
d[x]=[y]
else:
d[x].append(y)
if y not in d.keys():
d[y] = [x]
else:
d[y].append(x)
return d
def gr2(n):
d={}
for i in range(n):
x,y=mi()
if x not in d.keys():
d[x]=[y]
else:
d[x].append(y)
return d
def connected_components(graph):
seen = set()
def dfs(v):
vs = set([v])
component=[]
while vs:
v = vs.pop()
seen.add(v)
vs |= set(graph[v]) - seen
component.append(v)
return component
ans=[]
for v in graph:
if v not in seen:
d=dfs(v)
ans.append(d)
return ans
n=ii()
s=li()
c=0
x=s[0]
y=s[0]
for i in range(1,n):
if s[i]<x:
x=s[i]
if i==n-1:
c+=y-s[i]
break
elif s[i]>x:
x=s[i]
c+=y-s[i-1]
y=s[i]
print(c)
``` | instruction | 0 | 103,145 | 12 | 206,290 |
No | output | 1 | 103,145 | 12 | 206,291 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant loves sortings.
He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 β€ l β€ r β€ n) and increase ai by 1 for all i such that l β€ i β€ r.
Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 β€ i < n) ai β€ ai + 1 holds.
Input
The first line contains a single integer n (1 β€ n β€ 105) β the size of array a. The next line contains n integers, separated by single spaces β array a (1 β€ ai β€ 109). The array elements are listed in the line in the order of their index's increasing.
Output
In a single line print a single integer β the answer to the problem.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
1 2 3
Output
0
Input
3
3 2 1
Output
2
Input
4
7 4 1 47
Output
6
Note
In the first sample the array is already sorted in the non-decreasing order, so the answer is 0.
In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]).
In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
Submitted Solution:
```
n=int(input())
l=list(map(int,input().split()))
cnt=0
ans=0
for i in range(1,n):
if l[i]+cnt<l[i-1]:
ans+=abs(l[i]+cnt-l[i-1])
l[i]=l[i-1]
cnt+=ans
print(ans)
``` | instruction | 0 | 103,146 | 12 | 206,292 |
No | output | 1 | 103,146 | 12 | 206,293 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant loves sortings.
He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 β€ l β€ r β€ n) and increase ai by 1 for all i such that l β€ i β€ r.
Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 β€ i < n) ai β€ ai + 1 holds.
Input
The first line contains a single integer n (1 β€ n β€ 105) β the size of array a. The next line contains n integers, separated by single spaces β array a (1 β€ ai β€ 109). The array elements are listed in the line in the order of their index's increasing.
Output
In a single line print a single integer β the answer to the problem.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
1 2 3
Output
0
Input
3
3 2 1
Output
2
Input
4
7 4 1 47
Output
6
Note
In the first sample the array is already sorted in the non-decreasing order, so the answer is 0.
In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]).
In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
Submitted Solution:
```
"""
Template written to be used by Python Programmers.
Use at your own risk!!!!
Owned by enraged(rating - 5 star at CodeChef and Specialist at Codeforces).
"""
import sys
import heapq
from math import ceil, floor, gcd, fabs, factorial, fmod
from collections import defaultdict as dd, deque, Counter as c
from itertools import combinations as comb
from bisect import bisect_left as bl, bisect_right as br, bisect
# sys.setrecursionlimit(2*pow(10, 6))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(var): sys.stdout.write(var)
def l(): return list(map(int, data().split()))
def sl(): return list(map(str, data().split()))
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [[val for i in range(n)] for j in range(m)]
n = int(data())
arr = l()
m = arr[0]
answer = 0
for i in range(1, n):
if arr[i] < m:
answer = max(answer, m-arr[i])
else:
m = max(arr[i], m)
out(str(answer))
``` | instruction | 0 | 103,147 | 12 | 206,294 |
No | output | 1 | 103,147 | 12 | 206,295 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant loves sortings.
He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 β€ l β€ r β€ n) and increase ai by 1 for all i such that l β€ i β€ r.
Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 β€ i < n) ai β€ ai + 1 holds.
Input
The first line contains a single integer n (1 β€ n β€ 105) β the size of array a. The next line contains n integers, separated by single spaces β array a (1 β€ ai β€ 109). The array elements are listed in the line in the order of their index's increasing.
Output
In a single line print a single integer β the answer to the problem.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
1 2 3
Output
0
Input
3
3 2 1
Output
2
Input
4
7 4 1 47
Output
6
Note
In the first sample the array is already sorted in the non-decreasing order, so the answer is 0.
In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]).
In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
m=a.index(max(a))
d=[0]
if(m==n-1):
for i in range(n-2):
if(a[i]>a[i+1]):
d.append(a[i]-a[i+1])
a[i+1]=a[i]
else:
for i in range(n-1):
if(a[i]>a[i+1]):
d.append(a[i]-a[i+1])
a[i+1]=a[i]
print(max(d))
``` | instruction | 0 | 103,148 | 12 | 206,296 |
No | output | 1 | 103,148 | 12 | 206,297 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once Bob needed to find the second order statistics of a sequence of integer numbers. Lets choose each number from the sequence exactly once and sort them. The value on the second position is the second order statistics of the given sequence. In other words it is the smallest element strictly greater than the minimum. Help Bob solve this problem.
Input
The first input line contains integer n (1 β€ n β€ 100) β amount of numbers in the sequence. The second line contains n space-separated integer numbers β elements of the sequence. These numbers don't exceed 100 in absolute value.
Output
If the given sequence has the second order statistics, output this order statistics, otherwise output NO.
Examples
Input
4
1 2 2 -4
Output
1
Input
5
1 2 3 1 1
Output
2 | instruction | 0 | 103,149 | 12 | 206,298 |
Tags: brute force
Correct Solution:
```
notused, inp = 0*int(input()), sorted(list(set(map(int, input().split(" ")))))
print(inp[1]) if len(inp) > 1 else print("NO")
``` | output | 1 | 103,149 | 12 | 206,299 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once Bob needed to find the second order statistics of a sequence of integer numbers. Lets choose each number from the sequence exactly once and sort them. The value on the second position is the second order statistics of the given sequence. In other words it is the smallest element strictly greater than the minimum. Help Bob solve this problem.
Input
The first input line contains integer n (1 β€ n β€ 100) β amount of numbers in the sequence. The second line contains n space-separated integer numbers β elements of the sequence. These numbers don't exceed 100 in absolute value.
Output
If the given sequence has the second order statistics, output this order statistics, otherwise output NO.
Examples
Input
4
1 2 2 -4
Output
1
Input
5
1 2 3 1 1
Output
2 | instruction | 0 | 103,150 | 12 | 206,300 |
Tags: brute force
Correct Solution:
```
n = int(input())
a = set(map(int,input().split(" ")))
if len(a)>1:
a= sorted(a)
print(a[1])
else:
print("NO")
``` | output | 1 | 103,150 | 12 | 206,301 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once Bob needed to find the second order statistics of a sequence of integer numbers. Lets choose each number from the sequence exactly once and sort them. The value on the second position is the second order statistics of the given sequence. In other words it is the smallest element strictly greater than the minimum. Help Bob solve this problem.
Input
The first input line contains integer n (1 β€ n β€ 100) β amount of numbers in the sequence. The second line contains n space-separated integer numbers β elements of the sequence. These numbers don't exceed 100 in absolute value.
Output
If the given sequence has the second order statistics, output this order statistics, otherwise output NO.
Examples
Input
4
1 2 2 -4
Output
1
Input
5
1 2 3 1 1
Output
2 | instruction | 0 | 103,151 | 12 | 206,302 |
Tags: brute force
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
a.sort()
i = 1
while i < n:
if a[i] == a[i-1]:
i += 1
else:
break
if i == n:
print ('NO')
else:
print (a[i])
``` | output | 1 | 103,151 | 12 | 206,303 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once Bob needed to find the second order statistics of a sequence of integer numbers. Lets choose each number from the sequence exactly once and sort them. The value on the second position is the second order statistics of the given sequence. In other words it is the smallest element strictly greater than the minimum. Help Bob solve this problem.
Input
The first input line contains integer n (1 β€ n β€ 100) β amount of numbers in the sequence. The second line contains n space-separated integer numbers β elements of the sequence. These numbers don't exceed 100 in absolute value.
Output
If the given sequence has the second order statistics, output this order statistics, otherwise output NO.
Examples
Input
4
1 2 2 -4
Output
1
Input
5
1 2 3 1 1
Output
2 | instruction | 0 | 103,152 | 12 | 206,304 |
Tags: brute force
Correct Solution:
```
from functools import reduce
from operator import *
from math import *
from sys import *
from string import *
from collections import *
setrecursionlimit(10**7)
dX= [-1, 1, 0, 0,-1, 1,-1, 1]
dY= [ 0, 0,-1, 1, 1,-1,-1, 1]
RI=lambda: list(map(int,input().split()))
RS=lambda: input().rstrip().split()
#################################################
n=RI()[0]
a=RI()
a.sort()
i=0
while i<n and a[i]==a[0]:
i+=1
if i==n:
print("NO")
else:
print(a[i])
``` | output | 1 | 103,152 | 12 | 206,305 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once Bob needed to find the second order statistics of a sequence of integer numbers. Lets choose each number from the sequence exactly once and sort them. The value on the second position is the second order statistics of the given sequence. In other words it is the smallest element strictly greater than the minimum. Help Bob solve this problem.
Input
The first input line contains integer n (1 β€ n β€ 100) β amount of numbers in the sequence. The second line contains n space-separated integer numbers β elements of the sequence. These numbers don't exceed 100 in absolute value.
Output
If the given sequence has the second order statistics, output this order statistics, otherwise output NO.
Examples
Input
4
1 2 2 -4
Output
1
Input
5
1 2 3 1 1
Output
2 | instruction | 0 | 103,153 | 12 | 206,306 |
Tags: brute force
Correct Solution:
```
n = int(input())
nums = list(map(int, input().split()))
u_nums = list(set(nums))
u_nums.sort()
if len(u_nums)>1:
print(u_nums[1])
else:
print("NO")
``` | output | 1 | 103,153 | 12 | 206,307 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once Bob needed to find the second order statistics of a sequence of integer numbers. Lets choose each number from the sequence exactly once and sort them. The value on the second position is the second order statistics of the given sequence. In other words it is the smallest element strictly greater than the minimum. Help Bob solve this problem.
Input
The first input line contains integer n (1 β€ n β€ 100) β amount of numbers in the sequence. The second line contains n space-separated integer numbers β elements of the sequence. These numbers don't exceed 100 in absolute value.
Output
If the given sequence has the second order statistics, output this order statistics, otherwise output NO.
Examples
Input
4
1 2 2 -4
Output
1
Input
5
1 2 3 1 1
Output
2 | instruction | 0 | 103,154 | 12 | 206,308 |
Tags: brute force
Correct Solution:
```
n = int(input())
l = list(map(int, input().split()))
k = sorted(list(set(l)))
if len(k) < 2:
print("NO")
else:
print(k[1])
``` | output | 1 | 103,154 | 12 | 206,309 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once Bob needed to find the second order statistics of a sequence of integer numbers. Lets choose each number from the sequence exactly once and sort them. The value on the second position is the second order statistics of the given sequence. In other words it is the smallest element strictly greater than the minimum. Help Bob solve this problem.
Input
The first input line contains integer n (1 β€ n β€ 100) β amount of numbers in the sequence. The second line contains n space-separated integer numbers β elements of the sequence. These numbers don't exceed 100 in absolute value.
Output
If the given sequence has the second order statistics, output this order statistics, otherwise output NO.
Examples
Input
4
1 2 2 -4
Output
1
Input
5
1 2 3 1 1
Output
2 | instruction | 0 | 103,155 | 12 | 206,310 |
Tags: brute force
Correct Solution:
```
n = int(input())
ls = list(map(int, input().split()))
def second_order(ls):
if len(list(dict.fromkeys(sorted(ls)))) > 1:
return list(dict.fromkeys(sorted(ls)))[1]
else:
return "NO"
print(second_order(ls))
``` | output | 1 | 103,155 | 12 | 206,311 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once Bob needed to find the second order statistics of a sequence of integer numbers. Lets choose each number from the sequence exactly once and sort them. The value on the second position is the second order statistics of the given sequence. In other words it is the smallest element strictly greater than the minimum. Help Bob solve this problem.
Input
The first input line contains integer n (1 β€ n β€ 100) β amount of numbers in the sequence. The second line contains n space-separated integer numbers β elements of the sequence. These numbers don't exceed 100 in absolute value.
Output
If the given sequence has the second order statistics, output this order statistics, otherwise output NO.
Examples
Input
4
1 2 2 -4
Output
1
Input
5
1 2 3 1 1
Output
2 | instruction | 0 | 103,156 | 12 | 206,312 |
Tags: brute force
Correct Solution:
```
n = int(input())
mas = {int(x) for x in input().split()}
smas = sorted(mas)
if smas == mas or len(smas) < 2:
print("NO")
else:
print(smas[1])
``` | output | 1 | 103,156 | 12 | 206,313 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points.
After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·105) β the number of elements in the array. The next line contains n integers ai (1 β€ ai β€ 106) β the values of the array elements.
Output
In a single line print a single integer β the maximum number of points Artem can get.
Examples
Input
5
3 1 5 2 6
Output
11
Input
5
1 2 3 4 5
Output
6
Input
5
1 100 101 100 1
Output
102 | instruction | 0 | 103,239 | 12 | 206,478 |
Tags: data structures, greedy
Correct Solution:
```
n = input()
s = []
a = 0
for i in map(int, input().split()):
while len(s) > 1 and min(s[-2], i)>=s[-1]:
a += min(i, s[-2])
del(s[-1])
s.append(i)
s.sort()
print(a + sum(s[0: -2]))
``` | output | 1 | 103,239 | 12 | 206,479 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 β€ n β€ 100, 0 β€ x β€ 100) β the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer β the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil. | instruction | 0 | 103,383 | 12 | 206,766 |
Tags: greedy, implementation
Correct Solution:
```
_, x = [int(x) for x in input().split(' ')]
s = set([int(i) for i in input().split(' ') if int(i) <= x])
d = set(range(x)).symmetric_difference(s)
print(len(d))
``` | output | 1 | 103,383 | 12 | 206,767 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 β€ n β€ 100, 0 β€ x β€ 100) β the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer β the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil. | instruction | 0 | 103,386 | 12 | 206,772 |
Tags: greedy, implementation
Correct Solution:
```
from bisect import bisect
n, k = [int(x) for x in input().strip().split()]
arr = [False for _ in range(101)]
inp = [int(x) for x in input().strip().split()]
for i in inp:
arr[i] = True
count = 1 if arr[k] else 0
for i in range(0, k):
if not (arr[i]):
count+=1
print(count)
``` | output | 1 | 103,386 | 12 | 206,773 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 β€ n β€ 100, 0 β€ x β€ 100) β the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer β the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil.
Submitted Solution:
```
n,x=list(map(int,input().split(" ")))
arr=list(map(int,input().split(" ")))
arr1=range(0,x)
num=arr.count(x)
for i in arr1:
if i not in arr:
num+=1
print(num)
``` | instruction | 0 | 103,392 | 12 | 206,784 |
Yes | output | 1 | 103,392 | 12 | 206,785 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's denote that some array b is bad if it contains a subarray b_l, b_{l+1}, ..., b_{r} of odd length more than 1 (l < r and r - l + 1 is odd) such that β i β \{0, 1, ..., r - l\} b_{l + i} = b_{r - i}.
If an array is not bad, it is good.
Now you are given an array a_1, a_2, ..., a_n. Some elements are replaced by -1. Calculate the number of good arrays you can obtain by replacing each -1 with some integer from 1 to k.
Since the answer can be large, print it modulo 998244353.
Input
The first line contains two integers n and k (2 β€ n, k β€ 2 β
10^5) β the length of array a and the size of "alphabet", i. e., the upper bound on the numbers you may use to replace -1.
The second line contains n integers a_1, a_2, ..., a_n (a_i = -1 or 1 β€ a_i β€ k) β the array a.
Output
Print one integer β the number of good arrays you can get, modulo 998244353.
Examples
Input
2 3
-1 -1
Output
9
Input
5 2
1 -1 -1 1 2
Output
0
Input
5 3
1 -1 -1 1 2
Output
2
Input
4 200000
-1 -1 12345 -1
Output
735945883 | instruction | 0 | 103,732 | 12 | 207,464 |
Tags: combinatorics, divide and conquer, dp
Correct Solution:
```
MOD = 998244353
n, k = map(int, input().split())
a = list(map(int, input().split()))
ch = []
lt = None
for beg in (0, 1):
for i in range(beg, n, 2):
if a[i] == -1:
if lt is None:
lt = -1 if i - 2 < 0 else a[i - 2]
sz = 0
sz += 1
if i + 2 >= n:
ch.append((lt, -1, sz))
lt = None
elif a[i + 2] != -1:
ch.append((lt, a[i + 2], sz))
lt = None
ans = int(all(a[i] != a[i + 2] for i in range(n - 2) if a[i] != -1))
for lt, rt, sz in ch:
if sz == 1:
cur = k if lt == -1 and rt == -1 else k - 1 if lt == -1 or rt == -1 or lt == rt else k - 2
else:
eq, neq = 1 if lt == -1 else 0, 1
for _ in range(sz - 1):
eq, neq = neq * (k - 1) % MOD, (neq * (k - 2) + eq) % MOD
cur = neq * (k - 1) + eq if rt == -1 else neq * (k - 1) if lt == rt else neq * (k - 2) + eq
ans = ans * cur % MOD
print(ans)
``` | output | 1 | 103,732 | 12 | 207,465 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's denote that some array b is bad if it contains a subarray b_l, b_{l+1}, ..., b_{r} of odd length more than 1 (l < r and r - l + 1 is odd) such that β i β \{0, 1, ..., r - l\} b_{l + i} = b_{r - i}.
If an array is not bad, it is good.
Now you are given an array a_1, a_2, ..., a_n. Some elements are replaced by -1. Calculate the number of good arrays you can obtain by replacing each -1 with some integer from 1 to k.
Since the answer can be large, print it modulo 998244353.
Input
The first line contains two integers n and k (2 β€ n, k β€ 2 β
10^5) β the length of array a and the size of "alphabet", i. e., the upper bound on the numbers you may use to replace -1.
The second line contains n integers a_1, a_2, ..., a_n (a_i = -1 or 1 β€ a_i β€ k) β the array a.
Output
Print one integer β the number of good arrays you can get, modulo 998244353.
Examples
Input
2 3
-1 -1
Output
9
Input
5 2
1 -1 -1 1 2
Output
0
Input
5 3
1 -1 -1 1 2
Output
2
Input
4 200000
-1 -1 12345 -1
Output
735945883 | instruction | 0 | 103,733 | 12 | 207,466 |
Tags: combinatorics, divide and conquer, dp
Correct Solution:
```
n,k = map(int, input().strip().split())
l = list(map(int, input().strip().split()))
test = True
md = 998244353
evens = [0]
odds = [0]
for i in range(n):
if i%2:
odds.append(l[i])
else:
evens.append(l[i])
evens.append(-10)
odds.append(-10)
segs = []
l = len(odds)
cont = False
test = True
for i in range(l):
if odds[i] == -1 and cont == False:
a = i-1
cont = True
elif odds[i] != -1 and cont == True:
cont = False
b = i
segs.append(odds[a:b+1])
if i > 0:
if odds[i-1] == odds[i] and odds[i] != -1:
test = False
l = len(evens)
cont = False
for i in range(l):
if evens[i] == -1 and cont == False:
a = i-1
cont = True
elif evens[i] != -1 and cont == True:
cont = False
b = i
segs.append(evens[a:b+1])
if i > 0:
if evens[i-1] == evens[i] and evens[i] != -1:
test = False
ans = 1
for seg in segs:
l = len(seg) - 2
dp = [[0,0] for i in range(l)]
a = seg[-1]
b = seg[0]
if b == 0:
dp[0][0] = 1
dp[0][1] = k-1
elif b == a:
dp[0][0] = 0
dp[0][1] = k-1
elif b != a:
dp[0][0] = 1
dp[0][1] = k-2
for i in range(1,l):
dp[i][0] = (dp[i-1][1])%md
dp[i][1] = (dp[i-1][0]*(k-1) + dp[i-1][1]*(k-2))%md
if a == -10:
ans *= (dp[l-1][0] + dp[l-1][1])
else:
ans *= dp[l-1][1]
ans %= md
if test == False:
print(0)
else:
print(ans)
``` | output | 1 | 103,733 | 12 | 207,467 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's denote that some array b is bad if it contains a subarray b_l, b_{l+1}, ..., b_{r} of odd length more than 1 (l < r and r - l + 1 is odd) such that β i β \{0, 1, ..., r - l\} b_{l + i} = b_{r - i}.
If an array is not bad, it is good.
Now you are given an array a_1, a_2, ..., a_n. Some elements are replaced by -1. Calculate the number of good arrays you can obtain by replacing each -1 with some integer from 1 to k.
Since the answer can be large, print it modulo 998244353.
Input
The first line contains two integers n and k (2 β€ n, k β€ 2 β
10^5) β the length of array a and the size of "alphabet", i. e., the upper bound on the numbers you may use to replace -1.
The second line contains n integers a_1, a_2, ..., a_n (a_i = -1 or 1 β€ a_i β€ k) β the array a.
Output
Print one integer β the number of good arrays you can get, modulo 998244353.
Examples
Input
2 3
-1 -1
Output
9
Input
5 2
1 -1 -1 1 2
Output
0
Input
5 3
1 -1 -1 1 2
Output
2
Input
4 200000
-1 -1 12345 -1
Output
735945883 | instruction | 0 | 103,734 | 12 | 207,468 |
Tags: combinatorics, divide and conquer, dp
Correct Solution:
```
mod = 998244353
maxn = 200002
def pow(a, e):
if e <= 0: return 1
ret = 1
while e:
if e & 1:
ret = (ret * a) % mod
a = (a * a) % mod
e >>= 1
return ret
fn, fe = [1], [None]
def build(k):
fn.append(k-2)
fe.append(k-1)
for i in range(2, maxn):
fe.append( ((k-1) * fn[i-1]) % mod )
fn.append( ((k-1) * fn[i-2] + (k-2) * fn[i-1]) % mod )
def getRanges(arr):
q, st, en = [], -1, -1
for i, x in enumerate(arr):
if x == -1:
if st == -1:
st = en = i
else:
en = i
else:
if st >= 0:
q.append((st, en))
st, en = -1, -1
if arr[-1] == -1:
q.append((st, en))
return q
def getWays(arr, k):
ans = 1
for st, en in getRanges(arr):
if st == 0 and en == len(arr)-1:
ans *= k * pow(k-1, en-st)
elif st == 0 or en == len(arr)-1:
ans *= pow(k-1, en-st+1)
elif arr[st-1] == arr[en+1]:
ans *= fe[en-st+1]
else:
ans *= fn[en-st+1]
ans %= mod
return ans
def incorrect(arr, n):
for i in range(1, n-1):
if arr[i-1] == arr[i+1] and arr[i-1] != -1:
return True
return False
def main():
n, k = map(int, input().split())
arr = [int(x) for x in input().split()]
if incorrect(arr, n):
print(0)
return
build(k)
even = [x for i, x in enumerate(arr) if i & 1 == 0 ]
odd = [x for i, x in enumerate(arr) if i & 1 == 1 ]
e, o = getWays(even, k), getWays(odd, k)
print((e * o) % mod)
if __name__ == "__main__":
main()
``` | output | 1 | 103,734 | 12 | 207,469 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's denote that some array b is bad if it contains a subarray b_l, b_{l+1}, ..., b_{r} of odd length more than 1 (l < r and r - l + 1 is odd) such that β i β \{0, 1, ..., r - l\} b_{l + i} = b_{r - i}.
If an array is not bad, it is good.
Now you are given an array a_1, a_2, ..., a_n. Some elements are replaced by -1. Calculate the number of good arrays you can obtain by replacing each -1 with some integer from 1 to k.
Since the answer can be large, print it modulo 998244353.
Input
The first line contains two integers n and k (2 β€ n, k β€ 2 β
10^5) β the length of array a and the size of "alphabet", i. e., the upper bound on the numbers you may use to replace -1.
The second line contains n integers a_1, a_2, ..., a_n (a_i = -1 or 1 β€ a_i β€ k) β the array a.
Output
Print one integer β the number of good arrays you can get, modulo 998244353.
Examples
Input
2 3
-1 -1
Output
9
Input
5 2
1 -1 -1 1 2
Output
0
Input
5 3
1 -1 -1 1 2
Output
2
Input
4 200000
-1 -1 12345 -1
Output
735945883 | instruction | 0 | 103,735 | 12 | 207,470 |
Tags: combinatorics, divide and conquer, dp
Correct Solution:
```
#!/usr/bin/python3.7
import sys
mod = 998244353
n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
ub = [0 for i in range(n)]
b = [[0, 0] for i in range(n)]
ub[0] = 1
b[0] = [0, 1]
for i in range(1, n):
ub[i] = ub[i - 1] * (k - 1) % mod
sb = b[i - 1][0] + b[i - 1][1] * (k - 1) % mod
if sb >= mod:
sb -= mod
for j in range(2):
b[i][j] = sb - b[i - 1][j]
if b[i][j] < 0:
b[i][j] += mod
ans = 1
for arr in [a[::2], a[1::2]]:
for i in range(1, len(arr)):
if (arr[i] != -1) and (arr[i] == arr[i - 1]):
print(0)
sys.exit()
cur = -1
for i, x in enumerate(arr):
if x == -1:
continue
cnt = i - cur - 1
if cnt > 0:
if cur == -1:
ans = (ans * ub[cnt - 1] * (k - 1)) % mod
else:
s = b[cnt - 1][0] + b[cnt - 1][1] * (k - 1) % mod
if s >= mod:
s -= mod
if x == arr[cur]:
s -= b[cnt - 1][0]
else:
s -= b[cnt - 1][1]
if s < 0:
s += mod
ans = ans * s % mod
cur = i
if cur == -1:
ans = (ans * ub[len(arr) - 1] * k) % mod
elif cur < len(arr) - 1:
cnt = len(arr) - cur - 1
s = b[cnt - 1][0] + b[cnt - 1][1] * (k - 1) % mod
if s >= mod:
s -= mod
ans = ans * s % mod
print(ans)
``` | output | 1 | 103,735 | 12 | 207,471 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's denote that some array b is bad if it contains a subarray b_l, b_{l+1}, ..., b_{r} of odd length more than 1 (l < r and r - l + 1 is odd) such that β i β \{0, 1, ..., r - l\} b_{l + i} = b_{r - i}.
If an array is not bad, it is good.
Now you are given an array a_1, a_2, ..., a_n. Some elements are replaced by -1. Calculate the number of good arrays you can obtain by replacing each -1 with some integer from 1 to k.
Since the answer can be large, print it modulo 998244353.
Input
The first line contains two integers n and k (2 β€ n, k β€ 2 β
10^5) β the length of array a and the size of "alphabet", i. e., the upper bound on the numbers you may use to replace -1.
The second line contains n integers a_1, a_2, ..., a_n (a_i = -1 or 1 β€ a_i β€ k) β the array a.
Output
Print one integer β the number of good arrays you can get, modulo 998244353.
Examples
Input
2 3
-1 -1
Output
9
Input
5 2
1 -1 -1 1 2
Output
0
Input
5 3
1 -1 -1 1 2
Output
2
Input
4 200000
-1 -1 12345 -1
Output
735945883 | instruction | 0 | 103,736 | 12 | 207,472 |
Tags: combinatorics, divide and conquer, dp
Correct Solution:
```
import io, os
input = io.StringIO(os.read(0, os.fstat(0).st_size).decode()).readline
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
MOD = 998244353
n, k = mi()
a = li()
grps = []
ok = 1
for i in range(1, n - 1):
if a[i - 1] == a[i + 1] and a[i - 1] != -1:
ok = 0
break
lt = sz = None
for i in range(0, n, 2):
if a[i] == -1:
if lt is None:
if i - 2 < 0:
lt = -1
else:
lt = a[i - 2]
sz = 0
sz += 1
if i + 2 >= n:
grps.append((lt, -1, sz))
lt = None
elif a[i + 2] != -1:
grps.append((lt, a[i + 2], sz))
lt = None
lt = sz = None
for i in range(1, n, 2):
if a[i] == -1:
if lt is None:
if i - 2 < 0:
lt = -1
else:
lt = a[i - 2]
sz = 0
sz += 1
if i + 2 >= n:
grps.append((lt, -1, sz))
lt = None
elif a[i + 2] != -1:
grps.append((lt, a[i + 2], sz))
lt = None
ans = ok
for lt, rt, sz in grps:
lt, rt = min(lt, rt), max(lt, rt)
if sz == 1:
if lt == -1:
if rt == -1:
cur = k
else:
cur = k - 1
elif lt == rt:
cur = k - 1
else:
cur = k - 2
else:
if lt == -1:
eq, neq = 1, 1
else:
eq, neq = 0, 1
for _ in range(sz - 1):
eq2 = neq * (k - 1)
neq2 = neq * (k - 2) + eq
eq, neq = eq2 % MOD, neq2 % MOD
if rt == -1:
cur = neq * (k - 1) + eq
elif lt == rt:
cur = neq * (k - 1)
else:
cur = neq * (k - 2) + eq
ans = ans * cur % MOD
print(ans)
``` | output | 1 | 103,736 | 12 | 207,473 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's denote that some array b is bad if it contains a subarray b_l, b_{l+1}, ..., b_{r} of odd length more than 1 (l < r and r - l + 1 is odd) such that β i β \{0, 1, ..., r - l\} b_{l + i} = b_{r - i}.
If an array is not bad, it is good.
Now you are given an array a_1, a_2, ..., a_n. Some elements are replaced by -1. Calculate the number of good arrays you can obtain by replacing each -1 with some integer from 1 to k.
Since the answer can be large, print it modulo 998244353.
Input
The first line contains two integers n and k (2 β€ n, k β€ 2 β
10^5) β the length of array a and the size of "alphabet", i. e., the upper bound on the numbers you may use to replace -1.
The second line contains n integers a_1, a_2, ..., a_n (a_i = -1 or 1 β€ a_i β€ k) β the array a.
Output
Print one integer β the number of good arrays you can get, modulo 998244353.
Examples
Input
2 3
-1 -1
Output
9
Input
5 2
1 -1 -1 1 2
Output
0
Input
5 3
1 -1 -1 1 2
Output
2
Input
4 200000
-1 -1 12345 -1
Output
735945883 | instruction | 0 | 103,737 | 12 | 207,474 |
Tags: combinatorics, divide and conquer, dp
Correct Solution:
```
import sys
mod = 998244353
def mult(a, b):
y = 1
for i in range(b):
y = y * a % mod
return y
def getS(cnt, k, b1, b2):
if b1 < b2:
b1, b2 = b2, b1
if b1 == b2 == 0:
s = mult(k - 1, cnt - 1)
s = s * k % mod
return s
if b2 == 0:
return mult(k - 1, cnt)
re = [k - 1] * cnt
for i in range(1, cnt):
re[i] = re[i - 1] * (k - 1) % mod
re[0] = k - (1 if b1 == b2 else 2)
# print(re)
tot = 0
mm = 1
for i in range(cnt - 1, -1, -1):
tot += mm * re[i]
mm *= -1
tot = (tot + mod) % mod
return tot
def solve(x, k):
n = len(x)
x = [0] + x + [0]
st = -1
rt = 1
for i in range(n + 2):
if x[i] != -1:
if st != -1:
rt = (rt * getS(i - st, k, x[st - 1], x[i])) % mod
st = -1
else:
if st == -1:
st = i
return rt
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
for i in range(0, n - 2):
if a[i] != -1 and a[i] == a[i + 2]:
print(0)
sys.exit(0)
even = solve(a[::2], k)
odd = solve(a[1::2], k)
print(even * odd % mod)
``` | output | 1 | 103,737 | 12 | 207,475 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's denote that some array b is bad if it contains a subarray b_l, b_{l+1}, ..., b_{r} of odd length more than 1 (l < r and r - l + 1 is odd) such that β i β \{0, 1, ..., r - l\} b_{l + i} = b_{r - i}.
If an array is not bad, it is good.
Now you are given an array a_1, a_2, ..., a_n. Some elements are replaced by -1. Calculate the number of good arrays you can obtain by replacing each -1 with some integer from 1 to k.
Since the answer can be large, print it modulo 998244353.
Input
The first line contains two integers n and k (2 β€ n, k β€ 2 β
10^5) β the length of array a and the size of "alphabet", i. e., the upper bound on the numbers you may use to replace -1.
The second line contains n integers a_1, a_2, ..., a_n (a_i = -1 or 1 β€ a_i β€ k) β the array a.
Output
Print one integer β the number of good arrays you can get, modulo 998244353.
Examples
Input
2 3
-1 -1
Output
9
Input
5 2
1 -1 -1 1 2
Output
0
Input
5 3
1 -1 -1 1 2
Output
2
Input
4 200000
-1 -1 12345 -1
Output
735945883 | instruction | 0 | 103,738 | 12 | 207,476 |
Tags: combinatorics, divide and conquer, dp
Correct Solution:
```
import sys
input = sys.stdin.readline
def compress(string):
n = len(string)
begin, cnt = 0, 0
ans = []
if n == 0:
return ans
for end in range(n + 1):
if end == n or string[begin] != string[end]:
ans.append((string[begin], cnt))
begin, cnt = end, 1
else:
cnt += 1
return ans
n, k = map(int, input().split())
a = list(map(int, input().split()))
MOD = 998244353
dp = [[0] * 2 for i in range(n + 1)]
dp[0][0] = 1
for i in range(n):
dp[i + 1][0] += dp[i][1]
dp[i + 1][0] %= MOD
dp[i + 1][1] += dp[i][0] * (k - 1)
dp[i + 1][1] += dp[i][1] * (k - 2)
dp[i + 1][1] %= MOD
dq = [0 for i in range(n + 1)]
dq[1] = k
for i in range(1, n):
dq[i + 1] += dq[i] * (k - 1)
dq[i + 1] %= MOD
odd = []
even = []
for i, val in enumerate(a):
if i % 2 == 0:
odd.append(val)
else:
even.append(val)
ans = 1
odd = compress(odd)
for i, (val, cnt) in enumerate(odd):
if val != -1 and cnt > 1:
ans = 0
continue
if val != -1:
continue
else:
if i == 0:
tmp = dq[cnt]
if i + 1 == len(odd):
ans *= tmp
ans %= MOD
else:
ans *= tmp * pow(k, MOD - 2, MOD) * (k - 1)
ans %= MOD
continue
tmp1, tmp2 = dp[cnt]
if cnt == 1:
if i + 1 == len(odd):
ans *= tmp2
ans %= MOD
elif odd[i + 1][0] == odd[i - 1][0]:
ans *= tmp2
ans %= MOD
elif odd[i + 1][0] != odd[i - 1][0]:
ans *= tmp2 * pow(k - 1, MOD - 2, MOD) * (k - 2)
ans %= MOD
else:
if i + 1 == len(odd):
ans *= (tmp1 + tmp2)
ans %= MOD
elif odd[i + 1][0] == odd[i - 1][0]:
ans *= tmp2
ans %= MOD
elif odd[i + 1][0] != odd[i - 1][0]:
ans *= tmp1 + tmp2 * pow(k - 1, MOD - 2, MOD) * (k - 2)
ans %= MOD
odd = compress(even)
for i, (val, cnt) in enumerate(odd):
if val != -1 and cnt > 1:
ans = 0
continue
if val != -1:
continue
else:
if i == 0:
tmp = dq[cnt]
if i + 1 == len(odd):
ans *= tmp
ans %= MOD
else:
ans *= tmp * pow(k, MOD - 2, MOD) * (k - 1)
ans %= MOD
continue
tmp1, tmp2 = dp[cnt]
if cnt == 1:
if i + 1 == len(odd):
ans *= tmp2
ans %= MOD
elif odd[i + 1][0] == odd[i - 1][0]:
ans *= tmp2
ans %= MOD
elif odd[i + 1][0] != odd[i - 1][0]:
ans *= tmp2 * pow(k - 1, MOD - 2, MOD) * (k - 2)
ans %= MOD
else:
if i + 1 == len(odd):
ans *= (tmp1 + tmp2)
ans %= MOD
elif odd[i + 1][0] == odd[i - 1][0]:
ans *= tmp2
ans %= MOD
elif odd[i + 1][0] != odd[i - 1][0]:
ans *= tmp1 + tmp2 * pow(k - 1, MOD - 2, MOD) * (k - 2)
ans %= MOD
print(ans % MOD)
``` | output | 1 | 103,738 | 12 | 207,477 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's denote that some array b is bad if it contains a subarray b_l, b_{l+1}, ..., b_{r} of odd length more than 1 (l < r and r - l + 1 is odd) such that β i β \{0, 1, ..., r - l\} b_{l + i} = b_{r - i}.
If an array is not bad, it is good.
Now you are given an array a_1, a_2, ..., a_n. Some elements are replaced by -1. Calculate the number of good arrays you can obtain by replacing each -1 with some integer from 1 to k.
Since the answer can be large, print it modulo 998244353.
Input
The first line contains two integers n and k (2 β€ n, k β€ 2 β
10^5) β the length of array a and the size of "alphabet", i. e., the upper bound on the numbers you may use to replace -1.
The second line contains n integers a_1, a_2, ..., a_n (a_i = -1 or 1 β€ a_i β€ k) β the array a.
Output
Print one integer β the number of good arrays you can get, modulo 998244353.
Examples
Input
2 3
-1 -1
Output
9
Input
5 2
1 -1 -1 1 2
Output
0
Input
5 3
1 -1 -1 1 2
Output
2
Input
4 200000
-1 -1 12345 -1
Output
735945883 | instruction | 0 | 103,739 | 12 | 207,478 |
Tags: combinatorics, divide and conquer, dp
Correct Solution:
```
def solve1(a, l, r, k, mod):
n = len(a)
if l == 0 and r == n:
return k * (k - 1) ** (n - 1)
if l == 0 or r == n:
return (k - 1) ** (r - l)
x = a[l - 1]
y = a[r]
dp0 = [0] * (r - l)
dp1 = [0] * (r - l)
if x != y:
dp0[0] = k - 2
dp1[0] = 1
else:
dp0[0] = k - 1
dp1[0] = 0
for i in range(1, (r - l)):
dp0[i] = dp0[i - 1] * (k - 2) + dp1[i - 1] * (k - 1)
dp1[i] = dp0[i - 1]
dp0[i]%=mod
dp1[i]%=mod
return dp0[-1]
def solve(a, k, mod):
n = len(a)
res = 1
i = 0
while i < n:
if i < n - 1 and a[i] != -1 and a[i] == a[i + 1]:
return 0
if a[i] != -1:
i += 1
continue
j = i
while j < n and a[i] == a[j]:
j += 1
res *= solve1(a, i, j, k, mod)
i = j
return res
n, k = map(int, (input().split()))
src = list(map(int, (input().split())))
e = src[0::2]
o = src[1::2]
mod = 998244353
print(solve(e, k, mod) * solve(o, k, mod) % mod)
``` | output | 1 | 103,739 | 12 | 207,479 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's denote that some array b is bad if it contains a subarray b_l, b_{l+1}, ..., b_{r} of odd length more than 1 (l < r and r - l + 1 is odd) such that β i β \{0, 1, ..., r - l\} b_{l + i} = b_{r - i}.
If an array is not bad, it is good.
Now you are given an array a_1, a_2, ..., a_n. Some elements are replaced by -1. Calculate the number of good arrays you can obtain by replacing each -1 with some integer from 1 to k.
Since the answer can be large, print it modulo 998244353.
Input
The first line contains two integers n and k (2 β€ n, k β€ 2 β
10^5) β the length of array a and the size of "alphabet", i. e., the upper bound on the numbers you may use to replace -1.
The second line contains n integers a_1, a_2, ..., a_n (a_i = -1 or 1 β€ a_i β€ k) β the array a.
Output
Print one integer β the number of good arrays you can get, modulo 998244353.
Examples
Input
2 3
-1 -1
Output
9
Input
5 2
1 -1 -1 1 2
Output
0
Input
5 3
1 -1 -1 1 2
Output
2
Input
4 200000
-1 -1 12345 -1
Output
735945883
Submitted Solution:
```
import sys
from array import array # noqa: F401
import typing as Tp # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
def main():
from itertools import groupby
n, k = map(int, input().split())
a = (-2, -2,) + tuple(map(int, input().split())) + (-2, -2,)
if any(a[i] != -1 and a[i] == a[j] for i, j in zip(range(0, n + 4, 2), range(2, n + 4, 2)))\
or any(a[i] != -1 and a[i] == a[j] for i, j in zip(range(1, n + 4, 2), range(3, n + 4, 2))):
print(0)
exit()
m = ((n + 1) >> 1) + 10
mod = 998244353
dp = [array('i', [0]) * m for _ in range(4)]
dp[0][1], dp[1][1], dp[2][1], dp[3][1] = k - 1, k - 2, k - 1, k
for i in range(2, m):
dp[0][i] = dp[1][i - 1] * (k - 1) % mod
dp[1][i] = (dp[0][i - 1] + dp[1][i - 1] * (k - 2)) % mod
dp[2][i] = dp[2][i - 1] * (k - 1) % mod
dp[3][i] = dp[2][i - 1] * k % mod
odd = [(key, len(tuple(val))) for key, val in groupby(a[::2])]
even = [(key, len(tuple(val))) for key, val in groupby(a[1::2])]
ans = array('i', [1])
for group in (odd, even):
for x, y, z in zip(group, group[1:], group[2:]):
if y[0] != -1:
continue
if x[0] == -2 and z[0] == -2:
ans[0] = ans[0] * dp[3][y[1]] % mod
elif x[0] == -2 or z[0] == -2:
ans[0] = ans[0] * dp[2][y[1]] % mod
elif x[0] == z[0]:
ans[0] = ans[0] * dp[0][y[1]] % mod
else:
ans[0] = ans[0] * dp[1][y[1]] % mod
print(ans[0])
if __name__ == '__main__':
main()
``` | instruction | 0 | 103,740 | 12 | 207,480 |
Yes | output | 1 | 103,740 | 12 | 207,481 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's denote that some array b is bad if it contains a subarray b_l, b_{l+1}, ..., b_{r} of odd length more than 1 (l < r and r - l + 1 is odd) such that β i β \{0, 1, ..., r - l\} b_{l + i} = b_{r - i}.
If an array is not bad, it is good.
Now you are given an array a_1, a_2, ..., a_n. Some elements are replaced by -1. Calculate the number of good arrays you can obtain by replacing each -1 with some integer from 1 to k.
Since the answer can be large, print it modulo 998244353.
Input
The first line contains two integers n and k (2 β€ n, k β€ 2 β
10^5) β the length of array a and the size of "alphabet", i. e., the upper bound on the numbers you may use to replace -1.
The second line contains n integers a_1, a_2, ..., a_n (a_i = -1 or 1 β€ a_i β€ k) β the array a.
Output
Print one integer β the number of good arrays you can get, modulo 998244353.
Examples
Input
2 3
-1 -1
Output
9
Input
5 2
1 -1 -1 1 2
Output
0
Input
5 3
1 -1 -1 1 2
Output
2
Input
4 200000
-1 -1 12345 -1
Output
735945883
Submitted Solution:
```
import io, os
input = io.StringIO(os.read(0, os.fstat(0).st_size).decode()).readline
MOD = 998244353
n, k = map(int, input().split())
a = list(map(int, input().split()))
grps = []
lt = None
for start in (0, 1):
for i in range(start, n, 2):
if a[i] == -1:
if lt is None:
if i - 2 < 0:
lt = -1
else:
lt = a[i - 2]
sz = 0
sz += 1
if i + 2 >= n:
grps.append((lt, -1, sz))
lt = None
elif a[i + 2] != -1:
grps.append((lt, a[i + 2], sz))
lt = None
ans = int(all(a[i] != a[i + 2] for i in range(n - 2) if a[i] != -1))
for lt, rt, sz in grps:
if sz == 1:
cur = k if lt == -1 and rt == -1 else k - 1 if lt == -1 or rt == -1 or lt == rt else k - 2
else:
eq, neq = 1 if lt == -1 else 0, 1
for _ in range(sz - 1):
eq, neq = neq * (k - 1) % MOD, (neq * (k - 2) + eq) % MOD
cur = neq * (k - 1) + eq if rt == -1 else neq * (k - 1) if lt == rt else neq * (k - 2) + eq
ans = ans * cur % MOD
print(ans)
``` | instruction | 0 | 103,741 | 12 | 207,482 |
Yes | output | 1 | 103,741 | 12 | 207,483 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's denote that some array b is bad if it contains a subarray b_l, b_{l+1}, ..., b_{r} of odd length more than 1 (l < r and r - l + 1 is odd) such that β i β \{0, 1, ..., r - l\} b_{l + i} = b_{r - i}.
If an array is not bad, it is good.
Now you are given an array a_1, a_2, ..., a_n. Some elements are replaced by -1. Calculate the number of good arrays you can obtain by replacing each -1 with some integer from 1 to k.
Since the answer can be large, print it modulo 998244353.
Input
The first line contains two integers n and k (2 β€ n, k β€ 2 β
10^5) β the length of array a and the size of "alphabet", i. e., the upper bound on the numbers you may use to replace -1.
The second line contains n integers a_1, a_2, ..., a_n (a_i = -1 or 1 β€ a_i β€ k) β the array a.
Output
Print one integer β the number of good arrays you can get, modulo 998244353.
Examples
Input
2 3
-1 -1
Output
9
Input
5 2
1 -1 -1 1 2
Output
0
Input
5 3
1 -1 -1 1 2
Output
2
Input
4 200000
-1 -1 12345 -1
Output
735945883
Submitted Solution:
```
# AC
import sys
from heapq import heappush, heappop
class Main:
def __init__(self):
self.buff = None
self.index = 0
def next(self):
if self.buff is None or self.index == len(self.buff):
self.buff = sys.stdin.readline().split()
self.index = 0
val = self.buff[self.index]
self.index += 1
return val
def next_int(self):
return int(self.next())
def pp(self, a, b, mod):
if b == 0:
return 1
tmp = self.pp(a, b // 2, mod)
tmp = tmp * tmp % mod
if b % 2 == 1:
tmp = tmp * a % mod
return tmp
def same(self, cc, k, mod):
t = 1
y = 0
for i in range(0, cc):
tt = y
yy = (t * (k - 1) + y * (k - 2)) % mod
y = yy
t = tt
return y
def diff(self, cc, k, mod):
t = 0
y = 1
for i in range(0, cc):
tt = y
yy = (t * (k - 1) + y * (k - 2)) % mod
y = yy
t = tt
return y
def solve(self):
n = self.next_int()
k = self.next_int()
mod = 998244353
pre = [-1, -1]
cc = [0, 0]
ans = 1
for i in range(0, n):
d = self.next_int()
ii = i % 2
if d == -1:
cc[ii] += 1
else:
if pre[ii] == -1:
ans = ans * self.pp(k - 1, cc[ii], mod) % mod
elif pre[ii] == d:
ans = ans * self.same(cc[ii], k, mod) % mod
else:
ans = ans * self.diff(cc[ii], k, mod) % mod
cc[ii] = 0
pre[ii] = d
if cc[0] == (n + 1) // 2:
ans = ans * k * self.pp(k - 1, cc[0] - 1, mod) % mod
else:
ans = ans * self.pp(k - 1, cc[0], mod) % mod
if cc[1] == n // 2:
ans = ans * k * self.pp(k - 1, cc[1] - 1, mod) % mod
else:
ans = ans * self.pp(k - 1, cc[1], mod) % mod
print(ans)
if __name__ == '__main__':
Main().solve()
``` | instruction | 0 | 103,742 | 12 | 207,484 |
Yes | output | 1 | 103,742 | 12 | 207,485 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's denote that some array b is bad if it contains a subarray b_l, b_{l+1}, ..., b_{r} of odd length more than 1 (l < r and r - l + 1 is odd) such that β i β \{0, 1, ..., r - l\} b_{l + i} = b_{r - i}.
If an array is not bad, it is good.
Now you are given an array a_1, a_2, ..., a_n. Some elements are replaced by -1. Calculate the number of good arrays you can obtain by replacing each -1 with some integer from 1 to k.
Since the answer can be large, print it modulo 998244353.
Input
The first line contains two integers n and k (2 β€ n, k β€ 2 β
10^5) β the length of array a and the size of "alphabet", i. e., the upper bound on the numbers you may use to replace -1.
The second line contains n integers a_1, a_2, ..., a_n (a_i = -1 or 1 β€ a_i β€ k) β the array a.
Output
Print one integer β the number of good arrays you can get, modulo 998244353.
Examples
Input
2 3
-1 -1
Output
9
Input
5 2
1 -1 -1 1 2
Output
0
Input
5 3
1 -1 -1 1 2
Output
2
Input
4 200000
-1 -1 12345 -1
Output
735945883
Submitted Solution:
```
import sys
input = sys.stdin.readline
mod=998244353
n,k=map(int,input().split())
A=list(map(int,input().split()))
A0=[A[i] for i in range(n) if i%2==0]
A1=[A[i] for i in range(n) if i%2==1]
for j in range(1,len(A0)):
if A0[j]!=-1 and A0[j]==A0[j-1]:
print(0)
sys.exit()
for j in range(1,len(A1)):
if A1[j]!=-1 and A1[j]==A1[j-1]:
print(0)
sys.exit()
OPENLIST=[]
j=0
L=len(A0)
while j<L:
OPEN=CLOSE=-10
if A0[j]==-1:
if j==0:
OPEN=-11
else:
OPEN=A0[j-1]
COUNT=0
while A0[j]==-1:
COUNT+=1
j+=1
if j==L:
CLOSE=-11
break
if OPEN==-11 and CLOSE==-11:
OPENLIST.append([COUNT,0])
elif OPEN==-11 or CLOSE==-11:
OPENLIST.append([COUNT,1])
else:
if A0[j]==OPEN:
OPENLIST.append([COUNT,3])
else:
OPENLIST.append([COUNT,2])
else:
j+=1
j=0
L=len(A1)
while j<L:
OPEN=CLOSE=-10
if A1[j]==-1:
if j==0:
OPEN=-11
else:
OPEN=A1[j-1]
COUNT=0
while A1[j]==-1:
COUNT+=1
j+=1
if j==L:
CLOSE=-11
break
if OPEN==-11 and CLOSE==-11:
OPENLIST.append([COUNT,0])
elif OPEN==-11 or CLOSE==-11:
OPENLIST.append([COUNT,1])
else:
if A1[j]==OPEN:
OPENLIST.append([COUNT,3])
else:
OPENLIST.append([COUNT,2])
else:
j+=1
ANS=1
for x,y in OPENLIST:
if y==0:
ANS=ANS*k*pow(k-1,x-1,mod)%mod
elif y==1:
ANS=ANS*pow(k-1,x,mod)%mod
elif y==2:
DP0=0
DP1=1
for r in range(x+1):
NDP0=DP1
NDP1=(DP0*(k-1)+DP1*(k-2))%mod
DP0=NDP0
DP1=NDP1
ANS=ANS*DP0%mod
else:
DP0=1
DP1=0
for r in range(x+1):
NDP0=DP1
NDP1=(DP0*(k-1)+DP1*(k-2))%mod
DP0=NDP0
DP1=NDP1
ANS=ANS*DP0%mod
print(ANS)
``` | instruction | 0 | 103,743 | 12 | 207,486 |
Yes | output | 1 | 103,743 | 12 | 207,487 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's denote that some array b is bad if it contains a subarray b_l, b_{l+1}, ..., b_{r} of odd length more than 1 (l < r and r - l + 1 is odd) such that β i β \{0, 1, ..., r - l\} b_{l + i} = b_{r - i}.
If an array is not bad, it is good.
Now you are given an array a_1, a_2, ..., a_n. Some elements are replaced by -1. Calculate the number of good arrays you can obtain by replacing each -1 with some integer from 1 to k.
Since the answer can be large, print it modulo 998244353.
Input
The first line contains two integers n and k (2 β€ n, k β€ 2 β
10^5) β the length of array a and the size of "alphabet", i. e., the upper bound on the numbers you may use to replace -1.
The second line contains n integers a_1, a_2, ..., a_n (a_i = -1 or 1 β€ a_i β€ k) β the array a.
Output
Print one integer β the number of good arrays you can get, modulo 998244353.
Examples
Input
2 3
-1 -1
Output
9
Input
5 2
1 -1 -1 1 2
Output
0
Input
5 3
1 -1 -1 1 2
Output
2
Input
4 200000
-1 -1 12345 -1
Output
735945883
Submitted Solution:
```
import io, os
input = io.StringIO(os.read(0, os.fstat(0).st_size).decode()).readline
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
MOD = 998244353
n, k = mi()
a = li()
grps = []
ok = 1
for i in range(1, n - 1):
if a[i - 1] == a[i + 1] and a[i - 1] != -1:
ok = 0
break
lt = sz = None
for i in range(0, n, 2):
if a[i] == -1:
if lt is None:
if i - 2 < 0:
lt = -1
else:
lt = a[i - 2]
sz = 0
sz += 1
if i + 2 >= n:
grps.append((lt, -1, sz))
lt = None
elif a[i + 2] != -1:
grps.append((lt, a[i + 2], sz))
lt = None
lt = sz = None
for i in range(1, n, 2):
if a[i] == -1:
if lt is None:
if i - 2 < 0:
lt = -1
else:
lt = a[i - 2]
sz = 0
sz += 1
if i + 2 >= n:
grps.append((lt, -1, sz))
lt = None
elif a[i + 2] != -1:
grps.append((lt, a[i + 2], sz))
lt = None
ans = ok
for lt, rt, sz in grps:
if sz == 1:
if lt == -1:
if rt == -1:
cur = k
else:
cur = k - 1
elif lt == rt:
cur = k - 1
else:
cur = k - 2
else:
if lt == -1:
eq, neq = 1, 1
else:
eq, neq = 0, 1
for _ in range(sz - 1):
eq2 = neq * (k - 1)
neq2 = neq * (k - 2) + eq
eq, neq = eq2 % MOD, neq2 % MOD
if rt == -1:
cur = neq * (k - 1) + eq
elif lt == rt:
cur = neq * (k - 1)
else:
cur = neq * (k - 2) + eq
ans = ans * cur % MOD
print(ans)
``` | instruction | 0 | 103,744 | 12 | 207,488 |
No | output | 1 | 103,744 | 12 | 207,489 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's denote that some array b is bad if it contains a subarray b_l, b_{l+1}, ..., b_{r} of odd length more than 1 (l < r and r - l + 1 is odd) such that β i β \{0, 1, ..., r - l\} b_{l + i} = b_{r - i}.
If an array is not bad, it is good.
Now you are given an array a_1, a_2, ..., a_n. Some elements are replaced by -1. Calculate the number of good arrays you can obtain by replacing each -1 with some integer from 1 to k.
Since the answer can be large, print it modulo 998244353.
Input
The first line contains two integers n and k (2 β€ n, k β€ 2 β
10^5) β the length of array a and the size of "alphabet", i. e., the upper bound on the numbers you may use to replace -1.
The second line contains n integers a_1, a_2, ..., a_n (a_i = -1 or 1 β€ a_i β€ k) β the array a.
Output
Print one integer β the number of good arrays you can get, modulo 998244353.
Examples
Input
2 3
-1 -1
Output
9
Input
5 2
1 -1 -1 1 2
Output
0
Input
5 3
1 -1 -1 1 2
Output
2
Input
4 200000
-1 -1 12345 -1
Output
735945883
Submitted Solution:
```
n, k = map(int, input().split())
nums = [int(x) for x in input().split()]
card = [k-2 if nu==-1 else 1 for nu in nums]
for i in range(2):
if nums[i] == -1:
card[i] += 1
for i in range(n-2, n):
if nums[i] == -1:
card[i] += 1
for i in range(1, n-1):
if nums[i-1] == nums[i+1] and nums[i+1]!= -1:
card[i] = 0
res = 1
for c in card:
res*=c
res %= 998244353
print(res)
``` | instruction | 0 | 103,745 | 12 | 207,490 |
No | output | 1 | 103,745 | 12 | 207,491 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's denote that some array b is bad if it contains a subarray b_l, b_{l+1}, ..., b_{r} of odd length more than 1 (l < r and r - l + 1 is odd) such that β i β \{0, 1, ..., r - l\} b_{l + i} = b_{r - i}.
If an array is not bad, it is good.
Now you are given an array a_1, a_2, ..., a_n. Some elements are replaced by -1. Calculate the number of good arrays you can obtain by replacing each -1 with some integer from 1 to k.
Since the answer can be large, print it modulo 998244353.
Input
The first line contains two integers n and k (2 β€ n, k β€ 2 β
10^5) β the length of array a and the size of "alphabet", i. e., the upper bound on the numbers you may use to replace -1.
The second line contains n integers a_1, a_2, ..., a_n (a_i = -1 or 1 β€ a_i β€ k) β the array a.
Output
Print one integer β the number of good arrays you can get, modulo 998244353.
Examples
Input
2 3
-1 -1
Output
9
Input
5 2
1 -1 -1 1 2
Output
0
Input
5 3
1 -1 -1 1 2
Output
2
Input
4 200000
-1 -1 12345 -1
Output
735945883
Submitted Solution:
```
input1 = input('').split(' ')
k = int(input1.pop())
n = int(input1.pop())
input2 = input('').split(' ')
lis11 = []
lis12 = []
lis21 = []
lis22 = []
lisk = []
long = 0
op = 0
for i in range(n):
if i % 2 == 0:
lis11.append(int(input2[i]))
else:
lis12.append(int(input2[i]))
tong = [0,1,k-1]
yi = [0,1,k-2]
for i in range((n+1)//2):
if lis11[i] != -1:
lis21.append(i)
if len(lis21)>1:
if (i-lis21[-2]) > long:
long = i-lis21[-2]
for i in range(n//2):
if lis12[i] != -1:
lis22.append(i)
if len(lis22)>1:
if (i-lis22[-2]) > long:
long = i-lis22[-2]
for i in range(3,long+1):
tong.append(int(yi[i-1])*(k-1)%998244353)
yi.append((int(yi[i-1])*(k-1)+tong[i-1])%998244353)
if lis21:
for i in range(lis21[0] - lis21[-1] + (n+1)//2 - 1):
lisk.append(k-1)
for i in range(1,len(lis21)):
if lis11[lis21[i]] == lis11[lis21[i-1]]:
lisk.append(tong[lis21[i]-lis21[i-1]])
else:
lisk.append(yi[lis21[i]-lis21[i-1]])
else:
lisk.append(k)
for i in range(1,(n+1)//2):
lisk.append(k-1)
if lis22:
for i in range(lis22[0] - lis22[-1] + n//2 - 1):
lisk.append(k-1)
for i in range(1,len(lis22)):
if lis12[lis22[i]] == lis12[lis22[i-1]]:
lisk.append(tong[lis22[i]-lis22[i-1]])
else:
lisk.append(yi[lis22[i]-lis22[i-1]])
else:
lisk.append(k)
for i in range(1,n//2):
lisk.append(k-1)
if len(lisk) > 0:
op=1
for i in range(len(lisk)):
op = op * lisk[i] % 998244353
print(op)
``` | instruction | 0 | 103,746 | 12 | 207,492 |
No | output | 1 | 103,746 | 12 | 207,493 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's denote that some array b is bad if it contains a subarray b_l, b_{l+1}, ..., b_{r} of odd length more than 1 (l < r and r - l + 1 is odd) such that β i β \{0, 1, ..., r - l\} b_{l + i} = b_{r - i}.
If an array is not bad, it is good.
Now you are given an array a_1, a_2, ..., a_n. Some elements are replaced by -1. Calculate the number of good arrays you can obtain by replacing each -1 with some integer from 1 to k.
Since the answer can be large, print it modulo 998244353.
Input
The first line contains two integers n and k (2 β€ n, k β€ 2 β
10^5) β the length of array a and the size of "alphabet", i. e., the upper bound on the numbers you may use to replace -1.
The second line contains n integers a_1, a_2, ..., a_n (a_i = -1 or 1 β€ a_i β€ k) β the array a.
Output
Print one integer β the number of good arrays you can get, modulo 998244353.
Examples
Input
2 3
-1 -1
Output
9
Input
5 2
1 -1 -1 1 2
Output
0
Input
5 3
1 -1 -1 1 2
Output
2
Input
4 200000
-1 -1 12345 -1
Output
735945883
Submitted Solution:
```
mod = 998244353
x=input().split(' ')
n=int(x[0])
k=int(x[1])
a=input().split(' ')
arr=[]
i=0
while i<n:
arr.append(int(a[i]))
i=i+1
taken=[0]*n
p=1
i=0
while i<n:
if arr[i]==-1:
left=-2
right=-2
if i-2>=0:
left=arr[i-2]
if i+2<n:
right=arr[i+2]
if left==right:
if left==-1:
p=(p*(k-1)%mod)%mod
elif left==-2:
p=(p*k)%mod
else:
p=(p*(k-1)%mod)%mod
break
else:
if left==-1:
if right==-2:
p=(p*(k-1)%mod)%mod
else:
v=k-taken[i-2]
if v!=0:
p=( ((p//v)*(v-1)*(k-2))%mod
+ ((p//v)*(1)*(k-1))%mod)%mod
else:
p=0
break
elif left==-2:
if right ==-1:
p=(p*k)%mod
else:
p=(p*(k-1)%mod)%mod
else:
if right ==-1 or right == -2:
p=(p*(k-1)%mod)%mod
else:
p=(p*(k-2)%mod)%mod
if left==-1:
taken[i]=1
else:
taken[i]=0
else:
left=-1
right=-1
if i-2>=0:
left=arr[i-2]
if i+2<n:
right=arr[i+2]
if left==arr[i] or right==arr[i]:
p=0
break
i=i+1
print(p)
``` | instruction | 0 | 103,747 | 12 | 207,494 |
No | output | 1 | 103,747 | 12 | 207,495 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of length n that has a special condition: every element in this array has at most 7 divisors. Find the length of the shortest non-empty subsequence of this array product of whose elements is a perfect square.
A sequence a is a subsequence of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements.
Input
The first line contains an integer n (1 β€ n β€ 10^5) β the length of a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
Output the length of the shortest non-empty subsequence of a product of whose elements is a perfect square. If there are several shortest subsequences, you can find any of them. If there's no such subsequence, print "-1".
Examples
Input
3
1 4 6
Output
1
Input
4
2 3 6 6
Output
2
Input
3
6 15 10
Output
3
Input
4
2 3 5 7
Output
-1
Note
In the first sample, you can choose a subsequence [1].
In the second sample, you can choose a subsequence [6, 6].
In the third sample, you can choose a subsequence [6, 15, 10].
In the fourth sample, there is no such subsequence. | instruction | 0 | 103,834 | 12 | 207,668 |
Tags: brute force, dfs and similar, graphs, number theory, shortest paths
Correct Solution:
```
import sys
input = sys.stdin.readline
n=int(input())
A=list(map(int,input().split()))
Primes=(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997)
E=[[] for i in range(10**6)]
U=set()
for i in range(n):
a=A[i]
X=[]
for p in Primes:
while a%(p*p)==0:
a//=p*p
if a%p==0:
a//=p
X.append(p)
if a!=1:
X.append(a)
if a==1 and X==[]:
print(1)
sys.exit()
elif len(X)==1:
E[1].append(X[0])
E[X[0]].append(1)
U.add(X[0])
else:
E[X[0]].append(X[1])
E[X[1]].append(X[0])
U.add(X[0]*X[1])
if len(A)!=len(U):
ANS=2
else:
ANS=n+5
D=[]
E2=[]
for i in range(10**6):
if E[i]==[]:
continue
D.append(i)
E2.append(E[i])
DD={x:i for i,x in enumerate(D)}
for i in range(len(E2)):
for k in range(len(E2[i])):
E2[i][k]=DD[E2[i][k]]
from collections import deque
for start in range(min(170,len(E2))):
if E2[start]==[]:
continue
Q=deque([start])
D=[0]*len(E2)
FROM=[0]*len(E2)
D[start]=1
while Q:
x=Q.popleft()
if D[x]*2-2>=ANS:
break
for to in E2[x]:
if to==FROM[x]:
continue
elif D[to]==0:
Q.append(to)
FROM[to]=x
D[to]=D[x]+1
else:
#print(start,x,to,D[x],D[to])
ANS=min(ANS,D[x]+D[to]-1)
if ANS>len(A):
print(-1)
else:
print(ANS)
``` | output | 1 | 103,834 | 12 | 207,669 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of length n that has a special condition: every element in this array has at most 7 divisors. Find the length of the shortest non-empty subsequence of this array product of whose elements is a perfect square.
A sequence a is a subsequence of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements.
Input
The first line contains an integer n (1 β€ n β€ 10^5) β the length of a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
Output the length of the shortest non-empty subsequence of a product of whose elements is a perfect square. If there are several shortest subsequences, you can find any of them. If there's no such subsequence, print "-1".
Examples
Input
3
1 4 6
Output
1
Input
4
2 3 6 6
Output
2
Input
3
6 15 10
Output
3
Input
4
2 3 5 7
Output
-1
Note
In the first sample, you can choose a subsequence [1].
In the second sample, you can choose a subsequence [6, 6].
In the third sample, you can choose a subsequence [6, 15, 10].
In the fourth sample, there is no such subsequence. | instruction | 0 | 103,835 | 12 | 207,670 |
Tags: brute force, dfs and similar, graphs, number theory, shortest paths
Correct Solution:
```
from collections import deque,defaultdict
import sys
input=sys.stdin.readline
def make_mf():
res=[-1]*(mxa+5)
ptoi=[0]*(mxa+5)
pn=1
for i in range(2,mxa+5):
if res[i]==-1:
res[i]=i
ptoi[i]=pn
pn+=1
for j in range(i**2,mxa+5,i):
if res[j]==-1:res[j]=i
return res,ptoi,pn
def make_to():
to=[[] for _ in range(pn)]
ss=set()
for a in aa:
pp=[]
while a>1:
p=mf[a]
e=0
while p==mf[a]:
a//=p
e^=1
if e:pp.append(p)
if len(pp)==0:ext(1)
elif len(pp)==1:pp.append(1)
u,v=ptoi[pp[0]],ptoi[pp[1]]
to[u].append(v)
to[v].append(u)
if pp[0]**2<=mxa:ss.add(u)
if pp[1]**2<=mxa:ss.add(v)
return to,ss
def ext(ans):
print(ans)
exit()
def solve():
ans=inf
for u in ss:
ans=bfs(u,ans)
if ans==inf:ans=-1
return ans
def bfs(u,ans):
dist=[-1]*pn
q=deque()
q.append((u,0,-1))
dist[u]=0
while q:
u,d,pu=q.popleft()
if d*2>=ans:break
for v in to[u]:
if v==pu:continue
if dist[v]!=-1:ans=min(ans,d+dist[v]+1)
dist[v]=d+1
q.append((v,d+1,u))
return ans
inf=10**9
n=int(input())
aa=list(map(int,input().split()))
mxa=max(aa)
mf,ptoi,pn=make_mf()
to,ss=make_to()
print(solve())
``` | output | 1 | 103,835 | 12 | 207,671 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of length n that has a special condition: every element in this array has at most 7 divisors. Find the length of the shortest non-empty subsequence of this array product of whose elements is a perfect square.
A sequence a is a subsequence of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements.
Input
The first line contains an integer n (1 β€ n β€ 10^5) β the length of a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
Output the length of the shortest non-empty subsequence of a product of whose elements is a perfect square. If there are several shortest subsequences, you can find any of them. If there's no such subsequence, print "-1".
Examples
Input
3
1 4 6
Output
1
Input
4
2 3 6 6
Output
2
Input
3
6 15 10
Output
3
Input
4
2 3 5 7
Output
-1
Note
In the first sample, you can choose a subsequence [1].
In the second sample, you can choose a subsequence [6, 6].
In the third sample, you can choose a subsequence [6, 15, 10].
In the fourth sample, there is no such subsequence. | instruction | 0 | 103,836 | 12 | 207,672 |
Tags: brute force, dfs and similar, graphs, number theory, shortest paths
Correct Solution:
```
from collections import deque
import sys
input=sys.stdin.readline
def make_mf():
res=[-1]*(mxa+5)
ptoi=[0]*(mxa+5)
pn=1
for i in range(2,mxa+5):
if res[i]==-1:
res[i]=i
ptoi[i]=pn
pn+=1
for j in range(i**2,mxa+5,i):
if res[j]==-1:res[j]=i
return res,ptoi,pn
def make_to():
to=[[] for _ in range(pn)]
ss=set()
for a in aa:
pp=[]
while a>1:
p=mf[a]
e=0
while p==mf[a]:
a//=p
e^=1
if e:pp.append(p)
if len(pp)==0:ext(1)
elif len(pp)==1:pp.append(1)
u,v=ptoi[pp[0]],ptoi[pp[1]]
to[u].append(v)
to[v].append(u)
if pp[0]**2<=mxa:ss.add(u)
if pp[1]**2<=mxa:ss.add(v)
return to,ss
def ext(ans):
print(ans)
exit()
def solve():
ans=inf
for u in ss:
ans=bfs(u,ans)
if ans==inf:ans=-1
return ans
def bfs(u,ans):
dist=[-1]*pn
q=deque()
q.append((u,0,-1))
dist[u]=0
while q:
u,d,pu=q.popleft()
if d*2>=ans:break
for v in to[u]:
if v==pu:continue
if dist[v]!=-1:ans=min(ans,d+dist[v]+1)
dist[v]=d+1
q.append((v,d+1,u))
return ans
inf=10**9
n=int(input())
aa=list(map(int,input().split()))
mxa=max(aa)
mf,ptoi,pn=make_mf()
to,ss=make_to()
print(solve())
``` | output | 1 | 103,836 | 12 | 207,673 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length n that has a special condition: every element in this array has at most 7 divisors. Find the length of the shortest non-empty subsequence of this array product of whose elements is a perfect square.
A sequence a is a subsequence of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements.
Input
The first line contains an integer n (1 β€ n β€ 10^5) β the length of a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
Output the length of the shortest non-empty subsequence of a product of whose elements is a perfect square. If there are several shortest subsequences, you can find any of them. If there's no such subsequence, print "-1".
Examples
Input
3
1 4 6
Output
1
Input
4
2 3 6 6
Output
2
Input
3
6 15 10
Output
3
Input
4
2 3 5 7
Output
-1
Note
In the first sample, you can choose a subsequence [1].
In the second sample, you can choose a subsequence [6, 6].
In the third sample, you can choose a subsequence [6, 15, 10].
In the fourth sample, there is no such subsequence.
Submitted Solution:
```
from collections import deque,defaultdict
import sys
input=sys.stdin.readline
def make_mf():
res=[-1]*mxa
for i in range(2,mxa):
if res[i]==-1:
res[i]=i
for j in range(i**2,mxa,i):
if res[j]==-1:res[j]=i
return res
def make_to():
to=defaultdict(set)
ss=set()
ans=inf
for a in aa:
pp=[]
while a>1:
p=mf[a]
e=0
while p==mf[a]:
a//=p
e^=1
if e:pp.append(p)
if len(pp)==0:ext(1)
elif len(pp)==1:pp.append(1)
u,v=pp
if v in to[u]:ans=2
to[u].add(v)
to[v].add(u)
if u**2<=mxa:ss.add(u)
if v**2<=mxa:ss.add(v)
return to,ss,ans
def ext(ans):
print(ans)
exit()
def solve(ans):
for u in ss:
ans=bfs(u,ans)
if ans==inf:ans=-1
return ans
def bfs(u,ans):
dist=defaultdict(int)
q=deque()
q.append((u,0,-1))
dist[u]=0
while q:
u,d,pu=q.popleft()
if d*2>=ans:break
for v in to[u]:
if v==pu:continue
if v in dist:
ans=min(ans,d+dist[v]+1)
dist[v]=d+1
q.append((v,d+1,u))
return ans
inf=10**9
n=int(input())
aa=map(int,input().split())
mxa=max(aa)
mf=make_mf()
to,ss,ans=make_to()
if ans==2:ext(2)
print(solve(ans))
``` | instruction | 0 | 103,837 | 12 | 207,674 |
No | output | 1 | 103,837 | 12 | 207,675 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length n that has a special condition: every element in this array has at most 7 divisors. Find the length of the shortest non-empty subsequence of this array product of whose elements is a perfect square.
A sequence a is a subsequence of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements.
Input
The first line contains an integer n (1 β€ n β€ 10^5) β the length of a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
Output the length of the shortest non-empty subsequence of a product of whose elements is a perfect square. If there are several shortest subsequences, you can find any of them. If there's no such subsequence, print "-1".
Examples
Input
3
1 4 6
Output
1
Input
4
2 3 6 6
Output
2
Input
3
6 15 10
Output
3
Input
4
2 3 5 7
Output
-1
Note
In the first sample, you can choose a subsequence [1].
In the second sample, you can choose a subsequence [6, 6].
In the third sample, you can choose a subsequence [6, 15, 10].
In the fourth sample, there is no such subsequence.
Submitted Solution:
```
import sys
readline = sys.stdin.readline
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
n = 10**6 + 5
p = [-1] * n
for i in range(2, n):
if p[i] < 0:
for j in range(i*2, n, i):
if p[j] < 0:
p[j] = i
def solve():
mod = 998244353
n = ni()
a = nl()
c = 0
d = dict()
d[0] = -1
ans = n + 5
for i in range(n):
x = a[i]
while p[x] > 0:
c ^= p[x]
x //= p[x]
if x > 1:
c ^= x
if c in d:
ans = min(ans, i - d[c])
d[c] = i
print(-1 if ans > n else ans)
return
solve()
# T = ni()
# for _ in range(T):
# solve()
``` | instruction | 0 | 103,838 | 12 | 207,676 |
No | output | 1 | 103,838 | 12 | 207,677 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length n that has a special condition: every element in this array has at most 7 divisors. Find the length of the shortest non-empty subsequence of this array product of whose elements is a perfect square.
A sequence a is a subsequence of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements.
Input
The first line contains an integer n (1 β€ n β€ 10^5) β the length of a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
Output the length of the shortest non-empty subsequence of a product of whose elements is a perfect square. If there are several shortest subsequences, you can find any of them. If there's no such subsequence, print "-1".
Examples
Input
3
1 4 6
Output
1
Input
4
2 3 6 6
Output
2
Input
3
6 15 10
Output
3
Input
4
2 3 5 7
Output
-1
Note
In the first sample, you can choose a subsequence [1].
In the second sample, you can choose a subsequence [6, 6].
In the third sample, you can choose a subsequence [6, 15, 10].
In the fourth sample, there is no such subsequence.
Submitted Solution:
```
def prime_numbers():
numbers = list()
p = 2
while (p < 1000):
for n in numbers:
if p % n == 0:
break
else:
numbers.append(p)
p += 1
return numbers
def solve():
primes = prime_numbers()
n = int(input())
A = list(map(int, input().split()))
#n = 4
#A = [2, 3, 6, 6]
A_primes_more1000 = list()
B = [set() for i in range(n)]
D = set()
solved = False
for i in range(n):
a = A[i]
if a == 1:
solved = True
break
for d in primes:
count = 0
while (a % d == 0):
a //= d
count += 1
count = count % 2
if count:
B[i].add(d)
D.add(d)
if a > 1:
B[i].add(a)
D.add(a)
if len(B[i]) == 0:
solved = True
break
if solved:
print(1)
return
if len(A_primes_more1000) > len(set(A_primes_more1000)):
print(2)
return
D = sorted(list(D))
len_D = len(D)
Matrix = [[0 for j in range(len_D)] for i in range(n)]
for i in range(n):
for j in range(len_D):
if D[j] in B[i]:
Matrix[i][j] = 1
V = [0 for i in range(n)]
for j in range(min(len_D, n)):
if Matrix[j][j] == 0:
swapped = False
for k in range(j + 1, n):
if Matrix[k][j] != 0:
Matrix[k], Matrix[j] = Matrix[j], Matrix[k]
V[j], V[k] = V[k], V[j]
swapped = True
break
else:
solved = j + 1
break
for k in range(j + 1, n):
for i in range(len_D):
Matrix[k][i] = (Matrix[k][i] + Matrix[j][i]) % 2
V[k] = (V[k] + V[j]) % 2
j = min(len_D, n)
if solved:
print(solved)
return
#print(Matrix)
#print(j)
#print(V)
for k in range(j, n):
if V[k] != 0:
solved = -1
break
if solved:
print(solved)
return
print(j)
solve()
``` | instruction | 0 | 103,839 | 12 | 207,678 |
No | output | 1 | 103,839 | 12 | 207,679 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length n that has a special condition: every element in this array has at most 7 divisors. Find the length of the shortest non-empty subsequence of this array product of whose elements is a perfect square.
A sequence a is a subsequence of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements.
Input
The first line contains an integer n (1 β€ n β€ 10^5) β the length of a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
Output the length of the shortest non-empty subsequence of a product of whose elements is a perfect square. If there are several shortest subsequences, you can find any of them. If there's no such subsequence, print "-1".
Examples
Input
3
1 4 6
Output
1
Input
4
2 3 6 6
Output
2
Input
3
6 15 10
Output
3
Input
4
2 3 5 7
Output
-1
Note
In the first sample, you can choose a subsequence [1].
In the second sample, you can choose a subsequence [6, 6].
In the third sample, you can choose a subsequence [6, 15, 10].
In the fourth sample, there is no such subsequence.
Submitted Solution:
```
import math
n=int(input())
a=list(map(int,(input().split())))
k=0
count=0
for i in range(n):
sum=1
k=a[i]
for j in range(1,k+1):
if(a[i]%j==0):
sum=sum*j
root = math.sqrt(sum)
if(root ==k):
count=count+1
if(count==0):
print(-1)
else:
print(count)
``` | instruction | 0 | 103,840 | 12 | 207,680 |
No | output | 1 | 103,840 | 12 | 207,681 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of 2n distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its 2 neighbours.
More formally, find an array b, such that:
* b is a permutation of a.
* For every i from 1 to 2n, b_i β \frac{b_{i-1}+b_{i+1}}{2}, where b_0 = b_{2n} and b_{2n+1} = b_1.
It can be proved that under the constraints of this problem, such array b always exists.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) β the number of testcases. The description of testcases follows.
The first line of each testcase contains a single integer n (1 β€ n β€ 25).
The second line of each testcase contains 2n integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^9) β elements of the array.
Note that there is no limit to the sum of n over all testcases.
Output
For each testcase, you should output 2n integers, b_1, b_2, β¦ b_{2n}, for which the conditions from the statement are satisfied.
Example
Input
3
3
1 2 3 4 5 6
2
123 456 789 10
1
6 9
Output
3 1 4 2 5 6
123 10 456 789
9 6
Note
In the first testcase, array [3, 1, 4, 2, 5, 6] works, as it's a permutation of [1, 2, 3, 4, 5, 6], and (3+4)/(2)β 1, (1+2)/(2)β 4, (4+5)/(2)β 2, (2+6)/(2)β 5, (5+3)/(2)β 6, (6+1)/(2)β 3. | instruction | 0 | 103,923 | 12 | 207,846 |
Tags: constructive algorithms, sortings
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
l = sorted(list(map(int, input().split())), reverse=True)
ans = [0]*(2*n)
for i, j in enumerate(l):
if i < n:
ans[2*i+1] = j
else:
x = n - i
ans[2*x] = j
print(*ans)
``` | output | 1 | 103,923 | 12 | 207,847 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of 2n distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its 2 neighbours.
More formally, find an array b, such that:
* b is a permutation of a.
* For every i from 1 to 2n, b_i β \frac{b_{i-1}+b_{i+1}}{2}, where b_0 = b_{2n} and b_{2n+1} = b_1.
It can be proved that under the constraints of this problem, such array b always exists.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) β the number of testcases. The description of testcases follows.
The first line of each testcase contains a single integer n (1 β€ n β€ 25).
The second line of each testcase contains 2n integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^9) β elements of the array.
Note that there is no limit to the sum of n over all testcases.
Output
For each testcase, you should output 2n integers, b_1, b_2, β¦ b_{2n}, for which the conditions from the statement are satisfied.
Example
Input
3
3
1 2 3 4 5 6
2
123 456 789 10
1
6 9
Output
3 1 4 2 5 6
123 10 456 789
9 6
Note
In the first testcase, array [3, 1, 4, 2, 5, 6] works, as it's a permutation of [1, 2, 3, 4, 5, 6], and (3+4)/(2)β 1, (1+2)/(2)β 4, (4+5)/(2)β 2, (2+6)/(2)β 5, (5+3)/(2)β 6, (6+1)/(2)β 3. | instruction | 0 | 103,924 | 12 | 207,848 |
Tags: constructive algorithms, sortings
Correct Solution:
```
t=int(input())
for _ in range(t):
n=int(input())
arr=list(map(int,input().split(' ')))
arr.sort()
l=[]
i=0
j=2*n-1
while(i<j):
l.append(arr[i])
l.append(arr[j])
i=i+1
j=j-1
for k in l:
print(k,end=' ')
``` | output | 1 | 103,924 | 12 | 207,849 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of 2n distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its 2 neighbours.
More formally, find an array b, such that:
* b is a permutation of a.
* For every i from 1 to 2n, b_i β \frac{b_{i-1}+b_{i+1}}{2}, where b_0 = b_{2n} and b_{2n+1} = b_1.
It can be proved that under the constraints of this problem, such array b always exists.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) β the number of testcases. The description of testcases follows.
The first line of each testcase contains a single integer n (1 β€ n β€ 25).
The second line of each testcase contains 2n integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^9) β elements of the array.
Note that there is no limit to the sum of n over all testcases.
Output
For each testcase, you should output 2n integers, b_1, b_2, β¦ b_{2n}, for which the conditions from the statement are satisfied.
Example
Input
3
3
1 2 3 4 5 6
2
123 456 789 10
1
6 9
Output
3 1 4 2 5 6
123 10 456 789
9 6
Note
In the first testcase, array [3, 1, 4, 2, 5, 6] works, as it's a permutation of [1, 2, 3, 4, 5, 6], and (3+4)/(2)β 1, (1+2)/(2)β 4, (4+5)/(2)β 2, (2+6)/(2)β 5, (5+3)/(2)β 6, (6+1)/(2)β 3. | instruction | 0 | 103,925 | 12 | 207,850 |
Tags: constructive algorithms, sortings
Correct Solution:
```
# cook your dish here
# from math import factorial, ceil, pow, sqrt, floor, gcd
from sys import stdin, stdout
#from collections import defaultdict, Counter, deque
#from bisect import bisect_left, bisect_right
# import sympy
# from itertools import permutations
# import numpy as np
# n = int(stdin.readline())
# stdout.write(str())
# s = stdin.readline().strip('\n')
# map(int, stdin.readline().split())
# l = list(map(int, stdin.readline().split()))
#for _ in range(1):
for _ in range(int(stdin.readline())):
n = int(stdin.readline())
l = list(map(int, stdin.readline().split()))
l.sort()
shan = []
i = 0
j = 2*n-1
while i<j:
shan.append(l[i])
shan.append(l[j])
i += 1
j -= 1
print(*shan)
``` | output | 1 | 103,925 | 12 | 207,851 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of 2n distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its 2 neighbours.
More formally, find an array b, such that:
* b is a permutation of a.
* For every i from 1 to 2n, b_i β \frac{b_{i-1}+b_{i+1}}{2}, where b_0 = b_{2n} and b_{2n+1} = b_1.
It can be proved that under the constraints of this problem, such array b always exists.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) β the number of testcases. The description of testcases follows.
The first line of each testcase contains a single integer n (1 β€ n β€ 25).
The second line of each testcase contains 2n integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^9) β elements of the array.
Note that there is no limit to the sum of n over all testcases.
Output
For each testcase, you should output 2n integers, b_1, b_2, β¦ b_{2n}, for which the conditions from the statement are satisfied.
Example
Input
3
3
1 2 3 4 5 6
2
123 456 789 10
1
6 9
Output
3 1 4 2 5 6
123 10 456 789
9 6
Note
In the first testcase, array [3, 1, 4, 2, 5, 6] works, as it's a permutation of [1, 2, 3, 4, 5, 6], and (3+4)/(2)β 1, (1+2)/(2)β 4, (4+5)/(2)β 2, (2+6)/(2)β 5, (5+3)/(2)β 6, (6+1)/(2)β 3. | instruction | 0 | 103,926 | 12 | 207,852 |
Tags: constructive algorithms, sortings
Correct Solution:
```
#Fast I/O
import sys,os
import math
# To enable the file I/O i the below 2 lines are uncommented.
# read from in.txt if uncommented
if os.path.exists('in.txt'): sys.stdin=open('in.txt','r')
# will print on Console if file I/O is not activated
#if os.path.exists('out.txt'): sys.stdout=open('out.txt', 'w')
# inputs template
from io import BytesIO, IOBase
def main():
for _ in range(int(input())):
n=int(input())
arr=list(MI())
arr.sort()
out=[arr[0]]
for i in range(1,n):
out.append(arr[2*i])
out.append(arr[2*i-1])
out.append(arr[-1])
print(*out)
# Sample Inputs/Output
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#for array of integers
def MI():return (map(int,input().split()))
# endregion
#for fast output, always take string
def outP(var): sys.stdout.write(str(var)+'\n')
# end of any user-defined functions
MOD=10**9+7
mod=998244353
# main functions for execution of the program.
if __name__ == '__main__':
#This doesn't works here but works wonders when submitted on CodeChef or CodeForces
main()
``` | output | 1 | 103,926 | 12 | 207,853 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of 2n distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its 2 neighbours.
More formally, find an array b, such that:
* b is a permutation of a.
* For every i from 1 to 2n, b_i β \frac{b_{i-1}+b_{i+1}}{2}, where b_0 = b_{2n} and b_{2n+1} = b_1.
It can be proved that under the constraints of this problem, such array b always exists.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) β the number of testcases. The description of testcases follows.
The first line of each testcase contains a single integer n (1 β€ n β€ 25).
The second line of each testcase contains 2n integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^9) β elements of the array.
Note that there is no limit to the sum of n over all testcases.
Output
For each testcase, you should output 2n integers, b_1, b_2, β¦ b_{2n}, for which the conditions from the statement are satisfied.
Example
Input
3
3
1 2 3 4 5 6
2
123 456 789 10
1
6 9
Output
3 1 4 2 5 6
123 10 456 789
9 6
Note
In the first testcase, array [3, 1, 4, 2, 5, 6] works, as it's a permutation of [1, 2, 3, 4, 5, 6], and (3+4)/(2)β 1, (1+2)/(2)β 4, (4+5)/(2)β 2, (2+6)/(2)β 5, (5+3)/(2)β 6, (6+1)/(2)β 3. | instruction | 0 | 103,927 | 12 | 207,854 |
Tags: constructive algorithms, sortings
Correct Solution:
```
from random import random
def good(a,n):
for i in range(1,2*n-1):
if a[i-1] + a[i+1] == a[i]*2:
return False
if a[0]*2 == a[1] + a[-1]:
return False
if a[-1]*2 == a[-2] + a[0]:
return False
return True
for nt in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
while not good(a,n):
a = sorted(a,key=lambda x:random())
print(*a)
``` | output | 1 | 103,927 | 12 | 207,855 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of 2n distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its 2 neighbours.
More formally, find an array b, such that:
* b is a permutation of a.
* For every i from 1 to 2n, b_i β \frac{b_{i-1}+b_{i+1}}{2}, where b_0 = b_{2n} and b_{2n+1} = b_1.
It can be proved that under the constraints of this problem, such array b always exists.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) β the number of testcases. The description of testcases follows.
The first line of each testcase contains a single integer n (1 β€ n β€ 25).
The second line of each testcase contains 2n integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^9) β elements of the array.
Note that there is no limit to the sum of n over all testcases.
Output
For each testcase, you should output 2n integers, b_1, b_2, β¦ b_{2n}, for which the conditions from the statement are satisfied.
Example
Input
3
3
1 2 3 4 5 6
2
123 456 789 10
1
6 9
Output
3 1 4 2 5 6
123 10 456 789
9 6
Note
In the first testcase, array [3, 1, 4, 2, 5, 6] works, as it's a permutation of [1, 2, 3, 4, 5, 6], and (3+4)/(2)β 1, (1+2)/(2)β 4, (4+5)/(2)β 2, (2+6)/(2)β 5, (5+3)/(2)β 6, (6+1)/(2)β 3. | instruction | 0 | 103,928 | 12 | 207,856 |
Tags: constructive algorithms, sortings
Correct Solution:
```
import sys
input = sys.stdin.buffer.readline
for _ in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
a.sort()
b = [0]*(2*n)
for i in range(n):
b[2*i] = a[i]
for i in range(n):
b[2*i+1] = a[2*n-1-i]
print(*b)
``` | output | 1 | 103,928 | 12 | 207,857 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.