index
int64
0
5.16k
difficulty
int64
7
12
question
stringlengths
126
7.12k
solution
stringlengths
30
18.6k
test_cases
dict
500
8
Alexander is a well-known programmer. Today he decided to finally go out and play football, but with the first hit he left a dent on the new Rolls-Royce of the wealthy businessman Big Vova. Vladimir has recently opened a store on the popular online marketplace "Zmey-Gorynych", and offers Alex a job: if he shows his pro...
def gcd(x, y): while(y): x, y = y, x % y return x for _ in range(int(input())): n=int(input()) l=list(map(int,input().split())) m=x=0 while(l): m,x=max((gcd(m,i),i)for i in l) print(x) l.remove(x)
{ "input": [ "7\n2\n2 5\n4\n1 8 2 3\n3\n3 8 9\n5\n64 25 75 100 50\n1\n42\n6\n96 128 88 80 52 7\n5\n2 4 8 16 17\n" ], "output": [ "5 2\n8 2 1 3\n9 3 8\n100 50 25 75 64\n42\n128 96 80 88 52 7\n17 2 4 8 16\n" ] }
501
12
Igor had a sequence d_1, d_2, ..., d_n of integers. When Igor entered the classroom there was an integer x written on the blackboard. Igor generated sequence p using the following algorithm: 1. initially, p = [x]; 2. for each 1 ≀ i ≀ n he did the following operation |d_i| times: * if d_i β‰₯ 0, then he looke...
import sys, io, os if os.environ['USERNAME']=='kissz': inp=open('in55.txt','r').readline def debug(*args): print(*args,file=sys.stderr) else: inp=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def debug(*args): pass def mexp(size,power): A=[] for i in range(size): ...
{ "input": [ "3\n100\n5 -3 6\n", "3\n3\n1 -1 2\n", "5\n34\n1337 -146 42 -69 228\n", "3\n1\n999999999 0 1000000000\n" ], "output": [ "\n9 7\n", "\n3 3\n", "\n1393 3876\n", "\n2000000000 1\n" ] }
502
9
Several months later Alex finally got his brother Bob's creation by post. And now, in his turn, Alex wants to boast about something to his brother. He thought for a while, and came to the conclusion that he has no ready creations, and decided to write a program for rectangles detection. According to his plan, the progr...
def main(): dict = {0: 0, 1: 0} final = "YES" for i in range(4): x1, y1, x2, y2 = input().split() if(x1 == x2 and y1 != y2): dict[0] += 1 elif(y1 == y2 and x1 != x2): dict[1] += 1 for j in [(x1, y1), (x2, y2)]: dict[j] = dict.get(j, 0) + 1 for i in dict.values(): if(i ...
{ "input": [ "0 0 0 3\n2 0 0 0\n2 2 2 0\n0 2 2 2\n", "1 1 6 1\n1 0 6 0\n6 0 6 1\n1 1 1 0\n" ], "output": [ "NO\n", "YES\n" ] }
503
8
As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answe...
# SHRi GANESHA author: Kunal Verma # import os import sys from bisect import bisect_right from collections import Counter, defaultdict, deque from heapq import * from io import BytesIO, IOBase from math import gcd, inf, sqrt, ceil def lcm(a, b): return (a * b) // gcd(a, b) ''' mod = 10 ** 9 + 7 fac = [1]...
{ "input": [ "4 1\n+2\n-3\n+4\n-1\n", "1 1\n+1\n", "3 2\n-1\n-2\n-3\n" ], "output": [ "Lie\nNot defined\nLie\nNot defined\n", "Truth\n", "Not defined\nNot defined\nNot defined\n" ] }
504
12
The Smart Beaver from ABBYY was offered a job of a screenwriter for the ongoing TV series. In particular, he needs to automate the hard decision: which main characters will get married by the end of the series. There are n single men and n single women among the main characters. An opinion poll showed that viewers lik...
I=lambda:list(map(int,input().split())) n,k,T=I() t=[I()for _ in '0'*k] def b(h,w,r,a): if h>n:a+=[r] else: b(h+1,w,r,a) for f,s,v in t: if f==h and s in w:b(h+1,w-set([s]),r+v,a) return a print(sorted(b(1,set(range(1,n+1)), 0,[]))[T-1])
{ "input": [ "2 4 3\n1 1 1\n1 2 2\n2 1 3\n2 2 7\n", "2 4 7\n1 1 1\n1 2 2\n2 1 3\n2 2 7\n" ], "output": [ "2\n", "8\n" ] }
505
9
Qwerty the Ranger took up a government job and arrived on planet Mars. He should stay in the secret lab and conduct some experiments on bacteria that have funny and abnormal properties. The job isn't difficult, but the salary is high. At the beginning of the first experiment there is a single bacterium in the test tub...
#------------------------template--------------------------# import os import sys from math import * from collections import * from fractions import * from bisect import * from heapq import* from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') BUF...
{ "input": [ "2 2 4 100\n", "3 1 3 5\n", "1 4 4 7\n" ], "output": [ "0\n", "2\n", "3\n" ] }
506
10
A boy named Vasya has taken part in an Olympiad. His teacher knows that in total Vasya got at least x points for both tours of the Olympiad. The teacher has the results of the first and the second tour of the Olympiad but the problem is, the results have only points, no names. The teacher has to know Vasya's chances. ...
import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.wri...
{ "input": [ "6 7\n4 3 5 6 4 4\n8 6 0 4 3 4\n", "5 2\n1 1 1 1 1\n1 1 1 1 1\n" ], "output": [ "1 5\n", "1 5\n" ] }
507
8
Polycarpus has an array, consisting of n integers a1, a2, ..., an. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times: * he chooses two elements of the array ai, aj (i β‰  j); ...
def solve(l): n = len(l) if(not sum(l)%n): print(n) else: print(n-1) N = int(input()) l = list(map(int, input().split())) ans = solve(l)
{ "input": [ "3\n1 4 1\n", "2\n2 1\n" ], "output": [ "3\n", "1\n" ] }
508
9
The Greatest Secret Ever consists of n words, indexed by positive integers from 1 to n. The secret needs dividing between k Keepers (let's index them by positive integers from 1 to k), the i-th Keeper gets a non-empty set of words with numbers from the set Ui = (ui, 1, ui, 2, ..., ui, |Ui|). Here and below we'll presup...
import sys def solve(): n, k = map(int, input().split()) if n // k < 3 or n < 6: print(-1) return res = list() for i in range(1, k + 1): res.append(i) res.append(i) for i in range(1, k + 1): res.append(i) while len(res) < n: res.append(1); print(" ".j...
{ "input": [ "11 3\n", "5 2\n" ], "output": [ "1 1 2 2 3 3 1 2 3 1 1\n", "-1" ] }
509
9
Let us call a pair of integer numbers m-perfect, if at least one number in the pair is greater than or equal to m. Thus, the pairs (3, 3) and (0, 2) are 2-perfect while the pair (-1, 1) is not. Two integers x, y are written on the blackboard. It is allowed to erase one of them and replace it with the sum of the number...
#_________________ Mukul Mohan Varshney _______________# #Template import sys import os import math import copy from math import gcd from bisect import bisect from io import BytesIO, IOBase from math import sqrt,floor,factorial,gcd,log,ceil from collections import deque,Counter,defaultdict from itertools import permut...
{ "input": [ "-1 4 15\n", "1 2 5\n", "0 -1 5\n" ], "output": [ "4\n", " 2\n", "-1\n" ] }
510
10
Fox Ciel wants to write a task for a programming contest. The task is: "You are given a simple undirected graph with n vertexes. Each its edge has unit length. You should calculate the number of shortest paths between vertex 1 and vertex 2." Same with some writers, she wants to make an example with some certain output...
n, m, cnt = int(input()), 148, 0 ans = [['N'] * m for i in range(m)] def edge(i, j): ans[i][j] = ans[j][i] = 'Y' def node(adj): global cnt i = cnt cnt += 1 for j in adj: edge(i, j) return i start = node([]) end = node([]) choice = node([end] if n&1 else []) for i in range(1, 30): ...
{ "input": [ "2", "1", "9" ], "output": [ "4\nNNYY\nNNYY\nYYNN\nYYNN\n", "2\nNY\nYN\n", "11\nNNYYNNNNYNN\nNNNNNNYYNNY\nYNNNYYNNNNN\nYNNNYYNNNNN\nNNYYNNYYNNN\nNNYYNNYYNNN\nNYNNYYNNNNN\nNYNNYYNNNNN\nYNNNNNNNNYN\nNNNNNNNNYNY\nNYNNNNNNNYN\n" ] }
511
8
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks. Fortunately, Picks remembers something about his set S: * its elements were distinct integers from 1 to limit; * the value of <ima...
def lowbit(x): return x & (- x) s, l = map(int , input().split()) lst = [] while s > 0 and l > 0: if s - l >= 0: s -= lowbit(l) lst.append(l) l -= 1 if s != 0: print(-1) else: print(len(lst)) print(*lst)
{ "input": [ "4 3\n", "5 1\n", "5 5\n" ], "output": [ "3\n3 2 1 ", "-1", "2\n5 4 " ] }
512
10
On a history lesson the teacher asked Vasya to name the dates when n famous events took place. He doesn't remembers the exact dates but he remembers a segment of days [li, ri] (inclusive) on which the event could have taken place. However Vasya also remembers that there was at most one event in one day. Help him choose...
# http://codeforces.com/contest/45/problem/D from sys import stdin inFile = stdin tokens = [] tokens_next = 0 def next_str(): global tokens, tokens_next while tokens_next >= len(tokens): tokens = inFile.readline().split() tokens_next = 0 tokens_next += 1 return tokens[tokens_next - 1] ...
{ "input": [ "3\n1 2\n2 3\n3 4\n", "2\n1 3\n1 3\n" ], "output": [ "1 2 3 ", "1 2 " ] }
513
8
You are given a n Γ— m field consisting only of periods ('.') and asterisks ('*'). Your task is to count all right triangles with two sides parallel to the square sides, whose vertices are in the centers of '*'-cells. A right triangle is a triangle in which one angle is a right angle (that is, a 90 degree angle). Input...
import sys from array import array # noqa: F401 def input(): return sys.stdin.buffer.readline().decode('utf-8') n, m = map(int, input().split()) a = ([[0] * (m + 2)] + [[0] + list(1 if c == '*' else 0 for c in input().rstrip()) + [0] for _ in range(n)] + [[0] * (m + 2)]) lr = [[0] * (m + 2) for _ in...
{ "input": [ "3 4\n*..*\n.**.\n*.**\n", "2 2\n**\n*.\n" ], "output": [ "9", "1" ] }
514
9
Petya loves computer games. Finally a game that he's been waiting for so long came out! The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is...
import math def func(x): return 10*math.ceil(x/10)-x n,k = map(int,input().split()) skills = list(map(int,input().split())) skills_sort = sorted(skills,key=func) rating = tens = 0 for i in skills_sort: p = math.ceil(i/10) tens += (100-p*10)//10 t = p*10-i if k>=t: rating += p k -= t ...
{ "input": [ "2 2\n99 100\n", "2 4\n7 9\n", "3 8\n17 15 19\n" ], "output": [ "20\n", "2\n", "5\n" ] }
515
8
As behooves any intelligent schoolboy, Kevin Sun is studying psycowlogy, cowculus, and cryptcowgraphy at the Bovinia State University (BGU) under Farmer Ivan. During his Mathematics of Olympiads (MoO) class, Kevin was confronted with a weird functional equation and needs your help. For two fixed integers k and p, where...
def main(): p, k = map(int, input().split()) s = 1 m = pow(10,9)+7 if k == 0: s = pow(p,p-1,m) elif k == 1: s = pow(p,p,m) else: o = 1 n = k while n != 1: n = k*n %p o += 1 c = (p-1)//o s = pow(p,c,m) print(s%m) main()
{ "input": [ "3 2\n", "5 4\n" ], "output": [ "3", "25" ] }
516
7
Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its first element is equal to a (s1 = a), and the difference between any two neighbouring elements is equal to c (si - si - 1 = c). In particular, Vasya wonders if his favourite integer b appears in this sequence, that is, the...
def f(l): a,b,c = l if c==0: return a==b return (b-a)%c==0 and (b-a)//c>=0 l = list(map(int,input().split())) print('YES' if f(l) else 'NO')
{ "input": [ "1 -4 5\n", "1 7 3\n", "0 60 50\n", "10 10 0\n" ], "output": [ "NO\n", "YES\n", "NO\n", "YES\n" ] }
517
8
You are given a description of a depot. It is a rectangular checkered field of n Γ— m size. Each cell in a field can be empty (".") or it can be occupied by a wall ("*"). You have one bomb. If you lay the bomb at the cell (x, y), then after triggering it will wipe out all walls in the row x and all walls in the column...
import sys;input = sys.stdin.readline;print = sys.stdout.write def main(): n, m = map(int, input().split()) arr, have, dpx, dpy, cnt = [0]*n, set(), [0]*n, [0]*m, 0 for i in range(n): arr[i] = input().rstrip() for j in range(m): if arr[i][j] == "*": dpx[i], dpy[...
{ "input": [ "3 3\n..*\n.*.\n*..\n", "6 5\n..*..\n..*..\n*****\n..*..\n..*..\n..*..\n", "3 4\n.*..\n....\n.*..\n" ], "output": [ "NO\n", "YES\n3 3\n", "YES\n1 2\n" ] }
518
10
Vasya has a pack of 54 cards (52 standard cards and 2 distinct jokers). That is all he has at the moment. Not to die from boredom, Vasya plays Solitaire with them. Vasya lays out nm cards as a rectangle n Γ— m. If there are jokers among them, then Vasya should change them with some of the rest of 54 - nm cards (which a...
ranks = '23456789TJQKA' suits = 'CDHS' n, m = [int(i) for i in input().split()] b = [input().split() for _ in range(n)] p = [r + s for r in ranks for s in suits] j1, j2 = False, False for r in b: for c in r: if c == 'J1': j1 = True elif c == 'J2': j2 = True else: ...
{ "input": [ "4 6\n2S 3S 4S 7S 8S AS\n5H 6H 7H 5S TC AC\n8H 9H TH 7C 8C 9C\n2D 2C 3C 4C 5C 6C\n", "4 6\n2S 3S 4S 7S 8S AS\n5H 6H 7H QC TC AC\n8H 9H TH 7C 8C 9C\n2D 2C 3C 4C 5C 6C\n", "4 6\n2S 3S 4S 7S 8S AS\n5H 6H 7H J1 TC AC\n8H 9H TH 7C 8C 9C\n2D 2C 3C 4C 5C 6C\n" ], "output": [ "No solution.\n"...
519
7
As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are a...
def gcd(a, b): return a if b == 0 else gcd(b, a % b) def lcm(a, b): return a * b // gcd(a, b) n = int(input()) c = [int(v)-1 for v in input().split(' ')] t = 1 for i in range(n): k = i for j in range(1, n+2): k = c[k] if k == i: break if j > n: print(-1) e...
{ "input": [ "4\n2 1 4 3\n", "4\n4 4 4 4\n", "4\n2 3 1 4\n" ], "output": [ "1\n", "-1\n", "3\n" ] }
520
11
Little Timofey likes integers a lot. Unfortunately, he is very young and can't work with very big integers, so he does all the operations modulo his favorite prime m. Also, Timofey likes to look for arithmetical progressions everywhere. One of his birthday presents was a sequence of distinct integers a1, a2, ..., an. ...
from functools import reduce def gcd_extended(bigger, less): if less == 0: return(bigger, 1, 0) mod = bigger % less div = bigger // less gcd, c_less, c_mod = gcd_extended(less, mod) #gcd == c_less * less + c_mod * mod #mod == bigger - div * less #gcd = (c_less - c_mod * div) * less ...
{ "input": [ "17 5\n0 2 4 13 15\n", "5 3\n1 2 3\n", "17 5\n0 2 4 13 14\n" ], "output": [ " 13 2\n", " 1 ...
521
8
<image> It's the end of July – the time when a festive evening is held at Jelly Castle! Guests from all over the kingdom gather here to discuss new trends in the world of confectionery. Yet some of the things discussed here are not supposed to be disclosed to the general public: the information can cause discord in th...
def main(): n, m = map(int, input().split()) s, l = input(), [] for c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ': i = s.find(c) if i != -1: l += (((i, -1), (s.rfind(c), 1))) for _, i in sorted(l): m += i if m < 0: print('YES') return print('NO...
{ "input": [ "5 1\nAABBB\n", "5 1\nABABB\n" ], "output": [ "NO\n", "YES\n" ] }
522
9
Petya learned a new programming language CALPAS. A program in this language always takes one non-negative integer and returns one non-negative integer as well. In the language, there are only three commands: apply a bitwise operation AND, OR or XOR with a given constant to the current integer. A program can contain an...
def fun (n1,o,n2): if o=="|": return n1|n2 elif o=="&": return n1&n2 else: return n1^n2 def fun2(n): l=[0 for i in range(10)] for i in range(9,-1,-1): if 1<<i <=n: l[i]=1 n-=1<<i return l n=int(input()) a=0 b=1023 for i in range(n): o,n...
{ "input": [ "3\n&amp; 1\n&amp; 3\n&amp; 5\n", "3\n^ 1\n^ 2\n^ 3\n", "3\n| 3\n^ 2\n| 1\n" ], "output": [ "3\n& 0\n| 0\n^ 0\n", "3\n& 1023\n| 0\n^ 0\n", "3\n& 1021\n| 1\n^ 0\n" ] }
523
7
In the year of 30XX participants of some world programming championship live in a single large hotel. The hotel has n floors. Each floor has m sections with a single corridor connecting all of them. The sections are enumerated from 1 to m along the corridor, and all sections with equal numbers on different floors are l...
from bisect import bisect_left input = __import__('sys').stdin.readline MIS = lambda: map(int,input().split()) def walk(x1, y1, x2, y2, L, v): if y1 == y2: return abs(x2-x1) dy = abs(y1-y2) vertical = dy // v if dy%v: vertical+= 1 i = bisect_left(L, x1) xs1 = L[i-1] if 0<=i-1<len(L) e...
{ "input": [ "5 6 1 1 3\n2\n5\n3\n1 1 5 6\n1 3 5 4\n3 3 5 3\n" ], "output": [ "7\n5\n4\n" ] }
524
8
You are given a string s consisting of n lowercase Latin letters. You have to type this string using your keyboard. Initially, you have an empty string. Until you type the whole string, you may perform the following operation: * add a character to the end of the string. Besides, at most once you may perform one...
def main(): n = int(input()) line = input() ans = 0 for i in range(50): if line[:i + 1] == line[i + 1:2 * (i + 1)]: ans = i print(n - ans) main()
{ "input": [ "8\nabcdefgh\n", "7\nabcabca\n" ], "output": [ "8\n", "5\n" ] }
525
7
A necklace can be described as a string of links ('-') and pearls ('o'), with the last link or pearl connected to the first one. <image> You can remove a link or a pearl and insert it between two other existing links or pearls (or between a link and a pearl) on the necklace. This process can be repeated as many times...
def go(): s = input() p = s.count('o') l = s.count('-') if p == 0: return 'YES' if l % p == 0: return 'YES' return 'NO' print(go())
{ "input": [ "<span class=\"tex-font-style-tt\">-o---o-</span>\n", "<span class=\"tex-font-style-tt\">-o-o--</span>", "ooo\n", "<span class=\"tex-font-style-tt\">-o---</span>\n" ], "output": [ "NO\n", "NO\n", "YES\n", "NO\n" ] }
526
7
Yakko, Wakko and Dot, world-famous animaniacs, decided to rest from acting in cartoons, and take a leave to travel a bit. Yakko dreamt to go to Pennsylvania, his Motherland and the Motherland of his ancestors. Wakko thought about Tasmania, its beaches, sun and sea. Dot chose Transylvania as the most mysterious and unpr...
from fractions import Fraction def main(): a = Fraction(7 - max(map(int, input().split())), 6) print(a if a != 1 else '1/1') main()
{ "input": [ "4 2\n" ], "output": [ "1/2\n" ] }
527
9
The heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnormally hot. But any scientific claim sounds a lot more reasonable if there are some numbers involved, so they have decided to actually cal...
def mp():return map(int,input().split()) def it():return int(input()) n,k=mp() l=list(mp()) ans=0 for i in range(n): avg,count=0,0 for j in range(i,n): count+=l[j] if j-i+1>=k: avg=count/(j-i+1) ans=max(avg,ans) print(ans)
{ "input": [ "4 3\n3 4 1 2\n" ], "output": [ "2.666666666666667" ] }
528
10
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 4 (hopefully) years. The dormitory consists of n rooms and a single mouse! Girls decided to set mouse tra...
def main(): n=int(input()) c=[int(i) for i in input().split()] a=[int(i) for i in input().split()] vis=[0]*(n+1) sum=0 for i in range(1,n+1): x=i while(vis[x]==0): vis[x]=i x=a[x-1] ...
{ "input": [ "4\n1 10 2 10\n2 4 2 2\n", "5\n1 2 3 2 10\n1 3 4 3 3\n", "7\n1 1 1 1 1 1 1\n2 2 2 3 6 7 6\n" ], "output": [ "10", "3", "2" ] }
529
9
Formula 1 officials decided to introduce new competition. Cars are replaced by space ships and number of points awarded can differ per race. Given the current ranking in the competition and points distribution for the next race, your task is to calculate the best possible ranking for a given astronaut after the next r...
import sys def get_ints(): return map(int, sys.stdin.readline().strip().split()) n,d=get_ints() s=list(map(int,input().split())) p=list(map(int,input().split())) m=s.pop(d-1) a=p.pop(0)+m p.reverse() j=0 ans=-1 for i in range(n-1): if s[i]+p[j]>a: z=p.pop(-1) s[i]+=z else: s[i]+=p[j] ...
{ "input": [ "4 3\n50 30 20 10\n15 10 7 3\n" ], "output": [ "2" ] }
530
8
Berkomnadzor β€” Federal Service for Supervision of Communications, Information Technology and Mass Media β€” is a Berland federal executive body that protects ordinary residents of Berland from the threats of modern internet. Berkomnadzor maintains a list of prohibited IPv4 subnets (blacklist) and a list of allowed IPv4 ...
#!/usr/bin/env python3 # Copied solution import collections import sys import traceback class Input(object): def __init__(self): self.fh = sys.stdin def next_line(self): while True: line = sys.stdin.readline() if line == '\n': continue retur...
{ "input": [ "5\n-127.0.0.4/31\n+127.0.0.8\n+127.0.0.0/30\n-195.82.146.208/29\n-127.0.0.6/31\n", "4\n-149.154.167.99\n+149.154.167.100/30\n+149.154.167.128/25\n-149.154.167.120/29\n", "2\n+127.0.0.1/32\n-127.0.0.1\n", "1\n-149.154.167.99\n" ], "output": [ "2\n127.0.0.4/30\n128.0.0.0/1\n", ...
531
12
Bob is a duck. He wants to get to Alice's nest, so that those two can duck! <image> Duck is the ultimate animal! (Image courtesy of See Bang) The journey can be represented as a straight line, consisting of n segments. Bob is located to the left of the first segment, while Alice's nest is on the right of the last seg...
def read(type = 1): if type: file = open("input.dat", "r") n = int(file.readline()) a = list(map(int, file.readline().split())) b = file.readline() file.close() else: n = int(input().strip()) a = list(map(int, input().strip().split())) b = input()....
{ "input": [ "2\n1 2\nWL\n", "3\n10 10 10\nGLW\n", "2\n10 10\nWL\n", "1\n10\nG\n" ], "output": [ "8", "80", "40", "30" ] }
532
10
You are playing a game of Jongmah. You don't need to know the rules to solve this problem. You have n tiles in your hand. Each tile has an integer between 1 and m written on it. To win the game, you will need to form some number of triples. Each triple consists of three tiles, such that the numbers written on the tile...
import sys class Reader: def __init__(self): self.in_strs = list(reversed(sys.stdin.read().split())) def read_int(self): out = self.in_strs.pop() return int(out) def main(): r = Reader() cc = r.read_int() n = r.read_int() a = [0 for i in range(n)] for i in ran...
{ "input": [ "10 6\n2 3 3 3 4 4 4 5 5 6\n", "13 5\n1 1 5 1 2 3 3 2 4 2 3 4 5\n", "12 6\n1 5 3 3 3 4 3 5 3 2 3 3\n" ], "output": [ "3\n", "4\n", "3\n" ] }
533
7
Ivan recently bought a detective book. The book is so interesting that each page of this book introduces some sort of a mystery, which will be explained later. The i-th page contains some mystery that will be explained on page a_i (a_i β‰₯ i). Ivan wants to read the whole book. Each day, he reads the first page he didn'...
input() l=list(map(int,input().split())) d=0 def read(): global d for i in range(len(l)): if max(l[0:i+1])==i+1: d+=1 read() print(d)
{ "input": [ "9\n1 3 3 6 7 6 8 8 9\n" ], "output": [ "4\n" ] }
534
10
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle. After a long time thinking, Tolik's uncle hasn't any...
from sys import stdout as so n, m = [int(x) for x in input().split()] def printa(x1, x2): so.write(str(x1) + " " + str(x2) + "\n") mn = 1 mx = n while mn < mx: for i in range(1, m+1): printa(mn, i) printa(mx, m-i+1) mn += 1 mx -= 1 if mn == mx: for i in range(1, m//2+1): p...
{ "input": [ "1 1\n", "2 3\n" ], "output": [ "1 1\n", "1 1\n2 3\n1 2\n2 2\n1 3\n2 1\n" ] }
535
10
There is a country with n citizens. The i-th of them initially has a_{i} money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have. Sometimes the governm...
""" Code of Ayush Tiwari Codeforces: servermonk Codechef: ayush572000 """ import sys input = sys.stdin.buffer.readline def solution(): n=int(input()) l=list(map(int,input().split())) m=-1 a=[-1]*n q=[] for _ in range(int(input())): t=list(map(int,input().split())) q.append(t) ...
{ "input": [ "5\n3 50 2 1 10\n3\n1 2 0\n2 8\n1 3 20\n", "4\n1 2 3 4\n3\n2 3\n1 2 2\n2 1\n" ], "output": [ "8 8 20 8 10\n", "3 2 3 4\n" ] }
536
9
There is a white sheet of paper lying on a rectangle table. The sheet is a rectangle with its sides parallel to the sides of the table. If you will take a look from above and assume that the bottom left corner of the table has coordinates (0, 0), and coordinate axes are left and bottom sides of the table, then the bott...
def R():x,y,*u=map(int,input().split());return[-x,-y]+u x=lambda a,b:[*map(min,a,b)] s=lambda x,y,u,v:max(0,x+u)*max(0,y+v) a,b,c=R(),R(),R() d=x(a,b) print('YNEOS'[s(*a)==s(*d)+s(*x(a,c))-s(*x(d,c))::2])
{ "input": [ "0 0 1000000 1000000\n0 0 499999 1000000\n500000 0 1000000 1000000\n", "3 3 7 5\n0 0 4 6\n0 0 7 4\n", "5 2 10 5\n3 1 7 6\n8 1 11 7\n", "2 2 4 4\n1 1 3 5\n3 1 5 5\n" ], "output": [ "YES\n", "YES\n", "YES\n", "NO\n" ] }
537
12
Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example: * 2 and 4 are similar (binary representations are 10 and 100); * 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101); * 3 and 2 are not similar (binary repr...
H = 15 n = int(input()) a = list(map(int, input().split())) def pc(v): v = v - ((v >> 1) & 0x55555555) v = (v & 0x33333333) + ((v >> 2) & 0x33333333) return (((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) & 0xFFFFFFFF) >> 24 mask = (1 << H) - 1 rec = {} for x in range(1 << H): r = [pc((ai & mask) ^ x) for ...
{ "input": [ "4\n3 17 6 0\n", "3\n1 2 3\n", "2\n7 2\n", "3\n43 12 12\n" ], "output": [ "5\n", "-1", "1\n", "1\n" ] }
538
7
We just discovered a new data structure in our research group: a suffix three! It's very useful for natural language processing. Given three languages and three suffixes, a suffix three can determine which language a sentence is written in. It's super simple, 100% accurate, and doesn't involve advanced machine learni...
def main(): n = int(input()) for _ in range(n): print({"o": "FILIPINO", "u": "JAPANESE", "a": "KOREAN"}[input()[-1]]) main()
{ "input": [ "8\nkamusta_po\ngenki_desu\nohayou_gozaimasu\nannyeong_hashimnida\nhajime_no_ippo\nbensamu_no_sentou_houhou_ga_okama_kenpo\nang_halaman_doon_ay_sarisari_singkamasu\nsi_roy_mustang_ay_namamasu\n" ], "output": [ "FILIPINO\nJAPANESE\nJAPANESE\nKOREAN\nFILIPINO\nFILIPINO\nJAPANESE\nJAPANESE\n" ...
539
7
You are given three strings a, b and c of the same length n. The strings consist of lowercase English letters only. The i-th letter of a is a_i, the i-th letter of b is b_i, the i-th letter of c is c_i. For every i (1 ≀ i ≀ n) you must swap (i.e. exchange) c_i with either a_i or b_i. So in total you'll perform exactly...
def solve(): a=input() b=input() c=input() n=len(a) for x in range(n): if a[x]!=c[x] and b[x]!=c[x]: return "NO" return "YES" t=int(input()) for i in range(t): print(solve())
{ "input": [ "4\naaa\nbbb\nccc\nabc\nbca\nbca\naabb\nbbaa\nbaba\nimi\nmii\niim\n" ], "output": [ "NO\nYES\nYES\nNO\n" ] }
540
7
You are given a positive integer x. Find any such 2 positive integers a and b such that GCD(a,b)+LCM(a,b)=x. As a reminder, GCD(a,b) is the greatest integer that divides both a and b. Similarly, LCM(a,b) is the smallest integer such that both a and b divide it. It's guaranteed that the solution always exists. If ther...
def main(): for i in range(int(input())): print(1, int(input())-1) main()
{ "input": [ "2\n2\n14\n" ], "output": [ "1 1\n1 13\n" ] }
541
12
We guessed a permutation p consisting of n integers. The permutation of length n is the array of length n where each element from 1 to n appears exactly once. This permutation is a secret for you. For each position r from 2 to n we chose some other index l (l < r) and gave you the segment p_l, p_{l + 1}, ..., p_r in s...
import copy t = int(input()) def fn(ivs, v, n): result = list() while len(result) < n - 1: w = None for iv in ivs: if v in iv: iv.remove(v) if len(iv) == 1: if w is None: w = iv.pop() else: return if w is None: return result....
{ "input": [ "5\n6\n3 2 5 6\n2 4 6\n3 1 3 4\n2 1 3\n4 1 2 4 6\n5\n2 2 3\n2 1 2\n2 1 4\n2 4 5\n7\n3 1 2 6\n4 1 3 5 6\n2 1 2\n3 4 5 7\n6 1 2 3 4 5 6\n3 1 3 6\n2\n2 1 2\n5\n2 2 5\n3 2 3 5\n4 2 3 4 5\n5 1 2 3 4 5\n" ], "output": [ "3 1 4 6 2 5 \n3 2 1 4 5 \n2 1 6 3 5 4 7 \n1 2 \n2 5 3 4 1 \n" ] }
542
11
Egor is a famous Russian singer, rapper, actor and blogger, and finally he decided to give a concert in the sunny Republic of Dagestan. There are n cities in the republic, some of them are connected by m directed roads without any additional conditions. In other words, road system of Dagestan represents an arbitrary d...
import sys from math import ceil, gcd from collections import deque input = sys.stdin.buffer.readline # def print(val): # sys.stdout.write(str(val) + '\n') n, m = [int(i) for i in input().split()] adj = [[] for i in range(n+1)] mark = [0] * (n+1) d = [-1] * (n+1) c = [-1] * (n+1) for i in range(m): u, v, b = [int(i...
{ "input": [ "3 4\n1 2 0\n1 3 1\n2 3 0\n2 3 1\n", "5 10\n1 2 0\n1 3 1\n1 4 0\n2 3 0\n2 3 1\n2 5 0\n3 4 0\n3 4 1\n4 2 1\n4 5 0\n", "4 8\n1 1 0\n1 3 0\n1 3 1\n3 2 0\n2 1 0\n3 4 1\n2 4 0\n2 4 1\n" ], "output": [ "2\n010\n", "-1\n01010", "3\n1100" ] }
543
9
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated....
def ab(): s = input() c=0 for i in s: if (i=='B' and c!=0): c-=1 else: c+=1 print(c) t = int(input()) for i in range(t): ab()
{ "input": [ "3\nAAA\nBABA\nAABBBABBBB\n" ], "output": [ "3\n2\n0\n" ] }
544
9
Ashish has two strings a and b, each of length n, and an integer k. The strings only contain lowercase English letters. He wants to convert string a into string b by performing some (possibly zero) operations on a. In one move, he can either * choose an index i (1 ≀ i≀ n-1) and swap a_i and a_{i+1}, or * choos...
def fun(A,B,n,k): temp = [0]*26 t = ord("a") for i in range(n): temp[ord(A[i])-t] -= 1 temp[ord(B[i])-t] += 1 cu = 0 for i in range(26): if temp[i] == 0: continue else: if abs(temp[i])%k != 0: print("NO") return if temp[i]>0: if cu<temp[i]: print("N...
{ "input": [ "4\n3 3\nabc\nbcd\n4 2\nabba\nazza\n2 1\nzz\naa\n6 2\naaabba\nddddcc\n" ], "output": [ "\nNo\nYes\nNo\nYes\n" ] }
545
9
At the school where Vasya is studying, preparations are underway for the graduation ceremony. One of the planned performances is a ball, which will be attended by pairs of boys and girls. Each class must present two couples to the ball. In Vasya's class, a boys and b girls wish to participate. But not all boys and not...
def lkp(l): r = {} for e in l: if e in r: r[e] += 1 else: r[e] = 1 return r for _ in range(int(input())): a,b,k = map(int,input().split()) la = list(map(int, input().split())) lb = list(map(int, input().split())) la_look = lkp(la) lb_look...
{ "input": [ "3\n3 4 4\n1 1 2 3\n2 3 2 4\n1 1 1\n1\n1\n2 2 4\n1 1 2 2\n1 2 1 2\n" ], "output": [ "\n4\n0\n2\n" ] }
546
7
It was the third month of remote learning, Nastya got sick of staying at dormitory, so she decided to return to her hometown. In order to make her trip more entertaining, one of Nastya's friend presented her an integer array a. Several hours after starting her journey home Nastya remembered about the present. To ente...
def path(arr): d={} for i in range(len(arr)): for j in range(i): if d.get(arr[i]+arr[j],None)!=None: ii,jj=d[arr[i]+arr[j]] if len(set([ii,jj,i,j]))==4: print("YES") print(ii+1,jj+1,i+1,j+1) return "" d[arr[i]+arr[j]]=(i,j) return "NO" a=input() lst=list(map(int,input().strip().split...
{ "input": [ "5\n1 3 1 9 20\n", "6\n2 1 5 2 7 4\n" ], "output": [ "\nNO", "\nYES\n2 3 1 6 " ] }
547
9
There are n robots driving along an OX axis. There are also two walls: one is at coordinate 0 and one is at coordinate m. The i-th robot starts at an integer coordinate x_i~(0 < x_i < m) and moves either left (towards the 0) or right with the speed of 1 unit per second. No two robots start at the same coordinate. Whe...
def solve(robots, m, ans): robots.sort();stack = [] for x, dire, i in robots: if dire=='L': if not stack:stack.append((i, -x)) else:i2, x2 = stack[-1];ans[i] = ans[i2] = (x-x2)//2;stack.pop() else:stack.append((i,x)) while len(stack) >= 2:i1, x1 = stack[-1];stack.pop();x1 = m + (m-x1);i2, x2 = stack[-1];st...
{ "input": [ "5\n7 12\n1 2 3 4 9 10 11\nR R L L R R R\n2 10\n1 6\nR R\n2 10\n1 3\nL L\n1 10\n5\nR\n7 8\n6 1 7 2 3 5 4\nR L R L L L L\n" ], "output": [ "\n1 1 1 1 2 -1 2 \n-1 -1 \n2 2 \n-1 \n-1 2 7 3 2 7 3 \n" ] }
548
7
Everyone knows that 2010 FIFA World Cup is being held in South Africa now. By the decision of BFA (Berland's Football Association) next World Cup will be held in Berland. BFA took the decision to change some World Cup regulations: * the final tournament features n teams (n is always even) * the first n / 2 teams ...
#!/usr/bin/env python3 from collections import defaultdict def score(x, y): if x > y: return 3 if x == y: return 1 return 0 n = int(input()) players = [input() for _ in range(n)] mp = defaultdict(lambda:[0, 0, 0]) for _ in range(n*(n-1)//2): part1, part2 = input().split() p1, p2 ...
{ "input": [ "4\nA\nB\nC\nD\nA-B 1:1\nA-C 2:2\nA-D 1:0\nB-C 1:0\nB-D 0:3\nC-D 0:3\n", "2\na\nA\na-A 2:1\n" ], "output": [ "A\nD\n", "a\n" ] }
549
7
Dima and his friends have been playing hide and seek at Dima's place all night. As a result, Dima's place got messy. In the morning they decided that they need to clean the place. To decide who exactly would clean the apartment, the friends want to play a counting-out game. First, all the guys stand in a circle, and t...
def main(): n = int(input().strip()) + 1 x = sum(map(int, input().strip().split())) % n return sum((x + i) % n != 1 for i in range(1, 6)) print(main())
{ "input": [ "1\n2\n", "2\n3 5\n", "1\n1\n" ], "output": [ "2\n", "3\n", "3\n" ] }
550
7
As a tradition, every year before IOI all the members of Natalia Fan Club are invited to Malek Dance Club to have a fun night together. Malek Dance Club has 2n members and coincidentally Natalia Fan Club also has 2n members. Each member of MDC is assigned a unique id i from 0 to 2n - 1. The same holds for each member o...
t = input() n, d = len(t), 1000000007 def f(k): return 0 if k == n else 2 * f(k + 1) + (pow(2, 2 * (n - k - 1), d) if t[k] == '1' else 0) print(f(0) % d)
{ "input": [ "1\n", "01\n", "11\n" ], "output": [ "1\n", "2\n", "6\n" ] }
551
7
Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain resistance value. However, all Mike has is lots of identical resistors with unit resistance R0 = 1. Elements with other resistance can be constructed from these resistors. In this problem, we will co...
def gcd(a,b): return 0 if b == 0 else a//b + gcd(b, a%b) print(gcd(*(int(x) for x in input().split())))
{ "input": [ "3 2\n", "1 1\n", "199 200\n" ], "output": [ "3\n", "1\n", "200\n" ] }
552
9
Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to have a walk, Inna decided to cook something. Dima and Seryozha have n fruits in the fridge. Each fruit has two parameters: the taste and the number of calories. Inna decided to make a fruit sala...
class Dict(dict): def __missing__(self, key): return float('-inf') n, k = map(int, input().split()) a = [0] + list(map(int, input().split())) b = [0] + list(map(int, input().split())) dp = [Dict() for _ in range(n+1)] dp[0][n*100] = 0 for i in range(1, n+1): s = a[i] - b[i]*k for j in range(n*200...
{ "input": [ "5 3\n4 4 4 4 4\n2 2 2 2 2\n", "3 2\n10 8 1\n2 7 1\n" ], "output": [ "-1\n", "18\n" ] }
553
8
Two chess pieces, a rook and a knight, stand on a standard chessboard 8 Γ— 8 in size. The positions in which they are situated are known. It is guaranteed that none of them beats the other one. Your task is to find the number of ways to place another knight on the board so that none of the three pieces on the board bea...
r = input() k = input() cont = 0 def kn(k, i, j): a, b = [ord(x)-96 for x in [k[0], i]] return( k == (i+str(j)) or (abs(a-b) == 1 and abs(int(k[1]) - j) == 2) or (abs(a-b) == 2 and abs(int(k[1]) - j) == 1) ) for i in "abcdefgh": if i == r[0]: continue for j in range(1,9):...
{ "input": [ "a1\nb2\n", "a8\nd4\n" ], "output": [ "44\n", "38\n" ] }
554
7
Not so long ago as a result of combat operations the main Berland place of interest β€” the magic clock β€” was damaged. The cannon's balls made several holes in the clock, that's why the residents are concerned about the repair. The magic clock can be represented as an infinite Cartesian plane, where the origin correspond...
def findcolor(x,p): i=0 while i*i<x*x+p*p: i+=1 l=0 if (i*i==x*x+p*p) or (x*p>=0)!=(i%2==0): l=1 print("black")if l else print("white") x,p=map(int,input().split()) findcolor(x,p)
{ "input": [ "-2 1\n", "4 3\n", "2 1\n" ], "output": [ "white\n", "black\n", "black\n" ] }
555
7
Your friend has recently learned about coprime numbers. A pair of numbers {a, b} is called coprime if the maximum number that divides both a and b is equal to one. Your friend often comes up with different statements. He has recently supposed that if the pair (a, b) is coprime and the pair (b, c) is coprime, then the...
def f(ll): l,r =ll if r-l<2 or (r-l==2 and l%2>0): return [-1] return [l,l+1,l+2] if l%2==0 else [l+1,l+2,l+3] l = list(map(int,input().split())) print(*f(l))
{ "input": [ "900000000000000009 900000000000000029\n", "2 4\n", "10 11\n" ], "output": [ "900000000000000010 900000000000000011 900000000000000012\n", "2 3 4\n", "-1\n" ] }
556
7
Amr is a young coder who likes music a lot. He always wanted to learn how to play music but he was busy coding so he got an idea. Amr has n instruments, it takes ai days to learn i-th instrument. Being busy, Amr dedicated k days to learn how to play the maximum possible number of instruments. Amr asked for your help ...
def sortl(inp): return inp[0] n,k=map(int,input().split(" ")) mas = [[x,i] for i,x in enumerate(map(int,input().split(" ")))] mas.sort(key=sortl) res=0 for i in range(n): k-=mas[i][0] if k>=0: res+=1 else: break print(res) for i in range(res): print(mas[i][1]+1,end=" ")
{ "input": [ "5 6\n4 3 1 1 2\n", "1 3\n4\n", "4 10\n4 3 1 2\n" ], "output": [ "3\n3 4 5 ", "0\n", "4\n3 4 2 1 " ] }
557
8
Andrewid the Android is a galaxy-famous detective. He is now investigating a case of frauds who make fake copies of the famous Stolp's gears, puzzles that are as famous as the Rubik's cube once was. Its most important components are a button and a line of n similar gears. Each gear has n teeth containing all numbers f...
def main(): n = int(input()) l = list(map(int, input().split())) a = l[0] for i, x in enumerate(l): l[i] = ((x + a if i & 1 else x - a) - i) % n print(("Yes", "No")[any(l)]) if __name__ == '__main__': main()
{ "input": [ "4\n0 2 3 1\n", "5\n4 2 1 4 3\n", "3\n1 0 0\n" ], "output": [ "No\n", "Yes\n", "Yes\n" ] }
558
9
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be uniq...
def right(m, n, a): b = True n2 = a // 2 - a // 6 n3 = a // 3 - a // 6 n6 = a // 6 if n2+n6<n or n3+n6<m or n2+n3+n6<n+m: b = False return b n, m = map(int, input().split()) ans = n+m while not right(m, n, ans): ans += 1 print(ans)
{ "input": [ "3 2\n", "5 0\n", "1 3\n" ], "output": [ "8\n", "10\n", "9\n" ] }
559
7
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is located at point (xi, yi). They need to arrange a plan, but there are some difficulties on their way. As you know, Doctor Manhattan consider...
def f(t): return sum(v*(v-1)/2 for v in t.values()) a,b,c={},{},{} for _ in range(int(input())): x,y=map(int,input().split()) a[x]=a.get(x,0)+1 b[y]=b.get(y,0)+1 c[(x,y)]=c.get((x,y),0)+1 print(int(f(a)+f(b)-f(c)))
{ "input": [ "3\n1 1\n7 5\n1 5\n", "6\n0 0\n0 1\n0 2\n-1 1\n0 1\n1 1\n" ], "output": [ "2", "11" ] }
560
9
Misha and Vanya have played several table tennis sets. Each set consists of several serves, each serve is won by one of the players, he receives one point and the loser receives nothing. Once one of the players scores exactly k points, the score is reset and a new set begins. Across all the sets Misha scored a points ...
string = input() n, a, b = map(int, string.split()) def check(p, q): return p % n != 0 and q < n if check(a, b) or check(b, a): print(-1) else: print(a // n + b // n)
{ "input": [ "11 2 3\n", "11 11 5\n" ], "output": [ "-1", "1" ] }
561
8
Do you remember a kind cartoon "Beauty and the Beast"? No, no, there was no firing from machine guns or radiation mutants time-travels! There was a beauty named Belle. Once she had violated the Beast's order and visited the West Wing. After that she was banished from the castle... Everybody was upset. The beautiful ...
def main(): x,y = map(int,input().split(":")) x = x%12 + 1*(y/60) print(x*30,(y*360)/60) main()
{ "input": [ "04:30\n", "08:17\n", "12:00\n" ], "output": [ "135 180\n", "248.5 102\n", "0 0\n" ] }
562
7
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence. When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time,...
a,b=[int(x) for x in input().split()] def hcf(a,b): if a==0: return b return hcf(b%a,a) lcm=(a*b)/hcf(a,b) d=lcm/a m=lcm/b if d-m>1: print('Dasha') elif m-d>1: print('Masha') else: print('Equal')
{ "input": [ "2 3\n", "3 7\n", "5 3\n" ], "output": [ "Equal\n", "Dasha\n", "Masha\n" ] }
563
9
Mishka has got n empty boxes. For every i (1 ≀ i ≀ n), i-th box is a cube with side length ai. Mishka can put a box i into another box j if the following conditions are met: * i-th box is not put into another box; * j-th box doesn't contain any other boxes; * box i is smaller than box j (ai < aj). Mishka ...
def f(A): x = 0 for i in A: if A.count(i)>x: x = A.count(i) return x n = input() A = input().split(' ') print(f(A))
{ "input": [ "3\n1 2 3\n", "4\n4 2 4 3\n" ], "output": [ "1\n", "2\n" ] }
564
11
Consider a system of n water taps all pouring water into the same container. The i-th water tap can be set to deliver any amount of water from 0 to ai ml per second (this amount may be a real number). The water delivered by i-th tap has temperature ti. If for every <image> you set i-th tap to deliver exactly xi ml of ...
import sys readline = sys.stdin.readline readlines = sys.stdin.readlines ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep='\n') def solve(): n, T = nm() a = nl() t...
{ "input": [ "2 12\n1 3\n10 15\n", "2 100\n3 10\n50 150\n", "3 9\n5 5 30\n6 6 10\n" ], "output": [ "1.6666666667\n", "6.0000000000\n", "40\n" ] }
565
10
SaMer has written the greatest test case of all time for one of his problems. For a given array of integers, the problem asks to find the minimum number of groups the array can be divided into, such that the product of any pair of integers in the same group is a perfect square. Each integer must be in exactly one gro...
from math import sqrt primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, ...
{ "input": [ "2\n5 5\n", "1\n0\n", "5\n5 -4 2 1 8\n" ], "output": [ "3 0 ", "1 ", "5 5 3 2 0 " ] }
566
10
In one very old text file there was written Great Wisdom. This Wisdom was so Great that nobody could decipher it, even Phong β€” the oldest among the inhabitants of Mainframe. But still he managed to get some information from there. For example, he managed to learn that User launches games for pleasure β€” and then terribl...
dic={} def dfs(a,res): if a<=1 and res<=a: return 1 elif a*100+res in dic: return dic[a*100+res] else: cnt=0 for i in range(a): if i<res-1 and a-i-1<res-1: continue else: cnt=cnt+dfs(i,0)*dfs(a-i-1,res-1)+dfs(i,res-1)*dfs(a-i-1,0)-dfs(i,res-1)*dfs(a-i-1,res-1) dic[a*100+res]=cnt return cnt ...
{ "input": [ "3 2\n", "3 3\n" ], "output": [ "5\n", "4\n" ] }
567
7
Vitya has just started learning Berlanese language. It is known that Berlanese uses the Latin alphabet. Vowel letters are "a", "o", "u", "i", and "e". Other letters are consonant. In Berlanese, there has to be a vowel after every consonant, but there can be any letter after any vowel. The only exception is a consonant...
def check(a): n = len(a) for i in range(n): if a[i] not in 'aeioun': if i+1>=n or a[i+1] not in 'aeiou': return "NO" return "YES" a = input() print(check(a))
{ "input": [ "codeforces\n", "ninja\n", "sumimasen\n" ], "output": [ "NO\n", "YES\n", "YES\n" ] }
568
12
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. An inversion in a permutation p is a pair of indices (i, j) such that i > j and a_i < a_j. For example, a permutation [4, 1, 3, 2] contains 4 inversions: (2, 1), (3, 1), (4, 1), (4, 3). You are given a p...
K = 998244353 def mu(a, n): if n == 0: return 1 q = mu(a, n // 2) if n % 2 == 0: return q * q % K return q * q % K * a % K MAXN = 200005 dd = [0 for i in range(MAXN)] p = [0 for i in range(MAXN)] s = [0 for i in range(MAXN)] a = [0 for i in range(MAXN)] fen = [0 for i in range(MAXN)] def add(u, v): i = u while (...
{ "input": [ "2\n1 2\n", "2\n-1 -1\n", "3\n3 -1 -1\n" ], "output": [ " 0", " 499122177", " 499122179" ] }
569
9
Two integer sequences existed initially β€” one of them was strictly increasing, and the other one β€” strictly decreasing. Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and th...
def solve(): n = int(input()) arr=list(map(int,input().split())) s1 = set(); s2 = set() for i in arr: if not i in s1: s1.add(i) elif not i in s2: s2.add(i) else: print("NO") return print("YES") print(len(s1)) print(*sort...
{ "input": [ "5\n4 3 1 5 3\n", "5\n0 1 2 3 4\n", "7\n7 2 7 3 3 1 4\n", "5\n1 1 2 1 2\n" ], "output": [ "YES\n4\n1 3 4 5\n1\n3\n", "YES\n5\n0 1 2 3 4\n0\n\n", "YES\n5\n1 2 3 4 7\n2\n7 3\n", "NO\n" ] }
570
8
Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day β€” exactly 2 problems, during the third day β€” exactly 3 problems, and so on. During the k-th day he should solve k problems. Polycarp has a list of n contests, th...
def arr_inp(): return [int(x) for x in input().split()] n, a = int(input()), arr_inp() a.sort() days = 0 for i in a: if i >= days + 1: days += 1 print(days)
{ "input": [ "4\n3 1 4 1\n", "5\n1 1 1 2 2\n", "3\n1 1 1\n" ], "output": [ "3\n", "2\n", "1\n" ] }
571
9
The Cybermen and the Daleks have long been the Doctor's main enemies. Everyone knows that both these species enjoy destroying everything they encounter. However, a little-known fact about them is that they both also love taking Turing tests! Heidi designed a series of increasingly difficult tasks for them to spend the...
n = int(input()) l = [ list(map(int,input().split())) for _ in range(4*n+1) ] x, y = map(list, zip(*l)) hx, lx, hy, ly = max(x), min(x), max(y), min(y) def find(w, i): for j in l: if j[ w ] == i: return j if x.count(hx) == 1: print( *find( 0, hx ) ) elif x.count(lx) == 1: print( *find( ...
{ "input": [ "2\n0 0\n0 1\n0 2\n0 3\n1 0\n1 2\n2 0\n2 1\n2 2\n", "2\n0 0\n0 1\n0 2\n1 0\n1 1\n1 2\n2 0\n2 1\n2 2\n" ], "output": [ "0 3\n", "1 1\n" ] }
572
10
The subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. You are given an integer n. You have to find a sequence s consisting of digits \{1, 3, 7\} such that it has exactly n subsequences equal to 1337. For example, seque...
def string_1337(n, L = [], first = True): j=int((2*n)**0.5) if j*(j+1)//2<=n: j+=1 m = j*(j-1)//2 print('1','33','7'*(n-m),'3'*(j-2),'7',sep='') n = int(input()) for _ in range(n): string_1337(int(input()),[])
{ "input": [ "2\n6\n1\n" ], "output": [ "133337\n1337\n" ] }
573
10
You are given a tree consisting of n vertices. A tree is an undirected connected acyclic graph. <image> Example of a tree. You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color. You have to paint the vertices so that any path consisting of exactly th...
#!/usr/bin/env python3 import sys from itertools import permutations from math import inf #lines = stdin.readlines() def rint(): return map(int, sys.stdin.readline().split()) def input(): return sys.stdin.readline().rstrip('\n') def oint(): return int(input()) n = oint() c = [] for i in range(3): ...
{ "input": [ "5\n3 4 2 1 2\n4 2 1 5 4\n5 3 2 1 1\n1 2\n3 2\n4 3\n5 3\n", "5\n3 4 2 1 2\n4 2 1 5 4\n5 3 2 1 1\n1 2\n3 2\n4 3\n5 4\n", "3\n3 2 3\n4 3 2\n3 1 3\n1 2\n2 3\n" ], "output": [ "-1\n", "9\n1 3 2 1 3 \n", "6\n1 3 2 \n" ] }
574
8
An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to 1. More formally, a sequence s_1, s_2, …, s_{n} is beautiful if |s_i - s_{i+1}| = 1 for all 1 ≀ i ≀ n - 1. Trans has a numbers 0, b numbers 1, c numbers 2 and d numbers 3. He wants to construct a beautiful sequenc...
def printit(s): print("YES") print(*s) exit() def solve(j,i): tt = sum(j) jj = [] for nn in j: jj.append(nn) s = [] while(1): if(i==4 or jj[i]==0): break s.append(i) jj[i]-=1 if(i>0 and jj[i-1]>0): i-=1 ...
{ "input": [ "1 2 3 4\n", "2 2 2 3\n", "2 2 2 1\n" ], "output": [ "NO\n", "NO\n", "YES\n0 1 0 1 2 3 2 \n" ] }
575
10
Today, as a friendship gift, Bakry gave Badawy n integers a_1, a_2, ..., a_n and challenged him to choose an integer X such that the value \underset{1 ≀ i ≀ n}{max} (a_i βŠ• X) is minimum possible, where βŠ• denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR). As always, Badawy is too ...
def q(s,b): if(not s)or b<0:return 0 n,f=[],[] for i in s: if i&(1<<b):n+=i, else:f+=i, if not n:return q(f,b-1) if not f:return q(n,b-1) return min(q(n,b-1),q(f,b-1))+2**b input() print(q([*map(int,input().split())],32))
{ "input": [ "2\n1 5\n", "3\n1 2 3\n" ], "output": [ "4", "2" ] }
576
7
Dreamoon likes coloring cells very much. There is a row of n cells. Initially, all cells are empty (don't contain any color). Cells are numbered from 1 to n. You are given an integer m and m integers l_1, l_2, …, l_m (1 ≀ l_i ≀ n) Dreamoon will perform m operations. In i-th operation, Dreamoon will choose a number ...
def res(): print(-1) exit() n,m = map(int,input().split()) l = list(map(int,input().split())) idx = [] if sum(l)<n:res() else: for i in range(m): if i+l[i]>n:res() for i in range(m-1,-1,-1): if n>min(i,m-1-i): curr = min(n-i,l[i]) idx.append(n-curr+1) ...
{ "input": [ "10 1\n1\n", "5 3\n3 2 2\n" ], "output": [ "-1", "1 2 4 " ] }
577
7
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow: * \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x. * lcm(s) is the minimum positive integer x, that divisible on all integ...
from math import gcd def lcm(a,b): return a*b//gcd(a,b) input() a=list(map(int,input().split())) t=gcd(a[0],a[1]) q=lcm(a[0],a[1]) for i in range(2,len(a)): q=gcd(q,lcm(a[i],t)) t=gcd(t,a[i]) print(q)
{ "input": [ "2\n1 1\n", "10\n540 648 810 648 720 540 594 864 972 648\n", "4\n10 24 40 80\n" ], "output": [ "1\n", "54\n", "40\n" ] }
578
11
Lee bought some food for dinner time, but Lee's friends eat dinner in a deadly way. Lee is so scared, he doesn't want to die, at least not before seeing Online IOI 2020... There are n different types of food and m Lee's best friends. Lee has w_i plates of the i-th type of food and each friend has two different favorit...
import sys from collections import deque def input(): return sys.stdin.readline().rstrip() def input_split(): return [int(i) for i in input().split()] n,m = input_split() w = input_split() num = [0 for i in range(n)] p = [[] for i in range(n)] done = [False for i in range(n)] done_people = [False for i in range(m)...
{ "input": [ "3 2\n1 1 0\n1 2\n1 3\n", "4 4\n1 2 0 1\n1 3\n1 2\n2 3\n2 4\n", "5 5\n1 1 1 2 1\n3 4\n1 2\n2 3\n4 5\n4 5\n", "4 10\n2 4 1 4\n3 2\n4 2\n4 1\n3 1\n4 1\n1 3\n3 2\n2 1\n3 1\n2 4\n", "3 3\n1 2 1\n1 2\n2 3\n1 3\n" ], "output": [ "ALIVE\n2 1 \n", "ALIVE\n1 3 2 4\n", "ALIVE\n5...
579
10
A binary matrix is called good if every even length square sub-matrix has an odd number of ones. Given a binary matrix a consisting of n rows and m columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all. All the terms above have their...
from collections import * from sys import exit, stdin, stderr def rl(): return [int(w) for w in stdin.readline().split()] n, m = rl() if n > 3: print(-1) exit(0) if n < 2: print(0) exit(0) a = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)] if n == 2: a.append(a[0]) r = n * m fo...
{ "input": [ "3 3\n101\n001\n110\n", "7 15\n000100001010010\n100111010110001\n101101111100100\n010000111111010\n111010010100001\n000011001111101\n111111011010011\n" ], "output": [ "2\n", "-1\n" ] }
580
7
You are given an array a consisting of n integers numbered from 1 to n. Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there i...
maxn=3e5+5 def solve(arr,n): f=[0]*(n+1) last=[0]*(n+1) ans=[-1]*(n+1) for i in range(1,n+1): x=arr[i] f[x]=max(f[x],i-last[x]) last[x]=i # print(last) for x in range(1,n+1): f[x]=max(f[x],n-last[x]+1) i=f[x] while i<=n and ans[i]==-1: ans[i]=x i+=1 print(*ans[1:]) for _ in range(...
{ "input": [ "3\n5\n1 2 3 4 5\n5\n4 4 4 4 2\n6\n1 3 1 5 3 1\n" ], "output": [ "-1 -1 3 2 1\n-1 4 4 4 2\n-1 -1 1 1 1 1\n" ] }
581
7
Homer has two friends Alice and Bob. Both of them are string fans. One day, Alice and Bob decide to play a game on a string s = s_1 s_2 ... s_n of length n consisting of lowercase English letters. They move in turns alternatively and Alice makes the first move. In a move, a player must choose an index i (1 ≀ i ≀ n) ...
def doit(): s=input() for i in range(len(s)): if ((i&1)==0): print('b' if s[i]=='a' else 'a', end='') else: print('y' if s[i]=='z' else 'z', end='') print('') T=int(input()) for _ in range(T): doit()
{ "input": [ "3\na\nbbbb\naz\n" ], "output": [ "\nb\nazaz\nby\n" ] }
582
7
Today we will be playing a red and white colouring game (no, this is not the Russian Civil War; these are just the colours of the Canadian flag). You are given an n Γ— m grid of "R", "W", and "." characters. "R" is red, "W" is white and "." is blank. The neighbours of a cell are those that share an edge with it (those ...
t=int(input()) def solve(): n,m=map(int,input().split()) d=-1; x=True for i in range(n): s=input() for j in range(m): cnt=i+j if s[j]!='.': if d==-1: d=cnt+(s[j]!='R') elif s[j]=='R': if (cnt-d)%2!=0: x=False else: if (cnt-d)%2!=1: x=False if x: print("Yes") s="" f...
{ "input": [ "3\n4 6\n.R....\n......\n......\n.W....\n4 4\n.R.W\n....\n....\n....\n5 1\nR\nW\nR\nW\nR\n" ], "output": [ "\nYES\nWRWRWR\nRWRWRW\nWRWRWR\nRWRWRW\nNO\nYES\nR\nW\nR\nW\nR\n" ] }
583
9
Polycarpus analyzes a string called abracadabra. This string is constructed using the following algorithm: * On the first step the string consists of a single character "a". * On the k-th step Polycarpus concatenates two copies of the string obtained on the (k - 1)-th step, while inserting the k-th character of ...
aaa =0 def f(l1,r1,l2,r2,top): global aaa if (l1>r1 or l2> r2): return 0 if (top-1<=aaa) or (r1-l1+1<=aaa) or (r2-l2+1<=aaa): return 0 if top==2: return 1 if (l1>top): l1 -=top r1 -= top if (l2>top): l2-=top r2-=top #print(l1,r1,l2,r2,top) if (l1==l2 and r1==r2): return...
{ "input": [ "3 6 1 4\n", "1 1 4 4\n" ], "output": [ "2\n", "0\n" ] }
584
11
Let's imagine that you're playing the following simple computer game. The screen displays n lined-up cubes. Each cube is painted one of m colors. You are allowed to delete not more than k cubes (that do not necessarily go one after another). After that, the remaining cubes join together (so that the gaps are closed) an...
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) from collections import de...
{ "input": [ "3 1 2\n1 1 1\n", "10 2 2\n1 2 1 2 1 1 2 1 1 2\n", "10 3 2\n1 2 1 1 3 2 1 1 2 2\n" ], "output": [ "3", "5", "4" ] }
585
7
The Little Elephant very much loves sums on intervals. This time he has a pair of integers l and r (l ≀ r). The Little Elephant has to find the number of such integers x (l ≀ x ≀ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be inclu...
def f(x): s = str(x); return x//10 + min(x,9) - (s[0] > s[-1]) n,m = map(int,input().split()) print(f(m)-f(n-1))
{ "input": [ "47 1024\n", "2 47\n" ], "output": [ "98\n", "12\n" ] }
586
11
Berland has n cities, some of them are connected by bidirectional roads. For each road we know whether it is asphalted or not. The King of Berland Valera II wants to asphalt all roads of Berland, for that he gathered a group of workers. Every day Valera chooses exactly one city and orders the crew to asphalt all roads...
s=list(map(int,input().split())) n=s[0] m=s[1] roads=[] for i in range(0,n+1): roads.append([]) for i in range(0,m): s=list(map(int,input().split())) roads[s[0]].append([s[1],s[2]]) roads[s[1]].append([s[0],s[2]]) col=[-1]*(n+1) ##def dfs(v,c): ## success=True ## col[v]=c ## for e in roads[...
{ "input": [ "3 3\n1 2 0\n2 3 0\n3 1 0\n", "4 4\n1 2 1\n2 4 0\n4 3 1\n3 2 0\n" ], "output": [ "Impossible\n", "2\n1 2\n" ] }
587
9
Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d. Note that the order of the points inside the group of three chosen point...
def main(): n, d = map(int, input().split()) x = list(map(int, input().split())) ans = j = 0 for i in range(2, n): while x[i] - x[j] > d: j += 1 span = i - j ans += span * (span - 1) // 2 print(ans) if __name__ == "__main__": main()
{ "input": [ "4 3\n1 2 3 4\n", "4 2\n-3 -2 -1 0\n", "5 19\n1 10 20 30 50\n" ], "output": [ "4\n", "2\n", "1\n" ] }
588
7
The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a pla...
a = [] b = {} def solve(): n = int(input()) for i in range(n): x, y = input().split() y = int(y) b[x] = b.get(x, 0) + y a.append([x, b[x]]) m = max(b.values()) for i, j in a: if j >= m and b[i] == m: print(i) break #if __name__ == "__...
{ "input": [ "3\nandrew 3\nandrew 2\nmike 5\n", "3\nmike 3\nandrew 5\nmike 2\n" ], "output": [ "andrew\n", "andrew\n" ] }
589
7
You are given n rectangles. The corners of rectangles have integer coordinates and their edges are parallel to the Ox and Oy axes. The rectangles may touch each other, but they do not overlap (that is, there are no points that belong to the interior of more than one rectangle). Your task is to determine if the rectan...
import sys fin = sys.stdin # fout = sys.stdout n = int(fin.readline()) p = [] for i in range(n): a, b, c, d = map(int, fin.readline().split()) p += [((a, b), (c, d))] def width(rect): return rect[1][0] - rect[0][0] def height(rect): return rect[1][1] - rect[0][1] def square(rect): return...
{ "input": [ "5\n0 0 2 3\n0 3 3 5\n2 0 5 2\n3 2 5 5\n2 2 3 3\n", "4\n0 0 2 3\n0 3 3 5\n2 0 5 2\n3 2 5 5\n" ], "output": [ "YES\n", "NO\n" ] }
590
9
Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (...
def f(x): cb = max(0, (tb*x - nb)*pb) cs = max(0, (ts*x - ns)*ps) cc = max(0, (tc*x - nc)*pc) return cc+cs+cb <= rs s = list(input()) tb, ts, tc = s.count('B'), s.count('S'), s.count('C') nb, ns, nc = map(int, input().split()) pb, ps, pc = map(int, input().split()) rs = int(input()) l, r = 0, 10**14 wh...
{ "input": [ "BBC\n1 10 1\n1 10 1\n21\n", "BSC\n1 1 1\n1 1 3\n1000000000000\n", "BBBSSC\n6 4 1\n1 2 3\n4\n" ], "output": [ "7\n", "200000000001\n", "2\n" ] }
591
7
A TV show called "Guess a number!" is gathering popularity. The whole Berland, the old and the young, are watching the show. The rules are simple. The host thinks of an integer y and the participants guess it by asking questions to the host. There are four types of acceptable questions: * Is it true that y is stric...
def main(): lo, hi = -2000000000, 2000000001 for _ in range(int(input())): s, x, yn = input().split() if yn == "N": s = {"<": ">=", ">": "<=", "<=": ">", ">=": "<"}[s] x = int(x) + 1 if s in ("<=", ">") else int(x) if s[0] == "<": if hi > x: ...
{ "input": [ "4\n&gt;= 1 Y\n&lt; 3 N\n&lt;= -3 N\n&gt; 55 N\n", "2\n&gt; 100 Y\n&lt; -100 Y\n" ], "output": [ "-2000000000\n", "-2000000000\n" ] }
592
10
Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right. Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy: * Each piece should contain at least l numbers. * The difference between the maximal and the minimal number on the piece...
def split(a,n,s,l): pieces = [] i = 1 tmpmin = a[0] tmpmax = a[0] tmppc = [a[0]] while i<n: if abs(a[i]-tmpmin)<=s and abs(a[i]-tmpmax)<=s: tmppc.append(a[i]) if a[i]<tmpmin: tmpmin=a[i] elif a[i]>tmpmax: tmpmax = a[i] else: piece...
{ "input": [ "7 2 2\n1 3 1 2 4 1 2\n", "7 2 2\n1 100 1 100 1 100 1\n" ], "output": [ "3", "-1" ] }
593
9
Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). The i-th fox is ai years old. They will have dinner around some round tables. You want to distribute foxes such that: 1. Each fox is sitting at some table. 2. Each table has at least 3 foxes sitting around it. 3...
#E def main(): sieve = [False, True] * 10001 for i in range(3, 140, 2): if sieve[i]: j, k = i * 2, i * i le = (20001 - k) // j + 1 sieve[k::j] = [False] * le n = int(input()) aa = list(map(int, input().split())) pp = [-1] * n def dget(v): if ds...
{ "input": [ "5\n2 2 2 2 2\n", "4\n3 4 8 9\n", "12\n2 3 4 5 6 7 8 9 10 11 12 13\n", "24\n2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25\n" ], "output": [ "Impossible\n", "1\n4 1 2 4 3\n", "1\n12 1 2 3 6 5 12 9 8 7 10 11 4\n", "3\n8 1 2 3 24 5 6 23 4\n10 7 8 9 12 15...
594
11
Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we'll just say that the game takes place on an h Γ— w field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white and only some of them are black. Currently Gerald is finishing a game of gia...
#!/usr/bin/env python # 560E_chess.py - Codeforces.com 560E Chess program # # Copyright (C) 2015 Sergey """ Input The first line of the input contains three integers: h,w,n the sides of the board and the number of black cells Next n lines contain the description of black cells. The ith of these lines contains number...
{ "input": [ "100 100 3\n15 16\n16 15\n99 88\n", "3 4 2\n2 2\n2 3\n" ], "output": [ "545732279\n", "2\n" ] }
595
7
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimi...
def inc(a,w): n=len(a) i=w while(i<n and a[i]==1): a[i]=0 i+=1 if(i<n): a[i]=1 else: a.append(1) n=int(input()) x=input().split(' ') w=[int(y) for y in x] a=[0]*1000024 for t in w: inc(a,t) print(sum(a))
{ "input": [ "5\n1 1 2 3 3\n", "4\n0 1 2 3\n" ], "output": [ "2\n", "4\n" ] }
596
9
In the school computer room there are n servers which are responsible for processing several computing tasks. You know the number of scheduled tasks for each server: there are mi tasks assigned to the i-th server. In order to balance the load for each server, you want to reassign some tasks to make the difference betw...
def inpList(): return list(map(int,input().split())) def inp(): return int(input()) n=inp() a=inpList() x=sum(a) p=x//n t=x%n a.sort() suma=0 for i in range(n-t): suma+=abs(p-a[i]) for i in range(n-t,n): suma+=abs(a[i]-p-1) print(suma//2)
{ "input": [ "2\n1 6\n", "7\n10 11 10 11 10 11 11\n", "5\n1 2 3 4 5\n" ], "output": [ "2\n", "0\n", "3\n" ] }
597
8
The city administration of IT City decided to fix up a symbol of scientific and technical progress in the city's main square, namely an indicator board that shows the effect of Moore's law in real time. Moore's law is the observation that the number of transistors in a dense integrated circuit doubles approximately ev...
def f(l): n,t = l #1e3-1e4; 2e9; return n*(1.000000011**t) l = list(map(int,input().split())) print(f(l))
{ "input": [ "1000 1000000\n" ], "output": [ "1011.060722405749039" ] }
598
12
Input The only line of the input is a string of 7 characters. The first character is letter A, followed by 6 digits. The input is guaranteed to be valid (for certain definition of "valid"). Output Output a single integer. Examples Input A221033 Output 21 Input A223635 Output 22 Input A232726 Outpu...
def f(n): if n == 'A': return 1 if n == '1': return 10 return int(n) print(sum(map(f, input())))
{ "input": [ "A232726\n", "A223635\n", "A221033\n" ], "output": [ "23\n", "22\n", "21\n" ] }
599
7
A little bear Limak plays a game. He has five cards. There is one number written on each card. Each number is a positive integer. Limak can discard (throw out) some cards. His goal is to minimize the sum of numbers written on remaining (not discarded) cards. He is allowed to at most once discard two or three cards wi...
import sys from collections import Counter def main(): s = list(map(int, sys.stdin.read().strip().split())) return sum(s) - max((i*min(j, 3) for i,j in Counter(s).items() if j > 1), default=0) print(main())
{ "input": [ "7 9 3 1 8\n", "7 3 7 3 20\n", "10 10 10 10 10\n" ], "output": [ "28\n", "26\n", "20\n" ] }