context
stringlengths
88
7.54k
groundtruth
stringlengths
9
28.8k
groundtruth_language
stringclasses
3 values
type
stringclasses
2 values
code_test_cases
listlengths
1
565
dataset
stringclasses
6 values
code_language
stringclasses
1 value
difficulty
float64
0
1
mid
stringlengths
32
32
Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum o...
n = int(input()) a = list(map(int, input().split())) s = set() for i in a: j = i while j in s: s.remove(j) j += 1 s.add(j) print(max(s)-len(s)+1)
python
code_algorithm
[ { "input": "1\n3\n", "output": "3\n" }, { "input": "4\n0 1 1 1\n", "output": "0\n" }, { "input": "1\n2000000000\n", "output": "2000000000\n" }, { "input": "26\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2\n", "output": "5\n" }, { "input": "1\n0\n", "out...
code_contests
python
0
996972fd97f33ca42cebd7a646799e84
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up. For another exercise, Pasha needs exactly <image> hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster e...
n = int(input()) s = str(input()) half = n//2 sit = s.count('x') stand = s.count('X') ans = max(sit, stand) - half t = ans while ans!=0: if sit>stand: temp = s.index('x') s = s[:temp] + 'X' + s[temp+1:] sit = sit-1 stand = stand +1 ans = ans -1 else: temp = s.inde...
python
code_algorithm
[ { "input": "2\nXX\n", "output": "1\nxX\n" }, { "input": "6\nxXXxXx\n", "output": "0\nxXXxXx\n" }, { "input": "4\nxxXx\n", "output": "1\nXxXx\n" }, { "input": "4\nXXXX\n", "output": "2\nxxXX\n" }, { "input": "200\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
code_contests
python
0.2
339099d9184a0c98c5f75d2adf924c86
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a. Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen w...
n,m = map(int, input().split()) p = [m+int(m<n),m-int(m>1)] print(p[n-m<=m-1])
python
code_algorithm
[ { "input": "3 1\n", "output": "2\n" }, { "input": "4 3\n", "output": "2\n" }, { "input": "692501734 346250868\n", "output": "346250867\n" }, { "input": "616075979 207073797\n", "output": "207073798\n" }, { "input": "296647497 148323750\n", "output": "148323749...
code_contests
python
0.1
fe15a7752a1eccadbf3736e7ac220543
After their adventure with the magic mirror Kay and Gerda have returned home and sometimes give free ice cream to kids in the summer. At the start of the day they have x ice cream packs. Since the ice cream is free, people start standing in the queue before Kay and Gerda's house even in the night. Each person in the q...
x, y = input().split(" ") a = x,y a = list(a) for i in range(len(a)): a[i]=int(a[i]) count=0 num =a[1] for i in range(a[0]): z,f = input().split(" ") char=z inp=int(f) if char == '+': num = num + inp elif char == '-': num = num - inp if num < 0: count+=1 ...
python
code_algorithm
[ { "input": "5 7\n+ 5\n- 10\n- 20\n+ 40\n- 20\n", "output": "22 1\n" }, { "input": "5 17\n- 16\n- 2\n- 98\n+ 100\n- 98\n", "output": "3 2\n" }, { "input": "6 1000000000\n+ 1000000000\n+ 1000000000\n+ 1000000000\n+ 1000000000\n+ 1000000000\n+ 1000000000\n", "output": "7000000000 0\n" ...
code_contests
python
1
0055c0522279c13cae624545d4cc1b2a
Stepan has n pens. Every day he uses them, and on the i-th day he uses the pen number i. On the (n + 1)-th day again he uses the pen number 1, on the (n + 2)-th — he uses the pen number 2 and so on. On every working day (from Monday to Saturday, inclusive) Stepan spends exactly 1 milliliter of ink of the pen he uses t...
import sys def Min(x, y): if x > y: return y else: return x def Gcd(x, y): if x == 0: return y else: return Gcd(y % x, x) def Lcm(x, y): return x * y // Gcd(x, y) n = int(input()) a = [int(i) for i in input().split()] d = [int(0) for i in range(0, n)] ok = 0 cur...
python
code_algorithm
[ { "input": "5\n5 4 5 4 4\n", "output": "5\n" }, { "input": "3\n3 3 3\n", "output": "2\n" }, { "input": "2\n1000000000 1000000000\n", "output": "2\n" }, { "input": "3\n999999999 1000000000 1000000000\n", "output": "1\n" }, { "input": "4\n1000000000 1000000000 10000...
code_contests
python
0
4c7060b585c5655435b870308d0319f3
Polycarp thinks about the meaning of life very often. He does this constantly, even when typing in the editor. Every time he starts brooding he can no longer fully concentrate and repeatedly presses the keys that need to be pressed only once. For example, instead of the phrase "how are you" he can type "hhoow aaaare yy...
from collections import deque def plugin(s): # stack for characters to check string_stack = deque() # input string in a list arr_s = [c for c in s] # for each character, append to stack. if for c in arr_s: string_stack.append(c) if len(string_stack) > 1: if string...
python
code_algorithm
[ { "input": "hhoowaaaareyyoouu\n", "output": "wre\n" }, { "input": "reallazy\n", "output": "rezy\n" }, { "input": "abacabaabacabaa\n", "output": "a\n" }, { "input": "xraccabccbry\n", "output": "xy\n" }, { "input": "abb\n", "output": "a\n" }, { "input": ...
code_contests
python
0.8
caa1ed040d2fcc4be110524e1ce553cd
Polycarp has just attempted to pass the driving test. He ran over the straight road with the signs of four types. * speed limit: this sign comes with a positive integer number — maximal speed of the car after the sign (cancel the action of the previous sign of this type); * overtake is allowed: this sign means th...
n = int(input()) events = [] for i in range(n): events.append(list(map(lambda s: int(s), input().split()))) max_speeds = [] overtaking_forbidden = 0 current_speed = 0 ignores = 0 for event in events: if event[0] == 1: current_speed = event[1] while max_speeds and current_speed > max_speeds...
python
code_algorithm
[ { "input": "7\n1 20\n2\n6\n4\n6\n6\n2\n", "output": "2\n" }, { "input": "11\n1 100\n3 70\n4\n2\n3 120\n5\n3 120\n6\n1 150\n4\n3 300\n", "output": "2\n" }, { "input": "5\n1 100\n3 200\n2\n4\n5\n", "output": "0\n" }, { "input": "10\n1 37\n6\n5\n2\n5\n6\n5\n2\n4\n2\n", "outp...
code_contests
python
0
bf5ab34a8cf1cdb6f0b5d2a8f0505612
Given an array a1, a2, ..., an of n integers, find the largest number in the array that is not a perfect square. A number x is said to be a perfect square if there exists an integer y such that x = y2. Input The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array. The second ...
n = int(input()) a = [int(i) for i in input().split()] p = [] i = 0 while(True): p.append(i*i) i += 1 if(p[-1] > 1000000): break m = -99999999 for i in a: if i not in p: m = max(i,m) print(m)
python
code_algorithm
[ { "input": "2\n4 2\n", "output": "2\n" }, { "input": "8\n1 2 4 8 16 32 64 576\n", "output": "32\n" }, { "input": "2\n370881 659345\n", "output": "659345\n" }, { "input": "5\n804610 765625 2916 381050 93025\n", "output": "804610\n" }, { "input": "1\n-1000000\n", ...
code_contests
python
0.5
aebf843dbae055f32c4a67c9e51e24e1
Polycarp has created his own training plan to prepare for the programming contests. He will train for n days, all days are numbered from 1 to n, beginning from the first. On the i-th day Polycarp will necessarily solve a_i problems. One evening Polycarp plans to celebrate the equator. He will celebrate it on the first...
n=int(input()) l=list(map(int,input().split())) s=sum(l)/2 #print(s) i=0; curr=0 while(curr<s): curr+=l[i] i+=1 print(i)
python
code_algorithm
[ { "input": "6\n2 2 2 2 2 2\n", "output": "3\n" }, { "input": "4\n1 3 2 1\n", "output": "2\n" }, { "input": "6\n1 1 1 1 1 2\n", "output": "4\n" }, { "input": "2\n1 3\n", "output": "2\n" }, { "input": "6\n3 3 3 2 4 4\n", "output": "4\n" }, { "input": "2\...
code_contests
python
0.1
4a1f0ab0f2926d0c4943c210293a177b
This is the modification of the problem used during the official round. Unfortunately, author's solution of the original problem appeared wrong, so the problem was changed specially for the archive. Once upon a time in a far away kingdom lived the King. The King had a beautiful daughter, Victoria. They lived happily, ...
from fractions import * n,L=int(input()),0 while (n%2==0):n,L=n//2,L+1 if (n==1):print('%d/1'%L) else: s,t=1,1 for i in range(n): t=t*2%n s*=2 if (t==1): m=i+1 break r,t,i,ans=s,s*n,L,0 while (r>1): i,t=i+1,t//2 if (r-t>0): r,an...
python
code_algorithm
[ { "input": "2\n", "output": "1/1\n" }, { "input": "3\n", "output": "8/3\n" }, { "input": "4\n", "output": "2/1\n" }, { "input": "9556\n", "output": "611056105825961880640136329760182381695948472631241546423866895948076970978854470645702077030837973752292864919411644376894...
code_contests
python
0
ae860349d677eed84649f6f7498685a6
Bob is an active user of the social network Faithbug. On this network, people are able to engage in a mutual friendship. That is, if a is a friend of b, then b is also a friend of a. Each user thus has a non-negative amount of friends. This morning, somebody anonymously sent Bob the following link: [graph realization ...
def main(): n=int(input()) a=list(map(int,input().split())) a.sort(reverse=True) mod=sum(a)%2 counts=[0]*(n+1) for guy in a: counts[guy]+=1 cumcounts=[counts[0]] for i in range(n): cumcounts.append(cumcounts[-1]+counts[i+1]) partialsums=[0] curr=0 for i in ran...
python
code_algorithm
[ { "input": "2\n0 2\n", "output": "-1\n" }, { "input": "4\n1 1 1 1\n", "output": "0 2 4 \n" }, { "input": "35\n21 26 18 4 28 2 15 13 16 25 6 32 11 5 31 17 9 3 24 33 14 27 29 1 20 4 12 7 10 30 34 8 19 23 22\n", "output": "13 15 17 19 21 \n" }, { "input": "3\n3 3 3\n", "outp...
code_contests
python
0
3b223bd5519ea2ade6b5c2a7eda55c2c
Recently Vasya decided to improve his pistol shooting skills. Today his coach offered him the following exercise. He placed n cans in a row on a table. Cans are numbered from left to right from 1 to n. Vasya has to knock down each can exactly once to finish the exercise. He is allowed to choose the order in which he wi...
n = map(int, input().split()) A = list(map(int, input().split())) la = [] ind = 1 for a in A: la.append((a, ind)) ind += 1 la.sort(key=lambda x: x[0], reverse=True) # print(la) x = 0 sm = 0 li = [] for tp in la: a = tp[0] sm += (a * x + 1) x += 1 li.append(tp[1]) print(sm) print(" ".join(str(i) for i in li)...
python
code_algorithm
[ { "input": "3\n20 10 20\n", "output": "43\n1 3 2\n" }, { "input": "2\n1 4\n", "output": "3\n2 1\n" }, { "input": "4\n10 10 10 10\n", "output": "64\n1 2 3 4\n" }, { "input": "6\n5 4 5 4 4 5\n", "output": "69\n1 3 6 2 4 5\n" }, { "input": "5\n13 16 20 18 11\n", ...
code_contests
python
0.3
cfc4279b3b7b42fbb0c401a86e2a48ba
In the snake exhibition, there are n rooms (numbered 0 to n - 1) arranged in a circle, with a snake in each room. The rooms are connected by n conveyor belts, and the i-th conveyor belt connects the rooms i and (i+1) mod n. In the other words, rooms 0 and 1, 1 and 2, …, n-2 and n-1, n-1 and 0 are connected with conveyo...
from sys import stdin import sys tt = int(stdin.readline()) for loop in range(tt): n = int(stdin.readline()) s = stdin.readline()[:-1] if ("<" not in s) or (">" not in s): print (n) continue ans = 0 for i in range(n): if s[(i-1)%n] == "-" or s[i] == "-": ans...
python
code_algorithm
[ { "input": "4\n4\n-&gt;&lt;-\n5\n&gt;&gt;&gt;&gt;&gt;\n3\n&lt;--\n2\n&lt;&gt;\n", "output": "4\n5\n3\n2\n" }, { "input": "1\n6\n->>-<-\n", "output": "5\n" }, { "input": "1\n7\n->>-<<-\n", "output": "5\n" }, { "input": "1\n15\n--->>>---<<<---\n", "output": "11\n" }, { ...
code_contests
python
0
c0d507c3e08cff7911ae41301530b6f7
Bob likes to draw camels: with a single hump, two humps, three humps, etc. He draws a camel by connecting points on a coordinate plane. Now he's drawing camels with t humps, representing them as polylines in the plane. Each polyline consists of n vertices with coordinates (x1, y1), (x2, y2), ..., (xn, yn). The first ve...
import sys from array import array # noqa: F401 def input(): return sys.stdin.buffer.readline().decode('utf-8') n, t = map(int, input().split()) dp = [[[0] * 5 for _ in range(2 * t + 1)] for _ in range(n)] dp[0][0] = [0] + [1] * 4 for i in range(n - 1): for j in range(min(2 * t, i + 1)): if (j & ...
python
code_algorithm
[ { "input": "6 1\n", "output": "6\n" }, { "input": "4 2\n", "output": "0\n" }, { "input": "19 10\n", "output": "0\n" }, { "input": "19 4\n", "output": "32632\n" }, { "input": "20 9\n", "output": "90700276\n" }, { "input": "19 7\n", "output": "197939...
code_contests
python
0
7fe7d3f69872521814978696dcb00389
As Sherlock Holmes was investigating another crime, he found a certain number of clues. Also, he has already found direct links between some of those clues. The direct links between the clues are mutual. That is, the direct link between clues A and B and the direct link between clues B and A is the same thing. No more ...
def dfs(node, my_cc): vis[node] = True acc[my_cc]+=1 for i in adj[node]: if not vis[i]: dfs(i, my_cc) def ittDfs(node): queue = [node] curr = 0 while(queue): node = queue.pop() if vis[node]: continue vis[node] = True acc[cc] += 1 ...
python
code_algorithm
[ { "input": "4 1 1000000000\n1 4\n", "output": "8\n" }, { "input": "3 0 100\n", "output": "3\n" }, { "input": "2 0 1000000000\n", "output": "1\n" }, { "input": "100000 0 1\n", "output": "0\n" }, { "input": "2 1 100000\n1 2\n", "output": "1\n" }, { "inpu...
code_contests
python
0
e00bad78ec8adf5678d25c0908df59c2
Fibonacci strings are defined as follows: * f1 = «a» * f2 = «b» * fn = fn - 1 fn - 2, n > 2 Thus, the first five Fibonacci strings are: "a", "b", "ba", "bab", "babba". You are given a Fibonacci string and m strings si. For each string si, find the number of times it occurs in the given Fibonacci string as...
F = ['', 'a', 'b', 'ba', 'bab', 'babba', 'babbabab', 'babbababbabba', 'babbababbabbababbabab', 'babbababbabbababbababbabbababbabba', 'babbababbabbababbababbabbababbabbababbababbabbababbabab', 'babbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabba', 'babbababbabbababbababbabbababbabba...
python
code_algorithm
[ { "input": "6 5\na\nb\nab\nba\naba\n", "output": "3\n5\n3\n3\n1\n" }, { "input": "50 100\nbb\naa\nb\nbaa\nbbba\naa\nba\na\nabba\nbaa\naa\naab\nab\nbabb\naabb\nbaa\nbaaa\nbaa\naab\nbba\nbb\naba\naaba\nbab\naaba\naa\naaaa\nbabb\nbbb\naaba\naaa\nab\nbab\nb\nb\naa\naaab\naa\nbba\nbaa\nbabb\nbaba\nba\naa...
code_contests
python
0
7af590908b9cbdb72cb0a94bdf32e733
The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank. One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible. Vasya ha...
#!/usr/bin/python3 def readln(): return tuple(map(int, input().split())) n, = readln() d = readln() a, b = readln() print(sum(d[a - 1:b - 1]))
python
code_algorithm
[ { "input": "3\n5 6\n1 2\n", "output": "5\n" }, { "input": "3\n5 6\n1 3\n", "output": "11\n" }, { "input": "80\n65 15 43 6 43 98 100 16 69 98 4 54 25 40 2 35 12 23 38 29 10 89 30 6 4 8 7 96 64 43 11 49 89 38 20 59 54 85 46 16 16 89 60 54 28 37 32 34 67 9 78 30 50 87 58 53 99 48 77 3 5 6 1...
code_contests
python
0.8
5cede7b7b9a4e8304ab941a49d78322f
Mr. Kitayuta has kindly given you a string s consisting of lowercase English letters. You are asked to insert exactly n lowercase English letters into s to make it a palindrome. (A palindrome is a string that reads the same forward and backward. For example, "noon", "testset" and "a" are all palindromes, while "test" a...
palindrom = lambda s: s == s[::-1] printans = lambda l: print(''.join(l)) s = list(input()) for i in range(len(s)+1): for letter in 'abcdefghijklmnopqrstvwuxyz': tmp = s[:] tmp.insert(i,letter) if palindrom(tmp): printans(tmp) exit() print('NA')
python
code_algorithm
[ { "input": "add\n2\n", "output": "adda\n" }, { "input": "revive\n1\n", "output": "reviver\n" }, { "input": "noon\n5\n", "output": "nooon\n" }, { "input": "lsdijfjisl\n1\n", "output": "lsdijfjidsl\n" }, { "input": "lsdijfjisl\n209\n", "output": "lsdijfjidsl\n" ...
code_contests
python
0
45697781ec0fe983ff23e8b25e0a4e15
Catherine has a deck of n cards, each of which is either red, green, or blue. As long as there are at least two cards left, she can do one of two actions: * take any two (not necessarily adjacent) cards with different colors and exchange them for a new card of the third color; * take any two (not necessarily adj...
''' Author : Md. Rezwanul Haque Email : r.haque.249.rh@gmail.com ''' import sys from sys import stdout,stdin input = lambda : sys.stdin.readline() if __name__ == '__main__': n = int(input()) s = input() s = (s.count('B'), s.count('G'), s.count('R')) if s[0] > 0 and s[1] > 0 and s[2] > 0: stdo...
python
code_algorithm
[ { "input": "2\nRB\n", "output": "G" }, { "input": "5\nBBBBB\n", "output": "B" }, { "input": "3\nGRG\n", "output": "BR" }, { "input": "2\nRG\n", "output": "B" }, { "input": "6\nGRRGBB\n", "output": "BGR" }, { "input": "1\nB\n", "output": "B" }, ...
code_contests
python
0
91e5a36ca3a1b63c15fcd6615152b01f
Note that girls in Arpa’s land are really attractive. Arpa loves overnight parties. In the middle of one of these parties Mehrdad suddenly appeared. He saw n pairs of friends sitting around a table. i-th pair consisted of a boy, sitting on the ai-th chair, and his girlfriend, sitting on the bi-th chair. The chairs wer...
import sys def solve(): n = int(input()) partner = [0]*(2*n) pacani = [] for line in sys.stdin: pacan, telka = [int(x) - 1 for x in line.split()] partner[pacan] = telka partner[telka] = pacan pacani.append(pacan) khavka = [None]*(2*n) for i in range(2*n): ...
python
code_algorithm
[ { "input": "3\n1 4\n2 5\n3 6\n", "output": "1 2\n2 1\n1 2\n" }, { "input": "6\n2 11\n7 1\n12 8\n4 10\n3 9\n5 6\n", "output": "2 1\n2 1\n2 1\n2 1\n1 2\n1 2\n" }, { "input": "26\n8 10\n52 21\n2 33\n18 34\n30 51\n5 19\n22 32\n36 28\n42 16\n13 49\n11 17\n31 39\n43 37\n50 15\n29 20\n35 46\n47...
code_contests
python
0
d250f761225252389c40828de1c11073
Kostya likes Codeforces contests very much. However, he is very disappointed that his solutions are frequently hacked. That's why he decided to obfuscate (intentionally make less readable) his code before upcoming contest. To obfuscate the code, Kostya first looks at the first variable name used in his program and rep...
Alphabet = "abcdefghijklmnopqrstuvwxyz" X = input() Checked = [] i, Index = 0, 0 while i < len(X): if X[i] not in Checked and X[i] == Alphabet[Index]: Checked.append(Alphabet[Index]) Index += 1 elif X[i] not in Checked and X[i] != Alphabet[Index]: print("NO") exit() i += 1 pr...
python
code_algorithm
[ { "input": "jinotega\n", "output": "NO\n" }, { "input": "abacaba\n", "output": "YES\n" }, { "input": "ac\n", "output": "NO\n" }, { "input": "za\n", "output": "NO\n" }, { "input": "bab\n", "output": "NO\n" }, { "input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
code_contests
python
0
9e7c029b37a7bf51072ebca14c91e72e
A prime number is a number which has exactly two distinct divisors: one and itself. For example, numbers 2, 7, 3 are prime, and 1, 6, 4 are not. The next prime number after x is the smallest prime number greater than x. For example, the next prime number after 2 is 3, and the next prime number after 3 is 5. Note that ...
from math import ceil, sqrt def array(arr, struc): return (list(map(struc, arr.split()))) def isPrime(x): for i in range(2, ceil(sqrt(x))+1): if x % i == 0: return False return True arr = array(input(), int) prime1 = arr[0] prime2 = arr[1] counter = 0 tmp = prime1 + 1 while tmp ...
python
code_algorithm
[ { "input": "7 9\n", "output": "NO\n" }, { "input": "3 5\n", "output": "YES\n" }, { "input": "7 11\n", "output": "YES\n" }, { "input": "2 6\n", "output": "NO\n" }, { "input": "31 33\n", "output": "NO\n" }, { "input": "2 11\n", "output": "NO\n" }, ...
code_contests
python
0.9
609a4ac0b42beb6d33f8abba2fb77a98
Vova is again playing some computer game, now an RPG. In the game Vova's character received a quest: to slay the fearsome monster called Modcrab. After two hours of playing the game Vova has tracked the monster and analyzed its tactics. The Modcrab has h2 health points and an attack power of a2. Knowing that, Vova has...
class Character: def __init__(self, hPoints, attPoints, healPoints): self.hPoints = hPoints self.attPoints = attPoints self.healPoints = healPoints def attack(self, boss): boss.setHP(boss.getHP() - self.attPoints) def recAttack(self, boss): self.hPoints -= boss.getAP(...
python
code_algorithm
[ { "input": "10 6 100\n17 5\n", "output": "4\nSTRIKE\nHEAL\nSTRIKE\nSTRIKE\n" }, { "input": "11 6 100\n12 5\n", "output": "2\nSTRIKE\nSTRIKE\n" }, { "input": "6 6 100\n12 5\n", "output": "2\nSTRIKE\nSTRIKE\n" }, { "input": "79 4 68\n9 65\n", "output": "21\nSTRIKE\nHEAL\nHE...
code_contests
python
0
1228f1b40aa0623c6061db8e6496128a
Little town Nsk consists of n junctions connected by m bidirectional roads. Each road connects two distinct junctions and no two roads connect the same pair of junctions. It is possible to get from any junction to any other junction by these roads. The distance between two junctions is equal to the minimum possible num...
from collections import deque def bfs(s, graph): q = deque() d = [0] * len(graph) used = [False] * len(graph) used[s] = True q.append(s) while len(q): cur = q[0] q.popleft() for to in graph[cur]: if not used[to]: used[to] = True ...
python
code_algorithm
[ { "input": "5 4 3 5\n1 2\n2 3\n3 4\n4 5\n", "output": "5\n" }, { "input": "5 6 1 5\n1 2\n1 3\n1 4\n4 5\n3 5\n2 5\n", "output": "3\n" }, { "input": "5 4 1 5\n1 2\n2 3\n3 4\n4 5\n", "output": "0\n" }, { "input": "3 3 2 3\n1 2\n2 3\n1 3\n", "output": "0\n" }, { "inpu...
code_contests
python
1
32a2ca06543d202e6ca78665eeb932c6
One beautiful July morning a terrible thing happened in Mainframe: a mean virus Megabyte somehow got access to the memory of his not less mean sister Hexadecimal. He loaded there a huge amount of n different natural numbers from 1 to n to obtain total control over her energy. But his plan failed. The reason for this w...
n=int(input()) a=[] c=int for i in range(515): a.append(c(bin(i)[2:])) a.remove(0) ans=0 for i in a: if i<=n: ans+=1 print(ans)
python
code_algorithm
[ { "input": "10\n", "output": "2\n" }, { "input": "100\n", "output": "4\n" }, { "input": "1010011\n", "output": "83\n" }, { "input": "1\n", "output": "1\n" }, { "input": "999999999\n", "output": "511\n" }, { "input": "112\n", "output": "7\n" }, ...
code_contests
python
0
40eb36ac733090ffdcb0ce16d6d00984
As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon. Elections are coming. You know the number of voters and the number of parties — n and m respectively. For each vote...
n, m = map(int, input().split()) pc = [(0, 0) for _ in range(n)] party_votes = [0 for _ in range(m)] for i in range(n): p, c = map(int, input().split()) pc[i] = (p - 1, c) party_votes[p - 1] += 1 pc.sort(key=lambda x: x[1]) min_cost = 10**20 for votes in range(n + 1): _party_votes = party_votes[:]...
python
code_algorithm
[ { "input": "1 2\n1 100\n", "output": "0\n" }, { "input": "5 5\n2 100\n3 200\n4 300\n5 800\n5 900\n", "output": "600\n" }, { "input": "5 5\n2 100\n3 200\n4 300\n5 400\n5 900\n", "output": "500\n" }, { "input": "5 5\n2 5\n2 4\n2 1\n3 6\n3 7\n", "output": "10\n" }, { ...
code_contests
python
0
5dba697fcb6f735cb0d534feef84e067
You are given a tube which is reflective inside represented as two non-coinciding, but parallel to Ox lines. Each line has some special integer points — positions of sensors on sides of the tube. You are going to emit a laser ray in the tube. To do so, you have to choose two integer points A and B on the first and the...
n, y1 = map(int, input().split()) a = list(map(int, input().split())) m, y2 = map(int, input().split()) b = list(map(int, input().split())) a_st, b_st = dict(), dict() osn = 2 ** 30 k_a, k_b = set(), set() for el in a: try: a_st[el % osn] += 1 except KeyError: a_st[el % osn] = 1 ...
python
code_algorithm
[ { "input": "3 1\n1 5 6\n1 3\n3\n", "output": "3\n" }, { "input": "6 94192\n0 134217728 268435456 402653184 536870912 671088640\n6 435192\n67108864 201326592 335544320 469762048 603979776 738197504\n", "output": "12\n" }, { "input": "8 896753688\n106089702 120543561 161218905 447312211 76...
code_contests
python
0
5af360f095c88bb8bfc66dfa58dd65a1
Lunar New Year is approaching, and Bob is struggling with his homework – a number division problem. There are n positive integers a_1, a_2, …, a_n on Bob's homework paper, where n is always an even number. Bob is asked to divide those numbers into groups, where each group must contain at least 2 numbers. Suppose the n...
n = int(input()) a = [int(s) for s in input().split(" ")] a.sort() ans = 0 for i in range(n//2): ans += (a[i]+a[n-i-1])**2 print(ans)
python
code_algorithm
[ { "input": "6\n1 1 1 2 2 2\n", "output": "27\n" }, { "input": "4\n8 5 2 3\n", "output": "164\n" }, { "input": "100\n28 27 23 6 23 11 25 20 28 15 29 23 20 2 8 24 8 9 30 8 8 1 11 7 6 17 17 27 26 30 12 22 17 22 9 25 4 26 9 26 10 30 13 4 16 12 23 19 10 22 12 20 3 16 10 4 29 11 15 4 5 7 29 16...
code_contests
python
1
f4419709729fa1b9daee771333429119
You are given two arrays a and b, each contains n integers. You want to create a new array c as follows: choose some real (i.e. not necessarily integer) number d, and then for every i ∈ [1, n] let c_i := d ⋅ a_i + b_i. Your goal is to maximize the number of zeroes in array c. What is the largest possible answer, if y...
from fractions import Fraction n = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) count = {} ans = 0 zeros = 0 for i in range(n): if A[i] == 0 and B[i] != 0: continue elif A[i] == 0 and B[i] == 0: zeros += 1 else: temp = Fraction(abs(B[i]), ab...
python
code_algorithm
[ { "input": "4\n0 0 0 0\n1 2 3 4\n", "output": "0\n" }, { "input": "5\n1 2 3 4 5\n2 4 7 11 3\n", "output": "2\n" }, { "input": "3\n13 37 39\n1 2 3\n", "output": "2\n" }, { "input": "3\n1 2 -1\n-6 -12 6\n", "output": "3\n" }, { "input": "5\n-2 2 1 0 2\n0 0 2 -1 -2\n...
code_contests
python
0
9a9bed225b2731d410001953d5da4110
You are both a shop keeper and a shop assistant at a small nearby shop. You have n goods, the i-th good costs a_i coins. You got tired of remembering the price of each product when customers ask for it, thus you decided to simplify your life. More precisely you decided to set the same price for all n goods you have. ...
import math for _ in range(int(input())): k=int(input()) l=list(map(int,input().split())) k=sum(l)/k print(math.ceil(k))
python
code_algorithm
[ { "input": "3\n5\n1 2 3 4 5\n3\n1 2 2\n4\n1 1 1 1\n", "output": "3\n2\n1\n" }, { "input": "3\n5\n1 2 3 4 5\n3\n1 2 3\n2\n777 778\n", "output": "3\n2\n778\n" }, { "input": "1\n2\n777 778\n", "output": "778\n" }, { "input": "1\n2\n777 1\n", "output": "389\n" }, { "i...
code_contests
python
0.9
830c6aea010a6761ea8850017224cb84
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string s consisting of n lowercase Latin letters. You have to color all its characters one of the two ...
# ------------------- fast io -------------------- 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...
python
code_algorithm
[ { "input": "7\nabcdedc\n", "output": "NO\n" }, { "input": "9\nabacbecfd\n", "output": "YES\n001010101\n" }, { "input": "5\nabcde\n", "output": "YES\n00000\n" }, { "input": "8\naaabbcbb\n", "output": "YES\n00000011\n" }, { "input": "6\nqdlrhw\n", "output": "NO\...
code_contests
python
0.1
a9dcbac4ca77eddad3533a9f5d83b109
A queen is the strongest chess piece. In modern chess the queen can move any number of squares in any horizontal, vertical or diagonal direction (considering that there're no other pieces on its way). The queen combines the options given to the rook and the bishop. There are m queens on a square n × n chessboard. You ...
#Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase from fractions import Fraction import collections from itertools import permutations from collections import defaultdict from collections import deque import threadi...
python
code_algorithm
[ { "input": "10 3\n1 1\n1 2\n1 3\n", "output": "0 2 1 0 0 0 0 0 0\n" }, { "input": "8 4\n4 3\n4 8\n6 5\n1 6\n", "output": "0 3 0 1 0 0 0 0 0\n" }, { "input": "10 10\n6 5\n3 5\n3 4\n6 10\n3 10\n4 6\n6 2\n7 5\n1 8\n2 2\n", "output": "0 5 1 2 1 1 0 0 0\n" }, { "input": "10 20\n6 ...
code_contests
python
0.1
3a63c984d34c6469834a838baab15dcd
During one of the space missions, humans have found an evidence of previous life at one of the planets. They were lucky enough to find a book with birth and death years of each individual that had been living at this planet. What's interesting is that these years are in the range (1, 10^9)! Therefore, the planet was na...
dct = {} for i in range(int(input())): a,b = map(int,input().split()) dct[a] = dct.get(a,0)+1 dct[b] = dct.get(b,0)-1 cnt = curr = y = 0 for i in sorted(dct.keys()): curr += dct[i] if curr > cnt : cnt = curr y = i print(y,cnt)
python
code_algorithm
[ { "input": "3\n1 5\n2 4\n5 6\n", "output": "2 2\n" }, { "input": "4\n3 4\n4 5\n4 6\n8 10\n", "output": "4 2\n" }, { "input": "1\n1 2\n", "output": "1 1\n" }, { "input": "1\n1 1000000000\n", "output": "1 1\n" }, { "input": "1\n125 126\n", "output": "125 1\n" ...
code_contests
python
1
da4cc49469f71074e2c108c364871af5
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Pety...
a=input() b=input() da={'4':0,'7':0} db={'4':0,'7':0} for i in a: da[i]+=1 for i in b: db[i]+=1 dif=0 for i in range(len(a)): if(a[i]!=b[i]): dif+=1 ans=0 if(da==db): ans=dif//2 else: x=abs(da['4']-db['4']) ans+=x dif-=x ans+=(dif//2) print(ans)
python
code_algorithm
[ { "input": "47\n74\n", "output": "1\n" }, { "input": "774\n744\n", "output": "1\n" }, { "input": "777\n444\n", "output": "3\n" }, { "input": "44447777447744444777777747477444777444447744444\n47444747774774744474747744447744477747777777447\n", "output": "13\n" }, { ...
code_contests
python
0.7
c4d043c2388f399e26cad738b016f5ac
One day n cells of some array decided to play the following game. Initially each cell contains a number which is equal to it's ordinal number (starting from 1). Also each cell determined it's favourite number. On it's move i-th cell can exchange it's value with the value of some other j-th cell, if |i - j| = di, where ...
n=int(input()) a=list(map(int,input().split())) b=list(map(int,input().split())) c=[[] for i in range(n)] for i in range(n): if i-b[i]>=0: c[i].append(i-b[i]) c[i-b[i]].append(i) if i+b[i]<n: c[i].append(i+b[i]) c[i+b[i]].append(i) v=[1]*n def dfs(u): global v,c v[u]=0 ...
python
code_algorithm
[ { "input": "7\n4 3 5 1 2 7 6\n4 6 6 1 6 6 1\n", "output": "NO\n" }, { "input": "5\n5 4 3 2 1\n1 1 1 1 1\n", "output": "YES\n" }, { "input": "7\n4 2 5 1 3 7 6\n4 6 6 1 6 6 1\n", "output": "YES\n" }, { "input": "80\n39 2 33 16 36 27 65 62 40 17 44 6 13 10 43 31 66 64 63 20 59 7...
code_contests
python
0
8c7204dcc82d7a99bf6f2de9545b242e
Iahub accidentally discovered a secret lab. He found there n devices ordered in a line, numbered from 1 to n from left to right. Each device i (1 ≤ i ≤ n) can create either ai units of matter or ai units of antimatter. Iahub wants to choose some contiguous subarray of devices in the lab, specify the production mode f...
import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(2*10**5+10) write = lambda x: sys.stdout.write(x+"\n") debug = lambda x: sys.stderr.write(x+"\n") writef = lambda x: print("{:.12f}".format(x)) n = int(input()) a = list(map(int, input().split())) m = sum(a) M = 10**9+7 dp = [0]*(2*m+1) ...
python
code_algorithm
[ { "input": "4\n1 1 1 1\n", "output": "12\n" }, { "input": "50\n2 1 5 2 1 3 1 2 3 2 1 1 5 2 2 2 3 2 1 2 2 2 3 3 1 3 1 1 2 2 2 2 1 2 3 1 2 4 1 1 1 3 2 1 1 1 3 2 1 3\n", "output": "115119382\n" }, { "input": "250\n2 2 2 2 3 2 4 2 3 2 5 1 2 3 4 4 5 3 5 1 2 5 2 3 5 3 2 3 3 3 5 1 5 5 5 4 1 3 2...
code_contests
python
0
0cbb9843fa2b1d2bec0ed3251732f709
Valera had an undirected connected graph without self-loops and multiple edges consisting of n vertices. The graph had an interesting property: there were at most k edges adjacent to each of its vertices. For convenience, we will assume that the graph vertices were indexed by integers from 1 to n. One day Valera count...
from sys import exit n, k = map(int, input().split()) nodes = [[] for _ in range(n+1)] edges = [] for node, dist in enumerate(map(int, input().split())): nodes[dist].append(node) if len(nodes[0]) != 1 or len(nodes[1]) > k: print(-1) else: for i in range(1, n): if len(nodes[i])*(k-1) < len(nodes[...
python
code_algorithm
[ { "input": "3 2\n0 1 1\n", "output": "2\n1 2\n1 3\n" }, { "input": "3 1\n0 0 0\n", "output": "-1\n" }, { "input": "4 2\n2 0 1 3\n", "output": "3\n2 3\n3 1\n1 4\n" }, { "input": "3 1\n0 1 2\n", "output": "-1\n" }, { "input": "5 3\n0 2 1 2 1\n", "output": "4\n1 ...
code_contests
python
0
e0cf1a4394c9a09202c20bc56bba9d86
Twilight Sparkle once got a crystal from the Crystal Mine. A crystal of size n (n is odd; n > 1) is an n × n matrix with a diamond inscribed into it. You are given an odd integer n. You need to draw a crystal of size n. The diamond cells of the matrix should be represented by character "D". All other cells of the matr...
n=int(input()) a=int(n/2) b=1 for i in range(int(n/2)): for j in range(a): print("*",end="") for j in range(b): print('D',end="") b=b+2 for j in range(a): print("*",end="") a=a-1 print() for i in range(b): print("D",end="") print() a=a+1 b=b-2 for i in range(int(n/2))...
python
code_algorithm
[ { "input": "5\n", "output": "**D**\n*DDD*\nDDDDD\n*DDD*\n**D**\n" }, { "input": "7\n", "output": "***D***\n**DDD**\n*DDDDD*\nDDDDDDD\n*DDDDD*\n**DDD**\n***D***\n" }, { "input": "3\n", "output": "*D*\nDDD\n*D*\n" }, { "input": "101\n", "output": "**************************...
code_contests
python
0
cde745665b6de27683d4a9da67d4f27d
Let's consider a table consisting of n rows and n columns. The cell located at the intersection of i-th row and j-th column contains number i × j. The rows and columns are numbered starting from 1. You are given a positive integer x. Your task is to count the number of cells in a table that contain number x. Input T...
import sys n, x = map(int, sys.stdin.readline().split()) s = 0 for i in range(1, n + 1): if (x % i == 0 and x / i <= n): s += 1 print(s)
python
code_algorithm
[ { "input": "5 13\n", "output": "0\n" }, { "input": "10 5\n", "output": "2\n" }, { "input": "6 12\n", "output": "4\n" }, { "input": "100000 997920\n", "output": "222\n" }, { "input": "100000 989460910\n", "output": "4\n" }, { "input": "22740 989460910\n...
code_contests
python
0.8
53cb17d0410a75216170922d84eb00b6
There are n pearls in a row. Let's enumerate them with integers from 1 to n from the left to the right. The pearl number i has the type ai. Let's call a sequence of consecutive pearls a segment. Let's call a segment good if it contains two pearls of the same type. Split the row of the pearls to the maximal number of ...
n=int(input()) d={} b=[] e=1 a=list(map(int,input().split())) for i in range(n): if a[i] in d: b.append([e,i+1]) e=i+2 d.clear() else: d[a[i]]=i+1 if len(b)==0: print(-1) exit() print(len(b)) k=b[-1] b[-1]=[k[0],n] for i in b: print(*i)
python
code_algorithm
[ { "input": "5\n1 2 3 4 1\n", "output": "1\n1 5\n" }, { "input": "7\n1 2 1 3 1 2 1\n", "output": "2\n1 3\n4 7\n" }, { "input": "5\n1 2 3 4 5\n", "output": "-1\n" }, { "input": "7\n13 9 19 13 3 13 12\n", "output": "1\n1 7\n" }, { "input": "4\n1 2 1 2\n", "output...
code_contests
python
0
ca0cdbada85cc1fd12fadf743323b561
Little Artem has invented a time machine! He could go anywhere in time, but all his thoughts of course are with computer science. He wants to apply this time machine to a well-known data structure: multiset. Artem wants to create a basic multiset of integers. He wants these structure to support operations of three typ...
# by the authority of GOD author: manhar singh sachdev # import os,sys from io import BytesIO, IOBase new = lambda xx: (xx|xx-1)+1 def buildBIT(bit,n): for i in range(1,n+1): x = new(i) if x <= n: bit[x] += bit[i] def pointUpdate(bit,point,n,diff): while point <= n: bi...
python
code_algorithm
[ { "input": "3\n1 1 1\n2 2 1\n3 3 1\n", "output": "0\n" }, { "input": "6\n1 1 5\n3 5 5\n1 2 5\n3 6 5\n2 3 5\n3 7 5\n", "output": "1\n2\n1\n" }, { "input": "10\n1 1 1000000000\n1 4 1000000000\n2 2 1000000000\n1 5 1000000000\n1 8 1000000000\n2 15 1000000000\n3 3 1000000000\n3 10 1000000000\...
code_contests
python
0.4
842139d7d66d3d425fcf144d8544aa3e
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide. We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise,...
n=int(input()) A=[] js=0 B=[] for i in range(n): A.append(list(map(int,input().split()))) def product(a,b,c): pr=0 for m in range(5): pr=pr+(A[b][m]-A[a][m])*(A[c][m]-A[a][m]) return (pr) if(n>11): print(0) else: for j in range(n): k=0 l=0 flag=0 while(k...
python
code_algorithm
[ { "input": "3\n0 0 1 2 0\n0 0 9 2 0\n0 0 5 9 0\n", "output": "0\n" }, { "input": "6\n0 0 0 0 0\n1 0 0 0 0\n0 1 0 0 0\n0 0 1 0 0\n0 0 0 1 0\n0 0 0 0 1\n", "output": "1\n1\n" }, { "input": "10\n0 -110 68 -51 -155\n-85 -110 68 -51 -155\n85 -70 51 68 -230\n0 -40 51 68 75\n0 5 -51 -68 -190\n8...
code_contests
python
0.2
59ce6c54070d61b3673538696e1453d8
This problem is same as the previous one, but has larger constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the n days since the day Shiro moved to th...
n = int(input()) a = list(map(int,input().split())) if(n == 1 or n == 2): print(n) exit() notpar = 0 occ = [0 for i in range(10**5 + 1)] base = 1 gotone = False leader = -1 leadby = 0 best = 0 occ[a[0]] += 1 cnt = 1 at1 = 1 for i in range(1, n): occ[a[i]] +=1 if(occ[a[i]] == 1): notpar ...
python
code_algorithm
[ { "input": "5\n10 100 20 200 1\n", "output": "5\n" }, { "input": "6\n1 1 1 2 2 2\n", "output": "5\n" }, { "input": "1\n100000\n", "output": "1\n" }, { "input": "13\n1 1 1 2 2 2 3 3 3 4 4 4 5\n", "output": "13\n" }, { "input": "7\n3 2 1 1 4 5 1\n", "output": "6...
code_contests
python
0.2
4e78e4cd4170845322b0130f4172bfb7
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between th...
def main(): from sys import stdin input = stdin.readline # input = open('25-B.txt', 'r').readline n, k = map(int, input().split()) s = input()[:-1] dp = [[0] * 26 for i in range(n + 1)] dp[0][0] = 1 for ch in s: j = ord(ch) - ord('a') for i in range(n, 0, -1): ...
python
code_algorithm
[ { "input": "4 5\nasdf\n", "output": "4\n" }, { "input": "5 6\naaaaa\n", "output": "15\n" }, { "input": "5 7\naaaaa\n", "output": "-1\n" }, { "input": "10 100\najihiushda\n", "output": "233\n" }, { "input": "50 50\ndxldyzmsrrwzwaofkcxwehgvtrsximxgdqrhjthkgfucrjdvwl...
code_contests
python
0
483ab27a6fb381a17d6a722341395dd1
Filled with optimism, Hyunuk will host a conference about how great this new year will be! The conference will have n lectures. Hyunuk has two candidate venues a and b. For each of the n lectures, the speaker specified two time intervals [sa_i, ea_i] (sa_i ≤ ea_i) and [sb_i, eb_i] (sb_i ≤ eb_i). If the conference is s...
import sys input = sys.stdin.readline import heapq n=int(input()) C=[tuple(map(int,input().split())) for i in range(n)] CA=[] CB=[] for ind,(a,b,c,d) in enumerate(C): CA.append((a,0,ind)) CA.append((b,1,ind)) CB.append((c,0,ind)) CB.append((d,1,ind)) CA.sort() CB.sort() SMAX=[] EMIN=[] FINISHED=...
python
code_algorithm
[ { "input": "3\n1 3 2 4\n4 5 6 7\n3 4 5 5\n", "output": "NO\n" }, { "input": "6\n1 5 2 9\n2 4 5 8\n3 6 7 11\n7 10 12 16\n8 11 13 17\n9 12 14 18\n", "output": "YES\n" }, { "input": "2\n1 2 3 6\n3 4 7 8\n", "output": "YES\n" }, { "input": "4\n1 5 1 3\n2 6 2 4\n3 7 3 5\n4 8 4 6\n...
code_contests
python
0
336621ebf397961a513333ee95e29199
Imagine that you have a twin brother or sister. Having another person that looks exactly like you seems very unusual. It's hard to say if having something of an alter ego is good or bad. And if you do have a twin, then you very well know what it's like. Now let's imagine a typical morning in your family. You haven't w...
n = int(input()) a = sorted(list(map(int, input().split())))[::-1] b = sum(a)//2 sum1 = count = 0 for i in a: sum1+=i count+=1 if sum1>b: break print(count)
python
code_algorithm
[ { "input": "2\n3 3\n", "output": "2\n" }, { "input": "3\n2 1 2\n", "output": "2\n" }, { "input": "100\n5 5 4 3 5 1 2 5 1 1 3 5 4 4 1 1 1 1 5 4 4 5 1 5 5 1 2 1 3 1 5 1 3 3 3 2 2 2 1 1 5 1 3 4 1 1 3 2 5 2 2 5 5 4 4 1 3 4 3 3 4 5 3 3 3 1 2 1 4 2 4 4 1 5 1 3 5 5 5 5 3 4 4 3 1 2 5 2 3 5 4 2 4...
code_contests
python
0.7
6424820afb681c09a6bbd9ee3c249db3
An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!). The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the...
def binpow(a,n): res = 1 while(n>0): if(n&1): res = (res*a)%m a = (a*a)%m n>>=1 return res n,m = map(int,input().split()) ans = binpow(3,n)-1 if(ans%m ==0 and ans != 0): print(m-1) else: print(ans%m)
python
code_algorithm
[ { "input": "1 10\n", "output": "2\n" }, { "input": "3 8\n", "output": "2\n" }, { "input": "8 100\n", "output": "60\n" }, { "input": "514853447 9\n", "output": "8\n" }, { "input": "7 63\n", "output": "44\n" }, { "input": "9 95\n", "output": "17\n" ...
code_contests
python
0.2
270505f4cff173687d0180a90bf1ebf4
Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d. Note that the order of the points inside the group of three chosen point...
from sys import stdin a,b=map(int,stdin.readline().split());a+=1 z=[1]+list(map(int,stdin.readline().split()));i,j=1,1;ans=0 r=lambda x:(x*(x+1))//2 while i<a: if j<=i:j=i while j<a and abs(z[j]-z[i])<=b:j+=1 if j-i-1>=2:ans+=r(j-i-2) i+=1 print(ans)
python
code_algorithm
[ { "input": "4 2\n-3 -2 -1 0\n", "output": "2\n" }, { "input": "4 3\n1 2 3 4\n", "output": "4\n" }, { "input": "5 19\n1 10 20 30 50\n", "output": "1\n" }, { "input": "1 14751211\n847188590\n", "output": "0\n" }, { "input": "10 90\n24 27 40 41 61 69 73 87 95 97\n", ...
code_contests
python
0.9
fbf9eadad933d5a6c135c8c62620838d
The Little Girl loves problems on games very much. Here's one of them. Two players have got a string s, consisting of lowercase English letters. They play a game that is described by the following rules: * The players move in turns; In one move the player can remove an arbitrary letter from string s. * If the pl...
import sys #sys.stdin = open('in', 'r') #sys.stdout = open('out', 'w') def Out(x): sys.stdout.write(str(x) + '\n') def In(): return sys.stdin.readline().strip() def main(): s = str(input()) Map = {} for word in s: if word not in Map: Map[word] = 1 else: ...
python
code_algorithm
[ { "input": "aba\n", "output": "First\n" }, { "input": "abca\n", "output": "Second\n" }, { "input": "zz\n", "output": "First\n" }, { "input": "aabc\n", "output": "Second\n" }, { "input": "vqdtkbvlbdyndheoiiwqhnvcmmhnhsmwwrvesnpdfxvprqbwzbodoihrywagphlsrcbtnvppjsquu...
code_contests
python
0
bc5e8751ffaa93f8c0a9c6d7fd2b9af0
Fox Ciel has some flowers: r red flowers, g green flowers and b blue flowers. She wants to use these flowers to make several bouquets. There are 4 types of bouquets: * To make a "red bouquet", it needs 3 red flowers. * To make a "green bouquet", it needs 3 green flowers. * To make a "blue bouquet", it needs 3 ...
a,b,c = map(int,input().split()) max_mix = min(a,b,c) res = -99999999999999 for i in range (0,min(3,max_mix)+1): pos = (i+(a-i)//3 + (b-i)//3 + (c-i)//3) res=max(res,pos) print(res)
python
code_algorithm
[ { "input": "3 6 9\n", "output": "6\n" }, { "input": "4 4 4\n", "output": "4\n" }, { "input": "0 0 0\n", "output": "0\n" }, { "input": "0 1 0\n", "output": "0\n" }, { "input": "2 2 0\n", "output": "0\n" }, { "input": "7 8 9\n", "output": "7\n" }, ...
code_contests
python
0
5b52a9db29416f455c94e2c25dc7cb6c
Valera loves his garden, where n fruit trees grow. This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days,...
frukti_po_dnyam = {} n, resurs =map(int,input().split(' ')) for i in range(n): den, chislo_fruktov = map(int, input().split(' ')) if den in frukti_po_dnyam: frukti_po_dnyam[den] += chislo_fruktov else: frukti_po_dnyam[den] = chislo_fruktov if den + 1 not in frukti_po_dnyam: fru...
python
code_algorithm
[ { "input": "5 10\n3 20\n2 20\n1 20\n4 20\n5 20\n", "output": "60\n" }, { "input": "2 3\n1 5\n2 3\n", "output": "8\n" }, { "input": "5 3000\n5 772\n1 2522\n2 575\n4 445\n3 426\n", "output": "4740\n" }, { "input": "2 100\n3000 100\n3000 100\n", "output": "200\n" }, { ...
code_contests
python
0
5a388a75dc43e27ac09c30e71ab56bbf
Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right. Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy: * Each piece should contain at least l numbers. * The difference between the maximal and the minimal number on the piece...
def split(a,n,s,l): pieces = [] i = 1 tmpmin = a[0] tmpmax = a[0] tmppc = [a[0]] while i<n: if abs(a[i]-tmpmin)<=s and abs(a[i]-tmpmax)<=s: tmppc.append(a[i]) if a[i]<tmpmin: tmpmin=a[i] elif a[i]>tmpmax: tmpmax = a[i] else: piece...
python
code_algorithm
[ { "input": "7 2 2\n1 100 1 100 1 100 1\n", "output": "-1\n" }, { "input": "7 2 2\n1 3 1 2 4 1 2\n", "output": "3\n" }, { "input": "1 0 1\n0\n", "output": "1\n" }, { "input": "10 3 3\n1 1 1 1 1 5 6 7 8 9\n", "output": "-1\n" }, { "input": "2 1000000000 2\n-10000000...
code_contests
python
0
d439aa68a8c0fbb798964842f03a816b
Fox Ciel starts to learn programming. The first task is drawing a fox! However, that turns out to be too hard for a beginner, so she decides to draw a snake instead. A snake is a pattern on a n by m table. Denote c-th cell of r-th row as (r, c). The tail of the snake is located at (1, 1), then it's body extends to (1,...
n,m=map(int,input().split()) s="."*(m-1)+"#" #print(s) for i in range(n): if i%2==0:print("#"*m) else: print(s) s=s[::-1]
python
code_algorithm
[ { "input": "9 9\n", "output": "#########\n........#\n#########\n#........\n#########\n........#\n#########\n#........\n#########\n" }, { "input": "5 3\n", "output": "###\n..#\n###\n#..\n###\n" }, { "input": "3 3\n", "output": "###\n..#\n###\n" }, { "input": "3 4\n", "outp...
code_contests
python
0
f86595da33108a8cdf415f43987e55c9
Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to operate an elevator in one of its other hotels. The elevator is special — it starts on the top floor, can only move down, and has infinite capacity. Floors are numbered from 0 to s and elevator initially starts on floor s...
I=lambda:map(int,input().split()) n,s=I() print(max(max(sum(I()),s)for _ in '0'*n))
python
code_algorithm
[ { "input": "5 10\n2 77\n3 33\n8 21\n9 12\n10 64\n", "output": "79\n" }, { "input": "3 7\n2 1\n3 8\n5 2\n", "output": "11\n" }, { "input": "2 10\n9 10\n6 11\n", "output": "19\n" }, { "input": "2 7\n6 3\n1 5\n", "output": "9\n" }, { "input": "2 10\n9 3\n1 4\n", ...
code_contests
python
0.5
7af7f25d539640feaf87b9edede7ef7b
Vova plays a computer game known as Mages and Monsters. Vova's character is a mage. Though as he has just started, his character knows no spells. Vova's character can learn new spells during the game. Every spell is characterized by two values xi and yi — damage per second and mana cost per second, respectively. Vova ...
#!/usr/bin/env python3 # solution after hint # (instead of best hit/mana spell store convex hull of spells) # O(n^2) instead of O(n log n) [q, m] = map(int, input().strip().split()) qis = [tuple(map(int, input().strip().split())) for _ in range(q)] mod = 10**6 j = 0 spell_chull = [(0, 0)] # lower hull _/ def is_...
python
code_algorithm
[ { "input": "3 100\n1 4 9\n2 19 49\n2 19 49\n", "output": "YES\nNO\n" }, { "input": "2 424978864039\n2 7 3\n2 10 8\n", "output": "NO\nNO\n" }, { "input": "15 100\n1 8 8\n2 200 101\n2 10 99\n1 9 9\n2 10 99\n2 200 101\n1 14 4\n2 194 195\n2 194 194\n2 990 290\n1 2 999992\n2 6 256\n2 7 256\n1...
code_contests
python
0
be7ccf64138f33e020cb953e8f1c53db
A new set of desks just arrived, and it's about time! Things were getting quite cramped in the office. You've been put in charge of creating a new seating chart for the engineers. The desks are numbered, and you sent out a survey to the engineering team asking each engineer the number of the desk they currently sit at,...
n = int(input()) m = 2 * n + 1 u = [[] for i in range(m)] v = [0] * m s = [0] * m d = 10 ** 9 + 7 y = 1 for j in range(n): a, b = map(int, input().split()) v[a] = b if a != b: s[b] += 1 u[b].append(a) for b in range(m): if not v[b]: x = 0 p = [b] while p: ...
python
code_algorithm
[ { "input": "4\n1 5\n5 2\n3 7\n7 3\n", "output": "6\n" }, { "input": "5\n1 10\n2 10\n3 10\n4 10\n5 5\n", "output": "5\n" }, { "input": "1\n1 2\n", "output": "2\n" }, { "input": "30\n22 37\n12 37\n37 58\n29 57\n43 57\n57 58\n58 53\n45 4\n1 4\n4 51\n35 31\n21 31\n31 51\n51 53\n5...
code_contests
python
0
7e0baa13fdf2c76950b837c22e9c7f35
Ivan has n different boxes. The first of them contains some balls of n different colors. Ivan wants to play a strange game. He wants to distribute the balls into boxes in such a way that for every i (1 ≤ i ≤ n) i-th box will contain all balls with color i. In order to do this, Ivan will make some turns. Each turn he ...
from heapq import * n = int(input()) a = list(map(int, input().split())) heap = [] res = 0 for i in range(n): heappush(heap, a[i]) if n % 2 == 0: heappush(heap, 0) while n > 1: cur = heappop(heap) cur += heappop(heap) cur += heappop(heap) res += cur heappush(heap, cur) n -= 2 print(res)
python
code_algorithm
[ { "input": "3\n1 2 3\n", "output": "6\n" }, { "input": "4\n2 3 4 5\n", "output": "19\n" }, { "input": "2\n3 4\n", "output": "7\n" }, { "input": "8\n821407370 380061316 428719552 90851747 825473738 704702117 845629927 245820158\n", "output": "8176373828\n" }, { "in...
code_contests
python
0
72446bea232272648e8a93922a1b5b5e
Misha was interested in water delivery from childhood. That's why his mother sent him to the annual Innovative Olympiad in Irrigation (IOI). Pupils from all Berland compete there demonstrating their skills in watering. It is extremely expensive to host such an olympiad, so after the first n olympiads the organizers int...
from bisect import bisect_left as bl import sys N, M, Q = map(int, sys.stdin.readline().split()) count = [0] * (M + 1) A = [] for a in sys.stdin.readline().split(): a = int(a) A.append(count[a] * M + a) count[a] += 1 A.sort() A = [a - i for i, a in enumerate(A, 1)] for _ in range(Q): q = int(sys.stdin....
python
code_algorithm
[ { "input": "4 5 4\n4 4 5 1\n15\n9\n13\n6\n", "output": "5\n3\n3\n3\n" }, { "input": "6 4 10\n3 1 1 1 2 2\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n", "output": "4\n3\n4\n2\n3\n4\n1\n2\n3\n4\n" }, { "input": "100 21 20\n11 17 21 14 7 15 18 6 17 11 21 3 17 21 21 7 21 17 14 21 14 14 3 6 18 21 1...
code_contests
python
0
e54f115ffffd3749c1c060e4c82471a6
Theatre Square in the capital city of Berland has a rectangular shape with the size n × m meters. On the occasion of the city's anniversary, a decision was taken to pave the Square with square granite flagstones. Each flagstone is of the size a × a. What is the least number of flagstones needed to pave the Square? It'...
import math def main(): n, m, a = map(int, input().split()) sq_area = n * m stone_area = a * a if stone_area > sq_area: return 1 return math.ceil(m / a) * math.ceil(n / a) if __name__ == '__main__': print(main())
python
code_algorithm
[ { "input": "6 6 4\n", "output": "4\n" }, { "input": "1 1 3\n", "output": "1\n" }, { "input": "1000000000 1000000000 1\n", "output": "1000000000000000000\n" }, { "input": "2 1 2\n", "output": "1\n" }, { "input": "222 332 5\n", "output": "3015\n" }, { "i...
code_contests
python
1
b5363242180e9890d76365494ca6553d
Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a "plus") and negative (a "minus"). If two magnets are put together at a close distance, then the like poles will repel each other and the oppo...
s=0 prev='' for i in range(int(input())): kk=input() if kk!=prev: s+=1 prev=kk print(s)
python
code_algorithm
[ { "input": "4\n01\n01\n10\n10\n", "output": "2\n" }, { "input": "6\n10\n10\n10\n01\n10\n10\n", "output": "3\n" }, { "input": "1\n10\n", "output": "1\n" }, { "input": "3\n10\n01\n10\n", "output": "3\n" }, { "input": "3\n10\n10\n01\n", "output": "2\n" }, { ...
code_contests
python
1
ef33c709a884a31fa2dc576f72913748
Let's call an array consisting of n integer numbers a1, a2, ..., an, beautiful if it has the following property: * consider all pairs of numbers x, y (x ≠ y), such that number x occurs in the array a and number y occurs in the array a; * for each pair x, y must exist some position j (1 ≤ j < n), such that at leas...
def readdata(): #fread = open('input.txt', 'r') global n, m, w, q n, m = [int(x) for x in input().split()] q = [0] * m w = [0] * m for i in range(m): q[i], w[i] = [int(x) for x in input().split()] def podg(): global summ w.sort(reverse = True) summ ...
python
code_algorithm
[ { "input": "100 3\n1 2\n2 1\n3 1\n", "output": "4\n" }, { "input": "1 2\n1 1\n2 100\n", "output": "100\n" }, { "input": "5 2\n1 2\n2 3\n", "output": "5\n" }, { "input": "58 38\n6384 48910\n97759 90589\n28947 5031\n45169 32592\n85656 26360\n88538 42484\n44042 88351\n42837 7902...
code_contests
python
0
79f3cb1267656f9cd9b6200559e4822e
You have probably registered on Internet sites many times. And each time you should enter your invented password. Usually the registration form automatically checks the password's crypt resistance. If the user's password isn't complex enough, a message is displayed. Today your task is to implement such an automatic che...
n=input() d=[] sp="!?.,_" for i in n: if len(n)>=5: d.append(1) if i.isupper()==True: d.append(2) if i.islower()==True: d.append(3) if i.isdigit()==True: d.append(4) if i in sp: d.append(5) s=set(d) if s=={1,2,3,4} or s=={1,2,3,4,5}: print("Correct") else:...
python
code_algorithm
[ { "input": "X12345\n", "output": "Too weak\n" }, { "input": "CONTEST_is_STARTED!!11\n", "output": "Correct\n" }, { "input": "abacaba\n", "output": "Too weak\n" }, { "input": "Vu7jQU8.!FvHBYTsDp6AphaGfnEmySP9te\n", "output": "Correct\n" }, { "input": "P1H\n", "...
code_contests
python
0.4
b00c046a8c16a58a61b08c2e641289b2
Let's denote as <image> the number of bits set ('1' bits) in the binary representation of the non-negative integer x. You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, and <image> is maximum possible. If there are multiple such numbers find the sma...
n = int(input()) for i in range(n): l,r=map(int,input().split()) while(l|(l+1)<=r): # or make like max function here . l|=l+1 # or here like equal print(l)
python
code_algorithm
[ { "input": "3\n1 2\n2 4\n1 10\n", "output": "1\n3\n7\n" }, { "input": "18\n1 10\n1 100\n1 1000\n1 10000\n1 100000\n1 1000000\n1 10000000\n1 100000000\n1 1000000000\n1 10000000000\n1 100000000000\n1 1000000000000\n1 10000000000000\n1 100000000000000\n1 1000000000000000\n1 10000000000000000\n1 1000000...
code_contests
python
0
175a62a91d18ed78f5b42a45bbc13581
Kevin and Nicky Sun have invented a new game called Lieges of Legendre. In this game, two players take turns modifying the game state with Kevin moving first. Initially, the game is set up so that there are n piles of cows, with the i-th pile containing ai cows. During each player's turn, that player calls upon the pow...
G_EVEN = {0:0, 1:1, 2:2} G_ODD = {0:0, 1:1, 2:0, 3:1} def grundy(k, ai): if k % 2: if ai <= 3: return G_ODD[ai] elif ai % 2: return 0 else: p = 0 j = ai while not j & 1: p += 1 j >>= 1 i...
python
code_algorithm
[ { "input": "1 2\n3\n", "output": "Nicky\n" }, { "input": "2 1\n3 4\n", "output": "Kevin\n" }, { "input": "2 1\n24 1\n", "output": "Kevin\n" }, { "input": "1 1\n1\n", "output": "Kevin\n" }, { "input": "5 1\n1 7 7 6 6\n", "output": "Kevin\n" }, { "input"...
code_contests
python
0
00a80c210cc7f86deac3491cba505711
Friends are going to play console. They have two joysticks and only one charger for them. Initially first joystick is charged at a1 percent and second one is charged at a2 percent. You can connect charger to a joystick only at the beginning of each minute. In one minute joystick either discharges by 2 percent (if not c...
import sys input = sys.stdin.readline def inp(): return (int(input())) def inlt(): return (list(map(int, input().split()))) def insr(): s = input() return (list(s[:len(s) - 1])) def invr(): return list((map(int, input().split()))) hash_map = {} def solve(a1, a2): if a1 <= 0 or a2 <=...
python
code_algorithm
[ { "input": "4 4\n", "output": "5\n" }, { "input": "3 5\n", "output": "6\n" }, { "input": "1 4\n", "output": "2\n" }, { "input": "7 2\n", "output": "7\n" }, { "input": "100 25\n", "output": "122\n" }, { "input": "29 1\n", "output": "28\n" }, { ...
code_contests
python
0
6c35d843c192333245e482167168c9df
Anton and Dasha like to play different games during breaks on checkered paper. By the 11th grade they managed to play all the games of this type and asked Vova the programmer to come up with a new game. Vova suggested to them to play a game under the code name "dot" with the following rules: * On the checkered pape...
import sys from array import array # noqa: F401 def input(): return sys.stdin.buffer.readline().decode('utf-8') x, y, n, d = map(int, input().split()) vector = [list(map(int, input().split())) for _ in range(n)] dp = [[-1] * (d * 2 + 1) for _ in range(d * 2 + 1)] for i in range(d * 2 + 1): for j in range(...
python
code_algorithm
[ { "input": "0 0 2 4\n1 1\n1 2\n", "output": "Dasha\n" }, { "input": "0 0 2 3\n1 1\n1 2\n", "output": "Anton\n" }, { "input": "3 -1 20 200\n19 12\n24 121\n25 32\n28 19\n28 87\n29 49\n32 88\n33 70\n37 77\n54 33\n56 27\n61 59\n67 42\n73 15\n76 40\n80 73\n83 39\n91 34\n91 112\n95 95\n", ...
code_contests
python
0
37dcb42661f2af9fe43afa1cab6227da
Recently Adaltik discovered japanese crosswords. Japanese crossword is a picture, represented as a table sized a × b squares, and each square is colored white or black. There are integers to the left of the rows and to the top of the columns, encrypting the corresponding row or column. The number of integers represents...
n=int(input()) s=input() arr,k=[],0 for i in range(len(s)): if(s[i]=='B'): k+=1 else: if(k>0): arr.append(k) k=0 if(k>0): arr.append(k) print(len(arr)) print(*arr,sep=' ')
python
code_algorithm
[ { "input": "3\nBBW\n", "output": "1\n2\n" }, { "input": "5\nBWBWB\n", "output": "3\n1 1 1\n" }, { "input": "4\nWWWW\n", "output": "0\n\n" }, { "input": "13\nWBBBBWWBWBBBW\n", "output": "3\n4 1 3\n" }, { "input": "4\nBBBB\n", "output": "1\n4\n" }, { "in...
code_contests
python
0.8
b164d4a5ee69017921f421506b29d29c
Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if...
from sys import stdin, stdout from collections import defaultdict as dd read, write = stdin.readline, stdout.write class DisjointSetUnion: def __init__(self, n): self.parent = list(range(n)) self.size = [1] * n self.num_sets = n def find(self, a): acopy = a while a != s...
python
code_algorithm
[ { "input": "4 2 11\n2 4 6 6\n6 4 2 1\n1 2\n2 3\n", "output": "7\n" }, { "input": "3 1 5\n3 2 5\n2 4 2\n1 2\n", "output": "6\n" }, { "input": "10 5 100\n64 90 3 94 96 97 52 54 82 31\n796554 444893 214351 43810 684158 555762 686198 339093 383018 699152\n6 8\n8 3\n3 9\n2 3\n10 3\n", "ou...
code_contests
python
0.3
a4624425f0badb70048e4a8f6bc748fc
While Mahmoud and Ehab were practicing for IOI, they found a problem which name was Longest common subsequence. They solved it, and then Ehab challenged Mahmoud with another problem. Given two strings a and b, find the length of their longest uncommon subsequence, which is the longest string that is a subsequence of o...
def uncommon(a, b): if (a == b): return -1 return max(len(a), len(b)) A = input() B = input() print(uncommon(A, B))
python
code_algorithm
[ { "input": "abcd\ndefgh\n", "output": "5\n" }, { "input": "a\na\n", "output": "-1\n" }, { "input": "abb\nabb\n", "output": "-1\n" }, { "input": "abcde\nbbcde\n", "output": "5\n" }, { "input": "abc\ncba\n", "output": "3\n" }, { "input": "aaa\naaa\n", ...
code_contests
python
0.6
c5922938aaef716cedf3ef954399bc43
There is an automatic door at the entrance of a factory. The door works in the following way: * when one or several people come to the door and it is closed, the door immediately opens automatically and all people immediately come inside, * when one or several people come to the door and it is open, all people im...
BigNum = 10 ** 20 n, m, a, d = map(int, input().split(' ')) ts = [0] + list(map(int, input().split(' '))) + [BigNum] def empsInRange(l, r): em1 = l // a + 1 em2 = r // a return (em1, min(em2, n)) empDoorGroup = d // a + 1 def moveEmps(emps, last): em1, em2 = emps if em1 > em2: return las...
python
code_algorithm
[ { "input": "1 1 3 4\n7\n", "output": "1\n" }, { "input": "4 3 4 2\n7 9 11\n", "output": "4\n" }, { "input": "100 30 36 47\n44 155 275 390 464 532 1186 1205 1345 1349 1432 1469 1482 1775 1832 1856 1869 2049 2079 2095 2374 2427 2577 2655 2792 2976 3020 3317 3482 3582\n", "output": "51\...
code_contests
python
0
e55239f7813ed3a8467026ccb90375bc
Ramesses knows a lot about problems involving trees (undirected connected graphs without cycles)! He created a new useful tree decomposition, but he does not know how to construct it, so he asked you for help! The decomposition is the splitting the edges of the tree in some simple paths in such a way that each two pa...
n = int(input()) flag = True f = [0] * (n - 1) t = [0] * (n - 1) co = [0] * n for i in range(n - 1): f[i], t[i] = map(int, input().split()) co[f[i] - 1] += 1 co[t[i] - 1] += 1 mid = 0 en = [] for i in range(n): if co[i] == 1: en.append(i + 1) elif co[i] > 2: if mid == 0: ...
python
code_algorithm
[ { "input": "6\n1 2\n2 3\n3 4\n2 5\n3 6\n", "output": "No\n" }, { "input": "4\n1 2\n2 3\n3 4\n", "output": "Yes\n1\n1 4\n" }, { "input": "5\n1 2\n1 3\n1 4\n1 5\n", "output": "Yes\n4\n1 2\n1 3\n1 4\n1 5\n" }, { "input": "9\n1 2\n1 3\n1 4\n1 5\n1 6\n6 7\n7 8\n7 9\n", "output...
code_contests
python
0
39ce352f98a9fdeba2a28318cd353df9
As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon. Elections are coming. You know the number of voters and the number of parties — n and m respectively. For each vote...
import sys f=sys.stdin out=sys.stdout n,m=map(int,f.readline().rstrip('\r\n').split()) cos={} cost=[] nvot=[0 for i in range(m+1)] party=[[] for i in range(m+1)] for i in range(n): p,c=map(int,f.readline().rstrip('\r\n').split()) if p!=1: if c in cos: cos[c]+=1 else: cos[c]=1 cost.append(c) party[p...
python
code_algorithm
[ { "input": "1 2\n1 100\n", "output": "0\n" }, { "input": "5 5\n2 100\n3 200\n4 300\n5 800\n5 900\n", "output": "600\n" }, { "input": "5 5\n2 100\n3 200\n4 300\n5 400\n5 900\n", "output": "500\n" }, { "input": "1 3000\n2006 226621946\n", "output": "226621946\n" }, { ...
code_contests
python
0
5dba697fcb6f735cb0d534feef84e067
Recently you have received two positive integer numbers x and y. You forgot them, but you remembered a shuffled list containing all divisors of x (including 1 and x) and all divisors of y (including 1 and y). If d is a divisor of both numbers x and y at the same time, there are two occurrences of d in the list. For ex...
t=int(input()) lis=list(map(int,input().split())) maxnum=max(lis) lis.remove(maxnum) for x in range (1,maxnum//2+1): if maxnum%x==0: lis.remove(x) maxnum2=max(lis) print("{} {}".format(maxnum,maxnum2))
python
code_algorithm
[ { "input": "10\n10 2 8 1 2 4 1 20 4 5\n", "output": "20 8\n" }, { "input": "4\n1 2 53 1\n", "output": "53 2\n" }, { "input": "6\n1 58 29 1 2 2\n", "output": "58 2\n" }, { "input": "6\n29 29 1 1 58 2\n", "output": "58 29\n" }, { "input": "9\n7 49 1 1 98 14 49 7 2\n...
code_contests
python
0
a926e1d55e2f64423aab364a7c8aba99
Bob is decorating his kitchen, more precisely, the floor. He has found a prime candidate for the tiles he will use. They come in a simple form factor — a square tile that is diagonally split into white and black part as depicted in the figure below. <image> The dimension of this tile is perfect for this kitchen, as h...
def powmod(x, k, m): res = 1 s = 1 while s <= k: if k & s: res = res * x % m x = x * x % m s <<= 1 return res m = 998244353 w, h = map(int, input().split()) print(powmod(2, w + h, m))
python
code_algorithm
[ { "input": "2 4\n", "output": "64\n" }, { "input": "2 2\n", "output": "16\n" }, { "input": "2 1\n", "output": "8\n" }, { "input": "2 3\n", "output": "32\n" }, { "input": "17 23\n", "output": "444595123\n" }, { "input": "2 5\n", "output": "128\n" ...
code_contests
python
0.2
14e423508acd9e2a238a3883faba8956
The only difference between easy and hard versions is the number of elements in the array. You are given an array a consisting of n integers. In one move you can choose any a_i and divide it by 2 rounding down (in other words, in one move you can set a_i := ⌊(a_i)/(2)⌋). You can perform such an operation any (possibl...
from sys import stdin n, k = [int(i) for i in stdin.readline().strip().split()] a = [int(i) for i in stdin.readline().strip().split()] p = [19 * [0] for _ in range(max(a) + 1)] nums = set() for m in a: for i in range(19): p[m >> i][i] += 1 nums.add(m >> i) if (m >> i) == 0: ...
python
code_algorithm
[ { "input": "5 3\n1 2 3 3 3\n", "output": "0\n" }, { "input": "5 3\n1 2 3 4 5\n", "output": "2\n" }, { "input": "5 3\n1 2 2 4 5\n", "output": "1\n" }, { "input": "50 7\n199961 199990 199995 199997 199963 199995 199985 199994 199974 199974 199997 199991 199993 199982 199991 199...
code_contests
python
0.1
0b6256ef902cfa5453407cbe7e1a7d0e
As the name of the task implies, you are asked to do some work with segments and trees. Recall that a tree is a connected undirected graph such that there is exactly one simple path between every pair of its vertices. You are given n segments [l_1, r_1], [l_2, r_2], ..., [l_n, r_n], l_i < r_i for every i. It is guara...
import sys class segmTree(): def __init__(self, size=None, array=None): if array is not None: size = len(array) N = 1 while N < size: N <<= 1 self.N = N self.tree = [0] * (2*self.N) if array is not None: for i in range(size): ...
python
code_algorithm
[ { "input": "5\n5 8\n3 6\n2 9\n7 10\n1 4\n", "output": "NO\n" }, { "input": "5\n1 3\n2 4\n5 9\n6 8\n7 10\n", "output": "NO\n" }, { "input": "6\n9 12\n2 11\n1 3\n6 10\n5 7\n4 8\n", "output": "YES\n" }, { "input": "4\n1 2\n3 6\n4 7\n5 8\n", "output": "NO\n" }, { "inp...
code_contests
python
0.1
b1c363e08aeaf7c060beabc5a840d973
Mr. Chanek is currently participating in a science fair that is popular in town. He finds an exciting puzzle in the fair and wants to solve it. There are N atoms numbered from 1 to N. These atoms are especially quirky. Initially, each atom is in normal state. Each atom can be in an excited. Exciting atom i requires D_...
n, k = map(int, input().split()) a = list(map(int, input().split())) d = list(map(int, input().split())) pref=[0] for i in range(n): pref.append(pref[-1]+a[i]) if k==0: ans=0 for i in range(1,n+1): ans=max(ans,pref[-1]-pref[i-1]-d[i-1]) print(ans) elif k == 1: best = sum(a[:n-1]) - min(d[:n...
python
code_algorithm
[ { "input": "6 1\n5 6 7 8 10 2\n3 5 6 7 1 10\n", "output": "35\n" }, { "input": "4 1\n1 12 13 20\n10 5 4 100\n", "output": "40\n" }, { "input": "6 1\n1 10 10 5 20 100\n1000000 1 10 10 100 1000000\n", "output": "144\n" }, { "input": "5 0\n1 1 1 1 1\n10 10 10 10 10\n", "outp...
code_contests
python
0
7271d3c722f5148f0a9728010dbd5eff
Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the N-ish language. Sentences of the N-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks. Sergey totally forgot about ...
ans = 0 t = [] x = input() y = int(input()) for i in range(y): z = input() t.append(z) #x = codeforces #y = 2 #t = [do, cs] pt = -1 ln = len(x) for i in t: a = i[0] b = i[1] pt = 0 for j in range(ln): ded1=0 ded2=0 if j >= pt: if x[j] in [a,b]: ...
python
code_algorithm
[ { "input": "ababa\n1\nab\n", "output": "2\n" }, { "input": "codeforces\n2\ndo\ncs\n", "output": "1\n" }, { "input": "vefneyamdzoytemupniw\n13\nve\nfg\noi\nan\nck\nwx\npq\nml\nbt\nud\nrz\nsj\nhy\n", "output": "1\n" }, { "input": "pgpgppgggpbbnnn\n2\npg\nnb\n", "output": "7...
code_contests
python
0.1
c51d50d78c0f3d626b7e347a65cb62fc
You've got string s, consisting of only lowercase English letters. Find its lexicographically maximum subsequence. We'll call a non-empty string s[p1p2... pk] = sp1sp2... spk(1 ≤ p1 < p2 < ... < pk ≤ |s|) a subsequence of string s = s1s2... s|s|. String x = x1x2... x|x| is lexicographically larger than string y = y1y...
s = input() n = len(s) temp = list(set(s)) x = {} temp = sorted(temp,reverse=True) i = 0 while(i < n ): x[s[i]] = i i +=1 string = "" i = 0 j = 0 while(i< n and j < len(temp)): if i <= x[temp[j]]: if s[i]== temp[j]: string += temp[j] if i== x[temp[j]]: j += 1 if(l...
python
code_algorithm
[ { "input": "ababba\n", "output": "bbba\n" }, { "input": "abbcbccacbbcbaaba\n", "output": "cccccbba\n" }, { "input": "b\n", "output": "b\n" }, { "input": "abcdeabcd\n", "output": "ed\n" }, { "input": "aza\n", "output": "za\n" }, { "input": "abcdedcba\n"...
code_contests
python
0.7
f3cb0d0a24cf5e61c1ae3195ee2d1544
Jabber ID on the national Berland service «Babber» has a form <username>@<hostname>[/resource], where * <username> — is a sequence of Latin letters (lowercase or uppercase), digits or underscores characters «_», the length of <username> is between 1 and 16, inclusive. * <hostname> — is a sequence of word separat...
s = input() if s.count('@') != 1 or s.count('/') > 1: print('NO') exit() p1 = s.find('@') p2 = s.find('/') if p2 == -1: p2 = len(s) import re u = re.compile('(\w){1,16}$') h = re.compile('(\w{1,16}\.)*\w{1,16}$') k1 = h.match(s[p1 + 1 : p2]) k2 = u.match(s[0:p1]) k3 = u.match(s[p2 + 1 : len(s)]) if len(s[p1 + 1...
python
code_algorithm
[ { "input": "mike@codeforces.com\n", "output": "YES\n" }, { "input": "john.smith@codeforces.ru/contest.icpc/12\n", "output": "NO\n" }, { "input": "@ops\n", "output": "NO\n" }, { "input": "xLEctap0T@22U9W_fA/7iQeJGFu1lSgMZ\n", "output": "YES\n" }, { "input": "lNC9D1...
code_contests
python
0.4
668050a5954017b06638c6fb2d2faf2f
Smart Beaver decided to be not only smart, but also a healthy beaver! And so he began to attend physical education classes at school X. In this school, physical education has a very creative teacher. One of his favorite warm-up exercises is throwing balls. Students line up. Each one gets a single ball in the beginning....
m = 1000000007 n = int(input()) a = map(int, input().split()) t1, t2 = 0, 0 for i in a: if i == 1: t1 += 1 else: t2 += 1 a = [1, 2] for i in range(3, t1+1): a = a[::-1] a[1] = (a[0]+(i-1)*a[1])%m if not t1 or t1 == 1: a[1] = 1 for i in range(t1+1, n+1): a[1] = a[1]*i%m print(a[1])
python
code_algorithm
[ { "input": "8\n1 2 2 1 2 1 1 2\n", "output": "16800\n" }, { "input": "5\n1 2 2 1 2\n", "output": "120\n" }, { "input": "10\n2 2 2 2 2 2 2 2 2 2\n", "output": "3628800\n" }, { "input": "100\n2 2 2 2 1 1 2 2 1 2 2 1 1 2 2 2 2 2 2 1 1 2 2 2 2 1 2 1 1 2 2 1 2 2 1 2 2 2 2 2 2 1 2 ...
code_contests
python
0
35a14e8ee06be66da167bee08bc76337
In one little known, but very beautiful country called Waterland, lives a lovely shark Valerie. Like all the sharks, she has several rows of teeth, and feeds on crucians. One of Valerie's distinguishing features is that while eating one crucian she uses only one row of her teeth, the rest of the teeth are "relaxing". ...
n, m, k = list(map(int,input().split())) tooth = [[0,True]] * n for i in range(n): r, c = list(map(int,input().split())) if tooth[r-1][0] > c or tooth[r-1][1] == True: tooth[r-1] = [c,False] ad = 0 for i in tooth: ad += i[0] print(min(ad,k))
python
code_algorithm
[ { "input": "2 2 13\n1 13\n2 12\n", "output": "13\n" }, { "input": "4 3 18\n2 3\n1 2\n3 6\n2 3\n", "output": "11\n" }, { "input": "10 4 14\n2 6\n1 5\n2 8\n2 6\n2 5\n4 1\n4 0\n2 4\n3 4\n1 0\n", "output": "8\n" }, { "input": "4 2 8\n1 9\n1 10\n1 4\n2 6\n", "output": "8\n" ...
code_contests
python
0.1
827f506598b217d489bdaf34f82723ff
In this problem we consider a special type of an auction, which is called the second-price auction. As in regular auction n bidders place a bid which is price a bidder ready to pay. The auction is closed, that is, each bidder secretly informs the organizer of the auction price he is willing to pay. After that, the auct...
a=int(input()) a=list(map(int,input().split())) ind=a.index(max(a)) a.sort() print(str(ind+1)+' '+str(a[-2]))
python
code_algorithm
[ { "input": "6\n3 8 2 9 4 14\n", "output": "6 9\n" }, { "input": "2\n5 7\n", "output": "2 5\n" }, { "input": "3\n10 2 8\n", "output": "1 8\n" }, { "input": "3\n30 20 10\n", "output": "1 20\n" }, { "input": "3\n3 4 1\n", "output": "2 3\n" }, { "input": "...
code_contests
python
0.9
317a195f80e19d6282fb9d63f88a8919
Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory. Though Ryouko is forgetful, she is also bo...
def median(a): if len(a) == 0: return 0 if len(a) % 2 == 1: return a[len(a) // 2] else: return (a[len(a) // 2] + a[(len(a) // 2) - 1]) // 2 def profit(a, old_val): a.sort() med = median(a) sum_old = 0 sum_new = 0 for i in a: sum_old += abs(i - old_val) ...
python
code_algorithm
[ { "input": "4 6\n1 2 3 4 3 2\n", "output": "3\n" }, { "input": "10 5\n9 4 3 8 8\n", "output": "6\n" }, { "input": "5 10\n2 5 2 2 3 5 3 2 1 3\n", "output": "7\n" }, { "input": "1000 10\n1 1 1 1 1 1000 1000 1000 1000 1000\n", "output": "0\n" }, { "input": "10 20\n6 ...
code_contests
python
0.4
01feb03c75bed6d789189bc90083a719
Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long winter evening he came up with a game and decided to play it. Given a sequence a consisting of n integers. The player can make several steps. In a single step he can choose an element of the sequence (let's denote it ak) and...
#!/usr/bin/env python3 # -*- coding: utf-8 -*- s=0 n = int(input()) dp=[0]*(10**5+1) sec = [int(x) for x in input().split()] ls = [0]*(10**5+1) for x in sec: ls[x]+=1 dp[1]=ls[1] for i in range(2,10**5+1): dp[i]=max(dp[i-1],dp[i-2]+ls[i]*i) print(dp[10**5])
python
code_algorithm
[ { "input": "9\n1 2 1 3 2 2 2 2 3\n", "output": "10\n" }, { "input": "3\n1 2 3\n", "output": "4\n" }, { "input": "2\n1 2\n", "output": "2\n" }, { "input": "5\n3 3 4 5 4\n", "output": "11\n" }, { "input": "10\n8 9 6 5 6 4 10 9 1 4\n", "output": "39\n" }, { ...
code_contests
python
0.6
305215dbf1df4fb3940774f0f14bc4df
Petya studies in a school and he adores Maths. His class has been studying arithmetic expressions. On the last class the teacher wrote three positive integers a, b, c on the blackboard. The task was to insert signs of operations '+' and '*', and probably brackets between the numbers so that the value of the resulting e...
a1 = int(input()) a2 = int(input()) a3 = int(input()) listValue = [a1*a2*a3, a1*(a2+a3), a1*a2+a3, a1+a2*a3, (a1+a2)*a3, a1+a2+a3] maxValue = listValue[0] for x in listValue: if x > maxValue: maxValue = x print(maxValue)
python
code_algorithm
[ { "input": "2\n10\n3\n", "output": "60\n" }, { "input": "1\n2\n3\n", "output": "9\n" }, { "input": "2\n1\n2\n", "output": "6\n" }, { "input": "5\n6\n1\n", "output": "35\n" }, { "input": "10\n10\n10\n", "output": "1000\n" }, { "input": "8\n9\n7\n", ...
code_contests
python
0.9
6611559dfb9a9c0703c856687bc32498
Vanya got an important task — he should enumerate books in the library and label each book with its number. Each of the n books should be assigned with a number from 1 to n. Naturally, distinct books should be assigned distinct numbers. Vanya wants to know how many digits he will have to write down as he labels the bo...
n = input() s = len(n) ''' 9 - 1 1 1 1 1 1 1 1 1 90 - 11 11 11 11 11 11 11 11 11 11 ... 11 11 900 - 111 111 111 111 111 111 111 ... 111 111 9000 - 1111 1111 1111 ... 1111 1111 ''' q = 0 if s > 2: for c in range(s-1): v = int('9' + c*'0') q += v*(c+1) k = int(n) - (int('1' + (s-1)*'0') - 1) q += k * s ...
python
code_algorithm
[ { "input": "13\n", "output": "17\n" }, { "input": "4\n", "output": "4\n" }, { "input": "995\n", "output": "2877\n" }, { "input": "100000\n", "output": "488895\n" }, { "input": "10000\n", "output": "38894\n" }, { "input": "99995\n", "output": "48886...
code_contests
python
0.7
bace2fad081e0de46be7607b591a96db
You are given string s. Let's call word any largest sequence of consecutive symbols without symbols ',' (comma) and ';' (semicolon). For example, there are four words in string "aba,123;1a;0": "aba", "123", "1a", "0". A word can be empty: for example, the string s=";;" contains three empty words separated by ';'. You ...
def main(): str = input().replace(';', ',') input_list = str.split(',') def pred(x): return x.isdigit() and (x[0] != '0' or len(x) == 1) lA = list(filter(pred, input_list)) lB = list(filter(lambda x: not pred(x), input_list)) print('"{}"'.format(','.join(lA))) if lA else print("-") ...
python
code_algorithm
[ { "input": "1;;01,a0,\n", "output": "\"1\"\n\",01,a0,\"\n" }, { "input": "a\n", "output": "-\n\"a\"\n" }, { "input": "aba,123;1a;0\n", "output": "\"123,0\"\n\"aba,1a\"\n" }, { "input": "1\n", "output": "\"1\"\n-\n" }, { "input": "5345rhhr34t.k;k;k;k;k;5677;000000,...
code_contests
python
0
086ec51b065691b229d9175fd94e3cc9
As we all know Barney's job is "PLEASE" and he has not much to do at work. That's why he started playing "cups and key". In this game there are three identical cups arranged in a line from left to right. Initially key to Barney's heart is under the middle cup. <image> Then at one turn Barney swaps the cup in the midd...
k = int(input()) n = list(map(int, input().split())) for i in range(k): n[i] = bin(n[i]) n[i] = n[i][2:] magic = 1000000007 def par(s): if s[-1] == '0': return True else: return False def mod_pow(x, s, p): ans = 1 for i in range(len(s)): if s[i] == '1': ...
python
code_algorithm
[ { "input": "1\n2\n", "output": "1/2\n" }, { "input": "3\n1 1 1\n", "output": "0/1\n" }, { "input": "1\n983155795040951739\n", "output": "145599903/436799710\n" }, { "input": "12\n254904759290707697 475737283258450340 533306428548398547 442127134828578937 779740159015946254 27...
code_contests
python
0
e54af3e9e0a525a530e41e74268a81c8
In this problem you will write a simple generator of Brainfuck (<https://en.wikipedia.org/wiki/Brainfuck>) calculators. You are given an arithmetic expression consisting of integers from 0 to 255 and addition/subtraction signs between them. Output a Brainfuck program which, when executed, will print the result of eval...
for n in str(eval(input())): for _ in range(ord(n)): print('+',sep='',end='') print('.>')
python
code_algorithm
[ { "input": "2+3\n", "output": "+++++++++++++++++++++++++++++++++++++++++++++++++++++.>\n" }, { "input": "9-7\n", "output": "++++++++++++++++++++++++++++++++++++++++++++++++++.>\n" }, { "input": "255-255+255-255+255-255+255-255+255\n", "output": "++++++++++++++++++++++++++++++++++++++...
code_contests
python
0
e7c4b72dda38d76e4074df7bae8e7599
Array of integers is unimodal, if: * it is strictly increasing in the beginning; * after that it is constant; * after that it is strictly decreasing. The first block (increasing) and the last block (decreasing) may be absent. It is allowed that both of this blocks are absent. For example, the following th...
n=int(input()) arr=list(map(int,input().split())) s,t,p=0,0,0 for i in range(1,n) : if arr[i] < arr[i - 1]: s = 1 elif arr[i]==arr[i-1] and s: t=1 elif arr[i]==arr[i-1] and not s: p=1 if arr[i]>arr[i-1] and (s or p):t=1 if t: print("NO") else : print("YES")
python
code_algorithm
[ { "input": "7\n3 3 3 3 3 3 3\n", "output": "YES\n" }, { "input": "6\n1 5 5 5 4 2\n", "output": "YES\n" }, { "input": "4\n1 2 1 2\n", "output": "NO\n" }, { "input": "5\n10 20 30 20 10\n", "output": "YES\n" }, { "input": "5\n2 2 1 1 1\n", "output": "NO\n" }, ...
code_contests
python
0.5
b9425b003423c1ec158d18b90cc847f1
Due to the recent popularity of the Deep learning new countries are starting to look like Neural Networks. That is, the countries are being built deep with many layers, each layer possibly having many cities. They also have one entry, and one exit point. There are exactly L layers, each having N cities. Let us look at...
from sys import stdin input = stdin.readline def main(): mod = 10**9 + 7 n,l,m = map(int,input().split()) ar = [0 for i in range(m)] ar3 = [0 for i in range(m)] def f(num): return int(num) % m def f2(num): return num % 1000000007 ai1 = list(map(f,input().split())) ...
python
code_algorithm
[ { "input": "2 3 13\n4 6\n2 1\n3 4\n", "output": "2\n" }, { "input": "5 4 3\n2 1 0 1 2\n0 1 2 1 0\n1 2 1 0 2\n", "output": "209\n" }, { "input": "1 1234 5\n1\n1\n1\n", "output": "1\n" }, { "input": "3 2 2\n0 1 0\n0 0 1\n1 1 0\n", "output": "3\n" }, { "input": "4 4 ...
code_contests
python
0
f0fd20d9641a9dbf3880a35b8bb52517
Imp is in a magic forest, where xorangles grow (wut?) <image> A xorangle of order n is such a non-degenerate triangle, that lengths of its sides are integers not exceeding n, and the xor-sum of the lengths is equal to zero. Imp has to count the number of distinct xorangles of order n to get out of the forest. Forma...
import math n = int(input()) ans = 0 for a in range(1, n+1): for b in range(a, n+1): c= a^b if c < b or c > n: continue if a+b > c: ans += 1 print(ans)
python
code_algorithm
[ { "input": "6\n", "output": "1\n" }, { "input": "10\n", "output": "2\n" }, { "input": "1051\n", "output": "145985\n" }, { "input": "2498\n", "output": "699536\n" }, { "input": "846\n", "output": "82106\n" }, { "input": "1508\n", "output": "247634\n...
code_contests
python
0.3
aaab50851ff2af861be866c12dfc5647
Throughout Igor K.'s life he has had many situations worthy of attention. We remember the story with the virus, the story of his mathematical career and of course, his famous programming achievements. However, one does not always adopt new hobbies, one can quit something as well. This time Igor K. got disappointed in ...
import math n,m,a,b=map(int,input().split()) if (a-1)//m==(b-1)//m: print(1) elif (a-1)%m==0 and b%m==0: print(1) elif (a-1)%m==0 and b==n or m==1: print(1) elif (a-1)%m==0 or b%m==0 or b==n: print(2) elif abs((a-1)//m - (b-1)//m)==1 or m==2: print(2) elif (a-1)%m==b%m: print(2) else: print...
python
code_algorithm
[ { "input": "20 5 2 20\n", "output": "2\n" }, { "input": "11 4 3 9\n", "output": "3\n" }, { "input": "28 5 4 26\n", "output": "3\n" }, { "input": "4 3 3 4\n", "output": "2\n" }, { "input": "14 8 2 12\n", "output": "2\n" }, { "input": "7 3 2 6\n", "o...
code_contests
python
0
229e52db7fec0d3c035e798bb882b990
Petya has an array a consisting of n integers. He wants to remove duplicate (equal) elements. Petya wants to leave only the rightmost entry (occurrence) for each element of the array. The relative order of the remaining unique elements should not be changed. Input The first line contains a single integer n (1 ≤ n ≤ ...
n = int(input()) A = list(map(int, input().split())) from collections import Counter C = Counter(A) res = [] for a in A: C[a] -= 1 if C[a] == 0: res.append(a) print(len(res)) print(*res)
python
code_algorithm
[ { "input": "5\n6 6 6 6 6\n", "output": "1\n6\n" }, { "input": "6\n1 5 5 1 6 1\n", "output": "3\n5 6 1\n" }, { "input": "5\n2 4 2 4 4\n", "output": "2\n2 4\n" }, { "input": "28\n997 994 991 994 994 995 1000 992 995 994 994 995 991 996 991 996 991 999 999 993 994 997 995 992 99...
code_contests
python
0.1
bae4dccc2633d4c788ccfbc3727e2fe2
On a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number. Ever...
import bisect n, m = map(int, input().split()) ar1 = [1] + [int(input()) for _ in range(n)] ar1.append(10 ** 9) ar1.sort() ar2 = [list(map(int, input().split())) for _ in range(m)] kek = list() for x in ar2: j1 = bisect.bisect_left(ar1, x[0]) j2 = bisect.bisect_right(ar1, x[1]) if x[0] == 1: kek.app...
python
code_algorithm
[ { "input": "1 3\n4\n1 5 3\n1 9 4\n4 6 6\n", "output": "1\n" }, { "input": "0 0\n", "output": "0\n" }, { "input": "0 2\n1 1000000000 4\n1 1000000000 2\n", "output": "2\n" }, { "input": "2 3\n6\n8\n1 5 6\n1 9 4\n2 4 2\n", "output": "1\n" }, { "input": "2 3\n4\n6\n1 ...
code_contests
python
0
09c25a55a89af8eeabe6d9cd4952c958
Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement is a string of length n consisting of lowercase Latin latters. Vasya thinks that the statement can be considered hard if it contains a subsequence hard; otherwise the statement is easy. For example, hard, hzazrzd, haaaaa...
_ = input() s = input() a = list(map(int, input().split())) hard = "hard" dp = [0]*4 for i in range(len(s)): if s[i] == hard[0]: dp[0] += a[i] for j in range(1, 4): if s[i] == hard[j]: # If same letter, either leave as-is (extend prefix) or remove it dp[j] = min(dp[j-1]...
python
code_algorithm
[ { "input": "6\nhhaarr\n1 2 3 4 5 6\n", "output": "0\n" }, { "input": "6\nhhardh\n3 2 9 11 7 1\n", "output": "5\n" }, { "input": "8\nhhzarwde\n3 2 6 9 4 8 7 1\n", "output": "4\n" }, { "input": "13\nahrhzhdrrhrra\n129663316 931385006 161287509 358388863 419876340 273873401 6284...
code_contests
python
0
9464ffc699ce5184791f5ab06a82c71b
Phoenix is picking berries in his backyard. There are n shrubs, and each shrub has a_i red berries and b_i blue berries. Each basket can contain k berries. But, Phoenix has decided that each basket may only contain berries from the same shrub or berries of the same color (red or blue). In other words, all berries in a...
n,k=map(int,input().split()) totA=0 totB=0 A=[0] B=[0] dp=[] for i in range(n+1): h=[] for j in range(k): h.append(False) dp.append(h) dp[0][0]=True for i in range(n): a,b=map(int,input().split()) A.append(a) B.append(b) totA+=a totB+=b for i in range(1,n+1): for j in...
python
code_algorithm
[ { "input": "1 5\n2 3\n", "output": "1\n" }, { "input": "1 2\n1000000000 1\n", "output": "500000000\n" }, { "input": "2 4\n5 2\n2 1\n", "output": "2\n" }, { "input": "2 5\n2 1\n1 3\n", "output": "0\n" }, { "input": "3 4\n7 0\n0 3\n3 3\n", "output": "4\n" }, ...
code_contests
python
0
83da13d3f4793ab42bdbd08c92a16972
Consider a conveyor belt represented using a grid consisting of n rows and m columns. The cell in the i-th row from the top and the j-th column from the left is labelled (i,j). Every cell, except (n,m), has a direction R (Right) or D (Down) assigned to it. If the cell (i,j) is assigned direction R, any luggage kept o...
for _ in range(int(input())): n,m=map(int,input().split()) a,result=[],0 for i in range(n): a.append(input()) for i in range(n): if i==n-1: for k in range(m): if a[n-1][k]=='D': a[n-1]=a[i][:k]+'R'+a[i][k+1:] result += 1...
python
code_algorithm
[ { "input": "4\n3 3\nRRD\nDDR\nRRC\n1 4\nDDDC\n6 9\nRDDDDDRRR\nRRDDRRDDD\nRRDRDRRDR\nDDDDRDDRR\nDRRDRDDDR\nDDRDRRDDC\n1 1\nC\n", "output": "1\n3\n9\n0\n" }, { "input": "10\n1 1\nC\n1 1\nC\n1 1\nC\n1 1\nC\n1 1\nC\n1 1\nC\n1 1\nC\n1 1\nC\n1 1\nC\n1 1\nC\n", "output": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n...
code_contests
python
0.9
40ed700a6f617730bb1388eade0c010f
Meka-Naruto plays a computer game. His character has the following ability: given an enemy hero, deal a instant damage to him, and then heal that enemy b health points at the end of every second, for exactly c seconds, starting one second after the ability is used. That means that if the ability is used at time t, the ...
# region fastio # from https://codeforces.com/contest/1333/submission/75948789 import sys, io, os BUFSIZE = 8192 class FastIO(io.IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = io.BytesIO() self.writable = "x" in file.mode or "r" not in file.mo...
python
code_algorithm
[ { "input": "7\n1 1 1 1\n2 2 2 2\n1 2 3 4\n4 3 2 1\n228 21 11 3\n239 21 11 3\n1000000 1 1000000 1\n", "output": "1\n2\n1\n5\n534\n-1\n500000500000\n" }, { "input": "4\n568133 729913 934882 371491\n916127 997180 932938 203988\n112133 793452 857041 842130\n572010 190716 396183 683429\n", "output": ...
code_contests
python
0
d6a34e5ac982b4af239fe758d7f501f8