context stringlengths 88 7.54k | groundtruth stringlengths 9 28.8k | groundtruth_language stringclasses 3
values | type stringclasses 2
values | code_test_cases listlengths 1 565 ⌀ | dataset stringclasses 6
values | code_language stringclasses 1
value | difficulty float64 0 1 ⌀ | mid stringlengths 32 32 |
|---|---|---|---|---|---|---|---|---|
You are given a Young diagram.
Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1).
<image> Young diagram for a=[3,2,2,2,1].
Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 recta... | n = int(input())
a = [int(i) for i in input().split()]
m = [0,0]
for i in range(n):
m[0] += a[i] // 2
m[1] += a[i] // 2
if a[i] % 2 != 0:
m[i % 2] += 1
print(int(min(m[0], m[1])))
| python | code_algorithm | [
{
"input": "5\n3 2 2 2 1\n",
"output": "4\n"
},
{
"input": "1\n300000\n",
"output": "150000\n"
},
{
"input": "10\n9 8 7 7 6 4 3 2 1 1\n",
"output": "23\n"
},
{
"input": "100\n1980 1932 1906 1898 1892 1883 1877 1858 1842 1833 1777 1710 1689 1678 1660 1653 1648 1647 1644 1639 1... | code_contests | python | 0 | 87d3b5c03b922f9a18f79eb671e32105 |
You are given two integers n and d. You need to construct a rooted binary tree consisting of n vertices with a root at the vertex 1 and the sum of depths of all vertices equals to d.
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex v is the last diffe... | t=int(input())
import math as m
def print_tree(l):
t=[{'p':None, 'c':[]} for i in range(sum(l))]
l2={i:[] for i in range(len(l))}
j=0
for i in range(sum(l)):
l2[j].append(i+1)
if len(l2[j])==l[j]:
j+=1
for i in range(1,len(l)):
p=0
for n in l2[i]:
... | python | code_algorithm | [
{
"input": "3\n5 7\n10 19\n10 18\n",
"output": "YES\n1 1 2 4 \nYES\n1 1 2 2 3 3 4 4 5 \nNO\n"
},
{
"input": "7\n15 49\n13 77\n18 65\n9 30\n4 6\n11 44\n577 4729\n",
"output": "YES\n1 1 2 2 3 3 4 4 5 8 11 12 13 14 \nYES\n1 2 3 4 5 6 7 8 9 10 11 11 \nYES\n1 1 2 2 3 3 4 4 5 5 8 8 12 14 15 16 17 \nYE... | code_contests | python | 0 | 675040272fe82c4037c5b51a90d36dd1 |
A and B are preparing themselves for programming contests.
To train their logical thinking and solve problems better, A and B decided to play chess. During the game A wondered whose position is now stronger.
For each chess piece we know its weight:
* the queen's weight is 9,
* the rook's weight is 5,
* the ... | # -*- coding: utf-8 -*-
"""
Created on Thu Nov 8 10:34:19 2018
@author: Quaint Sun
"""
line=[]
t=0
while t<8:
line=line+list(input())
t=t+1
white=line.count('Q')*9+line.count('R')*5+line.count('B')*3+line.count('N')*3+line.count('P')
black=line.count('q')*9+line.count('r')*5+line.count('b')*3+line.count... | python | code_algorithm | [
{
"input": "rnbqkbnr\npppppppp\n........\n........\n........\n........\nPPPPPPPP\nRNBQKBNR\n",
"output": "Draw\n"
},
{
"input": "rppppppr\n...k....\n........\n........\n........\n........\nK...Q...\n........\n",
"output": "Black\n"
},
{
"input": "...QK...\n........\n........\n........\n.... | code_contests | python | 0.2 | 2badf40a89671642b1420cd33c66bcd8 |
Fangy the little walrus, as all the modern walruses, loves to communicate via text messaging. One day he faced the following problem: When he sends large texts, they are split into parts each containing n characters (which is the size of one text message). Thus, whole sentences and words get split!
Fangy did not like ... | import re
n=int(input())
ans,sumL=1,0
for s in re.split("[.?!]", input()):
s=s.strip()+'.'
L=len(s)
if L>1:
if L>n:
print("Impossible")
exit()
if sumL+L+(sumL>0) > n:
ans+=1
sumL=L
else:
sumL=sumL+L+(sumL>0)
print(ans)
| python | code_algorithm | [
{
"input": "25\nHello. I am a little walrus.\n",
"output": "2\n"
},
{
"input": "2\nHow are you?\n",
"output": "Impossible\n"
},
{
"input": "19\nHello! Do you like fish? Why?\n",
"output": "3\n"
},
{
"input": "16\nAbacaba. Abacaba. abacaba. abacab.\n",
"output": "3\n"
},... | code_contests | python | 0.2 | c6541c6ae2c5c185c0fee06049200c15 |
A famous Berland's painter Kalevitch likes to shock the public. One of his last obsessions is chess. For more than a thousand years people have been playing this old game on uninteresting, monotonous boards. Kalevitch decided to put an end to this tradition and to introduce a new attitude to chessboards.
As before, th... | e=0
s1=''
s2=''
s3=''
s4=''
s5=''
s6=''
s7=''
s8=''
for i in range(8):
s=str(input())
if(s==('B'*8)):
e=e+1
s1=s1+s[0]
s2=s2+s[1]
s3=s3+s[2]
s4=s4+s[3]
s5=s5+s[4]
s6=s6+s[5]
s7=s7+s[6]
s8=s8+s[7]
if(s1==('B'*8)):
e=e+1
if(s2==('B'*8)):
e=e+1
if(s3==('B'*8)):
e... | python | code_algorithm | [
{
"input": "WWWWWWWW\nBBBBBBBB\nWWWWWWWW\nWWWWWWWW\nWWWWWWWW\nWWWWWWWW\nWWWWWWWW\nWWWWWWWW\n",
"output": "1\n"
},
{
"input": "WWWBWWBW\nBBBBBBBB\nWWWBWWBW\nWWWBWWBW\nWWWBWWBW\nWWWBWWBW\nWWWBWWBW\nWWWBWWBW\n",
"output": "3\n"
},
{
"input": "BBBBBBBB\nWBWWBBBW\nBBBBBBBB\nWBWWBBBW\nWBWWBBBW... | code_contests | python | 0 | 4cd09749c6be2327e58ad9060ecca9da |
Recently Luba bought a monitor. Monitor is a rectangular matrix of size n × m. But then she started to notice that some pixels cease to work properly. Luba thinks that the monitor will become broken the first moment when it contains a square k × k consisting entirely of broken pixels. She knows that q pixels are alread... | from sys import stdout
from sys import stdin
def get():
return stdin.readline().strip()
def getf():
return [int(i) for i in get().split()]
def put(a, end = "\n"):
stdout.write(str(a) + end)
def putf(a, sep = " ", end = "\n"):
stdout.write(sep.join([str(i) for i in a]) + end)
def check(lmt, qr, k, n, m,... | python | code_algorithm | [
{
"input": "3 3 2 5\n1 2 2\n2 2 1\n2 3 5\n3 2 10\n2 1 100\n",
"output": "-1\n"
},
{
"input": "2 3 2 5\n2 1 8\n2 2 8\n1 2 1\n1 3 4\n2 3 2\n",
"output": "8\n"
},
{
"input": "500 500 1 0\n",
"output": "-1\n"
},
{
"input": "29 50 5 29\n21 42 1565821\n21 43 53275635\n21 44 2717830... | code_contests | python | 0.8 | 78a6ab9b99751cfff106cd12c9579b53 |
Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.
Now he tries to solve a quest. The task is to come to a settlement named Ove... | n, m = map(int, input().split())
c = [0] + list(map(int, input().split()))
parent = [i for i in range(n + 1)]
def find(i):
while i != parent[i]:
parent[i] = parent[parent[i]]
i = parent[i]
return i
for i in range(m):
a, b = map(lambda x: find(int(x)), input().split())
if c[a] < c[b]... | python | code_algorithm | [
{
"input": "10 5\n1 6 2 7 3 8 4 9 5 10\n1 2\n3 4\n5 6\n7 8\n9 10\n",
"output": "15\n"
},
{
"input": "5 2\n2 5 3 4 8\n1 4\n4 5\n",
"output": "10\n"
},
{
"input": "10 0\n1 2 3 4 5 6 7 8 9 10\n",
"output": "55\n"
},
{
"input": "2 0\n1000000000 1000000000\n",
"output": "20000... | code_contests | python | 1 | 79b1a748f8a17d2b9d85a83c043eacb1 |
Victor tries to write his own text editor, with word correction included. However, the rules of word correction are really strange.
Victor thinks that if a word contains two consecutive vowels, then it's kinda weird and it needs to be replaced. So the word corrector works in such a way: as long as there are two consec... | n = int(input())
s = input()
s2 = s[0]
for i in range(1,n):
if(ord(s[i]) == ord("a") or ord(s[i]) == ord("e") or ord(s[i]) == ord("i") or ord(s[i]) == ord("o") or ord(s[i]) == ord("u") or ord(s[i]) == ord("y")) and (ord(s[i - 1]) == ord("a") or ord(s[i - 1]) == ord("e") or ord(s[i - 1]) == ord("i") or ord(s[i - 1])... | python | code_algorithm | [
{
"input": "4\nword\n",
"output": "word\n"
},
{
"input": "5\naaeaa\n",
"output": "a\n"
},
{
"input": "5\nweird\n",
"output": "werd\n"
},
{
"input": "4\naepo\n",
"output": "apo\n"
},
{
"input": "6\naaaaaa\n",
"output": "a\n"
},
{
"input": "2\nou\n",
... | code_contests | python | 0.7 | 26741b27e3fdd962a45da9fabe201b3e |
You are given two integers a and b. Moreover, you are given a sequence s_0, s_1, ..., s_{n}. All values in s are integers 1 or -1. It's known that sequence is k-periodic and k divides n+1. In other words, for each k ≤ i ≤ n it's satisfied that s_{i} = s_{i - k}.
Find out the non-negative remainder of division of ∑ _{i... | MOD = 1000000009
def xpow(desk,step):
ret=1
desk=desk%MOD
while step:
if(step%2):
ret=(ret*desk)%MOD
step=step//2
desk = (desk * desk) % MOD
return ret
if __name__ == '__main__':
n,a,b,k=map(int,input().split())
s=input()
base=0
for i in range(0,k):
... | python | code_algorithm | [
{
"input": "4 1 5 1\n-\n",
"output": "999999228\n"
},
{
"input": "2 2 3 3\n+-+\n",
"output": "7\n"
},
{
"input": "234179195 430477711 115381398 12\n++++-+-+-+++\n",
"output": "549793323\n"
},
{
"input": "262060935 184120408 148332034 148\n+--+-------+-+-+--++-+++--++-+-++++++... | code_contests | python | 0 | 4a6f8d991463d78fb9c723210a10b32c |
Little C loves number «3» very much. He loves all things about it.
Now he is playing a game on a chessboard of size n × m. The cell in the x-th row and in the y-th column is called (x,y). Initially, The chessboard is empty. Each time, he places two chessmen on two different empty cells, the Manhattan distance between ... | n, m = map(int, input().split())
if n > m:
n, m = m, n
if n == 1:
ans = (m//6)*6+2*max(m%6-3, 0)
print(ans)
elif n == 2:
if m == 2:
print(0)
elif m == 3:
print(4)
elif m == 7:
print(12)
else:
print(n*m)
else:
ans = ((n*m)//2)*2
print(ans)
| python | code_algorithm | [
{
"input": "3 3\n",
"output": "8\n"
},
{
"input": "2 2\n",
"output": "0\n"
},
{
"input": "11111111 77777777\n",
"output": "864197513580246\n"
},
{
"input": "3 7\n",
"output": "20\n"
},
{
"input": "4 3\n",
"output": "12\n"
},
{
"input": "1 4\n",
"ou... | code_contests | python | 0 | 09aca64126e3a53a42653ff1dd2864c0 |
A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly n minutes. After a round ends, the next round starts immediately. This is repeated over and over again.
Each round has the same scenario. It is described by a sequence of n numbers: d_1, d_2, ..., d_n (-10^6 ≤ d_i ≤ 10^6). Th... | INF = 10 ** 13
h, n = map(int, input().split())
a = list(map(int, input().split()))
b = a[:]
for i in range(1, n):
b[i] += b[i - 1]
low = min(b)
tot = b[-1]
if h + low <= 0:
lo = 0
else:
lo, hi = 0, INF
while lo < hi:
mid = (lo + hi) // 2
cur = h + mid * tot + low
if cur <= 0:
... | python | code_algorithm | [
{
"input": "1000 6\n-100 -200 -300 125 77 -4\n",
"output": "9\n"
},
{
"input": "10 4\n-3 -6 5 4\n",
"output": "-1\n"
},
{
"input": "1000000000000 5\n-1 0 0 0 0\n",
"output": "4999999999996\n"
},
{
"input": "1 1\n-1000000\n",
"output": "1\n"
},
{
"input": "1000 13\... | code_contests | python | 0 | b129bf730e7dc401793335a7b5dac00b |
Logical quantifiers are very useful tools for expressing claims about a set. For this problem, let's focus on the set of real numbers specifically. The set of real numbers includes zero and negatives. There are two kinds of quantifiers: universal (∀) and existential (∃). You can read more about them here.
The universa... | import sys
input = sys.stdin.readline
############ ---- Input Functions ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input().strip()
return(list(s[:len(s)]))
def invr():
return(map(int,input().split()))
def from_file(f):
... | python | code_algorithm | [
{
"input": "3 2\n1 3\n2 3\n",
"output": "2\nAAE\n"
},
{
"input": "4 3\n1 2\n2 3\n3 1\n",
"output": "-1\n"
},
{
"input": "2 1\n1 2\n",
"output": "1\nAE\n"
},
{
"input": "51 50\n4 34\n50 28\n46 41\n37 38\n29 9\n4 29\n38 42\n16 3\n34 21\n27 39\n34 29\n22 50\n14 47\n23 35\n11 4\n... | code_contests | python | 0 | 873d81113fb180d1c84d4626dd5e2c97 |
Polycarp wrote on the board a string s containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input.
After that, he erased some letters from the string s, and he rewrote the remaining letters in any order. As a result, he got some new string t. You have to find it with some ad... | t=int(input())
for i in range(t):
s=input()
b=[]
d=dict()
for j in s:
if j in d.keys():
d[j]+=1
else:
b.append(j)
d[j]=1
b.sort(reverse=True)
n=int(input())
a=list(map(int,input().split()))
c=["a"]*(n)
m=1
f=dict()
p=0
w... | python | code_algorithm | [
{
"input": "4\nabac\n3\n2 1 0\nabc\n1\n0\nabba\n3\n1 0 1\necoosdcefr\n10\n38 13 24 14 11 5 3 24 17 0\n",
"output": "aac\nc\naba\ncodeforces\n"
},
{
"input": "2\naccccccc\n1\n0\naaaaac\n3\n0 0 0\n",
"output": "c\naaa\n"
},
{
"input": "1\naba\n2\n0 0\n",
"output": "aa\n"
},
{
"... | code_contests | python | 0 | 169e897deb18514cdbf66c5e735d40b5 |
Recently, Valery have come across an entirely new programming language. Most of all the language attracted him with template functions and procedures. Let us remind you that templates are tools of a language, designed to encode generic algorithms, without reference to some parameters (e.g., data types, buffer sizes, de... | def main():
from collections import defaultdict
import sys
strings = sys.stdin.read().split('\n')
strings.reverse()
f = defaultdict(lambda: defaultdict(int))
n = int(strings.pop())
for i in range(n):
s = strings.pop().replace('(', ' ').replace(')', ' ').replace(',', ' ... | python | code_algorithm | [
{
"input": "6\nvoid f(string,double,int)\nvoid f(int)\n void f ( T )\nvoid procedure(int,double)\nvoid f (T, double,int) \nvoid f(string, T,T)\n4\n int a\n int x\nstring t\ndouble val \n5\nf(t, a, a)\nf(t,val,a)\nf(val,a, val)\n solve300(val, val)\nf (x)\n",
"output": "1\n3\n0\n0\n2\n"
},
{
... | code_contests | python | 0 | 77a32b4a1ad63e44322fae056b298d6d |
Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Berland decided to keep up with new trends. Formerly all n cities of Berland were connected by n two-way roads in the ring, i. e. each city was connected directly to exactly two ot... | n = int(input())
g=[]
d=[]
cost=[]
for i in range(n+1):
g.append([])
d.append([])
for j in range(n+1):
g[i].append(0)
d[i].append(0)
x,y=[],[]
for i in range(n):
a,b,c = tuple([int(x) for x in input().split()])
g[a][b]=c
g[b][a]=c
d[a][b]=c
d[b][a]=-c
ans = 0
tot=0
stack=[1]
visited=[1]
while stack!=[]:... | python | code_algorithm | [
{
"input": "3\n1 3 1\n1 2 1\n3 2 1\n",
"output": "1\n"
},
{
"input": "3\n1 3 1\n1 2 5\n3 2 1\n",
"output": "2\n"
},
{
"input": "6\n1 5 4\n5 3 8\n2 4 15\n1 6 16\n2 3 23\n4 6 42\n",
"output": "39\n"
},
{
"input": "4\n1 2 9\n2 3 8\n3 4 7\n4 1 5\n",
"output": "0\n"
},
{
... | code_contests | python | 0 | 7304ba8c80f728057c25c0f47c557fec |
A tree is a graph with n vertices and exactly n - 1 edges; this graph should meet the following condition: there exists exactly one shortest (by number of edges) path between any pair of its vertices.
A subtree of a tree T is a tree with both vertices and edges as subsets of vertices and edges of T.
You're given a tr... | n = int(input())
r = [[] for i in range(n + 1)]
r[1] = [0]
for i in range(n - 1):
a, b = map(int, input().split())
r[a].append(b)
r[b].append(a)
t = list(map(int, input().split()))
u, v = [0] * (n + 1), [0] * (n + 1)
for i, j in enumerate(t, 1):
if j < 0: u[i] = - j
else: v[i] = j
# print(u,v)
t, p ... | python | code_algorithm | [
{
"input": "3\n1 2\n1 3\n1 -1 1\n",
"output": "3\n"
},
{
"input": "5\n2 3\n4 5\n2 5\n1 3\n0 2 1 4 3\n",
"output": "8\n"
},
{
"input": "12\n1 6\n10 1\n4 1\n7 1\n1 2\n5 1\n1 8\n1 11\n3 1\n12 1\n9 1\n580660007 861441526 -264928594 488291045 253254575 -974301934 709266786 926718320 87511873 ... | code_contests | python | 0 | b507a402d0f43bd4f8733e3edd32affe |
In this problem at each moment you have a set of intervals. You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d. Also there is a path from interval I1 from our set to interval I2 from our set if there is a sequence of successive moves starting from I1 so ... | def main():
intervals = []
visited = []
for _ in range(int(input())):
read_input(intervals, visited)
def read_input(intervals: list, visited: list) -> None:
command, x, y = [int(x) for x in input().split(" ")]
if (command == 1):
intervals.append((x, y))
visited.append(Fals... | python | code_algorithm | [
{
"input": "5\n1 1 5\n1 5 11\n2 1 2\n1 2 9\n2 1 2\n",
"output": "NO\nYES\n"
},
{
"input": "9\n1 1 4\n1 5 20\n1 11 30\n1 29 60\n1 59 100\n1 100 200\n2 1 5\n2 1 6\n2 2 5\n",
"output": "NO\nNO\nYES\n"
},
{
"input": "10\n1 -1365 -865\n1 1244 1834\n2 1 2\n1 -1508 -752\n2 3 2\n2 2 1\n1 -779 59... | code_contests | python | 0 | f8beed0cc9a36f22fc47175f29d86790 |
One day Vasya decided to have a look at the results of Berland 1910 Football Championship’s finals. Unfortunately he didn't find the overall score of the match; however, he got hold of a profound description of the match's process. On the whole there are n lines in that description each of which described one goal. Eve... | n=int(input())
a,b=input(),""
x,y=1,0
for i in range(n-1):
s=input()
if a==s:
x+=1
else:
b=s
y+=1
print([b,a][x>y]) | python | code_algorithm | [
{
"input": "1\nABC\n",
"output": "ABC\n"
},
{
"input": "5\nA\nABA\nABA\nA\nA\n",
"output": "A\n"
},
{
"input": "100\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\nHA\... | code_contests | python | 0.9 | b6733826d1a586e580fa8542215c86de |
Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have one of the following types:
1. Fold the sheet of paper at position pi. After this query the leftmost... | from itertools import starmap
def main():
n, q = map(int, input().split())
a = list(range(n + 1))
flipped = False
start = 0
end = n
for _ in range(q):
cmd, *args = map(int, input().split())
if cmd == 1:
p = args[0]
if p > end-start-p:
... | python | code_algorithm | [
{
"input": "10 9\n2 2 9\n1 1\n2 0 1\n1 8\n2 0 8\n1 2\n2 1 3\n1 4\n2 2 4\n",
"output": "7\n2\n10\n4\n5\n"
},
{
"input": "7 4\n1 3\n1 2\n2 0 1\n2 1 2\n",
"output": "4\n3\n"
},
{
"input": "10 5\n2 1 9\n2 4 10\n1 1\n2 0 1\n2 0 1\n",
"output": "8\n6\n2\n2\n"
},
{
"input": "100000 ... | code_contests | python | 0 | c8f1bc189f093382de2f5a8a61bdb313 |
Many computer strategy games require building cities, recruiting army, conquering tribes, collecting resources. Sometimes it leads to interesting problems.
Let's suppose that your task is to build a square city. The world map uses the Cartesian coordinates. The sides of the city should be parallel to coordinate axes.... | # This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
mx = -10 ** 10
mix = 10 ** 10
my = mx
miy = mix
for _ in range(int(input())):
a, b = map(int, input().split())
mx = ... | python | code_algorithm | [
{
"input": "2\n0 0\n2 2\n",
"output": "4\n"
},
{
"input": "2\n0 0\n0 3\n",
"output": "9\n"
},
{
"input": "2\n-10 0\n-9 0\n",
"output": "1\n"
},
{
"input": "3\n2 2\n1 1\n3 3\n",
"output": "4\n"
},
{
"input": "10\n-200157522 -824574736\n299208799 -287211553\n-160170... | code_contests | python | 0.7 | ac9bc489ac96557b03e51659e66891b0 |
Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment.
Amr has n different types of chemicals. Each chemical i has an initial volume of ai liters. For this experiment, Amr has to mix all the chemicals together, but all the chemicals volumes must be equal first. So his t... | from collections import Counter
def main():
input()
aa = list(map(int, input().split()))
le, l = max(aa).bit_length(), []
for i, a in enumerate(aa):
j = le - a.bit_length()
aa[i] = a << j
l.append(j)
mi, ma = min(aa), max(aa)
a = mask = (1 << le) - 1
if mi == ma:
... | python | code_algorithm | [
{
"input": "3\n4 8 2\n",
"output": "2\n"
},
{
"input": "3\n3 5 6\n",
"output": "5\n"
},
{
"input": "2\n99999 99998\n",
"output": "2\n"
},
{
"input": "7\n7 4096 8192 16384 32768 65536 100000\n",
"output": "51\n"
},
{
"input": "17\n1 2 4 8 16 32 64 128 256 512 1024 ... | code_contests | python | 0 | b0ea3e88412556d86693496f1ea24ac3 |
Olesya loves numbers consisting of n digits, and Rodion only likes numbers that are divisible by t. Find some number that satisfies both of them.
Your task is: given the n and t print an integer strictly larger than zero consisting of n digits that is divisible by t. If such number doesn't exist, print - 1.
Input
T... | n, t = input().split(' ')
n = int(n)
t = int(t)
a = 10 ** (n-1)
b = 10 ** (n)
for i in range(a, b):
if i == b-1:
if i%t ==0:
print(i)
break
else:
print(-1)
break
else:
if i%t == 0:
print(i)
break
i = i + 1
| python | code_algorithm | [
{
"input": "3 2\n",
"output": "100\n"
},
{
"input": "2 8\n",
"output": "16\n"
},
{
"input": "98 4\n",
"output": "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n"
},
{
"input": "6 10\n",
"output": "100000\n"
},
{
... | code_contests | python | 0.6 | 3ceba1dfbc23dd2bf71d079043ac0bec |
Carl is a beginner magician. He has a blue, b violet and c orange magic spheres. In one move he can transform two spheres of the same color into one sphere of any other color. To make a spell that has never been seen before, he needs at least x blue, y violet and z orange spheres. Can he get them (possible, in multiple... | have = list(map(int, input().split()))
goal = list(map(int, input().split()))
deficit = 0
makeable = 0
for i in range(3):
if have[i] < goal[i]:
deficit += goal[i] - have[i]
else:
makeable += (have[i] - goal[i]) // 2
print('Yes' if makeable >= deficit else 'No')
| python | code_algorithm | [
{
"input": "3 3 3\n2 2 2\n",
"output": "Yes\n"
},
{
"input": "5 6 1\n2 7 2\n",
"output": "No\n"
},
{
"input": "4 4 0\n2 1 2\n",
"output": "Yes\n"
},
{
"input": "135522 188282 377041\n245719 212473 108265\n",
"output": "Yes\n"
},
{
"input": "1000000 500000 500000\n... | code_contests | python | 0 | e0a7ba4ea74249df9b1012b1ae0cae9c |
A tennis tournament with n participants is running. The participants are playing by an olympic system, so the winners move on and the losers drop out.
The tournament takes place in the following way (below, m is the number of the participants of the current round):
* let k be the maximal power of the number 2 such ... | n, b, p = map(int, input().split())
ansb = 0
anst = n*p
while n > 1:
x = n//2
y = n % 2
n -= x
ansb += b*x*2 + x
print(ansb, anst)
| python | code_algorithm | [
{
"input": "5 2 3\n",
"output": "20 15\n"
},
{
"input": "8 2 4\n",
"output": "35 32\n"
},
{
"input": "59 1 1\n",
"output": "174 59\n"
},
{
"input": "1 2 133\n",
"output": "0 133\n"
},
{
"input": "1 2 4\n",
"output": "0 4\n"
},
{
"input": "63 1 1\n",
... | code_contests | python | 1 | 013a138d5991139f257afb91421671b6 |
There are several days left before the fiftieth birthday of a famous Berland's writer Berlbury. In this connection the local library decided to make an exposition of the works of this famous science-fiction writer. It was decided as well that it is necessary to include into the exposition only those books that were pub... | from collections import deque
def mini_in_window(A, n, k):
d = deque()
res = []
for i in range(n):
if i >= k and d[0] == i - k:
d.popleft()
while len(d) and A[d[-1]] >= A[i]:
d.pop()
d.append(i)
if i >= k - 1:
res.append(d[0])
return re... | python | code_algorithm | [
{
"input": "3 3\n14 12 10\n",
"output": "2 2\n1 2\n2 3\n"
},
{
"input": "4 5\n8 19 10 13\n",
"output": "2 1\n3 4\n"
},
{
"input": "2 0\n10 10\n",
"output": "2 1\n1 2\n"
},
{
"input": "2 10\n35 45\n",
"output": "2 1\n1 2\n"
},
{
"input": "3 0\n1000000 1000000 10000... | code_contests | python | 0.7 | edc225c4eda1121a37f1909d95441410 |
You are given a text consisting of n lines. Each line contains some space-separated words, consisting of lowercase English letters.
We define a syllable as a string that contains exactly one vowel and any arbitrary number (possibly none) of consonants. In English alphabet following letters are considered to be vowels:... | n = int(input())
p = input().split()
r = 'YES'
for i in range(n):
s = input()
if(r!='NO' and s.count('a')+s.count('e')+s.count('i')+s.count('o')+s.count('u')+s.count('y')!=int(p[i])):
r = 'NO'
print(r)
| python | code_algorithm | [
{
"input": "3\n2 2 3\nintel\ncode\nch allenge\n",
"output": "YES\n"
},
{
"input": "4\n1 2 3 1\na\nbcdefghi\njklmnopqrstu\nvwxyz\n",
"output": "NO\n"
},
{
"input": "4\n13 11 15 15\nto be or not to be that is the question\nwhether tis nobler in the mind to suffer\nthe slings and arrows of ... | code_contests | python | 0.8 | 0d4e7c0c87b525b75be7f6190b421290 |
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow.
He know... | ##n = int(input())
##a = list(map(int, input().split()))
##print(' '.join(map(str, res)))
[ts , tf, t] = list(map(int, input().split()))
n = int(input())
if n == 0:
print(ts)
exit(0)
a = list(map(int, input().split()))
if a[0] > ts:
print(ts)
exit(0)
min_wait = 1e18
tbest = a[0]-1
tnow = ts
twai... | python | code_algorithm | [
{
"input": "10 15 2\n2\n10 13\n",
"output": "12\n"
},
{
"input": "8 17 3\n4\n3 4 5 8\n",
"output": "2\n"
},
{
"input": "30 70 10\n3\n30 32 35\n",
"output": "60\n"
},
{
"input": "100000000000 200000000000 10000000000\n10\n1 1 110000000000 110000000000 110000000000 110000000000... | code_contests | python | 0 | 85f5e504d151bab49a26bff4283fbf29 |
Bear Limak wants to become the largest of bears, or at least to become larger than his brother Bob.
Right now, Limak and Bob weigh a and b respectively. It's guaranteed that Limak's weight is smaller than or equal to his brother's weight.
Limak eats a lot and his weight is tripled after every year, while Bob's weight... | def banyak_tahun(a,b, count):
c = a*3
d = b*2
if c <= d:
banyak_tahun(c,d, count+1)
else:
print(count)
a, b = list(map(int, input().split()))
banyak_tahun(a,b,1) | python | code_algorithm | [
{
"input": "4 9\n",
"output": "3\n"
},
{
"input": "1 1\n",
"output": "1\n"
},
{
"input": "4 7\n",
"output": "2\n"
},
{
"input": "1 9\n",
"output": "6\n"
},
{
"input": "3 3\n",
"output": "1\n"
},
{
"input": "6 7\n",
"output": "1\n"
},
{
"inp... | code_contests | python | 0.3 | ced37d24ebfdb0b099449f0e12ed8fd0 |
Vladik often travels by trains. He remembered some of his trips especially well and I would like to tell you about one of these trips:
Vladik is at initial train station, and now n people (including Vladik) want to get on the train. They are already lined up in some order, and for each of them the city code ai is know... | """
from
http://codeforces.com/contest/811/problem/C
"""
length = int(input())
array = [0] + [int(x) for x in input().split()]
dp = [0]*(length+1)
end = [0]*5001
start = [5001]*5001
vis = [0]*5001
for i in range(1,length+1):
start[array[i]] = min(start[array[i]],i)
end[array[i]] = max(end[array[i]],i)
for i i... | python | code_algorithm | [
{
"input": "9\n5 1 3 1 5 2 4 2 5\n",
"output": "9\n"
},
{
"input": "6\n4 4 2 5 2 3\n",
"output": "14\n"
},
{
"input": "100\n915 7 282 162 24 550 851 240 39 302 538 76 131 150 104 848 507 842 32 453 998 990 1002 225 887 1005 259 199 873 87 258 318 837 511 663 1008 861 516 445 426 335 743 ... | code_contests | python | 0 | 13199e6e056f9835738a69152d10a3e9 |
Vasya is studying number theory. He has denoted a function f(a, b) such that:
* f(a, 0) = 0;
* f(a, b) = 1 + f(a, b - gcd(a, b)), where gcd(a, b) is the greatest common divisor of a and b.
Vasya has two numbers x and y, and he wants to calculate f(x, y). He tried to do it by himself, but found out that calcul... | def bgcd(a,b):
d=0
while a%2==0 and b%2==0:
a=a//2
b=b//2
d+=1
while a!=b:
if a%2==0:
a=a//2
elif b%2==0:
b=b//2
else:
if a>b:
a=(a-b)//2
else:
b=(b-a)//2
g=a
return g*2**d... | python | code_algorithm | [
{
"input": "3 5\n",
"output": "3\n"
},
{
"input": "6 3\n",
"output": "1\n"
},
{
"input": "1 100000000000\n",
"output": "100000000000\n"
},
{
"input": "100000000000 100000000000\n",
"output": "1\n"
},
{
"input": "191480607107 629918602611\n",
"output": "5547678... | code_contests | python | 0 | cf86d1e010e5b933d7757a6a1075f526 |
For a given positive integer n denote its k-rounding as the minimum positive integer x, such that x ends with k or more zeros in base 10 and is divisible by n.
For example, 4-rounding of 375 is 375·80 = 30000. 30000 is the minimum integer such that it ends with 4 or more zeros and is divisible by 375.
Write a program... | def nok(a, b):
c = a*b
while (a!=0) and (b!=0):
if a>b: a%=b;
else: b%=a;
return c//(a+b);
nk = input().split()
n, k = int(nk[0]), int(nk[1])
print (nok(10**k, n)) | python | code_algorithm | [
{
"input": "10000 1\n",
"output": "10000\n"
},
{
"input": "375 4\n",
"output": "30000\n"
},
{
"input": "38101 0\n",
"output": "38101\n"
},
{
"input": "123456789 8\n",
"output": "12345678900000000\n"
},
{
"input": "9 1\n",
"output": "90\n"
},
{
"input":... | code_contests | python | 0 | 4c6e117621cb616d64bd46ff981488d9 |
Polycarp has a strict daily schedule. He has n alarms set for each day, and the i-th alarm rings each day at the same time during exactly one minute.
Determine the longest time segment when Polycarp can sleep, i. e. no alarm rings in that period. It is possible that Polycarp begins to sleep in one day, and wakes up in... | n = int(input())
a = []
for i in range(n):
str = input()
h = int(str[0:2]) * 60
m = int(str[3:5])
a.append(h + m)
a.sort()
mx = 0
for i in range(n - 1):
if mx < ((-a[i] + a[i + 1]) - 1):
mx = ((-a[i] + a[i + 1]) - 1)
if mx < (1440 + a[0] - a[n - 1] - 1):
mx = 1440 + a[0] - a[n - 1] -... | python | code_algorithm | [
{
"input": "4\n22:00\n03:21\n16:03\n09:59\n",
"output": "06:37\n"
},
{
"input": "1\n05:43\n",
"output": "23:59\n"
},
{
"input": "9\n01:38\n15:16\n18:50\n00:45\n17:26\n16:30\n09:10\n00:46\n05:49\n",
"output": "06:05\n"
},
{
"input": "2\n06:25\n22:43\n",
"output": "16:17\n"... | code_contests | python | 0.4 | b0094b99fad48aeed95a04c3973dfc44 |
You're given a row with n chairs. We call a seating of people "maximal" if the two following conditions hold:
1. There are no neighbors adjacent to anyone seated.
2. It's impossible to seat one more person without violating the first rule.
The seating is given as a string consisting of zeros and ones (0 means... | n = int(input())
l = '0' + input() +'0'
o = 0
z = 0
flag = True
for i in l:
if i == "0" and z < 2:
z += 1
o = 0
elif i == "1" and o != 1:
o = 1
z = 0
else:
flag = False
break
if flag:
print("Yes")
else:
print("No")
| python | code_algorithm | [
{
"input": "3\n101\n",
"output": "Yes\n"
},
{
"input": "5\n10001\n",
"output": "No\n"
},
{
"input": "4\n1011\n",
"output": "No\n"
},
{
"input": "459\n000111000101010000100001001010111110011011010001100101111010111011101110111101111101100101100011011001100110001001111001101000... | code_contests | python | 0.8 | dcfb0baebebaefb95d8c2a8c99e0b470 |
Vasya has got an undirected graph consisting of n vertices and m edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges (... | n, m = map(int, input().split())
print(max(n-2*m, 0), end=' ')
for i in range(0, n+1):
if i*(i-1)/2>=m: break
print(n-i) | python | code_algorithm | [
{
"input": "3 1\n",
"output": "1 1\n"
},
{
"input": "4 2\n",
"output": "0 1\n"
},
{
"input": "10 2\n",
"output": "6 7\n"
},
{
"input": "100 0\n",
"output": "100 100\n"
},
{
"input": "1 0\n",
"output": "1 1\n"
},
{
"input": "18889 138011083\n",
"out... | code_contests | python | 0 | 3399dffec1cf396a6fbd16f403494578 |
Lunar New Year is approaching, and Bob received a gift from his friend recently — a recursive sequence! He loves this sequence very much and wants to play with it.
Let f_1, f_2, …, f_i, … be an infinite sequence of positive integers. Bob knows that for i > k, f_i can be obtained by the following recursive equation:
$... | from math import ceil, sqrt
p = 998244353
def bsgs(g, h):
'''
Solve for x in h = g^x mod p given a prime p.
'''
N = ceil(sqrt(p - 1)) # phi(p) is p-1 if p is prime
# Store hashmap of g^{1...m} (mod p). Baby step.
tbl = {pow(g, i, p): i for i in range(N)}
# Precompute via Fermat's Little... | python | code_algorithm | [
{
"input": "1\n2\n88888 66666\n",
"output": "-1\n"
},
{
"input": "10\n283 463 213 777 346 201 463 283 102 999\n2333333 6263423\n",
"output": "382480067\n"
},
{
"input": "8\n2 3 5 6 1 7 9 10\n23333 1\n",
"output": "1\n"
},
{
"input": "3\n998244352 998244352 998244352\n4 2\n",
... | code_contests | python | 0 | 5c7545605485a8d9e0d11088088b2fce |
There is a robot staying at X=0 on the Ox axis. He has to walk to X=n. You are controlling this robot and controlling how he goes. The robot has a battery and an accumulator with a solar panel.
The i-th segment of the path (from X=i-1 to X=i) can be exposed to sunlight or not. The array s denotes which segments are ex... | n,b,a=map(int,input().split())
s=list(map(int,input().split()))
maxa=a
cnt=0
for i in range(n):
if(s[i]==0):
if(a>0):
a-=1
cnt+=1
elif(b>0):
b-=1
cnt+=1
else:
if(a<maxa and b>0):
b-=1
a+=1
cnt+=1
elif(a>0):
a-=1
cnt+=1
if(b==0 and a==0):
break
print(cnt) | python | code_algorithm | [
{
"input": "5 2 1\n0 1 0 1 0\n",
"output": "5\n"
},
{
"input": "6 2 1\n1 0 0 1 0 1\n",
"output": "3\n"
},
{
"input": "1 1 1\n0\n",
"output": "1\n"
},
{
"input": "100 1 1\n0 0 0 1 1 0 1 0 1 0 1 0 0 1 0 0 1 1 0 0 1 0 1 1 1 1 1 1 1 0 1 1 1 0 0 0 0 0 0 1 1 0 0 0 1 0 1 0 0 1 0 1 1... | code_contests | python | 0 | 18b780d1ecddab4674c22db7efaf3aac |
Old timers of Summer Informatics School can remember previous camps in which each student was given a drink of his choice on the vechorka (late-evening meal). Or may be the story was more complicated?
There are n students living in a building, and for each of them the favorite drink a_i is known. So you know n integer... | from math import ceil
n, k = map(int, input().split())
bev = [0] * k
for i in range(n):
x = int(input())
bev[x-1] += 1
bad = 0
good = 0
for i in range(k):
bad += bev[i] & 1
good += bev[i] - (bev[i] & 1)
# print(good, bad)
bad = ceil(bad / 2)
print(good + bad) | python | code_algorithm | [
{
"input": "5 3\n1\n3\n1\n1\n2\n",
"output": "4\n"
},
{
"input": "10 3\n2\n1\n3\n2\n3\n3\n1\n3\n1\n2\n",
"output": "9\n"
},
{
"input": "1 1000\n548\n",
"output": "1\n"
},
{
"input": "16 6\n1\n1\n1\n2\n2\n2\n3\n3\n3\n4\n4\n4\n5\n5\n6\n6\n",
"output": "14\n"
},
{
"i... | code_contests | python | 0.3 | cc14b574e906c304a67cce862c8cf84f |
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along... | n, m = map(int, input().split())
Q = [[]for _ in range(n)]
for _ in range(m):
u, v = map(int, input().split())
u -= 1
v -= 1
Q[v].append(u)
k = int(input())
p = [int(T) - 1 for T in input().split()]
W = [-1] * n
E = [0] * n
q = [(p[-1], 0)]
for u, d in q:
if W[u] < 0:
W[u] = d
d += 1... | python | code_algorithm | [
{
"input": "7 7\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 1\n7\n1 2 3 4 5 6 7\n",
"output": "0 0\n"
},
{
"input": "8 13\n8 7\n8 6\n7 5\n7 4\n6 5\n6 4\n5 3\n5 2\n4 3\n4 2\n3 1\n2 1\n1 8\n5\n8 7 5 2 1\n",
"output": "0 3\n"
},
{
"input": "6 9\n1 5\n5 4\n1 2\n2 3\n3 4\n4 1\n2 6\n6 4\n4 2\n4\n1 2 3 4\... | code_contests | python | 0.1 | 147f3709b6743a9c22a0bb0880883b4c |
In number world, two different numbers are friends if they have a lot in common, but also each one has unique perks.
More precisely, two different numbers a and b are friends if gcd(a,b), (a)/(gcd(a,b)), (b)/(gcd(a,b)) can form sides of a triangle.
Three numbers a, b and c can form sides of a triangle if a + b > c, b... | from math import sqrt
from sys import stdin, stdout
from time import time
start = time()
t = int(stdin.readline().strip())
tests = list(map(int, stdin.readline().split()))
def gen_primes(n):
sieve = [True] * (n//2)
for i in range(3,int(n**0.5)+1,2):
if sieve[i//2]:
sieve[i*i//2::i] = [Fal... | python | code_algorithm | [
{
"input": "3\n1 5 10\n",
"output": "1\n3\n3\n"
},
{
"input": "6\n12 432 21 199 7 1\n",
"output": "4\n76\n7\n41\n4\n1\n"
},
{
"input": "100\n791 303 765 671 210 999 106 489 243 635 807 104 558 628 545 926 35 3 75 196 35 460 523 621 748 45 501 143 240 318 78 908 207 369 436 6 285 200 236 ... | code_contests | python | 0 | 8e75f7761d644474f37da444362ff83e |
Lolek and Bolek are about to travel abroad by plane. The local airport has a special "Choose Your Plane" offer. The offer's conditions are as follows:
* it is up to a passenger to choose a plane to fly on;
* if the chosen plane has x (x > 0) empty seats at the given moment, then the ticket for such a plane costs ... | n,m=list(map(int,input().split()))
L=list(map(int,input().split()))
P=list(map(int,L))
mi=0
ma=0
for i in range(n):
x=max(L)
ma+=x
L[L.index(x)]-=1
#print(P)
for i in range(n):
x=min(P)
if x==0:
P.remove(x)
x=min(P)
mi+=x
P[P.index(x)]-=1
print(ma,mi) | python | code_algorithm | [
{
"input": "4 3\n2 2 2\n",
"output": "7 6\n"
},
{
"input": "4 3\n2 1 1\n",
"output": "5 5\n"
},
{
"input": "10 2\n4 7\n",
"output": "37 37\n"
},
{
"input": "10 1\n19\n",
"output": "145 145\n"
},
{
"input": "40 10\n1 2 3 4 5 6 7 10 10 10\n",
"output": "223 158\... | code_contests | python | 0 | ac8b8200583208469fa9f276a9c52f12 |
Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some pair of settlements and is bidirectional. Moreover, it is possible to r... |
import heapq
def dfs(graph, start):
n = len(graph)
dist = [-0 for i in range(n + 1)]
visited = [False for i in range(n + 1)]
visited[start] = True
stack = []
dist[start] = 0
heapq.heappush(stack, start)
while stack:
u = heapq.heappop(stack)
for v in graph[u]:
... | python | code_algorithm | [
{
"input": "6 2 3\n1 2\n1 5\n2 3\n3 4\n4 5\n5 6\n",
"output": "3\n"
},
{
"input": "10 1 0\n3\n10 1\n9 4\n4 5\n6 4\n2 4\n7 5\n8 3\n5 3\n1 3\n",
"output": "1\n"
},
{
"input": "5 2 1\n1 5\n1 2\n2 3\n3 4\n4 5\n",
"output": "0\n"
},
{
"input": "5 2 0\n1 2\n1 2\n2 3\n3 4\n4 5\n",
... | code_contests | python | 0.6 | 8b43ae6b9071ed290a39a83f4873280c |
Iahub wants to enhance his multitasking abilities. In order to do this, he wants to sort n arrays simultaneously, each array consisting of m integers.
Iahub can choose a pair of distinct indices i and j (1 ≤ i, j ≤ m, i ≠ j). Then in each array the values at positions i and j are swapped only if the value at position ... | n,m,k = map(int,input().split())
for i in range(n):
a = [int(x) for x in input().split()]
d = []
for i in range(1,m):
for j in range(i+1,m+1):
if k == 0:
d.append((i,j))
else:
d.append((j,i))
print(len(d))
for i in d:
print(*i) | python | code_algorithm | [
{
"input": "2 5 0\n1 3 2 5 4\n1 4 3 2 5\n",
"output": "10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n4 5\n"
},
{
"input": "3 2 1\n1 2\n2 3\n3 4\n",
"output": "1\n2 1\n"
},
{
"input": "2 1 0\n1\n2\n",
"output": "0\n"
},
{
"input": "2 5 1\n331081 525217 574775 753333 840639\... | code_contests | python | 0.4 | bfc946fe887febda4d6df74881e060e8 |
Little Chris is bored during his physics lessons (too easy), so he has built a toy box to keep himself occupied. The box is special, since it has the ability to change gravity.
There are n columns of toy cubes in the box arranged in a line. The i-th column contains ai cubes. At first, the gravity in the box is pulling... | cols = int(input(""))
inputs = input("")
inputList = inputs.split(' ')
inputList.sort(key=int)
for i in inputList:
print(i, end=" ")
| python | code_algorithm | [
{
"input": "3\n2 3 8\n",
"output": "2 3 8 "
},
{
"input": "4\n3 2 1 2\n",
"output": "1 2 2 3 "
},
{
"input": "90\n17 75 51 30 100 5 50 95 51 73 66 5 7 76 43 49 23 55 3 24 95 79 10 11 44 93 17 99 53 66 82 66 63 76 19 4 51 71 75 43 27 5 24 19 48 7 91 15 55 21 7 6 27 10 2 91 64 58 18 21 16 ... | code_contests | python | 0 | 36ba0e9a54030a7ab9efb7c36f264030 |
Consider a football tournament where n teams participate. Each team has two football kits: for home games, and for away games. The kit for home games of the i-th team has color xi and the kit for away games of this team has color yi (xi ≠ yi).
In the tournament, each team plays exactly one home game and exactly one aw... | n = int(input())
a = [0]*(10**5 + 1)
c = []
for i in range(n):
x,y = map(int, input().split())
c.append([x,y])
a[x]+=1
ans = n - 1
for i in range(n):
d = ans + a[c[i][1]]
print(d,2*(n - 1) - d)
| python | code_algorithm | [
{
"input": "2\n1 2\n2 1\n",
"output": "2 0\n2 0\n"
},
{
"input": "3\n1 2\n2 1\n1 3\n",
"output": "3 1\n4 0\n2 2\n"
},
{
"input": "3\n1 100000\n1 100000\n100000 2\n",
"output": "3 1\n3 1\n2 2\n"
},
{
"input": "30\n14 1\n12 5\n16 18\n17 9\n17 5\n13 4\n5 17\n10 8\n13 9\n11 9\n11... | code_contests | python | 0.3 | ff35d447cb3a9cea2db8253b168aeac9 |
A new e-mail service "Berlandesk" is going to be opened in Berland in the near future. The site administration wants to launch their project as soon as possible, that's why they ask you to help. You're suggested to implement the prototype of site registration system. The system should work on the following principle.
... | users = {}
for x in range (0, int(input())):
want = input()
if want in users:
print(want + str(users[want]))
users[want] += 1
else:
print("OK")
users[want] = 1 | python | code_algorithm | [
{
"input": "6\nfirst\nfirst\nsecond\nsecond\nthird\nthird\n",
"output": "OK\nfirst1\nOK\nsecond1\nOK\nthird1\n"
},
{
"input": "4\nabacaba\nacaba\nabacaba\nacab\n",
"output": "OK\nOK\nabacaba1\nOK\n"
},
{
"input": "10\nzzzzzzzzzzzzzzzzzzzzzzzzzzz\nzzzzzzzzzzzzzzzzzzzzzzzzzzz\nzzzzzzzzzzzz... | code_contests | python | 1 | 6b9ed1e108ba299306ce0248c69b126f |
Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string.
Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent ... | s = list(input())
m = int(input())
a = [int(x) for x in input().split()]
n = len(s) // 2
sums = [0] * (n+1)
for i in a:
sums[i-1] += 1
for i in range (1, len(sums)):
sums[i] += sums[i-1]
for i in range(n):
if sums[i] % 2 != 0:
s[i], s[len(s) - i - 1] = s[len(s) - i - 1], s[i]
print("".join(s))
| python | code_algorithm | [
{
"input": "abcdef\n3\n1 2 3\n",
"output": "fbdcea\n"
},
{
"input": "abcdef\n1\n2\n",
"output": "aedcbf\n"
},
{
"input": "vwxyz\n2\n2 2\n",
"output": "vwxyz\n"
},
{
"input": "wljqgdlxyc\n13\n3 4 3 3 5 4 4 2 4 4 5 3 3\n",
"output": "wyjldgqxlc\n"
},
{
"input": "xwc... | code_contests | python | 0.7 | af286c70de1e36a0d3776c9a705585d0 |
Marina loves Sasha. But she keeps wondering whether Sasha loves her. Of course, the best way to know it is fortune telling. There are many ways of telling fortune, but Marina has picked the easiest one. She takes in her hand one or several camomiles and tears off the petals one by one. After each petal she pronounces a... | n = int(input())
l = list(map(int, input().split()))
r = 0
par = []
nep = []
for item in l:
if item % 2 == 0:
par.append(item)
else:
nep.append(item)
if len(nep) % 2 == 0 and nep != []:
nep.remove(min(nep))
r += sum(nep)
if r != 0:
r += sum(par)
print(r)
| python | code_algorithm | [
{
"input": "3\n5 6 7\n",
"output": "13\n"
},
{
"input": "1\n2\n",
"output": "0\n"
},
{
"input": "1\n1\n",
"output": "1\n"
},
{
"input": "99\n86 16 38 20 68 60 84 16 28 88 60 48 80 28 4 92 70 60 46 46 20 34 12 100 76 2 40 10 8 86 6 80 50 66 12 34 14 28 26 70 46 64 34 96 10 90 ... | code_contests | python | 0.2 | 98f00bb4fdecec211c71ab447b5c79b1 |
Little Artem has invented a time machine! He could go anywhere in time, but all his thoughts of course are with computer science. He wants to apply this time machine to a well-known data structure: multiset.
Artem wants to create a basic multiset of integers. He wants these structure to support operations of three typ... | # ---------------------------iye ha aam zindegi---------------------------------------------
import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
threading.stack_size(10**8)
mod... | python | code_algorithm | [
{
"input": "3\n1 1 1\n2 2 1\n3 3 1\n",
"output": "0\n"
},
{
"input": "6\n1 1 5\n3 5 5\n1 2 5\n3 6 5\n2 3 5\n3 7 5\n",
"output": "1\n2\n1\n"
},
{
"input": "12\n1 9 1\n1 8 1\n1 7 1\n1 6 1\n1 1 1\n1 2 1\n1 3 1\n1 4 1\n2 5 1\n3 12 1\n3 14 2\n3 15 999999999\n",
"output": "7\n0\n0\n"
},
... | code_contests | python | 0.4 | 842139d7d66d3d425fcf144d8544aa3e |
Mr. Funt now lives in a country with a very specific tax laws. The total income of mr. Funt during this year is equal to n (n ≥ 2) burles and the amount of tax he has to pay is calculated as the maximum divisor of n (not equal to n, of course). For example, if n = 6 then Funt has to pay 3 burles, while for n = 25 he ne... | # ========= /\ /| |====/|
# | / \ | | / |
# | /____\ | | / |
# | / \ | | / |
# ========= / \ ===== |/====|
# code
if __name__ == "__main__":
def prime(n):
if n == 1:
return False
i = 2
... | python | code_algorithm | [
{
"input": "27\n",
"output": "3\n"
},
{
"input": "4\n",
"output": "2\n"
},
{
"input": "13\n",
"output": "1\n"
},
{
"input": "200000015\n",
"output": "3\n"
},
{
"input": "719241201\n",
"output": "3\n"
},
{
"input": "3\n",
"output": "1\n"
},
{
... | code_contests | python | 0 | ec2c495bdaaaaad31e421e9d66b99fc1 |
n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if h... | n, m, k = map(int, input().split())
m -= n
d = 0
k -= 1
out = 1
while m > 1 and d != max(k, n - k - 1):
out += 1
m -= 1
left = min(d, k)
right = min(d, n - k - 1)
d += 1
m -= left
m -= right
out += m // n
print(out)
| python | code_algorithm | [
{
"input": "3 10 3\n",
"output": "4\n"
},
{
"input": "3 6 1\n",
"output": "3\n"
},
{
"input": "4 6 2\n",
"output": "2\n"
},
{
"input": "100 1000000000 20\n",
"output": "10000034\n"
},
{
"input": "100 999981057 92\n",
"output": "9999852\n"
},
{
"input":... | code_contests | python | 0 | b20fd0f8c5d9ec2e678798d1dcd894e4 |
It is winter now, and Max decided it's about time he watered the garden.
The garden can be represented as n consecutive garden beds, numbered from 1 to n. k beds contain water taps (i-th tap is located in the bed xi), which, if turned on, start delivering water to neighbouring beds. If the tap on the bed xi is turned ... | t=int(input())
for _ in range(t):
n,k=list(map(int,input().split()))
x=list(map(int,input().split()))
b=[]
for i in range(1,n+1):
a=[]
for j in range(k):
a.append(abs(x[j]-i))
b.append(min(a))
print(max(b)+1) | python | code_algorithm | [
{
"input": "3\n5 1\n3\n3 3\n1 2 3\n4 1\n1\n",
"output": "3\n1\n4\n"
},
{
"input": "1\n8 4\n1 2 3 7\n",
"output": "3\n"
},
{
"input": "1\n13 2\n8 13\n",
"output": "8\n"
},
{
"input": "1\n200 2\n50 150\n",
"output": "51\n"
},
{
"input": "31\n5 1\n5\n5 1\n4\n5 2\n4 5... | code_contests | python | 0.2 | 47f787729c2cf37cb086518d4e327886 |
You are given a sequence of n positive integers d1, d2, ..., dn (d1 < d2 < ... < dn). Your task is to construct an undirected graph such that:
* there are exactly dn + 1 vertices;
* there are no self-loops;
* there are no multiple edges;
* there are no more than 106 edges;
* its degree set is equal to d.... | from sys import stdin
from sys import stdout
n = int(stdin.readline())
d = [0] + list(map(int, stdin.readline().split()))
e = []
for i in range(1, n+1):
for u in range(d[i-1]+1, d[i]+1):
for v in range(u+1, d[n-i+1]+2):
e.append([u,v])
stdout.write("{}\n".format(len(e)))
for ei in e: stdout.write("{} {}\n".forma... | python | code_algorithm | [
{
"input": "3\n1 2 3\n",
"output": "4\n1 2\n1 3\n1 4\n2 3\n"
},
{
"input": "3\n2 3 4\n",
"output": "8\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n"
},
{
"input": "2\n2 3\n",
"output": "5\n1 2\n1 3\n1 4\n2 3\n2 4\n"
},
{
"input": "2\n1 2\n",
"output": "2\n1 2\n1 3\n"
},
{... | code_contests | python | 0 | 40951bbd277304a0848fb31d49906142 |
Allen wants to enter a fan zone that occupies a round square and has n entrances.
There already is a queue of a_i people in front of the i-th entrance. Each entrance allows one person from its queue to enter the fan zone in one minute.
Allen uses the following strategy to enter the fan zone:
* Initially he stands... | _ = input()
arr = list(map(int, input().split()))
best = None
for i in range(len(arr)):
pos_at_zero = arr[i] % len(arr)
if pos_at_zero > i:
extra = len(arr) - pos_at_zero + i
else:
extra = i - pos_at_zero
time_needed = arr[i] + extra
#print(i, time_needed, extra, arr[i])
if best ... | python | code_algorithm | [
{
"input": "4\n2 3 2 0\n",
"output": "3\n"
},
{
"input": "6\n5 2 6 5 7 4\n",
"output": "6\n"
},
{
"input": "2\n10 10\n",
"output": "1\n"
},
{
"input": "2\n1 1\n",
"output": "2\n"
},
{
"input": "2\n483544186 940350702\n",
"output": "1\n"
},
{
"input": "... | code_contests | python | 0 | a92213cd79b6e8b2e955ea36e1944164 |
One of Arkady's friends works at a huge radio telescope. A few decades ago the telescope has sent a signal s towards a faraway galaxy. Recently they've received a response t which they believe to be a response from aliens! The scientists now want to check if the signal t is similar to s.
The original signal s was a se... | import sys
from math import *
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int, minp().split())
def add(a,b):
return (a+b)%1000000007
def sub(a,b):
return (a+1000000007-b)%1000000007
def mul(a,b):
return (a*b)%1000000007
p = 102367
s = list(map(int,... | python | code_algorithm | [
{
"input": "001\nkokokokotlin\n",
"output": "2\n"
},
{
"input": "01\naaaaaa\n",
"output": "4\n"
},
{
"input": "010\nugkircaaaaaaaaaab\n",
"output": "0\n"
},
{
"input": "010\ngvmorcaaaaaaaaaab\n",
"output": "0\n"
},
{
"input": "01\nzbrronwaofovklkopelo\n",
"out... | code_contests | python | 0.1 | d7cb18a4520e538f7c45e24873457c1a |
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).
For each house, the... | import sys
input = sys.stdin.readline
for _ in range(1):
n,m=[int(x) for x in input().split()]
g,v=[[] for i in range(n)],[False for i in range(n)]
s=[False for i in range(n)]
for i in range(m):
a,b,c=[int(x) for x in input().split()]
s[b-1]=True
g[a-1].append([b-1,c])
ans=[]... | python | code_algorithm | [
{
"input": "4 2\n1 2 60\n3 4 50\n",
"output": "2\n1 2 60\n3 4 50\n"
},
{
"input": "3 3\n1 2 20\n2 3 10\n3 1 5\n",
"output": "0\n"
},
{
"input": "3 2\n1 2 10\n2 3 20\n",
"output": "1\n1 3 10\n"
},
{
"input": "3 1\n1 2 10\n",
"output": "1\n1 2 10\n"
},
{
"input": "4... | code_contests | python | 0.6 | d5a34cb95285445b30c448d2df070ea4 |
Inaka has a disc, the circumference of which is n units. The circumference is equally divided by n points numbered clockwise from 1 to n, such that points i and i + 1 (1 ≤ i < n) are adjacent, and so are points n and 1.
There are m straight segments on the disc, the endpoints of which are all among the aforementioned ... | from math import gcd
def primes():
yield 2; yield 3; yield 5; yield 7;
bps = (p for p in primes()) # separate supply of "base" primes (b.p.)
p = next(bps) and next(bps) # discard 2, then get 3
q = p * p # 9 - square of next base prime to keep track o... | python | code_algorithm | [
{
"input": "10 2\n1 6\n2 7\n",
"output": "Yes\n"
},
{
"input": "10 3\n1 2\n3 2\n7 2\n",
"output": "No\n"
},
{
"input": "9 6\n4 5\n5 6\n7 8\n8 9\n1 2\n2 3\n",
"output": "Yes\n"
},
{
"input": "12 6\n1 3\n3 7\n5 7\n7 11\n9 11\n11 3\n",
"output": "Yes\n"
},
{
"input":... | code_contests | python | 0.5 | a33f723d441215b0914c649c07c531fe |
Polycarp is a frequent user of the very popular messenger. He's chatting with his friends all the time. He has n friends, numbered from 1 to n.
Recall that a permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once in this array.
So his recent chat list can be represented wit... | n,m=map(int,input().split())
a=[int(i)-1 for i in input().split()]
ans1=[i+1 for i in range(n)]
ans2=[-1 for i in range(n)]
for i in set(a):
ans1[i]=1
N=1
while N<n+m:N<<=1
st=[0 for i in range(N<<1)]
pos=[i+m for i in range(n)]
for i in range(n):
st[i+N+m]=1
for i in range(N-1,0,-1):
st[i]=st[i<<1]+st[i<<1|1... | python | code_algorithm | [
{
"input": "5 4\n3 5 1 4\n",
"output": "1 3\n2 5\n1 4\n1 5\n1 5\n"
},
{
"input": "4 3\n1 2 4\n",
"output": "1 3\n1 2\n3 4\n1 4\n"
},
{
"input": "2 1\n2\n",
"output": "1 2\n1 2\n"
},
{
"input": "5 5\n1 1 4 2 2\n",
"output": "1 3\n1 3\n3 4\n1 4\n5 5\n"
},
{
"input":... | code_contests | python | 0.2 | 4eebcc02a52d6f4f0cfed5fa92572abd |
You have been blessed as a child of Omkar. To express your gratitude, please solve this problem for Omkar!
An array a of length n is called complete if all elements are positive and don't exceed 1000, and for all indices x,y,z (1 ≤ x,y,z ≤ n), a_{x}+a_{y} ≠ a_{z} (not necessarily distinct).
You are given one integer ... | t=int(input())
for i in range(0,t):
n=int(input())
L=[1]*n
print(*L) | python | code_algorithm | [
{
"input": "2\n5\n4\n",
"output": "1 1 1 1 1\n1 1 1 1\n"
},
{
"input": "21\n20\n31\n23\n1\n2\n3\n6\n7\n8\n12\n14\n15\n26\n25\n37\n60\n81\n99\n101\n173\n256\n",
"output": "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1 1... | code_contests | python | 0 | 2c3d1e09fbd544f0115e484b490cb568 |
A new agent called Killjoy invented a virus COVID-2069 that infects accounts on Codeforces. Each account has a rating, described by an integer (it can possibly be negative or very large).
Killjoy's account is already infected and has a rating equal to x. Its rating is constant. There are n accounts except hers, number... | for _ in range(int(input())):
n,x=map(int,input().split())
a=list(map(int,input().split()))
if len(set(a))==1 and list(set(a))[0]==x:
print(0)
elif sum(a)/n==x:
print(1)
elif x in a:
print(1)
else:
print(2) | python | code_algorithm | [
{
"input": "3\n2 69\n68 70\n6 4\n4 4 4 4 4 4\n9 38\n-21 83 50 -59 -77 15 -71 -78 20\n",
"output": "1\n0\n2\n"
},
{
"input": "1\n2 1\n-10 13\n",
"output": "2\n"
},
{
"input": "1\n3 4\n5 5 5\n",
"output": "2\n"
},
{
"input": "1\n2 2\n0 5\n",
"output": "2\n"
},
{
"in... | code_contests | python | 0 | f585ae56d7dc4302d489dff44eabdc0f |
There is a game called "I Wanna Be the Guy", consisting of n levels. Little X and his friend Little Y are addicted to the game. Each of them wants to pass the whole game.
Little X can pass only p levels of the game. And Little Y can pass only q levels of the game. You are given the indices of levels Little X can pass ... | n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
d=[]
for i in range(1,p[0]+1):
d+=[p[i]]
for i in range(1,q[0]+1):
if(q[i] not in d):
d+=[q[i]]
if(len(d)==n):
print("I become the guy.")
else:
print("Oh, my keyboard!")
| python | code_algorithm | [
{
"input": "4\n3 1 2 3\n2 2 3\n",
"output": "Oh, my keyboard!\n"
},
{
"input": "4\n3 1 2 3\n2 2 4\n",
"output": "I become the guy.\n"
},
{
"input": "100\n78 63 59 39 11 58 4 2 80 69 22 95 90 26 65 16 30 100 66 99 67 79 54 12 23 28 45 56 70 74 60 82 73 91 68 43 92 75 51 21 17 97 86 44 62 ... | code_contests | python | 0.1 | e1780ec88048f5c7a755cefd7ce58e1c |
Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and unpredictable. A palindromic number is another matter. It is aesthetically pleasing, and it has a number of remarkable properties. Help Rikhail to convince the scientific community in this!
Let us remind you... | import sys
p,q = map(int,sys.stdin.readline().split())
def prime(n):
#print(int(n**0.5))
for div in range(2,int(n**0.5)+1):
if n%div==0:
return False
return True
def palindrom(n):
n = str(n)
for pos in range((len(n)+1)//2):
if n[pos]!=n[-1-pos]:
return ... | python | code_algorithm | [
{
"input": "6 4\n",
"output": "172\n"
},
{
"input": "1 1\n",
"output": "40\n"
},
{
"input": "1 42\n",
"output": "1\n"
},
{
"input": "238 9996\n",
"output": "1\n"
},
{
"input": "9999 9999\n",
"output": "40\n"
},
{
"input": "620 35\n",
"output": "251... | code_contests | python | 0 | c478dcbee35ee1e92e178ac36d6373a2 |
A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filter) and its many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practi... | import sys
lines = sys.stdin.readlines()
n = int(lines[0].strip())
arr = list(map(int, lines[1].strip().split(" ")))
arr.append(arr[-1])
res = []
maxChange = 0
left = 0
for i in range(1, n+1):
if arr[i] == arr[i-1]:
L = i - left
res += [arr[left]]*(L//2) + [arr[i-1]]*(L-L//2)
maxChange = ma... | python | code_algorithm | [
{
"input": "5\n0 1 0 1 0\n",
"output": "2\n0 0 0 0 0\n"
},
{
"input": "4\n0 0 1 1\n",
"output": "0\n0 0 1 1\n"
},
{
"input": "3\n0 1 0\n",
"output": "1\n0 0 0\n"
},
{
"input": "168\n0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 ... | code_contests | python | 1 | c650c99302b1b1eae84f23dce5bb3e33 |
Small, but very brave, mouse Brain was not accepted to summer school of young villains. He was upset and decided to postpone his plans of taking over the world, but to become a photographer instead.
As you may know, the coolest photos are on the film (because you can specify the hashtag #film for such).
Brain took a ... | n, m = [int(s) for s in input().split()]
for i in range(n):
a = [str(s) for s in input().split()]
if 'C' in a or 'Y' in a or 'M' in a:
m = -1
break
else:
pass
print('#Color') if m == -1 else print('#Black&White')
| python | code_algorithm | [
{
"input": "2 2\nC M\nY Y\n",
"output": "#Color\n"
},
{
"input": "1 1\nW\n",
"output": "#Black&White\n"
},
{
"input": "3 2\nW W\nW W\nB B\n",
"output": "#Black&White\n"
},
{
"input": "1 3\nW G B\n",
"output": "#Black&White\n"
},
{
"input": "1 1\nB\n",
"output"... | code_contests | python | 1 | c357ad31035eb2fbd9ee7a3ae6247751 |
Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a string si, erase the first character and append it to the end of the string. For example, if he has the string "coolmike", in one move he can transform it into the string "oolmikec".
Now Mike asks himself: what... | def fn(s1,s2):
l1=list(s1)
l2=list(s2)
ans=0
for i in range(len(l1)):
if l1==l2:
break
l1=l1[1:]+[l1[0]]
ans+=1
if l1==l2:
return ans
else:
print(-1)
exit()
n1=int(input())
l=[]
if n1==1:
print(0)
exit()
for i in range(n1):
... | python | code_algorithm | [
{
"input": "4\nxzzwo\nzwoxz\nzzwox\nxzzwo\n",
"output": "5\n"
},
{
"input": "3\nkc\nkc\nkc\n",
"output": "0\n"
},
{
"input": "3\naa\naa\nab\n",
"output": "-1\n"
},
{
"input": "2\nmolzv\nlzvmo\n",
"output": "2\n"
},
{
"input": "2\na\nb\n",
"output": "-1\n"
},... | code_contests | python | 0.4 | a0e7712ce91f2bc0e07ccd8682afd270 |
Firecrackers scare Nian the monster, but they're wayyyyy too noisy! Maybe fireworks make a nice complement.
Little Tommy is watching a firework show. As circular shapes spread across the sky, a splendid view unfolds on the night of Lunar New Year's eve.
A wonder strikes Tommy. How many regions are formed by the circl... | from math import sqrt
class vector:
def __init__(self, _x = 0, _y = 0):
self.x = _x
self.y = _y
def len(self):
return sqrt(self.x ** 2 + self.y ** 2)
def len_sq(self):
return self.x ** 2 + self.y ** 2
def __mul__(self, other):
if (type(self) == type(other)):
return self.x * other.x + self.y * other.y
... | python | code_algorithm | [
{
"input": "3\n0 0 2\n2 0 2\n1 1 2\n",
"output": "8\n"
},
{
"input": "3\n0 0 1\n2 0 1\n4 0 1\n",
"output": "4\n"
},
{
"input": "3\n0 0 2\n3 0 2\n6 0 2\n",
"output": "6\n"
},
{
"input": "3\n2 5 4\n-6 -6 7\n1 6 6\n",
"output": "4\n"
},
{
"input": "3\n0 0 2\n0 0 4\n3... | code_contests | python | 0 | 9cb60c35181249d79632e06850d8d568 |
The Fair Nut is going to travel to the Tree Country, in which there are n cities. Most of the land of this country is covered by forest. Furthermore, the local road system forms a tree (connected graph without cycles). Nut wants to rent a car in the city u and go by a simple path to city v. He hasn't determined the pat... | from sys import stdin
input=lambda : stdin.readline().strip()
from math import ceil,sqrt,factorial,gcd
from collections import deque
n=int(input())
l=list(map(int,input().split()))
visited=set()
graph={i:set() for i in range(1,n+1)}
d={}
papa=[0 for i in range(n+1)]
level=[[] for i in range(n+1)]
z=[[0] for i in range(... | python | code_algorithm | [
{
"input": "5\n6 3 2 5 0\n1 2 10\n2 3 3\n2 4 1\n1 5 1\n",
"output": "7\n"
},
{
"input": "3\n1 3 3\n1 2 2\n1 3 2\n",
"output": "3\n"
},
{
"input": "10\n28 8 0 1 5 2 9 1 2 81\n10 1 9\n6 5 78\n8 4 38\n3 10 74\n8 6 41\n7 2 21\n9 2 54\n2 6 90\n4 1 30\n",
"output": "100\n"
},
{
"in... | code_contests | python | 0.1 | 55eebd791bcc8b1835293a8a0c6428e4 |
Tokitsukaze and CSL are playing a little game of stones.
In the beginning, there are n piles of stones, the i-th pile of which has a_i stones. The two players take turns making moves. Tokitsukaze moves first. On each turn the player chooses a nonempty pile and removes exactly one stone from the pile. A player loses if... | import sys
n = int(input())
a = sorted([int(i) for i in input().split()])
t = 0
for i in range(1,n):
t += a[i]==a[i-1]
if t >= 2:
print("cslnb")
sys.exit(0)
if t:
for i in range(n):
if a[i]==a[i+1]:
if a[i] and a[i]!=a[i-1]+1:
a[i] -= 1
break
else:
print("cslnb")
sys.exit(0)
print(["cslnb","... | python | code_algorithm | [
{
"input": "3\n2 3 1\n",
"output": "sjfnb\n"
},
{
"input": "1\n0\n",
"output": "cslnb\n"
},
{
"input": "2\n1 0\n",
"output": "cslnb\n"
},
{
"input": "2\n2 2\n",
"output": "sjfnb\n"
},
{
"input": "2\n0 1\n",
"output": "cslnb\n"
},
{
"input": "5\n0 1 3 3... | code_contests | python | 0 | bd2e305200c796023563db71eb04bbea |
Ania has a large integer S. Its decimal representation has length n and doesn't contain any leading zeroes. Ania is allowed to change at most k digits of S. She wants to do it in such a way that S still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
Input
The first... | n, k = [int(x) for x in input().strip().split()]
S = input().strip()
if n == 1 and k == 1:
res = '0'
elif k == 0:
res = S
else:
S = [c for c in S]
if S[0] != '1':
S[0] = '1'
k -= 1
i = 1
while k > 0:
if i >= n:
break
if S[i] == '0':
i +=... | python | code_algorithm | [
{
"input": "1 1\n1\n",
"output": "0\n"
},
{
"input": "3 2\n102\n",
"output": "100\n"
},
{
"input": "5 3\n51528\n",
"output": "10028\n"
},
{
"input": "5 5\n10000\n",
"output": "10000\n"
},
{
"input": "3 3\n888\n",
"output": "100\n"
},
{
"input": "50 30\... | code_contests | python | 0.7 | 6b7ac7450bf3c060a459a9e1d9d4d369 |
You are given a rectangular matrix of size n × m consisting of integers from 1 to 2 ⋅ 10^5.
In one move, you can:
* choose any element of the matrix and change its value to any integer between 1 and n ⋅ m, inclusive;
* take any column and shift it one cell up cyclically (see the example of such cyclic shift belo... | from collections import Counter
from sys import stdin
def input():
return(next(stdin))
def main():
n, m = map(int, input().split())
aaa = []
for _ in range(n):
aaa.append([int(a) for a in input().split()])
aaar = list(zip(*aaa))
t = m * n
cost = 0
for i,aa in enumerate(aaar, 1... | python | code_algorithm | [
{
"input": "3 3\n3 2 1\n1 2 3\n4 5 6\n",
"output": "6\n"
},
{
"input": "3 4\n1 6 3 4\n5 10 7 8\n9 2 11 12\n",
"output": "2\n"
},
{
"input": "4 3\n1 2 3\n4 5 6\n7 8 9\n10 11 12\n",
"output": "0\n"
},
{
"input": "7 6\n42104 92376 101047 169739 147311 6\n9571 14822 9 147008 1080... | code_contests | python | 0 | 30872802c6aaa7bf54a33d53e8b3136b |
Ichihime is the current priestess of the Mahjong Soul Temple. She claims to be human, despite her cat ears.
These days the temple is holding a math contest. Usually, Ichihime lacks interest in these things, but this time the prize for the winner is her favorite — cookies. Ichihime decides to attend the contest. Now sh... | """
Author : thekushalghosh
Team : CodeDiggers
"""
import sys,math,cmath,time
start_time = time.time()
################# ---- USER DEFINED INPUT FUNCTIONS ---- #################
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(s[... | python | code_algorithm | [
{
"input": "4\n1 3 5 7\n1 5 5 7\n100000 200000 300000 400000\n1 1 977539810 977539810\n",
"output": "3 5 5\n5 5 5\n200000 300000 300000\n1 977539810 977539810\n"
},
{
"input": "1\n1 3 4 7\n",
"output": "3 4 4\n"
},
{
"input": "14\n2374453 2374454 8591131 23094546\n5813291 5813292 9709163... | code_contests | python | 0.5 | e231997b894e39831f1970d17986e702 |
We define x mod y as the remainder of division of x by y (\% operator in C++ or Java, mod operator in Pascal).
Let's call an array of positive integers [a_1, a_2, ..., a_k] stable if for every permutation p of integers from 1 to k, and for every non-negative integer x, the following condition is met:
(((x mod a_1) m... | import sys
import string
input = sys.stdin.readline
import math
#import numpy
#letters = list(string.ascii_lowercase)
from decimal import Decimal
n = list(map(int, input().split()))
n,k = n[0], n[1]
m = 998244353
fact = []
fac = 1
for i in range(1, n+2):
fac *= i
fac = fac % m
fact.append(fac)
ans = 0
f... | python | code_algorithm | [
{
"input": "500000 1\n",
"output": "500000\n"
},
{
"input": "1337 42\n",
"output": "95147305\n"
},
{
"input": "7 3\n",
"output": "16\n"
},
{
"input": "1 1\n",
"output": "1\n"
},
{
"input": "3 7\n",
"output": "0\n"
},
{
"input": "1 500000\n",
"outpu... | code_contests | python | 0 | 8cdadcb826ce3890d0a872dd0ae327ae |
In a far away land, there are two cities near a river. One day, the cities decide that they have too little space and would like to reclaim some of the river area into land.
The river area can be represented by a grid with r rows and exactly two columns — each cell represents a rectangular area. The rows are numbered ... | r,n = [int(x) for x in input().split()]
cells = [[int(x) for x in input().split()] for i in range(n)]
cells.sort()
#print(cells)
out = False
res = {True:"WIN",False:"LOSE"}
if len(cells) == 0: print(res[r%2 == 1])
else:
out = False
#print(cells[0][0] > 1)
#print(cells[-1][0] < r)
for i in range(1,n):
... | python | code_algorithm | [
{
"input": "1 1\n1 2\n",
"output": "LOSE\n"
},
{
"input": "12 2\n4 1\n8 1\n",
"output": "WIN\n"
},
{
"input": "3 1\n1 1\n",
"output": "WIN\n"
},
{
"input": "10 4\n8 2\n2 2\n5 2\n7 2\n",
"output": "WIN\n"
},
{
"input": "30 4\n11 2\n8 2\n21 1\n23 1\n",
"output":... | code_contests | python | 0 | 889935db45008bfe1081683358e27f45 |
Once Volodya was at the museum and saw a regular chessboard as a museum piece. And there were only four chess pieces on it: two white rooks, a white king and a black king. "Aha, blacks certainly didn't win!", — Volodya said and was right for sure. And your task is to say whether whites had won or not.
Pieces on the ch... | import sys
b = 'sabcdefgh'
a1,a2,a3,a4 = map(str,input().split())
x1 = b.index(a1[0])-1
y1 = int(a1[1]) -1
x2 = b.index(a2[0])-1
y2 = int(a2[1]) -1
x3 = b.index(a3[0])-1
y3 = int(a3[1]) -1
x4 = b.index(a4[0])-1
y4 = int(a4[1]) -1
c = []
for i in range(8):
c.append([0]*8)
pr = 0
pr1 = 0
pr4 = 0
pr3 = 0
for i in rang... | python | code_algorithm | [
{
"input": "a2 b1 a3 a1\n",
"output": "OTHER\n"
},
{
"input": "a6 b4 c8 a8\n",
"output": "CHECKMATE\n"
},
{
"input": "a6 c4 b6 b8\n",
"output": "OTHER\n"
},
{
"input": "e8 e7 d8 g8\n",
"output": "CHECKMATE\n"
},
{
"input": "b3 a8 d3 a3\n",
"output": "OTHER\n"
... | code_contests | python | 0 | 680bb2eba460a93bec35f7d05812411e |
You have written on a piece of paper an array of n positive integers a[1], a[2], ..., a[n] and m good pairs of integers (i1, j1), (i2, j2), ..., (im, jm). Each good pair (ik, jk) meets the following conditions: ik + jk is an odd number and 1 ≤ ik < jk ≤ n.
In one operation you can perform a sequence of actions:
* ... | # by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO,IOBase
def matching(n,m,path):
match1 = [-1]*n
match2 = [-1]*m
for node in range(n):
for nei in path[node]:
if match2[nei] == -1:
match1[node] = nei
match2[... | python | code_algorithm | [
{
"input": "3 2\n8 3 8\n1 2\n2 3\n",
"output": "0\n"
},
{
"input": "3 2\n8 12 8\n1 2\n2 3\n",
"output": "2\n"
},
{
"input": "20 10\n512 64 536870912 256 1 262144 8 2097152 8192 524288 32 2 16 16777216 524288 64 268435456 256 67108864 131072\n17 20\n2 13\n11 12\n18 19\n4 7\n4 13\n8 9\n14 ... | code_contests | python | 0 | f30efb4fb11aacdb610671c17f6ceac9 |
The developers of Looksery have to write an efficient algorithm that detects faces on a picture. Unfortunately, they are currently busy preparing a contest for you, so you will have to do it for them.
In this problem an image is a rectangular table that consists of lowercase Latin letters. A face on the image is a 2 ... | n, m = [int(i) for i in input().split()]
img = [list(input()) for i in range(n)]
cnt = 0
for i in range(n - 1):
for j in range(m - 1):
arr = [img[i][j], img[i + 1][j], img[i][j + 1], img[i + 1][j + 1]]
if 'f' in arr and 'a' in arr and 'c' in arr and 'e' in arr:
cnt += 1
print(cnt) | python | code_algorithm | [
{
"input": "4 2\nxx\ncf\nae\nxx\n",
"output": "1\n"
},
{
"input": "4 4\nxxxx\nxfax\nxcex\nxxxx\n",
"output": "1\n"
},
{
"input": "1 4\nface\n",
"output": "0\n"
},
{
"input": "2 3\nfac\ncef\n",
"output": "2\n"
},
{
"input": "2 5\nacdmw\nefazb\n",
"output": "1\n... | code_contests | python | 1 | 1bc30fc5a93080000c9c24d4115d2c40 |
Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x that a ≤ x ≤ b and x is divisible by k.
Input
The only line contains three space-separated integers k, a and b (1 ≤ k ≤ 1018; - 1018 ≤ a ≤ b ≤ 1018).
Output
Print the required number.
E... | k, a, b = map(int, input().split())
def Do(a, b,):
suma = 0
if a < 0:
if b == 0:
print(-a // k + 1)
if b > 0:
suma = 1
a = -a
if a % k == 0:
if b % k == 0:
suma += b // k + a // k
... | python | code_algorithm | [
{
"input": "2 -4 4\n",
"output": "5\n"
},
{
"input": "1 1 10\n",
"output": "10\n"
},
{
"input": "3 124 456\n",
"output": "111\n"
},
{
"input": "3 -191381 -1910\n",
"output": "63157\n"
},
{
"input": "3 -191380 -1910\n",
"output": "63157\n"
},
{
"input":... | code_contests | python | 0.4 | 2afaccfc8230eb4d6fa7cdeb5cca493a |
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.
Spring is starting and the Winter sleep is ... | import sys
window = set()
n, k, q = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
for i in range(q):
a, b = [int(x) for x in input().split()]
if (a == 1):
if (len(window) < k):
window.add(arr[b - 1])
else:
window.add(arr[b - 1])
m = min(window)
window.remove(m)
else:
... | python | code_algorithm | [
{
"input": "6 3 9\n50 20 51 17 99 24\n1 3\n1 4\n1 5\n1 2\n2 4\n2 2\n1 1\n2 4\n2 3\n",
"output": "NO\nYES\nNO\nYES\n"
},
{
"input": "4 2 8\n300 950 500 200\n1 3\n2 4\n2 3\n1 1\n1 2\n2 1\n2 2\n2 3\n",
"output": "NO\nYES\nNO\nYES\nYES\n"
},
{
"input": "20 2 15\n12698951 55128070 116962690 1... | code_contests | python | 0.8 | f6150e392f463db467c2695e2591077c |
Way to go! Heidi now knows how many brains there must be for her to get one. But throwing herself in the midst of a clutch of hungry zombies is quite a risky endeavor. Hence Heidi wonders: what is the smallest number of brains that must be in the chest for her to get out at all (possibly empty-handed, but alive)?
The ... | n = int(input())
if n & 1:
print(n//2)
else:
k = 1
while k <= n:
k *= 2
print((n - k//2)//2)
| python | code_algorithm | [
{
"input": "1\n",
"output": "0\n"
},
{
"input": "3\n",
"output": "1\n"
},
{
"input": "99\n",
"output": "49\n"
},
{
"input": "19\n",
"output": "9\n"
},
{
"input": "536870910\n",
"output": "134217727\n"
},
{
"input": "100\n",
"output": "18\n"
},
... | code_contests | python | 0 | a0d2fc5729932fa17899492a4f05fb57 |
Polycarp is crazy about round numbers. He especially likes the numbers divisible by 10k.
In the given number of n Polycarp wants to remove the least number of digits to get a number that is divisible by 10k. For example, if k = 3, in the number 30020 it is enough to delete a single digit (2). In this case, the result ... | n , k = map(int,input().split())
n = list(str(n))
counter = 0
zero_counter = 0
if n.count('0') >= k :
for i in range(len(n)-1 , -1 , -1):
if (n[i] != '0'):
counter += 1
if (n[i] == '0'):
zero_counter += 1
if (zero_counter == k):
break
print(counter)
else:
print(len(n) - 1) | python | code_algorithm | [
{
"input": "10203049 2\n",
"output": "3\n"
},
{
"input": "30020 3\n",
"output": "1\n"
},
{
"input": "100 9\n",
"output": "2\n"
},
{
"input": "1024 2\n",
"output": "3\n"
},
{
"input": "650 1\n",
"output": "0\n"
},
{
"input": "1034543 4\n",
"output":... | code_contests | python | 0.1 | a97e5cf97aaaf2af7c43e83d412051b8 |
Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to d... | def dfs(u,cur):
global ans
vis[u]=True
flag=True
for x in vec[u] :
v=x[0]
c=x[1]
if not vis[v] :
dfs(v,cur+c)
flag=False
if flag:
ans=max(cur,ans)
ans=0
vec=[]
vis=[]
i=0
n=int(input())
while(i<n) :
vec.append([])
vis.append(False)
... | python | code_algorithm | [
{
"input": "6\n1 2 3\n0 2 100\n1 4 2\n0 3 7\n3 5 10\n",
"output": "105\n"
},
{
"input": "4\n0 1 4\n0 2 2\n2 3 3\n",
"output": "5\n"
},
{
"input": "11\n1 0 1664\n2 0 881\n3 2 4670\n4 2 1555\n5 1 1870\n6 2 1265\n7 2 288\n8 7 2266\n9 2 1536\n10 6 3378\n",
"output": "5551\n"
},
{
... | code_contests | python | 0.7 | 5b1d7f69ae60d9f1582a22bbe7ebf2b4 |
Ivan wants to write a letter to his friend. The letter is a string s consisting of lowercase Latin letters.
Unfortunately, when Ivan started writing the letter, he realised that it is very long and writing the whole letter may take extremely long time. So he wants to write the compressed version of string s instead of... | def prefix(s):
p = [0]
for i in range(1, len(s)):
j = p[-1]
while j > 0 and s[j] != s[i]:
j = p[j - 1]
if s[i] == s[j]:
j += 1
p.append(j)
return p
s = input()
n = len(s)
ans = [0] * (n + 1)
i = n - 1
while i >= 0:
p = prefix(s[i:])
ans[i] =... | python | code_algorithm | [
{
"input": "cczabababab\n",
"output": "7\n"
},
{
"input": "abcab\n",
"output": "6\n"
},
{
"input": "aaaaaaaaaa\n",
"output": "3\n"
},
{
"input": "cbbbcccbbc\n",
"output": "10\n"
},
{
"input": "hltcdvuobkormkxkbmpfjniilublkrckmvvxemcyietgxcyjgrjwsdsgsfmoqnmbxozfavx... | code_contests | python | 0 | b015c689b659f93b6705cfe4580986ca |
A never-ending, fast-changing and dream-like world unfolds, as the secret door opens.
A world is an unordered graph G, in whose vertex set V(G) there are two special vertices s(G) and t(G). An initial world has vertex set {s(G), t(G)} and an edge between them.
A total of n changes took place in an initial world. In e... | mod = int(1e9 + 7)
n, m = map(int, input().split())
f = [ [0 for i in range(60)] for j in range(60) ]
g = [ [0 for i in range(60)] for j in range(60) ]
s = [ [0 for i in range(60)] for j in range(60) ]
inv = [ 1 ]
f[0][0] = s[0][0] = 1
def pow(x, exp) :
res = 1
for i in range(0, 31) :
if exp & 1 : res ... | python | code_algorithm | [
{
"input": "4 4\n",
"output": "3\n"
},
{
"input": "31 8\n",
"output": "64921457\n"
},
{
"input": "3 2\n",
"output": "6\n"
},
{
"input": "7 3\n",
"output": "1196\n"
},
{
"input": "6 1\n",
"output": "0\n"
},
{
"input": "50 33\n",
"output": "805999139... | code_contests | python | 0 | 97c4c0f97db02751f7102b01fc8f130d |
You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?
D... | I = lambda: map(int, input().split())
_, k = I()
A = list(I())
print(min(A) if k<2 else max(A) if k>2 else max(A[0], A[-1])) | python | code_algorithm | [
{
"input": "5 1\n-4 -5 -3 -2 -1\n",
"output": "-5\n"
},
{
"input": "5 2\n1 2 3 4 5\n",
"output": "5\n"
},
{
"input": "3 2\n1 5 3\n",
"output": "3\n"
},
{
"input": "10 2\n10 9 1 -9 -7 -9 3 8 -10 5\n",
"output": "10\n"
},
{
"input": "14 2\n-14 84 44 46 -75 -75 77 -4... | code_contests | python | 0.7 | d53ea07e551d09bb4a5755ca2d44c077 |
Are you going to Scarborough Fair?
Parsley, sage, rosemary and thyme.
Remember me to one who lives there.
He once was the true love of mine.
Willem is taking the girl to the highest building in island No.28, however, neither of them knows how to get there.
Willem asks his friend, Grick for directions, Grick helped... | _,m=map(int,input().split())
a=input()
for _ in [0]*m:
l,r,b,c=input().split();l=int(l)-1;r=int(r)
a=a[:l]+a[l:r].replace(b,c)+a[r:]
print(a) | python | code_algorithm | [
{
"input": "3 1\nioi\n1 1 i n\n",
"output": "noi\n"
},
{
"input": "5 3\nwxhak\n3 3 h x\n1 5 x a\n1 3 w g\n",
"output": "gaaak\n"
},
{
"input": "94 13\nbcaaaaaaccacddcdaacbdaabbcbaddbccbccbbbddbadddcccbddadddaadbdababadaacdcdbcdadabdcdcbcbcbcbbcd\n52 77 d d\n21 92 d b\n45 48 c b\n20 25 d ... | code_contests | python | 1 | f49353a38c583719c48b8eddf84ff980 |
As the guys fried the radio station facilities, the school principal gave them tasks as a punishment. Dustin's task was to add comments to nginx configuration for school's website. The school has n servers. Each server has a name and an ip (names aren't necessarily unique, but ips are). Dustin knows the ip and name of ... | a , b = map(int , input().split())
names = dict()
for i in range(a):
x , y = input().split()
names[y] = x
for i in range(b):
com = input()
h = com.split()[-1][:-1]
print(f"{com} #{names[h]}")
| python | code_algorithm | [
{
"input": "2 2\nmain 192.168.0.2\nreplica 192.168.0.1\nblock 192.168.0.1;\nproxy 192.168.0.2;\n",
"output": "block 192.168.0.1; #replica\nproxy 192.168.0.2; #main\n"
},
{
"input": "3 5\ngoogle 8.8.8.8\ncodeforces 212.193.33.27\nserver 138.197.64.57\nredirect 138.197.64.57;\nblock 8.8.8.8;\ncf 212.1... | code_contests | python | 0.9 | e128426d6a9418f432616bde0680b32f |
Right now she actually isn't. But she will be, if you don't solve this problem.
You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations:
1. Subtract 1 from x. This operation costs you A coins.
2. Divide x by k. Can be performed... | n = int(input())
k = int(input())
A = int(input())
B = int(input())
ans = 0
if k == 1:
print(A*(n-1))
exit(0)
while n > 1:
subt = (n % k)
ans += A * subt
n -= (n%k)
ans += min(A * (n - (n // k)),B)
n //= k
if n == 0:
ans -= A
print(ans) | python | code_algorithm | [
{
"input": "19\n3\n4\n2\n",
"output": "12\n"
},
{
"input": "9\n2\n3\n1\n",
"output": "6\n"
},
{
"input": "5\n5\n2\n20\n",
"output": "8\n"
},
{
"input": "1000000000\n1999999999\n789987\n184569875\n",
"output": "789986999210013\n"
},
{
"input": "1000999777\n19349993... | code_contests | python | 0 | c8283a91dc2a42de2cb53830d1ecd36f |
Vasya has two arrays A and B of lengths n and m, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array [1, ... | from sys import stdin
input=stdin.readline
n=int(input())
a=list(map(int,input().split()))
m=int(input())
b=list(map(int,input().split()))
if sum(a)!=sum(b):
print(-1)
exit()
j,k=0,0
sa,sb=0,0
cnt=0
pa,pb=1,1
while j<n and k<m:
if pa==1:
sa+=a[j]
if pb==1:
sb+=b[k]
if sa==sb:
cnt+=1
j+=1
k... | python | code_algorithm | [
{
"input": "3\n1 2 3\n3\n1 2 3\n",
"output": "3\n"
},
{
"input": "5\n11 2 3 5 7\n4\n11 7 3 7\n",
"output": "3\n"
},
{
"input": "2\n1 2\n1\n100\n",
"output": "-1\n"
},
{
"input": "2\n2 3\n1\n2\n",
"output": "-1\n"
},
{
"input": "49\n63 13 6 1 50 37 1 1 1 6 34 9 4 2... | code_contests | python | 0 | 216537a921f468b6fbe79d68830caf5d |
Let's call the following process a transformation of a sequence of length n.
If the sequence is empty, the process ends. Otherwise, append the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) (GCD) of all the elements of the sequence to the result and remove one arbitrary element from t... | from math import log2
n = int(input())
if n == 3:
print(1,1,3)
elif n == 1:
print(1)
else:
num = n // 2
num3 = 2 ** int(log2(num)) * 2
num2 = n % 2
print("1 " * (num2 + num),end="")
cur_num = 2
while num > 1:
num2 = num % 2
num //= 2
print((str(cur_num)+ " ") *(nu... | python | code_algorithm | [
{
"input": "3\n",
"output": "1 1 3\n"
},
{
"input": "1\n",
"output": "1\n"
},
{
"input": "2\n",
"output": "1 2\n"
},
{
"input": "20\n",
"output": "1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 4 4 4 8 16\n"
},
{
"input": "12\n",
"output": "1 1 1 1 1 1 2 2 2 4 4 12\n"
},
{... | code_contests | python | 0 | b6b0af2450991d3c3563244c29b57417 |
Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab.
There are n solutions, the i-th of them should be tested on a_i tests, testing one solution on one test takes 1 second. The solutions are judged in the order from 1 to n. There are k testing p... | n,k = map(int,input().split())
a = list(map(int,input().split()))
caption = 0
tested = [0 for i in range(n)]
interesting = [0 for i in range(n)]
ans = 0
while len(a) != 0:
m = min(a[0:k])
for j in range(m):
for i in range(min(k,len(a))):
tested[i] += 1
a[i] -= 1
if ca... | python | code_algorithm | [
{
"input": "14 5\n48 19 6 9 50 20 3 42 38 43 36 21 44 6\n",
"output": "5\n"
},
{
"input": "2 2\n49 100\n",
"output": "1\n"
},
{
"input": "4 2\n32 100 33 1\n",
"output": "2\n"
},
{
"input": "1 100\n79\n",
"output": "0\n"
},
{
"input": "50 3\n33 7 96 30 68 37 44 50 ... | code_contests | python | 0 | aaf8945934af2a6363aa0fd1174350e3 |
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place.
You can perform zero or more operations of the following type:
* take two stones with indices i and j so that s_i ≤ s_j, choose an integer d (0 ≤ 2 ⋅ d ≤ s_j - s_i)... | from collections import namedtuple
Stone = namedtuple('Stone', ['s', 'i'])
def debug(*args, **kwargs):
import sys
#print(*args, *('{}={}'.format(k, v) for k, v in kwargs.items()), sep='; ', file=sys.stderr)
def solve(n, s, t):
#debug(s=s, t=t)
s = list(map(lambda s_i: Stone(s_i[1], s_i[0]), enumer... | python | code_algorithm | [
{
"input": "3\n1 5 10\n3 5 7\n",
"output": "NO\n"
},
{
"input": "5\n2 2 7 4 9\n5 4 5 5 5\n",
"output": "YES\n3\n1 3 2\n2 5 3\n4 5 1\n"
},
{
"input": "1\n1\n1\n",
"output": "YES\n0\n"
},
{
"input": "1\n1\n1000000000\n",
"output": "NO\n"
},
{
"input": "5\n16 2 8 19 ... | code_contests | python | 0 | 33acd32ee908d34b68ba77777ec95472 |
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n × m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of ... | n,m = map(int,input().split())
x,cnt = [],0
for _ in range(n):
s = list(map(str,input()))
for i in s:
x.append(i)
if n==m==1: print(0)
elif n==1 or m==1:
for i in range(len(x)):
if x[i]=='W':
if i==0:
if x[i+1]=='P': x[i+1]='.';cnt+=1
elif i==(len(x)-1):
if x[i-1]=='P': x[i-1]='.';cnt+=1
else:
... | python | code_algorithm | [
{
"input": "2 3\nPPW\nW.P\n",
"output": "2\n"
},
{
"input": "3 3\nP.W\n.P.\nW.P\n",
"output": "0\n"
},
{
"input": "10 10\nPPPPPPPPPP\nPPPPPPPWPP\nPPPPPPPPPP\nPPPPPPPPPP\nPPPPPPPPPP\nPPPPPPPPPP\nPPPPPPPPPP\nPPPPPPPPPP\nPPPPPPPPPP\nPPPPPPPPPP\n",
"output": "1\n"
},
{
"input": "... | code_contests | python | 0.8 | 3bd02cc64ddf2ff4dedc145216d06dc9 |
You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0.
You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 × 2, and replace every element in the chosen sub... | #!/usr/bin/python3
import pdb
n, m = map(int, input().split(" "))
matrix = []
for i in range(n):
matrix.append(list(map(int, input().split(" "))))
def check_pos(matrix, x, y):
if x + 1 >= len(matrix):
return False
if y + 1 >= len(matrix[0]):
return False
if matrix[x][y] == 0 or matrix[x+1][y] == 0 or matrix[x... | python | code_algorithm | [
{
"input": "3 2\n0 0\n0 0\n0 0\n",
"output": "0\n"
},
{
"input": "3 3\n1 1 1\n1 1 1\n0 1 1\n",
"output": "3\n1 1\n1 2\n2 2\n"
},
{
"input": "3 3\n1 0 1\n1 0 1\n0 0 0\n",
"output": "-1\n"
},
{
"input": "4 4\n1 1 1 1\n1 1 1 1\n0 1 0 0\n0 0 0 0\n",
"output": "-1\n"
},
{
... | code_contests | python | 0 | d6b361d394a0f4e4b11631a6535e85ab |
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer.
For example,... | if __name__ == '__main__':
n, p = map(int, input().split())
aws = 1
while True:
min_number = aws * ((2**0) + p)
if min_number > n:
aws = -1
break
elif min_number == n:
break
elif bin((n - aws * p)).count('1') <= aws:
break
... | python | code_algorithm | [
{
"input": "24 0\n",
"output": "2\n"
},
{
"input": "1 1\n",
"output": "-1\n"
},
{
"input": "24 -1\n",
"output": "4\n"
},
{
"input": "24 1\n",
"output": "3\n"
},
{
"input": "4 -7\n",
"output": "2\n"
},
{
"input": "47 23\n",
"output": "-1\n"
},
{... | code_contests | python | 0 | c7dae3f3b5fab0e21dfa7d35617ca683 |
You are given a Young diagram.
Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1).
<image> Young diagram for a=[3,2,2,2,1].
Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 recta... | # by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO,IOBase
def main():
n = int(input())
a = list(map(int,input().split()))
base,su,bl = 0,sum(a),0
for i in range(n):
if not base:
bl += (a[i]+1)//2
else:
bl += a[i]//2
... | python | code_algorithm | [
{
"input": "5\n3 2 2 2 1\n",
"output": "4\n"
},
{
"input": "1\n1\n",
"output": "0\n"
},
{
"input": "3\n3 3 3\n",
"output": "4\n"
},
{
"input": "1\n300000\n",
"output": "150000\n"
},
{
"input": "10\n99 83 62 53 47 33 24 15 10 9\n",
"output": "216\n"
},
{
... | code_contests | python | 0 | 87d3b5c03b922f9a18f79eb671e32105 |
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you ... | from sys import stdin
input = stdin.readline
n , k = [int(i) for i in input().split()]
pairs = [i + k for i in range(k)] + [i for i in range(k)]
initial_condition = list(map(lambda x: x == '1',input().strip()))
data = [i for i in range(2*k)]
constrain = [-1] * (2*k)
h = [0] * (2*k)
L = [1] * k + [0] * k
dp1 = [-1 for... | python | code_algorithm | [
{
"input": "19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"output": "0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n"
},
{
"input": "8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"output": "1\n1\n1\n1\n1\n1\n4\n4\n"
},
{
... | code_contests | python | 0 | ae86fea13f1ef4e812fa9f7d6a89219a |
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
*... | import sys;input=sys.stdin.readline
N, a, b, c, k = map(int, input().split())
X = list(map(int, input().split()))
dp = [[10**18]*2 for _ in range(N+1)]
dp[0][0] = 0
R = 10**18
for i in range(N):
dp[i+1][0] = min(dp[i][0] + a*X[i]+c, dp[i][1] + min(a*(X[i]+2), a+b))
dp[i+1][1] = dp[i][0] + min(a*(X[i]+2), a+b) +... | python | code_algorithm | [
{
"input": "4 2 4 4 1\n4 5 1 2\n",
"output": "31\n"
},
{
"input": "4 1 3 4 3\n3 2 5 1\n",
"output": "34\n"
},
{
"input": "2 2 5 7 3\n4 5\n",
"output": "23\n"
},
{
"input": "77 2 8 8 3\n7 9 3 6 2 7 8 4 4 1 8 6 1 7 6 3 4 6 1 1 6 5 6 6 4 8 7 5 10 6 9 2 1 2 4 5 1 3 8 2 2 7 3 8 8 ... | code_contests | python | 0 | 7e5d155e40823430f660e873f109866e |
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chanc... | w,b = list( map(int, input().split()) )
p = []
for i in range(w+1): p.append([0]*(b+1))
for i in range(1,w+1): p[i][0] = 1
for i in range(1,w+1):
for j in range(1,b+1):
p[i][j] = i/(i+j)
if j>=3:
p[i][j] += (j/(i+j)) * ((j-1)/(i+j-1)) * ((j-2)/(i+j-2)) * p[i][j-3]
if j>=2:
... | python | code_algorithm | [
{
"input": "1 3\n",
"output": "0.500000000\n"
},
{
"input": "5 5\n",
"output": "0.658730159\n"
},
{
"input": "773 467\n",
"output": "0.726347987\n"
},
{
"input": "50 372\n",
"output": "0.531448979\n"
},
{
"input": "329 31\n",
"output": "0.920544382\n"
},
{... | code_contests | python | 0 | f516559dca7229de9e5cd1341ad68b70 |
A median in an array with the length of n is an element which occupies position number <image> after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17.
We define... | n, x = map(int, input().split())
a = list(map(int, input().split()))
ret = 0
if x not in a:
a.append(x)
ret += 1
a.sort()
while a[(len(a) + 1) // 2 - 1] != x:
a.append(x)
a.sort()
ret += 1
print(ret)
| python | code_algorithm | [
{
"input": "3 4\n1 2 3\n",
"output": "4\n"
},
{
"input": "3 10\n10 20 30\n",
"output": "1\n"
},
{
"input": "100 37\n20 20 3 35 36 14 30 9 33 36 32 46 43 22 43 50 34 6 13 25 1 34 10 6 21 30 19 17 1 23 9 23 38 21 7 43 49 28 33 42 4 19 39 23 12 42 31 13 26 23 1 26 24 48 10 6 12 48 40 18 10 ... | code_contests | python | 0.1 | d64b48fb63d9c97f5fb3fb4ea7bebf5d |
A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules:
* A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right;
* Before each move the table has several piles of cards lying ... | n = int(input())
arr = list(input().split())
def evl(c1, c2):
return c1[0] == c2[0] or c1[1] == c2[1]
vt = set()
q = [arr[:]]
while q:
ar = q.pop()
vt.add(''.join(ar))
if len(ar) > 3 and evl(ar[-1], ar[-4]):
tmp = ar[:len(ar) - 1]
tmp[-3] = ar[-1]
if ''.join(tmp) not in vt:
... | python | code_algorithm | [
{
"input": "2\n3S 2C\n",
"output": "NO\n"
},
{
"input": "4\n2S 2S 2C 2C\n",
"output": "YES\n"
},
{
"input": "21\nJS 5S 9S KH 9D JH 3S KH QH 5D TC 3S 5S 4H 4H 5S 7S AH 4S 3S 6S\n",
"output": "NO\n"
},
{
"input": "11\nJS KH JC JS 9S 9H 6H 7H JH AS AH\n",
"output": "YES\n"
... | code_contests | python | 0 | 3a0e98d7d0692b74b54ff3502fad8384 |
Since most contestants do not read this part, I have to repeat that Bitlandians are quite weird. They have their own jobs, their own working method, their own lives, their own sausages and their own games!
Since you are so curious about Bitland, I'll give you the chance of peeking at one of these games.
BitLGM and Bi... | from math import *
n=int(input())
if n==3:
li=list(map(int,input().split()))
ans=0
flag=0
for i in li:
ans^=i
if ans==0:
print("BitAryo")
else:
print("BitLGM")
elif n==2:
li=list(map(int,input().split()))
li.sort()
phi=(1+sqrt(5))/2
ch=[0]*(785)
for i ... | python | code_algorithm | [
{
"input": "2\n1 1\n",
"output": "BitLGM\n"
},
{
"input": "3\n1 2 1\n",
"output": "BitLGM\n"
},
{
"input": "2\n1 2\n",
"output": "BitAryo\n"
},
{
"input": "3\n299 290 288\n",
"output": "BitLGM\n"
},
{
"input": "3\n124 47 228\n",
"output": "BitLGM\n"
},
{
... | code_contests | python | 0 | faae7fac838b2fe8ddbab7bc43e9b82a |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.