index int64 0 5.16k | difficulty int64 7 12 | question stringlengths 126 7.12k | solution stringlengths 30 18.6k | test_cases dict |
|---|---|---|---|---|
0 | 11 | There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* ... | from collections import deque
def solve(adj, m, k, uv):
n = len(adj)
nn = [len(a) for a in adj]
q = deque()
for i in range(n):
if nn[i] < k:
q.append(i)
while q:
v = q.popleft()
for u in adj[v]:
nn[u] -= 1
if nn[u] == k-1:
... | {
"input": [
"4 4 2\n2 3\n1 2\n1 3\n1 4\n",
"5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n",
"5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n"
],
"output": [
"0\n0\n3\n3\n",
"0\n0\n0\n3\n3\n4\n4\n5\n",
"0\n0\n0\n0\n3\n4\n4\n"
]
} |
1 | 7 | Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone n... | n=int(input())
s=input()
def ct():
c=0
for d in s:
if d=='8':
c+=1
return c
c=ct()
m=n//11
print(min(m,c))
| {
"input": [
"22\n0011223344556677889988\n",
"11\n00000000008\n",
"11\n31415926535\n"
],
"output": [
"2\n",
"1\n",
"0\n"
]
} |
2 | 7 | You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l ≤ x ≤ r.
Input
The first l... | q=int(input())
def f(l,r,d):
if d<l or d>r:return d
return d*(int(r/d)+1)
for k in range(q):
l,r,d=map(int,input().split())
print(f(l,r,d)) | {
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
} |
3 | 10 | Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on al... | def ii():
return int(input())
def ss():
return [x for x in input()]
def si():
return [int(x) for x in input().split()]
def mi():
return map(int, input().split())
a = ii()
s = [0 for i in range(a)]
for i in range(a - 1):
c, d = [int(x) - 1 for x in input().split()]
s[c] += 1
s[d] += 1
if 2 in s:
print("... | {
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
} |
4 | 10 | An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
... | NN = 18
BIT=[0]*(2**NN+1)
def addbit(i, x):
while i <= 2**NN:
BIT[i] += x
i += i & (-i)
def getsum(i):
ret = 0
while i != 0:
ret += BIT[i]
i -= i&(-i)
return ret
def searchbit(x):
l, sl = 0, 0
d = 2**(NN-1)
while d:
m = l + d
sm = sl + BIT[m... | {
"input": [
"3\n0 0 0\n",
"5\n0 1 1 1 10\n",
"2\n0 1\n"
],
"output": [
"3 2 1 ",
"1 4 3 2 5 ",
"1 2 "
]
} |
5 | 10 | This is the easier version of the problem. In this version 1 ≤ n, m ≤ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily... | def main():
n = int(input())
a = list(enumerate(map(int, (input().split()))))
a.sort(key = lambda item: (item[1], -item[0]))
#print(a)
m = int(input())
for i in range(m):
k, pos = map(int, input().split())
s = a[-k:]
s = sorted(s)
print(s[pos - 1][1])
main() | {
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
} |
6 | 11 | You are given a permutation p_1, p_2, …, p_n.
In one move you can swap two adjacent values.
You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k.
... | n = int(input())
a = [0] + list(map(int, input().split()))
pos, pb, ps = [[0] * (n + 1) for x in range(3)]
def add(bit, i, val):
while i <= n:
bit[i] += val
i += i & -i
def sum(bit, i):
res = 0
while i > 0:
res += bit[i]
i -= i & -i
return res
def find(bit, sum):
... | {
"input": [
"3\n1 2 3\n",
"5\n5 4 3 2 1\n"
],
"output": [
"0 0 0\n",
"0 1 3 6 10\n"
]
} |
7 | 11 | 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... | {
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"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",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1... |
8 | 12 | There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ... | # BIT
def add(bit, x, v):
while x < len(bit):
bit[x] += v
x += x & (-x)
def query(bit, x):
ans = 0
while x > 0:
ans += bit[x]
x -= x & (-x)
return ans
def relabel(arr):
srt = sorted(set(arr))
mp = {v:k for k, v in enumerate(srt, 1)}
arr = [mp[a] for a in arr]... | {
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
} |
9 | 10 | You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of... | def find(a):
global k, tot
if a > n * (n - 1):
return 1
while a > tot + (n - k) * 2:
tot += (n - k) * 2
k += 1
if a & 1:
return k
return (a - tot) // 2 + k
for _ in range(int(input())):
n, l, r = map(int, input().split())
global k, tot
k = 1
tot = 0
... | {
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
} |
10 | 12 | Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other.
Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions c... | def read_int():
return int(input())
def read_ints():
return map(int, input().split(' '))
t = read_int()
for case_num in range(t):
n, k = read_ints()
p = []
for i in range(n):
ai, bi = read_ints()
p.append((bi, ai, i + 1))
p.sort()
dp = [[0 for j in range(k + 1)] for i in ... | {
"input": [
"3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n"
],
"output": [
"8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n"
]
} |
11 | 11 | Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will ... | def read_ints():
line = input()
return [int(e) for e in line.strip().split(' ')]
n, k = read_ints()
a = []
b = []
t = []
for _ in range(n):
it, ia, ib = read_ints()
if ia == 1 and ib == 1:
t.append(it)
elif ia == 1:
a.append(it)
elif ib == 1:
b.append(it)
a.sort()
b.sor... | {
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
} |
12 | 7 | You are given an array a_1, a_2, ... , a_n, which is sorted in non-decreasing order (a_i ≤ a_{i + 1}).
Find three indices i, j, k such that 1 ≤ i < j < k ≤ n and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to a_i, a_j and a_k (for example it is possible to... | def main():
for _ in range(int(input())):
s = int(input())
l = [int(i) for i in input().split()]
if l[0] + l[1] > l[-1]:
print(-1)
else:
print(1,2, s)
main()
# | {
"input": [
"3\n7\n4 6 11 11 15 18 20\n4\n10 10 10 11\n3\n1 1 1000000000\n"
],
"output": [
"1 2 7\n-1\n1 2 3\n"
]
} |
13 | 8 | Pink Floyd are pulling a prank on Roger Waters. They know he doesn't like [walls](https://www.youtube.com/watch?v=YR5ApYxkU-U), he wants to be able to walk freely, so they are blocking him from exiting his room which can be seen as a grid.
Roger Waters has a square grid of size n× n and he wants to traverse his grid f... | T=int(input())
for Tid in range(T):
n=int(input())
a=[]
for i in range(n):
a.append(input())
a0,a1,a2=[],[],[]
def add(x,y,c):
if a[x][y]==c:
a0.append([x+1,y+1])
else:
a1.append([x+1,y+1])
add(0,1,'0')
add(1,0,'0')
add(n-1,n-2,'1')
ad... | {
"input": [
"3\n4\nS010\n0001\n1000\n111F\n3\nS10\n101\n01F\n5\nS0101\n00000\n01111\n11111\n0001F\n"
],
"output": [
"1\n3 4\n2\n1 2\n2 1\n0\n"
]
} |
14 | 7 | Vasilisa the Wise from the Kingdom of Far Far Away got a magic box with a secret as a present from her friend Hellawisa the Wise from the Kingdom of A Little Closer. However, Vasilisa the Wise does not know what the box's secret is, since she cannot open it again. She hopes that you will help her one more time with tha... | def arr_inp():
return [int(x) for x in input().split()]
r, c, d = [arr_inp() for i in range(3)]
C=((c[0]-d[0]+r[1])/2)
if(C!=int(C)):
exit(print(-1))
C=int(C)
D=r[1]-C
A=d[0]-D
B=r[0]-A
arr=[A,B,C,D]
if(min(arr)<1 or max(arr)>9 or A==B or A==C or A==D or B==C or B==D or C==D ):
exit(print(-1))
print(A,B)
... | {
"input": [
"1 2\n3 4\n5 6\n",
"11 10\n13 8\n5 16\n",
"3 7\n4 6\n5 5\n",
"10 10\n10 10\n10 10\n"
],
"output": [
"-1\n",
"4 7\n9 1\n",
"1 2\n3 4\n",
"-1\n"
]
} |
15 | 12 | You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare.
In his bad dream Euclid has a set S of n m-dimensional vectors over the Z_2 field and can perform vector addition on them. In other words h... | import sys
input = sys.stdin.buffer.readline
def _find(s, u):
p = []
while s[u] != u:
p.append(u)
u = s[u]
for v in p: s[v] = u
return u
def _union(s, u, v):
su, sv = _find(s, u), _find(s, v)
if su != sv: s[su] = sv
n, m = map(int, input().split())
s, res = list(range(m+1)), [... | {
"input": [
"3 2\n1 1\n1 2\n2 2 1\n",
"3 5\n2 1 2\n1 3\n1 4\n",
"2 3\n2 1 3\n2 1 2\n"
],
"output": [
"\n4 2\n1 2 \n",
"\n8 3\n1 2 3 \n",
"\n4 2\n1 2 \n"
]
} |
16 | 9 | You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since... | import sys
def inp():
return sys.stdin.readline().rstrip()
p=1_000_000_007
t = int(inp())
rec = [1]*200_010
for i in range(10):
rec[i] = 1
for i in range(10,200_010):
rec[i] = (rec[i-9] + rec[i-10])%p
for z in range(t):
n,m = inp().split()
m = int(m)
res = 0
for c in n:
res = (res+rec[m+int(c)])%p
pr... | {
"input": [
"5\n1912 1\n5 6\n999 1\n88 2\n12 100\n"
],
"output": [
"\n5\n2\n6\n4\n2115\n"
]
} |
17 | 9 | This is the easy version of the problem. The only difference is that in this version q = 1. You can make hacks only if both versions of the problem are solved.
There is a process that takes place on arrays a and b of length n and length n-1 respectively.
The process is an infinite sequence of operations. Each operat... | def putin():
return map(int, input().split())
def sol():
n = int(input())
C = list(putin())
B = list(putin())
q = int(input())
x = int(input())
min_arr = [x]
min_part_sums = [x]
part_sums = [C[0]]
for i in range(1, n):
part_sums.append(part_sums[-1] + C[i])
for elem... | {
"input": [
"3\n2 3 4\n2 1\n1\n-1\n"
],
"output": [
"56\n"
]
} |
18 | 7 | Some country is populated by wizards. They want to organize a demonstration.
There are n people living in the city, x of them are the wizards who will surely go to the demonstration. Other city people (n - x people) do not support the wizards and aren't going to go to the demonstration. We know that the city administr... | def f(l):
n,x,y = l
return max((n*y+99)//100-x,0)
l = list(map(int,input().split()))
print(f(l))
| {
"input": [
"1000 352 146\n",
"10 1 14\n",
"20 10 50\n"
],
"output": [
"1108\n",
"1\n",
"0\n"
]
} |
19 | 8 | You are given an equation:
Ax2 + Bx + C = 0.
Your task is to find the number of distinct roots of the equation and print all of them in ascending order.
Input
The first line contains three integer numbers A, B and C ( - 105 ≤ A, B, C ≤ 105). Any coefficient may be equal to 0.
Output
In case of infinite root cou... | from math import *
def kv():
q,w,e=map(int,input().split())
if (q==0) & (w==0) & (e==0):
print(-1)
elif (q==0) & (w==0):
print(0)
elif q==0:
print(1)
print("%.6f" % ((-e)/w))
else:
d=w*w-4*q*e
if d<0:
print('0')
elif d==0:
print('1')
prin... | {
"input": [
"1 -5 6\n"
],
"output": [
"2\n2.000000\n3.000000\n"
]
} |
20 | 8 | A recently found Ancient Prophesy is believed to contain the exact Apocalypse date. The prophesy is a string that only consists of digits and characters "-".
We'll say that some date is mentioned in the Prophesy if there is a substring in the Prophesy that is the date's record in the format "dd-mm-yyyy". We'll say tha... | from re import compile
from collections import defaultdict
from time import strptime
def validDate(date):
try:
strptime(date, "%d-%m-%Y")
return True
except:
return False
myFormat = compile(r'(?=([0-2]\d|3[0-1])-(0\d|1[0-2])-(201[3-5]))' )
Dict = defaultdict(int)
for d in myFormat.find... | {
"input": [
"777-444---21-12-2013-12-2013-12-2013---444-777\n"
],
"output": [
"13-12-2013\n"
]
} |
21 | 8 | There are n cows playing poker at a table. For the current betting phase, each player's status is either "ALLIN", "IN", or "FOLDED", and does not change throughout the phase. To increase the suspense, a player whose current status is not "FOLDED" may show his/her hand to the table. However, so as not to affect any bett... | from collections import Counter
def go():
n = int(input())
d = Counter()
for c in input():
d[c] += 1
if d['I'] == 0: return d['A']
elif d['I'] == 1: return 1
else: return 0
print(go())
| {
"input": [
"3\nAFI\n",
"6\nAFFAAA\n"
],
"output": [
"1",
"4"
]
} |
22 | 7 | Vasily the Programmer loves romance, so this year he decided to illuminate his room with candles.
Vasily has a candles.When Vasily lights up a new candle, it first burns for an hour and then it goes out. Vasily is smart, so he can make b went out candles into a new candle. As a result, this new candle can be used like... | def f(a,b):
return (a-1)//(b-1)+a
if __name__ == '__main__':
a,b=map(int,input().split())
print(f(a,b))
| {
"input": [
"4 2\n",
"6 3\n"
],
"output": [
"7\n",
"8\n"
]
} |
23 | 8 | To celebrate the opening of the Winter Computer School the organizers decided to buy in n liters of cola. However, an unexpected difficulty occurred in the shop: it turned out that cola is sold in bottles 0.5, 1 and 2 liters in volume. At that, there are exactly a bottles 0.5 in volume, b one-liter bottles and c of two... | def nik(rudy,pig,y,z):
temp = 0
for i in range(z+1):
for j in range(y+1):
t = rudy - i*2 -j
if t>=0 and pig*0.5 >= t:
temp+=1
print(temp)
rudy, pig, y, z = list(map(int,input().split()))
nik(rudy,pig,y,z)
| {
"input": [
"10 5 5 5\n",
"3 0 0 2\n"
],
"output": [
"9\n",
"0\n"
]
} |
24 | 8 | A and B are preparing themselves for programming contests.
B loves to debug his code. But before he runs the solution and starts debugging, he has to first compile the code.
Initially, the compiler displayed n compilation errors, each of them is represented as a positive integer. After some effort, B managed to fix s... | input()
def d():
return sum(map(int, input().split()))
r = d(), d(), d()
print(r[0]-r[1])
print(r[1]-r[2])
| {
"input": [
"6\n1 4 3 3 5 7\n3 7 5 4 3\n4 3 7 5\n",
"5\n1 5 8 123 7\n123 7 5 1\n5 1 7\n"
],
"output": [
"1\n3\n",
"8\n123\n"
]
} |
25 | 9 | Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below.
There are n trees located along the road at points with coordinates x1, x2, ..., xn. Each ... | n=int(input())
l=[]
for _ in range(n):
l.append(list(map(int, input().split())))
def wood(l):
n=len(l)
a=float('-inf')
s=0
for i in range(n-1):
j=l[i]
if j[0]-j[1]>a:
s+=1
a=j[0]
elif j[0]+j[1]<l[i+1][0]:
s+=1
a=j[0]+j[1]
else:
a=j[0]
s+=1
return s
print(wood(l))
| {
"input": [
"5\n1 2\n2 1\n5 10\n10 9\n20 1\n",
"5\n1 2\n2 1\n5 10\n10 9\n19 1\n"
],
"output": [
"4\n",
"3\n"
]
} |
26 | 9 | Every day Ruslan tried to count sheep to fall asleep, but this didn't help. Now he has found a more interesting thing to do. First, he thinks of some set of circles on a plane, and then tries to choose a beautiful set of points, such that there is at least one point from the set inside or on the border of each of the i... | def ex(values):
e = None
for i, v in enumerate(values):
e_ = f'({v//2}*((1-abs((t-{i})))+abs((1-abs((t-{i}))))))'
if e is None:
e = e_
else:
e = f'({e}+{e_})'
return e
def solve(circles):
xs = [c[0] for c in circles]
ys = [c[1] for c in circles]
... | {
"input": [
"3\n0 10 4\n10 0 4\n20 10 4\n"
],
"output": [
"(((0*((1-abs((t-0)))+abs((abs((t-0))-1))))+(5*((1-abs((t-1)))+abs((abs((t-1))-1)))))+(10*((1-abs((t-2)))+abs((abs((t-2))-1)))))\n(((5*((1-abs((t-0)))+abs((abs((t-0))-1))))+(0*((1-abs((t-1)))+abs((abs((t-1))-1)))))+(5*((1-abs((t-2)))+abs((abs((t-2... |
27 | 7 | Vasya wants to turn on Christmas lights consisting of m bulbs. Initially, all bulbs are turned off. There are n buttons, each of them is connected to some set of bulbs. Vasya can press any of these buttons. When the button is pressed, it turns on all the bulbs it's connected to. Can Vasya light up all the bulbs?
If Va... | def read(): return list(map(int, input().split()))
n, m = read()
s = []
for _ in range(n):
s.extend(read()[1:])
print("YES" if len(set(s)) == m else "NO") | {
"input": [
"3 4\n2 1 4\n3 1 3 1\n1 2\n",
"3 3\n1 1\n1 2\n1 1\n"
],
"output": [
"YES\n",
"NO\n"
]
} |
28 | 9 | A factory produces thimbles in bulk. Typically, it can produce up to a thimbles a day. However, some of the machinery is defective, so it can currently only produce b thimbles each day. The factory intends to choose a k-day period to do maintenance and construction; it cannot produce any thimbles during this time, but ... | 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,x,v):
while x<len(a):
a[x] += v
x |= x+1
def get(a,x):
r = 0
while x>=0:
r += a[x]
x = (x&(x+1))-1
return r
n, k, a, b, q = mints()
h1 ... | {
"input": [
"5 4 10 1 6\n1 1 5\n1 5 5\n1 3 2\n1 5 2\n2 1\n2 2\n",
"5 2 2 1 8\n1 1 2\n1 5 3\n1 2 1\n2 2\n1 4 2\n1 3 2\n2 1\n2 3\n"
],
"output": [
"7\n1\n",
"3\n6\n4\n"
]
} |
29 | 7 | You are given a rebus of form ? + ? - ? + ? = n, consisting of only question marks, separated by arithmetic operation '+' and '-', equality and positive integer n. The goal is to replace each question mark with some positive integer from 1 to n, such that equality holds.
Input
The only line of the input contains a re... | def set_numbers(S, k):
ans = []
for i in range(k):
ans.append(S // k)
for i in range(S - k*(S // k)):
ans[i] += 1
return ans
s = input().split()
i = s.count('+') + 1
j = s.count('-')
n = int(s[-1])
if i-j*n <= n <= i*n-j:
print('Possible')
S1 = max(i, j + n)
l1 = [] if i == 0 else set_numbers(S1, i)
S2 ... | {
"input": [
"? - ? = 1\n",
"? + ? - ? + ? + ? = 42\n",
"? = 1000000\n"
],
"output": [
"Impossible\n",
"Possible\n40 + 1 - 1 + 1 + 1 = 42\n",
"Possible\n1000000 = 1000000\n"
]
} |
30 | 7 | Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim.
The killer starts with two potential victims on his first day, selects one of these two, kills selected ... | def main():
a, b = map(str, input().split())
n = int(input())
print(a, b)
for i in range(n):
x, y = map(str, input().split())
if (x == a):
a = y
else:
b = y
print(a, b)
main() | {
"input": [
"icm codeforces\n1\ncodeforces technex\n",
"ross rachel\n4\nross joey\nrachel phoebe\nphoebe monica\nmonica chandler\n"
],
"output": [
"icm codeforces\nicm technex\n",
"ross rachel\njoey rachel\njoey phoebe\njoey monica\njoey chandler\n"
]
} |
31 | 8 | There is little time left before the release of the first national operating system BerlOS. Some of its components are not finished yet — the memory manager is among them. According to the developers' plan, in the first release the memory manager will be very simple and rectilinear. It will support three operations:
... | t, m = map(int, input().split())
a, b = [0] * m, 1
def alloc(n):
global b
f = 0
for i in range(m):
if not a[i]:
f += 1
if f == n:
a[i - n + 1:i + 1] = [b] * n
b += 1
return b - 1
else:
f = 0
return 'NULL... | {
"input": [
"6 10\nalloc 5\nalloc 3\nerase 1\nalloc 6\ndefragment\nalloc 6\n"
],
"output": [
"1\n2\nNULL\n3\n"
]
} |
32 | 10 | Some time ago Mister B detected a strange signal from the space, which he started to study.
After some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation whi... | def main():
n = int(input())
data = input().split()
#print(str(n) + " " + str(data))
data = list(map(lambda x: int(x), data))
res = 0
ires = 0
neg = 0
when = [0] * n
for i in range(n):
data[i] = i + 1 - data[i]
res += abs(data[i])
if data[i] <= 0:
neg += 1
a = -data[i]
if a < 0:
a = a + n
... | {
"input": [
"3\n3 2 1\n",
"3\n1 2 3\n",
"3\n2 3 1\n"
],
"output": [
"2 1\n",
"0 0\n",
"0 1\n"
]
} |
33 | 11 | Igor is a post-graduate student of chemistry faculty in Berland State University (BerSU). He needs to conduct a complicated experiment to write his thesis, but laboratory of BerSU doesn't contain all the materials required for this experiment.
Fortunately, chemical laws allow material transformations (yes, chemistry i... | import sys
# @profile
def main():
f = sys.stdin
# f = open('input.txt', 'r')
# fo = open('log.txt', 'w')
n = int(f.readline())
# b = []
# for i in range(n):
# b.append()
b = list(map(int, f.readline().strip().split(' ')))
a = list(map(int, f.readline().strip().split(' ')))
# ... | {
"input": [
"3\n3 2 1\n1 2 3\n1 1\n1 2\n",
"3\n1 2 3\n3 2 1\n1 1\n1 1\n"
],
"output": [
"NO\n",
"YES\n"
]
} |
34 | 7 | As technologies develop, manufacturers are making the process of unlocking a phone as user-friendly as possible. To unlock its new phone, Arkady's pet dog Mu-mu has to bark the password once. The phone represents a password as a string of two lowercase English letters.
Mu-mu's enemy Kashtanka wants to unlock Mu-mu's p... | a=input()
n=int(input())
s=[input() for i in range(n)]
def go():
print("YES")
exit()
for x in s:
if x == a: go()
for y in s:
if a in (x + y): go()
print("NO") | {
"input": [
"ah\n1\nha\n",
"ya\n4\nah\noy\nto\nha\n",
"hp\n2\nht\ntp\n"
],
"output": [
"YES",
"YES",
"NO"
]
} |
35 | 10 | Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card.
She starts with 0 money on her account.
In the evening of i-th day a transaction ai occurs. If ai > 0, then ai bourles are deposited to Luba's account. If ai < 0, then ai bourles are withdrawn. And if ai = 0, ... | def main():
n, d = map(int, input().split())
a = list(map(int, input().split()))
pref, mx, add, ans = [0] * n, [0] * n, 0, 0
for pos in range(n):
pref[pos] = a[pos] if not pos else a[pos] + pref[pos-1]
for pos in range(n-1, -1, -1):
mx[pos] = pref[pos] if pos == n - 1 else max(mx[pos + 1], pref[pos])
for... | {
"input": [
"5 10\n-5 0 10 -11 0\n",
"5 10\n-1 5 0 -5 3\n",
"3 4\n-10 0 20\n"
],
"output": [
"2\n",
"0\n",
"-1\n"
]
} |
36 | 7 | Luba thinks about watering her garden. The garden can be represented as a segment of length k. Luba has got n buckets, the i-th bucket allows her to water some continuous subsegment of garden of length exactly ai each hour. Luba can't water any parts of the garden that were already watered, also she can't water the gro... | def main():
n, k = map(int, input().split())
arr = list(map(int, input().split()))
arr = [i for i in arr if k % i == 0]
print(k // max(arr))
main() | {
"input": [
"3 6\n2 3 5\n",
"6 7\n1 2 3 4 5 6\n"
],
"output": [
"2\n",
"7\n"
]
} |
37 | 8 | You and your friend are participating in a TV show "Run For Your Prize".
At the start of the show n prizes are located on a straight line. i-th prize is located at position ai. Positions of all prizes are distinct. You start at position 1, your friend — at position 106 (and there is no prize in any of these two positi... | import sys
lines = sys.stdin.read().splitlines()
lincnt = -1
def input():
global lincnt
lincnt += 1
return lines[lincnt]
input()
l = map(int, input().split())
print(max(min(x - 1, 1000000 - x) for x in l))
| {
"input": [
"2\n2 999995\n",
"3\n2 3 9\n"
],
"output": [
"5\n",
"8\n"
]
} |
38 | 8 | You are given a tree (a graph with n vertices and n - 1 edges in which it's possible to reach any vertex from any other vertex using only its edges).
A vertex can be destroyed if this vertex has even degree. If you destroy a vertex, all edges connected to it are also deleted.
Destroy all vertices in the given tree or... | from collections import defaultdict,deque
import sys
import bisect
import math
input=sys.stdin.readline
mod=1000000007
def bfs(root,count):
q=deque([root])
vis.add(root)
while q:
vertex=q.popleft()
for child in graph[vertex]:
if ans[child]==0:
ans[child]=count+1
... | {
"input": [
"5\n0 1 2 1 2\n",
"4\n0 1 2 3\n"
],
"output": [
"YES\n1\n2\n3\n5\n4\n",
"NO\n"
]
} |
39 | 11 | Adilbek's house is located on a street which can be represented as the OX axis. This street is really dark, so Adilbek wants to install some post lamps to illuminate it. Street has n positions to install lamps, they correspond to the integer numbers from 0 to n - 1 on the OX axis. However, some positions are blocked an... | import sys
from sys import stdin,stdout
n,m,k=map(int,stdin.readline().split(' '))
t22=stdin.readline()#;print(t22,"t2222")
bl=[]
if len(t22.strip())==0:
bl=[]
else:
bl=list(map(int,t22.split(' ')))
bd={}
for i in bl:
bd[i]=1
cost=list(map(int,stdin.readline().split(' ')))
dp=[-1 for i in range(n)]
dp[0]=0
def f... | {
"input": [
"5 1 5\n0\n3 3 3 3 3\n",
"4 3 4\n1 2 3\n1 10 100 1000\n",
"7 4 3\n2 4 5 6\n3 14 15\n",
"6 2 3\n1 3\n1 2 3\n"
],
"output": [
"-1\n",
"1000\n",
"-1\n",
"6\n"
]
} |
40 | 9 | At a geometry lesson Gerald was given a task: to get vector B out of vector A. Besides, the teacher permitted him to perform the following operations with vector А:
* Turn the vector by 90 degrees clockwise.
* Add to the vector a certain vector C.
Operations could be performed in any order any number of times.
... | import math
def ok(xa, ya):
x, y = xb - xa, yb - ya
d = math.gcd(abs(xc), abs(yc))
if xc == 0 and yc == 0:
return x == 0 and y == 0
if xc == 0:
return x % yc == 0 and y % yc == 0
if yc == 0:
return x % xc == 0 and y % xc == 0
if (x % d != 0) or (y % d != 0):
retu... | {
"input": [
"0 0\n1 1\n1 1\n",
"0 0\n1 1\n0 1\n",
"0 0\n1 1\n2 2\n"
],
"output": [
"YES\n",
"YES\n",
"NO\n"
]
} |
41 | 7 | Awruk is taking part in elections in his school. It is the final round. He has only one opponent — Elodreip. The are n students in the school. Each student has exactly k votes and is obligated to use all of them. So Awruk knows that if a person gives a_i votes for Elodreip, than he will get exactly k - a_i votes from t... | import sys
def main():
_, *l = map(int, sys.stdin.read().strip().split())
return max(max(l), int(2*sum(l)/len(l)) + 1)
print(main())
| {
"input": [
"5\n2 2 3 2 2\n",
"5\n1 1 1 5 1\n"
],
"output": [
"5\n",
"5\n"
]
} |
42 | 10 | You are given a binary matrix A of size n × n. Let's denote an x-compression of the given matrix as a matrix B of size n/x × n/x such that for every i ∈ [1, n], j ∈ [1, n] the condition A[i][j] = B[⌈ i/x ⌉][⌈ j/x ⌉] is met.
Obviously, x-compression is possible only if x divides n, but this condition is not enough. For... | from math import gcd
def D():
n = int(input())
matrix = [0 for _ in range(n)]
for i in range(n):
a = bin(int(input(), 16))[2:]
matrix[i] = '0'*(n-len(a))+ a
ans = 0
i = 0
while(i<n):
j = i+1
while(j<n and matrix[i]==matrix[j]): #Recorro por filas
j+=1
ans = gcd(ans,j-i)
col = 0
while(col<n):
... | {
"input": [
"8\nE7\nE7\nE7\n00\n00\nE7\nE7\nE7\n",
"4\n7\nF\nF\nF\n"
],
"output": [
"1",
"1"
]
} |
43 | 10 | At the big break Nastya came to the school dining room. There are n pupils in the school, numbered from 1 to n. Unfortunately, Nastya came pretty late, so that all pupils had already stood in the queue, i.e. Nastya took the last place in the queue. Of course, it's a little bit sad for Nastya, but she is not going to de... | import sys
def read() : return sys.stdin.readline()
n, m = map(int, read().split())
p = [int(i) for i in read().split()]
gr = [0] * (n + 1)
for i in range(n + 1) :
gr[i] = []
for i in range(m) :
u, v = map(int, read().split())
gr[u].append(v)
can = [0] * (n + 1)
can[p[n-1]] = 1
idx = n-2; good = 1; ... | {
"input": [
"5 2\n3 1 5 4 2\n5 2\n5 4\n",
"3 3\n3 1 2\n1 2\n3 1\n3 2\n",
"2 1\n1 2\n1 2\n"
],
"output": [
"1\n",
"2\n",
"1\n"
]
} |
44 | 7 | You are given a string s consisting of n lowercase Latin letters.
Let's define a substring as a contiguous subsegment of a string. For example, "acab" is a substring of "abacaba" (it starts in position 3 and ends in position 6), but "aa" or "d" aren't substrings of this string. So the substring of the string s from po... | def solve():
n = int(input())
s = input()
for i in range(n - 1):
if s[i] > s[i + 1]:
print('YES')
print(i + 1, i + 2)
return
print('NO')
solve()
| {
"input": [
"7\nabacaba\n",
"6\naabcfg\n"
],
"output": [
"YES\n2 3\n",
"NO\n"
]
} |
45 | 12 | You are playing a computer card game called Splay the Sire. Currently you are struggling to defeat the final boss of the game.
The boss battle consists of n turns. During each turn, you will get several cards. Each card has two parameters: its cost c_i and damage d_i. You may play some of your cards during each turn i... | import sys
import math
import cProfile
DEBUG = False
def log(s):
if DEBUG and False:
print(s)
def calc_dmg(num, arr):
maximum = 0
if num - len(arr) < 0:
maximum = max(arr)
return sum(arr) + maximum
if DEBUG:
sys.stdin = open('input.txt')
pr = cProfile.Profile()
pr.... | {
"input": [
"5\n3\n1 6\n1 7\n1 5\n2\n1 4\n1 3\n3\n1 10\n3 5\n2 3\n3\n1 15\n2 4\n1 10\n1\n1 100\n"
],
"output": [
"263\n"
]
} |
46 | 10 | This problem differs from the previous one only in the absence of the constraint on the equal length of all numbers a_1, a_2, ..., a_n.
A team of SIS students is going to make a trip on a submarine. Their target is an ancient treasure in a sunken ship lying on the bottom of the Great Rybinsk sea. Unfortunately, the st... | mod=998244353
s=0
input()
d=[0]*11
def get(n,l,i):
if l<i: return (n,(d[i]*11*(int(n)%mod))%mod)
t=((int(n)%mod)*10)%mod
n=n[:-(2*i-1)]+'0'+n[-(2*i-1):]
return (n,(d[i]*(t+int(n)%mod)%mod)%mod)
l=input().split()
for i in l:
d[len(i)]+=1
for i in l:
n=i
x=len(n)
for j in range(1,11):
n,tmp=get(n,x,j)
s=(s+tm... | {
"input": [
"3\n12 3 45\n",
"2\n123 456\n"
],
"output": [
"12330\n",
"1115598\n"
]
} |
47 | 7 | Alice is playing with some stones.
Now there are three numbered heaps of stones. The first of them contains a stones, the second of them contains b stones and the third of them contains c stones.
Each time she can do one of two operations:
1. take one stone from the first heap and two stones from the second heap (... | def test():
a, b, c = map(int, input().split())
x1 = min(b, c//2)
b-=x1
x2 = min(a, b//2)
print((x1+x2)*3)
for _ in range(int(input())):test() | {
"input": [
"3\n3 4 5\n1 0 5\n5 3 2\n"
],
"output": [
"9\n0\n6\n"
]
} |
48 | 11 | There are n cities in Berland and some pairs of them are connected by two-way roads. It is guaranteed that you can pass from any city to any other, moving along the roads. Cities are numerated from 1 to n.
Two fairs are currently taking place in Berland — they are held in two different cities a and b (1 ≤ a, b ≤ n; a ... | def solve():
N, M, A, B = map(int, input().split())
roads = [list(map(lambda x:int(x)-1, input().split())) for _ in range(M)]
A -= 1
B -= 1
adj = [[] for _ in range(N)]
for a, b in roads:
adj[a].append(b)
adj[b].append(a)
def dfs(start, invalid):
Q = [start]
... | {
"input": [
"3\n7 7 3 5\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 5\n4 5 2 3\n1 2\n2 3\n3 4\n4 1\n4 2\n4 3 2 1\n1 2\n2 3\n4 1\n"
],
"output": [
"4\n0\n1\n"
]
} |
49 | 7 | You are given two arrays of integers a_1,…,a_n and b_1,…,b_m.
Your task is to find a non-empty array c_1,…,c_k that is a subsequence of a_1,…,a_n, and also a subsequence of b_1,…,b_m. If there are multiple answers, find one of the smallest possible length. If there are still multiple of the smallest possible length, f... | def help():
input()
a=set(map(int,input().split()))&set(map(int,input().split()))
if a:
print("YES")
print(1,list(a)[0])
return
print("NO")
t=int(input())
for T in range(t):
help() | {
"input": [
"5\n4 5\n10 8 6 4\n1 2 3 4 5\n1 1\n3\n3\n1 1\n3\n2\n5 3\n1000 2 2 2 3\n3 1 5\n5 5\n1 2 3 4 5\n1 2 3 4 5\n"
],
"output": [
"YES\n1 4\nYES\n1 3\nNO\nYES\n1 3\nYES\n1 1\n"
]
} |
50 | 10 | Please note the non-standard memory limit.
There are n problems numbered with integers from 1 to n. i-th problem has the complexity c_i = 2^i, tag tag_i and score s_i.
After solving the problem i it's allowed to solve problem j if and only if IQ < |c_i - c_j| and tag_i ≠ tag_j. After solving it your IQ changes and be... | def nr():return int(input())
def nrs():return [int(i) for i in input().split()]
def f(n,t,s):
d=[0]*n
for i in range(1,n):
for j in range(i-1,-1,-1):
if t[i]==t[j]:continue
sc=abs(s[i]-s[j])
d[i],d[j]=max(d[i],d[j]+sc),max(d[j],d[i]+sc)
return max(d)
for _ in range(nr()):
n=nr()
t=nrs()
s=nrs()
print(... | {
"input": [
"5\n4\n1 2 3 4\n5 10 15 20\n4\n1 2 1 2\n5 10 15 20\n4\n2 2 4 1\n2 8 19 1\n2\n1 1\n6 9\n1\n1\n666\n"
],
"output": [
"\n35\n30\n42\n0\n0\n"
]
} |
51 | 9 | You can't possibly imagine how cold our friends are this winter in Nvodsk! Two of them play the following game to warm up: initially a piece of paper has an integer q. During a move a player should write any integer number that is a non-trivial divisor of the last written number. Then he should run this number of circl... | import math
def prime(n):
b = int(math.sqrt(n))
l = []
while n % 2 == 0:
l.append(2)
n = n // 2
for i in range(3, b+1,2):
while n % i== 0:
l.append(i)
n = n // i
if n > 2:
l.append(n)
ret... | {
"input": [
"1\n",
"6\n",
"30\n"
],
"output": [
"1\n0",
"2",
"1\n6"
]
} |
52 | 9 | Monocarp and Polycarp are learning new programming techniques. Now they decided to try pair programming.
It's known that they have worked together on the same file for n + m minutes. Every minute exactly one of them made one change to the file. Before they started, there were already k lines written in the file.
Ever... | def solve(k, n, m, arr1, arr2):
res = []
i = j = 0
while i != n or j != m:
if i != n and arr1[i] <= k:
res.append(arr1[i])
if arr1[i] == 0:
k += 1
i += 1
elif j != m and arr2[j] <= k:
res.append(arr2[j])
if arr2[j] == 0:
k += 1
j += 1
else:
res = [-1]
break
return ' '.join(map(... | {
"input": [
"5\n\n3 2 2\n2 0\n0 5\n\n4 3 2\n2 0 5\n0 6\n\n0 2 2\n1 0\n2 3\n\n5 4 4\n6 0 8 0\n0 7 0 9\n\n5 4 1\n8 7 8 0\n0\n"
],
"output": [
"0 2 0 5\n0 2 0 5 6\n-1\n0 6 0 7 0 8 0 9\n-1\n"
]
} |
53 | 7 | Valeric and Valerko missed the last Euro football game, so they decided to watch the game's key moments on the Net. They want to start watching as soon as possible but the connection speed is too low. If they turn on the video right now, it will "hang up" as the size of data to watch per second will be more than the si... | def f(l):
a,b,c =l
return (c*(a-b)+b-1)//b
l = list(map(int,input().split()))
print(f(l))
| {
"input": [
"10 3 2\n",
"13 12 1\n",
"4 1 1\n"
],
"output": [
"5\n",
"1\n",
"3\n"
]
} |
54 | 7 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.
You are given a string s, ... | def f(a,b):
for i in a:
if a[i]%b==0:a[i]//=b
else:return'-1'
c=''
for i in a:
c+=i*a[i]
return c*b
b=int(input())
d=input()
a={}
for i in d:
if i in a:a[i]+=1
else:a[i]=1
print(f(a,b)) | {
"input": [
"2\naazz\n",
"3\nabcabcabz\n"
],
"output": [
"azaz",
"-1\n"
]
} |
55 | 9 | The black king is standing on a chess field consisting of 109 rows and 109 columns. We will consider the rows of the field numbered with integers from 1 to 109 from top to bottom. The columns are similarly numbered with integers from 1 to 109 from left to right. We will denote a cell of the field that is located in the... | from collections import deque
def I(): return list(map(int, input().split()))
dic = {}
x0, y0, x1, y1 = I()
for i in range(int(input())):
r, a, b = I()
while a != b + 1:
tup = (r, a)
dic[tup] = -1
a += 1
x = [-1, -1, -1, 0, 0, 1, 1, 1]
y = [-1, 0, 1, -1, 1, -1, 0, 1]
dic[(x0, y0)] = 0... | {
"input": [
"3 4 3 10\n3\n3 1 4\n4 5 9\n3 10 10\n",
"1 1 2 10\n2\n1 1 3\n2 6 10\n",
"5 7 6 11\n3\n5 3 8\n6 7 11\n5 2 5\n"
],
"output": [
"6\n",
"-1\n",
"4\n"
]
} |
56 | 7 | Manao works on a sports TV. He's spent much time watching the football games of some country. After a while he began to notice different patterns. For example, each team has two sets of uniforms: home uniform and guest uniform. When a team plays a game at home, the players put on the home uniform. When a team plays as ... | a=int(input())
A=[]
B=[]
c=0
for i in range(a):
b=input().split()
A.append(b[0])
B.append(b[1])
def cz(x,Y):
d=0
for i in Y:
if i==x:
d+=1
return d
e=0
for i in A:
e+=cz(i,B)
print(e) | {
"input": [
"2\n1 2\n1 2\n",
"4\n100 42\n42 100\n5 42\n100 5\n",
"3\n1 2\n2 4\n3 4\n"
],
"output": [
"0\n",
"5\n",
"1\n"
]
} |
57 | 8 | In the rush of modern life, people often forget how beautiful the world is. The time to enjoy those around them is so little that some even stand in queues to several rooms at the same time in the clinic, running from one queue to another.
(Cultural note: standing in huge and disorganized queues for hours is a native ... | def f(x, p):
q = []
while x:
q.append(x)
x = p[x]
return q
from collections import defaultdict
n, k = map(int, input().split())
t = list(map(int, input().split()))
p = [0] * (n + 1)
for i, j in enumerate(t, 1):
p[j] = i
p = [f(i, p) for i, j in enumerate(t, 1) if j == 0]
s = defaultdict(... | {
"input": [
"6 2\n2 3 0 5 6 0\n",
"6 2\n0 0 1 0 4 5\n",
"6 1\n2 0 4 0 6 0\n",
"4 1\n0 0 0 0\n"
],
"output": [
"2\n5\n",
"1\n3\n4\n6\n",
"2\n4\n6\n",
"1\n2\n3\n4\n"
]
} |
58 | 7 | Xenia the beginner mathematician is a third year student at elementary school. She is now learning the addition operation.
The teacher has written down the sum of multiple numbers. Pupils should calculate the sum. To make the calculation easier, the sum only contains numbers 1, 2 and 3. Still, that isn't enough for Xe... | pl='+'
def tr(s):
v=s.split(pl)
v.sort()
return pl.join(v)
print(tr(input())) | {
"input": [
"2\n",
"3+2+1\n",
"1+1+3+1+3\n"
],
"output": [
"2\n",
"1+2+3\n",
"1+1+1+3+3\n"
]
} |
59 | 9 | Levko loves array a1, a2, ... , an, consisting of integers, very much. That is why Levko is playing with array a, performing all sorts of operations with it. Each operation Levko performs is of one of two types:
1. Increase all elements from li to ri by di. In other words, perform assignments aj = aj + di for all j ... | 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.writa... | {
"input": [
"4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 8\n",
"4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 13\n"
],
"output": [
"YES\n8 7 4 7 ",
"NO\n"
]
} |
60 | 7 | The bear decided to store some raspberry for the winter. He cunningly found out the price for a barrel of honey in kilos of raspberry for each of the following n days. According to the bear's data, on the i-th (1 ≤ i ≤ n) day, the price for one barrel of honey is going to is xi kilos of raspberry.
Unfortunately, the b... | def main():
n, c = map(int, input().split())
l = list(map(int, input().split()))
print(max(0, max(a - b for a, b in zip(l, l[1:])) - c))
main()
| {
"input": [
"6 2\n100 1 10 40 10 40\n",
"5 1\n5 10 7 3 20\n",
"3 0\n1 2 3\n"
],
"output": [
"97\n",
"3\n",
"0\n"
]
} |
61 | 11 | Little Chris is participating in a graph cutting contest. He's a pro. The time has come to test his skills to the fullest.
Chris is given a simple undirected connected graph with n vertices (numbered from 1 to n) and m edges. The problem is to cut it into edge-distinct paths of length 2. Formally, Chris has to partiti... | import sys
input = sys.stdin.readline
print = sys.stdout.write
def get_input():
n, m = [int(x) for x in input().split(' ')]
graph = [[] for _ in range(n + 1)]
for _ in range(m):
c1, c2 = [int(x) for x in input().split(' ')]
graph[c1].append(c2)
graph[c2].append(c1)
if m % 2 ... | {
"input": [
"3 2\n1 2\n2 3\n",
"3 3\n1 2\n2 3\n3 1\n",
"8 12\n1 2\n2 3\n3 4\n4 1\n1 3\n2 4\n3 5\n3 6\n5 6\n6 7\n6 8\n7 8\n"
],
"output": [
"1 2 3\n",
"No solution\n",
"6 7 8 5 6 8 4 3 5 2 3 6 1 2 4 4 1 3 "
]
} |
62 | 7 | Kitahara Haruki has bought n apples for Touma Kazusa and Ogiso Setsuna. Now he wants to divide all the apples between the friends.
Each apple weights 100 grams or 200 grams. Of course Kitahara Haruki doesn't want to offend any of his friend. Therefore the total weight of the apples given to Touma Kazusa must be equal ... | from collections import Counter
def main():
n = int(input())
c = Counter(input().split())
print(('YES', 'NO')[c['100'] & 1 if c['100'] else c['200'] & 1])
if __name__ == '__main__':
main()
| {
"input": [
"4\n100 100 100 200\n",
"3\n100 200 100\n"
],
"output": [
"NO\n",
"YES\n"
]
} |
63 | 7 | There are five people playing a game called "Generosity". Each person gives some non-zero number of coins b as an initial bet. After all players make their bets of b coins, the following operation is repeated for several times: a coin is passed from one player to some other player.
Your task is to write a program that... | s = sum(list(map(int,input().split())))
def ans():
for i in range(1,101):
if 5*i == s:
return i
return -1
print(ans()) | {
"input": [
"2 5 4 0 4\n",
"4 5 9 2 1\n"
],
"output": [
"3\n",
"-1\n"
]
} |
64 | 9 | New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ i ≤ n) book is wi.
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a... | def f(s):
return int(s) - 1
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(f, input().split()))
c = [[0] * n for i in range(n)]
o = 0
for i in range(m):
for j in range(n):
if j != b[i]:
c[j][b[i]] = 1
for j in range(n):
if c[b[i]][j] == 1:
... | {
"input": [
"3 5\n1 2 3\n1 3 2 3 1\n"
],
"output": [
"12\n"
]
} |
65 | 7 | In this problem you will meet the simplified model of game King of Thieves.
In a new ZeptoLab game called "King of Thieves" your aim is to reach a chest with gold by controlling your character, avoiding traps and obstacles on your way.
<image>
An interesting feature of the game is that you can design your own levels... | def main():
n, s = int(input()), input()
for step in range(1, (n + 3) // 4):
for start in range(step):
if "*****" in s[start::step]:
print("yes")
return
print("no")
if __name__ == '__main__':
main() | {
"input": [
"16\n.**.*..*.***.**.\n",
"11\n.*.*...*.*.\n"
],
"output": [
"yes",
"no"
]
} |
66 | 8 | Professor GukiZ doesn't accept string as they are. He likes to swap some letters in string to obtain a new one.
GukiZ has strings a, b, and c. He wants to obtain string k by swapping some letters in a, so that k should contain as many non-overlapping substrings equal either to b or c as possible. Substring of string x... | from collections import Counter
from collections import defaultdict
def get_single(a, b):
res = 2 ** 32
for s in b.keys():
res = min(res, a[s] // b[s])
return res
def get_count(a, b, c):
best = 0, get_single(a, c)
r1 = 0
while a & b == b:
r1 += 1
a = a - b
r2 = get_single(a, c)
if r1 + r2 > sum(best... | {
"input": [
"pozdravstaklenidodiri\nniste\ndobri\n",
"aaa\na\nb\n",
"abbbaaccca\nab\naca\n"
],
"output": [
"nisteaadddiiklooprrvz\n",
"aaa\n",
"ababacabcc\n"
]
} |
67 | 9 | You are given a sequence of n integers a1, a2, ..., an.
Determine a real number x such that the weakness of the sequence a1 - x, a2 - x, ..., an - x is as small as possible.
The weakness of a sequence is defined as the maximum value of the poorness over all segments (contiguous subsequences) of a sequence.
The poor... | n = int(input())
a = list(map(int, input().split()))
def f(x):
mx, cur = 0, 0
for i in range(n):
cur = max(cur, 0) + a[i] - x
mx = max(mx, cur)
cur = 0
for i in range(n):
cur = max(cur, 0) + x - a[i]
mx = max(mx, cur)
return mx
L, R = min(a), max(a) + 1
for t in ra... | {
"input": [
"10\n1 10 2 9 3 8 4 7 5 6\n",
"4\n1 2 3 4\n",
"3\n1 2 3\n"
],
"output": [
"4.500000000000024\n",
"2.000000000000003\n",
"1.000000000000000\n"
]
} |
68 | 7 | Polycarp is working on a new project called "Polychat". Following modern tendencies in IT, he decided, that this project should contain chat as well. To achieve this goal, Polycarp has spent several hours in front of his laptop and implemented a chat server that can process three types of commands:
* Include a perso... | def readln(): return tuple(map(int, input().split()))
cnt = ans = 0
while True:
try:
inp = input()
if inp[0] == '+':
cnt += 1
elif inp[0] == '-':
cnt -= 1
else:
ans += cnt * len(inp.split(':')[1])
except:
break
print(ans)
| {
"input": [
"+Mike\nMike:hello\n+Kate\n+Dmitry\n-Dmitry\nKate:hi\n-Kate\n",
"+Mike\n-Mike\n+Mike\nMike:Hi I am here\n-Mike\n+Kate\n-Kate\n"
],
"output": [
"9\n",
"14\n"
]
} |
69 | 10 | Wet Shark asked Rat Kwesh to generate three positive real numbers x, y and z, from 0.1 to 200.0, inclusive. Wet Krash wants to impress Wet Shark, so all generated numbers will have exactly one digit after the decimal point.
Wet Shark knows Rat Kwesh will want a lot of cheese. So he will give the Rat an opportunity to ... | from math import log, inf
from itertools import product, permutations
def comp_key(p, A, mode):
a = log(A[p[0][1]])*A[p[0][2]] if p[1] else log(A[p[0][1]]) + log(A[p[0][2]])
k = A[p[0][0]] if mode else 1/A[p[0][0]]
return a + log(log(k)) if k > 1 else -inf
def solve(A):
mode = any((x > 1 for x in A))
... | {
"input": [
"1.1 3.4 2.5\n",
"1.9 1.8 1.7\n",
"2.0 2.0 2.0\n"
],
"output": [
"z^y^x\n",
"(x^y)^z\n",
"x^y^z\n"
]
} |
70 | 8 | Bearland has n cities, numbered 1 through n. Cities are connected via bidirectional roads. Each road connects two distinct cities. No two roads connect the same pair of cities.
Bear Limak was once in a city a and he wanted to go to a city b. There was no direct connection so he decided to take a long walk, visiting ea... | def main():
n, k = [int(i) for i in input().strip().split()]
a, b, c, d = [int(i) for i in input().strip().split()]
if k <= n or n <= 4:
print(-1)
return
s = [a, b, c, d]
others = [i for i in range(1, n + 1) if i not in s]
print(" ".join(map(str, [a, c] + others + [d, b])))
... | {
"input": [
"1000 999\n10 20 30 40\n",
"7 11\n2 4 7 3\n"
],
"output": [
"-1",
"2 7 1 5 6 3 4 \n7 2 1 5 6 4 3 "
]
} |
End of preview. Expand in Data Studio
README.md exists but content is empty.
- Downloads last month
- 11