index int64 0 5.16k | difficulty int64 7 12 | question stringlengths 126 7.12k | solution stringlengths 30 18.6k | test_cases dict |
|---|---|---|---|---|
200 | 7 | Vasya has recently got a job as a cashier at a local store. His day at work is L minutes long. Vasya has already memorized n regular customers, the i-th of which comes after t_{i} minutes after the beginning of the day, and his service consumes l_{i} minutes. It is guaranteed that no customer will arrive while Vasya is... | def inp():
return map(int, input().split())
n, L, a = inp()
count,time=0,0
for i in range(n):
l, t = inp()
diff=l-time
if(diff>=a):
count+=diff//a
time=l+t
if(L-time>=a):
count+=(L-time)//a
print(count)
| {
"input": [
"2 11 3\n0 1\n1 1\n",
"0 5 2\n",
"1 3 2\n1 2\n"
],
"output": [
"3\n",
"2\n",
"0\n"
]
} |
201 | 9 | Recently, Masha was presented with a chessboard with a height of n and a width of m.
The rows on the chessboard are numbered from 1 to n from bottom to top. The columns are numbered from 1 to m from left to right. Therefore, each cell can be specified with the coordinates (x,y), where x is the column number, and y is ... | def count(x,y,p,q,color):
if x>p or y>q:
return 0
ret=(p-x+1)*(q-y+1)//2
if (p-x)%2==1 or (q-y)%2==1:
return ret
if color==(x+y)%2:
return ret+1
else:
return ret
t=int(input())
for _ in range(t):
n,m=map(int,input().split())
x1,y1,x2,y2=list(map(int,input().s... | {
"input": [
"5\n2 2\n1 1 2 2\n1 1 2 2\n3 4\n2 2 3 2\n3 1 4 3\n1 5\n1 1 5 1\n3 1 5 1\n4 4\n1 1 4 2\n1 3 4 4\n3 4\n1 2 4 2\n2 1 3 3\n"
],
"output": [
"0 4\n3 9\n2 3\n8 8\n4 8\n"
]
} |
202 | 8 | All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the respon... | n,k=map(int,input().split())
group=list(map(int,input().split()))
available=[[k,1] for i in range(k+1)]
center=(k+1)//2
def calc(center,row,col,num):
end_col=col+num-1
distance=abs(center-row)*num
if col>=center:
distance+=(col-center)*num+(num-1)*num//2
elif end_col<=center:
distance+=(center-end_col)*... | {
"input": [
"4 3\n1 2 3 1\n",
"2 1\n1 1\n"
],
"output": [
"2 2 2\n1 1 2\n3 1 3\n2 1 1\n",
"1 1 1\n-1\n"
]
} |
203 | 7 | Everybody knows that the m-coder Tournament will happen soon. m schools participate in the tournament, and only one student from each school participates.
There are a total of n students in those schools. Before the tournament, all students put their names and the names of their schools into the Technogoblet of Fire. ... | def inpl(): return list(map(int, input().split()))
from collections import defaultdict
H = defaultdict(lambda: 0)
N, M, K = inpl()
P = inpl()
S = inpl()
C = inpl()
for p, s in zip(P, S):
H[s] = max(H[s], p)
ans = 0
for c in C:
if P[c-1] != H[S[c-1]]:
ans += 1
print(ans) | {
"input": [
"8 4 4\n1 2 3 4 5 6 7 8\n4 3 2 1 4 3 2 1\n3 4 5 6\n",
"7 3 1\n1 5 3 4 6 7 2\n1 3 1 2 1 2 3\n3\n"
],
"output": [
"2\n",
"1\n"
]
} |
204 | 9 | You are given a permutation p of integers from 1 to n, where n is an even number.
Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
* take two indices i and j such that 2 β
|i - j| β₯ n and swap p_i and p_j.
There is no need to minimize the number of... | n = int(input())
l = [*map(int, input().split())]
l = [i - 1 for i in l]
pos = {}
ans = []
for i, j in enumerate(l):
pos[j] = i
def swap(i, j):
ans.append((pos[l[i]] + 1, pos[l[j]] + 1))
pos[l[i]], pos[l[j]] = pos[l[j]], pos[l[i]]
l[i], l[j] = l[j], l[i]
def solve(i):
u = n - 1 if pos[i] < n // 2 else 0
v = 0 if ... | {
"input": [
"2\n2 1\n",
"4\n3 4 1 2\n",
"6\n2 5 3 1 4 6\n"
],
"output": [
"1\n1 2\n",
"2\n1 3\n2 4\n",
"5\n1 6\n2 6\n1 6\n1 5\n1 4\n"
]
} |
205 | 12 | You are given a tree with n nodes. You have to write non-negative integers on its edges so that the following condition would be satisfied:
For every two nodes i, j, look at the path between them and count the sum of numbers on the edges of this path. Write all obtained sums on the blackboard. Then every integer from ... | import math
n = int(input())
if n == 1:
print()
else:
edge = [list(map(int, input().split())) for i in range(1, n) ]
g = {}
for x, y in edge:
if x not in g:
g[x] = []
if y not in g:
g[y] = []
g[x].append(y)
g[y].append(x)
... | {
"input": [
"4\n2 4\n2 3\n2 1\n",
"3\n2 3\n2 1\n",
"5\n1 2\n1 3\n1 4\n2 5\n"
],
"output": [
"2 4 1\n2 3 2\n2 1 4\n",
"2 3 1\n2 1 2\n",
"1 2 1\n2 5 1\n1 3 3\n1 4 6\n"
]
} |
206 | 8 | The only difference between easy and hard versions is constraints.
The BerTV channel every day broadcasts one episode of one of the k TV shows. You know the schedule for the next n days: a sequence of integers a_1, a_2, ..., a_n (1 β€ a_i β€ k), where a_i is the show, the episode of which will be shown in i-th day.
The... | def main():
n, k, d = map(int, input().split())
l = list(map(int, input().split()))
rez = d
for i in range(n - d + 1):
rez = min(rez, len(set(l[i:i+d])))
print(rez)
t = int(input())
for i in range(t):
main()
| {
"input": [
"4\n5 2 2\n1 2 1 2 1\n9 3 3\n3 3 3 2 2 2 1 1 1\n4 10 4\n10 8 6 4\n16 9 8\n3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3\n"
],
"output": [
"2\n1\n4\n5\n"
]
} |
207 | 10 | The only difference between easy and hard versions is constraints.
You are given n segments on the coordinate axis OX. Segments can intersect, lie inside each other and even coincide. The i-th segment is [l_i; r_i] (l_i β€ r_i) and it covers all integer points j such that l_i β€ j β€ r_i.
The integer point is called bad... |
n,k = map(int,input().split())
L = [list(map(int,input().split())) for i in range(n)]
for i in range(n):
L[i].append(i)
L.sort(key=lambda x:x[1])
# print(L)
def c(x):
for i in range(x[0],x[1]+1):
if T[i] >= k:
return 0
return 1
ans = 0
A=[]
T = [0] * 203
i = 0
t = 0
while i < n :
... | {
"input": [
"5 1\n29 30\n30 30\n29 29\n28 30\n30 30\n",
"7 2\n11 11\n9 11\n7 8\n8 9\n7 8\n9 11\n7 9\n",
"6 1\n2 3\n3 3\n2 3\n2 2\n2 3\n2 3\n"
],
"output": [
"3\n4 1 5 ",
"3\n7 4 6 ",
"4\n1 3 5 6\n"
]
} |
208 | 7 | You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for h... | from sys import stdin, stderr
readline = stdin.buffer.readline
def rl():
return [int(w) for w in readline().split()]
t, = rl()
for _ in range(t):
n,m,k = rl()
a = rl()
k = min(k, m-1)
print(max(
min(max(a[x+y], a[n-m+x+y]) for y in range(m-k))
for x in range(k+1)
))
| {
"input": [
"4\n6 4 2\n2 9 2 3 8 5\n4 4 1\n2 13 60 4\n4 1 3\n1 2 2 1\n2 2 0\n1 2\n"
],
"output": [
"8\n4\n1\n1\n"
]
} |
209 | 9 | You are given a board of size n Γ n, where n is odd (not divisible by 2). Initially, each cell of the board contains one figure.
In one move, you can select exactly one figure presented in some cell and move it to one of the cells sharing a side or a corner with the current cell, i.e. from the cell (i, j) you can move... | def solve():
n=int(input())
return int((n-1)*n*(n+1)/3)
T=int(input())
for t in range(T):
print(solve()) | {
"input": [
"3\n1\n5\n499993\n"
],
"output": [
"0\n40\n41664916690999888\n"
]
} |
210 | 8 | Alica and Bob are playing a game.
Initially they have a binary string s consisting of only characters 0 and 1.
Alice and Bob make alternating moves: Alice makes the first move, Bob makes the second move, Alice makes the third one, and so on. During each move, the current player must choose two different adjacent char... | def f(n):
if min(n.count("1"),n.count("0")) %2==0:
print("NET")
else:
print("DA")
x = int(input())
for i in range(x):
n = input()
f(n) | {
"input": [
"3\n01\n1111\n0011\n"
],
"output": [
"DA\nNET\nNET\n"
]
} |
211 | 7 | You are given an array a of n integers.
You want to make all elements of a equal to zero by doing the following operation exactly three times:
* Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different).
It can... | def mulLen():
n = int(input())
a = list(map(int, input().split()))
print(1, 1)
print(-a[0])
a[0] = 0
if n == 1:
print(1,1)
print(0)
print(1,1)
print(0)
else:
print(2,n)
for i in a[1:]: print(i*(n-1), end=" ")
print()
print(1, n... | {
"input": [
"4\n1 3 2 4\n"
],
"output": [
"1 1\n-1\n2 4\n9 6 12\n1 4\n0 -12 -8 -16\n"
]
} |
212 | 9 | In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and t... | n = int(input())
a = []
for _ in range(n):
name, num = input().split()
a.append([name, int(num)])
a = sorted(a, key = lambda x: x[1])
h = []
def solve(a):
h = []
for name, num in a:
if num > len(h):
return False, -1
if num == 0:
if len(h) == 0:
... | {
"input": [
"4\nvasya 0\npetya 1\nmanya 3\ndunay 3\n",
"4\na 0\nb 2\nc 0\nd 0\n"
],
"output": [
"-1\n",
"a 1\nc 3\nd 4\nb 2\n"
]
} |
213 | 9 | Artem is building a new robot. He has a matrix a consisting of n rows and m columns. The cell located on the i-th row from the top and the j-th column from the left has a value a_{i,j} written in it.
If two adjacent cells contain the same value, the robot will break. A matrix is called good if no two adjacent cells c... | def solve(n, m):
for i in range(n):
row = list(map(int, input().split()))
for j in range(m):
if (row[j]+i+j) % 2:
row[j] += 1
print(*row)
t = int(input())
for i in range(t):
n, m = map(int, input().split())
solve(n, m)
| {
"input": [
"3\n3 2\n1 2\n4 5\n7 8\n2 2\n1 1\n3 3\n2 2\n1 3\n2 2\n"
],
"output": [
"2 3\n5 6\n8 9\n2 1\n3 4\n2 3\n3 2\n"
]
} |
214 | 8 | You are given an array [a_1, a_2, ..., a_n] such that 1 β€ a_i β€ 10^9. Let S be the sum of all elements of the array a.
Let's call an array b of n integers beautiful if:
* 1 β€ b_i β€ 10^9 for each i from 1 to n;
* for every pair of adjacent integers from the array (b_i, b_{i + 1}), either b_i divides b_{i + 1}, or... | #Created by Pradeep
import math
def solve():
n=int(input())
nrr=list(map(int, input().split()))
for i in nrr:
p=int(math.log2(i))
ans=2**p
print(ans, end=' ')
for _ in range(int(input())):
solve()
| {
"input": [
"4\n5\n1 2 3 4 5\n2\n4 6\n2\n1 1000000000\n6\n3 4 8 1 2 3\n"
],
"output": [
"\n3 3 3 3 3\n3 6\n1 1000000000\n4 4 8 1 3 3\n"
]
} |
215 | 8 | The princess is going to escape the dragon's cave, and she needs to plan it carefully.
The princess runs at vp miles per hour, and the dragon flies at vd miles per hour. The dragon will discover the escape after t hours and will chase the princess immediately. Looks like there's no chance to success, but the princess ... |
def main():
vp, vd, t, f, c = [int(input()) for x in range(5)]
p, d, count = t * vp, 0, 0
while (p < c) and (vd > vp):
p = ((p / (vd - vp)) * vd)
if p < c:
count += 1
p += ((p / vd) * vp) + (f * vp)
print(count)
main()
| {
"input": [
"1\n2\n1\n1\n10\n",
"1\n2\n1\n1\n8\n"
],
"output": [
"2\n",
"1\n"
]
} |
216 | 10 | Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β "PriceFixed". Here are some rules of that store:
* The store has an infinite number of items of every product.
* All products have the same price: 2 rubles per item.
* ... | # stdin = open('testdata.txt')
# def input():
# return stdin.readline().strip()
n = int(input())
a = [""]*n
ans = 0
sm=0
for i in range(n):
g,h = [int(x) for x in input().split()]
a[i] = [h,g]
sm+=g
a.sort()
for i in range(n-1,-1,-1):
x = a[i]
k = min(x[0],sm)
ans += min(sm - ans - k,x[1])
print(sm*2-ans... | {
"input": [
"5\n2 7\n2 8\n1 2\n2 4\n1 8\n",
"3\n3 4\n1 3\n1 5\n"
],
"output": [
"12\n",
"8\n"
]
} |
217 | 7 | Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.
You know the rules of comparing the results of two give... | def main():
n,k=[int(i) for i in input().split()]
l=[]
for i in range(n):
l.append([int(i) for i in input().split()])
l.sort(key=lambda x : (-x[0],x[1]))
print(l.count(l[k-1]))
return 0
if __name__ == '__main__':
main()
| {
"input": [
"5 4\n3 1\n3 1\n5 3\n3 1\n3 1\n",
"7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10\n"
],
"output": [
"4\n",
"3\n"
]
} |
218 | 9 | John Doe started thinking about graphs. After some thought he decided that he wants to paint an undirected graph, containing exactly k cycles of length 3.
A cycle of length 3 is an unordered group of three distinct graph vertices a, b and c, such that each pair of them is connected by a graph edge.
John has been pa... | # https://codeforces.com/contest/232/problem/A
# WA
def f_3(n):
return n * (n-1) * (n-2) // 6
def f_2(n):
return n * (n-1) // 2
a_3 = [f_3(i) for i in range(100)]
a_2 = [f_2(i) for i in range(100)]
def find_2(x, a):
arr = []
cur = len(a) - 1
while x > 0:
while x < a[cur]:
... | {
"input": [
"10\n",
"1\n"
],
"output": [
"5\n01111\n10111\n11011\n11101\n11110\n",
"3\n011\n101\n110\n"
]
} |
219 | 8 | Little Elephant loves magic squares very much.
A magic square is a 3 Γ 3 table, each cell contains some positive integer. At that the sums of integers in all rows, columns and diagonals of the table are equal. The figure below shows the magic square, the sum of integers in all its rows, columns and diagonals equals 15... | def mi():
return map(int, input().split())
q,w,e = mi()
a,s,d = mi()
z,x,c = mi()
q = int((a+2*d-w)/2)
s = e+d-q
c = e+s+z-q-s
print (q, w, e)
print (a, s, d)
print (z, x, c)
| {
"input": [
"0 1 1\n1 0 1\n1 1 0\n",
"0 3 6\n5 0 5\n4 7 0\n"
],
"output": [
"1 1 1\n1 1 1\n1 1 1\n",
"6 3 6\n5 5 5\n4 7 4\n"
]
} |
220 | 8 | The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. h... | from sys import stdin
stdin.readline
def mp(): return list(map(int, stdin.readline().strip().split()))
def it():return int(stdin.readline().strip())
from math import pi
n=it()
su=0
s=[]
for i in range(n):
a,g=mp()
if su+a<=500:
su+=a
s+=["A"]
else:
su-=g
s+=["G"]
print(''.join(s))
| {
"input": [
"2\n1 999\n999 1\n",
"3\n400 600\n400 600\n400 600\n"
],
"output": [
"AG\n",
"AGA\n"
]
} |
221 | 7 | Unfortunately, Vasya can only sum pairs of integers (a, b), such that for any decimal place at least one number has digit 0 in this place. For example, Vasya can sum numbers 505 and 50, but he cannot sum 1 and 4.
Vasya has a set of k distinct non-negative integers d1, d2, ..., dk.
Vasya wants to choose some integers ... | def fn(a,b):
(a,b)=((3-len(a))*'0'+a),((3-len(b))*'0'+b)
for i in range(3):
if a[i]!='0' and b[i]!='0':return False
return True
n=int(input())
a=list(map(str, input().split()))
ans1=[]
temp=[]
ans2=0
for i in range(n):
temp=[a[i]]
for j in range(n):
if fn(a[i],a[j]) and a[i]!=a[j]:
... | {
"input": [
"4\n100 10 1 0\n",
"3\n2 70 3\n"
],
"output": [
"4\n100 10 1 0\n",
"2\n2 70\n"
]
} |
222 | 7 | β Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me?
β Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night?
At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcomin... | def main():
n, aa = int(input()), list(map(int, input().split()))
partialsum, s, d, ranges = [0] * n, 0, {}, []
for i, a in enumerate(aa):
if a > 0:
s += a
partialsum[i] = s
if a in d:
d[a].append(i)
else:
d[a] = [i]
ranges = []
for... | {
"input": [
"5\n1 2 3 1 2\n",
"5\n1 -2 3 1 -2\n"
],
"output": [
"8 1\n1 ",
"5 2\n2 5 "
]
} |
223 | 7 | Pavel loves grid mazes. A grid maze is an n Γ m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side.
Pavel drew a grid maze with all empty cells forming a connected area. That is, you can go from any empty cell to any oth... | from collections import defaultdict, deque
def helper(row, col):
queue = deque([(row, col)])
visited = {(row, col)}
while queue:
r, c = queue.popleft()
for x, y in [(r-1, c), (r+1, c), (r, c-1), (r, c+1)]:
if 0 <= x < n and 0 <= y < m and grid[x][y] == '.' and (x, y) not in visited:
queue.append((x, y)... | {
"input": [
"3 4 2\n#..#\n..#.\n#...\n",
"5 4 5\n#...\n#.#.\n.#..\n...#\n.#.#\n"
],
"output": [
"#..#\n..#X\n#..X\n",
"#...\n#.#.\nX#..\nXX.#\nX#X#\n"
]
} |
224 | 9 | User ainta loves to play with cards. He has a cards containing letter "o" and b cards containing letter "x". He arranges the cards in a row, and calculates the score of the deck by the formula below.
1. At first, the score is 0.
2. For each block of contiguous "o"s with length x the score increases by x2.
3. F... | a,b=map(int,input().split())
def sqr(x):
return x*x
def work( num, flag=0 ):
ans=sqr(a-num+1)+num-1
could = min(b, num+1)
cc=b//could
res=b%could
ans-=res * sqr(cc+1) + (could-res)*sqr(cc)
if flag:
print(ans)
list=''
res2=could-res
if could==num+1:
... | {
"input": [
"0 4\n",
"2 3\n",
"4 0\n"
],
"output": [
"-16\nxxxx\n",
"-1\nxxoox\n",
"16\noooo\n"
]
} |
225 | 10 | Recently a serious bug has been found in the FOS code. The head of the F company wants to find the culprit and punish him. For that, he set up an organizational meeting, the issue is: who's bugged the code? Each of the n coders on the meeting said: 'I know for sure that either x or y did it!'
The head of the company d... | from collections import defaultdict
from bisect import bisect_left as lower
import sys
input = sys.stdin.readline
def put():
return map(int, input().split())
try:
n,m = put()
cnt, mp, ans = [0]*n, defaultdict(), [0]*n
for _ in range(n):
x,y = put()
x,y = x-1,y-1
key = (min(x,y),... | {
"input": [
"8 6\n5 6\n5 7\n5 8\n6 2\n2 1\n7 3\n1 3\n1 4\n",
"4 2\n2 3\n1 4\n1 4\n2 1\n"
],
"output": [
"1\n",
"6\n"
]
} |
226 | 8 | Bizon the Champion isn't just a bison. He also is a favorite of the "Bizons" team.
At a competition the "Bizons" got the following problem: "You are given two distinct words (strings of English letters), s and t. You need to transform word s into word t". The task looked simple to the guys because they know the suffix... | def issub(a,b):
i=0
for ch in a:
if i<len(b) and b[i]==ch: i+=1
return i==len(b)
a,b=input(),input()
sa,sb=sorted(a),sorted(b)
if issub(a,b): print('automaton')
elif sa==sb: print('array')
elif issub(sa,sb): print('both')
else: print('need tree') | {
"input": [
"need\ntree\n",
"automaton\ntomat\n",
"array\narary\n",
"both\nhot\n"
],
"output": [
"need tree\n",
"automaton\n",
"array\n",
"both\n"
]
} |
227 | 9 | Today there is going to be an unusual performance at the circus β hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together... | def f(k):
res=0
if k=='T':
res=1
return(res)
n=int(input())
s=input()
t=0
for i in range(n):
t+=f(s[i])
tw=0
for i in range(t):
tw=tw+1-f(s[i])
m=tw
for i in range(n-1):
tw=tw+f(s[i])-f(s[(i+t)%n])
m=min(m,tw)
print(m) | {
"input": [
"3\nHTH\n",
"9\nHTHTHTHHT\n"
],
"output": [
"0",
"2"
]
} |
228 | 10 | Vanya and his friend Vova play a computer game where they need to destroy n monsters to pass a level. Vanya's character performs attack with frequency x hits per second and Vova's character performs attack with frequency y hits per second. Each character spends fixed time to raise a weapon and then he hits (the time to... | n,x,y=map(int,input().split())
def f(a,x,y):return (a*x+x+y-1)//(x+y)
for _ in[0]*n:n=int(input());r=f(n,x,y)*y-f(n,y,x)*x;print(['Both','Vova','Vanya'][(r>0)+(r<0)*2]) | {
"input": [
"2 1 1\n1\n2\n",
"4 3 2\n1\n2\n3\n4\n"
],
"output": [
"Both\nBoth\n",
"Vanya\nVova\nVanya\nBoth\n"
]
} |
229 | 11 | Vasya is interested in arranging dominoes. He is fed up with common dominoes and he uses the dominoes of different heights. He put n dominoes on the table along one axis, going from left to right. Every domino stands perpendicular to that axis so that the axis passes through the center of its base. The i-th domino has ... | from typing import TypeVar, Generic, Callable, List
import sys
from array import array # noqa: F401
from bisect import bisect_left, bisect_right
def input():
return sys.stdin.buffer.readline().decode('utf-8')
T = TypeVar('T')
class SegmentTree(Generic[T]):
__slots__ = ["size", "tree", "identity", "op", "... | {
"input": [
"4\n16 5\n20 5\n10 10\n18 2\n",
"4\n0 10\n1 5\n9 10\n15 10\n"
],
"output": [
"3 1 4 1 ",
"4 1 2 1 "
]
} |
230 | 9 | A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filter) and its many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practi... | def mp():
return map(int,input().split())
def lt():
return list(map(int,input().split()))
def pt(x):
print(x)
n = int(input())
L = lt()
count = 0
i = 0
c = [None for i in range(n)]
while i < n-1:
j = i
while j < n-1 and L[j] != L[j+1]:
j += 1
span = j-i+1
for k in range(i,i+span//2)... | {
"input": [
"5\n0 1 0 1 0\n",
"4\n0 0 1 1\n"
],
"output": [
"2\n0 0 0 0 0",
"0\n0 0 1 1"
]
} |
231 | 7 | Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it... | def main():
n, a, b = map(int, input().split())
l, res = [], []
for _ in range(n):
u, v = input().split()
l.append((int(u) - a, int(v) - b))
x0, y0 = l[-1]
for x1, y1 in l:
res.append(x1 * x1 + y1 * y1)
dx, dy = x1 - x0, y1 - y0
if (x0 * dx + y0 * dy) * (x1 * ... | {
"input": [
"3 0 0\n0 1\n-1 2\n1 2\n",
"4 1 -1\n0 0\n1 2\n2 0\n1 1\n"
],
"output": [
"12.566370614359172464\n",
"21.991148575128551812\n"
]
} |
232 | 7 | Dante is engaged in a fight with "The Savior". Before he can fight it with his sword, he needs to break its shields. He has two guns, Ebony and Ivory, each of them is able to perform any non-negative number of shots.
For every bullet that hits the shield, Ebony deals a units of damage while Ivory deals b units of dama... | def main():
a, b, c = map(int, input().split())
while c % a != 0 and c > 0:
c -= b
print('No' if c < 0 else 'Yes')
if __name__ == '__main__':
main()
| {
"input": [
"6 11 6\n",
"3 2 7\n",
"4 6 15\n"
],
"output": [
"YES",
"YES",
"NO"
]
} |
233 | 10 | Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and chan... | def main():
s = input().split()
n = int(s[0])
l = int(s[1])
r = int(s[2])
s = input().split()
arr1 = []
for i in range(n):
k = int(s[i])
arr1.append(k)
s = input().split()
for i in range(n):
k = int(s[i])
if arr1[i] != k and (i+1 < l or i+1 > r):
... | {
"input": [
"4 2 4\n1 1 1 1\n1 1 1 1\n",
"5 2 4\n3 4 2 3 1\n3 2 3 4 1\n",
"3 1 2\n1 2 3\n3 1 2\n"
],
"output": [
"TRUTH\n",
"TRUTH\n",
"LIE\n"
]
} |
234 | 10 | In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are n extensions, the i-th of them multiplies the width or the length (by Ar... | def isin(a,b,h,w):
return (h >= a and w >= b) or (h >= b and w >= a)
a,b,h,w,n = map(int, input().split())
c = sorted(list(map(int, input().split())), key=lambda x: -x)
if isin(a,b,h,w):
print(0)
exit()
vis = {h: w}
for i in range(len(c)):
nc = c[i]
pairs = []
for l in vis.keys():
pai... | {
"input": [
"5 5 1 2 3\n2 2 3\n",
"3 3 3 3 5\n2 3 5 4 2\n",
"3 3 2 4 4\n2 5 4 10\n",
"3 4 1 1 3\n2 3 2\n"
],
"output": [
"-1",
"0",
"1",
"3"
]
} |
235 | 8 | Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky.
The ticket is considered lucky if the sum of first three digits equals to the sum of las... | def solve():
s = input()
x = [int(z) for z in s]
y, z = x[:3], x[3:]
if sum(y) < sum(z):
y, z = z, y
a = sum(y) - sum(z)
b = [y[-1], y[-2], y[-3], 9 - z[0], 9 - z[1], 9 - z[2]]
b.sort()
c = 0
cnt = 0
while c < a:
c += b.pop()
cnt += 1
return cnt
... | {
"input": [
"111000\n",
"000000\n",
"123456\n"
],
"output": [
"1\n",
"0\n",
"2\n"
]
} |
236 | 10 | You can perfectly predict the price of a certain stock for the next N days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, each day you will either buy one share, sell one share, or do nothing. Initially you own zero shares, and you cannot sell shares when you... | import math
from heapq import *
def maxProfit(prices, days):
payoff = 0
maxPrice, minPrice = max(prices), min(prices)
maxIndex, minIndex = prices.index(maxPrice), prices.index(minPrice)
iterator = iter(prices)
h = [] # heap
if days == 1:
print(0)
return
for i in range(... | {
"input": [
"9\n10 5 4 7 9 12 6 2 10\n",
"20\n3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4\n"
],
"output": [
"20\n",
"41\n"
]
} |
237 | 7 | Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after k minutes after turning on.
During cooking, Julia goes to the kitchen every d minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. The s... | def check(x):
X=int(x)
r=X//d*(k+d)
x-=X//d*d
if(x>=k):
r+=k+x
else:
r+=2*x
return r>=2*t
k,d,t=map(int,input().split())
d*=k//d+(k%d!=0)
l=0
r=1e20
for i in range(200):
med=(r+l)/2
if(check(med)):
r=med
else:
l=med
print(l)
| {
"input": [
"4 2 20\n",
"3 2 6\n"
],
"output": [
"20.000000000000000",
"6.500000000000000"
]
} |
238 | 9 | Vasya's house is situated in a forest, and there is a mushroom glade near it. The glade consists of two rows, each of which can be divided into n consecutive cells. For each cell Vasya knows how fast the mushrooms grow in this cell (more formally, how many grams of mushrooms grow in this cell each minute). Vasya spends... | import sys
import io
stream_enable = 0
inpstream = """
3
1 2 3
6 5 4
"""
if stream_enable:
sys.stdin = io.StringIO(inpstream)
input()
def inpmap():
return list(map(int, input().split()))
n = int(input())
arr = [inpmap(), inpmap()]
s = [0] * n
s[-1] = arr[0][-1] + arr[1][-1]
for i in range(n - 2, -1, -... | {
"input": [
"3\n1 2 3\n6 5 4\n",
"3\n1 1000 10000\n10 100 100000\n"
],
"output": [
"70\n",
"543210\n"
]
} |
239 | 10 | You are given a positive integer n greater or equal to 2. For every pair of integers a and b (2 β€ |a|, |b| β€ n), you can transform a into b if and only if there exists an integer x such that 1 < |x| and (a β
x = b or b β
x = a), where |x| denotes the absolute value of x.
After such a transformation, your score increas... | def fun_with_integers_linear2_solve():
n = int(input())
ans = 0
for i in range(2, n):
cant = n // i
ans += (cant * (cant + 1) // 2 - 1)
print(ans * 4)
fun_with_integers_linear2_solve() | {
"input": [
"2\n",
"4\n",
"6\n"
],
"output": [
"0",
"8",
"28"
]
} |
240 | 9 | The Fair Nut found a string s. The string consists of lowercase Latin letters. The Nut is a curious guy, so he wants to find the number of strictly increasing sequences p_1, p_2, β¦, p_k, such that:
1. For each i (1 β€ i β€ k), s_{p_i} = 'a'.
2. For each i (1 β€ i < k), there is such j that p_i < j < p_{i + 1} and s... | mod = 1000000007
def md(x):
return ((x%mod)+mod)%mod
s = input()
r = 1
a = 1
for i in s:
if i == "a":
a += 1
elif i == "b":
r = md(r*a)
a = 1
r = md(r*a)
print (md(r-1))
| {
"input": [
"agaa\n",
"baaaa\n",
"abbaa\n"
],
"output": [
"3\n",
"4\n",
"5\n"
]
} |
241 | 11 | Roman and Denis are on the trip to the programming competition. Since the trip was long, they soon got bored, and hence decided to came up with something. Roman invented a pizza's recipe, while Denis invented a string multiplication. According to Denis, the result of multiplication (product) of strings s of length m an... | from math import *
import sys
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int, minp().split())
n = mint()
a = [0]*256
b = [0]*256
for k in range(0,n):
#print(a[ord('a'):ord('z')+1])
for i in range(ord('a'),ord('z')+1):
b[i] = min(a[i],1)
i = 0
s = ... | {
"input": [
"2\nbnn\na\n",
"3\na\nb\na\n"
],
"output": [
"1",
"3"
]
} |
242 | 7 | On a random day, Neko found n treasure chests and m keys. The i-th chest has an integer a_i written on it and the j-th key has an integer b_j on it. Neko knows those chests contain the powerful mysterious green Grapes, thus Neko wants to open as many treasure chests as possible.
The j-th key can be used to unlock the ... | def I(): return map(int, input().split())
n, m = I()
a = sum(i % 2 for i in list(I()))
b = sum(i % 2 for i in list(I()))
print(min(a, m-b)+min(n-a, b))
| {
"input": [
"5 1\n2 4 6 8 10\n5\n",
"1 4\n10\n20 30 40 50\n",
"5 4\n9 14 6 2 11\n8 4 7 20\n"
],
"output": [
"1\n",
"0\n",
"3\n"
]
} |
243 | 9 | Recently, Tokitsukaze found an interesting game. Tokitsukaze had n items at the beginning of this game. However, she thought there were too many items, so now she wants to discard m (1 β€ m β€ n) special items of them.
These n items are marked with indices from 1 to n. In the beginning, the item with index i is placed o... | def gns():
return list(map(int,input().split()))
n,m,k=gns()
ms=gns()
ms=[x-1 for x in ms]
ans=0
if k==1:
print(m)
quit()
i=0
while i<m:
ans+=1
p=(ms[i]%k+k-i%k)%k
to=ms[i]+(k-p)-1
while i<m and ms[i]<=to:
i+=1
print(ans) | {
"input": [
"13 4 5\n7 8 9 10\n",
"10 4 5\n3 5 7 10\n"
],
"output": [
"1\n",
"3\n"
]
} |
244 | 7 | Dawid has four bags of candies. The i-th of them contains a_i candies. Also, Dawid has two friends. He wants to give each bag to one of his two friends. Is it possible to distribute the bags in such a way that each friend receives the same amount of candies in total?
Note, that you can't keep bags for yourself or thro... | def I(): return map(int, input().split())
a = sorted(list(I()))
print("YES" if a[0]+a[3] == a[1]+a[2] or a[0]+a[1]+a[2] == a[3] else "NO")
| {
"input": [
"1 7 11 5\n",
"7 3 2 5\n"
],
"output": [
"YES\n",
"NO\n"
]
} |
245 | 11 | The only difference between easy and hard versions is constraints.
Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you.
There are n voters, and two ways to convince each of them to vote for you. The first way to convince the i-th voter is to pay him p_i coins.... | import sys
import heapq as hq
readline = sys.stdin.readline
read = sys.stdin.read
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 = ni()
vot = [tu... | {
"input": [
"3\n3\n1 5\n2 10\n2 8\n7\n0 1\n3 1\n1 1\n6 1\n1 1\n4 1\n4 1\n6\n2 6\n2 3\n2 8\n2 7\n4 4\n5 5\n"
],
"output": [
"8\n0\n7\n"
]
} |
246 | 9 | The map of the capital of Berland can be viewed on the infinite coordinate plane. Each point with integer coordinates contains a building, and there are streets connecting every building to four neighbouring buildings. All streets are parallel to the coordinate axes.
The main school of the capital is located in (s_x, ... | def main():
n, sx, sy = map(int, input().split())
t, b, l, r = 0, 0, 0, 0
for i in range(n):
x, y = map(int, input().split())
if x < sx:
l += 1
if x > sx:
r += 1
if y < sy:
b += 1
if y > sy:
t += 1
a = [(l, (sx - 1, ... | {
"input": [
"7 10 12\n5 6\n20 23\n15 4\n16 5\n4 54\n12 1\n4 15\n",
"4 3 2\n1 3\n4 2\n5 1\n4 1\n",
"3 100 100\n0 0\n0 0\n100 200\n"
],
"output": [
"4\n11 12",
"3\n4 2",
"2\n99 100"
]
} |
247 | 10 | Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples:
* for the array [0, 0, 1, 0, 2] MEX equals to 3 because numbers 0, 1 and 2 are presented in the array and 3 is the minimum non-negative integer not presented in the array;
* for the array [1, 2, 3, 4] MEX eq... | import sys
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int, minp().split())
def solve():
n,x = mints()
c = [0]*x
ans = 0
for i in range(n):
y = mint()
c[y%x] += 1
while c[ans%x] > 0:
c[ans%x] -= 1
ans += 1
print(ans)
solve()
| {
"input": [
"4 3\n1\n2\n1\n2\n",
"7 3\n0\n1\n2\n2\n0\n0\n10\n"
],
"output": [
"0\n0\n0\n0\n",
"1\n2\n3\n3\n4\n4\n7\n"
]
} |
248 | 10 | VK news recommendation system daily selects interesting publications of one of n disjoint categories for each user. Each publication belongs to exactly one category. For each category i batch algorithm selects a_i publications.
The latest A/B test suggests that users are reading recommended publications more actively ... | # from collections import defaultdict
# for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
ts = list(map(int, input().split()))
tt = [(ts[i], i) for i in range(n)]
tt.sort(reverse=True)
conflict = {}
next = {}
money = 0
def get_next(i):
if i not in conflict:
return i
if next[i]... | {
"input": [
"5\n3 7 9 7 8\n5 2 5 7 5\n",
"5\n1 2 3 4 5\n1 1 1 1 1\n"
],
"output": [
"6\n",
"0\n"
]
} |
249 | 10 | Alice and Bob are playing yet another card game. This time the rules are the following. There are n cards lying in a row in front of them. The i-th card has value a_i.
First, Alice chooses a non-empty consecutive segment of cards [l; r] (l β€ r). After that Bob removes a single card j from that segment (l β€ j β€ r). Th... | def do(arr):
res = 0
for i in range(30, -31, -1):
temp = 0
for x in arr:
if x > i:
temp = 0
else:
res = max(res, temp + x - i)
temp = max(0, temp + x)
return res
input()
lst = list(map(int, input().split()))
print(do(l... | {
"input": [
"3\n-10 6 -15\n",
"8\n5 2 5 3 -30 -30 6 9\n",
"5\n5 -2 10 -1 4\n"
],
"output": [
"0\n",
"10\n",
"6\n"
]
} |
250 | 10 | Andrey's favourite number is n. Andrey's friends gave him two identical numbers n as a New Year present. He hung them on a wall and watched them adoringly.
Then Andrey got bored from looking at the same number and he started to swap digits first in one, then in the other number, then again in the first number and so o... | import itertools
def countZeroes(s):
ret = 0
for i in s:
if i != '0':
break
ret += 1
return ret
def stupid(n):
ansMax = 0
bn1 = n
bn2 = n
for n1 in itertools.permutations(n):
for n2 in itertools.permutations(n):
val = str(int(''.join(n1)) + i... | {
"input": [
"500\n",
"198\n"
],
"output": [
"500\n500",
"981\n819"
]
} |
251 | 7 | Today the kindergarten has a new group of n kids who need to be seated at the dinner table. The chairs at the table are numbered from 1 to 4n. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers a and b (a β b) will indulge if:
1. gcd(a, b) = 1 or,
2. a divides b or b d... | def fun(n):
ls=[i for i in range(2*n,4*n,2)]
print(*ls)
T = int(input())
for i in range(T):
t= int(input())
fun(t) | {
"input": [
"3\n2\n3\n4\n"
],
"output": [
"8 6\n12 10 8\n16 14 12 10\n"
]
} |
252 | 9 | You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is... | from collections import Counter
def calc(c, l):
s = 0
r = [0]*26
for i in range(26):
s += (k-c[i]%k)%k
r[i] = (k-c[i]%k)%k
if s>l:
return -1
ans = "a"*(l-s)
for i in range(26):
ans += (chr(i+97))*r[i]
return ans
for nt in range(int(input())):
n, k = map(int,input().split())
s = input()
if n%k!=0:
... | {
"input": [
"4\n4 2\nabcd\n3 1\nabc\n4 3\naaaa\n9 3\nabaabaaaa\n"
],
"output": [
"\nacac\nabc\n-1\nabaabaaab\n"
]
} |
253 | 8 | Highway 201 is the most busy street in Rockport. Traffic cars cause a lot of hindrances to races, especially when there are a lot of them. The track which passes through this highway can be divided into n sub-tracks. You are given an array a where a_i represents the number of traffic cars in the i-th sub-track. You def... | def calc():
n = int(input())
l = list(map(int, input().split(' ')))
ss = sum(l)
ans = (ss % n) * (n - (ss % n))
print(ans)
t = int(input())
for _i in range(t):
calc() | {
"input": [
"3\n3\n1 2 3\n4\n0 1 1 0\n10\n8 3 6 11 5 2 1 7 10 4\n"
],
"output": [
"0\n4\n21\n"
]
} |
254 | 9 | How to make a cake you'll never eat.
Ingredients.
* 2 carrots
* 0 calories
* 100 g chocolate spread
* 1 pack of flour
* 1 egg
Method.
1. Put calories into the mixing bowl.
2. Take carrots from refrigerator.
3. Chop carrots.
4. Take chocolate spread from refrigerator.
5. Put chocolate spread ... | # PRE-PROGRAM
inp = []
def read():
global inp
if not inp:
inp = list(map(int, input().split()))[::-1]
return inp.pop()
# INGREDIENTS
carrots = 2
calories = 0
chocolate_spread = 100 #g
pack_of_flour = 1
egg = 1
mixing_bowl = []
baking_dish = []
# METHOD
mixing_bowl.append(calories) ... | {
"input": [
"4 1 2 3 4\n"
],
"output": [
"30"
]
} |
255 | 7 | The ancient Berlanders believed that the longer the name, the more important its bearer is. Thus, Berland kings were famous for their long names. But long names are somewhat inconvenient, so the Berlanders started to abbreviate the names of their kings. They called every king by the first letters of its name. Thus, the... | from queue import PriorityQueue
from queue import Queue
import math
from collections import *
import sys
import operator as op
from functools import reduce
# sys.setrecursionlimit(10 ** 6)
MOD = int(1e9 + 7)
input = sys.stdin.readline
def ii(): return list(map(int, input().strip().split()))
def ist(): return list(inpu... | {
"input": [
"4\nvvp\nvvp\ndam\nvvp\n",
"3\nab\nc\ndef\n",
"3\nabc\nca\ncba\n"
],
"output": [
"0\n",
"1\n",
"6\n"
]
} |
256 | 7 | Vasya's bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation.
We know that the i-th star on the pedal axle has ai (0 < a1 < a2 < ... < an) teeth, and the j-th star on t... | from sys import stdin
import math
def read(): return map(int, stdin.readline().split())
read()
a = list(read())
read()
b = list(read())
magic = [ x // y for x in b for y in a if x % y == 0 ]
print(magic.count ( max(magic) ) )
| {
"input": [
"4\n1 2 3 4\n5\n10 11 12 13 14\n",
"2\n4 5\n3\n12 13 15\n"
],
"output": [
"1\n",
"2\n"
]
} |
257 | 7 | Valera had two bags of potatoes, the first of these bags contains x (x β₯ 1) potatoes, and the second β y (y β₯ 1) potatoes. Valera β very scattered boy, so the first bag of potatoes (it contains x potatoes) Valera lost. Valera remembers that the total amount of potatoes (x + y) in the two bags, firstly, was not gerater ... | def potatoes_bags(y,k,n):
s=' '.join(map(str,range(k-y%k,n-y+1,k)))
return s if s else -1
y,k,n=map(int,input().split())
print(potatoes_bags(y,k,n)) | {
"input": [
"10 6 40\n",
"10 1 10\n"
],
"output": [
"2 8 14 20 26\n",
"-1\n"
]
} |
258 | 10 | You've got a undirected graph G, consisting of n nodes. We will consider the nodes of the graph indexed by integers from 1 to n. We know that each node of graph G is connected by edges with at least k other nodes of this graph. Your task is to find in the given graph a simple cycle of length of at least k + 1.
A simpl... | def f():
n, m, k = map(int, input().split())
p = [[] for i in range(n + 1)]
for i in range(m):
a, b = map(int, input().split())
p[a].append(b)
p[b].append(a)
t, r = [0] * (n + 1), [1]
x = t[1] = 1
i = 0 - k
while True:
for y in p[x]:
if t[y] == 2: ... | {
"input": [
"4 6 3\n4 3\n1 2\n1 3\n1 4\n2 3\n2 4\n",
"3 3 2\n1 2\n2 3\n3 1\n"
],
"output": [
"4\n1 2 3 4 ",
"3\n1 2 3 "
]
} |
259 | 8 | Given a string s, determine if it contains any palindrome of length exactly 100 as a subsequence. If it has any, print any one of them. If it doesn't have any, print a palindrome that is a subsequence of s and is as long as possible.
Input
The only line of the input contains one string s of length n (1 β€ n β€ 5Β·104) c... | def p2(a):
n = len(a)
last = [[0] * 26 for _ in range(n)]
last[0][ord(a[0])-97] = 0
for i in range(1, n):
for j in range(26):
last[i][j] = last[i-1][j]
last[i][ord(a[i])-97] = i
dp = [''] * n
for i in range(n-1, -1, -1):
for j in range(n-1, i, -1):
k = last[j][ord(a[i])-97]
... | {
"input": [
"rquwmzexectvnbanemsmdufrg\n",
"bbbabcbbb\n"
],
"output": [
"rumenanemur",
"bbbcbbb"
]
} |
260 | 8 | Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.
Dima and Inna are using a secret code in their text messages. ... | def readln(): return tuple(map(int, input().split()))
n, = readln()
sms = '<3'.join([''] + [input() for _ in range(n)] + [''])
s = 0
for c in list(input()):
if sms[s] == c:
s += 1
if s == len(sms):
break
print('yes' if s == len(sms) else 'no')
| {
"input": [
"7\ni\nam\nnot\nmain\nin\nthe\nfamily\n<3i<>3am<3the<3<main<3in<3the<3><3family<3\n",
"3\ni\nlove\nyou\n<3i<3love<23you<3\n"
],
"output": [
"no\n",
"no\n"
]
} |
261 | 7 | Sereja and Dima play a game. The rules of the game are very simple. The players have n cards in a row. Each card contains a number, all numbers on the cards are distinct. The players take turns, Sereja moves first. During his turn a player can take one card: either the leftmost card in a row, or the rightmost one. The ... | n = int(input())
c = [int(x) for x in input().split()]
s, d = 0, 0
def pop():
if c[0] > c[-1]:
return c.pop(0)
else:
return c.pop()
while c:
s += pop()
if c:
d += pop()
print(s, d)
| {
"input": [
"7\n1 2 3 4 5 6 7\n",
"4\n4 1 2 10\n"
],
"output": [
"16 12\n",
"12 5\n"
]
} |
262 | 7 | It's a very unfortunate day for Volodya today. He got bad mark in algebra and was therefore forced to do some work in the kitchen, namely to cook borscht (traditional Russian soup). This should also improve his algebra skills.
According to the borscht recipe it consists of n ingredients that have to be mixed in propor... | def mi():
return map(int, input().split())
n,v = mi()
a = list(mi())
b = list(mi())
x = 100000
for i in range(n):
x = min(x,b[i]/a[i])
print (min(sum(a)*x,v)) | {
"input": [
"1 100\n1\n40\n",
"2 100\n1 1\n60 60\n",
"2 100\n1 1\n25 30\n"
],
"output": [
"40.0000",
"100.0000",
"50.0000"
]
} |
263 | 10 | We call a string good, if after merging all the consecutive equal characters, the resulting string is palindrome. For example, "aabba" is good, because after the merging step it will become "aba".
Given a string, you have to find two values:
1. the number of good substrings of even length;
2. the number of good ... | def main():
s = input()
a = [0, 0]
b = [0, 0]
res = [0, 0]
for i, c in enumerate(s, 1):
ii, ij = i%2, (i+1)%2
_a = a if c == 'a' else b
res[1] += _a[ii] + 1
res[0] += _a[ij]
_a[ii] += 1
print(*res)
if __name__ == '__main__':
main()
| {
"input": [
"babaa\n",
"bb\n",
"baab\n",
"babb\n"
],
"output": [
"2 7\n",
"1 2\n",
"2 4\n",
"2 5\n"
]
} |
264 | 9 | Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles.
Initially, each mole i (1 β€ i β€ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments com... | def r(t): t[0], t[1] = t[2] + t[3] - t[1], t[3] + t[0] - t[2]
f = [(0, 0), (1, 3), (2, 15), (3, 63)]
h = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
g = lambda u, v: (u[0] - v[0]) ** 2 + (u[1] - v[1]) ** 2
for i in range(int(input())):
p = [list(map(int, input().split())) for j in range(4)]
s = 13
for ... | {
"input": [
"4\n1 1 0 0\n-1 1 0 0\n-1 1 0 0\n1 -1 0 0\n1 1 0 0\n-2 1 0 0\n-1 1 0 0\n1 -1 0 0\n1 1 0 0\n-1 1 0 0\n-1 1 0 0\n-1 1 0 0\n2 2 0 1\n-1 0 0 -2\n3 0 0 -2\n-1 1 -2 0\n"
],
"output": [
"1\n-1\n3\n3\n"
]
} |
265 | 8 | Bob has a permutation of integers from 1 to n. Denote this permutation as p. The i-th element of p will be denoted as pi. For all pairs of distinct integers i, j between 1 and n, he wrote the number ai, j = min(pi, pj). He writes ai, i = 0 for all integer i from 1 to n.
Bob gave you all the values of ai, j that he wro... | def s():
n = int(input())
a = []
for _ in range(n):
a.append(max(map(int,input().split(' '))))
a[a.index(n-1)] = n
print(*a)
s() | {
"input": [
"2\n0 1\n1 0\n",
"5\n0 2 2 1 2\n2 0 4 1 3\n2 4 0 1 3\n1 1 1 0 1\n2 3 3 1 0\n"
],
"output": [
"1 2 ",
"2 4 5 1 3 "
]
} |
266 | 10 | A super computer has been built in the Turtle Academy of Sciences. The computer consists of nΒ·mΒ·k CPUs. The architecture was the paralellepiped of size n Γ m Γ k, split into 1 Γ 1 Γ 1 cells, each cell contains exactly one CPU. Thus, each CPU can be simultaneously identified as a group of three numbers from the layer nu... | def safe(pos):
return pos[0] >= 0 and pos[0] < n and pos[1] >= 0 and pos[1] < m and pos[2] >= 0 and pos[2] < p
def CPU_status(pos, number):
return safe(pos) and super_computer[pos[0]][pos[1]][pos[2]] == number
def critical(x,y,z):
if super_computer[x][y][z] != '0':
current = [x,y,z]
for i in... | {
"input": [
"1 1 10\n0101010101\n",
"2 2 3\n000\n000\n\n111\n111\n",
"3 3 3\n111\n111\n111\n\n111\n111\n111\n\n111\n111\n111\n"
],
"output": [
"0\n",
"2\n",
"19\n"
]
} |
267 | 7 | Tonight is brain dinner night and all zombies will gather together to scarf down some delicious brains. The artful Heidi plans to crash the party, incognito, disguised as one of them. Her objective is to get away with at least one brain, so she can analyze the zombies' mindset back home and gain a strategic advantage.
... | def zombi(n):
return (n + 1) // 2
print(zombi(int(input())))
| {
"input": [
"1\n",
"4\n"
],
"output": [
"1\n",
"2\n"
]
} |
268 | 10 | Memory and his friend Lexa are competing to get higher score in one popular computer game. Memory starts with score a and Lexa starts with score b. In a single turn, both Memory and Lexa get some integer in the range [ - k;k] (i.e. one integer among - k, - k + 1, - k + 2, ..., - 2, - 1, 0, 1, 2, ..., k - 1, k) and add... | def c(n, k):
if k > n:
return 0
a = b = 1
for i in range(n - k + 1, n + 1):
a *= i
for i in range(1, k + 1):
b *= i
return a // b
a, b, k, t = map(int, input().split())
n, m, s = 2 * k + 1, 2 * t, 2 * k * t + b - a
ans, mod = 0, 1000000007
for i in range(m + 1):
ans = (ans + [1, -1][i & 1] * c(m, i) * c(m +... | {
"input": [
"1 2 2 1\n",
"1 1 1 2\n",
"2 12 3 1\n"
],
"output": [
" 6\n",
" 31\n",
"0\n"
]
} |
269 | 9 | There was an epidemic in Monstropolis and all monsters became sick. To recover, all monsters lined up in queue for an appointment to the only doctor in the city.
Soon, monsters became hungry and began to eat each other.
One monster can eat other monster if its weight is strictly greater than the weight of the monste... | f = lambda: list(map(int, input().split()))
def g(i, j, k):
z = y = i
for x in range(i, j):
if a[x] > a[y]: y = x
if y > i: z = y
else:
while a[y] == a[i]:
y += 1
if y == j: return []
y -= 1
l = [str(k + y - x) + ' L' for x in range(i, y)]
r = [st... | {
"input": [
"5\n1 1 1 3 3\n3\n2 1 6\n",
"6\n1 2 2 2 1 2\n2\n5 5\n",
"5\n1 2 3 4 5\n1\n15\n"
],
"output": [
"NO\n",
"YES\n2 L\n1 R\n2 R\n2 R\n",
"YES\n5 L\n4 L\n3 L\n2 L\n"
]
} |
270 | 7 | In Berland each high school student is characterized by academic performance β integer value between 1 and 5.
In high school 0xFF there are two groups of pupils: the group A and the group B. Each group consists of exactly n students. An academic performance of each student is known β integer value between 1 and 5.
Th... | def calc():
sum=0
for i in range(1,6):
a1=a.count(i)
a2=b.count(i)
if((a1+a2)%2==1):
return -1
sum+=abs(a1-a2)
return (sum//4)
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
print(calc()) | {
"input": [
"6\n1 1 1 1 1 1\n5 5 5 5 5 5\n",
"4\n5 4 4 4\n5 5 4 5\n",
"9\n3 2 5 5 2 3 3 3 2\n4 1 4 1 1 2 4 4 1\n",
"1\n5\n3\n"
],
"output": [
"3\n",
"1\n",
"4\n",
"-1\n"
]
} |
271 | 11 | You are given a directed acyclic graph with n vertices and m edges. There are no self-loops or multiple edges between any pair of vertices. Graph can be disconnected.
You should assign labels to all vertices in such a way that:
* Labels form a valid permutation of length n β an integer sequence such that each integ... |
import collections
import heapq
def constructGraph(n, edges):
graph = collections.defaultdict(list)
inDegree = [0 for _ in range(n)]
for edge in edges:
a, b = edge[0]-1, edge[1]-1
graph[b].append(a)
inDegree[a] += 1
return [graph, inDegree]
def solve(n, edges):
graph, ... | {
"input": [
"3 3\n1 2\n1 3\n3 2\n",
"4 5\n3 1\n4 1\n2 3\n3 4\n2 4\n",
"5 4\n3 1\n2 1\n2 3\n4 5\n"
],
"output": [
"1 3 2\n",
"4 1 2 3\n",
"3 1 2 4 5\n"
]
} |
272 | 7 | You are given two lists of non-zero digits.
Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer?
Input
The first line contains two integers n and m (1 β€ n, m β€ 9) β the length... | def readInts(): return map(int, input().split())
input()
a = list(readInts())
b = list(readInts())
for i in range(1,10):
if ( i in a ) and ( i in b ):
print(i)
exit()
a = min(a)
b = min(b)
print ( min(a,b)*10 + max(a,b) )
| {
"input": [
"8 8\n1 2 3 4 5 6 7 8\n8 7 6 5 4 3 2 1\n",
"2 3\n4 2\n5 7 6\n"
],
"output": [
"1\n",
"25\n"
]
} |
273 | 7 | Eleven wants to choose a new name for herself. As a bunch of geeks, her friends suggested an algorithm to choose a name for her. Eleven wants her name to have exactly n characters.
<image>
Her friend suggested that her name should only consist of uppercase and lowercase letters 'O'. More precisely, they suggested th... | def main():
n = int(input())
name = ["o"] * n
b,c=1,1
while c <= n:
name[c - 1] = 'O'
b,c = c,b+c
print(''.join(name))
if __name__ == '__main__':
main() | {
"input": [
"8\n",
"15\n"
],
"output": [
"OOOoOooO\n",
"OOOoOooOooooOoo\n"
]
} |
274 | 7 | We've got no test cases. A big olympiad is coming up. But the problemsetters' number one priority should be adding another problem to the round.
The diameter of a multiset of points on the line is the largest distance between two points from this set. For example, the diameter of the multiset {1, 3, 2, 1} is 2.
Diame... | def D(a):
return a[-1]-a[0]
n,d=map(int,input().split())
a=[int(i) for i in input().split()]
a.sort()
m=1000
for i in range(0,n):
for j in range(i,n):
if D(a[i:j+1])<=d:
m=min(m,n-len(a[i:j+1]))
print(m) | {
"input": [
"6 3\n1 3 4 6 9 10\n",
"3 1\n2 1 4\n",
"3 0\n7 7 7\n"
],
"output": [
"3\n",
"1\n",
"0\n"
]
} |
275 | 10 | One department of some software company has n servers of different specifications. Servers are indexed with consecutive integers from 1 to n. Suppose that the specifications of the j-th server may be expressed with a single integer number c_j of artificial resource units.
In order for production to work, it is needed ... | def fin(c, x):
return (x + c - 1) // c
def ck(x, b):
r = (n, n)
for i in range(b, n):
r = min(r, (i + fin(c[i][0], x), i))
return r
def sol(r, l):
if r[0] <= n and l[0] <= n and r[1] < n and l[1] < n :
print("Yes")
print(r[0] - r[1], l[0]- l[1])
print(' '.join([str... | {
"input": [
"4 20 32\n21 11 11 12\n",
"4 11 32\n5 5 16 16\n",
"6 8 16\n3 5 2 9 8 7\n",
"5 12 20\n7 8 4 11 9\n"
],
"output": [
"Yes\n1 3\n1 \n2 3 4 \n",
"No\n",
"Yes\n2 2\n2 6 \n5 4 ",
"No\n"
]
} |
276 | 10 | You need to execute several tasks, each associated with number of processors it needs, and the compute power it will consume.
You have sufficient number of analog computers, each with enough processors for any task. Each computer can execute up to one task at a time, and no more than two tasks total. The first task ca... | # Codeforces Round #488 by NEAR (Div. 2)
import collections
from functools import cmp_to_key
#key=cmp_to_key(lambda x,y: 1 if x not in y else -1 )
import math
import sys
def getIntList():
return list(map(int, input().split()))
import bisect
def makePair(z):
return [(z[i], z[i+1]) for i in range(0,le... | {
"input": [
"6\n8 10 9 9 8 10\n1 1 1 1 1 1\n",
"6\n8 10 9 9 8 10\n1 10 5 5 1 10\n"
],
"output": [
"9000\n",
"1160\n"
]
} |
277 | 10 | You are given a rectangular parallelepiped with sides of positive integer lengths A, B and C.
Find the number of different groups of three integers (a, b, c) such that 1β€ aβ€ bβ€ c and parallelepiped AΓ BΓ C can be paved with parallelepipeds aΓ bΓ c. Note, that all small parallelepipeds have to be rotated in the same d... | N=100001
fac=[0 for i in range(N)]
for i in range(1,N):
for j in range(i,N,i):
fac[j]+=1
def gcd(a,b):
if a<b:
a,b=b,a
while b>0:
a,b=b,a%b
return a
def ctt(A,B,C):
la=fac[A]
lb=fac[B]
lc=fac[C]
ab=gcd(A,B)
ac=gcd(A,C)
bc=gcd(B,C)
a... | {
"input": [
"4\n1 1 1\n1 6 1\n2 2 2\n100 100 100\n"
],
"output": [
"1\n4\n4\n165\n"
]
} |
278 | 8 | When Masha came to math classes today, she saw two integer sequences of length n - 1 on the blackboard. Let's denote the elements of the first sequence as a_i (0 β€ a_i β€ 3), and the elements of the second sequence as b_i (0 β€ b_i β€ 3).
Masha became interested if or not there is an integer sequence of length n, which e... | def my(a, b, c):
for x, y in zip(a, b):
for z in range(4):
if c[-1] | z == x and c[-1] & z == y:
c += [z]
break
else:
return []
return c
n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
for i in... | {
"input": [
"4\n3 3 2\n1 2 0\n",
"3\n1 3\n3 2\n"
],
"output": [
"YES\n1 3 2 0 ",
"NO"
]
} |
279 | 8 | Initially Ildar has an empty array. He performs n steps. On each step he takes a subset of integers already added to the array and appends the mex of this subset to the array.
The mex of an multiset of integers is the smallest non-negative integer not presented in the multiset. For example, the mex of the multiset [0... | def mex(a,n):
m = -1
for i in range(n):
if a[i]-m>1:
return i+1
m = max(m,a[i])
return -1
n = int(input())
a = [*map(int,input().split())]
print(mex(a,n)) | {
"input": [
"3\n1 0 1\n",
"4\n0 1 2 1\n",
"4\n0 1 2 239\n"
],
"output": [
"1\n",
"-1\n",
"4\n"
]
} |
280 | 8 | You are given an integer number n. The following algorithm is applied to it:
1. if n = 0, then end algorithm;
2. find the smallest prime divisor d of n;
3. subtract d from n and go to step 1.
Determine the number of subtrations the algorithm will make.
Input
The only line contains a single integer n (2 β€... | def subtraction(n):
i=2
while i*i<n and n%i:
i+=1
if i*i>n:
i=n
return 1+(n-i)//2
n=int(input())
print(subtraction(n)) | {
"input": [
"4\n",
"5\n"
],
"output": [
"2\n",
"1\n"
]
} |
281 | 8 | Petr has just bought a new car. He's just arrived at the most known Petersburg's petrol station to refuel it when he suddenly discovered that the petrol tank is secured with a combination lock! The lock has a scale of 360 degrees and a pointer which initially points at zero:
<image>
Petr called his car dealer, who in... | def f(a, i):
if i == n:
return a % 360 == 0
return f(a - ls[i], i+1) or f(a + ls[i], i+1)
n = int(input())
ls = [int(input()) for _ in '0'*n]
print('YNEOS'[not f(0,0)::2]) | {
"input": [
"3\n10\n10\n10\n",
"3\n120\n120\n120\n",
"3\n10\n20\n30\n"
],
"output": [
"NO\n",
"YES\n",
"YES\n"
]
} |
282 | 9 | Let's call some square matrix with integer values in its cells palindromic if it doesn't change after the order of rows is reversed and it doesn't change after the order of columns is reversed.
For example, the following matrices are palindromic:
<image>
The following matrices are not palindromic because they change... | n = int(input())
a = list(map(int, input().split()))
d = {}
res = [[0] * n for i in range(n)]
for i in a:
d[i] = d.get(i, 0) + 1
def put(a, i, j, k, d):
d[k] -= 4
a[i][j] = k
a[n-i-1][j] = k
a[i][n-j-1] = k
a[n-i-1][n-j-1] = k
for i in range(n//2):
for j in range(n//2):
for k i... | {
"input": [
"4\n1 8 8 1 2 2 2 2 2 2 2 2 1 8 8 1\n",
"4\n1 2 1 9 8 4 3 8 8 3 4 8 9 2 1 1\n",
"1\n10\n",
"3\n1 1 1 1 1 3 3 3 3\n"
],
"output": [
"YES\n1 2 2 1 \n2 8 8 2 \n2 8 8 2 \n1 2 2 1 \n",
"NO\n",
"YES\n10 \n",
"YES\n1 3 1 \n3 1 3 \n1 3 1 \n"
]
} |
283 | 12 | You are given a connected undirected graph consisting of n vertices and m edges. There are no self-loops or multiple edges in the given graph.
You have to direct its edges in such a way that the obtained directed graph does not contain any paths of length two or greater (where the length of path is denoted as the numb... | n,m = map(int,input().split())
a = []
g = [[] for i in range(n)]
for i in range(m):
x,y = map(int,input().split())
x-=1;y-=1
g[x].append(y)
g[y].append(x)
a.append([x,y])
b = [-1]*n
b[0] = 0
que = [0]
def nibu(x):
for i in g[x]:
if b[i] != -1 and (b[i] == b[x]):
print("NO")
exit()
elif b... | {
"input": [
"6 5\n1 5\n2 1\n1 4\n3 1\n6 1\n"
],
"output": [
"YES\n10100"
]
} |
284 | 11 | You are given two arrays a and b, both of length n.
Let's define a function f(l, r) = β_{l β€ i β€ r} a_i β
b_i.
Your task is to reorder the elements (choose an arbitrary order of elements) of the array b to minimize the value of β_{1 β€ l β€ r β€ n} f(l, r). Since the answer can be very large, you have to print it modulo... | def main():
R = lambda : list(map(int,input().split()))
n = int(input())
a = sorted([num*(idx+1)*(n-idx) for idx,num in enumerate(R())])
b = sorted(R(),reverse = True)
print(sum((x*y) for x,y in zip(a,b))%998244353)
if __name__ == '__main__':
main() | {
"input": [
"1\n1000000\n1000000\n",
"5\n1 8 7 2 4\n9 7 2 9 3\n",
"2\n1 3\n4 2\n"
],
"output": [
"757402647\n",
"646\n",
"20\n"
]
} |
285 | 10 | The Third Doctor Who once correctly said that travel between parallel universes is "like travelling sideways". However, he incorrectly thought that there were infinite parallel universes, whereas in fact, as we now all know, there will never be more than 250.
Heidi recently got her hands on a multiverse observation to... | import sys
def main():
input = sys.stdin.readline()
n, k, m, t = [int(j) for j in input.split()]
for i in range(t):
input = sys.stdin.readline()
d, c = [int(j) for j in input.split()]
if d == 0:
if c<k:
n -= c
k -= c
else:
... | {
"input": [
"5 2 10 4\n0 1\n1 1\n0 4\n1 2\n"
],
"output": [
"4 1\n5 2\n4 2\n5 3\n"
]
} |
286 | 7 | There are n students standing in a circle in some order. The index of the i-th student is p_i. It is guaranteed that all indices of students are distinct integers from 1 to n (i. e. they form a permutation).
Students want to start a round dance. A clockwise round dance can be started if the student 2 comes right after... | d=lambda l,n: l[n:] + l[:n]
def k(m):
for i in range(len(m)):
if(d(m,i)==sorted(m) or d(m,i)==sorted(m,reverse=True)):
return "YES"
return "NO"
for _ in range(int(input())):
n=int(input())
f=list(map(int,input().split()))
print(k(f)) | {
"input": [
"5\n4\n1 2 3 4\n3\n1 3 2\n5\n1 2 3 5 4\n1\n1\n5\n3 2 1 5 4\n"
],
"output": [
"YES\nYES\nNO\nYES\nYES\n"
]
} |
287 | 9 | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how ... | def lucky(x):
s=str(x)
return s.count('4')+s.count('7')==len(s)
def Gen_lucky(n):
if(len(n)==1):
if(n<"4"):
return 0
if(n<"7"):
return 1
return 2
s=str(n)
if(s[0]<'4'):
return 0
if(s[0]=='4'):
return Gen_lucky(s[1:])
if(s[0]<'7... | {
"input": [
"4 7\n",
"7 4\n"
],
"output": [
"1",
"1"
]
} |
288 | 7 | Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland.... | from functools import lru_cache
N = int(input())
nums = list(map(int, input().strip().split()))
@lru_cache(None)
def dp(a, b, last):
if a < 0 or b < 0:
return N
if a == 0 and b == 0:
return 0
i = N - a - b
if nums[i] != 0:
bit = nums[i] & 1
if bit == 0:
a -=... | {
"input": [
"7\n1 0 0 5 0 0 2\n",
"5\n0 5 0 2 3\n"
],
"output": [
"1\n",
"2\n"
]
} |
289 | 8 | Now that Kuroni has reached 10 years old, he is a big boy and doesn't like arrays of integers as presents anymore. This year he wants a Bracket sequence as a Birthday present. More specifically, he wants a bracket sequence so complex that no matter how hard he tries, he will not be able to remove a simple subsequence!
... | import sys
input = sys.stdin.readline
def main():
al = list(input())
al.pop()
n = len(al)
ans = [False]*n
ok = 0
i,j = 0,n-1
while i<j:
while i<n and al[i]==')':
i+=1
while j>=0 and al[j]=='(':
j-=1
if i<j and al[i]=='(' and al[j]==')':
ans[i] = ans[j] = True
ok+=2
i+=1
j-=1
print(1... | {
"input": [
"(()((\n",
"(()())\n",
")(\n"
],
"output": [
"1\n2\n1 3 ",
"1\n4\n1 2 5 6 ",
"0"
]
} |
290 | 10 | Slime and his n friends are at a party. Slime has designed a game for his friends to play.
At the beginning of the game, the i-th player has a_i biscuits. At each second, Slime will choose a biscuit randomly uniformly among all a_1 + a_2 + β¦ + a_n biscuits, and the owner of this biscuit will give it to a random unifor... | MOD = 998244353
n = int(input())
a = list(map(int, input().split()))
tot = sum(a)
def inv(x):
return pow(x, MOD - 2, MOD)
l = [0, pow(n, tot, MOD) - 1]
for i in range(1, tot):
aC = i
cC = (n - 1) * (tot - i)
curr = (aC + cC) * l[-1]
curr -= tot * (n - 1)
curr -= aC * l[-2]
curr *= inv(c... | {
"input": [
"2\n1 2\n",
"5\n8 4 2 0 1\n",
"2\n1 1\n",
"5\n0 0 0 0 35\n"
],
"output": [
"3\n",
"801604029\n",
"1\n",
"0\n"
]
} |
291 | 8 | Little Petya very much likes computers. Recently he has received a new "Ternatron IV" as a gift from his mother. Unlike other modern computers, "Ternatron IV" operates with ternary and not binary logic. Petya immediately wondered how the xor operation is performed on this computer (and whether there is anything like it... | def f(l):
a,c = l
p = 1
r = 0
while a>0 or c>0:
r += p*((c%3-a%3)%3)
p *= 3
c = c//3
a = a//3
return r
l = list(map(int,input().split()))
print(f(l))
| {
"input": [
"387420489 225159023\n",
"14 34\n",
"50 34\n",
"5 5\n"
],
"output": [
"1000000001\n",
"50\n",
"14\n",
"0\n"
]
} |
292 | 8 | Being stuck at home, Ray became extremely bored. To pass time, he asks Lord Omkar to use his time bending power: Infinity Clock! However, Lord Omkar will only listen to mortals who can solve the following problem:
You are given an array a of n integers. You are also given an integer k. Lord Omkar wants you to do k ope... | def iter(a):
m = max(a)
return [m-a for a in a]
for _ in range(int(input())):
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
print(*iter(a)) if k%2 else print(*iter(iter(a))) | {
"input": [
"3\n2 1\n-199 192\n5 19\n5 -1 4 2 0\n1 2\n69\n"
],
"output": [
"391 0 \n0 6 1 3 5 \n0 \n"
]
} |
293 | 9 | There are n piranhas with sizes a_1, a_2, β¦, a_n in the aquarium. Piranhas are numbered from left to right in order they live in the aquarium.
Scientists of the Berland State University want to find if there is dominant piranha in the aquarium. The piranha is called dominant if it can eat all the other piranhas in the... | def f():
input()
s = list(map(int, input().split()))
i = s.index(max(s)) + 1
if i > 1:
return i
while i < len(s):
if s[i - 1] != s[i]:
return i
i += 1
return -1
for i in range(int(input())):
print(f()) | {
"input": [
"6\n5\n5 3 4 4 5\n3\n1 1 1\n5\n4 4 3 4 4\n5\n5 5 4 3 2\n3\n1 1 2\n5\n5 4 3 5 5\n"
],
"output": [
"1\n-1\n2\n2\n3\n1\n"
]
} |
294 | 8 | There are n glasses on the table numbered 1, β¦, n. The glass i can hold up to a_i units of water, and currently contains b_i units of water.
You would like to choose k glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, ... | import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n=int(input())
AB=[tuple(map(int,input().split())) for i in range(n)]
SUMB=0
for a,b in AB:
SUMB+=b
def calc(suma,sumb):
return min(suma,(SUMB+sumb)/2)
DP=[[-1]*10005 for i in range(n+1)]
DP[0][0]=0
for i in range(n):
... | {
"input": [
"3\n6 5\n6 5\n10 2\n"
],
"output": [
"\n7.0000000000 11.0000000000 12.0000000000\n"
]
} |
295 | 8 | After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you).
<image>
You are given an array h_1, h_2, ..., ... | def process(h):
for i in range(1, len(h)):
if h[i] > h[i-1]:
h[i-1] += 1
return i-1
return -1
for _ in range(int(input())):
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
for j in range(k):
res = process(h)
if res == -1:
print(-1)
break
else:
print(res+1) | {
"input": [
"4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1\n"
],
"output": [
"\n2\n1\n-1\n-1\n"
]
} |
296 | 9 | As a teacher, Riko Hakozaki often needs to help her students with problems from various subjects. Today, she is asked a programming task which goes as follows.
You are given an undirected complete graph with n nodes, where some edges are pre-assigned with a positive weight while the rest aren't. You need to assign all... | import sys, os
if os.environ['USERNAME']=='kissz':
inp=open('in3.txt','r').readline
def debug(*args):
print(*args,file=sys.stderr)
else:
inp=sys.stdin.readline
def debug(*args):
pass
# SCRIPT STARTS HERE
def getp(i):
L=[]
while parent[i]>=0:
L+=[i]
i=parent... | {
"input": [
"4 4\n2 1 14\n1 4 14\n3 2 15\n4 3 8\n",
"5 6\n2 3 11\n5 3 7\n1 4 10\n2 4 14\n4 3 8\n2 5 6\n",
"6 6\n3 6 4\n2 4 1\n4 5 7\n3 4 10\n3 5 1\n5 2 15\n"
],
"output": [
"\n15\n",
"\n6\n",
"\n0\n"
]
} |
297 | 10 | This is an interactive problem.
Little Dormi was faced with an awkward problem at the carnival: he has to guess the edges of an unweighted tree of n nodes! The nodes of the tree are numbered from 1 to n.
The game master only allows him to ask one type of question:
* Little Dormi picks a node r (1 β€ r β€ n), and the... | import sys
input = sys.stdin.readline
flush = sys.stdout.flush
def query(x):
print('?', x)
flush()
return list(map(int, input().split()))
n = int(input())
ans = set()
A = query(1)
X, Y = [], []
for i, a in enumerate(A, 1):
if not a % 2: X.append(i)
else: Y.append(i)
if len(X) > len(Y): X, Y = Y, X... | {
"input": [
"4\n\n0 1 2 2\n\n1 0 1 1",
"5\n\n2 2 1 1 0\n"
],
"output": [
"\n? 1\n\n? 2\n\n!\n4 2\n1 2\n2 3\n",
"\n? 5\n\n!\n4 5\n3 5\n2 4\n1 3\n"
]
} |
298 | 8 | You are given n points on a plane. All points are different.
Find the number of different groups of three points (A, B, C) such that point B is the middle of segment AC.
The groups of three points are considered unordered, that is, if point B is the middle of segment AC, then groups (A, B, C) and (C, B, A) are consi... | import sys
import fractions
def solve():
n , res = int(input()), 0
xs, ys = list(), list()
mem = set()
for i in range(n):
x, y = map(int, input().split())
xs.append(x)
ys.append(y)
mem.add((x, y))
for i in range(n):
for j in range(i + 1, n):
sumx, ... | {
"input": [
"3\n0 0\n-1 0\n0 1\n",
"3\n1 1\n2 2\n3 3\n"
],
"output": [
"0\n",
"1\n"
]
} |
299 | 7 | There are n boys and m girls studying in the class. They should stand in a line so that boys and girls alternated there as much as possible. Let's assume that positions in the line are indexed from left to right by numbers from 1 to n + m. Then the number of integers i (1 β€ i < n + m) such that positions with indexes i... | import sys,os,io,time,copy,math
from functools import lru_cache
if os.path.exists('input.txt'):
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
def main():
#n=int(input())
#arr=list(map(int,input().split()))
n,m=map(int,input().split())
if n<m:
for i in range(n... | {
"input": [
"4 2\n",
"3 3\n"
],
"output": [
"BGBGBB\n",
"GBGBGB\n"
]
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.